Code quality automatic detection method and device based on artificial intelligence

Code quality detection through graph convolutional neural network solves the problems of traditional methods that are time-consuming, high false alarm rate and lack of intelligence, and achieves efficient and intelligent code quality detection, which is suitable for large-scale projects and complex code libraries.

CN118733442APending Publication Date: 2024-10-01JIANGSU SUNING BANK CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410741769.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-11
Publication Date
2024-10-01

AI Technical Summary

Technical Problem

Traditional code quality inspection methods rely on manual review and static code analysis tools, which are time-consuming, susceptible to experience, difficult to scale, lack intelligence and adaptability, and difficult to adapt to complex programming logic and new programming paradigms.

Method used

Using a method based on graph convolutional neural network, it collects source code data, generates abstract syntax trees, constructs graph data, and inputs the trained graph convolutional neural network for quality inspection and generates code analysis reports to achieve automation and intelligence. Code quality inspection.

Benefits of technology

Significantly reduces the false positive rate, is suitable for large-scale projects, can continuously learn and adapt to the needs of different projects and code bases, and improves the efficiency and accuracy of code quality detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118733442A_ABST
    Figure CN118733442A_ABST
Patent Text Reader

Abstract

The invention discloses a code quality automatic detection method and device based on artificial intelligence. The method comprises the steps of collecting source code data; the code format of the source code data is preprocessed, an abstract syntax tree is generated according to codes subjected to standardization processing, and then the abstract syntax tree is constructed into graph data; inputting the constructed graph data into the trained qualified graph convolutional neural network so as to carry out quality detection on the code, and outputting a detection result of each node; and generating a code analysis report according to the detection result of each node. According to the method, the logic and the flow direction of the code can be better understood, the complex structure and the semantic relation in the code are captured, the requirements of different items and code libraries are met, and the false alarm rate can be remarkably reduced; and various types of problems can be detected at the same time, and the method is suitable for large-scale projects and more efficient.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of automatic code quality detection methods based on artificial intelligence, and in particular to an automatic code quality detection method and device based on artificial intelligence. Background Art

[0002] With the rapid development of the software industry, the complexity and quantity of codes are increasing. Code quality affects the reliability and performance of software, especially in the banking industry, where software reliability is crucial. Traditional code quality detection methods mainly rely on manual review and static code analysis tools, which have the following limitations:

[0003] 1. Limitations of manual review: Manual code review is time-consuming and easily affected by the reviewer’s experience, making it difficult to ensure consistency and comprehensiveness. In addition, manual review is difficult to scale to handle large projects and complex systems.

[0004] 2. Deficiencies of static code analysis tools: Although static code analysis tools can automatically detect some common problems in the code, such as syntax errors, potential null pointer exceptions, etc., they are usually based on preset rules, have limited understanding of complex programming logic and context, and are prone to generate a large number of false positives.

[0005] 3. Lack of intelligence and adaptability: Existing tools often lack the ability to self-learn and adapt, and are difficult to evolve with the development of projects and the evolution of code bases, and are also difficult to adapt to new programming paradigms and language features. Summary of the invention

[0006] The purpose of the present invention is to provide a method and device for automatic detection of code quality based on artificial intelligence in view of the deficiencies in the prior art.

[0007] To achieve the above objectives, in a first aspect, the present invention provides an artificial intelligence-based automatic code quality detection method, comprising:

[0008] Step 1: Collect source code data;

[0009] Step 2: preprocess the code format of the source code data, and generate an abstract syntax tree based on the standardized code. i Represents a syntax element, the syntax element includes variables, operators and statement blocks, and then constructs the abstract syntax tree as graph data, i=1, 2, ... N, N is the total number of nodes;

[0010] Step 3: Input the constructed graph data into the trained graph convolutional neural network to detect the quality of the code and output each node v i Test results Among them, k means there are k kinds of quality problems, y ij ∈{0,1}, representing node v i The label on the j-th quality problem, j = 1, 2, ... k;

[0011] Step 4: According to each node v i Test results Generate a code analysis report.

[0012] Furthermore, the interfaces for collecting source code include a development environment interface, a version control system interface, a code repository access interface, and a code extraction interface.

[0013] Furthermore, the preprocessing includes formatting and removing comments in the code, and the formatting includes indentation, line break, and space processing.

[0014] Furthermore, each node v i Test results The calculation method is as follows:

[0015]

[0016] Among them, Sigmoid(.) is the activation function of the second hidden layer of the graph convolutional neural network, and ReLU(.) is the activation function of the first hidden layer of the graph convolutional neural network. is the degree matrix, H is the adjacency matrix of the graph data with self-connection added, (0) is the node v input to the graph convolutional neural network i The characteristics of W (0) , W (1) are the weight matrices of the first hidden layer and the second hidden layer respectively.

[0017] Furthermore, the step 4 specifically includes:

[0018] Collect the detection results of all nodes from the output of the graph convolutional neural network

[0019]

[0020] Summarize the test results of all nodes, count the distribution of each quality problem in the test results in the entire code, and map the node test results back to the source code to record the specific location of each problem in the source code;

[0021] Generate corresponding natural language descriptions based on quality issues in the test results;

[0022] Sort quality issues by severity, impact on the system, and frequency of occurrence, and determine the priority of each quality issue;

[0023] Generate statistical charts to show the number and distribution of different types of quality issues;

[0024] A code analysis report is generated based on the specific location of each problem in the source code, the natural language description of the quality problem, the priority of the quality problem, and statistical charts.

[0025] In a second aspect, the present invention provides an artificial intelligence-based automatic code quality detection device, comprising:

[0026] Code collection module, used to collect source code data;

[0027] A preprocessing module is used to preprocess the code format of the source code data and generate an abstract syntax tree according to the standardized code. Each node v i Represents a syntax element, the syntax element includes variables, operators and statement blocks, and then constructs the abstract syntax tree as graph data, i=1, 2, ... N, N is the total number of nodes;

[0028] The code analysis module is used to receive the graph data using a trained graph convolutional neural network to perform quality detection on the code and output each node v i Test results Among them, k means there are k kinds of quality problems, y ij ∈{0,1}, representing node v i The label on the j-th quality problem, j = 1, 2, ... k;

[0029] The analysis includes generating modules to generate i Test results Generate a code analysis report.

[0030] Furthermore, the interfaces of the code collection module include a development environment interface, a version control system interface, a code repository access interface and a code extraction interface.

[0031] Furthermore, the preprocessing includes formatting and removing comments in the code, and the formatting includes indentation, line break, and space processing.

[0032] Furthermore, each node v i Test results The calculation method is as follows:

[0033]

[0034] Among them, Sigmoid(.) and ReLU(.) are both activation functions. is the degree matrix, H is the adjacency matrix of the graph data with self-connection added, (0) is the node v input to the graph convolutional neural network i The characteristics of W (0) , W (1) are the weight matrices of the first hidden layer and the second hidden layer of the graph convolutional neural network, respectively.

[0035] Furthermore, the analysis includes generating a code analysis report by the generation module, specifically including:

[0036] Collect the detection results of all nodes from the output of the graph convolutional neural network

[0037]

[0038] Summarize the test results of all nodes, count the distribution of each quality problem in the test results in the entire code, and map the node test results back to the source code to record the specific location of each problem in the source code;

[0039] Generate corresponding natural language descriptions based on quality issues in the test results;

[0040] Sort quality issues by severity, impact on the system, and frequency of occurrence, and determine the priority of each quality issue;

[0041] Generate statistical charts to show the number and distribution of different types of quality issues;

[0042] A code analysis report is generated based on the specific location of each problem in the source code, the natural language description of the quality problem, the priority of the quality problem, and statistical charts.

[0043] Beneficial effects: 1. The present invention can accurately represent the grammatical structure of the code by modeling the abstract syntax tree AST as graph data, better understand the logic and flow of the code, and capture the complex structure and semantic relationship in the code;

[0044] 2. The present invention can continuously learn new code examples and quality issues through model training, continuously improve the model's own detection capabilities, and adapt to the needs of different projects and code bases;

[0045] 3. The present invention adopts graph convolutional neural network GCN to identify quality problems in the code, which can significantly reduce the false alarm rate compared with traditional methods; and can detect multiple types of problems at the same time, which is suitable for large-scale projects and more efficient. BRIEF DESCRIPTION OF THE DRAWINGS

[0046] Figure 1 1 is a flow chart of an automatic code quality detection method based on artificial intelligence according to an embodiment of the present invention;

[0047] Figure 2 It is a structural schematic diagram of an automatic code quality detection device based on artificial intelligence according to an embodiment of the present invention. DETAILED DESCRIPTION

[0048] The present invention is further explained below in conjunction with the accompanying drawings and specific embodiments. The present embodiments are implemented based on the technical solution of the present invention. It should be understood that these embodiments are only used to illustrate the present invention and are not used to limit the scope of the present invention.

[0049] like Figure 1 As shown, an embodiment of the present invention provides an automatic code quality detection method based on artificial intelligence, comprising:

[0050] Step 1: Collect source code data. The interface for collecting source code is integrated with various popular development tools and version control systems (such as Git) to achieve real-time monitoring and collection of code, including the following interfaces:

[0051] 1. Integrated Development Environment (IDE) interface: Develop interfaces with mainstream IDEs to achieve real-time monitoring and collection of code, including but not limited to Eclipse, Visual Studio, IntelliJ IDEA, etc.

[0052] 2. Version control system interface: Integrate with version control systems (such as Git, SVN, etc.) to automatically trigger the collection process when the code is submitted.

[0053] 3. Code repository access interface: Get code directly from code repositories (such as GitHub, Bitbucket, etc.), supporting projects in multiple programming languages.

[0054] 4. Code extraction interface: extract specified code files or code snippets, including source code files, library files, etc., according to preset rules and requirements.

[0055] Step 2: Preprocess the code format of the source code data and generate an abstract syntax tree AST based on the standardized code. iRepresents a grammatical element. The above grammatical elements include variables, operators and statement blocks. Then the abstract syntax tree is constructed as graph data, i = 1, 2, ... N, N is the total number of nodes, and its value is a natural number greater than 2. The above preprocessing includes formatting and removing comments in the code. The formatting includes indentation, line breaks, and space processing to unify the code style. Removing comments in the code can reduce interference factors in subsequent analysis. The above graph data can be represented as G = (V, E, X, Y), where V = (v 1 ,v 2 ……v N ) represents the set of grammatical element nodes. E is the edge in the graph data, representing the grammatical relationship. X is the node feature matrix, defined as X = {x 1 ,x 2 ,…,x d},and in, is a real number, d is the dimension of each node feature vector, and the node features may include grammatical type, vocabulary information, context information, static analysis information, etc.

[0056] Step 3: Input the constructed graph data into a trained graph convolutional neural network (GCN) to detect the quality of the code and output each node v i Test results Among them, k means there are k kinds of quality problems, y ij ∈{0,1}, representing node v i The label of the jth quality problem, j = 1, 2, ... k. It can be a grammatical error, a logical error, a style inconsistency, a security vulnerability, a performance problem, etc. ij When the value of 0 indicates that there is no quality problem, ij When the value is 1, it means that this quality problem exists.

[0057] Specifically, the above graph convolutional neural network has an input layer, two hidden layers and an output layer, and each node v i Test results The calculation method is as follows:

[0058]

[0059] Among them, Sigmoid(.) is the activation function of the second hidden layer of the graph convolutional neural network, and ReLU(.) is the activation function of the first hidden layer of the graph convolutional neural network. is the degree matrix, H is the adjacency matrix of the graph data with self-connection added, (0) is the node v input to the graph convolutional neural network i The characteristics of W (0) , W(1) are the weight matrices of the first hidden layer and the second hidden layer respectively.

[0060] Step 4: According to each node v i Test results Generate a code analysis report. Details are as follows:

[0061] Collect the detection results of all nodes from the output of the graph convolutional neural network

[0062]

[0063] The detection results of all nodes are summarized, and the distribution of each quality problem in the detection results in the entire code is counted. At the same time, the detection results of the nodes are mapped back to the source code, and the specific location of each problem in the source code is recorded.

[0064] Generate corresponding natural language descriptions based on the quality issues in the detection results. For example, if a node is detected to have a "potential null pointer reference" problem, the report describes the specific location of the problem and the potential risks.

[0065] Sort quality issues according to their severity, impact on the system, and frequency of occurrence, and determine the priority of each quality issue.

[0066] Generate statistical charts to show the number and distribution of different types of quality issues. It also provides problem trend analysis to show the improvement or degradation of code quality over time.

[0067] A code analysis report is generated based on the specific location of each problem in the source code, the natural language description of the quality problem, the priority of the quality problem, and statistical charts. The report format can be HTML, PDF, Markdown, etc., which is convenient for developers to read and share.

[0068] See also Figure 2 Based on the above embodiments, those skilled in the art can easily understand that the present invention also provides an automatic code quality detection device based on artificial intelligence, including a code acquisition module 1, a preprocessing module 2, a code analysis module 3 and an analysis generation module 4.

[0069] The code collection module 1 is used to collect source code data. The interface of the code collection module is integrated with various popular development tools and version control systems (such as Git) to achieve real-time monitoring and collection of code, including the following interfaces:

[0070] 1. Integrated Development Environment (IDE) interface: Develop interfaces with mainstream IDEs to achieve real-time monitoring and collection of code, including but not limited to Eclipse, Visual Studio, IntelliJ IDEA, etc.

[0071] 2. Version control system interface: Integrate with version control systems (such as Git, SVN, etc.) to automatically trigger the collection process when the code is submitted.

[0072] 3. Code repository access interface: Get code directly from code repositories (such as GitHub, Bitbucket, etc.), supporting projects in multiple programming languages.

[0073] 4. Code extraction interface: extract specified code files or code snippets, including source code files, library files, etc., according to preset rules and requirements.

[0074] The preprocessing module 2 is used to preprocess the code format of the source code data and generate an abstract syntax tree based on the standardized code. i Represents a grammatical element, which includes variables, operators and statement blocks, and then constructs the abstract syntax tree as graph data, i = 1, 2, ... N, N is the total number of nodes, and its value is a natural number greater than 2. The above preprocessing includes formatting and removing comments in the code, where formatting includes indentation, line breaks, and space processing to unify the code style. Removing comments in the code can reduce interference factors in subsequent analysis. The above graph data can be represented as G = (V, E, X, Y), where V = (v 1 ,v 2 ……v N ) represents the set of grammatical element nodes. E is the edge in the graph data, representing the grammatical relationship. X is the node feature matrix, defined as X = {x 1 ,x 2 ,…,x d},and in, is a real number, d is the dimension of each node feature vector, and the node features may include grammatical type, vocabulary information, context information, static analysis information, etc.

[0075] The code analysis module 3 is used to receive the graph data using a trained graph convolutional neural network (GCN) to perform quality detection on the code and output each node v i Test results Among them, k means there are k kinds of quality problems, y ij ∈{0,1}, representing node v iThe label of the jth quality problem, j = 1, 2, ... k. It can be a grammatical error, a logical error, a style inconsistency, a security vulnerability, a performance problem, etc. ij When the value of 0 indicates that there is no quality problem, ij When the value is 1, it means that this quality problem exists.

[0076] Specifically, the above graph convolutional neural network has an input layer, two hidden layers and an output layer, and each node v i Test results The calculation method is as follows:

[0077]

[0078] Among them, Sigmoid(.) is the activation function of the second hidden layer of the graph convolutional neural network, and ReLU(.) is the activation function of the first hidden layer of the graph convolutional neural network. is the degree matrix, H is the adjacency matrix of the graph data with self-connection added, (0) is the node v input to the graph convolutional neural network i The characteristics of W (0) , W (1) are the weight matrices of the first hidden layer and the second hidden layer respectively.

[0079] The analysis includes generating module 4 for generating a i Test results Generate a code analysis report. Details are as follows:

[0080] Collect the detection results of all nodes from the output of the graph convolutional neural network

[0081]

[0082] The detection results of all nodes are summarized, and the distribution of each quality problem in the detection results in the entire code is counted. At the same time, the detection results of the nodes are mapped back to the source code, and the specific location of each problem in the source code is recorded.

[0083] Generate corresponding natural language descriptions based on the quality issues in the detection results. For example, if a node is detected to have a "potential null pointer reference" problem, the report describes the specific location of the problem and the potential risks.

[0084] Sort quality issues according to their severity, impact on the system, and frequency of occurrence, and determine the priority of each quality issue.

[0085] Generate statistical charts to show the number and distribution of different types of quality issues. It also provides problem trend analysis to show the improvement or degradation of code quality over time.

[0086] A code analysis report is generated based on the specific location of each problem in the source code, the natural language description of the quality problem, the priority of the quality problem, and statistical charts. The report format can be HTML, PDF, Markdown, etc., which is convenient for developers to read and share.

[0087] The above is only a preferred embodiment of the present invention. It should be noted that for ordinary technicians in this technical field, other parts not specifically described belong to the prior art or common knowledge. Several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered as the protection scope of the present invention.

Claims

1. An automatic code quality detection method based on artificial intelligence, characterized in that: include: Step 1: Collect source code data; Step 2: preprocess the code format of the source code data, and generate an abstract syntax tree based on the standardized code. i Represents a syntax element, the syntax element includes variables, operators and statement blocks, and then constructs the abstract syntax tree as graph data, i=1, 2, ... N, N is the total number of nodes; Step 3: Input the constructed graph data into the trained graph convolutional neural network to detect the quality of the code and output each node v i Test results Among them, k means there are k kinds of quality problems, y ij ∈{0,1}, representing node v i The label on the j-th quality problem, j = 1, 2, ... k; Step 4: According to each node v i Test results Generate a code analysis report.

2. According to the method of automatic detection of code quality based on artificial intelligence in claim 1, it is characterized in that: The interfaces used to collect source code include development environment interface, version control system interface, code repository access interface and code extraction interface.

3. The method for automatic detection of code quality based on artificial intelligence according to claim 1, characterized in that: The preprocessing includes formatting and removing comments in the code, and the formatting includes indentation, line break, and space processing.

4. The method for automatic detection of code quality based on artificial intelligence according to claim 1, characterized in that: Each node v i Test results The calculation method is as follows: Among them, Sigmoid(.) is the activation function of the second hidden layer of the graph convolutional neural network, and ReLU(.) is the activation function of the first hidden layer of the graph convolutional neural network. is the degree matrix, H is the adjacency matrix of the graph data with self-connection added, (0) is the node v input to the graph convolutional neural network i The characteristics of W (0) , W (1) are the weight matrices of the first hidden layer and the second hidden layer respectively.

5. The method for automatic detection of code quality based on artificial intelligence according to claim 1, characterized in that: The step 4 specifically includes: Collect the detection results of all nodes from the output of the graph convolutional neural network Summarize the test results of all nodes, count the distribution of each quality problem in the test results in the entire code, and map the node test results back to the source code to record the specific location of each problem in the source code; Generate corresponding natural language descriptions based on quality issues in the test results; Sort quality issues by severity, impact on the system, and frequency of occurrence, and determine the priority of each quality issue; Generate statistical charts to show the number and distribution of different types of quality issues; A code analysis report is generated based on the specific location of each problem in the source code, the natural language description of the quality problem, the priority of the quality problem, and statistical charts.

6. An automatic code quality detection device based on artificial intelligence, characterized in that: include: Code collection module, used to collect source code data; A preprocessing module is used to preprocess the code format of the source code data and generate an abstract syntax tree according to the standardized code. Each node v i Represents a syntax element, the syntax element includes variables, operators and statement blocks, and then constructs the abstract syntax tree as graph data, i=1, 2, ... N, N is the total number of nodes; The code analysis module is used to receive the graph data using a trained graph convolutional neural network to perform quality detection on the code and output each node v i Test results Among them, k means there are k kinds of quality problems, y ij ∈{0,1}, representing node v i The label on the j-th quality problem, j = 1, 2, ... k; The analysis includes generating modules to generate i Test results Generate a code analysis report.

7. The code quality automatic detection device based on artificial intelligence according to claim 6 is characterized in that: The interfaces of the code acquisition module include a development environment interface, a version control system interface, a code repository access interface and a code extraction interface.

8. The code quality automatic detection device based on artificial intelligence according to claim 6 is characterized in that: The preprocessing includes formatting and removing comments in the code, and the formatting includes indentation, line break, and space processing.

9. The code quality automatic detection device based on artificial intelligence according to claim 6 is characterized in that: Each node v i Test results The calculation method is as follows: Among them, Sigmoid(.) and ReLU(.) are both activation functions. is the degree matrix, H is the adjacency matrix of the graph data with self-connection added, (0) is the node v input to the graph convolutional neural network i The characteristics of W (0) , W (1) are the weight matrices of the first hidden layer and the second hidden layer of the graph convolutional neural network, respectively.

10. The code quality automatic detection device based on artificial intelligence according to claim 6 is characterized in that: The analysis includes generating a code analysis report by the generating module, specifically including: Collect the detection results of all nodes from the output of the graph convolutional neural network Summarize the test results of all nodes, count the distribution of each quality problem in the test results in the entire code, and map the node test results back to the source code to record the specific location of each problem in the source code; Generate corresponding natural language descriptions based on quality issues in the test results; Sort quality issues by severity, impact on the system, and frequency of occurrence, and determine the priority of each quality issue; Generate statistical charts to show the number and distribution of different types of quality issues; A code analysis report is generated based on the specific location of each problem in the source code, the natural language description of the quality problem, the priority of the quality problem, and statistical charts.