Code inspection method and device, computer device and storage medium
Patent Information
- Application Number
- CN202110506380.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-05-10
- Publication Date
- 2026-10-27
- Estimated Expiration
- 2041-05-10
AI Technical Summary
[0002]代码检查是软件开发过程中的重要工作之一,传统技术中,通常先对源码进行编译,再通过代码检查工具(如coverity、infer)基于编译后的代码进行分析检查,编译过程需要编译器(如GCC、Clang)参与,对于中大型项目,编译代码的时间长达分钟至小时级别,影响代码检查效率
[0024]上述代码检查方法、装置、计算机设备和存储介质,当监听到代码文件变更时,获取变更的代码文件作为待检查代码文件;根据待检查代码文件的语言类型,确定待检查代码文件对应的解析脚本和检查规则;通过对应的解析脚本对待检查代码文件进行解析,获得待检查代码文件对应的解析树;根据对应的解析树和检查规则进行匹配检查,获得待检查代码文件的检查结果。据此,无需编译过程,可在代码开发阶段实现代码实时检查,提高代码检查效率,帮助开发人员在编译前及时发现代码问题,缩短开发人员修复问题代码的流程,提高代码修复效率,降低修复问题代码的沟通成本。
Smart Images

Figure CN115328487B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a code inspection method, apparatus, computer device, and storage medium. Background Technology
[0002] Code inspection is a crucial part of the software development process. Traditionally, the source code is compiled first, and then code inspection tools (such as Coverity and Infer) analyze and inspect the compiled code. The compilation process requires a compiler (such as GCC or Clang), and for medium to large projects, compilation time can range from minutes to hours, impacting code inspection efficiency. This traditional approach is only suitable for the automated testing phase of continuous integration. If code issues are exposed during testing, they need to be reported to the development team by testers, who then fix the problematic code, further hindering code repair efficiency. Summary of the Invention
[0003] Therefore, it is necessary to provide a code inspection method, apparatus, computer equipment, and storage medium that can improve the efficiency of code inspection and repair in response to the above-mentioned technical problems.
[0004] A code inspection method, the method comprising:
[0005] When a code file change is detected, the changed code file is obtained as the code file to be inspected;
[0006] Based on the language type of the code file to be checked, determine the corresponding parsing script and checking rules for the code file to be checked;
[0007] The code file to be checked is parsed using the corresponding parsing script to obtain the parsing tree corresponding to the code file to be checked;
[0008] The matching check is performed based on the corresponding parse tree and the checking rules to obtain the check result of the code file to be checked.
[0009] A code inspection apparatus, the apparatus comprising:
[0010] The acquisition module is used to acquire the changed code file as the code file to be inspected when a code file change is detected.
[0011] The determination module is used to determine the parsing script and inspection rules corresponding to the code file to be inspected based on the language type of the code file to be inspected;
[0012] The parsing module is used to parse the code file to be checked using the corresponding parsing script to obtain the parse tree corresponding to the code file to be checked;
[0013] The inspection module is used to perform matching checks based on the corresponding parse tree and the inspection rules to obtain the inspection results of the code file to be inspected.
[0014] A computer device includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program performing the following steps:
[0015] When a code file change is detected, the changed code file is obtained as the code file to be inspected;
[0016] Based on the language type of the code file to be checked, determine the corresponding parsing script and checking rules for the code file to be checked;
[0017] The code file to be checked is parsed using the corresponding parsing script to obtain the parsing tree corresponding to the code file to be checked;
[0018] The matching check is performed based on the corresponding parse tree and the checking rules to obtain the check result of the code file to be checked.
[0019] A computer-readable storage medium having a computer program stored thereon, the computer program performing the following steps when executed by a processor:
[0020] When a code file change is detected, the changed code file is obtained as the code file to be inspected;
[0021] Based on the language type of the code file to be checked, determine the corresponding parsing script and checking rules for the code file to be checked;
[0022] The code file to be checked is parsed using the corresponding parsing script to obtain the parsing tree corresponding to the code file to be checked;
[0023] The matching check is performed based on the corresponding parse tree and the checking rules to obtain the check result of the code file to be checked.
[0024] The aforementioned code inspection method, apparatus, computer equipment, and storage medium, when detecting changes to a code file, acquire the changed code file as the code file to be inspected; determine the corresponding parsing script and inspection rules based on the language type of the code file to be inspected; parse the code file to be inspected using the corresponding parsing script to obtain a parse tree corresponding to the code file to be inspected; and perform matching checks based on the corresponding parse tree and inspection rules to obtain the inspection result of the code file to be inspected. Therefore, without a compilation process, real-time code inspection can be implemented during the code development stage, improving code inspection efficiency, helping developers to discover code problems in a timely manner before compilation, shortening the process for developers to fix problematic code, improving code repair efficiency, and reducing the communication costs of fixing problematic code. Attached Figure Description
[0025] Figure 1 This is a diagram illustrating the application environment of the code inspection method in one embodiment;
[0026] Figure 2 This is a flowchart illustrating a code inspection method in one embodiment;
[0027] Figure 3 This is a schematic diagram illustrating the pop-up notification of error information based on the error level in one embodiment;
[0028] Figure 4 This is a schematic diagram illustrating the pop-up notification of error messages at different warning levels in one embodiment.
[0029] Figure 5 This is a schematic diagram illustrating the error message in one embodiment;
[0030] Figure 6 This is a flowchart illustrating a code inspection method in one embodiment;
[0031] Figure 7 This is a structural block diagram of a code inspection device in one embodiment;
[0032] Figure 8 This is an internal structural diagram of a computer device in one embodiment;
[0033] Figure 9 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation
[0034] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0035] The code inspection method provided in this application can be applied to, for example... Figure 1 In the application environment shown, terminal 102 communicates with server 104 via a network. Users can access the software development platform through terminal 102, and server 104 can be the server hosting the platform. The code inspection method can be applied to terminals with the software development platform installed, to the server hosting the platform, or to systems including both terminals and servers, and is implemented through interaction between the terminal and the server. The server can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms. The terminal can be a smartphone, tablet, laptop, desktop computer, smart speaker, smartwatch, etc., but is not limited to these. The terminal and server can be directly or indirectly connected via wired or wireless communication, which is not limited herein.
[0036] In one embodiment, such as Figure 2 As shown, a code inspection method is provided, which is applied to... Figure 1 The following steps are used as an example of the terminal in the example, including steps S202 to S208.
[0037] S202: When a code file change is detected, the changed code file is obtained as the code file to be inspected.
[0038] While developers are writing code, the terminal can monitor file changes in the project directory in real time through the Application Programming Interface (API). When a code file change is detected, the changed code file is retrieved as the file to be inspected, and code inspection begins. This enables real-time code inspection during the development phase, helping developers to promptly identify and quickly fix problematic code. It's understood that the file to be inspected at this stage is uncompiled; there's no need to compile the source code before inspection, shortening the code inspection and repair process.
[0039] S204. Based on the language type of the code file to be checked, determine the corresponding parsing script and checking rules for the code file to be checked.
[0040] The language type of the code file to be checked refers to the language used by the developer to write the code file to be checked, which may include, but is not limited to, C++, Objective-C and Java.
[0041] In one embodiment, the file extension of the code file to be inspected is obtained, and the language type of the code file is determined based on the file extension. There is a correspondence between file extensions and language types; for example, one language type can correspond to one or more file extensions. The language type of the code file to be inspected can be determined based on its file extension and the aforementioned correspondence. For example, if the file extension is .cpp, the language type can be determined to be C++; if the file extension is .m or .mm, the language type can be determined to be Objective-C; and if the file extension is .java, the language type can be determined to be Java.
[0042] A parsing script is a script used to parse code files to obtain their main syntax information. The information obtained after parsing a code file by a parsing script can contain only the necessary syntax information, such as syntax information related to classes, variables, and methods. Compared with the information obtained by a compiler, it is more concise and helps to improve parsing speed.
[0043] Because different languages have different syntaxes, there will be subtle differences in parsing. Therefore, different parsing scripts need to be written for different language types. After determining the language type of the code file to be checked, the parsing script used to parse the code file can be determined based on the language type of the code file. In one embodiment, the parsing script can be written using the scripting language PHP.
[0044] The inspection rules include the syntax rules that the code file must meet. Since different languages have different syntaxes, different inspection rules need to be written for different language types. After determining the language type of the code file to be inspected, the syntax rules that the code file must meet can be determined based on that language type. In one embodiment, the inspection rules may also include custom business rules. Business rules can be flexibly configured according to business needs. For example, if the business requires all colors to be written according to a specific specification, this rule is not a basic syntax rule and needs to be written by the business side itself.
[0045] S206, the corresponding parsing script is used to parse the code file to be checked and obtain the parse tree corresponding to the code file to be checked.
[0046] The parse tree corresponding to the code file to be checked is a tree-structured representation of the syntax information of the code file to be checked. It is obtained by parsing the code file to be checked using a parsing script. In one embodiment, the parse tree only contains the necessary syntax information and is applicable to all language types. That is, after code files of different language types are parsed by the corresponding parsing script, the structure of the generated parse tree is the same. This flattens some language features into a unified format, making it convenient to use a unified checking script for subsequent code checking.
[0047] S208: Perform a matching check based on the corresponding parse tree and checking rules to obtain the check result of the code file to be checked.
[0048] Matching based on the corresponding parse tree and checking rules refers to checking whether the syntax information in the parse tree conforms to the requirements of the checking rules. The checking rules for the code file to be checked can include multiple rules, specifically a rule list. The rule list is imported into the parse tree, and the checking result for each rule is output. The checking result for the code file to be checked includes the checking results for all rules.
[0049] In the code inspection method described above, when a code file change is detected, the changed code file is obtained as the code file to be inspected. Based on the language type of the code file to be inspected, the corresponding parsing script and inspection rules are determined. The code file to be inspected is parsed using the corresponding parsing script to obtain a parse tree. A matching check is then performed based on the parse tree and inspection rules to obtain the inspection result. Therefore, real-time code inspection can be implemented during the code development phase without a compilation process, improving code inspection efficiency, helping developers identify code problems before compilation, shortening the process of fixing problematic code, improving code repair efficiency, and reducing communication costs associated with fixing problematic code.
[0050] In one embodiment, parsing scripts and checking rules corresponding to various language types are pre-configured, and a first mapping relationship between language types and parsing scripts, and a second mapping relationship between language types and checking rules are established.
[0051] Because different languages have different syntaxes, different parsing scripts and checking rules are written for different language types, thus configuring corresponding parsing scripts and checking rules for each language type. Based on this, code file checking in multiple languages can be supported. Furthermore, code checking rules can be flexibly customized to meet diverse checking needs.
[0052] The first mapping represents the mapping between language types and parsing scripts, with one parsing script corresponding to one language type. The second mapping represents the mapping between language types and checking rules, with one checking rule corresponding to one language type.
[0053] In one embodiment, the step of determining the parsing script and inspection rules corresponding to the code file to be inspected based on the language type of the code file to be inspected may specifically include: determining the parsing script corresponding to the code file to be inspected based on the language type of the code file to be inspected and a first mapping relationship; and determining the inspection rules corresponding to the code file to be inspected based on the language type of the code file to be inspected and a second mapping relationship.
[0054] Based on the language type of the code file to be checked and the first mapping relationship, the parsing script corresponding to the language type of the code file to be checked can be obtained, and this script serves as the parsing script for the code file to be checked. Based on the language type of the code file to be checked and the second mapping relationship, the checking rules corresponding to the language type of the code file to be checked can be obtained, and these rules serve as the checking rules for the code file to be checked.
[0055] In one embodiment, the step of parsing the code file to be checked using a corresponding parsing script to obtain the parse tree corresponding to the code file to be checked may specifically include: extracting the syntax information of the code file to be checked using the corresponding parsing script; and obtaining the parse tree corresponding to the code file to be checked based on the syntax information of the code file to be checked.
[0056] Specifically, regular expressions and string matching are used to extract syntax information from the code file to be inspected. In this embodiment, only necessary syntax information is extracted, including syntax information related to classes, variables, and methods. Taking class definition identification as an example, "@interface RenderModel:NSObject" is an Objective-C class definition. By recognizing the keyword "@interface", this line of statements can be identified as a class definition. Furthermore, the regular expression "@interface(.*):(.*)" can be used to extract the class name and the inherited parent class. The parse tree obtained after parsing the entire code file to be inspected in this way has the following structure:
[0057]
[0058] The definitions of classes, variables, and methods are as follows:
[0059]
[0060]
[0061] In this embodiment, parsing the code file to be checked using a parsing script improves parsing speed. Parsing a 1000-line code file takes only about 100 milliseconds in this embodiment, while a compiler takes about 10-30 seconds. Furthermore, the parse tree generated in this embodiment is more suitable for code inspection, which primarily checks for erroneous syntax. In most cases, feature values can be extracted using regular expressions, requiring only auxiliary checks on classes, variables, and methods. In contrast, the abstract syntax tree output by the compiler contains considerably redundant syntax information, and traversing this information takes much longer. The parse tree generated in this embodiment contains more concise information, allowing for faster inspection.
[0062] In one embodiment, the step of performing a matching check based on the corresponding parse tree and checking rules to obtain the check result of the code file to be checked may specifically include: for each rule in the checking rules, extracting the grammatical features corresponding to the rule in the parse tree, matching the feature values of the grammatical features with the rule to obtain the check result corresponding to the rule; and obtaining the check result of the code file to be checked based on the check results corresponding to all rules in the checking rules.
[0063] The parse tree contains syntactic information related to classes, variables, and methods. Classes, variables, and methods represent syntactic features, and each syntactic feature can correspond to multiple feature values. Taking variables as an example, the corresponding feature values can include the variable's code location, name, type, and modifiers. Similarly, taking methods as an example, the corresponding feature values can include the method's code location, name, method type, return type, parameter list, and method content.
[0064] Each rule contains information related to grammatical features and feature values. For each rule, based on the grammatical feature information in the rule, grammatical features that match the grammatical feature information are obtained from the parse tree. Based on the matched grammatical features, grammatical information with the grammatical features is obtained from the parse tree. The feature values in the grammatical information are matched with the feature value information in the rule to determine whether the feature values in the grammatical information meet the requirements of the feature value information in the rule, thereby obtaining the check result corresponding to the rule.
[0065] The inspection rules can include general rules and business rules. General rules refer to common syntax rules, while business rules refer to custom rules based on business requirements. There can be multiple general rules and business rules. Each rule is matched and checked against the parse tree until all rules have been checked. The inspection results corresponding to all rules are then merged to obtain the inspection result of the code file to be inspected.
[0066] The general rule takes "method content line count < 100" as an example. Its corresponding syntax feature is "method", and the feature value is the number of lines of the method content. Using regular expressions and string matching, all methods in the class are extracted. The number of newline characters in the method content is calculated, and it is checked whether the number of newline characters is less than 100. If the number of newline characters is not less than 100, the rule check is considered to have failed, and the check result is output as an error. The above check steps are continued until all method content has been checked.
[0067] The business rule takes "NSString modifier incorrectly used strong" as an example. Its corresponding syntax feature is variable, and the feature value is variable modifier. Regular expressions and string matching are used to extract all variables in the class, filter out variables of type NSString, and check whether the variable modifier is strong. If the variable modifier is strong, the rule check is considered to have failed, and the check result is output as an error. The above check continues until all variables of type NSString have been checked.
[0068] In one embodiment, when the inspection result of the code file to be inspected contains error information, an alarm is triggered for the error information.
[0069] Error messages can be alerted, specifically by displaying error information to prompt developers to fix them. In one embodiment, the inspection rule has an error level attribute, categorized as Info, Warning, and Error, providing different alerts based on the error level. For example, ... Figure 3 and Figure 4 As shown, error messages with a warning level or higher will be displayed in a pop-up window. Figure 5 As shown, error messages of all levels will be displayed in a fixed window.
[0070] In one embodiment, such as Figure 6 As shown, a code inspection method is provided, including the following steps S601 to S606.
[0071] S601: When a code file change is detected, the changed code file is obtained as the code file to be inspected.
[0072] S602, determine the parsing script and inspection rules corresponding to the code file to be inspected based on the language type of the code file to be inspected.
[0073] S603 extracts the syntax information of the code file to be checked through the corresponding parsing script, and obtains the parse tree corresponding to the code file to be checked based on the syntax information of the code file to be checked.
[0074] S604. For each rule in the inspection rules, extract the grammatical features corresponding to the rule from the parse tree, match the feature values of the grammatical features with the rule, and obtain the inspection result corresponding to the rule.
[0075] S605: Based on the inspection results corresponding to all rules in the inspection rules, obtain the inspection results of the code file to be inspected.
[0076] S606: When the inspection result of the code file to be inspected contains error information, an alarm is triggered for the error information.
[0077] For specific limitations on steps S601 to S606, please refer to the above embodiment, which will not be repeated here. In this embodiment, no compilation process is required, and real-time code inspection can be achieved during the code development stage, improving code inspection efficiency, helping developers to discover code problems in time before compilation, shortening the process for developers to fix problematic code, improving code repair efficiency, and reducing the communication cost of fixing problematic code.
[0078] It should be understood that although the steps in the flowcharts of the above embodiments are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the above embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages in other steps.
[0079] In one embodiment, such as Figure 7 As shown, a code inspection device 700 is provided. This device can be a software module, a hardware module, or a combination of both as part of a computer device. Specifically, the device includes: an acquisition module 710, a determination module 720, a parsing module 730, and an inspection module 740, wherein:
[0080] The acquisition module 710 is used to acquire the changed code file as the code file to be inspected when a code file change is detected.
[0081] The determination module 720 is used to determine the parsing script and inspection rules corresponding to the code file to be inspected based on the language type of the code file to be inspected.
[0082] The parsing module 730 is used to parse the code file to be checked using the corresponding parsing script to obtain the parse tree corresponding to the code file to be checked.
[0083] The inspection module 740 is used to perform matching checks based on the corresponding parse tree and inspection rules to obtain the inspection results of the code file to be inspected.
[0084] In one embodiment, the determining module 720 is further configured to: obtain the file extension of the code file to be inspected, and determine the language type of the code file to be inspected based on the file extension.
[0085] In one embodiment, the device further includes a configuration module for pre-configuring parsing scripts and checking rules corresponding to various language types, and establishing a first mapping relationship between language types and parsing scripts, and a second mapping relationship between language types and checking rules.
[0086] In one embodiment, when determining the parsing script and inspection rules corresponding to the code file to be inspected based on the language type of the code file to be inspected, the determining module 720 is specifically used to: determine the parsing script corresponding to the code file to be inspected based on the language type of the code file to be inspected and the first mapping relationship; and determine the inspection rules corresponding to the code file to be inspected based on the language type of the code file to be inspected and the second mapping relationship.
[0087] In one embodiment, when the parsing module 730 parses the code file to be checked using the corresponding parsing script to obtain the parse tree corresponding to the code file to be checked, it is specifically used to: extract the syntax information of the code file to be checked using the corresponding parsing script; and obtain the parse tree corresponding to the code file to be checked based on the syntax information of the code file to be checked.
[0088] In one embodiment, when the inspection module 740 performs matching checks based on the corresponding parse tree and inspection rules to obtain the inspection result of the code file to be inspected, it specifically performs the following: for each rule in the inspection rules, it extracts the grammatical features corresponding to the rule from the parse tree, matches the feature values of the grammatical features with the rule, and obtains the inspection result corresponding to the rule; and obtains the inspection result of the code file to be inspected based on the inspection results corresponding to all rules in the inspection rules.
[0089] In one embodiment, the device further includes an alarm module for issuing an alarm when the inspection result of the code file to be inspected contains error information.
[0090] For specific limitations regarding the code inspection device, please refer to the limitations on the code inspection method above, which will not be repeated here. Each module in the aforementioned code inspection device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in hardware or independently of the processor in the computer device, or stored in software in the memory of the computer device, so that the processor can call and execute the operations corresponding to each module.
[0091] In one embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows: Figure 8 As shown, the computer device includes a processor, memory, and a network interface connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and databases. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The network interface is used to communicate with external terminals via a network connection. When the computer program is executed by the processor, it implements a code inspection method.
[0092] In one embodiment, a computer device is provided, which may be a terminal, and its internal structure diagram may be as follows: Figure 9 As shown, the computer device includes a processor, memory, communication interface, display screen, and input devices connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, carrier networks, NFC (Near Field Communication), or other technologies. When the computer program is executed by the processor, it implements a code inspection method. The display screen can be an LCD screen or an e-ink screen. The input devices can be a touch layer covering the display screen, buttons, a trackball, or a touchpad on the computer device's casing, or an external keyboard, touchpad, or mouse.
[0093] Those skilled in the art will understand that Figure 8 or Figure 9 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0094] In one embodiment, a computer device is also provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps in the above method embodiments.
[0095] In one embodiment, a computer-readable storage medium is provided storing a computer program that, when executed by a processor, implements the steps in the above method embodiments.
[0096] In one embodiment, a computer program product or computer program is provided, the computer program product or computer program including computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and executes the computer instructions, causing the computer device to perform the steps in the above method embodiments.
[0097] It should be understood that the terms "first," "second," etc., used in the above embodiments are for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Furthermore, in the description of this application, unless otherwise stated, "a plurality of" means at least two.
[0098] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the methods described above. Any references to memory, storage, databases, or other media 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, or optical storage, etc. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc.
[0099] The technical features of the above embodiments can be combined in any way. For the sake of brevity, 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.
[0100] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the invention patent. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this patent application should be determined by the appended claims.
Claims
1. A code inspection method, characterized in that, The method includes: When a code file change is detected, the changed code file is obtained as the code file to be inspected; Based on the language type of the code file to be checked, the corresponding parsing script and checking rules are determined; the parsing script is used to parse the code file to be checked to obtain necessary syntax information; the necessary syntax information includes syntax information related to classes, variables, and methods; Using the corresponding parsing script, the syntax information of the code file to be checked is extracted by regular expressions and string matching. Based on the extracted syntax information, a parse tree corresponding to the code file to be checked is obtained. The parse tree is used to express the syntax information of the code file to be checked in a tree structure. The structure of the parse tree generated after parsing code files of different language types by the corresponding parsing script is the same. For each rule in the inspection rules, extract the grammatical features corresponding to the rule from the parse tree, match the feature values of the grammatical features with the rule, and obtain the inspection result corresponding to the rule; Based on the inspection results corresponding to all rules in the inspection rules, the inspection result of the code file to be inspected is obtained.
2. The method according to claim 1, characterized in that, Also includes: Obtain the file extension of the code file to be inspected, and determine the language type of the code file to be inspected based on the file extension.
3. The method according to claim 1, characterized in that, Also includes: Pre-configure parsing scripts and checking rules for various language types, and establish a first mapping relationship between language types and parsing scripts, as well as a second mapping relationship between language types and checking rules.
4. The method according to claim 3, characterized in that, Based on the language type of the code file to be checked, determine the corresponding parsing script and checking rules for the code file to be checked, including: Based on the language type of the code file to be checked and the first mapping relationship, determine the parsing script corresponding to the code file to be checked; Based on the language type of the code file to be checked and the second mapping relationship, the corresponding inspection rules for the code file to be checked are determined.
5. The method according to any one of claims 1 to 4, characterized in that, Also includes: When the inspection result of the code file to be inspected contains error information, an alarm is issued for the error information.
6. A code checking device, characterized in that, The device includes: The acquisition module is used to acquire the changed code file as the code file to be inspected when a code file change is detected. The determination module is used to determine the parsing script and inspection rules corresponding to the code file to be inspected based on the language type of the code file to be inspected; the parsing script is used to parse the code file to be inspected to obtain necessary syntax information; the necessary syntax information includes syntax information related to classes, variables, and methods; The parsing module is used to extract the syntax information of the code file to be checked by using regular expressions and string matching through the corresponding parsing script, and to obtain the parse tree corresponding to the code file to be checked based on the extracted syntax information; the parse tree is used to express the syntax information of the code file to be checked in a tree structure; the structure of the parse tree generated after parsing code files of different language types by the corresponding parsing script is the same; The inspection module is used to extract the grammatical features corresponding to the rule in the parse tree for each rule in the inspection rules, match the feature values of the grammatical features with the rule, and obtain the inspection result corresponding to the rule; and obtain the inspection result of the code file to be inspected based on the inspection results corresponding to all rules in the inspection rules.
7. The apparatus according to claim 6, characterized in that, The determining module is further configured to: obtain the file extension of the code file to be inspected, and determine the language type of the code file to be inspected based on the file extension of the code file to be inspected.
8. The apparatus according to claim 6, characterized in that, The device further includes a configuration module for: pre-configuring parsing scripts and checking rules corresponding to various language types, and establishing a first mapping relationship between language types and parsing scripts, and a second mapping relationship between language types and checking rules.
9. The apparatus according to claim 8, characterized in that, The determining module is specifically used for: Based on the language type of the code file to be checked and the first mapping relationship, determine the parsing script corresponding to the code file to be checked; Based on the language type of the code file to be checked and the second mapping relationship, the corresponding inspection rules for the code file to be checked are determined.
10. The apparatus according to any one of claims 6 to 9, characterized in that, The device further includes an alarm module, used to: issue an alarm for the error information when the inspection result of the code file to be inspected contains error information.
11. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 5.
12. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Method and apparatus for detecting front-end code quality
CN109032949A
Method and device for detecting defects of program source file
CN109857641A