Code analysis method, device, equipment, and storage medium based on artificial intelligence
Through the artificial intelligence-based code analysis method, combined with static analysis, dynamic analysis and semantic recognition, the problem that the code cannot effectively verify whether it meets the project needs, improves development efficiency and provides detailed repair tips.
Patent Information
- Application Number
- CN202411656082.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-19
- Publication Date
- 2025-04-08
- Estimated Expiration
- 2044-11-19
AI Technical Summary
The existing technology cannot effectively verify whether the code meets project requirements, resulting in the impact of development efficiency.
Using artificial intelligence-based code analysis methods, we generate code analysis results by obtaining requirements files and target code sets, combining static analysis, dynamic analysis and semantic recognition to ensure that the code meets project requirements.
Improve development efficiency and help developers discover and repair problems in the code in a timely manner through detailed code analysis and repair tips.
Smart Images

Figure CN119322733B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to an artificial intelligence-based code analysis method, device, equipment, and storage medium. Background Art
[0002] The code written by developers is likely to contain static errors and dynamic errors. Static errors are errors caused by human factors when writing code, such as code syntax errors and call errors, and dynamic errors are errors that occur when the code is running, such as logic errors, performance bottlenecks, and synchronization errors. Therefore, after completing code writing, developers usually use relevant tools to analyze the code, such as the common SonarQube or FindBugs, which can perform static analysis on the source code to find code errors. The Natural Language Processing (NLP) model can analyze the running logic of the code, realize the detection of dynamic errors, and improve development efficiency.
[0003] Although the combination of static analysis tools and NLP models can discover errors in the code itself, code development may also result in deviations from requirements implementation, that is, there is a deviation between the functions implemented by the code and the designed functions, or the developer's naming of variables does not comply with project specifications, etc. These deviations will not cause code abnormalities. Existing methods cannot verify whether the normally running code meets project requirements, affecting development efficiency. Summary of the invention
[0004] The present invention aims to solve at least one of the technical problems existing in the prior art. To this end, the present invention proposes a code analysis method, device, equipment, and storage medium based on artificial intelligence, which can analyze whether the code meets the project requirements and improve development efficiency.
[0005] In a first aspect, an embodiment of the present invention provides a code analysis method based on artificial intelligence, comprising:
[0006] Obtaining a requirement file and a target code set to be analyzed, wherein the target code set includes a plurality of code modules, the requirement file records a naming rule, requirement input information and requirement output information of each of the code modules, and the naming rule is used to indicate the structure of an object name;
[0007] Scanning the target code set based on a preset static analysis tool to determine a static analysis result, and determining a second object from the scanned first object, wherein the static analysis result corresponding to the second object indicates that the code is correct;
[0008] Executing the target code set through a preset dynamic analysis tool to obtain a dynamic analysis result, obtaining a debug log output by the dynamic analysis tool, and recording the operation output information of each of the code modules, wherein the debug log records the calling relationship of each of the first objects;
[0009] Perform semantic recognition based on the operation output information and the demand output information to determine the demand deviation analysis results of each of the code modules;
[0010] Inputting the debug log, the naming rule and each second object into a preset prediction model, and determining a naming prediction result of each second object based on the prediction model, wherein the naming prediction result is used to indicate whether the second object is correctly named or incorrectly named;
[0011] A code analysis result of the target code set is generated based on the static analysis result, the dynamic analysis result, the requirement deviation analysis result and the naming prediction result.
[0012] According to some embodiments of the present invention, before inputting the debug log, the naming rule and each of the second objects into a preset prediction model, the method further includes:
[0013] Acquire the target content, object type and object scope of each of the second objects from the target code set;
[0014] Based on each of the second objects, after deleting stop words from the target content, the target content is segmented into a plurality of target word segments, wherein each of the target word segments is a word or a mark character;
[0015] The uppercase characters in the target word are converted to lowercase. When the target word is a verb, the target word is restored to the original form of the verb.
[0016] According to some embodiments of the present invention, determining the naming prediction result of each second object based on the prediction model includes:
[0017] Selecting a target object from the second objects, determining a target call relationship of the target object based on the debug log, and obtaining a target name of the target object;
[0018] Predicting the calling context information of the target object based on the target calling relationship, and determining the target structure from the naming rule based on the object type of the target object;
[0019] Predicting a predicted name based on the target structure, the call context information, the object scope, and the target word segmentation, and determining a name similarity based on the predicted name and the target name;
[0020] A naming prediction result is determined based on the name similarity and a preset similarity threshold.
[0021] According to some embodiments of the present invention, determining a static analysis result based on scanning the target code set using a preset static analysis tool, and determining a second object from the scanned first object includes:
[0022] Scanning the target code set to determine each of the first objects;
[0023] Performing a syntax check on the first object to determine a syntax analysis result, wherein the syntax analysis result is used to indicate whether there is a syntax error in the first object;
[0024] When the first object records at least one object pointer, a null pointer analysis result is determined by detecting a pointer calling object of the object pointer, wherein the null pointer analysis result is used to indicate whether the pointer calling object is null;
[0025] Determine a resource leakage analysis result based on a resource file called by the first object or an established network connection, wherein the resource leakage analysis result is used to indicate whether the resource file or the network connection is closed correctly;
[0026] When the first object includes a conditional statement or a loop statement, determining a logic analysis result of the first object, wherein the logic analysis result is used to indicate whether there is a logic error in the conditional statement or the loop statement;
[0027] Determine the syntax analysis result, the null pointer analysis result, the resource leak analysis result and the logic analysis result as the static analysis result;
[0028] The first object represented correctly by both the grammatical analysis result and the logical analysis result is determined as the second object.
[0029] According to some embodiments of the present invention, executing the target code set by a preset dynamic analysis tool to obtain a dynamic analysis result includes:
[0030] Inputting the target code set and the requirement file into the dynamic analysis tool, and the dynamic analysis tool executes the corresponding code module based on the requirement input information;
[0031] When the code module includes at least one module function, a predicted execution time of the module function is predicted based on a code corresponding to the module function, the running execution time of the module function is recorded during the running process, and a performance analysis result of the code module is determined based on the predicted execution time and the running execution time;
[0032] Determine an exception analysis result of the code module, wherein the exception analysis result is used to indicate whether an operation exception occurs, and the operation exception includes a type conversion error, an array out of bounds, or a deadlock;
[0033] When the exception analysis result is used to indicate the occurrence of an exception, an exception handling result is generated based on the response of the code module to the operation exception, wherein the exception handling result is used to indicate whether the code module correctly responds to the operation exception;
[0034] The dynamic analysis result is generated based on the performance analysis result, the exception analysis structure and the exception handling result.
[0035] According to some embodiments of the present invention, after generating the code analysis result of the target code set based on the static analysis result, the dynamic analysis result, the requirement deviation analysis result and the naming prediction result, the method further includes:
[0036] Build a visual interface;
[0037] Determine the first object that represents an error in at least one of the syntax analysis result, the null pointer analysis result, the resource leak analysis result, and the logic analysis result as a static exception object, and display the static exception object and the corresponding static analysis result on the visualization interface;
[0038] Determine the code module that represents an abnormality in at least one of the performance analysis result, the abnormal analysis structure, and the abnormal handling result as a dynamic abnormal module, and display the dynamic abnormal module and the corresponding dynamic analysis result on the visual interface;
[0039] Determine the second object whose naming prediction result indicates a naming error as a naming abnormality object, and display the naming prediction result of the naming abnormality object on the visual interface;
[0040] The code module whose demand deviation analysis result represents the demand deviation is determined as a deviation module, and the demand deviation analysis result of the deviation module is displayed on the visual interface.
[0041] According to some embodiments of the present invention, after the visualization interface displays the dynamic anomaly module and the corresponding dynamic analysis result, the method further includes:
[0042] Determine the syntax analysis result, the null pointer analysis result, the resource leak analysis result or the logic analysis result corresponding to the static exception object as a static exception root cause, generate static repair prompt information based on the static exception root cause, and display the static repair prompt information on the visualization interface;
[0043] Acquire debug information of the dynamic exception module from the debug log, determine a dynamic exception root cause of the dynamic exception module based on the debug information, generate dynamic repair prompt information based on the dynamic exception root cause, and display the dynamic repair prompt information on the visualization interface;
[0044] Displaying the predicted name of the named abnormal object on the visualization interface;
[0045] Deviation information is determined based on the information difference between the demand output information and the operation output information of the deviation module, and a code corresponding to the deviation information in the deviation module is displayed on an information visualization interface.
[0046] In a second aspect, an embodiment of the present invention provides an artificial intelligence-based code analysis device, comprising at least one control processor and a memory for communicating with the at least one control processor; the memory stores instructions executable by the at least one control processor, and the instructions are executed by the at least one control processor so that the at least one control processor can execute the artificial intelligence-based code analysis method as described in the first aspect above.
[0047] In a third aspect, an embodiment of the present invention provides an electronic device, comprising an artificial intelligence-based code analysis device as described in the second aspect above.
[0048] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium storing computer-executable instructions, wherein the computer-executable instructions are used to execute the artificial intelligence-based code analysis method as described in the first aspect above.
[0049] The artificial intelligence-based code analysis method according to an embodiment of the present invention has at least the following beneficial effects: obtaining a requirement file and a target code set to be analyzed, wherein the target code set includes multiple code modules, and the requirement file records a naming rule, requirement input information and requirement output information of each of the code modules, and the naming rule is used to indicate the structure of the object name; based on a preset static analysis tool, scanning the target code set to determine a static analysis result, determining a second object from the scanned first object, wherein the static analysis result corresponding to the second object indicates that the code is correct; executing the target code set through a preset dynamic analysis tool to obtain a dynamic analysis result, and obtaining a debug log output by the dynamic analysis tool. log, record the running output information of each code module, wherein the debug log records the calling relationship of each first object; perform semantic recognition based on the running output information and the demand output information, and determine the demand deviation analysis results of each code module; input the debug log, the naming rule and each second object into a preset prediction model, and determine the naming prediction results of each second object based on the prediction model, wherein the naming prediction results are used to indicate whether the second object is named correctly or incorrectly; generate the code analysis results of the target code set based on the static analysis results, the dynamic analysis results, the demand deviation analysis results and the naming prediction results. According to the technical solution of the embodiment of the present invention, on the basis of obtaining the static analysis results and the dynamic analysis results, the static analysis results can be further used to filter out the second object, obtain the debug log and running output information generated by the dynamic analysis tool during the dynamic analysis process, combine with the preset demand file, determine whether the second object is named correctly through the prediction model, and determine the demand deviation of the code module through semantic analysis, so as to ensure that the target code set meets the development requirements and improve the development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] Figure 1 is a flow chart of a code analysis method based on artificial intelligence provided by an embodiment of the present invention;
[0051] Figure 2 is a schematic diagram of a code analysis process provided by another embodiment of the present invention;
[0052] Figure 3 is a complete flow chart of a code analysis method based on artificial intelligence provided by another embodiment of the present invention;
[0053] Figure 4 It is a structural diagram of an artificial intelligence-based code analysis device provided by another embodiment of the present invention. DETAILED DESCRIPTION
[0054] Embodiments of the present invention are described in detail below, examples of which are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and cannot be understood as limiting the present invention.
[0055] In the description of the present invention, it should be understood that descriptions involving orientations, such as up, down, front, back, left, right, etc., and orientations or positional relationships indicated are based on the orientations or positional relationships shown in the accompanying drawings, and are only for the convenience of describing the present invention and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore should not be understood as a limitation on the present invention.
[0056] In the description of the present invention, "several" means one or more, "more" means more than two, "greater than", "less than", "exceed" etc. are understood as not including the number itself, and "above", "below", "within" etc. are understood as including the number itself. If there is a description of "first" or "second", it is only used for the purpose of distinguishing the technical features, and cannot be understood as indicating or implying the relative importance or implicitly indicating the number of the indicated technical features or implicitly indicating the order of the indicated technical features.
[0057] In the description of the present invention, unless otherwise clearly defined, terms such as setting, installing, connecting, etc. should be understood in a broad sense, and technicians in the relevant technical field can reasonably determine the specific meanings of the above terms in the present invention based on the specific content of the technical solution.
[0058] The embodiment of the present invention provides a code analysis method, apparatus, device, and storage medium based on artificial intelligence, wherein the code analysis method based on artificial intelligence includes: obtaining a requirement file and a target code set to be analyzed, wherein the target code set includes multiple code modules, and the requirement file records a naming rule, requirement input information, and requirement output information of each of the code modules, and the naming rule is used to indicate the structure of the object name; based on a preset static analysis tool, scanning the target code set to determine a static analysis result, and determining a second object from the scanned first object, wherein the static analysis result corresponding to the second object indicates that the code is correct; executing the target code set through a preset dynamic analysis tool to obtain a dynamic analysis result, and obtaining the dynamic analysis result. The debug log output by the tool records the operation output information of each of the code modules, wherein the debug log records the call relationship of each of the first objects; semantic recognition is performed based on the operation output information and the demand output information to determine the demand deviation analysis results of each of the code modules; the debug log, the naming rule and each of the second objects are input into a preset prediction model, and the naming prediction results of each of the second objects are determined based on the prediction model, wherein the naming prediction results are used to indicate whether the second object is named correctly or incorrectly; the code analysis results of the target code set are generated based on the static analysis results, the dynamic analysis results, the demand deviation analysis results and the naming prediction results. According to the technical solution of the embodiment of the present invention, on the basis of obtaining the static analysis results and the dynamic analysis results, the static analysis results can be further used to filter out the second object, obtain the debug log and operation output information generated by the dynamic analysis tool during the dynamic analysis process, combine with the preset demand file, determine whether the second object is named correctly through the prediction model, determine the demand deviation of the code module through semantic analysis, ensure that the target code set meets the development requirements, and improve development efficiency.
[0059] The technical solution of the embodiment of the present invention is further described below based on the accompanying drawings.
[0060] Reference Figure 1 , Figure 1 A flowchart of a code analysis method based on artificial intelligence provided by an embodiment of the present invention, the code analysis method based on artificial intelligence includes but is not limited to the following steps:
[0061] S11, obtaining a requirement file and a target code set to be analyzed, wherein the target code set includes multiple code modules, and the requirement file records a naming rule, requirement input information and requirement output information of each code module, and the naming rule is used to indicate the structure of an object name;
[0062] S12, scanning the target code set based on a preset static analysis tool to determine a static analysis result, and determining a second object from the scanned first object, wherein the static analysis result corresponding to the second object indicates that the code is correct;
[0063] S13, executing the target code set through a preset dynamic analysis tool to obtain a dynamic analysis result, obtaining a debug log output by the dynamic analysis tool, and recording the operation output information of each code module, wherein the debug log records the calling relationship of each first object;
[0064] S14, performing semantic recognition based on the operation output information and the demand output information to determine the demand deviation analysis results of each code module;
[0065] S15, inputting the debug log, the naming rule and each second object into a preset prediction model, and determining a naming prediction result of each second object based on the prediction model, wherein the naming prediction result is used to indicate whether the second object is correctly named or incorrectly named;
[0066] S16, generating code analysis results of the target code set based on the static analysis results, the dynamic analysis results, the requirement deviation analysis results and the naming prediction results.
[0067] It should be noted that the target code set can be a code collection of a code development project. The target code set includes multiple code modules. Each code module can be a code file or a code function. There is no limitation on the granularity of the division of code modules. It is sufficient that the code modules can be run independently to implement one or more functions.
[0068] It should be noted that the requirement file is a development requirement document for the target code set. Before coding, the functional description text of each code module is set according to the actual development requirements. The requirement file is formed based on the collection of functional description texts. For example, the requirement input information and output information that each code module needs to implement are recorded in a mapping table. For example, in the property field, the requirement input information of the code module used for property fee statistics is the area of each property unit, and the requirement output information is the property management fee of each property unit and the aggregated property management fee.
[0069] It should be noted that the objects of this embodiment include functions, variables, methods and other units in the code that need to be named. Since the target code set corresponds to a code development project, the naming rules can be formulated based on the actual needs of each target code set. This embodiment does not limit all target code sets to share the same naming rules. The naming rules of this embodiment are used to indicate the naming structure of the object, the composition method and components of the set object name, for example, the function name needs to include context information and function description in sequence, and the two are separated by an underscore; for another example, the variable name of each calling method needs to use the same prefix, and the prefix is the abbreviation of the calling method. The specific rules can be set according to actual needs, and can be recorded in the demand file in the form of text descriptions, and semantic prediction can be performed later.
[0070] It should be noted that if Figure 2 As shown, when the static analysis tool scans the target code set, it scans the target code set to determine multiple first objects. The first object can be the functions, variables, methods, etc. mentioned above. The static analysis result of each first object is determined during the scanning process. For example, the static analysis result is obtained by detecting syntax errors, null pointer analysis, and other analysis and processing of the first object. After obtaining the static analysis result, this embodiment further determines the second object from the first object based on the static analysis result. The second object is the first object without static errors. If a static error exists, the first object will be prompted by the static analysis result. Human errors occur in the process of writing the code for the first object. If naming prediction is performed through a prediction model, prediction errors may also occur due to erroneous information. Therefore, there is no need to perform naming detection on the first object with static errors. This embodiment performs naming detection on the second object to improve detection efficiency and reliability.
[0071] It should be noted that if Figure 2 As shown, after determining the second object, this embodiment does not directly perform name prediction on the second object, but needs to further input the target code set into the dynamic analysis tool. When the dynamic analysis tool performs dynamic analysis, a debug log is generated. Since the debug log records the calling relationship of each first object, it can be used as a representation of context information. According to the description of the above embodiment, the position of the context information in the name can be set in the naming rule. Therefore, this embodiment needs to combine the information output by the static analysis tool and the dynamic analysis tool to perform naming prediction to ensure the accuracy of the naming prediction.
[0072] It should be noted that the dynamic analysis tool executes the target code set in the test environment. In the related art, the input of the dynamic analysis tool can be random data. Figure 2As shown, this embodiment uses the demand input information in the demand file as the running input of each code module, and obtains the running output information of each code module through the debug log to determine whether the code module can meet the design requirements during operation. The demand input information and demand output information are used to characterize the input and output requirements, but are not necessarily accurate data. The code module can obtain the actual data of the property area of each owner from the database as input, extract the variable name corresponding to the running output information from the debug log, and perform semantic recognition between the variable name and the demand output information to determine whether the output data variable meets the output requirements, thereby realizing demand deviation analysis. For example, in the above example, the demand input information is the owner's property area, the demand output information is the property management fee of each property unit and the summarized property management fee, and the operation output information can be the calculated management fee of each business property and the final summary fee. The variable names of the corresponding data queried in the debug log are "A unit property fee", "B unit property fee" and "property fee sum". Through semantic recognition, it can be determined that the operation output information is consistent with the demand output information and meets the design requirements; if the "property fee sum" is missing in the operation output information, it will not cause the code module to run incorrectly, but compared with the demand output information, one type of output is missing and the demand is not met. Therefore, the demand deviation of the code module that is running normally can be accurately identified.
[0073] It should be noted that the debug log records the calling relationship of each first object, and the second object is selected from the first object, so the calling relationship of the second object can be obtained, and the context information of the second object can be determined according to the calling relationship, such as the method to which the second object belongs, the data required to call when executing the function call, etc., and the calling relationship between objects recorded in the debug log can be used to provide a prediction basis for the components other than the object name itself; the naming rule is a specific naming structure, such as the naming structure of the function, the naming structure of the variable, etc., which can provide a prediction basis for whether the composition of the object name meets the design specification; after the second object is input into the prediction model, the prediction model obtains the content of the second object, such as the definition statement of the variable, the functional statement of the function, etc., and provides a prediction basis for the text selection logic of the object name. The prediction model can use common support vector machines, random forests or neural networks, etc., index the calling relationship from the debug log through the target name, match the naming structure from the naming rule, extract the content features of the second object or predict the name of the second object after vectorization, and the predicted name and the actual name do not need to be exactly the same, but only need the similarity to meet the preset threshold. The similarity comparison between the two can determine whether the second object is named correctly.
[0074] It should be noted that the static analysis results, dynamic analysis results, demand deviation results and naming prediction results of this embodiment can record the object or code module where the error occurs, and record the specific error type. Those skilled in the art are familiar with how to give error prompts, so they will not be described in detail here. After obtaining the above analysis results, they can be summarized into code analysis results.
[0075] In addition, in one embodiment, referring to Figure 3 Before executing step S15, the following steps are also included but not limited to:
[0076] S21, acquiring target content, object type and object scope of each second object from the target code set;
[0077] S22, based on each second object, after deleting stop words from the target content, segmenting the target content into a plurality of target word segments, wherein each target word segment is a word or a mark character;
[0078] S23, convert the uppercase characters in the target word into lowercase characters, and when the target word is a verb, restore the target word to the original form of the verb.
[0079] It should be noted that after determining the second object, the relevant information of the second object needs to be preprocessed before prediction to ensure that the prediction result can be more accurate. The target content of the second object is the code statement belonging to the second object, and the object type is used to characterize the type information of the second object, for example, the second object is a variable, function or method. The object scope is used to characterize the second object as a global object or a local object.
[0080] It should be noted that during the preprocessing of this embodiment, the target content is the actual extracted characters. This embodiment is used to determine whether the naming is correct, and the judgment basis is the semantic similarity between the predicted name and the actual name. Therefore, stop words that are irrelevant to the similarity judgment are first deleted from the target content. Stop words are words that need to be filtered in the field of natural language processing, such as "the", "is", etc., which do not affect the semantics. Therefore, when performing name prediction, this embodiment does not predict the similarity of all characters, but considers the similarity of characters other than stop words. The target name only needs to meet the specifications on the naming structure, and stop words that do not affect the semantics are not considered.
[0081] It should be noted that after deleting the stop words, this embodiment divides the target content into multiple target word segments. Word segmentation is a common operation in the field of natural language processing. The specific word segmentation principle is a technology well known to those skilled in the art and will not be elaborated here.
[0082] It should be noted that, after word segmentation, this embodiment converts the uppercase of the target word into lowercase to avoid uppercase and lowercase characters being recognized as different characters, thereby improving the accuracy of semantic recognition. At the same time, when the target word is a verb, it is also necessary to restore the target word to the original form of the verb. For example, "calculatedAverage" is split into two target words "calculated" and "Average", "calculated" is restored to "calculate", and "Average" is converted to "average".
[0083] In addition, in one embodiment, referring to Figure 3 Step S15 specifically includes but is not limited to the following steps:
[0084] S151, select a target object from the second object, determine the target call relationship of the target object based on the debug log, and obtain the target name of the target object;
[0085] S152, predicting the calling context information of the target object based on the target calling relationship, and determining the target structure from the naming rule based on the object type of the target object;
[0086] S153, predicting a predicted name based on the target structure, the call context information, the object scope and the target word segmentation, and determining a name similarity based on the predicted name and the target name;
[0087] S154, determining a naming prediction result based on the name similarity and a preset similarity threshold.
[0088] It should be noted that this embodiment executes the second objects one by one when performing naming prediction. The second object currently being executed is the target object. The target name of the target object is used as an index to determine the target call relationship from the debug log. The target call relationship includes other first objects that call the target object, or other first objects called by the target object. For example, if the target object is a variable, the value of the variable is calculated by calling function 1, and the variable is called by function 2, and the calling relationship between function 1 and function 2 and the variable is determined as the target call relationship; for another example, if the target object is a function, which is used to calculate the values of variables 1, 2, and 3, then variables 1, 2, and 3 are the target call relationship of the target object.
[0089] It should be noted that when naming a variable or function, it is usually necessary to reflect context information to a certain extent. For example, in the above example where the target object is a function, the name can be named "Computing Variable 1 Variable 2 Variable 3". The name carries the context information indicated by the target call relationship. Therefore, this embodiment predicts the call context information based on the target call relationship. The target structure is used to indicate the structure of the target name. After determining the call context information, this embodiment further determines the object scope. There are different specifications for naming global variables and local variables. Therefore, it is necessary to determine the object scope of the target object when making naming predictions.
[0090] It should be noted that when naming an object, the object content is usually used as the basis. For example, the target object is a calculation function used to perform summary calculations. Its object content must record the operations of summarizing the acquired data. These operations will be reflected in the form of target participles. The calling context information can be the value of the variable to be summarized. The scope is global, and the target structure is "context + content + scope". Therefore, the correct optional name is "getUnit + CalculateTotal + Global".
[0091] It should be noted that the prediction model of this embodiment takes calling context information, object scope and target segmentation as input, determines the predicted name through feature extraction and prediction, and the specific prediction algorithm is a technology well known to those skilled in the art, and can implement name prediction based on the input conditions of this embodiment.
[0092] It should be noted that after predicting the predicted name, the similarity between the target name and the predicted name is determined. When the similarity between the two is greater than the similarity threshold, it can be determined that the naming method of the target object complies with the naming rules, and the naming prediction result is determined to be correct.
[0093] In addition, in one embodiment, referring to Figure 3 Step S12 specifically includes but is not limited to the following steps:
[0094] S121, scanning the target code set to determine each first object;
[0095] S122, performing a syntax check on the first object to determine a syntax analysis result, wherein the syntax analysis result is used to indicate whether the first object has a syntax error;
[0096] S123, when the first object records at least one object pointer, determining a null pointer analysis result by detecting a pointer calling object of the object pointer, wherein the null pointer analysis result is used to indicate whether the pointer calling object is null;
[0097] S124, determining a resource leakage analysis result based on the resource file called by the first object or the established network connection, wherein the resource leakage analysis result is used to indicate whether the resource file or the network connection is closed correctly;
[0098] S125, when the first object includes a conditional statement or a loop statement, determining a logic analysis result of the first object, wherein the logic analysis result is used to indicate whether there is a logic error in the conditional statement or the loop statement;
[0099] S126, determining the syntax analysis result, the null pointer analysis result, the resource leak analysis result and the logic analysis result as the static analysis result;
[0100] S127, determining the first object that is correctly represented by both the grammatical analysis result and the logical analysis result as the second object.
[0101] It is worth noting that the syntax analysis results, null pointer analysis results, resource leak analysis results and logic analysis results of this embodiment can all be correct. If any of them has an error, the specific location where the error occurs will be recorded in the corresponding result. For example, the field where the syntax error occurs will be recorded in the syntax analysis result, and no further details will be given later.
[0102] It should be noted that after scanning the first object, Figure 2 As shown, this embodiment determines the syntax analysis result of the first object through syntax detection, for example, determining whether there are spelling errors and missing punctuation in the first object, such as missing semicolons at the line break position of a code statement.
[0103] It should be noted that when the first object has an object pointer, this embodiment further detects whether the pointer calling object of the object pointer is null. For example, when the object pointer calls an object method that is Null, an error may occur due to the lack of a return value. Therefore, this embodiment introduces null pointer analysis results in static analysis to ensure that pointer call errors are discovered in a timely manner.
[0104] It should be noted that when the first object calls a resource file or establishes a network connection, it needs to stop accessing the resource file or disconnect the network connection after use. If it is not disconnected correctly, resource leakage is likely to occur. Therefore, this embodiment uses static analysis to identify whether there is a related closing statement in the first object to determine the resource leakage analysis result.
[0105] It should be noted that when the first object includes a conditional statement or a loop statement, static analysis can be used to determine whether the logic of the conditional statement or the loop statement is correct, such as whether there are conflicts between multiple conditions in the conditional statement, whether the loop condition of the loop statement is established, etc.
[0106] In addition, in one embodiment, referring to Figure 3Step S13 specifically includes but is not limited to the following steps:
[0107] S131, inputting the target code set and the requirement file into the dynamic analysis tool, and the dynamic analysis tool executes the corresponding code module based on the requirement input information;
[0108] S132, when the code module includes at least one module function, predicting the predicted execution time of the module function based on the code corresponding to the module function, recording the running execution time of the module function during the running process, and determining the performance analysis result of the code module based on the predicted execution time and the running execution time;
[0109] S133, determining an exception analysis result of the code module, wherein the exception analysis result is used to indicate whether an operation exception occurs, and the operation exception includes a type conversion error, an array out of bounds, or a deadlock;
[0110] S134, when the exception analysis result indicates that an exception has occurred, generating an exception handling result based on the response of the code module to the operation exception, wherein the exception handling result indicates whether the code module correctly responds to the operation exception;
[0111] S135, generating a dynamic analysis result based on the performance analysis result, the exception analysis structure and the exception handling result.
[0112] It should be noted that the dynamic analysis tool can be built using a common NLP model. After the target code set and requirement file are input into the dynamic analysis tool, the requirement input information is extracted from the requirement file, and a code module and the corresponding requirement input information are input into the NLP model for operation.
[0113] It should be noted that, in the present embodiment, the running execution time of each module function of the code module is timed by the dynamic analysis tool during operation, and the predicted execution time is predicted by the code of the module function. When the difference between the running execution time and the predicted execution time is large, it can be determined that a performance bottleneck has occurred. For example, by setting a time threshold, when the difference between the two is greater than the time threshold, it is determined that a performance bottleneck has occurred, such as Figure 2 As shown, the performance analysis result is determined as the function execution time is too long.
[0114] It should be noted that in addition to performance bottlenecks, this embodiment also captures exceptions that occur when the code module is running. When the code module has a type conversion error, array out of bounds, deadlock or race condition, an exception is thrown. These exceptions are captured through dynamic analysis tools and exception analysis results are generated. The code segment where the exception occurs is recorded in the exception analysis results.
[0115] It should be noted that when an exception occurs, there is usually an exception response mechanism in the code module. This embodiment obtains the operation exception response through a dynamic analysis tool. For example, whether to cancel the process that caused the deadlock after a deadlock occurs, the corresponding response process can be queried from the debug log to determine the exception handling result.
[0116] In addition, in one embodiment, referring to Figure 3 After executing step S16, the following steps are also included but not limited to:
[0117] S31, building a visualization interface;
[0118] S32, determining at least one first object representing an error among a syntax analysis result, a null pointer analysis result, a resource leak analysis result, and a logic analysis result as a static abnormal object, and displaying the static abnormal object and the corresponding static analysis result on a visual interface;
[0119] S33, determining a code module representing an abnormality in at least one of the performance analysis result, the abnormality analysis structure, and the abnormality handling result as a dynamic abnormality module, and displaying the dynamic abnormality module and the corresponding dynamic analysis result on a visual interface;
[0120] S34, determining the second object whose naming prediction result indicates that the second object is named incorrectly as a naming abnormal object, and displaying the naming prediction result of the naming abnormal object on a visual interface;
[0121] S35, determining the code module whose demand deviation analysis result represents the demand deviation as a deviation module, and displaying the demand deviation analysis result of the deviation module on a visual interface.
[0122] It should be noted that this embodiment displays static analysis results, dynamic analysis results, naming prediction results and requirement deviation analysis results through a visual interface, thereby comprehensively displaying the code analysis results of the target code set, making it easier for developers to correct the code in a timely manner.
[0123] It should be noted that since the target code set has a large number of codes, this embodiment only displays the results where errors occur. For example, in the static analysis results, at least one of the first objects representing the error among the syntax analysis results, null pointer analysis results, resource leakage analysis results, and logic analysis results is determined as a static exception object, and the static exception object and the corresponding static analysis results are displayed in the visual interface, such as the field where the syntax error occurs, the object pointer called as Null, etc.
[0124] It should be noted that the performance analysis results, exception analysis structure and exception handling results are determined for each code module. Therefore, any code module that represents an error can be determined as a dynamic exception module, and the dynamic exception module and the corresponding dynamic analysis results can be displayed in a visual interface, such as the function of the execution time process in a dynamic exception module, the code segment where a deadlock occurs, etc.
[0125] It should be noted that after performing naming prediction on the second object, it can be determined whether the naming of the second object complies with the naming rules recorded in the requirement document. The second object that does not comply with the naming rules is determined as a naming abnormality object, and the corresponding naming prediction results are displayed visually to prompt the developer to correct the name.
[0126] It should be noted that, in this embodiment, the code module whose demand deviation is characterized by the demand deviation analysis result is determined as the deviation module. The deviation module does not have an erroneous dynamic analysis result and has no error at the operation level. Instead, the erroneous processing flow is executed due to a code writing error, resulting in the operation output information being different from the demand output information. Therefore, the deviation module cannot be detected by static analysis tools and dynamic analysis tools. This embodiment can analyze the demand deviation of the code module that runs correctly through semantic recognition of the operation output information and the demand output information, and display the demand deviation analysis results of the deviation module in a visual interface, prompting developers to promptly correct the code modules that run normally but have demand deviations.
[0127] In addition, in one embodiment, referring to Figure 3 After executing step S33, the following steps are also included but not limited to:
[0128] S36, determining the syntax analysis result, null pointer analysis result, resource leak analysis result or logic analysis result corresponding to the static exception object as the static exception root cause, generating static repair prompt information based on the static exception root cause, and displaying the static repair prompt information on the visual interface;
[0129] S37, obtaining debug information of the dynamic exception module from the debug log, determining the dynamic exception root cause of the dynamic exception module based on the debug information, generating dynamic repair prompt information based on the dynamic exception root cause, and displaying the dynamic repair prompt information on the visual interface;
[0130] S38, displaying the predicted name of the named abnormal object on the visual interface;
[0131] S39, determining deviation information based on the information difference between the demand output information and the operation output information of the deviation module, and displaying the code corresponding to the deviation information in the deviation module on the information visualization interface.
[0132] It should be noted that this embodiment not only displays the static analysis results and other results in the visual interface, but also displays the corresponding repair prompt information to provide a reference for developers to solve the exception.
[0133] It should be noted that for static exception objects, the repair prompt only needs to know the code statement where the exception occurs, and each item that represents the error in the syntax analysis result, null pointer analysis result, resource leak analysis result or logical analysis result is determined as the static exception root cause. For example, if only the syntax analysis result represents the error, then the syntax analysis error is the static exception root cause. If the resource leak analysis result and the logical analysis result represent the error, then the resource leak analysis result and the logical analysis result are both the static exception root cause, and so on. After determining the static exception root cause, static repair prompt information is generated based on the inspection basis during the static analysis tool detection. You can also set a repair strategy for each type of exception in the static analysis tool. For example, when performing syntax detection, if it is identified that there is a typo in the name of the first object, the correct character is used as the static repair prompt information; for example, if a network connection is not closed correctly after use, the static repair prompt information is the closing statement of the network connection, which will not be described one by one here.
[0134] It should be noted that the principle of the dynamic exception root cause can refer to the static exception root cause, and can be determined based on the error record recorded in the debug information. Those skilled in the art are familiar with how to capture error records based on debug information, and will not be repeated here. After determining the dynamic exception root cause, a corresponding repair strategy is set based on each type of dynamic exception root cause. For example, when the dynamic exception root cause is determined to be a function execution time that is too long, a dynamic repair prompt message is generated based on the name of the function to prompt the developer to check the function; for another example, when the dynamic exception root cause is determined to be an exception that is not handled correctly, a dynamic repair prompt message is generated based on the unhandled exception.
[0135] It should be noted that the naming of abnormal objects is determined based on name prediction, so the predicted name can be used as repair prompt information. Similarly, the deviation information code is the root cause of the information deviation, and the deviation information code can be used as repair prompt information.
[0136] like Figure 4 As shown, Figure 4 : is a structural diagram of a code analysis device based on artificial intelligence provided by an embodiment of the present invention. The present invention also provides a code analysis device based on artificial intelligence, including:
[0137] The processor 401 may be implemented by a general-purpose central processing unit (CPU), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of the present application;
[0138] The memory 402 can be implemented in the form of a read-only memory (ROM), a static storage device, a dynamic storage device, or a random access memory (RAM). The memory 402 can store an operating system and other application programs. When the technical solution provided in the embodiment of this specification is implemented by software or firmware, the relevant program code is stored in the memory 402, and the processor 401 calls and executes the artificial intelligence-based code analysis method of the embodiment of this application;
[0139] Input / output interface 403, used to implement information input and output;
[0140] Communication interface 404, used to realize communication interaction between the device and other devices, which can be realized through wired mode (such as USB, network cable, etc.) or wireless mode (such as mobile network, WIFI, Bluetooth, etc.);
[0141] A bus 405 that transmits information between various components of the device (e.g., processor 401, memory 402, input / output interface 403, and communication interface 404);
[0142] The processor 401 , the memory 402 , the input / output interface 403 and the communication interface 404 are connected to each other in communication within the device via the bus 405 .
[0143] An embodiment of the present application also provides an electronic device, including the artificial intelligence-based code analysis device as described above.
[0144] An embodiment of the present application also provides a storage medium, which is a computer-readable storage medium. The storage medium stores a computer program, and when the computer program is executed by a processor, the above-mentioned artificial intelligence-based code analysis method is implemented.
[0145] As a non-transient computer-readable storage medium, the memory can be used to store non-transient software programs and non-transient computer executable programs. In addition, the memory may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage devices. In some embodiments, the memory may optionally include a memory remotely arranged relative to the processor, and these remote memories may be connected to the processor via a network. Examples of the above-mentioned networks include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and a combination thereof. The device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and are implemented to be located in one place, or may also be distributed to multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the present embodiment.
[0146] It will be appreciated by those skilled in the art that all or some of the steps and systems in the methods disclosed above may be implemented as software, firmware, hardware, and appropriate combinations thereof. Some or all physical components may be implemented as software executed by a processor, such as a central processing unit, a digital signal processor, or a microprocessor, or as hardware, or as an integrated circuit, such as an application-specific integrated circuit. Such software may be distributed on a computer-readable medium, which may include a computer storage medium (or non-transitory medium) and a communication medium (or transient medium). As known to those skilled in the art, the term computer storage medium includes volatile and non-volatile, removable and non-removable media implemented in any method or technology for storing information (such as computer-readable instructions, data structures, program modules, or other data). Computer storage media include, but are not limited to, RAM, ROM, EEPROM, flash memory or other memory technology, CD-ROM, digital versatile disk (DVD) or other optical disk storage, magnetic cassettes, magnetic tapes, disk storage or other magnetic storage devices, or any other medium that can be used to store desired information and can be accessed by a computer. Furthermore, it is well known to those skilled in the art that communication media generally include computer readable instructions, data structures, program modules, or other data in a modulated data signal such as a carrier wave or other transport mechanism, and may include any information delivery media.
[0147] The above is a specific description of the preferred implementation of the present invention, but the present invention is not limited to the above-mentioned implementation mode. Technical personnel familiar with the field can also make various equivalent deformations or substitutions under the shared conditions without violating the spirit of the present invention. These equivalent deformations or substitutions are all included in the scope defined by the claims of the present invention.
Claims
1. A code analysis method based on artificial intelligence, characterized in that: include: Obtaining a requirement file and a target code set to be analyzed, wherein the target code set includes a plurality of code modules, the requirement file records a naming rule, requirement input information and requirement output information of each of the code modules, and the naming rule is used to indicate the structure of an object name; Scanning the target code set based on a preset static analysis tool to determine a static analysis result, and determining a second object from the scanned first object, wherein the static analysis result corresponding to the second object indicates that the code is correct; Executing the target code set through a preset dynamic analysis tool to obtain a dynamic analysis result, obtaining a debug log output by the dynamic analysis tool, and recording the operation output information of each of the code modules, wherein the debug log records the calling relationship of each of the first objects; Perform semantic recognition based on the operation output information and the demand output information to determine the demand deviation analysis results of each of the code modules; Inputting the debug log, the naming rule and each second object into a preset prediction model, and determining a naming prediction result of each second object based on the prediction model, wherein the naming prediction result is used to indicate whether the second object is correctly named or incorrectly named; Generate a code analysis result of the target code set based on the static analysis result, the dynamic analysis result, the requirement deviation analysis result and the naming prediction result; Before inputting the debug log, the naming rule and each of the second objects into a preset prediction model, the method further includes: Acquire the target content, object type and object scope of each of the second objects from the target code set; Based on each of the second objects, after deleting stop words from the target content, the target content is segmented into a plurality of target word segments, wherein each of the target word segments is a word or a mark character; Convert the uppercase characters in the target word to lowercase, and when the target word is a verb, restore the target word to the original form of the verb; Determining a naming prediction result of each of the second objects based on the prediction model includes: Selecting a target object from the second objects, determining a target call relationship of the target object based on the debug log, and obtaining a target name of the target object; Predicting the calling context information of the target object based on the target calling relationship, and determining the target structure from the naming rule based on the object type of the target object; Predicting a predicted name based on the target structure, the call context information, the object scope, and the target word segmentation, and determining a name similarity based on the predicted name and the target name; A naming prediction result is determined based on the name similarity and a preset similarity threshold.
2. The code analysis method based on artificial intelligence according to claim 1 is characterized in that: Scanning the target code set based on a preset static analysis tool to determine a static analysis result, and determining a second object from the scanned first object, including: Scanning the target code set to determine each of the first objects; Performing a syntax check on the first object to determine a syntax analysis result, wherein the syntax analysis result is used to indicate whether there is a syntax error in the first object; When the first object records at least one object pointer, a null pointer analysis result is determined by detecting a pointer calling object of the object pointer, wherein the null pointer analysis result is used to indicate whether the pointer calling object is null; Determine a resource leakage analysis result based on a resource file called by the first object or an established network connection, wherein the resource leakage analysis result is used to indicate whether the resource file or the network connection is closed correctly; When the first object includes a conditional statement or a loop statement, determining a logic analysis result of the first object, wherein the logic analysis result is used to indicate whether there is a logic error in the conditional statement or the loop statement; Determine the syntax analysis result, the null pointer analysis result, the resource leak analysis result and the logic analysis result as the static analysis result; The first object represented correctly by both the grammatical analysis result and the logical analysis result is determined as the second object.
3. The code analysis method based on artificial intelligence according to claim 2 is characterized in that: The target code set is executed by a preset dynamic analysis tool to obtain dynamic analysis results, including: Inputting the target code set and the requirement file into the dynamic analysis tool, and the dynamic analysis tool executes the corresponding code module based on the requirement input information; When the code module includes at least one module function, a predicted execution time of the module function is predicted based on a code corresponding to the module function, the running execution time of the module function is recorded during the running process, and a performance analysis result of the code module is determined based on the predicted execution time and the running execution time; Determine an exception analysis result of the code module, wherein the exception analysis result is used to indicate whether an operation exception occurs, and the operation exception includes a type conversion error, an array out of bounds, or a deadlock; When the exception analysis result is used to indicate the occurrence of an exception, an exception handling result is generated based on the response of the code module to the operation exception, wherein the exception handling result is used to indicate whether the code module correctly responds to the operation exception; The dynamic analysis result is generated based on the performance analysis result, the exception analysis result and the exception handling result.
4. The code analysis method based on artificial intelligence according to claim 3 is characterized in that: After generating the code analysis result of the target code set based on the static analysis result, the dynamic analysis result, the requirement deviation analysis result and the naming prediction result, the method further includes: Build a visual interface; Determine the first object that represents an error in at least one of the syntax analysis result, the null pointer analysis result, the resource leak analysis result, and the logic analysis result as a static exception object, and display the static exception object and the corresponding static analysis result on the visualization interface; Determine the code module that represents an abnormality in at least one of the performance analysis result, the abnormality analysis result, and the abnormality handling result as a dynamic abnormality module, and display the dynamic abnormality module and the corresponding dynamic analysis result on the visual interface; Determine the second object whose naming prediction result indicates a naming error as a naming abnormality object, and display the naming prediction result of the naming abnormality object on the visual interface; The code module whose demand deviation analysis result represents the demand deviation is determined as a deviation module, and the demand deviation analysis result of the deviation module is displayed on the visual interface.
5. The code analysis method based on artificial intelligence according to claim 4 is characterized in that: After the dynamic abnormality module and the corresponding dynamic analysis result are displayed on the visual interface, the method further includes: Determine the syntax analysis result, the null pointer analysis result, the resource leak analysis result or the logic analysis result corresponding to the static exception object as a static exception root cause, generate static repair prompt information based on the static exception root cause, and display the static repair prompt information on the visualization interface; Acquire debug information of the dynamic exception module from the debug log, determine a dynamic exception root cause of the dynamic exception module based on the debug information, generate dynamic repair prompt information based on the dynamic exception root cause, and display the dynamic repair prompt information on the visualization interface; Displaying the predicted name of the named abnormal object on the visualization interface; Deviation information is determined based on the information difference between the demand output information and the operation output information of the deviation module, and a code corresponding to the deviation information in the deviation module is displayed on an information visualization interface.
6. A code analysis device based on artificial intelligence, characterized in that: It includes at least one control processor and a memory for communicating with the at least one control processor; the memory stores instructions that can be executed by the at least one control processor, and the instructions are executed by the at least one control processor to enable the at least one control processor to execute the artificial intelligence-based code analysis method as described in any one of claims 1 to 5.
7. An electronic device, characterized in that: Including the artificial intelligence based code analysis device as described in claim 6.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute the artificial intelligence-based code analysis method as described in any one of claims 1 to 5.
Citation Information
Patent Citations
Software demand and code mapping method and system
CN113791757A
Code quality detection method and device, computer equipment and storage medium
CN116893960A