A code compilation detection method and automatic compilation platform
By constructing and matching the abbreviation syntax tree, the division code is to optimize and not optimize detection subties, and compile and detect targeted, solve the problem of low pre -detection efficiency in large -scale code, and achieve a more efficient detection process.
Patent Information
- Application Number
- CN202411433802.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-15
- Publication Date
- 2025-01-28
- Estimated Expiration
- 2044-10-15
AI Technical Summary
In large-scale code, when directly using rule-based methods for pre-compilation detection, it will lead to too large detection time and memory, and the number of rules will increase, resulting in too low overall detection efficiency.
By obtaining the compilation code to build an abstract syntax tree, it matches it with the normal abstract grammar tree of history to determine the optimized detection subtree and not optimized detection subtreas. Then, compile and detect the detection process for unattracting nodes and non -optimized testing subties, and use preset rules to optimize the detection process.
By dividing the code into two types of sub -trees that can be optimized and not optimized, targeted detection, which significantly reduces the actual part of the compilation and detection, reduce the detection time, and improve the detection efficiency.
Smart Images

Figure CN118963772B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of electronic digital data processing, and in particular to a code compilation detection method and an automatic compilation platform. Background Art
[0002] Code compilation checking is usually divided into pre-compilation checking and post-compilation checking. Pre-compilation checking refers to the process of compiling source code to check whether it conforms to the syntax rules, type system and other compile-time requirements of the programming language during the software development process, so as to ensure that the code can be correctly converted into an instruction set that the machine can understand before execution; while post-compilation checking is performed after the code has been compiled into machine language or bytecode. Among them, the purpose of pre-compilation checking is to find and fix problems in the code as early as possible, reduce the possibility of compilation failure, and improve the overall quality of the code.
[0003] At present, pre-compilation detection of code usually adopts rule-based methods to detect syntax anomalies, program anomalies, etc. in the code. However, when the code to be compiled and detected is large-scale code, directly using the above method to compile and detect the code will result in excessive time and memory required for detection, and the number of rules will also increase significantly, resulting in low overall detection efficiency. Summary of the invention
[0004] In order to solve the above technical problems, the purpose of the present invention is to provide a code compilation detection method and an automatic compilation platform, and the technical solutions adopted are as follows:
[0005] In a first aspect, an embodiment of the present application provides a method for code compilation detection, comprising:
[0006] Obtaining code to be compiled, and constructing an abstract syntax tree according to the code to be compiled;
[0007] Matching the abstract syntax tree with several historical normal abstract syntax trees to determine an optimizable detection subtree and an unoptimizable detection subtree;
[0008] Determining unmatched nodes in the optimizable detection subtree, and when the number of the non-optimizable detection subtrees is multiple, determining the correlation between the non-optimizable detection subtrees;
[0009] The unmatched nodes are compiled and tested by a preset rule method, and the non-optimizable detection subtree is compiled and tested according to the correlation and the preset rule method.
[0010] In one embodiment, before the step of matching the abstract syntax tree with several historical normal abstract syntax trees, the method further includes:
[0011] Performing type analysis, semantic analysis, and data flow analysis on each node in the abstract syntax tree to obtain analysis results;
[0012] When the analysis result meets the preset requirement, the step of matching the abstract syntax tree with a plurality of historical normal abstract syntax trees is performed.
[0013] In one implementation, matching the abstract syntax tree with several historical normal abstract syntax trees to determine an optimizable detection subtree and an unoptimizable detection subtree includes:
[0014] Determine the lower-level nodes adjacent to the root node from the abstract syntax tree, and determine a plurality of tree structures to be matched based on each lower-level node and all nodes under the lower-level node;
[0015] Determine a candidate matching tree structure from the plurality of tree structures to be matched, and determine a candidate abstract syntax tree from the plurality of normal abstract syntax trees;
[0016] Determine whether the candidate abstract syntax tree has a target node with the same structure code as the root node of the candidate matching tree structure;
[0017] When there is the target node, determine all subtrees corresponding to the target node, determine the semantic structure consistency between the candidate matching tree structure and each subtree, and determine whether there is a target subtree based on the semantic structure consistency. When there is no target subtree, return to the step of determining the candidate abstract syntax tree from a plurality of normal abstract syntax trees until an existing target subtree is determined or all normal abstract syntax trees are used as candidate abstract syntax trees. If all normal abstract syntax trees are used as candidate abstract syntax trees and there is no target subtree, use each of the lower-level nodes of the candidate matching tree structure as a new root node to determine a new candidate matching tree structure. According to the new candidate matching tree structure, return to the step of determining whether there is a target node with the same structural code as the root node of the candidate matching tree structure in the candidate abstract syntax tree until there is a target subtree or the new root node is the lowest node. When there is a target subtree, return to the step of determining the candidate matching tree structure from a plurality of tree structures to be matched until all existing target subtrees are determined.
[0018] When there is no target node, returning to the step of determining candidate abstract syntax trees from the plurality of normal abstract syntax trees until all existing target subtrees are determined;
[0019] All candidate matching tree structures corresponding to the target subtree are determined as optimizable detection subtrees, and all candidate matching tree structures corresponding to the target subtree are determined as non-optimizable detection subtrees.
[0020] In one implementation, determining a candidate matching tree structure from a plurality of tree structures to be matched includes:
[0021] Determine the structure code of the root node in each of the tree structures to be matched;
[0022] The to-be-matched tree structure whose structure code is one of a statement node, an expression node and a control flow node forms a to-be-matched set;
[0023] A candidate matching tree structure is selected from the to-be-matched set.
[0024] In one embodiment, determining the semantic structure consistency between the candidate matching tree structure and each of the subtrees includes:
[0025] Determine a first depth of the candidate matching tree structure and a second depth of each of the subtrees, and respectively calculate an absolute value of a difference between the first depth and each of the second depths;
[0026] Determine a minimum depth between the first depth and each of the second depths, determine a total number of nodes of each of the subtrees at the minimum depth, and calculate a first product of the minimum depth and the total number of nodes respectively;
[0027] Respectively determine the number of codes in each node of the candidate matching tree structure that is the same as the number of codes in the node corresponding to the same position in each subtree, and determine the logical similarity value corresponding to each subtree according to the same number;
[0028] The ratio of the logical similarity value to the first product is determined respectively, and the semantic structure consistency of the candidate matching tree structure and each of the subtrees is obtained according to the reciprocal of the absolute value and the second product of the ratio.
[0029] In one implementation, determining whether a target subtree exists according to the semantic structure consistency includes:
[0030] When the semantic structure consistency is greater than a preset threshold, determining that the candidate matching tree structure has a target subtree;
[0031] When the semantic structure consistency is less than or equal to a preset threshold, it is determined that the candidate matching tree structure does not have a target subtree.
[0032] In one implementation, determining the unmatched nodes in the optimizable detection subtree includes:
[0033] Determine the semantic structure consistency corresponding to all target subtrees of the candidate matching tree structure, and take the target subtree with the greatest semantic structure consistency as the matching subtree;
[0034] Compare the code in each node of the candidate matching tree structure with the code in the node corresponding to the same position in the matching subtree;
[0035] The nodes with the same comparison results in the candidate matching tree structure are taken as matching nodes, and the other nodes in the candidate matching tree structure except the matching nodes are taken as unmatched nodes.
[0036] In one implementation, determining the correlation between the non-optimizable detection subtrees includes:
[0037] Determine the semantic structure consistency between each of the non-optimizable detection subtrees, perform distance quantification according to the semantic structure consistency between each of the non-optimizable detection subtrees, and obtain the distance between each of the non-optimizable detection subtrees;
[0038] The non-optimizable detection subtrees are clustered according to the distances to obtain a plurality of clusters; wherein each cluster includes at least one non-optimizable detection subtree, and the clusters are used to characterize the correlation between the non-optimizable detection subtrees.
[0039] In one implementation, the compiling detection of the non-optimizable detection subtree according to the correlation and the preset rule method includes:
[0040] Determine a target cluster from the clusters, and determine a first target non-optimizable detection subtree and a second target non-optimizable detection subtree from the target cluster;
[0041] By using the preset rule method, compile and detect the first target non-optimizable detection subtree to obtain a rule set;
[0042] Determine a dissimilar node in the second target non-optimizable detection subtree that is dissimilar to the code of the first target non-optimizable detection subtree, and determine a structure code of the dissimilar node and a rule type corresponding to the structure code;
[0043] Determining a target rule according to the rule set and the rule type;
[0044] Compile and check the second target non-optimizable detection subtree according to the target rule, return to the step of determining the first target non-optimizable detection subtree and the second target non-optimizable detection subtree from the target cluster, until compile and check all non-optimizable detection subtrees in the target cluster, return to the step of determining the target cluster from the cluster, until compile and check all non-optimizable detection subtrees of each of the clusters.
[0045] In a second aspect, an embodiment of the present application provides an automatic compilation platform, including:
[0046] An acquisition module, used to acquire the code to be compiled and construct an abstract syntax tree according to the code to be compiled;
[0047] A matching module, used for matching the abstract syntax tree with several historical normal abstract syntax trees to determine an optimizable detection subtree and an unoptimizable detection subtree;
[0048] A determination module, configured to determine unmatched nodes in the optimizable detection subtree, and when the number of the non-optimizable detection subtrees is multiple, determine the correlation between the non-optimizable detection subtrees;
[0049] A compile detection module is used to perform compile detection on the unmatched node by using a preset rule method, and to perform compile detection on the non-optimizable detection subtree according to the correlation and the preset rule method.
[0050] In one embodiment, the matching module is further used for:
[0051] Performing type analysis, semantic analysis, and data flow analysis on each node in the abstract syntax tree to obtain analysis results;
[0052] When the analysis result meets the preset requirement, the step of matching the abstract syntax tree with a plurality of historical normal abstract syntax trees is performed.
[0053] The present invention has the following beneficial effects:
[0054] By obtaining the code to be compiled and building an abstract syntax tree based on the code to be compiled, the abstract syntax tree is matched with several normal abstract syntax trees in the history, the optimizable detection subtree and the non-optimizable detection subtree are determined, the unmatched nodes in the optimizable detection subtree are determined, and when the number of non-optimizable detection subtrees is multiple, the correlation between the non-optimizable detection subtrees is determined, the code to be compiled is divided into two types: optimizable detection subtrees and non-optimizable detection subtrees, and compilation detection is performed on each type in a targeted manner. Compilation detection is performed on the unmatched nodes through a preset rule method, and compilation detection is performed on the non-optimizable detection subtree according to the correlation and the preset rule method, which is beneficial to reducing the part that actually needs to be compiled and detected, reducing the detection time and improving the detection efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0055] In order to more clearly illustrate the technical solutions and advantages in the embodiments of the present invention or the prior art, the drawings required for use in the embodiments or the prior art descriptions are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0056] Figure 1 A schematic diagram of the steps of a code compilation detection method provided by an embodiment of the present invention;
[0057] Figure 2 A schematic diagram of a code to be compiled and its corresponding abstract syntax tree provided by an embodiment of the present invention;
[0058] Figure 3 A schematic diagram of a common preset rule provided by an embodiment of the present invention;
[0059] Figure 4 A schematic diagram of an abstract syntax tree provided by an embodiment of the present invention;
[0060] Figure 5 A structural block diagram of an automatic compilation platform provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0061] In order to further explain the technical means and effects adopted by the present invention to achieve the predetermined invention purpose, the following is a detailed description of a code compilation detection method and an automatic compilation platform proposed according to the present invention, its specific implementation method, structure, features and effects, in conjunction with the accompanying drawings and preferred embodiments. In the following description, different "one embodiment" or "another embodiment" does not necessarily refer to the same embodiment. In addition, specific features, structures or characteristics in one or more embodiments may be combined in any suitable form.
[0062] Unless defined otherwise, 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 invention belongs.
[0063] The following specifically describes a code compilation detection method and an automatic compilation platform provided by the present invention in conjunction with the accompanying drawings.
[0064] See also Figure 1 , which shows a flow chart of a code compilation detection method provided by an embodiment of the present invention. The code compilation detection method may at least include steps S100-S400:
[0065] S100: Obtain the code to be compiled, and construct an abstract syntax tree according to the code to be compiled.
[0066] S200: Match the abstract syntax tree with several historical normal abstract syntax trees to determine an optimizable detection subtree and an unoptimizable detection subtree.
[0067] S300, determining unmatched nodes in an optimizable detection subtree, and when there are multiple non-optimizable detection subtrees, determining correlations between non-optimizable detection subtrees.
[0068] S400: compile and detect the unmatched nodes using a preset rule method, and compile and detect the non-optimizable detection subtree based on the correlation and the preset rule method.
[0069] The technical solution of the embodiment of the present application obtains the code to be compiled, builds an abstract syntax tree according to the code to be compiled, matches the abstract syntax tree with several historical normal abstract syntax trees, determines the optimizable detection subtrees and the non-optimizable detection subtrees, determines the unmatched nodes in the optimizable detection subtrees, and when the number of non-optimizable detection subtrees is multiple, determines the correlation between the non-optimizable detection subtrees, divides the code to be compiled into two types: optimizable detection subtrees and non-optimizable detection subtrees, performs compilation detection on each type in a targeted manner, performs compilation detection on the unmatched nodes through a preset rule method, and performs compilation detection on the non-optimizable detection subtrees according to the correlation and the preset rule method, which is beneficial to reducing the part that actually needs to be compiled and detected, reducing the detection time, and improving the detection efficiency.
[0070] In one embodiment, in step S100, the code to be compiled is obtained from the database, and then the sampled syntax tree algorithm is used to construct the tree structure of the code to be compiled to obtain an abstract syntax tree. The sampled syntax tree is a tree representation of the abstract syntax structure of the code to be compiled, and each node on the tree represents a structure in the code to be compiled. The sampled syntax tree algorithm can adopt the existing method, which will not be described in detail. Figure 2 As shown in FIG. 1 , a code to be compiled and its corresponding abstract syntax tree are shown.
[0071] Optionally, after determining the abstract syntax tree and before S200, each node in the abstract syntax tree can be subjected to type analysis, semantic analysis, data flow analysis and other analyses based on preset rules to obtain analysis results, and then determine whether the analysis results meet the preset requirements. If not, the user is prompted. If so, the step of matching the abstract syntax tree with several normal abstract syntax trees in the history is performed, and S200 is continued. For example, the preset rules are preset in the compiler's rule library. The preset rules of the rule library are a series of algorithms and data structures implemented inside the compiler. The compiler can use the preset rules to perform type analysis, semantic analysis, data flow analysis and other analyses. When judging the analysis, the compiler makes its own judgment and checks the code that executes the preset rules. If there are no errors in the check, the analysis results meet the preset requirements (i.e., it is correct). If an error occurs, it does not meet the preset requirements, and the compiler will report an error at the corresponding code location and mark it on the code construction page. The above processes can be analyzed and judged by the compiler itself. Figure 3 Several common preset rules are shown.
[0072] It should be noted that after the abstract syntax tree is constructed, since different functions may use the same loop statements or conditional statements in the construction process of the code to be compiled, they are usually represented by the same branch (subtree) structure in the abstract syntax tree. Therefore, when performing compilation safety detection, these similar structures can be optimized for detection. The corresponding similar parts can be detected only once, and the different parts can be detected separately, so as to achieve the purpose of optimizing the entire detection process. To this end, it is necessary to first find the same structure in the abstract syntax tree of the source code to be compiled.
[0073] In one implementation, step S200 includes steps S201-S206:
[0074] S201. Determine the lower-level nodes adjacent to the root node from the abstract syntax tree, and determine a number of tree structures to be matched based on each lower-level node and all nodes under the lower-level node.
[0075] Alternatively, assume that the abstract syntax tree is Figure 4 , at this time, determine the lower nodes adjacent to the root node 1, that is, lower nodes 2 and 3, and then determine several tree structures to be matched according to each lower node and all nodes under the lower node, that is, determine all nodes of the two subtrees (branches) including nodes 4 and 5 under the lower node 2, determine a tree structure to be matched, and determine a tree structure to be matched according to node 6 under the lower node 3, so that several tree structures to be matched can be determined. It should be noted that Figure 4 The examples listed are only for the sake of convenience. When the abstract syntax tree is other structures, several tree structures to be matched can be determined based on the same principle.
[0076] S202: Determine a candidate matching tree structure from a plurality of to-be-matched tree structures, and determine a candidate abstract syntax tree from a plurality of normal abstract syntax trees.
[0077] It should be noted that a normal abstract syntax tree refers to an abstract syntax tree that has been verified in advance and has no abnormalities in the codes of each node, so it is called a normal abstract syntax tree.
[0078] Optionally, the step of determining a candidate matching tree structure from a plurality of tree structures to be matched is:
[0079] First, determine the structure code of the root node in each tree structure to be matched, such as Figure 4 As shown, the root nodes in the tree structure to be matched are node 2 and node 3, and the structure codes of node 2 and node 3 are determined.
[0080] Secondly, common types corresponding to structural codes include statement nodes (such as assignment statements, loop statements, conditional statements, etc.), expression nodes (such as arithmetic expressions, logical expressions, etc.), control flow nodes (such as if-else statements, for loops, while loops, etc.), type nodes, parameter nodes, etc.; statement nodes, expression nodes, control flow nodes, which are nodes with functions and logic, not only represent the structure of the code, but also carry the semantic information of the code. For example, an if statement node will contain a conditional expression and a corresponding branch statement, expressing the logic of "if...then...else...". Statement nodes, expression nodes, control flow nodes and other nodes are uniformly recorded as a node set M, and the composition of the node set M is a preset statement code set such as In addition, type nodes and parameter nodes do not carry semantic information of the code. Therefore, the tree structures to be matched, which are one of the structure codes of statement nodes, expression nodes and control flow nodes and belong to the node set M, constitute a set to be matched, and then a candidate matching tree structure is selected from the set to be matched.
[0081] Finally, a candidate abstract syntax tree is determined from the plurality of normal abstract syntax trees. For example, the first normal abstract syntax tree may be used as the candidate abstract syntax tree.
[0082] S203: Determine whether the candidate abstract syntax tree has a target node with the same structure code as the root node of the candidate matching tree structure.
[0083] Optionally, each node of the candidate abstract syntax tree is compared with the root node of the candidate matching tree structure to determine whether the candidate abstract syntax tree has a target node with the same structural code as the root node of the candidate matching tree structure.
[0084] S204. When there is a target node, determine all subtrees corresponding to the target node, determine the semantic structure consistency between the candidate matching tree structure and each subtree, and determine whether there is a target subtree based on the semantic structure consistency. When there is no target subtree, return to the step of determining the candidate abstract syntax tree from a number of normal abstract syntax trees until an existing target subtree is determined or all normal abstract syntax trees are used as candidate abstract syntax trees. If all normal abstract syntax trees are used as candidate abstract syntax trees and there is no target subtree, use each lower-level node of the candidate matching tree structure as a new root node to determine a new candidate matching tree structure. According to the new candidate matching tree structure, return to the step of determining whether there is a target node with the same structural code as the root node of the candidate matching tree structure in the candidate abstract syntax tree until there is a target subtree or the new root node is the lowest node. When there is a target subtree, return to the step of determining the candidate matching tree structure from a number of tree structures to be matched until all existing target subtrees are determined.
[0085] Optionally, when there is a target node, the target node is used as the root node, and all subtrees under the lower nodes adjacent to the root node are determined, the principle is the same as step S201, so as to obtain all subtrees corresponding to the target node. corresponding subtrees, then when calculating the candidate matching tree structure and the When the semantic structures of the two corresponding subtrees are consistent, it is necessary to compare the depth information of the two. The more similar the depth information of the two is, the more similar the logical structure or semantic structure of the two structures is.
[0086] Optionally, the semantic structure consistency between the candidate matching tree structure and each subtree is determined, specifically:
[0087] First, determine the first depth of the candidate matching tree structure and the second depth of each subtree (i.e. subtrees), and calculate the first depth respectively. With every second depth The absolute value of the difference .
[0088] Next, determine the minimum depth of the first depth and each second depth , and determine the total number of nodes in each subtree at the minimum depth , respectively calculate the first product of the minimum depth and the total number of nodes .
[0089] Furthermore, the number of codes in each node of the candidate matching tree structure that are identical to the number of codes in the node corresponding to the same position in each subtree is determined, and the logical similarity value corresponding to each subtree is determined based on the identical number. , that is, The logical similarity value corresponding to the subtree is In each subtree, whenever there is a node with the same code as the node at the same position in the candidate matching tree structure, the logical similarity value +1 processing is performed, so if the number of similarities is 10, the logical similarity value is 10.
[0090] Then, determine the logical similarity values respectively With the first product Ratio , and obtain the semantic structure consistency of the candidate matching tree structure and each subtree according to the product of the reciprocal of the absolute value and the ratio. The specific formula is:
[0091]
[0092] in, The candidate matching tree structure and The semantic structure consistency of the subtrees, The 1 in the figure is to prevent the denominator from being 0, and other non-zero values may be used in other embodiments; The larger the value, the more candidate matching tree structure and the The greater the consistency of the language structure of the subtrees.
[0093] Optionally, based on the semantic structure consistency, it is determined whether the target subtree exists. The threshold is 0.7, for example. When the semantic structure consistency is greater than the preset threshold, it indicates that the match is successful, and it is determined that the candidate matching tree structure has a matching target subtree. When the semantic structure consistency is less than or equal to the preset threshold, the match fails, and it is determined that the candidate matching tree structure does not have a target subtree.
[0094] Optionally, when there is no target subtree, the step of determining a candidate abstract syntax tree from a plurality of normal abstract syntax trees is returned until the target subtree that exists is determined or all normal abstract syntax trees are used as candidate abstract syntax trees. If at this time all normal abstract syntax trees are used as candidate abstract syntax trees and there is still no target subtree, each lower-level node of the candidate matching tree structure is used as a new root node to determine a new candidate matching tree structure, as in the principle of step S201, and then according to the new candidate matching tree structure, the step of determining whether there is a target node in the candidate abstract syntax tree with the same structural code as the root node of the candidate matching tree structure is returned until there is a target subtree, or at this time all normal abstract syntax trees are used as candidate abstract syntax trees, and the new root node is already the lowest node, and it is impossible to continue to determine a new candidate matching tree structure.
[0095] When a target subtree exists, the process returns to the step of determining a candidate matching tree structure from a plurality of tree structures to be matched, until a target subtree corresponding to each candidate matching tree structure is determined, and all existing target subtrees are obtained.
[0096] S205: When there is no target node, return to the step of determining candidate abstract syntax trees from a number of normal abstract syntax trees until all existing target subtrees are determined.
[0097] Optionally, when there is no target node, the step of determining a candidate abstract syntax tree from among several normal abstract syntax trees is returned, based on the process principle of the above step S204, until all existing target subtrees are determined.
[0098] S206: Determine all candidate matching tree structures corresponding to the target subtree as optimizable detection subtrees, and determine all candidate matching tree structures corresponding to the target subtree as non-optimizable detection subtrees.
[0099] Optionally, all candidate matching tree structures corresponding to the target subtrees are determined as optimizable detection subtrees, and all candidate matching tree structures corresponding to the target subtrees are determined as non-optimizable detection subtrees, thereby distinguishing between two types of subtrees: detectable and non-detectable.
[0100] In one embodiment, determining the unmatched nodes in the optimizable detection subtree in step S300 includes steps S301-S303:
[0101] S301 , determining the semantic structure consistency corresponding to all target subtrees of the candidate matching tree structure, and taking the target subtree with the greatest semantic structure consistency as the matching subtree.
[0102] Optionally, the semantic structure consistency corresponding to all target subtrees of the candidate matching tree structure is determined, and then the target subtree with the largest semantic structure consistency is used as the matching subtree, so that the matching subtree corresponding to each candidate matching tree structure can be determined. It should be noted that, because normal abstract syntax trees are normal, stopping when a matching subtree is found can avoid the situation where too much search causes the overall search efficiency to be higher.
[0103] S302: Compare the code in each node of the candidate matching tree structure with the code in the node corresponding to the same position in the matching subtree.
[0104] S303: Nodes with the same comparison results in the candidate matching tree structure are regarded as matching nodes, and other nodes in the candidate matching tree structure except the matching nodes are regarded as unmatched nodes.
[0105] Optionally, the code in each node of the candidate matching tree structure is compared with the code in the node corresponding to the same position in the matching subtree, and the nodes with the same comparison results in the candidate matching tree structure are regarded as matching nodes, and the matched nodes are normal nodes, so no subsequent compilation detection is required. The other nodes in the candidate matching tree structure except the matching nodes are unmatched nodes, and only compilation detection needs to be performed on the unmatched nodes according to the existing preset rule method, thereby improving the compilation detection efficiency of all optimizable detection subtrees.
[0106] It should be noted that if the candidate matching tree structure has neither a corresponding target subtree nor a corresponding matching subtree, all nodes in the candidate matching tree structure can be regarded as unmatched nodes.
[0107] In one implementation, when the number of non-optimizable detection subtrees is multiple in step S300, since there may be similar situations between the multiple non-optimizable detection subtrees, similar functions will be constructed when the representations of different structures corresponding to the code to be compiled are relatively similar in structure to the subtrees under different nodes in the abstract syntax tree of the code to be compiled. Therefore, further analysis can be performed to determine the correlation between the non-optimizable detection subtrees, specifically including steps S304-S305:
[0108] S304: Determine the semantic structure consistency between each non-optimizable detection subtree, perform distance quantification according to the semantic structure consistency between each non-optimizable detection subtree, and obtain the distance between each non-optimizable detection subtree.
[0109] Optionally, based on S204 The calculation principle can determine the semantic structure consistency between each non-optimizable detection subtree, and then quantify the distance according to the semantic structure consistency between each non-optimizable detection subtree to obtain the distance between each non-optimizable detection subtree.
[0110] S305 , clustering the non-optimizable detection subtrees according to each distance to obtain a number of clusters.
[0111] Then, the non-optimizable detection subtrees are clustered according to each distance, and the non-optimizable detection subtrees with similar structures are clustered into one category to obtain a number of clusters, for example, Y clusters. Each cluster includes at least one non-optimizable detection subtree, and the cluster is used to characterize the correlation (or similarity) between the non-optimizable detection subtrees. When the non-optimizable subtrees are subsequently compiled and tested, the compilation and testing results of the non-optimizable subtrees in the same cluster can refer to each other, thereby optimizing the compilation and testing efficiency of the entire non-optimizable detection subtree.
[0112] In one implementation, in step S400, the unmatched nodes are compiled and detected by a preset rule method. The preset rule method can utilize an existing method and will not be described in detail.
[0113] In one implementation, in step S400, the non-optimizable detection subtree is compiled and detected according to the correlation and the preset rule method, which specifically includes steps S401-S405:
[0114] S401 : Determine a target cluster from the clusters, and determine a first target non-optimizable detection subtree and a second target non-optimizable detection subtree from the target cluster.
[0115] Optionally, a target cluster is determined from the clusters, and then a first target non-optimizable detection subtree and a second target non-optimizable detection subtree are determined from the target cluster. For example, the first non-optimizable detection subtree and the second non-optimizable detection subtree in the cluster can be used as the first target non-optimizable detection subtree and the second target non-optimizable detection subtree, respectively.
[0116] S402: Compile and detect the first target non-optimizable detection subtree by using a preset rule method to obtain a rule set.
[0117] Optionally, the first target non-optimizable detection subtree is compiled and detected by a preset rule method to obtain a rule set y.
[0118] S403: Determine dissimilar nodes in the second target non-optimizable detection subtree that are dissimilar to the codes of the first target non-optimizable detection subtree, and determine structural codes of the dissimilar nodes and rule types corresponding to the structural codes.
[0119] Optionally, the code of the node of the second target non-optimizable detection subtree is then compared with the code of the node of the first target non-optimizable detection subtree, so as to determine the dissimilar nodes in the second target non-optimizable detection subtree that are dissimilar to the code of the first target non-optimizable detection subtree; wherein the similarity calculation method is not specifically limited, and the calculated similarity can be combined with a threshold to determine whether it is similar. Then, the structural code of the dissimilar node and the rule type corresponding to the structural code are determined.
[0120] S404: Determine the target rule according to the rule set and rule type.
[0121] Optionally, the rule set and the rule type are combined to determine the target rule, which is a set .
[0122] S405. Compile and check the second target non-optimizable detection subtree according to the target rule, and return to the step of determining the first target non-optimizable detection subtree and the second target non-optimizable detection subtree from the target cluster, until all non-optimizable detection subtrees in the target cluster are compiled and checked, and return to the step of determining the target cluster from the cluster until all non-optimizable detection subtrees in each cluster are compiled and checked.
[0123] Optionally, according to the target rules, the second target non-optimizable detection subtree is compiled and checked, which can avoid the situation where too many rules are used during the compilation and detection, resulting in too low compilation and detection efficiency for the non-optimizable detection subtree. Then, return to the step of determining the first target non-optimizable detection subtree and the second target non-optimizable detection subtree from the target cluster, for example, use the second target non-optimizable detection subtree as the new first target non-optimizable detection subtree, and use the third non-optimizable detection subtree as the new second target non-optimizable detection subtree, and return to step S401 of determining the first target non-optimizable detection subtree and the second target non-optimizable detection subtree from the target cluster until all non-optimizable detection subtrees in the target cluster are compiled and checked.
[0124] In one embodiment, according to the above method, the compiled code is compiled based on the preset rule method. If the compiled code passes all the compilation tests and no syntax errors, type mismatches, naming conflicts, etc. are found, the compiled code is converted into machine code that can be recognized by the machine through the compiler, providing a basis for the subsequent code operation and debugging. In addition, if there are errors in the compiled code, such as syntax errors, type mismatches, naming conflicts, etc., the compiler reports specific error information and points out the location where the error occurs, and does not convert the machine code, reminding the developer to adjust the compiled code in time.
[0125] Through the method of the embodiment of the present application, the similarity between different subtrees can be quantified by calculating the semantic structure consistency between the tree structure to be matched and the normal abstract syntax tree in the abstract syntax tree of the code to be compiled; then, the matching subtree of the subtree in the abstract syntax tree of the code to be compiled is determined according to the semantic structure consistency, and then the abstract syntax tree of the code to be compiled is divided into two types: subtrees that can be optimized for detection and subtrees that cannot be optimized for detection. Then, the subtrees that can be optimized for detection are optimized according to the set method, and the subtrees that cannot be optimized for detection are optimized by calculating the correlation between the subtrees and according to the relevant situation of the rules in the detection process, thereby significantly improving the compilation detection efficiency of large-scale codes.
[0126] Reference Figure 5 , shows a structural block diagram of an automatic compilation platform of an embodiment of the present application, and the automatic compilation platform may include:
[0127] An acquisition module is used to acquire the code to be compiled and construct an abstract syntax tree according to the code to be compiled;
[0128] A matching module, used to match the abstract syntax tree with several historical normal abstract syntax trees, and determine an optimizable detection subtree and an unoptimizable detection subtree;
[0129] A determination module, used to determine unmatched nodes in an optimizable detection subtree, and when there are multiple non-optimizable detection subtrees, determine the correlation between the non-optimizable detection subtrees;
[0130] The compile detection module is used to perform compile detection on unmatched nodes through a preset rule method, and to perform compile detection on non-optimizable detection subtrees based on relevance and preset rule methods.
[0131] In one embodiment, the matching module may also be used to:
[0132] Perform type analysis, semantic analysis, and data flow analysis on each node in the abstract syntax tree to obtain analysis results;
[0133] When the analysis result meets the preset requirements, the step of matching the abstract syntax tree with several historical normal abstract syntax trees is executed.
[0134] In the embodiment of the present application, the functions of each module in the automatic compilation platform can refer to the corresponding description in the above method, which will not be repeated here.
[0135] It should be noted that the above-mentioned sequence of the embodiments of the present application is only for description and does not represent the advantages and disadvantages of the embodiments. The processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0136] The various embodiments in this specification are described in a progressive manner, and the same or similar parts between the various embodiments can be referenced to each other, and each embodiment focuses on the differences from other embodiments.
Claims
1. A code compilation detection method, characterized in that: The method comprises: Obtaining code to be compiled, and constructing an abstract syntax tree according to the code to be compiled; Matching the abstract syntax tree with several historical normal abstract syntax trees to determine an optimizable detection subtree and an unoptimizable detection subtree; Determine unmatched nodes in the optimizable detection subtree, and when the number of the non-optimizable detection subtrees is multiple, determine the correlation between the non-optimizable detection subtrees; Compile and detect the unmatched nodes using a preset rule method, and compile and detect the non-optimizable detection subtree based on the correlation and the preset rule method; Among them, the method for determining the optimizable detection subtree and the non-optimizable detection subtree is: Determine the lower-level nodes adjacent to the root node from the abstract syntax tree, and determine a plurality of tree structures to be matched based on each lower-level node and all nodes under the lower-level node; Determine a candidate matching tree structure from the plurality of tree structures to be matched, and determine a candidate abstract syntax tree from the plurality of normal abstract syntax trees; Determine whether the candidate abstract syntax tree has a target node with the same structure code as the root node of the candidate matching tree structure; When there is the target node, determine all subtrees corresponding to the target node, determine the semantic structure consistency between the candidate matching tree structure and each subtree, and determine whether there is a target subtree based on the semantic structure consistency. When there is no target subtree, return to the step of determining the candidate abstract syntax tree from a plurality of normal abstract syntax trees until an existing target subtree is determined or all normal abstract syntax trees are used as candidate abstract syntax trees. If all normal abstract syntax trees are used as candidate abstract syntax trees and there is no target subtree, use each of the lower-level nodes of the candidate matching tree structure as a new root node to determine a new candidate matching tree structure. According to the new candidate matching tree structure, return to the step of determining whether there is a target node with the same structural code as the root node of the candidate matching tree structure in the candidate abstract syntax tree until there is a target subtree or the new root node is the lowest node. When there is a target subtree, return to the step of determining the candidate matching tree structure from a plurality of tree structures to be matched until all existing target subtrees are determined. When there is no target node, returning to the step of determining candidate abstract syntax trees from the plurality of normal abstract syntax trees until all existing target subtrees are determined; Determine all candidate matching tree structures corresponding to the target subtree as optimizable detection subtrees, and determine all candidate matching tree structures corresponding to the target subtree as non-optimizable detection subtrees; The method for determining the correlation between the non-optimizable detection subtrees is as follows: Determine the semantic structure consistency between each of the non-optimizable detection subtrees, perform distance quantification according to the semantic structure consistency between each of the non-optimizable detection subtrees, and obtain the distance between each of the non-optimizable detection subtrees; Clustering the non-optimizable detection subtrees according to the distances to obtain a plurality of clusters; wherein each cluster includes at least one non-optimizable detection subtree, and the clusters are used to characterize the correlation between the non-optimizable detection subtrees; The method for compiling and detecting the non-optimizable detection subtree is as follows: Determine a target cluster from the clusters, and determine a first target non-optimizable detection subtree and a second target non-optimizable detection subtree from the target cluster; By using the preset rule method, compile and detect the first target non-optimizable detection subtree to obtain a rule set; Determine a dissimilar node in the second target non-optimizable detection subtree that is dissimilar to the code of the first target non-optimizable detection subtree, and determine a structure code of the dissimilar node and a rule type corresponding to the structure code; Determining a target rule according to the rule set and the rule type; Compile and check the second target non-optimizable detection subtree according to the target rule, return to the step of determining the first target non-optimizable detection subtree and the second target non-optimizable detection subtree from the target cluster, until compile and check all non-optimizable detection subtrees in the target cluster, return to the step of determining the target cluster from the cluster, until compile and check all non-optimizable detection subtrees of each of the clusters.
2. The code compilation detection method according to claim 1, characterized in that: Before the step of matching the abstract syntax tree with several historical normal abstract syntax trees, the method further includes: Performing type analysis, semantic analysis, and data flow analysis on each node in the abstract syntax tree to obtain analysis results; When the analysis result meets the preset requirement, the step of matching the abstract syntax tree with a plurality of historical normal abstract syntax trees is performed.
3. The code compilation detection method according to claim 1, characterized in that: The determining of a candidate matching tree structure from the plurality of tree structures to be matched comprises: Determine the structure code of the root node in each of the tree structures to be matched; The to-be-matched tree structure whose structure code is one of a statement node, an expression node and a control flow node forms a to-be-matched set; A candidate matching tree structure is selected from the to-be-matched set.
4. The code compilation detection method according to claim 1, characterized in that: Determining the semantic structure consistency between the candidate matching tree structure and each of the subtrees includes: Determine a first depth of the candidate matching tree structure and a second depth of each of the subtrees, and respectively calculate an absolute value of a difference between the first depth and each of the second depths; Determine a minimum depth between the first depth and each of the second depths, determine a total number of nodes of each of the subtrees at the minimum depth, and calculate a first product of the minimum depth and the total number of nodes respectively; Respectively determine the number of codes in each node of the candidate matching tree structure that is the same as the number of codes in the node corresponding to the same position in each subtree, and determine the logical similarity value corresponding to each subtree according to the same number; The ratio of the logical similarity value to the first product is determined respectively, and the semantic structure consistency of the candidate matching tree structure and each of the subtrees is obtained according to the reciprocal of the absolute value and the second product of the ratio.
5. The code compilation detection method according to claim 4, characterized in that: The determining whether a target subtree exists according to the semantic structure consistency includes: When the semantic structure consistency is greater than a preset threshold, determining that the candidate matching tree structure has a target subtree; When the semantic structure consistency is less than or equal to a preset threshold, it is determined that the candidate matching tree structure does not have a target subtree.
6. The code compilation detection method according to claim 5, characterized in that: The determining of the unmatched nodes in the optimizable detection subtree comprises: Determine the semantic structure consistency corresponding to all target subtrees of the candidate matching tree structure, and take the target subtree with the greatest semantic structure consistency as the matching subtree; Compare the code in each node of the candidate matching tree structure with the code in the node corresponding to the same position in the matching subtree; The nodes with the same comparison results in the candidate matching tree structure are taken as matching nodes, and the other nodes in the candidate matching tree structure except the matching nodes are taken as unmatched nodes.
7. An automatic compilation platform, characterized in that: The platform is used to implement the code compilation detection method described in any one of claims 1 to 6, and the platform includes: An acquisition module, used to acquire the code to be compiled and construct an abstract syntax tree according to the code to be compiled; A matching module, used for matching the abstract syntax tree with several historical normal abstract syntax trees to determine an optimizable detection subtree and an unoptimizable detection subtree; A determination module, configured to determine unmatched nodes in the optimizable detection subtree, and when the number of the non-optimizable detection subtrees is multiple, determine the correlation between the non-optimizable detection subtrees; A compile detection module is used to perform compile detection on the unmatched node by using a preset rule method, and to perform compile detection on the non-optimizable detection subtree according to the correlation and the preset rule method.
Citation Information
Patent Citations
Code rule checking method and system based on knowledge base feature matching
CN113849413A
Optimized code decompiling method and system based on deep learning
CN114691151A