Code error detection method and device and storage medium
By combining the code with the detection results, generating correction suggestions, automatically positioning and correcting code errors, the problem of insufficient error coverage in the existing technology is solved, and development efficiency and project progress are improved.
Patent Information
- Application Number
- CN202510828535.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-19
- Publication Date
- 2025-07-22
AI Technical Summary
In the prior art, the code error detection method has insufficient error coverage, which causes developers to spend a lot of time positioning and fixing problems, reducing development efficiency and project progress.
By obtaining the code to be detected for preprocessing, the detection results are fused using at least one code detection algorithm, the target error prediction results are generated, and correction suggestions are generated based on the context information, and the code errors are automatically positioned and corrected.
Improve the coverage and accuracy of code errors, reduce manual intervention, improve development efficiency, and ensure project progress.
Smart Images

Figure CN120353713A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the technical field of data processing, and particularly to a code error detection method, apparatus, and storage medium. Background Art
[0002] During the software development process, the detection of code errors is a key link in ensuring software quality. Among them, traditional error detection methods can discover errors through compilers and runtime environments. Compilers can catch syntax errors during the compilation stage, but logical errors and exceptions that occur during runtime need to be discovered during program execution, resulting in insufficient error coverage. Also, when an error occurs, developers need to spend a lot of time and effort to locate the problem and fix it, reducing development efficiency and delaying project progress. Summary of the Invention
[0003] The present disclosure provides a code error detection method, apparatus, and system, which can determine a target error prediction result through at least one code detection algorithm, and generate at least one target correction suggestion based on the target prediction result and target context information, thereby improving error coverage, eliminating the need for manual location and repair, improving development efficiency, and ensuring project progress.
[0004] To solve the above technical problems, the present disclosure provides a code error detection method, including: Obtaining the code to be detected, and preprocessing the code to be detected to obtain preprocessed code; Performing code detection on the preprocessed code through at least one code detection algorithm, and fusing the obtained detection results to obtain a target error prediction result; Locating the preprocessed code based on the target error prediction result to obtain corresponding target context information; Generating at least one target correction suggestion based on the target error prediction result and the target context information; Generating a target code error detection result based on the target error prediction result and the at least one target correction suggestion.
[0005] An embodiment of the present disclosure further provides a code error detection apparatus, including: A preprocessing module, configured to obtain the code to be detected, and preprocess the code to be detected to obtain preprocessed code; A code detection module, configured to perform code detection on the preprocessed code through at least one code detection algorithm, and fuse the obtained detection results to obtain a target error prediction result; A location module, configured to locate the preprocessed code based on the target error prediction result to obtain corresponding target context information; A first generation module, configured to generate at least one target correction suggestion based on the target error prediction result and the target context information; A second generation module, configured to generate a target code error detection result based on the target error prediction result and the at least one target correction suggestion.
[0006] An embodiment of the present disclosure further provides an electronic device, including: A memory, configured to store a computer program; A processor, configured to implement the steps of any code error detection method provided by the embodiments of the present disclosure when executing the computer program.
[0007] An embodiment of the present disclosure further provides a computer-readable storage medium, in which a computer program is stored, and when the computer program is executed by a processor, the steps of any code error detection method provided by the embodiments of the present disclosure are implemented.
[0008] An embodiment of the present disclosure further provides a computer program product, including a computer program, and when the computer program is executed by a processor, the steps of any code error detection method provided by the embodiments of the present disclosure are implemented.
[0009] A code error detection method provided by the present disclosure includes: obtaining a code to be detected, and preprocessing the code to be detected to obtain a preprocessed code; performing code detection on the preprocessed code through at least one code detection algorithm, and fusing the obtained detection results to obtain a target error prediction result; positioning the preprocessed code based on the target error prediction result to obtain corresponding target context information; generating at least one target correction suggestion based on the target error prediction result and the target context information; generating a target code error detection result based on the target error prediction result and the at least one target correction suggestion. Determining the target error prediction result through at least one code detection algorithm, and generating at least one target correction suggestion based on the target prediction result and the target context information, so that the user can locate and correct the error code according to the at least one target correction suggestion, without manual location and repair, thereby improving the error coverage rate, improving the development efficiency, and ensuring the project progress.
[0010] It should be understood that the content described in this part is not intended to identify the key or important features of the embodiments of the present disclosure, nor is it used to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] The drawings are used to better understand the solution and do not constitute a limitation to the present disclosure. Among them: Figure 1Schematic flowchart of a code error detection method provided by an embodiment of the present disclosure; Figure 2 Schematic flowchart of another code error detection method provided by an embodiment of the present disclosure; Figure 3 Schematic flowchart of yet another code error detection method provided by an embodiment of the present disclosure; Figure 4 Schematic flowchart of yet another code error detection method provided by an embodiment of the present disclosure; Figure 5 Schematic flowchart of yet another code error detection method provided by an embodiment of the present disclosure; Figure 6 Schematic structural diagram of a code error detection device provided by an embodiment of the present disclosure; Figure 7 Schematic structural diagram of another code error detection device provided by an embodiment of the present disclosure; Figure 8 Schematic structural diagram of yet another code error detection device provided by an embodiment of the present disclosure. Detailed implementation manners
[0012] The following describes exemplary embodiments of the present disclosure with reference to the accompanying drawings. Various details of the embodiments of the present disclosure are included to assist in understanding, and they should be considered merely exemplary. Therefore, those of ordinary skill in the art should recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the present disclosure. Similarly, for clarity and conciseness, descriptions of well-known functions and structures are omitted below.
[0013] The following describes a code error detection method, device, and storage medium according to an embodiment of the present disclosure with reference to the accompanying drawings.
[0014] Figure 1 is a schematic flowchart of a code error detection method provided according to an embodiment of the present disclosure. As Figure 1 shown, the method may include the following steps: Step 101, obtain the code to be detected, and preprocess the code to be detected to obtain preprocessed code.
[0015] In some embodiments, the code to be detected may be in Java / C language / C++ language.
[0016] In some embodiments, after obtaining the code to be detected, the code to be detected may be preprocessed to obtain preprocessed code to clean and normalize the code to be detected for subsequent accurate and efficient analysis.
[0017] In some embodiments, the method for preprocessing the code to be detected to obtain preprocessed code may include the following steps: Step 1011, perform comment removal processing on the code to be detected through regular expressions to obtain the main code of the code to be detected.
[0018] In some embodiments, single-line comments (such as / / ) and multi-line comments ( / * * / ) in the code to be detected can be removed through regular expressions, so as to retain the main part of the code to be detected.
[0019] Step 1012, perform formatting processing on the main code to obtain preprocessed code.
[0020] In some embodiments, after obtaining the main code through the above steps, formatting processing such as indentation, line break, and space addition can be performed on the main code to obtain preprocessed code with a unified code style specification, thereby facilitating subsequent static code analysis.
[0021] Step 102, perform code detection on the preprocessed code through at least one code detection algorithm, and fuse the obtained detection results to obtain a target error prediction result.
[0022] In some embodiments, after obtaining the preprocessed code through the above steps, code detection can be performed on the preprocessed code through at least one code detection algorithm, and the detection results of each code detection algorithm obtained are fused to obtain a target error prediction result.
[0023] In some embodiments, the method for performing code detection on the preprocessed code through at least one code detection algorithm and fusing the obtained detection results to obtain a target error prediction result may include the following steps: Step 1021, perform code detection on the preprocessed code through a static code detection algorithm to obtain a first code detection result.
[0024] In some embodiments, the method for performing code detection on the preprocessed code through a static code detection algorithm to obtain a first code detection result may include: performing syntax error detection, control flow detection, and data flow detection on the preprocessed code through code explicit rules to obtain a first code detection result, where the first code detection result may include a first error list, and the first error list may include a first error type and the corresponding first code position.
[0025] In some embodiments, syntax error detection can be performed on the lexical analysis of the preprocessed code through code display rules to detect whether the preprocessed code structure conforms to the language specification (such as JAVA). For example, assuming that the code display rules include whether a variable is assigned a value after declaration, it is detected through the code display rules that the variable is not assigned a value after declaration (such as ; The variable is not assigned a value. (it is of integer type), then an error of "variable not initialized" in the syntax error type is recognized.
[0026] In some embodiments, the control flow detection of the preprocessing code can be performed through the code display rule to detect whether the conditional statements and loop logics in the preprocessing code are correct. For example, assuming that the code display rule includes whether the condition is always true, and it is detected through this code display rule that there is a condition that is always true (such as ), then an error of "condition always true" in the logical error type is recognized.
[0027] In some embodiments, the data flow detection of the preprocessing code can be performed through the code display rule to track the variable life cycle in the preprocessing code. For example, assuming that the code display rule includes whether the variable is assigned a value after declaration, and it is detected through this code display rule that the variable is not assigned a value after declaration (such as ; The variable is not assigned a value), then an error of "variable not initialized" in the syntax error type is recognized.
[0028] In some embodiments, the above first error list may include a first error type and a corresponding first code position. For example, the first error type: logical error type (array out of bounds); the first code position: line 6.
[0029] Step 1022, perform code detection on the preprocessing code through the target code detection model to obtain a second code detection result.
[0030] In some embodiments, the above target code detection model can be trained through code samples. Among them, the code samples can include normal codes and codes with various error annotations.
[0031] In some embodiments, the output of the above target code detection model may include a second error list, and this second error list may include a second error type, an error probability, and a corresponding second code position. For example, the second error type: logical error type (array out of bounds); the error probability: 98%; the second code position: line 8.
[0032] In some embodiments, the above target code detection model can be any one of a decision tree, a random forest, and a neural network.
[0033] In some embodiments, the above first error type and second error type may include syntax error type, logical error type, data flow error type, runtime exception type, and code specification problem type. By way of example, the syntax error type includes undeclared variables and unclosed parentheses; the logical error type includes always-true conditions and loop out-of-bounds; the data flow error type includes using uninitialized variables; the runtime exception type includes null pointer exceptions and file not found exceptions; the code specification problem type includes not using generics and not releasing resources.
[0034] Step 1023, fuse the first code detection result and the second code detection result to obtain a target error prediction result.
[0035] In some embodiments, after obtaining the first code detection result and the second code detection result through the above steps, the first code detection result and the second code detection result may be fused to obtain a target error prediction result.
[0036] In some embodiments, the above target error prediction result may include a target error type, an error probability, an error source, and a target code location. For example, target error type: array out-of-bounds; error probability: 98%; error source: static code detection algorithm and target code detection model; target code location: line 10.
[0037] Step 103, locate the preprocessed code based on the target error prediction result to obtain corresponding target context information.
[0038] In some embodiments, after obtaining the target error prediction result through the above steps, the preprocessed code may be located based on the target error prediction result to obtain corresponding target context information.
[0039] In some embodiments, an abstract syntax tree, a control flow graph, and a data flow graph may be generated based on the preprocessed code, and based on the target error type in the target error prediction result, the target context information corresponding to the target error type may be obtained through the abstract syntax tree, the control flow graph, and the data flow graph, so that accurate correction suggestions can be generated according to the target context information subsequently.
[0040] Step 104, generate at least one target correction suggestion based on the target error prediction result and the target context information.
[0041] In some embodiments, after obtaining the target error prediction result and the target context information through the above steps, at least one target correction suggestion may be generated based on the target error prediction result and the target context information.
[0042] In some embodiments, the method for generating at least one target correction suggestion based on the target error prediction result and the target context information may include the following steps: Step 1041, determine the target correction rule in the correction rule library that matches the target error type in the target error prediction result.
[0043] In some embodiments, the target correction rule that matches the target error type can be searched in the correction rule library according to the target error type.
[0044] Step 1042, adjust the target correction rule based on the target context information to generate at least one candidate correction suggestion.
[0045] In some embodiments, the method of adjusting the target correction rule based on the target context information to generate at least one candidate correction suggestion can include: inputting the target context information and the target correction rule into a large language model to generate at least one candidate correction suggestion. In some embodiments, the above large language model can be an existing trained model.
[0046] Step 1043, evaluate the candidate correction suggestions to obtain the scores of the candidate correction suggestions.
[0047] In some embodiments, experts can evaluate the candidate correction suggestions according to the feasibility, code style consistency, and impact on code functionality of multiple candidate correction suggestions to obtain the scores of the candidate correction suggestions.
[0048] Step 1044, generate at least one target correction suggestion based on the scores of the candidate correction suggestions.
[0049] In some embodiments, the scores of the candidate correction suggestions can be sorted in descending order, and the first threshold number of candidate correction suggestions in the descending order result can be determined as at least one target correction suggestion. Among them, the threshold number can be set as needed, such as 5.
[0050] In some embodiments, the above generation of at least one target correction suggestion through the target error prediction result and the target context information makes at least one target correction suggestion more accurate, convenient for users to correct, and improves the development efficiency.
[0051] Step 105, generate a target code error detection result based on the target error prediction result and at least one target correction suggestion.
[0052] In some embodiments, after obtaining the target error prediction result and at least one target correction suggestion through the above steps, the at least one target correction suggestion corresponding to each target error type in the target error prediction result can be merged into the target error prediction result to generate a target code error detection result.
[0053] In some embodiments, the above target code error detection result can be displayed to the user on the front-end page.
[0054] The code error detection method provided by the embodiments of the present disclosure obtains the code to be detected, and preprocesses the code to be detected to obtain preprocessed code; performs code detection on the preprocessed code through at least one code detection algorithm, and fuses the obtained detection results to obtain a target error prediction result; locates the preprocessed code based on the target error prediction result to obtain corresponding target context information; generates at least one target correction suggestion based on the target error prediction result and the target context information; generates a target code error detection result based on the target error prediction result and at least one target correction suggestion. By determining the target error prediction result through at least one code detection algorithm and generating at least one target correction suggestion based on the target prediction result and the target context information, the user can locate and correct the error code according to at least one target correction suggestion without manual location and repair, thereby improving the error coverage rate, improving the development efficiency, and ensuring the project progress.
[0055] In some embodiments, as a refinement of step 1023, as Figure 2 shown, it may further include: Step 201, match based on the first error list and the second error list to obtain first error information pointing to the same code location.
[0056] In some embodiments, the first error type, the second error type, the error probability, and the code location information corresponding to the same code location are determined as the first error information.
[0057] Step 202, obtain a first error prediction result based on the first error information.
[0058] In some embodiments, the first error prediction result pointing to the same code location in the first error list and the second error list can be obtained through the first error information, thereby reducing the false positives and false negatives of code errors and improving the error coverage rate and error accuracy.
[0059] Step 203, analyze the remaining second code locations in the second error list to obtain a second error prediction result.
[0060] In some embodiments, the remaining second code locations in the second error list except the code location in the first error information can be analyzed to obtain a second error prediction result to reduce the false positives and false negatives of code errors.
[0061] In some embodiments, the method of analyzing the remaining second code positions in the second error list to obtain a second error prediction result may include: determining, as the second error prediction result, the second error type, error probability, and corresponding second code position in the remaining second code positions in the second error list where the error probability is greater than the target probability threshold. The target probability threshold can be set as needed, such as 95%.
[0062] Step 204, fuse the first error prediction result and the second error prediction result to obtain a target error prediction result.
[0063] In some embodiments, the first error prediction result and the second error prediction result can be fused, and the corresponding error sources can be combined to obtain a target error prediction result.
[0064] It should be noted that in some embodiments, through the above steps, a target error prediction result can be obtained based on the first error list and the second error list, so that the preprocessed code can be fused and analyzed based on the detection results obtained by each code detection algorithm, reducing the false positives and false negatives of code errors and improving the error coverage and error accuracy.
[0065] In some embodiments, as a refinement of step 202, as Figure 3 shown, it may further include: Step 301, determine whether the first error type and the second error type are the same.
[0066] Step 302, if it is determined that the first error type and the second error type are the same, determine whether the error probability exceeds the preset probability threshold.
[0067] In some embodiments, if it is determined that the first error type and the second error type are the same, it can be determined that the detection results of the same code position by the static code detection algorithm and the target code detection model are the same. At this time, it can be determined whether it is the first error prediction result based on the error probability corresponding to the second error type.
[0068] In some embodiments, if it is determined that the first error type and the second error type are different, it can be determined that there is an ambiguity in the detection results of the same code position by the static code detection algorithm and the target code detection model being different. At this time, manual review can be performed, and the determined target error type and the corresponding error probability of 100% can be added to the first error prediction result.
[0069] Step 303, if it is determined that the error probability exceeds the preset probability threshold, obtain the first error prediction result based on the first error type, second error type, error probability, and code position.
[0070] In some embodiments, if it is determined that the error probability exceeds the preset probability threshold, then based on the corresponding first error type, second error type, error probability, and code location, a first error prediction result is obtained. At this time, the first error type and the second error type are the same. Based on this, the second error type, error probability, and code location can be determined as the first error prediction result.
[0071] In some embodiments, if it is determined that the error probability does not exceed the preset probability threshold, it indicates that the target code detection model obtains a relatively low error probability for this error type, and there may be a false alarm. Therefore, the error detection at this code location can be ignored.
[0072] In some embodiments, the above-mentioned preset probability threshold can be set as needed, and this preset probability threshold corresponds to the same detection results of different code detection algorithms at the same location. Based on this, the preset probability threshold can be less than the above-mentioned target probability threshold, such as 80%.
[0073] In some embodiments, through the above steps, the first error prediction result of this code location can be obtained by analyzing the detection results of different code detection algorithms at the same code location, thereby reducing the false alarms and missed reports of code errors, and improving the error coverage rate and error accuracy.
[0074] In some embodiments, as a refinement of step 103, for example Figure 4 as shown, it may further include: Step 401: Generate an abstract syntax tree, a control flow graph, and a data flow graph based on the preprocessed code.
[0075] In some embodiments, an abstract syntax tree can be obtained based on the preprocessed code through a first generation tool, a control flow graph can be obtained through a second generation tool, and a data flow graph can be obtained through a third generation tool. By way of example, the first generation tool is , the second generation tool is , and the third generation tool is .
[0076] Step 402: Based on the target error type in the target error prediction result and the abstract syntax tree, determine the target node corresponding to the target error type through the node matching rule.
[0077] In some embodiments, the above-mentioned node matching rule is the matching rule between the error type and the AST node.
[0078] In some embodiments, based on the target error type in the target error prediction result and the abstract syntax tree, the target error type is matched with the nodes of the abstract syntax tree through the node matching rule to obtain the target node corresponding to the target error type.
[0079] Step 403: Perform control flow context analysis on the target node based on the control flow graph to obtain the first context information.
[0080] In some embodiments, after obtaining the target node through the above steps, the method for performing control flow context analysis on the target node based on the control flow graph to obtain the first context information may include the following steps: Step 4031: Map the target node to the target basic block in the control flow graph.
[0081] Step 4032: Traverse the control flow graph in reverse to obtain the preconditions of all reachable paths reaching the target basic block.
[0082] Step 4033: Traverse the control flow graph forward to identify the postconditions affected by the target basic block.
[0083] Step 4034: Determine the preconditions and postconditions as the first context information.
[0084] Step 404: Perform context analysis on the variables corresponding to the target node based on the data flow graph to obtain the second context information.
[0085] In some embodiments, the method for performing context analysis on the variables corresponding to the target node based on the data flow graph to obtain the second context information may include the following steps: Identify the variable definitions on which the target node depends based on the data flow graph, and determine the variable definitions as the second context information.
[0086] Step 405: Obtain the target context information based on the first context information and the second context information.
[0087] In some embodiments, the first context information and the second context information may be determined as the target context information.
[0088] In some embodiments, through the above steps, an abstract syntax tree, a control flow graph, and a data flow graph can be generated based on the preprocessed code, and based on the abstract syntax tree, the control flow graph, and the data flow graph, the corresponding target context information can be obtained, so that the target context information of the target node can be obtained, making the subsequent correction suggestions generated based on the target context information more accurate and facilitating the user to make corrections.
[0089] In some embodiments, as a refinement of the code error detection method, as Figure 5 shown, the above method may further include: Step 501: Obtain the feedback information of the user on the target correction suggestion.
[0090] In some embodiments, the user can select a target correction suggestion for correction according to the target code error detection result and provide feedback on the target correction suggestion.
[0091] In some embodiments, the above-mentioned feedback information obtained for the user corresponding to the target correction suggestion may include an evaluation of the target correction suggestion, error cases, and correction methods.
[0092] Step 502, update the correction rule library based on the feedback information.
[0093] In some embodiments, after obtaining the feedback information of the user for the target correction suggestion through the above steps, the correction rule library can be updated based on the feedback information.
[0094] In some embodiments, the correction methods and new error cases fed back by the user can be sorted into correction rules and added to the correction rule library to complete the update of the correction rule library, so as to continuously optimize the correction rule library and make the correction rule library more accurate.
[0095] In some embodiments, the target code detection model can also be trained using the correction methods and new error cases fed back by the user to complete the update of the target code detection model, so that the target code detection model can continuously learn and make the detection results obtained through the target code detection model more accurate.
[0096] To implement the code error detection method provided by the embodiments of the present disclosure, the embodiments of the present disclosure also provide a code error detection device. As Figure 6 shown, it includes: A preprocessing module 611, configured to obtain the code to be detected and preprocess the code to be detected to obtain preprocessed code; A code detection module 612, configured to perform code detection on the preprocessed code through at least one code detection algorithm and fuse the obtained detection results to obtain a target error prediction result; A positioning module 613, configured to locate the preprocessed code based on the target error prediction result to obtain corresponding target context information; A first generation module 614, configured to generate at least one target correction suggestion based on the target error prediction result and the target context information; A second generation module 615, configured to generate a target code error detection result based on the target error prediction result and at least one target correction suggestion.
[0097] The code error detection device provided by the embodiments of the present disclosure obtains the code to be detected, and preprocesses the code to be detected to obtain preprocessed code; performs code detection on the preprocessed code through at least one code detection algorithm, and fuses the obtained detection results to obtain a target error prediction result; locates the preprocessed code based on the target error prediction result to obtain corresponding target context information; generates at least one target correction suggestion based on the target error prediction result and the target context information; generates a target code error detection result based on the target error prediction result and at least one target correction suggestion. Determining the target error prediction result through at least one code detection algorithm, and generating at least one target correction suggestion based on the target prediction result and the target context information enables the user to locate and correct the error code according to at least one target correction suggestion, without manual location and repair, thereby improving the error coverage rate, improving the development efficiency, and ensuring the project progress.
[0098] Further, in a possible implementation manner of the embodiments of the present disclosure, the preprocessing module 611 is specifically configured to: Remove comments from the code to be detected through regular expressions to obtain the main code of the code to be detected; Format the main code to obtain preprocessed code.
[0099] Further, in a possible implementation manner of the embodiments of the present disclosure, as Figure 7 shown, the code detection module 612 includes: The first code detection sub-module 6121 is used to perform code detection on the preprocessed code through a static code detection algorithm to obtain a first code detection result; The second code detection sub-module 6122 is used to perform code detection on the preprocessed code through a target code detection model to obtain a second code detection result; The fusion sub-module 6123 is used to fuse the first code detection result and the second code detection result to obtain a target error prediction result.
[0100] Further, in a possible implementation manner of the embodiments of the present disclosure, the fusion sub-module 6123 is specifically configured to: Match based on the first error list and the second error list to obtain first error information pointing to the same code position; Obtain a first error prediction result based on the first error information; Analyze the remaining second code positions in the second error list to obtain a second error prediction result; Fuse the first error prediction result and the second error prediction result to obtain a target error prediction result.
[0101] Further, in a possible implementation manner of the embodiments of the present disclosure, the fusion sub-module 6123 is further configured to: Determine whether the first error type is the same as the second error type; If it is determined that the first error type is the same as the second error type, determine whether the error probability exceeds a preset probability threshold; If it is determined that the error probability exceeds the preset probability threshold, obtain a first error prediction result based on the first error type, the second error type, the error probability, and the code position.
[0102] Further, in a possible implementation manner of the embodiments of the present disclosure, the positioning module 613 is specifically configured to: Generate an abstract syntax tree, a control flow graph, and a data flow graph based on the preprocessed code; Based on the target error type in the target error prediction result and the abstract syntax tree, determine the target node corresponding to the target error type through the node matching rule; Perform control flow context analysis on the target node based on the control flow graph to obtain first context information; Perform context analysis on the variable corresponding to the target node based on the data flow graph to obtain second context information; Obtain target context information based on the first context information and the second context information.
[0103] Further, in a possible implementation manner of the embodiments of the present disclosure, the first generation module 614 is specifically configured to: Determine the target correction rule in the correction rule library that matches the target error type in the target error prediction result; Adjust the target correction rule based on the target context information to generate at least one candidate correction suggestion; Evaluate the candidate correction suggestions to obtain the scores of the candidate correction suggestions; Generate at least one target correction suggestion based on the scores of the candidate correction suggestions.
[0104] Further, in a possible implementation manner of the embodiments of the present disclosure, as Figure 8 shown, the above code error detection device further includes an update module 616, and the update module 616 is specifically configured to: Obtain the feedback information of the user on the target correction suggestion; Update the correction rule library based on the feedback information.
[0105] An embodiment of the present disclosure further provides an electronic device, including a memory and a processor. A computer program is stored in the memory, and the processor is configured to run the computer program to execute the steps in any of the above embodiments of the code error detection method.
[0106] Embodiments of the present disclosure also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to execute the steps in any of the above-described embodiments of the code error detection method when running.
[0107] In an exemplary embodiment, the above computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs such as USB flash drives, read-only memories (ROM), random access memories (RAM), mobile hard disks, magnetic disks, or optical discs.
[0108] Embodiments of the present disclosure also provide a computer program product, where the computer program product includes a computer program, and the steps in any of the above-described embodiments of the code error detection method are implemented when the computer program is executed by a processor.
[0109] Embodiments of the present disclosure also provide another computer program product, including a non-volatile computer-readable storage medium storing a computer program, and the steps in any of the above-described embodiments of the code error detection method are implemented when the computer program is executed by a processor.
[0110] Those skilled in the art can further realize that the units and algorithm steps of each example described in combination with the embodiments disclosed herein can be implemented by electronic hardware, computer software, or a combination of the two. To clearly illustrate the interchangeability of hardware and software, the components and steps of each example have been generally described according to their functions in the above description. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Skilled professionals can use different methods for each specific application to implement the described functions, but such implementation should not be considered to exceed the scope of the present disclosure.
[0111] The above has introduced in detail a code error detection method provided by the present disclosure. Specific examples are used herein to elaborate on the principle and implementation manner of the present disclosure. The description of the above embodiments is only used to help understand the method and its core idea of the present disclosure. It should be noted that for those of ordinary skill in the art in the technical field, without departing from the principle of the present disclosure, several improvements and modifications can be made to the present disclosure, and these improvements and modifications also fall within the protection scope of the claims of the present disclosure.
Claims
1. A method for detecting code errors, characterized in that, The method includes: Obtain the code to be detected, and perform preprocessing on the code to be detected to obtain preprocessed code; Perform code detection on the preprocessed code through at least one code detection algorithm, and fuse the obtained detection results to obtain a target error prediction result; Locate the preprocessed code based on the target error prediction result to obtain corresponding target context information; Generate at least one target correction suggestion based on the target error prediction result and the target context information; Generate a target code error detection result based on the target error prediction result and the at least one target correction suggestion.
2. The method according to claim 1, wherein The performing preprocessing on the code to be detected to obtain preprocessed code includes: Perform comment removal processing on the code to be detected through regular expressions to obtain the main code of the code to be detected; Perform formatting processing on the main code to obtain preprocessed code.
3. The method according to claim 1, characterized in that, The performing code detection on the preprocessed code through at least one code detection algorithm and fusing the obtained detection results to obtain a target error prediction result includes: Perform code detection on the preprocessed code through a static code detection algorithm to obtain a first code detection result; Perform code detection on the preprocessed code through a target code detection model to obtain a second code detection result; Fuse the first code detection result and the second code detection result to obtain a target error prediction result.
4. The method according to claim 3, characterized in that, The first code detection result includes a first error list, and the first error list includes a first error type and a corresponding first code location; the second code detection result includes a second error list, and the second error list includes a second error type, an error probability, and a corresponding second code location; The fusing the first code detection result and the second code detection result to obtain a target error prediction result includes: Match based on the first error list and the second error list to obtain first error information pointing to the same code location; Obtain a first error prediction result based on the first error information; Analyze the remaining second code locations in the second error list to obtain a second error prediction result; Fuse the first error prediction result and the second error prediction result to obtain a target error prediction result.
5. The method according to claim 4, wherein The first error information includes a first error type, a second error type, an error probability, and a code location; the obtaining a first error prediction result based on the first error information includes: Determine whether the first error type and the second error type are the same; If it is determined that the first error type and the second error type are the same, then determine whether the error probability exceeds a preset probability threshold; If it is determined that the error probability exceeds the preset probability threshold, then obtain a first error prediction result based on the first error type, the second error type, the error probability, and the code location.
6. The method according to claim 1, characterized in that The locating the preprocessed code based on the target error prediction result to obtain corresponding target context information includes: Generate an abstract syntax tree, a control flow graph, and a data flow graph based on the preprocessed code; Based on the target error type in the target error prediction result and the abstract syntax tree, determine the target node corresponding to the target error type through the node matching rule; Based on the control flow graph, perform control flow context analysis on the target node to obtain the first context information; Based on the data flow graph, perform context analysis on the variable corresponding to the target node to obtain the second context information; Based on the first context information and the second context information, obtain the target context information.
7. The method according to claim 1, wherein Based on the target error prediction result and the target context information, generate at least one target correction suggestion, including: Determine the target correction rule in the correction rule library that matches the target error type in the target error prediction result; Based on the target context information, adjust the target correction rule to generate at least one candidate correction suggestion; Evaluate the candidate correction suggestions to obtain the scores of the candidate correction suggestions; Based on the scores of the candidate correction suggestions, generate at least one target correction suggestion.
8. The method according to claim 1, characterized in that The method further includes: Obtain the feedback information of the user on the target correction suggestion; Update the correction rule library based on the feedback information.
9. An apparatus for detecting code errors, characterized in that, The device includes: A preprocessing module, configured to obtain the code to be detected and preprocess the code to be detected to obtain preprocessed code; A code detection module, configured to perform code detection on the preprocessed code through at least one code detection algorithm and fuse the obtained detection results to obtain a target error prediction result; A positioning module, configured to locate the preprocessed code based on the target error prediction result to obtain the corresponding target context information; A first generation module, configured to generate at least one target correction suggestion based on the target error prediction result and the target context information; A second generation module, configured to generate a target code error detection result based on the target error prediction result and the at least one target correction suggestion.
10. A computer-readable storage medium, characterized in that, A computer program is stored in the computer-readable storage medium, wherein when the computer program is executed by a processor, the steps of the code error detection method according to any one of claims 1 to 8 are implemented.
Citation Information
Patent Citations
Quantization strategy detection method and device, equipment and storage medium
CN116775035A
Source code vulnerability detection method, system and equipment and storage medium
CN118536122A
Visual front-end code quality detection method and device and computer storage medium
CN118916296A
Code testing method and device and electronic equipment
CN119179642A
Code repairing method and device
CN120085900A