Function Declaration-oriented code intelligent comparison and incremental update method and system
By generating an abstract syntax tree and using a collision-resistant hash algorithm to compare function declarations, the problem of the inability to accurately identify function declaration changes in existing technologies is solved, efficient incremental updates are achieved, and the efficiency and reliability of code upgrades are improved.
Patent Information
- Application Number
- CN202510984865.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-17
- Publication Date
- 2025-09-16
- Estimated Expiration
- 2045-07-17
AI Technical Summary
Existing technologies cannot accurately identify structural changes in function declarations in code version management, which makes it easy to miss key logic or mistakenly modify unchanged code during the update process, making it difficult to meet the efficient update requirements in complex software development scenarios.
Use grammar parsing to generate an abstract syntax tree, use a collision-resistant hash algorithm to compare the function declaration set, mark changes and perform difference analysis, formulate an incremental update strategy, and retain unchanged code to the greatest extent possible.
It achieves accurate identification of function declaration changes and efficient incremental updates, reduces update costs and risks, and improves code upgrade efficiency and reliability.
Smart Images

Figure CN120491978B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of data analysis technology, and more specifically, provides a method and system for intelligent code comparison and incremental update for Function Declaration. Background Art
[0002] Code version management and updates are critical aspects of software development. Traditional code comparison methods, typically based on line-by-line text comparison, cannot accurately identify structural changes in function declarations, leading to the omission of key logic or the accidental modification of unchanged code during the update process. Existing tools such as diff-based merging only provide text-level diffs and lack an understanding of function declaration semantics, making it difficult to meet the requirements for efficient updates in complex software development scenarios. Therefore, a method that can accurately identify function declaration changes and implement incremental updates is urgently needed. Summary of the Invention
[0003] To address the shortcomings of the existing technology, the present invention proposes a method and system for intelligent code comparison and incremental update for Function Declaration. It performs syntax parsing on code files of different versions, constructs an abstract syntax tree, traverses the abstract syntax tree to extract a set of function declarations, uses an anti-collision hash algorithm to compare the set of function declarations, marks changed function declarations, and performs difference analysis. Based on the difference analysis results, the system automatically formulates and executes an incremental update strategy to retain unchanged code to the greatest extent possible, thereby improving the efficiency and reliability of code upgrades.
[0004] To achieve the above object, the present invention provides the following technical solutions:
[0005] Function Declaration-oriented code intelligent comparison and incremental update method, including:
[0006] S1: Use the syntax parser corresponding to the programming language to parse the first version code file and the second version code file to be compared, and generate their respective abstract syntax trees;
[0007] S2: traverse the abstract syntax tree, extract function declaration information, and store the extracted function declaration information in a data structure to form a function declaration set corresponding to the first version and the second version respectively;
[0008] S3: Use a collision-resistant hash algorithm to compare the function declaration information in the two versions of the function declaration set, mark the changed function declarations, and record the changed content and its location in the code;
[0009] S4: Perform difference analysis based on the marked function declaration changes and recorded change information, and formulate a code update strategy based on the difference analysis results;
[0010] S5: Perform an incremental update operation on the first version of the code according to the code update strategy.
[0011] Specifically, the specific steps of S1 include:
[0012] S1.1: Use the file stream to read the original code in the first version code file and the second version code file byte by byte, and preprocess them to obtain independent preprocessed character streams for each version;
[0013] S1.2: Initialize a finite state machine-based lexical analyzer and define a set of token types. Simultaneously, scan the preprocessed character stream for each version according to the lexical rules in the lexical analyzer to generate a sequence of tokens with type tags and attribute values for each version, denoted as the first token sequence and the second token sequence. Token types include identifying keywords, identifiers, operators, constants, and delimiters.
[0014] S1.3: Load the context-free grammar rules of the target language, construct an LR parsing table, and use the LR parsing algorithm to construct intermediate parse trees based on the context-free grammar rules, combining the first token sequence and the second token sequence, respectively, to obtain a first version of the intermediate parse tree and a second version of the intermediate parse tree; each internal node in the intermediate parse tree represents a non-terminal symbol, and the leaf node is a token;
[0015] S1.4: Define a hierarchical symbol table data structure; the hierarchical structure includes three levels: global scope, function scope, and block scope;
[0016] S1.5: Traverse the intermediate parse tree of each version and use the hierarchical symbol table to record the definition information of variables or functions according to the scope level. At the same time, assign a unique identifier to each symbol and associate the type information with the symbol table entry, obtaining the symbol table after the first version is filled and the symbol table after the second version is filled;
[0017] S1.6: Perform a static semantic check based on the populated symbol table for each version to check whether the function call parameters match the definition. If they do, perform constant folding optimization, record semantic error information, and generate a semantically marked intermediate parse tree for the first version and a semantically marked intermediate parse tree for the second version. The semantic error information includes undefined variables and type mismatches. The check is performed by searching the scope chain of the symbol table.
[0018] S1.7: Extract the core semantic nodes from the semantically tagged intermediate parse tree for each version. Create abstract syntax tree nodes and establish parent-child relationships based on the defined hierarchical symbol table data structure. Inject the type and scope metadata from the populated symbol table for each version. By standardizing the equivalent syntax structure, form a final abstract syntax tree independent of each version.
[0019] S1.8: Convert the final abstract syntax tree for each version into a storable serialized format.
[0020] Specifically, the specific steps of S2 include:
[0021] S2.1: Read the first version serialized abstract syntax tree and the second version serialized abstract syntax tree, and perform a deserialization operation to obtain the first version abstract syntax tree and the second version abstract syntax tree;
[0022] S2.2: Define functions to declare data structures and create two empty hash tables and ;
[0023] S2.3: Use a depth-first search method to traverse each node of the first version of the abstract syntax tree starting from the root node and determine whether it is a function node;
[0024] If the current node is a function node, extract the function declaration information in the first version of the abstract syntax tree, generate a function signature based on the function declaration information in the extracted first version of the abstract syntax tree, and use the function signature as the unique key and the function declaration information as the storage value. , obtaining a first version of the function declaration set; the function declaration information at least includes a function name, a parameter list, and a return value type; the function signature is a string composed of the function declaration information;
[0025] If the current node is not a function node, recursively traverse the child nodes of the current node;
[0026] S2.4: Repeat the process of S2.3 to traverse the second version of the abstract syntax tree, and store the extracted function declaration information and the corresponding function signature in the form of key-value pairs. , get the second version of the function declaration set.
[0027] Specifically, the specific steps of S3 include:
[0028] S3.1: Get the function declaration set of the first version and the function declaration set of the second version, and establish an index mapping from function name to function declaration to obtain the function declaration set of the first version after index optimization. and the second version of the function declaration set ;
[0029] S3.2: Use a collision-resistant hash algorithm and Perform hash calculation on each function declaration in the , and construct mappings from hash values to function declarations to form a first hash mapping table and a second hash mapping table;
[0030] S3.3: Construct two hash value sets based on the first hash mapping table and the second hash mapping table and ; The hash value set Contains all hash values in the first hash mapping table; the hash value set Contains all hash values in the second hash map;
[0031] S3.4: Initialize a pool to be analyzed;
[0032] for ,like , then take out the corresponding function declaration from the first hash mapping table, mark it as coming from the first version code file, and store it in the pool to be analyzed, where h represents the hash value; taking out means removing;
[0033] for ,like , then take out the corresponding function declaration from the second hash mapping table, mark it as coming from the second version code file, and store it in the pool to be analyzed.
[0034] Specifically, the specific steps of S3 further include:
[0035] S3.5: Divide the function declarations in the pool to be analyzed into two subsets according to the source version, and obtain the first pool to be analyzed and the second pool to be analyzed ;
[0036] S3.6: Initialize a change record list and traverse the first pool to be analyzed Each function declaration in , and in the second pool to be analyzed Find and Function declaration with the same name;
[0037] If in the second pool to be analyzed There exists Function declarations with the same name , then the function declaration is retrieved synchronously and function declarations ; The function declaration is removed In the first pool to be analyzed Remove ; Remove the function declaration In the second pool to be analyzed Remove ;
[0038] S3.7: Comparison and Parameter list and return value type;
[0039] If the parameter list and return value types are exactly the same, record the change type as move and record and The location information includes the file path and the starting and ending line numbers;
[0040] If the parameter list and return value types are not exactly the same, the change type is recorded as modification, and the signature difference information and location information are recorded, and the record is added to the change record list; the signature difference information includes parameter list changes and return value change information;
[0041] S3.8: Search and compare the results based on the parameter list and return value type to obtain the first pool to be analyzed after screening and the second pool to be analyzed .
[0042] Specifically, the specific steps of S3 further include:
[0043] S3.9: Initialize a renamed record list and traverse the first pool to be analyzed after screening Each function declaration in ,calculate The second pool to be analyzed after screening Each function declaration in The signature similarity of
[0044] If the signature similarity is greater than the preset similarity threshold, it is marked as renamed and modified, and the marked record is written into the renamed record list. from Remove from Removed;
[0045] If the signature similarity is less than or equal to the preset similarity threshold, The remaining function declarations in are marked for deletion, The remaining function declarations in are marked as new and added to the change log list;
[0046] S3.10: Summarize the change record list and the rename record list and generate a change report; the change report includes the location information, change type, change content and function name of each changed function declaration in the first version code file and the second version code file.
[0047] Specifically, the specific steps of S4 include:
[0048] S4.1: Obtain change reports and classify changes based on the change reports;
[0049] S4.2: Develop a code update strategy for each change type. The code update strategy includes:
[0050] If the function name is modified, find the original function call location in the first version code file and replace it with the new function name;
[0051] If the parameter list changes, check the parameter passing at the function call and make adjustments based on the addition or deletion of parameters, including:
[0052] For parameter addition, add the new parameter in the function declaration and add the corresponding actual parameter at all call points that call the function and need to provide the new parameter value;
[0053] For parameter deletion, delete the corresponding parameter in the function declaration and remove the corresponding actual argument at all call sites of the function;
[0054] If the parameter type or return value type changes, the type information in the function declaration is updated, and the actual parameter or return value processing logic of the function call location is updated according to the type compatibility check or forced conversion requirements.
[0055] Function Declaration-oriented code intelligent comparison and incremental update system, including: code parsing module, declaration extraction module, declaration comparison module, update strategy formulation module, incremental update execution module;
[0056] The code parsing module is used to convert the first version code file and the second version code file into an abstract syntax tree;
[0057] The declaration extraction module is used to extract function declaration information from the abstract syntax tree and construct a structured storage set;
[0058] The declaration comparison module identifies function declaration changes and generates a change report based on anti-collision hashing and multi-dimensional comparison logic;
[0059] The update strategy formulation module plans code update logic for different change types based on the change report;
[0060] The incremental update execution module completes the incremental update of the first version code according to the landing update strategy.
[0061] Specifically, the statement comparison module includes: a hash mapping unit, a pool to be analyzed construction unit, a change analysis unit, and a report generation unit;
[0062] The hash mapping unit calculates the function declaration hash value using a collision-resistant hash algorithm, constructs a hash value-function declaration mapping, and extracts a hash value set;
[0063] The to-be-analyzed pool construction unit is configured to compare two hash value sets, mark function declarations corresponding to hash values that exist only in a single set, and store the marked versions in the to-be-analyzed pool;
[0064] The change analysis unit is used to split the pool to be analyzed by version, traverse and find function declarations with the same name, compare parameter lists and return value types, distinguish between moves and modifications, record differences and location information, and at the same time, calculate signature similarity to identify renames, additions, and deletions, mark them, and update the pool to be analyzed;
[0065] The report generating unit is used to integrate the change information and output a change report.
[0066] Compared with the prior art, the present invention has the following beneficial effects:
[0067] 1. This invention proposes an intelligent code comparison and incremental update system for Function Declaration, and optimizes and improves its architecture, operation steps, and process. The system has the advantages of simple process, low investment and operation costs, and low production work costs.
[0068] 2. This paper proposes an intelligent code comparison and incremental update method for Function Declarations. By using a syntax parser to generate an abstract syntax tree, this method parses the code in a structured manner, breaking away from the limitations of text comparison and accurately extracting function declaration information. By using a collision-resistant hash algorithm to compare function declarations, this method can efficiently and accurately identify changes, deeply explore code differences at the syntactic and semantic levels, and improve the accuracy and comprehensiveness of code difference identification.
[0069] 3. This paper proposes an intelligent code comparison and incremental update method for Function Declarations. Based on precise difference analysis, it formulates an update strategy and implements incremental updates of the first version of the code. Compared with full replacement, this method reduces update costs and risks, eliminates unnecessary code changes, and improves update efficiency. Whether it is version evolution during code iteration or code maintenance for complex projects, this method can quickly locate function-level changes, reasonably update code, and ensure the smoothness and stability of code iteration. BRIEF DESCRIPTION OF THE DRAWINGS
[0070] Figure 1Schematic diagram of the intelligent code comparison and incremental update method for Function Declaration of the present invention;
[0071] Figure 2 Constructing a flow chart for the abstract syntax tree of the present invention;
[0072] Figure 3 Generate a flow chart for the invention change report;
[0073] Figure 4 This is the architecture diagram of the intelligent code comparison and incremental update system for Function Declaration in the present invention. DETAILED DESCRIPTION
[0074] Example 1
[0075] See also Figure 1-Figure 3 The present invention provides an embodiment of a method for intelligent code comparison and incremental update for Function Declaration, comprising the following steps:
[0076] S1: Use the syntax parser corresponding to the programming language to parse the first version code file and the second version code file to be compared, and generate their respective abstract syntax trees;
[0077] The first version code file is used as the old version, and the second version code file is used as the version that needs to be updated.
[0078] Among them, the syntax parser is a dedicated parser corresponding to the programming language of the code file, including but not limited to Java's ANTLR parser and Python's PyParsing parser; the abstract syntax tree generation process includes lexical analysis, syntax analysis and semantic analysis of the code file to ensure that the generated syntax tree accurately reflects the code structure.
[0079] S2: traverse the abstract syntax tree, extract function declaration information, and store the extracted function declaration information in a data structure to form a function declaration set corresponding to the first version and the second version respectively;
[0080] The data structure is a dictionary structure with the function name as the key and the structure containing the parameter list and return value type as the value.
[0081] S3: Use a collision-resistant hash algorithm to compare the function declaration information in the two versions of the function declaration set, mark the changed function declarations, and record the changed content and its location in the code;
[0082] In the present invention, the collision-resistant hash algorithm adopts SHA-256. SHA-256 is the existing technology content in this field and is not the inventive solution of this application, so it will not be described here.
[0083] S4: Perform difference analysis based on the marked function declaration changes and recorded change information, and formulate a code update strategy based on the difference analysis results;
[0084] S5: Perform an incremental update operation on the first version of the code according to the code update strategy.
[0085] It should be noted that the incremental update operation only modifies the marked changed function declarations and the parts of the function declarations and their codes that are called in the code, while keeping the unchanged function declarations and their code parts unchanged. Before performing the incremental update operation, the first version of the code is backed up, and after the incremental update operation is completed, the updated code is tested and verified.
[0086] The specific steps of S1 include:
[0087] S1.1: Use the file stream to read the original code in the first version code file and the second version code file byte by byte, and preprocess them to obtain independent preprocessed character streams for each version;
[0088] Furthermore, the specific process of S1.1 includes:
[0089] (1) Configure the paths of the first version code file and the second version code file;
[0090] (2) Create dual file streams based on the configured path;
[0091] (3) Open the file stream of the first version code file and initialize the byte buffer of the first version code file;
[0092] (4) Traverse the bytes in the file stream of the first version code file to determine whether the end of the first version code file has been reached;
[0093] If the end of the first version code file is reached, the character stream of the first version code file is returned;
[0094] If the end of the first version code file has not been reached, the next byte is read and filtered using preprocessing;
[0095] (5) Store the filtered bytes into the buffer and determine again whether the end of the first version code file has been reached;
[0096] (6) Process the second version code file using steps (3) to (5) to obtain a preprocessed character stream of the second version code file.
[0097] S1.2: Initialize a finite state machine-based lexical analyzer and define a set of token types. Simultaneously, scan the preprocessed character stream for each version independently according to the lexical rules in the lexical analyzer to generate a token sequence with type tags and attribute values for each version, which are recorded as a first token sequence and a second token sequence. The token types include identification keywords, identifiers, operators, constants, and separators. Finite state machines are prior art in this field and are not an inventive solution of this application, so they are not described in detail here.
[0098] S1.3: Load the context-free grammar rules of the target language, construct an LR parsing table, and use the LR parsing algorithm to construct intermediate parse trees based on the context-free grammar rules, combining the first token sequence and the second token sequence, respectively, to obtain a first version of the intermediate parse tree and a second version of the intermediate parse tree; each internal node in the intermediate parse tree represents a non-terminal symbol, and the leaf node is a token;
[0099] Furthermore, the specific steps of S1.3 include:
[0100] (1) loading context-free grammar rules from a grammar rule source of a target language, wherein the grammar rule source of the target language includes a grammar rule file or a built-in rule library; the grammar rule is a quadruple consisting of a non-terminal symbol set, a terminal symbol set, a production rule set, and a start symbol;
[0101] (2) Verify whether the loaded context-free grammar rules meet the LR parsing requirements. The verification process includes:
[0102] a. Parse the grammar rule source and extract the production set;
[0103] b. Identify the terminal symbol set and non-terminal symbol set in the grammar, where the terminal symbol set refers to the token type and the non-terminal symbol set refers to the grammatical structure;
[0104] c. Determine the starting symbol of the grammar;
[0105] d. If the grammar does not meet the LR requirements, such as if there is a shift-reduce conflict, an error is recorded and the parsing process is terminated;
[0106] (3) Based on the verified context-free grammar rules, an LR item set family is constructed. Then, based on the LR item set family, an LR analysis table is generated, including an action table and a transition table, to support the execution of the LR parsing algorithm.
[0107] (4) Obtain the first token sequence and the second token sequence from S1.2, respectively combine them with the constructed LR analysis table, and execute the LR parsing algorithm. The process of executing the LR parsing algorithm includes:
[0108] a. Use the LR parsing table to parse each token sequence, including shift, reduce, and accept operations, and build an intermediate parse tree during the parsing process;
[0109] b. Each internal node of the intermediate analysis tree represents a non-terminal symbol, that is, a corresponding grammatical structure, and the leaf node is a token, that is, it carries a type and attribute value;
[0110] c. If a syntax error is detected during parsing, such as an invalid token, the error location is recorded and recovery attempts are made, such as ignoring the incorrect token or rolling back to the most recent valid state;
[0111] d. Finally, generate the first version of the intermediate parse tree for the first token sequence and the second version of the intermediate parse tree for the second token sequence.
[0112] Furthermore, each token sequence is parsed using the LR parsing table, including shift, reduction, and acceptance operations. An intermediate parse tree is constructed during the parsing process, including:
[0113] (1) Push the initial state onto the stack and set the input pointer to point to the first token in the token sequence;
[0114] (2) Based on the current stack top state and input token, query the analysis table and perform corresponding actions, including shifting, reducing, accepting or reporting an error;
[0115] Move in: Push the current token and the next state into the stack and move the pointer back;
[0116] Reduction: Reduce the top symbols of the stack to non-terminal symbols according to the production rule and update the stack status;
[0117] Accept: parsing is completed successfully and an intermediate parse tree is generated;
[0118] (3) During reduction, a non-terminal symbol node is created, whose child nodes are the symbols to be reduced, i.e., tokens or non-terminal symbols;
[0119] (4) Finally, an intermediate analysis tree is constructed with the start symbol as the root node and the leaf nodes as Tokens.
[0120] S1.4: Define a hierarchical symbol table data structure; the hierarchical structure includes three levels: global scope, function scope, and block scope;
[0121] In the present invention, the hierarchical structure is to accurately match the scope rules of the programming language, wherein the global scope is the outermost scope, which usually includes symbols declared at the entire program or module level, such as global variables, constants, class names, and function names, which are valid throughout the life cycle of the entire program / module; the function scope is nested within the global scope, and includes symbols declared inside the function, such as function parameters and local variables within the function. If there is no block scope within the function scope of the language, all local variables within the function belong to this layer, which are created when the function is called and destroyed when the function returns; the block scope is nested within the function scope, and includes curly braces {} or block delimiters, such as symbols declared inside if, for, while, and switch statement blocks, such as loop variables and temporary variables defined in conditional statement blocks, which are created when entering the block and destroyed when leaving the block. In the present invention, the three-layer structure accurately reflects the basic rules of symbol visibility and lifetime in programming languages, allowing symbols with the same name to be defined in different scopes. When a symbol is used in any scope, the symbol table lookup operation will search upward along the scope hierarchy, such as starting from the current innermost scope and searching step by step toward the outermost scope until the definition of the symbol is found or it is determined to be undefined. At the same time, the entry and exit of the scope, that is, the creation and destruction, are associated with the creation and destruction of symbol entries at the corresponding level in the symbol table.
[0122] S1.5: Traverse the intermediate parse tree of each version and use the hierarchical symbol table to record the definition information of variables or functions according to the scope level. At the same time, assign a unique identifier to each symbol and associate the type information with the symbol table entry, obtaining the symbol table after the first version is filled and the symbol table after the second version is filled;
[0123] Furthermore, the specific steps of S1.5 include:
[0124] (1) Obtain the hierarchical symbol table data structure from S1.4 and create a stack structure to maintain the current scope chain;
[0125] (2) Push the global scope as the initial scope to the bottom of the stack;
[0126] (3) Starting from the root node, recursively traverse each node in the intermediate analysis tree of each version, and execute according to the node type:
[0127] If a function declaration node is encountered, a new function scope is created and the function name, parameter list, and return type are stored in the new function scope;
[0128] If a block statement node is encountered, a new block scope is created;
[0129] If a variable declaration node is encountered, a unique symbolic identifier is generated from the current stack top scope, and the variable type, initial value, and scope level are recorded in the symbolic entry;
[0130] When traversal enters a new block scope or function scope, the new scope is pushed onto the top of the stack as the current scope;
[0131] When encountering a scope end node, exit the current scope and return to the parent scope;
[0132] (4) After completing the traversal of the entire intermediate analysis tree, a fully populated hierarchical symbol table structure is obtained, and the symbol table is saved as the populated symbol table of the current version.
[0133] S1.6: Perform a static semantic check based on the populated symbol table for each version to check whether the function call parameters match the definition. If they do, perform constant folding optimization, record semantic error information, and generate a semantically marked intermediate parse tree for the first version and a semantically marked intermediate parse tree for the second version. The semantic error information includes undefined variables and type mismatches. The check is performed by searching the scope chain of the symbol table.
[0134] Furthermore, the specific steps of performing static semantic checking include:
[0135] (1) Obtain the intermediate analysis tree and hierarchical symbol table data structure for each version;
[0136] (2) Search the scope chain of the symbol table to find the matching function definition in the current scope or the parent scope;
[0137] Compare the number of actual parameters and the number of formal parameters to see if they are consistent;
[0138] Verify whether the types of actual parameters and formal parameters are compatible one by one;
[0139] (3) If there is no match, the error type and location are recorded. Error types include mismatched number of parameters or incompatible types.
[0140] Furthermore, the undefined variable checking process includes:
[0141] (1) Traverse the intermediate analysis tree and mark all variable reference nodes;
[0142] (2) Starting from the current scope, search for variable definitions level by level;
[0143] (3) If the definition is still not found after traversing the global scope, an undefined variable error is recorded.
[0144] Furthermore, the type mismatch check process includes:
[0145] (1) Derived type for each node in the intermediate parse tree;
[0146] (2) Verify whether the operand types on both sides of the operator are legal;
[0147] (3) Ensure that the type of the expression on the right side of the assignment is compatible with the type of the variable on the left side;
[0148] (4) If they are not compatible, a type mismatch error is recorded.
[0149] S1.7: Extract the core semantic nodes from the semantically tagged intermediate parse tree for each version. Create abstract syntax tree nodes and establish parent-child relationships based on the defined hierarchical symbol table data structure. Inject the type and scope metadata from the populated symbol table for each version. By standardizing the equivalent syntax structure, form a final abstract syntax tree independent of each version.
[0150] Furthermore, the specific steps of S1.7 include:
[0151] (1) Obtain the semantically marked intermediate analysis tree of each version, filter the nodes according to the context-free grammar rules, and retain the core semantic nodes;
[0152] (2) Create a corresponding abstract syntax tree node object for each core semantic node;
[0153] (3) Reconstruct the abstract syntax tree hierarchy based on the hierarchical symbol table data structure;
[0154] (4) Bind the type or scope in the symbol table to the abstract syntax tree node;
[0155] (5) Use context-free grammar rules to eliminate syntactic sugar and equivalent structures to obtain the final abstract syntax tree.
[0156] S1.8: Convert the final abstract syntax tree for each version into a storable serialized format.
[0157] The specific steps of S2 include:
[0158] S2.1: Read the first version serialized abstract syntax tree and the second version serialized abstract syntax tree, and perform a deserialization operation to obtain the first version abstract syntax tree and the second version abstract syntax tree;
[0159] Among them, the deserialization operation is implemented by calling the function library of the programming language.
[0160] S2.2: Define functions to declare data structures and create two empty hash tables and ;
[0161] S2.3: Using a depth-first search method, traverse each node of the first version of the abstract syntax tree starting from the root node and determine whether it is a function node. The depth-first search method is prior art in this field and does not constitute an inventive solution of this application, and is not described in detail here.
[0162] If the current node is a function node, extract the function declaration information in the first version of the abstract syntax tree, generate a function signature based on the function declaration information in the extracted first version of the abstract syntax tree, and use the function signature as the unique key and the function declaration information as the storage value. , obtaining a first version of the function declaration set; the function declaration information at least includes a function name, a parameter list, and a return value type; the function signature is a string composed of the function declaration information;
[0163] If the current node is not a function node, recursively traverse the child nodes of the current node;
[0164] S2.4: Repeat the process of S2.3 to traverse the second version of the abstract syntax tree, and store the extracted function declaration information and the corresponding function signature in the form of key-value pairs. , get the second version of the function declaration set.
[0165] The specific steps of S3 include:
[0166] S3.1: Get the function declaration set of the first version and the function declaration set of the second version, and establish an index mapping from function name to function declaration to obtain the function declaration set of the first version after index optimization. and the second version of the function declaration set ;
[0167] S3.2: Use a collision-resistant hash algorithm and Perform hash calculation on each function declaration in the , and construct mappings from hash values to function declarations to form a first hash mapping table and a second hash mapping table;
[0168] S3.3: Construct two hash value sets based on the first hash mapping table and the second hash mapping table and ; The hash value set Contains all hash values in the first hash mapping table; the hash value set Contains all hash values in the second hash map;
[0169] S3.4: Initialize a pool to be analyzed;
[0170] for ,like , then take out the corresponding function declaration from the first hash mapping table, mark it as coming from the first version code file, and store it in the pool to be analyzed, where h represents the hash value; taking out means removing;
[0171] for ,like , then take out the corresponding function declaration from the second hash map, mark it as coming from the second version code file, and store it in the pool to be analyzed;
[0172] S3.5: Divide the function declarations in the pool to be analyzed into two subsets according to the source version, and obtain the first pool to be analyzed and the second pool to be analyzed ;
[0173] S3.6: Initialize a change record list and traverse the first pool to be analyzed Each function declaration in , and in the second pool to be analyzed Find and Function declaration with the same name;
[0174] If in the second pool to be analyzed There exists Function declarations with the same name , then the function declaration is retrieved synchronously and function declarations ; The function declaration is removed In the first pool to be analyzed Remove ; Remove the function declaration In the second pool to be analyzed Remove ;
[0175] S3.7: Comparison and Parameter list and return value type;
[0176] If the parameter list and return value types are exactly the same, record the change type as move and record and The location information includes the file path and the starting and ending line numbers;
[0177] If the parameter list and return value types are not exactly the same, the change type is recorded as modification, and the signature difference information and location information are recorded, and the record is added to the change record list; the signature difference information includes parameter list changes and return value change information;
[0178] S3.8: Search and compare the results based on the parameter list and return value type to obtain the first pool to be analyzed after screening and the second pool to be analyzed ;
[0179] S3.9: Initialize a renamed record list and traverse the first pool to be analyzed after screening Each function declaration in ,calculate The second pool to be analyzed after screening Each function declaration in The signature similarity of
[0180] If the signature similarity is greater than the preset similarity threshold, it is marked as renamed and modified, and the marked record is written into the renamed record list. from Remove from Removed;
[0181] If the signature similarity is less than or equal to the preset similarity threshold, The remaining function declarations in are marked for deletion, The remaining function declarations in are marked as new and added to the change log list;
[0182] S3.10: Summarize the change record list and the rename record list and generate a change report; the change report includes the location information, change type, change content and function name of each changed function declaration in the first version code file and the second version code file.
[0183] The specific steps of S4 include:
[0184] S4.1: Obtain change reports and classify changes based on the change reports;
[0185] S4.2: Develop a code update strategy for each change type. The code update strategy includes:
[0186] If the function name is modified, find the original function call location in the first version code file and replace it with the new function name;
[0187] If the parameter list changes, check the parameter passing at the function call and make adjustments based on the addition or deletion of parameters, including:
[0188] For parameter addition, add the new parameter in the function declaration and add the corresponding actual parameter at all call points that call the function and need to provide the new parameter value;
[0189] For parameter deletion, delete the corresponding parameter in the function declaration and remove the corresponding actual argument at all call sites of the function;
[0190] If the parameter type or return value type changes, the type information in the function declaration is updated, and the actual parameter or return value processing logic of the function call location is updated according to the type compatibility check or forced conversion requirements.
[0191] Example 2
[0192] See also Figure 4 Another embodiment provided by the present invention is a code intelligent comparison and incremental update system for Function Declaration, comprising:
[0193] Code parsing module, declaration extraction module, declaration comparison module, update strategy formulation module, incremental update execution module;
[0194] A code parsing module, used to convert the first version code file and the second version code file into an abstract syntax tree, providing a structured basis for subsequent function declaration extraction and comparison;
[0195] Declaration extraction module, used to extract function declaration information from the abstract syntax tree and build a structured storage set;
[0196] Declaration comparison module, based on anti-collision hashing and multi-dimensional comparison logic, identifies function declaration changes and generates change reports;
[0197] The update strategy formulation module plans code update logic for different change types based on change reports and provides execution plans for incremental updates;
[0198] The incremental update execution module completes the precise incremental update of the first version of the code according to the implementation update strategy.
[0199] The code parsing module includes: preprocessing unit, lexical analysis unit, syntax analysis unit, and semantic analysis unit;
[0200] A preprocessing unit, configured to read the first version code file and the second version code file byte by byte, remove comments, normalize indentation and line breaks, and output a preprocessed character stream;
[0201] Lexical analysis unit, used to convert character streams into token sequences and complete the initial structuring of the code;
[0202] The syntax analysis unit builds an intermediate analysis tree based on the context-free grammar rules of the target language and combines the token sequence to realize grammatical structure recognition;
[0203] The semantic analysis unit is used to supplement the semantic information of the intermediate analysis tree, generate the final abstract syntax tree, and serialize it.
[0204] The declaration comparison module includes: hash mapping unit, analysis pool construction unit, change analysis unit, and report generation unit;
[0205] A hash mapping unit calculates the function declaration hash value using a collision-resistant hash algorithm, constructs a hash value-function declaration mapping, and extracts a hash value set;
[0206] The unit for constructing a pool to be analyzed is used to compare two hash value sets, mark the function declarations corresponding to the hash values that only exist in a single set with their versions, and store them in the pool to be analyzed;
[0207] The change analysis unit is used to split the pool to be analyzed by version. It searches for function declarations with the same name, compares parameter lists and return value types, distinguishes between moves and modifications, and records differences and location information. At the same time, it calculates signature similarity to identify renames, additions, and deletions, marking them and updating the pool to be analyzed.
[0208] The report generation unit is used to integrate change information and output change reports.
[0209] The update strategy formulation module includes: a change classification unit and a strategy generation unit;
[0210] The change classification unit is used to parse the change report, classify it according to function name modification, parameter list change, and type change, and output the classified change set;
[0211] The policy generation unit is used to customize update policies for various types of changes.
[0212] The embodiments of the present invention are described above in conjunction with the accompanying drawings, but the present invention is not limited to the above-mentioned specific embodiments. The above-mentioned specific embodiments are merely illustrative and not restrictive. Under the guidance of the present invention, ordinary technicians in this field can also change, modify, replace and modify the above-mentioned embodiments without departing from the purpose and scope of protection of the present invention. These are all protected by the present invention.
Claims
1. Function Declaration-oriented code intelligent comparison and incremental update method, characterized by: include: S1: Use the syntax parser corresponding to the programming language to parse the first version code file and the second version code file to be compared, and generate their respective abstract syntax trees; S2: traverse the abstract syntax tree, extract function declaration information, and store the extracted function declaration information in a data structure to form a function declaration set corresponding to the first version and the second version respectively; S3: Use a collision-resistant hash algorithm to compare the function declaration information in the two versions of the function declaration set, mark the changed function declarations, and record the changed content and its location in the code; S4: Perform difference analysis based on the marked function declaration changes and recorded change information, and formulate a code update strategy based on the difference analysis results; S5: Perform an incremental update operation on the first version of the code according to the code update strategy.
2. The method for intelligent code comparison and incremental update for Function Declaration according to claim 1, characterized in that: The specific steps of S1 include: S1.1: Use the file stream to read the original code in the first version code file and the second version code file byte by byte, and preprocess them to obtain independent preprocessed character streams for each version; S1.2: Initialize a finite state machine-based lexical analyzer and define a set of token types. Simultaneously, scan the preprocessed character stream for each version according to the lexical rules in the lexical analyzer to generate a sequence of tokens with type tags and attribute values for each version, denoted as the first token sequence and the second token sequence. Token types include identifying keywords, identifiers, operators, constants, and delimiters. S1.3: Load the context-free grammar rules of the target language, construct an LR parsing table, and use the LR parsing algorithm to construct intermediate parse trees based on the context-free grammar rules, combining the first token sequence and the second token sequence, respectively, to obtain a first version of the intermediate parse tree and a second version of the intermediate parse tree; each internal node in the intermediate parse tree represents a non-terminal symbol, and the leaf node is a token; S1.4: Define a hierarchical symbol table data structure; the hierarchical structure includes three levels: global scope, function scope, and block scope; S1.5: Traverse the intermediate parse tree of each version and use the hierarchical symbol table to record the definition information of variables or functions according to the scope level. At the same time, assign a unique identifier to each symbol and associate the type information with the symbol table entry, obtaining the symbol table after the first version is filled and the symbol table after the second version is filled; S1.6: Perform a static semantic check based on the populated symbol table for each version to check whether the function call parameters match the definition. If they do, perform constant folding optimization, record semantic error information, and generate a semantically marked intermediate parse tree for the first version and a semantically marked intermediate parse tree for the second version. The semantic error information includes undefined variables and type mismatches. The check is performed by searching the scope chain of the symbol table. S1.7: Extract the core semantic nodes from the semantically tagged intermediate parse tree for each version. Create abstract syntax tree nodes and establish parent-child relationships based on the defined hierarchical symbol table data structure. Inject the type and scope metadata from the populated symbol table for each version. By standardizing the equivalent syntax structure, form a final abstract syntax tree independent of each version. S1.8: Convert the final abstract syntax tree for each version into a storable serialized format.
3. The method for intelligent code comparison and incremental update for Function Declaration according to claim 2, characterized in that: The specific steps of S2 include: S2.1: Read the first version serialized abstract syntax tree and the second version serialized abstract syntax tree, and perform a deserialization operation to obtain the first version abstract syntax tree and the second version abstract syntax tree; S2.2: Define functions to declare data structures and create two empty hash tables and ; S2.3: Use a depth-first search method to traverse each node of the first version of the abstract syntax tree starting from the root node and determine whether it is a function node; If the current node is a function node, extract the function declaration information in the first version of the abstract syntax tree, generate a function signature based on the function declaration information in the extracted first version of the abstract syntax tree, and use the function signature as the unique key and the function declaration information as the storage value. , obtaining a first version of the function declaration set; the function declaration information at least includes a function name, a parameter list, and a return value type; the function signature is a string composed of the function declaration information; If the current node is not a function node, recursively traverse the child nodes of the current node; S2.4: Repeat the process of S2.3 to traverse the second version of the abstract syntax tree, and store the extracted function declaration information and the corresponding function signature in the form of key-value pairs. , get the second version of the function declaration set.
4. The method for intelligent code comparison and incremental update for Function Declaration according to claim 3, characterized in that: The specific steps of S3 include: S3.1: Get the function declaration set of the first version and the function declaration set of the second version, and establish an index mapping from function name to function declaration to obtain the function declaration set of the first version after index optimization. and the second version of the function declaration set ; S3.2: Use a collision-resistant hash algorithm and Perform hash calculation on each function declaration in the , and construct mappings from hash values to function declarations to form a first hash mapping table and a second hash mapping table; S3.3: Construct two hash value sets based on the first hash mapping table and the second hash mapping table and ; The hash value set Contains all hash values in the first hash mapping table; the hash value set Contains all hash values in the second hash map; S3.4: Initialize a pool to be analyzed; for ,like , then take out the corresponding function declaration from the first hash mapping table, mark it as coming from the first version code file, and store it in the pool to be analyzed, where h represents the hash value; taking out means removing; for ,like , then take out the corresponding function declaration from the second hash mapping table, mark it as coming from the second version code file, and store it in the pool to be analyzed.
5. The method for intelligent code comparison and incremental update for Function Declaration according to claim 4, characterized in that: The specific steps of S3 also include: S3.5: Divide the function declarations in the pool to be analyzed into two subsets according to the source version, and obtain the first pool to be analyzed and the second pool to be analyzed ; S3.6: Initialize a change record list and traverse the first pool to be analyzed Each function declaration in , and in the second pool to be analyzed Find and Function declaration with the same name; If in the second pool to be analyzed There exists Function declarations with the same name , then the function declaration is retrieved synchronously and function declarations ; The function declaration is removed In the first pool to be analyzed Remove ; Remove the function declaration In the second pool to be analyzed Remove ; S3.7: Comparison and Parameter list and return value type; If the parameter list and return value types are exactly the same, record the change type as move and record and The location information includes the file path and the starting and ending line numbers; If the parameter list and return value types are not exactly the same, the change type is recorded as modification, and the signature difference information and location information are recorded, and the record is added to the change record list; the signature difference information includes parameter list changes and return value change information; S3.8: Search and compare the results based on the parameter list and return value type to obtain the first pool to be analyzed after screening and the second pool to be analyzed .
6. The method for intelligent code comparison and incremental update for Function Declaration according to claim 5, characterized in that: The specific steps of S3 also include: S3.9: Initialize a renamed record list and traverse the first pool to be analyzed after screening Each function declaration in ,calculate The second pool to be analyzed after screening Each function declaration in The signature similarity of If the signature similarity is greater than the preset similarity threshold, it is marked as renamed and modified, and the marked record is written into the renamed record list. from Remove from Removed; If the signature similarity is less than or equal to the preset similarity threshold, The remaining function declarations in are marked for deletion, The remaining function declarations in are marked as new and added to the change log list; S3.10: Summarize the change record list and the rename record list and generate a change report; the change report includes the location information, change type, change content and function name of each changed function declaration in the first version code file and the second version code file.
7. The method for intelligent code comparison and incremental update for Function Declaration according to claim 6, characterized in that: The specific steps of S4 include: S4.1: Obtain change reports and classify changes based on the change reports; S4.2: Develop a code update strategy for each change type. The code update strategy includes: If the function name is modified, find the original function call location in the first version code file and replace it with the new function name; If the parameter list changes, check the parameter passing at the function call and make adjustments based on the addition or deletion of parameters, including: For parameter addition, add the new parameter in the function declaration and add the corresponding actual parameter at all call points that call the function and need to provide the new parameter value; For parameter deletion, delete the corresponding parameter in the function declaration and remove the corresponding actual argument at all call sites of the function; If the parameter type or return value type changes, the type information in the function declaration is updated, and the actual parameter or return value processing logic of the function call location is updated according to the type compatibility check or forced conversion requirements.
8. A code intelligent comparison and incremental update system for Function Declaration, which is used to implement the code intelligent comparison and incremental update method for Function Declaration according to any one of claims 1 to 7, characterized in that: include: Code parsing module, declaration extraction module, declaration comparison module, update strategy formulation module, incremental update execution module; The code parsing module is used to convert the first version code file and the second version code file into an abstract syntax tree; The declaration extraction module is used to extract function declaration information from the abstract syntax tree and construct a structured storage set; The declaration comparison module identifies function declaration changes and generates a change report based on anti-collision hashing and multi-dimensional comparison logic; The update strategy formulation module plans code update logic for different change types based on the change report; The incremental update execution module completes the incremental update of the first version code according to the landing update strategy.
9. The intelligent code comparison and incremental update system for Function Declaration according to claim 8, characterized in that: The statement comparison module includes: a hash mapping unit, a pool to be analyzed construction unit, a change analysis unit, and a report generation unit; The hash mapping unit calculates the function declaration hash value using a collision-resistant hash algorithm, constructs a hash value-function declaration mapping, and extracts a hash value set; The to-be-analyzed pool construction unit is configured to compare two hash value sets, mark function declarations corresponding to hash values that exist only in a single set, and store the marked versions in the to-be-analyzed pool; The change analysis unit is used to split the pool to be analyzed by version, traverse and find function declarations with the same name, compare parameter lists and return value types, distinguish between moves and modifications, record differences and location information, and at the same time, calculate signature similarity to identify renames, additions, and deletions, mark them, and update the pool to be analyzed; The report generating unit is used to integrate the change information and output a change report.
Citation Information
Patent Citations
A decision support method and device for software code change integration
CN109947462A
Code auxiliary optimization method and system suitable for integrated development environment
CN119556899A