Source code processing method and device and electronic equipment
By combining static code analysis technology and dependency information extraction method, the dependencies in the source code are automatically extracted, which solves the problem of difficult code dependency sorting in the existing technology and improves the efficiency of code transplantation.
Patent Information
- Application Number
- CN202411879179.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-19
- Publication Date
- 2025-04-11
- Estimated Expiration
- 2044-12-19
AI Technical Summary
It is difficult for the existing technology to effectively extract dependencies in the code during source code software testing, which affects the efficiency of code transplantation.
By combining static code analysis technology and dependency information extraction method, the dependencies of each function in the functional module are automatically extracted, and abstract syntax trees, parsing processing results, code block division results and control flow diagrams are generated to perform dependency analysis.
It realizes automatic extraction of code dependency information during source code software testing, improves code understanding and porting efficiency, and solves the problem of difficulty in sorting out code dependencies.
Smart Images

Figure CN119807010A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, device and electronic device for processing source code. Background Art
[0002] With the development of intelligent automobiles, automobile software plays an important role in vehicle use. Since the functional modules of automobile software need to be transplanted between different platforms, the dependency relationships between functional modules often need to be sorted out during the transplantation process to adapt the platform. Manually determining the dependency relationships in each function can only achieve half the result with twice the effort, and extracting dependency relationships plays an indispensable role in understanding the code and code transplantation.
[0003] In the prior art, after the source code is developed, static code analysis tools are usually used to perform software testing on the source code to ensure the software quality. However, these static code analysis tools mostly focus on basic analysis such as lexical analysis, syntax analysis, type analysis, control flow analysis, and data flow analysis, and there is a problem of difficulty in sorting out the dependencies in the code. In order to improve the efficiency of code transplantation, how to extract the dependency information in the code when performing software testing on the source code has become an urgent problem to be solved. Summary of the invention
[0004] In order to solve the problems of the prior art, the present application provides a source code processing method, device and electronic device, which combine common static code analysis technology and dependency information extraction methods to automatically extract the dependency relationships of various functions in functional modules, assist in code understanding and code transplantation, and solve the problem of how to extract dependency information in the code when performing software testing on the source code.
[0005] According to one aspect of the present application, a method for processing source code is provided, the method comprising:
[0006] Obtaining source code and configuration information of a project; the configuration information represents information required for the operation of the project;
[0007] Performing syntax analysis on the source code based on the configuration information to generate an abstract syntax tree of the source code;
[0008] Determine a target node of the associated function in the abstract syntax tree, perform parsing on the target node, and obtain a parsing result of the target node; the parsing result represents function information of the target node;
[0009] Perform code block division based on the parsing result of the target node to obtain the code block division result of the target node, perform control flow parsing based on the code block division result of the target node to generate a control flow graph of the target node;
[0010] Dependency analysis is performed based on the control flow graph of the target node to obtain dependency information of the target node.
[0011] In a possible implementation, the configuration information includes an additional include directory, an additional macro definition, and a project path of the project, wherein the additional include directory is a header file that the project depends on, and the additional macro definition is a macro that the project depends on.
[0012] The performing syntax analysis on the source code based on the configuration information to generate an abstract syntax tree of the source code includes:
[0013] Generate compilation parameters according to the additional include directory, the additional macro definition and the project path of the project;
[0014] A compiler is called based on the compilation parameters to perform syntax analysis on the source code to generate an abstract syntax tree of the source code.
[0015] In a possible implementation, each node of the abstract syntax tree includes type information; and determining a target node of an associated function in the abstract syntax tree includes:
[0016] Each node of the abstract syntax tree is traversed, and for a current node traversed, if the type information of the current node matches the function type, the node is determined to be the target node.
[0017] In a possible implementation, the parsing the target node to obtain a parsing result of the target node includes:
[0018] Performing statement analysis on the function represented by the target node to obtain the function name, function parameters, return value type and statement of the function;
[0019] Based on the function name, function parameters, return value type and statement of the function, a parsing result for the target node is obtained.
[0020] In a possible implementation, performing code block division based on the parsing result of the target node to obtain the code block division result of the target node includes:
[0021] According to the statements of the function in the parsing result of the target node, the function is divided into multiple code blocks;
[0022] Determine that the multiple code blocks are code block division results of the target node.
[0023] In a possible implementation, the method further includes:
[0024] If the type information of the node matches the data type, the node is determined to be a type node, and type parsing is performed on the data type represented by the type node to obtain type information of the data type corresponding to the type node;
[0025] Based on the type information of the data type corresponding to the type node, a type parsing result of the type node in each code block is determined.
[0026] In a possible implementation, performing dependency analysis based on the control flow graph of the target node to obtain dependency information of the target node includes:
[0027] Based on the control flow graph, extract the global variables, function call information and type dependency information referenced in each code block to obtain the dependency information of each code block; the type dependency information includes the type parsing results of the type nodes in each code block;
[0028] The dependency information of the node is composed based on the dependency information of each code block.
[0029] In a possible implementation, the method further includes:
[0030] In the event that an exception occurs in the generation of the abstract syntax tree of the source code, generating an exception report;
[0031] Outputting error information of the source code based on the exception report.
[0032] In another aspect, a source code processing device is provided, the device comprising:
[0033] An information acquisition module, used to acquire source code and configuration information of a project; the configuration information represents information required for the operation of the project;
[0034] An abstract syntax tree generation module, configured to perform syntax analysis on the source code based on the configuration information to generate an abstract syntax tree of the source code;
[0035] A parsing module, used to determine a target node of an associated function in the abstract syntax tree, perform parsing on the target node, and obtain a parsing result of the target node; the parsing result represents function information of the target node;
[0036] A code block division module is used to divide the code blocks based on the parsing result of the target node, obtain the code block division result of the target node, perform control flow analysis based on the code block division result of the target node, and generate a control flow graph of the target node;
[0037] The dependency information determination module is used to perform dependency analysis based on the control flow graph of the target node to obtain the dependency information of the target node.
[0038] In a possible implementation, the configuration information includes an additional include directory, an additional macro definition, and a project path of the project, wherein the additional include directory is a header file that the project depends on, and the additional macro definition is a macro that the project depends on.
[0039] The abstract syntax tree generation module is used to:
[0040] Generate compilation parameters according to the additional include directory, the additional macro definition and the project path of the project;
[0041] A compiler is called based on the compilation parameters to perform syntax analysis on the source code to generate an abstract syntax tree of the source code.
[0042] In a possible implementation, each node of the abstract syntax tree includes type information; the parsing processing module includes a target node determination unit, and the target node determination unit is used to:
[0043] Each node of the abstract syntax tree is traversed, and for a current node traversed, if the type information of the current node matches the function type, the node is determined to be the target node.
[0044] In a possible implementation, the analysis processing module further includes a function analysis unit, and the function analysis unit is used to:
[0045] Performing statement analysis on the function represented by the target node to obtain the function name, function parameters, return value type and statement of the function;
[0046] Based on the function name, function parameters, return value type and statement of the function, a parsing result for the target node is obtained.
[0047] In a possible implementation, the code block division module is used to:
[0048] According to the statements of the function in the parsing result of the target node, the function is divided into multiple code blocks;
[0049] Determine that the multiple code blocks are code block division results of the target node.
[0050] In a possible implementation, the device further includes a type parsing module, and the type parsing module is used to:
[0051] If the type information of the node matches the data type, the node is determined to be a type node, and type parsing is performed on the data type represented by the type node to obtain type information of the data type corresponding to the type node;
[0052] Based on the type information of the data type corresponding to the type node, a type parsing result of the type node in each code block is determined.
[0053] In a possible implementation, the dependency information determining module is used to:
[0054] Based on the control flow graph, extract the global variables, function call information and type dependency information referenced in each code block to obtain the dependency information of each code block; the type dependency information includes the type parsing results of the type nodes in each code block;
[0055] The dependency information of the node is composed based on the dependency information of each code block.
[0056] In a possible implementation, the device further includes an error information output module, and the error information output module is used to:
[0057] In the event that an exception occurs in the generation of the abstract syntax tree of the source code, generating an exception report;
[0058] Outputting error information of the source code based on the exception report.
[0059] On the other hand, an electronic device is provided, comprising a processor and a memory, wherein the memory stores at least one instruction or at least one program, and the at least one instruction or the at least one program is loaded and executed by the processor to implement the source code processing method of any of the above aspects.
[0060] On the other hand, a computer-readable storage medium is provided, in which at least one instruction or at least one program is stored, and the at least one instruction or the at least one program is loaded and executed by a processor to implement a source code processing method as described in any of the above aspects.
[0061] On the other hand, a computer program product or a computer program is provided, the computer program product or the computer program includes computer instructions, the computer instructions are stored in a computer-readable storage medium. A processor of an electronic device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the electronic device executes the source code processing method of any of the above aspects.
[0062] The embodiment of the present application obtains the source code and configuration information of the project; the configuration information represents the information required for the operation of the project; the source code is subjected to syntax analysis based on the configuration information to generate an abstract syntax tree of the source code; the target node of the associated function in the abstract syntax tree is determined, the target node is subjected to parsing, and the parsing result of the target node is obtained; the parsing result represents the function information of the target node; the code block is divided based on the parsing result of the target node to obtain the code block division result of the target node, the control flow is parsed based on the code block division result of the target node to generate the control flow graph of the target node; the dependency analysis is performed based on the control flow graph of the target node to obtain the dependency information of the target node. Combining common static code analysis techniques and dependency information extraction methods, the dependency relationship of each function in the functional module is automatically extracted to help code understanding and code transplantation, and the problem of how to extract dependency information in the code when performing software testing on the source code is solved. BRIEF DESCRIPTION OF THE DRAWINGS
[0063] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0064] Figure 1 It is a flowchart of a source code processing method provided in an embodiment of the present application;
[0065] Figure 2 It is a flowchart of a source code processing method provided in an embodiment of the present application;
[0066] Figure 3 It is a structural block diagram of a source code processing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0067] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0068] Automotive software often needs to adapt to different target platforms, so for the same functional module, it is often necessary to sort out the dependencies between functional modules multiple times to adapt the platform. This embodiment is mainly used for porting functional modules in the operating system between different platforms. Clang (Clang is a lightweight compiler for C, C++, and Objective-C) is used to extract the global variable usage, function call relationship, and type dependency relationship in each function in a specific functional module, and a complete functional module is divided into multiple "particles". When code porting is required, the "particles" extracted by this embodiment can be used as units to modify the code as a whole.
[0069] In recent years, automotive software has developed rapidly, and the corresponding software complexity has also increased day by day. Manually determining the dependencies in each function often only achieves half the result with twice the effort, and extracting dependencies plays an indispensable role in understanding code and code transplantation. This embodiment aims to replace manual inspection and divide the complete functional module into multiple units through the Clang compilation tool to help code understanding and code transplantation.
[0070] With the development of software products, people gradually realize the importance of code quality. Software defects caused by source code defects not only affect user use, but also further increase the cost of software maintenance. Therefore, it is very important to capture and properly handle these defects during the software source code development process. Traditional software testing technology is carried out after the source code is basically completed and before formal delivery. It can only detect defects that occur during software testing and requires a lot of manpower and material resources. Code static analysis technology has the characteristics of not needing to execute code to detect and being able to handle large-scale programs. It has been widely cited in the industry and has become an important means to ensure software quality.
[0071] Existing static code analysis tools mostly focus on basic analysis, namely lexical analysis, syntax analysis, type analysis, control flow analysis, and data flow analysis. By scanning the source code, the static properties of the code are abstracted to a certain extent, and then specific methods are used to analyze and infer possible faults that may occur during the actual operation of the program, and report possible defects in the code, such as memory leaks and array out-of-bounds access, to further locate the location of the error statement and check the overall structure of the program. Although some analysis tools also have functions such as symbolic execution, constraint solving, and type inference, the focus of the analysis is still defect detection, and no analysis is performed on the dependencies in the code, so the help for code understanding and code transplantation is limited.
[0072] This embodiment proposes a static analysis method, which not only includes the common functions of common code static analysis tools (type analysis, control flow analysis, etc.), but also can extract the use of global variables, function call relationships and type dependencies in functions, to assist in understanding the code and code porting between different target platforms.
[0073] The present application provides a source code processing method that combines common static code analysis technology and dependency information extraction methods to automatically extract the dependency relationships of various functions in a functional module, thereby helping to understand and transplant the code. Figure 1 The source code processing method includes steps S101 to S109.
[0074] In step S101, the source code and configuration information of the project are obtained; the configuration information represents the information required for the operation of the project.
[0075] In a possible implementation, the item represents a goal of achieving a specific function, which may be, for example, an assisted driving function, a voice control function, or a communication function.
[0076] In a possible implementation, the source code is used to implement the function of the project. Specifically, the source code may be a code written in C language, that is, C code.
[0077] In a possible implementation, configuration information of the project is obtained by parsing a configuration file, where the configuration file is a file containing information required for successfully operating a program.
[0078] In a possible implementation, the configuration information represents information required for the project to run. Specifically, the configuration information can be set manually.
[0079] In a possible implementation, the configuration information includes an additional include directory, an additional macro definition, and a project path of the project.
[0080] In a possible implementation, the additional include directory is an additional include path, that is, the include path for the current project.
[0081] In a possible implementation, the additional macro is defined as a custom macro for simplifying configuration items when configuring an include directory (include) or adding dependencies.
[0082] In a possible implementation, the project path of the project is the project root directory, that is, the top-level directory selected for the entire project in the file system. Specifically, the project root directory usually contains all the contents of the project, including source code, build configuration files, documents, test files, etc.
[0083] In step S103, a syntax analysis process is performed on the source code based on the configuration information to generate an abstract syntax tree of the source code.
[0084] In a possible implementation, the configuration information includes an additional include directory, an additional macro definition, and a project path of the project, wherein the additional include directory is a header file that the project depends on, and the additional macro definition is a macro that the project depends on.
[0085] The performing syntax analysis on the source code based on the configuration information to generate an abstract syntax tree of the source code includes:
[0086] Generate compilation parameters according to the additional include directory, the additional macro definition and the project path of the project;
[0087] A compiler is called based on the compilation parameters to perform syntax analysis on the source code to generate an abstract syntax tree of the source code.
[0088] In a possible implementation, compilation parameters required for calling Clang / clang are generated according to configuration information in a configuration file, and an abstract syntax tree AST (abstract syntax code) of the source code of the project is generated.
[0089] In a possible implementation, Clang / clang is a lightweight compiler for C, C++, and Objective-C. The Clang compiler is used to perform lexical analysis and syntax analysis to generate an abstract syntax tree corresponding to the source code.
[0090] In a possible implementation, the AST of the source code is an abstract grammatical structure of the source code, which represents the grammatical structure of the language in the form of a tree. Each node on the tree stores the type information of the current node, and each node represents a structure in the source code.
[0091] In a possible implementation, the type information represented by the nodes in the AST of the source code can be mainly divided into three categories: declaration, statement, and type. Among them, the node whose type information is function declaration is confirmed as a function node, and the node whose type information is data type declaration is confirmed as a type node.
[0092] In a possible implementation, a function node usually contains a statement node, under which other declaration nodes may be nested, such as a variable declaration node, and may also contain expressions. In C, a type node generally only contains a type node as a child node.
[0093] In a possible implementation, the nodes in the AST of the source code are traversed, and when the node is a function node, the function node is subjected to corresponding function parsing, and when the node is a type node, the type node is subjected to corresponding type parsing.
[0094] In this implementation, compilation parameters are generated based on configuration information, and the compiler is called based on the compilation parameters to perform syntax analysis on the source code, generate an abstract syntax tree of the source code, and perform abstract syntax on the source code to facilitate understanding of the structure of the code and improve the efficiency of understanding the code.
[0095] In a possible implementation, the method further includes:
[0096] In the event that an exception occurs in the generation of the abstract syntax tree of the source code, generating an exception report;
[0097] Outputting error information of the source code based on the exception report.
[0098] In a possible implementation, when an error occurs during the process of generating the AST, detailed error information in the source code is output.
[0099] In a possible implementation, error information of the source code is checked, and defects in the source code are captured and processed.
[0100] In this implementation, if an error occurs when generating AST, the error information is output to locate the defect in the source code, so as to facilitate the modification of the defect in the source code and improve the quality of the code.
[0101] In step S105, a target node of the associated function in the abstract syntax tree is determined, and the target node is parsed to obtain a parsing result of the target node; the parsing result represents function information of the target node.
[0102] In a possible implementation, each node of the abstract syntax tree includes type information; and determining a target node of an associated function in the abstract syntax tree includes:
[0103] Each node of the abstract syntax tree is traversed, and for a current node traversed, if the type information of the current node matches the function type, the node is determined to be the target node.
[0104] In a possible implementation, the parsing the target node to obtain a parsing result of the target node includes:
[0105] Performing statement analysis on the function represented by the target node to obtain the function name, function parameters, return value type and statement of the function;
[0106] Based on the function name, function parameters, return value type and statement of the function, a parsing result for the target node is obtained.
[0107] In a possible implementation, each node of the abstract syntax tree is traversed, and it is determined whether the current node is the target node according to type information stored in the current traversed node.
[0108] In a possible implementation manner, a node whose type information is a function declaration is determined to be a function node, and the function node in the abstract syntax tree is determined to be the target node.
[0109] In a possible implementation, when the target node is matched, that is, a function node is matched, the function corresponding to the function node is parsed, and the function name, parameter list, return value information, global variables referenced within the function, and data types used within the function are extracted as the parsing result of the target node. Specifically, the data type used within the function can be a structure.
[0110] In a possible implementation, a function node usually includes a statement node, under which other declaration nodes may be nested, such as a variable declaration node, and may also include an expression. The parsing result of the target node also includes the statement in the function.
[0111] In this implementation, each node of the abstract syntax tree is traversed, and for the current node traversed, the target node is determined based on the type information of the current node, the target node is parsed, and the parsing result of the target node is obtained. The source code is statically analyzed to ensure the quality of the source code and facilitate the subsequent implementation of code particleization according to the statement branch of the function.
[0112] In step S107, code block division is performed based on the parsing result of the target node to obtain the code block division result of the target node, and control flow analysis is performed based on the code block division result of the target node to generate a control flow graph of the target node.
[0113] In a possible implementation, performing code block division based on the parsing result of the target node to obtain the code block division result of the target node includes:
[0114] According to the statements of the function in the parsing result of the target node, the function is divided into multiple code blocks;
[0115] Determine that the multiple code blocks are code block division results of the target node.
[0116] In a possible implementation, the statement of the function in the parsing result of the target node may include at least one of a sequential statement, a switch statement, an if statement, an until statement, and a while statement.
[0117] In a possible implementation, a control flow parsing module of the compiler is called to divide each function into multiple code blocks according to branch conditions of statements, and a control flow graph of each function is generated.
[0118] In one possible implementation, a control flow graph (CFG) is an abstract representation of a process or program. It is an abstract data structure used in compilers and maintained internally by the compiler. It represents all the paths that a program will traverse during execution. It uses a graph to represent the possible flow of execution of all basic blocks in a process, and can also reflect the real-time execution of a process.
[0119] In a possible implementation, the control flow graph includes the entry (first basic block) and exit (last basic block) of a process, all possible next basic blocks of a basic block (all exits), all possible previous basic blocks of a basic block (all entries), and a statement table corresponding to a basic block. Specifically, the control flow graph can display the relationship between basic blocks in a process, the dynamic execution status, and the statement table corresponding to each basic block. In addition, there is other auxiliary information, such as the number of executions of each basic block, the execution time, etc.
[0120] In this implementation, code block division is performed based on the parsing result of the target node to obtain the code block division result of the target node, control flow analysis is performed based on the code block division result of the target node to generate a control flow graph of the target node. The control flow graph can intuitively display the structure of the source code. Through the representation of the control flow graph nodes and edges, the input and output of the program and the transfer relationship between each basic block can be clearly seen, which simplifies the code logic and improves the efficiency of source code analysis.
[0121] In step S109, dependency analysis is performed based on the control flow graph of the target node to obtain dependency information of the target node.
[0122] In a possible implementation, the method further includes:
[0123] If the type information of the node matches the data type, the node is determined to be a type node, and type parsing is performed on the data type represented by the type node to obtain type information of the data type corresponding to the type node;
[0124] Based on the type information of the data type corresponding to the type node, a type parsing result of the type node in each code block is determined.
[0125] In a possible implementation manner, a node whose type information is a data type declaration is confirmed as a type node.
[0126] In a possible implementation, the data types corresponding to the type node include typedef (declared type) and custom struct (struct type), union (union type) and enum (enumeration type). When the data types corresponding to the type node are nested, the nested data types are extracted in sequence until the built-in data types are basic data types. Specifically, the basic data types include integer (int), floating point (float, double), character (char) and Boolean (bool).
[0127] In one possible implementation, when the data type of the type node is a structure, the structure name, members in the structure, and the data type of each member in the structure are extracted from the structure, where the data type of each member may be other structures. In this case, the extraction will continue until the basic data type is obtained.
[0128] In a possible implementation, when the data type of the type node is a structure, the structure name, members in the structure, and data types of each member in the structure are extracted from the structure as type information of the data type corresponding to the type node.
[0129] In a possible implementation, type parsing is performed on the data type corresponding to the type node, and the type name, member name, and data type of the member of the data type are extracted as type information of the data type corresponding to the type node.
[0130] In a possible implementation, after parsing the type nodes, type information of data types corresponding to all type nodes in each code block is obtained to obtain type parsing results of the type nodes in each code block.
[0131] In this implementation, after type parsing the type node, type information of the data type corresponding to the type node is obtained, which helps to discover and fix potential errors at an early stage and improve the robustness of the program; based on the type information of the data type corresponding to the type node, the type parsing result of the type node in each code block is determined, and the type information makes the code more readable, reduces the runtime type checking and conversion overhead, and speeds up the understanding of the purpose of variables and functions.
[0132] In a possible implementation, performing dependency analysis based on the control flow graph of the target node to obtain dependency information of the target node includes:
[0133] Based on the control flow graph, extract the global variables, function call information and type dependency information referenced in each code block to obtain the dependency information of each code block; the type dependency information includes the type parsing results of the type nodes in each code block;
[0134] The dependency information of the node is composed based on the dependency information of each code block.
[0135] In a possible implementation, the control flow graph is traversed to extract global variables, function call information, and type dependency information referenced in each code block to obtain dependency information of each code block.
[0136] In a possible implementation manner, based on the dependency information of each code block corresponding to the node, a complete dependency relationship of the function corresponding to the node, that is, the dependency information of the node is obtained.
[0137] In a possible implementation, after obtaining the dependency information of the target node, the file location of the referenced content can be saved based on the dependency information and the corresponding source code can be extracted to obtain the particle code. Specifically, the referenced content actually refers to the dependent content represented by the dependency information.
[0138] In one possible implementation, assume that there is a function f in file A, and function f references type t in file B. The file location of the referenced content is saved and the corresponding source code is extracted. This step can obtain the location of t in file B, such as the line number and column number, and then extract the part of the source code that declares type t. In this way, function f and type t can be taken out separately. If you want to use this function f in other projects later, you can extract it together with type t and migrate it as a whole without making any other modifications.
[0139] In this implementation, based on the control flow graph, the dependency information of each code block of the node is obtained, and the dependency information of the node is composed based on the dependency information of each code block. Since the dependency information is data that describes the mutual dependencies between modules, libraries, and files, it can help clarify the relationship between code blocks. Through clear dependency relationships, the coupling between modules can be reduced, making each module more independent, reducing redundant code information, and facilitating separate migration and modification of code modules.
[0140] The beneficial effects of the present application are as follows: by obtaining the source code and configuration information of the project; the configuration information represents the information required for the operation of the project; based on the configuration information, the source code is subjected to syntax analysis and processing to generate an abstract syntax tree of the source code; the target node of the associated function in the abstract syntax tree is determined, the target node is subjected to parsing and processing to obtain the parsing and processing result of the target node; the parsing and processing result represents the function information of the target node; based on the parsing and processing result of the target node, code block division is performed to obtain the code block division result of the target node, control flow parsing is performed based on the code block division result of the target node, and a control flow graph of the target node is generated; dependency analysis is performed based on the control flow graph of the target node to obtain the dependency information of the target node. Combining common static code analysis techniques and dependency information extraction methods, the dependency relationship of each function in the functional module is automatically extracted to help code understanding and code transplantation, and the problem of how to extract dependency information in the code when performing software testing on the source code is solved.
[0141] Figure 2 A flowchart of a source code processing method provided by an embodiment of the present application is shown. Figure 2 , the source code processing method includes:
[0142] Generate the compilation parameters required to call clang according to the configuration information in the configuration file, and generate the AST of the current project;
[0143] When an error occurs during AST generation, the error information is passed to the diagnostic information processing module, which outputs detailed error information in the source code;
[0144] Traverse the AST and match the nodes therein. When matching function nodes and type nodes, call the function parsing module and type parsing module for processing respectively;
[0145] After the function is parsed, the control flow parsing module is called to divide each function into multiple code blocks according to the branch conditions and generate the control flow graph of the function;
[0146] Traverse the control flow graph, extract the global variables, function calls and type dependency information referenced in each code block, save the file location of the referenced content and extract the corresponding source code.
[0147] Figure 3 The schematic diagram of the structure of a source code processing device 300 provided in an embodiment of the present application is shown. The device has the function of implementing the source code processing method in the above method embodiment. The function can be implemented by hardware or by hardware executing corresponding software. Figure 3 As shown, the device may include:
[0148] The information acquisition module 301 is used to acquire the source code and configuration information of the project; the configuration information represents the information required for the operation of the project;
[0149] An abstract syntax tree generation module 302 is used to perform syntax analysis on the source code based on the configuration information to generate an abstract syntax tree of the source code;
[0150] The parsing module 303 is used to determine the target node of the associated function in the abstract syntax tree, perform parsing on the target node, and obtain a parsing result of the target node; the parsing result represents the function information of the target node;
[0151] A code block division module 304 is used to perform code block division based on the parsing result of the target node, obtain the code block division result of the target node, perform control flow analysis based on the code block division result of the target node, and generate a control flow graph of the target node;
[0152] The dependency information determination module 305 is used to perform dependency analysis based on the control flow graph of the target node to obtain the dependency information of the target node.
[0153] In a possible implementation, the configuration information includes an additional include directory, an additional macro definition, and a project path of the project, wherein the additional include directory is a header file that the project depends on, and the additional macro definition is a macro that the project depends on.
[0154] The abstract syntax tree generation module 302 is used to:
[0155] Generate compilation parameters according to the additional include directory, the additional macro definition and the project path of the project;
[0156] A compiler is called based on the compilation parameters to perform syntax analysis on the source code to generate an abstract syntax tree of the source code.
[0157] In a possible implementation, each node of the abstract syntax tree includes type information; the parsing processing module 303 includes a target node determination unit, and the target node determination unit is used to:
[0158] Each node of the abstract syntax tree is traversed, and for a current node traversed, if the type information of the current node matches the function type, the node is determined to be the target node.
[0159] In a possible implementation, the analysis processing module 303 further includes a function analysis unit, and the function analysis unit is used to:
[0160] Performing statement analysis on the function represented by the target node to obtain the function name, function parameters, return value type and statement of the function;
[0161] Based on the function name, function parameters, return value type and statement of the function, a parsing result for the target node is obtained.
[0162] In a possible implementation, the code block division module 304 is used to:
[0163] According to the statements of the function in the parsing result of the target node, the function is divided into multiple code blocks;
[0164] Determine that the multiple code blocks are code block division results of the target node.
[0165] In a possible implementation, the apparatus further includes a type parsing module 306, and the type parsing module 306 is configured to:
[0166] If the type information of the node matches the data type, the node is determined to be a type node, and type parsing is performed on the data type represented by the type node to obtain type information of the data type corresponding to the type node;
[0167] Based on the type information of the data type corresponding to the type node, a type parsing result of the type node in each code block is determined.
[0168] In a possible implementation, the dependency information determining module 305 is used to:
[0169] Based on the control flow graph, extract the global variables, function call information and type dependency information referenced in each code block to obtain the dependency information of each code block; the type dependency information includes the type parsing results of the type nodes in each code block;
[0170] The dependency information of the node is composed based on the dependency information of each code block.
[0171] In a possible implementation, the device further includes an error information output module 307, and the error information output module 307 is used to:
[0172] In the event that an exception occurs in the generation of the abstract syntax tree of the source code, generating an exception report;
[0173] Outputting error information of the source code based on the exception report.
[0174] It should be noted that the device provided in the above embodiment, when implementing its functions, is only illustrated by the division of the above functional modules. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the device and method embodiments provided in the above embodiment belong to the same concept, and the specific implementation process is detailed in the method embodiment, which will not be repeated here.
[0175] An embodiment of the present application provides an electronic device, which includes a processor and a memory, wherein the memory stores at least one instruction or at least one program, and the at least one instruction or the at least one program is loaded and executed by the processor to implement any source code processing method provided in the above method embodiments.
[0176] The memory can be used to store software programs and modules. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, application programs required for functions, etc.; the data storage area can store data created according to the use of the device, etc. In addition, the memory can include a high-speed random access memory and can also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices. Accordingly, the memory can also include a memory controller to provide the processor with access to the memory.
[0177] An embodiment of the present application also provides a computer-readable storage medium, which can be set in an electronic device to store at least one instruction or at least one program related to implementing a source code processing method. The at least one instruction or the at least one program is loaded and executed by the processor to implement any source code processing method provided in the above method embodiments.
[0178] Optionally, in this embodiment, the above-mentioned storage medium may include but is not limited to: a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and other media that can store program codes.
[0179] It should be noted that the above-mentioned sequence of the embodiments of the present application is for description only and does not represent the advantages and disadvantages of the embodiments. The above-mentioned specific embodiments of this specification are described. Other embodiments are within the scope of the attached claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0180] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0181] A person skilled in the art will understand that all or part of the steps to implement the above embodiments may be accomplished by hardware or by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, and the above-mentioned storage medium may be a read-only memory, a disk or an optical disk, etc.
[0182] The above description is only a preferred embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application should be included in the protection scope of the present application.
Claims
1. A method for processing source code, characterized in that: The method comprises: Obtaining source code and configuration information of a project; the configuration information represents information required for the operation of the project; Performing syntax analysis on the source code based on the configuration information to generate an abstract syntax tree of the source code; Determine a target node of the associated function in the abstract syntax tree, perform parsing on the target node, and obtain a parsing result of the target node; the parsing result represents function information of the target node; Perform code block division based on the parsing result of the target node to obtain the code block division result of the target node, perform control flow parsing based on the code block division result of the target node to generate a control flow graph of the target node; Dependency analysis is performed based on the control flow graph of the target node to obtain dependency information of the target node.
2. The method for processing source code according to claim 1, characterized in that: The configuration information includes additional include directories, additional macro definitions, and a project path of the project. The additional include directories are header files that the project depends on, and the additional macro definitions are macros that the project depends on. The performing syntax analysis on the source code based on the configuration information to generate an abstract syntax tree of the source code includes: Generate compilation parameters according to the additional include directory, the additional macro definition and the project path of the project; A compiler is called based on the compilation parameters to perform syntax analysis on the source code to generate an abstract syntax tree of the source code.
3. The method for processing source code according to claim 1, characterized in that: Each node of the abstract syntax tree includes type information; and determining a target node of an associated function in the abstract syntax tree includes: Each node of the abstract syntax tree is traversed, and for a current node traversed, if the type information of the current node matches the function type, the node is determined to be the target node.
4. The method for processing source code according to claim 3, characterized in that: The performing parsing processing on the target node to obtain the parsing processing result of the target node includes: Performing statement analysis on the function represented by the target node to obtain the function name, function parameters, return value type and statement of the function; Based on the function name, function parameters, return value type and statement of the function, a parsing result for the target node is obtained.
5. The method for processing source code according to claim 4, characterized in that: The code block division is performed based on the parsing result of the target node to obtain the code block division result of the target node, including: According to the statements of the function in the parsing result of the target node, the function is divided into multiple code blocks; Determine that the multiple code blocks are code block division results of the target node.
6. The method for processing source code according to claim 5, characterized in that: The method further comprises: If the type information of the node matches the data type, the node is determined to be a type node, and type parsing is performed on the data type represented by the type node to obtain type information of the data type corresponding to the type node; Based on the type information of the data type corresponding to the type node, a type parsing result of the type node in each code block is determined.
7. The method for processing source code according to claim 6, characterized in that: The performing dependency analysis based on the control flow graph of the target node to obtain dependency information of the target node includes: Based on the control flow graph, extract the global variables, function call information and type dependency information referenced in each code block to obtain the dependency information of each code block; the type dependency information includes the type parsing results of the type nodes in each code block; The dependency information of the node is composed based on the dependency information of each code block.
8. The method for processing source code according to claim 1, characterized in that: The method further comprises: In the event that an exception occurs in the generation of the abstract syntax tree of the source code, generating an exception report; Outputting error information of the source code based on the exception report.
9. A source code processing device, characterized in that: The device comprises: An information acquisition module, used to acquire source code and configuration information of a project; the configuration information represents information required for the operation of the project; An abstract syntax tree generation module, configured to perform syntax analysis on the source code based on the configuration information to generate an abstract syntax tree of the source code; A parsing module, used to determine a target node of an associated function in the abstract syntax tree, perform parsing on the target node, and obtain a parsing result of the target node; the parsing result represents function information of the target node; A code block division module is used to perform code block division based on the parsing result of the target node, obtain the code block division result of the target node, perform control flow analysis based on the code block division result of the target node, and generate a control flow graph of the target node; The dependency information determination module is used to perform dependency analysis based on the control flow graph of the target node to obtain the dependency information of the target node.
10. An electronic device, characterized in that: It comprises a processor and a memory, wherein the memory stores at least one instruction or at least one program, and the at least one instruction or the at least one program is loaded and executed by the processor to implement the source code processing method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Redundant variable and redundant method detection method and system based on abstract syntax tree
CN112650680A
Fuzz drive program generation method and device, electronic equipment and storage medium
CN116893963A
Code analysis method and system and readable storage medium
CN117193852A