A code review method and device, electronic equipment and storage medium
Patent Information
- Application Number
- CN202611151668.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-30
- Publication Date
- 2026-10-13
AI Technical Summary
[0003]本申请提供了一种代码审查方法、装置、电子设备及存储介质,以至少解决相关技术中代码审查无法跨模块或跨文件分析,进而导致代码审查准确性较低的问题
[0009]通过本申请,由于通过获取全局代码和代码修改内容,根据全局代码,得到至少一个符号实体,根据符号实体和全局代码表征的语义关系,生成全局调用关系向量表,根据代码修改内容,确定至少一个目标符号实体,根据目标符号实体和代码修改内容表征的语义关系,在全局调用关系向量表中,确定至少一个关联调用关系向量,根据至少一个关联调用关系向量,生成目标代码修改,以使用户根据目标代码修改意见进行全局代码修改,通过全局调用关系向量表确定目标符号实体在全局代码的关系调用关系向量,实现了跨模块或跨文件分析,避免了相关联的调用被遗漏修改,提升了代码审查的准确性。
Smart Images

Figure CN122903300A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and more specifically to a code review method, apparatus, electronic device, and storage medium. Background Technology
[0002] As business requirements evolve, functions serve as the basic encapsulation unit during the independent development of each functional module. These functions may be called by other modules or files during use, resulting in complex call chains between functions in the codebase. While static analysis tools are typically used to analyze single modules or files, they fail to analyze the impact of changes on other modules or files when users modify the current module's interface. This leads to missed modifications of related calls, resulting in numerous code errors and defects during runtime. Summary of the Invention
[0003] This application provides a code review method, apparatus, electronic device, and storage medium to at least solve the problem in related technologies that code review cannot be analyzed across modules or files, resulting in low accuracy of code review.
[0004] This application provides a code review method, including: Get global code and code modifications; Based on the global code, at least one symbol entity is obtained; For any symbolic entity, generate a global call relationship vector table based on the semantic relationship between the symbolic entity and the global code representation; Based on the code modifications, identify at least one target symbol entity; For any target symbol entity, based on the semantic relationship between the target symbol entity and the code modification content, determine at least one associated call relationship vector in the global call relationship vector table; Based on at least one associated call relationship vector, target code modification suggestions are generated so that users can make global code modifications according to the target code modification suggestions.
[0005] This application also provides a code review apparatus, including: The acquisition module is used to retrieve global code and code modifications. The first determining module is used to obtain at least one symbol entity based on the global code; The relation table generation module is used to generate a global call relation vector table for any symbolic entity based on the semantic relationship between the symbolic entity and the global code representation. The second determination module is used to determine at least one target symbol entity based on the code modification content; The third determination module is used to determine at least one associated call relationship vector in the global call relationship vector table for any target symbol entity, based on the semantic relationship represented by the target symbol entity and the code modification content. The suggestion generation module is used to generate suggestions for modifying the target code based on at least one associated call relationship vector, so that users can make global code modifications based on the suggestions.
[0006] This application also provides an electronic device, including: a memory for storing a computer program; and a processor for implementing the steps of any of the above-described code review methods when executing the computer program.
[0007] This application also provides a computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the steps of any of the above-described code review methods.
[0008] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of any of the above-described code review methods.
[0009] This application achieves cross-module or cross-file analysis by obtaining global code and code modification content, obtaining at least one symbolic entity based on the global code, generating a global call relationship vector table based on the semantic relationship represented by the symbolic entity and the global code, determining at least one target symbolic entity based on the code modification content, determining at least one associated call relationship vector in the global call relationship vector table based on the semantic relationship represented by the target symbolic entity and the code modification content, and generating target code modifications based on at least one associated call relationship vector. This allows users to modify the global code according to the target code modification suggestions. By determining the relationship call relationship vector of the target symbolic entity in the global code through the global call relationship vector table, it avoids missing related calls and improves the accuracy of code review. Attached Figure Description
[0010] To more clearly illustrate the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0011] Figure 1 This is a schematic diagram of the structure of the code review system on which the embodiments of this application are based; Figure 2 A flowchart illustrating the code review method provided in this application embodiment; Figure 3A schematic flowchart illustrating an exemplary global call to a relation vector generation method provided in this application embodiment; Figure 4 A flowchart illustrating an exemplary method for generating local call relation vectors provided in this application embodiment; Figure 5 A flowchart illustrating an exemplary method for determining the associated call relationship vector provided in an embodiment of this application; Figure 6 A flowchart illustrating an exemplary defect rule generation method provided in an embodiment of this application; Figure 7 A flowchart illustrating an exemplary method for generating a preset defect rule base provided in this application embodiment; Figure 8 A schematic diagram of the structure of the code review device provided in the embodiments 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
[0012] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the protection scope of this application.
[0013] It should be noted that, in the description of this application, 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. The terms "first," "second," etc., in this application are used to distinguish similar objects and are not used to describe a specific order or sequence.
[0014] Gerrit is a Git-based centralized code review platform that provides a traceable audit trail for multi-person collaboration through a mandatory Change-Set mechanism. Jenkins and other similar platforms integrate testing and quality inspection processes into the pipeline.
[0015] In related technologies, static analysis tools such as SonarQube, Coverity, and FindSecBugs typically use a single compilation unit or single file as the smallest analysis granularity, performing rule matching through abstract syntax trees or bytecode. When modifying the interface of module A, it is impossible to infer the impact of the change on the remote module B, leading to missed detections or frequent post-mortem defects. Using large language models, such as LLM, there is a token limit bottleneck when sending all code to the large language model, resulting in the inability to fully input millions of lines of code, thus failing to establish relationship chains, and lacking the ability to automatically generate and continuously evolve review rules by leveraging enterprise-specific vulnerabilities.
[0016] To address the aforementioned technical problems, this application provides a code review method, apparatus, electronic device, and storage medium. The method includes: acquiring global code and code modification content; obtaining at least one symbol entity based on the global code; generating a global call relationship vector table for any symbol entity based on the semantic relationship represented by the symbol entity and the global code; determining at least one target symbol entity based on the code modification content; determining at least one associated call relationship vector in the global call relationship vector table for any target symbol entity based on the semantic relationship represented by the target symbol entity and the code modification content; and generating target code modification suggestions based on the at least one associated call relationship vector, so that the user can modify the global code according to the target code modification suggestions. The method described above obtains global code and code modification content. Based on the global code, it identifies at least one symbolic entity. Based on the semantic relationship between the symbolic entity and the global code, it generates a global call relationship vector table. Based on the code modification content, it identifies at least one target symbolic entity. Based on the semantic relationship between the target symbolic entity and the code modification content, it identifies at least one associated call relationship vector in the global call relationship vector table. Based on at least one associated call relationship vector, it generates target code modifications, enabling users to modify the global code according to the target code modification suggestions. By determining the relationship call relationship vector of the target symbolic entity in the global code through the global call relationship vector table, it achieves cross-module or cross-file analysis, avoids missing related calls, and improves the accuracy of code review.
[0017] To enable those skilled in the art to better understand the present application, the present application will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0018] The specific application environment architecture or specific hardware architecture on which the code review method depends is described here.
[0019] First, the structure of the code review system on which this application is based will be explained: The code review method, apparatus, electronic device, and storage medium provided in this application are applicable to analyzing the impact of modified code on the global codebase, such as... Figure 1 The diagram shows the structure of the code review system based on the embodiments of this application. It mainly includes the code to be reviewed, a data acquisition device, and a code review device. The data acquisition device is used to acquire the global code and the code modification content, and the code review device is used to analyze the impact of the modified code on the global code based on the code review method provided in the embodiments of this application.
[0020] This application provides a code review method for analyzing the impact of modified code on the global code. The execution subject of this application is an electronic device, such as a server, desktop computer, laptop computer, tablet computer, and other electronic devices that can be used for code review.
[0021] like Figure 2 The diagram shown is a flowchart illustrating the code review method provided in this application embodiment. The method includes: Step 201: Obtain the global code and code modifications.
[0022] Specifically, global code refers to all code in the current project, including all code across files or modules. Code changes refer to the collection of code changes pushed through the code review system. (This can be represented by the code review system Gerrit and the code change set Change-Set.)
[0023] Step 202: Obtain at least one symbol entity based on the global code.
[0024] Specifically, all code across files or modules is analyzed to identify at least one symbolic entity. Symbolic entities include functions or methods, classes or interfaces, global variables or constants, and structs or enumerations.
[0025] For example, functions or methods include ordinary functions, constructors, and lambda expressions; classes or interfaces are represented by the mean vector of member methods; global variables or constants are associated with corresponding functions through data flow; structs or enumerations are treated as equivalent functions.
[0026] Step 203: For any symbolic entity, generate a global call relationship vector table based on the semantic relationship between the symbolic entity and the global code representation.
[0027] Specifically, for any project, static analysis is performed on all code in the project's codebase to obtain multiple symbolic entities and their calling relationships in the global code. Based on the calling relationships of the symbolic entities in the global code, a global calling relationship vector corresponding to the symbolic entity is generated. Based on the global calling relationship vectors of each symbolic entity, a global calling relationship vector table is generated.
[0028] Accordingly, by calling the global call relation vector table, the semantic relationships between symbolic entities in the global code are represented, laying the foundation for subsequent retrieval of the association relationships in the global code for target symbolic entities.
[0029] Step 204: Based on the code modification content, determine at least one target symbol entity.
[0030] Specifically, the code review system receives code modifications submitted by users. Based on these modifications, it identifies the code to be modified, converts the modified code and the corresponding original code into an abstract syntax tree (AST), and compares the modified AST with the original AST to obtain a comparison result. Based on the comparison result, it identifies at least one symbolic entity involved, designating it as the target symbolic entity. This target symbolic entity is then organized into a set of change symbols. Furthermore, based on the comparison result between the modified and original code, it determines the modification operations corresponding to each symbolic entity, where modification operations include at least addition, modification, and deletion.
[0031] The code modifications may involve multiple changes, affecting multiple files or functions.
[0032] Step 205: For any target symbol entity, determine at least one associated call relationship vector in the global call relationship vector table based on the semantic relationship between the target symbol entity and the code modification content.
[0033] Specifically, for any target symbol entity, based on the target symbol entity and the code modification content, a local call relationship corresponding to the target symbol entity is generated, and then a target call relationship vector corresponding to the local call relationship is generated. Based on the target call relationship vector, at least one associated call relationship vector is determined in the global call relationship vector table.
[0034] Step 206: Generate target code modification suggestions based on at least one associated call relationship vector, so that the user can make global code modifications based on the target code modification suggestions.
[0035] Specifically, based on the associated call relationship vector and the preset defect rule base, the proposed modifications to the target code are determined.
[0036] Accordingly, by constructing a global call relationship vector table, symbolic entities are associated through semantic relationships. By calculating the target call relationship vector of the target symbolic entity and retrieving it from the global call relationship vector table, the associated call relationship vector in the global code is determined. Compared with manual retrieval, this avoids the omission of associated call relationship vectors. Compared with static analysis, the global call relationship vector table enables cross-module or cross-file retrieval, improving the accuracy of code review.
[0037] Based on the above embodiments, as an implementable approach, in one embodiment, for any symbolic entity, a global call relationship vector table is generated according to the semantic relationship between the symbolic entity and the global code representation, including: Step 2031: For any symbolic entity, analyze the global code based on the symbolic entity to obtain the global call relationship of at least one symbolic entity; Step 2032: Generate at least one call relationship vector corresponding to the symbol entity based on the symbol entity and the corresponding global call relationship; Step 2033: Generate a global call relationship vector table based on at least one call relationship vector corresponding to each symbol entity.
[0038] Specifically, for any symbolic entity, all code files of the project are analyzed to obtain the global call relationship of the symbolic entity in all code. The global call relationship represents the data interaction relationship between the symbolic entity and other symbolic entities in all code. The global call relationship includes multiple call relationships, such as call semantic relationship, data flow semantic relationship, exception propagation semantic relationship and concurrency semantic relationship.
[0039] For the global call relationships of symbolic entities, an embedding model is used to generate 1024-dimensional dense vectors for each symbolic entity, resulting in at least one call relationship vector corresponding to the symbolic entity. The call semantic relationships, data flow semantic relationships, exception propagation semantic relationships, and concurrency semantic relationships of the symbolic entity are added to the global call relationship vector of the symbolic entity through vector concatenation or relational encoding. When the definition of X is modified, the system searches backward along the data flow for all usage points Q. For example, after modifying the definition of a database field, it automatically locates all unupdated Data Access Object (DAO) layer code. For exception propagation semantic relationships, exception types, propagation stack frames, and capture ranges are mapped to the vector space. For concurrency semantic relationships, the acquisition and release relationships of concurrency primitives such as synchronized, lock, channel, and semaphore, as well as the corresponding resource dependency chains, are vector-encoded and added to the global call relationship vector.
[0040] Based on at least one call relationship vector of a symbolic entity, generate a global call relationship vector for the symbolic entity. Organize the global call relationship vectors of all symbolic entities corresponding to all code to obtain a global call relationship vector table.
[0041] Correspondingly, by using vector concatenation or relational encoding, the cross-module or cross-file calling relationships of symbolic entities are bound to the vectors of the symbolic entities. This allows the system to quickly retrieve other symbolic entities with semantic relationships when a symbolic entity is modified, laying the foundation for determining cross-module or cross-file relationships.
[0042] In some alternative implementations, step 2031 above includes: Step a1: Determine the calling semantic relationship based on the function call relationship of symbolic entities in the global code; Specifically, function call relationships characterize the semantic relationship of calling and being called between symbolic entities and other symbolic entities. A symbolic entity can be either a caller or a callee. These other symbolic entities originate from all code files. For example, when function A calls function B, a semantic edge is established from caller A to callee B.
[0043] Step a2: Determine the semantic relationship of data flow based on the data read / write relationship of symbolic entities in the global code; Specifically, data read / write relationships characterize the semantic relationships between symbolic entities from their definition location to their usage location. The usage location encompasses all code files. For example, when variable X is defined at location P and used at location Q, a semantic edge is established from P to Q.
[0044] Step a3: Determine the exception propagation semantic relationship based on the exception handling path of the symbolic entity in the global code; specifically, the exception handling path represents the semantic relationship between the exception throwing point and the exception receiving point of the symbolic entity, for example, including the semantic relationship between the try-catch capture chain and the throw throwing point.
[0045] Step a4: Determine the concurrency semantic relationships based on the shared resource contention relationships of symbolic entities in the global code; Specifically, the shared resource contention relationship characterizes the competitive relationship between symbolic entities and other symbolic entities for the same resource.
[0046] Step a5: Determine the global call relationship based on the call semantic relationship, data flow semantic relationship, exception propagation semantic relationship, and concurrency semantic relationship.
[0047] Specifically, for any symbolic entity, the corresponding global call relationship is determined based on all the call semantic relationships, data flow semantic relationships, exception propagation semantic relationships, and concurrency semantic relationships involved in the symbolic entity.
[0048] For example, such as Figure 3 The diagram shown is a flowchart illustrating an exemplary global call relationship vector generation method provided in this application embodiment. The method parses global code through an abstract rule tree to determine at least one symbolic entity, as well as the function call relationship, data flow semantic relationship, exception propagation semantic relationship, and concurrency semantic relationship of the symbolic entity. The corresponding global call relationship is then determined, and a global call relationship vector is generated based on the symbolic entity and the global call relationship.
[0049] Accordingly, by analyzing the semantic relationships of symbolic entities in the global code, including the calling semantic relationships, data flow semantic relationships, exception propagation semantic relationships, and concurrency semantic relationships, the association relationships between symbolic entities in the global code were determined, laying the foundation for cross-file retrieval.
[0050] Based on the above embodiments, as an implementable approach, in one embodiment, for any target symbolic entity, according to the semantic relationship represented by the target symbolic entity and the code modification content, at least one associated call relationship vector is determined in the global call relationship vector table, including: Step 2051: For any target symbol entity, determine the local call relationship of the target symbol entity based on the target symbol entity and the code modification content; Step 2052: Generate a target call relationship vector based on the target symbol entity and the corresponding local call relationship; Step 2053: Determine at least one associated call relationship vector based on the target call relationship vector and the global call relationship vector table.
[0051] Specifically, for any target symbolic entity, based on the target symbolic entity and the code modification content, the corresponding modified code within the code modification content is determined. Then, based on the target symbolic entity and the code modification content, the local call relationship of the target symbolic entity in the current module or current file is determined. This local call relationship includes call semantic relationships, data flow semantic relationships, exception propagation semantic relationships, and concurrency semantic relationships. Based on the target symbolic entity and the local call relationship, an embedding model is used to generate a 1024-dimensional dense vector for the symbolic entity, obtaining the target call relationship vector corresponding to the target symbolic entity.
[0052] For example, such as Figure 4The diagram illustrates an exemplary method for generating local call relationship vectors according to an embodiment of this application. Code modifications sent by a code review system are automatically analyzed using Jenkins to obtain at least one target symbol entity, generating a set of modified symbols. For any target symbol entity, local call relationships are determined based on the code modifications, and a target call relationship vector is generated based on the target symbol entity and its corresponding local call relationship.
[0053] Accordingly, by identifying the target symbolic entities affected by code modifications, and determining the corresponding local call relationships, static analysis of the global code is avoided, reducing the system's computational load and improving the efficiency of code review. Furthermore, through target call relationship vectors and global call relationship vector tables, the retrieval of relationships within the global code scope is achieved, enabling cross-module or cross-file code review and preventing the omission of related code.
[0054] In some alternative implementations, step 2053 above includes: Step b1: Generate a global call relationship network based on the global call relationship vector table; Step b2: Based on the target call relationship vector and the global call relationship network, determine at least one neighboring associated call relationship vector; Step b3: Determine the vector similarity based on the target call relationship vector and the neighboring associated call relationship vector; Step b4: Based on vector similarity and a preset similarity threshold, determine at least one associated call relationship vector among at least one neighboring associated call relationship vectors.
[0055] Specifically, a global call relationship network is generated based on the global call relationship vector table. Starting from the target call relationship vector, and combining it with the global call relationship network, the nearest neighboring call relationships are determined. This process is then further traversed forward until a preset traversal level is reached, identifying at least one nearest neighboring call relationship vector. This nearest neighboring call relationship vector represents other symbolic entities that have a relationship with the target symbolic entity. The cosine similarity between the target call relationship vector and the nearest neighboring call relationship vectors is calculated as the vector similarity.
[0056] If the vector similarity is less than a preset similarity threshold, the nearest associated call relationship vector is determined as the associated call relationship vector of the target symbol entity. Furthermore, based on the vector similarity, the associated call relationship vectors are sorted to obtain the top-k associated symbol entities.
[0057] The global call relation vector table stores the global call relation vectors corresponding to all symbolic entities. Built on a hierarchical graph structure, this table quickly locates candidate regions from a massive dataset when retrieving neighboring call relation vectors. The vectors are then compressed to reduce storage space and accelerate distance calculation. The table also includes cross-language call relation vectors, such as Java calling Kotlin extension functions.
[0058] For example, such as Figure 5 The diagram shown is a flowchart illustrating an exemplary method for determining associated call relationship vectors provided in this application embodiment. For any target symbol entity in the change symbol set, the associated call relationship vectors corresponding to the top-k associated symbol entities are determined in the global call relationship vector table based on the target call relationship vector corresponding to the target symbol entity.
[0059] Accordingly, a global call relationship network characterizes the call semantic relationships, data flow semantic relationships, exception propagation semantic relationships, and concurrency semantic relationships between symbolic entities and other symbolic entities. Through vector similarity, it more accurately matches the associated call relationship vectors highly correlated with the target symbolic entity, and by calculating vector similarity with global code, it achieves cross-module or cross-file retrieval.
[0060] Based on the above embodiments, as an implementable approach, in one embodiment, for any target symbolic entity, according to the semantic relationship represented by the target symbolic entity and the code modification content, at least one associated call relationship vector is determined in the global call relationship vector table, including: Step 2061: Determine at least one defect information based on at least one associated call relationship vector and a preset defect rule base; Step 2062: For any defect information, determine the defect risk level of the target call relationship vector based on the defect information; Step 2063: If the defect risk level is high, determine the target code modification suggestions based on the defect information so that the user can modify the global code according to the target code modification suggestions.
[0061] In cases where the defect risk level is low, a defect summary is generated based on at least one defect information and sent to the user.
[0062] Specifically, the defect is the potential impact of not making corresponding modifications to other code associated with the modified code. This impact includes errors or exceptions generated by the associated code during compilation or runtime.
[0063] The preset defect rule base includes various historical defect types. Each historical defect type includes defect rules, historical defect code, and corresponding historical fix code. For any associated call relationship, the corresponding historical defect type is matched in the preset defect rule base to generate corresponding defect information. The defect information includes the modified code corresponding to the target symbol entity in the code modification content, the defect type, defect rule, historical defect code, and corresponding historical fix code matched in the preset defect rule base.
[0064] The defect information is input into the LLM to obtain the code corresponding to the associated call relationship, which is used as the target code, as well as the defect risk level and modification suggestions for the target code. The modification suggestions for the target code include the affected file identifier, the line number of the code in the file, the example code for the fix, and the confidence level of the modification suggestions.
[0065] When the defect risk level is high, code modification suggestions are added as comments to the corresponding parts of the code to be modified, so that users can modify the related code in all code files according to the code modification suggestions.
[0066] When the defect risk level is low, multiple suggested modifications to the target code are compiled into a defect summary and sent to the user. The user can then refer to the defect summary to decide whether to make the corresponding code modifications. Based on the defect summary, the user reviews the target code and the corresponding modification suggestions. When the system falsely reports related code or unreasonable modification suggestions, the user marks the error information and reports it back to the system, thereby dynamically adjusting the preset similarity threshold.
[0067] For example, such as Figure 6 The diagram shown illustrates an exemplary defect rule generation method provided in this application. Based on the code modification content, at least one target symbol entity is determined. For any target symbol entity, a global call relationship vector table is traversed and retrieved to determine multiple associated call relationship vectors. For any associated call relationship vector, based on the corresponding defect information, target code modification suggestions and defect risk levels are generated using LLM. The target code modification suggestions are sent to the user so that the user can modify the code according to the suggestions and provide feedback. For adopted target code modification suggestions, defect repair pairs are generated. Combined with a preset text template, defect rules are generated using LLM and added to a preset defect rule library.
[0068] Specifically, in one embodiment, for any target call relationship vector, if the defect risk level corresponding to the target call relationship vector is high, a defect repair pair is generated based on defect information, code modification suggestions, and defect metadata information; Based on the defect repair pair and the preset text template, generate the corresponding defect rule content and defect rule confidence level; If the confidence level of a defect rule is greater than the preset confidence threshold, the defect rule content is determined to be valid and added to the preset defect rule library.
[0069] Specifically, the system listens for code modification events. When the user confirms and accepts the suggested code modifications, and these modifications are deemed valid, a defect repair pair is generated based on the defect information, the suggested modifications, and the defect metadata. The defect metadata includes the defect type, business domain, defect risk level, and a textual description of the defect. The defect repair pair includes the modified code representing the changes, the modified target code, and the defect metadata. ,in, The modified code represents the modified content. This indicates the modified target code. This indicates defect metadata information.
[0070] Based on the defect repair pairs and the preset text template, generate defect rule question and answer text, input the defect rule question and answer text into the large language model, summarize the rules of the defect repair pairs, and generate the summarized defect rules and the corresponding rule confidence.
[0071] The summarized defect rules and their corresponding rule confidence scores are formatted as follows: { "pattern": "<Anti-pattern of natural language description>", "fix_template" : "<placeholder-level fix template>", "confidence" :<float 0–1> } The natural language description of the anti-pattern is the summarized defect rule. This summarized defect rule does not include specific class, method, or variable names to ensure applicability across various languages. Placeholder-level repair templates represent the corresponding modification suggestions. `confidence` represents the rule's confidence level, which indicates whether the input defect repair pair is reasonable and whether the generated defect rule and corresponding modification suggestions are compatible.
[0072] For example, regarding the Java NullPointerException, D is `String name = user.getProfile().getName()`. A NullPointerException (NPE) will be triggered when `user.getProfile()` returns null. F is `String name = user.getProfile() == null ? null : user.getProfile().getName()`. The summarized defect rules and their corresponding confidence levels are: JSON= { "pattern": "Continuous chaining of calls without null checks", "fix_template" : " <expr1>== null ? null : <expr1> . <method>()"} Based on the summarized defect rules and corresponding modification suggestions, a defect rule vector is generated. First, the confidence level of the defect rule is determined. If the confidence level is greater than a preset threshold, a search is performed in a preset defect rule library based on the defect rule vector to determine if a similar defect rule exists. If it does, the summarized defect rule is discarded. If not, the defect rule is further identified as a trial rule and added to the preset defect rule library for trial use. Within a preset trial period, based on user feedback, it is determined whether the defect rule should be officially added to the preset defect rule library. If the percentage of satisfied users in the feedback is greater than the preset trial threshold, the defect rule is identified as a usage rule and officially added to the preset defect rule library.
[0073] For any defect rule in the preset defect rule base, obtain user feedback on usage suggestions. If the satisfaction rate among the user feedback usage suggestions is less than the preset trial threshold, change the defect rule's identifier from "use" to "trial" so that it can be used within the preset trial period.
[0074] For example, such as Figure 7 The diagram illustrates an exemplary method for generating a preset defect rule base according to an embodiment of this application. For any target symbol entity, based on at least one defect repair pair corresponding to the target symbol entity, a preset text module is used to generate defect rule question-and-answer text. The defect rule question-and-answer text is input into an LLM (Local Management Library) to obtain summarized defect rules. Based on these defect rules, a defect rule vector is generated. The quality assessment result of the defect rule is determined by the rule confidence level and user feedback suggestions within a preset trial period. If the quality assessment result is satisfactory, the defect rule is added to the preset defect rule base. For defect rules marked as "used" in the preset defect rule base, user feedback suggestions are collected in real time. If the user feedback suggestions indicate a high false positive rate, the defect rule's identifier is changed to "trial" for downgrading, or the defect rule is discarded. If the quality assessment result is unsatisfactory, the defect rule is directly discarded.
[0075] Correspondingly, various defect rules are generated through defect repair pairs. These defect rules include both standard defect rules and enterprise-specific rules, which can be adapted to the enterprise's business logic. As the defect rules corresponding to the defect repair pairs accumulate, an enterprise-specific preset defect rule library is gradually built, which is more in line with the code requirements of enterprise projects.
[0076] Specifically, in one embodiment, after determining whether there are similar defect rules in the preset defect rule base, it is determined whether the summarized defect rules contradict the defect rules in the preset defect rule base. If there is a contradiction, the summarized defect rules are discarded; if there is no contradiction, the summarized defect rules are added to the preset defect rule base. By determining whether there is a contradiction, contradictory defect rules are avoided from being output to the user, thus improving the accuracy of code modification suggestions.
[0077] The code review method provided in this application includes: obtaining global code and code modification content; obtaining at least one symbol entity based on the global code; generating a global call relationship vector table for any symbol entity based on the semantic relationship represented by the symbol entity and the global code; determining at least one target symbol entity based on the code modification content; determining at least one associated call relationship vector in the global call relationship vector table for any target symbol entity based on the semantic relationship represented by the target symbol entity and the code modification content; and generating target code modification suggestions based on the at least one associated call relationship vector, so that the user can modify the global code according to the target code modification suggestions.
[0078] The method described above obtains global code and code modification content. Based on the global code, it identifies at least one symbolic entity. Based on the semantic relationship between the symbolic entity and the global code, it generates a global call relationship vector table. Based on the code modification content, it identifies at least one target symbolic entity. Based on the semantic relationship between the target symbolic entity and the code modification content, it identifies at least one associated call relationship vector in the global call relationship vector table. Based on at least one associated call relationship vector, it generates target code modifications, enabling users to modify the global code according to the target code modification suggestions. By determining the relationship call relationship vector of the target symbolic entity in the global code through the global call relationship vector table, it achieves cross-module or cross-file analysis, avoids missing related calls, and improves the accuracy of code review.
[0079] Furthermore, by constructing a global call relationship vector table, the semantic relationships between various symbolic entities in the global code are represented, laying the foundation for subsequent retrieval of the association relationships in the global code for target symbolic entities. By building the global call relationship vector table, symbolic entities are associated through semantic relationships. By calculating the target call relationship vector of the target symbolic entity and retrieving it from the global call relationship vector table, the associated call relationship vectors in the global code are determined. Compared to manual retrieval, this avoids the omission of associated call relationship vectors. Compared to static analysis, the global call relationship vector table enables cross-module or cross-file retrieval, improving the accuracy of code review. Through vector concatenation or relationship encoding, the call relationships of symbolic entities across modules or files are bound to the vectors of the symbolic entities. This allows the system to quickly retrieve other semantically associated symbolic entities when a symbolic entity is modified, laying the foundation for determining cross-module or cross-file association relationships. By analyzing the call semantic relationships, data flow semantic relationships, exception propagation semantic relationships, and concurrency semantic relationships of symbolic entities in the global code, the association relationships between symbolic entities in the global code are determined, laying the foundation for cross-file retrieval. By modifying code, the target symbolic entities affected by the changes are identified, and corresponding local call relationships are determined. This avoids static analysis of the global code, reducing system computation and improving code review efficiency. Furthermore, through target call relationship vectors and global call relationship vector tables, it enables the retrieval of relationships within the global code scope, facilitating cross-module or cross-file code review and preventing the omission of related code. The global call relationship network represents the call semantic relationships, data flow semantic relationships, exception propagation semantic relationships, and concurrency semantic relationships between symbolic entities and other symbolic entities. Vector similarity allows for more accurate matching of call relationship vectors highly associated with the target symbolic entity, and cross-module or cross-file retrieval is achieved by calculating vector similarity with the global code. Defect repair pairs generate various defect rules, including standard defect rules and enterprise-specific rules, adaptable to the enterprise's business logic. As the defect rules corresponding to defect repair pairs accumulate, an enterprise-specific preset defect rule library is gradually built, better meeting the code requirements of enterprise projects.
[0080] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to 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.
[0081] Embodiments of this application also provide a code review apparatus for performing the code review method provided in the above embodiments.
[0082] like Figure 8 The diagram shown is a structural schematic of the code review device provided in an embodiment of this application. The code review device 80 includes: an acquisition module 801, a first determination module 802, a relationship table generation module 803, a second determination module 804, a third determination module 805, and an opinion generation module 806.
[0083] The system comprises the following modules: an acquisition module for acquiring global code and code modification content; a first determination module for obtaining at least one symbolic entity based on the global code; a relationship table generation module for generating a global call relationship vector table for any symbolic entity based on the semantic relationship represented by the symbolic entity and the global code; a second determination module for determining at least one target symbolic entity based on the code modification content; a third determination module for determining at least one associated call relationship vector in the global call relationship vector table for any target symbolic entity based on the semantic relationship represented by the target symbolic entity and the code modification content; and an opinion generation module for generating target code modification opinions based on at least one associated call relationship vector, so that users can modify the global code according to the target code modification opinions.
[0084] Specifically, in one embodiment, the relation table generation module is specifically used for: For any symbolic entity, analyze the global code based on the symbolic entity to obtain at least one global call relationship of the symbolic entity; generate at least one call relationship vector corresponding to the symbolic entity based on the symbolic entity and the corresponding global call relationship; generate a global call relationship vector table based on at least one call relationship vector corresponding to each symbolic entity.
[0085] Specifically, in one embodiment, the relation table generation module is further configured to: Based on the function call relationships of symbolic entities in the global code, determine the call semantic relationships; based on the data read and write relationships of symbolic entities in the global code, determine the data flow semantic relationships; based on the exception handling paths of symbolic entities in the global code, determine the exception propagation semantic relationships; based on the shared resource contention relationships of symbolic entities in the global code, determine the concurrency semantic relationships; and based on the call semantic relationships, data flow semantic relationships, exception propagation semantic relationships, and concurrency semantic relationships, determine the global call relationships.
[0086] Specifically, in one embodiment, the third determining module is specifically used for: For any target symbol entity, determine the local call relationship of the target symbol entity based on the target symbol entity and the code modification content; generate a target call relationship vector based on the target symbol entity and the corresponding local call relationship; and determine at least one associated call relationship vector based on the target call relationship vector and the global call relationship vector table.
[0087] Specifically, in one embodiment, the third determining module is further configured to: Based on the global call relationship vector table, a global call relationship network is generated; based on the target call relationship vector and the global call relationship network, at least one neighboring associated call relationship vector is determined; based on the target call relationship vector and the neighboring associated call relationship vectors, the vector similarity is determined; based on the vector similarity and a preset similarity threshold, at least one associated call relationship vector is determined from at least one neighboring associated call relationship vector.
[0088] Specifically, in one embodiment, the opinion generation module is specifically used for: Based on at least one associated call relationship vector and a preset defect rule base, at least one defect information is determined; for any defect information, the defect risk level of the target call relationship vector is determined based on the defect information; if the defect risk level is high, the target code modification suggestions are determined based on the defect information, so that the user can modify the global code according to the target code modification suggestions; if the defect risk level is low, a defect summary is generated based on at least one defect information and sent to the user.
[0089] Specifically, in one embodiment, the opinion generation module is further configured to: For any target call relationship vector, if the defect risk level corresponding to the target call relationship vector is high, a defect repair pair is generated based on the defect information, code modification suggestions, and defect metadata information. Based on the defect repair pair and the preset text template, the corresponding defect rule content and defect rule confidence level are generated. If the defect rule confidence level is greater than the preset confidence threshold, the defect rule content is determined to be valid and is added to the preset defect rule library.
[0090] For a description of the features in the embodiment corresponding to the code review device, please refer to the relevant description of the embodiment corresponding to the code review method, which will not be repeated here.
[0091] Embodiments of this application also provide an electronic device, such as... Figure 9 The diagram shown is a schematic diagram of the structure of an electronic device provided in an embodiment of this application, including a processor 10 and a memory 20. The memory 20 stores a computer program, and the processor 10 is configured to run the computer program to perform the steps in any of the above-described code review method embodiments.
[0092] Embodiments of this application also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to execute the steps in any of the code review method embodiments described above when run.
[0093] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard disk, magnetic disk, or optical disk.
[0094] Embodiments of this application also provide a computer program product, which includes a computer program that, when executed by a processor, implements the steps in any of the code review method embodiments described above.
[0095] Embodiments of this application also provide another computer program product, including a non-volatile computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps in any of the code review method embodiments described above.
[0096] Any of the components, modules, units, parts, methods, and operations described herein can be implemented using software, firmware, hardware (e.g., fixed logic circuitry), manual processing, or any combination thereof. Alternatively or additionally, any functionality described herein can be performed at least in part by one or more hardware logic components, such as, but not limited to, a central processing unit (CPU), a field-programmable gate array (FPGA), an application-specific integrated circuit (ASIC), an application-specific standard product (ASSP), a system-on-a-chip (SoC), a complex programmable logic device (CPLD), a microprocessor (MCU), etc. The terms "system," "computing device," or "apparatus" used herein encompass various means, devices, and machines for processing data, including, for example, one or more programmable processors, computers, SoCs, or combinations thereof. The apparatus may also include code that creates an execution environment for the computer program in question, such as code constituting processor firmware, a protocol stack, a database management system, an operating system, a cross-platform runtime environment, a virtual machine, or one or more combinations thereof. The aforementioned computer program (also known as a program, software, software application, app, script, or code) can be written in any form of programming language, including compiled or interpreted languages, declarative or procedural languages, and can be deployed in any form, including as a standalone program or as a module, component, subroutine, object, or other unit suitable for a computing environment.
[0097] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0098] The foregoing has provided a detailed description of the code review method, apparatus, electronic device, and storage medium provided in this application. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the embodiments above are only intended to aid in understanding the method and core ideas of this application. It should be noted that those skilled in the art can make various improvements and modifications to this application without departing from its principles, and these improvements and modifications also fall within the protection scope of the claims of this application.< / method> < / expr1>
Claims
1. A code review method, characterized in that, The method includes: Get global code and code modifications; Based on the global code, at least one symbol entity is obtained; For any symbolic entity, a global call relationship vector table is generated based on the semantic relationship between the symbolic entity and the global code representation; Based on the modified code, at least one target symbol entity is identified. For any target symbol entity, based on the semantic relationship represented by the target symbol entity and the code modification content, at least one associated call relationship vector is determined in the global call relationship vector table; Based on the at least one associated call relationship vector, target code modification suggestions are generated so that users can make global code modifications according to the target code modification suggestions.
2. The code review method according to claim 1, characterized in that, The step of generating a global call relationship vector table for any symbolic entity, based on the semantic relationship between the symbolic entity and the global code representation, includes: For any symbolic entity, the global code is analyzed based on the symbolic entity to obtain at least one global call relationship of the symbolic entity; Based on the symbol entity and the corresponding global call relationship, generate at least one call relationship vector corresponding to the symbol entity; Generate a global call relationship vector table based on at least one call relationship vector corresponding to each symbol entity.
3. The code review method according to claim 2, characterized in that, The step of analyzing the global code based on any given symbolic entity to obtain at least one global call relationship for that symbolic entity includes: Determine the calling semantic relationship based on the function call relationship of the symbolic entities in the global code; Based on the data read / write relationships of the symbolic entities in the global code, determine the semantic relationships of data flow; Determine the exception propagation semantic relationship based on the exception handling path of the symbolic entity in the global code; Based on the shared resource contention relationships of the symbolic entities in the global code, determine the concurrency semantic relationships; The global call relationship is determined based on the call semantic relationship, data flow semantic relationship, exception propagation semantic relationship and concurrency semantic relationship.
4. The code review method according to claim 1, characterized in that, For any target symbol entity, based on the semantic relationship represented by the target symbol entity and the code modification content, at least one associated call relationship vector is determined in the global call relationship vector table, including: For any target symbol entity, determine the local call relationship of the target symbol entity based on the target symbol entity and the code modification content; Based on the target symbol entity and the corresponding local call relationship, generate a target call relationship vector; Based on the target call relationship vector and the global call relationship vector table, at least one associated call relationship vector is determined.
5. The code review method according to claim 4, characterized in that, The step of determining at least one associated call relationship vector based on the target call relationship vector and the global call relationship vector table includes: Generate a global call relationship network based on the global call relationship vector table; Based on the target call relationship vector and the global call relationship network, at least one neighboring associated call relationship vector is determined; Determine vector similarity based on the target call relationship vector and the neighboring associated call relationship vector; Based on the vector similarity and the preset similarity threshold, at least one associated call relationship vector is determined from the at least one neighboring associated call relationship vector.
6. The code review method according to claim 4, characterized in that, The step of generating target code modification suggestions based on the at least one associated call relationship vector, so that the user can make global code modifications based on the target code modification suggestions, includes: Based on the at least one associated call relationship vector and the preset defect rule base, at least one defect information is determined; For any defect information, determine the defect risk level of the target call relationship vector based on the defect information; If the defect risk level is high, the target code modification suggestions are determined based on the defect information, so that the user can modify the global code according to the target code modification suggestions; In cases where the defect risk level is low, a defect summary is generated based on at least one defect information and sent to the user.
7. The code review method according to claim 6, characterized in that, The method further includes: For any target call relationship vector, if the defect risk level corresponding to the target call relationship vector is high, a defect repair pair is generated based on the defect information, code modification suggestions, and defect metadata information. Based on the defect repair pair and the preset text template, generate the corresponding defect rule content and defect rule confidence level; If the confidence level of the defect rule is greater than a preset confidence threshold, the defect rule content is determined to be valid, and the defect rule content is added to the preset defect rule library.
8. A code review device, characterized in that, The device includes: The acquisition module is used to retrieve global code and code modifications. The first determining module is used to obtain at least one symbol entity based on the global code; The relation table generation module is used to generate a global call relation vector table for any symbolic entity based on the semantic relationship between the symbolic entity and the global code representation. The second determining module is used to determine at least one target symbol entity based on the modified code content; The third determining module is used to determine at least one associated call relationship vector in the global call relationship vector table for any target symbol entity, based on the semantic relationship represented by the target symbol entity and the code modification content. The suggestion generation module is used to generate target code modification suggestions based on the at least one associated call relationship vector, so that the user can make global code modifications based on the target code modification suggestions.
9. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor for implementing the steps of the code review method as described in any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements the steps of the code review method as described in any one of claims 1 to 7.