Code defect detection method and system based on large model
By adopting a large-model-based method in code defect detection, using language models, sequence pattern recognition, graph neural networks and reinforcement learning algorithms, the problem of poor code defect detection in the existing technology is solved, efficient and accurate defect recognition and repair suggestions are achieved, and code quality and the reliability of repair solutions are improved.
Patent Information
- Application Number
- CN202510206786.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-25
- Publication Date
- 2025-03-25
- Estimated Expiration
- 2045-02-25
AI Technical Summary
The existing technology is not effective in code defect detection, making it difficult to efficiently and accurately identify and solve code problems. Especially in large projects, traditional methods are difficult to provide effective repair suggestions and ignore the overall quality evaluation of the code.
The code defect detection method based on large models is adopted, and a pre-trained large-scale language model is used to combine attention mechanisms for semantic understanding, sequence pattern recognition algorithm and graph neural network to identify logical error patterns, and defect type and location are determined through fuzzy matching algorithms. Then, a remediation code snippet is generated using a reinforcement learning algorithm and a quality score is performed through the code quality evaluation model, providing repair suggestions and change comments.
Improves the accuracy of code defect detection and the quality of repair suggestions, reduces the need for manual intervention, improves the effectiveness and reliability of repair solutions, and ensures that the repaired code maintains good maintainability and performance.
Smart Images

Figure CN119690512A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of code detection technology, and in particular to a code defect detection method and system based on a large model. Background Art
[0002] During the software development process, the detection and repair of code defects is a key link in ensuring software quality. As the scale of software continues to expand and the complexity increases, traditional manual review or rule-based static analysis tools can no longer meet the needs of efficiently and accurately identifying and solving code problems. Especially in large projects, developers often need to deal with a large amount of code, which puts higher requirements on automated defect detection. Therefore, there is a strong demand in the market for technical solutions that can automatically detect and intelligently repair code defects, especially in terms of improving detection accuracy, reducing false alarm rates, and providing high-quality repair suggestions.
[0003] There are many methods and technologies for code defect detection on the market, including but not limited to rule-based static analysis tools, which use a series of predefined rules to check whether there are potential problems in the code. And by actually running the program to observe its behavior, runtime errors can be discovered. Including machine learning-assisted analysis systems, which use historical data to train models to predict possible defect types. Although these methods have improved the ability to detect code defects to a certain extent, they each have limitations.
[0004] Rule-based methods usually rely on hand-written rule sets, which makes it difficult for them to capture all types of logical errors and is prone to high false positive rates. Although dynamic analysis can locate problems more accurately, it requires a specific test environment setup and is powerless against errors on non-execution paths. Most existing tools focus on the defect detection stage and rarely provide effective repair suggestions. Even if a few tools try to make modification suggestions, they are usually generated based on simple templates and lack flexibility and adaptability. Traditional methods often focus on defect detection in a single dimension (such as security or functionality), ignoring the overall quality assessment of the code, such as readability, performance, and other aspects. In addition, existing solutions rarely consider long-term influencing factors such as the maintenance cost of the repaired code. Summary of the invention
[0005] The embodiments of the present application provide a large model-based code defect detection method and system to solve the problem of poor code defect detection effect in the prior art.
[0006] In a first aspect, an embodiment of the present application provides a code defect detection method based on a large model, comprising: Using a pre-trained large-scale language model combined with an attention mechanism, a semantic understanding process is performed on the input initial source code to generate an enhanced semantic understanding vector corresponding to the initial source code; According to the enhanced semantic understanding vector, a sequential pattern recognition algorithm combined with a graph neural network is used for analysis to identify a logic error pattern, and the logic error pattern is compared with a fuzzy matching algorithm based on a known defect library to determine the defect type and location of the initial source code; Based on the defect type and location of the initial source code, a reinforcement learning algorithm is used to generate a repair code snippet, wherein the reinforcement learning algorithm selects an optimal repair path by simulating multiple repair paths and evaluating the effectiveness of each repair path, and generates a repair code snippet through the optimal repair path; Replacing the corresponding part of the initial source code with the repair code fragment and generating a target source code; Performing a quality score on the target source code based on a code quality assessment model to obtain a quality feedback result, and generating a corresponding change annotation in the target source code for the code replaced by the repair code snippet; Outputting the target source code with the change annotation and the quality feedback result.
[0007] Optionally, the generating of the repair code snippet by using a reinforcement learning algorithm based on the defect type and location of the initial source code, wherein the reinforcement learning algorithm selects an optimal repair path by simulating multiple repair paths and evaluating the effectiveness of each repair path, and generates the repair code snippet by using the optimal repair path, includes: Determine code defects using defect types and locations of the initial source code; Initializing the reinforcement learning environment to obtain a code state including the initial source code and an environment state of the code defect; According to the environment state, corresponding actions are defined and constituted into an action set, wherein the actions are used to repair the code defect; Based on the action set, a reinforcement learning algorithm is used to simulate multiple repair paths, each repair path corresponds to an action sequence, and the action sequence consists of multiple actions; For each repair path, a corresponding action sequence is executed, and a code state of the initial source code after the action sequence is executed is evaluated to obtain a reward value reflecting the repair effect; According to the reward value, evaluating the effectiveness of each repair path, and selecting the repair path with the highest reward value as the optimal repair path; A repair code snippet is generated based on the action sequence included in the optimal repair path.
[0008] Optionally, generating a repair code snippet based on the action sequence included in the optimal repair path includes: Parsing each action in the action sequence in the optimal repair path to obtain an operation type and an action scope of each action, wherein the operation type includes replacement, insertion or deletion; Generate corresponding code modification instructions based on the operation type and scope of each action; Based on the code modification instruction, the initial source code is modified to obtain a repair code fragment.
[0009] Optionally, the step of analyzing the enhanced semantic understanding vector using a sequential pattern recognition algorithm combined with a graph neural network to identify a logic error pattern, and comparing the logic error pattern using a fuzzy matching algorithm based on a known defect library to determine the defect type and location of the initial source code includes: Using the enhanced semantic understanding vector, performing sequence pattern recognition processing on the initial source code to obtain a logic error pattern; Building a graph structure based on the abstract syntax tree of the initial source code, wherein nodes in the graph structure represent code elements, and edges represent relationships between the code elements, and processing the graph structure using a graph neural network to extract relationship information between the code elements and generate a structural feature representation; combining the logical error pattern with the structural feature representation to form a comprehensive feature representation; Based on the comprehensive feature representation, a fuzzy matching algorithm is used to compare the defect information in the known defect library, and the defect information matching the logic error pattern is identified by traversing the similarity scores between the comprehensive feature representation and the plurality of defect information; The defect type and location of the initial source code are generated according to the defect information.
[0010] Optionally, the comparing the comprehensive feature representation with the defect information in the known defect library by using a fuzzy matching algorithm, and identifying the defect information matching the logic error pattern by traversing the similarity scores between the comprehensive feature representation and a plurality of the defect information, includes: Using the comprehensive feature representation as an input vector, a fuzzy matching algorithm is performed on each defect information in the known defect library; Calculating a similarity score between the comprehensive feature representation and each defect information, wherein the similarity score reflects the degree of matching between the two; Traversing the similarity scores between all defect information and the comprehensive feature representation, and selecting the defect information with the highest score as the defect information matching the logical error pattern; The step of generating the defect type and location of the initial source code according to the defect information includes: The defect type and position corresponding to the defect information are used as the defect type and position of the initial source code.
[0011] Optionally, the using of a pre-trained large-scale language model in combination with an attention mechanism to perform semantic understanding processing on the input initial source code to generate an enhanced semantic understanding vector corresponding to the initial source code includes: Using a pre-trained large-scale language model, the initial source code is encoded to obtain a preliminary semantic representation. Based on the preliminary semantic representation, an attention mechanism is applied to perform weighted processing on key code snippets to generate an enhanced semantic understanding vector.
[0012] Optionally, the performing a quality score on the target source code based on a code quality assessment model to obtain a quality feedback result includes: Analyze and process the target source code using a pre-trained code quality assessment model to extract various indicators reflecting code quality; According to various indicators reflecting code quality, the target source code is scored to obtain a quality score; Based on the quality score, a quality feedback result is generated, which includes an overall quality evaluation of the code, a specific problem description, and improvement suggestions.
[0013] In a second aspect, an embodiment of the present application provides a code defect detection system based on a large model, including: A generation module, used to use a pre-trained large-scale language model combined with an attention mechanism to perform semantic understanding processing on the input initial source code, and generate an enhanced semantic understanding vector corresponding to the initial source code; A determination module is used to identify the logic error pattern by using a sequence pattern recognition algorithm combined with a graph neural network to analyze the enhanced semantic understanding vector, and compare the logic error pattern with a fuzzy matching algorithm based on a known defect library to determine the defect type and location of the initial source code; The generation module is further used to generate a repair code snippet based on the defect type and location of the initial source code by using a reinforcement learning algorithm, wherein the reinforcement learning algorithm selects an optimal repair path by simulating multiple repair paths and evaluating the effectiveness of each repair path, and generates a repair code snippet through the optimal repair path; replace the corresponding part in the initial source code with the repair code snippet, and generate a target source code; and generate a corresponding change annotation in the target source code for the code replaced by the repair code snippet; A calculation module, used to perform a quality score on the target source code based on a code quality assessment model to obtain a quality feedback result; The output module is used to output the target source code with the change annotation and the quality feedback result.
[0014] In a third aspect, an embodiment of the present application provides a computing device, comprising a processing component and a storage component; the storage component stores one or more computer instructions; the one or more computer instructions are used to be called and executed by the processing component to implement a large model-based code defect detection method as described in the first aspect above.
[0015] In a fourth aspect, an embodiment of the present application provides a computer storage medium storing a computer program. When the computer program is executed by a computer, it implements a code defect detection method based on a large model as described in the first aspect.
[0016] In an embodiment of the present application, a pre-trained large-scale language model is used in combination with an attention mechanism to perform semantic understanding processing on the input initial source code to generate an enhanced semantic understanding vector corresponding to the initial source code; according to the enhanced semantic understanding vector, a sequential pattern recognition algorithm is used in combination with a graph neural network for analysis to identify a logical error pattern, and the logical error pattern is compared with a fuzzy matching algorithm based on a known defect library to determine the defect type and location of the initial source code; based on the defect type and location of the initial source code, a reinforcement learning algorithm is used to generate a repair code snippet, the reinforcement learning algorithm simulates multiple repair paths and evaluates the effectiveness of each repair path to select the optimal repair path, and generates a repair code snippet through the optimal repair path; the repair code snippet replaces the corresponding part in the initial source code and generates a target source code; the target source code is scored based on a code quality assessment model to obtain a quality feedback result, and in the target source code, a corresponding change annotation is generated for the code replaced by the repair code snippet; the target source code with the change annotation and the quality feedback result is output.
[0017] The technical solution of this application has the following beneficial effects: This application uses a pre-trained large-scale language model combined with an attention mechanism to more deeply parse the semantic information of the source code and generate an enhanced semantic understanding vector. This helps to improve the understanding of complex programming logic, thereby more accurately identifying potential defects. And use reinforcement learning algorithms to generate and evaluate different repair paths, select the optimal solution, and provide developers with high-quality repair suggestions. This approach not only reduces the need for manual intervention, but also improves the effectiveness and reliability of the repair solution. After the repair is completed, by scoring the quality of the target source code and adding change comments, it is ensured that the repaired code not only solves the original problem, but also maintains good maintainability and performance.
[0018] Furthermore, combining sequence pattern recognition with graph neural networks can capture logical error patterns in the code from different angles. This method not only takes into account the linear structure of the code, but also analyzes the relationship between code elements, enhancing the ability to detect complex logical errors. By comparing the known defect library with the fuzzy matching algorithm, the specific defect type and location in the code can be more accurately determined. This pattern matching-based method can effectively reduce the false alarm rate and improve the accuracy of the detection results. And by using the abstract syntax tree to build a graph structure and processing it through the graph neural network, the system can better understand the internal structure of the code, thereby more accurately locating the specific location of the defect.
[0019] Furthermore, by traversing all possible defect information and calculating the similarity score with the comprehensive feature representation, the best matching defect record can be quickly found. This method greatly improves the matching efficiency, especially when facing a large-scale defect library. And the use of fuzzy matching algorithms allows a certain degree of incomplete matching, which is particularly useful for dealing with defects that are slightly different in expression but are essentially the same or similar. This can more comprehensively cover various forms of defects and improve the coverage and accuracy of identification. And as newly discovered defects are continuously added to the known defect library, this method can easily adapt to these changes and continuously improve its detection capabilities. At the same time, by continuously optimizing the matching algorithm, the overall performance of the system can be further improved.
[0020] These and other aspects of the present application will become more clearly understood in the description of the following embodiments. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0022] Figure 1 A flowchart of a code defect detection method based on a large model provided in an embodiment of the present application; Figure 2 A schematic diagram of the structure of a code defect detection system based on a large model provided in an embodiment of the present application; Figure 3 A schematic diagram of the structure of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0023] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application.
[0024] In some of the processes described in the specification and claims of this application and the above-mentioned figures, multiple operations that appear in a specific order are included, but it should be clearly understood that these operations may not be executed in the order in which they appear in this article or executed in parallel. The serial numbers of the operations, such as 101, 102, etc., are only used to distinguish between different operations, and the serial numbers themselves do not represent any execution order. In addition, these processes may include more or fewer operations, and these operations may be executed in sequence or in parallel. It should be noted that the descriptions of "first", "second", etc. in this article are used to distinguish different messages, devices, modules, etc., do not represent the order of precedence, and do not limit the "first" and "second" to be different types.
[0025] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of this application.
[0026] Figure 1 A flowchart of a code defect detection method based on a large model is provided for an embodiment of the present application, such as Figure 1 As shown, the method includes: 101. Using a pre-trained large-scale language model combined with an attention mechanism, perform semantic understanding processing on the input initial source code to generate an enhanced semantic understanding vector corresponding to the initial source code; A large-scale language model refers to a language processing model trained with a large amount of text data and capable of understanding complex semantic relationships.
[0027] Attention is a technique that enables a model to focus on specific parts of an input sequence, thereby improving processing efficiency and accuracy.
[0028] The enhanced semantic understanding vector refers to a representation generated based on the source code, which captures the deep semantic information of the code for subsequent processing.
[0029] In the field of software development, you can use pre-trained models like CodeBERT, which are optimized for programming languages. When a piece of Java or Python code is input, the model can output a vector that contains important information such as the function of the code and the relationship between variables.
[0030] Optionally, in step 101, "using a pre-trained large-scale language model in combination with an attention mechanism to perform semantic understanding processing on the input initial source code to generate an enhanced semantic understanding vector corresponding to the initial source code" includes: using a pre-trained large-scale language model to encode the input initial source code to obtain a preliminary semantic representation; based on the preliminary semantic representation, applying an attention mechanism to weight key code fragments to generate an enhanced semantic understanding vector.
[0031] The use of pre-trained large-scale language models combined with attention mechanisms to process source code aims to understand the semantic structure of the code through deep learning technology. The pre-trained large-scale language model is trained based on a large amount of programming language data and can capture the grammatical and semantic information in the code; the attention mechanism is a technology that allows the model to focus on specific parts when processing sequences, thereby better understanding the key parts of the code. The enhanced semantic understanding vector generated by this method is a high-level representation of the input code, which not only reflects the basic structure of the code, but also highlights the parts that have an important impact on the logic or function implementation.
[0032] First, the original source code is converted into a preliminary semantic representation through a pre-trained large-scale language model. In this step, the model automatically encodes the vocabulary, grammar, and contextual information in the code. Next, the attention mechanism is used to further process this preliminary representation. In particular, it will give different weights according to the importance of each code snippet, so that the final generated enhanced semantic understanding vector can more accurately reflect the core logic and key features of the code. The entire process, from encoding to weighted processing, is completed automatically in order to improve the efficiency and accuracy of subsequent steps (such as error detection and repair).
[0033] In an embodiment of the present application, consider a Java development project as an example, assuming that there is a class file containing multiple method definitions. First, use the CodeBERT model that has been trained with a large amount of Java code as a pre-trained large-scale language model, take the entire class file as input, and the model outputs a preliminary semantic representation of each method and its surrounding context. Next, apply an attention mechanism to these representations, such as giving higher weights to methods involving complex conditional judgments or loop control flows, because such codes are often more prone to logical errors. The final enhanced semantic understanding vector not only contains information about the overall structure of the class file, but also places special emphasis on areas where potential problems may exist. Such output can be used to support more accurate error location and suggestion generation.
[0034] 102. According to the enhanced semantic understanding vector, a sequential pattern recognition algorithm is used in combination with a graph neural network to perform analysis to identify a logic error pattern, and the logic error pattern is compared with a fuzzy matching algorithm based on a known defect library to determine the defect type and location of the initial source code; Sequential pattern recognition refers to discovering frequently occurring patterns from a series of events.
[0035] Graph neural network is a deep learning method suitable for processing graph structure data and can be used to analyze complex structures such as program control flow graphs.
[0036] In an embodiment of the present application, for a given section of C++ code, it is first converted into an abstract syntax tree (AST) form, and then a graph neural network is applied to analyze the AST, and a sequential pattern recognition algorithm is combined to detect whether there are common error patterns such as infinite loops and deadlocks.
[0037] Optionally, in step 102, "according to the enhanced semantic understanding vector, a sequential pattern recognition algorithm is used in combination with a graph neural network for analysis to identify a logical error pattern, and the logical error pattern is compared with a fuzzy matching algorithm based on a known defect library to determine the defect type and location of the initial source code" includes: using the enhanced semantic understanding vector to perform sequential pattern recognition processing on the initial source code to obtain a logical error pattern; constructing a graph structure based on an abstract syntax tree of the initial source code, wherein nodes in the graph structure represent code elements, and edges represent relationships between the code elements, and using a graph neural network to process the graph structure to extract relationship information between code elements and generate a structural feature representation; merging the logical error pattern with the structural feature representation to form a comprehensive feature representation; based on the comprehensive feature representation, using a fuzzy matching algorithm to compare with defect information in a known defect library, and identifying defect information matching the logical error pattern by traversing the similarity scores between the comprehensive feature representation and multiple defect information; and generating the defect type and location of the initial source code according to the defect information.
[0038] Sequential pattern recognition algorithm is a technique for discovering recurring subsequences from time series data. Here it is used to analyze the logic flow in the source code to identify potential error patterns. Graph neural network (GNN) is a neural network model specially designed to process graph structured data, which can effectively capture the relationship and dependency between nodes. Abstract syntax tree (AST) is a tree representation of program source code, in which each node represents a construction unit, such as variable declaration, function call, etc. Fuzzy matching algorithm allows comparison of two or more elements without complete precision, and is usually used for text search and similarity calculation. And each defect information includes at least defect description, defect sample code, defect type, location and association ID. Among them, defect description refers to a detailed text description of each defect, including problem manifestation, impact range, etc. Defect sample code refers to a specific code snippet or pattern that shows how the defect appears in the code. Defect type refers to classification according to the nature of the defect, such as logical error, memory leak, security vulnerability, etc. Location refers to the specific location of the defect in the code (such as file name, line number). Association ID refers to a unique identifier associated with the defect for easy tracking and management. This solution aims to accurately locate and classify logical errors in source code by combining these technical means.
[0039] First, the enhanced semantic understanding vector generated in step 101 is used to perform sequence pattern recognition processing on the initial source code to extract possible logical error patterns. Next, the corresponding abstract syntax tree is constructed based on the source code and converted into a graph structure suitable for graph neural network processing, where nodes represent code elements and edges reflect the relationship between these elements. Then, the graph neural network is used to learn the graph structure and extract deep structural feature representations. Next, the previously identified logical error pattern is merged with the newly generated structural feature representation to form a more comprehensive comprehensive feature representation. Finally, a fuzzy matching algorithm is used to compare this comprehensive feature representation with the information in the known defect library, and the closest defect type and its location are determined by calculating the similarity score, thereby completing the specific positioning of the logical errors in the source code.
[0040] In an embodiment of the present application, it is assumed that in a Python project, there is a section of code containing complex conditional judgments. First, a sequential pattern recognition algorithm is used to analyze this section of code to identify patterns that may misuse conditional operators. Subsequently, a corresponding abstract syntax tree is established based on this section of code and converted into a graph structure, in which each node corresponds to an operation or variable in the code, and the edge represents the control flow or data dependency. This graph structure is processed using a pre-trained graph neural network model to obtain high-level features that reflect the internal structural characteristics of the code. After that, the previously identified logical error patterns are combined with these structural features to form a comprehensive feature vector. In the last step, this comprehensive feature vector is compared with various known defect features stored in the database through a fuzzy matching algorithm, such as a common problem template such as "the wrong operator is used in the conditional expression". Finally, the system will report that there is a logical error in the code due to the misuse of == instead of !=, and indicate the specific line number to help developers quickly locate and fix the problem.
[0041] Optionally, in step 102, "based on the comprehensive feature representation, using a fuzzy matching algorithm to compare with defect information in a known defect library, and by traversing the similarity scores between the comprehensive feature representation and multiple defect information to identify defect information matching the logical error pattern" includes: using the comprehensive feature representation as an input vector, executing a fuzzy matching algorithm on each defect information in a known defect library; calculating the similarity score between the comprehensive feature representation and each defect information, wherein the similarity score reflects the degree of matching between the two; traversing the similarity scores between all defect information and the comprehensive feature representation, and selecting the defect information with the highest score as the defect information matching the logical error pattern; generating the defect type and location of the initial source code according to the defect information includes: using the defect type and location corresponding to the defect information as the defect type and location of the initial source code.
[0042] Fuzzy matching algorithm is a technique for comparing two or more elements without complete precision, which allows matching within a certain error range. This method is widely used in fields such as text search and pattern recognition to improve matching capabilities in the presence of noise or variants. In this scheme, the fuzzy matching algorithm is used to compare the comprehensive feature representation (a vector that combines code logic error patterns and structural features) with the defect information in the known defect library, and the most likely defect type is determined by calculating the similarity score. The similarity score is a quantitative indicator to measure the degree of similarity between two sets of data, and is used here to evaluate the degree of match between a given code snippet and a known defect.
[0043] First, the comprehensive feature representation is used as input, and the fuzzy matching algorithm is executed for each entry in the known defect library. Then, a similarity score is calculated for each pair of comprehensive feature representation and defect information, which directly reflects the degree of match between the two. After that, by traversing all the obtained similarity scores, the record with the highest score is selected, which indicates that the defect information is most likely to correspond to the logical error pattern existing in the current source code. The last step is to clearly indicate the specific defect type and location in the initial source code based on the selected defect information, so as to provide accurate information support for subsequent repair work.
[0044] In the embodiment of the present application, it is assumed that a section of C++ code is being processed, and a comprehensive feature representation has been obtained through the previous steps. Now, this representation is used to match a database containing various common programming error types. For example, there are entries such as "null pointer dereference" and "array out-of-bounds access" in the database. For each entry, a fuzzy matching algorithm (such as Levenshtein distance or Jaccard similarity coefficient) is used to calculate the similarity score between it and the comprehensive feature representation. If it is found that "null pointer dereference" has the highest score, then the system will think that this section of C++ code is likely to have such a problem. Next, based on this defect information, it will not only report that this is a "null pointer dereference" error, but also specifically point out which line of code has this problem, such as "there may be a risk of null pointer dereference on line 45". In this way, developers can quickly locate the problem and take appropriate measures to solve it.
[0045] 103. Based on the defect type and location of the initial source code, generate a repair code snippet using a reinforcement learning algorithm, wherein the reinforcement learning algorithm selects an optimal repair path by simulating multiple repair paths and evaluating the effectiveness of each repair path, and generates a repair code snippet through the optimal repair path; Reinforcement learning is a machine learning method that learns how to make decisions through a reward / penalty mechanism.
[0046] A repair path is a sequence of actions from the current state to the desired state.
[0047] In an embodiment of the present application, assuming that the system detects an error of an uninitialized variable, in the process of attempting to repair it, the reinforcement learning algorithm will explore multiple possible modification methods (such as direct assignment, modification of function parameters, etc.) and select the most effective solution based on the simulation results.
[0048] Optionally, in step 103, “generating a repair code snippet by using a reinforcement learning algorithm based on the defect type and location of the initial source code, wherein the reinforcement learning algorithm simulates multiple repair paths and evaluates the effectiveness of each repair path to select an optimal repair path, and generates a repair code snippet by using the optimal repair path” includes: The code defect is determined by using the defect type and location of the initial source code; the reinforcement learning environment is initialized to obtain a code state including the initial source code and an environment state of the code defect; according to the environment state, corresponding actions are defined and an action set is formed, and the action is used to repair the code defect; based on the action set, a reinforcement learning algorithm is used to simulate multiple repair paths, each repair path corresponds to an action sequence, and the action sequence is composed of multiple actions; for each repair path, a corresponding action sequence is executed, and the code state of the initial source code after executing the action sequence is evaluated to obtain a reward value reflecting the repair effect; according to the reward value, the effectiveness of each repair path is evaluated, and the repair path with the highest reward value is selected as the optimal repair path; based on the action sequence contained in the optimal repair path, a repair code fragment is generated.
[0049] In this solution, the environment state is a vectorized representation of the source code text structure, defect location, and type characteristics. The action set refers to predefined atomic repair operations (such as variable initialization, conditional branch insertion, etc.). The reward function refers to a quantitative evaluation system based on test pass rate, code quality indicators, and repair completeness to obtain a reward value.
[0050] In the embodiment of the present application, first enter the defective code snippet (Java language): public class PaymentProcessor { public void processTransaction() { UserAccount account; if (account.getBalance()>0) { / / Defect: account object is not initialized executePayment(); } } } The environmental state is encoded as: { "code_state": { "ast_hash": "a1b2c3d4", "variables": ["account"], "method_calls": ["getBalance()"], "defect_type": "NP_NULL_ON_SOME_PATH", "defect_location": [3, 15] } } Secondly, candidate actions are generated according to the defect type, as shown in Table 1 below: Table 1
[0051] Furthermore, a repair path simulation is performed, assuming that three typical repair paths are generated as examples: Path α: A1 → End (To simplify the process, assume that path α has only one action A1. In actual applications, it can be a collection of multiple actions) UserAccount account = new UserAccount(); Path β: A2 → End (To simplify the process, assume that path β has only one action A2. In actual applications, it can be a collection of multiple actions) if (account != null&&account.getBalance()>0); Path γ: A3 → A2 → End UserAccount account = getCurrentAccount(); if (account != null&&account.getBalance()>0); Next, the reward value is calculated by building a multi-dimensional evaluation system: def calculate_reward(modified_code): # Test coverage (weight 40%) test_score = run_unit_tests(modified_code) 40 # Code quality (weight 30%) quality_score = (1 - cyclomatic_complexity_growth) 30 # Repair integrity (weight 20%) defect_remain = static_analysis(modified_code) completeness = (1 - defect_remain) 20 # Side effect penalty (weight 10%) penalty = -10 if find_new_warnings() else 0 return test_score + quality_score + completeness + penalty The evaluation results of each path are shown in Table 2 below: Table 2 Next, the optimal path is selected through iterative calculation of Q-value (γ=0.9): Q(s,γ) = 0.9 (87) + 0.1 (0.95 87) ≈ 83.2; Q(s,α) = 75 (1-0.9) + 0.9 0 = 7.5; Q(s,β) = 61 (1-0.9) + 0.9 (-10) = -2.9; Step 6: Generate a repair code snippet based on the action sequence contained in the optimal repair path γ, which specifically includes the following process: The sequence of actions to parse path γ: A3 Action Execution: - UserAccount account; + UserAccount account = getCurrentAccount(); A2 action execution: - if (account.getBalance()>0) + if (account != null&&account.getBalance()>0) Final fixed code: public class PaymentProcessor { public void processTransaction() { UserAccount account = getCurrentAccount(); if (account != null&&account.getBalance()>0) { executePayment(); } } } This embodiment fully demonstrates the whole process from defect location to repair solution generation by constructing a code repair framework based on reinforcement learning: first, the code defect is converted into a computable environmental state, and a set of atomic repair actions is defined; then, multiple repair paths are simulated, and the reward value is calculated through a multi-dimensional evaluation system; then, the Q-learning algorithm is used to select the optimal repair path, and specific code modification instructions are generated; final verification shows that this method can effectively repair null pointer exception defects and improve code quality without introducing new problems, which fully proves the feasibility and effectiveness of the solution and meets the requirements of full disclosure.
[0052] This application takes into account that in the software development process, code repair not only needs to solve known defects, but also needs to ensure the quality of the repaired code. To this end, a reward function is designed to comprehensively evaluate the repair effect. , which takes into account several key factors: degree of defect repair, code readability, code performance, code complexity, and maintenance cost. In this way, it can encourage the generation of both effective and high-quality fixes.
[0053] Optionally, the step 103 of “for each repair path, executing a corresponding action sequence, and evaluating the code state of the initial source code after executing the action sequence to obtain a reward value reflecting the repair effect” includes: For each repair path, execute the corresponding action sequence; After executing the action sequence, based on the new code state , define a complex reward function , used to comprehensively evaluate the repair effect, the reward function is determined according to the degree of defect repair, code readability, code performance, code complexity and code maintenance cost; Specifically, the reward function It can be calculated by the following formula: ; in, Indicates the change in the degree of defect repair. The specific expression is: ; in, is the number of defects before repair, is the number of defects after repair. These two values can be obtained through static analysis tools or defect detection tools; Indicates the change in code readability. The specific expression is: ; in, and The code readability scores before and after the repair are obtained through static analysis tools (such as cyclomatic complexity, number of lines of code, etc.); and are the maximum and minimum possible readability scores, respectively. These values can be set using historical data or empirical values. For example, assuming (best readability), (worst readability).
[0054] Indicates the change in code performance. The specific expression is: ; in, and The code performance scores before and after the repair are obtained through performance testing tools (such as execution time, memory usage, etc.); and are the maximum and minimum possible performance scores, respectively. These values can be set using historical data or empirical values. For example, assuming (Best Performance), (worst performance).
[0055] Indicates the change in code complexity. The specific expression is: ; in, and They are the code complexity scores before and after the fix. These scores can be obtained through static analysis tools (such as control flow complexity, number of lines of code, etc.); and are the possible maximum and minimum complexity scores, respectively, which can be set through historical data or experience. (highest complexity), (Minimum complexity).
[0056] Represents the change in code maintenance cost. The specific expression is: ; in, and They are the code maintenance cost scores before and after the repair; these scores can be obtained through static analysis tools (such as annotation coverage, modularity, etc.); and are the possible maximum and minimum maintenance cost scores, respectively. These values can be set from historical data or empirical values. For example, assuming (highest maintenance cost), (Minimum maintenance cost).
[0057] and are the weight coefficients of each factor, satisfying ; and is an adjustment factor used to control the sensitivity of readability and performance changes; and It is a penalty factor used to control the penalty intensity of code complexity and maintenance cost increase.
[0058] This reward function aims to balance the optimization objectives of different aspects. Bug fixing is the top priority, so its weight is usually high. At the same time, good code structure (such as readability and low complexity) and efficient execution efficiency are also important considerations. In addition, considering the need for long-term maintenance, low maintenance costs are equally important. The overall formula is in the form of a weighted sum, which allows the importance of each factor to be adjusted according to specific project needs.
[0059] The following is a brief introduction to the design reasons of each sub-item of the formula: ; in, Indicates the change in the degree of defect repair. The specific expression is: ; Indicates the change in code readability. The specific expression is: ; Indicates the change in code performance. The specific expression is: ; Indicates the change in code complexity. The specific expression is: ; Represents the change in code maintenance cost. The specific expression is: ; Changes in the degree of defect repair: As the core goal of the repair process, the degree of defect repair directly reflects the effectiveness of the repair operation. By calculating the change ratio of the number of defects before and after the repair, the effectiveness of the repair can be quantified. A higher value means more defects are successfully fixed, thus improving the overall quality of the code. Changes in code readability: Code readability is an important indicator of how easy it is to understand and maintain a code. By introducing the Sigmoid function , even small improvements can be positively incentivized. This encourages the repair process to not only solve defects, but also focus on code structure and clarity, making the code more readable and easier to maintain. Code performance changes: Performance is a key factor in software efficiency. By using the Sigmoid function , ensuring that even small performance improvements are rewarded. This incentivizes fixes to not only solve problems, but also optimize code execution efficiency, thereby improving user experience and system responsiveness. Changes in code complexity: Excessive code complexity will lead to difficulties in maintenance and increased potential error risks. This is treated as a penalty term and a negative weight is used. and adjustment factors To control the negative impact of increased complexity. This encourages fixes to reduce defects while avoiding the introduction of complex code structures and keeping the code concise and clear.
[0060] The following is a brief introduction to how to obtain the parameters of the formula: in, and : It can be automatically calculated by static analysis tools or defect detection tools (such as SonarQube, FindBugs, etc.). and Use static analysis tools to evaluate code characteristics, such as cyclomatic complexity, number of lines, and other indicators. and It uses performance testing tools (such as JMeter, LoadRunner, etc.) to measure the performance of the program at runtime. and Evaluate code complexity related metrics through static analysis tools. and It is evaluated based on static analysis results, such as annotation coverage, modularity, etc. Weight coefficient ( , , , ) and adjustment factor ( , , , ) is set according to project requirements and determined based on expert opinions or historical data analysis.
[0061] In the embodiment of the present application, it is assumed that there is a method in a Java project, and after a series of repair actions, it is desired to evaluate the effects of these repair actions. The following is a specific example of numerical substitution: Number of defects ; Readability score ; Performance Rating ; Complexity score ; Maintenance cost score ; Set the weight coefficient to ; The adjustment factor is , ; The penalty factor is , ; Calculate the scores: , , , , ; Substituting into the formula: ; Calculation results: The first item: , the second item: , the third item: , the fourth item: ; Final score: .
[0062] According to the above calculation results, this repair action received a reward value of approximately 0.5206. This shows that the repair action not only reduced defects, but also improved the readability and performance of the code. Although the code complexity and maintenance cost increased slightly, the overall repair effect was still good. In particular, the significant improvement in the degree of defect repair (from 3 to 1) contributed the most to the total score, and although other factors also had a positive effect, their impact on the final score was relatively limited due to their small weights. In this case, developers can further optimize those parts that lead to increased complexity and maintenance costs to achieve a better balance.
[0063] Optionally, the "generating a repair code snippet based on the action sequence included in the optimal repair path" in step 103 includes: parsing each action in the action sequence in the optimal repair path to obtain an operation type and a scope of action for each action, wherein the operation type includes replacement, insertion or deletion; generating corresponding code modification instructions according to the operation type and scope of action of each action; and modifying the initial source code based on the code modification instructions to obtain a repair code snippet.
[0064] In the process of generating repair code snippets based on the optimal repair path, key concepts include operation type and scope. The operation type defines the specific modification method to the source code, such as replacing existing code, inserting new code, or deleting part of the code; while the scope specifies the specific locations in the source code where these modifications should be applied. By combining the operation type and scope of each action, detailed code modification instructions can be formed, which directly guide how to change the original code to achieve defect repair.
[0065] First, for the optimal repair path selected from the reinforcement learning algorithm, parse each action in the path one by one to clarify its operation type (replacement, insertion or deletion) and scope (i.e., the specific location in the source code). Then, based on the parsed information, generate corresponding code modification instructions for each action, such as "insert a new if statement after line 10" or "replace the code between lines 5 and 7 with...". Finally, adjust the initial source code step by step according to these specific modification instructions, and finally form the repaired code fragment.
[0066] In an embodiment of the present application, it is assumed that a JavaScript program is being processed, and the optimal repair path has been determined in the previous step, which indicates that an error handling logic needs to be added in the function processData to prevent exceptions caused by null values. The first step is to parse the actions in this path and find that it requires the execution of an "insert" operation, and the scope of action is a specific location in the function processData. Next, specific code modification instructions are generated based on this information, for example: "In the processData function, add a line of code before data processing: if (data === null) {throw new Error ('Invalid data');}". The last step is to implement the above modification instructions based on the original code to ensure that the correct error checking logic is added at the appropriate location, thereby completing the repair process and generating the final repair code snippet. This not only solves potential problems, but also improves the overall robustness of the code.
[0067] 104. Replace the corresponding part of the initial source code with the repair code fragment, and generate a target source code; This step involves actually applying the best fix generated in the above steps to the original code.
[0068] If the best fix is to add a line of initialization code, that line of code is inserted exactly where it was, correcting the problem.
[0069] 105. Performing a quality score on the target source code based on a code quality assessment model to obtain a quality feedback result, and generating a corresponding change annotation in the target source code for the code replaced by the repair code snippet; Code quality assessment models are standard tools or algorithms used to measure aspects such as code readability and complexity.
[0070] In the embodiment of the present application, a tool such as SonarQube is used to evaluate the quality of the repaired code to ensure that no new problems are introduced and that the code maintains good maintainability and readability.
[0071] Optionally, "performing a quality score on the target source code based on the code quality assessment model to obtain a quality feedback result" in step 105 includes: using a pre-trained code quality assessment model to analyze and process the target source code to extract various indicators reflecting code quality; performing a quality score processing on the target source code based on various indicators reflecting code quality to obtain a quality score; and generating a quality feedback result based on the quality score, wherein the quality feedback result includes an overall quality evaluation of the code, a specific problem description, and improvement suggestions.
[0072] A code quality assessment model is a tool built on machine learning or deep learning technology that can automatically analyze source code and give evaluations based on a series of predefined quality indicators. These indicators may include but are not limited to code complexity, readability, maintainability, and whether coding standards are followed. Through this model, the overall quality of the code can be objectively quantified and specific improvement suggestions can be provided to help developers improve code standards.
[0073] First, use a pre-trained code quality assessment model to comprehensively analyze the target source code and extract key indicator data reflecting code quality from multiple dimensions. Then, based on the specific values of these indicators, a comprehensive quality score is calculated according to certain scoring rules or algorithms. This score intuitively reflects the current quality level of the code. The last step is to generate a detailed feedback report, which will not only give an overall quality evaluation, but also list the specific problems found and their locations, and put forward targeted improvement suggestions so that developers can quickly locate and solve problems.
[0074] In the embodiment of the present application, in a Python Web application development project, it is assumed that the repair work of a functional module code has been completed, and it is hoped that the quality of the code has been evaluated. Using a code quality management platform such as SonarQube as a pre-trained code quality evaluation model, the repaired code is uploaded to the system for analysis. After processing, SonarQube provides detailed data on multiple aspects such as code complexity, duplicate code ratio, unit test coverage, etc. Based on this information, the system automatically calculates a comprehensive quality score, such as 85 points (out of 100), indicating that the overall quality of the code is good but there is still room for improvement. Subsequently, SonarQube generates a comprehensive feedback report, pointing out some potential problems such as "the function calculate_discount is too long, it is recommended to split it into smaller functions", "lack of processing logic for abnormal situations", etc., and gives specific improvement suggestions, such as introducing more auxiliary functions to simplify the main logic, adding try-catch blocks to improve program stability, etc. In this way, developers can further optimize the code based on these suggestions, thereby continuously improving the overall quality of the software project.
[0075] 106. Output the target source code with the change annotation and the quality feedback result.
[0076] Change notes record what has been modified and why.
[0077] Quality feedback results provide specific evaluations of code quality.
[0078] In the embodiment of the present application, the final output includes not only the updated code file, but also detailed document descriptions, such as "Fixed the null pointer exception on line 35", "Code complexity reduced by 10%", etc., to help developers quickly understand the changes and their impact.
[0079] Figure 2 A schematic diagram of the structure of a code defect detection system based on a large model is provided for an embodiment of the present application. Figure 2 As shown, the device comprises: A generation module 21 is used to use a pre-trained large-scale language model combined with an attention mechanism to perform semantic understanding processing on the input initial source code and generate an enhanced semantic understanding vector corresponding to the initial source code; A determination module 22 is used to identify the logic error pattern based on the enhanced semantic understanding vector by using a sequence pattern recognition algorithm combined with a graph neural network for analysis, and compare the logic error pattern with a fuzzy matching algorithm based on a known defect library to determine the defect type and location of the initial source code; The generation module is further used to generate a repair code snippet based on the defect type and location of the initial source code by using a reinforcement learning algorithm, wherein the reinforcement learning algorithm selects an optimal repair path by simulating multiple repair paths and evaluating the effectiveness of each repair path, and generates a repair code snippet through the optimal repair path; replace the corresponding part in the initial source code with the repair code snippet, and generate a target source code; and generate a corresponding change annotation in the target source code for the code replaced by the repair code snippet; A calculation module 23 is used to perform a quality score on the target source code based on a code quality assessment model to obtain a quality feedback result; The output module 24 is used to output the target source code with the change annotation and the quality feedback result.
[0080] Figure 2 The code defect detection system based on a large model can be executed Figure 1 The implementation principle and technical effect of the large model-based code defect detection method described in the illustrated embodiment will not be described in detail. The specific manner in which each module and unit performs operations in the large model-based code defect detection system in the above embodiment has been described in detail in the embodiment of the method, and will not be elaborated here.
[0081] In one possible design, Figure 2 A code defect detection system based on a large model in the embodiment shown can be implemented as a computing device, such as Figure 3 As shown, the computing device may include a storage component 31 and a processing component 32; The storage component 31 stores one or more computer instructions, wherein the one or more computer instructions are called and executed by the processing component 32 .
[0082] The processing component 32 is used to: use a pre-trained large-scale language model combined with an attention mechanism to perform semantic understanding processing on the input initial source code, and generate an enhanced semantic understanding vector corresponding to the initial source code; according to the enhanced semantic understanding vector, use a sequential pattern recognition algorithm combined with a graph neural network for analysis to identify the logic error pattern, and compare the logic error pattern with a fuzzy matching algorithm based on a known defect library to determine the defect type and location of the initial source code; based on the defect type and location of the initial source code, use a reinforcement learning algorithm to generate a repair code snippet, the reinforcement learning algorithm simulates multiple repair paths and evaluates the effectiveness of each repair path to select the optimal repair path, and generates a repair code snippet through the optimal repair path; replace the corresponding part in the initial source code with the repair code snippet, and generate a target source code; perform a quality score on the target source code based on a code quality assessment model to obtain a quality feedback result, and generate a corresponding change annotation in the target source code for the code replaced by the repair code snippet; output the target source code with the change annotation and the quality feedback result.
[0083] The processing component 32 may include one or more processors to execute computer instructions to complete all or part of the steps in the above method. Of course, the processing component may also be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors or other electronic components to perform the above method.
[0084] The storage component 31 is configured to store various types of data to support operations at the terminal. The storage component can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.
[0085] Of course, the computing device may also include other components, such as input / output interfaces, display components, communication components, etc.
[0086] The input / output interface provides an interface between the processing component and the peripheral interface module, which may be an output device, an input device, etc.
[0087] The communication component is configured to facilitate, among other things, wired or wireless communications between the computing device and other devices.
[0088] Among them, the computing device can be a physical device or an elastic computing host provided by a cloud computing platform, etc. In this case, the computing device can refer to a cloud server, and the above-mentioned processing components, storage components, etc. can be basic server resources rented or purchased from the cloud computing platform.
[0089] The present application also provides a computer storage medium storing a computer program, wherein the computer program can achieve the above-mentioned Figure 1 A code defect detection method based on a large model in the illustrated embodiment.
[0090] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0091] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0092] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0093] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A code defect detection method based on a large model, characterized in that: include: Using a pre-trained large-scale language model combined with an attention mechanism, a semantic understanding process is performed on the input initial source code to generate an enhanced semantic understanding vector corresponding to the initial source code; According to the enhanced semantic understanding vector, a sequential pattern recognition algorithm combined with a graph neural network is used for analysis to identify a logic error pattern, and the logic error pattern is compared with a fuzzy matching algorithm based on a known defect library to determine the defect type and location of the initial source code; Based on the defect type and location of the initial source code, a reinforcement learning algorithm is used to generate a repair code snippet, wherein the reinforcement learning algorithm selects an optimal repair path by simulating multiple repair paths and evaluating the effectiveness of each repair path, and generates a repair code snippet through the optimal repair path; Replacing the corresponding part of the initial source code with the repair code fragment and generating a target source code; Performing a quality score on the target source code based on a code quality assessment model to obtain a quality feedback result, and generating a corresponding change annotation in the target source code for the code replaced by the repair code snippet; Outputting the target source code with the change annotation and the quality feedback result.
2. The method according to claim 1, characterized in that The method of generating a repair code snippet based on the defect type and location of the initial source code by using a reinforcement learning algorithm, wherein the reinforcement learning algorithm selects an optimal repair path by simulating multiple repair paths and evaluating the effectiveness of each repair path, and generates a repair code snippet through the optimal repair path, including: Determine code defects using defect types and locations of the initial source code; Initializing the reinforcement learning environment to obtain a code state including the initial source code and an environment state of the code defect; According to the environment state, corresponding actions are defined and constituted into an action set, wherein the actions are used to repair the code defect; Based on the action set, a reinforcement learning algorithm is used to simulate multiple repair paths, each repair path corresponds to an action sequence, and the action sequence consists of multiple actions; For each repair path, a corresponding action sequence is executed, and a code state of the initial source code after the action sequence is executed is evaluated to obtain a reward value reflecting the repair effect; According to the reward value, evaluating the effectiveness of each repair path, and selecting the repair path with the highest reward value as the optimal repair path; A repair code snippet is generated based on the action sequence included in the optimal repair path.
3. The method according to claim 2, characterized in that The generating of the repair code snippet based on the action sequence included in the optimal repair path includes: Parsing each action in the action sequence in the optimal repair path to obtain an operation type and an action scope of each action, wherein the operation type includes replacement, insertion or deletion; Generate corresponding code modification instructions based on the operation type and scope of each action; Based on the code modification instruction, the initial source code is modified to obtain a repair code fragment.
4. The method according to claim 1, characterized in that The method of analyzing the enhanced semantic understanding vector using a sequential pattern recognition algorithm combined with a graph neural network to identify a logic error pattern, and comparing the logic error pattern using a fuzzy matching algorithm based on a known defect library to determine the defect type and location of the initial source code includes: Using the enhanced semantic understanding vector, performing sequence pattern recognition processing on the initial source code to obtain a logic error pattern; Building a graph structure based on the abstract syntax tree of the initial source code, wherein nodes in the graph structure represent code elements, and edges represent relationships between the code elements, and processing the graph structure using a graph neural network to extract relationship information between the code elements and generate a structural feature representation; combining the logical error pattern with the structural feature representation to form a comprehensive feature representation; Based on the comprehensive feature representation, a fuzzy matching algorithm is used to compare the defect information in the known defect library, and the defect information matching the logic error pattern is identified by traversing the similarity scores between the comprehensive feature representation and the plurality of defect information; The defect type and location of the initial source code are generated according to the defect information.
5. The method according to claim 4, characterized in that The method of comparing the comprehensive feature representation with the defect information in the known defect library by using a fuzzy matching algorithm, and identifying the defect information matching the logic error pattern by traversing the similarity scores between the comprehensive feature representation and a plurality of the defect information, includes: Using the comprehensive feature representation as an input vector, a fuzzy matching algorithm is performed on each defect information in the known defect library; Calculating a similarity score between the comprehensive feature representation and each defect information, wherein the similarity score reflects the degree of matching between the two; Traversing the similarity scores between all defect information and the comprehensive feature representation, and selecting the defect information with the highest score as the defect information matching the logical error pattern; The step of generating the defect type and location of the initial source code according to the defect information includes: The defect type and position corresponding to the defect information are used as the defect type and position of the initial source code.
6. The method according to claim 1, characterized in that The method uses a pre-trained large-scale language model in combination with an attention mechanism to perform semantic understanding processing on the input initial source code to generate an enhanced semantic understanding vector corresponding to the initial source code, including: Using a pre-trained large-scale language model, the initial source code is encoded to obtain a preliminary semantic representation. Based on the preliminary semantic representation, an attention mechanism is applied to perform weighted processing on key code snippets to generate an enhanced semantic understanding vector.
7. The method according to claim 1, characterized in that The quality scoring of the target source code based on the code quality assessment model to obtain a quality feedback result includes: Analyze and process the target source code using a pre-trained code quality assessment model to extract various indicators reflecting code quality; According to various indicators reflecting code quality, the target source code is scored to obtain a quality score; Based on the quality score, a quality feedback result is generated, which includes an overall quality evaluation of the code, a specific problem description, and improvement suggestions.
8. A code defect detection system based on a large model, characterized in that: include: A generation module, used to use a pre-trained large-scale language model combined with an attention mechanism to perform semantic understanding processing on the input initial source code, and generate an enhanced semantic understanding vector corresponding to the initial source code; A determination module is used to identify the logic error pattern by using a sequence pattern recognition algorithm combined with a graph neural network to analyze the enhanced semantic understanding vector, and compare the logic error pattern with a fuzzy matching algorithm based on a known defect library to determine the defect type and location of the initial source code; The generation module is further used to generate a repair code snippet based on the defect type and location of the initial source code by using a reinforcement learning algorithm, wherein the reinforcement learning algorithm selects an optimal repair path by simulating multiple repair paths and evaluating the effectiveness of each repair path, and generates a repair code snippet through the optimal repair path; replace the corresponding part of the initial source code with the repair code snippet, and generate a target source code; In the target source code, generating corresponding change annotations for the code replaced by the repair code snippet; A calculation module, used to perform a quality score on the target source code based on a code quality assessment model to obtain a quality feedback result; The output module is used to output the target source code with the change annotation and the quality feedback result.
9. A computing device, characterized in that It comprises a processing component and a storage component; the storage component stores one or more computer instructions; the one or more computer instructions are used to be called and executed by the processing component to implement a large model-based code defect detection method as described in any one of claims 1 to 7.
10. A computer storage medium, characterized in that: A computer program is stored, and when the computer program is executed by a computer, a code defect detection method based on a large model as described in any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Software security vulnerability detection method based on grammatical features and semantic features
CN112541180A
Code generation and defect repair method and device
CN116909532A
Source code vulnerability detection method based on sequence and graph two-channel model
CN118296612A
Numerical control machine tool code early warning method based on fine-tuning large model
CN119045830A
Code optimization and reconstruction system
CN119415104A
Cited By
Method and device for constructing code complex defect detection model
CN120492311A
Code error repairing method and system based on large model
CN120492319A
Code repairing method and device based on large language model, equipment and medium
CN120909640A
A code repair method, device and equipment based on a large language model and a medium
CN120909640B