Software source code analysis method and device, electronic equipment and storage medium
By compiling and analyzing the source code and selectively flipping the preprocessing conditions, identifying and analyzing the architecture-related elements, the problem of low recognition efficiency of the source code instruction set architecture-related program elements in the existing technology is solved, and a more efficient cross-architecture adaptation process is achieved.
Patent Information
- Application Number
- CN202411628136.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-14
- Publication Date
- 2025-03-28
- Estimated Expiration
- 2044-11-14
AI Technical Summary
In the prior art, the identification efficiency of program elements related to the source code instruction set architecture is low, resulting in a lot of time and effort required for manual analysis and modification during cross-architecture adaptation.
By compiling and analyzing the source code of the software, the free edges in the preprocessing process flow chart are obtained, and the relevant preprocessing conditions are selectively flipped until the source code fragments corresponding to all the free edges are compiled, and the architecture-related elements of the source code are identified and their correlation level is determined with the instruction set architecture.
It improves the integrity of source code analysis and the efficiency of identifying architecture-related elements, and can accurately identify architecture-related code snippets that will cause problems, thereby improving the success rate of source code adaptation to new instruction set architectures.
Smart Images

Figure CN119718397A_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 source code of software. Background Art
[0002] Reduced Instruction Set Computer - Five (RISC-V) is an emerging reduced instruction set architecture. Its open source nature has attracted wide attention and application from hardware and software manufacturers at home and abroad. RISC-V is not only free and commercially available, but also supports hardware manufacturers to customize it according to actual needs. It has the technology and market potential to replace the existing commercial instruction set architecture. However, as the core element of computing devices, the development of processor instruction set architecture is highly dependent on the support of the software ecosystem. Compared with commercial instruction sets such as X86 and Advanced RISC Machine (ARM), which started earlier and have rich software ecosystems, RISC-V is still in the early stages of development. In order to quickly enrich the software ecosystem of RISC-V, it has become an important way to adapt the software that supports the existing instruction set to the RISC-V instruction set across architectures. The quality and efficiency of cross-architecture adaptation work also have a far-reaching impact on the RISC-V architecture software ecosystem.
[0003] RISC-V is an emerging processor instruction set architecture in recent years. There are still a lot of software that needs to be adapted across architectures, and there is no direct identification technology for architecture-related code in software. There are currently a variety of identification and analysis technologies for software source code, but they only lay the foundation for specific identification and analysis tasks, and there is still a lack of architecture-related code identification and analysis technology for cross-architecture adaptation. Faced with a large amount of software to be adapted to the RISC-V architecture, even if developers use the software automatic build system to automate the compilation and build process, they still need to invest a lot of time and energy to manually analyze the specific reasons for the build failure, locate and modify the problem code, and then repeat the compilation and build process. The efficiency and quality of this process depends on the experience and knowledge of the developer, and it still has a high technical threshold and uncertainty.
[0004] In summary, the existing recognition efficiency of source code instruction set architecture related program elements is low. Summary of the invention
[0005] The present invention provides a software source code analysis method, device, electronic device and storage medium, which are used to solve the defect of low recognition efficiency of program elements related to source code instruction set architecture in the prior art, and to improve the recognition efficiency of program elements related to source code instruction set architecture.
[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 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 preprocessing conditions related to the free edges until all source code fragments corresponding to the free edges have been compiled to obtain 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 include program elements directly related to the architecture and program elements indirectly related to the architecture; based on the architecture-related code fragments of the architecture-related elements, determining the correlation level between the architecture-related elements and the instruction set architecture.
[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, and 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: when the control preprocessing condition of the free edge is flipped for the first time, the control preprocessing condition is flipped to determine that the source code fragment corresponding to the free edge is compiled, and a compilation result is obtained; when the control preprocessing condition of the free edge is not flipped for the first time, the associated preprocessing condition of the control preprocessing condition is obtained, the associated preprocessing condition is locked and not flipped, and the control preprocessing condition is flipped to determine that the source code fragment corresponding to the free edge is compiled, and a compilation result is obtained, and the associated preprocessing condition is a preprocessing condition that has a dependency relationship with the control preprocessing condition; when all source code fragments corresponding to the free edges have been compiled, the flipping of the preprocessing conditions related to the free edges is stopped.
[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 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.
[0009] According to the source code analysis method of software provided by the present invention, architecture-related elements of the source code are identified based on at least one compilation result, including: identifying multiple types of program elements of 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 preset list of relevant program elements related to the architecture and preset 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 program elements indirectly related to the architecture in the other unidentified program elements, including: when the other unidentified program elements are controlled by architecture-related macros, the other unidentified program elements are determined to be program elements indirectly related to the architecture; when the other unidentified program elements are variables, and there are architecture-related variables in the data flow of the variables, the other unidentified program elements are determined to be program elements indirectly related to the architecture; when the other unidentified program elements are functions, and the functions directly or indirectly call architecture-related functions, the other unidentified program elements are determined to be program elements indirectly related to the architecture; when the other unidentified program elements are data types, and the names of the data types and their members include architecture-related keywords, the other unidentified program elements are determined to be program elements indirectly related to the architecture.
[0011] According to the source code analysis method for software provided by the present invention, the list of related program elements includes multiple preset related program elements, and 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 source code analysis method for software 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 instruction set architecture on 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, 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 source code of the software 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 flip 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 to obtain 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; a determination module, used to determine the correlation level of the architecture-related elements with 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, wherein when the processor executes the computer program, a source code analysis method of any of the above software is implemented.
[0015] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the source code analysis method 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 improving the efficiency of identifying architecture-related elements of 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 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. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces 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 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 schematic diagrams of the compilation process of the source code 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] Fig. 9 It is a structural schematic diagram of the software source code analysis device provided by the present invention.
[0027] Fig.10 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0028] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0029] Combine the following Figure 1-Figure 10 The 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 the hardware platform, operating system, compiler version and other environmental factors remain unchanged, the source code is compiled and analyzed according to the given source code and compilation parameters to obtain the source code preprocessing process flow chart.
[0033] S200: Obtaining free edges of a preprocessing process flow chart, where the free edges correspond to code snippets 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++ language, 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: Line 1: #define M1 (define macro M1) Line 2: Line 3: #ifdef M1 (checks if macro M1 is defined) Line 4: #define M2 (if macro M1 is defined, define macro M2) Line 5: #else Line 6: #ifdef M3 (if macro M1 is not defined, check whether macro M3 is defined) Line 7: #define M4 (if macro M3 is defined, define macro M4) Line 8: #else Line 9: #define M5 (if macro M3 is not defined, define macro M5) Line 10: #endif (end) Line 11: #ifndef M6 (checks if macro M6 is undefined) Line 12: #define M7 (if macro M6 is not defined, define macro M7) Line 13: #else Line 14: #define M8 (if macro M6 is defined, define macro M8) Line 15: #endif (end) Line 16: #endif (end) Line 17: Line 18: #ifdef M7 (checks if macro M7 is defined) Line 19: #define M9 (if macro M7 is defined, define macro M9) Line 20: #else Line 21: #define M10 (if macro M7 is not defined, define macro M10) Line 22: #endif (end) like Figure 2 As shown in the figure, the source code of the software 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 in Figure 2 The free edge F1 is formed in the code. Line 18 (if macro M7 is defined) is preprocessing condition 2, and the true branch corresponding to preprocessing condition 2 is the uncompiled code fragment (line 19). Since macro M7 is not defined, line 19 is Figure 2 A free edge F2 is formed in the process.
[0035] S300: 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.
[0036] like Figure 8 As shown, in order to analyze the code fragments that do not participate in the compilation corresponding to the skipped free edges, 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.
[0037] After the source code is compiled for the first time, the preprocessing process flowchart (original analysis result) and the first compilation result after the first compilation are obtained. If there are free edges in the preprocessing process flowchart after the first compilation, it means that there are uncompiled code fragments in the source code, and 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 result 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 to perform 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.).
[0038] The compilers used in the present invention include the GNU compiler collection (GNU C / C++ Compiler, GCC), the LLVM C language family front-end (LLVM Clang) compiler and its variants. The present invention takes the LLVM Clang compiler as an example to illustrate: (1) write an LLVM Clang compiler plug-in (preprocessing analysis plug-in) to implement the required source code recognition and analysis function logic; (2) obtain the source code of the software to be analyzed, ensure that it has been fully expanded and the required patch files have been applied; (3) extract and summarize the compilation parameters of each module and source code of the software to be analyzed; (4) add the relevant parameters required by the plug-in to the final compilation parameters; (5) simulate the compilation of the source code according to the normal process, without generating the target code file. It is sufficient to ensure that the functional logic related to the plug-in can be triggered.
[0039] 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.
[0040] According to the above-mentioned at least one compilation result of the source code, perform program element analysis to identify all program elements of the source code. Obtain the plug-in output results and parse them to identify the program elements related to the architecture from all program elements. The architecture includes but is not limited to X86, ARM, and RISC-V.
[0041] A first pass through all program elements of the source code is performed to identify program elements among the program elements that are directly related to the architecture.
[0042] The remaining unidentified program elements in the program elements are traversed again to identify the program elements indirectly related to the architecture among the other unidentified program elements.
[0043] Architecture-related elements include macros, variables, functions, data types, etc.
[0044] S500: Determine a correlation level between the architecture-related element and the instruction set architecture based on the architecture-related code fragment of the architecture-related element.
[0045] Based on the architecture-related code snippets of the architecture-related elements, determine the correlation level 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 of the architecture-related code snippet being referenced in the source code; based on the architecture-related code snippets and the property supplementary data, obtain the correlation level between the architecture-related elements and the instruction set architecture.
[0046] The specific scope of the architecture-related code snippet usually 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 usually includes statements for defining, assigning, and using the variable. If the architecture-related element is a function, the corresponding architecture-related code snippet usually includes statements for declaring, defining, and using the function. If the architecture-related element is a macro, the corresponding architecture-related code snippet usually includes statements for defining, redefining, and using the macro. If the architecture-related element is a data type, the corresponding architecture-related code snippet usually includes statements for defining and using the data type.
[0047] After obtaining the architecture-related code snippets of architecture-related elements, in order to improve the richness of the recognition results and facilitate subsequent reference and expansion, the property supplementary data of the architecture-related code snippets can be further obtained. Table 1 lists the property supplementary data of some architecture-related code snippets, which can be supplemented in the recognition results of architecture-related code snippets according to the capabilities of the corresponding analysis tools of each programming language.
[0048] Table 1 Properties of some architecture-related code snippets Supplementary data
[0049] According to the architecture-related code snippets and the supplementary data of properties, the degree of association between the architecture-related elements and the instruction set architecture is determined, and the relevance level of the degree of association is determined. According to the relevance level, the architecture-related code snippets that will cause problems when the source code of the software is adapted to the new instruction set architecture are identified, and the architecture-related code snippets that will cause problems are modified in a targeted manner.
[0050] The software source code analysis method provided by the present invention realizes 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 improving the efficiency of identifying architecture-related elements of 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. The present invention determines the level of correlation between architecture-related elements and instruction set architecture, thereby accurately identifying architecture-related code fragments that may cause problems when 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.
[0051] 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 specific steps are as follows.
[0052] S310: When the control preprocessing condition of the free edge is flipped for the first time, the control preprocessing condition is flipped to determine that the source code segment corresponding to the free edge is compiled, and a compilation result is obtained.
[0053] 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 determine that the source code fragment corresponding to the free edge is compiled, and obtain a compilation result, in which the associated preprocessing condition is a preprocessing condition that has a dependency relationship with the control preprocessing condition.
[0054] S330: When all source code fragments corresponding to the free edges have been compiled, the flipping of the preprocessing conditions related to the free edges is stopped.
[0055] 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.
[0056] The selective flipping can be performed in a top-down or bottom-up order, and the present invention is described by taking the top-down order as an example.
[0057] Compile the source code for the first time, and obtain the preprocessing process flow chart after the first compilation and the first compilation result.
[0058] The control preprocessing condition is the preprocessing condition that directly controls the free edge. For example, the control preprocessing condition of the free edge F1 is preprocessing condition 1 (if macro M1 has been defined), and the control preprocessing condition of the free edge F2 is preprocessing condition 2 (if macro M7 has been defined).
[0059] 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 macro M1 is defined (ifdef M1)" to "check whether macro M1 is defined (ifndef M1)". Flip the preprocessing condition 2 (the 18th line of code), and flip "check whether macro M7 is defined (ifdef M7)" to "check whether macro M7 is undefined (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 to obtain a preprocessing process flow chart after the second compilation and the second compilation result.
[0060] Depend on Figure 3 It can be seen that in the preprocessing process flow chart after the second compilation, the free edge F1 disappears. The free edge F2' corresponding to the free edge F2 is still a free edge. The reason is that the 12th line of code in the source code after the first flip restricts "define macro M7", which offsets the effect of flipping the preprocessing condition 2.
[0061] 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 (associated preprocessing conditions), forming an uncompiled code fragment, which constitutes a free edge F3.
[0062] 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.
[0063] 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 not 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 3In 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 3 As shown in the figure, the associated preprocessing conditions of the control preprocessing condition (line 18) corresponding to the free edge F2' are line 11 and line 3 (line 6 is at the same level as line 11 and both are preprocessing conditions, but line 6 has no effect on the definition status of macro M7, so line 6 is not the control preprocessing condition corresponding to the free edge F2'). Therefore, it is only necessary to lock line 11 and line 3, flip line 18, and flip "check whether macro M7 is undefined (ifndefM7)" to "check whether macro M7 is defined (ifdef M7) to ensure that the free edge F2' is compiled. For the control preprocessing condition (line 6) of the branch where the free edge F3 is located, the control preprocessing condition (line 6) of the branch where the free edge F3 is located is flipped for the first time, 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 after the third compilation and the third compilation result are obtained. Figure 4 It can be seen that in the preprocessing process flow chart after the third compilation, since the 11th line of code and the 3rd line of code are both locked, after the second selective flip, the free edge F4 does not change and remains the free edge F4'. The free edges F2' and F3 both disappear.
[0064] In the third selective flipping process, the control processing condition (11th line of code) of the branch where the free edge F4' is located is flipped for the first time. Directly flip the control processing condition (11th line of code) 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.
[0065] Combining the above process, four compilation results of the source code are obtained.
[0066] Optionally, the control preprocessing condition is flipped, including: when the control preprocessing condition is to check whether the previous condition is satisfied (elifdef), the preprocessing condition after flipping 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 preprocessing condition after flipping is to check whether the previous condition is satisfied.
[0067] The present invention realizes the compilation of the source code fragment corresponding to the free edge by flipping the control preprocessing condition of the free edge. For the control preprocessing condition that is not flipped for the first time, the success rate of free edge compilation is improved by locking the associated preprocessing condition and then flipping it. The present invention improves the integrity of source code compilation by iteratively flipping the control preprocessing condition for multiple times.
[0068] Based on the above embodiment, based on at least one compilation result, identifying architecture-related elements of the source code includes S410 to S430, and each step is specifically as follows.
[0069] S410: Based on at least one compilation result, identify multiple types of program elements of the source code.
[0070] 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 keywords related to the architecture.
[0071] 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.
[0072] According to the above-mentioned at least one compilation result, various program elements of the source code are identified and traversed, and relevant information corresponding to each program element is extracted through an analysis tool. According to the relevant information of each program element, it is determined whether the program element is an architecture-related element.
[0073] 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.
[0074] A list of relevant program elements is preset, and the list of relevant program elements includes a plurality of preset relevant program elements. The preset relevant program elements include preset architecture-related functions, preset macros, and preset data types. For example, a list of relevant program elements such as architecture-related functions and data types is extracted from a compiler or library file in advance, or the list of relevant program elements is supplemented through post-processing operations.
[0075] like Figure 6 As 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.
[0076] like Figure 6 As shown, each program element is traversed, and 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.
[0077] 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.
[0078] Traversing other unidentified program elements in the program elements, 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.
[0079] 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.
[0080] If the current other unrecognized program elements are controlled by the architecture-related macro, it is determined that the current other unrecognized program elements are architecture-indirectly-related program elements.
[0081] If the other currently unidentified program elements are variables, data flow analysis is performed on the variable. If the analysis shows that there are architecture-related variables in the data flow of the variable, the variable is marked as an architecture-indirectly related program element.
[0082] If the other currently unidentified program elements are functions, function pointing analysis is performed on the function itself and all function calls within the function body. If the analysis result shows that the function directly or indirectly calls an architecture-related function, the function is marked as an architecture-indirectly related program element.
[0083] If the current other unrecognized program element is a data type, then perform alias analysis on the data type and its members (if any). If the alias of the data type itself (or any of its members) contains architecture-related keywords, then mark the data type as an indirectly architecture-related program element.
[0084] 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.
[0085] 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 architecture indirect relevance judgment conditions are met, the corresponding other unidentified program elements are marked as architecture-indirect related program elements.
[0086] The present invention realizes accurate identification of program elements directly related to the architecture based on the list of relevant program elements and architecture-related keywords, and performs different analyses on other unidentified program elements of different categories to identify program elements indirectly related to the architecture, thereby realizing comprehensive identification of program elements indirectly related to the architecture.
[0087] 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 referred to each other.
[0088] like Fig. 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 preprocessing process of the source code.
[0089] The acquisition module 902 is used to acquire the free edges of the preprocessing process flow chart, where the free edges correspond to code fragments in the source code that are not involved in compilation.
[0090] The flipping and compiling module 903 is used to selectively flip the preprocessing conditions related to the free edges until all the source code fragments corresponding to the free edges have been compiled to obtain at least one compilation result of the source code.
[0091] The identification module 904 is used to identify architecture-related elements of the source code based on at least one compilation result, where the architecture-related elements include program elements directly related to the architecture and program elements indirectly related to the architecture.
[0092] The determination module 905 is used to determine the relevance level of the architecture-related element to the instruction set architecture based on the architecture-related code fragment of the architecture-related element.
[0093] The software source code analysis device provided by the present invention realizes 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 improving the efficiency of identifying architecture-related elements of 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 correlation level between architecture-related elements and instruction set architecture, the present invention realizes accurate identification of architecture-related code fragments that may cause problems when 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.
[0094] In one embodiment, the preprocessing conditions related to the free edge include the control preprocessing conditions that directly control the free edge and the associated preprocessing conditions that indirectly affect the free edge. The flipping and compiling 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 determine that the source code segment corresponding to the free edge is compiled, and obtain 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 determine that the source code segment corresponding to the free edge is compiled, and obtain a compilation result, wherein the associated preprocessing condition is a preprocessing condition that has a dependency relationship with the control preprocessing condition; when all the source code segments corresponding to the free edges have been compiled, stop flipping the preprocessing conditions related to the free edges.
[0095] In one embodiment, the flip and compile module 903 is used for: 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.
[0096] 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.
[0097] 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 program elements indirectly related to the architecture; 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 program elements indirectly related to the architecture; 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 program elements indirectly related to the architecture; 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 program elements indirectly related to the architecture.
[0098] 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.
[0099] In one embodiment, the determination module 905 is used to: obtain property supplement data of the architecture-related code snippet, the property supplement 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 supplement data, obtain the correlation level between the architecture-related elements and the instruction set architecture.
[0100] Fig.10 An example of a physical structure diagram of an electronic device is shown in FIG. Fig.10As shown, the electronic device may include: a processor 1010, a communication interface 1020, a memory 1030 and a communication bus 1040, wherein the processor 1010, the communication interface 1020 and the memory 1030 communicate with each other through the communication bus 1040. The processor 1010 may call the logic instructions in the memory 1030 to execute the source code analysis method of the software, the method comprising: compiling and analyzing the source code of the software to obtain a preprocessing process flow chart of the source code; obtaining the free edges of the preprocessing process flow chart, the free edges corresponding to the 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 the source code fragments corresponding to the free edges have been compiled, and obtaining at least one compilation result of the source code; based on at least one compilation result, identifying the architecture-related elements of the source code, the architecture-related elements include program elements directly related to the architecture and program elements indirectly related to the architecture; based on the architecture-related code fragments of the architecture-related elements, determining the correlation level between the architecture-related elements and the instruction set architecture.
[0101] In addition, the logic instructions in the above-mentioned memory 1030 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.
[0102] 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 preprocessing conditions related to the free edges until all source code fragments corresponding to the free edges have been compiled to obtain at least one compilation result of the source code; based on 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; based on the architecture-related code fragments of the architecture-related elements, determining the level of correlation between the architecture-related elements and the instruction set architecture.
[0103] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0104] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0105] 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 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 preprocessing process flow chart of the source code; Obtaining free edges of the preprocessing process flow chart, wherein the free edges correspond to code fragments in the source code that are not involved in compilation; Selectively flipping 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; 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; Based on the architecture-dependent code snippets of the architecture-dependent elements, a level of dependency of the architecture-dependent elements 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 associated 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: When the control preprocessing condition of the free edge is flipped for the first time, flipping the control preprocessing condition to determine that the source code segment corresponding to the free edge is compiled, and obtaining the compilation result once; 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 without flipping it, flipping the control preprocessing condition to determine that the source code fragment corresponding to the free edge is compiled, and obtaining the compilation result once, wherein the associated preprocessing condition is a preprocessing condition having a dependency relationship with the control preprocessing condition; When the source code fragments 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 comprises: 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 preset list of related program elements related to the architecture and preset keywords related to the architecture; Other unidentified program elements among the program elements are traversed to identify program elements indirectly related to the architecture among the other unidentified program elements, wherein 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 to identify program elements indirectly related to the architecture in the other unidentified program elements includes: When the other unidentified program elements are controlled by architecture-related macros, determining that the other unidentified program elements are program elements indirectly related to the architecture; When the other unidentified program element is a variable and there is an architecture-related variable 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 a program element indirectly related to the architecture; When the other unrecognized program elements are data types, and the names of the data types and their members include architecture-related keywords, it is determined that the other unrecognized program elements are program elements indirectly related to the architecture.
6. The software source code analysis method according to claim 4, characterized in that: The list of related program elements includes a plurality of preset related program elements, and the step of identifying program elements directly related to the architecture among the program elements based on the preset list of related 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 step of determining the relevance level of the architecture-related element to the instruction set architecture based on the architecture-related code snippet of the architecture-related element comprises: Acquire 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, the control of the instruction set architecture on the architecture-related code snippet, and the frequency of the architecture-related code snippet being referenced in the source code; Based on the architecture-dependent code snippet and the property supplementary data, a correlation level between the architecture-dependent 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 preprocessing process flow chart of the source code; An acquisition module, used for acquiring free edges of the preprocessing process flow chart, wherein the free edges correspond to uncompiled code fragments in the source code; A flipping and compiling module, used for 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; 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 used to determine a correlation level between the architecture-related element and an instruction set architecture based on the architecture-related code fragment 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 of the 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
Conditional compilation preprocessing method and device
CN106843996A
Code scanning method, code migration method and related devices
CN114647439A
Code compiling method, electronic equipment and computer readable storage medium
CN117742725A
Program cross-architecture migration method and device, computer equipment and storage medium
CN118276931A
Using compiler commentary for configuring an integrated development environment
US20090125873A1