Code analysis method and device, electronic equipment and storage medium

By dividing large code repositories into functional modules and performing distributed computing, the low efficiency of existing tools in large git repositories is solved, and efficient code analysis and result accuracy are achieved.

CN120631431APending Publication Date: 2025-09-12YUANQIXIN (SHANDONG) SEMICONDUCTOR TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510505840.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-22
Publication Date
2025-09-12

AI Technical Summary

Technical Problem

Existing code analysis tools are inefficient when dealing with large git repositories, are limited in the amount of code they can analyze, and have difficulty effectively utilizing multi-core processors and distributed computing resources.

Method used

By dividing the code repository into blocks based on functional modules, building a code dependency graph, and using distributed computing framework and load balancing technology, the functional code blocks are analyzed separately and the results are integrated.

Benefits of technology

It improves the efficiency and applicability of code analysis, reduces data dependency and coupling, can handle ultra-large-scale code repositories, supports multi-language hybrid projects, and reduces computing resource consumption.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120631431A_ABST
    Figure CN120631431A_ABST
Patent Text Reader

Abstract

The invention provides a code analysis method and device, electronic equipment and a storage medium, and the method comprises the steps: carrying out the code partitioning of a to-be-analyzed code warehouse based on a function module of the to-be-analyzed code warehouse under the condition that the to-be-analyzed code warehouse is analyzed for the first time, and obtaining function code blocks; and performing code analysis on each function code block to obtain a block analysis result of each function code block. And integrating block analysis results of the function code blocks to obtain a code warehouse analysis result. The method provided by the invention comprises the following steps: performing code partitioning on a to-be-analyzed code warehouse through a functional module of the to-be-analyzed code warehouse to obtain functional code blocks; according to the method, code analysis is performed on each function code block to obtain a block analysis result of each function code block, and the block analysis results of the function code blocks are integrated to obtain a code warehouse analysis result, so that the data dependence and the coupling degree in the analysis process are reduced, and the analysis efficiency and applicability of code analysis are greatly improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a code analysis method, device, electronic device and storage medium. Background Art

[0002] During the code review process, code analysis is crucial for quickly becoming familiar with Git repositories. However, existing code analysis tools have many shortcomings when working with large Git repositories. While some current code analysis tools can provide some code analysis capabilities, large Git repositories contain numerous code files and complex dependencies. Analysis consumes significant memory and computing resources, resulting in low analysis efficiency and even failure due to resource exhaustion.

[0003] Therefore, current code analysis tools are not very efficient in code analysis and have certain limitations on the amount of code to be analyzed. Summary of the Invention

[0004] The present invention provides a code analysis method, device, electronic device and storage medium to address the defects in the prior art that the efficiency of current code analysis is low and there is a certain limit on the amount of code to be analyzed.

[0005] The present invention provides a code analysis method, comprising: Get the code repository to be analyzed; In a case where the code repository to be analyzed is analyzed for the first time, dividing the code repository to be analyzed into code blocks based on the functional modules of the code repository to be analyzed to obtain functional code blocks; Perform code analysis on each functional code block respectively to obtain block analysis results of each functional code block; The block analysis results of the functional code blocks are integrated to obtain the code warehouse analysis results of the code warehouse to be analyzed.

[0006] According to a code analysis method provided by the present invention, the code repository to be analyzed is divided into code blocks based on the functional modules of the code repository to be analyzed to obtain functional code blocks, including: Performing semantic analysis on the code repository to be analyzed to obtain static code semantics; Analyze the code comments of the code repository to be analyzed to obtain function annotation information; Analyze the code naming rules of the code repository to be analyzed to obtain inferred code semantics; Based on the static code semantics, the functional annotation information and the inferred code semantics, call relationship analysis is performed to construct a code dependency graph; Identify the code dependency graph and obtain the functional module; Based on the functional modules and code block rules, the code repository to be analyzed is divided into code blocks to obtain functional code blocks; The code block rule is determined based on the amount of code corresponding to the functional modules.

[0007] According to a code analysis method provided by the present invention, the code analysis is performed on each functional code block to obtain the block analysis results of each functional code block, including: Evaluate the current resource information of each computing node in the computing cluster to determine the current computing capacity of each computing node; Matching the analysis computing nodes corresponding to the functional code blocks based on the current computing capabilities of the computing nodes and the code size of the functional code blocks; Based on the analysis and calculation nodes corresponding to the functional code blocks, code analysis is performed on the functional code blocks respectively to obtain block analysis results of the functional code blocks.

[0008] According to a code analysis method provided by the present invention, the current resource information includes at least one of current computing resources, current memory resources, and current network resources.

[0009] According to a code analysis method provided by the present invention, the step of obtaining a code repository to be analyzed further includes: If the code repository to be analyzed is not analyzed for the first time and the current code version number of the code repository to be analyzed does not belong to a historical version number, determining an updated functional code block of the code repository to be analyzed; Performing code analysis on the update function code blocks to obtain update code analysis results; The updated code analysis result and the historical code analysis result are integrated to obtain the code repository analysis result.

[0010] According to a code analysis method provided by the present invention, the historical code analysis result is the code analysis result corresponding to the original function code block, and the original function code block is the function code block excluding the updated function code block in the function code block.

[0011] According to a code analysis method provided by the present invention, the block analysis results of the functional code blocks are integrated to obtain the code warehouse analysis results of the code warehouse to be analyzed, and then the method includes: The repository identifier of the code repository to be analyzed is used as a cache key, and the current code version number of the code repository to be analyzed and the code repository analysis result are stored as cache values.

[0012] According to a code analysis method provided by the present invention, the repository identifier includes a code project name and a code branch name.

[0013] The present invention also provides a code analysis device, comprising: Get the unit and get the code repository to be analyzed; A code block unit, when the code repository to be analyzed is analyzed for the first time, divides the code repository to be analyzed into code blocks based on the functional modules of the code repository to be analyzed to obtain functional code blocks; A code analysis unit performs code analysis on each functional code block to obtain a block analysis result of each functional code block; The result integration unit integrates the block analysis results of the functional code blocks to obtain the code warehouse analysis results of the code warehouse to be analyzed.

[0014] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, any of the above-described code analysis methods is implemented.

[0015] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which implements any of the above-mentioned code analysis methods when executed by a processor.

[0016] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements any one of the above-mentioned code analysis methods.

[0017] The code analysis method, device, electronic device and storage medium provided by the present invention divide the code warehouse to be analyzed into code blocks through the functional modules of the code warehouse to be analyzed to obtain functional code blocks; perform code analysis on each functional code block separately to obtain the block analysis results of each functional code block, and integrate the block analysis results of the functional code blocks to obtain the code warehouse analysis results, thereby reducing data dependence and coupling in the analysis process, thereby greatly improving the analysis efficiency and applicability of code analysis. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] In order to more clearly illustrate the technical solutions in the present invention or the prior art, a brief introduction is given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0019] Figure 1 This is one of the flow charts of the code analysis method provided by the present invention; Figure 2 This is the second flow chart of the code analysis method provided by the present invention; Figure 3 This is the third flow chart of the code analysis method provided by the present invention; Figure 4 It is a structural diagram of the code analysis device provided by the present invention; Figure 5 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0020] To make the objectives, technical solutions, and advantages of the present invention more clear, the technical solutions of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the embodiments described are only some of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts shall fall within the scope of protection of the present invention.

[0021] In view of the above problems, the present invention provides a code analysis method to achieve efficient code analysis. Figure 1 This is one of the flow charts of the code analysis method provided by the present invention, such as Figure 1 As shown, the method includes: Step 110: Obtain the code repository to be analyzed; Here, the code repository to be analyzed refers to the project code to be analyzed. The code repository here can refer to all the codes corresponding to a single project.

[0022] Specifically, you can clone or pull the code repository to be analyzed from the code hosting platform through APIs (such as the GitHub API and GitLab API) or local Git command-line tools. It should be noted that the code repository to be analyzed can contain thousands of code files, and the entire repository can be over 500MB in size. Furthermore, a single repository to be analyzed can involve multiple programming languages, such as Python, JavaScript, and SQL.

[0023] In addition, the code repository to be analyzed has complex dependencies and business logic. For example, the code repository to be analyzed here can be a shopping project.

[0024] Step 120: If the code repository to be analyzed is being analyzed for the first time, the code repository to be analyzed is divided into code blocks based on the functional modules of the code repository to be analyzed to obtain functional code blocks. Specifically, a resource library can be established to store the warehouse code analysis results of the code repository to be analyzed. The resource library can store the warehouse code analysis results of the completed code analysis. Therefore, after obtaining the code repository to be analyzed, it is first necessary to determine whether the analysis results of this code repository to be analyzed exist in the resource library. For example, the hash value of the code repository to be analyzed can be calculated based on the warehouse name and branch name of the code repository to be analyzed. The calculated hash value can be retrieved in the resource library. If the hash value is not retrieved, it means that the code repository to be analyzed is being analyzed for the first time.

[0025] Furthermore, when the code repository to be analyzed is being analyzed for the first time, the functional modules of the code repository to be analyzed can be determined. For example, static semantic analysis can be performed on the code repository to be analyzed, and the abstract syntax tree can be used to parse the code structure of the code repository to obtain the functional modules.

[0026] Then, the code can be divided into blocks based on the functional modules and the code repository to be analyzed, obtaining the functional code blocks corresponding to each functional module. For example, if the code repository to be analyzed is the code repository corresponding to a shopping item, the functional modules of this code repository to be analyzed may include multiple functional modules such as user management, product management, order management, and payment management. The code repository to be analyzed can then be divided into functional code blocks corresponding to these modules.

[0027] It should be noted that based on the functional modules of the code repository to be analyzed, the code repository to be analyzed is divided into code blocks to obtain functional code blocks, which realizes code blocks that conform to the business logic of the code repository to be analyzed, ensures the integrity of the functional code blocks after segmentation, and thus ensures the accuracy of subsequent code analysis of the functional code blocks.

[0028] Furthermore, large code repositories to be analyzed are divided into multiple independent analysis units, or functional code blocks, based on the code's functional modules, business logic, and dependencies. Each unit has relatively independent functionality and clear interface definitions. This block division strategy emphasizes the semantics of the code, allowing each functional code block to be analyzed independently, reducing data dependencies and coupling during the analysis process.

[0029] Step 130: performing code analysis on each functional code block to obtain a block analysis result of each functional code block; Specifically, the computing nodes can be used to perform code analysis on each functional code block to obtain block analysis results for each functional code block. For example, the functional code blocks can be analyzed using open source code analysis tools or custom analysis scripts.

[0030] It's understandable that compared to the massive code repository to be analyzed, the code size and complexity of a single functional code block is far smaller than the code repository to be analyzed. Therefore, splitting the massive code repository to be analyzed into multiple functional code blocks allows for code analysis of larger code repositories, improving the applicability of code analysis methods.

[0031] Moreover, the speed of code analysis for functional code blocks with small code volume and low complexity is much higher than that for code analysis for code repositories with large code volume and high complexity to be analyzed, thereby greatly improving the efficiency of code analysis for code repositories to be analyzed and improving the real-time performance and efficiency of code analysis.

[0032] Step 140 : Integrate the block analysis results of the functional code blocks to obtain a code warehouse analysis result of the code warehouse to be analyzed.

[0033] Specifically, the code repository analysis results for the code repository to be analyzed can be obtained by integrating the block analysis results of the functional code blocks. For example, the block analysis results output by each computing node can be sent to a central server, which then aggregates the block analysis results to obtain the code repository analysis results. The code repository analysis results can be presented in the form of an analysis report.

[0034] The method provided by the present invention divides the code warehouse to be analyzed into code blocks through the functional modules of the code warehouse to be analyzed to obtain functional code blocks; performs code analysis on each functional code block separately to obtain block analysis results of each functional code block, and integrates the block analysis results of the functional code blocks to obtain code warehouse analysis results, thereby reducing data dependence and coupling in the analysis process, thereby greatly improving the analysis efficiency and applicability of code analysis.

[0035] It should be noted that existing tools lack effective block partitioning strategies when processing large Git repositories, making it difficult to reasonably split Git repositories into independently analyzable code units. This makes it difficult to parallelize the analysis process and to fully utilize multi-core processors and distributed computing resources. To address this issue, based on any of the above embodiments, in step 120, the code repository to be analyzed is partitioned based on the functional modules of the code repository to be analyzed, obtaining functional code blocks, including: Performing semantic analysis on the code repository to be analyzed to obtain static code semantics; Analyze the code comments of the code repository to be analyzed to obtain function annotation information; Analyze the code naming rules of the code repository to be analyzed to obtain inferred code semantics; Based on the static code semantics, the functional annotation information and the inferred code semantics, call relationship analysis is performed to construct a code dependency graph; Identify the code dependency graph and obtain the functional module; Based on the functional modules and code block rules, the code repository to be analyzed is divided into code blocks to obtain functional code blocks; The code block rule is determined based on the amount of code corresponding to the functional modules.

[0036] Specifically, first, you can use professional code analysis tools or custom scripts to perform static semantic analysis on the code repository to obtain static code semantics. For example, you can parse the code repository using an abstract syntax tree, traverse the syntax tree nodes, and extract semantic elements such as variables, functions, and classes in the code repository and their relationships to obtain static code semantics.

[0037] Furthermore, large-scale language models can be used to analyze the code comments in the code repository to achieve semantic understanding of the code comments and obtain functional annotation information. It is understood that code comments often contain natural language descriptions of the code's function, design intent, and usage. Therefore, the functional annotation information obtained by semantic understanding based on code comments can serve as a supplement to static code semantics.

[0038] In addition, we can analyze the naming conventions of the code repository to obtain inferred code semantics. For example, by analyzing the naming conventions of the code, we can automatically infer the code function or intention, which also serves as a supplement to the static code semantics.

[0039] Therefore, the present embodiment does not specifically limit the order in which static code semantics, functional annotation information, and inferred code semantics are obtained. After obtaining the static code semantics, functional annotation information, and inferred code semantics, the static code semantics, the functional annotation information, and the inferred code semantics can be input into a large language model, and the large language model can be used to perform call relationship analysis to obtain the call relationships between code modules. The call relationships between code modules can then be used to construct a code dependency graph.

[0040] It should be noted that the code dependency graph here can be used to reflect the calling relationship between code modules in the code repository to be analyzed. The code modules can be used as nodes, and the calling relationship between code modules can be used as connecting edges.

[0041] Furthermore, the functional modules can be obtained by identifying the code dependency graph. For example, the nodes in the code dependency graph can be identified using a graph algorithm, and the code module corresponding to each identified node is used as the corresponding functional module.

[0042] Finally, the code repository to be analyzed can be segmented into functional code blocks using functional modules and code segmentation rules. This allows each functional module to be treated as an independent analysis unit, ensuring functional independence and clear interface definitions. For example, the user management module interacts with other modules through APIs. Detailed interface information for cross-functional code block dependencies is recorded. For example, when the order management module calls the get_user_info(user_id) interface of the user management module, relevant information is recorded.

[0043] The code block rule here can be determined by the amount of code corresponding to the functional modules. For example, when the amount of code corresponding to the functional modules is large, the code block rule may be to divide the code corresponding to each functional module into a separate functional code block; when the amount of code corresponding to the functional modules is small, the code block rule may be to divide the code corresponding to multiple functional modules into one functional code block.

[0044] The method provided by the present invention obtains static code semantics by performing semantic analysis on the code repository to be analyzed; obtains functional annotation information by analyzing the code comments of the code repository to be analyzed; obtains inferred code semantics by analyzing the code naming rules of the code repository to be analyzed; performs call relationship analysis based on the static code semantics, functional annotation information and inferred code semantics, and constructs a code dependency graph; identifies the code dependency graph to obtain functional modules; and performs code segmentation on the code repository to be analyzed based on the functional modules and code segmentation rules to obtain functional code segments, thereby achieving accurate and business-logical code segmentation. The segmentation strategy not only considers the physical structure of the code (such as the file directory structure), but also pays more attention to the semantic information of the code, thereby reducing data dependence and coupling during the analysis process.

[0045] Based on any of the above embodiments, step 130 includes: Evaluate the current resource information of each computing node in the computing cluster to determine the current computing capacity of each computing node; Matching the analysis computing nodes corresponding to the functional code blocks based on the current computing capabilities of the computing nodes and the code size of the functional code blocks; Based on the analysis and calculation nodes corresponding to the functional code blocks, code analysis is performed on the functional code blocks respectively to obtain block analysis results of the functional code blocks.

[0046] Here, the computer cluster may be composed of multiple servers, which may include a central node and a computing node. The central node may select a separate server, or a computing node with stronger computing power may be used as the central node.

[0047] Specifically, the current resource information of each computing node in the computing cluster can be obtained. Then, by evaluating the current resource information of each computing node, the current computing capacity of each computing node can be determined. The current resource information here can refer to resource information such as the CPU, memory, and network bandwidth of each computing node at the current moment. It is understood that the richer the current resource information of a computing node, the stronger the current computing capacity of the computing node; conversely, the weaker the current computing capacity of the computing node.

[0048] Furthermore, the analysis computing nodes corresponding to each functional code block can be matched based on the current computing power of each computing node and the code size of each functional code block. For example, the larger the code size of a functional code block, the more complex the code, and the more powerful computing nodes are needed for code analysis. Conversely, the smaller the code size of a functional code block, the less complex the code, and the less powerful computing nodes can be used for code analysis.

[0049] Finally, code analysis can be performed on each functional code block using the analysis and computation nodes corresponding to each functional code block to obtain block analysis results for each functional code block. It is understood that functional code blocks with large code volumes may correspond to multiple analysis and computation nodes. For functional code blocks with small code volumes, multiple functional code blocks with small code volumes can be assigned to the same analysis and computation node.

[0050] The method provided by the embodiment of the present invention determines the current computing capacity of each computing node by evaluating the current resource information of each computing node in the computing cluster; matches the analysis computing node corresponding to each functional code block based on the current computing capacity of each computing node and the code amount of each functional code block; based on the analysis computing node corresponding to each functional code block, performs code analysis on each functional code block separately to achieve reasonable computing resource allocation, thereby improving the overall efficiency of code analysis.

[0051] Based on any of the above embodiments, the current resource information includes at least one of current computing resources, current memory resources, and current network resources.

[0052] It should be noted that for any compute node, the node's current computing resources, current memory resources, and current network resources can be used to assess the node's real-time performance and load. This allows for load balancing across the entire computing cluster by matching the analysis compute nodes to each functional code block based on the node's current computing power and the amount of code in each functional code block.

[0053] The method provided by the embodiments of the present invention utilizes a distributed computing framework to distribute the divided functional code blocks to multiple computing nodes for parallel processing. Each computing node independently completes the analysis task for the functional code block it is responsible for, and then aggregates the block analysis results to a central node for integration and verification. This distributed collaborative analysis architecture fully utilizes multi-core processors and distributed computing resources, significantly improving analysis performance and scalability.

[0054] Based on any of the above embodiments, step 110 may further include: If the code repository to be analyzed is not analyzed for the first time and the current code version number of the code repository to be analyzed does not belong to a historical version number, determining an updated functional code block of the code repository to be analyzed; Performing code analysis on the update function code blocks to obtain update code analysis results; The updated code analysis result and the historical code analysis result are integrated to obtain the code repository analysis result.

[0055] Specifically, a resource library can be established to store the warehouse code analysis results of the code repository to be analyzed, and the resource library can store the warehouse code analysis results of the completed code analysis. Therefore, after obtaining the code repository to be analyzed, it can be determined whether the analysis results of the code repository to be analyzed exist in the resource library. For example, the hash value of the code repository to be analyzed can be calculated based on the warehouse name and branch name of the code repository to be analyzed. By retrieving the calculated hash value in the resource library, if the analysis result corresponding to the hash value is retrieved, it means that the code repository to be analyzed is not the first analysis.

[0056] Next, you can check whether there are any code updates in the code repository to be analyzed. You can search by the code version number of the code repository to be analyzed to see if the code version number belongs to a historical version number. If the code version number of the code repository to be analyzed is not retrieved in the historical version number, it means that there is incremental code in the current code repository to be analyzed.

[0057] Therefore, if the code repository to be analyzed is not being analyzed for the first time and the current code version number of the code repository to be analyzed is not a historical version number, code tools such as git can be used to determine the updated functional code blocks of the code repository to be analyzed, that is, to determine the incremental code in the code repository to be analyzed. It is understood that the updated functional code blocks here belong to the functional code blocks of the code repository to be analyzed and include at least one functional code block.

[0058] Furthermore, the code analysis tool can be used to perform code analysis on the update function code block to obtain the update code analysis results corresponding to the update function code block. It should be noted that the embodiment of the present invention performs code analysis on all the codes of the update function code block, that is, it includes the original code and incremental code of the update function code block, so that the obtained update code analysis results are more accurate. It is understandable that when performing code analysis on the update function code block, distributed task allocation can still be used. Finally, the update code analysis results and the historical code analysis results are integrated to obtain the code warehouse analysis results.

[0059] The method provided by the present invention compares differences between code versions when a code repository is updated. It then reanalyzes only the updated functional code blocks that have changed, combining them with previous analysis results to quickly generate updated code analysis results. This incremental analysis mechanism significantly reduces analysis time and computing resource consumption, improving both real-time analysis and efficiency.

[0060] Based on any of the above embodiments, the historical code analysis result is a code analysis result corresponding to an original function code block, and the original function code block is a function code block in the function code block excluding the updated function code block.

[0061] Specifically, the function code blocks of the code repository to be analyzed, excluding the updated function code blocks, can be used as the original function code blocks. In the code analysis result integration phase, the updated code analysis results and the historical code analysis results can be summarized to obtain the code repository analysis results.

[0062] Based on any of the above embodiments, step 140 may include: The repository identifier of the code repository to be analyzed is used as a cache key, and the current code version number of the code repository to be analyzed and the code repository analysis result are stored as cache values.

[0063] Based on any of the above embodiments, the repository identifier includes a code project name and a code branch name.

[0064] Here, the repository identifier refers to the code used to identify the code repository to be analyzed, which can be obtained by performing a hash value calculation on the project name and branch name of the code repository to be analyzed.

[0065] It is understandable that a single code repository to be analyzed may include multiple code branches, thus including multiple code branch names. For any code branch, it may include multiple code versions.

[0066] Specifically, after obtaining the code repository analysis results, a caching mechanism can be established to cache the results. The repository identifier of the code repository to be analyzed can be used as the cache key, and the current code version number of the code repository to be analyzed and the code repository analysis results can be stored as the cache value. The repository identifier can be calculated by hashing the project name and branch name of the code repository to be analyzed. The current version number of the code repository to be analyzed can be represented by the commit ID.

[0067] It's important to note that a cache is set up using the project name and branch name of the code repository to be analyzed as the key, and the code analysis results and the current code version number as the cache value. During the code analysis process, the required information is retrieved from the cache first, avoiding repeated parsing and calculations, further improving analysis efficiency.

[0068] Based on any of the above embodiments, Figure 2 This is the second flow chart of the code analysis method provided by the present invention, such as Figure 2 As shown, the method includes: First, obtain the code repository (the repository to be analyzed). Suppose the repository to be analyzed is named shopping, the code branch is release1.1, and the code version commit ID is abc. Then, determine whether the code repository exists in a cache. For example, you can calculate a hash value for the repository name and code branch name. Use the calculated hash value as the key to search the cache.

[0069] If a cache exists, a second query is performed using the code version number as the key to determine whether the code repository has been updated. If the query succeeds, indicating that the code repository has not been updated, the code analysis results in the cache are directly output as the analysis document. If the query fails, indicating that the code repository has been updated, the code repository is incrementally analyzed and tested to obtain change units (updated functional code blocks). Furthermore, the change units are re-tested and integrated with the results. That is, the updated code analysis results and historical code analysis results are integrated to obtain the code repository analysis results. Then, the current code version number and the integrated code repository analysis results are updated in the cache.

[0070] If no cache exists, it means that the code repository is being analyzed for the first time. When the code repository is being analyzed for the first time, the code repository is segmented into functional code segments. Then, for the computing cluster used for code analysis, a distributed task allocation method is used to determine the computing nodes responsible for code analysis of each functional code segment. Specifically, a computing cluster is first built and tasks are assigned. A computing cluster consisting of multiple servers is built, and the performance of each server, such as CPU, memory, and network bandwidth, is evaluated. The segmented functional code is distributed based on performance and load balancing. Servers with strong performance are responsible for functional code segments with large code size and high complexity; conversely, servers with low performance are responsible for functional code segments with small code size and low complexity.

[0071] Each compute node then independently completes the code analysis task for the corresponding functional code block, performing distributed computing and outputting the analysis results for each functional code block. Next, the results are aggregated and consolidated, integrating the analysis results for each functional code block according to business logic to obtain the complete code repository analysis results. The cache is then updated, using the code project name and code branch name as the cache key, and the code repository analysis results and code version number as the cache value. Finally, the analysis document (code repository analysis results) is output.

[0072] In yet another embodiment, Figure 3 This is the third flow chart of the code analysis method provided by the present invention, as shown in FIG. Figure 3 As shown, the method includes: First, obtain the code repository (the repository to be analyzed). Suppose the repository to be analyzed is named shopping, the code branch is release1.1, and the code version commit ID is abc. Then, determine whether the code repository exists in a cache. For example, you can calculate a hash value for the repository name and code branch name. Use the calculated hash value as the key to search the cache.

[0073] If a cache exists, the code version number is used as the key for a secondary query to determine whether the code repository has changed. If the query is successful, it means that there is no change in the code repository, and the code analysis results in the cache are directly output as a code analysis report. If the query fails, it means that there are changes in the code repository. Next, the changed files and units (updated functional code blocks) are determined, and the code repository can be incrementally analyzed and tested to obtain the changed files and units. Further, the changed units are re-analyzed. Finally, the analysis results are integrated and the cache is updated. That is, the updated code analysis results and the historical code analysis results are integrated to obtain the code repository analysis results. Then, the current code version number and the integrated code repository analysis results are updated in the cache.

[0074] If no cache exists, this indicates that the code repository is being analyzed for the first time. In the absence of a cache, static semantic analysis is performed on the code repository. Then, segmentation rules are established. Based on the static semantic analysis results and the established segmentation rules, the code files are divided into functional code segments. Next, a computing cluster is built and tasks are assigned to each computing node. In other words, each computing node is assigned the code analysis task corresponding to the functional code segment. The computing cluster then actively performs distributed code analysis and outputs the segment analysis results corresponding to each functional code segment. The results are then aggregated and the cache is updated. Specifically, the segment analysis results corresponding to each functional code segment can be integrated according to business logic to obtain complete code repository analysis results. The cache is then updated, using the code project name and code branch name as the cache key, and the code repository analysis results and code version number as the cache value. Finally, a code analysis report (code repository analysis results) is output.

[0075] It should be noted that compared to traditional code analysis tools, the method provided by the embodiments of the present invention rationally divides large code repositories to be analyzed into independent units (functional code blocks), breaking through processing scale limitations and easily handling extremely large Git code repositories. Furthermore, the distributed computing framework, coupled with dynamic load balancing, enables code analysis tasks to be performed in parallel, significantly improving analysis efficiency. Both initial and incremental analyses can be completed in a short time, significantly accelerating the process of combing through Git repositories.

[0076] Furthermore, the block strategy reduces the workload of individual computing nodes, and the caching mechanism effectively reduces repeated calculations, comprehensively reducing the consumption of memory, computing resources, and network bandwidth, thereby saving enterprise operation and maintenance costs. The block strategy is highly flexible and can be flexibly adjusted according to the characteristics of different projects and programming languages. It can also properly handle multi-language mixed projects and has excellent scalability. At the same time, based on semantic block segmentation, distributed collaborative analysis, and precise incremental analysis, the accuracy of analysis results is guaranteed, providing a reliable basis for code quality assessment, effectively improving the overall quality of software, and providing solid technical support for software development and management.

[0077] Based on any of the above embodiments, Figure 4 This is a schematic diagram of the structure of the code analysis device provided by the present invention. Figure 4 As shown, the device includes: An acquisition unit 410 acquires a code repository to be analyzed; The code block unit 420 divides the code repository to be analyzed into code blocks based on the functional modules of the code repository to be analyzed to obtain functional code blocks when the code repository to be analyzed is analyzed for the first time. The code analysis unit 430 performs code analysis on each functional code block to obtain a block analysis result of each functional code block; The result integration unit 440 integrates the block analysis results of the functional code blocks to obtain the code warehouse analysis results of the code warehouse to be analyzed.

[0078] The device provided by the present invention divides the code warehouse to be analyzed into code blocks through the functional modules of the code warehouse to be analyzed to obtain functional code blocks; performs code analysis on each functional code block separately to obtain block analysis results of each functional code block, integrates the block analysis results of the functional code blocks to obtain code warehouse analysis results, reduces data dependence and coupling in the analysis process, and thus greatly improves the analysis efficiency and applicability of code analysis.

[0079] Based on any of the above embodiments, the code block unit is specifically used to: Performing semantic analysis on the code repository to be analyzed to obtain static code semantics; Analyze the code comments of the code repository to be analyzed to obtain function annotation information; Analyze the code naming rules of the code repository to be analyzed to obtain inferred code semantics; Based on the static code semantics, the functional annotation information and the inferred code semantics, call relationship analysis is performed to construct a code dependency graph; Identify the code dependency graph and obtain the functional module; Based on the functional modules and code block rules, the code repository to be analyzed is divided into code blocks to obtain functional code blocks; The code block rule is determined based on the amount of code corresponding to the functional modules.

[0080] Based on any of the above embodiments, the code analysis unit is specifically used to: Evaluate the current resource information of each computing node in the computing cluster to determine the current computing capacity of each computing node; Matching the analysis computing nodes corresponding to the functional code blocks based on the current computing capabilities of the computing nodes and the code size of the functional code blocks; Based on the analysis and calculation nodes corresponding to the functional code blocks, code analysis is performed on the functional code blocks respectively to obtain block analysis results of the functional code blocks.

[0081] Based on any of the above embodiments, the current resource information includes at least one of current computing resources, current memory resources, and current network resources.

[0082] Based on any of the above embodiments, the acquisition unit further includes an incremental analysis unit, which is specifically configured to: If the code repository to be analyzed is not analyzed for the first time and the current code version number of the code repository to be analyzed does not belong to a historical version number, determining an updated functional code block of the code repository to be analyzed; Performing code analysis on the update function code blocks to obtain update code analysis results; The updated code analysis result and the historical code analysis result are integrated to obtain the code repository analysis result.

[0083] Based on any of the above embodiments, the historical code analysis result is a code analysis result corresponding to an original function code block, and the original function code block is a function code block in the function code block excluding the updated function code block.

[0084] Based on any of the above embodiments, the result integration unit includes a cache unit, and the cache is specifically used to: The repository identifier of the code repository to be analyzed is used as a cache key, and the current code version number of the code repository to be analyzed and the code repository analysis result are stored as cache values.

[0085] Based on any of the above embodiments, the repository identifier includes a code project name and a code branch name.

[0086] Figure 5 An example of a physical structure diagram of an electronic device is shown below. Figure 5 As shown, the electronic device may include: a processor 510, a communications interface 520, a memory 530, and a communications bus 540, wherein the processor 510, the communications interface 520, and the memory 530 communicate with each other via the communications bus 540. The processor 510 may call logic instructions in the memory 530 to execute a code analysis method, which includes: obtaining a code repository to be analyzed; when the code repository to be analyzed is being analyzed for the first time, dividing the code repository to be analyzed into code blocks based on the functional modules of the code repository to be analyzed to obtain functional code blocks; performing code analysis on each functional code block separately to obtain block analysis results for each functional code block; and integrating the block analysis results of the functional code blocks to obtain code repository analysis results for the code repository to be analyzed.

[0087] Furthermore, the logic instructions in the aforementioned memory 530 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the portion that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, a mobile hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.

[0088] On the other hand, the present invention also provides a computer program product, which includes a computer program, which can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the code analysis method provided by the above methods, which includes: obtaining a code warehouse to be analyzed; when the code warehouse to be analyzed is analyzed for the first time, based on the functional modules of the code warehouse to be analyzed, the code warehouse to be analyzed is divided into code blocks to obtain functional code blocks; code analysis is performed on each functional code block separately to obtain block analysis results of each functional code block; and the block analysis results of the functional code blocks are integrated to obtain code warehouse analysis results of the code warehouse to be analyzed.

[0089] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the code analysis method provided by the above-mentioned methods, the method comprising: obtaining a code warehouse to be analyzed; when the code warehouse to be analyzed is analyzed for the first time, based on the functional modules of the code warehouse to be analyzed, performing code block analysis on the code warehouse to be analyzed to obtain functional code blocks; performing code analysis on each functional code block separately to obtain block analysis results of each functional code block; integrating the block analysis results of the functional code blocks to obtain code warehouse analysis results of the code warehouse to be analyzed.

[0090] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, i.e., they may be located in one location or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of the present embodiment. Persons of ordinary skill in the art will be able to understand and implement the present invention without inventive effort.

[0091] Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented using software plus a necessary general-purpose hardware platform, or of course, hardware. Based on this understanding, the essence of the above technical solution, or the portion that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a magnetic disk, or an optical disk, and includes a number of instructions for causing a computer device (such as a personal computer, server, or network device) to execute the methods described in each embodiment or certain portions of the embodiments.

[0092] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the various embodiments of the present invention.

Claims

1. A code analysis method, characterized in that: include: Get the code repository to be analyzed; In a case where the code repository to be analyzed is analyzed for the first time, dividing the code repository to be analyzed into code blocks based on the functional modules of the code repository to be analyzed to obtain functional code blocks; Perform code analysis on each functional code block respectively to obtain block analysis results of each functional code block; The block analysis results of the functional code blocks are integrated to obtain the code warehouse analysis results of the code warehouse to be analyzed.

2. The code analysis method according to claim 1, characterized in that The function module based on the code repository to be analyzed, dividing the code repository to be analyzed into code blocks to obtain function code blocks, includes: Performing semantic analysis on the code repository to be analyzed to obtain static code semantics; Analyze the code comments of the code repository to be analyzed to obtain function annotation information; Analyze the code naming rules of the code repository to be analyzed to obtain inferred code semantics; Based on the static code semantics, the functional annotation information and the inferred code semantics, call relationship analysis is performed to construct a code dependency graph; Identify the code dependency graph and obtain the functional module; Based on the functional modules and code block rules, the code repository to be analyzed is divided into code blocks to obtain functional code blocks; The code block rule is determined based on the amount of code corresponding to the functional modules.

3. The code analysis method according to claim 1, wherein: The code analysis is performed on each functional code block to obtain the block analysis results of each functional code block, including: Evaluate the current resource information of each computing node in the computing cluster to determine the current computing capacity of each computing node; Matching the analysis computing nodes corresponding to the functional code blocks based on the current computing capabilities of the computing nodes and the code size of the functional code blocks; Based on the analysis and calculation nodes corresponding to the functional code blocks, code analysis is performed on the functional code blocks respectively to obtain block analysis results of the functional code blocks.

4. The code analysis method according to claim 3, characterized in that The current resource information includes at least one of current computing resources, current memory resources, and current network resources.

5. The code analysis method according to any one of claims 1 to 4, characterized in that: The step of obtaining the code repository to be analyzed further includes: If the code repository to be analyzed is not analyzed for the first time and the current code version number of the code repository to be analyzed is not a historical version number, determining an updated functional code block of the code repository to be analyzed; Performing code analysis on the update function code blocks to obtain update code analysis results; The updated code analysis result and the historical code analysis result are integrated to obtain the code repository analysis result.

6. The code analysis method according to claim 5, characterized in that: The historical code analysis result is a code analysis result corresponding to an original function code block, and the original function code block is a function code block in the function code block excluding the updated function code block.

7. The code analysis method according to any one of claims 1 to 4, characterized in that: The step of integrating the block analysis results of the functional code blocks to obtain the code warehouse analysis results of the code warehouse to be analyzed further includes: The repository identifier of the code repository to be analyzed is used as a cache key, and the current code version number of the code repository to be analyzed and the code repository analysis result are stored as cache values.

8. The code analysis method according to claim 7, characterized in that: The repository identifier includes a code project name and a code branch name.

9. A code analysis device, characterized in that: include: Get the unit and get the code repository to be analyzed; A code block unit, when the code repository to be analyzed is analyzed for the first time, divides the code repository to be analyzed into code blocks based on the functional modules of the code repository to be analyzed to obtain functional code blocks; A code analysis unit performs code analysis on each functional code block to obtain a block analysis result of each functional code block; The result integration unit integrates the block analysis results of the functional code blocks to obtain the code warehouse analysis results of the code warehouse to be analyzed.

10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the code analysis method according to any one of claims 1 to 8 is implemented.