Target code positioning method and device, equipment and storage medium
By generating an abstract syntax tree and applying preset check rules, the code is checked, and the problem of difficulty in positioning exceptions is solved, and the rapid identification and positioning of codes that fail to throw exceptions are achieved.
Patent Information
- Application Number
- CN202411877213.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-18
- Publication Date
- 2025-05-13
AI Technical Summary
During the system operation, abnormal situations are difficult to accurately locate, making it difficult for operation and maintenance and developers to quickly solve problems.
By generating an abstract syntax tree (AST) and applying preset check rules, check the code, identify and locate exception points in the target code.
It quickly recognizes and locates codes that fail to throw exceptions normally, avoiding maintenance difficulties caused by the inability to locate exceptions after they are launched.
Smart Images

Figure CN119988191A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software development technology, and in particular to a method and device for locating a target code, an electronic device, and a storage medium. Background Art
[0002] During system operation, exceptions are inevitable. For operation and maintenance personnel and developers, it is crucial to accurately obtain the cause of the exception. Usually, after an exception occurs at a certain stage of system operation, it will be passed up layer by layer to the caller, and finally fed back to the user through the interface or log. If the transmission of error information is distorted during this process, it will make it difficult to locate the exception.
[0003] Therefore, how to check the code to avoid the problem of unlocatable anomalies in online products is an issue that needs to be solved urgently. Summary of the invention
[0004] The embodiment of the present application provides a method for locating a target code to solve the problem of how to check the code and avoid the problem of being unable to locate anomalies in online products.
[0005] Correspondingly, an embodiment of the present application also provides a target code positioning device, an electronic device and a storage medium to ensure the implementation and application of the above method.
[0006] In order to solve the above problems, an embodiment of the present application discloses a method for locating a target code, the method comprising:
[0007] Get the code to be checked;
[0008] Generate an abstract syntax tree according to the code to be checked; the abstract syntax tree includes nodes;
[0009] Using a preset checking rule to check the nodes of the abstract syntax tree to obtain a target node;
[0010] The target code in the code to be checked is located according to the target node.
[0011] In an optional embodiment of the present application, the step of checking the nodes of the abstract syntax tree using a preset checking rule to obtain a target node includes:
[0012] Checking whether the node is a function;
[0013] When the node is the function, determining whether the function has a return value;
[0014] When the function has a return value, the node corresponding to the function is used as the node to be checked;
[0015] The node to be checked is checked to obtain the target node.
[0016] In an optional embodiment of the present application, the step of inspecting the node to be inspected to obtain the target node includes:
[0017] Check whether the node to be checked is a conditional statement;
[0018] When the node to be checked is the conditional statement, determining whether the conditional statement is a binary expression; the binary expression includes an operation element and an operator;
[0019] When the conditional statement is the binary expression, determining whether the operation element and the operator are preset operation elements and preset operators;
[0020] When the operation element and the operator are the preset operation element and the preset operator, parsing the conditional statement to obtain a parsing result;
[0021] According to the parsing result, it is determined whether the node to be checked is the target node.
[0022] In an optional embodiment of the present application, the conditional statement includes an execution code when the condition is true, and judging whether the node to be checked is the target node according to the parsing result includes:
[0023] Checking whether the execution code of the conditional statement of the node to be checked includes a return statement;
[0024] When the execution code includes the return statement, determining the return content of the return statement;
[0025] When the returned content is preset content, the node to be checked is used as the target node.
[0026] In an optional embodiment of the present application, the method further includes:
[0027] When the returned content is not a preset content, checking whether the returned statement is a preset statement;
[0028] When the return statement is the preset statement, the node to be checked is used as the target node.
[0029] In an optional embodiment of the present application, the method further includes:
[0030] When the node to be checked is the conditional statement, and it is impossible to determine whether the node to be checked is the target node, an alarm log is output; and the alarm log includes the conditional statement.
[0031] In an optional embodiment of the present application, the obtaining of the code to be checked includes:
[0032] Traversing the directory to be checked; the directory to be checked includes code files, the code files have corresponding file name suffixes, and the code files include codes;
[0033] If the file name suffix of the code file is a preset suffix, the code in the code file is used as the code to be checked.
[0034] The embodiment of the present application also discloses a device for locating a target code, the device comprising:
[0035] A module for obtaining codes to be checked, used for obtaining codes to be checked;
[0036] A syntax tree generation module, used to generate an abstract syntax tree according to the code to be checked; the abstract syntax tree includes nodes;
[0037] A node checking module, used to check the nodes of the abstract syntax tree using preset checking rules to obtain a target node;
[0038] The target code locating module is used to locate the target code in the code to be checked according to the target node.
[0039] An embodiment of the present application also discloses an electronic device, including: a processor; and a memory, on which executable code is stored. When the executable code is executed, the processor executes a method for locating a target code as described in one or more of the embodiments of the present application.
[0040] The embodiments of the present application also disclose one or more machine-readable media on which executable codes are stored. When the executable codes are executed, the processor executes the method for locating the target code as described in one or more of the embodiments of the present application.
[0041] Compared with the prior art, the embodiments of the present application have the following advantages:
[0042] In an embodiment of the present application, the code to be checked is obtained; based on the code to be checked, an abstract syntax tree is generated; the abstract syntax tree includes nodes; the nodes of the abstract syntax tree are checked using preset checking rules to obtain target nodes; based on the target nodes, the target code in the code to be checked is located. The embodiment of the present application performs abstract processing on the code that does not throw exceptions normally, and uses tools rather than manual troubleshooting. The embodiment of the present application abstracts the reasons why the exception cannot be obtained normally at the application level from the code level into code patterns that fail to throw exceptions normally. Through AST (Abstract SyntaxTree, Abstract Syntax Tree) parsing technology, these written code exception patterns are structured and converted into models that can be understood by the program. Subsequently, by parsing the code into AST and deeply parsing it, the code that fails to throw exceptions normally can be quickly identified, thereby avoiding the consequences of such code being difficult to maintain after going online. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 It is a flowchart of the steps of an embodiment of a method for locating a target code of the present application;
[0044] Figure 2 is a flow chart of an embodiment of a method for locating a target code of the present application;
[0045] Figure 3 It is a structural block diagram of an embodiment of a positioning device for a target code of the present application;
[0046] Figure 4 It is a schematic diagram of the structure of a device provided in one embodiment of the present application. DETAILED DESCRIPTION
[0047] In order to make the above-mentioned objects, features and advantages of the present application more obvious and easy to understand, the present application is further described in detail below in conjunction with the accompanying drawings and specific implementation methods.
[0048] During system operation, exceptions are inevitable. For operation and maintenance personnel and developers, it is crucial to accurately obtain the cause of the exception. Usually, after an exception occurs at a certain stage of system operation, it will be passed up layer by layer to the caller, and finally fed back to the user through the interface or log. If the transmission of error information is distorted during this process, it will make it difficult to locate the exception.
[0049] There is a target code as follows:
[0050] err:=someFunc()
[0051] if err! = nil{
[0052] return fmt.Errorf("call someFunc failed")
[0053] }
[0054] In this target code, if an exception is thrown when calling the someFunc() function, the operation and maintenance personnel can only obtain the output "call someFunc failed" in the end, and cannot learn the specific content of the exception, making it difficult to locate the subsequent problem. This brings great trouble to the operation and maintenance work of the online business.
[0055] For example, suppose Xiao Ming is the owner of a restaurant, Zhang San is the person in charge of the kitchen, and Li Si is a chef in the kitchen. One day, Xiao Ming asked Zhang San to arrange the kitchen to make a dish of fish-flavored shredded pork. Zhang San gave this task to Li Si. However, when Li Si was performing the task, he found that there was not enough shredded pork, which made the task impossible to complete. So Li Si reported to Zhang San the abnormal situation of "not enough shredded pork, failed to make fish-flavored shredded pork". According to the normal process, Zhang San only needs to feedback this message to Xiao Ming intact, and Xiao Ming can take corresponding measures based on this information (such as replenishing shredded pork or removing the dish of fish-flavored shredded pork) to avoid encountering such abnormalities again. However, if Zhang San reports to Xiao Ming with distorted information, such as only telling "failed to make fish-flavored shredded pork", then this abnormal message loses its validity, because Xiao Ming cannot obtain the specific cause of the error from it, and thus cannot take further repair measures.
[0056] To solve related problems, an embodiment of the present application proposes a method for locating target code, which can check the writing of the code and quickly identify the code that fails to throw exceptions normally, thereby avoiding the consequences of difficult maintenance of such code after it goes online.
[0057] Reference Figure 1 , is a flowchart of an embodiment of a method for locating a target code of the present application, comprising the following steps:
[0058] Step 101: Get the code to be checked.
[0059] A method for locating a target code shown in an embodiment of the present application can be applied to checking golang (i.e., Go language) code.
[0060] For golang code, there is currently a de facto standard golangci-lint (a static code analysis tool for the Go language), which is widely used for static checking in various scenarios. Currently, golangci-lint has more than 100 checking rules (a checking rule is called a linter), and a method for locating a target code shown in the embodiment of the present application can also be used as a checking rule for checking golang code.
[0061] In one embodiment, before step 101, the directory to be checked may be traversed to obtain files with a suffix of ".go" in the directory to be checked, and these files contain the golang code to be checked.
[0062] In step 101, the code to be checked is obtained from a code file.
[0063] Step 102: Generate an abstract syntax tree according to the code to be checked; the abstract syntax tree includes nodes.
[0064] In step 102, syntax parsing is performed on the code to be checked in each code file, so as to generate an abstract syntax tree (AST) including nodes for the code file.
[0065] An abstract syntax tree is an abstract representation of the syntax structure of source code. It represents the syntax structure of a programming language in a tree-like form. Each node of the abstract syntax tree represents a syntax structure in the code (such as an expression, statement, function, etc.).
[0066] Step 103: Use a preset checking rule to check the nodes of the abstract syntax tree to obtain a target node.
[0067] In step 103, the nodes of the abstract syntax tree are checked according to preset checking rules to obtain a target node, and the code syntax structure corresponding to the target node is a code syntax structure that has a risk of failing to throw an exception normally.
[0068] In one embodiment, the conditions that the target node needs to meet may include: the function in which the target node is located is a function whose return value is error; there is an if statement (conditional statement), and the if statement contains a judgment statement of err!=nil; and there is a return statement (return statement) in the body of the if statement (the code block to be executed when the condition is true).
[0069] Step 104: Locate the target code in the code to be checked according to the target node.
[0070] In step 104, after locating the target node, the target code in the code to be checked may be further located according to the target node, and the target code is the code that may not normally throw an exception.
[0071] In an embodiment of the present application, the code to be checked is obtained; based on the code to be checked, an abstract syntax tree is generated; the abstract syntax tree includes nodes; the nodes of the abstract syntax tree are checked using preset checking rules to obtain target nodes; based on the target nodes, the target code in the code to be checked is located. The embodiment of the present application performs abstract processing on the code that does not throw exceptions normally, and uses tools rather than manual troubleshooting. The embodiment of the present application abstracts the reasons why the exception cannot be obtained normally at the application level from the code level into code patterns that fail to throw exceptions normally. Through AST parsing technology, these written code exception patterns are structured and converted into models that can be understood by the program. Subsequently, by parsing the code into AST and performing in-depth analysis on it, the code that fails to throw exceptions normally can be quickly identified, thereby avoiding the consequences of such code being difficult to maintain after going online.
[0072] In an optional embodiment of the present application, step 103 includes:
[0073] Checking whether the node is a function;
[0074] When the node is the function, determining whether the function has a return value;
[0075] When the function has a return value, the node corresponding to the function is used as the node to be checked;
[0076] The node to be checked is checked to obtain the target node.
[0077] In this embodiment, checking the node may specifically include the following steps.
[0078] First, check whether the node is a function, that is, check whether the node is a FuncDecl (function declaration). When the node is a function, further determine whether the function has a return value. In one embodiment, it is possible to determine whether the function has a return value by checking the length of the return value list. If the length of the return value list is 0, it means that the function has no return value; if the length of the return value list is not 0, the function has a return value.
[0079] When the function has a return value, the node corresponding to the function is used as the node to be checked, so as to check the node to be checked and obtain the target node.
[0080] In one embodiment, when a function has a return value, the types of the return values can be traversed, and as long as there is an error type, AST parsing of the function continues to be performed to determine whether the node is the target node.
[0081] The embodiments of the present application can structure the written code exception pattern, convert it into an abstract syntax tree that can be understood by the program, and perform in-depth analysis on it, so as to quickly identify the code that fails to throw an exception normally.
[0082] In an optional embodiment of the present application, the step 103 further includes:
[0083] Check whether the node to be checked is a conditional statement;
[0084] When the node to be checked is the conditional statement, determining whether the conditional statement is a binary expression; the binary expression includes an operation element and an operator;
[0085] When the conditional statement is the binary expression, determining whether the operation element and the operator are preset operation elements and preset operators;
[0086] When the operation element and the operator are the preset operation element and the preset operator, parsing the conditional statement to obtain a parsing result;
[0087] According to the parsing result, it is determined whether the node to be checked is the target node.
[0088] In this embodiment, checking the node to be checked to determine whether it is a target node may specifically include the following steps.
[0089] First check whether the node to be checked is a conditional statement, that is, check whether the node to be checked is IfStmt(fStatement, conditional statement). When the node to be checked is a conditional statement, determine whether the conditional statement is a binary expression. A binary expression is an expression that includes two operation elements and an operator. If the conditional statement is not a binary expression, then the conditional statement must not be "err!=nil", and there is no need to continue checking the node.
[0090] When the conditional statement is a binary expression, determine whether the operator is a preset operator. In one embodiment, the preset operator may be "!=". If the operator is not "!=", the conditional statement must not be "err!=nil", and there is no need to continue checking the node. If the operator is a preset operator, determine whether the operation element is a preset operation element. In one embodiment, the preset operation element may be "nil". If the operation element on the right is not "nil", the conditional statement must not be "err!=nil", and there is no need to continue checking the node.
[0091] When the operation element and the operator are the preset operation element and the preset operator, the conditional statement is further parsed to obtain a parsing result, and then it is determined whether the node to be checked is the target node according to the parsing result.
[0092] The embodiments of the present application can structure the written code exception pattern, convert it into an abstract syntax tree that can be understood by the program, and perform in-depth analysis on it, so as to quickly identify the code that fails to throw an exception normally.
[0093] In an optional embodiment of the present application, the conditional statement includes an execution code when the condition is true, and the step 103 further includes:
[0094] Checking whether the execution code of the conditional statement of the node to be checked includes a return statement;
[0095] When the execution code includes the return statement, determining the return content of the return statement;
[0096] When the returned content is preset content, the node to be checked is used as the target node.
[0097] In this embodiment, if the operation element and the operator of the conditional statement are the preset operation element and the preset operator, the execution code when the condition of the conditional statement is true may continue to be parsed, which may specifically include the following steps.
[0098] Check whether the execution code of the conditional statement of the node to be checked includes a return statement, that is, check whether the execution code includes returnStmt (Return Statement). When the execution code includes a return statement, determine the return content of the return statement. When the return content is the preset content, use the node to be checked as the target node.
[0099] In one embodiment, the preset content may be error.
[0100] The embodiments of the present application can structure the written code exception pattern, convert it into an abstract syntax tree that can be understood by the program, and perform in-depth analysis on it, so as to quickly identify the code that fails to throw an exception normally.
[0101] In an optional embodiment of the present application, the method further includes:
[0102] When the returned content is not a preset content, checking whether the returned statement is a preset statement;
[0103] When the return statement is the preset statement, the node to be checked is used as the target node.
[0104] In this embodiment, if the execution code includes a return statement and the return content is not a preset content, it can be further checked whether the return statement is a preset statement. If the return statement is a preset statement, the node to be checked is used as the target node.
[0105] In one embodiment, if the content returned by the return statement is not error, it is necessary to continue to determine whether the return statement is a preset statement, that is, whether the statement is another statement that can return error. For example, statements such as return fmt.Errorf("%s", err) and return fmt.Errorf("%s", err.Error()) can all return error. This judgment method is used to determine whether the content of the return statement is a function call, and whether the name of the function call is Errorf. If it is Errorf, it is further determined whether its parameters contain error or err.Error(). If these conditions are met, it indicates that the return statement may not be able to throw an exception normally, and the node where it is located can be used as the target node.
[0106] The embodiments of the present application can structure the written code exception pattern, convert it into an abstract syntax tree that can be understood by the program, and perform in-depth analysis on it, so as to quickly identify the code that fails to throw an exception normally.
[0107] In an optional embodiment of the present application, the method further includes:
[0108] When the node to be checked is the conditional statement, and it is impossible to determine whether the node to be checked is the target node, an alarm log is output; and the alarm log includes the conditional statement.
[0109] In this embodiment, if the node to be checked is a conditional statement, but the above-mentioned inspection rules cannot determine whether the node to be checked to which the conditional statement belongs is the target node, an alarm can be output through an alarm log to prompt the operation and maintenance personnel to perform further manual investigation to determine whether the conditional statement here will indeed not return an error.
[0110] When it is impossible to determine whether the node to be checked is the target node, the embodiment of the present application outputs an alarm log to prompt the operation and maintenance personnel to perform further manual investigation, thereby improving the accuracy of identifying codes that fail to throw exceptions normally.
[0111] In an optional embodiment of the present application, the obtaining of the code to be checked includes:
[0112] Traversing the directory to be checked; the directory to be checked includes code files, the code files have corresponding file name suffixes, and the code files include codes;
[0113] If the file name suffix of the code file is a preset suffix, the code in the code file is used as the code to be checked.
[0114] In this embodiment, since a method for locating a target code shown in the embodiment of the present application can be applied to checking a preset code, before step 101, the directory to be checked can be traversed to obtain files in the directory to be checked with a preset suffix as the file name suffix, and these files contain the code to be checked.
[0115] In one embodiment, the preset code may be a Go language code, and the preset suffix may be “.go”.
[0116] The embodiments of the present application can realize the rapid identification of the code in the golang code that fails to throw an exception normally.
[0117] In an embodiment of the present application, the code to be checked is obtained; based on the code to be checked, an abstract syntax tree is generated; the abstract syntax tree includes nodes; the nodes of the abstract syntax tree are checked using preset checking rules to obtain target nodes; based on the target nodes, the target code in the code to be checked is located. The embodiment of the present application performs abstract processing on the code that does not throw exceptions normally, and uses tools rather than manual troubleshooting. The embodiment of the present application abstracts the reasons why the exception cannot be obtained normally at the application level from the code level into code patterns that fail to throw exceptions normally. Through AST parsing technology, these written code exception patterns are structured and converted into models that can be understood by the program. Subsequently, by parsing the code into AST and performing in-depth analysis on it, the code that fails to throw exceptions normally can be quickly identified, thereby avoiding the consequences of such code being difficult to maintain after going online.
[0118] In order to enable those skilled in the art to further understand the technical solution of the present application, the complete process of an embodiment of a method for locating a target code of the present application is described below.
[0119] Reference Figure 2 , showing a flow chart of an embodiment of a method for locating a target code of the present application.
[0120] Step 201, traverse all files in the folder.
[0121] Step 202: If the current directory is a vendor directory (a way to manage project dependencies), skip the directory; if it is not a vendor directory, jump to step 203.
[0122] Step 203, traverse the nodes of the AST of a single file. If the traversal of all the nodes of the AST of the file is completed, jump to step 201.
[0123] Step 204: If the node is a function and the return value is empty or does not contain an error type return value, jump to step 203; otherwise, jump to step 205.
[0124] Step 205 , determine whether the node is an if statement, if not, jump to step 203 ; if yes, jump to step 206 .
[0125] Step 206, determine whether the if statement meets the requirement that the left value is error, the operator is != and the right value is nil. If not, jump to step 203; if yes, jump to step 207.
[0126] Step 207 , determine whether there is a return statement in the code block to be executed when the if condition is met in the if statement, if not, jump to step 203 ; if yes, jump to step 208 .
[0127] Step 208 , determine whether the return statement contains an Errorf call or an err variable, if not, jump to step 209 ; if yes, the code corresponding to the node is the target code, jump to step 203 .
[0128] Step 209: Print out the content and position of the current return statement for the operation and maintenance personnel to check, and jump to step 203.
[0129] In an embodiment of the present application, the code to be checked is obtained; based on the code to be checked, an abstract syntax tree is generated; the abstract syntax tree includes nodes; the nodes of the abstract syntax tree are checked using preset checking rules to obtain target nodes; based on the target nodes, the target code in the code to be checked is located. The embodiment of the present application performs abstract processing on the code that does not throw exceptions normally, and uses tools rather than manual troubleshooting. The embodiment of the present application abstracts the reasons why the exception cannot be obtained normally at the application level from the code level into code patterns that fail to throw exceptions normally. Through AST parsing technology, these written code exception patterns are structured and converted into models that can be understood by the program. Subsequently, by parsing the code into AST and performing in-depth analysis on it, the code that fails to throw exceptions normally can be quickly identified, thereby avoiding the consequences of such code being difficult to maintain after going online.
[0130] It should be noted that, for the method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the embodiments of the present application are not limited by the described order of actions, because according to the embodiments of the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present application.
[0131] On the basis of the above embodiments, this embodiment further provides a device for locating a target code, which is applied to electronic devices such as terminal devices and servers.
[0132] Reference Figure 3 , shows a structural block diagram of an embodiment of a positioning device for a target code of the present application, which may specifically include the following modules:
[0133] The code to be checked obtaining module 301 is used to obtain the code to be checked;
[0134] A syntax tree generation module 302, configured to generate an abstract syntax tree according to the code to be checked; the abstract syntax tree includes nodes;
[0135] A node checking module 303 is used to check the nodes of the abstract syntax tree using a preset checking rule to obtain a target node;
[0136] The target code locating module 304 is used to locate the target code in the code to be checked according to the target node.
[0137] In an optional embodiment of the present application, the node checking module 303 includes:
[0138] A function checking submodule, used to check whether the node is a function;
[0139] A return value checking submodule, used for determining whether the function has a return value when the node is the function;
[0140] A submodule for determining a node to be checked, used for taking a node corresponding to the function as a node to be checked when the function has a return value;
[0141] The node to be checked checking submodule is used to check the node to be checked to obtain the target node.
[0142] In an optional embodiment of the present application, the to-be-checked node checking submodule is further used to:
[0143] Check whether the node to be checked is a conditional statement;
[0144] When the node to be checked is the conditional statement, determining whether the conditional statement is a binary expression; the binary expression includes an operation element and an operator;
[0145] When the conditional statement is the binary expression, determining whether the operation element and the operator are preset operation elements and preset operators;
[0146] When the operation element and the operator are the preset operation element and the preset operator, parsing the conditional statement to obtain a parsing result;
[0147] According to the parsing result, it is determined whether the node to be checked is the target node.
[0148] In an optional embodiment of the present application, the conditional statement includes an execution code when the condition is true, and judging whether the node to be checked is the target node according to the parsing result includes:
[0149] Checking whether the execution code of the conditional statement of the node to be checked includes a return statement;
[0150] When the execution code includes the return statement, determining the return content of the return statement;
[0151] When the returned content is preset content, the node to be checked is used as the target node.
[0152] In an optional embodiment of the present application, judging whether the node to be checked is the target node according to the parsing result further includes:
[0153] When the returned content is not a preset content, checking whether the returned statement is a preset statement;
[0154] When the return statement is the preset statement, the node to be checked is used as the target node.
[0155] In an optional embodiment of the present application, the device further includes:
[0156] The log output module is used to output an alarm log when the node to be checked is the conditional statement and it is impossible to determine whether the node to be checked is the target node; the alarm log includes the conditional statement.
[0157] In an optional embodiment of the present application, the to-be-checked code acquisition module 301 includes:
[0158] A directory traversal submodule, used to traverse a directory to be checked; the directory to be checked includes a code file, the code file has a corresponding file name suffix, and the code file includes code;
[0159] The code file acquisition submodule is used to use the code in the code file as the code to be checked if the file name suffix of the code file is a preset suffix.
[0160] In an embodiment of the present application, the code to be checked is obtained; based on the code to be checked, an abstract syntax tree is generated; the abstract syntax tree includes nodes; the nodes of the abstract syntax tree are checked using preset checking rules to obtain target nodes; based on the target nodes, the target code in the code to be checked is located. The embodiment of the present application performs abstract processing on the code that does not throw exceptions normally, and uses tools rather than manual troubleshooting. The embodiment of the present application abstracts the reasons why the exception cannot be obtained normally at the application level from the code level into code patterns that fail to throw exceptions normally. Through AST parsing technology, these written code exception patterns are structured and converted into models that can be understood by the program. Subsequently, by parsing the code into AST and performing in-depth analysis on it, the code that fails to throw exceptions normally can be quickly identified, thereby avoiding the consequences of such code being difficult to maintain after going online.
[0161] The embodiment of the present application also provides a non-volatile readable storage medium, which stores one or more modules (programs). When the one or more modules are applied to a device, the device can execute instructions (instructions) of each method step in the embodiment of the present application.
[0162] The present application embodiment provides one or more machine-readable media on which instructions are stored, and when executed by one or more processors, an electronic device executes one or more of the methods described in the above embodiments. In the present application embodiment, the electronic device includes various types of devices such as terminal devices and servers (clusters).
[0163] The embodiments of the present disclosure may be implemented as a device configured as desired using any appropriate hardware, firmware, software, or any combination thereof, and the device may include electronic devices such as terminal devices and servers (clusters). Figure 4 An exemplary apparatus 400 that may be used to implement various embodiments described in this application is schematically illustrated.
[0164] For one embodiment, Figure 4 An exemplary apparatus 400 is shown having one or more processors 402, a control module (chip set) 404 coupled to at least one of the processor(s) 402, a memory 406 coupled to the control module 404, a non-volatile memory (NVM) / storage device 408 coupled to the control module 404, one or more input / output devices 410 coupled to the control module 404, and a network interface 412 coupled to the control module 404.
[0165] The processor 402 may include one or more single-core or multi-core processors, and the processor 402 may include any combination of general-purpose processors or special-purpose processors (such as graphics processors, application processors, baseband processors, etc.). In some embodiments, the device 400 can be used as a terminal device, server (cluster), etc. described in the embodiments of the present application.
[0166] In some embodiments, the apparatus 400 may include one or more computer-readable media (e.g., memory 406 or NVM / storage device 408) having instructions 414 and one or more processors 402 combined with the one or more computer-readable media and configured to execute the instructions 414 to implement a module to perform the actions described in the present disclosure.
[0167] For one embodiment, control module 404 may include any suitable interface controller to provide any suitable interface to at least one of processor(s) 402 and / or any suitable device or component in communication with control module 404 .
[0168] The control module 404 may include a memory controller module to provide an interface to the memory 406. The memory controller module may be a hardware module, a software module, and / or a firmware module.
[0169] The memory 406 may be used, for example, to load and store data and / or instructions 414 for the device 400. For one embodiment, the memory 406 may include any suitable volatile memory, such as a suitable DRAM. In some embodiments, the memory 406 may include a double data rate type four synchronous dynamic random access memory (DDR4 SDRAM).
[0170] For one embodiment, control module 404 may include one or more input / output controllers to provide interfaces to NVM / storage device 408 and input / output device(s) 410 .
[0171] For example, NVM / storage 408 may be used to store data and / or instructions 414. NVM / storage 408 may include any suitable non-volatile memory (e.g., flash memory) and / or may include any suitable non-volatile storage device(s) (e.g., one or more hard disk drives (HDDs), one or more compact disk (CD) drives, and / or one or more digital versatile disk (DVD) drives).
[0172] NVM / storage device 408 may include storage resources that are physically part of the device on which apparatus 400 is installed, or it may be accessible to the device without being part of the device. For example, NVM / storage device 408 may be accessed via input / output device(s) 410 over a network.
[0173] (One or more) input / output devices 410 may provide an interface for the apparatus 400 to communicate with any other appropriate device, and the input / output device 410 may include a communication component, an audio component, a sensor component, etc. The network interface 412 may provide an interface for the apparatus 400 to communicate through one or more networks, and the apparatus 400 may wirelessly communicate with one or more components of a wireless network according to any of one or more wireless network standards and / or protocols, for example, accessing a wireless network based on a communication standard, such as WiFi, 2G, 3G, 4G, 5G, etc., or a combination thereof for wireless communication.
[0174] For one embodiment, at least one of the processor(s) 402 may be packaged together with the logic of one or more controllers (e.g., a memory controller module) of the control module 404. For one embodiment, at least one of the processor(s) 402 may be packaged together with the logic of one or more controllers of the control module 404 to form a system-in-package (SiP). For one embodiment, at least one of the processor(s) 402 may be integrated on the same die with the logic of one or more controllers of the control module 404. For one embodiment, at least one of the processor(s) 402 may be integrated on the same die with the logic of one or more controllers of the control module 404 to form a system-on-chip (SoC).
[0175] In various embodiments, the device 400 may be, but is not limited to, a terminal device such as a server, a desktop computing device, or a mobile computing device (e.g., a laptop computing device, a handheld computing device, a tablet computer, a netbook, etc.). In various embodiments, the device 400 may have more or fewer components and / or different architectures. For example, in some embodiments, the device 400 includes one or more cameras, a keyboard, a liquid crystal display (LCD) screen (including a touch screen display), a non-volatile memory port, multiple antennas, a graphics chip, an application-specific integrated circuit (ASIC), and a speaker.
[0176] Among them, the main control chip can be used as a processor or control module in the detection device, sensor data, location information, etc. are stored in a memory or NVM / storage device, the sensor group can be used as an input / output device, and the communication interface may include a network interface.
[0177] As for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0178] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0179] The embodiments of the present application are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable target code-based terminal device to produce a machine, so that the instructions executed by the processor of the computer or other programmable target code-based terminal device generate instructions for implementing the processes in the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0180] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable target code location terminal device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0181] These computer program instructions can also be loaded onto a computer or other programmable object code location terminal device, so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, so that the instructions executed on the computer or other programmable terminal device provide for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0182] Although the preferred embodiments of the present application have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present application.
[0183] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the elements.
[0184] The above is a detailed introduction to a method and device for locating a target code, an electronic device and a storage medium provided by the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea; at the same time, for a person skilled in the art, according to the idea of the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A method for locating a target code, characterized in that: The method comprises: Get the code to be checked; Generate an abstract syntax tree according to the code to be checked; the abstract syntax tree includes nodes; Using a preset checking rule to check the nodes of the abstract syntax tree to obtain a target node; The target code in the code to be checked is located according to the target node.
2. The method according to claim 1, characterized in that The step of checking the nodes of the abstract syntax tree using a preset checking rule to obtain a target node includes: Checking whether the node is a function; When the node is the function, determining whether the function has a return value; When the function has a return value, the node corresponding to the function is used as the node to be checked; The node to be checked is checked to obtain the target node.
3. The method according to claim 2, characterized in that The step of checking the node to be checked to obtain the target node includes: Check whether the node to be checked is a conditional statement; When the node to be checked is the conditional statement, determining whether the conditional statement is a binary expression; the binary expression includes an operation element and an operator; When the conditional statement is the binary expression, determining whether the operation element and the operator are preset operation elements and preset operators; When the operation element and the operator are the preset operation element and the preset operator, parsing the conditional statement to obtain a parsing result; According to the parsing result, it is determined whether the node to be checked is the target node.
4. The method according to claim 3, characterized in that The conditional statement includes an execution code when the condition is true, and judging whether the node to be checked is the target node according to the parsing result includes: Checking whether the execution code of the conditional statement of the node to be checked includes a return statement; When the execution code includes the return statement, determining the return content of the return statement; When the returned content is preset content, the node to be checked is used as the target node.
5. The method according to claim 4, characterized in that The method further comprises: When the returned content is not a preset content, checking whether the returned statement is a preset statement; When the return statement is the preset statement, the node to be checked is used as the target node.
6. The method according to claim 3, characterized in that: The method further comprises: When the node to be checked is the conditional statement, and it is impossible to determine whether the node to be checked is the target node, an alarm log is output; and the alarm log includes the conditional statement.
7. The method according to claim 1, characterized in that The obtaining of the code to be checked includes: Traversing the directory to be checked; the directory to be checked includes code files, the code files have corresponding file name suffixes, and the code files include codes; If the file name suffix of the code file is a preset suffix, the code in the code file is used as the code to be checked.
8. A device for locating a target code, characterized in that: The device comprises: A module for obtaining codes to be checked, used for obtaining codes to be checked; A syntax tree generation module, used to generate an abstract syntax tree according to the code to be checked; the abstract syntax tree includes nodes; A node checking module, used to check the nodes of the abstract syntax tree using preset checking rules to obtain a target node; The target code locating module is used to locate the target code in the code to be checked according to the target node.
9. An electronic device, characterized in that: include: processor; and A memory having executable codes stored thereon, which, when executed, causes the processor to execute the target code positioning method as claimed in one or more of claims 1-7.
10. One or more machine-readable media having executable codes stored thereon, which, when executed, cause a processor to execute the target code positioning method according to one or more of claims 1-7.