Code processing method and device, computer equipment, storage medium and program product
Through static analysis and automatic repair methods based on defect detection models, the problem of high labor costs caused by the increased difficulty of code maintenance is solved, efficient automatic repair of codes is achieved, and developers' time and energy consumption are reduced.
Patent Information
- Application Number
- CN202410583939.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-11
- Publication Date
- 2024-08-23
AI Technical Summary
As mobile application functions become richer and code volume increases, code maintenance becomes more difficult, resulting in high labor costs. It is difficult for existing technologies to effectively reduce developers' time and energy consumption.
Provide a code processing method that obtains the input target code, performs static analysis based on the defect detection model, automatically detects code defects and outputs repair suggestions, and performs automatic repair after the user selects the repair suggestions, using the preset defect repair suggestions and Content feature similarity mapping relationship for matching.
Without consuming developers' time and energy, code defects are automatically detected and repaired, labor costs are reduced, and code repair accuracy and efficiency are improved.
Smart Images

Figure CN118535361A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a code processing method, apparatus, computer equipment, storage medium and computer program product. Background Art
[0002] As the functions of mobile applications become more and more abundant and the size of projects becomes larger, the difficulty of code maintenance increases day by day. Usually, developers with certain coding experience are required to analyze and process the codes one by one.
[0003] However, the above method consumes a lot of time and energy, resulting in high labor costs. Summary of the invention
[0004] Based on this, it is necessary to provide a code processing method, device, computer equipment, computer-readable storage medium and computer program product that can reduce labor costs in response to the above technical problems.
[0005] In a first aspect, the present application provides a code processing method. The method comprises:
[0006] Get the input target code;
[0007] Performing static analysis on the target code based on a defect detection model to obtain a static analysis result of the target code; the defect detection model is trained based on normal code fragments and defective code fragments;
[0008] When the static analysis result indicates that the target code has a defect, outputting a defect repair suggestion for the target code;
[0009] In a case where a selected operation for the defect repair suggestion is detected, the target code is automatically repaired based on the defect repair suggestion.
[0010] In some embodiments, when the static analysis result indicates that the target code has a defect, outputting a defect repair suggestion for the target code includes:
[0011] In the case where the static analysis result indicates that the target code has a defect, determining the defect type of the target code;
[0012] Based on the mapping relationship between the defect type and the defect repair suggestion, a defect repair suggestion matching the defect type of the target code is determined, and the defect repair suggestion is output.
[0013] In some embodiments, when the static analysis result indicates that the target code has a defect, outputting a defect repair suggestion for the target code includes:
[0014] When the static analysis result indicates that the target code has a defect, obtaining a first content feature of the target code and a second content feature of each preset defect code;
[0015] Based on the similarity between the first content feature and each of the second content features, and the mapping relationship between the preset defect code and the defect repair suggestion, the target code is analyzed, the defect repair suggestion matching the target code is determined, and the defect repair suggestion is output.
[0016] In some embodiments, the mapping relationship is obtained by:
[0017] Determine the availability status of current network services;
[0018] When the current network service is available, obtaining the mapping relationship from the cloud;
[0019] When the current network service is unavailable, the mapping relationship is obtained locally.
[0020] In some embodiments, the step of obtaining the input target code includes:
[0021] In the case of detecting an input operation on the code input interface, if the input content is detected to be consistent with the annotation symbol for the first time, the content in the code input interface before the annotation symbol is determined as the target code;
[0022] If the input content is detected to be consistent with the annotation symbol for the Nth time, the content between the annotation symbol at the Nth time and the annotation symbol at the N-1th time is determined as the target code, where N is an integer greater than 2.
[0023] In some embodiments, the number of the defect repair suggestions is multiple; the method further includes:
[0024] Get the time between the current time and the output time of the defect repair suggestion;
[0025] In the case where the duration exceeds the preset duration, if no selection operation for the defect repair suggestion is detected, selecting a defect repair suggestion from the defect repair suggestions;
[0026] The target code is automatically repaired based on the selected defect repair suggestion.
[0027] In a second aspect, the present application also provides a code processing device. The device comprises:
[0028] An acquisition module is used to obtain the input target code;
[0029] An analysis module, configured to perform static analysis on the target code based on a defect detection model to obtain a static analysis result of the target code; the defect detection model is trained based on normal code fragments and defective code fragments;
[0030] A processing module, configured to output defect repair suggestions for the target code if the static analysis result indicates that the target code has defects;
[0031] A repair module is used to automatically repair the target code based on the defect repair suggestion when a selected operation for the defect repair suggestion is detected.
[0032] In a third aspect, the present application further provides a computer device. The computer device includes a memory and a processor, the memory stores a computer program, and the processor implements the following steps when executing the computer program:
[0033] Get the input target code;
[0034] Performing static analysis on the target code based on a defect detection model to obtain a static analysis result of the target code; the defect detection model is trained based on normal code fragments and defective code fragments;
[0035] When the static analysis result indicates that the target code has a defect, outputting a defect repair suggestion for the target code;
[0036] In a case where a selected operation for the defect repair suggestion is detected, the target code is automatically repaired based on the defect repair suggestion.
[0037] In a fourth aspect, the present application further provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the following steps are implemented:
[0038] Get the input target code;
[0039] Performing static analysis on the target code based on a defect detection model to obtain a static analysis result of the target code; the defect detection model is trained based on normal code fragments and defective code fragments;
[0040] When the static analysis result indicates that the target code has a defect, outputting a defect repair suggestion for the target code;
[0041] In a case where a selected operation for the defect repair suggestion is detected, the target code is automatically repaired based on the defect repair suggestion.
[0042] In a fifth aspect, the present application further provides a computer program product. The computer program product includes a computer program, and when the computer program is executed by a processor, the following steps are implemented:
[0043] Get the input target code;
[0044] Performing static analysis on the target code based on a defect detection model to obtain a static analysis result of the target code; the defect detection model is trained based on normal code fragments and defective code fragments;
[0045] When the static analysis result indicates that the target code has a defect, outputting a defect repair suggestion for the target code;
[0046] In a case where a selected operation for the defect repair suggestion is detected, the target code is automatically repaired based on the defect repair suggestion.
[0047] The above-mentioned code processing method, apparatus, computer equipment, storage medium and computer program product obtain the static analysis result of the target code by obtaining the input target code and performing static analysis on the target code based on the defect detection model, thereby automatically detecting defects in the target code based on the pre-trained model without wasting the time and energy of the developer, which can reduce labor costs. Furthermore, when the static analysis result indicates that the target code has defects, a defect repair suggestion for the target code is output, and when a selected operation for the defect repair suggestion is detected, the target code is automatically repaired based on the defect repair suggestion, thereby automatically repairing the target code based on the defect repair suggestion that matches the target code, which can improve the accuracy of code repair. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] Figure 1 An application environment diagram of a code processing method in an embodiment;
[0049] Figure 2 A schematic diagram of a flow chart of a code processing method in one embodiment;
[0050] Figure 3 A schematic diagram of a process for outputting defect repair suggestions for a target code when a static analysis result indicates that the target code has defects in an embodiment;
[0051] Figure 4 A schematic diagram of a process for outputting defect repair suggestions for a target code when a static analysis result indicates that the target code has defects in another embodiment;
[0052] Figure 5is a flowchart of a code processing method in another embodiment;
[0053] Figure 6 is a flowchart of a code processing method in another embodiment;
[0054] Figure 7 is a structural block diagram of a code processing device in an embodiment;
[0055] Figure 8 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0056] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0057] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant regulations; and the acquisition, storage, processing, transmission, etc. of data must comply with relevant laws and regulations. In the embodiments of this application, some existing solutions in the industry such as certain software, components, models, etc. may be mentioned, and they should be considered as exemplary. Their purpose is only to illustrate the feasibility of the implementation of the technical solution of this application, but it does not mean that the applicant has or will necessarily use the solution.
[0058] During the development of an iOS project, the code may have problems such as memory leaks, excessive object release, type errors, misjudgment, header file introduction errors, and poor algorithm performance. At this time, developers are required to analyze and deal with these problems, which will take a lot of time and energy, but with little effect.
[0059] Currently, the development tool provided by iOS is Xcode, which is an integrated development tool running on the operating system Mac OS X. Xcode has the function of providing automatic repair. For simple type error judgments, it can provide solutions based on clang's local syntax analysis. However, it cannot solve more complex problems. If you want to improve them, you need to master the C++ development language. The cost of learning and implementation is high, and this solution is a localized solution, which is difficult to continuously optimize.
[0060] In view of this, the present application provides a code processing method, which can be based on the existing clang local language analysis and combined with the static code scanning on the iOS side to automatically repair the defective code when there are defects in the static scanning code.
[0061] The code processing method provided in the embodiment of the present application can be applied to Figure 1 In the application environment shown, the terminal 102 communicates with the server 104 through a network. The data storage system can store data that the server 104 needs to process. The data storage system can be integrated on the server 104, or placed on the cloud or other network servers.
[0062] Specifically, the server 104 obtains the input target code from the terminal 102. The server 104 performs static analysis on the target code based on the defect detection model to obtain the static analysis result of the target code; the defect detection model is trained based on the normal code fragment and the defect code fragment; when the static analysis result indicates that the target code has a defect, the server 104 outputs a defect repair suggestion for the target code to the terminal 102. When the server 104 detects the selected operation for the defect repair suggestion, it automatically repairs the target code based on the defect repair suggestion.
[0063] The terminal 102 refers to a front-end device, which can provide a code input interface to developers in the form of an integrated development environment (IDE) plug-in; the terminal 102 can be, but is not limited to, various personal computers, laptops, smart phones, tablet computers, etc. The server 104 refers to a back-end device, which is used to perform a defect detection process for the code; the server 104 can be implemented by an independent server or a server cluster consisting of multiple servers.
[0064] In one embodiment, Figure 2 As shown, a code processing method is provided, which is applied to Figure 1 Taking the server 104 in the example as an example, the following steps are included:
[0065] S202, obtaining the input target code.
[0066] In this embodiment, the target code refers to the code that needs to be defect-checked. The target code may refer to a code snippet, or the target code may refer to a complete code that meets the project requirements. The specific content of the target code may be set according to the application scenario, and this embodiment does not specifically limit it.
[0067] In some embodiments, in combination Figure 1The terminal 102 provides a code input interface, which is used by developers to input target codes that need to be tested for defects. The server 104 can obtain the target code based on the code input interface.
[0068] Specifically, when a paste operation is detected on the code input interface, the server 104 determines the content in the current code input interface as the target code. In other words, the developer pastes the target code that needs to be defect-checked into the code input interface, and the target code may refer to a complete code or a code fragment that meets the project requirements.
[0069] It should be understood that code snippets for implementing different functions may be separated by comment symbols (such as " / "). When a comment symbol is detected, it can be considered that the developer is about to enter the next code snippet, so the target code can be determined based on the comment symbol.
[0070] Specifically, when an input operation on the code input interface is detected, if the input content is detected to be consistent with the annotation symbol for the first time, the server 104 determines the content in the code input interface before the annotation symbol as the target code; if the input content is detected to be consistent with the annotation symbol for the Nth time, the server 104 determines the content between the annotation symbol at the Nth time and the annotation symbol at the N-1th time as the target code, where N is an integer greater than 2. The target code refers to a code fragment, and the number of target codes is at least one.
[0071] S204, performing static analysis on the target code based on the defect detection model to obtain a static analysis result of the target code.
[0072] In this embodiment, static analysis refers to a code analysis method that scans program code through lexical analysis, syntax analysis, control flow, data flow analysis and other technologies without running the code to verify whether the code meets the indicators of standardization, security, reliability, maintainability, etc. Therefore, by statically analyzing the target code through the defect detection model, manual participation in the code analysis process can be avoided, reducing labor costs.
[0073] Among them, the defect detection model is trained based on normal code snippets and defective code snippets, and the defective code snippets include but are not limited to: code snippets with type errors, code snippets with judgment errors, code snippets with header file import errors, etc. When information is annotated before model training, normal code snippets can be annotated as defect-free, and defective code snippets can be annotated as defective. Therefore, based on normal code snippets and abnormal code snippets with annotated information, a defect detection model can be trained. Then, by inputting the target code into the defect detection model, it can be determined whether the target code has defects, that is, the static analysis result of the target code indicates that the target code has defects, or the static analysis result of the target code indicates that the target code does not have defects.
[0074] S206: When the static analysis result indicates that the target code has defects, output defect repair suggestions for the target code.
[0075] In this embodiment, the defect repair suggestion refers to a solution to a defect problem in the target code. When the static analysis result indicates that the target code has a defect, a general code repair solution can be used as a defect repair suggestion for the target code.
[0076] Combination Figure 1 The terminal 102 provides a code input interface, and the server 104 can output defect repair suggestions for the target code in the form of a pop-up window in the code input interface.
[0077] S208: When a selected operation for the defect repair suggestion is detected, the target code is automatically repaired based on the defect repair suggestion.
[0078] In this embodiment, when outputting defect repair suggestions for the target code, if a selection operation for the defect repair suggestion is detected, it can be said that the developer believes that the defect repair suggestion is feasible. Therefore, when the target code is automatically repaired based on the defect repair suggestion, the accuracy of code repair can be improved.
[0079] In some embodiments, the defect repair suggestion may include repair code. Specifically, the defect code in the target code may be replaced with the repair code to achieve automatic repair of the target code.
[0080] In summary, based on Figure 2The method shown obtains the input target code and performs static analysis on the target code based on the defect detection model to obtain the static analysis result of the target code, thereby automatically detecting defects in the target code based on the pre-trained model without wasting the developer's time and energy, thereby reducing labor costs. Furthermore, when the static analysis result indicates that the target code has defects, a defect repair suggestion for the target code is output, and when a selected operation for the defect repair suggestion is detected, the target code is automatically repaired based on the defect repair suggestion, thereby automatically repairing the target code based on the defect repair suggestion that matches the target code, thereby improving the accuracy of code repair.
[0081] In one embodiment, Figure 3 As shown, a flowchart of outputting defect repair suggestions for the target code when the static analysis results indicate that the target code has defects is provided. Figure 1 Taking the server 104 in the example as an example, the following steps are included:
[0082] S302: When the static analysis result indicates that the target code has a defect, determine the defect type of the target code.
[0083] In this embodiment, when the static analysis result indicates that the target code has a defect, the target code may be analyzed based on the type detection model to determine the defect type of the target code.
[0084] The type detection model is trained based on the normal code training set and the defective code training set. The defective code training set includes but is not limited to: code snippets with type errors, code snippets with judgment errors, code snippets with header file introduction errors, etc. When information is annotated before model training, the type of each normal code snippet in the normal code training set can be annotated as non-defective, and the defective code snippet in the defective code training set can be annotated as type error, judgment error, or header file introduction error, etc. according to the actual defect type. Thus, based on the normal code training set and the defective code training set with annotated information, a type detection model can be trained. Therefore, the defect type of the target code can be determined by inputting the target code into the type detection model.
[0085] S304 , based on the mapping relationship between the defect type and the defect repair suggestion, determine the defect repair suggestion that matches the defect type of the target code, and output the defect repair suggestion.
[0086] In this embodiment, by associating defect types with their corresponding repair solutions, a mapping relationship between defect types and defect repair suggestions can be obtained. The repair solution corresponding to the defect type can refer to a general repair solution or a specific repair solution set for a project.
[0087] In some embodiments, the server 104 may call a plug-in to run a code written in an Xcode compiler to determine a defect repair suggestion that matches the defect type of the target code from a mapping relationship between defect types and defect repair suggestions.
[0088] Specifically, a target defect type that is the same as the defect type of the target code is determined from each defect type in the mapping relationship; and a defect repair suggestion corresponding to the target defect type in the mapping relationship is determined as a defect repair suggestion that matches the defect type of the target code.
[0089] In summary, based on Figure 3 The method shown can improve the efficiency of obtaining defect repair suggestions for target code by setting the mapping relationship between defect types and defect repair suggestions in advance, thereby improving the efficiency of repairing the target code.
[0090] In one embodiment, Figure 4 As shown in FIG. 1 , a flowchart of outputting defect repair suggestions for a target code when a static analysis result indicates that the target code has defects is provided. The method is applied to Figure 1 Taking the server 104 in the example as an example, the following steps are included:
[0091] S402, when the static analysis result indicates that the target code has defects, obtaining a first content feature of the target code and a second content feature of each preset defect code.
[0092] In this embodiment, the first content feature is used to characterize the characteristics of the code content of the target code, and the second content feature is used to characterize the characteristics of the code content of the preset defect code, which may include characteristics of at least one dimension such as syntax, structure, and interface. The first content feature and the second content feature may be represented by a vector or a matrix. In one embodiment, the sub-features included in the first content feature and the second content feature are the same, so that subsequent similarity analysis can be facilitated.
[0093] Specifically, the server can analyze the code content of the target code based on the trained feature extraction network to obtain the first content feature; based on the trained feature extraction network, the server can analyze the code content of each preset defect code to obtain the second content feature of each preset defect code.
[0094] S404: Based on the similarity between the first content feature and each second content feature and the mapping relationship between the preset defect code and the defect repair suggestion, the target code is analyzed to determine the defect repair suggestion matching the target code, and the defect repair suggestion is output.
[0095] In this embodiment, the server 104 may perform similarity analysis on the first content feature and each second content feature to determine the similarity between the first content feature and each second content feature. The similarity may be represented by at least one of a plurality of similarity indicators such as Euclidean distance, Manhattan distance, Mahalanobis distance or cosine similarity.
[0096] In some embodiments, the similarity is represented by at least two similarity indicators. The server 104 can perform similarity analysis on the first content feature and each second content feature based on the similarity algorithm of each similarity indicator to determine the index value of each similarity indicator; and perform statistics on each index value based on the index weight of each similarity indicator to determine the similarity between the first content feature and each second content feature.
[0097] Specifically, based on the similarity between the first content feature and each second content feature, determine from each second content feature a target content feature having the greatest similarity to the first content feature; determine from each preset defect code a target defect code corresponding to the target content feature; and from a mapping relationship between preset defect codes and defect repair suggestions, determine the defect repair suggestion corresponding to the target defect code as a defect repair suggestion that matches the target code.
[0098] In summary, based on Figure 4 The method shown can improve the efficiency of obtaining defect repair suggestions for target codes by setting the mapping relationship between preset defect codes and defect repair suggestions in advance, thereby improving the efficiency of repairing the target code.
[0099] In combination with the above content, it can be seen that when the static analysis results indicate that there are defects in the target code, it is necessary to obtain the mapping relationship between the defect type and the defect repair suggestion, or the mapping relationship between the preset defect code and the defect repair suggestion, so as to determine the defect repair suggestion that matches the target code.
[0100] The mapping relationship between the preset defect code and the defect repair suggestion, and the mapping relationship between the defect type and the defect repair suggestion can be stored locally or in the cloud. The mapping relationship stored in the cloud is the latest mapping relationship. The mapping relationship stored locally can be the same as the mapping relationship stored in the cloud. The mapping relationship stored locally can also be a subset of the mapping relationship stored in the cloud, that is, the mapping relationship stored locally does not yet include the newly added mapping relationship.
[0101] In some embodiments, the availability status of the current network service can be determined to determine whether to obtain the mapping relationship from the cloud or locally. The mapping relationship includes: a mapping relationship between a defect type and a defect repair suggestion, or a mapping relationship between a preset defect code and a defect repair suggestion.
[0102] Specifically, when the current network service is available, the mapping relationship is obtained from the cloud. Furthermore, the mapping relationship stored in the cloud can be synchronized locally to improve the timeliness of updating the mapping relationship stored locally.
[0103] Specifically, when the current network service is unavailable, the mapping relationship is obtained locally. Furthermore, when the current network service is available, the mapping relationship stored in the cloud is synchronized locally to improve the timeliness of updating the mapping relationship stored locally.
[0104] In one embodiment, the number of defect repair suggestions for the target code may be multiple, specifically, Figure 5 As shown, a code processing method is provided, which is applied to Figure 1 Taking the server 104 in the example as an example, the following steps are included:
[0105] S502: Obtain the time between the current time and the output time of the defect repair suggestion.
[0106] In this embodiment, a timer may be set, and when the defect repair suggestion for the target code is output, the timer is started. Thus, based on the timer, the duration between the current time and the output time of the defect repair suggestion may be obtained.
[0107] S504: When the duration exceeds the preset duration, if no selection operation for the defect repair suggestion is detected, select a defect repair suggestion from the defect repair suggestions.
[0108] Specifically, a defect repair suggestion may be randomly selected from the defect repair suggestions; or, the number of times each defect repair suggestion is selected may be counted, and the defect repair suggestion with the largest number of selections may be selected.
[0109] S506: Automatically repair the target code based on the selected defect repair suggestion.
[0110] In summary, based on Figure 5 In the method shown, if the selection operation for the defect repair suggestion is not detected when the duration exceeds the preset duration, it means that the developer has not actively selected the defect repair suggestion from the output of the defect repair suggestion to the present. In this case, the repair efficiency can be improved by automatically repairing the target code by selecting a defect repair suggestion from various defect repair suggestions.
[0111] In combination with the above, in one embodiment, Figure 6 As shown, a code processing method is provided, which is applied to Figure 1 Taking the server 104 in the example as an example, the following steps are included:
[0112] S602, obtaining an input target code; performing static analysis on the target code based on a defect detection model to obtain a static analysis result of the target code; the defect detection model is trained based on normal code snippets and defective code snippets.
[0113] Furthermore, when the static analysis result indicates that the target code has defects, the implementation method of outputting defect repair suggestions for the target code may refer to the contents described in S604 or S606.
[0114] S604, when the static analysis result indicates that the target code has a defect, determine the defect type of the target code; based on the mapping relationship between the defect type and the defect repair suggestion, determine the defect repair suggestion that matches the defect type of the target code, and output the defect repair suggestion.
[0115] S606, when the static analysis result indicates that the target code has a defect, obtain the first content feature of the target code and the second content feature of each preset defect code; based on the similarity between the first content feature and each second content feature, and the mapping relationship between the preset defect code and the defect repair suggestion, analyze the target code, determine the defect repair suggestion that matches the target code, and output the defect repair suggestion.
[0116] S608: When a selected operation for the defect repair suggestion is detected, the target code is automatically repaired based on the defect repair suggestion.
[0117] Among them, the specific content of S602-S608 can be adapted to the description of the above content and will not be repeated here.
[0118] In combination with the above content, it can be seen that the code processing method provided by this application can quickly complete the replacement and repair of codes without wasting the time and energy of developers, thereby improving work efficiency.
[0119] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps is not strictly limited in order, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0120] Based on the same inventive concept, the embodiment of the present application also provides a code processing device for implementing the above-mentioned code processing method. The implementation solution provided by the device to solve the problem is similar to the implementation solution recorded in the above-mentioned method, so the specific limitations in one or more code processing device embodiments provided below can refer to the limitations of the code processing method above, and will not be repeated here.
[0121] In one embodiment, Figure 7 As shown, a code processing device is provided, including: an acquisition module 702, an analysis module 704, a processing module 706 and a repair module 708, wherein:
[0122] The acquisition module 702 is used to acquire the input target code.
[0123] The analysis module 704 is used to perform static analysis on the target code based on a defect detection model to obtain a static analysis result of the target code; the defect detection model is trained based on normal code snippets and defective code snippets.
[0124] The processing module 706 is used to output defect repair suggestions for the target code when the static analysis result indicates that the target code has defects.
[0125] The repair module 708 is used to automatically repair the target code based on the defect repair suggestion when a selected operation for the defect repair suggestion is detected.
[0126] In some embodiments, the processing module 706 is also used to: determine the defect type of the target code when the static analysis result indicates that the target code has a defect; determine the defect repair suggestion that matches the defect type of the target code based on the mapping relationship between the defect type and the defect repair suggestion, and output the defect repair suggestion.
[0127] In some embodiments, the processing module 706 is also used to: when the static analysis result indicates that the target code has a defect, obtain a first content feature of the target code and a second content feature of each preset defect code; based on the similarity between the first content feature and each of the second content features, and the mapping relationship between the preset defect code and the defect repair suggestion, analyze the target code, determine a defect repair suggestion that matches the target code, and output the defect repair suggestion.
[0128] In some embodiments, the acquisition module 702 is further used to: determine the availability status of the current network service;
[0129] When the current network service is available, the mapping relationship is obtained from the cloud; when the current network service is unavailable, the mapping relationship is obtained locally.
[0130] In some embodiments, the acquisition module 702 is also used for: in the case of detecting an input operation on the code input interface, if the input content is detected to be consistent with the annotation symbol for the first time, the content in the code input interface before the annotation symbol is determined as the target code; if the input content is detected to be consistent with the annotation symbol for the Nth time, the content between the annotation symbol at the Nth time and the annotation symbol at the N-1th time is determined as the target code, where N is an integer greater than 2.
[0131] In some embodiments, the repair module 708 is also used to: obtain the duration between the current time and the output time of the defect repair suggestion; if the duration exceeds the preset duration, if no selection operation for the defect repair suggestion is detected, select a defect repair suggestion from the defect repair suggestions; and automatically repair the target code based on the selected defect repair suggestion.
[0132] Each module in the above code processing device can be implemented in whole or in part by software, hardware, or a combination thereof. Each module can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute operations corresponding to each module.
[0133] In one embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as follows: Figure 8As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, referred to as I / O) and a communication interface. Among them, the processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store the mapping relationship between defect types and defect repair suggestions, and the mapping relationship between preset defect codes and defect repair suggestions. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a code processing method is implemented.
[0134] Those skilled in the art will understand that Figure 8 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0135] In one embodiment, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, the following steps are implemented:
[0136] Obtain input target code; perform static analysis on the target code based on a defect detection model to obtain a static analysis result of the target code; the defect detection model is trained based on normal code snippets and defective code snippets; when the static analysis result indicates that the target code has defects, output defect repair suggestions for the target code; when a selected operation for the defect repair suggestion is detected, automatically repair the target code based on the defect repair suggestion.
[0137] In one embodiment, when the processor executes the computer program, the following steps are also implemented: when the static analysis result indicates that there is a defect in the target code, determining the defect type of the target code; based on the mapping relationship between the defect type and the defect repair suggestion, determining the defect repair suggestion that matches the defect type of the target code, and outputting the defect repair suggestion.
[0138] In one embodiment, when the processor executes the computer program, the following steps are also implemented: when the static analysis result indicates that the target code has a defect, a first content feature of the target code and a second content feature of each preset defect code are obtained; based on the similarity between the first content feature and each of the second content features, and a mapping relationship between preset defect codes and defect repair suggestions, the target code is analyzed to determine a defect repair suggestion that matches the target code, and the defect repair suggestion is output.
[0139] In one embodiment, when the processor executes the computer program, the following steps are also implemented: determining the availability status of the current network service; if the current network service is available, obtaining the mapping relationship from the cloud; if the current network service is unavailable, obtaining the mapping relationship locally.
[0140] In one embodiment, the processor further implements the following steps when executing the computer program: when an input operation is detected for the code input interface, if the input content is detected to be consistent with the annotation symbol for the first time, the content in the code input interface before the annotation symbol is determined as the target code; if the input content is detected to be consistent with the annotation symbol for the Nth time, the content between the annotation symbol at the Nth time and the annotation symbol at the N-1th time is determined as the target code, where N is an integer greater than 2.
[0141] In one embodiment, there are multiple defect repair suggestions; when the processor executes the computer program, the following steps are also implemented: obtaining the duration between the current time and the output time of the defect repair suggestion; when the duration exceeds the preset duration, if no selection operation for the defect repair suggestion is detected, selecting a defect repair suggestion from each of the defect repair suggestions; and automatically repairing the target code based on the selected defect repair suggestion.
[0142] In one embodiment, a computer readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by a processor, the following steps are implemented:
[0143] Obtain input target code; perform static analysis on the target code based on a defect detection model to obtain a static analysis result of the target code; the defect detection model is trained based on normal code snippets and defective code snippets; when the static analysis result indicates that the target code has defects, output defect repair suggestions for the target code; when a selected operation for the defect repair suggestion is detected, automatically repair the target code based on the defect repair suggestion.
[0144] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented: when the static analysis result indicates that there is a defect in the target code, determining the defect type of the target code; based on the mapping relationship between the defect type and the defect repair suggestion, determining the defect repair suggestion that matches the defect type of the target code, and outputting the defect repair suggestion.
[0145] In one embodiment, when the computer program is executed by the processor, the following steps are also implemented: when the static analysis result indicates that the target code has a defect, a first content feature of the target code and a second content feature of each preset defect code are obtained; based on the similarity between the first content feature and each of the second content features, and a mapping relationship between preset defect codes and defect repair suggestions, the target code is analyzed to determine a defect repair suggestion that matches the target code, and the defect repair suggestion is output.
[0146] In one embodiment, when the computer program is executed by the processor, the following steps are also implemented: determining the availability status of the current network service; when the current network service is available, obtaining the mapping relationship from the cloud; when the current network service is unavailable, obtaining the mapping relationship locally.
[0147] In one embodiment, when the computer program is executed by the processor, the following steps are also implemented: when an input operation is detected for the code input interface, if the input content is detected to be consistent with the annotation symbol for the first time, the content in the code input interface before the annotation symbol is determined as the target code; if the input content is detected to be consistent with the annotation symbol for the Nth time, the content between the annotation symbol at the Nth time and the annotation symbol at the N-1th time is determined as the target code, where N is an integer greater than 2.
[0148] In one embodiment, there are multiple defect repair suggestions; when the computer program is executed by the processor, the following steps are also implemented: obtaining the duration between the current time and the output time of the defect repair suggestion; when the duration exceeds the preset duration, if no selection operation for the defect repair suggestion is detected, selecting a defect repair suggestion from the defect repair suggestions; and automatically repairing the target code based on the selected defect repair suggestion.
[0149] In one embodiment, a computer program product is provided, comprising a computer program, which, when executed by a processor, implements the following steps:
[0150] Obtain input target code; perform static analysis on the target code based on a defect detection model to obtain a static analysis result of the target code; the defect detection model is trained based on normal code snippets and defective code snippets; when the static analysis result indicates that the target code has defects, output defect repair suggestions for the target code; when a selected operation for the defect repair suggestion is detected, automatically repair the target code based on the defect repair suggestion.
[0151] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented: when the static analysis result indicates that there is a defect in the target code, determining the defect type of the target code; based on the mapping relationship between the defect type and the defect repair suggestion, determining the defect repair suggestion that matches the defect type of the target code, and outputting the defect repair suggestion.
[0152] In one embodiment, when the computer program is executed by the processor, the following steps are also implemented: when the static analysis result indicates that the target code has a defect, a first content feature of the target code and a second content feature of each preset defect code are obtained; based on the similarity between the first content feature and each of the second content features, and a mapping relationship between preset defect codes and defect repair suggestions, the target code is analyzed to determine a defect repair suggestion that matches the target code, and the defect repair suggestion is output.
[0153] In one embodiment, when the computer program is executed by the processor, the following steps are also implemented: determining the availability status of the current network service; when the current network service is available, obtaining the mapping relationship from the cloud; when the current network service is unavailable, obtaining the mapping relationship locally.
[0154] In one embodiment, when the computer program is executed by the processor, the following steps are also implemented: when an input operation is detected for the code input interface, if the input content is detected to be consistent with the annotation symbol for the first time, the content in the code input interface before the annotation symbol is determined as the target code; if the input content is detected to be consistent with the annotation symbol for the Nth time, the content between the annotation symbol at the Nth time and the annotation symbol at the N-1th time is determined as the target code, where N is an integer greater than 2.
[0155] In one embodiment, there are multiple defect repair suggestions; when the computer program is executed by the processor, the following steps are also implemented: obtaining the duration between the current time and the output time of the defect repair suggestion; when the duration exceeds the preset duration, if no selection operation for the defect repair suggestion is detected, selecting a defect repair suggestion from the defect repair suggestions; and automatically repairing the target code based on the selected defect repair suggestion.
[0156] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., but are not limited to this.
[0157] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0158] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A code processing method, characterized in that: The method comprises: Get the input target code; Performing static analysis on the target code based on a defect detection model to obtain a static analysis result of the target code; the defect detection model is trained based on normal code fragments and defective code fragments; When the static analysis result indicates that the target code has a defect, outputting a defect repair suggestion for the target code; In a case where a selected operation for the defect repair suggestion is detected, the target code is automatically repaired based on the defect repair suggestion.
2. The method according to claim 1, characterized in that When the static analysis result indicates that the target code has a defect, outputting a defect repair suggestion for the target code includes: In the case where the static analysis result indicates that the target code has a defect, determining the defect type of the target code; Based on the mapping relationship between the defect type and the defect repair suggestion, a defect repair suggestion matching the defect type of the target code is determined, and the defect repair suggestion is output.
3. The method according to claim 1, characterized in that When the static analysis result indicates that the target code has a defect, outputting a defect repair suggestion for the target code includes: When the static analysis result indicates that the target code has a defect, obtaining a first content feature of the target code and a second content feature of each preset defect code; Based on the similarity between the first content feature and each of the second content features, and the mapping relationship between the preset defect code and the defect repair suggestion, the target code is analyzed, the defect repair suggestion matching the target code is determined, and the defect repair suggestion is output.
4. The method according to claim 2 or 3, characterized in that: The method for obtaining the mapping relationship includes: Determine the availability status of current network services; When the current network service is available, obtaining the mapping relationship from the cloud; When the current network service is unavailable, the mapping relationship is obtained locally.
5. The method according to any one of claims 1 to 3, characterized in that: The step of obtaining the input target code comprises: In the case of detecting an input operation on the code input interface, if the input content is detected to be consistent with the annotation symbol for the first time, the content in the code input interface before the annotation symbol is determined as the target code; If the input content is detected to be consistent with the annotation symbol for the Nth time, the content between the annotation symbol at the Nth time and the annotation symbol at the N-1th time is determined as the target code, where N is an integer greater than 2.
6. The method according to any one of claims 1 to 3, characterized in that: The number of the defect repair suggestions is multiple; and the method further includes: Get the time between the current time and the output time of the defect repair suggestion; In the case where the duration exceeds the preset duration, if no selection operation for the defect repair suggestion is detected, selecting a defect repair suggestion from the defect repair suggestions; The target code is automatically repaired based on the selected defect repair suggestion.
7. A code processing device, characterized in that: The device comprises: An acquisition module is used to obtain the input target code; An analysis module, configured to perform static analysis on the target code based on a defect detection model to obtain a static analysis result of the target code; the defect detection model is trained based on normal code fragments and defective code fragments; A processing module, configured to output defect repair suggestions for the target code if the static analysis result indicates that the target code has defects; A repair module is used to automatically repair the target code based on the defect repair suggestion when a selected operation for the defect repair suggestion is detected.
8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
