A code verification method and device, equipment, storage medium and program product
By parsing and annotating the code, constructing a logical structure, and adding attribute information, the problem of insufficient flexibility in code inspection is solved. This enables comprehensive code inspection without compilation, improving the flexibility and scalability of code inspection.
Patent Information
- Application Number
- CN202210145005.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-17
- Publication Date
- 2026-11-24
- Estimated Expiration
- 2042-02-17
AI Technical Summary
The flexibility of code verification in existing technologies is relatively low, mainly due to the large number of call dependencies during syntax analysis in the compilation environment, which leads to insufficient flexibility.
By parsing and annotating the code to be tested, a logical structure is constructed and attribute information, including object type, inheritance relationship and call relationship, static analysis is performed using testing rules to avoid compilation dependencies and improve flexibility.
It enables comprehensive code inspection without compilation, improving the flexibility and scalability of code inspection, reducing reliance on compilation dependencies, and enhancing the flexibility of code inspection.
Smart Images

Figure CN116661792B_ABST
Abstract
Description
Technical Field
[0001] This application relates to computer technology, and more particularly to a code verification method, apparatus, device, storage medium, and program product. Background Technology
[0002] Validating written code is a common practice in software development. Validating code clarifies its structure, variable definitions, function call relationships, and facilitates the discovery of defects and errors, as well as the validity of its syntax. In related technologies, code validation typically involves compiling the code in its own compilation environment for syntax analysis. However, the numerous dependencies inherent in the compilation environment limit the flexibility of code validation. Summary of the Invention
[0003] This application provides a code verification method, apparatus, device, computer-readable storage medium, and program product, which can improve the flexibility of code verification.
[0004] The technical solution of this application embodiment is implemented as follows:
[0005] This application provides a code verification method, including:
[0006] The code to be tested is parsed and annotated to obtain a code annotation sequence;
[0007] The code annotation sequence is logically structured to obtain the logical structure of the code to be tested; the logical structure describes the execution order and logical relationships of the code to be tested.
[0008] Add corresponding attribute information to the logical structure to obtain the relationship information to be tested corresponding to the code to be tested; the attribute information includes at least the object type, inheritance relationship and calling relationship in the code to be tested.
[0009] The code to be tested is tested using the relationship information to be tested and the obtained testing rules to obtain the testing results.
[0010] This application provides a code verification device, including:
[0011] The parsing and annotation module is used to parse and annotate the code to be tested, resulting in a code annotation sequence;
[0012] The structure construction module is used to construct the logical structure of the code annotation sequence to obtain the logical structure of the code to be tested; the logical structure describes the execution order and logical association of the code to be tested.
[0013] An attribute adding module is used to add corresponding attribute information to the logical structure to obtain the relationship information to be tested corresponding to the code to be tested; the attribute information includes at least the object type, inheritance relationship and calling relationship in the code to be tested.
[0014] The result generation module is used to examine the code to be examined using the relationship information to be examined and the obtained examination rules, and to obtain the examination result.
[0015] In some embodiments of this application, the structure construction module is further configured to divide the code annotation sequence according to line units to obtain the line object corresponding to the code to be tested; and to perform logical structure association on the line object to obtain the logical structure of the code to be tested.
[0016] In some embodiments of this application, the code annotation sequence includes: multiple annotated code blocks; the structure construction module is further configured to perform the following processing on each of the multiple annotated code blocks: perform type judgment on the annotated code block to obtain a judgment result; the judgment result indicates whether the annotated code block is an end symbol; when the judgment result indicates that the annotated code block is not the end symbol, add the annotated code block to a temporary code sequence, and determine the next code block of the annotated code block as a new annotated code block, and continue to perform type judgment; when the judgment result indicates that the annotated code block is the end symbol, use the temporary code sequence and the annotated code block to generate the line object of the code to be checked.
[0017] In some embodiments of this application, the structure construction module is further configured to use the symbol information in the temporary code sequence to determine the line level corresponding to the temporary code sequence; when the line level is the same as the level threshold, the temporary code sequence and the marked code block are concatenated to form the line object of the code to be tested.
[0018] In some embodiments of this application, the structure construction module is further configured to determine the type information of the row object based on the end character in the row object; merge the row object into the corresponding target structure according to the type information; merge the target structure into the corresponding parent structure, and determine the parent structure as the new target structure, and continue to merge structures until the logical structure of the code to be tested is obtained when the structure merging is completed.
[0019] In some embodiments of this application, the structure construction module is further configured to: create the target structure for the line object when the type information indicates that the line object is a starting line, and merge the line object into the target structure; and when the type information indicates that the line object is an ending line or a statement line, search for the corresponding target structure from the already created structures for the line object, and merge the line object into the target structure.
[0020] In some embodiments of this application, the created structure includes: a class structure, a function structure, and a condition structure; the structure construction module is further configured to find the parent structure to which the row object belongs from the class structure, the function structure, and the condition structure, and determine the parent structure to which the row object belongs as the target structure.
[0021] In some embodiments of this application, the attribute adding module is further configured to: classify the object content of the row object in the logical structure to obtain the object type of the row object; parse the function structure in the logical structure to obtain the function structure call relationship; parse the class structure in the logical structure to obtain the class structure inheritance relationship; determine the object information of the row object, the function structure call relationship, and the class structure inheritance relationship as the attribute information corresponding to the code to be tested, and add the attribute information to the logical structure to obtain the test relationship information corresponding to the code to be tested.
[0022] In some embodiments of this application, the code verification device further includes: a definition query module;
[0023] The definition query module is used to extract reference pointers from the variable objects in the relationship information to be tested; and to iteratively obtain the definition information of the variable objects through the reference pointers.
[0024] In some embodiments of this application, the definition query module is further configured to obtain the initial definition of the variable object based on the reference pointer; when the initial definition is a string type or an array type, the initial definition is determined as the definition information of the variable object; when the initial definition is a variable type or a constant type, the scope of the variable object is traversed to obtain the definition information of the variable object; when the initial definition is a function call return type, the return cell line of the function is traversed to obtain the definition information of the variable object; when the initial definition is an iterator type, the key-value pairs of the variable type in the initial definition are determined as the definition information of the variable object.
[0025] This application provides a code verification device, including:
[0026] Memory, used to store executable instructions;
[0027] The processor, when executing executable instructions stored in the memory, implements the code verification method provided in the embodiments of this application.
[0028] This application provides a computer-readable storage medium storing executable instructions for implementing the code verification method provided in this application when executed by a processor.
[0029] This application provides a computer program product, including a computer program or instructions, which, when executed by a processor, implement the code verification method provided in this application.
[0030] The embodiments of this application have the following beneficial effects: The code verification device first uses the code annotation sequence of the code to be verified to determine the static logical structure of the code to be verified, so as to clarify the internal execution order and logical relationship of the code to be verified. Then, it adds attribute information such as variable definition, inheritance relationship and call relationship to the logical structure. By statically completing the analysis of the code to be verified, the verification relationship information describing all the information in the code to be verified can be obtained. It is best to use the verification rules to verify the verification relationship information, so that the code to be verified does not need to be compiled, and there is no need to load the compilation dependencies. This decouples code verification from compilation dependencies and improves the flexibility of code verification. Attached Figure Description
[0031] Figure 1 This is a schematic diagram of the architecture of the code verification system provided in the embodiments of this application;
[0032] Figure 2 This is a schematic diagram of the server structure provided in an embodiment of this application;
[0033] Figure 3 This is a flowchart illustrating a code verification method provided in an embodiment of this application;
[0034] Figure 4 This is another flowchart illustrating the code verification method provided in the embodiments of this application;
[0035] Figure 5 This is another flowchart illustrating the code verification method provided in the embodiments of this application;
[0036] Figure 6 This is the process of constructing the static relational library provided in the embodiments of this application;
[0037] Figure 7This is a schematic diagram of the process for constructing a row object according to an embodiment of this application;
[0038] Figure 8 This is a flowchart illustrating the construction logic structure provided in an embodiment of this application;
[0039] Figure 9 This is a schematic diagram illustrating the process of querying variable definitions provided in an embodiment of this application;
[0040] Figure 10 This is a schematic diagram of the iterative query variable definition process provided in the embodiments of this application. Detailed Implementation
[0041] To make the objectives, technical solutions, and advantages of this application clearer, the application will be further described in detail below with reference to the accompanying drawings. The described embodiments should not be regarded as limitations on this application. All other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0042] In the following description, references are made to “some embodiments,” which describe a subset of all possible embodiments. However, it is understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.
[0043] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used herein is for the purpose of describing embodiments of this application only and is not intended to limit this application.
[0044] Before providing a further detailed description of the embodiments of this application, the nouns and terms involved in the embodiments of this application will be explained, and the nouns and terms involved in the embodiments of this application shall be interpreted as follows.
[0045] 1) The relationship information to be verified is the library object constructed after the code is structured. This library object contains the project code syntax structure, class function inheritance relationship, statement line definition type and call pointer, etc.
[0046] 2) A structure is a structural object generated from the relational information to be tested. Function code blocks, class code blocks, etc., within the relational information to be tested are all stored in the form of structures. Structures directly contain each other; for example, a class structure can contain multiple function structures, and a function structure can contain multiple conditional logic structures.
[0047] 3) Line objects are units of code separated by syntax statements. Unlike code blocks separated by newline characters, an array defined across multiple lines will be identified as a single complete line object, while multiple variable declarations sharing a single line will be identified as multiple distinct line objects.
[0048] 4) Static analysis, which is the scanning of code without compiling or executing it, is generally used to discover key information in the code or to analyze code problems.
[0049] 5) Type annotation tool (Pygemts), a tool that performs code syntax annotation for various languages (such as PHP, C++, Java, etc.). The type annotation tool can annotate the keywords in the code with type.
[0050] This application provides a code verification method, apparatus, device, computer-readable storage medium, and program product, which can improve the flexibility of code detection. The exemplary application of the code verification device provided in this application is described below. The code verification device provided in this application can be implemented as various types of terminals such as laptops, tablets, desktop computers, set-top boxes, and mobile devices (e.g., mobile phones, portable music players, personal digital assistants, dedicated messaging devices, portable gaming devices), or as a server, or as a cluster of devices composed of terminals and servers. The exemplary application of the code verification device as a server will be described below.
[0051] See Figure 1 , Figure 1 This is a schematic diagram of the architecture of the code verification system provided in the embodiments of this application. In order to support a code verification application, terminal 400 (terminal 400-1 and terminal 400-2 are shown as examples) connects to server 200 through network 300. Network 300 can be a wide area network or a local area network, or a combination of the two.
[0052] Terminal 400-1 is used to receive operations from R&D personnel on the graphical interface 410-1, generate code to be tested, and send the code to be tested to the server 200.
[0053] Server 200 is used to parse the code to be inspected to obtain a code annotation sequence; construct a logical structure from the code annotation sequence to obtain a logical structure of the code to be inspected; the logical structure describes the logical relationships between code blocks within the code to be inspected; add corresponding attribute information to the logical structure to obtain the relationship information to be inspected corresponding to the code to be inspected; and inspect the code to be inspected using the relationship information to be inspected and the obtained inspection rules to obtain the inspection result.
[0054] Server 200 is also used to send the test results to terminal 400-2 via network 300.
[0055] Terminal 400-2 is used to display test results on graphical interface 410-2 so that staff can perform analysis.
[0056] In some embodiments, server 200 may 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. Terminal 400 may be a smartphone, tablet computer, laptop computer, desktop computer, smart speaker, smartwatch, in-vehicle terminal, smart home appliance, etc., but is not limited to these. Terminals and servers can be directly or indirectly connected via wired or wireless communication, which is not limited in this embodiment of the invention.
[0057] See Figure 2 , Figure 2 This is provided by the embodiments of this application. Figure 1 A schematic diagram of the structure of the server. Figure 2 The server 200 shown includes at least one processor 210, memory 250, at least one network interface 220, and a user interface 230. The various components in server 200 are coupled together via a bus system 240. It is understood that the bus system 240 is used to implement communication between these components. In addition to a data bus, the bus system 240 also includes a power bus, a control bus, and a status signal bus. However, for clarity, ... Figure 2 The general labeled all buses as Bus System 240.
[0058] Processor 210 can be an integrated circuit chip with signal processing capabilities, such as a general-purpose processor, a digital signal processor (DSP), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Among them, the general-purpose processor can be a microprocessor or any conventional processor, etc.
[0059] User interface 230 includes one or more output devices 231 that enable the presentation of media content, including one or more speakers and / or one or more visual displays. User interface 230 also includes one or more input devices 232, including user interface components that facilitate user input, such as a keyboard, mouse, microphone, touch screen display, camera, other input buttons and controls.
[0060] The memory 250 may be removable, non-removable, or a combination thereof. Exemplary hardware devices include solid-state storage, hard disk drives, optical disk drives, etc. The memory 250 may optionally include one or more storage devices physically located away from the processor 210.
[0061] Memory 250 may include volatile memory or non-volatile memory, or both. Non-volatile memory may be read-only memory (ROM), and volatile memory may be random access memory (RAM). The memory 250 described in this application embodiment is intended to include any suitable type of memory.
[0062] In some embodiments, memory 250 is capable of storing data to support various operations, examples of which include programs, modules, and data structures or subsets or supersets thereof, as illustrated below.
[0063] Operating system 251 includes system programs for handling various basic system services and performing hardware-related tasks, such as the framework layer, core library layer, driver layer, etc., for implementing various basic business functions and handling hardware-based tasks;
[0064] The network communication module 252 is used to reach other computing devices via one or more (wired or wireless) network interfaces 220, such as Bluetooth, Wi-Fi, and Universal Serial Bus (USB).
[0065] Presentation module 253 is configured to enable the presentation of information (e.g., a user interface for operating peripheral devices and displaying content and information) via one or more output devices 231 (e.g., a display screen, a speaker, etc.) associated with user interface 230;
[0066] The input processing module 254 is used to detect and translate one or more user inputs or interactions from one or more input devices 232.
[0067] In some embodiments, the apparatus provided in this application can be implemented in software. Figure 2A code verification device 255 stored in memory 250 is shown. This device can be software in the form of programs or plug-ins, and includes the following software modules: a parsing and annotation module 2551, a structure construction module 2552, an attribute addition module 2553, a result generation module 2554, and a definition query module 2555. These modules are logically connected and can therefore be arbitrarily combined or further separated according to their implemented functions. The functions of each module will be described below.
[0068] In other embodiments, the code verification device provided in this application can be implemented in hardware. As an example, the code verification device provided in this application can be a processor in the form of a hardware decoding processor, which is programmed to execute the code verification method provided in this application. For example, the processor in the form of a hardware decoding processor can be one or more application-specific integrated circuits (ASICs), DSPs, programmable logic devices (PLDs), complex programmable logic devices (CPLDs), field-programmable gate arrays (FPGAs), or other electronic components.
[0069] In some embodiments, the code verification device can implement the code verification method provided in this application by running a computer program. For example, the computer program can be a native program or software module in an operating system; it can be a native application (APP), i.e., a program that needs to be installed in the operating system to run, such as a code scanning APP; it can also be a mini-program, i.e., a program that only needs to be downloaded to a browser environment to run; or it can be a mini-program that can be embedded in any APP. In short, the above-mentioned computer program can be any form of application, module, or plugin.
[0070] The embodiments of this application can be applied to various scenarios such as cloud technology, artificial intelligence, smart transportation, and vehicle-mounted systems. Below, the code verification method provided by the embodiments of this application will be described in conjunction with exemplary applications and implementations of the code verification equipment provided in the embodiments of this application.
[0071] See Figure 3 , Figure 3 This is a flowchart illustrating a code verification method provided in an embodiment of this application, which will be combined with... Figure 3 The steps shown are explained.
[0072] S101. The code to be tested is parsed to obtain the code annotation sequence.
[0073] This application embodiment is implemented in the scenario of verifying written project code. The project can be any application software, service architecture, etc. The code verification device identifies the written project code as the code to be verified, and then parses the code to be verified to divide it into code blocks. Then, it marks the code blocks with key information, and the marked code blocks are determined as the code annotation sequence.
[0074] It is understood that the code to be tested can be written in Java, PHP, or C / C++, and this application does not specifically limit it.
[0075] It should be noted that parsing the code to be inspected refers to extracting keywords, custom statements, symbols, etc., from the code to be inspected, resulting in multiple different code blocks. The code inspection equipment then annotates key information for each of these different code blocks, producing a code annotation sequence upon completion.
[0076] For example, when the code to be verified is "define('CORRCET_CODE',0);", the code annotation sequence can be (Token.Builtin, 'define')(Token.Punctuation, '(')(Token n.String.Single, "CORRECT_CODE"))(Token.Punctuation, ',')(Token.Text, '-')(Token.Number.Integer, '0')(Token.Punctuation, ');').
[0077] S102. Construct the logical structure of the code annotation sequence to obtain the logical structure of the code to be tested.
[0078] After obtaining the code annotation sequence, the code verification device constructs a logical structure from the code annotation sequence to clarify the execution order of each statement in the code to be verified, as well as the logical relationships between the statements. It then constructs a structure that represents this order and logical relationships, using this constructed structure as the logical structure of the code to be verified. In other words, in this embodiment, the logical structure describes the execution order and logical relationships of the code to be verified. For example, it describes which functions are contained in the classes of the code to be verified, which conditional logic statements are contained in the functions, which statements are executed first, which statements are executed later, etc., so that the execution logic of the code to be verified can be clarified from the logical structure.
[0079] In some embodiments, the code verification device analyzes each annotated code block in the code annotation sequence to determine which code blocks belong to the same line unit, and then segments the code annotation sequence according to different line units. Next, the code verification device uses the segmented line objects to determine the logical structure of the code annotation sequence.
[0080] In other embodiments, the code verification device can also match the code annotation sequence with the annotation sequences corresponding to multiple preset structures, and determine the preset structure corresponding to the matched annotation sequence as the logical structure of the code to be verified.
[0081] S103. Add corresponding attribute information to the logical structure to obtain the relationship information to be tested corresponding to the code to be tested.
[0082] After determining the logical structure of the code to be inspected, the code verification equipment also identifies information such as variable definitions, function call relationships, and inheritance relationships between classes from the code annotation sequence. This information is added to the logical structure as attribute information to obtain the relationship information to be inspected. In other words, the attribute information includes at least the object types, inheritance relationships, and call relationships in the code to be inspected.
[0083] It is understandable that the relationship information to be tested can provide the static relationship of all information (logical relationships, variable definitions, and other attributes) of the code to be tested, so that the code to be tested can be tested without running or compiling it.
[0084] S104. Using the relationship information to be tested and the obtained testing rules, test the code to be tested and obtain the test results.
[0085] The code verification equipment retrieves pre-set verification rules from its own storage space, or obtains verification rules input by staff through external devices such as a mouse and keyboard. Then, the code verification equipment uses the verification rules to match or verify the relational information to be verified, so as to perform logical verification of the syntax in the code to be verified, or retrieve variable definitions for key fields in the code to be verified, etc., thereby obtaining the verification result of the code to be verified.
[0086] It should be noted that, in the embodiments of this application, the verification rules can be general variable definition acquisition scripts or language verification rules written by staff themselves. This embodiment of the application does not make specific limitations here.
[0087] Understandably, compared to related technologies that typically involve compiling code and performing syntax analysis within a code compilation environment, in this embodiment, the code verification device first uses the code annotation sequence of the code to be verified to determine the static logical structure of the code, thus clarifying the internal execution order and logical relationships of the code. Then, it adds attribute information such as variable definitions, inheritance relationships, and call connections to the logical structure, statically completing the analysis of the code to be verified. This allows for the acquisition of verification relationship information describing all information within the code. Ideally, verification rules are used to verify this relationship information, eliminating the need to compile the code and load compilation dependencies. This decouples code verification from compilation dependencies, improving the flexibility of code verification. Furthermore, in this embodiment, the verification rules can be written or set by the staff, resulting in greater flexibility and thus greater scalability of the code verification process.
[0088] based on Figure 3 See Figure 4 , Figure 4 This is another flowchart illustrating the code verification method provided in this application. In some embodiments of this application, the logical structure of the code annotation sequence is constructed to obtain the logical structure of the code to be verified, i.e., the specific implementation process of S102 may include: S1021-S1022, as follows:
[0089] S1021. Divide the code annotation sequence into line units to obtain the line object corresponding to the code to be tested.
[0090] The code verification device segments the code annotation sequence according to line units, and each segment results in an independent line object. In this way, multiple distinct line objects of the code to be verified can be obtained.
[0091] S1022. Perform logical structure association on the row objects to obtain the logical structure of the code to be tested.
[0092] The code verification device will then determine the logical structure of the obtained line objects and merge the line objects into the parent structure through logical structure association. Then, it will merge the structure into the even higher-level structure, and so on, until a complete structure is obtained. This structure is the logical structure of the code to be verified.
[0093] In this embodiment, the code verification device first traverses the code annotation sequence to generate all line objects of the code to be verified, and then, based on the line objects, begins a logical merging process to obtain a complete logical structure of the code to be verified, so as to facilitate the subsequent generation of the relationship information to be verified based on the logical structure.
[0094] In some embodiments of this application, the code annotation sequence includes multiple annotated code blocks. In this case, the code annotation sequence is divided according to line units to obtain the specific implementation process of the line object corresponding to the code to be checked, i.e., the process of S1021. This can be achieved by performing the following processing on the multiple annotated code blocks respectively:
[0095] S1021a. Perform type judgment on the marked code block and obtain the judgment result.
[0096] The code verification device performs type judgment on each marked code block to determine whether the marked code block is the statement terminator in the code to be verified. Thus, the judgment result indicates whether the marked code block is a terminator.
[0097] Understandably, the closing character can be "{", "}", or ";", meaning the code annotation device determines whether the annotated code block is any one of "{", "}", or ";" to obtain the judgment result.
[0098] S1021b: When the judgment result indicates that the marked code block is not the end symbol, the marked code block is added to the temporary code sequence, and the next code block of the marked code block is determined as the new marked code block, and the type judgment continues.
[0099] When the judgment result indicates that the marked code block is not the end symbol, it means that the marked code block is still part of the line unit, that is, the line unit has not ended at the marked code block. Therefore, the code verification device will add the marked code block to the temporary code sequence and start the type judgment process for the next code block of the marked code block, so as to determine whether the line unit has ended through the new judgment result.
[0100] In other words, the temporary code sequence stores different marked code blocks that the code verification device determines belong to the same line unit. Before the code verification device determines that the marked code block is the end character, it will continuously add code blocks to the temporary code sequence until the code verification device obtains the end character.
[0101] S1021c When the judgment result indicates that the marked code block is the end symbol, the line object of the code to be checked is generated using the temporary code sequence and the marked code block.
[0102] When the judgment result indicates that the marked code block is the end symbol, it is determined that all marked code blocks of the line unit have been stored in the temporary code sequence, that is, the line unit has ended. Then, all marked code blocks in the temporary line code sequence are directly connected together and combined with the marked code block, that is, the end symbol, to obtain the line object of the code to be checked.
[0103] In this embodiment, the code detection device determines whether a line unit has ended based on whether the marked code block is an end marker, thereby completing the segmentation of the line unit and obtaining the line object of the code to be inspected, so as to facilitate the subsequent association of logical structures based on the line object.
[0104] In some embodiments of this application, the specific implementation process of generating the line object of the code to be verified using temporary code sequences and marked code blocks, i.e., S1021c, may include: S201-S202, as follows:
[0105] S201. Using the symbol information in the temporary code sequence, determine the line level corresponding to the temporary code sequence.
[0106] When the code verification device generates a line object of the code to be verified using a temporary code sequence, it first determines the type of symbol information appearing in the temporary code sequence, and then determines the corresponding line level of the temporary code sequence based on the type of symbol information.
[0107] The code verification device determines the number of symbols in the symbol information corresponding to hierarchical increment operations. Based on the number of symbols corresponding to hierarchical increment operations, it increments the current level of the temporary code sequence (which can be the initial level or the level after decrementing). Next, the device determines the number of symbols in the symbol information corresponding to hierarchical decrement operations and decrements the current level of the temporary code sequence (which can be the initial level or the level after incrementing) according to this number. The level obtained after completing the increment and decrement operations is the final line level corresponding to the temporary code sequence.
[0108] For example, the symbol corresponding to the level increment operation can be "[" or "(", and the symbol corresponding to the level decrement operation can be "]" or ")". In this case, the code verification device will increment by n based on the number of "[" and "(") based on the initial level (usually set to 0) corresponding to the temporary code sequence. Then, based on the number of "]" or ")" n, it will decrement by n based on the level obtained by the increment operation, thereby obtaining the line level corresponding to the temporary code sequence, which is 0.
[0109] S202. When the row level matches the row level threshold, the temporary code sequence and the marked code block are concatenated into a row object of the code to be checked.
[0110] The code inspection device compares the line level of the temporary code sequence with a level threshold to determine if the line level is the same as the level threshold. When the line level is the same as the level threshold, the code inspection device determines that the temporary code sequence has met the condition for the end of a line unit, and then concatenates a marked code block on the temporary code sequence, that is, adds a terminator, and treats the temporary code sequence with the terminator as a line object.
[0111] It is understandable that the level threshold can be set according to the actual situation, such as 0 or 5, and this application does not limit it here.
[0112] In this embodiment of the application, when the code verification device determines that the marked code block is the end symbol, it determines the line level of the temporary code sequence and continues to determine whether the line level reaches the level threshold. Therefore, only when the marked code block is the end symbol and the line level is the level threshold will the temporary code sequence be determined as a complete line unit, thereby obtaining the line object.
[0113] based on Figure 4 See Figure 5 , Figure 5 This is another flowchart illustrating the code verification method provided in this application. In some embodiments of this application, the logical structure of the line object is associated to obtain the logical structure of the code to be verified, i.e., the specific implementation process of S1022 may include: S1022a-S1022c, as follows:
[0114] S1022a. Determine the type information of the row object based on the end character in the row object.
[0115] The terminators for line objects of different types are generally different. Code verification devices identify the terminators in line objects to determine the type information corresponding to the line object.
[0116] For example, lines ending with ";" are ordinary statement lines, lines ending with "{" are start lines, and lines ending with "}" are end lines.
[0117] S1022b: Based on the type information, merge the row objects into the corresponding target structure.
[0118] The code verification device determines the corresponding target structure for each line object based on its type information, that is, it determines the parent structure of the line object, and then merges the line objects into the corresponding target structure.
[0119] S1022c: Merge the target structure into the corresponding parent structure, and determine the parent structure as the new target structure. Continue merging the structures until the logical structure of the code to be tested is obtained when the structure merging is completed.
[0120] Next, the code verification device continues to determine the parent structure of the target structure and merges the target structure into its corresponding parent structure. This process is repeated until no more parent structures can be merged, at which point the structure obtained from the last merge is determined as the logical structure.
[0121] For example, if the code to be tested is:
[0122]
[0123] Therefore, the logical structure determined by the code verification device for the code to be verified is as follows:
[0124]
[0125] Through this logical structure, the code verification device can clearly identify the execution order and logical relationships in the code to be verified, providing a basis for analyzing the execution logic for subsequent code verification.
[0126] In this embodiment of the application, the code verification device can first merge the lowest-level line objects into the upper-level structure, i.e. the target structure, and then continue to merge the target structure into its parent structure. Thus, through layer-by-layer merging, the logical structure of the code to be verified can be obtained.
[0127] In some embodiments of this application, the specific implementation process of merging row objects into the corresponding target structure based on type information, i.e., S1022b, may include: S301 or S302, as follows:
[0128] S301. When the type information represents the row object as the starting row, create a target structure for the row object and merge the row object into the target structure.
[0129] When the type information indicates that the line object is the starting line, it means that the line object is the beginning of its corresponding structure. Therefore, the code verification device will create a brand new structure for the line object as the target structure. This target structure will be subsequently merged into the statement lines and ending lines belonging to it.
[0130] S302. When the type information indicates that the line object is the end line or a statement line, the corresponding target structure is searched from the already created structures for the line object, and the line object is merged into the target structure.
[0131] When the type information indicates that the line object is either the end line or a statement line, it means that it should be merged into its corresponding structure. Therefore, the code verification device will search for the structure to which the line object belongs from the already created structures, determine the found structure as the target structure, and then merge the line object into the target structure.
[0132] In some embodiments of this application, the created structures include: class structures, function structures, and condition structures. The specific implementation process of searching for the corresponding target structure from the created structures for a row object, i.e., S302, may include: S3021, as follows:
[0133] S3021. From the class structure, function structure, and condition structure, find the parent structure to which the row object belongs, and determine the parent structure to which the row object belongs as the target structure.
[0134] The code verification device searches for the target structure of the row object, which is essentially searching for the parent structure to which the row object belongs. The device evaluates each created class structure, function structure, and condition structure to determine which structure is the parent structure of the row object, thus obtaining the target structure.
[0135] In some embodiments, the code verification device determines the parent structure of the line object as the structure corresponding to the most recent starting line preceding the line object. In other embodiments, the code verification device determines the parent structure of the line object based on the indentation of the line object, as well as the indentation of class structures, function structures, and condition structures.
[0136] In this embodiment, the code verification device can determine whether a line object is the start of a structure or an element within a structure based on the type information of the line object. This clarifies whether to create a corresponding parent structure for the line object or to find the corresponding parent structure, and merges the line object into the parent structure so that the target structure can be merged into a higher-level structure in the future.
[0137] In some embodiments of this application, corresponding attribute information is added to the logical structure to obtain the relationship information to be tested corresponding to the code to be tested. That is, the specific implementation process of S103 may include: S1031-S1034, as follows:
[0138] S1031. Divide the object content of the row object in the logical structure into types to obtain the object type of the row object.
[0139] The code verification device classifies variables within a line object based on the type of the declared object content. For example, if the line object contains a string type, the object type is determined to be a string type; if the line object contains a variable type, the object type is determined to be a variable type.
[0140] It is understandable that the type of the object content can be obtained from the preceding and following fields in the annotation code sequence, or from the type field in the annotation code sequence.
[0141] For example, Table 1 is an example of the object types provided in the embodiments of this application.
[0142] Table 1
[0143] Type of object content Identifier of object type Example String variable.STRING 'demo string' array variable.ARRAY ['test'=>$param,'test2'=>2] variable variable.VARIABLE $param['test'] define constant variable.DEFINE GROUP_ID const constant variable.CONST self::PACKAGE_ID function call variable.CALL $this->getParam($test) Class object variable.CLASS new Logic\LotteryLogic() Iterating objects variable.FOREACH foreach($params as$param) counter varibale.INT $i++
[0144] This shows that the code verification device will obtain different identifiers for different object contents.
[0145] S1032. Parse the function structure calls in the logical structure to obtain the function structure call relationships.
[0146] The code verification device also needs to determine the calling and called relationships within the function bodies of the logical structure, thereby obtaining the calling connections of the function structure. At this point, the code verification device will first use the parameters in the function call line to find the corresponding class, and then search for the called function in the class structure. If the called function does not exist in the class, the code verification device will search in the class's parent class until the function object is found.
[0147] Understandably, code verification devices use two pointers to store the call relationships of function structures: one pointer stores the function that calls the function's line object, pointing to the corresponding entity function; the other pointer stores the pointer to the function's called line, pointing to the called object.
[0148] S1033. Perform inheritance analysis on the class structures in the logical structure to obtain the inheritance relationship of the class structures.
[0149] Code verification equipment can use inheritance keywords to find the parent class, and even the grandparent class, of a class structure, and use a chain of pointers to record the inheritance relationship between class structures. At this point, the chain pointer points to the inherited class structure.
[0150] It is understood that the inherited keyword can be the extend keyword or a keyword set by the user. This application does not limit this.
[0151] In addition, functions defined within a class structure will be pointed to by pointers to the class structure in which they are called. This allows the calling entity of the function to be located during subsequent code verification.
[0152] S1034. The object information of the row object, the calling relationship of the function structure and the inheritance relationship of the class structure are determined as the attribute information corresponding to the code to be tested, and the attribute information is added to the logical structure to obtain the relationship information to be tested corresponding to the code to be tested.
[0153] For example, for the logical structure constructed above, the relationship information to be tested after adding attribute information can be as follows:
[0154]
[0155]
[0156] At this point, the code verification device has obtained static information about the relationships to be verified, so that the code to be verified can be verified without running or compiling the code.
[0157] In some embodiments of this application, the object information of the row object, the calling relationship of the function structure, and the inheritance relationship of the class structure are determined as the attribute information corresponding to the code to be tested, and added to the logical structure to obtain the relationship information to be tested corresponding to the code to be tested. That is, after S1034, the method may further include: S105-S106, as follows:
[0158] S105. For the variable objects in the relation information to be tested, extract the reference pointers from the relation information to be tested.
[0159] In this embodiment, the code verification device can obtain the definition of the variable object used in a certain line of the relation information to be verified. First, the code verification device obtains the reference pointer of the variable object by performing a bubble query on the parent structure, so as to obtain all possible values of its definition through the reference pointer, that is, to obtain the definition information.
[0160] S106. Obtain the definition information of the variable object by referencing the pointer.
[0161] After obtaining the reference pointer, the code verification device will iterate through the variable object's scope and its corresponding definition information.
[0162] In some embodiments of this application, the definition information of the variable object is obtained iteratively by referencing the pointer, i.e., the specific implementation process of S106, which may include S1061 and any one of S1062-S1065, as follows:
[0163] S1061. Obtain the initial definition of the variable object based on the reference pointer.
[0164] The code verification device first identifies the definition pointed to by the reference pointer as the initial definition of the variable object. The initial definition can be a definition of a definite value such as a string type or an array type, or a definition of an indefinite value such as a variable type or a constant type. The definition information of the variable object needs to be further determined later based on the type of the initial definition.
[0165] S1062. When the initial definition is a string type or an array type, the initial definition is determined as the definition information of the variable object.
[0166] S1063. When the initial definition is a variable type or a constant type, the scope of the variable object is traversed to obtain the definition information of the variable object.
[0167] When the initial definition is a variable type or a constant type, the code verification device will search for the definition point of the variable in the current scope of the variable object, and repeat the process until the definition information of the variable object is found.
[0168] S1064. When the initial definition is a function call return type, the return unit line of the function is traversed to obtain the definition information of the variable object.
[0169] When the initial definition is a function call return type, the code verification device first obtains the class in which the function is located, then obtains all the return unit lines of the function, traverses these definition points, and continues until the definition information of the variable object is found.
[0170] S1065. When the initial definition is an iterator type, the key-value pairs of the variable type in the initial definition are used to determine the definition information of the variable object.
[0171] Finally, when the initial definition is an iterator type, the code verification device will directly return the key-value pairs of the initially defined variable type as the definition information of the variable object.
[0172] In this embodiment of the application, the code verification device provides an interface for querying the definition of variable objects in the row objects of the relationship information to be verified, so as to retrieve the definition information of the variable objects.
[0173] The following will describe an exemplary application of the embodiments of this application in a real-world application scenario.
[0174] This application's embodiment is implemented in the scenario of analyzing and verifying the code of a completed project. First, staff can upload the complete code file to a server (code verification device). The server analyzes the code to obtain the project's static relational database. Staff can then perform rule matching within this database to determine the rationality of syntax statements in the project code, etc. It should be noted that the number of static relational databases is not unique. For example, the project framework can correspond to one static relational database, while business logic can correspond to another. The project being verified can consist of a combination of two or more static relational databases.
[0175] See Figure 6 , Figure 6 This describes the construction process of the static relational database provided in this application embodiment. The server receives code files 6-A, 6-B, and 6-C (all code to be verified) belonging to the same project. Then, for code files 6-A, 6-B, and 6-C, it performs sequence labeling 6-1, row object construction 6-2, logical structure construction 6-3, and class function structure construction 6-4, respectively, to obtain the static relational database 6-5 (relationship information to be verified) that corresponds to code files 6-A, 6-B, and 6-C.
[0176] First, during the construction of the annotation sequence, the server uses a type annotation tool (such as the Pygments extension library) to parse the code files, obtaining an annotation sequence (annotated code sequence) for each code file. The annotation sequence is the result of annotating each code block within the code.
[0177] Next, the server constructs line objects based on the annotation sequence. The server first segments the annotation sequence of the code file into line units. These line units are not lines separated by newline characters, but rather line units separated by logical blocks in the programming language. For example, an array spanning multiple lines will be determined as a single line object, while multiple variable declarations on the same line will be split into multiple separate line objects.
[0178] See Figure 7 , Figure 7 This is a schematic diagram of the row object construction process provided in an embodiment of this application. The row object construction process may include:
[0179] S401, Get the code block.
[0180] S402. Determine if the current code block is the end-of-block character.
[0181] The server analyzes each code block in the labeled sequence. When it determines that the code block is a symbol type, it checks whether it is a line terminator. If it is a line terminator, it executes S408; otherwise, it executes S403.
[0182] Before that, the server will first initialize a temporary object (temporary code sequence) with the line level set to 0.
[0183] S403. Add the code block to the temporary object.
[0184] S404. Determine whether the current code block contains "[" and "(" (symbol information). If yes, execute S405; otherwise, execute S406.
[0185] S405, row hierarchy increases.
[0186] The server performs a row-level operation plus n based on the number of left parentheses.
[0187] S406. Determine if the current code block contains "]" and ")" (symbol information). If so, execute S407.
[0188] S407, row hierarchy decreases.
[0189] The server performs a line level minus n operation based on the number of closing parentheses. This allows the calculation of the line level of the code block (temporary objects store code blocks; determining the line level of a temporary object is equivalent to determining the line level of the code block).
[0190] S408. Determine if the row level is 0. If yes, execute S409; otherwise, execute S403.
[0191] S409. Construct a new row object using a temporary object. Then execute S410.
[0192] S410, Clear temporary objects.
[0193] After clearing, the next code block is retrieved and analyzed.
[0194] Continue this process until the labeled sequence has been traversed.
[0195] In other words, to determine whether a row is complete, that is, to identify a complete row object, the following two conditions must be met simultaneously: First, the end character is "{", "}", or ","; second, the row level is 0.
[0196] When constructing a logical structure, the server archives each row object generated during the traversal into a parent structure (target structure), thus obtaining a complete logical structure. Logical structures are interconnected. For example, a class structure named `Lottery` can contain a function structure named `login`, which in turn contains row objects and `if` structures (conditional structures). The `if` structures also contain other row objects.
[0197] Figure 8 This is a schematic diagram illustrating the process of constructing the logical structure provided in an embodiment of this application. See also... Figure 8 An example of the construction process of a logical structure can be shown below:
[0198] S501, Get the row object.
[0199] S502. Determine if the line object is a normal unit line (statement line). If yes, execute S503; otherwise, execute S504 and S505.
[0200] S503, Push the if structure.
[0201] That is, push ordinary row objects directly into their parent structure.
[0202] S504. Determine if the row object is the starting unit row (starting row). If so, execute S506.
[0203] S505. Determine if the row object is the end cell row (end row). If so, execute S503 and S507.
[0204] S506. Create new structures. For example, create an if structure.
[0205] Subsequent row objects of ordinary types will be pushed into this structure.
[0206] S507, Push Function Structure.
[0207] S508. Push the function structure into the class structure. This achieves the pushing of the parent structure into the higher-level structure.
[0208] pass Figure 8 After the process of archiving line objects, a complete single-file structure can be obtained for each code file, making it very clear to determine the execution order and logical relationships of the code when analyzing the code logic.
[0209] After the server constructs the logical structure for each code file, it will find the parent class in the static relational database for all class objects through the inheritance keyword extends, and record the inheritance parent class structure of each class object in the form of pointers.
[0210] Since there are cases where parent classes inherit from grandparent classes in the code file, a chain of class inheritance relationships can be obtained through pointers. Furthermore, for functions defined within a class, pointers can be used to point to the class structure where the function is located. During subsequent logical rule matching, the call entity of the function in the code can be located based on the parent class overloading rules.
[0211] Next, the server will classify the variable definitions within the ordinary row object according to the declared object content (obtaining the object type). For example, if the annotation sequence contains string types, it will be declared as a string type; if it contains variable types, it will be declared as a variable. The type here can be obtained by sorting the preceding and following fields in the annotation sequence or by directly retrieving the type from the annotation sequence.
[0212] When a variable within a regular object is defined as a function call, such as `$res = $this->addGiftQual($qualKey)`, the server can determine the function object being called using the following methods:
[0213] 1) Separate by -> or :: to get the $this, addGiftQual, and ($qualKey) parameter parts;
[0214] 2) Find the corresponding class object based on the first parameter ($this or self in some language code files refer to the current class). If it is not the current class, it can be concatenated according to the namespace rules declared by the current class. For example, if use\Lib\Lottery is declared in the current class, and Lottery::query is used in the code, the Lottery class object being called is the \Lib\Lottery class object.
[0215] 3) Search for the called function object within the class object. If the class object does not contain a function object, search through the constructed inheritance chain, looking towards each parent class until the corresponding class function object is found. Next, create two pointers: one is a function pointer to the line object that called the function, pointing to the corresponding entity function; the other is a pointer to the line object that was called, pointing to the line object that called the function.
[0216] In this way, the call and called relationship (call connection) of the line number is established, providing traceable capability for querying function objects.
[0217] At this point, the server has completed the construction of the static project library. Developers can process the structured static relational library by editing validation rules or scripts, for example, retrieving key information in the code (such as gift pack IDs) or retrieving errors in the use of variable objects, etc.
[0218] In addition, in this embodiment, the static relational database also provides an API to retrieve all definitions of variables used in a specific row of the project. This primarily relies on variable scope settings.
[0219] Figure 9 This is a schematic diagram illustrating the process of querying variable definitions (definition information) provided in an embodiment of this application. From Figure 9 As can be seen, variable definition first requires recursively adding elements to the parent structure. For example, starting with the definition line object 9-1, it is recursively added to the condition structure 9-2, condition structure 9-3, function structure 9-4, and class structure 9-5, thus completing the recursive addition 9-6. Then, when the server queries the variable definition, it obtains the reference pointer of the variable object through the parent class structure, that is, it uses bubble query 9-7 to find the usage line object 9-9 in the condition structure 9-8, and then iterates to obtain all possible values of the definition.
[0220] The variable definition of the obtained variable object may not be a string or other definite value. In this case, it is necessary to iterate through the definition content again to obtain the variable definition.
[0221] Figure 10 This is a schematic diagram illustrating the iterative query process for variable definitions provided in an embodiment of this application. See also... Figure 10 The process includes:
[0222] S601. Determine whether the current object type is a string (variable.STRING) or an array (variable.ARRAY). If yes, execute S602; otherwise, execute S603.
[0223] S602, Return the defined value.
[0224] S603. Determine if the current object is a function call return value. If yes, first obtain the class of the current function; if yes, execute S604; otherwise, execute S605.
[0225] S604. Retrieve the function return line. Then execute S609.
[0226] S605. Determine whether the current object is a variable (variable.VARIABLE) or a constant (variable.CONST|variable.DEFINE). If yes, execute S606; otherwise, execute S607.
[0227] S606. Obtain the variable line in the current scope. Then execute S609.
[0228] S607. Determine if the current object is an iterator type (variable|FOREACH|variable.INT). If yes, execute S608.
[0229] S608. Returns key-value pairs of the variable type of the current object. Then execute S609.
[0230] S609. Obtain the line definition items and iterate through the reference definitions.
[0231] Through the above process, the server obtains all boxes of string or array types and returns a collection of variable object definitions.
[0232] The following description continues to illustrate the exemplary structure of the code verification device 255 provided in the embodiments of this application as a software module. In some embodiments, such as... Figure 2 As shown, the software module stored in the code verification device 255 of the memory 250 may include:
[0233] The parsing and annotation module 2551 is used to parse and annotate the code to be tested, and obtain the code annotation sequence;
[0234] The structure construction module 2552 is used to construct the logical structure of the code annotation sequence to obtain the logical structure of the code to be tested; the logical structure describes the execution order and logical association of the code to be tested.
[0235] The attribute adding module 2553 is used to add corresponding attribute information to the logical structure to obtain the relationship information to be tested corresponding to the code to be tested; the attribute information includes at least the object type, inheritance relationship and calling relationship in the code to be tested.
[0236] The result generation module 2554 is used to examine the code to be examined using the relationship information to be examined and the obtained examination rules, and to obtain the examination result.
[0237] In some embodiments of this application, the structure construction module 2552 is further configured to divide the code annotation sequence according to line units to obtain the line object corresponding to the code to be tested; and to perform logical structure association on the line object to obtain the logical structure of the code to be tested.
[0238] In some embodiments of this application, the code annotation sequence includes: multiple annotated code blocks; the structure construction module 2552 is further configured to perform the following processing on the multiple annotated code blocks respectively: perform type judgment on the annotated code blocks to obtain a judgment result; the judgment result indicates whether the annotated code block is an end symbol; when the judgment result indicates that the annotated code block is not the end symbol, add the annotated code block to a temporary code sequence, and determine the next code block of the annotated code block as a new annotated code block, and continue to perform type judgment; when the judgment result indicates that the annotated code block is the end symbol, use the temporary code sequence and the annotated code block to generate the line object of the code to be checked.
[0239] In some embodiments of this application, the structure construction module 2552 is further configured to use the symbol information in the temporary code sequence to determine the line level corresponding to the temporary code sequence; when the line level is the same as the level threshold, the temporary code sequence and the marked code block are concatenated to form the line object of the code to be tested.
[0240] In some embodiments of this application, the structure construction module 2552 is further configured to determine the type information of the row object based on the end character in the row object; merge the row object into the corresponding target structure according to the type information; merge the target structure into the corresponding parent structure, and determine the parent structure as the new target structure, and continue to merge structures until the logical structure of the code to be tested is obtained when the structure merging is completed.
[0241] In some embodiments of this application, the structure construction module 2552 is further configured to: when the type information indicates that the line object is a starting line, create the target structure for the line object and merge the line object into the target structure; when the type information indicates that the line object is an ending line or a statement line, search for the corresponding target structure from the already created structures for the line object and merge the line object into the target structure.
[0242] In some embodiments of this application, the created structure includes: a class structure, a function structure, and a condition structure; the structure construction module 2552 is further configured to find the parent structure to which the row object belongs from the class structure, the function structure, and the condition structure, and determine the parent structure to which the row object belongs as the target structure.
[0243] In some embodiments of this application, the attribute adding module 2553 is further configured to classify the object content of the row object in the logical structure to obtain the object type of the row object; parse the function structure in the logical structure to obtain the function structure call relationship; parse the class structure in the logical structure to obtain the class structure inheritance relationship; determine the object information of the row object, the function structure call relationship and the class structure inheritance relationship as the attribute information corresponding to the code to be tested, and add the attribute information to the logical structure to obtain the test relationship information corresponding to the code to be tested.
[0244] In some embodiments of this application, the code verification device 255 further includes: a definition query module 2555;
[0245] The definition query module 2555 is used to extract reference pointers from the variable objects in the relationship information to be tested; and to iteratively obtain the definition information of the variable objects through the reference pointers.
[0246] In some embodiments of this application, the definition query module 2555 is further configured to obtain the initial definition of the variable object based on the reference pointer; when the initial definition is a string type or an array type, the initial definition is determined as the definition information of the variable object; when the initial definition is a variable type or a constant type, the scope of the variable object is traversed to obtain the definition information of the variable object; when the initial definition is a function call return type, the return cell line of the function is traversed to obtain the definition information of the variable object; when the initial definition is an iterator type, the key-value pairs of the variable type in the initial definition are determined as the definition information of the variable object.
[0247] This application provides a computer program product or computer program that includes 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 code verification method described in this application.
[0248] This application provides a computer-readable storage medium storing executable instructions. When these executable instructions are executed by a processor, they cause the processor to execute the code verification method provided in this application. For example, ... Figure 3 The code verification method is shown.
[0249] In some embodiments, the computer-readable storage medium may be a memory such as FRAM, ROM, PROM, EP ROM, EEPROM, flash memory, magnetic surface memory, optical disk, or CD-ROM; or it may be a variety of devices including one or any combination of the above-mentioned memories.
[0250] In some embodiments, executable instructions may take the form of a program, software, software module, script, or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), and may be deployed in any form, including as a standalone program or as a module, component, subroutine, or other unit suitable for use in a computing environment.
[0251] As an example, executable instructions may, but do not necessarily, correspond to files in a file system. They may be stored as part of a file that holds other programs or data, for example, in one or more scripts in a Hyper Text Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple co-located files (e.g., files that store one or more modules, subroutines, or code sections).
[0252] As an example, executable instructions can be deployed to execute on a single computing device (code verification device), or on multiple computing devices located in one location, or on multiple computing devices distributed across multiple locations and interconnected via a communication network.
[0253] In summary, through the embodiments of this application, the code verification device first uses the code annotation sequence of the code to be verified to determine the static logical structure of the code to be verified, thereby clarifying the internal execution order and logical relationships of the code to be verified. Then, it adds attribute information such as variable definitions, inheritance relationships, and call relationships to the logical structure, statically completing the analysis of the code to be verified. This allows for the acquisition of verification relationship information describing all information in the code to be verified. Ideally, verification rules are used to verify this verification relationship information, thus eliminating the need to compile the code to be verified and load compilation dependencies. This decouples code verification from compilation dependencies, improving the flexibility of code verification. Furthermore, in the embodiments of this application, the verification rules can be written or set by the staff, resulting in greater flexibility of the verification rules and thus greater flexibility and scalability of code verification.
[0254] The above description is merely an embodiment of this application and is not intended to limit the scope of protection of this application. Any modifications, equivalent substitutions, and improvements made within the spirit and scope of this application are included within the scope of protection of this application.
Claims
1. A code verification method, characterized in that, The code verification method includes: The code to be tested is parsed and annotated to obtain a code annotation sequence; The code annotation sequence is logically structured to obtain the logical structure of the code to be tested; the logical structure describes the execution order and logical relationships of the code to be tested. The object content of the row objects in the logical structure is classified by type to obtain the object type of the row objects; wherein, the row objects are multiple code blocks obtained by dividing the code annotation sequence by syntax statements; The function structures within the logical structure are parsed to obtain the call relationships between the function structures; The inheritance relationships of the class structures in the logical structure are obtained by parsing the inheritance relationships of the class structures. The object type of the row object, the calling relationship of the function structure, and the inheritance relationship of the class structure are determined as the attribute information corresponding to the code to be tested, and the attribute information is added to the logical structure to obtain the relationship information to be tested corresponding to the code to be tested. The code to be tested is tested using the relationship information to be tested and the obtained testing rules to obtain the testing results.
2. The method according to claim 1, characterized in that, The step of constructing a logical structure for the code annotation sequence to obtain the logical structure of the code to be tested includes: For each annotated code block in the code annotation sequence, the following processing is performed: when the annotated code block is not a terminator, the annotated code block is added to a temporary code sequence, and the next code block of the annotated code block is determined as a new annotated code block, and the determination of whether it is a terminator is continued; when the annotated code block is the terminator, the line object of the code to be checked is generated using the temporary code sequence and the annotated code block; wherein, the terminator is a preset symbol used to indicate the end of a statement; Based on the terminator in the row object, the type information of the row object is determined; wherein, the type information is used to distinguish the type to which the row object belongs; When the type information indicates that the line object is a start line, a target structure is created for the line object, and the line object is merged into the target structure; when the type information indicates that the line object is an end line or a statement line, the corresponding target structure is searched from the already created structures for the line object, and the line object is merged into the target structure. The target structure is merged into the corresponding parent structure, and the parent structure is determined as the new target structure. The merging of structures continues until the logical structure of the code to be tested is obtained when the structure merging is completed.
3. The method according to claim 2, characterized in that, The step of generating the line object of the code to be verified using the temporary code sequence and the marked code block includes: Using the symbol information in the temporary code sequence, the line level corresponding to the temporary code sequence is determined; wherein, the symbol information includes the symbol corresponding to the level increment operation and the symbol corresponding to the level decrement operation; the line level corresponding to the temporary code sequence is obtained by updating the current level of the temporary code sequence based on the symbol information; When the row level is the same as the level threshold, the temporary code sequence and the marked code block are concatenated to form the row object of the code to be checked; the level threshold is a pre-set value used to determine whether the temporary code sequence meets the termination condition.
4. The method according to claim 2, characterized in that, The already created structures include: class structures, function structures, and condition structures; the step of searching for the corresponding target structure from the already created structures for the row object includes: From the class structure, the function structure, and the condition structure, find the parent structure to which the row object belongs, and determine the parent structure to which the row object belongs as the target structure.
5. The method according to claim 1, characterized in that, After determining the object type of the row object, the calling relationship of the function structure, and the inheritance relationship of the class structure as the attribute information corresponding to the code to be tested, and adding the attribute information to the logical structure to obtain the relationship information to be tested corresponding to the code to be tested, the method further includes: For the variable objects in the relationship information to be tested, extract the reference pointers from the relationship information to be tested; The definition information of the variable object is obtained iteratively through the reference pointer.
6. The method according to claim 5, characterized in that, The step of iteratively obtaining the definition information of the variable object through the reference pointer includes: Based on the reference pointer, obtain the initial definition of the variable object; When the initial definition is of type string or array, the initial definition is determined as the definition information of the variable object; When the initial definition is a variable type or a constant type, the scope of the variable object is traversed to obtain the definition information of the variable object; When the initial definition is a function call return type, the return unit line of the function is traversed to obtain the definition information of the variable object; When the initial definition is of type iterator, the key-value pairs of the variable type in the initial definition are determined as the definition information of the variable object.
7. A code verification device, characterized in that, The code verification device includes: The parsing and annotation module is used to parse and annotate the code to be tested, resulting in a code annotation sequence; The structure construction module is used to construct the logical structure of the code annotation sequence to obtain the logical structure of the code to be tested; the logical structure describes the execution order and logical association of the code to be tested. The attribute addition module is used to classify the object content of the row objects in the logical structure to obtain the object type of the row objects; wherein, the row objects are multiple code blocks obtained by segmenting the code annotation sequence with syntax statements; the module parses the function structures in the logical structure to obtain the function structure call relationships; the module parses the class structures in the logical structure to obtain the class structure inheritance relationships; the module determines the object type of the row objects, the function structure call relationships, and the class structure inheritance relationships as the attribute information corresponding to the code to be tested, and adds the attribute information to the logical structure to obtain the test relationship information corresponding to the code to be tested; The result generation module is used to examine the code to be examined using the relationship information to be examined and the obtained examination rules, and to obtain the examination result.
8. The apparatus according to claim 7, characterized in that, The structure construction module is further configured to: perform the following processing on each annotated code block in the code annotation sequence: when the annotated code block is not a terminator, add the annotated code block to a temporary code sequence, and determine the next code block of the annotated code block as a new annotated code block, and continue to determine whether it is a terminator; when the annotated code block is the terminator, use the temporary code sequence and the annotated code block to generate the line object of the code to be checked; wherein, the terminator is a preset symbol used to represent the end of a statement; Based on the terminator in the row object, the type information of the row object is determined; wherein, the type information is used to distinguish the type to which the row object belongs; When the type information indicates that the line object is a start line, a target structure is created for the line object, and the line object is merged into the target structure; when the type information indicates that the line object is an end line or a statement line, the corresponding target structure is searched from the already created structures for the line object, and the line object is merged into the target structure. The target structure is merged into the corresponding parent structure, and the parent structure is determined as the new target structure. The merging of structures continues until the logical structure of the code to be tested is obtained when the structure merging is completed.
9. The apparatus according to claim 8, characterized in that, The structure building module is also used for: Using the symbol information in the temporary code sequence, the line level corresponding to the temporary code sequence is determined; wherein, the symbol information includes the symbol corresponding to the level increment operation and the symbol corresponding to the level decrement operation; the line level corresponding to the temporary code sequence is obtained by updating the current level of the temporary code sequence based on the symbol information; When the row level is the same as the level threshold, the temporary code sequence and the marked code block are concatenated to form the row object of the code to be checked; the level threshold is a pre-set value used to determine whether the temporary code sequence meets the termination condition.
10. The apparatus according to claim 8, characterized in that, The created structures include: class structures, function structures, and condition structures; the structure construction module is also used for: From the class structure, the function structure, and the condition structure, find the parent structure to which the row object belongs, and determine the parent structure to which the row object belongs as the target structure.
11. The apparatus according to claim 7, characterized in that, The device further includes a definition query module, used for: For the variable objects in the relationship information to be tested, extract the reference pointers from the relationship information to be tested; The definition information of the variable object is obtained iteratively through the reference pointer.
12. The apparatus according to claim 11, characterized in that, The definition query module is also used for: Based on the reference pointer, obtain the initial definition of the variable object; When the initial definition is of type string or array, the initial definition is determined as the definition information of the variable object; When the initial definition is a variable type or a constant type, the scope of the variable object is traversed to obtain the definition information of the variable object; When the initial definition is a function call return type, the return unit line of the function is traversed to obtain the definition information of the variable object; When the initial definition is of type iterator, the key-value pairs of the variable type in the initial definition are determined as the definition information of the variable object.
13. A code verification device, characterized in that, The code verification equipment includes: Memory, used to store executable instructions; A processor, when executing executable instructions stored in the memory, implements the code verification method according to any one of claims 1 to 6.
14. A computer-readable storage medium storing executable instructions, characterized in that, When the executable instructions are executed by the processor, they implement the code verification method according to any one of claims 1 to 6.
15. A computer program product, comprising a computer program or instructions, characterized in that, When the computer program or instructions are executed by the processor, they implement the code verification method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Static code check method and apparatus, storage medium and computer device
CN108614707A
