CN117539492A - Method and device for deleting redundant sentences in codes, electronic equipment and storage medium - Google Patents

Method and device for deleting redundant sentences in codes, electronic equipment and storage medium Download PDF

Info

Publication number
CN117539492A
CN117539492A CN202210916488.7A CN202210916488A CN117539492A CN 117539492 A CN117539492 A CN 117539492A CN 202210916488 A CN202210916488 A CN 202210916488A CN 117539492 A CN117539492 A CN 117539492A
Authority
CN
China
Prior art keywords
file
header
graph
directed acyclic
symbols
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
CN202210916488.7A
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.)
Tencent Technology Shanghai Co Ltd
Original Assignee
Tencent Technology Shanghai 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 Tencent Technology Shanghai Co Ltd filed Critical Tencent Technology Shanghai Co Ltd
Priority to CN202210916488.7A priority Critical patent/CN117539492A/en
Publication of CN117539492A publication Critical patent/CN117539492A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/443Optimisation
    • G06F8/4434Reducing the memory space required by the program code
    • G06F8/4435Detection or removal of dead or redundant code
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • G06F8/427Parsing

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Stored Programmes (AREA)

Abstract

The application provides a method and a device for deleting redundant sentences in codes, electronic equipment and a storage medium, wherein the method comprises the following steps: acquiring a source file and a header file of a code project file, and acquiring an initial reference relation between the files, wherein the initial reference relation is used for describing a file reference path of a symbol used by the source file and a file reference path of a symbol used by the header file; generating an initial directed acyclic graph with a source file and a header file as graph nodes based on the initial reference relation; node screening is carried out on the initial directed acyclic graph based on the used symbols of the source file, the local symbols directly declared by the header files and the local symbol quantity of the header files, so as to obtain a target directed acyclic graph; and deleting redundant sentences used for referencing other header files in the source file and the header file according to the target reference relation between the graph nodes described by the target directed acyclic graph. According to the embodiment of the application, the compiling efficiency of the code project file can be improved.

Description

Method and device for deleting redundant sentences in codes, electronic equipment and storage medium
Technical Field
The present invention relates to the field of code analysis, and in particular, to a method and apparatus for deleting redundant statements in a code, an electronic device, and a storage medium.
Background
In the process of compiling code project files, developers often write statements for referencing header files, through which header files are embedded into source files. In practical development, some unnecessary redundant sentences are often included in the code project file, so that the compiling time of the code project file is unnecessarily increased, and the compiling efficiency of the code project file is lower.
Disclosure of Invention
An object of the present invention is to provide a method, an apparatus, an electronic device, and a storage medium for deleting redundant sentences in a code, which can improve the compiling efficiency of a code item file.
According to an aspect of an embodiment of the present application, a method for deleting redundant statements in a code is disclosed, where the method includes:
acquiring a source file and a header file of a code project file, and acquiring an initial reference relation between the files, wherein the initial reference relation is used for describing a file reference path of a symbol used by the source file and a file reference path of a symbol used by the header file;
Generating an initial directed acyclic graph taking the source file and the header file as graph nodes based on the initial reference relation;
node screening is carried out on the initial directed acyclic graph based on the used symbols of the source file, the local symbols directly declared by the header files and the local symbol quantity of the header files, so that a target directed acyclic graph is obtained;
and deleting redundant sentences used for referencing other header files in the source file and the header file according to the target reference relation between the graph nodes described by the target directed acyclic graph.
According to an aspect of an embodiment of the present application, a device for deleting redundant statements in a code is disclosed, where the device includes:
the acquisition module is configured to acquire a source file and a header file of a code project file, and acquire an initial reference relation between the files, wherein the initial reference relation is used for describing a file reference path of a symbol used by the source file and a file reference path of a symbol used by the header file;
a graph generating module configured to generate an initial directed acyclic graph with the source file and the header file as graph nodes based on the initial reference relationship;
The node screening module is configured to screen nodes of the initial directed acyclic graph based on the symbols used by the source file, the local symbols directly declared by the header files and the local symbol numbers of the header files, so as to obtain a target directed acyclic graph;
and the deleting module is configured to delete redundant sentences used for referencing other header files in the source file and the header file according to the target reference relation between the graph nodes described by the target directed acyclic graph.
According to an aspect of an embodiment of the present application, an electronic device is disclosed, including: one or more processors; and storage means for storing one or more programs that, when executed by the one or more processors, cause the electronic device to implement the methods provided in the various alternative implementations described above.
According to an aspect of embodiments of the present application, a computer program medium having computer readable instructions stored thereon, which when executed by a processor of a computer, cause the computer to perform the methods provided in the various alternative implementations described above is disclosed.
According to an aspect of embodiments of the present application, there is provided a computer program product or computer program comprising computer instructions stored in a computer readable storage medium. The computer instructions are read from the computer-readable storage medium by a processor of a computer device, and executed by the processor, cause the computer device to perform the methods provided in the various alternative implementations described above.
In the embodiment of the application, based on the initial reference relation between the source file and the header file of the code project file, an initial directed acyclic graph taking the source file and the header file as graph nodes is generated, and based on the used symbols of the source file, the local symbols directly declared by the header file and the local symbol numbers of the header file, the target directed acyclic graph corresponding to the code project file with effectively reduced compiling symbol numbers can be obtained after node screening is carried out on the initial directed acyclic graph. Therefore, according to the target reference relation between the graph nodes described by the target directed acyclic graph, after deleting redundant sentences used for referencing other header files in the source file and the header file, the code project file with effectively reduced number of required compiling symbols can be obtained, so that the compiling time of the code project file can be effectively reduced, and the compiling efficiency of the code project file is improved.
Other features and advantages of the present application will be apparent from the following detailed description, or may be learned in part by the practice of the application.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the application.
Drawings
The above and other objects, features and advantages of the present application will become more apparent by describing in detail exemplary embodiments thereof with reference to the attached drawings.
FIG. 1 illustrates a schematic diagram of an exemplary system architecture according to one embodiment of the present application.
FIG. 2 illustrates a flow chart of a method of deleting redundant statements in code according to one embodiment of the present application.
FIG. 3 illustrates a schematic diagram of the number of symbols required to compile a code item file versus the compile time of the code item file, according to one embodiment of the present application.
FIG. 4 illustrates a schematic diagram of a delete flow of redundant statements in code according to one embodiment of the present application.
Fig. 5 illustrates a node screening schematic of a directed acyclic graph according to an embodiment of the application.
FIG. 6 illustrates a block diagram of a means for deleting redundant statements in code in accordance with one embodiment of the present application.
FIG. 7 illustrates a block diagram of a means for deleting redundant statements in code in accordance with one embodiment of the present application.
FIG. 8 illustrates an electronic device hardware diagram according to one embodiment of the present application.
Detailed Description
Example embodiments will now be described more fully with reference to the accompanying drawings. However, the exemplary embodiments may be embodied in many forms and should not be construed as limited to the examples set forth herein; rather, these example embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the concept of the example embodiments to those skilled in the art. The drawings are merely schematic illustrations of the present application and are not necessarily drawn to scale. The same reference numerals in the drawings denote the same or similar parts, and thus a repetitive description thereof will be omitted.
Furthermore, the described features, structures, or characteristics may be combined in any suitable manner in one or more example embodiments. In the following description, numerous specific details are provided to give a thorough understanding of example embodiments of the present application. One skilled in the relevant art will recognize, however, that the aspects of the application may be practiced without one or more of the specific details, or with other methods, components, steps, etc. In other instances, well-known structures, methods, implementations, or operations are not shown or described in detail to avoid obscuring aspects of the application.
Some of the block diagrams shown in the figures are functional entities and do not necessarily correspond to physically or logically separate entities. These functional entities may be implemented in software or in one or more hardware modules or integrated circuits or in different networks and/or processor devices and/or microcontroller devices.
The method for deleting redundant sentences in the codes is mainly used for deleting the redundant sentences used for referencing other header files in source files and header files of code item files, and can be applied to the field of games. Wherein, referencing other header files refers to source files referencing header files, and header files other than the header files referencing themselves. Redundant statements refer to non-essential statements that, even if deleted, do not destroy the integrity of the code item file at compile time.
Take the application in the c++ environment as an example. One or more C++ projects can be contained in a C++ project file with a suffix of sln, and each C++ project can also contain a plurality of source files with a suffix of cpp and a plurality of head files with a suffix of h. The cpp source file may be referred to by a #include statement, and the h header file may be referred to by a #include statement. In many cases, the reference relationship between files can be described by one #include statement, but in practice, the reference relationship will be described by a plurality of parallel #include statements. These parallel #include statements all require compilation at the time of compilation of the sln project file, resulting in an unnecessary increase in compilation time. By deleting redundant #include sentences among the parallel #include sentences, the time taken for compiling the sln item file can be reduced, and the compiling efficiency can be improved.
FIG. 1 shows a schematic diagram of an exemplary system architecture of an embodiment of the present application.
As shown in fig. 1, the execution body for deleting redundant sentences in the code in the system architecture includes: one or more of a server 101, a portable computer 102, a tablet 103, and a smartphone 104. The server 101 may be an independent physical server, a server cluster or a distributed system formed by a plurality of physical servers, or a cloud server that provides cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDNs, basic cloud computing services such as big data and artificial intelligence platforms.
As shown in fig. 1, the number of header files of the code item files 20 after the redundant statement deletion is reduced, the time taken for compiling the code item files 20 is reduced, and the compiling efficiency is improved, compared to before the redundant statement deletion.
It should be noted that, after the redundant statement is deleted, the header file of the code item file 20 can provide necessary support for various functions that are originally required for the code item file 20. Thus, the number of header files is reduced without compromising the compilation feasibility of the code item files 20 and the functional integrity of the compiled files.
It should also be noted that the present embodiment is merely exemplary to illustrate the system architecture and the performance of the present application, and should not limit the functions and the application scope of the present application.
Fig. 2 is a flowchart of a method for deleting redundant statements in code provided in an embodiment of the present application, where an exemplary execution body of the method is a server, and the method includes:
step S210, acquiring a source file and a header file of a code project file, and acquiring an initial reference relation between the files, wherein the initial reference relation is used for describing a file reference path of a symbol used by the source file and a file reference path of a symbol used by the header file;
Step S220, generating an initial directed acyclic graph with a source file and a header file as graph nodes based on the initial reference relation;
step S230, based on the used symbols of the source file, the local symbols directly declared by the header files and the local symbol numbers of the header files, node screening is carried out on the initial directed acyclic graph to obtain a target directed acyclic graph;
and step S240, deleting redundant sentences used for referencing other header files in the source file and the header file according to the target referencing relation between the graph nodes described by the target directed acyclic graph.
Specifically, in the embodiment of the present application, for a code item file that needs to be subjected to redundant statement deletion processing, a source file and a header file of the code item file are extracted, and an initial reference relationship between the files is obtained.
The initial reference relationship between files is mainly used for describing the file reference path of the symbol used by the source file and the file reference path of the symbol used by the header file. Where a symbol refers to a syntax structural element of the code, including but not limited to constants, variables, etc. in the code.
Further, the file reference path of the symbol used by the source file refers to the path of the header file required to be sequentially referenced by the source file for using the required symbol; similarly, the file reference path of the symbol used by the header file refers to the path of the header file beyond itself that is required to be sequentially referenced for the header file to use the required symbol. For example: the source file a is a symbol, and needs to refer to the header file B first, and then refer to the header file C that declares the symbol. The source file a uses the file reference path of the symbol as "a- > B- > C".
It follows that the initial reference relationship describes the file reference paths that are actually available when the source file uses a notation, and the file reference paths that are actually available when the header file uses a notation. And further, taking the source file and the header file as graph nodes, and organizing the direct topological relation of the graph nodes based on the file reference path described by the initial reference relation to obtain the corresponding initial directed acyclic graph. The directed acyclic graph refers to a graph in which edges are directed, and from any node, the directed acyclic graph cannot return to the node from the node through a plurality of edges.
The initial directed acyclic graph describes all available statements in the source file and header file that are used to reference other header files prior to deleting redundant statements. Specifically, each edge in the initial directed acyclic graph describes one of the available statements in the source file and header file that are used to reference other header files. Therefore, the deletion of redundant statements may translate into the deletion of redundant edges and redundant nodes in the initial directed acyclic graph.
After the initial directed acyclic graph is obtained, node screening is carried out on the initial directed acyclic graph based on the symbols used by the source file, the local symbols directly declared by the header files and the local symbol numbers of the header files, and the target directed acyclic graph with redundant edges and redundant nodes deleted is obtained.
It should be noted that, in the embodiment of the present application, node filtering is performed on the initial directed acyclic graph based on the respective local symbol numbers of the header file, because the present application recognizes that the compiling time of the code project file is positively correlated with the symbol number required to be compiled by the code project file. Referring to the schematic diagram of the relation between the number of symbols required to be compiled for the code project file and the compiling time of the code project file shown in fig. 3, it can be seen that the greater the number of symbols required to be compiled for the code project file, the longer the compiling time thereof.
Further, considering that the number of symbols required to be compiled of the code project file is mainly influenced by the number of local symbols directly declared by the header file, the embodiment of the application performs node screening on the initial directed acyclic graph based on the respective number of the local symbols of the header file, can effectively reduce the number of symbols required to be compiled of the code project file, and obtains the target directed acyclic graph corresponding to the code project file with the effectively reduced number of symbols required to be compiled.
After the target directed acyclic graph is obtained, determining target reference relations among graph nodes described by the target directed acyclic graph according to edges and nodes in the target directed acyclic graph. And deleting redundant sentences used for referencing other header files in the source file and the header file according to the target referencing relation. Because the deletion of the redundant statement is performed according to the target directed acyclic graph corresponding to the code project file with the number of required compiling symbols effectively reduced, the number of required compiling symbols of the code project file consisting of the source file and the header file after the redundant statement is deleted can be effectively reduced, so that the compiling time of the code project file can be effectively reduced, and the compiling efficiency of the code project file is improved.
Therefore, in the embodiment of the application, based on the initial reference relation between the source file and the header file of the code project file, an initial directed acyclic graph taking the source file and the header file as graph nodes is generated, and based on the symbols used by the source file, the local symbols directly declared by the header file and the local symbol numbers of the header file, the target directed acyclic graph corresponding to the code project file with effectively reduced required compiling symbol numbers can be obtained after node screening is performed on the initial directed acyclic graph. Therefore, according to the target reference relation between the graph nodes described by the target directed acyclic graph, after deleting redundant sentences used for referencing other header files in the source file and the header file, the code project file with effectively reduced number of required compiling symbols can be obtained, so that the compiling time of the code project file can be effectively reduced, and the compiling efficiency of the code project file is improved.
In one embodiment, obtaining an initial reference relationship between files includes:
code analysis is carried out on the source file and the header file, and an abstract syntax tree of the source file and an abstract syntax tree of the header file are generated;
the initial reference relation is obtained based on the abstract syntax tree of the source file and the abstract syntax tree of the header file.
In this embodiment, the initial reference relationship between files is obtained by parsing the abstract syntax tree of the source file and the abstract syntax tree of the header file.
Specifically, code analysis is performed on the source file and the header file, a grammar structure including symbols in the source file is extracted, and a grammar structure including symbols in the header file is extracted, so that the extracted grammar structure is organized into a tree structure, and an abstract grammar tree of a corresponding file is obtained.
After the abstract syntax tree of the source file and the abstract syntax tree of the header file are obtained, the abstract syntax tree is combined according to the relation between symbols, and the combined abstract syntax tree is obtained. And then according to the position of the original abstract syntax tree in the merged abstract syntax tree, determining the reference relation between the original abstract syntax tree, and obtaining the initial reference relation between the files.
The method and the device have the advantages that the relation between the symbols can be perfectly resolved by generating the abstract syntax tree, so that the obtained initial reference relation can be guaranteed to perfectly describe the reference relation actually existing between the files.
FIG. 4 is a schematic diagram of a process for deleting redundant statements in code according to one embodiment of the present application.
Referring to FIG. 4, in one embodiment, a. sln code item file in a C++ environment is parsed to obtain configuration parameters including, but not limited to, macros, code search paths, and the like.
And then, carrying out code analysis on the cpp source file and the h header file of the sln code item file according to the configuration parameters to obtain an abstract syntax tree of the cpp source file and an abstract syntax tree of the h header file. And then according to the obtained abstract syntax tree, analyzing to obtain initial reference relations between the files, and further generating a corresponding initial directed acyclic graph according to a topological structure described by the initial reference relations.
And obtaining the target directed acyclic graph by node screening of the initial directed acyclic graph. And at the same time of node screening, judging whether the #include statement corresponding to each edge is redundant or not, further screening out the redundant #include statement according to the target directed acyclic graph, and deleting the redundant #include statement.
And (3) deleting the redundant #include statement, and correspondingly adjusting the cpp source file and the h header file, so that the compiling efficiency of the sln code item file is improved.
In one embodiment, node screening is performed on the initial directed acyclic graph based on the used symbol of the source file, the local symbol directly declared by each header file, and the local symbol number of each header file, to obtain a target directed acyclic graph, including:
Selecting first class diagram nodes of an initial directed acyclic graph necessary for compiling a code project file to be reserved based on symbols used by a source file and local symbols;
selecting a second class diagram node of the initial directed acyclic graph which is not needed for compiling the code project file to delete the code project file based on the symbols used by the source file and the local symbols;
and taking the header files except the first class graph node and the second class graph node as a third class graph node of the initial directed acyclic graph, and screening the third class graph node based on the number of local symbols to obtain the target directed acyclic graph.
In this embodiment, when node screening is performed on an initial directed acyclic graph, the graph nodes are classified into three categories: and compiling the first class diagram nodes which are required, compiling the second class diagram nodes which are not required, and compiling the third class diagram nodes which are optionally required.
Specifically, at the time of compiling a code project file, the symbols used by the source file need to be declared and referenced. It can thus be determined whether each header file is a necessary file that allows the notation used by the source file to be declared and referenced based on the local notation each directly declared by the source file and whether each header file is an unnecessary file that is completely independent of the declaration and reference of the notation used by the source file.
If a header file is a necessary file that enables the notation used by the source file to be declared and referenced, and indicates that the header file is a header file necessary for compiling the code item file, a node corresponding to the header file is a first class graph node, and is reserved.
If a header file is an unnecessary file completely irrelevant to the statement and the reference of the symbol used by the source file, which indicates that the header file is the header file unnecessary for compiling the code item file, the node corresponding to the header file is a second class diagram node, and the second class diagram node is deleted.
The first class diagram nodes and the second class diagram nodes which are needed by compiling are removed, and the third class diagram nodes which are needed by compiling selectively are left. In order to effectively reduce the number of symbols declared in the code project file, the nodes of the third class of graph are screened based on the respective local number of symbols of the header file, and the target directed acyclic graph is obtained.
In one embodiment, selecting a first class of graph nodes necessary to compile a code project file for reservation based on a notation used by a source file and a native notation, includes:
selecting a first target header file directly declaring the sign used by the source file based on the sign used by the source file and the local sign, and selecting a first path header file on a unique reference path of the sign used by the source file;
And reserving the source file, the first target header file and the first path header file as first class graph nodes.
In this embodiment, based on the local symbols directly declared by the symbols used by the source file and the header files, the header files directly declared by the symbols used by the source file are located, and these header files are used as the first target header files.
And selecting a unique reference path of the source file when the source file uses the symbol based on the local symbol directly declared by the symbol used by the source file and the header file, and taking all the header files on the unique reference path as first path header files.
Since the source file cannot be deleted from the code item file, the source file is preserved as a first class graph node. Since the notation used by the source file must be declared by the first object header file, the first object header file is also reserved as a first class graph node. Since the source file must reference the first path header file when using the notation, the first path header file is also reserved as a first class graph node.
Fig. 5 illustrates a node screening schematic of a directed acyclic graph according to an embodiment of the application.
Referring to FIG. 5, in one embodiment, source file A uses a notation that is declared in header file E and uses a notation that is declared in header file F. Thus, both the header file E and the header file F are reserved as the first target header file.
Also, as shown, when the source file a uses the notation declared in header file E, header file B must be referenced, with the corresponding unique reference path being "a#include B, b#include E". Thus, header file B and header file E are both reserved as first path header files.
Meanwhile, as shown, when the source file a uses the symbols declared in the header file F, two reference paths are optional, namely "a#include C, c#include F" and "a#include D, d#include F". Therefore, the header file C and the header file D do not belong to the first path header file.
And further, the source file A, the header file B, the header file E and the header file F are reserved as first class graph nodes. Since "a#include B, b#include E" is a unique reference path, edges of the source file a to the header file B must not be deleted, and edges of the header file B to the header file E must not be deleted.
In one embodiment, selecting a second class of graph nodes of the initial directed acyclic graph for deletion that is not needed to compile the code project file based on the symbols used by the source file and the local symbols includes:
selecting a second header file which is not directly declared with the symbols used by the source file and is not quoted with the first class graph nodes based on the symbols used by the source file and the local symbols;
And deleting the second header file as a second class diagram node.
In this embodiment, based on the symbols used by the source file and the local symbols directly declared by the header files, the second header file having neither the symbols used by the source file nor the reference to the first class graph node is located.
The second header file does not declare the sign used by the source file, indicating that the second header file cannot provide sign declaration support for the source file use of the sign. The second header file does not reference the first class graph nodes, indicating that the second header file cannot provide symbolic reference support for source files using symbols.
When the source file uses the symbol, the declaration support can not be obtained from the second header file, and the quotation support can not be obtained from the second header file, so the second header file is the header file required for compiling the code project file, and the second header file is used as a second class diagram node to be deleted.
Referring to FIG. 5, in one embodiment, the notation used by source file A is declared only in header file E and header file F.
As can be seen, since the notation used by source file a is not declared in header file G, source file a cannot be declaratively supported from header file G when the notation is used. Further, since the header file G does not refer to any of the first class graph nodes (source file a, header file B, header file E, header file F), the source file a cannot be reference-supported from the header file G when using the notation.
It can be seen that the header file G has no positive effect on the compilation of the code item file, and is therefore deleted as a second class graph node.
In an embodiment, the screening the third class graph node based on the number of the local symbols to obtain the target directed acyclic graph includes:
calculating the sum of the number of symbols recursively referenced by the initial directed acyclic graph after deleting the nodes of the third class graph respectively based on the number of the local symbols;
and screening the third class graph nodes based on the sum of the number of the symbols to obtain the target directed acyclic graph.
In this embodiment, based on the number of local symbols of each header file, the sum of the numbers of symbols recursively referenced by the initial directed acyclic graph after each third-class graph node is deleted is calculated.
Wherein, the sum of the number of the symbols recursively referenced by the directed acyclic graph, also called RIPS (Recursive Included Symbols of Project), refers to the sum obtained by accumulating the number of the symbols recursively referenced by each source file in the directed acyclic graph. The number of symbols recursively referenced by the source file, also known as RIS (Recursive Included Symbols), refers to the sum of the accumulated number of local symbols each directly declaring for the header file referenced by the source file.
In detail, see the schematic diagram of the directed acyclic graph of an embodiment of the present application shown in fig. 6. In one embodiment, source file A directly states a number of symbols of 1, source text B directly states a number of symbols of 2, and header file C directly states a number of symbols of 3. Similarly, the header D, E, F has the number of directly declared symbols of 4, 5, and 6, respectively. Since the header file directly or indirectly referenced by the source file a includes the header file C, D, E, F, the number of symbols recursively referenced by the source file a is 1+3+4+5+6=19. Similarly, since the header file directly or indirectly referenced by the source file B includes the header file D, F, the number of symbols recursively referenced by the source file B is 2+4+6=12. The sum of the number of symbols recursively referenced by the directed acyclic graph in fig. 6 is therefore 19+12=31.
And obtaining the sum of the symbol numbers recursively referenced by the initial directed acyclic graph, namely taking the sum as an index, and screening the third class graph nodes by confirming the influence of the third class graph nodes on the sum of the symbol numbers, thereby obtaining the target directed acyclic graph.
The method has the advantages that the total of the symbol numbers recursively referenced by the initial directed acyclic graph can be used for describing the compiling workload brought by the symbol numbers when compiling the code project file more accurately, so that the compiling time of the code project file can be reduced more effectively for screening nodes of the third class graph.
In an embodiment, the screening the third class graph node based on the sum of the number of symbols to obtain the target directed acyclic graph includes:
and deleting one third class graph node and other third class graph nodes referenced by the third class graph node each time by taking the value of the sum of the number of the symbols after each deleting operation as a minimum, until the rest third class graph nodes cannot be deleted, and obtaining the target directed acyclic graph.
In this embodiment, the third class graph node is screened mainly by adopting the greedy algorithm idea.
Specifically, for each third class graph node, the sum of the number of symbols recursively referenced by the initial directed acyclic graph is calculated after deleting it and the other third class graph nodes referenced by it. And then selecting a third class graph node with the minimum symbol quantity sum value, and deleting the third class graph node and other third class graph nodes referenced by the third class graph node.
And repeating the operation until the rest third class graph nodes cannot be deleted, and obtaining the target directed acyclic graph after screening is completed aiming at the third class graph nodes.
The method has the advantages that the third class graph nodes are screened by adopting the greedy algorithm idea, and node screening can be completed rapidly and efficiently.
Referring to fig. 5, in an embodiment, a source file a, a header file B, a header file E, and a header file F are first class graph nodes that must be reserved, a header file G is a second class graph node that must be deleted, and a header file C and a header file D outside the first class graph node and the second class graph node are third class graph nodes that need to be selectively deleted.
Wherein the number of directly declared symbols of header file C is greater than the number of directly declared symbols of header file D. Since the RISP after deletion of header file C is smaller than the RISP after deletion of header file D, header file C is selected for deletion.
After header C is deleted, source file A must reference header D when it needs to use the notation declared in header F. Therefore, after the header file C is deleted, the header file D cannot be deleted. And (5) screening the nodes of the third class of graph so as to obtain the target directed acyclic graph shown at the right side of fig. 5.
In an embodiment, if the code item file includes a plurality of source files, the source files are used as boundaries to isolate the deletion process of the redundant statement, so that a plurality of mixed projects are prevented from having higher computational demands on the deletion process of the redundant statement, and the deletion process speed of the redundant statement is improved.
Specifically, the process of deleting redundant sentences is isolated by taking a source file as a boundary, namely, each source file in a code item file is separated to obtain a plurality of initial directed acyclic graphs, and then the process of deleting the redundant sentences is carried out on each initial directed acyclic graph. Each initial directed acyclic graph contains only one source file, and header files referenced directly or indirectly by that source file.
FIG. 7 shows a block diagram of a device for deleting redundant statements in code according to an embodiment of the present application, the device comprising:
an obtaining module 310 configured to obtain a source file and a header file of a code item file, and obtain an initial reference relationship between the files, where the initial reference relationship is used to describe a file reference path of a symbol used by the source file and a file reference path of a symbol used by the header file;
a graph generation module 320 configured to generate an initial directed acyclic graph with the source file and the header file as graph nodes based on the initial reference relationships;
the node screening module 330 is configured to perform node screening on the initial directed acyclic graph based on the symbols used by the source file, the local symbols directly declared by the header files and the local symbol numbers of the header files, so as to obtain a target directed acyclic graph;
And the deleting module 340 is configured to delete redundant sentences used for referencing other header files in the source file and the header file according to the target referencing relationship between the graph nodes described by the target directed acyclic graph.
In an exemplary embodiment of the present application, the acquisition module is configured to:
code analysis is carried out on the source file and the header file, and an abstract syntax tree of the source file and an abstract syntax tree of the header file are generated;
and acquiring the initial reference relation based on the abstract syntax tree of the source file and the abstract syntax tree of the header file.
In an exemplary embodiment of the present application, the node screening module is configured to:
selecting a first class diagram node of an initial directed acyclic graph necessary for compiling the code project file to be reserved based on the symbols used by the source file and the local symbols;
selecting a second class diagram node of the initial directed acyclic graph, which is not needed for compiling the code project file, to delete based on the symbols used by the source file and the local symbols;
and taking the header files except the first class graph node and the second class graph node as a third class graph node of the initial directed acyclic graph, and screening the third class graph node based on the number of the local symbols to obtain the target directed acyclic graph.
In an exemplary embodiment of the present application, the node screening module is configured to:
selecting a first target header file directly declared with the sign used by the source file based on the sign used by the source file and the local sign, and selecting a first path header file on a unique reference path of the sign used by the source file;
and reserving the source file, the first target header file and the first path header file as the first class graph nodes.
In an exemplary embodiment of the present application, the node screening module is configured to:
selecting a second header file which is not directly declared with the symbols used by the source file and is not quoted with the first class graph nodes based on the symbols used by the source file and the local symbols;
and deleting the second header file as a second class diagram node.
In an exemplary embodiment of the present application, the node screening module is configured to:
calculating the sum of the number of symbols recursively referenced by the initial directed acyclic graph after deleting the third class graph nodes respectively based on the number of local symbols;
and screening the third class graph nodes based on the sum of the symbol numbers to obtain the target directed acyclic graph.
In an exemplary embodiment of the present application, the node screening module is configured to:
and aiming at minimizing the value of the sum of the symbol numbers after each deleting operation, deleting one third class graph node and other third class graph nodes referenced by the third class graph node each time until the rest third class graph nodes cannot be deleted, and obtaining the target directed acyclic graph.
An electronic device 40 according to an embodiment of the present application is described below with reference to fig. 8. The electronic device 40 shown in fig. 8 is merely an example and should not be construed as limiting the functionality and scope of use of the embodiments herein.
As shown in fig. 8, the electronic device 40 is in the form of a general purpose computing device. Components of electronic device 40 may include, but are not limited to: the at least one processing unit 410, the at least one memory unit 420, and a bus 430 connecting the various system components, including the memory unit 420 and the processing unit 410.
Wherein the storage unit stores program code that is executable by the processing unit 410 such that the processing unit 410 performs the steps according to various exemplary embodiments of the present invention described in the description of the exemplary methods described above in this specification. For example, the processing unit 410 may perform the various steps as shown in fig. 2.
The storage unit 420 may include readable media in the form of volatile storage units, such as Random Access Memory (RAM) 4201 and/or cache memory 4202, and may further include Read Only Memory (ROM) 4203.
The storage unit 420 may also include a program/utility 4204 having a set (at least one) of program modules 4205, such program modules 4205 including, but not limited to: an operating system, one or more application programs, other program modules, and program data, each or some combination of which may include an implementation of a network environment.
Bus 430 may be a local bus representing one or more of several types of bus structures including a memory unit bus or memory unit controller, a peripheral bus, an accelerated graphics port, a processing unit, or using any of a variety of bus architectures.
The electronic device 40 may also communicate with one or more external devices 500 (e.g., keyboard, pointing device, bluetooth device, etc.), one or more devices that enable a user to interact with the electronic device 40, and/or any device (e.g., router, modem, etc.) that enables the electronic device 40 to communicate with one or more other computing devices. Such communication may occur through an input/output (I/O) interface 450. An input/output (I/O) interface 450 is connected to the display unit 440. Also, the electronic device 40 may communicate with one or more networks such as a Local Area Network (LAN), a Wide Area Network (WAN) and/or a public network, such as the Internet, through the network adapter 460. As shown, network adapter 460 communicates with other modules of electronic device 40 over bus 430. It should be appreciated that although not shown, other hardware and/or software modules may be used in connection with electronic device 40, including, but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, data backup storage systems, and the like.
From the above description of embodiments, those skilled in the art will readily appreciate that the example embodiments described herein may be implemented in software, or may be implemented in software in combination with the necessary hardware. Thus, the technical solution according to the embodiments of the present application may be embodied in the form of a software product, which may be stored in a non-volatile storage medium (may be a CD-ROM, a usb disk, a mobile hard disk, etc.) or on a network, and includes several instructions to cause a computing device (may be a personal computer, a server, a terminal device, or a network device, etc.) to perform the method according to the embodiments of the present application.
In an exemplary embodiment of the present application, there is also provided a computer-readable storage medium having stored thereon computer-readable instructions, which, when executed by a processor of a computer, cause the computer to perform the method described in the method embodiment section above.
According to an embodiment of the present application, there is also provided a program product for implementing the method in the above method embodiments, which may employ a portable compact disc read only memory (CD-ROM) and comprise program code and may be run on a terminal device, such as a personal computer. However, the program product of the present invention is not limited thereto, and in this document, a readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device.
The program product may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium can be, for example, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples (a non-exhaustive list) of the readable storage medium would include the following: an electrical connection having one or more wires, a portable disk, a hard disk, random Access Memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
The computer readable signal medium may include a data signal propagated in baseband or as part of a carrier wave with readable program code embodied therein. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A readable signal medium may also be any readable medium that is not a readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device.
Program code embodied on a readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
Program code for carrying out operations of the present invention may be written in any combination of one or more programming languages, including an object oriented programming language such as Java, C++ or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computing device, partly on the user's device, as a stand-alone software package, partly on the user's computing device, partly on a remote computing device, or entirely on the remote computing device or server. In the case of remote computing devices, the remote computing device may be connected to the user computing device through any kind of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or may be connected to an external computing device (e.g., connected via the Internet using an Internet service provider).
It should be noted that although in the above detailed description several modules or units of a device for action execution are mentioned, such a division is not mandatory. Indeed, the features and functions of two or more modules or units described above may be embodied in one module or unit, in accordance with embodiments of the present application. Conversely, the features and functions of one module or unit described above may be further divided into a plurality of modules or units to be embodied.
Furthermore, although the various steps of the methods herein are depicted in the accompanying drawings in a particular order, this is not required to either suggest that the steps must be performed in that particular order, or that all of the illustrated steps must be performed, to achieve desirable results. Additionally or alternatively, certain steps may be omitted, multiple steps combined into one step to perform, and/or one step decomposed into multiple steps to perform, etc.
From the above description of embodiments, those skilled in the art will readily appreciate that the example embodiments described herein may be implemented in software, or may be implemented in software in combination with the necessary hardware. Thus, the technical solution according to the embodiments of the present application may be embodied in the form of a software product, which may be stored in a non-volatile storage medium (may be a CD-ROM, a U-disk, a mobile hard disk, etc.) or on a network, and includes several instructions to cause a computing device (may be a personal computer, a server, a mobile terminal, or a network device, etc.) to perform the method according to the embodiments of the present application.
Other embodiments of the present application will be apparent to those skilled in the art from consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the application following, in general, the principles of the application and including such departures from the present disclosure as come within known or customary practice within the art to which the application pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the application being indicated by the following claims.

Claims (10)

1. A method for deleting redundant statements in a code, the method comprising:
acquiring a source file and a header file of a code project file, and acquiring an initial reference relation between the files, wherein the initial reference relation is used for describing a file reference path of a symbol used by the source file and a file reference path of a symbol used by the header file;
generating an initial directed acyclic graph taking the source file and the header file as graph nodes based on the initial reference relation;
node screening is carried out on the initial directed acyclic graph based on the used symbols of the source file, the local symbols directly declared by the header files and the local symbol quantity of the header files, so that a target directed acyclic graph is obtained;
and deleting redundant sentences used for referencing other header files in the source file and the header file according to the target reference relation between the graph nodes described by the target directed acyclic graph.
2. The method of claim 1, wherein obtaining an initial reference relationship between files comprises:
code analysis is carried out on the source file and the header file, and an abstract syntax tree of the source file and an abstract syntax tree of the header file are generated;
And acquiring the initial reference relation based on the abstract syntax tree of the source file and the abstract syntax tree of the header file.
3. The method of claim 1, wherein node screening the initial directed acyclic graph based on the symbols used by the source file, the local symbols directly declared by the header file, and the number of local symbols of the header file, to obtain a target directed acyclic graph, comprises:
selecting a first class diagram node of an initial directed acyclic graph necessary for compiling the code project file to be reserved based on the symbols used by the source file and the local symbols;
selecting a second class diagram node of the initial directed acyclic graph, which is not needed for compiling the code project file, to delete based on the symbols used by the source file and the local symbols;
and taking the header files except the first class graph node and the second class graph node as a third class graph node of the initial directed acyclic graph, and screening the third class graph node based on the number of the local symbols to obtain the target directed acyclic graph.
4. The method of claim 1, wherein selecting a first class of graph nodes necessary to compile the code item file for reservation based on the symbols used by the source file and the local symbols comprises:
Selecting a first target header file directly declared with the sign used by the source file based on the sign used by the source file and the local sign, and selecting a first path header file on a unique reference path of the sign used by the source file;
and reserving the source file, the first target header file and the first path header file as the first class graph nodes.
5. The method of claim 1, wherein selecting a second class of graph nodes of the initial directed acyclic graph that are not needed to compile the code item file for deletion based on the symbols used by the source file and the local symbols comprises:
selecting a second header file which is not directly declared with the symbols used by the source file and is not quoted with the first class graph nodes based on the symbols used by the source file and the local symbols;
and deleting the second header file as a second class diagram node.
6. A method according to claim 3, wherein screening the third class graph nodes based on the number of local symbols to obtain the target directed acyclic graph comprises:
Calculating the sum of the number of symbols recursively referenced by the initial directed acyclic graph after deleting the third class graph nodes respectively based on the number of local symbols;
and screening the third class graph nodes based on the sum of the symbol numbers to obtain the target directed acyclic graph.
7. The method of claim 6, wherein screening the third class of graph nodes based on the sum of the number of symbols to obtain the target directed acyclic graph comprises:
and aiming at minimizing the value of the sum of the symbol numbers after each deleting operation, deleting one third class graph node and other third class graph nodes referenced by the third class graph node each time until the rest third class graph nodes cannot be deleted, and obtaining the target directed acyclic graph.
8. A device for deleting redundant statements in a code, the device comprising:
the acquisition module is configured to acquire a source file and a header file of a code project file, and acquire an initial reference relation between the files, wherein the initial reference relation is used for describing a file reference path of a symbol used by the source file and a file reference path of a symbol used by the header file;
A graph generating module configured to generate an initial directed acyclic graph with the source file and the header file as graph nodes based on the initial reference relationship;
the node screening module is configured to screen nodes of the initial directed acyclic graph based on the symbols used by the source file, the local symbols directly declared by the header files and the local symbol numbers of the header files, so as to obtain a target directed acyclic graph;
and the deleting module is configured to delete redundant sentences used for referencing other header files in the source file and the header file according to the target reference relation between the graph nodes described by the target directed acyclic graph.
9. An electronic device, comprising:
one or more processors;
storage means for storing one or more programs which, when executed by the one or more processors, cause the electronic device to implement the method of any of claims 1 to 7.
10. A computer readable storage medium having stored thereon computer readable instructions which, when executed by a processor of a computer, cause the computer to perform the method of any of claims 1 to 7.
CN202210916488.7A 2022-08-01 2022-08-01 Method and device for deleting redundant sentences in codes, electronic equipment and storage medium Pending CN117539492A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210916488.7A CN117539492A (en) 2022-08-01 2022-08-01 Method and device for deleting redundant sentences in codes, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210916488.7A CN117539492A (en) 2022-08-01 2022-08-01 Method and device for deleting redundant sentences in codes, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN117539492A true CN117539492A (en) 2024-02-09

Family

ID=89788627

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210916488.7A Pending CN117539492A (en) 2022-08-01 2022-08-01 Method and device for deleting redundant sentences in codes, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN117539492A (en)

Similar Documents

Publication Publication Date Title
US8893102B2 (en) Method and system for performing backward-driven path-sensitive dataflow analysis
US8555266B2 (en) Managing variable assignments in a program
US11675575B2 (en) Checking source code validity at time of code update
CN108139891B (en) Method and system for generating suggestions to correct undefined token errors
CN109086215B (en) Embedded software unit test case generation method and system
CN110058861B (en) Source code processing method and device, storage medium and electronic equipment
US10514898B2 (en) Method and system to develop, deploy, test, and manage platform-independent software
US20190146764A1 (en) Using comments of a program to provide optimizations
US20090328016A1 (en) Generalized expression trees
CN112100072A (en) Static detection method, device, equipment and medium for application program codes
US8458679B2 (en) May-constant propagation
CN114138281A (en) Compiling method, device, equipment and medium of software engineering
US10839124B1 (en) Interactive compilation of software to a hardware language to satisfy formal verification constraints
US9116714B2 (en) Methods and systems for file processing
WO2015003452A1 (en) Methods and systems for file processing
US10656922B2 (en) Systems and methods for providing an application transformation tool
US10268461B2 (en) Global data flow optimization for machine learning programs
CN116578282A (en) Code generation method, device, electronic equipment and medium
CN113141407B (en) Page resource loading method and device and electronic equipment
CN110737438A (en) data processing method and device
US11947966B2 (en) Identifying computer instructions enclosed by macros and conflicting macros at build time
CN117539492A (en) Method and device for deleting redundant sentences in codes, electronic equipment and storage medium
CN110297639B (en) Method and apparatus for detecting code
CN110727428B (en) Method and device for converting service logic layer codes and electronic equipment
US20170039044A1 (en) Compiling apparatus and compiling method

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