Code detection method and device, computer equipment and storage medium

By obtaining and analyzing the syntax tree and index tree of the code file, combining the detection rules of the syntax tree and index tree, the mixed code engineering of multiple language types is detected, which solves the problem of inaccurate detection in the existing technology, and realizes efficient and comprehensive detection of mixed code engineering.

CN119759741APending Publication Date: 2025-04-04GUANGZHOU QUYAN NETWORK TECH CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202411887406.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-20
Publication Date
2025-04-04

AI Technical Summary

Technical Problem

Existing static code inspection systems cannot conduct comprehensive inspections on mixed code projects containing multiple language types, resulting in inaccurate detection.

Method used

The code file is detected by obtaining the syntax tree structure and index tree structure of each code file in the code project to be detected, and determining the corresponding code detection rules based on the programming language type of the code file. This method combines syntax trees and index trees to check surface problems such as code style and naming specifications, as well as deeper logical problems such as code references and code relationships.

Benefits of technology

It realizes efficient and comprehensive inspection of hybrid code projects of multiple language types, and supports the detection of single programming language projects and hybrid programming language projects. Whether the code project uses a single programming language or multiple programming languages, it can perform accurate multi-level inspections.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119759741A_ABST
    Figure CN119759741A_ABST
Patent Text Reader

Abstract

The invention relates to a code detection method and device, computer equipment, a storage medium and a computer program product. The method comprises the steps of obtaining a to-be-detected code project; for any code file in the to-be-detected code project, obtaining a syntax tree structure and an index tree structure corresponding to the code file by calling a code analysis interface matched with the programming language type of the code file; according to the programming language type to which any code file belongs, determining each code detection rule for any code file; and for any code file, detecting the code file by using the target tree structure adaptive to each code detection rule of the code file to obtain a code detection result of the code file under each code detection rule. By adopting the method, multi-level detection can be efficiently, comprehensively and flexibly carried out on the single programming language project and the mixed programming language project.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a code detection method, apparatus, computer equipment, storage medium and computer program product. Background Art

[0002] Static Code Analysis System is a type of tool used to analyze source code before compilation or execution, aiming to detect potential errors, performance issues, security vulnerabilities, and parts that do not comply with coding standards in the code.

[0003] The most commonly used static code checking systems are SwiftLint and OCLint. SwiftLint is used to check Swift code, and OCLint is used to check Objective-C. However, SwiftLint or OCLint can only be used to check code of a single language type. When used to check mixed code projects containing multiple language types, comprehensive test results are often not obtained.

[0004] Therefore, there is a problem in the traditional technology that the detection of mixed code engineering source code containing multiple language types is not accurate enough. Summary of the invention

[0005] Based on this, it is necessary to provide a code detection method, device, computer equipment, computer-readable storage medium and computer program product that can accurately detect source code of mixed code engineering of multiple language types in response to the above technical problems.

[0006] A code detection method, comprising:

[0007] Get the code project to be tested;

[0008] For any code file in the code project to be detected, the syntax tree structure and index tree structure corresponding to the code file are obtained by calling the code parsing interface that matches the programming language type of the code file;

[0009] Determine each code detection rule for any code file according to the programming language type to which any code file belongs;

[0010] For any code file, the code file is detected using a target tree structure that is compatible with each code detection rule of the code file to obtain the code detection result of the code file under each code detection rule; the target tree structure includes one of a syntax tree structure and an index tree structure.

[0011] In one embodiment, by calling a code parsing interface that matches the programming language type of the code file, a syntax tree structure and an index tree structure corresponding to the code file are obtained, including:

[0012] When the programming language type of the code file is Objective-C, obtain the libclang library;

[0013] By calling the syntax tree acquisition interface provided by the libclang library, the source code of the code file is recursively traversed to build the syntax tree structure of the code file;

[0014] By calling the index tree acquisition interface provided by the libclang library, the index tree structure of the code file is constructed based on the syntax tree structure of the code file.

[0015] In one embodiment, by calling a code parsing interface that matches the programming language type of the code file, a syntax tree structure and an index tree structure corresponding to the code file are obtained, including:

[0016] When the programming language type of the code file is Swift, import the SwiftSyntax library, and use the SwiftSyntax library to parse the source code of the code file to obtain the syntax tree structure of the code file;

[0017] Use the SourceKit toolset to build an index tree structure for code files based on the syntax tree structure of the code files.

[0018] In one embodiment, the method further comprises:

[0019] Determine the code testing requirements for the code project to be tested;

[0020] For any programming language type, code detection rules for the programming language type under various code detection requirements are constructed according to the grammatical rules of the programming language type.

[0021] In one embodiment, the code file is detected using a target tree structure that is compatible with each code detection rule of the code file to obtain a code detection result of the code file under each code detection rule, including:

[0022] According to the detection logic of any code detection rule of the code file, determine the target tree structure that is compatible with any code detection rule;

[0023] Traverse each tree node of the target tree structure that is compatible with any code detection rule, and obtain the node information required to implement the detection logic of any code detection rule;

[0024] Based on the node information, determine whether the code file has problematic code that does not comply with any code detection rule.

[0025] In one embodiment, the method further comprises:

[0026] For any code file, when a code detection result of the code file under any code detection rule indicates that the code file has a problem code that does not comply with any code detection rule, locate a target position of the problem code in the code file;

[0027] Fill the problem description information corresponding to the problem code into the problem error template corresponding to any code detection rule to generate a problem error prompt box;

[0028] Displays a problem error prompt box at the target location of the code file.

[0029] A code detection device, comprising:

[0030] The acquisition module is used to obtain the code project to be tested;

[0031] A calling module is used to obtain a syntax tree structure and an index tree structure corresponding to any code file in the code project to be detected by calling a code parsing interface that matches the programming language type of the code file;

[0032] A determination module, used to determine each code detection rule for any code file according to the programming language type to which any code file belongs;

[0033] The detection module is used to detect any code file using a target tree structure that is compatible with each code detection rule of the code file to obtain the code detection result of the code file under each code detection rule; the target tree structure includes one of a syntax tree structure and an index tree structure.

[0034] A computer device includes a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the above method when executing the computer program.

[0035] A computer-readable storage medium stores a computer program, which implements the steps of the above method when executed by a processor.

[0036] A computer program product comprises a computer program, and when the computer program is executed by a processor, the steps of the above method are implemented.

[0037] The above-mentioned code detection method, apparatus, computer equipment, storage medium and computer program product obtain a code project to be detected; for any code file in the code project to be detected, a syntax tree structure and an index tree structure corresponding to the code file are obtained by calling a code parsing interface that matches the programming language type of the code file; according to the programming language type to which any code file belongs, each code detection rule for any code file is determined; for any code file, a target tree structure that is compatible with each code detection rule of the code file is used to detect the code file, and a code detection result of the code file under each code detection rule is obtained; the target tree structure includes a syntax tree structure, a target tree structure, and a target tree structure. One of the tree structures and index tree structures; in this way, the syntax tree and the index tree can be combined at the same time to efficiently and comprehensively perform multi-level detection on the source code of each code file in the code project to be detected, which can check both surface problems such as code style and naming conventions, and deeper logical problems such as code references and code relationships. In addition, it can support the detection of single programming language projects and mixed programming language projects. No matter whether the code project to be detected is programmed in a single programming language or in multiple programming languages, the syntax tree and the index tree can be combined at the same time to perform efficient and comprehensive multi-level detection on the source code of each code file in the code project to be detected. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0039] Figure 1 An application environment diagram of a code detection method in an embodiment;

[0040] Figure 2 A schematic diagram of a flow chart of a code detection method in an embodiment;

[0041] Figure 3 is a flowchart of a code detection method in another embodiment;

[0042] Figure 4 is a structural block diagram of a code detection device in one embodiment;

[0043] Figure 5 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION

[0044] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0045] Static Code Analysis System is a type of tool used to analyze source code before compilation or execution, designed to detect potential errors, performance issues, security vulnerabilities, and parts that do not comply with coding standards in the code. Unlike dynamic analysis, static code checking is performed without executing the code, so problems can be found in the early stages of development, thereby reducing repair costs and improving code quality and security.

[0046] At present, the most commonly used static code checking systems in the iOS industry are SwiftLint and OCLint. SwiftLint is for Swift language, and OCLint is for Objective-C. Both are open source libraries. Both libraries are currently based only on syntax trees and only support a single language. Taking SwiftLint and OCLint as examples, the existing technologies have the following main disadvantages: 1) They are only for a single language. If they are to be applied to mixed projects that use both Objective-C and Swift, it is difficult and requires a lot of preliminary integration work, which makes them more troublesome to use; 2) Based on syntax trees, since the syntax trees carry less node information, complex logical errors cannot be captured. For example, SwiftLint's inspection mainly focuses on surface issues such as code style and naming conventions, and the detection of code logic errors, performance issues or security vulnerabilities is relatively weak. Therefore, SwiftLint cannot replace deeper static analysis tools.

[0047] The code detection method provided in the embodiment of the present application can be applied to Figure 1In the application environment shown. Among them, the terminal 102 communicates with the server 104 through the network. The data storage system can store the data that the server 104 needs to process. The data storage system can be integrated on the server 104, or it can be placed on the cloud or other network servers. The server 104 obtains the code project to be detected; for any code file in the code project to be detected, the server 104 obtains the syntax tree structure and index tree structure corresponding to the code file by calling the code parsing interface that matches the programming language type of the code file; the server 104 determines the code detection rules for any code file according to the programming language type to which any code file belongs; for any code file, the server 104 uses the target tree structure that is compatible with the code detection rules of the code file to detect the code file, and obtains the code detection result of the code file under each code detection rule; the target tree structure includes one of the syntax tree structure and the index tree structure. Among them, the terminal 102 can be, but is not limited to, various personal computers, laptops, smart phones, tablet computers, Internet of Things devices and portable wearable devices, and the Internet of Things devices can be smart speakers, smart TVs, smart air conditioners, smart car-mounted devices, etc. The portable wearable device may be a smart watch, a smart bracelet, a head-mounted device, etc. The server 104 may be implemented as an independent server or a server cluster consisting of multiple servers.

[0048] In an exemplary embodiment, Figure 2 As shown, a code detection method is provided, which is applied to Figure 1 The server 104 in the example is used as an example to illustrate the method, which includes the following steps S202 to S208. Among them:

[0049] Step S202, obtaining the code project to be detected.

[0050] The code project to be tested may be a project programmed in a single programming language or a project programmed in a mixture of multiple programming languages. The programming language may refer to Objective-C, Swift or other languages.

[0051] For example, the code project to be tested may be a project project programmed in Objective-C, a project project programmed in Swift, or a project project programmed in a mixture of Objective-C and Swift.

[0052] Optionally, the code project to be tested is obtained, thereby obtaining each code file to be tested.

[0053] Step S204: for any code file in the code project to be detected, a code parsing interface matching the programming language type of the code file is called to obtain a syntax tree structure and an index tree structure corresponding to the code file.

[0054] The code parsing interface may be an interface for parsing the source code of a code file. Source codes of different programming language types need to be parsed using different code parsing interfaces. For example, the two programming languages ​​Objective-C and Swift have different corresponding code parsing interfaces.

[0055] Optionally, for any code file in the code project to be tested, if the programming language type of the code file is Objective-C, the server obtains the syntax tree structure and index tree structure of the code file by calling the source code parsing interface of Objective-C; if the programming language type of the code file is Swift, the server obtains the syntax tree structure and index tree structure of the code file by calling the source code parsing interface of Swift.

[0056] It should be noted that the index tree structure and the syntax tree structure are not completely in the upper and lower layers. Although the index tree structure is based on the syntax tree structure for field expansion, there are also some capabilities that the index tree cannot achieve, but the syntax tree can. For example, the syntax tree structure can construct punctuation marks on source code nodes (such as?;[], etc.), while the index tree structure focuses more on the relationship between specific class methods, resulting in the inability to construct these symbols. Therefore, for some applications that only target the source code string structure itself, it is better to use the syntax tree, such as code style checking.

[0057] In this application, both Objective-C and Swift obtain two sets of tree structures and retain them at the same time. Different trees are used for code checking according to different scene requirements, so as to maximize the advantages of each tree. For surface issues such as code style and naming conventions, it is sufficient to use syntax trees, and for code logic errors, performance issues or security vulnerabilities, use index trees.

[0058] Step S206, determining various code detection rules for any code file according to the programming language type to which any code file belongs.

[0059] The code detection rules may be rules followed when detecting code files.

[0060] Optionally, code files belonging to different programming language types need to use different code detection rules. For a code project to be detected: if all code files belong to Objective-C, then the code detection rules for all code files adopt the code detection rules corresponding to Objective-C; if all code files belong to Swift, then the code detection rules for all code files adopt the code detection rules corresponding to Swift; if there are both code files belonging to Objective-C and code files belonging to Swift among the code files, the code files belonging to Objective-C adopt the code detection rules corresponding to Objective-C, and the code files belonging to Swift adopt the code detection rules corresponding to Swift.

[0061] Step S208, for any code file, use the target tree structure that is compatible with each code detection rule of the code file to detect the code file and obtain the code detection result of the code file under each code detection rule; the target tree structure includes one of a syntax tree structure and an index tree structure.

[0062] Optionally, for any code file, determine that code detection rule 1 of the code file needs to use the syntax tree structure of the code file when implementing detection, code detection rule 2 of the code file needs to use the index tree structure of the code file when implementing detection, ..., code detection rule n of the code file needs to use the syntax tree structure of the code file when implementing detection, and then, use code detection rule 1 of the code file to analyze the syntax tree structure of the code file to obtain the code detection result of the code file under code detection rule 1, use code detection rule 2 of the code file to analyze the index tree structure of the code file to obtain the code detection result of the code file under code detection rule 2, and use code detection rule n of the code file to analyze the syntax tree structure of the code file to obtain the code detection result of the code file under code detection rule n.

[0063] In the above code detection method, a code project to be detected is obtained; for any code file in the code project to be detected, a syntax tree structure and an index tree structure corresponding to the code file are obtained by calling a code parsing interface that matches the programming language type of the code file; according to the programming language type to which any code file belongs, each code detection rule for any code file is determined; for any code file, a target tree structure that is compatible with each code detection rule of the code file is used to detect the code file to obtain a code detection result of the code file under each code detection rule; the target tree structure includes one of a syntax tree structure and an index tree structure; in this way, the syntax tree and the index tree can be combined at the same time to efficiently and comprehensively perform multi-level detection on the source code of each code file in the code project to be detected, and surface problems such as code style and naming specifications can be checked, as well as deeper logical problems such as code references and code relationships can be checked, and the detection of single programming language projects and mixed programming language projects can be supported. Regardless of whether the code project to be detected is programmed in a single programming language or in multiple programming languages, the syntax tree and the index tree can be combined at the same time to perform efficient and comprehensive multi-level detection on the source code of each code file in the code project to be detected.

[0064] In an exemplary embodiment, a syntax tree structure and an index tree structure corresponding to the code file are obtained by calling a code parsing interface that matches the programming language type of the code file, including: when the programming language type of the code file is Objective-C, obtaining a libclang library; recursively traversing the source code of the code file to construct a syntax tree structure of the code file by calling a syntax tree acquisition interface provided by the libclang library; and constructing an index tree structure of the code file based on the syntax tree structure of the code file by calling an index tree acquisition interface provided by the libclang library.

[0065] Among them, the libclang library is the interface library of Clang, and Clang is the compilation front end of the Objective-C language.

[0066] Among them, the compilation front end is the first part of the compiler, which is responsible for analyzing the source code and converting it into an intermediate representation (IR, Intermediate Representation), providing the compiler's back end with a data structure that is easy to optimize and generate target code. The compilation front end mainly includes lexical analysis, syntax analysis, semantic analysis, intermediate code generation and other steps.

[0067] Among them, there are three main libraries of Clang interface, which allow customized transformation of lexical or syntax trees for different scenarios: libclang (direct use): as a functional library, it provides some stable high-level C language abstract interfaces for source code parsing AST. The provided API is simple, which can bridge C and clang and analyze all source codes; clang plugin (local customization): clang plugin allows adding and running other operations on AST as part of compilation. The plugin is a dynamic library loaded by the compiler at runtime and is easy to integrate into the build environment; clang libTooling (deep transformation): C++ interface, designed to write independent tools and integrate into services running clang tools. It can perform any type of analysis on source code and even rewrite programs.

[0068] Optionally, if the programming language type of the code file is Objective-C, since the iOS official compiler Xcode comes with the libclang library, the libclang library can be directly obtained through the Xcode path and used; then, by calling the syntax tree acquisition interface provided by the libclang library, the source code of the code file is recursively traversed to obtain the order and information of each node, such as the node type type, node name name and node location location, to build the syntax tree structure of the code file; then, by calling the index tree acquisition interface provided by the libclang library, based on the basic information such as the node type type, node name name and node location location of the syntax tree structure of the code file, the relationship between any node and the surrounding nodes and the context is obtained, and the source, reference object, overridden object and globally unique identifier of any node are determined, thereby building the index tree structure of the code file.

[0069] In actual applications, more node information is obtained by calling the index tree acquisition interface provided by the libclang library, mainly including: the referenced declaration location refLocation; the overriden information of the parent class; and the unique identifier USR. Among them, USR (Unified Symbol Resolution) is a unified symbol resolution mechanism, which is usually used to identify symbols in the code in program analysis and code navigation tools. USR is widely used in compilers and IDEs (integrated development environments) to generate unique and consistent identifiers for each symbol (such as variables, functions, classes, etc.). Through USR, code tools can quickly locate and reference specific symbols in the code, supporting functions such as code navigation, refactoring, and static analysis.

[0070] In this embodiment, when the programming language type of the code file is Objective-C language, the libclang library is obtained; by calling the syntax tree acquisition interface provided by the libclang library, the source code of the code file is recursively traversed to build the syntax tree structure of the code file; by calling the index tree acquisition interface provided by the libclang library, based on the syntax tree structure of the code file, an index tree structure of the code file is built; in this way, the syntax tree structure can be built by calling the syntax tree interface provided by the libclang library, and then the index tree structure can be built on the basis of the constructed syntax tree structure by calling the syntax tree interface provided by the libclang library, and two sets of tree structures are retained at the same time, which is conducive to the subsequent use of different tree structures for code checking according to different scenario requirements, thereby maximizing the advantages of each tree.

[0071] In an exemplary embodiment, a syntax tree structure and an index tree structure corresponding to the code file are obtained by calling a code parsing interface that matches the programming language type of the code file, including: when the programming language type of the code file is Swift, importing the SwiftSyntax library, and using the SwiftSyntax library to parse the source code of the code file to obtain the syntax tree structure of the code file; using the SourceKit tool set to build an index tree structure of the code file based on the syntax tree structure of the code file.

[0072] Optionally, if the programming language type of the code file is Swift, import the SwiftSyntax library, and use the SwiftSyntax library to parse the source code of the code file to obtain the syntax tree structure of the code file, mainly including node type syntax.kind, node name syntax.text, node position syntax.offset and other information; then, use the SourceKit toolset to build an index tree structure of the code file based on the syntax tree structure of the code file, including the unique identifier usr, associated node information releted and attribute field information attributes, etc.

[0073] For Swift language, use SwiftSyntax library to obtain syntax tree. Corresponding to libclang interface library, Swift language also has a low-level function library called libSwiftSyntax function library, which provides an interface for parsing source code into AST. However, there is no need to use the low-level library libclang directly like Clang (manually traverse to obtain information through Block), but use some more advanced libraries, such as SwiftSyntax (the whole tree can be obtained directly through an interface). SwiftSyntax is a higher-level library built on libSwiftSyntax. It provides a SwiftAPI that allows developers to process Swift source code at a higher level of abstraction. SwiftSyntax enables developers to operate Swift source code more conveniently, and perform code analysis, code generation, code formatting and other tasks related to source code processing.

[0074] For the Swift language, the way to obtain the index tree is different from that of Objective-C. It is impossible to obtain extended information on more nodes through the same library SwiftSyntax, so the SourceKitten library is needed. SourceKitten is a library and command line tool written in Swift, which is used to communicate with SourceKit. SourceKitten provides a series of simplified interfaces so that developers can easily access and utilize the functions of SourceKit. SourceKit is a set of tools with functions such as source code parsing, syntax highlighting, typesetting, automatic completion, cross-language header file generation, etc. Since Xcode may evoke the compiler to perform thousands of operations such as parsing, highlighting, and typesetting source code during use, these frequent operations pose a great risk to the early form of directly integrating front-end dynamic libraries (such as libclang, libSwiftSyntax) into XCode. If a dynamic library crashes, Xcode will also crash. To solve this problem, a SourceKit XPC daemon was developed. Whenever Xcode loads any Swift code, the XPC daemon will be started.

[0075] When using the SourceKit toolset, you need to send a request to SourceKit, parse it through SourceKit, obtain more node information, and build an index tree.

[0076] In this embodiment, when the programming language type of the code file is Swift language, the SwiftSyntax library is imported, and the source code of the code file is parsed by using the SwiftSyntax library to obtain the syntax tree structure of the code file; the SourceKit tool set is used to build an index tree structure of the code file based on the syntax tree structure of the code file; in this way, the syntax tree structure can be built by calling the SwiftSyntax library, and then the index tree structure can be built on the basis of the built syntax tree structure by using the SourceKit tool set, and two sets of tree structures are retained at the same time, which is conducive to the subsequent use of different tree structures for code checking according to different scenario requirements, thereby maximizing the advantages of each tree.

[0077] In an exemplary embodiment, the method further includes: determining various code detection requirements for the code project to be detected; for any programming language type, constructing code detection rules for the programming language type under various code detection requirements according to the grammatical rules of the programming language type.

[0078] Among them, code detection requirements can refer to detection purposes such as code style, naming conventions, code logic, performance issues, and security vulnerabilities.

[0079] Optionally, determine the inspection items that need to be performed on the code project to be inspected, for example, it can be surface inspections such as code style and naming conventions, or deep inspections such as code logic errors, performance issues or security vulnerabilities; then, for any programming language type, according to the grammatical rules of the programming language type, construct code inspection rules for the programming language type under various code inspection requirements. For example, according to the grammatical rules of Swift, construct code inspection rules for the Swift language under code inspection requirements such as code style, naming conventions, code logic errors, performance issues or security vulnerabilities; according to the grammatical rules of Objective-C, construct code inspection rules for the Objective-C language under code inspection requirements such as code style, naming conventions, code logic errors, performance issues or security vulnerabilities.

[0080] In actual applications, for a code project to be detected, there are multiple code detection requirements. Since each code file belongs to the same code project to be detected, the code detection requirements / code detection targets of each code file in the code project to be detected are the same. If different code files use different programming language types, then for code files belonging to different programming language types, different code detection rules need to be used to achieve the same code detection requirement / code detection target. Therefore, it is necessary to pre-construct the code detection rules corresponding to different programming language types based on the code detection requirements of the code project to be detected, so that when the code files are subsequently code-checked, the corresponding code detection rules can be directly determined according to the programming language type to which the code files belong. The code detection rules corresponding to different programming language types correspond to the technical solution of this embodiment.

[0081] In this embodiment, by determining various code detection requirements for the code project to be detected; for any programming language type, according to the grammatical rules of the programming language type, construct code detection rules for the programming language type under various code detection requirements; in this way, according to the various code detection requirements of the code project to be detected, various code detection rules for different programming language types can be pre-constructed, which is conducive to the subsequent realization of accurate and comprehensive code detection of code projects obtained by mixed programming of multiple programming languages.

[0082] In an exemplary embodiment, a target tree structure adapted to each code detection rule of a code file is used to detect a code file to obtain a code detection result of the code file under each code detection rule, including: determining a target tree structure adapted to any code detection rule according to the detection logic of any code detection rule of the code file; traversing each tree node of the target tree structure adapted to any code detection rule to obtain the node information required to implement the detection logic of any code detection rule; and determining whether the code file has problematic code that does not comply with any code detection rule based on the node information.

[0083] The detection logic may refer to the detection process. In actual applications, different detection logics may be used depending on the inherited parent class, and different trees may be used. For example, the rule inheriting SwiftSyntaxRule uses the syntax tree and uses the Syntax service of SwiftSyntax to obtain the syntax tree. The rule inheriting SwiftIndexRule uses the index tree and uses the Index service of SourceKit to obtain the index tree.

[0084] The node information may be information such as node type, node name and node position of the syntax tree structure, or information such as node source, node reference object, node rewrite object and node global unique identifier of the index tree structure.

[0085] Optionally, according to the detection process of code detection rule 1 of the code file, determine whether the tree structure required for the detection process of code detection rule 1 is a syntax tree structure or an index tree structure. When the tree structure required for the detection process of code detection rule 1 is a syntax tree structure, traverse each tree node of the syntax tree structure to obtain the node information required to implement the detection logic of code detection rule 1, and based on the node information, determine whether the code file contains problematic code that does not comply with code detection rule 1.

[0086] To facilitate understanding by those skilled in the art, the following takes the code file SourceFile using the Swift language as an example to introduce the detection processes corresponding to the two code detection rules.

[0087] The first code detection rule is to check whether the developer compares two identical objects in the code. Although the compiler will not report an error for such code, this code is redundant code, which will waste performance and affect reading. Since it inherits from SwiftSyntaxRule, it can build a Visitor (SwiftIdenticalOperandsVisitor) to traverse and check each node of the syntax tree structure of the code file SourceFile. When traversing to the equal sign "=" node InfixOperatorExprSyntax, compare the names of the two nodes on the left and right of the equal sign "=" node to see if they are the same. If they are the same, add the error location, and the upper layer will warn all the error locations at the end.

[0088] The second code detection rule is to check whether the developer has used the abandoned interface UIGraphicsBeginImageContext provided by the system. Because this interface has been officially abandoned, developers are reminded to replace it as soon as possible. If it is forced to use, there will be risks of errors and crashes. Since it is necessary to determine whether the system's UIGraphicsBeginImageContext interface is used, rather than the interface of the same name provided by others, it is necessary to accurately determine the source of the interface, and you need to use the index tree because the index tree contains the source information of the node. Since it inherits from SwiftIndexRule, the index tree is used to traverse and check each node of the current file index tree, and compare the unique identifier of the system UIGraphicsBeginImageContext interface to determine whether the current node is using the system interface. If so, the error location is returned, and the upper layer will warn all the returned locations at the end.

[0089] In this embodiment, a target tree structure that is compatible with any code detection rule is determined based on the detection logic of any code detection rule of the code file; each tree node of the target tree structure that is compatible with any code detection rule is traversed to obtain the node information required for implementing the detection logic of any code detection rule; based on the node information, it is determined whether the code file contains problematic code that does not comply with any code detection rule; in this way, a matching tree structure can be selected based on the detection logic of each code detection rule, thereby achieving efficient detection of the code file.

[0090] In an exemplary embodiment, the method also includes: for any code file, when the code detection result of the code file under any code detection rule indicates that the code file contains problem code that does not comply with any code detection rule, locating the target position of the problem code in the code file; filling the problem description information corresponding to the problem code into the problem error template corresponding to any code detection rule to generate a problem error prompt box; and displaying the problem error prompt box at the target position of the code file.

[0091] Among them, the target position is the error reporting position.

[0092] The problem description information may include the rule type, rule description, rule version, rule level, rule language, source reference, correct writing method, incorrect writing method, and other detailed error information for the problem code. Rule types may include general specifications, security specifications, convention specifications, style specifications, performance specifications, privacy specifications, etc., and rule levels may include warning and error; rule languages ​​may include Objective-C, Swift, etc.

[0093] Among them, the problem error template can be generated when building the code detection rules.

[0094] Optionally, for any code file, if the code detection result of the code file under code detection rule 1 indicates that the code file contains problem code that does not comply with code detection rule 1, the target position of the problem code in the code file is located, and the problem description information corresponding to the problem code is filled into the problem error template corresponding to code detection rule 1, a problem error prompt box is generated, and the problem error prompt box is displayed at the target position of the code file.

[0095] In actual applications, the information on the error prompt box may be: "[TTLint error]ObjCBlockSelfRule (performance specification): To avoid memory leaks, use '_weak', '_strong', '@tt_weakify', '@'tt_strongify' when using self in block".

[0096] In the present embodiment, for any code file, when the code detection result of the code file under any code detection rule indicates that the code file has problem code that does not comply with any code detection rule, the target position of the problem code in the code file is located; the problem description information corresponding to the problem code is filled into the problem error template corresponding to any code detection rule to generate a problem error prompt box; the problem error prompt box is displayed at the target position of the code file; in this way, when the problem code is detected, the problem description information of the problem code can be displayed in detail in the interface, which is conducive to improving the processing efficiency of the problem code and reducing the time for troubleshooting code errors.

[0097] In another embodiment, Figure 3 As shown, a code detection method is provided, which is applied to Figure 1 Taking the server 104 in the example as an example, the following steps are included:

[0098] Step S302, obtaining the code project to be detected.

[0099] Step S304: for any code file in the code project to be detected, a code parsing interface matching the programming language type of the code file is called to obtain a syntax tree structure and an index tree structure corresponding to the code file.

[0100] Step S306, determining various code detection rules for any code file according to the programming language type to which any code file belongs.

[0101] Step S308, for any code file, use the target tree structure that is compatible with each code detection rule of the code file to detect the code file and obtain the code detection result of the code file under each code detection rule; the target tree structure includes one of a syntax tree structure and an index tree structure.

[0102] Step S310, for any code file, when a code detection result of the code file under any code detection rule indicates that the code file has a problem code that does not comply with any code detection rule, locate the target position of the problem code in the code file.

[0103] Step S312, fill the problem description information corresponding to the problem code into the problem error template corresponding to any code detection rule to generate a problem error prompt box.

[0104] Step S314, displaying a problem error prompt box at the target location of the code file.

[0105] It should be noted that the specific limitations of the above steps can refer to the specific limitations of a code detection method above.

[0106] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.

[0107] Based on the same inventive concept, the embodiment of the present application also provides a code detection device for implementing the above-mentioned code detection method. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above-mentioned method, so the specific limitations in one or more code detection device embodiments provided below can refer to the limitations of the code detection method above, and will not be repeated here.

[0108] In an exemplary embodiment, Figure 4 As shown, a code detection device is provided, including: an acquisition module 402, a calling module 404, a determination module 406 and a detection module 408, wherein:

[0109] The acquisition module 402 is used to acquire the code project to be detected;

[0110] The calling module 404 is used to obtain the syntax tree structure and index tree structure corresponding to any code file in the code project to be detected by calling the code parsing interface that matches the programming language type of the code file;

[0111] A determination module 406, for determining each code detection rule for any code file according to the programming language type to which any code file belongs;

[0112] The detection module 408 is used to detect any code file using a target tree structure that is compatible with each code detection rule of the code file to obtain the code detection result of the code file under each code detection rule; the target tree structure includes one of a syntax tree structure and an index tree structure.

[0113] In one of the embodiments, the calling module 404 is specifically used to obtain the libclang library when the programming language type of the code file is Objective-C language; by calling the syntax tree acquisition interface provided by the libclang library, the source code of the code file is recursively traversed to build the syntax tree structure of the code file; by calling the index tree acquisition interface provided by the libclang library, the index tree structure of the code file is built based on the syntax tree structure of the code file.

[0114] In one of the embodiments, the calling module 404 is specifically used to import the SwiftSyntax library when the programming language type of the code file is Swift language, and use the SwiftSyntax library to parse the source code of the code file to obtain the syntax tree structure of the code file; use the SourceKit tool set to build the index tree structure of the code file based on the syntax tree structure of the code file.

[0115] In one of the embodiments, the device further includes: a construction module for determining various code detection requirements for the code project to be detected; for any programming language type, constructing code detection rules for the programming language type under various code detection requirements according to the grammatical rules of the programming language type.

[0116] In one embodiment, the detection module 408 is specifically used to determine a target tree structure that is compatible with any code detection rule based on the detection logic of any code detection rule of the code file; traverse each tree node of the target tree structure that is compatible with any code detection rule to obtain the node information required to implement the detection logic of any code detection rule; based on the node information, determine whether the code file has problematic code that does not comply with any code detection rule.

[0117] In one of the embodiments, the device also includes: a display module, which is used to locate the target position of the problem code in the code file for any code file, when the code detection result of the code file under any code detection rule indicates that the code file contains problem code that does not comply with any code detection rule; fill the problem description information corresponding to the problem code into the problem error template corresponding to any code detection rule to generate a problem error prompt box; and display the problem error prompt box at the target position of the code file.

[0118] Each module in the above-mentioned code detection device can be implemented in whole or in part by software, hardware and a combination thereof. Each of the above-mentioned modules can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute the operations corresponding to each of the above modules.

[0119] In an exemplary embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as shown in FIG. Figure 5 As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, referred to as I / O) and a communication interface. Among them, the processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store code detection data. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a code detection method is implemented.

[0120] Those skilled in the art will understand that Figure 5 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0121] In one embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the above-mentioned code detection method. The steps of the code detection method here may be the steps of the code detection method in each of the above-mentioned embodiments.

[0122] In one embodiment, a computer-readable storage medium is provided, which stores a computer program, and when the computer program is executed by a processor, the processor executes the steps of the above-mentioned code detection method. The steps of the code detection method here can be the steps of the code detection method in each of the above-mentioned embodiments.

[0123] In one embodiment, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, the processor executes the steps of the above-mentioned code detection method. The steps of the code detection method here can be the steps of the code detection method in each of the above-mentioned embodiments.

[0124] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., but are not limited to this.

[0125] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0126] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.

Claims

1. A code detection method, characterized in that: The method comprises: Get the code project to be tested; For any code file in the code project to be detected, a syntax tree structure and an index tree structure corresponding to the code file are obtained by calling a code parsing interface that matches the programming language type of the code file; Determine, according to the programming language type to which any of the code files belongs, each code detection rule for any of the code files; For any of the code files, the code file is detected using a target tree structure that is compatible with each of the code detection rules of the code file to obtain a code detection result for the code file under each of the code detection rules; the target tree structure includes one of the syntax tree structure and the index tree structure.

2. The method according to claim 1, characterized in that The acquiring of the syntax tree structure and the index tree structure corresponding to the code file by calling a code parsing interface that matches the programming language type of the code file comprises: When the programming language type of the code file is Objective-C, obtaining a libclang library; By calling the syntax tree acquisition interface provided by the libclang library, the source code of the code file is recursively traversed to construct a syntax tree structure of the code file; By calling the index tree acquisition interface provided by the libclang library, the index tree structure of the code file is constructed based on the syntax tree structure of the code file.

3. The method according to claim 1, characterized in that The acquiring of the syntax tree structure and the index tree structure corresponding to the code file by calling a code parsing interface that matches the programming language type of the code file comprises: In the case where the programming language type of the code file is Swift language, importing the SwiftSyntax library, and using the SwiftSyntax library to parse the source code of the code file to obtain the syntax tree structure of the code file; The SourceKit tool set is used to construct an index tree structure of the code file based on the syntax tree structure of the code file.

4. The method according to claim 1, characterized in that: The method further comprises: Determine various code detection requirements for the code project to be detected; For any of the programming language types, code detection rules for the programming language type under each of the code detection requirements are constructed according to the grammatical rules of the programming language type.

5. The method according to claim 1, characterized in that The detecting the code file by using the target tree structure adapted to each of the code detection rules of the code file to obtain the code detection result of the code file under each of the code detection rules includes: Determine a target tree structure that is compatible with any of the code detection rules according to the detection logic of any of the code detection rules of the code file; Traversing each tree node of the target tree structure that is compatible with any of the code detection rules, and acquiring node information required for implementing the detection logic of any of the code detection rules; Based on the node information, it is determined whether the code file has problematic code that does not comply with any of the code detection rules.

6. The method according to claim 1, characterized in that The method further comprises: For any of the code files, if a code detection result of the code file under any of the code detection rules indicates that the code file contains problematic code that does not comply with any of the code detection rules, locate the target position of the problematic code in the code file; Fill the problem description information corresponding to the problem code into the problem error template corresponding to any of the code detection rules to generate a problem error prompt box; The problem error prompt box is displayed at the target location of the code file.

7. A code detection device, characterized in that: The device comprises: The acquisition module is used to obtain the code project to be tested; A calling module, configured to obtain a syntax tree structure and an index tree structure corresponding to any code file in the code project to be detected by calling a code parsing interface that matches the programming language type of the code file; A determination module, used to determine each code detection rule for any of the code files according to the programming language type to which any of the code files belongs; A detection module is used to detect any of the code files using a target tree structure that is compatible with each of the code detection rules of the code file, and obtain a code detection result of the code file under each of the code detection rules; the target tree structure includes one of the syntax tree structure and the index tree structure.

8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.

Citation Information

Patent Citations

  • Anti-reinforcement fast detection method of Android platform cloning application program

    CN107622201A

  • Code detection method and device

    CN113760729A

  • Code checking method and device, computer equipment and storage medium

    CN115328487A

  • Code detection method and device

    CN117632714A