CN113158627A - Code complexity detection method and device, storage medium and electronic equipment - Google Patents

Code complexity detection method and device, storage medium and electronic equipment Download PDF

Info

Publication number
CN113158627A
CN113158627A CN202110390516.1A CN202110390516A CN113158627A CN 113158627 A CN113158627 A CN 113158627A CN 202110390516 A CN202110390516 A CN 202110390516A CN 113158627 A CN113158627 A CN 113158627A
Authority
CN
China
Prior art keywords
abstract syntax
tree
syntax tree
branch
detecting
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202110390516.1A
Other languages
Chinese (zh)
Inventor
鲁海
肖丽霞
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Weimin Insurance Agency Co Ltd
Original Assignee
Weimin Insurance Agency Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Weimin Insurance Agency Co Ltd filed Critical Weimin Insurance Agency Co Ltd
Priority to CN202110390516.1A priority Critical patent/CN113158627A/en
Publication of CN113158627A publication Critical patent/CN113158627A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F40/00Handling natural language data
    • G06F40/10Text processing
    • G06F40/12Use of codes for handling textual entities
    • G06F40/14Tree-structured documents
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F40/00Handling natural language data
    • G06F40/20Natural language analysis
    • G06F40/205Parsing
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F40/00Handling natural language data
    • G06F40/20Natural language analysis
    • G06F40/279Recognition of textual entities
    • G06F40/289Phrasal analysis, e.g. finite state techniques or chunking

Abstract

The application discloses a code complexity detection method and device, a storage medium and electronic equipment, and belongs to the field of computers. Wherein, the method comprises the following steps: analyzing a syntactic structure of an object code file, and generating an abstract syntactic tree of the object code file, wherein the abstract syntactic tree comprises a plurality of mutually nested conditional branches; and calculating the circle complexity of the target code file in the nesting depth by adopting the abstract syntax tree. The technical problem that the complexity of detecting the circle complexity of the code file is insufficient in the related technology is solved, the complexity of nesting condition statements can be optimized from the code level, and the error rate and the maintenance cost of the code are greatly reduced.

Description

Code complexity detection method and device, storage medium and electronic equipment
Technical Field
The present application relates to the field of computers, and in particular, to a method and an apparatus for detecting code complexity, a storage medium, and an electronic device.
Background
In the related technology, along with the update iteration of daily business requirements, a program is inevitably developed and changed, the high complexity of circles indicates that the code quality is low and the code is difficult to test and maintain, and the daily efficiency is greatly influenced.
Related technology Escript provides a rule, which can detect the circle complexity of a code and compare conditional statement keywords in different lines and columns to judge the circle complexity of the code in a mode of scanning the code line by line. The method of scanning the code line by line is easy to be interfered by the irregularity of the code format, and the accuracy is not enough.
In view of the above problems in the related art, no effective solution has been found at present.
Disclosure of Invention
In order to solve the technical problem or at least partially solve the technical problem, the application provides a code complexity detection method and device, a storage medium and an electronic device.
According to an aspect of an embodiment of the present application, there is provided a method for detecting code complexity, including: analyzing a syntactic structure of an object code file, and generating an abstract syntactic tree of the object code file, wherein the abstract syntactic tree comprises a plurality of mutually nested conditional branches; and calculating the circle complexity of the target code file in the nesting depth by adopting the abstract syntax tree.
According to another aspect of the embodiments of the present application, there is also provided a device for detecting code complexity, including: the generating module is used for analyzing a syntactic structure of the object code file and generating an abstract syntactic tree of the object code file, wherein the abstract syntactic tree comprises a plurality of mutually nested conditional branches; and the calculation module is used for calculating the circle complexity of the target code file in the nesting depth by adopting the abstract syntax tree.
Further, the calculation module includes: the detection unit is used for circularly and recursively detecting the number of edges in the abstract syntax tree and circularly and recursively detecting the number of nodes in the abstract syntax tree, wherein the edges correspond to sequential structure statements in the codes, and the nodes correspond to branch statements in the codes; and the calculating unit is used for determining the circle complexity of the target code file in the nesting depth based on the number of the edges and the number of the nodes.
Further, the detection unit includes: a detecting subunit, configured to recursively detect a key character and a branch character in a downward loop starting from a root node of the AST, where the key character is used to identify a conditional statement, and the branch character is used to identify a conditional branch statement; a counting subunit, configured to add 1 to the number of nodes when the key character is detected in the tree structure of the abstract syntax tree; detecting first branch characters in a tree structure corresponding to the key characters, wherein when one first branch character is detected, the number of the edges is added by 1; and the processing subunit is configured to, for each first branch character, continue to detect in the tree branch structure where the first branch character is located until a leaf node of the abstract syntax tree is obtained, where the number of the nodes is increased by 1, and the leaf node is a node where no branch exists in the tree branch structure.
Further, the detection subunit is further configured to at least one of: detecting a first key character downwards, wherein the first key character is used for identifying an if conditional statement; detecting a second key character downwards, wherein the second key character is used for identifying a switch conditional statement; and downwards detecting a third key character, wherein the third key character is used for identifying the for conditional statement.
Further, the detection unit further includes: the analysis subunit is configured to, before the detection subunit performs downward loop-recursive detection on the key characters and the branch characters from a root node of the abstract syntax tree, analyze a tree structure of the abstract syntax tree, and read the number of the key characters in the abstract syntax tree, where the number of the characters is positively correlated with the nesting depth of the abstract syntax tree; and the determining subunit is used for determining to circularly and recursively detect the key characters and the branch characters from the root node of the abstract syntax tree downwards if the number of the characters is greater than a preset value.
Further, the generating module includes: the dividing unit is used for dividing the code character string in the target code file into lexical units, wherein each lexical unit corresponds to one code block; a generating unit configured to generate a lexical unit list based on the divided plurality of lexical units; and the conversion unit is used for converting the lexical unit list into an abstract syntax tree by taking the variable definition expression as a unit.
Further, the conversion unit includes: the splitting subunit is used for splitting the lexical unit list into a plurality of temporary arrays by taking the variable definition expressions as units, wherein each temporary array corresponds to one variable definition expression, and each variable definition expression is identified by adopting key characters; and the mapping subunit is used for defining an expression aiming at each variable of the lexical unit list and mapping the corresponding temporary array into an abstract syntax tree.
According to another aspect of the embodiments of the present application, there is also provided a storage medium including a stored program that executes the above steps when the program is executed.
According to another aspect of the embodiments of the present application, there is also provided an electronic device, including a processor, a communication interface, a memory, and a communication bus, where the processor, the communication interface, and the memory complete communication with each other through the communication bus; wherein: a memory for storing a computer program; a processor for executing the steps of the method by running the program stored in the memory.
According to an aspect of the application, a computer program product or computer program is provided, comprising computer instructions, the computer instructions being stored in a computer readable storage medium. The processor of the computer device reads the computer instructions from the computer readable storage medium, and the processor executes the computer instructions to cause the computer device to execute the steps of any embodiment of the code complexity detection method.
Compared with the prior art, the technical scheme provided by the embodiment of the application has the following advantages:
in the embodiment of the application, the AST is adopted to calculate the circle complexity of the code, so that a circle complexity detection mechanism based on the nested depth dimension of the conditional statements is realized, the technical problem that the precision of detecting the circle complexity of the code file is insufficient in the related technology is solved, the complexity of the nested conditional statements can be optimized from the code level, and the error rate and the maintenance cost of the code are greatly reduced.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and together with the description, serve to explain the principles of the application.
In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings needed to be used in the description of the embodiments or the prior art will be briefly described below, and it is obvious for those skilled in the art to obtain other drawings without inventive exercise.
FIG. 1 is a flow chart of a method for detecting code complexity according to an embodiment of the present application;
fig. 2 is a diagram illustrating a conversion of a code block into an AST according to an embodiment of the present application;
fig. 3 is an AST diagram of three nested expressions in an embodiment of the present application;
FIG. 4 is a diagram illustrating mapping between expressions and codes in an embodiment of the present application;
FIG. 5 is a control flow diagram of an expression in an embodiment of the present application;
fig. 6 is a schematic diagram of AST in an embodiment of the present application;
FIG. 7 is a block diagram of an apparatus for detecting code complexity according to an embodiment of the present application;
fig. 8 is a block diagram of an electronic device according to an embodiment of the present application.
Detailed Description
In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be described clearly and completely with reference to the drawings in the embodiments of the present application, it is obvious that the described embodiments are some embodiments of the present application, but not all embodiments, and the illustrative embodiments and descriptions thereof of the present application are used for explaining the present application and do not constitute a limitation to the present application. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
It is noted that, in this document, relational terms such as "first" and "second," and the like, may be used solely to distinguish one entity or action from another similar entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other identical elements in a process, method, article, or apparatus that comprises the element.
In this embodiment, a method for detecting code complexity is provided, and fig. 1 is a flowchart of a method for detecting code complexity according to an embodiment of the present application, and as shown in fig. 1, the flowchart includes the following steps:
step S102, analyzing a syntactic structure of a target code file, and generating an abstract syntactic tree of the target code file, wherein the abstract syntactic tree comprises a plurality of mutually nested conditional branches;
in this embodiment, an Abstract Syntax Tree (AST) is a Tree representation of the Abstract Syntax structure of the source code in the object code file, with each node on the Tree representing a structure in the source code. The source code of the object code file of the embodiment comprises a plurality of conditional branches which are nested at different levels of depth.
Step S104, calculating the circle complexity of the target code file in the nesting depth by adopting the abstract syntax tree;
the round complexity (also called conditional complexity) of this embodiment is a measure of the complexity of the code, and the larger the round complexity is, the more complex the code in the target code file is.
Through the steps, the syntactic structure of the target code file is analyzed, the abstract syntactic tree of the target code file is generated, the abstract syntactic tree comprises a plurality of mutually nested conditional branches, the abstract syntactic tree is adopted to calculate the circle complexity of the target code file in the nesting depth, and the circle complexity of the code is calculated through the abstract syntactic tree, so that a circle complexity detection mechanism based on the nesting depth dimension of the conditional statements is realized, the technical problem that the accuracy of detecting the circle complexity of the code file by using the related technology is insufficient is solved, the complexity of the nested conditional statements can be optimized from the code level, and the error rate and the maintenance cost of the code are greatly reduced.
In an implementation manner of this embodiment, parsing the syntax structure of the object code file, and generating the abstract syntax tree of the object code file includes: dividing the code character string in the target code file into lexical units, wherein each lexical unit corresponds to one code block; generating a lexical unit list based on the divided plurality of lexical units; and converting the lexical unit list into an abstract syntax tree by taking a variable definition expression as a unit.
Optionally, converting the lexical unit list into an abstract syntax tree by using a variable definition expression as a unit includes: splitting the lexical unit list into a plurality of temporary arrays by taking the variable definition expressions as units, wherein each temporary array corresponds to one variable definition expression, and each variable definition expression is identified by adopting key characters; and defining an expression aiming at each variable of the lexical unit list, and mapping the corresponding temporary array into an abstract syntax tree.
In one example, the key character of a variable definition expression (abbreviated as expression) is VariableDeclaration, and the temporary array is: VariableDeclaration, VariableDeclarator, Identifier, leral.
Firstly, dividing code character strings in a target code file into meaningful code blocks, wherein the code blocks are called lexical units (tokens), the meaningful code blocks are minimum character string units which can be compiled and executed, the code blocks comprise variable types, variable names and the like, and by dividing the code character strings in the target code file into the code blocks, all syntactic structures of the target code file can be analyzed, and then a complete abstract syntactic tree of the target code file is generated; these lexical units are then placed into a token list, as follows:
Figure BDA0003016542040000061
in JavaScript, the smallest logical execution unit is an expression, such as let value ═ 0, if () in the above code, each expression has a key character to identify the expression, such as variable declaration, ifstate, etc., and the present embodiment constructs an abstract syntax tree by recognizing these key characters.
And converting the token array list obtained by lexical analysis into a tree form. If the definition expression VariableDeclaration is divided into 3 groups of VariableDeclarator, Identifier and Literal which are nodes in the abstract syntax tree respectively, the IfStatement is divided into 3 groups of Identifier, BlockStatement and BlockStatement which are nodes in the abstract syntax tree respectively.
Fig. 2 is a schematic diagram of converting a code block into an AST in the embodiment of the present application, the right side in fig. 2 is an abstract syntax tree of each expression, and the abstract syntax tree is a tree branch of the abstract syntax tree of a target code file, and the abstract syntax tree of a complete code fragment is formed by circularly nesting various expressions, fig. 3 is a schematic diagram of an AST in the embodiment of the present application, a blockastate in fig. 3 is a code block, and an expressonstate is an assignment expression (a type of variable definition expression), which are both key characters identifying various expressions, or can be split into multiple components. Fig. 4 is a schematic diagram of mapping between expressions and codes in the embodiment of the present application, where the left side is an expression, and the right side is a disassembled temporary array.
In this embodiment, calculating the round complexity of the target code file at the nesting depth by using the AST includes:
s11, circularly and recursively detecting the number of edges in the abstract syntax tree and circularly and recursively detecting the number of nodes in the abstract syntax tree, wherein the edges correspond to sequential structure statements in the codes, and the nodes correspond to branch statements in the codes;
in some embodiments, circularly recursively detecting the number of edges in the abstract syntax tree and circularly recursively detecting the number of nodes in the abstract syntax tree comprises: starting from a root node of the abstract syntax tree, circularly and recursively detecting key characters and branch characters, wherein the key characters are used for identifying conditional statements, and the branch characters are used for identifying conditional branch statements; when the key character is detected in the tree structure of the abstract syntax tree, adding 1 to the number of the nodes; detecting first branch characters in a tree structure corresponding to the key characters, wherein when one first branch character is detected, the number of the edges is added by 1; and for each first branch character, continuing to detect in the tree branch structure in which the first branch character is located until the number of the nodes is increased by 1 until the leaf nodes of the abstract syntax tree are nodes in which no branch exists in the tree branch structure. In the detection process of each first branch character, when the key character is detected in the tree branch structure of the first branch character, the number of nodes is added by 1, and when each second branch character is detected in the tree branch structure of the first branch character, the number of edges is added by 1.
Optionally, the recursive detection of key characters in the downward loop may be, but is not limited to: detecting a first key character downwards, wherein the first key character is used for identifying an if conditional statement; detecting a second key character downwards, wherein the second key character is used for identifying a switch conditional statement; and downwards detecting a third key character, wherein the third key character is used for identifying the for conditional statement. Of course, the scheme of the embodiment may also be applied to other types of conditional statements.
Fig. 5 is a control flow diagram of an expression in the embodiment of the present application, which corresponds to a sequential structure statement, a switch branch statement, an if branch statement, and a for branch statement, respectively.
S12, determining the circle complexity of the target code file in the nesting depth based on the number of the edges and the number of the nodes;
in one example, the degree of circularity v (g) of the object code file at nesting depth is calculated using the following formula: v (g) ═ e-n +2, where e corresponds to the number of edges and n corresponds to the number of nodes.
In some implementation scenarios, before recursively detecting key characters and branch characters in a downward loop starting from a root node of the AST, further comprising: analyzing the tree structure of the AST, and reading the character number of key characters in the AST, wherein the character number is positively correlated with the nesting depth of the AST; and if the number of the characters is larger than a preset value, determining that key characters and branch characters are detected in a downward circulating recursion mode from a root node of the AST.
The scheme of the embodiment is related to the complexity of the conditional statement, so the key definition expression ifStatement of the if expression is to be detected in the abstract syntax tree. Firstly, converting codes of the target code file into a corresponding abstract syntax tree, wherein the abstract syntax tree comprises the following two sections of codes:
code one:
If(a){
}elseif(b){
}else{
}
code two:
Figure BDA0003016542040000091
the conditional statement segment of the code I has three branches which are all conditional branches under the same node; and the conditional branches of the conditional statement fragment of code two are nested with one another. Fig. 6 is a diagram of AST in an embodiment of the present application, where the converted abstract syntax trees correspond to the first code and the second code, respectively.
The circle complexity detection is not limited to the statistics and detection of the complexity of the conditional statements of the codes under the same node, but also can detect a plurality of conditional statements under the nested node, and as the business logic becomes more and more complex, the phenomenon of excessive conditional statements under the same node cannot be avoided (cannot be solved from the code level), while the conditional statements of the nested node can be optimized from the code level and are not influenced by the business. In some implementation scenarios, the detection of the conditional statement complexity is based on the conditional statement complexity detection under the same node, such as code one, in other implementation scenarios, the detection of the conditional statement complexity of the nested conditional statement is to be solved, such as code two, and the circle complexity detection and calculation method is described by taking code two as an example:
three IfStatements can be seen from the abstract syntax tree of the second code, and the calculation formula V (G) of the circle complexity is adopted, wherein e is equal to e-n +2(e represents the number of edges in the control flow graph (corresponding to the part of the sequence structure in the code), n represents the number of nodes in the control flow graph, and the calculation formula comprises a starting point and an end point (a, all the end points are calculated only once, even if a plurality of return or throw exists; b, the nodes correspond to branch statements in the code)), so that the number of e and n is required to be known.
Presetting an e and an n initial value to be 0, starting from the uppermost ifStatement, when the code detects that an ifStatement keyword exists, indicating that an if condition statement exists, which is a starting point, at this time, the n count is increased by 1, in FIG. 6, a dependent is an if () { } branch of the condition statement, a branch of an else { } or an else if () { } is represented as an alternative, and the ifStatement node only has a dependent branch, so that the e is increased by 1; detecting continuously along the join branch, wherein the code block has an ifState element which indicates that the code block has an if conditional statement, n is added with 1, the conditional statement has two branches of join and alternative, e is added with 2, the code block continues along the alternative branch, no more ifState element key word exists and reaches a leaf node (a node without a branch), so that the end point of the path is reached, n is added with 1 (the end point n is not added with 1), the join branch has an ifState element key word, and according to the cyclic recursion statistics of the previous steps, the result is that e is equal to 4, n is equal to 4, and the loop complexity of the code two is 2(4-4+2 is equal to 2).
In the process of writing js codes, along with the expansion and increase of functions, the code complexity also rises, and in order to ensure that the codes are not easy to make mistakes and difficult to maintain, the detection of the complexity of circles and the corresponding code optimization are particularly important.
With the update iteration of daily business requirements, programs are inevitably developed and changed, the high complexity of circles indicates that the code quality is low and the code is difficult to test and maintain, and the daily efficiency is greatly influenced. The detection of the circle complexity and the corresponding optimization of the code with high complexity are particularly important, and with the development of business logic becoming more and more complex, the phenomenon that the code has too many conditional statements under the nodes of the same layer cannot be avoided, but the complexity of the nested conditional statements can be optimized from the code level. Therefore, the program for detecting the complexity of the code nested conditional statement has great benefits, and the error rate and the maintenance cost of the code are greatly reduced.
Through the above description of the embodiments, those skilled in the art can clearly understand that the method according to the above embodiments can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware, but the former is a better implementation mode in many cases. Based on such understanding, the technical solutions of the present application may be embodied in the form of a software product, which is stored in a storage medium (e.g., ROM/RAM, magnetic disk, optical disk) and includes instructions for enabling a terminal device (e.g., a mobile phone, a computer, a server, or a network device) to execute the method according to the embodiments of the present application.
In this embodiment, a device for detecting code complexity is further provided, which is used to implement the foregoing embodiments and preferred embodiments, and the description already made is omitted for brevity. As used below, the term "module" may be a combination of software and/or hardware that implements a predetermined function. Although the means described in the embodiments below are preferably implemented in software, an implementation in hardware, or a combination of software and hardware is also possible and contemplated.
Fig. 7 is a block diagram of a code complexity detection apparatus according to an embodiment of the present application, and as shown in fig. 7, the apparatus includes: a generation module 70, a calculation module 72, wherein,
a generating module 70, configured to parse a syntax structure of an object code file and generate an abstract syntax tree of the object code file, where the abstract syntax tree includes a plurality of mutually nested conditional branches;
and the calculating module 72 is configured to calculate the round complexity of the target code file in the nesting depth by using the abstract syntax tree.
Optionally, the calculation module includes: the detection unit is used for circularly and recursively detecting the number of edges in the abstract syntax tree and circularly and recursively detecting the number of nodes in the abstract syntax tree, wherein the edges correspond to sequential structure statements in the codes, and the nodes correspond to branch statements in the codes; and the calculating unit is used for determining the circle complexity of the target code file in the nesting depth based on the number of the edges and the number of the nodes.
Optionally, the detecting unit includes: a detecting subunit, configured to recursively detect a key character and a branch character in a downward loop starting from a root node of the AST, where the key character is used to identify a conditional statement, and the branch character is used to identify a conditional branch statement; a counting subunit, configured to add 1 to the number of nodes when the key character is detected in the tree structure of the abstract syntax tree; detecting first branch characters in a tree structure corresponding to the key characters, wherein when one first branch character is detected, the number of the edges is added by 1; and the processing subunit is configured to, for each first branch character, continue to detect in the tree branch structure where the first branch character is located until a leaf node of the abstract syntax tree is obtained, where the number of the nodes is increased by 1, and the leaf node is a node where no branch exists in the tree branch structure.
Optionally, the detection subunit is further configured to at least one of: detecting a first key character downwards, wherein the first key character is used for identifying an if conditional statement; detecting a second key character downwards, wherein the second key character is used for identifying a switch conditional statement; and downwards detecting a third key character, wherein the third key character is used for identifying the for conditional statement.
Optionally, the detecting unit further includes: the analysis subunit is configured to, before the detection subunit performs downward loop-recursive detection on the key characters and the branch characters from a root node of the abstract syntax tree, analyze a tree structure of the abstract syntax tree, and read the number of the key characters in the abstract syntax tree, where the number of the characters is positively correlated with the nesting depth of the abstract syntax tree; and the determining subunit is used for determining to circularly and recursively detect the key characters and the branch characters from the root node of the abstract syntax tree downwards if the number of the characters is greater than a preset value.
Optionally, the generating module includes: the dividing unit is used for dividing the code character string in the target code file into lexical units, wherein each lexical unit corresponds to one code block; a generating unit configured to generate a lexical unit list based on the divided plurality of lexical units; and the conversion unit is used for converting the lexical unit list into an abstract syntax tree by taking the variable definition expression as a unit.
Optionally, the conversion unit includes: the splitting subunit is used for splitting the lexical unit list into a plurality of temporary arrays by taking the variable definition expressions as units, wherein each temporary array corresponds to one variable definition expression, and each variable definition expression is identified by adopting key characters; and the mapping subunit is used for defining an expression aiming at each variable of the lexical unit list and mapping the corresponding temporary array into an abstract syntax tree.
It should be noted that, the above modules may be implemented by software or hardware, and for the latter, the following may be implemented, but not limited to: the modules are all positioned in the same processor; alternatively, the modules are respectively located in different processors in any combination.
Fig. 8 is a structural diagram of an electronic device according to an embodiment of the present application, and as shown in fig. 8, the electronic device includes a processor 81, a communication interface 82, a memory 83, and a communication bus 84, where the processor 81, the communication interface 82, and the memory 83 complete communication with each other through the communication bus 84,
a memory 83 for storing a computer program;
the processor 81 is configured to implement the method for detecting the complexity of the code according to any one of the embodiments when executing the program stored in the memory 83.
The communication bus mentioned in the above terminal may be a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, or the like. The communication bus may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is shown, but this does not mean that there is only one bus or one type of bus.
The communication interface is used for communication between the terminal and other equipment.
The Memory may include a Random Access Memory (RAM) or a non-volatile Memory (non-volatile Memory), such as at least one disk Memory. Optionally, the memory may also be at least one memory device located remotely from the processor.
The Processor may be a general-purpose Processor, and includes a Central Processing Unit (CPU), a Network Processor (NP), and the like; the Integrated Circuit may also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field Programmable Gate Array (FPGA) or other Programmable logic device, a discrete Gate or transistor logic device, or a discrete hardware component.
In another embodiment provided by the present application, a computer-readable storage medium is further provided, where instructions are stored in the computer-readable storage medium, and when the instructions are executed on a computer, the instructions cause the computer to perform the method for detecting code complexity in any one of the above embodiments.
In yet another embodiment provided herein, a computer program product or computer program is provided that includes computer instructions stored in a computer-readable storage medium. The processor of the computer device reads the computer instructions from the computer readable storage medium, and the processor executes the computer instructions to cause the computer device to execute the steps of any embodiment of the code complexity detection method.
In the above embodiments, the implementation may be wholly or partially realized by software, hardware, firmware, or any combination thereof. When implemented in software, may be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When loaded and executed on a computer, cause the processes or functions described in accordance with the embodiments of the application to occur, in whole or in part. The computer may be a general purpose computer, a special purpose computer, a network of computers, or other programmable device. The computer instructions may be stored in a computer readable storage medium or transmitted from one computer readable storage medium to another, for example, from one website site, computer, server, or data center to another website site, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, Digital Subscriber Line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device, such as a server, a data center, etc., that incorporates one or more of the available media. The usable medium may be a magnetic medium (e.g., floppy Disk, hard Disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., Solid State Disk (SSD)), among others.
The above description is only for the preferred embodiment of the present application, and is not intended to limit the scope of the present application. Any modification, equivalent replacement, improvement and the like made within the spirit and principle of the present application are included in the protection scope of the present application.
The above description is merely exemplary of the present application and is presented to enable those skilled in the art to understand and practice the present application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the application. Thus, the present application is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims (10)

1. A method for detecting code complexity, comprising:
analyzing a syntactic structure of an object code file, and generating an abstract syntactic tree of the object code file, wherein the abstract syntactic tree comprises a plurality of mutually nested conditional branches;
and calculating the circle complexity of the target code file in the nesting depth by adopting the abstract syntax tree.
2. The method of claim 1, wherein computing the round-robin complexity of the object code file at the nesting depth using the abstract syntax tree comprises:
circularly and recursively detecting the number of edges in the abstract syntax tree and circularly and recursively detecting the number of nodes in the abstract syntax tree, wherein the edges correspond to sequential structure statements in the codes, and the nodes correspond to branch statements in the codes;
and determining the circle complexity of the target code file in the nesting depth based on the number of the edges and the number of the nodes.
3. The method of claim 2, wherein recursively detecting the number of edges in the abstract syntax tree and recursively detecting the number of nodes in the abstract syntax tree comprises:
starting from a root node of the abstract syntax tree, circularly and recursively detecting key characters and branch characters, wherein the key characters are used for identifying conditional statements, and the branch characters are used for identifying conditional branch statements;
when the key character is detected in the tree structure of the abstract syntax tree, adding 1 to the number of the nodes;
detecting first branch characters in a tree structure corresponding to the key characters, wherein when one first branch character is detected, the number of the edges is added by 1;
and for each first branch character, continuing to detect in the tree branch structure in which the first branch character is located until the number of the nodes is increased by 1 until the leaf nodes of the abstract syntax tree are nodes in which no branch exists in the tree branch structure.
4. The method of claim 3, wherein recursively detecting key characters in a loop comprises at least one of:
detecting a first key character downwards, wherein the first key character is used for identifying an if conditional statement;
detecting a second key character downwards, wherein the second key character is used for identifying a switch conditional statement;
and downwards detecting a third key character, wherein the third key character is used for identifying the for conditional statement.
5. The method of claim 3, wherein prior to recursively detecting key characters and branch characters in a downward loop starting from a root node of the abstract syntax tree, the method further comprises:
analyzing the tree structure of the abstract syntax tree, and reading the character number of key characters in the abstract syntax tree, wherein the character number is positively correlated with the nesting depth of the abstract syntax tree;
and if the number of the characters is larger than a preset value, determining that key characters and branch characters are detected in a downward circulating and recursive manner from the root node of the abstract syntax tree.
6. The method of claim 1, wherein parsing the syntax structure of the object code file and generating the abstract syntax tree of the object code file comprises:
dividing the code character string in the target code file into lexical units, wherein each lexical unit corresponds to one code block;
generating a lexical unit list based on the divided plurality of lexical units;
and converting the lexical unit list into an abstract syntax tree by taking the variable definition expression as a unit.
7. The method of claim 6, wherein converting the lexical unit list to an abstract syntax tree in units of variable definition expressions comprises:
dividing the lexical unit list into a plurality of temporary arrays by taking a variable definition expression as a unit, wherein each temporary array corresponds to one variable definition expression, and each variable definition expression is identified by adopting key characters;
and defining an expression aiming at each variable of the lexical unit list, and mapping the corresponding temporary array into an abstract syntax tree.
8. An apparatus for detecting code complexity, comprising:
the generating module is used for analyzing a syntactic structure of the object code file and generating an abstract syntactic tree of the object code file, wherein the abstract syntactic tree comprises a plurality of mutually nested conditional branches;
and the calculation module is used for calculating the circle complexity of the target code file in the nesting depth by adopting the abstract syntax tree.
9. A storage medium, characterized in that the storage medium comprises a stored program, wherein the program is operative to perform the method steps of any of the preceding claims 1 to 7.
10. An electronic device comprises a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory are communicated with each other through the communication bus; wherein:
a memory for storing a computer program;
a processor for performing the method steps of any of claims 1 to 7 by executing a program stored on a memory.
CN202110390516.1A 2021-04-12 2021-04-12 Code complexity detection method and device, storage medium and electronic equipment Pending CN113158627A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110390516.1A CN113158627A (en) 2021-04-12 2021-04-12 Code complexity detection method and device, storage medium and electronic equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110390516.1A CN113158627A (en) 2021-04-12 2021-04-12 Code complexity detection method and device, storage medium and electronic equipment

Publications (1)

Publication Number Publication Date
CN113158627A true CN113158627A (en) 2021-07-23

Family

ID=76890058

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110390516.1A Pending CN113158627A (en) 2021-04-12 2021-04-12 Code complexity detection method and device, storage medium and electronic equipment

Country Status (1)

Country Link
CN (1) CN113158627A (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113918951A (en) * 2021-12-16 2022-01-11 北京微步在线科技有限公司 Malicious code detection method and device based on abstract syntax tree and electronic equipment
CN116302043A (en) * 2023-05-25 2023-06-23 深圳市明源云科技有限公司 Code maintenance problem detection method and device, electronic equipment and readable storage medium

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113918951A (en) * 2021-12-16 2022-01-11 北京微步在线科技有限公司 Malicious code detection method and device based on abstract syntax tree and electronic equipment
CN113918951B (en) * 2021-12-16 2022-03-22 北京微步在线科技有限公司 Malicious code detection method and device based on abstract syntax tree and electronic equipment
CN116302043A (en) * 2023-05-25 2023-06-23 深圳市明源云科技有限公司 Code maintenance problem detection method and device, electronic equipment and readable storage medium
CN116302043B (en) * 2023-05-25 2023-10-10 深圳市明源云科技有限公司 Code maintenance problem detection method and device, electronic equipment and readable storage medium

Similar Documents

Publication Publication Date Title
CN109062794B (en) Method and device for determining software evaluation result and electronic equipment
US9875264B2 (en) Identifying properties of a communication device
CN101751333A (en) Method, computer program and computer system for assisting in analyzing program
CN113158627A (en) Code complexity detection method and device, storage medium and electronic equipment
US10482175B2 (en) Identifying properties of a communication device
CN106295346B (en) Application vulnerability detection method and device and computing equipment
CN108710662B (en) Language conversion method and device, storage medium, data query system and method
CN111124479B (en) Method and system for analyzing configuration file and electronic equipment
CN112328732A (en) Sensitive word detection method and device and sensitive word tree construction method and device
WO2023241529A1 (en) Vulnerability information processing method, service apparatus and vulnerability detection module
CN105260357A (en) Sensitive word checking method and device based on Hash sensitive words directed graph
CN112181430A (en) Code change statistical method and device, electronic equipment and storage medium
CN109189395A (en) A kind of data analysis method and device
CN114265860A (en) Execution statement identification method and device
CN113987405A (en) AST-based mathematical expression calculation algorithm
CN112000690B (en) Method and device for analyzing structured operation statement
CN111309301B (en) Program language conversion method, device and conversion equipment
TWI746520B (en) Method and device for compiling computer language
CN117076727A (en) Query statement analysis method, device, equipment and storage medium
CN109284268A (en) A kind of method, system and the electronic equipment of fast resolving log
JP6261669B2 (en) Query calibration system and method
CN113946516A (en) Code coverage rate determining method and device and storage medium
CN110244954B (en) Compiling method and equipment for application program
Chen et al. CGAP-align: a high performance DNA short read alignment tool
CN110543642A (en) Translation method and device based on machine translation engine

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination