Software source code analysis method, device, electronic device and storage medium
By compiling, analyzing, and selectively flipping the source code of the RISC-V instruction set architecture, we can identify free edges and architecture-related elements, solve the problem of low recognition efficiency in cross-architecture adaptation, and achieve more efficient code adaptation and recognition effects.
Patent Information
- Application Number
- CN202411628136.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-14
- Publication Date
- 2025-09-26
- Estimated Expiration
- 2044-11-14
AI Technical Summary
In the existing technology, the recognition efficiency of cross-architecture software adaptation of the RISC-V instruction set architecture is low, and there is a lack of effective architecture-related code identification and analysis technology. As a result, developers need to spend a lot of time and manually analyze the reasons for build failures, and there are high technical barriers and uncertainties.
By compiling and analyzing the software source code, preprocessing conditions related to the free edges are obtained, and selective flipping is performed until the source code fragments corresponding to all free edges are compiled, and architecture-related elements are identified, including directly and indirectly related program elements, and their correlation level with the instruction set architecture is determined.
It improves the completeness and efficiency of source code analysis, can accurately identify architecture-related code snippets, increases the success rate of cross-architecture adaptation, and reduces the need for manual intervention.
Smart Images

Figure CN119718397B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software development, and in particular to a method, device, electronic device and storage medium for analyzing software source code. Background Art
[0002] As an emerging RISC-V architecture, its open-source nature has garnered widespread attention and adoption among hardware and software vendors both domestically and internationally. RISC-V is not only free and commercially available, but also supports customization by hardware vendors based on specific needs. It holds the technical and market potential to replace existing commercial ISAs. However, as a core element of computing devices, the development of processor ISAs is highly dependent on the support of the software ecosystem. Compared to commercial ISAs like X86 and Advanced RISC Machine (ARM), which have been around longer and boast rich software ecosystems, RISC-V is still in its early stages of development. To rapidly enrich the RISC-V software ecosystem, cross-architecture adaptation of existing ISA software to the RISC-V ISA has become a crucial step. The quality and efficiency of this cross-architecture adaptation also have a profound impact on the RISC-V architecture software ecosystem.
[0003] RISC-V, an emerging processor instruction set architecture, still has a large amount of software that needs to be adapted across architectures, and there is no direct technology to identify architecture-related code in software. Currently, there are a variety of identification and analysis technologies for software source code, but these only lay the foundation for specific identification and analysis tasks. There is still a lack of architecture-related code identification and analysis technology for cross-architecture adaptation. Faced with the massive amount of software that needs to be adapted for the RISC-V architecture, even if developers use software automatic build systems to automate the compilation and build process, they still need to invest a lot of time and effort to manually analyze the specific reasons for build failures, locate and modify the problematic code, and then repeat the compilation and build process. The efficiency and quality of this process depends on the developer's experience and knowledge, and still has a high technical threshold and uncertainty.
[0004] In summary, the existing source code instruction set architecture related program elements recognition efficiency is low. Summary of the Invention
[0005] The present invention provides a software source code analysis method, device, electronic device and storage medium to solve the defect of low recognition efficiency of source code instruction set architecture related program elements in the prior art, thereby improving the recognition efficiency of source code instruction set architecture related program elements.
[0006] The present invention provides a software source code analysis method, comprising: compiling and analyzing the software source code to obtain a preprocessing process flow chart of the source code; obtaining free edges in the preprocessing process flow chart, wherein the free edges correspond to code segments in the source code that are not involved in the compilation; selectively flipping preprocessing conditions related to the free edges until all source code segments corresponding to the free edges have been compiled, thereby obtaining at least one compilation result of the source code; identifying architecture-related elements of the source code based on the at least one compilation result, wherein the architecture-related elements include program elements directly related to the architecture and program elements indirectly related to the architecture; and determining the level of correlation between the architecture-related elements and the instruction set architecture based on the architecture-related code segments of the architecture-related elements.
[0007] According to the software source code analysis method provided by the present invention, the preprocessing conditions related to the free edges include control preprocessing conditions that directly control the free edges and associated preprocessing conditions that indirectly affect the free edges. The preprocessing conditions related to the free edges are selectively flipped until all source code segments corresponding to the free edges have been compiled, and at least one compilation result of the source code is obtained. The method includes: when the control preprocessing condition of the free edge is flipped for the first time, flipping the control preprocessing condition to ensure that the source code segment corresponding to the free edge is compiled, and obtaining a compilation result; when the control preprocessing condition of the free edge is not flipped for the first time, obtaining an associated preprocessing condition of the control preprocessing condition, locking the associated preprocessing condition and not flipping it, flipping the control preprocessing condition to ensure that the source code segment corresponding to the free edge is compiled, and obtaining a compilation result, wherein the associated preprocessing condition is a preprocessing condition having a dependency relationship with the control preprocessing condition; and when all source code segments corresponding to the free edges have been compiled, stopping flipping the preprocessing conditions related to the free edges.
[0008] According to the software source code analysis method provided by the present invention, the control preprocessing condition is flipped, including: when the control preprocessing condition is to check whether the macro is defined, the flipped preprocessing condition is to check whether the macro is undefined; when the control preprocessing condition is to check whether the macro is undefined, the flipped preprocessing condition is to check whether the macro is defined.
[0009] The software source code analysis method provided by the present invention identifies architecture-related elements of the source code based on at least one compilation result, including: identifying multiple types of program elements in the source code based on at least one compilation result; traversing the program elements and identifying program elements directly related to the architecture among the program elements based on a pre-set list of architecture-related program elements and pre-set architecture-related keywords; traversing other unidentified program elements among the program elements and identifying program elements indirectly related to the architecture among the other unidentified program elements, where the other unidentified program elements are program elements other than program elements directly related to the architecture.
[0010] According to the software source code analysis method provided by the present invention, other unidentified program elements in the program elements are traversed to identify the architecture-indirectly related program elements in the other unidentified program elements, including: when the other unidentified program elements are controlled by architecture-related macros, determining that the other unidentified program elements are architecture-indirectly related program elements; when the other unidentified program elements are variables, and there are architecture-related variables in the data flow of the variables, determining that the other unidentified program elements are architecture-indirectly related program elements; when the other unidentified program elements are functions, and the functions directly or indirectly call architecture-related functions, determining that the other unidentified program elements are architecture-indirectly related program elements; when the other unidentified program elements are data types, and the names of the data types and their members include architecture-related keywords, determining that the other unidentified program elements are architecture-indirectly related program elements.
[0011] According to the software source code analysis method provided by the present invention, the list of related program elements includes multiple preset related program elements. Based on the preset list of related program elements related to the architecture and the preset architecture-related keywords, program elements directly related to the architecture among the program elements are identified, including: if the program element is a preset related program element, determining that the program element is a program element directly related to the architecture; if the program element includes the architecture-related keywords, determining that the program element is a program element directly related to the architecture.
[0012] According to the software source code analysis method provided by the present invention, the correlation level between the architecture-related elements and the instruction set architecture is determined based on the architecture-related code snippets of the architecture-related elements, including: obtaining property supplementary data of the architecture-related code snippets, the property supplementary data including the instruction set architecture to which the architecture-related code snippet belongs, the control of the architecture-related code snippet by the instruction set architecture, and the frequency with which the architecture-related code snippet is referenced in the source code; based on the architecture-related code snippets and the property supplementary data, obtaining the correlation level between the architecture-related elements and the instruction set architecture.
[0013] The present invention also provides a software source code analysis device, comprising: an initial compilation module, used to compile and analyze the software source code to obtain a preprocessing process flow chart of the source code; an acquisition module, used to obtain free edges in the preprocessing process flow chart, the free edges corresponding to uncompiled code fragments in the source code; a flipping and compilation module, used to selectively flip the preprocessing conditions related to the free edges until all source code fragments corresponding to the free edges have been compiled, thereby obtaining at least one compilation result of the source code; an identification module, used to identify architecture-related elements of the source code based on at least one compilation result, the architecture-related elements including program elements directly related to the architecture and program elements indirectly related to the architecture; and a determination module, used to determine the level of correlation between the architecture-related elements and the instruction set architecture based on the architecture-related code fragments of the architecture-related elements.
[0014] The present invention also provides an electronic device comprising a memory, a processor and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, a source code analysis method for any of the above software is implemented.
[0015] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the method for analyzing source code of any of the above-mentioned software is implemented.
[0016] The software source code analysis method, device, electronic device and storage medium provided by the present invention achieve the compilation of source code fragments corresponding to free edges by selectively flipping the preprocessing conditions related to free edges, thereby improving the integrity of source code analysis and facilitating the improvement of the efficiency of identifying architecture-related elements of the source code. Through program elements directly related to the architecture and program elements indirectly related to the architecture, complete identification of architecture-related elements is achieved, and more architecture-related code fragments can be identified. By determining the level of correlation between architecture-related elements and instruction set architecture, the present invention achieves accurate identification of architecture-related code fragments that may cause problems when the source code is adapted to the new instruction set architecture, which is conducive to improving the success rate of source code adaptation to the new instruction set architecture. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions in the present invention or the prior art, a brief introduction is given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0018] Figure 1 This is one of the flow charts of the software source code analysis method provided by the present invention.
[0019] Figure 2 This is one of the source code compilation flow diagrams provided by the present invention.
[0020] Figure 3 This is the second schematic diagram of the compilation process of the source code provided by the present invention.
[0021] Figure 4 This is the third schematic diagram of the compilation process of the source code provided by the present invention.
[0022] Figure 5 This is the fourth schematic diagram of the compilation process of the source code provided by the present invention.
[0023] Figure 6 It is a flowchart of program elements directly related to the identification architecture provided by the present invention.
[0024] Figure 7 It is a flowchart of program elements indirectly related to the identification architecture provided by the present invention.
[0025] Figure 8 This is the second flow chart of the software source code analysis method provided by the present invention.
[0026] Figure 9 It is a structural diagram of the software source code analysis device provided by the present invention.
[0027] Figure 10 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0028] To make the objectives, technical solutions, and advantages of the present invention more clear, the technical solutions of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the embodiments described are only some of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts shall fall within the scope of protection of the present invention.
[0029] The following combination Figures 1-10 The present invention describes a method, device and electronic device for analyzing software source code.
[0030] Figure 1 This is one of the flow charts of the software source code analysis method provided by the present invention, such as Figure 1 As shown, the software source code analysis method includes S100 to S500, and each step is specifically as follows.
[0031] S100: Compile and analyze the source code of the software to obtain a flow chart of the preprocessing process of the source code.
[0032] The source code is written in C or C++. Under the premise that environmental factors such as the hardware platform, operating system, and compiler version remain unchanged, the source code is compiled and analyzed based on the given source code and compilation parameters to obtain a flow chart of the source code preprocessing process.
[0033] S200: Obtaining free edges of a preprocessing process flow chart, where the free edges correspond to code segments in the source code that are not involved in compilation.
[0034] Generally speaking, if specific preprocessing conditions are used in source code written in C / C++, some source code that does not meet the preprocessing condition logic will be skipped during the compiler preprocessing process, making this part of the code invisible in the subsequent static analysis process. This invisible source code forms a free edge. For example, Figure 2 As shown, the source code is:
[0035] Line 1: #define M1 (define macro M1)
[0036] Line 2:
[0037] Line 3: #ifdef M1 (checks if macro M1 is defined)
[0038] Line 4: #define M2 (if macro M1 is defined, define macro M2)
[0039] Line 5: #else
[0040] Line 6: #ifdef M3 (If macro M1 is not defined, check whether macro M3 is defined)
[0041] Line 7: #define M4 (if macro M3 is defined, define macro M4)
[0042] Line 8: #else
[0043] Line 9: #define M5 (if macro M3 is not defined, define macro M5)
[0044] Line 10: #endif (end)
[0045] Line 11: #ifndef M6 (checks if macro M6 is undefined)
[0046] Line 12: #define M7 (if macro M6 is not defined, define macro M7)
[0047] Line 13: #else
[0048] Line 14: #define M8 (If macro M6 is defined, define macro M8)
[0049] Line 15: #endif (end)
[0050] Line 16: #endif (end)
[0051] Line 17:
[0052] Line 18: #ifdef M7 (checks if macro M7 is defined)
[0053] Line 19: #define M9 (if macro M7 is defined, define macro M9)
[0054] Line 20: #else
[0055] Line 21: #define M10 (If macro M7 is not defined, define macro M10)
[0056] Line 22: #endif (end)
[0057] like Figure 2 As shown in the figure, the software source code is compiled for the first time, and the preprocessing process flow chart after the first compilation is obtained. The code fragments corresponding to the free edges are skipped during compilation. Line 3 (if macro M1 is defined) is preprocessing condition 1, and the false branches corresponding to preprocessing condition 1 (lines 6 to 15) are uncompiled code fragments. Because macro M1 is defined, lines 6 to 15 are skipped. Figure 2 The free edge F1 is formed in the .18th line (if macro M7 is defined) is the preprocessing condition 2, and the true branch corresponding to the preprocessing condition 2 is the uncompiled code fragment (line 19). Because macro M7 is not defined, line 19 is in Figure 2 A free edge F2 is formed in the process.
[0058] S300: selectively flipping preprocessing conditions related to free edges until all source code segments corresponding to the free edges have been compiled, thereby obtaining at least one compilation result of the source code.
[0059] like Figure 8 As shown, in order to enable the code fragments that do not participate in the compilation corresponding to the skipped free edges to be analyzed, the preprocessing conditions related to the free edges are selectively flipped in sequence to change the branch direction of the preprocessing conditions related to the free edges, and an updated preprocessing process flow chart is obtained, until all the code fragments that do not participate in the compilation corresponding to the free edges in the updated preprocessing process flow chart can be compiled.
[0060] After the source code is compiled for the first time, the preprocessing process flowchart (original analysis results) and the first compilation results are obtained. If the preprocessing process flowchart after the first compilation contains free edges, it indicates that there are uncompiled code fragments in the source code. The preprocessing conditions related to the free edges are selectively flipped. The source code after the first flip is compiled again to obtain the preprocessing process flowchart and the second compilation results after the second compilation. Check whether there are free edges in the preprocessing process flowchart after the second compilation. If there are free edges, continue the above selective flipping and compilation process until there are no free edges in the preprocessing process flowchart after compilation. Obtain at least one compilation result of the above process (including the first compilation result, the second compilation result, etc.).
[0061] The compilers used in this invention include the GNU C / C++ Compiler (GCC), the LLVM C language family front-end (LLVM Clang) compiler, and their variants. The present invention uses the LLVM Clang compiler as an example to illustrate the following: (1) writing an LLVM Clang compiler plug-in (preprocessing analysis plug-in) to implement the required source code recognition and analysis functional logic; (2) obtaining the source code of the software to be analyzed, ensuring that it is fully expanded and the required patch files have been applied; (3) extracting and summarizing the compilation parameters of each module and source code of the software to be analyzed; (4) adding the relevant parameters required by the plug-in to the final compilation parameters; and (5) performing a simulated compilation of the source code according to the normal process, without generating a target code file. It is sufficient to ensure that the functional logic related to the plug-in can be triggered.
[0062] S400: Based on at least one compilation result, identify architecture-related elements of the source code, where the architecture-related elements include program elements directly related to the architecture and program elements indirectly related to the architecture.
[0063] Based on the results of at least one compilation of the source code, perform program element analysis to identify all program elements in the source code. Obtain and parse the plugin output to identify architecture-specific program elements from all program elements. Architectures include, but are not limited to, X86, ARM, and RISC-V.
[0064] A first pass is performed on all program elements of the source code to identify program elements that are directly related to the architecture among the program elements.
[0065] The remaining unidentified program elements are traversed again to identify the program elements indirectly related to the architecture among the remaining unidentified program elements.
[0066] Architecture-related elements include macros, variables, functions, data types, etc.
[0067] S500: Determine a correlation level between the architecture-related element and the instruction set architecture based on the architecture-related code snippet of the architecture-related element.
[0068] Based on the architecture-related code snippets of the architecture-related elements, determine the level of correlation between the architecture-related elements and the instruction set architecture. Specifically, obtain property supplementary data of the architecture-related code snippet, the property supplementary data including the instruction set architecture to which the architecture-related code snippet belongs, the control of the instruction set architecture over the architecture-related code snippet, and the frequency with which the architecture-related code snippet is referenced in the source code; based on the architecture-related code snippets and the property supplementary data, obtain the level of correlation between the architecture-related elements and the instruction set architecture.
[0069] The specific scope of an architecture-related code snippet typically depends on the type and scope of the architecture-related element. If the architecture-related element is a variable, the corresponding architecture-related code snippet typically includes statements for defining, assigning a value to, and using the variable. If the architecture-related element is a function, the corresponding architecture-related code snippet typically includes statements for declaring, defining, and using the function. If the architecture-related element is a macro, the corresponding architecture-related code snippet typically includes statements for defining, redefining, and using the macro. If the architecture-related element is a data type, the corresponding architecture-related code snippet typically includes statements for defining and using the data type.
[0070] After obtaining architecture-related code snippets for architecture-related elements, we can further obtain supplementary data on the properties of these snippets to enhance the richness of the recognition results and facilitate subsequent reference and expansion. Table 1 lists some supplementary data on the properties of these snippets. This data can be added to the recognition results of architecture-related code snippets based on the capabilities of the analysis tools for each programming language.
[0071] Table 1. Supplementary data on the properties of some architecture-related code snippets
[0072]
[0073] Based on the architecture-related code snippets and the supplemental data, determine the degree of correlation between the architecture-related elements and the instruction set architecture, and determine the relevance level of this correlation. Based on this relevance level, identify architecture-related code snippets in the software source code that may cause problems when adapting to the new instruction set architecture, and make targeted modifications to these architecture-related code snippets that may cause problems.
[0074] The software source code analysis method provided by the present invention achieves the compilation of source code fragments corresponding to free edges by selectively flipping the preprocessing conditions related to free edges, thereby improving the integrity of source code analysis and facilitating the improvement of the efficiency of identifying architecture-related elements of the source code. Through program elements directly related to the architecture and program elements indirectly related to the architecture, complete identification of architecture-related elements is achieved, and more architecture-related code fragments can be identified. By determining the level of correlation between architecture-related elements and instruction set architecture, the present invention achieves accurate identification of architecture-related code fragments that may cause problems when the source code is adapted to a new instruction set architecture, thereby improving the success rate of source code adaptation to a new instruction set architecture.
[0075] Based on the above embodiment, the preprocessing conditions related to the free edges include control preprocessing conditions that directly control the free edges and association preprocessing conditions that indirectly affect the free edges. The preprocessing conditions related to the free edges are selectively flipped until all source code fragments corresponding to the free edges have been compiled, and at least one compilation result of the source code is obtained, including S310 to S330. The details of each step are as follows.
[0076] S310: When the control preprocessing condition of the free edge is flipped for the first time, the control preprocessing condition is flipped to ensure that the source code segment corresponding to the free edge is compiled, thereby obtaining a compilation result.
[0077] S320: When the control preprocessing condition of the free edge is not flipped for the first time, obtain the associated preprocessing condition of the control preprocessing condition, lock the associated preprocessing condition and do not flip it, flip the control preprocessing condition to ensure that the source code fragment corresponding to the free edge is compiled, and obtain a compilation result. The associated preprocessing condition is a preprocessing condition that has a dependency relationship with the control preprocessing condition.
[0078] S330: When all source code segments corresponding to the free edges have been compiled, the flipping of the preprocessing conditions related to the free edges is stopped.
[0079] The control preprocessing condition is flipped. Specifically, when the control preprocessing condition is to check whether the macro is defined, the flipped preprocessing condition is to check whether the macro is undefined; when the control preprocessing condition is to check whether the macro is undefined, the flipped preprocessing condition is to check whether the macro is defined.
[0080] The selective flipping can be performed in a top-down or bottom-up order. The present invention takes the top-down order as an example for description.
[0081] Compile the source code for the first time and obtain a preprocessing process flow chart after the first compilation and the first compilation result.
[0082] The control preprocessing condition is the preprocessing condition that directly controls the free edge. For example, the control preprocessing condition for free edge F1 is preprocessing condition 1 (if macro M1 is defined), and the control preprocessing condition for free edge F2 is preprocessing condition 2 (if macro M7 is defined).
[0083] In the first selective flipping process, the control preprocessing conditions of the branch where the free edge F1 is located and the control preprocessing conditions of the branch where the free edge F2 is located are both flipped for the first time. Flip the preprocessing condition 1 (the third line of code), and flip "check whether the macro M1 is defined (ifdef M1)" to "check whether the macro M1 is defined (ifndef M1)". Flip the preprocessing condition 2 (the 18th line of code), and flip "check whether the macro M7 is defined (ifdef M7)" to "check whether the macro M7 is not defined (ifndef M7)", and get the source code after the first flip. Figure 3 As shown, the source code after the first flip is compiled for the second time, and a preprocessing process flow chart after the second compilation and the second compilation result are obtained.
[0084] Depend on Figure 3 As can be seen in the preprocessing flow chart after the second compilation, free edge F1 disappears. The free edge F2' corresponding to free edge F2 remains a free edge. This is because line 12 of the source code after the first flip restricts "define macro M7," which cancels out the effect of flipping preprocessing condition 2.
[0085] The branch where the 7th line of code is located is skipped under the control of the 6th line of code (control preprocessing conditions) and the 3rd line of code (association preprocessing conditions), forming an uncompiled code fragment and a free edge F3.
[0086] The branch where the 14th line of code is located is skipped under the control of the 11th line of code (control preprocessing conditions) and the 3rd line of code (association preprocessing conditions), forming a free edge F4.
[0087] In the second selective flipping process, since the edge F2' obtained after the control preprocessing condition of the free edge F2 is flipped is still a free edge, the control preprocessing condition of the free edge F2' is no longer flipped for the first time, which means that the control preprocessing condition of the free edge F2' (preprocessing condition 2) has a dependency relationship with other branches. Figure 3 In the corresponding preprocessing process flow chart after the second compilation, the 12th line of code (defining macro M7) changes the definition status of macro M7 in the control preprocessing condition of edge F2', making the two form a dependent relationship. In order to safely flip the control preprocessing condition corresponding to the free edge F2' (line 18 of code), other preprocessing conditions related to the definition status of macro M7 (associated preprocessing conditions) must be locked (i.e., not flipped). Figure 3As shown, the associated preprocessing conditions of the control preprocessing condition (18th line of code) corresponding to the free edge F2' are the 11th line of code and the 3rd line of code (the 6th line of code is at the same level as the 11th line of code, and both are preprocessing conditions, but the 6th line of code has no effect on the definition status of macro M7. Therefore, the 6th line of code is not the control preprocessing condition corresponding to the free edge F2'). Therefore, it is only necessary to lock the 11th line of code and the 3rd line of code, and flip the 18th line of code, flipping "check whether macro M7 is undefined (ifndefM7)" again to "check whether macro M7 is defined (ifdef M7) to ensure that the free edge F2' is compiled. The control preprocessing condition (line 6) of the branch where the free edge F3 is located is flipped for the first time. The control preprocessing condition (line 6) of the branch where the free edge F3 is located is flipped, and the "check whether the macro M3 is defined (ifdefM3)" is flipped to "check whether the macro M3 is undefined (ifndefM3)" to ensure that the source code fragment corresponding to the free edge F3 is compiled. Figure 4 As shown, the source code after the second flip is compiled for the third time, and the preprocessing process flow chart and the third compilation result after the third compilation are obtained. Figure 4 As can be seen in the preprocessing process flow chart after the third compilation, since the 11th and 3rd lines of code are locked, after the second selective flip, the free edge F4 remains unchanged and remains the free edge F4'. The free edges F2' and F3 disappear.
[0088] During the third selective flipping process, the control processing condition (line 11) of the branch where the free edge F4' is located is flipped for the first time. Directly flip the control processing condition (line 11) of the branch where the free edge F4' is located, and change "check whether macro M6 is undefined (ifndef M6)" to "check whether macro M6 is defined (ifdef M6)". After the third flipping, the free edge F4' disappears. Figure 5 As shown, the source code after the third flip is compiled for the fourth time, and a preprocessing process flow chart after the fourth compilation and the fourth compilation result are obtained.
[0089] Combining the above process, we get the four compilation results of the source code.
[0090] Optionally, the control preprocessing condition is flipped, including: when the control preprocessing condition is to check whether the previous condition is satisfied (elifdef), the flipped preprocessing condition is to check whether the previous condition is not satisfied (elifndef), or when the control preprocessing condition is to check whether the previous condition is not satisfied, the flipped preprocessing condition is to check whether the previous condition is satisfied.
[0091] The present invention achieves compilation of the source code fragment corresponding to a free edge by flipping the control preprocessing conditions of the free edge. For control preprocessing conditions that are not flipped for the first time, the success rate of free edge compilation is improved by locking the associated preprocessing conditions before flipping. By repeatedly flipping the control preprocessing conditions, the present invention improves the integrity of source code compilation.
[0092] Based on the above embodiment, identifying architecture-related elements of source code based on at least one compilation result includes S410 to S430 , and each step is specifically as follows.
[0093] S410: Identify multiple types of program elements in the source code based on at least one compilation result.
[0094] S420: Traverse the program elements and identify program elements directly related to the architecture among the program elements based on a preset list of related program elements related to the architecture and preset architecture-related keywords.
[0095] S430: Traverse other unidentified program elements in the program elements, and identify program elements indirectly related to the architecture among the other unidentified program elements. The other unidentified program elements are program elements other than program elements directly related to the architecture.
[0096] Based on the at least one compilation result, various program elements in the source code are identified and traversed, and relevant information corresponding to each program element is extracted using an analysis tool. Based on the relevant information of each program element, it is determined whether the program element is an architecture-related element.
[0097] The list of relevant program elements includes multiple preset relevant program elements. Based on the preset list of relevant program elements related to the architecture and the preset architecture-related keywords, program elements directly related to the architecture in the program elements are identified, including: if the program element is a preset relevant program element, then the program element is determined to be a program element directly related to the architecture; if the program element includes the architecture-related keywords, then the program element is determined to be a program element directly related to the architecture.
[0098] A list of relevant program elements is pre-set, and the list of relevant program elements includes multiple preset relevant program elements. The preset relevant program elements include preset architecture-specific functions, preset macros, and preset data types. For example, the list of relevant program elements such as architecture-specific functions and data types can be pre-extracted from a compiler or library file, or supplemented through post-processing operations.
[0099] like Figure 6As shown, each program element is traversed, and if the program element is a preset-related program element (including preset architecture-related functions, preset macros, and preset data types), the program element is directly marked as a program element directly related to the architecture.
[0100] like Figure 6 As shown, each program element is traversed. If the name of the current program element itself or the name of its members (such as attributes, fields, enumeration names, etc.) contains predefined architecture-related keywords, the program element is marked as a program element directly related to the architecture.
[0101] The present invention realizes accurate identification of program elements directly related to the architecture based on the relevant program element list and architecture-related keywords.
[0102] Traversing other unidentified program elements in the program elements, and identifying the architecture-indirectly related program elements in the other unidentified program elements, including: when the other unidentified program elements are controlled by architecture-related macros, determining that the other unidentified program elements are architecture-indirectly related program elements; when the other unidentified program elements are variables, and there are architecture-related variables in the data flow of the variables, determining that the other unidentified program elements are architecture-indirectly related program elements; when the other unidentified program elements are functions, and the functions directly or indirectly call architecture-related functions, determining that the other unidentified program elements are architecture-indirectly related program elements; when the other unidentified program elements are data types, and the names of the data types and their members include architecture-related keywords, determining that the other unidentified program elements are architecture-indirectly related program elements.
[0103] like Figure 7 As shown, after the first round of screening of program elements, program elements directly related to the architecture are obtained, and other unidentified program elements are traversed again to identify program elements indirectly related to the architecture among the other unidentified program elements.
[0104] If the current other unidentified program elements are controlled by the architecture-related macro, it is determined that the current other unidentified program elements are architecture-indirectly related program elements.
[0105] If the currently unidentified program element is a variable, data flow analysis is performed on the variable. If the analysis shows that there is an architecture-related variable in the data flow of the variable, the variable is marked as an architecture-indirect program element.
[0106] If the currently unidentified program element is a function, function pointer analysis is performed on the function itself and all function calls within it. If the analysis results indicate that the function directly or indirectly calls an architecture-related function, the function is marked as an architecture-indirectly related program element.
[0107] If the currently unrecognized program element is a data type, alias analysis is performed on the data type and its members (if any). If the alias of the data type itself (or any of its members) contains a schema-related keyword, the data type is marked as an indirectly schema-related program element.
[0108] The present invention performs different analyses on other unidentified program elements of different categories, identifies program elements indirectly related to the architecture, and realizes comprehensive identification of program elements indirectly related to the architecture.
[0109] Furthermore, if the other unidentified program elements are other types of program elements, the process of this stage is expanded according to the needs, and necessary analysis is performed on the corresponding types of program elements. When the set architectural indirect relevance judgment conditions are met, the corresponding other unidentified program elements are marked as architecturally indirectly related program elements.
[0110] The present invention accurately identifies program elements directly related to the architecture based on a list of relevant program elements and architecture-related keywords. It also conducts different analyses on other unidentified program elements of different categories to identify program elements indirectly related to the architecture, achieving comprehensive identification of program elements indirectly related to the architecture.
[0111] The software source code analysis device provided by the present invention is described below. The software source code analysis device described below and the software source code analysis method described above can be referenced to each other.
[0112] like Figure 9 As shown, a software source code analysis device includes: an initial compilation module 901, which is used to compile and analyze the software source code to obtain a flow chart of the source code preprocessing process.
[0113] The acquisition module 902 is used to acquire free edges of the preprocessing process flow chart, where the free edges correspond to code segments in the source code that are not involved in compilation.
[0114] The flipping and compiling module 903 is used to selectively flip the pre-processing conditions related to the free edges until all source code segments corresponding to the free edges have been compiled, thereby obtaining at least one compilation result of the source code.
[0115] The identification module 904 is configured to identify architecture-related elements of the source code based on at least one compilation result, where the architecture-related elements include program elements that are directly related to the architecture and program elements that are indirectly related to the architecture.
[0116] The determination module 905 is configured to determine a level of relevance between the architecture-related element and the instruction set architecture based on the architecture-related code snippet of the architecture-related element.
[0117] The software source code analysis device provided by the present invention achieves the compilation of source code fragments corresponding to free edges by selectively flipping the preprocessing conditions related to free edges, thereby improving the integrity of source code analysis and facilitating the improvement of the efficiency of identifying architecture-related elements of the source code. Through program elements directly related to the architecture and program elements indirectly related to the architecture, complete identification of architecture-related elements is achieved, and more architecture-related code fragments can be identified. By determining the level of correlation between architecture-related elements and instruction set architecture, the present invention achieves accurate identification of architecture-related code fragments that may cause problems when the source code is adapted to a new instruction set architecture, which is conducive to improving the success rate of source code adaptation to a new instruction set architecture.
[0118] In one embodiment, the preprocessing conditions associated with the free edge include control preprocessing conditions that directly control the free edge and associated preprocessing conditions that indirectly affect the free edge. The flipping and compilation module 903 is used to: when the control preprocessing condition of the free edge is flipped for the first time, flip the control preprocessing condition to ensure that the source code segment corresponding to the free edge is compiled, thereby obtaining a compilation result; when the control preprocessing condition of the free edge is not flipped for the first time, obtain the associated preprocessing condition of the control preprocessing condition, lock the associated preprocessing condition and do not flip it, flip the control preprocessing condition to ensure that the source code segment corresponding to the free edge is compiled, thereby obtaining a compilation result, wherein the associated preprocessing condition is a preprocessing condition that has a dependency relationship with the control preprocessing condition; and when the source code segments corresponding to all free edges have been compiled, stop flipping the preprocessing conditions associated with the free edge.
[0119] In one embodiment, the flip and compile module 903 is used to: when the control preprocessing condition is to check whether the macro is defined, the preprocessing condition after flipping is to check whether the macro is undefined; when the control preprocessing condition is to check whether the macro is undefined, the preprocessing condition after flipping is to check whether the macro is defined.
[0120] In one embodiment, the identification module 904 is used to: identify multiple types of program elements in the source code based on at least one compilation result; traverse the program elements and identify program elements directly related to the architecture among the program elements based on a preset list of related program elements related to the architecture and preset architecture-related keywords; traverse other unidentified program elements among the program elements and identify program elements indirectly related to the architecture among the other unidentified program elements, where the other unidentified program elements are program elements other than program elements directly related to the architecture.
[0121] In one embodiment, the identification module 904 is used to: when other unidentified program elements are controlled by architecture-related macros, determine that the other unidentified program elements are architecture-indirectly related program elements; when other unidentified program elements are variables and there are architecture-related variables in the data flow of the variables, determine that the other unidentified program elements are architecture-indirectly related program elements; when other unidentified program elements are functions and the functions directly or indirectly call architecture-related functions, determine that the other unidentified program elements are architecture-indirectly related program elements; when other unidentified program elements are data types and the names of the data types and their members include architecture-related keywords, determine that the other unidentified program elements are architecture-indirectly related program elements.
[0122] In one embodiment, the list of related program elements includes multiple preset related program elements, and the identification module 904 is used to: if the program element is a preset related program element, determine that the program element is a program element directly related to the architecture; if the program element includes an architecture-related keyword, determine that the program element is a program element directly related to the architecture.
[0123] In one embodiment, the determination module 905 is used to: obtain property supplementary data of the architecture-related code snippet, the property supplementary data including the instruction set architecture to which the architecture-related code snippet belongs, the control of the instruction set architecture over the architecture-related code snippet, and the frequency with which the architecture-related code snippet is referenced in the source code; based on the architecture-related code snippet and the property supplementary data, obtain the level of correlation between the architecture-related elements and the instruction set architecture.
[0124] Figure 10 An example of a physical structure diagram of an electronic device is shown below. Figure 10As shown, the electronic device may include: a processor 1010, a communications interface 1020, a memory 1030, and a communications bus 1040, wherein the processor 1010, the communications interface 1020, and the memory 1030 communicate with each other via the communications bus 1040. The processor 1010 may call logic instructions in the memory 1030 to execute a software source code analysis method, the method comprising: compiling and analyzing the software source code to obtain a preprocessing flow chart of the source code; obtaining free edges in the preprocessing flow chart, wherein the free edges correspond to code segments in the source code that are not involved in the compilation; selectively flipping preprocessing conditions associated with the free edges until all source code segments corresponding to the free edges have been compiled, thereby obtaining at least one compilation result of the source code; identifying architecture-related elements of the source code based on the at least one compilation result, wherein the architecture-related elements include program elements directly related to the architecture and program elements indirectly related to the architecture; and determining the level of relevance of the architecture-related elements to the instruction set architecture based on the architecture-related code segments of the architecture-related elements.
[0125] Furthermore, the logic instructions in the aforementioned memory 1030 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the portion that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, a mobile hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.
[0126] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements a source code analysis method for executing the software provided by the above methods, the method comprising: compiling and analyzing the source code of the software to obtain a preprocessing process flow chart of the source code; obtaining free edges of the preprocessing process flow chart, the free edges corresponding to code fragments in the source code that are not involved in the compilation; selectively flipping the preprocessing conditions related to the free edges until all source code fragments corresponding to the free edges have been compiled, obtaining at least one compilation result of the source code; based on the at least one compilation result, identifying architecture-related elements of the source code, the architecture-related elements including program elements directly related to the architecture and program elements indirectly related to the architecture; and determining the level of correlation between the architecture-related elements and the instruction set architecture based on the architecture-related code fragments of the architecture-related elements.
[0127] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, i.e., they may be located in one location or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of the present embodiment. Persons of ordinary skill in the art will be able to understand and implement the present invention without inventive effort.
[0128] Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented using software plus a necessary general-purpose hardware platform, or of course, hardware. Based on this understanding, the essence of the above technical solution, or the portion that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a magnetic disk, or an optical disk, and includes a number of instructions for causing a computer device (such as a personal computer, server, or network device) to execute the methods described in each embodiment or certain portions of the embodiments.
[0129] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the various embodiments of the present invention.
Claims
1. A method for analyzing software source code, characterized in that: include: Compile and analyze the source code of the software to obtain a flow chart of the preprocessing process of the source code; Obtaining free edges of the preprocessing process flow chart, wherein the free edges correspond to code segments in the source code that are not involved in compilation; Selectively flipping preprocessing conditions related to free edges until all source code fragments corresponding to the free edges have been compiled, thereby obtaining at least one compilation result of the source code; Based on the at least one compilation result, identifying architecture-related elements of the source code, wherein the architecture-related elements include program elements directly related to the architecture and program elements indirectly related to the architecture; Based on the architecture-dependent code snippets of the architecture-dependent element, a dependency level of the architecture-dependent element to an instruction set architecture is determined.
2. The software source code analysis method according to claim 1, characterized in that: The preprocessing conditions related to the free edges include control preprocessing conditions that directly control the free edges and association preprocessing conditions that indirectly affect the free edges. The preprocessing conditions related to the free edges are selectively flipped until all source code fragments corresponding to the free edges have been compiled, thereby obtaining at least one compilation result of the source code, including: When the control preprocessing condition of the free edge is flipped for the first time, flipping the control preprocessing condition to ensure that the source code segment corresponding to the free edge is compiled, thereby obtaining the compilation result; When the control preprocessing condition of the free edge is not flipped for the first time, obtaining an associated preprocessing condition of the control preprocessing condition, locking the associated preprocessing condition and not flipping it, flipping the control preprocessing condition to ensure that the source code fragment corresponding to the free edge is compiled, and obtaining a compilation result, wherein the associated preprocessing condition is a preprocessing condition having a dependency relationship with the control preprocessing condition; When the source code segments corresponding to all the free edges have been compiled, the flipping of the preprocessing conditions related to the free edges is stopped.
3. The software source code analysis method according to claim 2, characterized in that: The reversing of the control preprocessing condition includes: When the control preprocessing condition is to check whether the macro is defined, the flipped preprocessing condition is to check whether the macro is undefined; When the control preprocessing condition is whether the check macro is undefined, the flipped preprocessing condition is whether the check macro is defined.
4. The software source code analysis method according to claim 1, characterized in that: The identifying architecture-related elements of the source code based on the at least one compilation result includes: Based on the at least one compilation result, identifying multiple types of program elements of the source code; Traversing the program elements, and identifying program elements directly related to the architecture among the program elements based on a predetermined list of related program elements related to the architecture and predetermined architecture-related keywords; Other unidentified program elements in the program elements are traversed to identify program elements indirectly related to the architecture among the other unidentified program elements, where the other unidentified program elements are program elements other than the program elements directly related to the architecture.
5. The software source code analysis method according to claim 4, characterized in that: The traversing other unidentified program elements in the program elements and identifying program elements indirectly related to the architecture in the other unidentified program elements includes: When the other unidentified program elements are controlled by architecture-dependent macros, determining that the other unidentified program elements are architecture-indirectly dependent program elements; When the other unidentified program element is a variable and an architecture-related variable exists in the data flow of the variable, determining that the other unidentified program element is a program element indirectly related to the architecture; When the other unidentified program element is a function, and the function directly or indirectly calls an architecture-related function, determining that the other unidentified program element is the architecture-indirectly related program element; When the other unrecognized program elements are data types, and the names of the data types and their members include architecture-related keywords, the other unrecognized program elements are determined to be program elements indirectly related to the architecture.
6. The software source code analysis method according to claim 4, characterized in that: The list of relevant program elements includes a plurality of preset relevant program elements, and identifying program elements directly related to the architecture among the program elements based on the preset list of relevant program elements related to the architecture and the preset architecture-related keywords includes: If the program element is the preset related program element, determining that the program element is a program element directly related to the architecture; If the program element includes the architecture-related keyword, the program element is determined to be a program element directly related to the architecture.
7. The software source code analysis method according to claim 1, characterized in that: The determining, based on the architecture-related code snippet of the architecture-related element, a level of relevance between the architecture-related element and the instruction set architecture, includes: Obtaining supplementary property data of the architecture-related code snippet, the supplementary property data including the instruction set architecture to which the architecture-related code snippet belongs, control of the architecture-related code snippet by the instruction set architecture, and a frequency of reference of the architecture-related code snippet in the source code; Based on the architecture-related code snippet and the property supplementary data, a level of relevance between the architecture-related element and the instruction set architecture is obtained.
8. A software source code analysis device, characterized in that: include: An initial compilation module is used to compile and analyze the source code of the software to obtain a flow chart of the preprocessing process of the source code; An acquisition module, configured to acquire free edges of the preprocessing process flow chart, wherein the free edges correspond to uncompiled code segments in the source code; a flipping and compiling module, configured to selectively flip preprocessing conditions related to free edges until all source code segments corresponding to the free edges have been compiled, thereby obtaining at least one compilation result of the source code; an identification module, configured to identify architecture-related elements of the source code based on the at least one compilation result, wherein the architecture-related elements include program elements directly related to the architecture and program elements indirectly related to the architecture; A determination module is configured to determine a level of relevance between the architecture-related element and an instruction set architecture based on the architecture-related code snippet of the architecture-related element.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the source code analysis method for software according to any one of claims 1 to 7 is implemented.
10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the source code analysis method of the software according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Program cross-architecture migration method and device, computer equipment and storage medium
CN118276931A
Using compiler commentary for configuring an integrated development environment
US20090125873A1