A code impact analysis method, apparatus, device, and computer program product
By using abstract syntax trees and call chain analysis, the accuracy problem of code impact analysis in existing technologies is solved, enabling precise identification of affected functions and analysis of API interface impacts. It supports automated code impact analysis and optimizes testing strategies.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- GF SECURITIES CO LTD
- Filing Date
- 2026-03-24
- Publication Date
- 2026-06-12
AI Technical Summary
Existing code impact analysis methods suffer from accuracy issues when dealing with complex code structures and dependencies, failing to accurately identify affected functions, especially in asynchronous programming, callback functions, and event-driven architectures. Traditional call graph methods cannot provide precise location at the line or statement level and lack support for API interface impact analysis.
Abstract syntax trees are used to analyze code change files to obtain the range of changed line numbers and method line numbers. The affected functions are identified by comparison, and the direct and indirect impact of API interfaces is identified by combining call chain analysis.
It enables precise identification of affected functions, provides more granular impact analysis, accurately identifies the impact of code changes on API interfaces, supports automated post-commit impact analysis, and optimizes testing strategies.
Smart Images

Figure CN122195501A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of software development technology, and in particular to a code impact analysis method, apparatus, device, and computer program product. Background Technology
[0002] In the software development process, code commit is the basic unit of version control. Each code commit may introduce new features, fix defects, or refactor existing code. Accurately identifying the scope of these changes' impact on the entire system is crucial for ensuring software quality, assessing release risks, and developing testing strategies. Relevant code impact analysis methods mainly include static analysis based on text similarity, dynamic analysis based on call graphs, and rule-based regular expression matching. However, these methods have accuracy issues when dealing with complex code structures and dependencies, and cannot accurately identify affected functions. Summary of the Invention
[0003] This application provides a code impact analysis method, apparatus, device, and computer program product that can accurately identify affected functions.
[0004] The technical solution of this application embodiment is implemented as follows: This application provides a code impact analysis method, the method comprising: Get the range of line numbers corresponding to the code change file after code commit; The code modification file is analyzed using an abstract syntax tree to obtain the range of method line numbers corresponding to the first method function; Based on the range of changed line numbers and the range of method line numbers, determine whether the first method function is an affected function.
[0005] This application provides a code impact analysis device, including: The retrieval unit is used to retrieve the range of line numbers corresponding to the code change files after code submission. The analysis unit is used to analyze the code change file using an abstract syntax tree to obtain the range of method line numbers corresponding to the first method function; The determining unit is used to determine whether the first method function is an affected function based on the change line number range and the method line number range.
[0006] This application provides an electronic device, including: a processor and a memory for storing computer programs capable of running on the processor. When the processor runs the computer program, it executes the code impact analysis method provided in the embodiments of this application.
[0007] This application provides a computer program product, including a computer program or computer executable instructions. When the computer program or computer executable instructions are executed by a processor, they implement the code impact analysis method provided in this application.
[0008] The embodiments of this application have the following beneficial effects: By obtaining the range of changed line numbers in the code change file after code submission and analyzing the code change file using an abstract syntax tree, the range of method line numbers corresponding to the first method function is obtained. The range of changed line numbers and the range of method line numbers are used together to determine whether the first method function is an affected function. That is, both the changed line numbers in the code change file and the method line numbers of the first method function are considered, which can accurately identify whether the first method function is an affected function, and thus accurately identify affected functions. Attached Figure Description
[0009] Figure 1 A flowchart illustrating a code impact analysis method provided in an embodiment of this application; Figure 2 A schematic diagram illustrating an exemplary code submission impact analysis based on an abstract syntax tree, provided for embodiments of this application; Figure 3 This is a schematic diagram illustrating an exemplary process for extracting change codes, provided as an embodiment of this application. Figure 4 This is a schematic diagram illustrating an exemplary process for constructing an abstract syntax tree, provided as an embodiment of this application. Figure 5 A schematic flowchart of an exemplary function influence analysis provided for an embodiment of this application; Figure 6 A schematic diagram illustrating an exemplary call chain analysis process provided for an embodiment of this application; Figure 7 This is a schematic flowchart illustrating an exemplary API interface impact analysis provided for an embodiment of this application. Figure 8 This is a schematic diagram of the structure of a code impact analysis device provided in an embodiment of this application; Figure 9 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0010] To make the objectives, technical solutions, and advantages of this application clearer, the application will be further described in detail below with reference to the accompanying drawings. The described embodiments should not be regarded as limitations on this application. All other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0011] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used herein is for the purpose of describing embodiments of this application only and is not intended to limit this application.
[0012] In the following description, references to "some embodiments" refer to a subset of all possible embodiments. It is understood that "some embodiments" may be the same or different subsets of all possible embodiments and may be combined with each other without conflict. It should also be noted that the terms "first," "second," etc., used in the embodiments of this application are merely for distinguishing similar objects and do not represent a specific ordering of objects. It is understood that "first," "second," etc., may be interchanged in a specific order or sequence where permissible, so that the embodiments of this application described herein can be implemented in an order other than that illustrated or described herein.
[0013] As software projects grow in scale and development team collaborations become increasingly complex, impact analysis of code changes is becoming more and more important. Traditional code impact analysis mainly relies on manual review or simple text matching techniques, which is not only inefficient but also prone to missing important impact relationships.
[0014] Call graph-based dynamic analysis is the mainstream technique for code impact analysis. This method traces call relationships between functions by constructing a call graph, and determines the scope of impact by traversing the call graph when code changes occur. However, patterns widely used in software development, such as asynchronous programming, callback functions, and event-driven architectures, generate complex call relationships, including indirect calls, dynamic calls, and conditional calls. Traditional call graph analysis techniques often struggle to handle these indirect call relationships, easily missing important impact propagation paths. Call graph methods typically perform impact analysis at the function level, but in practice, different code segments within the same function may have different importance. Traditional call graph methods cannot provide finer-grained impact analysis, such as precise line-level or statement-level localization, resulting in overly coarse impact analysis results. While call graph methods can identify call relationships between functions, they lack support for Application Programming Interface (API) impact analysis. In modern software development, analyzing the impact of code changes on exposed API interfaces is crucial. Traditional call graph methods cannot effectively identify how code changes propagate to externally exposed API interfaces, which can easily lead to the omission of important impact analysis during version releases and API change management, potentially affecting the stability of the system's external services and user experience.
[0015] Based on this, embodiments of this application provide a code impact analysis method. Figure 1 This is a flowchart illustrating a code impact analysis method provided in an embodiment of this application; as shown below. Figure 1 As shown, the method includes; S101. Obtain the range of line numbers corresponding to the code change file after code submission.
[0016] It's important to note that a code change file can be understood as a file that has been modified since the code was committed, compared to existing code files. The number of line number ranges can be one or more, meaning there are multiple changes in the code change file. The specific number can be determined based on the actual situation and is not limited here. When there are multiple line number ranges, they can be listed, i.e., a list of line number ranges. A line number range includes the start line number and the end line number of the change. Obtaining the line number ranges corresponding to the code change file after the code commit can be understood as using the diff function of the distributed version control system (Global Information Tracker, Git) to extract the change information of all files in the code commit. The change information includes at least the file path, name, line number range, and specific content.
[0017] S102. Use an abstract syntax tree to analyze the code change file and obtain the range of method line numbers corresponding to the first method function.
[0018] It should be noted that the first method function can be understood as any one of the one or more method functions extracted after analyzing the code change file using an abstract syntax tree; no specific limitation is made here. The method line number range can be understood as the range of line numbers corresponding to the first method function; the method line number range includes the method's starting line number and the method's ending line number.
[0019] In this embodiment of the application, the process of analyzing the code change file using an abstract syntax tree to obtain the method line number range corresponding to the first method function specifically includes: analyzing the code change file using an abstract syntax tree to obtain one or more nodes corresponding to the abstract syntax tree; if the first node in one or more nodes is a first method, recording the first method as a first method function, and obtaining the method line number range corresponding to the first method function.
[0020] It should be noted that analyzing the code change file using an Abstract Syntax Tree (AST) to obtain one or more nodes corresponding to the AST can be understood as analyzing the code structure of the code change file using an AST to obtain the definition nodes of the AST. These definition nodes are denoted as one or more nodes, which include methods, classes, and references. The number of nodes can be determined based on the actual situation and is not limited here. The first node can be understood as any one of the one or more nodes, and is not limited here.
[0021] It should be noted that when the first node in one or more nodes is the first method, recording the first method as the first method function and obtaining the range of method line numbers corresponding to the first method function can be understood as recording the full name of the current method as type.method name, i.e., the first method function, and obtaining the range of method line numbers corresponding to the first method function, i.e., the method start line number and the method end line number.
[0022] In this embodiment of the application, if the first node in one or more nodes is of a first type, the second node within the first type is obtained; if the second node is a first method, the first method is recorded as a first method function, and the range of method line numbers corresponding to the first method function is obtained.
[0023] It should be noted that when the first node in one or more nodes is of the first class, obtaining the second node within the first class can be understood as follows: when the first node is of the first class, record the current class name and sequentially obtain the abstract syntax tree nodes (i.e., the second nodes) within the class; when the second node is the first method, record the full name of the current method as type.method name, i.e., the first method function, and obtain the range of method line numbers corresponding to the first method function, i.e., the method start line number and the method end line number.
[0024] It should be noted that the same steps as those for the first node need to be performed on each of the one or more nodes.
[0025] The solution in this application uses an abstract syntax tree for syntax analysis, which can accurately identify the code structure and avoid the misjudgment problem caused by comments, strings or functions with the same name in traditional text matching methods.
[0026] S103. Determine whether the first method function is affected based on the change line number range and the method line number range.
[0027] It should be noted that determining whether the first method function is affected based on the range of changed line numbers and the range of method line numbers can be understood as comparing the range of changed line numbers with the range of method line numbers to determine whether the first method function is affected.
[0028] In this embodiment of the application, the process of determining whether the first method function is an affected function based on the change line number range and the method line number range specifically includes: obtaining the change start line number and change end line number corresponding to the change line number range; obtaining the method start line number and method end line number corresponding to the method line number range; and determining whether the first method function is an affected function based on the change start line number and method end line number, and the change end line number and method start line number.
[0029] It should be noted that the range of change line numbers includes the change start line number and the change end line number; the range of method line numbers includes the method start line number and the method end line number; determining whether the first method function is an affected function based on the change start line number and the method end line number, as well as the change end line number and the method start line number, can be understood as comparing the change start line number with the method end line number, and comparing the change end line number with the method start line number to determine whether the first method function is an affected function.
[0030] In this embodiment of the application, the process of determining whether the first method function is an affected function based on the change start line number and the method end line number, and the change end line number and the method start line number specifically includes: if the method end line number is greater than or equal to the change start line number, and the method start line number is less than or equal to the change end line number, the first method function is determined to be an affected function.
[0031] It should be noted that if the method termination line number is greater than or equal to the change start line number, and the method start line number is less than or equal to the change termination line number, the first method function is determined to be the affected function.
[0032] If the method termination line number is less than the change start line number, or the method start line number is greater than the change termination line number, then the first method function is determined not to be an affected function.
[0033] The solution in this application embodiment obtains the range of changed line numbers corresponding to the code change file after code submission, and analyzes the code change file using an abstract syntax tree to obtain the range of method line numbers corresponding to the first method function. The range of changed line numbers and the range of method line numbers are used together to determine whether the first method function is an affected function. That is, both the changed line numbers of the code change file and the method line numbers of the first method function are considered, which can accurately identify whether the first method function is an affected function, that is, it can accurately identify the affected function.
[0034] In this embodiment of the application, the method further includes: obtaining a first code file after code submission; constructing a call relationship graph based on the first code file, and obtaining one or more call chains in the call relationship graph; and determining the first call chain as the affected call chain if the first call chain in one or more call chains contains an affected function.
[0035] It should be noted that the first code file can be the code file after code submission. Building a call graph based on the first code file can be understood as using the open-source tool Python Analyzer (pyan) to construct the call graph corresponding to the first code file. The call graph abstracts the program as a graph structure composed of nodes (functions) and edges (call relationships). A call chain can be understood as one or more call chains in the call graph; the specific number can be determined based on the actual situation and is not limited here. The first call chain is any one of the one or more call chains. If the first call chain in one or more call chains contains an affected function, the first call chain is determined to be an affected call chain. If the first call chain does not contain an affected function, the traversal of other call chains continues until all call chains have been traversed.
[0036] In this embodiment of the application, the method further includes: when the first call chain is an affected call chain and the starting node of the first call chain is an API routing node, determining that the first call chain is a call chain containing an external routing interface, and outputting the first call chain.
[0037] It should be noted that when the first call chain is an affected call chain and its starting node is an API routing node, the first call chain is determined to be a call chain containing an external routing interface, and the first call chain is output. This can be understood as follows: when the first call chain is an affected call chain and its starting node is an API routing node (the specific format is determined according to the current project framework; for example, the routing node in the FastAPI framework is identified by the prefix @Router), then the first call chain is determined to be a call chain containing an external routing interface, and the first call chain is output.
[0038] The solution in this application embodiment, combined with call chain analysis technology, can not only identify the directly affected functions, but also analyze the indirect effects propagated through function call relationships, and provide the externally exposed API interfaces affected by the changes, thus providing more comprehensive information for risk assessment.
[0039] To facilitate understanding, an example is provided here to illustrate the above scheme. The code impact analysis method described above can be exemplified as a code commit impact analysis method based on an abstract syntax tree.
[0040] AST function analyzer design. This application's embodiment designs a dedicated ASTFunctionAnalyzer class that inherits from ast.NodeVisitor, implementing the following functions: Maintain the class stack structure and track the context of the currently nested classes; Identify function definition nodes and construct the complete function name (including the class name); Record the start and end line numbers of the function.
[0041] class ASTFunctionAnalyzer(ast.NodeVisitor): def __init__(self): self.class_stack = [] # Tracks the currently nested classes; self.functions = {}; def visit_ClassDef(self, node): # Enter the class definition and record the class name; self.class_stack.append(node.name); self.generic_visit(node); # Leave the class definition; self.class_stack.pop(); def visit_FunctionDef(self, node): # Construct the complete function name (including the class name); if self.class_stack: full_name = f"{'.'.join(self.class_stack)}.{node.name}"; else: full_name = node.name; self.functions[full_name] = (node.lineno, self._get_end_line(node)); self.generic_visit(node).
[0042] Change line range extraction algorithm: By parsing the Git diff output, the algorithm accurately extracts the range of line numbers affected by the change.
[0043] def fetch_changed_lines_in_commit(commit, parent_commit): # Get all changes diff between two commits; diffs = parent_commit.diff(commit, create_patch=True); for diff in diffs: try: diff_content = diff.diff.decode('utf-8', errors='ignore'); for line in diff_content.split('\n'): In the Git diff format, line number ranges begin with @@, such as: @@ -1,5 +1,5@@; if line.startswith('@@'): # Separates the range of line numbers, in the format "-starting line, line number + starting line, line number"; parts = line.split(); # Extract the line number range information (the part after the + sign) of the new file; new_range = parts[2].lstrip('+'); # Parse line numbers and number of changed lines; if ',' in new_range: # The format is "starting line, line number", such as "1,5" which means 5 lines starting from line 1; start, count = map(int, new_range.split(',')); else: # The format is "starting line", and by default only one line is changed; start = int(new_range); count = 1; # If there are any changes, add them to the change scope list; if count > 0: changed_ranges.append((start, start + count -1)); # If there are changes to the current file, add them to the result dictionary; if changed_ranges: file_changes[file_path] = changed_ranges; except Exception as e: logger.error(f"Exception caught: {e}", exc_info=True); # Returns the range of changed line numbers for all files; return file_changes.
[0044] Affected function identification algorithm: By comparing the range of line numbers defined for the function with the range of line numbers changed, the specific affected function can be identified.
[0045] def analyze_affected_functions_in_branches(project_path, commit,parent_commit): # Get the range of line numbers for all changed files; file_changes = fetch_changed_lines_in_commit(commit, parent_commit); affected_functions = defaultdict(list); for file_path, changed_ranges in file_changes.items(): if not file_path.endswith('.py') or ' / test / ' in file_path: continue; try: # Get the contents of the current file from the Git tree; blob = commit.tree[file_path]; content = blob.data_stream.read().decode('utf-8', errors='ignore'); analyzer = ASTFunctionAnalyzer(); tree = ast.parse(content); analyzer.visit(tree); # Get the full path and module name of the file; full_file_path = os.path.join(project_path, file_path); module_name = get_module_name(full_file_path); for func_name, (start_line, end_line) inanalyzer.functions.items(): # Check if the function overlaps with any change line number range; for change_start, change_end in changed_ranges: # Determine if the function definition line number range overlaps with the change line number range; if (start_line<= change_end) and (end_line>=change_start): affected_functions[file_path].append(f"{module_name}.{func_name}"); break; except Exception as e: logger.error(f"Exception caught: {e}", exc_info=True); # Returns the analysis results for all affected functions; return affected_functions.
[0046] Call chain analysis steps: In order to analyze indirect effects, the call chain analysis function is implemented.
[0047] def get_full_router_chains(): visitor = CallGraphVisitor(STARS_COMMON_MODULES, STARS_SERVICE_MODULES); # Construct a call relationship graph; edges = visitor.edges; nodes = visitor.nodes; # Find the root node; roots = find_roots(nodes, edges); # Generate a complete call chain; all_chains = []; for root in roots: root_node = find_node_by_name(nodes, root); if root_node: children = get_child_nodes(root_node, edges, [root_node]); chains = generate_chains(children); all_chains.extend(chains); return all_chains.
[0048] The code submission impact analysis method based on abstract syntax trees in this application embodiment... Figure 2 This application provides an exemplary flowchart of code commit impact analysis based on an abstract syntax tree; as shown in the embodiments. Figure 2 As shown, the specific steps are as follows: 1. Extract the change code.
[0049] It should be noted that this specifically includes obtaining the target Git branch, obtaining the changed files, and extracting the affected functions; Use the Git diff function to extract change information for all files in a specified commit, including file path, name, range of changed line numbers, and specific content.
[0050] 2. Construction of the abstract syntax tree.
[0051] It should be noted that this specifically includes traversing the code directory, obtaining modified code files, analyzing the code structure based on the AST, and extracting semantic information of functions and classes; For each modified Python file, the code is parsed using the AST module to generate an abstract syntax tree and identify key code structures.
[0052] 3. Functional influence analysis.
[0053] It should be noted that this specifically includes scanning the AST results, extracting function definitions, matching changed rows, and identifying the affected functions; By comparing the line number range defined for the function with the changed line number range, the specific function directly affected can be identified.
[0054] 4. Call chain analysis.
[0055] It should be noted that this specifically includes constructing a call relationship graph, identifying affected functions, and analyzing the call chain propagation path.
[0056] By combining AST function analysis and call graph analysis, indirect influence functions propagating through call chains are identified.
[0057] 5. Analysis of the impact of API interfaces.
[0058] It should be noted that this specifically includes collecting all analysis results and locating the affected API interfaces; Output a list of the changed functions and the affected exposed API interfaces, along with a detailed list of the affected call chains.
[0059] 6. Generate a report and send it.
[0060] Figure 3 This application provides an exemplary flowchart for extracting change codes; as shown in the embodiments. Figure 3 As shown, the specific steps are as follows: 1. Obtain the target Git branch.
[0061] It should be noted that this involves pulling the remote target branch to the local repository.
[0062] 2. Obtain the change documents.
[0063] It should be noted that this retrieves the changes and modified files between the latest commit and the previous commit of the target branch.
[0064] 3. Iterate through the changed files one by one.
[0065] 4. Parse the Git Diff content.
[0066] It should be noted that the Git Diff content is decoded and parsed for each changed file.
[0067] 5. Split the Diff content using newline characters.
[0068] It should be noted that the parsed content is split and extracted line by line.
[0069] 6. Obtain the split rows one by one.
[0070] It should be noted that the split Git Diff rows are obtained sequentially.
[0071] 7. Whether to change the record line.
[0072] It should be noted that whether a record is a changed row number range is determined by whether it begins with "@@". If it does, proceed to step 8; otherwise, proceed to step 6.
[0073] 8. Analyze the line number range after the changes in the new file.
[0074] It should be noted that the parsing retrieves the range of line numbers corresponding to the changes in the new file.
[0075] 9. Save the changed files and the corresponding range of line numbers.
[0076] Figure 4 This application provides an exemplary flowchart for constructing an abstract syntax tree; as shown in the embodiments. Figure 4 As shown, the specific steps are as follows: 1. Traverse the code directory.
[0077] 2. Obtain the change code files sequentially.
[0078] It should be noted that the system checks whether the files in the code directory are modified files. If so, it continues to step 3.
[0079] 3. Analyze the code structure based on AST.
[0080] 4. Obtain the nodes obtained from the AST analysis in sequence.
[0081] It should be noted that during the AST analysis process, the definition nodes of the abstract syntax tree (including methods, classes, references, etc.) are obtained, and the obtained nodes are traversed.
[0082] 5. Whether it is a class.
[0083] It should be noted that if the case is a class, proceed to step 6; otherwise, proceed to step 11.
[0084] 6. Record class name.
[0085] 7. Obtain the nodes within the class one by one.
[0086] 8. Is it a method?
[0087] It should be noted that if the method is not specified, proceed to steps 9-10; otherwise, proceed to step 7.
[0088] 9. Record the full name of the current method = type.method name.
[0089] 10. Obtain the start and end line numbers.
[0090] It should be noted that we return to step 7 to continue traversing the nodes within the class.
[0091] 11. Is it a method?
[0092] It should be noted that if the method is not specified, proceed to steps 12-13; otherwise, proceed to step 4.
[0093] 12. Record the full name of the current method = type.method name.
[0094] 13. Obtain the start and end line numbers.
[0095] It should be noted that we return to step 4 to continue traversing the nodes.
[0096] 14. End.
[0097] It should be noted that after the node traversal in step 4 is completed, step 14 is executed.
[0098] Figure 5 This application provides an exemplary flowchart of function influence analysis; as shown in the embodiments. Figure 5 As shown, the specific steps are as follows: 1. Obtain the AST results.
[0099] 2. Method of traversing the AST.
[0100] It should be noted that this refers to the list of methods in the traversal result.
[0101] 3. Obtain the method name, starting line number, and ending line number.
[0102] 4. Obtain the scope of changes to the current code file.
[0103] It should be noted that a code may have multiple scopes of change. For example, if lines 1-12 and lines 23-30 are all changed, then it is counted as two scopes of change.
[0104] 5. Whether the method start line is <= the change termination line and the method termination line is >= the change start line.
[0105] It should be noted that the method is compared with the change range list in turn. If the condition is met: the method start line <= the change end line and the method end line >= the change start line, then jump to step 6; otherwise, return to step 2 to traverse the method.
[0106] 6. Determine which functions are affected.
[0107] 7. End.
[0108] It should be noted that after the AST method in step 2 has completed its traversal, step 7 is executed.
[0109] Figure 6 A schematic flowchart of an exemplary call chain analysis provided for embodiments of this application; such as Figure 6 As shown, the specific steps are as follows: 1. Construct a call relationship graph.
[0110] It should be noted that the project's call graph is built using the open-source tool pyan.
[0111] 2. Obtain all call chains.
[0112] It should be noted that all call chains are obtained by calling the relationship graph.
[0113] 3. Traverse the call chain.
[0114] 4. Check if the call chain contains the affected method function.
[0115] It should be noted that the function in the call chain is one of the affected functions identified above. If it is, the process jumps to step 5; otherwise, it jumps to step 3 to continue traversing the call chain.
[0116] 5. Save the affected call chain.
[0117] It should be noted that the current call chain contains affected method functions, and the current call chain is saved as the affected call chain.
[0118] 6. End.
[0119] It should be noted that after the traversal of the call chain in step 3 is completed, step 6 is executed.
[0120] Figure 7 This application provides an exemplary flowchart of API interface impact analysis. Figure 7 As shown, the specific steps are as follows: 1. Obtain the affected call chain.
[0121] It should be noted that the affected call chain can also be a list of affected call chains.
[0122] 2. Traverse the affected call chain.
[0123] 3. Whether the starting node of the call chain is an API route.
[0124] It should be noted that if the starting node of the current call chain is an API route node (its specific format is determined by the current project framework; for example, FastAPI route nodes are identified by the prefix @Router), then jump to step 4; otherwise, return to step 2 to traverse the call chain.
[0125] 4. Output the current call chain.
[0126] It should be noted that the current call chain is a call chain that includes external routing interfaces, and the current call chain is output.
[0127] 5. End.
[0128] It should be noted that step 5 is executed after the traversal of the call chain in step 2 is completed.
[0129] The solution presented in this application can accurately pinpoint the impact of changes at the function level, providing more precise results than traditional file-level or line-level analysis. This enables developers and testers to more accurately assess the scope of code changes and optimize testing strategies. The entire analysis process is fully automated, requiring no manual intervention, and can be integrated into the CI / CD workflow to achieve automatic impact analysis after code commits.
[0130] This application provides a code impact analysis device. Figure 8 This is a schematic diagram of the structure of a code impact analysis device provided in an embodiment of this application; as shown below. Figure 8 As shown, the code impact analysis device 800 includes: Unit 801 is used to obtain the range of line numbers corresponding to the code change file after code submission. Analysis unit 802 is used to analyze the code change file using an abstract syntax tree to obtain the range of method line numbers corresponding to the first method function; The determining unit 803 is used to determine whether the first method function is an affected function based on the change line number range and the method line number range.
[0131] Optionally, the determining unit 803 is further configured to obtain the change start line number and change end line number corresponding to the change line number range; obtain the method start line number and method end line number corresponding to the method line number range; and determine whether the first method function is an affected function based on the change start line number and the method end line number, and the change end line number and the method start line number.
[0132] Optionally, the determining unit 803 is further configured to determine the first method function as an affected function when the method termination line number is greater than or equal to the change start line number and the method start line number is less than or equal to the change termination line number.
[0133] Optionally, the analysis unit 802 is further configured to analyze the code change file using the abstract syntax tree to obtain one or more nodes corresponding to the abstract syntax tree; if the first node among the one or more nodes is a first method, record the first method as the first method function, and obtain the method line number range corresponding to the first method function.
[0134] Optionally, the analysis unit 802 is further configured to: when the first node in one or more nodes is a first type, obtain a second node within the first type; when the second node is the first method, record that the first method is the first method function, and obtain the range of method line numbers corresponding to the first method function.
[0135] Optionally, the acquisition unit 801 is further configured to acquire the first code file after code submission; construct a call relationship graph based on the first code file; and acquire one or more call chains in the call relationship graph. The determining unit 803 is further configured to determine that the first call chain is an affected call chain if the first call chain in one or more call chains contains the affected function.
[0136] Optionally, the determining unit 803 is further configured to determine that the first call chain is a call chain containing an external routing interface and output the first call chain when the first call chain is an affected call chain and the starting node of the first call chain is an API routing node.
[0137] This application also provides an electronic device. Figure 9 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application; as shown below. Figure 9 As shown, the electronic device 900 includes a processor 901 and a memory 903. Optionally, the electronic device 900 may also include a communication bus 902.
[0138] In specific embodiments, the processor 901 described above can be at least one of the following: Application Specific Integrated Circuit (ASIC), Digital Signal Processor (DSP), Digital Signal Processing Device (DSPD), Programmable Logic Device (PLD), Field Programmable Gate Array (FPGA), CPU, controller, microcontroller, and microprocessor. It is understood that for different devices, the electronic device used to implement the above processor function can also be other types, and this embodiment does not specifically limit it.
[0139] In this embodiment, the communication bus 902 is used to establish communication between the processor 901 and the memory 903; when the processor 901 executes the running program stored in the memory 903, it implements the following code impact analysis method: Obtain the range of line numbers corresponding to the code change file after code submission; analyze the code change file using an abstract syntax tree to obtain the range of line numbers corresponding to the first method function; determine whether the first method function is an affected function based on the range of line numbers and the range of line numbers.
[0140] Furthermore, the processor 901 is also configured to obtain the change start line number and change end line number corresponding to the change line number range; obtain the method start line number and method end line number corresponding to the method line number range; and determine whether the first method function is an affected function based on the change start line number and the method end line number, and the change end line number and the method start line number.
[0141] Furthermore, the processor 901 is also configured to determine that the first method function is an affected function when the method termination line number is greater than or equal to the change start line number and the method start line number is less than or equal to the change termination line number.
[0142] Furthermore, the processor 901 is also used to analyze the code change file using the abstract syntax tree to obtain one or more nodes corresponding to the abstract syntax tree; if the first node in the one or more nodes is a first method, the processor records the first method as the first method function and obtains the range of method line numbers corresponding to the first method function.
[0143] Furthermore, the processor 901 is also configured to, when the first node in the one or more nodes is of a first type, obtain a second node within the first type; and when the second node is the first method, record that the first method is the first method function, and obtain the range of method line numbers corresponding to the first method function.
[0144] Furthermore, the processor 901 is also configured to obtain a first code file after code submission; construct a call relationship graph based on the first code file, and obtain one or more call chains in the call relationship graph; and determine the first call chain as the affected call chain if the first call chain in one or more call chains contains the affected function.
[0145] Furthermore, the processor 901 is also configured to determine that the first call chain is a call chain containing an external routing interface and output the first call chain when the first call chain is an affected call chain and the starting node of the first call chain is an API routing node.
[0146] This application provides a storage medium storing a computer program thereon. The computer-readable storage medium stores one or more programs, which can be executed by one or more processors. The computer program implements the code impact analysis method described above.
[0147] Based on the above embodiments, this application provides a computer program product, including a computer program that can be executed by one or more processors, and the computer program implements the code impact analysis method described above.
[0148] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0149] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this disclosure, in essence, or the part that contributes to the related technology, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes several instructions to cause an image display device (which may be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in the various embodiments of this disclosure.
[0150] The above are merely embodiments of this application and are not intended to limit the scope of protection of this application. Any modifications, equivalent substitutions, and improvements made within the spirit and scope of this application are included within the scope of protection of this application.
Claims
1. A code impact analysis method, characterized in that, The method includes: Get the range of line numbers corresponding to the code change file after code commit; The code modification file is analyzed using an abstract syntax tree to obtain the range of method line numbers corresponding to the first method function; Based on the range of changed line numbers and the range of method line numbers, determine whether the first method function is an affected function.
2. The method according to claim 1, characterized in that, Determining whether the first method function is affected based on the changed line number range and the method line number range includes: Obtain the start and end line numbers of the change corresponding to the range of change line numbers; Obtain the method start line number and method end line number corresponding to the method line number range; Whether the first method function is an affected function is determined based on the change start line number and the method end line number, and the change end line number and the method start line number.
3. The method according to claim 2, characterized in that, The step of determining whether the first method function is an affected function based on the change start line number and the method end line number, and the change end line number and the method start line number, includes: If the method termination line number is greater than or equal to the change start line number, and the method start line number is less than or equal to the change termination line number, then the first method function is determined to be the affected function.
4. The method according to claim 1, characterized in that, The code modification file is analyzed using an abstract syntax tree to obtain the range of method line numbers corresponding to the first method function, including: The code change file is analyzed using the abstract syntax tree to obtain one or more nodes corresponding to the abstract syntax tree; If the first node in one or more nodes is a first method, record the first method as the first method function, and obtain the range of method line numbers corresponding to the first method function.
5. The method according to claim 4, characterized in that, The method further includes: If the first node in one or more nodes is of the first type, then obtain the second node within the first type; When the second node is the first method, record that the first method is the first method function, and obtain the range of method line numbers corresponding to the first method function.
6. The method according to claim 1, characterized in that, The method further includes: Get the first code file after code submission; Construct a call relationship graph based on the first code file, and obtain one or more call chains in the call relationship graph; If the affected function is contained in the first call chain of one or more call chains, the first call chain is determined to be the affected call chain.
7. The method according to claim 6, characterized in that, The method further includes: If the first call chain is an affected call chain and the starting node of the first call chain is an application programming interface (API) routing node, then the first call chain is determined to be a call chain containing an external routing interface, and the first call chain is output.
8. A code influence analysis device, characterized in that, The device includes: The retrieval unit is used to retrieve the range of line numbers corresponding to the code change files after code submission. The analysis unit is used to analyze the code change file using an abstract syntax tree to obtain the range of method line numbers corresponding to the first method function; The determining unit is used to determine whether the first method function is an affected function based on the change line number range and the method line number range.
9. An electronic device, characterized in that, include: The processor and the memory used to store computer programs that can run on the processor. When the processor is used to run the computer program, it performs the steps of the method according to any one of claims 1 to 7.
10. A computer program product comprising a computer program or computer-executable instructions, characterized in that, When the computer program or computer-executable instructions are executed by a processor, they implement the steps of the method according to any one of claims 1 to 7.