Code reconstruction method and system based on data stream slicing and large language model driving
Through the code reconstruction method based on data flow slicing and large language model, the problems of inaccurate functional identification and insufficient tool support in long method reconstruction are solved, and more efficient and accurate code reconstruction is achieved, suitable for code of different development styles.
Patent Information
- Application Number
- CN202411768863.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-04
- Publication Date
- 2025-04-01
- Estimated Expiration
- 2044-12-04
AI Technical Summary
The prior art cannot accurately identify functions in long-term reconstruction, and insufficient support for automation tools, resulting in low reconstruction accuracy and difficult to adapt to code reconstruction tasks of different development styles.
The code reconstruction method based on data flow slicing and large language model is adopted. By generating a program dependency graph, the control flow is removed and the data flow is retained. The final data flow graph is generated using the active variable analysis method, the logical block is divided, the prompt word is generated and the large language model is input for code reconstruction.
More accurately describes and divides independent logic blocks in the code, improves the accuracy and efficiency of long method reconstruction, is suitable for code reconstruction tasks of different development styles, and improves code quality and maintenance.
Smart Images

Figure CN119248347B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of software engineering, and in particular relates to a code reconstruction method and system based on data stream slicing and large language model driving. Background Art
[0002] In the field of software engineering, irregular behaviors in actual development often lead to the generation of code smells. Code smells refer to parts of the source code that contain potential problems. These problems are usually related to software quality, including long methods (methods that perform multiple functions or have a large number of lines of code), feature envy (methods use more data from another class than from their own class), state checks (methods frequently check the state of objects or variables), and God classes (a class that takes on too many responsibilities and performs multiple functions). The frequency of long methods is much higher than other smells. Code smells can have an adverse effect on the long-term maintainability, readability, and extensibility of the code. Therefore, removing code smells and improving code quality are crucial in the field of software engineering.
[0003] Refactoring is a technical activity in the software development process. It improves the internal structure of the code without changing the external behavior of the software, thereby improving the readability, maintainability and scalability of the code. The concept of refactoring was first proposed by Kent Beck in the late 1990s and was widely cited in Martin Fowler et al.'s book "Refactoring: Improving the Design of Existing Code". Refactoring has important value in the field of software engineering. First, refactoring allows the code to better follow the design principles, which helps to improve the cohesion of the code and reduce the coupling between modules. Secondly, software requirements often change, and good refactoring practices make it easier for the software to adapt to these changes, improving the flexibility and responsiveness of the software. Most importantly, through refactoring, the readability, maintainability and scalability of the code can be improved, making it easier for other developers to understand the intent and structure of the code, so as to maintain the code more efficiently.
[0004] Researchers at home and abroad have done a lot of valuable research on the refactoring of various code smells. The existing long method refactoring generally has the following problems: inability to accurately identify functions, insufficient automation and tool support, which affect the accuracy of the method.
[0005] Currently, there are also some methods that try to identify long methods and refactor the code. Some of these methods use full computation slices to try to identify and extract methods in long methods. However, they only consider the changes in variable values, but not the places where the variables are used, even if these places may be involved in the implementation of larger functions. Other methods use blank lines between codes to separate different functions. These methods usually rely on manually defined rules and are difficult to adapt to the different development styles of different developers. Therefore, they cannot be generalized to code refactoring tasks with different styles in different fields. Summary of the invention
[0006] The purpose of the present invention is to solve the problems existing in the prior art in the scenario of long method reconstruction, and to provide a code reconstruction method and system based on data stream slicing and large language model driving.
[0007] In order to achieve the above-mentioned invention object, the present invention specifically adopts the following technical solutions:
[0008] In a first aspect, the present invention provides a code reconstruction method based on data stream slicing and large language model driving, which comprises the following steps:
[0009] S1. Obtain the original code to be refactored and preprocess the original code;
[0010] S2. Process the preprocessed code to generate a corresponding program dependency graph, use a program slicing method to remove the control flow edges in the program dependency graph and retain only the data flow edges to obtain an original data flow graph, update the active variable set before each statement and the active variable set after each statement in the original data flow graph according to the active variable analysis method, obtain the final data flow graph from the two updated active variable sets, generate a logic block set from the final data flow graph, and the final data flow graph and the logic block set constitute data flow information;
[0011] S3. Generate prompt words of the large language model according to the data flow information, user needs and local historical behavior library, input the preprocessed code and the generated prompt words into the large language model, and use the thinking chain to analyze and think about the data flow information to obtain the reconstructed code;
[0012] S4. Perform functional tests on the preprocessed code and the reconstructed code. If the functions of the two are inconsistent, adjust the prompt words and reconstruct the code to obtain a new reconstructed code until the functions of the preprocessed code and the reconstructed code are consistent;
[0013] S5. Review the refactored code and use the long method detection tool to check whether the refactored code still has long methods. If there are still long methods, refactor again until the refactored code has no long methods. If there are no long methods in the refactored code, check the dependencies between the refactored codes. If the number of serially executed code lines in the refactored code is less than the merge threshold, merge the serially executed code lines less than the merge threshold and then format them for output.
[0014] S6. For the reviewed code, after receiving feedback from the user that the code refactoring result is approved, the code refactoring task information is stored as a piece of data in the local historical behavior library for reference when the code refactoring task is performed next time; wherein the code refactoring task information includes the original code currently being refactored, data flow information, prompt words used in the current code refactoring task, and the code refactoring result.
[0015] Preferably, in step S1, the preprocessing specifically includes: removing redundant codes and comments in the original code, formatting the original code, and renaming variables and functions in the original code according to the camel case naming rule.
[0016] Preferably, in step S2, the preprocessed code is processed by a code analysis tool to generate a program dependency graph, wherein a node in the program dependency graph is a statement, and information in each node in the program dependency graph is a code fragment constituting a statement.
[0017] Preferably, in step S2, the specific process of updating the two active variable sets according to the active variable analysis method is as follows:
[0018] S21. Initialize the active variable set before each statement and the active variable set after each statement in the original data flow graph to an empty set;
[0019] S22. In the current iteration round, the active variable set before each statement in the current iteration round is calculated from the active variable set after each statement in the previous iteration round, and the active variable set after each statement in the current iteration round is calculated from the active variable set before all subsequent statements corresponding to each statement; wherein, the statement in the current iteration round The current set of active variables and statements The set of active variables after The calculation methods are:
[0020]
[0021]
[0022] In the formula, Representation Statement Variables defined in; It means taking the union; Representation Statement All the code contained in ; Representation Statement The set of all variables that appear in all the code; Indicates the subsequent statement The set of active variables before, Indicated by the statement The set of successor statements consisting of all the successor statements of ;
[0023] S23. Continuously iterate and update until the active variable set before each statement and the active variable set after each statement remain unchanged, thereby obtaining an updated active variable set before each statement and an updated active variable set after each statement.
[0024] Preferably, in step S2, the specific process of generating a logic block set from the final data flow graph is as follows: start reverse search from the last node in the final data flow graph, if a subgraph consisting of a branch in a branch structure has no direct connection relationship with the subgraphs consisting of other branches of the branch structure and the number of nodes in the subgraph consisting of the branch is greater than the set node threshold, it is considered that there is an independent logic block in the subgraph consisting of the branch, until all nodes in the final data flow graph are traversed, and all generated logic blocks constitute a logic block set.
[0025] Preferably, in step S3, the specific process of generating prompt words is as follows: matching the data flow information with the data in the pre-built local historical behavior library by graph similarity, first generating the corresponding control flow graph from the program dependency graph, if the control flow graph is the same as the control flow graph structure corresponding to the data in the local historical behavior library, then using the data as a reference and generating prompt words in combination with user needs, otherwise generating prompt words according to a preset prompt word template and user needs; wherein the prompt words are composed of a code refactoring task description, a refactoring sample, preprocessed code, and data flow information, the refactoring sample is composed of the sample code to be refactored, the data flow information of the sample code, and the refactoring result of the sample code, and the data flow information of the sample code is composed of its corresponding data flow graph and a set of logic blocks.
[0026] Preferably, in step S4, test data is generated by a test data generation tool according to the preprocessed code, the test data is tested by the preprocessed code, and the test data is tested by the reconstructed code. If the results obtained by the two are consistent, the function of the reconstructed code is deemed to be unchanged.
[0027] In a second aspect, the present invention provides a code reconstruction system based on data stream slicing and large language model driving, which includes:
[0028] A preprocessing module is used to obtain the original code to be refactored and preprocess the original code;
[0029] A data flow analysis module is used to process the preprocessed code and generate a corresponding program dependency graph. The program slicing method is used to remove the control flow edges in the program dependency graph and only retain the data flow edges to obtain the original data flow graph. The active variable set before each statement and the active variable set after each statement in the original data flow graph are updated according to the active variable analysis method. The final data flow graph is obtained from the two updated active variable sets. A logic block set is generated from the final data flow graph, and the final data flow graph and the logic block set constitute the data flow information.
[0030] A code reconstruction module is used to generate prompt words of a large language model according to the data flow information, user needs and a local historical behavior library, and input the preprocessed code and the generated prompt words into the large language model, and use a thinking chain to analyze and think about the data flow information to obtain the reconstructed code;
[0031] Functional testing module, used to perform functional testing on the preprocessed code and the reconstructed code. If the functions of the two are inconsistent, the prompt words are adjusted and the code is reconstructed again to obtain a new reconstructed code until the functions of the preprocessed code and the reconstructed code are consistent;
[0032] The code review module is used to review the refactored code and use the long method detection tool to detect whether the refactored code still has long methods. If there are still long methods, refactoring is performed again until there are no long methods in the refactored code. If there are no long methods in the refactored code, the dependencies between the refactored codes are checked. If the number of serially executed code lines in the refactored code is less than the merge threshold, the serially executed code that is less than the merge threshold is merged and then formatted for output.
[0033] The post-processing module is used to store the code refactoring task information as a piece of data in the local historical behavior library for the next reference when performing the code refactoring task. The code refactoring task information includes the original code currently being refactored, data flow information, prompt words used in the current code refactoring task, and code refactoring results.
[0034] In a third aspect, the present invention provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the code reconstruction method based on data stream slicing and large language model driving as described in any of the schemes of the first aspect above is implemented.
[0035] In a fourth aspect, the present invention provides a computer electronic device comprising a memory and a processor;
[0036] The memory is used to store computer programs;
[0037] The processor is used to implement the code reconstruction method based on data stream slicing and large language model driving as described in any solution of the first aspect when executing the computer program.
[0038] Compared with the prior art, the present invention has the following beneficial effects:
[0039] The present invention combines data flow graphs to more accurately describe and divide independent logic block parts in the code, which can effectively alleviate the situation where long methods cannot be accurately identified in refactoring scenarios, and provides a feasible solution for accurately refactoring code.
[0040] The present invention utilizes the understanding ability of a large language model to assist developers in code splitting. The present invention designs a reasonable code domain-specific prompt word template, which enables the large language model to understand the splitting task requirements more accurately, improves the quality of reconstructed code, provides reliable support for research and application in the field of code reconstructing, and can improve the efficiency of long method code reconstructing in the field of software engineering.
[0041] In general, the present invention utilizes the data flow analysis results to enhance the ability of large language models to automatically reconstruct code, and comprehensively considers previous reconstructing methods. The combination of the two can better reconstruct long methods. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] Figure 1 is a flow chart of the method of the present invention;
[0043] Figure 2 is a flow chart of the steps of the method of the present invention;
[0044] Figure 3 This is a flow chart of the large language model proxy technology of the present invention;
[0045] Figure 4 This is a schematic diagram of the data flow diagram obtained in this embodiment;
[0046] Figure 5 It is a system block diagram of the present invention. DETAILED DESCRIPTION
[0047] In order to make the above-mentioned purpose, features and advantages of the present invention more obvious and easy to understand, the specific implementation mode of the present invention is described in detail below in conjunction with the accompanying drawings. In the following description, many specific details are set forth to facilitate a full understanding of the present invention. However, the present invention can be implemented in many other ways different from those described herein, and those skilled in the art can make similar improvements without violating the connotation of the present invention. Therefore, the present invention is not limited to the specific embodiments disclosed below. The technical features in each embodiment of the present invention can be combined accordingly without conflicting with each other.
[0048] In the description of the present invention, it should be understood that the terms "first" and "second" are only used for the purpose of distinguishing descriptions, and should not be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features.
[0049] In the present invention, if Figure 1 As shown, the code refactoring method based on data flow slicing and large language model driving first obtains the code that needs to be refactored, and then performs six parts in sequence: code preprocessing, data flow analysis, prompt word generation, code refactoring, code testing, and code review. In the process of prompt word generation, user needs need to be considered; in the process of code review, if there are long methods, it is necessary to return to the data flow analysis process and re-perform code refactoring, code testing, and code review.
[0050] like Figure 2 As shown, in a preferred implementation of the present invention, the above-mentioned code reconstruction method based on data stream slicing and large language model driving includes the following steps S1 to S6. The specific implementation process is described in detail below.
[0051] S1. Obtain the original code to be refactored and preprocess the original code.
[0052] It should be noted that in step S1 of the present invention, the original code is preprocessed, specifically: redundant codes and comments in the original code are removed, the original code is formatted, and the variables and functions in the original code are renamed according to the camel case naming rule, to ensure that highly consistent code snippets are used when the code is refactored and input into the large language model.
[0053] In this embodiment, preprocessing the original code specifically includes the following process:
[0054] (1.1) When writing code, the coder will add some blank lines between the sub-logic blocks of the function to indicate the independence of the two parts of the code. These blank lines help developers understand the code functions, but for large language models, too many blank lines will also increase the workload of large language models in parsing the code, so it is necessary to check the blank lines between the code and delete all of them.
[0055] (1.2) During the development process, developers sometimes adjust the code, and the code before the adjustment may not be deleted, but only commented for possible reference and reuse in the subsequent development process. In the following text, these codes with comments and before the adjustment are called commented codes. These redundant commented codes will make all the codes bloated, and even cause the large language model to misunderstand the code function, resulting in refactoring failure, so these commented codes need to be identified and deleted.
[0056] (1.3) During the development process, due to the differences between programming languages and developers' personal habits, variables and functions are mostly named using snake case or camel case. However, for large language models, the coexistence of the two naming rules will undoubtedly increase the workload of judging and understanding the code. For this reason, it is necessary to rename the variables according to a unified rule. The present invention adopts the camel case naming rule and uses regular expressions to rename all function names and variable names in the code.
[0057] In this embodiment, if the preprocessed code Total Line of code, it can be expressed as:
[0058]
[0059] In the formula, Indicates Line of code.
[0060] S2. Process the preprocessed code to generate the corresponding program dependency graph, use the program slicing method to remove the control flow edges in the program dependency graph and only retain the data flow edges to obtain the original data flow graph, and update the active variable set before each statement and the active variable set after each statement in the original data flow graph according to the active variable analysis method, and obtain the final data flow graph from the two updated active variable sets, generate a logic block set from the final data flow graph, and the final data flow graph and the logic block set constitute the data flow information.
[0061] It should be noted that in step S2 of the present invention, a code analysis tool (such as TinyPDG) processes the preprocessed code to generate a program dependency graph. A node in the program dependency graph is a statement, and the information in each node in the program dependency graph is a code fragment that constitutes a statement.
[0062] In this embodiment, a statement refers to a complete instruction executed in a program, which is usually defined by the grammatical rules of the language and performs a specific operation.
[0063] It should be noted that in step S2 of the present invention, the control flow edge between statements can be determined based on the conditional jump statements and function calls in the code. Among them, the control flow edge refers to the dependency between control structures in the program. It describes the execution path of the program. Different from the control flow edge, the data flow edge in step S2 refers to the dependency between variables and data, which describes how data flows and is used in the program.
[0064] It should be noted that, in step S2 of the present invention, the variables in the original data flow graph are analyzed to determine the definition position and usage position of the variables in the program, and the life cycle of the variables is analyzed. Specifically, the active variable analysis method is used to update the active variable set before each statement in the original data flow graph and the active variable set after each statement. The active variable set before the statement refers to the variable value that may be used in the current statement when entering the statement, and the active variable set after the statement refers to the variable value that may be used in the subsequent statement when leaving the statement.
[0065] It should be noted that, in step S2 of the present invention, the specific process of updating the two active variable sets according to the active variable analysis method is as follows:
[0066] S21. Initialize the active variable set before each statement and the active variable set after each statement in the original data flow graph to empty sets.
[0067] S22. In the current iteration round, the active variable set before each statement in the current iteration round is calculated from the active variable set after each statement in the previous iteration round, and the active variable set after each statement in the current iteration round is calculated from the active variable set before all subsequent statements corresponding to each statement. The current set of active variables and statements The set of active variables after The calculation methods are:
[0068]
[0069]
[0070] In the formula, Representation Statement Variables defined in; It means taking the union; Representation Statement All the code contained in ; Representation Statement The set of all variables that appear in all the code; Indicates the subsequent statement The set of active variables before, Indicated by the statement The set of successor statements consists of all the successor statements of .
[0071] S23. Continuously iterate and update until the active variable set before each statement and the active variable set after each statement remain unchanged, thereby obtaining an updated active variable set before each statement and an updated active variable set after each statement.
[0072] In this embodiment, the active variable set before a statement refers to the variable values that may be used in the current statement when entering the statement, and the active variable set after a statement refers to the variable values that may be used in subsequent statements when leaving the statement.
[0073] It should be noted that in step S2 of the present invention, the purpose of active variable analysis is to obtain the final data flow diagram. The specific implementation method of this process belongs to the prior art and will not be described in detail in the present invention.
[0074] It should be noted that in step S2 of the present invention, the specific process of generating a logic block set from the final data flow graph is as follows: Start searching backwards from the last node in a branch structure. If a branch in a branch structure consists of a subgraph The subgraph formed by other branches of the branch structure There is no direct connection relationship and the subgraph formed by this branch The number of nodes is greater than the set node threshold , then the subgraph formed by this branch is considered There are independent logic blocks in the data flow until all nodes in the final data flow graph are traversed, and all generated logic blocks constitute a logic block set. .in, A set of nodes representing the final data flow graph, Respectively represent statements, each of which consists of a complete instruction executed in the program, usually defined by the grammatical rules of the language, and performs a specific operation; The data flow connection edge set representing the final data flow graph, Respectively Data flow connection edge, Data flow connection edge By Statements , No. Statements And the data transmitted by the data stream connection constitute, ; Represent subgraphs The node set and data flow connection edge set; Represent subgraphs The node set and data flow connection edge set; Indicates the generated logical blocks, For logical blocks The variables used in Represents logical blocks The node set and data flow connection edge set.
[0075] S3. Generate prompt words for the large language model according to the data flow information, user needs and local historical behavior library, input the preprocessed code and the generated prompt words into the large language model, and use the thinking chain to analyze and think about the data flow information to obtain the reconstructed code.
[0076] In step S3 of the present invention, according to the data flow information , user needs, and the local historical behavior library, and refine the requirements of the reconstruction task into prompt words that the large language model can better understand. The specific process of generating prompt words is as follows: the data flow information is matched with the data in the pre-built local historical behavior library for graph similarity, and the corresponding control flow graph is first generated from the program dependency graph , the control flow graph is matched with the control flow graph corresponding to all the data in the local historical behavior library. If the control flow graph is If the corresponding control flow graph structure is the same, the data As a reference, generate prompt words in combination with user needs, otherwise generate prompt words according to the preset prompt word template and user needs. Refactoring task description from code , reconstructed sample, preprocessed code and data flow information, reconstructed sample Sample code that needs to be refactored , data flow information of the sample code And the refactoring results of the sample code constitute, The data flow diagram in the data flow information representing the sample code, A collection of logical blocks in the data flow information of the sample code. The code refactoring task description tells the large language model the task that requires code refactoring.
[0077] In this embodiment, the control flow graph is a graphical representation in a program, describing the control flow relationship between code segments in the program. Each node represents a segment of sequentially executed code without interruption, and the edges represent possible paths of the control flow, such as sequential execution, conditional jumps, and loops.
[0078] In this embodiment, graph similarity matching refers to calculating the graph similarity between the control flow graph obtained from the current code to be reconstructed and the control flow graph stored in the local historical behavior library through a traditional method (such as an average hash algorithm) to obtain historical data with a structure similar to the control flow graph obtained from the current code to be reconstructed. Then, a graph embedding method based on deep learning (such as a graph convolutional neural network) is used to calculate the similarity between the historical data obtained by the traditional method and the control flow graph obtained from the current code to be reconstructed, and historical data with the same structure is obtained.
[0079] In step S3 of the present invention, the large language model reconstructs the input preprocessed code according to the prompt word. Here, the large language model performs the code reconstruction task using the code reconstruction task description in the prompt word and a set of prompt examples through context learning, and uses the Chain-of-Thought (CoT) to gradually decompose the input complex problem into sub-problems and solve them in sequence, so as to improve the performance of the large language model and generate a series of reasoning thought processes. And the refactored code :
[0080]
[0081] in, This is done by the large language model The mapping function applied to convert the preprocessed code Generalization to code refactoring tasks ; Here Indicates the task being solved, which is a code refactoring task in this embodiment; They are respectively the analysis and thinking of the thinking chain on the data flow information and the analysis and thinking on the specific functions of the parts that need to be reconstructed.
[0082] In this embodiment, the specific format of the generated prompt words is as follows: "You are a programming assistant who helps users solve problems related to their workspace code. Your main responsibility is to provide code refactoring solutions based on the analyzed data flow information. Please refer to the above responsibilities and the given examples, and provide detailed reasoning and analysis. Then the refactored code is given in the last line. Suppose we have a large method , and the data flow analysis results are: Therefore, this method can be refactored, and the refactored code is Now I need to refactor the code , and the data flow analysis results are: , please give the result of the refactored code. "
[0083] S4. Perform functional tests on the preprocessed code and the reconstructed code. If the functions of the two are inconsistent, adjust the prompt words and reconstruct the code to obtain new reconstructed code until the functions of the preprocessed code and the reconstructed code are consistent.
[0084] In the present invention, test data covering the main functions and boundary conditions of the code is generated by a test data generation tool (such as SonarQube) based on the code before reconstruction (i.e., the pre-processed code), the test data is tested by the code before reconstruction, and the test data is tested by the reconstructed code. If the results obtained by the two are consistent, it is deemed that the function of the reconstructed code remains unchanged.
[0085] In this embodiment, according to the code before reconstruction input by the user, the parameters of the test data and the expected results are specified, the branch points in the code are analyzed, and some test data generation tools based on machine learning, natural language processing or other related technologies are used to generate standardized, targeted and comprehensive test data. By analyzing the branch structure in the code before reconstruction, the test data ensures that all branch situations can be tested, so as to ensure that the code before and after reconstruction can be fully tested.
[0086] In addition, specific test execution work needs to be performed on the generated test data. Configure the appropriate test environment to ensure that the test data can run normally; load the test data, generate a test plan and execute the test data according to the plan; collect and record the execution results, time, errors and other information of each test data for subsequent analysis, statistics and feedback.
[0087] S5. Review the refactored code and use the long method detection tool to check whether the refactored code still has long methods: if there are still long methods, re-execute the refactoring until there are no long methods in the refactored code; if there are no long methods in the refactored code, check the dependencies between the refactored codes. If the number of lines of code executed serially in the refactored code is less than the merge threshold, , then the serially executed codes that are less than the merge threshold will be merged and then formatted for output.
[0088] In the present invention, a long method refers to a method that performs multiple functions and has a large number of code lines.
[0089] S6. For the reviewed code, after receiving feedback from the user that the code refactoring result is approved, the code refactoring task information is stored as a piece of data in the local historical behavior library for reference when the code refactoring task is performed next time; wherein the code refactoring task information includes the original code currently being refactored, data flow information, prompt words used in the current code refactoring task, and the code refactoring result.
[0090] In the present invention, if the user approves the code refactoring result, the refactoring task information will be Store it in the local historical behavior library to realize dynamic update of the historical behavior library, so that it can refer to the previous historical behavior organization prompt words and perform code refactoring in subsequent refactoring tasks.
[0091] Furthermore, the above S1~S6 are encapsulated using the large language model proxy technology to form an automated tool flow. Code preprocessing (step S1), data flow analysis (step S2) and test data generation tool (step S5) belong to the tool part; prompt word generation belongs to the planning part (step S3); code refactoring (step S3) and functional testing (step S4) belong to the agent part. Figure 3 As shown, the code to be reconstructed is input, and the code to be reconstructed is first preprocessed by a code preprocessing tool (such as CoolFormat) to obtain the preprocessed code; based on the code to be reconstructed, a data flow generation tool (such as TinyPDG) is used to generate a data flow graph, and a data flow analysis tool (such as step S2 of the present invention) is used to divide it into independent logic blocks; based on the preprocessed code, the generated data flow graph and the logic block, a prompt word generation tool (such as step S3 of the present invention) refers to the historical reconstruction sample to output prompt words, and the large language model generates the reconstructed code and thought chain based on the prompt words; based on the preprocessed code, a test sample generation tool (such as SonarQube) outputs a test sample; a test execution tool (such as JUnit) performs a test based on the test sample and the reconstructed code until the test passes, and a long method detection tool (such as Sonarlint) detects the code that has passed the test to determine whether there is a long method therein, and if so, regenerates prompt words to execute code reconstructing, and if there is no long method, performs simple method merging, and finally uses the reconstructed result as a historical reconstructed sample for reference in subsequent reconstructing.
[0092] In order to better demonstrate the specific implementation and technical effects of the present invention, a code reconstruction method based on data stream slicing and large language model driving shown in steps S1 to S6 in the above preferred implementation method is applied to a specific example.
[0093] Example
[0094] In order to demonstrate the actual effect of the code reconstruction method of the present invention, a real code reconstruction task is used here to verify the effectiveness of each step proposed in the present invention. The specific implementation process of the code reconstruction method based on data stream slicing and large language model driving adopted in this embodiment is as described above and will not be repeated here.
[0095] The code to be refactored is shown in Table 1. This code segment calculates the input parameters a and b independently, divides the calculated floating-point results a3 and b3 to get t, and returns the absolute value of t. Through data flow information analysis, this code segment contains two independent sub-functions. Sub-function 1 is: based on the input parameter a, three floating-point values are calculated, mathematical calculations related to a are processed independently, and the results are stored in a3; sub-function 2 is: based on the input parameter b, three floating-point values are calculated, mathematical calculations related to b are processed independently, and the results are stored in b3. Therefore, two independent small methods for obtaining variable a3 and obtaining variable b3 can be separated from this code segment, so that the function of each small method is more single and the code readability is better.
[0096] Table 1. Code examples to be refactored
[0097]
[0098] In this embodiment, a code analysis tool is first used to process the above-mentioned code to be refactored to generate a corresponding program dependency graph. The code snippet of each statement in the program dependency graph is shown in Table 2.
[0099] Table 2. Code snippet for each statement in the program dependency graph
[0100]
[0101] Then, the program slicing method is used to remove the control flow edges in the program dependency graph, leaving only the data flow edges, and the active variable analysis method is used to obtain a complete data flow graph, such as Figure 4 The data flow edge set corresponding to the data flow graph is shown in Table 3. Each data flow edge consists of two different statements and the data transmitted by a data flow edge. One statement is used to represent the starting node, and the other statement is used to represent the ending node.
[0102] Table 3. Detailed information of data flow connection edge set
[0103]
[0104] Then, starting from the last node of the data flow graph, reverse search is performed. Independent subgraphs in the data flow graph are divided, and these subgraphs are used as independent logic blocks that can be split into independent functions, representing parts that can be split into independent functions. The independent logic blocks corresponding to the division of this embodiment are shown in Table 4, and each logic block contains the nodes, data flow connection edges, and variable information belonging to the logic block.
[0105] Table 4. Collection of independent logic blocks
[0106]
[0107] Combined with the above code to be reconstructed and the data flow analysis results of Table 3 and Table 4, a complete prompt statement is generated and input into the large language model. The large language model generates the reconstructed code according to the input content. The code reconstruction results directly using the large language model and the code reconstruction results of the method of the present invention are shown in Table 5.
[0108] Table 5. Reconstruction results of different methods
[0109]
[0110] From the above results, it can be seen that without the prompt word, the large language model will only do some simple code simplification and will not split the long method into multiple small methods; with the prompt word, the large language model can correctly split the independent functional blocks and give the thinking process. It can be seen that the method proposed by the present invention is more effective in the scenario of code refactoring.
[0111] It should also be noted that the code reconstruction method based on data stream slicing and driven by a large language model in the above embodiment can essentially be executed by a computer program or module. Therefore, similarly, based on the same inventive concept, another preferred embodiment of the present invention also provides a code reconstruction system based on data stream slicing and driven by a large language model corresponding to the code reconstruction method based on data stream slicing and driven by a large language model provided in the above embodiment, such as Figure 5 As shown, it includes:
[0112] A preprocessing module is used to obtain the original code to be refactored and preprocess the original code;
[0113] A data flow analysis module is used to process the preprocessed code and generate a corresponding program dependency graph. The program slicing method is used to remove the control flow edges in the program dependency graph and only retain the data flow edges to obtain the original data flow graph. The active variable set before each statement and the active variable set after each statement in the original data flow graph are updated according to the active variable analysis method. The final data flow graph is obtained from the two updated active variable sets. A logic block set is generated from the final data flow graph, and the final data flow graph and the logic block set constitute the data flow information.
[0114] A code reconstruction module is used to generate prompt words of a large language model according to the data flow information, user needs and a local historical behavior library, and input the preprocessed code and the generated prompt words into the large language model, and use a thinking chain to analyze and think about the data flow information to obtain the reconstructed code;
[0115] Functional testing module, used to perform functional testing on the preprocessed code and the reconstructed code. If the functions of the two are inconsistent, the prompt words are adjusted and the code is reconstructed again to obtain a new reconstructed code until the functions of the preprocessed code and the reconstructed code are consistent;
[0116] The code review module is used to review the refactored code and use the long method detection tool to detect whether the refactored code still has long methods. If there are still long methods, refactoring is performed again until there are no long methods in the refactored code. If there are no long methods in the refactored code, the dependencies between the refactored codes are checked. If the number of serially executed code lines in the refactored code is less than the merge threshold, the serially executed code that is less than the merge threshold is merged and then formatted for output.
[0117] The post-processing module is used to store the code refactoring task information as a piece of data in the local historical behavior library for the next reference when performing the code refactoring task. The code refactoring task information includes the original code currently being refactored, data flow information, prompt words used in the current code refactoring task, and code refactoring results.
[0118] Similarly, based on the same inventive concept, another preferred embodiment of the present invention further provides a computer electronic device corresponding to the code reconstruction method based on data stream slicing and large language model driving provided in the above embodiment, which includes a memory and a processor;
[0119] The memory is used to store computer programs;
[0120] The processor is used to implement the code reconstruction method based on data stream slicing and large language model driving in the above embodiment when executing the computer program.
[0121] In addition, the logic instructions in the above-mentioned memory can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention.
[0122] Therefore, based on the same inventive concept, another preferred embodiment of the present invention also provides a computer-readable storage medium corresponding to the code reconstruction method based on data stream slicing and large language model driving provided in the above embodiment, and a computer program is stored on the storage medium. When the computer program is executed by the processor, it can implement the code reconstruction method based on data stream slicing and large language model driving in the above embodiment.
[0123] It is understandable that the above storage medium may include random access memory (RAM) or non-volatile memory (NVM), such as at least one disk storage. The storage medium may also be a U disk, a mobile hard disk, a magnetic disk or an optical disk, etc., which can store program codes.
[0124] It is understandable that the above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0125] It should also be noted that those skilled in the art can clearly understand that for the convenience and simplicity of description, the specific working process of the system described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here. In the various embodiments provided in this application, the division of steps or modules in the system and method is only a logical function division, and there may be other division methods in actual implementation, such as multiple modules or steps can be combined or integrated together, and a module or step can also be split.
[0126] The above-described embodiment is only a preferred solution of the present invention, but it is not intended to limit the present invention. A person skilled in the relevant technical field may make various changes and modifications without departing from the spirit and scope of the present invention. Therefore, any technical solution obtained by equivalent replacement or equivalent transformation falls within the protection scope of the present invention.
Claims
1. A code reconstruction method based on data stream slicing and large language model driving, characterized in that: The following steps are involved: S1. Obtain the original code to be refactored and preprocess the original code; S2. Process the preprocessed code to generate a corresponding program dependency graph, use a program slicing method to remove the control flow edges in the program dependency graph and retain only the data flow edges to obtain an original data flow graph, update the active variable set before each statement and the active variable set after each statement in the original data flow graph according to the active variable analysis method, obtain the final data flow graph from the two updated active variable sets, generate a logic block set from the final data flow graph, and the final data flow graph and the logic block set constitute data flow information; S3. Generate prompt words of the large language model according to the data flow information, user needs and local historical behavior library, input the preprocessed code and the generated prompt words into the large language model, and use the thinking chain to analyze and think about the data flow information to obtain the reconstructed code; S4. Perform functional tests on the preprocessed code and the reconstructed code. If the functions of the two are inconsistent, adjust the prompt words and reconstruct the code to obtain a new reconstructed code until the functions of the preprocessed code and the reconstructed code are consistent; S5. Review the refactored code and use the long method detection tool to check whether the refactored code still has long methods. If there are still long methods, refactor again until the refactored code has no long methods. If there are no long methods in the refactored code, check the dependencies between the refactored codes. If the number of serially executed code lines in the refactored code is less than the merge threshold, merge the serially executed code lines less than the merge threshold and then format them for output. S6. For the reviewed code, after receiving feedback from the user that the code refactoring result is approved, the code refactoring task information is stored as a piece of data in the local historical behavior library for reference when the code refactoring task is performed next time; wherein the code refactoring task information includes the original code currently being refactored, data flow information, prompt words used in the current code refactoring task, and the code refactoring result.
2. The code reconstruction method based on data stream slicing and large language model driving according to claim 1 is characterized in that: In step S1, the preprocessing specifically includes: removing redundant codes and comments in the original code, formatting the original code, and renaming the variables and functions in the original code according to the camel case naming rule.
3. The code reconstruction method based on data stream slicing and large language model driving according to claim 1 is characterized in that: In step S2, the code analysis tool processes the preprocessed code to generate a program dependency graph, where a node in the program dependency graph is a statement, and the information in each node in the program dependency graph is a code fragment that constitutes a statement.
4. The code reconstruction method based on data stream slicing and large language model driving according to claim 1 is characterized in that: In step S2, the specific process of updating the two active variable sets according to the active variable analysis method is as follows: S21. Initialize the active variable set before each statement and the active variable set after each statement in the original data flow graph to an empty set; S22. In the current iteration round, the active variable set before each statement in the current iteration round is calculated from the active variable set after each statement in the previous iteration round, and the active variable set after each statement in the current iteration round is calculated from the active variable set before all subsequent statements corresponding to each statement; wherein, the statement in the current iteration round The current set of active variables and statements The set of active variables after The calculation methods are: In the formula, Representation Statement Variables defined in; It means taking the union; Representation Statement All the code contained in ; Representation Statement The set of all variables that appear in all the code; Indicates the subsequent statement The set of active variables before, Indicated by the statement The set of successor statements consisting of all the successor statements of ; S23. Continuously iterate and update until the active variable set before each statement and the active variable set after each statement remain unchanged, thereby obtaining an updated active variable set before each statement and an updated active variable set after each statement.
5. The code reconstruction method based on data stream slicing and large language model driving according to claim 1, characterized in that: In step S2, the specific process of generating a logic block set from the final data flow graph is as follows: start reverse search from the last node in the final data flow graph. If a subgraph consisting of a branch in a branch structure has no direct connection relationship with the subgraphs consisting of other branches of the branch structure and the number of nodes in the subgraph consisting of the branch is greater than the set node threshold, it is considered that there is an independent logic block in the subgraph consisting of the branch, until all nodes in the final data flow graph are traversed, and all generated logic blocks constitute a logic block set.
6. The code reconstruction method based on data stream slicing and large language model driving according to claim 1, characterized in that: In step S3, the specific process of generating prompt words is as follows: matching the data flow information with the data in the pre-built local historical behavior library by graph similarity, first generating the corresponding control flow graph from the program dependency graph, if the control flow graph is the same as the control flow graph structure corresponding to the data in the local historical behavior library, then using the data as a reference, and generating prompt words in combination with user needs, otherwise generating prompt words according to a preset prompt word template and user needs; wherein the prompt words are composed of a code reconstruction task description, a reconstruction sample, preprocessed code, and data flow information, the reconstruction sample is composed of the sample code to be reconstructed, the data flow information of the sample code, and the reconstruction result of the sample code, and the data flow information of the sample code is composed of its corresponding data flow graph and a set of logic blocks.
7. The code reconstruction method based on data stream slicing and large language model driving according to claim 1, characterized in that: In step S4, test data is generated by a test data generation tool according to the preprocessed code, the test data is tested by the preprocessed code, and the test data is tested by the reconstructed code. If the results obtained by the two are consistent, the function of the reconstructed code is deemed to be unchanged.
8. A code reconstruction system based on data stream slicing and large language model driving, characterized in that: include: A preprocessing module is used to obtain the original code to be refactored and preprocess the original code; A data flow analysis module is used to process the preprocessed code and generate a corresponding program dependency graph. The program slicing method is used to remove the control flow edges in the program dependency graph and only retain the data flow edges to obtain the original data flow graph. The active variable set before each statement and the active variable set after each statement in the original data flow graph are updated according to the active variable analysis method. The final data flow graph is obtained from the two updated active variable sets. A logic block set is generated from the final data flow graph, and the final data flow graph and the logic block set constitute the data flow information. A code reconstruction module is used to generate prompt words of a large language model according to the data flow information, user needs and a local historical behavior library, and input the preprocessed code and the generated prompt words into the large language model, and use a thinking chain to analyze and think about the data flow information to obtain the reconstructed code; Functional testing module, used to perform functional testing on the preprocessed code and the reconstructed code. If the functions of the two are inconsistent, the prompt words are adjusted and the code is reconstructed again to obtain a new reconstructed code until the functions of the preprocessed code and the reconstructed code are consistent; The code review module is used to review the refactored code and use the long method detection tool to detect whether the refactored code still has long methods. If there are still long methods, refactoring is performed again until there are no long methods in the refactored code. If there are no long methods in the refactored code, the dependencies between the refactored codes are checked. If the number of serially executed code lines in the refactored code is less than the merge threshold, the serially executed code that is less than the merge threshold is merged and then formatted for output. The post-processing module is used to store the code refactoring task information as a piece of data in the local historical behavior library for the next reference when performing the code refactoring task. The code refactoring task information includes the original code currently being refactored, data flow information, prompt words used in the current code refactoring task, and code refactoring results.
9. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, and when the computer program is executed by the processor, the code reconstruction method based on data stream slicing and large language model driving as described in any one of claims 1 to 7 is implemented.
10. A computer electronic device, characterized in that: including memory and processor; The memory is used to store computer programs; The processor is used to implement the code reconstruction method based on data stream slicing and large language model driving as described in any one of claims 1 to 7 when executing the computer program.
Citation Information
Patent Citations
Method and system for automatically generating reusable API based on code snippets
CN117892031A
Locating security vulnerabilities in source code
US20100083240A1