A code dependency detection method, device and system
Patent Information
- Application Number
- CN202310387165.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-12
- Publication Date
- 2026-10-16
- Estimated Expiration
- 2043-04-12
AI Technical Summary
[0004]针对现有技术的以上缺陷或改进需求,本发明提供了一种代码依赖关系的检测方法、装置和系统,其目的在于,本发明通过创建代码仓库-版本-函数对应的嵌套索引字典,对其进行两次噪声函数过滤得到项目指纹函数;区别于现有技术中直接将待测函数与仓库内所有项目函数进行相似性比较,本发明将待测项目函数与项目指纹函数进行代码克隆检测得到待测项目函数的依赖关系,其计算复杂度更低且检测效率更高;由此解决现有代码可能检测计算复杂大且精度低的技术问题
[0039] (1) This invention creates a nested index dictionary corresponding to code repository, version, and function. It then performs deduplication and complexity filtering on the functions in the nested index dictionary, followed by centrality-based filtering to ultimately obtain the project fingerprint function. Unlike existing technologies that directly compare the function to be tested with all project functions in the repository, this invention uses code probability detection to determine code dependencies between the project function to be tested and the project fingerprint function obtained after two filtering steps. This results in lower computational complexity and better accuracy. This invention can quickly identify dependencies in large-scale code repositories. The two-step function filtering significantly reduces the number of functions in the code repository, lowers computational complexity, improves processing speed, and supports component dependency analysis of massive amounts of data.
Smart Images

Figure CN116521173B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of software component analysis technology, and more specifically, relates to a method, apparatus and system for detecting code dependencies. Background Technology
[0002] Software composition analysis is an important research area in software engineering. By analyzing the dependencies between software components, software composition analysis helps developers understand and manage the structure and complexity of software systems, thereby improving their maintainability and reusability. Currently, most mainstream programming languages primarily use standard project management tools to parse software dependency files to obtain software dependencies. Currently, software composition analysis can only be performed using code clone detection techniques. However, comparing the similarity of all functions or files in the project under test with functions or files in other components in the open-source community is too time-consuming. Therefore, limited by the time overhead of code clone detection methods, they are difficult to support software composition analysis for large-scale programming projects.
[0003] Existing software component analysis methods primarily rely on code clone detection techniques. This typically involves comparing functions at the function level with functions across all projects in the open-source community, assuming high similarity and code dependencies. However, only a small subset of functions in a project are truly representative—its core functions. Therefore, comparing all functions not only incurs significant time overhead but also introduces numerous false positives. For instance, most projects include file read / write functions; cloning these functions would inevitably reveal code dependencies with numerous other software components. However, these dependencies are not true component dependencies. Summary of the Invention
[0004] To address the aforementioned deficiencies or improvement needs of existing technologies, this invention provides a method, apparatus, and system for detecting code dependencies. The purpose of this invention is to obtain project fingerprint functions by creating a nested index dictionary corresponding to code repository-version-function and performing two rounds of noise function filtering. Unlike existing technologies that directly compare the function to be tested with all project functions in the repository, this invention performs code clone detection on the project function to be tested and the project fingerprint functions to obtain the dependency relationship of the project function to be tested. This results in lower computational complexity and higher detection efficiency, thereby solving the technical problem of potentially high computational complexity and low accuracy in existing code dependency detection methods.
[0005] To achieve the above objectives, according to one aspect of the present invention, a method for detecting code dependencies is provided, comprising:
[0006] S1: Break down the code of each version of each project in the code repository into multiple functions and generate a call graph for each function; then stitch together the call graphs of all functions corresponding to each version to obtain the function call graph for each version.
[0007] S2: Calculate the index dictionary D1 for each project based on the union of the functions corresponding to each version of each project; create a nested index dictionary H1 corresponding to the code repository-version-function; and use the index dictionary D1 of each project to obtain the index dictionary D2 of the code repository.
[0008] S3: Perform a duplicate check on the functions in the nested index dictionary H1 and the functions in the index dictionary D2 to remove duplicates. Then, perform complexity filtering on each function in the deduplicated nested index dictionary H1 to obtain a new nested index dictionary H2. Perform centrality filtering on the function call graph of each version in the nested index dictionary H1 to obtain the core function list FL2 for each version. Perform an intersection operation on the core function list FL2 and the function information list FL1 in the new nested index dictionary H2 to obtain the project fingerprint function H3.
[0009] S4: Perform code clone detection on the code functions of the project to be detected and the project fingerprint function H3 to obtain the code dependency relationship of the project to be detected.
[0010] In one embodiment, S3 includes:
[0011] S301: Perform a duplicate check and comparison between the functions in the nested index dictionary H1 and the functions in the index dictionary D2 to remove duplicates. Then, perform complexity filtering on each function in the deduplicated nested index dictionary H1 to obtain a new nested index dictionary H2 and its function information list FL1.
[0012] S302: Calculate the centrality evaluation value C of the function call graph for each version in each project of the code repository based on centrality analysis. mean The functions that meet the centrality requirement are included in the core function list FL2 of each version;
[0013] S303: Perform an intersection operation on the core function list FL2 and the function information list FL1 in the new nested index dictionary H2 to obtain the project fingerprint function H3.
[0014] In one embodiment, S302 includes:
[0015] S3021: Calculate the degree centrality, Katz centrality, betweenness centrality, eigenvector centrality, compact centrality, and harmonic centrality of the function call graph corresponding to each version in the nested index dictionary H1;
[0016] S3022: Calculate the centrality evaluation value C of the corresponding version of the function call graph based on the centrality and their respective weights obtained in S3021. mean .
[0017] In one embodiment, S3022 includes: using a formula The centrality evaluation value C of the computed version of the function call graph. mean ;
[0018] Among them, C d For Katz centrality, C k For Katz centrality, C b For betweenness centrality, C e For eigenvector centrality, C c For tight centrality, C h To harmonize centrality.
[0019] In one embodiment, S301 includes:
[0020] S3011: Compare the functions in the nested index dictionary H1 with the functions in the index dictionary D2 to remove duplicates;
[0021] S3012: Calculate the code maintainability index of each function in the deduplicated nested index dictionary H1 to characterize the computational complexity of the function, and then filter out functions whose complexity does not meet the preset requirements to obtain the function information list FL1 and its corresponding new nested index dictionary H2.
[0022] In one embodiment, S2 includes:
[0023] S201: Take the union of the function libraries corresponding to each version of each project as the unique function library of the project, and use the hash value of the unique function library of each project as the key and its function information as the value to construct the index dictionary D1 of each project;
[0024] S202: Create a nested index dictionary H1 corresponding to the repository-version-function information list; its first key is the hash value of the repository number, the second key is the hash value of the version information, and the value is the function information;
[0025] S203: Take the union of the unique function libraries of each project as the unique function library of the code repository, and construct the index dictionary D2 of the code repository with the hash value of the unique function library of the code repository as the key and the project name, function path and function information as the value.
[0026] In one embodiment, S4 includes:
[0027] S401: Convert all functions in the code function and project fingerprint function H3 of the project to be detected into semantic vectors;
[0028] S402: Use the semantic vectors of the code functions of the project to be detected by clustering algorithm and the semantic vectors of each function in the project fingerprint function H3 to perform code clone detection and obtain the code dependency relationship of the project to be detected.
[0029] In one embodiment, S402 includes:
[0030] S4021: The k-means algorithm is used to cluster the semantic vectors of the items to be detected and the semantic vectors of each function in the item fingerprint function H3, and the elbow method is used to determine the number of clusters.
[0031] S4022: If the code function of the project to be detected is in any cluster of the clustering results of each function in the project fingerprint function H3, then the project to be detected is identified as having code cloning.
[0032] According to another aspect of the present invention, a code dependency detection apparatus is provided, comprising:
[0033] The preprocessing module is used to break down the code of each version of each project in the code repository into multiple functions and generate a call graph for each function; the call graphs of all functions corresponding to each version are then concatenated to obtain the function call graph for each version.
[0034] The index building module is used to calculate the index dictionary D1 for each project based on the union of the functions corresponding to each version of each project; create a nested index dictionary H1 corresponding to the code repository-version-function; and obtain the index dictionary D2 of the code repository from the index dictionary D1 of each project.
[0035] The project fingerprint determination module is used to compare and remove duplicate functions in nested index dictionary H1 and index dictionary D2, then perform complexity filtering on each function in the deduplicated nested index dictionary H1 to obtain a new nested index dictionary H2; perform centrality filtering on the function call graph of each version in nested index dictionary H1 to obtain the core function list FL2 of each version; and perform the intersection operation of the core function list FL2 and the function information list FL1 in the new nested index dictionary H2 to obtain the project fingerprint function H3.
[0036] The dependency detection module is used to perform code clone detection on the code functions of the project to be detected and the project fingerprint function H3 to obtain the code dependency relationship of the project to be detected.
[0037] According to another aspect of the present invention, a code dependency detection system is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the method described above.
[0038] In summary, compared with the prior art, the above-described technical solutions conceived by this invention can achieve the following beneficial effects:
[0039] (1) This invention creates a nested index dictionary corresponding to code repository, version, and function. It then performs deduplication and complexity filtering on the functions in the nested index dictionary, followed by centrality-based filtering to ultimately obtain the project fingerprint function. Unlike existing technologies that directly compare the function to be tested with all project functions in the repository, this invention uses code probability detection to determine code dependencies between the project function to be tested and the project fingerprint function obtained after two filtering steps. This results in lower computational complexity and better accuracy. This invention can quickly identify dependencies in large-scale code repositories. The two-step function filtering significantly reduces the number of functions in the code repository, lowers computational complexity, improves processing speed, and supports component dependency analysis of massive amounts of data.
[0040] (2) This scheme is based on the centrality analysis method to calculate the centrality evaluation value C of the function call graph of each version in each project in the code repository. mean It can filter out functions that do not meet the centrality requirement and can quickly achieve secondary filtering; its algorithm complexity is low, which can improve the efficiency of obtaining project fingerprint functions, thereby reducing the computational complexity of the entire dependency detection method.
[0041] (3) This scheme calculates the degree centrality, Katz centrality, betweenness centrality, eigenvector centrality, compact centrality, and harmonic centrality of the function call graph corresponding to each version in the nested index dictionary H1; then, it uses the calculated centrality and its corresponding weights to calculate the centrality evaluation value C. mean It can ensure both computational efficiency and accuracy, efficiently and accurately obtaining the centrality evaluation value C. mean It can improve the efficiency of obtaining project fingerprint functions, thereby improving the computational efficiency of the entire dependency detection method.
[0042] (4) This scheme uses the mean of degree centrality, Katz centrality, betweenness centrality, eigenvector centrality, compact centrality, and harmonic centrality as the centrality evaluation value C. mean Furthermore, the centrality filtering algorithm has low complexity and high accuracy, and ultimately obtains the project fingerprint function accurately and efficiently, laying the foundation for the efficient and accurate detection of the dependencies of the projects to be detected.
[0043] (5) This scheme calculates the code maintainability index of each function in the nested index dictionary H1 after deduplication. The code maintainability index can be quickly calculated using the Halsted volume HV, cyclomatic complexity CC, and function line count LOC. The computational complexity is low, which can quickly filter out functions whose computational complexity does not meet the requirements, thereby improving the computational efficiency of the entire dependency detection algorithm.
[0044] (6) This scheme uses a hash algorithm to calculate the key of the index dictionary D1 of each project, the key of the index dictionary D2 of the code repository, and the two keys of the nested index dictionary H1. The hash algorithm has low computational complexity and has the function of automatic deduplication, which can reduce the computational complexity of the entire dependency detection algorithm.
[0045] (7) This scheme converts the code function and project fingerprint function H3 of the project to be detected into semantic vectors; uses a clustering algorithm to perform code clone detection on all semantic vectors, and can identify semantic code dependencies based on the clustering results, thereby improving the detection accuracy.
[0046] (8) This scheme uses the k-means algorithm to cluster all semantic vectors and the elbow method to determine the number of clusters, thus obtaining the optimal number of clusters. Finally, by analyzing whether the code function of the project to be detected belongs to any cluster of the H3 clustering result of the project fingerprint function, the code dependency relationship can be determined. By combining the k-means algorithm and the elbow method to achieve code probabilities detection, the detection accuracy can be improved. Attached Figure Description
[0047] Figure 1 This is a flowchart illustrating the code dependency detection method provided in Embodiment 1 of the present invention.
[0048] Figure 2 This is a flowchart illustrating step S3 in the code dependency detection method provided in Embodiment 2 of the present invention.
[0049] Figure 3 This is a flowchart illustrating step S3 in the code dependency detection method provided in Embodiment 6 of the present invention.
[0050] Figure 4 This is a flowchart illustrating step S3 in the code dependency detection method provided in Embodiment 7 of the present invention. Detailed Implementation
[0051] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention. Furthermore, the technical features involved in the various embodiments of this invention described below can be combined with each other as long as they do not conflict with each other.
[0052] Example 1
[0053] like Figure 1 As shown, the present invention provides a method for detecting code dependencies, including:
[0054] S1: Break down the code of each version of each project in the code repository into multiple functions and generate a call graph for each function; then stitch together the call graphs of all functions corresponding to each version to obtain the function call graph for each version.
[0055] S2: Calculate the index dictionary D1 for each project based on the union of the functions corresponding to each version of each project; create a nested index dictionary H1 corresponding to the code repository-version-function; and use the index dictionary D1 of each project to obtain the index dictionary D2 of the code repository.
[0056] S3: Perform a duplicate check on the functions in the nested index dictionary H1 and the functions in the index dictionary D2 to remove duplicates. Then, perform complexity filtering on each function in the deduplicated nested index dictionary H1 to obtain a new nested index dictionary H2. Perform centrality filtering on the function call graph of each version in the nested index dictionary H1 to obtain the core function list FL2 for each version. Perform an intersection operation on the core function list FL2 and the function information list FL1 in the new nested index dictionary H2 to obtain the project fingerprint function H3.
[0057] S4: Perform code clone detection on the code functions of the project to be detected and the project fingerprint function H3 to obtain the code dependency relationship of the project to be detected.
[0058] Step S1 involves acquiring the original dataset from the code repository and breaking down the project code for each version into functions. Function call graphs are then generated for these functions and pieced together to create function call graphs for each version of the project. Specifically, for the input original dataset, each version of the repository needs to be broken down into functions to obtain function information for all versions across all repositories. For all function information, tools such as Doxygen can be used to generate function call graphs for each function in each version of all projects, and these graphs can be connected to obtain the function call graphs for each specific version of all projects.
[0059] Step S2. Build an index dictionary for all versions of the repository, then integrate the code repository to build an index dictionary for the entire code repository, and also build a nested hash index of the repository-version-function information list for use in subsequent steps.
[0060] Example 2
[0061] like Figure 2 As shown, S3 includes:
[0062] S301: Perform a duplicate check and comparison between the functions in the nested index dictionary H1 and the functions in the index dictionary D2 to remove duplicates. Then, perform complexity filtering on each function in the deduplicated nested index dictionary H1 to obtain a new nested index dictionary H2 and its function information list FL1.
[0063] S302: Calculate the centrality evaluation value C of the function call graph for each version in each project of the code repository based on centrality analysis. mean The functions that meet the centrality requirement are included in the core function list FL2 of each version;
[0064] S303: Perform an intersection operation on the core function list FL2 and the function information list FL1 in the new nested index dictionary H2 to obtain the project fingerprint function H3.
[0065] Specifically, the nested index dictionary H1 is first deduplicated and subjected to coarse-grained complexity filtering. Then, centrality analysis is used to calculate the centrality evaluation value C of the function call graph for each version of each project in the code repository. mean The functions that meet the centrality requirement are listed in the core function list FL2 of each version. The intersection operation of the core function list FL2 and the function information list FL1 in the new nested index dictionary H2 is performed to achieve fine-grained secondary filtering, which can filter out noisy functions, reduce complexity, and obtain a more accurate project fingerprint function H3. H3 is the new nested index dictionary.
[0066] Example 3
[0067] S302 includes:
[0068] S3021: Calculate the degree centrality, Katz centrality, betweenness centrality, eigenvector centrality, compact centrality, and harmonic centrality of the function call graph corresponding to each version in the nested index dictionary H1;
[0069] S3022: Calculate the centrality evaluation value C of the corresponding version of the function call graph based on the centrality and their respective weights obtained in S3021. mean .
[0070] Specifically, using the function call graphs of each version in step S1, the degree centrality, Katz centrality, betweenness centrality, eigenvector centrality, tight centrality, and harmonic centrality of each function in each version of the project are calculated using centrality analysis. Then, through an ensemble method, based on the obtained centralities and their weights, a core function information list FL2 for each version is calculated. This FL2 is then intersected with the function information list FL1 in the nested hash index H2 to obtain the final project fingerprint F. The original FL1 is then replaced to obtain the final project fingerprint index.
[0071] Degree centrality is a metric that assigns importance scores based on the number of links each node has. In a network, nodes with higher degree centrality typically have more connections and are likely to play a more important role in information propagation and network flow. It normalizes a node's degree by dividing it by the maximum possible degree N-1 in the graph, where N represents the number of nodes in the graph and deg(v) represents the degree of node v. The calculation formula is as follows:
[0072] Katz centrality is a metric used to measure the relative influence of a node in a graph. It is determined by calculating the association between a node and its direct neighbors, as well as all other nodes connected to that node through these direct neighbors. Let the Katz centrality of node i be C. k (i) The element at position (i,j) of the adjacency matrix A is subjected to a power of k (i.e., A) k This reflects the total number of k-degree connections between node i and node j. The Katz centrality calculation formula is: Where α represents the attenuation factor.
[0073] Betweenness centrality is a metric that determines the importance of a node by measuring the number of shortest paths passing through it. The betweenness centrality of a node v can be expressed by the following formula, where δ s,t δ is the number of shortest paths from node s to node t. s,t (v) represents the number of shortest paths passing through node v. The formula for calculating the betweenness centrality of node v is:
[0074] Eigenvector centrality depends on the number and importance of neighboring nodes. Suppose in a graph with n nodes, x... i This represents the importance of node i. Here, c is a scaling constant, and a... ij =1 if and only if node i is connected to node j, otherwise it is 0. The formula for calculating the eigenvector centrality of a node is:
[0075] Tight centrality measures how easy it is for a node to reach all other nodes; it is the inverse average of the distances to all other nodes. The formula is: Where dis(i,j) represents the distance from node i to node j, and n is the number of nodes in the graph.
[0076] Harmonic centrality is specifically used to calculate the compact centrality of disconnected graphs. Therefore, part of the harmonic centrality formula is the same as the compact centrality measure formula. The formula for calculating harmonic centrality is:
[0077] Example 4
[0078] S3022 includes: using formulas The centrality evaluation value C of the computed version of the function call graph. mean ;
[0079] Among them, C d For Katz centrality, C k For Katz centrality, C b For betweenness centrality, C e For eigenvector centrality, C c For tight centrality, C h To harmonize centrality.
[0080] This scheme uses the mean of degree centrality, Katz centrality, betweenness centrality, eigenvector centrality, compact centrality, and harmonic centrality as the centrality evaluation value C. mean Furthermore, the centrality filtering algorithm has low complexity and high accuracy, ultimately obtaining the project fingerprint function accurately and efficiently, laying the foundation for efficient and accurate detection of project dependencies.
[0081] Example 5
[0082] S301 includes:
[0083] S3011: Compare the functions in the nested index dictionary H1 with the functions in the index dictionary D2 to remove duplicates;
[0084] S3012: Calculate the code maintainability index of each function in the deduplicated nested index dictionary H1 to characterize the computational complexity of the function, and then filter out functions whose complexity does not meet the preset requirements to obtain the function information list FL1 and its corresponding new nested index dictionary H2.
[0085] Specifically, the inputs to this step are the function call graph from step S1 and H1 from step S2. First, H1 is used for deduplication, followed by coarse-grained filtering using the code maintainability index. The code maintainability index (MI) can be calculated using the function's Halsted volume (HV), cyclomatic complexity (CC), and line count (LOC) to evaluate the function's complexity. The formula for calculating the code maintainability index is: MI = 171 - 5.2 * ln(HV) - 0.23 * (CC) - 16.2 * ln(LOC). A threshold can be set for the code maintainability index to complete the coarse-grained filtering, resulting in a new index dictionary H2.
[0086] Example 6
[0087] like Figure 3 As shown, S2 includes:
[0088] S201: Take the union of the function libraries corresponding to each version of each project as the unique function library of the project, and use the hash value of the unique function library of each project as the key and its function information as the value to construct the index dictionary D1 of each project;
[0089] S202: Create a nested index dictionary H1 corresponding to the repository-version-function information list; its first key is the hash value of the repository number, the second key is the hash value of the version information, and the value is the function information;
[0090] S203: Take the union of the unique function libraries of each project as the unique function library of the code repository, and construct the index dictionary D2 of the code repository with the hash value of the unique function library of the code repository as the key and the project name, function path and function information as the value.
[0091] Specifically, for each project's index dictionary D1, the hash value of the project's unique function library is the key, and the values include function information such as the function's line count, creation time, cyclomatic complexity, and Hallstead volume. For functions that appear repeatedly, the information from the index dictionary is used directly without parsing, and stored as a nested hash index H1 of repository-version-function information list. The first key is the hash value of the repository number, the second key is the hash value of the version information, and the values are function information such as the function's line count, creation time, cyclomatic complexity, and Hallstead volume. For the code repository's index dictionary D2, the hash value of the code repository's unique function library is used as the key, and the values are the project name, function path, and function information, constructing the code repository's index dictionary D2.
[0092] Example 7
[0093] like Figure 4 As shown, S4 includes:
[0094] S401: Convert all functions in the code function and project fingerprint function H3 of the project to be detected into semantic vectors;
[0095] S402: Use the semantic vectors of the code functions of the project to be detected by clustering algorithm and the semantic vectors of each function in the project fingerprint function H3 to perform code clone detection and obtain the code dependency relationship of the project to be detected.
[0096] Specifically, Codebert can be used to convert the code functions of the project to be detected and each function in the project fingerprint function H3 into semantic vectors; then, a clustering algorithm can be used to perform code clone detection on the semantic vectors of the project to be detected and the semantic vectors of the project fingerprint function H3.
[0097] Example 8
[0098] S402 includes:
[0099] S4021: The k-means algorithm is used to cluster the semantic vectors of the items to be detected and the semantic vectors of each function in the item fingerprint function H3, and the elbow method is used to determine the number of clusters.
[0100] S4022: If the code function of the project to be detected is in any cluster of the clustering results of each function in the project fingerprint function H3, then the project to be detected is identified as having code cloning.
[0101] Specifically, the semantic vectors of the items to be detected and the semantic vectors of the item fingerprint function H3 are clustered using the k-means method. The elbow method is used to automatically calculate the number of clusters in the k-means clustering, i.e., increasing the number of clusters and calculating the sum of squares error until the increase in the number of clusters can no longer significantly reduce the sum of squares error, indicating that the optimal cluster value has been found. x i It is the i-th sample, μ j Here, is the centroid of the j-th cluster, n represents the number of samples, and k represents the number of clusters. Finally, by checking which fingerprint functions cluster the target function and the target function in the same category, it's determined that there's a code clone relationship with the third-party component, thus achieving software component dependency analysis.
[0102] Example 9
[0103] This embodiment provides a code dependency detection device, including:
[0104] The preprocessing module is used to break down the code of each version of each project in the code repository into multiple functions and generate a call graph for each function; the call graphs of all functions corresponding to each version are then concatenated to obtain the function call graph for each version.
[0105] The index building module is used to calculate the index dictionary D1 for each project based on the union of the functions corresponding to each version of each project; create a nested index dictionary H1 corresponding to the code repository-version-function; and obtain the index dictionary D2 of the code repository from the index dictionary D1 of each project.
[0106] The project fingerprint determination module is used to compare and remove duplicate functions in nested index dictionary H1 and index dictionary D2, then perform complexity filtering on each function in the deduplicated nested index dictionary H1 to obtain a new nested index dictionary H2; perform centrality filtering on the function call graph of each version in nested index dictionary H1 to obtain the core function list FL2 of each version; and perform the intersection operation of the core function list FL2 and the function information list FL1 in the new nested index dictionary H2 to obtain the project fingerprint function H3.
[0107] The dependency detection module is used to perform code clone detection on the code functions of the project to be detected and the project fingerprint function H3 to obtain the code dependency relationship of the project to be detected.
[0108] Example 10
[0109] This embodiment provides a code dependency detection system, including a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the steps of the method described above.
[0110] Example 11
[0111] This embodiment provides a computer-readable storage medium storing a computer program thereon, characterized in that the computer program, when executed by a processor, implements the steps of the above-described method.
[0112] Those skilled in the art will readily understand that the above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. A method for detecting code dependencies, characterized in that, include: S1: Break down the code of each version of each project in the code repository into multiple functions and generate a call graph for each function; then stitch together the call graphs of all functions corresponding to each version to obtain the function call graph for each version. S2: Calculate the index dictionary D1 for each project based on the union of the functions corresponding to each version of each project; create a nested index dictionary H1 corresponding to the code repository-version-function; and use the index dictionary D1 of each project to obtain the index dictionary D2 of the code repository. S3: Perform a duplicate check on the functions in the nested index dictionary H1 and the functions in the index dictionary D2 to remove duplicates. Then, perform complexity filtering on each function in the deduplicated nested index dictionary H1 to obtain a new nested index dictionary H2. Perform centrality filtering on the function call graph of each version in the nested index dictionary H1 to obtain the core function list FL2 for each version. Perform an intersection operation on the core function list FL2 and the function information list FL1 in the new nested index dictionary H2 to obtain the project fingerprint function H3. S4: Perform code clone detection on the code functions of the project to be detected and the project fingerprint function H3 to obtain the code dependency relationship of the project to be detected; S2 includes: S201: Taking the union of the function libraries corresponding to each version of each project as the unique function library of that project, using the hash value of the unique function library of each project as the key and its function information as the value, constructing an index dictionary D1 for each project; S202: Creating a nested index dictionary H1 corresponding to the repository-version-function information list; its first key is the hash value of the repository number, the second key is the hash value of the version information, and the value is the function information; S203: Taking the union of the unique function libraries of each project as the unique function library of the code repository, using the hash value of the unique function library of the code repository as the key and the project name, function path, and function information as the value, constructing an index dictionary D2 for the code repository; S3 includes: S301: Comparing the functions in the nested index dictionary H1 with the functions in the index dictionary D2 to remove duplicates, and then performing complexity filtering on each function in the deduplicated nested index dictionary H1 to obtain a new nested index dictionary H2 and its function information list FL1; S302: Calculating the centrality evaluation value C of the function call graph of each version in each project in the code repository based on the centrality analysis method. mean S303: Add the functions that meet the centrality requirement to the core function list FL2 of each version; S304: Perform the intersection operation of the core function list FL2 and the function information list FL1 in the new nested index dictionary H2 to obtain the project fingerprint function H3.
2. The code dependency detection method as described in claim 1, characterized in that, S302 includes: S3021: Calculate the degree centrality, Katz centrality, betweenness centrality, eigenvector centrality, compact centrality, and harmonic centrality of the function call graph corresponding to each version in the nested index dictionary H1; S3022: Calculate the centrality evaluation value C of the corresponding version of the function call graph based on the centrality and their respective weights obtained in S3021. mean .
3. The code dependency detection method as described in claim 2, characterized in that, S3022 includes: using the formula The centrality evaluation value C of the computed version of the function call graph mean ; in, For Katz centrality, For Katz centrality, For betweenness centrality, For eigenvector centrality, For tight centrality, To harmonize centrality.
4. The code dependency detection method as described in claim 1, characterized in that, S301 includes: S3011: Compare the functions in the nested index dictionary H1 with the functions in the index dictionary D2 to remove duplicates; S3012: Calculate the code maintainability index of each function in the deduplicated nested index dictionary H1 to characterize the computational complexity of the function, and then filter out functions whose complexity does not meet the preset requirements to obtain the function information list FL1 and its corresponding new nested index dictionary H2.
5. The code dependency detection method as described in claim 1, characterized in that, S4 includes: S401: Convert all functions in the code function and project fingerprint function H3 of the project to be detected into semantic vectors; S402: Use the semantic vectors of the code functions of the project to be detected by clustering algorithm and the semantic vectors of each function in the project fingerprint function H3 to perform code clone detection and obtain the code dependency relationship of the project to be detected.
6. The code dependency detection method as described in claim 5, characterized in that, S402 includes: S4021: The k-means algorithm is used to cluster the semantic vectors of the items to be detected and the semantic vectors of each function in the item fingerprint function H3, and the elbow method is used to determine the number of clusters. S4022: If the code function of the project to be detected is in any cluster of the clustering results of each function in the project fingerprint function H3, then the project to be detected is identified as having code cloning.
7. A device for detecting code dependencies, characterized in that, A method for detecting code dependencies according to any one of claims 1-6, comprising: The preprocessing module is used to break down the code of each version of each project in the code repository into multiple functions and generate a call graph for each function; the call graphs of all functions corresponding to each version are then concatenated to obtain the function call graph for each version. The index building module is used to calculate the index dictionary D1 for each project based on the union of the functions corresponding to each version of each project; create a nested index dictionary H1 corresponding to the code repository-version-function; and obtain the index dictionary D2 of the code repository from the index dictionary D1 of each project. The project fingerprint determination module is used to compare and remove duplicate functions in nested index dictionary H1 and index dictionary D2, then perform complexity filtering on each function in the deduplicated nested index dictionary H1 to obtain a new nested index dictionary H2; perform centrality filtering on the function call graph of each version in nested index dictionary H1 to obtain the core function list FL2 of each version; and perform the intersection operation of the core function list FL2 and the function information list FL1 in the new nested index dictionary H2 to obtain the project fingerprint function H3. The dependency detection module is used to perform code clone detection on the code functions of the project to be detected and the project fingerprint function H3 to obtain the code dependency relationship of the project to be detected.
8. A code dependency detection system, comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Software replication rate measurement method
CN114428743A
Source code homology detection method and device, equipment and storage medium
CN115480823A