Source code processing method and device, and electronic device
By generating an abstract syntax tree and performing dependency analysis, dependency information in the code is automatically extracted, solving the difficulties of static code analysis tools in sorting out code dependencies and improving code portability efficiency and quality.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-19
- Publication Date
- 2026-04-10
AI Technical Summary
In existing technologies, static code analysis tools have difficulty in identifying code dependencies, resulting in low code portability efficiency and an inability to effectively extract dependency information from the code.
By combining static code analysis techniques and dependency information extraction methods, the dependencies in functional modules are automatically extracted by generating abstract syntax trees, parsing target nodes, dividing code blocks, and parsing control flow.
It improves the efficiency of code understanding and portability, helps to clarify the dependencies between code blocks, reduces module coupling, and simplifies code module migration and modification.
Smart Images

Figure CN119807010B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a source code processing method and device and electronic equipment. BACKGROUND
[0002] With the intelligent development of the automobile, the automobile software plays an important role in the use of the vehicle. Since the function modules of the automobile software need to be transplanted between different platforms, the dependency relationship between the function modules often needs to be sorted out for platform adaptation during the transplantation process. Determining the dependency relationship in each function through manual work can only achieve a half effect of double effort, and extracting the dependency relationship plays an indispensable role in understanding the code and code transplantation.
[0003] In the prior art, after the source code is developed, a static code analysis tool is usually used to perform software testing on the source code to ensure software quality, however, these static code analysis tools mainly 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 dependency relationship in the code. In order to improve the efficiency of code transplantation, how to extract the dependency information in the code during software testing on the source code becomes a problem to be solved. SUMMARY
[0004] In order to solve the problem of the prior art, the present application provides a source code processing method and device and electronic equipment, which automatically extracts the dependency relationship of each function in the function module by combining common static code analysis technology and dependency information extraction method, helps code understanding and code transplantation, and solves the problem of how to extract the dependency information in the code during software testing on the source code.
[0005] According to an aspect of the present application, a source code processing method is provided, which comprises:
[0006] Obtaining the source code and configuration information of a project; the configuration information represents information required for running the project;
[0007] Performing syntax analysis processing on the source code based on the configuration information to generate an abstract syntax tree of the source code;
[0008] Determining a target node of a related function in the abstract syntax tree, performing parsing processing on the target node to obtain a parsing processing result of the target node; the parsing processing result represents function information of the target node;
[0009] Performing code block division based on the parsing processing result of the target node to obtain a code block division result of the target node, and performing control flow analysis based on the code block division result of the target node to generate a control flow graph of the target node;
[0010] perform dependency analysis 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, the additional include directory is a header file depended by the project, and the additional macro definition is a macro depended by the project.
[0012] The syntax analysis processing of the source code based on the configuration information to generate an abstract syntax tree of the source code includes:
[0013] generating a compilation parameter according to the additional include directory, the additional macro definition, and the project path of the project;
[0014] performing syntax analysis of the source code based on the compilation parameter 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 the determination of the target node of the associated function in the abstract syntax tree includes:
[0016] traversing each node of the abstract syntax tree, and determining that the current node is the target node if the type information of the current node matches a function type.
[0017] In a possible implementation, the parsing processing of the target node to obtain a parsing processing result of the target node includes:
[0018] performing statement analysis on the function represented by the target node to obtain a function name, function parameters, a return value type, and a statement of the function.
[0019] obtaining the parsing processing result of the target node based on the function name, the function parameters, the return value type, and the statement of the function.
[0020] In a possible implementation, the code block division based on the parsing processing result of the target node to obtain a code block division result of the target node includes:
[0021] dividing the function into a plurality of code blocks according to the statement of the function in the parsing processing result of the target node.
[0022] determining that the plurality of code blocks are the code block division result 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 as a type node, type analysis is performed on the data type represented by the type node, and type information of the data type corresponding to the type node is obtained;
[0025] Based on the type information of the data type corresponding to the type node, a type analysis result of the type node in each code block is determined.
[0026] In a possible implementation, the 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, global variables, function call information and type dependency information referenced in each code block are extracted to obtain dependency information of each code block; the type dependency information includes a type analysis result of a type node in each code block;
[0028] The dependency information of each code block is used to form the dependency information of the node.
[0029] In a possible implementation, the method further includes:
[0030] In a case where generation of the abstract syntax tree of the source code is abnormal, an exception report is generated;
[0031] Error information of the source code is output based on the exception report.
[0032] In another aspect, a processing apparatus of a source code is provided, and the apparatus includes:
[0033] An information acquisition module is configured to acquire source code and configuration information of a project; the configuration information represents information required for running of the project;
[0034] An abstract syntax tree generation module is configured to perform syntax analysis processing on the source code based on the configuration information, and generate an abstract syntax tree of the source code;
[0035] An analysis processing module is configured to determine a target node of a related function in the abstract syntax tree, perform analysis processing on the target node, and obtain an analysis processing result of the target node; the analysis processing result represents function information of the target node;
[0036] A code block division module is configured to perform code block division based on the analysis processing result of the target node, obtain a 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] A dependency information determination module is configured to perform dependency analysis based on a control flow graph of the target node to obtain 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, the additional include directory is a header file depended by the project, and the additional macro definition is a macro depended by the project.
[0039] The abstract syntax tree generation module is configured to:
[0040] generate a compilation parameter according to the additional include directory, the additional macro definition, and the project path of the project;
[0041] invoke a compiler to perform syntax analysis on the source code based on the compilation parameter, and generate an abstract syntax tree of the source code.
[0042] In a possible implementation, each node of the abstract syntax tree includes type information, and the parsing processing module includes a target node determination unit configured to:
[0043] traverse each node of the abstract syntax tree, and determine that the current node is the target node if the type information of the current node matches a function type.
[0044] In a possible implementation, the parsing processing module further includes a function parsing unit configured to:
[0045] perform statement analysis on the function represented by the target node to obtain a function name, function parameters, a return value type, and statements of the function.
[0046] obtain a parsing processing result of the target node based on the function name, the function parameters, the return value type, and the statements of the function.
[0047] In a possible implementation, the code block division module is configured to:
[0048] divide the function into a plurality of code blocks according to the statements of the function in the parsing processing result of the target node.
[0049] determine the plurality of code blocks as a code block division result of the target node.
[0050] In a possible implementation, the apparatus further includes a type parsing module configured to:
[0051] If the type information of the node matches the data type, the node is determined as a type node, type analysis is performed on the data type represented by the type node, and type information of the data type corresponding to the type node is obtained;
[0052] Based on the type information of the data type corresponding to the type node, a type analysis result of the type node in each code block is determined.
[0053] In a possible implementation, the dependency information determination module is configured to:
[0054] Based on the control flow graph, global variables, function call information, and type dependency information referenced in each code block are extracted, and dependency information of each code block is obtained; the type dependency information includes a type analysis result of a type node 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 apparatus further includes an error information output module, configured to:
[0057] In a case where generation of the abstract syntax tree of the source code is abnormal, an exception report is generated;
[0058] Error information of the source code is output based on the exception report.
[0059] In another aspect, an electronic device is provided, including a processor and a memory, the memory storing at least one instruction or at least one program, the at least one instruction or the at least one program being loaded and executed by the processor to implement the source code processing method of any of the above aspects.
[0060] In another aspect, a computer-readable storage medium is provided, the computer-readable storage medium storing at least one instruction or at least one program, the at least one instruction or the at least one program being loaded and executed by a processor to implement the source code processing method of any of the above aspects.
[0061] In another aspect, a computer program product or computer program is provided, the computer program product or computer program including computer instructions 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 to cause the electronic device to perform the source code processing method of any of the above aspects.
[0062] The embodiment of the present application obtains source code and configuration information of a project; the configuration information represents information required for running the project; performs syntax analysis processing on the source code based on the configuration information, generates an abstract syntax tree of the source code; determines a target node of a correlation function in the abstract syntax tree, performs parsing processing on the target node, and obtains a parsing processing result of the target node; the parsing processing result represents function information of the target node; performs code block division based on the parsing processing result of the target node, obtains a code block division result of the target node, performs control flow analysis based on the code block division result of the target node, and generates a control flow graph of the target node; performs dependency analysis based on the control flow graph of the target node, and obtains dependency information of the target node. In combination with common static code analysis technology and dependency information extraction methods, the dependency relationship of each function in a functional module is automatically extracted, code understanding and code transplantation are facilitated, and the problem of how to extract dependency information in code when performing software testing on source code is solved. BRIEF DESCRIPTION OF DRAWINGS
[0063] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the embodiment description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor.
[0064] Figure 1 is a flow diagram of a source code processing method provided by the embodiment of the present application;
[0065] Figure 2 is a flow diagram of a source code processing method provided by the embodiment of the present application;
[0066] Figure 3 is a structural diagram of a source code processing device provided by the embodiment of the present application. DETAILED DESCRIPTION
[0067] The technical solutions in the embodiments of the present application will be described clearly and completely in combination with the drawings in the embodiments of the present application. Obviously, the described embodiments are only some embodiments of the present application, not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor are within the scope of protection of the present application.
[0068] Automobile software often needs to adapt to different target platforms, so it often needs to sort out the dependency relationship between function modules for multiple times to adapt to the platform. The embodiment is mainly applied to the transplantation of function modules in the operating system between different platforms. The global variable usage, function call relationship and type dependency relationship in each function in a certain specific function module are extracted by Clang (Clang is a lightweight compiler for C language, C++, and Objective-C language), and a complete function module is divided into multiple "particles". When code transplantation is needed, the extracted "particles" can be used as a unit to modify the code as a whole.
[0069] In recent years, automobile software has developed rapidly, and the complexity of the corresponding software has also increased day by day. Through manual determination of the dependency relationship in each function, only half the work can be done twice, and the extraction of the dependency relationship plays an indispensable role in understanding the code and code transplantation. The embodiment aims to replace manual inspection, divide a complete function module into multiple units through the Clang compilation tool, and help with 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, so it is crucial to capture and properly handle these defects during software source code development. Traditional software testing techniques are performed after the source code is basically completed and before it is officially delivered, which 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, and has been widely used in the industry, becoming an important means to ensure software quality.
[0071] Existing static code analysis tools mostly focus on basic analysis, i.e. 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 speculate possible faults that may occur during actual program execution, and report possible defects in the code, such as memory leaks and array out-of-bound access, further locate the error statement position, and check the overall structure of the program. Although some analysis tools also have symbolic execution, constraint solving, type derivation, etc. functions, the focus of the analysis is still defect checking, and the dependency relationship in the code is not analyzed, so the help for code understanding and code transplantation is limited.
[0072] The embodiment of the present application provides a static analysis method, which not only contains common functions of common code static analysis tools (type analysis, control flow analysis, etc.), but also extracts global variable use, function call relationship and type dependency relationship in a function, and assists in understanding code and code porting between different target platforms.
[0073] The embodiment of the present application provides a source code processing method, which combines common static code analysis technology and dependency information extraction method, automatically extracts dependency relationships of each function in a function module, and helps code understanding and code porting. Referring to Figure 1 , the source code processing method comprises steps S101 to S109.
[0074] In step S101, source code and configuration information of a project are acquired; the configuration information represents information required by the project to run.
[0075] In a possible implementation manner, the project represents a target of implementing a certain specific function. Specifically, the specific function can be an auxiliary driving function, a voice control function or a communication function, etc.
[0076] In a possible implementation manner, the source code is used for implementing the function of the project. Specifically, the source code can be C language code, that is, C code.
[0077] In a possible implementation manner, the configuration information of the project is acquired by parsing a configuration file, and the configuration file is a file containing information required by a successful operation program.
[0078] In a possible implementation manner, the configuration information represents information required by the project to run. Specifically, the configuration information can be set manually.
[0079] In a possible implementation manner, the configuration information comprises an additional include directory, an additional macro definition and a project path of the project.
[0080] In a possible implementation manner, the additional include directory is an additional include path, that is, an include path used for the current project.
[0081] In a possible implementation manner, the additional macro definition is a self-defined macro, which is used for simplifying a configuration item when a configuration include directory or an added dependency is configured.
[0082] In a possible implementation manner, the project path of the project is a project root directory, that is, a top-level directory selected for the entire project in a file system. Specifically, the project root directory usually contains all contents of the project, including source code, a build configuration file, a document, a test file, etc.
[0083] In step S103, the source code is parsed 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, the additional include directory is a header file dependent on the project, and the additional macro definition is a macro dependent on the project.
[0085] The parsing of the source code based on the configuration information to generate an abstract syntax tree of the source code includes:
[0086] The compilation parameters are generated according to the additional include directory, the additional macro definition, and the project path of the project.
[0087] The compiler is called based on the compilation parameters to parse the source code and generate an abstract syntax tree of the source code.
[0088] In a possible implementation, the compilation parameters required by Clang / clang are generated according to the configuration information in the 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 languages. The abstract syntax tree corresponding to the source code is generated by using the Clang compiler to perform lexical analysis and syntax analysis.
[0090] In a possible implementation, the AST of the source code is an abstract syntax structure of the source code, which represents the syntax structure of a language in the form of a tree. Each node on the tree stores 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, one is a declaration, one is a statement, and the other is a type. Among them, the node with type information of a function declaration is confirmed as a function node, and the node with type information of a data type declaration is confirmed as a type node.
[0092] In a possible implementation, a function node usually includes a statement node, and the statement node can further nest other declaration nodes, such as a variable declaration node, and can also include an expression. A type node in C generally only includes a type node as a child node.
[0093] In a possible implementation, nodes in the AST of the source code are traversed, in a case where a node is a function node, the function node is parsed accordingly, and in a case where a node is a type node, the type node is parsed accordingly.
[0094] In this implementation, the compilation parameter is generated based on the configuration information, the compiler is invoked based on the compilation parameter to perform syntax analysis on the source code, and the abstract syntax tree of the source code is generated, so that the structure of the code is understood, and the efficiency of understanding the code is improved.
[0095] In a possible implementation, the method further includes:
[0096] In a case where the generation of the abstract syntax tree of the source code is abnormal, an exception report is generated;
[0097] Error information of the source code is output based on the exception report.
[0098] In a possible implementation, when an error occurs in the process of generating the AST, detailed error information in the source code is output.
[0099] In a possible implementation, the error information of the source code is viewed, and defects in the source code are captured and processed.
[0100] In this implementation, if an error occurs when the AST is generated, the error information is output, the defects of the source code are located, the defects of the source code are modified, and the quality of the code is improved.
[0101] In step S105, a target node associated with a 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; the target node associated with the function in the abstract syntax tree is determined, including:
[0103] Each node of the abstract syntax tree is traversed, and for a current node traversed, if type information of the current node matches a function type, the node is determined to be the target node.
[0104] In a possible implementation, the target node is parsed to obtain a parsing result of the target node, including:
[0105] The function represented by the target node is analyzed to obtain a function name, function parameters, a return value type, and a statement of the function.
[0106] obtaining a parsing processing result of the target node based on the function name, the function parameters, the return value type and the statement of the function.
[0107] In a possible implementation, each node of the abstract syntax tree is traversed, and whether the current node is the target node is determined according to type information saved by the current node.
[0108] In a possible implementation, the type information is determined to be a function declaration node, and the function declaration 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, function analysis is performed on the function corresponding to the function node, and the function name, the parameter list, the return value information, the global variable referenced in the function and the data type used in the function are extracted as the parsing processing result of the target node. Specifically, the data type used in the function can be a structure.
[0110] In a possible implementation, the function node usually includes a statement node, and the statement node can further nest other declaration nodes such as a variable declaration node, and can further include an expression. The parsing processing result of the target node further includes a statement in the function.
[0111] In this implementation, each node of the abstract syntax tree is traversed, for the current node traversed, the target node is determined based on the type information of the current node, the target node is parsed to obtain the parsing processing result of the target node, and the source code is statically analyzed to ensure the quality of the source code and facilitate subsequent code granulation according to the statement branch of the function.
[0112] In step S107, code block division is performed based on the parsing processing result of the target node to obtain a 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, the code block division is performed based on the parsing processing result of the target node to obtain the code block division result of the target node, including:
[0114] The function is divided into a plurality of code blocks according to the statement of the function in the parsing processing result of the target node;
[0115] The plurality of code blocks are determined to be the code block division result of the target node.
[0116] In a possible implementation, the statement of the function in the analysis processing result of the target node can include at least one of a sequential statement, a swich statement, an if statement, an until statement, and a while statement.
[0117] In a possible implementation, a control flow analysis module of a compiler is invoked to cut each function into multiple code blocks according to branch conditions of statements, and a control flow graph of each function is generated.
[0118] In a possible implementation, a control flow graph (CFG) is also called a control flow chart, is an abstract representation of a process or program, is an abstract data structure used in a compiler, is internally maintained by the compiler, and represents all paths traversed in the execution process of a program. It represents possible flow directions of all basic blocks in a process in the form of a graph, and can also reflect the real-time execution process of a process.
[0119] In a possible implementation, the control flow graph includes an entry (first basic block) and an exit (last basic block) of a process, all possible next basic blocks (all exits) of a basic block, all possible previous basic blocks (all entries) of a basic block, and a statement table corresponding to a basic block. Specifically, the control flow graph can display the mutual relationship between the basic blocks in a process, the dynamic execution state, and the statement table corresponding to each basic block. In addition, there are other auxiliary information, such as the execution times and execution times of each basic block.
[0120] In this implementation, code block division is performed based on the analysis processing result of the target node to obtain a 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. The control flow graph can directly display the structure of the source code, and through the representation of the nodes and edges of the control flow graph, the input, output, and transfer relationship between the basic blocks of the program can be clearly seen, the code logic is simplified, and the analysis efficiency of the source code is improved.
[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, it is determined that the node is a type node, type analysis is performed on the data type represented by the type node, and type information of the data type corresponding to the type node is obtained.
[0124] Based on type information of a data type corresponding to the type node, a type resolution result of the type node in each code block is determined.
[0125] In a possible implementation, the type information is a node declared by a data type, and the node is confirmed as a type node.
[0126] In a possible implementation, the data type corresponding to the type node includes a typedef (a declared type) and a custom struct (a structure type), union (a union type) and enum (an enumeration type), and when the data type corresponding to the type node is nested, nested data types are extracted in sequence until the built-in data type is a basic data type. Specifically, the basic data type includes an integer type (int), a floating point type (float, double), a character type (char) and a Boolean type (bool).
[0127] In a possible implementation, in a case where the data type of the type node is a structure, a structure name, members in the structure and data types of the members in the structure are extracted, and the data types of the members can be other structures, in which case the extraction is continued until the basic data type.
[0128] In a possible implementation, in a case where the data type of the type node is a structure, a structure name, members in the structure and data types of the members in the structure are extracted, and the data types of the members can be other structures, in which case the extraction is continued until the basic data type.
[0129] In a possible implementation, the data type corresponding to the type node is type-resolved, and a type name, a member name and a data type of the member of the data type are extracted as the type information of the data type corresponding to the type node.
[0130] In a possible implementation, after the type node is resolved, type information of the data type corresponding to all type nodes in each code block is obtained, and a type resolution result of the type node in each code block is obtained.
[0131] In this implementation, after the type node is type-resolved, the type information of the data type corresponding to the type node is obtained, which helps to find 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 resolution result of the type node in each code block is determined, the type information makes the code more readable, reduces the type checking and conversion overhead at runtime, and speeds up the understanding of the use of variables and functions.
[0132] In a possible implementation, the dependency analysis based on the control flow graph of the target node obtains dependency information of the target node, including:
[0133] Based on the control flow graph, global variables, function call information and type dependency information referenced in each code block are extracted to obtain dependency information of each code block; the type dependency information includes type resolution results of 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, and global variables, function call information and type dependency information referenced in each code block are extracted to obtain dependency information of each code block.
[0136] In a possible implementation, based on the dependency information of each code block corresponding to the node, complete dependency relationships of a function corresponding to the node are obtained, that is, the dependency information of the node.
[0137] In a possible implementation, after obtaining the dependency information of the target node, a file position where referenced content is located can be saved and corresponding source code can be extracted based on the dependency information to obtain granular code. Specifically, the referenced content actually refers to content of dependency represented by the dependency information.
[0138] In a possible implementation, assuming that a function f exists in a file A, the function f references a type t in a file B, the file position where the referenced content is located is saved and corresponding source code is extracted, this step can obtain a position of the type t in the file B, such as a line number and a column number, and then a part of source code in which the type t is declared is extracted, so that the function f and the type t can be taken out separately, and if it is intended to use the function f in other projects, the function f and the type t can be extracted and then migrated integrally without the need of other modifications.
[0139] In this implementation, based on the control flow graph, 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 for describing mutual dependency relationships between various modules, libraries and files, the dependency information can help to clarify relationships between code blocks. Through the clear dependency relationships, coupling degrees between modules can be reduced, each module can be more independent, redundant information of code is reduced, and individual migration and modification of code modules are facilitated.
[0140] The beneficial effects of the present application are: by acquiring source code and configuration information of a project; the configuration information represents information required for running the project; based on the configuration information, the source code is processed by syntax analysis to generate an abstract syntax tree of the source code; a target node of a correlation function in the abstract syntax tree is determined, the target node is processed by analysis to obtain an analysis result of the target node; the analysis result represents function information of the target node; based on the analysis result of the target node, code block division is performed to obtain a code block division result of the target node, based on the code block division result of the target node, control flow analysis is performed to generate a control flow graph of the target node; based on the control flow graph of the target node, dependency analysis is performed to obtain dependency information of the target node. By combining common static code analysis technology and dependency information extraction method, the dependency relationship of each function in a functional module is automatically extracted, which helps code understanding and code porting, and solves the problem of how to extract dependency information in the code when software testing is performed on the source code.
[0141] Figure 2 A flowchart of a source code processing method provided by an embodiment of the present application is shown. Referring to Figure 2 , the source code processing method comprises:
[0142] generating a compilation parameter required for calling clang according to configuration information in a configuration file, and generating an AST of the current project;
[0143] when an error occurs in the process of generating the AST, transmitting the error information to a diagnosis information processing module, and outputting detailed error information in the source code;
[0144] traversing the AST and matching nodes therein, when a function node and a type node are matched, respectively calling a function analysis module and a type analysis module for processing;
[0145] after the function analysis is completed, calling a control flow analysis module, cutting each function into multiple code blocks according to branch conditions, and generating a control flow graph of the function;
[0146] traversing the control flow graph, extracting global variables, function calls and type dependency information in each code block, saving file positions of the referenced contents and extracting corresponding source code.
[0147] Figure 3 A structural schematic diagram of a source code processing device 300 provided by an embodiment of the present application is shown, the device has a function of implementing the source code processing method in the above method embodiment, and the function can be implemented by hardware or corresponding software executed by hardware. As Figure 3 shown, the device can comprise:
[0148] The information obtaining module 301 is configured to obtain source code and configuration information of a project; the configuration information represents information required for running of the project.
[0149] The abstract syntax tree generating module 302 is configured to perform syntax analysis processing on the source code based on the configuration information, and generate an abstract syntax tree of the source code.
[0150] The parsing processing module 303 is configured to determine a target node of a correlation function in the abstract syntax tree, perform parsing processing on the target node, and obtain a parsing processing result of the target node; the parsing processing result represents function information of the target node.
[0151] The code block dividing module 304 is configured to perform code block division based on the parsing processing result of the target node, obtain a 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 determining module 305 is configured to perform dependency analysis based on the control flow graph of the target node, and obtain 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; the additional include directory is a header file dependent on the project, and the additional macro definition is a macro dependent on the project.
[0154] The abstract syntax tree generating module 302 is configured to:
[0155] generate a compilation parameter according to the additional include directory, the additional macro definition, and the project path of the project;
[0156] invoke a compiler to perform syntax analysis on the source code based on the compilation parameter, and 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 determining unit, which is configured to:
[0158] traverse each node of the abstract syntax tree, and for a current node that is traversed, if type information of the current node matches a function type, determine that the node is the target node.
[0159] In a possible implementation, the parsing processing module 303 further includes a function parsing unit, which is configured to:
[0160] performing sentence analysis on the function represented by the target node to obtain a function name, function parameters, a return value type and a statement of the function;
[0161] obtaining a parsing processing result of the target node based on the function name, the function parameters, the return value type and the statement of the function.
[0162] In a possible implementation, the code block division module 304 is configured to:
[0163] divide the function into a plurality of code blocks according to the statement of the function in the parsing processing result of the target node;
[0164] determine the plurality of code blocks as a code block division result of the target node.
[0165] In a possible implementation, the apparatus further includes a type resolution module 306, which is configured to:
[0166] if the type information of the node matches the data type, determine that the node is a type node, perform type resolution on a data type represented by the type node to obtain type information of the data type corresponding to the type node;
[0167] determine a type resolution result of a type node in each code block based on the type information of the data type corresponding to the type node.
[0168] In a possible implementation, the dependency information determination module 305 is configured to:
[0169] extract global variables, function call information and type dependency information referenced in each code block based on the control flow graph to obtain dependency information of each code block; the type dependency information includes a type resolution result of a type node in each code block;
[0170] compose the dependency information of the node based on the dependency information of each code block.
[0171] In a possible implementation, the apparatus further includes an error information output module 307, which is configured to:
[0172] generate an exception report in a case where generation of the abstract syntax tree of the source code is abnormal;
[0173] output error information of the source code based on the exception report.
[0174] It should be noted that the apparatus provided by the above-mentioned embodiments, when realizing its functions, only takes the above-mentioned division of each functional module as an example, and in actual application, the above-mentioned functions can be completed by different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to complete all or part of the above-described functions. In addition, the apparatus and method embodiments provided by the above-mentioned embodiments belong to the same concept, and the specific implementation process is detailed in the method embodiments, which will not be repeated here.
[0175] The electronic device provided by the embodiment of the present application includes a processor and a memory, and the memory stores at least one instruction or at least one program, which is loaded and executed by the processor to realize any one of the source code processing methods provided by the above-mentioned method embodiments.
[0176] The memory can be used to store software programs and modules, and 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 by 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 magnetic disk storage device, a flash memory device, or other volatile solid-state memory device. Accordingly, the memory can also include a memory controller to provide access of the processor to the memory.
[0177] The embodiment of the present application also provides a computer readable storage medium, which can be arranged in an electronic device to save at least one instruction or at least one program for realizing a source code processing method, and the at least one instruction or the at least one program is loaded and executed by the processor to realize any one of the source code processing methods provided by the above-mentioned method embodiments.
[0178] Optionally, in the embodiment, the above-mentioned storage medium can include but is not limited to: a U disk, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and various media that can store program codes.
[0179] It should be noted that the above-mentioned order of the embodiments of the present application is only for description, and does not represent the advantages and disadvantages of the embodiments. And the above describes the specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in a different order than the order in which they are recited and still achieve desirable results. In addition, the processes depicted in the figures do not necessarily require the particular order shown, or sequential order, to achieve the desired results. In certain implementations, multitasking and parallel processing can be advantageous or necessary.
[0180] Each of the embodiments in the specification is described in a progressive manner, and the same or similar parts between each embodiment can be referred to each other. Each embodiment focuses on the difference from other embodiments. In particular, for the device embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the part of the method embodiments.
[0181] A person of ordinary skill in the art can understand that all or part of the steps of the above-mentioned embodiments can be completed by hardware, or by program instructing relevant hardware to complete, and the program can be stored in a computer readable storage medium. The storage medium mentioned above can be a read-only memory, a magnetic disk or an optical disk.
[0182] The above only describes the preferred embodiments of the present application and does not limit the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
Claims
1. A method of processing source code, characterized by, The method comprises: obtaining source code and configuration information of a project; the configuration information represents information required for running the project; performing syntax analysis processing on the source code based on the configuration information to generate an abstract syntax tree of the source code; determining a target node of a related function in the abstract syntax tree, performing parsing processing on the target node to obtain a parsing processing result of the target node; the parsing processing result represents function information of the target node; performing code block division based on the parsing processing result of the target node to obtain a code block division result of the target node, and performing control flow analysis based on the code block division result of the target node to generate a control flow graph of the target node; performing dependency analysis based on the control flow graph of the target node to obtain dependency information of the target node; the dependency information includes global variables, function call information, and type dependency information referenced by a function corresponding to the target node; performing recursive type analysis on type nodes in the type dependency information until type definition information of a basic data type is obtained; based on the dependency information, locating and extracting source code of the dependent global variables, called functions, and type definitions to form a code unit that can be independently migrated.
2. The method of claim 1, wherein, The configuration information includes an additional include directory, an additional macro definition, and a project path of the project, the additional include directory is a header file dependent on the project, the additional macro definition is a macro dependent on the project, and the syntax analysis processing on the source code based on the configuration information to generate the abstract syntax tree of the source code comprises: generating a compilation parameter according to the additional include directory, the additional macro definition, and the project path of the project; calling a compiler to perform syntax analysis on the source code based on the compilation parameter to generate the abstract syntax tree of the source code.
3. The method of claim 1, wherein, Each node of the abstract syntax tree includes type information; the determination of the target node of the related function in the abstract syntax tree comprises: traversing each node of the abstract syntax tree, and for a current node traversed, if the type information of the current node matches a function type, determining that the node is the target node.
4. The method of claim 3, wherein, The parsing processing on the target node to obtain the parsing processing result of the target node comprises: performing statement analysis on a function represented by the target node to obtain a function name, function parameters, a return value type, and a statement of the function; obtaining the parsing processing result of the target node based on the function name, the function parameters, the return value type, and the statement of the function.
5. The method of claim 4, wherein, The code block division based on the parsing processing result of the target node to obtain the code block division result of the target node comprises: dividing the function into a plurality of code blocks according to the statement of the function in the parsing processing result of the target node; determining that the plurality of code blocks are the code block division result of the target node.
6. The method of claim 5, wherein, The method further comprises: If the type information of the node matches the data type, the node is determined as a type node, type analysis is performed on the data type represented by the type node, and type information of the data type corresponding to the type node is obtained; Based on the type information of the data type corresponding to the type node, a type analysis result of the type node in each code block is determined.
7. The method of claim 6, wherein, The dependency information of the target node is obtained based on the control flow graph of the target node, including: Based on the control flow graph, global variables, function call information, and type dependency information referenced in each code block are extracted to obtain the dependency information of each code block; the type dependency information includes the type analysis result of the type node in each code block; The dependency information of the node is composed based on the dependency information of each code block.
8. The method of claim 1, wherein, The method further includes: In the case of an exception in the generation of the abstract syntax tree of the source code, an exception report is generated; Error information of the source code is output based on the exception report.
9. A processing apparatus of a source code, characterized by, The apparatus includes: An information acquisition module configured to acquire source code and configuration information of a project; the configuration information represents information required for running the project; An abstract syntax tree generation module configured to perform syntax analysis processing on the source code based on the configuration information, and generate an abstract syntax tree of the source code; An analysis processing module configured to determine a target node of a related function in the abstract syntax tree, perform analysis processing on the target node, and obtain an analysis processing result of the target node; the analysis processing result represents function information of the target node; A code block division module configured to perform code block division based on the analysis processing result of the target node, obtain a code block division result of the target node, and 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; A dependency information determination module configured to perform dependency analysis based on the control flow graph of the target node, and obtain dependency information of the target node; the dependency information includes global variables, function call information, and type dependency information referenced by the function corresponding to the target node; The apparatus is further configured to: For the type node in the type dependency information, recursive type analysis is performed until type definition information of a basic data type is obtained; Based on the dependency information, a global variable, a called function, and source code of a type definition that are dependent are located and extracted to form a code unit that can be independently migrated.
10. An electronic device, comprising: A processor and a memory are included, the memory stores at least one instruction or at least one program, 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 in any one of claims 1 to 8.
Citation Information
Patent Citations
Fuzz drive program generation method and device, electronic equipment and storage medium
CN116893963A