Code auditing method and device

Through the encoder-decoder model combined with AST and multi-head attention mechanism, the problem of difficulty in capturing the global context of the code in the existing technology is solved, more accurate and efficient code review is achieved, and personalized modification suggestions are provided.

CN119127212APending Publication Date: 2024-12-13WEBANK (CHINA)
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411225150.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-03
Publication Date
2024-12-13

AI Technical Summary

Technical Problem

Existing deep learning-based code auditing models are difficult to capture global context information in the code, especially when dealing with long-distance dependencies and complex code structures, resulting in insufficient code audit accuracy.

Method used

Using the encoder-decoder model combined with abstract syntax tree (AST) and multi-head attention mechanism, we enhance the understanding of the syntax structure, types and semantic role attention weights through syntax tree attention weights, type attention weights and semantic role attention weights. , and capture runtime behavior through dynamic code data to generate more accurate modification suggestions.

Benefits of technology

It improves the accuracy and efficiency of code review, provides more personalized and accurate modification suggestions, and helps developers improve code quality and performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119127212A_ABST
    Figure CN119127212A_ABST
Patent Text Reader

Abstract

The invention discloses a code auditing method and device. The method comprises the following steps: generating a corresponding abstract syntax tree for a to-be-audited code; according to the syntax tree attention weight, the type attention weight and the semantic role attention weight of each node of the abstract syntax tree of the code to be audited, obtaining the syntax tree attention weight, the type attention weight and the semantic role attention weight of each code snippet of the code to be audited; inputting each first code snippet of the to-be-audited code into an encoder in the encoder-decoder model, and obtaining a code of the first code snippet by taking a syntax tree attention weight, a type attention weight and a semantic role attention weight of the first code snippet as multi-head attention weights of the encoder; inputting the code of the first code snippet into a decoder in an encoder-decoder model to obtain a second code snippet after the first code snippet is modified; and taking the second code snippets corresponding to the first code snippets of the to-be-audited code as an audit result of the to-be-audited code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of neural network technology, and in particular to a code review method and device. Background Art

[0002] In the software development process, code review is an important activity aimed at improving code quality and reducing software defects. As the scale and complexity of software projects increase, manual code review becomes more and more time-consuming and error-prone. Therefore, automated code review technology has emerged. In the context of automated code review, the closest background technology is deep learning-based models, especially those that use convolutional neural networks (CNN) or recurrent neural networks (RNN) to analyze code. These models are able to handle the sequential characteristics of code and understand the structure and semantics of the code to a certain extent. However, these models still face challenges in dealing with long-distance dependencies and complex code structures, as they may have difficulty capturing global contextual information in the code.

[0003] Therefore, there is an urgent need for a solution to fully consider the context dependencies during the code review process, thereby improving the accuracy of code review. Summary of the invention

[0004] The present application provides a code review method and device, which are used to fully consider context dependencies during the code review process, thereby improving the accuracy of the code review.

[0005] In a first aspect, the present application provides a code review method, the method comprising: generating a corresponding abstract syntax tree for a code to be reviewed; each node of the abstract syntax tree represents a basic syntax unit in the syntax structure of the code to be reviewed; calculating the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed; obtaining the syntax tree attention weight, type attention weight and semantic role attention weight of each code fragment of the code to be reviewed according to the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed; inputting each first code fragment of the code to be reviewed into an encoder in an encoder-decoder model, using the syntax tree attention weight, type attention weight and semantic role attention weight of the first code fragment as the multi-head attention weight of the encoder, and obtaining the encoding of the first code fragment; inputting the encoding of the first code fragment into a decoder in the encoder-decoder model, and obtaining a second code fragment after modifying the first code fragment; and using the second code fragment corresponding to each first code fragment of the code to be reviewed as the review result of the code to be reviewed.

[0006] In the above technical solution, a neural network model is used to learn the representation and semantics of the code, and generate corresponding modification suggestions based on the input code to be reviewed. Among them, the encoder part increases the ability to deeply understand the syntax and structure of the programming language through the syntax tree attention weight, type attention weight and semantic role attention weight of each code fragment, while the decoder part focuses on generating grammatically correct and logically consistent code and modification suggestions. Based on this, the encoder-decoder can understand the code more comprehensively, not just limited to syntax and rule checking. It can provide more personalized and accurate modification suggestions to help developers improve code quality and performance more effectively.

[0007] In a possible design, after generating a corresponding abstract syntax tree for the code to be audited, it also includes: adding dynamic code data of the code to be audited during runtime to the abstract syntax tree to obtain an enhanced syntax tree; the dynamic code data includes the actual values ​​and states of the variables when the code to be audited is running; calculating the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be audited, including: calculating the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the enhanced syntax tree of the code to be audited.

[0008] In the above technical solution, through dynamic code data and semantic analysis, the dynamic behavior and semantic information of the code at runtime can be captured, making feature extraction more comprehensive and accurate.

[0009] In a possible design, the syntax tree attention weights, type attention weights and semantic role attention weights of each node of the abstract syntax tree of the code to be reviewed are obtained, including: for each code fragment, determining the contribution of each node of the abstract syntax tree included in the code fragment to the code fragment; determining the weight corresponding to each node of the abstract syntax tree included in the code fragment based on the contribution; according to the syntax tree attention weights, type attention weights and semantic role attention weights of each node of the abstract syntax tree of the code to be reviewed and the weights corresponding to each node, the syntax tree attention weights, type attention weights and semantic role attention weights of each code fragment of the code to be reviewed are obtained.

[0010] In the above technical solution, multiple attention mechanisms are integrated into the encoder-decoder model, including syntax tree attention, type attention, and semantic role attention, so that the model can carefully capture the grammatical structure, data type, semantic role, and context of the code, enabling the model to achieve higher accuracy and efficiency in code editing, repair, and generation tasks.

[0011] In one possible design, the encoder-decoder model is trained in the following manner: randomly mask at least one statement of the original code to obtain a masked code; predict the content of each masked statement in the masked code to obtain a filling suggestion and a corresponding probability for each masked statement; fill the masked statements in the masked code according to the filling suggestion and the corresponding probability of each masked statement to obtain a filled code; input at least one third code fragment in the filled code into the encoder-decoder model to obtain a fourth code fragment that is modified from the third code fragment; calculate the similarity between the fourth code fragment and the corresponding original code fragment; adjust the parameters of each attention weight in the encoder's multi-head attention weight according to the similarity between the fourth code fragment and the corresponding original code fragment; the multi-head attention weight includes syntax tree attention weight, type attention weight and semantic role attention weight; the parameters include weight matrix parameters, bias parameters and embedding dimension parameters.

[0012] In the above technical solution, when training the encoder-decoder model, the original code is masked and the code at the masked position is filled to generate more training data, so that the model can learn richer code modification suggestions that conform to the actual development process, thereby improving the generalization ability of the encoder-decoder model. Calculating the similarity between the code output by the model and the original code to identify potential problems or improvement points is the basis for generating modification suggestions, ensuring the relevance and accuracy of the generated modification suggestions.

[0013] In one possible design, calculating the similarity between the fourth code snippet and the corresponding original code snippet includes: obtaining static similarity based on static features of the fourth code snippet and the corresponding original code snippet; the static features include text features of the code itself; obtaining dynamic similarity based on dynamic features of the fourth code snippet and the corresponding original code snippet; the dynamic features include actual values ​​and states of variables at code runtime; obtaining the similarity between the fourth code snippet and the corresponding original code snippet based on the static similarity, the static similarity weight, the dynamic similarity and the dynamic similarity weight; the static similarity weight and the dynamic similarity weight are determined according to code review requirements.

[0014] In the above technical solution, when calculating the similarity, the accuracy of the similarity calculation is improved by combining the static and dynamic features of the code, so that the model can pay more attention to the actual functions and execution effects of the code when identifying potential problems and generating modification suggestions, rather than just the superficial text similarity.

[0015] In one possible design, according to the similarity between the fourth code fragment and the corresponding original code fragment, the parameters of each attention weight in the multi-head attention weight of the encoder are adjusted, including: obtaining a similarity loss according to the similarity between the fourth code fragment and the corresponding original code fragment; obtaining an accuracy loss according to the accuracy of the modification of the fourth code fragment; obtaining a total loss based on the similarity loss, the accuracy loss, the regularization loss and the corresponding weights; and adjusting the parameters of each attention weight in the multi-head attention weight of the encoder according to the total loss.

[0016] In the above technical solution, when calculating the model loss, multiple losses (including similarity loss, accuracy loss, and regularization loss) are combined to improve the accuracy of model training.

[0017] In a possible design, the syntax tree attention weight is calculated by the following formula:

[0018]

[0019] in, For node v i and v j The grammatical tree attention weight between them; σ is the activation function; ei is the node v i The embedding representation of j For node v j Embedding representation of i ;e j ] indicates e i and e j Perform vector concatenation; W syn is the weight matrix parameter; b syn is the bias parameter.

[0020] In the above technical solution, by calculating the attention weight of the syntax tree, the model's understanding of the grammatical structure can be strengthened: by explicitly using the abstract syntax tree AST of the code, the model can understand the grammatical relationship and hierarchy between various code fragments (such as variable declarations, function calls, loop structures, etc.). For example, the model can distinguish whether a variable is declared inside a loop or outside a loop, which is very helpful for identifying potential scope-related errors. Improve the model's attention: By calculating the attention weights between nodes, the syntax tree attention mechanism can guide the model to pay more attention to the feature representation of important nodes in the syntax tree. In the scenario of code review, this means that the model can focus more on those parts that are more critical to understanding the logic and function of the code, such as function definitions, key variables, etc., instead of being distracted by template code or less important details. Capture long-distance dependencies: Some logical errors or potential problems in the code may involve the relationship between nodes that are far apart on the syntax tree. Traditional sequence-based models may find it difficult to capture such long-distance dependencies. Syntax tree attention can effectively capture and utilize the dependencies between these grammatical structures across a wide range by directly calculating attention weights on the syntax tree AST, thereby improving the ability to identify and predict potential problems in the code.

[0021] In one possible design, the type of attention weight is calculated by the following formula:

[0022]

[0023] in, For node v i and v j The type attention weight between them; σ is the activation function;

[0024] T(v i ) is the node v i The type representation; T(v j ) is the node v j The type of W type is the weight matrix parameter; b type is the bias parameter.

[0025] In the above technical solutions, by calculating the type attention weight, type error detection can be improved: type error is one of the common programming errors, which may cause runtime exceptions or logical errors. Type attention can help the model identify potential type mismatch problems by explicitly considering the type information of variables and expressions. For example, if an integer variable is mistakenly assigned a string value, the model can identify this inconsistency by analyzing the type information and generate relevant warnings or suggestions. Understanding code semantics: In many cases, the semantics of the code is determined by the types of variables and functions. Type attention enables the model to understand the semantics of variables and function calls based on type information, thereby analyzing the code behavior more accurately. For example, the model can distinguish the different meanings of overloaded function calls or understand the behavior of generic code. Support safe and optimized code modification suggestions: When generating code modification suggestions, considering type information can help the model propose safer and optimized solutions. For example, when refactoring code or introducing new code snippets, the model can ensure the compatibility of the new code with the existing code at the type level, thereby avoiding the introduction of new type errors.

[0026] In one possible design, the semantic role attention weight is calculated by the following formula:

[0027]

[0028] in, For node e i and e j The attention weight between sem is the weight matrix parameter; d sem is the embedding dimension parameter.

[0029] In the above technical solution, when calculating the semantic role attention weight, the understanding of function calls can be enhanced: In the code review process, a deep understanding of the semantics of function calls is the key to discovering potential problems and optimizing code quality. Semantic role attention enables the model to more accurately understand the purpose and behavior of function calls by focusing on the semantic roles of each entity in the function call, such as which parameters are required, which are optional, and how the return value of the function should be handled. Improve the analysis of parameters and return values: Incorrect use of parameters or ignoring function return values ​​are common programming errors. The semantic role attention mechanism helps the model identify the correct semantic roles of parameters and return values ​​in function calls, thereby detecting problems such as type mismatch, incorrect parameter order, and unused return values, which are all issues that need to be focused on in code review. Promote the verification of code logic: Function calls are the basis of code logic and function implementation. By calculating attention weights to reflect the semantic importance of each entity in the function call, the model can better verify the correctness of the code logic and identify potential logical errors or design defects, such as incorrect function call order, missing function implementation, and inappropriate error handling.

[0030] In a second aspect, an embodiment of the present application provides a code review device, the device comprising:

[0031] A processing module, used to generate a corresponding abstract syntax tree from the code to be reviewed; each node of the abstract syntax tree represents a basic syntax unit in the syntax structure of the code to be reviewed;

[0032] The processing module is further used to calculate the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed;

[0033] The processing module is further used to obtain the syntax tree attention weight, type attention weight and semantic role attention weight of each code fragment of the code to be reviewed according to the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed;

[0034] An encoding module, configured to input each first code snippet of the code to be reviewed into an encoder in an encoder-decoder model, and use the syntax tree attention weight, type attention weight, and semantic role attention weight of the first code snippet as the multi-head attention weight of the encoder to obtain an encoding of the first code snippet;

[0035] A decoding module, configured to input the encoding of the first code segment into a decoder in an encoder-decoder model to obtain a second code segment that is a modification of the first code segment;

[0036] The processing module is further configured to use the second code segments corresponding to the first code segments of the code to be reviewed as the review result of the code to be reviewed.

[0037] In a possible design, after generating a corresponding abstract syntax tree for the code to be audited, the processing module is further used to add dynamic code data of the code to be audited during runtime to the abstract syntax tree to obtain an enhanced syntax tree; the dynamic code data includes the actual values ​​and states of the variables when the code to be audited is running; the processing module, when calculating the syntax tree attention weights, type attention weights and semantic role attention weights of each node of the abstract syntax tree of the code to be audited, is specifically used to calculate the syntax tree attention weights, type attention weights and semantic role attention weights of each node of the enhanced syntax tree of the code to be audited.

[0038] In one possible design, the processing module, when obtaining the syntax tree attention weights, type attention weights and semantic role attention weights of each code fragment of the code to be reviewed based on the syntax tree attention weights, type attention weights and semantic role attention weights of each node of the abstract syntax tree of the code to be reviewed, is specifically used to, for each code fragment, determine the contribution of each node of the abstract syntax tree included in the code fragment to the code fragment; determine the weight corresponding to each node of the abstract syntax tree included in the code fragment based on the contribution; obtain the syntax tree attention weights, type attention weights and semantic role attention weights of each code fragment of the code to be reviewed based on the syntax tree attention weights, type attention weights and semantic role attention weights of each node of the abstract syntax tree of the code to be reviewed and the weights corresponding to each node.

[0039] In one possible design, the device also includes a training module for training the encoder-decoder model in the following manner: randomly masking at least one statement of the original code to obtain a masked code; predicting the content of each masked statement in the masked code to obtain a filling suggestion and a corresponding probability for each masked statement; filling the masked statements in the masked code according to the filling suggestion and the corresponding probability of each masked statement to obtain a filled code; inputting at least one third code fragment in the filled code into the encoder-decoder model to obtain a fourth code fragment modified from the third code fragment; calculating the similarity between the fourth code fragment and the corresponding original code fragment; adjusting the parameters of each attention weight in the multi-head attention weight of the encoder according to the similarity between the fourth code fragment and the corresponding original code fragment; the multi-head attention weight includes a syntax tree attention weight, a type attention weight and a semantic role attention weight; the parameters include a weight matrix parameter, a bias parameter and an embedding dimension parameter.

[0040] In one possible design, the training module, when calculating the similarity between the fourth code snippet and the corresponding original code snippet, is also used to obtain static similarity based on static features of the fourth code snippet and the corresponding original code snippet; the static features include text features of the code itself; dynamic similarity is obtained based on dynamic features of the fourth code snippet and the corresponding original code snippet; the dynamic features include actual values ​​and states of variables during code runtime; the similarity between the fourth code snippet and the corresponding original code snippet is obtained based on the static similarity, the static similarity weight, the dynamic similarity and the dynamic similarity weight; the static similarity weight and the dynamic similarity weight are determined according to code review requirements.

[0041] In a possible design, when the training module adjusts the parameters of each attention weight in the multi-head attention weights of the encoder according to the similarity between the fourth code fragment and the corresponding original code fragment, it is specifically used to obtain the similarity loss according to the similarity between the fourth code fragment and the corresponding original code fragment; obtain the accuracy loss according to the accuracy of the modification of the fourth code fragment; obtain the total loss based on the similarity loss, the accuracy loss, the regularization loss and the corresponding weights; and adjust the parameters of each attention weight in the multi-head attention weights of the encoder according to the total loss.

[0042] In a possible design, the syntax tree attention weight is calculated by the following formula:

[0043]

[0044] in, For node v i and v j The grammatical tree attention weight between them; σ is the activation function; ei is the node v i The embedding representation of j For node v j Embedding representation of; [e i ;e j ] indicates e i and e j Perform vector concatenation; W syn is the weight matrix parameter; b syn is the bias parameter.

[0045] In one possible design, the type of attention weight is calculated by the following formula:

[0046]

[0047] in, For node v i and vj The type attention weight between them; σ is the activation function;

[0048] T(v i ) is the node v i The type representation; T(v j ) is the node v j The type of W type is the weight matrix parameter; b type is the bias parameter.

[0049] In one possible design, the semantic role attention weight is calculated by the following formula:

[0050]

[0051] in, For node e i and e j The attention weight between sem is the weight matrix parameter; d sem is the embedding dimension parameter.

[0052] In a third aspect, an embodiment of the present application further provides a computing device, including:

[0053] A memory for storing program instructions;

[0054] A processor is used to call the program instructions stored in the memory, and execute the method described in any possible design of the first aspect or the second aspect according to the obtained program instructions.

[0055] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, in which computer-readable instructions are stored. When a computer reads and executes the computer-readable instructions, the method described in any possible design of the first aspect or the second aspect mentioned above is implemented. BRIEF DESCRIPTION OF THE DRAWINGS

[0056] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0057] Figure 1 A flowchart of a code review method applicable to an embodiment of the present application;

[0058] Figure 2 A flowchart of an encoder-decoder model training method provided in an embodiment of the present application;

[0059] Figure 3 A flowchart of a method for determining the similarity of code snippets provided in an embodiment of the present application;

[0060] Figure 4 A flow chart of a method for adjusting parameters of various attention weights provided in an embodiment of the present application;

[0061] Figure 5 A schematic diagram of the structure of a code review device provided in an embodiment of the present application;

[0062] Figure 6 A schematic diagram of the structure of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0063] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application will be further described in detail below in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present application.

[0064] In the embodiments of the present application, a plurality refers to two or more than two. The words "first", "second" and the like are only used for the purpose of distinguishing descriptions and cannot be understood as indicating or implying relative importance or order.

[0065] Figure 1 A flowchart of a code review method provided in an embodiment of the present application is shown in FIG. Figure 1 As shown, the method includes:

[0066] Step 101: Generate a corresponding abstract syntax tree for the code to be reviewed.

[0067] In an embodiment of the present application, each node of the abstract syntax tree represents a basic syntax unit in the syntax structure of the code to be reviewed, such as an expression, a statement, etc. For example, a basic syntax unit can be an assignment statement, a loop statement, or a judgment statement, etc.

[0068] Specifically, generating the corresponding syntax tree from the code to be reviewed includes the following steps:

[0069] Step 1011: pre-process the code to be reviewed.

[0070] Among them, the preprocessing of the code to be reviewed includes operations such as word segmentation, stem extraction and word form restoration, and semantic processing.

[0071] Specifically, word segmentation refers to breaking down code text into basic grammatical units that the model can understand, such as keywords, operators, identifiers, etc. Correct word segmentation is a prerequisite for the model to accurately understand the structure and semantics of the code. Stemming and lemmatization refer to the processing of stemming and lemmatization for the natural language part of the code (such as comments) to normalize the word form and reduce the impact of deformation and spelling differences on model understanding. The natural language part of the code contains important descriptions of the function and purpose of the code. Through stemming and lemmatization, this part of the content is normalized, allowing the model to more accurately capture the meaning of these descriptions without being disturbed by word variants. Semantic processing refers to special processing of identifiers and string literals to retain their original meaning and purpose in the code. This includes retaining the naming conventions of identifiers and special considerations for string content. Identifiers and string literals in the code carry specific semantics. Special processing of these elements is to ensure that their semantics are not lost during model processing, so that the model can understand and generate code more accurately.

[0072] After the above preprocessing operation is performed on the code to be reviewed, the tag stream {t1, t2, ..., t n}.

[0073] Step 1012: Generate a parse tree of the code to be reviewed.

[0074] Based on the grammatical rule G of the programming language, the token stream group {t1, t2, ..., t n} into a parse tree T (Parse Tree), that is, T = Parse ({t1, t2, ..., t n}, G). The parse tree shows how the token stream of the code to be reviewed is combined according to the grammatical rules of the programming language, which is the basis for the subsequent construction of the abstract syntax tree AST. Each node of the parse tree represents a basic grammatical unit, such as an expression, statement, etc.

[0075] Step 1013: Generate an abstract syntax tree of the code to be reviewed based on the parse tree of the code to be reviewed.

[0076] The parse tree T shows the grammatical structure of the code in detail and retains all the grammatical details, including those parts that are not necessary for understanding the program logic (such as separators). By merging or deleting some unnecessary nodes in the parse tree, the structure and grammatical relationships required by the code logic are retained, and the parse tree is abstracted into an abstract syntax tree AST, that is, A = Abstract(T), making the tree more concise and easy to handle.

[0077] Step 1014: perform identifier binding on the abstract syntax tree.

[0078] Identifiers are usually used to refer to variables, functions, or other entities. The process of identifier binding is to associate the use of each identifier with the location where it is declared. Specifically, by traversing the abstract syntax tree AST, for each node N in the abstract syntax tree AST, whenever an identifier declaration is encountered, its location and scope T are recorded. N In subsequent passes, whenever a use of the same identifier is encountered, it is bound to the declaration. This step connects the use of the identifier with its declaration, ensuring that when the same identifier is used in different places in the code, it can correctly refer to the same entity.

[0079] For example, suppose we have the following Java code:

[0080]

[0081] In this code, a and b are variable identifiers. The specific process of identifier binding is as follows:

[0082] 1. Identifier declaration:

[0083] The a in int a=5; declares an integer variable a and assigns it a value of 5.

[0084] The b in int b=10; declares an integer variable b and assigns it a value of 10.

[0085] 2. Identifier usage:

[0086] a and b in int sum = a + b; are used as operands for the sum operation.

[0087] In System.out.println("Sum: "+sum); the sum is used as the variable for outputting the result.

[0088] 3. Identifier binding:

[0089] During the identifier binding process, the compiler traverses the abstract syntax tree AST and binds the use of sum to its declaration in int sum = a + b;. Similarly, it binds the use of a and b to their declarations int a = 5; and int b = 10; respectively.

[0090] Identifier binding ensures that when the code uses the same identifier in different contexts, it can correctly refer to the same entity, avoiding confusion caused by identifiers with the same name. In addition, binding identifiers is a prerequisite for type checking, scope checking, and other semantic analysis. For example, only after the identifier is bound can the compiler check whether a and b in "a+b" have compatible types. (For example, Boolean type and Int type cannot be added). In addition, through identifier binding, the compiler can detect the use of undeclared identifiers or identify unused variables. This not only helps code optimization, but also avoids potential runtime errors.

[0091] Step 1015: Perform type analysis on each node of the abstract syntax tree.

[0092] Infer or check the types of expressions and variables in each node of the abstract syntax tree AST. Types include basic types (such as integers and strings) and composite types (such as classes and interfaces). Based on the binding information of identifiers and the type rules of the language, recursively traverse the abstract syntax tree AST and apply the type rule R to each node N for type checking T N =TypeCheck(N,R). Check type compatibility for assignment statements and expressions.

[0093] In some dynamically typed or type inferred languages, the type of a variable may not be explicitly declared, and its type is inferred by analyzing the context of the variable usage and assignment expressions. For example, if a variable is assigned a string literal, the type of the variable is inferred to be string.

[0094] The following is a specific example to illustrate the specific steps of generating the corresponding abstract syntax tree for the code to be reviewed:

[0095] Assume that the code to be reviewed is as follows:

[0096]

[0097]

[0098] 1. Preprocess the code to be reviewed

[0099] Perform word segmentation, stem extraction and lemmatization on the code to be reviewed, and decompose the code into a token stream: [public,class,Example,{,public,static,void,main,(,String,[,],args,),{,int,a,=,5,;,int,b,=,10,;,int,sum,=,a,+,b,;,System,.,out,.,println,(,",Sum:",+,sum,),;,},}]

[0100] 2. Generate a parse tree for the code to be reviewed

[0101] The partial representation of the parse tree of the above code under review is as follows:

[0102]

[0103]

[0104]

[0105] The interpretation of the above parse tree is as follows:

[0106] ClassDeclaration represents a class declaration. The child nodes "public" and "class" are the class modifiers and keywords. "Example" is the class name. ClassBody: represents the body of the class, including the class members (methods and variables).

[0107] MethodDeclaration represents a method declaration. The child nodes "public", "static", and "void" are the method modifiers and return types. "main" is the method name. MethodParameters represents the parameter list of the method, including parameter types and parameter names. Block: represents the method body, including one or more statements.

[0108] BlockStatement: represents each statement in the method body, VariableDeclaration: represents a variable declaration, including the variable type (such as "int"), variable name (such as "a"), and initialization value (such as "5").

[0109] ExpressionStatement: represents an expression statement, and MethodCall represents a method call, including the method name (such as "System.out.println") and calling parameters.

[0110] 3. Generate an abstract syntax tree of the code to be reviewed based on the parse tree of the code to be reviewed

[0111] The partial representation of the abstract syntax tree AST of the above code to be reviewed is as follows:

[0112] ClassDeclaration->Example

[0113] MethodDeclaration->main

[0114] VariableDeclaration->a,b,sum

[0115] Assignment->a=5,b=10,sum=a+b

[0116] ExpressionStatement->System.out.println("Sum:"+sum)

[0117] 4. Bind identifiers to the abstract syntax tree

[0118] Binds the use of sum to its declaration in int sum = a + b;; and binds the uses of a and b to their declarations int a = 5; and int b = 10;, respectively.

[0119] 5. Perform type analysis on each node of the abstract syntax tree

[0120] The variables a and b are of type int; the result of the expression a+b is of type int.

[0121] In a possible implementation, after the code to be audited generates a corresponding abstract syntax tree, the dynamic code data when the code to be audited is running can also be added to the abstract syntax tree to obtain an enhanced syntax tree. The dynamic code data includes the actual value and state of the variables when the code to be audited is running. Then, in the subsequent steps, the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be audited are calculated, including: calculating the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the enhanced syntax tree of the code to be audited.

[0122] In the process of node context inference, dynamic code data DCD is generated by dynamically analyzing the runtime behavior of the code. The following is a specific example to illustrate the specific steps of generating the corresponding enhanced syntax tree for the code to be reviewed:

[0123] Assume that the code to be reviewed is as follows:

[0124]

[0125] Assume that the object value when the above code to be reviewed is: a=5

[0126] b=10

[0127] sum=15

[0128] Enhanced AST nodes:

[0129] ClassDeclaration:

[0130] name:Example

[0131] type:class

[0132] MethodDeclaration:

[0133] name:main

[0134] type:void

[0135] parameters:[String[]args]VariableDeclaration:

[0136] name:a

[0137] type:int

[0138] value:5

[0139] VariableDeclaration:

[0140] name:b

[0141] type:int

[0142] value:10

[0143] VariableDeclaration:

[0144] name:sum

[0145] type:int

[0146] value:15

[0147] Assignment:

[0148] variable:a

[0149] value:5

[0150] Assignment:

[0151] variable:b

[0152] value:10

[0153] Assignment:

[0154] variable:sum

[0155] value:15

[0156] ExpressionStatement:

[0157] expression:System.out.println("Sum:"+sum)

[0158] Dynamic code data DCD node:

[0159] VariableState:

[0160] variable:a

[0161] state:5

[0162] VariableState:

[0163] variable:b

[0164] state:10

[0165] VariableState:

[0166] variable:sum

[0167] state:15

[0168] MethodCall:

[0169] method:System.out.println

[0170] arguments:"Sum:15"

[0171] Combine the enhanced AST node and the dynamic code data DCD node to get the enhanced syntax tree: ClassDeclaration->Example:

[0172] AST:ClassDeclaration(name=Example,type=class)

[0173] MethodDeclaration->main:

[0174] AST:MethodDeclaration(name=main,type=void,parameters=[String[]args])VariableDeclaration->a:

[0175] AST:VariableDeclaration(name=a,type=int,value=5)

[0176] DCD:VariableState(variable=a,state=5)

[0177] VariableDeclaration->b:

[0178] AST:VariableDeclaration(name=b,type=int,value=10)

[0179] DCD:VariableState(variable=b,state=10)

[0180] VariableDeclaration->sum:

[0181] AST:VariableDeclaration(name=sum,type=int,value=15)

[0182] DCD:VariableState(variable=sum,state=15)

[0183] Assignment->a:

[0184] AST:Assignment(variable=a,type=int,value=5)

[0185] Assignment->b:

[0186] AST:Assignment(variable=b,type=int,value=10)

[0187] Assignment->sum:

[0188] AST:Assignment(variable=sum,type=int,value=15)

[0189] ExpressionStatement->System.out.println("Sum:"+sum):

[0190] AST:ExpressionStatement(expression=System.out.println("Sum:"+sum))

[0191] DCD:MethodCall(method=System.out.println,arguments="Sum:15")

[0192] Step 102: Calculate the syntax tree attention weight, type attention weight, and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed.

[0193] It should be noted that if the syntax tree is an enhanced syntax tree, the syntax tree attention weight, type attention weight, and semantic role attention weight of each node of the enhanced syntax tree of the code to be reviewed are calculated.

[0194] In one possible implementation, the syntax tree attention weight is calculated using the following formula:

[0195]

[0196] in, For node v i and v i The grammatical tree attention weight between them; σ is the activation function; ei is the node v i The embedding representation of j For node v j Embedding representation of; [e i ;e j ] indicates e i and e j Perform vector concatenation; W syn is the weight matrix parameter; b syn is the bias parameter.

[0197] For example, suppose the enhanced AST node representation of a code snippet of the code to be reviewed is as follows:

[0198] ClassDeclaration->Example

[0199] MethodDeclaration->main

[0200] VariableDeclaration->a(dynamic value: 5)

[0201] VariableDeclaration->b (dynamic value: 10)

[0202] VariableDeclaration->sum(dynamic value: 15)

[0203] Assignment->a=5

[0204] Assignment->b=10

[0205] Assignment->sum=a+b

[0206] ExpressionStatement->System.out.println("Sum:"+sum)

[0207] Assume that the embedded representation and dynamic value of the node are as follows:

[0208] VariableDeclaration a:

[0209] Embedding representation a =[0.1,0.2]

[0210] Dynamic value d a =5

[0211] VariableDeclaration sum:

[0212] Embedding representation sum =[0.3,0.4]

[0213] Dynamic value d sum =15

[0214] Weight matrix W syn =[[0.5,0.6],[0.7,0.8]]

[0215] Bias term b syn =[0.1]

[0216] The activation function σ is ReLU

[0217] Calculate the syntax tree attention weight

[0218] a i j(syn)=σ(W s yn[e i ;e j ;d i ;d j ]+b s yn)

[0219] =σ([0.5,0.6],[0.7,0.8][0.1,0.2,0.3,0.4,5,15]+0.1)

[0220] =σ([0.5*0.1+0.6*0.2+0.5*0.3+0.6*0.4+0.5*5+0.6*15,0.7*0.1+

[0221] 0.2+0.7*0.3+0.8*0.4+0.7*5+0.8*15]+0.1)

[0222] =σ([3.7,5.0]+0.1)

[0223] =σ([3.8,5.1])

[0224] =[3.8,5.1](RelU)

[0225] In the above technical solution, when calculating the attention weight of the syntax tree, not only the content of the nodes (represented by embedding) but also their structural relationship in the syntax tree is considered, thereby achieving an in-depth understanding and analysis of the code structure.

[0226] In one possible implementation, the type attention weight is calculated by the following formula:

[0227]

[0228] in, For node v i and v j The type attention weight between them; σ is the activation function;

[0229] T(v i ) is the node v i The type representation; T(v j ) is the node v j These representations are vectors obtained through type resolution; W type is the weight matrix parameter; b type is the bias parameter. The weight matrix parameter and the bias parameter are used to adjust the strength of the interaction between different types. The activation function σ model can determine which types of interactions are most critical to understanding and reviewing the code in a given code context.

[0230] For example, suppose the enhanced AST node representation of a code snippet of the code to be reviewed is as follows:

[0231] ClassDeclaration->Example

[0232] MethodDeclaration->main

[0233] VariableDeclaration->a(dynamic value: 5)

[0234] VariableDeclaration->b (dynamic value: 10)

[0235] VariableDeclaration->sum(dynamic value: 15)

[0236] Assignment->a=5

[0237] Assignment->b=10

[0238] Assignment->sum=a+b

[0239] ExpressionStatement->System.out.println("Sum:"+sum)

[0240] Assume that the node type is represented as follows:

[0241] VariableDeclaration a:

[0242] Type representation T(a) = [1,0] (representing int)

[0243] Assignment sum:

[0244] Type representation T(sum) = [1,0] (representing int)

[0245] Parameters of MethodCall:System.out.println:

[0246] Type representation: T(param) = [0,1] (representing string)

[0247] Weight matrix W type =[[0.2,0.3],[0.4,0.5]]

[0248] Bias term b type =[0.2]

[0249] The activation function σ is ReLU

[0250] Calculating attention weights

[0251]

[0252] In the above technical solution, when calculating the type attention weight, the type information in the code is explicitly considered to help the model identify potential type mismatch problems and understand the semantics of variables and function calls.

[0253] In one possible implementation, the semantic role attention weight is calculated by the following formula:

[0254]

[0255] in, For node e i and e j The attention weight between sem is the weight matrix parameter;

[0256] d sem is the embedding dimension parameter.

[0257] This formula describes how to calculate the value of two nodes e in a function call function() based on the semantic role of the function. i and e j The semantic role attention weight between them. sem is the weight matrix obtained by the model through learning, which is used to transform the embedding vector to reflect the importance of the semantic role; d sem Is a parameter of the embedding dimension, used for normalization to avoid dimensionality problems in the calculation results. Through the softmax function, the model can determine which nodes (based on their semantic roles) are more important for understanding the function behavior in a specific function call context, and thus give more attention when calculating the output of the model.

[0258] For example, suppose the semantic representation of an enhanced AST node of a code snippet to be reviewed is as follows:

[0259] MethodCall->System.out.println("Sum:"+sum)

[0260] Parameter "Sum:"+sum: consists of a string and the variable sum (dynamic value: 15).

[0261] Assume that the embedding representation of a node is as follows:

[0262] StringLiteral "Sum:":

[0263] Embedding representation str =[0.2,0.3]

[0264] Variable sum:

[0265] Embedding representation sum=[0.4,0.5]

[0266] Weight matrix W sem =[[0.1,0.2],[0.3,0.4]]

[0267] Normalization parameter d sem =1

[0268] 1. Calculate the value of each attention weight:

[0269]

[0270] Use softmax to calculate the probability distribution:

[0271] a ij (sem)=softmax([0.124,0.122])

[0272] Calculate softmax:

[0273]

[0274] For [0.124,0.122]:

[0275]

[0276] Calculation: e 0.124 ≈1.132

[0277] e 0.122 ≈1.130

[0278]

[0279] Therefore, the semantic role attention weight of the above node is: ij (sem) = [0.5009, 0.4991]

[0280] In the above technical solution, when calculating the semantic role attention weight, by focusing on the semantic roles of each entity in the function call, the model can more accurately understand the purpose and behavior of the function call.

[0281] Step 103: According to the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed, the syntax tree attention weight, type attention weight and semantic role attention weight of each code fragment of the code to be reviewed are obtained.

[0282] Specifically, for each code snippet, the contribution of each node of the abstract syntax tree included in the code snippet to the code snippet can be first determined; then the weight corresponding to each node of the abstract syntax tree included in the code snippet can be determined based on the contribution; and then the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed and the weight corresponding to each node can be used to obtain the syntax tree attention weight, type attention weight and semantic role attention weight of each code snippet of the code to be reviewed.

[0283] Among them, the contribution of a node is determined by the importance of the node, the contextual relationship of the node, the dynamic characteristics of the node, the static characteristics of the node and the complexity of the node.

[0284] Specifically, the importance of a node is mainly determined by the grammatical structure of the node. In the abstract syntax tree, different nodes correspond to different grammatical structures, such as expressions, statements, functions, classes, etc. Generally speaking, the core nodes that determine the logic of the program (such as control flow statements, function calls) have higher weights. For example, control flow statement nodes such as "if-else", "if", "for", and "while" usually have higher weights because they directly affect the execution path of the program.

[0285] Node contextual relationship: Mainly determined by the node's contextual dependency and the number of times it is referenced. If a node is relied upon in the execution or calculation of multiple other nodes, its weight will be higher. For example, a global variable or constant node is referenced in multiple places, and these referenced nodes will increase its weight. The more times an identifier is referenced by other nodes, the higher the weight of the node corresponding to the identifier. For example, if a global variable is used in multiple functions, the corresponding node contribution will be very high.

[0286] Dynamic characteristics of nodes: Mainly determined by the runtime frequency and the change of dynamic values. Some nodes may be executed frequently during the runtime of the code, such as a node in a loop body, which increases its weight because its execution frequency is much higher than other nodes. If the value change of a node during runtime has a significant impact on the final output of the program (such as a key computing node), its weight will also increase accordingly.

[0287] Static characteristics of nodes: Mainly determined by data type and semantic role. Nodes corresponding to some complex data types or data types that are crucial in the program (such as objects and arrays) may be given higher weights. If a node is a variable node or a return value node for a function parameter, it plays an important role in the entire program semantics, so its contribution may be higher.

[0288] Node complexity: Mainly determined by computational complexity. If a node represents a complex computational expression (such as nested mathematical operations or multi-level function calls), the node may be given a higher weight because it plays an important role in the code logic. Nodes with complex conditional judgments (multiple conditional combinations or nested conditions) have a higher weight because they determine the choice of different code paths.

[0289] Step 104: input each first code snippet of the code to be reviewed into the encoder in the encoder-decoder model, use the syntax tree attention weight, type attention weight and semantic role attention weight of the first code snippet as the multi-head attention weight of the encoder, and obtain the encoding of the first code snippet.

[0290] Using Attention Weights in the Encoder Multi-Head Self-Attention Mechanism The syntax tree attention weights, type attention weights, and semantic role attention weights are integrated into the Transformer encoder's multi-head self-attention mechanism to enhance the model's understanding of the code.

[0291] The specific steps include:

[0292] Step 1041: Convert the code snippet of the code to be reviewed into a vector representation X. Perform a linear transformation on the input vector X to generate query (Q), key (K) and value (V) vectors:

[0293] Q=XW Q ,K=XW K ,V=XW V

[0294] Step 1042: Calculate the weight of each attention head

[0295] (1) Syntax Tree Attention Head

[0296]

[0297] in:

[0298] (2) Type of attention head

[0299]

[0300] in:

[0301] (3) Semantic Role Attention Head

[0302]

[0303] in:

[0304] (4) Comprehensive Attention Head

[0305]

[0306] in,

[0307]

[0308] Among them, α, β, and γ are weight coefficients that determine the importance of each attention mechanism in the comprehensive weight.

[0309] (5) Standard head

[0310]

[0311] Step 1043: splice the outputs of all heads and obtain the final output through linear transformation:

[0312] MultiHead(Q,K,V)=Concat(head1,…,head h )W O

[0313] After the multi-head self-attention mechanism, residual connections and layer normalization are added. The output of the attention mechanism is then further processed by a feedforward neural network. The output of the feedforward neural network is normalized to obtain the final encoding representation. The final encoding can be converted into the corresponding required output through a decoder.

[0314] Step 105: Input the code of the first code fragment into the decoder in the encoder-decoder model to obtain a second code fragment that is a modification of the first code fragment.

[0315] Step 106: Use the second code segments corresponding to the first code segments of the code to be reviewed as the review result of the code to be reviewed.

[0316] The embodiment of the present application uses a neural network model to learn the representation and semantics of the code, and generates corresponding modification suggestions based on the input code to be reviewed. Among them, the encoder part increases the depth of understanding of the grammar and structure of the programming language through the syntax tree attention weight, type attention weight and semantic role attention weight of each code fragment, while the decoder part focuses on generating grammatically correct and logically consistent code and modification suggestions. Based on this encoder-decoder, the code can be understood more comprehensively, not just limited to grammar and rule checking. More personalized and accurate modification suggestions can be provided to help developers improve code quality and performance more effectively.

[0317] In a possible implementation, Figure 2 The encoder-decoder model is trained in the following manner:

[0318] Step 201: randomly mask at least one statement of the original code to obtain a masked code.

[0319] Based on the CodeT5 framework, at least one statement of the original code P can be randomly selected and several spans of code fragments can be replaced with [BLANK] to form a blocked code P′.

[0320] The training data used in the embodiment of the present application to train the encoder-decoder model comes from CodeSearchNet, which supports multiple programming languages ​​such as Python, Java, JavaScript, PHP, Ruby, Go, etc.

[0321] Step 202: predict the content of each occlusion statement in the occlusion code to obtain a filling suggestion and a corresponding probability for each occlusion statement.

[0322] The Code T5 model predicts multiple possible filling suggestions S = {s1, s2, ..., s n} and determine the probability corresponding to each filling suggestion.

[0323] Step 203: fill the occlusion statements in the occlusion code according to the filling suggestion and the corresponding probability of each occlusion statement to obtain a filling code.

[0324] The filling suggestions for each occlusion statement are ranked according to probability, and the highest-ranked non-original suggestion is selected to fill the occlusion statement in the occlusion code to obtain the filling code P″. Alternatively, multiple filling suggestions with probabilities greater than a set threshold can be selected to fill the occlusion statement in the occlusion code to obtain multiple filling codes.

[0325] After all the filling is completed, the edited version P″ and the original code P form a data pair. These data pairs reflect the transformation from the original code to the code after a series of meaningful edits, providing rich learning materials for model training. In this way, the model can not only learn the basic structure and syntax of the code, but also understand and generate more complex and diverse code editing suggestions, improving the model's ability to handle complex code editing tasks.

[0326] The above steps 201 to 203 can be expressed by the following formula:

[0327] P″=CodeT5(P′,Mask)

[0328] Among them, Mask represents the replaced code span, P″ represents the filled code after the occluded code P′ is filled, and Based on P′ represents the occluded code after the original code P is occluded.

[0329] The following is a specific example to introduce the above process:

[0330] Original code(P):

[0331]

[0332] 1. Randomly block at least one statement of the original code to obtain the blocked code P':

[0333] Suppose we randomly select and replace the variable declarations int a = 5; and System.out.println("Sum: " + sum); with [BLANK]. Replacement is a random operation that can replace variables, code statements, etc.

[0334]

[0335]

[0336] 2. Predict the content of each occlusion statement in the occlusion code to obtain the filling suggestion and corresponding probability of each occlusion statement.

[0337] The model predicts fill suggestions for each [BLANK] position based on the context and ranks them according to probability. For the first [BLANK], the fill suggestions and probabilities generated by the model may be as follows:

[0338] int a=3;(0.7)

[0339] int a=1;(0.2)

[0340] int a=0;(0.1)

[0341] For the second [BLANK], the fill suggestions and probabilities generated by the model might be as follows:

[0342] System.out.println(sum); (0.6)

[0343] System.out.println("Result:"+sum); (0.5)

[0344] 3. According to the filling suggestion and corresponding probability of each occlusion statement, the occlusion statement in the occlusion code is filled to obtain the filling code.

[0345] Select the high-probability filling suggestion (probability greater than 0.6 can be selected, and multiple data pairs can be generated):

[0346] First [BLANK]: select int a=3; (0.7)

[0347] Second [BLANK]: Select System.out.println("Result: "+sum); (0.6)

[0348] Updated code:

[0349] Replace the first [BLANK]: int a = 3;

[0350] Replace the second [BLANK]: System.out.println("Result: "+sum);

[0351] The filling code P" (which can generate multiple data pairs) is as follows:

[0352]

[0353]

[0354] The original code P and the padding code P″ form a data pair (P, P”).

[0355] Step 204: input at least one third code segment in the filling code into the encoder-decoder model to obtain a fourth code segment that is obtained by modifying the third code segment.

[0356] Step 205: Calculate the similarity between the fourth code snippet and the corresponding original code snippet.

[0357] In a possible implementation, Figure 3 The method shown in the figure calculates the similarity between the fourth code snippet and the corresponding original code snippet, including the following steps:

[0358] Step 301: Obtain static similarity based on static features of the fourth code snippet and the corresponding original code snippet.

[0359] The static features include the text features of the code itself. For example, the static similarity can be calculated by the following formula:

[0360]

[0361] Among them, Sim static (C1, C2) represents the static similarity between two code snippets C1 and C2.

[0362] Step 302: Obtain dynamic similarity based on dynamic features of the fourth code snippet and the corresponding original code snippet.

[0363] The dynamic features include the actual value and state of the variables when the code is running. For example, the dynamic similarity can be calculated by the following formula:

[0364]

[0365] Among them, Sim dynamic (C1, C2) represents the dynamic similarity between two code snippets C1 and C2.

[0366] It should be noted that the order of step 301 and step 302 is not critical.

[0367] Step 303: Obtain the similarity between the fourth code snippet and the corresponding original code snippet according to the static similarity, the static similarity weight, the dynamic similarity and the dynamic similarity weight.

[0368] Among them, the static similarity weight and the dynamic similarity weight are determined according to the code review requirements, and the static similarity weight and the dynamic similarity weight can also be dynamically adjusted according to the specific code review requirements. For example, in the reconstruction scenario, more emphasis may be placed on the matching of dynamic features, and the dynamic similarity weight is larger; while in the style consistency check, more emphasis may be placed on the matching of static features, and the static similarity weight is larger. Exemplarily, the similarity between the fourth code snippet and the corresponding original code snippet can be calculated by the following formula:

[0369] Sim(C1,C2)=α·Sim static (C1,C2)+β·Sim dynamic (C1,C2)

[0370] C1, C2 represent two code snippets respectively; Sim static (C1,C2) represents static similarity; Sim dynamic (C1, C2) represents dynamic similarity; α represents the weight coefficient of static similarity; β represents the dynamic similarity weight coefficient, which is used to adjust the contribution of static features and dynamic features in the overall similarity calculation respectively.

[0371] Step 206: According to the similarity between the fourth code fragment and the corresponding original code fragment, the parameters of each attention weight in the encoder's multi-head attention weight are adjusted.

[0372] Among them, the multi-head attention weights include syntax tree attention weights, type attention weights and semantic role attention weights; the parameters include weight matrix parameters, bias parameters and embedding dimension parameters.

[0373] In the embodiment of the present application, the objective function of the encoder-decoder model training can be expressed as:

[0374]

[0375] in, represents the fourth code snippet, i.e., the code snippet outputted by the encoder-decoder model, represents the original code snippet, is a function that measures the difference between the original code snippet and the code snippet output by the encoder-decoder model, and N is the number of training samples.

[0376] In one possible implementation, reference Figure 4 , according to the similarity between the fourth code snippet and the corresponding original code snippet, adjusting the parameters of each attention weight in the multi-head attention weight of the encoder, comprising the following steps:

[0377] Step 401: Obtain similarity loss according to the similarity between the fourth code segment and the corresponding original code segment.

[0378] Step 402: Obtain accuracy loss according to the accuracy modified by the fourth code snippet.

[0379] Step 403: Obtain a total loss based on the similarity loss, accuracy loss, regularization loss and their corresponding weights.

[0380] For example, the total loss can be calculated by the following formula:

[0381]

[0382] in, represents the total loss; Represents the similarity loss, which is used to evaluate the similarity between code snippets; represents the accuracy loss, which is used to evaluate the accuracy of the generated code modification suggestions; is the regularization loss, which is used to prevent the model from overfitting; α, β, and λ are hyperparameters used to balance the importance of different parts of the loss.

[0383] Step 404: adjust the parameters of each attention weight in the encoder's multi-head attention weight according to the total loss.

[0384] In one possible implementation, when the parameters of each attention weight in the encoder's multi-head attention weights are adjusted according to the total loss, the learning rate can be dynamically adjusted based on the training loss of the model to quickly adapt to the complexity of the data and the difficulty of learning, reduce training time and improve the accuracy of the model.

[0385] For example, the learning rate η t The update rule at time step t is as follows:

[0386]

[0387] Among them, γ is the preset attenuation factor, Represents the rate of change of the current batch loss and the previous batch loss. This dynamic adjustment mechanism allows the model to reduce the learning rate when the loss decreases slowly to avoid oscillation caused by too large a step size, and increase the learning rate when the loss decreases rapidly to accelerate the learning process.

[0388] In one possible implementation, the encoder-decoder model is first trained on an initial dataset to learn code review and modification suggestion generation. The trained model is then applied to the actual code review task, and user feedback on the generated modification suggestions (such as acceptance, rejection, modification, etc.) is collected. The collected user feedback is analyzed to identify the strengths and weaknesses of the model performance, and the model parameters or training strategy are adjusted accordingly. Based on the adjusted model parameters or strategy, the model is retrained and applied to the code review task, forming a continuous optimization cycle.

[0389] The above technical solution allows the model to self-adjust and optimize based on user feedback, thereby continuously improving performance in real application scenarios. This mechanism ensures that the model can remain efficient and accurate in long-term use and adapt to changing programming practices and needs.

[0390] Based on the same technical concept, Figure 5 A schematic diagram of a code review device provided in an embodiment of the present application is shown in FIG. Figure 5 As shown, the device 500 includes:

[0391] Processing module 501 is used to generate a corresponding abstract syntax tree from the code to be reviewed; each node of the abstract syntax tree represents a basic syntax unit in the syntax structure of the code to be reviewed;

[0392] The processing module 501 is further used to calculate the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed;

[0393] The processing module 501 is further used to obtain the syntax tree attention weight, type attention weight and semantic role attention weight of each code fragment of the code to be reviewed according to the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed;

[0394] An encoding module 502 is used to input each first code snippet of the code to be reviewed into an encoder in an encoder-decoder model, and use the syntax tree attention weight, type attention weight and semantic role attention weight of the first code snippet as the multi-head attention weight of the encoder to obtain the encoding of the first code snippet;

[0395] A decoding module 503, configured to input the encoding of the first code segment into a decoder in an encoder-decoder model to obtain a second code segment that is a modification of the first code segment;

[0396] The processing module 501 is further configured to use the second code segments corresponding to the first code segments of the code to be reviewed as the review result of the code to be reviewed.

[0397] In one possible design, after generating a corresponding abstract syntax tree for the code to be audited, the processing module 501 is further used to add dynamic code data of the code to be audited during runtime to the abstract syntax tree to obtain an enhanced syntax tree; the dynamic code data includes the actual values ​​and states of the variables of the code to be audited during runtime; the processing module 501, when calculating the syntax tree attention weights, type attention weights and semantic role attention weights of each node of the abstract syntax tree of the code to be audited, is specifically used to calculate the syntax tree attention weights, type attention weights and semantic role attention weights of each node of the enhanced syntax tree of the code to be audited.

[0398] In one possible design, the processing module 501, when obtaining the syntax tree attention weights, type attention weights and semantic role attention weights of each code fragment of the code to be reviewed based on the syntax tree attention weights, type attention weights and semantic role attention weights of each node of the abstract syntax tree of the code to be reviewed, is specifically used to, for each code fragment, determine the contribution of each node of the abstract syntax tree included in the code fragment to the code fragment; determine the weight corresponding to each node of the abstract syntax tree included in the code fragment based on the contribution; obtain the syntax tree attention weights, type attention weights and semantic role attention weights of each code fragment of the code to be reviewed based on the syntax tree attention weights, type attention weights and semantic role attention weights of each node of the abstract syntax tree of the code to be reviewed and the weights corresponding to each node.

[0399] In one possible design, the device also includes a training module 504, which is used to train the encoder-decoder model in the following manner: randomly mask at least one statement of the original code to obtain a masked code; predict the content of each masked statement in the masked code to obtain a filling suggestion and a corresponding probability for each masked statement; fill the masked statements in the masked code according to the filling suggestion and the corresponding probability of each masked statement to obtain a filled code; input at least one third code fragment in the filled code into the encoder-decoder model to obtain a fourth code fragment that is modified from the third code fragment; calculate the similarity between the fourth code fragment and the corresponding original code fragment; according to the similarity between the fourth code fragment and the corresponding original code fragment, adjust the parameters of each attention weight in the multi-head attention weight of the encoder; the multi-head attention weight includes syntax tree attention weight, type attention weight and semantic role attention weight; the parameters include weight matrix parameters, bias parameters and embedding dimension parameters.

[0400] In one possible design, the training module 504, when calculating the similarity between the fourth code snippet and the corresponding original code snippet, is also used to obtain static similarity based on static features of the fourth code snippet and the corresponding original code snippet; the static features include text features of the code itself; dynamic similarity is obtained based on dynamic features of the fourth code snippet and the corresponding original code snippet; the dynamic features include actual values ​​and states of variables during code runtime; the similarity between the fourth code snippet and the corresponding original code snippet is obtained based on the static similarity, the static similarity weight, the dynamic similarity and the dynamic similarity weight; the static similarity weight and the dynamic similarity weight are determined according to code review requirements.

[0401] In one possible design, the training module 504, when adjusting the parameters of each attention weight in the multi-head attention weight of the encoder according to the similarity between the fourth code fragment and the corresponding original code fragment, is specifically used to obtain a similarity loss according to the similarity between the fourth code fragment and the corresponding original code fragment; obtain an accuracy loss according to the accuracy of the modification of the fourth code fragment; obtain a total loss based on the similarity loss, the accuracy loss, the regularization loss and the corresponding weights; and adjust the parameters of each attention weight in the multi-head attention weight of the encoder according to the total loss.

[0402] In a possible design, the syntax tree attention weight is calculated by the following formula:

[0403]

[0404] in, For node v i and v j The grammatical tree attention weight between them; σ is the activation function; e i For node v i The embedding representation of j For node v j Embedding representation of i ;e j ] indicates e i and e j Perform vector concatenation; W syn is the weight matrix parameter; b syn is the bias parameter.

[0405] In one possible design, the type of attention weight is calculated by the following formula:

[0406]

[0407] in, For node v i and v j The type attention weight between them; σ is the activation function; T(v i ) is the node v i The type representation; T(v j ) is the node v j The type of W type is the weight matrix parameter; b type is the bias parameter.

[0408] In one possible design, the semantic role attention weight is calculated by the following formula:

[0409]

[0410] in, For node e i and e j The attention weight between sem is the weight matrix parameter;

[0411] d sem is the embedding dimension parameter.

[0412] Based on the same technical concept, the present application embodiment provides a computing device, such as Figure 6 As shown, it includes at least one processor 601 and a memory 602 connected to the at least one processor. The specific connection medium between the processor 601 and the memory 602 is not limited in the embodiment of the present application. Figure 6 For example, the processor 601 and the memory 602 are connected via a bus. The bus can be divided into an address bus, a data bus, a control bus, etc.

[0413] In the embodiment of the present application, the memory 602 stores instructions that can be executed by at least one processor 601, and the at least one processor 601 can execute the above-mentioned code review method by executing the instructions stored in the memory 602.

[0414] The processor 601 is the control center of the computing device, and can use various interfaces and lines to connect various parts of the computer device, and perform resource settings by running or executing instructions stored in the memory 602 and calling data stored in the memory 602.

[0415] Optionally, the processor 601 may include one or more processing units, and the processor 601 may integrate an application processor and a modem processor, wherein the application processor mainly processes an operating system, a user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the modem processor may not be integrated into the processor 601. In some embodiments, the processor 601 and the memory 602 may be implemented on the same chip, and in some embodiments, they may also be implemented separately on separate chips.

[0416] Processor 601 can be a general-purpose processor, such as a central processing unit (CPU), a digital signal processor, an application-specific integrated circuit (ASIC), a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component, and can implement or execute the methods, steps and logic block diagrams disclosed in the embodiments of the present application. A general-purpose processor can be a microprocessor or any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application can be directly embodied as a hardware processor for execution, or can be executed by a combination of hardware and software modules in the processor.

[0417] The memory 602 is a non-volatile computer-readable storage medium that can be used to store non-volatile software programs, non-volatile computer executable programs and modules. The memory 602 may include at least one type of storage medium, such as a flash memory, a hard disk, a multimedia card, a card-type memory, a random access memory (Random Access Memory, RAM), a static random access memory (Static Random Access Memory, SRAM), a programmable read-only memory (Programmable Read Only Memory, PROM), a read-only memory (Read Only Memory, ROM), an electrically erasable programmable read-only memory (Electrically Erasable Programmable Read-Only Memory, EEPROM), a magnetic memory, a disk, an optical disk, etc. The memory 602 is any other medium that can be used to carry or store a desired program code in the form of an instruction or data structure and can be accessed by a computer, but is not limited thereto. The memory 602 in the embodiment of the present application can also be a circuit or any other device that can realize a storage function, for storing program instructions and / or data.

[0418] Based on the same technical concept, an embodiment of the present application also provides a computer-readable storage medium, which stores a computer-executable program. The computer-executable program is used to enable a computer to execute the code review method listed in any of the above methods.

[0419] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0420] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0421] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0422] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0423] Although the preferred embodiments of the present application have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications falling within the scope of the present application.

[0424] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.

Claims

1. A code review method, characterized in that: The method comprises: Generate a corresponding abstract syntax tree from the code to be reviewed; each node of the abstract syntax tree represents a basic syntax unit in the syntax structure of the code to be reviewed; Calculate the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed; According to the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed, the syntax tree attention weight, type attention weight and semantic role attention weight of each code fragment of the code to be reviewed are obtained; Input each first code snippet of the code to be reviewed into an encoder in an encoder-decoder model, use the syntax tree attention weight, type attention weight, and semantic role attention weight of the first code snippet as the multi-head attention weight of the encoder, and obtain the encoding of the first code snippet; Inputting the encoding of the first code snippet into a decoder in the encoder-decoder model to obtain a second code snippet that is a modification of the first code snippet; The second code segments corresponding to the first code segments of the code to be reviewed are used as the review results of the code to be reviewed.

2. The method according to claim 1, characterized in that After generating the corresponding abstract syntax tree for the code to be reviewed, it also includes: Adding dynamic code data when the code to be audited is running to the abstract syntax tree to obtain an enhanced syntax tree; the dynamic code data includes actual values ​​and states of variables when the code to be audited is running; Calculating the syntax tree attention weight, type attention weight, and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed, including: The syntax tree attention weight, type attention weight, and semantic role attention weight of each node of the enhanced syntax tree of the code to be reviewed are calculated.

3. The method according to claim 1, characterized in that According to the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed, the syntax tree attention weight, type attention weight and semantic role attention weight of each code fragment of the code to be reviewed are obtained, including: For each code snippet, determining the contribution of each node of the abstract syntax tree included in the code snippet to the code snippet; Determine a weight corresponding to each node of an abstract syntax tree included in the code snippet based on the contribution; According to the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed and the weight corresponding to each node, the syntax tree attention weight, type attention weight and semantic role attention weight of each code fragment of the code to be reviewed are obtained.

4. The method according to claim 1, characterized in that: The encoder-decoder model is trained as follows: Randomly mask at least one statement of the original code to obtain a masked code; Predicting the content of each occlusion statement in the occlusion code to obtain a filling suggestion and a corresponding probability for each occlusion statement; Filling the occlusion statements in the occlusion code according to the filling suggestion and the corresponding probability of each occlusion statement to obtain a filling code; Inputting at least one third code segment in the padding code into the encoder-decoder model to obtain a fourth code segment that is a modification of the third code segment; Calculating the similarity between the fourth code snippet and the corresponding original code snippet; According to the similarity between the fourth code fragment and the corresponding original code fragment, the parameters of each attention weight in the multi-head attention weight of the encoder are adjusted; the multi-head attention weight includes syntax tree attention weight, type attention weight and semantic role attention weight; the parameters include weight matrix parameters, bias parameters and embedding dimension parameters.

5. The method according to claim 4, characterized in that Calculating the similarity between the fourth code snippet and the corresponding original code snippet includes: Obtaining static similarity based on static features of the fourth code snippet and the corresponding original code snippet; the static features include text features of the code itself; Obtaining a dynamic similarity based on dynamic features of the fourth code snippet and the corresponding original code snippet; the dynamic features including actual values ​​and states of variables at code runtime; The similarity between the fourth code snippet and the corresponding original code snippet is obtained according to the static similarity, the static similarity weight, the dynamic similarity and the dynamic similarity weight; the static similarity weight and the dynamic similarity weight are determined according to the code review requirements.

6. The method according to claim 4, characterized in that According to the similarity between the fourth code snippet and the corresponding original code snippet, adjusting the parameters of each attention weight in the multi-head attention weight of the encoder includes: Obtaining a similarity loss according to a similarity between the fourth code snippet and the corresponding original code snippet; The accuracy modified according to the fourth code snippet obtains an accuracy loss; Based on the similarity loss, the accuracy loss, the regularization loss and their corresponding weights, a total loss is obtained; The parameters of each attention weight in the encoder's multi-head attention weights are adjusted according to the total loss.

7. The method according to claim 1, characterized in that The syntax tree attention weight is calculated by the following formula: in, For node v i and v j The grammatical tree attention weight between them; σ is the activation function; e i For node v i The embedding representation of j For node v j Embedding representation of; [e i ;e j ] indicates e i and e j Perform vector concatenation; W syn is the weight matrix parameter; b syn is the bias parameter.

8. The method according to claim 1, characterized in that The type of attention weight is calculated by the following formula: in, For node v i and v j The type attention weight between them; σ is the activation function; T(v i ) is the node v i The type representation; T(v j ) is the node v j The type of W type is the weight matrix parameter; b type is the bias parameter.

9. The method according to claim 1, characterized in that: The semantic role attention weight is calculated by the following formula: in, For node e i and e j The attention weight between sem is the weight matrix parameter; d sem is the embedding dimension parameter.

10. A code review device, characterized in that: include: A processing module, used to generate a corresponding abstract syntax tree from the code to be reviewed; Each node of the abstract syntax tree represents a basic syntax unit in the syntax structure of the code to be reviewed; The processing module is further used to calculate the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed; The processing module is further used to obtain the syntax tree attention weight, type attention weight and semantic role attention weight of each code fragment of the code to be reviewed according to the syntax tree attention weight, type attention weight and semantic role attention weight of each node of the abstract syntax tree of the code to be reviewed; An encoding module, configured to input each first code snippet of the code to be reviewed into an encoder in an encoder-decoder model, use the syntax tree attention weight, type attention weight, and semantic role attention weight of the first code snippet as the multi-head attention weight of the encoder, and obtain an encoding of the first code snippet; A decoding module, configured to input the encoding of the first code segment into a decoder in an encoder-decoder model to obtain a second code segment that is a modification of the first code segment; The processing module is further configured to use the second code segments corresponding to the first code segments of the code to be reviewed as the review result of the code to be reviewed.