Method and device for determining code calling link
The method constructs an abstract syntax tree and union-find set to determine code call chains, overcoming the inaccuracies and resource issues of existing methods, providing a fast and efficient solution for identifying code call chains.
Patent Information
- Application Number
- CN202411854349.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-16
- Publication Date
- 2025-03-21
AI Technical Summary
Existing methods for determining code call chains, such as pointer analysis and bytecode engineering libraries, suffer from low accuracy, resource-intensive processing, and inability to handle uncalled code methods, leading to potential omissions in call chain analysis.
A method that constructs an abstract syntax tree from target code and generates an original code set based on predefined attributes, forming a union-find set to search for call chains independently of code execution, ensuring quick, accurate, and low-resource determination of call chains.
This approach enables rapid, precise, and resource-efficient identification of code call chains without relying on code execution, addressing the limitations of existing methods by leveraging static code analysis and offline chain searching.
Smart Images

Figure CN119668574A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and device for determining a code call link. Background Art
[0002] The code call chain refers to a series of method or function call processes in programming, forming a chain structure. The code call chain can help developers fully understand the behavior of the system at runtime, providing great convenience for development, debugging, and optimizing system performance. The existing code call chain is usually determined by pointer analysis or through bytecode engineering libraries.
[0003] In the process of implementing the present invention, the inventors found that the prior art has the following problems:
[0004] Existing pointer analysis methods may have false positives and omissions when analyzing code, have low accuracy, and require a certain amount of computing resources and time, with high resource consumption; while the bytecode engineering library method cannot handle code methods that have not been called, is too dependent on program execution, and the code call link may be missing. Summary of the invention
[0005] In view of this, an embodiment of the present invention provides a method and device for determining a code call link. The code call link is determined by adopting static code analysis and offline code call link search. The present invention does not rely on compilation and code running, and can determine the code call link quickly, accurately, and with low consumption.
[0006] To achieve the above object, according to one aspect of an embodiment of the present invention, a method for determining a code call link is provided, comprising:
[0007] Parsing the target code, and finding a matching abstract syntax tree template from a preset abstract syntax tree template according to the parsing result, thereby constructing an abstract syntax tree of the target code;
[0008] According to the preset attribute composition of the atomic code and the abstract syntax tree, the atomic code composition of the target code and the attribute information of each of the atomic codes are determined, thereby generating an atomic code set of the target code, wherein each atomic code in the atomic code set includes an upstream and downstream calling relationship between the atomic codes;
[0009] According to the upstream and downstream calling relationships, by merging the respective atomized codes and searching for the root nodes, a union-find set of the target codes is generated;
[0010] The union-find set is used to search for call links of each component code in the target code.
[0011] Optionally, the attribute composition of the atomic code includes basic attributes and calling relationship attributes; according to the preset attribute composition of the atomic code and the abstract syntax tree, the atomic code composition of the target code and the attribute information of each atomic code are determined, including: according to the leaf nodes in the abstract syntax tree, the atomic code composition of the target code is determined; according to the basic attributes in the attribute composition, the attribute value corresponding to the matching attribute name is extracted from the attribute information of each leaf node in the abstract syntax tree to obtain the basic attribute information of each atomic code of the target code; according to the connection relationship between each leaf node in the abstract syntax tree, the calling relationship attribute information of each atomic code of the target code is determined.
[0012] Optionally, based on the connection relationship between each leaf node in the abstract syntax tree, the calling relationship attribute information of each atomized code of the target code is determined, including: dividing the leaf nodes with the same node path in the abstract syntax tree into the same group; for each atomized code in the target code: selecting a target group that matches the node path of the atomized code from the leaf nodes of each group; extracting the calling relationship attribute information of the atomized code from the upper and lower leaf nodes connected to each leaf node of the target group.
[0013] Optionally, in response to the target code having an associated external application, the method further includes: obtaining the external application associated with the target code, constructing an abstract syntax tree of the external application based on the application code of the external application, and then generating an atomic code set of the application code in combination with the attribute composition, and then generating a union-find set of the application code; using the union-find set of the target code and the union-find set of the application code, searching for call links of each component code in the target code.
[0014] Optionally, the union-find set of the target code and the union-find set of the application code are used to search for call links of each component code in the target code, including: determining the association node between the union-find set of the target code and the union-find set of the application code based on the association code between the target code and the external application, establishing a connection between the union-find set of the target code and the union-find set of the application code to obtain a cross-application union-find set; searching for call links of each component code in the target code from the cross-application union-find set.
[0015] Optionally, parsing the target code includes: obtaining a latest version of the target code, dividing the latest version of the target code into code segments to obtain a code segment set; and parsing each code segment in the code segment set in a segmentation order.
[0016] Optionally, after constructing the abstract syntax tree of the target code, the method further includes: identifying independent leaf nodes without call relationships in the abstract syntax tree, and deleting the independent leaf nodes.
[0017] Optionally, before extracting the attribute value corresponding to the matching attribute name from the attribute information of each leaf node in the abstract syntax tree, the method also includes: judging and confirming that the attribute information of the leaf node in the abstract syntax tree matches the basic attributes of the atomic code; in response to the presence of leaf nodes in the abstract syntax tree that do not match the basic attributes of the atomic code, deleting the unmatched leaf nodes.
[0018] According to a second aspect of an embodiment of the present invention, a device for determining a code call link is provided, including:
[0019] A syntax tree generation module is used to parse the target code and, based on the parsing result, find a matching abstract syntax tree template from a preset abstract syntax tree template, thereby constructing an abstract syntax tree of the target code;
[0020] A code atomization module, used to determine the atomization code composition of the target code and the attribute information of each atomization code according to the preset attribute composition of the atomization code and the abstract syntax tree, so as to generate an atomization code set of the target code, wherein each atomization code in the atomization code set includes an upstream and downstream calling relationship between the atomization codes;
[0021] A union-find set generation module, used for generating a union-find set of the target code by merging the respective atomized codes and searching for the root node according to the upstream and downstream call relationships;
[0022] The call link determination module is used to use the union-find set to search for the call links of each component code in the target code.
[0023] According to a third aspect of an embodiment of the present invention, there is provided an electronic device for a code calling link, including:
[0024] one or more processors;
[0025] a storage device for storing one or more programs,
[0026] When the one or more programs are executed by the one or more processors, the one or more processors implement the method provided in the first aspect of the embodiment of the present invention.
[0027] According to a fourth aspect of an embodiment of the present invention, a computer-readable medium is provided, on which a computer program is stored, and when the program is executed by a processor, the method provided by the first aspect of the embodiment of the present invention is implemented.
[0028] According to a fifth aspect of an embodiment of the present invention, there is provided a computer program product, including a computer program, wherein when the computer program is executed by a processor, the method provided in the first aspect of the embodiment of the present invention is implemented.
[0029] One embodiment of the invention has the following advantages or beneficial effects: by parsing the target code, according to the parsing result, a matching abstract syntax tree template is found from a preset abstract syntax tree template, thereby constructing an abstract syntax tree of the target code; according to the preset attribute composition of the atomic code and the abstract syntax tree, the atomic code composition of the target code and the attribute information of each atomic code are determined, thereby generating an atomic code set of the target code, and each atomic code in the atomic code set contains the upstream and downstream calling relationship between the atomic codes; according to the upstream and downstream calling relationship, by merging each atomic code and searching the root node, a union-find set of the target code is generated; using the union-find set, a technical solution for searching the call links of each component code in the target code is implemented, and a method for determining the atomic code call link based on the code structure is implemented, and the code call link is determined by adopting static code analysis and offline code call link search, which does not rely on compilation and code running, and can quickly, accurately and low-cost determine the code call link. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.
[0031] Figure 1 is a schematic diagram of the main process of a method for determining a code call link according to an embodiment of the present invention;
[0032] Figure 2 is a flowchart of a method for determining a code call link according to an embodiment of the present invention;
[0033] Figure 3 Schematic diagram of a method for generating an atomized code according to an embodiment of the present invention;
[0034] Figure 4 is a comparative schematic diagram of different search methods for code call links across application scenarios in an embodiment of the present invention;
[0035] Figure 5 is a schematic diagram of main modules of a device for determining a code call link according to an embodiment of the present invention;
[0036] Figure 6 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;
[0037] Figure 7 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION
[0038] It should be noted that the acquisition, storage and application of user personal information involved in the technical solutions disclosed in the embodiments of the present invention are in compliance with the provisions of relevant laws and regulations and do not violate public order and good morals.
[0039] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0040] Existing pointer analysis methods may have false positives and omissions when analyzing code, have low accuracy, and require a certain amount of computing resources and time, with high resource consumption; while the bytecode engineering library method cannot handle code methods that have not been called, is too dependent on program execution, and the code call link may be missing.
[0041] In order to solve the above problems existing in the prior art, the present invention proposes a method for determining a code call link, which generates an atomic code set of the target code based on an abstract syntax tree constructed by the target code and the attribute composition of the preset atomic code, and then generates a union-find set of the target code according to the upstream and downstream calling relationships between the atomic codes contained in each atomic code in the atomic code set, and then uses the union-find set to search and query the code call link, thereby realizing a method for determining an atomic code call link based on code structure, which determines the code call link by adopting static code analysis and offline code call link search, does not rely on compilation and code running, and can determine the code call link quickly, accurately and with low consumption.
[0042] Figure 1 FIG. 1 is a schematic diagram of the main process of the method for determining a code call link according to an embodiment of the present invention. Figure 1 As shown, the method for determining a code call link according to an embodiment of the present invention includes the following steps S101 to S103.
[0043] Step S101: parse the target code, and according to the parsing result, find a matching abstract syntax tree template from preset abstract syntax tree templates, so as to construct an abstract syntax tree of the target code.
[0044] Specifically, when the system receives a confirmation request of the code call link, it obtains the corresponding target code. It can be understood that the acquisition of the target code can be triggered by the confirmation request of the code call link, or by a pre-configured timed task, which is not specifically limited in the embodiment of the present invention. The specific method of obtaining the target code can be to obtain the latest version of the code through the code collaborative development platform git, and of course, it can also obtain a specified version of the specified code, which is not specifically limited in the embodiment of the present invention. Furthermore, the acquired target code is parsed to obtain the types of each code statement contained in the target code, such as initialization statements, method calling statements, etc., as well as variable names, variable types, method calling names, etc. involved in each code statement. According to the types of these statements, an abstract syntax tree template that matches these statement types is found from a preset abstract syntax tree template. According to the matching abstract syntax tree template and the variable names, variable types, and method calling names obtained by parsing, an abstract syntax subtree corresponding to each code statement is constructed. Then, according to the front-to-back association relationship between these code statements, these abstract syntax subtrees are associated and aggregated to form an abstract syntax tree of the target code. It can be understood that each abstract syntax subtree that constitutes the abstract syntax tree of the target code is equivalent to a leaf node relative to the entire abstract syntax tree.
[0045] According to one embodiment of the present invention, parsing the target code includes: obtaining the latest version of the target code, dividing the latest version of the target code into code segments to obtain a code segment set; and parsing each code segment in the code segment set according to the segmentation order.
[0046] Specifically, under normal circumstances, the tracking of code call links is required in the performance optimization stage or development stage of the current system, so the latest version of the target code is obtained, and the latest version of the target code is passed to the word segmenter. The word segmenter segments the received target code from front to back to obtain code segments, and then inputs each code segment into the parser in the order of segmentation, and the parser parses each received code segment. It can be understood that the abstract syntax tree template of the embodiment of the present invention is defined by statement type, so the target code can be directly segmented according to code statements. The word segmenter identifies the various code statements contained in the target code according to the statement identifier of the code grammar, and segments the target code according to the statement. Each code segment is a code statement.
[0047] According to another embodiment of the present invention, after constructing the abstract syntax tree of the target code, the method further includes: identifying independent leaf nodes without call relationships in the abstract syntax tree, and deleting the independent leaf nodes.
[0048] Specifically, considering that the embodiments of the present invention are mainly used to determine the code call link in the target code, in order to improve the efficiency of determining the code call link and avoid interference from irrelevant factors, after constructing the abstract syntax tree of the target code, all leaf nodes in the abstract syntax tree are traversed to determine whether there is a connection calling relationship between them and other leaf nodes, and independent leaf nodes that do not have a connection calling relationship are identified, and the independent leaf nodes are deleted. The abstract syntax tree after deleting the independent leaf nodes is used to determine the atomic code composition of the target code and the attribute information of each atomic code.
[0049] Step S102: Determine the atomic code composition of the target code and the attribute information of each atomic code according to the preset atomic code attribute composition and the abstract syntax tree, thereby generating an atomic code set of the target code, wherein each atomic code in the atomic code set includes upstream and downstream calling relationships between the atomic codes.
[0050] Specifically, the embodiment of the present invention determines the code call link from the perspective of atomic parsing of the target code, and presets the attribute composition of the atomic code in advance. An atomic code can be understood as a node, including: the name of the application to which the code belongs, the id of the code submission, the service type of the code (whether there is an external service), other extended attributes, the code line where the atomic code is located, the code information summary md5 code, node name, node id, node type (digital representations corresponding to different node types can be enumerated), node short name, lower-level node of the current atomic code (based on file structure), upper-level node (based on file structure), logical downstream call node of the current atomic code (based on logical call), logical upstream call node (based on logical call), node path based on file structure, reference path based on logical call, code complexity, logical content (such as code in if block), and local file path. The specific character representation is as follows: self.appName = None Application name self.commitId = None code commit id self.serverType = None Service type (match whether it is an external service: interface, message, task) self.kwargs = kwargs extension self.line = None Code line self.nodeId = None node id self.md5 = None unique md5 code self.nodeName = None Node name self.nodeType = None node type self.shortName = None short name self.lowerNode = set() stores lower node ids (based on file structure) self.upperNode = set() stores upper node ids (based on file structure) self.L = set() Logical downstream call node ids (based on logical call) self.U = set() Logical upstream call node ids (based on logical call) self.nodePath = None Node path (based on file structure) self.indexPath = None Reference path (based on logic call) self.complexity = 0 complexity self.logicText = None Logical content (such as code in an if block) self.file = None File path.
[0051] Furthermore, based on the above-mentioned preset attribute composition of the atomic code, combined with the above-mentioned constructed abstract syntax tree of the target code, the various atomic codes contained in the target code and the attribute values of each attribute in each atomic code are determined, and the complete information of each atomic code is obtained, and then each atomic code constitutes the atomic code set of the target code. It can be seen from the attribute composition of the above-mentioned atomic code that the five attributes of nodeId, lowerNode, upperNode, L, and U can accurately characterize the upstream and downstream calling relationship between atomic codes, so the atomic code set of the target code provides key data support for the determination of the code call link of the embodiment of the present invention.
[0052] According to one embodiment of the present invention, the attribute composition of the atomic code includes basic attributes and calling relationship attributes; according to the preset attribute composition of the atomic code and the abstract syntax tree, the atomic code composition of the target code and the attribute information of each atomic code are determined, including: according to the leaf nodes in the abstract syntax tree, the atomic code composition of the target code is determined; according to the basic attributes in the attribute composition, the attribute value corresponding to the matching attribute name is extracted from the attribute information of each leaf node in the abstract syntax tree to obtain the basic attribute information of each atomic code of the target code; according to the connection relationship between each leaf node in the abstract syntax tree, the calling relationship attribute information of each atomic code of the target code is determined.
[0053] Specifically, based on the abstract syntax tree of the target code constructed above, it can be seen that the leaf nodes of the abstract syntax tree are obtained by segmenting the target code, and the segmentation unit of the code statement has reached the granularity of atomic segmentation, so with the help of the construction of the abstract syntax tree, the atomic extraction of the target code can be achieved, and each leaf node is used as the composition of the atomic code of the target code.
[0054] In addition, in the process of constructing the abstract syntax tree, in addition to code segmentation, it is also necessary to parse the segmented code statements, and assign the attribute values such as variable name and variable type contained in the parsed code statements to the attribute information corresponding to the leaf node in the abstract syntax tree, so the attribute information of each leaf node contains rich code information. Based on the above-mentioned preset attribute composition of the atomic code, the attribute composition can be divided into basic attributes and call relationship attributes. The attribute information of each leaf node in the abstract syntax tree basically includes the basic attributes, so according to the preset basic attributes, the attribute value corresponding to the matching attribute name is extracted from the attribute information of each leaf node in the abstract syntax tree to obtain the basic attribute information of each atomic code of the target code. As for the call relationship attribute, it is specifically reflected in the connection relationship between each leaf node in the abstract syntax tree. By searching the connection relationship between the leaf node corresponding to the atomic code and other leaf nodes on the abstract syntax tree, atomic association is performed to determine the call relationship attribute information of each atomic code of the target code, such as the nodeId, lowerNode, upperNode, L, U mentioned above.
[0055] According to another embodiment of the present invention, before extracting the attribute value corresponding to the matching attribute name from the attribute information of each leaf node in the abstract syntax tree, the method also includes: judging and confirming that the attribute information of the leaf node in the abstract syntax tree matches the basic attributes of the atomic code; in response to the presence of leaf nodes in the abstract syntax tree that do not match the basic attributes of the atomic code, deleting the unmatched leaf nodes.
[0056] Specifically, although the attribute information of the leaf nodes of the abstract syntax tree usually matches the basic attributes of the atomic code, there are inevitable special cases. Therefore, before extracting the attribute value corresponding to the matching attribute name from the attribute information of each leaf node in the abstract syntax tree, it is best to first determine whether the attribute name in the attribute information of the leaf node in the abstract syntax tree matches the basic attribute name of the atomic code. If the judgment result is a match, then extract the attribute value corresponding to the matching attribute name from the attribute information of each leaf node in the abstract syntax tree. If it does not match, then delete the leaf node, and accordingly, ignore the atomic code corresponding to the leaf node.
[0057] According to another embodiment of the present invention, the calling relationship attribute information of each of the atomic codes of the target code is determined according to the connection relationship between each leaf node in the abstract syntax tree, including: dividing the leaf nodes with the same node path in the abstract syntax tree into the same group; for each of the atomic codes in the target code: selecting a target group that matches the node path of the atomic code from the leaf nodes of each group; extracting the calling relationship attribute information of the atomic code from the upper and lower leaf nodes connected to each leaf node of the target group.
[0058] Specifically, considering the large scale of the target code, it takes a certain amount of time and resources to search and find from the abstract syntax tree. In order to facilitate the efficient and rapid determination of the calling relationship attribute information of the atomic code, the leaf nodes with the same node path in the abstract syntax tree can be divided into the same group based on the uniqueness of the node path to obtain multiple group leaf nodes. For each atomic code in the target code, a target group matching the node path of the atomic code is selected from the leaf nodes of each group, and then the calling relationship attribute information of the atomic code, lowerNode, upperNode, L, U, is extracted from the upper and lower leaf nodes connected to each leaf node of the target group. As for nodeid, the atomic code with the same node path can be assigned a self-increment value to play the role of identification.
[0059] Step S103: According to the upstream and downstream calling relationship, a union-find set of the target code is generated by merging the respective atomized codes and searching for the root nodes.
[0060] Specifically, in view of the fact that the upstream and downstream call relationships contained in each atomic code in the atomic code set of the target code constructed above are the relationships between atoms in pairs, if you need to find the most upstream of any atomic code, you need to traverse it in pairs, and the traversal speed is very slow. As a data structure, the union-find set is used to solve the query problem of element nodes in a set, with low complexity and high search efficiency. Therefore, the embodiment of the present invention chooses to use the union-find set to optimize the upstream and downstream call relationships between atomic codes.
[0061] According to the upstream and downstream calling relationships between the atomic codes contained in the atomic code set of the target code generated above, and the atomic code can be regarded as a node, the union-find set construction idea can be used to merge the atomic codes with the same upstream calling relationship and find the root node in the merged atomic code set to perform path compression and finally generate the union-find set of the target code.
[0062] Step S104: using the union-find method, searching for call links of each component code in the target code.
[0063] Specifically, based on the union-find set of the target code generated above, a graph search algorithm is used to efficiently and quickly search for the upstream and downstream calling relationships of each atomized code, and then obtain the calling links of each atomized code, that is, the calling links of each component code.
[0064] Figure 2 It is a flow chart of the method for determining the code call link of an embodiment of the present invention. First, the atomic code set of the generated target code is obtained by using a loading tool; then, according to the upstream and downstream call relationships between the atomic codes contained in each atomic code in the atomic code set, each atomic code is merged and the root node is searched to construct a union-find set of the generated target code; a search algorithm is used to search for the call links of each component code in the target code, and a visual display of the call links is performed according to an external acquisition request.
[0065] According to one embodiment of the present invention, in response to the target code having an associated external application, the method further includes: obtaining the external application associated with the target code, constructing an abstract syntax tree of the external application based on the application code of the external application, and then generating an atomic code set of the application code in combination with the attribute composition, and then generating a union-find set of the application code; using the union-find set of the target code and the union-find set of the application code, searching for call links of each component code in the target code.
[0066] Specifically, the embodiment of the present invention not only proposes a method for determining an atomic code call link based on the code structure, but also the determination method supports the determination of code call links across applications. When the target code has an associated or dependent external application, the external application or external service associated with the target code is obtained, which can be obtained specifically from the configuration file of the target code. Then, according to the above-mentioned method for generating an atomic code set of the target code, an atomic code set of the application code is generated, and then a union-find set of the application code is generated. Finally, the union-find set of the target code and the union-find set of the application code are combined to search for the call links of each component code in the target code.
[0067] Figure 3 It is a schematic diagram of the method for generating atomic code of an embodiment of the present invention. First, use the loading tool to obtain the configuration file, and obtain the code-associated and dependent external applications from the configuration file. Then obtain the latest version of the code through the code collaborative development platform git, clone the code, and obtain the target code. Use a word segmenter to segment the target code into code segments to obtain a code segment set. Then, according to the segmentation order, use a parser to parse each code segment, and according to the parsing result, find a matching abstract syntax tree template from a preset abstract syntax tree template, build an abstract syntax tree of the target code, and identify independent leaf nodes that have no call relationship in the abstract syntax tree, filter and delete independent leaf nodes, and obtain an optimized abstract syntax tree.
[0068] Further, according to the preset attribute composition of the atomic code and the optimized abstract syntax tree, determine whether the attribute information of the leaf nodes in the abstract syntax tree matches the basic attributes of the atomic code. If there are unmatched leaf nodes, delete the unmatched leaf nodes. If they all match, extract the attribute value corresponding to the matching attribute name from the attribute information of each leaf node in the abstract syntax tree to obtain the basic attribute information of each atomic code of the target code. Then, according to the connection relationship between each leaf node in the abstract syntax tree, perform atomic association, determine the calling relationship attribute information of each atomic code of the target code, and generate the atomic code set of the target code according to the basic attribute information and calling relationship attribute information of each atomic code, as well as the atomic code composition of the target code. Finally, store the atomic code set of the target code in a data file for subsequent construction of the query set.
[0069] According to another embodiment of the present invention, the calling links of each component code in the target code are searched by utilizing the union-find set of the target code and the union-find set of the application code, including: determining the associated node between the union-find set of the target code and the union-find set of the application code according to the associated code between the target code and the external application, establishing a connection between the union-find set of the target code and the union-find set of the application code to obtain a cross-application union-find set; searching the calling links of each component code in the target code from the cross-application union-find set.
[0070] Specifically, based on the above analysis of the target code, and when the target code has an associated or dependent external application, the interactive interface code or calling code between the target code and the external application is obtained from the analysis result. According to the name of the interface code and the calling code, the corresponding associated node is found from the union-find set of the target code and the union-find set of the application code, and the union-find set of the target code and the union-find set of the application code are associated and integrated by connecting the associated nodes to obtain a cross-application union-find set. Using a graph search algorithm, the call links of each component code in the target code are searched from the associated and integrated cross-application union-find set.
[0071] Figure 4 It is a comparative schematic diagram of different search methods for code call links across application scenarios of an embodiment of the present invention. The left figure adopts a traditional search method, which needs to search step by step according to the connection relationship of each node. The figure shows 5 associated external applications of the target code. The middle figure uses union-find to perform path compression in the same cross-application scenario, and the solid line is the node connection relationship after the union-find path is compressed. Based on the node connection relationship after the path compression of the middle graph, the cross-application union-find on the right is obtained, and then the graph search algorithm is called to quickly and efficiently obtain the complete call link of each component code in the target code.
[0072] The embodiment of the present invention uses static code analysis to atomize the target code, and generates an atomic code set of the target code by means of an abstract syntax tree combined with the attribute composition of the customized atomic code. Then, based on the upstream and downstream call relationships in the atomic code, a union-find set of the target code is generated, and an offline code call link is searched based on the union-find set, thereby realizing a method for determining the atomic code call link based on the code structure, which can determine the code call link quickly, accurately and with low consumption without relying on compilation and code running. In addition, the association and dependent external applications or services of the target code are obtained through a configuration file, and a union-find set of external applications is constructed according to the method of the embodiment of the present invention, and then the union-find set of the target code and the union-find set of the external application are used to obtain the cross-application code call link, thereby realizing the determination of the code call link across application scenarios and expanding the application scenarios.
[0073] Figure 5 Schematic diagram of the main modules of the device for determining the code call link according to an embodiment of the present invention. Figure 5 As shown, the device 500 for determining a code call link mainly includes a syntax tree generation module 501 , a code atomization module 502 , a union-find generation module 503 and a call link determination module 504 .
[0074] The syntax tree generation module 501 is used to parse the target code and find a matching abstract syntax tree template from a preset abstract syntax tree template according to the parsing result, so as to construct an abstract syntax tree of the target code;
[0075] The code atomization module 502 is used to determine the atomization code composition of the target code and the attribute information of each atomization code according to the preset attribute composition of the atomization code and the abstract syntax tree, so as to generate an atomization code set of the target code, wherein each atomization code in the atomization code set includes an upstream and downstream calling relationship between the atomization codes;
[0076] A union-find set generation module 503 is used to generate a union-find set of the target code by merging the respective atomized codes and searching for the root node according to the upstream and downstream call relationships;
[0077] The call link determination module 504 is used to use the union-find set to search for the call links of each component code in the target code.
[0078] According to one embodiment of the present invention, the attribute composition of the atomized code includes basic attributes and calling relationship attributes; the code atomization module 502 is also used to: determine the atomized code composition of the target code according to the leaf nodes in the abstract syntax tree; extract the attribute value corresponding to the matching attribute name from the attribute information of each leaf node in the abstract syntax tree according to the basic attributes in the attribute composition, and obtain the basic attribute information of each atomized code of the target code; determine the calling relationship attribute information of each atomized code of the target code according to the connection relationship between each leaf node in the abstract syntax tree.
[0079] According to another embodiment of the present invention, the code atomization module 502 is also used to: divide the leaf nodes with the same node path in the abstract syntax tree into the same group; for each of the atomized codes in the target code: select a target group that matches the node path of the atomized code from the leaf nodes of each group; and extract the calling relationship attribute information of the atomized code from the upper and lower leaf nodes connected to each leaf node of the target group.
[0080] According to another embodiment of the present invention, in response to the target code having an associated external application, the code call link determination device 500 also includes a cross-application call link determination module (not shown in the figure), which is used to: obtain the external application associated with the target code, build an abstract syntax tree of the external application based on the application code of the external application, and then generate an atomic code set of the application code in combination with the attribute composition, and then generate a union-find set of the application code; use the union-find set of the target code and the union-find set of the application code to search for call links of each component code in the target code.
[0081] According to another embodiment of the present invention, the cross-application call link determination module (not shown in the figure) is further used to: determine the association node between the union-find set of the target code and the union-find set of the application code based on the association code between the target code and the external application, establish a connection between the union-find set of the target code and the union-find set of the application code, and obtain a cross-application union-find set; search for call links of each component code in the target code from the cross-application union-find set.
[0082] According to another embodiment of the present invention, the syntax tree generation module 501 is further used to: obtain the latest version of the target code, segment the target code of the latest version into code segments to obtain a code segment set; and parse each code segment in the code segment set according to the segmentation order.
[0083] According to another embodiment of the present invention, the device 500 for determining the code call link also includes an independent node identification module (not shown in the figure), which is used to: after constructing the abstract syntax tree of the target code, identify independent leaf nodes that have no call relationship in the abstract syntax tree, and delete the independent leaf nodes.
[0084] According to another embodiment of the present invention, the device 500 for determining the code call link also includes a matching confirmation module (not shown in the figure), which is used to: before extracting the attribute value corresponding to the matching attribute name from the attribute information of each leaf node in the abstract syntax tree, judge and confirm that the attribute information of the leaf node in the abstract syntax tree matches the basic attributes of the atomic code; in response to the presence of leaf nodes in the abstract syntax tree that do not match the basic attributes of the atomic code, delete the unmatched leaf nodes.
[0085] Figure 6 is an exemplary system architecture diagram to which embodiments of the present invention can be applied.
[0086] like Figure 6 As shown, system architecture 600 may include terminal devices 601, 602, 603, network 604 and server 605. Network 604 is used to provide a medium for communication links between terminal devices 601, 602, 603 and server 605. Network 604 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0087] Users can use terminal devices 601, 602, 603 to interact with server 605 through network 604 to receive or send messages, etc. Various communication client applications, such as link determination applications, etc., can be installed on terminal devices 601, 602, 603 (only as an example).
[0088] The terminal devices 601 , 602 , and 603 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers, etc.
[0089] The server 605 may be a server that provides various services, such as a background management server (for example only) that provides support for the code call link performed by the user using the terminal devices 601, 602, and 603. The background management server may parse the target code, and according to the parsing result, find a matching abstract syntax tree template from a preset abstract syntax tree template, thereby constructing an abstract syntax tree of the target code; according to the preset attribute composition of the atomic code and the abstract syntax tree, determine the atomic code composition of the target code and the attribute information of each of the atomic codes, thereby generating an atomic code set of the target code, wherein each atomic code in the atomic code set contains an upstream and downstream call relationship between the atomic codes; according to the upstream and downstream call relationship, generate a union-find set of the target code by merging each of the atomic codes and searching for the root node; use the union-find set to search for the call link of each component code in the target code and other processing, and feed back the processing result (such as the code call link, etc. - for example only) to the terminal device.
[0090] It should be noted that the method for determining the code call link provided in the embodiment of the present invention is generally executed by the server 605 , and accordingly, the device for determining the code call link is generally set in the server 605 .
[0091] It should be understood that Figure 6 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0092] Reference below Figure 7 , Figure 7 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. Figure 7 The terminal device or server shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0093] like Figure 7 As shown, the computer system 700 includes a central processing unit (CPU) 701, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 702 or a program loaded from a storage part 708 into a random access memory (RAM) 703. In the RAM 703, various programs and data required for the operation of the system 700 are also stored. The CPU 701, the ROM 702, and the RAM 703 are connected to each other through a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.
[0094] The following components are connected to the I / O interface 705: an input section 706 including a keyboard, a mouse, etc.; an output section 707 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 708 including a hard disk, etc.; and a communication section 709 including a network interface card such as a LAN card, a modem, etc. The communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the I / O interface 705 as needed. A removable medium 711, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 710 as needed, so that a computer program read therefrom is installed into the storage section 708 as needed.
[0095] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program contains program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 709, and / or installed from the removable medium 711. When the computer program is executed by the central processing unit (CPU) 701, the above-mentioned functions defined in the system of the present invention are executed.
[0096] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination thereof.
[0097] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a sequence different from that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0098] The units involved in the embodiments of the present invention may be implemented in software or hardware. The units described may also be arranged in a processor, for example, may be described as follows: a processor includes: a syntax tree generation module, a code atomization module, a union-find generation module, and a call link determination module.
[0099] Among them, the names of these modules do not constitute a limitation on the modules themselves in certain cases. For example, the call link determination module can also be described as "a module used to search for call links of each component code in the target code by utilizing the union-find set."
[0100] On the other hand, the present invention also provides a computer-readable medium, which may be included in the device described in the embodiment; or it may exist independently and not be assembled into the device. The computer-readable medium carries one or more programs, and when the one or more programs are executed by a device, the device includes: parsing the target code, and according to the parsing result, finding a matching abstract syntax tree template from a preset abstract syntax tree template, thereby constructing an abstract syntax tree of the target code; according to the preset attribute composition of the atomic code and the abstract syntax tree, determining the atomic code composition of the target code and the attribute information of each atomic code, thereby generating an atomic code set of the target code, wherein each atomic code in the atomic code set contains an upstream and downstream calling relationship between the atomic codes; according to the upstream and downstream calling relationship, by merging each atomic code and searching for the root node, generating a union-find set of the target code; using the union-find set, searching for the calling links of each component code in the target code.
[0101] The technical solution according to the embodiment of the present invention has the following advantages or beneficial effects: by parsing the target code, according to the parsing result, a matching abstract syntax tree template is found from a preset abstract syntax tree template, thereby constructing an abstract syntax tree of the target code; according to the preset attribute composition of the atomic code and the abstract syntax tree, the atomic code composition of the target code and the attribute information of each atomic code are determined, thereby generating an atomic code set of the target code, and each atomic code in the atomic code set contains the upstream and downstream calling relationship between the atomic codes; according to the upstream and downstream calling relationship, by merging each atomic code and searching the root node, a union-find set of the target code is generated; using the union-find set, a technical solution for searching the call links of each component code in the target code is implemented, and a method for determining the atomic code call link based on the code structure is realized, and the code call link is determined by adopting static code analysis and offline code call link search, which does not rely on compilation and code running, and can quickly, accurately and low-cost determine the code call link.
[0102] The specific implementation methods described herein do not constitute limitations on the scope of protection of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention shall be included in the scope of protection of the present invention.
Claims
1. A method for determining a code call link, characterized in that: include: Parsing the target code, and finding a matching abstract syntax tree template from a preset abstract syntax tree template according to the parsing result, thereby constructing an abstract syntax tree of the target code; According to the preset attribute composition of the atomic code and the abstract syntax tree, the atomic code composition of the target code and the attribute information of each of the atomic codes are determined, thereby generating an atomic code set of the target code, wherein each atomic code in the atomic code set includes an upstream and downstream calling relationship between the atomic codes; According to the upstream and downstream calling relationships, by merging the respective atomized codes and searching for the root nodes, a union-find set of the target codes is generated; The union-find set is used to search for call links of each component code in the target code.
2. The method according to claim 1, characterized in that The attribute composition of the atomized code includes basic attributes and call relationship attributes; According to the preset attribute composition of the atomic code and the abstract syntax tree, the atomic code composition of the target code and the attribute information of each of the atomic codes are determined, including: Determining the atomic code composition of the target code according to the leaf nodes in the abstract syntax tree; According to the basic attributes in the attribute composition, extract the attribute value corresponding to the matching attribute name from the attribute information of each leaf node in the abstract syntax tree to obtain the basic attribute information of each atomized code of the target code; According to the connection relationship between each leaf node in the abstract syntax tree, the calling relationship attribute information of each atomized code of the target code is determined.
3. The method according to claim 2, characterized in that Determining the calling relationship attribute information of each of the atomized codes of the target code according to the connection relationship between each leaf node in the abstract syntax tree includes: Dividing leaf nodes having the same node path in the abstract syntax tree into the same group; For each of the atomized codes in the target code: a target group matching the node path of the atomized code is selected from the leaf nodes of each group; and the calling relationship attribute information of the atomized code is extracted from the upper and lower leaf nodes connected to each leaf node of the target group.
4. The method according to claim 1, characterized in that: In response to the object code having an associated external application, the method further includes: Acquire an external application associated with the target code, construct an abstract syntax tree of the external application according to the application code of the external application, and then generate an atomized code set of the application code in combination with the attribute composition, and then generate a union-find set of the application code; The call links of each component code in the target code are searched by using the union-find set of the target code and the union-find set of the application code.
5. The method according to claim 4, characterized in that Using the union-find set of the target code and the union-find set of the application code, searching for call links of each component code in the target code includes: According to the association code between the target code and the external application, determining the association node between the union-find set of the target code and the union-find set of the application code, establishing a connection between the union-find set of the target code and the union-find set of the application code, and obtaining a cross-application union-find set; The calling links of each component code in the target code are searched from the cross-application combined query set.
6. The method according to claim 1, characterized in that Parsing target code, including: Obtaining the latest version of the target code, dividing the target code of the latest version into code segments to obtain a code segment set; Each code segment in the code segment set is parsed according to the segmentation order.
7. The method according to claim 1, characterized in that After constructing the abstract syntax tree of the target code, the method further comprises: An independent leaf node without a call relationship in the abstract syntax tree is identified, and the independent leaf node is deleted.
8. The method according to claim 2, characterized in that: Before extracting the attribute value corresponding to the matched attribute name from the attribute information of each leaf node in the abstract syntax tree, the method further includes: Determine and confirm that the attribute information of the leaf nodes in the abstract syntax tree matches the basic attributes of the atomized code; In response to the presence of leaf nodes in the abstract syntax tree that do not match the basic attributes of the atomized code, the unmatched leaf nodes are deleted.
9. A device for determining a code call link, characterized in that: include: A syntax tree generation module is used to parse the target code and, based on the parsing result, find a matching abstract syntax tree template from a preset abstract syntax tree template, thereby constructing an abstract syntax tree of the target code; A code atomization module, used to determine the atomization code composition of the target code and the attribute information of each atomization code according to the preset attribute composition of the atomization code and the abstract syntax tree, so as to generate an atomization code set of the target code, wherein each atomization code in the atomization code set includes an upstream and downstream calling relationship between the atomization codes; A union-find set generation module, used for generating a union-find set of the target code by merging the respective atomized codes and searching for the root node according to the upstream and downstream call relationships; The call link determination module is used to use the union-find set to search for the call links of each component code in the target code.
10. A mobile electronic device terminal, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 8.
11. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.
12. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.