A code scanning method, a code migration method and related devices

By scanning the C/C++ source code, generating intermediate result files, recording macro definitions and conditional compilation information, the problem of insufficient identification of CPU architecture-related information in code migration is solved, and the accuracy and efficiency of migration is improved.

CN114647439BActive Publication Date: 2024-12-20HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202011524498.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-12-21
Publication Date
2024-12-20
Estimated Expiration
2040-12-21

AI Technical Summary

Technical Problem

The existing technology cannot accurately identify and record macro definitions and conditional compilation code blocks related to CPU architectures during the C/C++ code migration process, resulting in increased difficulty in code migration and cannot meet the migration requirements between different CPU architectures.

Method used

By scanning the source code, generating intermediate result files, recording macro definition list, macro reference context information and conditional compilation code block information, providing complete macro-related information, and guiding code migration tools to accurately port code.

Benefits of technology

It improves the accuracy and efficiency of code migration, ensures the smooth migration of C/C++ code between different CPU architectures, reduces the amount of migration data, and provides a detailed code migration report.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114647439B_ABST
    Figure CN114647439B_ABST
Patent Text Reader

Abstract

The embodiment of the present application provides a code scanning method, a code migration method and related devices. The method can scan the source code and accurately identify all valid macros and conditional compilation code blocks, efficiently identify the code segments that need to be migrated and give accurate code migration suggestions.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of computer technology, and in particular, to a code scanning method, a code migration method, and related devices. Background Art

[0002] Code migration refers to moving programming code from one system to another. Programming code refers to program code written in a programming language (such as C, C++, Java, Python, etc.).

[0003] Currently, C / C++ codes generally include system macros and conditional compilation related to the central processing unit (CPU) architecture. During the code migration process, these CPU architecture-related macros increase the difficulty of code migration between different CPU architectures.

[0004] Specifically, the existing standard C / C++ preprocessor will directly expand the macro definition during the processing of C / C++ source code, select only valid code block information for conditional compilation, and finally output the preprocessed intermediate file. The intermediate file obtained in this way cannot provide complete valid macro list information and conditional compilation code block information, resulting in the intermediate file being unable to meet the migration requirements of C / C++ code between different CPU architectures. Summary of the invention

[0005] The embodiments of the present application provide a code scanning method, a code migration method and related devices, which can scan the source code and accurately identify all valid macros and conditional compilation code blocks, efficiently identify the code segments that need to be migrated and give accurate code migration suggestions.

[0006] In the first aspect, an embodiment of the present application provides a method for code scanning, including: scanning source code in a source file to obtain macro definition list information, macro reference context information and conditional compilation code block information, the macro definition list information includes macro definition information of macros in the source code, the macro reference context information includes the reference position of the macro in the source file, and the conditional compilation code block information includes the macros involved in all conditional branches; generating an intermediate result file according to the macro definition list information, the macro reference context information and the conditional compilation code block information. The embodiment of the present application can record all the valid information required in the code migration tool and generate an intermediate result file during the C / C++ source code preprocessing process, which is used to guide the source code scanning of the subsequent code migration tool.

[0007] In conjunction with the first aspect, in an implementation of an embodiment of the present application, scanning the source code in the source file includes: if the source file includes a header file, expanding the header file and scanning to obtain macro definition list information, macro reference context information, and conditional compilation code block information corresponding to the header file. In this implementation, the method can expand the included header file and identify valid macro information.

[0008] In combination with the first aspect, in an implementation of the embodiment of the present application, in combination with the first aspect, in an implementation of the embodiment of the present application, the source code in the source file is scanned to obtain macro definition list information, macro reference context information and conditional compilation code block information including: if the text of the scanned source code includes macros, the macro definition information of the macro is recorded in the macro definition list information; if the macro in the scanned source code text is referenced, the corresponding line number of the reference position and the corresponding file identifier are recorded in the macro reference context information; if the conditional compilation code block in the text of the source code is scanned, the code involved in all conditional branches in the conditional compilation code block is recorded in the conditional compilation code block information. In this implementation, the macro definition list information, macro reference context information and conditional compilation code block information included in the file parse all macro-related information required for code migration, so that the code migration tool can give accurate code migration suggestions based on the complete macro information.

[0009] In combination with the first aspect, in an implementation of the embodiment of the present application, before scanning the source code in the source file, the method further includes: obtaining a preset macro definition and a whitelist macro information list related to the central processing unit CPU architecture. In this implementation, the code migration tool can find the macro definition related to the CPU architecture used after the code is migrated, so the code migration tool only needs to migrate the macro related to the CPU architecture, which greatly reduces the amount of data to be migrated and improves the efficiency of code migration.

[0010] In the second aspect, the embodiment of the present application provides a method for code migration, including: obtaining an intermediate result file obtained by the code scanning method of the first aspect and a whitelist macro information list related to a preset central processing unit CPU architecture; parsing the intermediate result file to obtain macro definition list information, macro reference context information and conditional compilation code block information; according to the whitelist macro information list, selecting macro definition information related to the preset CPU architecture from the macro definition list information to form effective macro list information; parsing the conditional compilation code block information to obtain the macros involved in each conditional branch; selecting macros matching the effective macro list information from the macros involved in each conditional branch; locating the first source code position according to the macro reference context information and the macro matching the effective macro list information; locating the first source code position after migrating the code or generating a transplantation suggestion corresponding to the first source code position. The embodiment of the present application can record all the effective information required in the code migration tool and generate an intermediate result file during the C / C++ source code preprocessing process. According to the macro definition list information, macro reference context information and conditional compilation code block information in the intermediate result file, the code migration tool can parse all the macro-related information required for code migration, so that the code migration tool can give accurate code transplantation suggestions based on the complete macro information.

[0011] In conjunction with the second aspect, in an implementation of the embodiment of the present application, the method further includes: scanning the code between the code segments corresponding to the conditional compilation code block information, and confirming whether code migration is required according to preset rules. In this implementation, for the code between each macro conditional code block, the code migration tool uses the original processing logic to scan line by line, and it is necessary to confirm whether code migration is required, so as not to miss all macros in the source code.

[0012] In conjunction with the second aspect, in an implementation of the embodiment of the present application, confirming whether code migration is required according to preset rules includes: selecting macros that match valid macro list information from macros in the code between code segments; locating the second source code location and migrating the code or generating a migration suggestion corresponding to the second source code location based on the macro reference context information and the macros that match the valid macro list information. In this implementation, the code migration tool matches the relevant macros according to the valid macro list information, and can find the macro definitions related to the CPU architecture used after the code is migrated. Therefore, the code migration tool only needs to migrate the macros related to the CPU architecture, which greatly reduces the amount of data to be migrated and improves the efficiency of code migration.

[0013] In conjunction with the second aspect, in an implementation of the embodiment of the present application, the method further includes: generating a code migration report based on the scan processing results of all source codes. In this implementation, the code migration tool can scan the source codes of multiple source files through the intermediate result file, and after the scanning is completed, generate a code migration report for the user to download and guide the completion of code migration.

[0014] In the third aspect, the embodiment of the present application provides a device for code scanning, including: an acquisition module for acquiring a source file; a processing module, the processing module user scans the source code in the source file to obtain macro definition list information, macro reference context information and conditional compilation code block information, the macro definition list information includes the macro definition information of the macro in the source code, the macro reference context information includes the reference position of the macro in the source file, and the conditional compilation code block information includes the macros involved in all conditional branches; the processing module is also used to generate an intermediate result file based on the macro definition list information, the macro reference context information and the conditional compilation code block information. The technical effect of the device of the third aspect of the present application can be understood with reference to the relevant effects of the method part of the first aspect above, and will not be repeated here.

[0015] In combination with the third aspect, in one implementation method of the embodiment of the present application, the processing module is used to: if the source file contains a header file, expand the header file and scan it to obtain the macro definition list information, macro reference context information and conditional compilation code block information corresponding to the header file.

[0016] In combination with the third aspect, in an implementation method of an embodiment of the present application, the processing module is used to: if the text of the scanned source code includes macros, the macro definition information of the macro is recorded in the macro definition list information; if the macro in the scanned source code text is referenced, the corresponding line number of the reference position and the corresponding file identifier are recorded in the macro reference context information; if a conditional compilation code block is scanned in the text of the source code, the code involved in all conditional branches in the conditional compilation code block is recorded in the conditional compilation code block information.

[0017] In combination with the third aspect, in one implementation of an embodiment of the present application, the acquisition module is used to: obtain a preset macro definition and a whitelist macro information list related to a central processing unit CPU architecture.

[0018] In the fourth aspect, the embodiment of the present application provides a device for code migration, including: an acquisition module, used to obtain the intermediate result file obtained by the code scanning device of the third aspect and the whitelist macro information list related to the preset central processing unit CPU architecture; a processing module, used to parse the intermediate result file, obtain macro definition list information, macro reference context information and conditional compilation code block information; the processing module is also used to select macro definition information related to the preset CPU architecture from the macro definition list information according to the whitelist macro information list to form effective macro list information; the processing module is also used to parse the conditional compilation code block information to obtain the macros involved in each conditional branch; the processing module is also used to select macros matching the effective macro list information from the macros involved in each conditional branch; the processing module is also used to locate the first source code position according to the macro reference context information and the macro matching the effective macro list information; the processing module is also used to migrate the code after locating the first source code position or generate a transplantation suggestion corresponding to the first source code position. The technical effect of the device of the fourth aspect of the present application can be understood with reference to the relevant effects of the method part of the second aspect above, and will not be repeated here.

[0019] In conjunction with the fourth aspect, in one implementation of an embodiment of the present application, the processing module is used to: scan the code between the code segments corresponding to the conditional compilation code block information, and confirm whether code migration is required according to preset rules.

[0020] In combination with the fourth aspect, in an implementation method of an embodiment of the present application, the processing module is used to: select macros that match the valid macro list information from the macros in the code between the code segments; locate the second source code position and migrate the code or generate a migration suggestion corresponding to the second source code position based on the macro reference context information and the macro that matches the valid macro list information.

[0021] In conjunction with the fourth aspect, in an implementation of an embodiment of the present application, the processing module is used to: generate a code migration report based on the scanning processing results of all source codes.

[0022] In a fifth aspect, an embodiment of the present application provides a code processing device, comprising: one or more central processing units, a memory, a communication bus, and a communication interface; the memory is a short-term storage memory or a persistent storage memory, and a program code is stored in the memory; the communication interface is used to send and receive data; the central processing unit communicates with the memory via the communication bus, and executes the program code in the memory on the code processing device to execute the method of the first aspect or the second aspect. The technical effect of the device in the fifth aspect of the present application can be understood by referring to the relevant effects of the method part of the first aspect or the second aspect above, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] Figure 1A schematic diagram of a code scanning method provided in an embodiment of the present application;

[0024] Figure 2 A schematic diagram of a logic processing unit provided in an embodiment of the present application;

[0025] Figure 3 A flowchart of a code migration method provided in an embodiment of the present application;

[0026] Figure 4 An example diagram of a code migration tool provided in an embodiment of the present application;

[0027] Figure 5 A schematic diagram of a code scanning device provided in an embodiment of the present application;

[0028] Figure 6 A schematic diagram of a code migration device provided in an embodiment of the present application;

[0029] Figure 7 A schematic diagram of a code processing device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0030] The embodiments of the present application provide a code scanning method, a code migration method and related devices, which can scan the source code and accurately identify all valid macros and conditional compilation code blocks, efficiently identify the code segments that need to be migrated and give accurate code migration suggestions.

[0031] The terms "first", "second", "third", "fourth", etc. (if any) in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "corresponding to" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0032] In the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.

[0033] In order to make the description of the following embodiments clear and concise, a brief introduction to the related technology is first given:

[0034] Macro Definition: A term for batch processing. In computer science, a macro is an abstraction that replaces certain text patterns according to a set of predefined rules.

[0035] Macro Substitution: When a macro definition is used, the text information replaced by the macro identifier is used to replace it. If there are other macro identifiers in the text information replaced by the macro identifier, macro expansion and replacement need to be performed recursively until macro expansion cannot be performed anymore.

[0036] Conditional Compilation: Conditional compilation is a compilation implementation that allows the compiler to generate different executable programs controlled by parameters provided during compilation.

[0037] To explain the above related technologies, this application provides a source code as follows:

[0038]

[0039]

[0040] These 12 lines of source code are stored in the source file "MacroTest.c". The first and second lines of code may refer to macro definition information, indicating the macro definition used in the source file for subsequent code use. Lines 7 to 11 are conditional compilation code segments. When these lines of code are executed, they first check whether the condition of line 7 is met. If so, the code of line 8 is executed. If not, the code of line 10 is executed.

[0041] C / C++ code contains system macros and conditional compilation related to CPU architectures, which increase the difficulty of code migration between different CPU architectures. Manually identifying the code segments that need to be migrated is inefficient. In order to efficiently identify the code segments that need to be migrated and give accurate code migration suggestions, it is necessary to scan the source code and accurately identify all valid macros and conditional compilation code blocks.

[0042] The existing standard C / C++ preprocessor will directly expand the macro definition when processing the C / C++ source code, and only select the valid code block information for conditional compilation, and finally output the preprocessed intermediate file. It cannot provide complete valid macro list information and conditional compilation code block information. Without this complete information, it cannot meet the migration requirements of C / C++ code between different CPU architectures.

[0043] Specifically, first, the existing standard C / C++ preprocessor recognizes different macros and conditional compilation valid code segments under different CPU architecture platforms, and finally generates different intermediate result files, which cannot guarantee the consistency of the results. In addition, for example, the GCC standard compiler requires the platform to support a complete compilation environment, otherwise the platform cannot support the operation of the compiler.

[0044] Second, the macro information, macro replacement information, and conditional compilation information scanned during the preprocessing process are not recorded. After the preprocessing is completed, the effective macro list information and conditional code segment information required for C / C++ code migration cannot be provided. The code snippets that need to be migrated and the code migration suggestions cannot be accurately identified, which will ultimately affect the accuracy and efficiency of the entire code migration.

[0045] Third, the currently implemented C / C++ code migration tool performs source code scanning based on whitelist or regular matching, and cannot accurately identify valid macro definition information (such as macros contained in header files, user-defined macros, etc.), which will affect the selection of the correct conditional compilation code blocks and ultimately affect the accuracy of the final code migration.

[0046] To solve the above problems, the embodiments of the present application provide a code scanning method, a code migration method and related devices, which can scan the source code and accurately identify all valid macros and conditional compilation code blocks, efficiently identify the code segments that need to be migrated and give accurate code migration suggestions.

[0047] Aiming at the problem that the preprocessor in the existing scheme does not record key information: the existing C / C++ preprocessor mainly selects valid code blocks through text replacement of macro definitions, macro expansion, and conditional compilation, and finally generates a valid intermediate code file, but does not record the macro definitions and conditional compilation code blocks scanned in each source file and header file during the preprocessing process. This information is exactly the key information required for C / C++ code migration between different platforms. Only by accurately obtaining this valid information can the code migration tool accurately provide the code location information required for code migration. The method provided in the embodiment of the present application can record all the valid information required in the code migration tool during the C / C++ source code preprocessing process and generate an intermediate result file for guiding the source code scanning of the subsequent code migration tool.

[0048] Therefore, an embodiment of the present application provides a code scanning method, which is executed by a preprocessor (or code scanning tool) to generate an intermediate result file that can record key information. Figure 1 A schematic diagram of a code scanning method provided in an embodiment of the present application. The process of the method includes:

[0049] 101. Scan the source code in the source file to obtain macro definition list information, macro reference context information and conditional compilation code block information;

[0050] In an embodiment of the present application, the macro definition list information includes the macro definition information in the source code. If the preprocessor scans that the text of the source code includes a macro definition, the macro definition is recorded in the macro definition list information. Exemplarily, the first line of code and the second line of code in the aforementioned source file "MacroTest.c" are two macro definition information. The preprocessor scans and recognizes that the first line of code and the second line of code are two macro definition information, which can be recorded in the macro definition list information.

[0051] As an example, the macro definition list information of the source file "MacroTest.c" provided in the embodiment of the present application is as follows:

[0052]

[0053] Among them, macroDefines indicates that the information is macro definition list information, macroName refers to the name of the macro of the macro definition, replaceText is the replacement content of the macro definition (which can be called macro replacement information), lineNum refers to the number of lines (i.e., position) of the macro definition in the source code of the source file, macroTrace refers to the macro tracing of the macro definition, and portable refers to whether the macro definition is portable. In actual applications, if the source code may also include richer other information, the preprocessor can record them one by one. Alternatively, according to actual needs, the preprocessor can record the specified macro definition information, which is not limited in the embodiments of the present application.

[0054] It can be understood that in the embodiment of the present application, the type of macro definition information can be a common standard macro definition or a user-defined macro, and the embodiment of the present application does not limit this.

[0055] In an embodiment of the present application, the macro reference context information includes the location (source code location) where the macro definition is used in the source file. If the preprocessor scans a reference to the macro definition list information in the text of the source code, the line number where the reference is located and the corresponding file identifier are recorded in the macro reference context information. Exemplarily, in the aforementioned source file "MacroTest.c", the eighth line uses the macro definition "CONDITION_ENABLE", and the tenth line uses the macro definition "CONDITION_DISABLE". After the preprocessor scans and identifies this situation, it can be recorded in the macro reference context information.

[0056] As an example, the macro reference context information of the source file "MacroTest.c" provided by the embodiment of the present application is as follows:

[0057]

[0058] Among them, macroRef indicates that the information is macro reference context information, and lineNum indicates the location (number of lines) where the macro definition is used. Other information is similar to the aforementioned macro definition list information and will not be repeated here.

[0059] In an embodiment of the present application, the conditional compilation code block information includes the macro definition information involved in all conditional branches. If the preprocessor scans the conditional compilation code block in the text of the source code, the code involved in all conditional branches in the conditional compilation code block is recorded in the conditional compilation code block information. Exemplarily, the seventh to eleventh lines of the source code in the aforementioned source file "MacroTest.c" are conditional compilation code blocks, and the preprocessor can record all the macro definition information contained therein.

[0060] As an example, the conditional compilation code block information of the source file "MacroTest.c" provided in the embodiment of the present application is as follows:

[0061]

[0062]

[0063] Among them, conditionCompilation indicates that the information is conditional compilation code block information, startLineNum indicates the starting line number of the conditional compilation code block, endLineNum indicates the ending line number of the conditional compilation code block, conditionType indicates the type format of the conditional compilation code block, conditions indicates the specific content of the conditional compilation code block, conditionName indicates the condition name, level indicates the level of the macro used in the conditional compilation code block, conditionText indicates the specific content of the condition, conditionValue indicates the value satisfied by the condition, macroList indicates the macro information in the conditional compilation code block, macroName is the name of the macro definition, and macroText is the content of the macro definition.

[0064] 102. Generate an intermediate result file according to the macro definition list information, the macro reference context information and the conditional compilation code block information.

[0065] In an embodiment of the present application, after the preprocessor scans the source code of the source file and records the macro definition list information, macro reference context information and conditional compilation code block information, the information can be merged to generate an intermediate result file. Exemplarily, as an example, the content of the intermediate result file of the source file "MacroTest.c" provided in the embodiment of the present application is:

[0066]

[0067]

[0068] Among them, JSON indicates that the file type of the intermediate result file is JSON. In practical applications, other types of files (such as YAML or user-defined files) can also be used, and this embodiment of the application does not limit this. fileName indicates which source file the following information belongs to. If the preprocessor scans multiple source files, the information recorded in the multiple source files can be saved in one intermediate result file.

[0069] It can be seen from the scanning process of the source code of the above source file that the embodiment of the present application can record all macro definition lists (including user-defined macros) information by scanning the C / C++ source code, including macro definition values, effective scope ranges, and the positions of macro references in the source code. The embodiment of the present application identifies all conditional compilation code blocks by scanning the C / C++ source code, calculates the truth or falsehood of the values ​​of each branch, and analyzes and records the macro lists used by each branch and the values ​​of each macro definition, and finally saves the starting position of the conditional compilation code block, the nested hierarchical relationship, and the macro list information involved in each level, and saves the structured data into an intermediate file (such as Json, Yaml or user-defined file), and as the input of the subsequent code migration tool, the code migration tool can accurately locate the code position that needs to be migrated and inserted.

[0070] In some embodiments, if the preprocessor identifies that some files are header files when scanning source files, the header files are expanded and scanned to obtain macro definition list information, macro reference context information, and conditional compilation code block information corresponding to the header files. Specifically, the preprocessor can identify whether the file is an ordinary source file or a header file by the file suffix. Exemplarily, if the file suffix is ​​".h", then the file is a header file, and if the file suffix is ​​".cpp", then the file is a source file. After the preprocessor identifies the header file, the header file can be expanded and scanned to obtain the macro definition list information, macro reference context information, and conditional compilation code block information corresponding to the header file. After the header file is expanded, the preprocessor scans it in a similar manner to the aforementioned method of scanning the source file, which will not be repeated here.

[0071] In some embodiments, before step 101, the preprocessor may also obtain preset macro definitions and a whitelist macro information list related to the CPU architecture. Wherein, obtaining the preset macro definitions means that the preprocessor may obtain the macro definitions in the user command line from the cmake or Make build tool. The whitelist macro information list related to the CPU architecture may be read from a preset database.

[0072] Based on the above Figure 1 The method shown in the embodiment of the present application provides a logic processing unit for scanning the source code of the source file and generating an intermediate result file. In some embodiments, the logic processing unit can also be called a preprocessor or a code scanning tool, which is not limited in the embodiment of the present application. Figure 2 Schematic diagram of a logic processing unit provided in an embodiment of the present application. The logic processing unit first obtains a C / C++ source file. If a header file is included in the source file, the logic processing unit expands and scans the header file. For source files that are not header files, the logic processing unit can directly scan their source code. The logic processing unit scans the source file and the expanded header file in the same manner as described above. Figure 1 The corresponding methods of scanning source files and expanded header files in each embodiment are similar and will not be described here. After the logic processing unit scans the source files and expanded header files, it can obtain relevant macro definition list information, macro reference context information, and conditional compilation code block information. After the logic processing unit completes scanning of all source files and header files, the logic processing unit can package this information into an intermediate result file.

[0073] The code scanning method provided in the embodiment of the present application can scan the source code in the source file, obtain macro definition list information, macro reference context information and conditional compilation code block information, and generate a corresponding intermediate result file, the macro definition list information, macro reference context information and conditional compilation code block information included in the file parse all macro-related information required for code migration, so that the code migration tool can give accurate code migration suggestions based on the complete macro information. The code migration tool provides code migration guidance based on the macro information of the intermediate result file, which can improve the accuracy and efficiency of code migration.

[0074] Specifically, the code migration tool can execute the code migration method provided in the embodiments of the present application. Figure 3 A flowchart of a code migration method provided in an embodiment of the present application, the process includes:

[0075] 301. Obtain the intermediate result file and the whitelist macro information list related to the CPU architecture;

[0076] In the embodiment of the present application, the intermediate result file can be obtained by Figure 1The intermediate result file obtained by the methods in the corresponding embodiments may also be an intermediate result file obtained by other methods and includes macro definition list information, macro reference context information and conditional compilation code block information, which is not limited to the embodiments of the present application.

[0077] In an embodiment of the present application, the macros used in the source code of the source file may be related to the first type of CPU, or may be related to the second type of CPU. The first type of CPU and the second type of CPU here may refer to the CPU of the X86 architecture, the CPU of the ARM architecture, etc., and the embodiment of the present application does not limit this. When performing code migration, if the code migration tool can find the macro definition related to the CPU architecture used after the migration code, the code migration tool only needs to migrate the macro related to the CPU architecture, which greatly reduces the amount of data migrated and improves the efficiency of code migration. Specifically, if the source code is migrated from the X86 platform to the ARM platform, the code migration tool can obtain a whitelist macro information list related to the ARM architecture CPU.

[0078] It is understandable that, in some embodiments, the preprocessor is a module in the code migration tool, and the code migration tool can directly read the intermediate result file from the preprocessor. In other embodiments, the preprocessor and the code migration tool belong to different devices, and the code migration tool can obtain the intermediate result file from the preprocessor through data transmission.

[0079] 302. Parse the intermediate result file to obtain macro definition list information, macro reference context information, and conditional compilation code block information;

[0080] In an embodiment of the present application, the code migration tool can parse the intermediate result file to obtain macro definition list information, macro reference context information and conditional compilation code block information. For example, as the intermediate result file obtained based on the source file "MacroTest.c" mentioned above, "macroDefines" is used in the file to indicate that the information corresponding to the title is macro definition list information, "macroRef" indicates that the information corresponding to the title is macro reference context information, and "conditionCompilation" indicates that the information corresponding to the title is conditional compilation code block information. The code migration tool can read the relevant title and parse it to obtain relevant information.

[0081] 303. According to the whitelist macro information list, select macro definition information related to the preset CPU architecture from the macro definition list information to form valid macro list information;

[0082] In an embodiment of the present application, the code migration tool can select macro definition information related to the preset CPU architecture from the macro definition list information according to the whitelist macro information list to form the effective macro list information. Specifically, the code migration tool can select macro definition information that is simultaneously present in the whitelist macro information list from the macro definition list information to form the effective macro list information. Exemplarily, the macro definition list information includes macro definition information of macro A, macro B, and macro C, while the whitelist macro information list only includes macro definition information of macro B and macro C, then the code migration tool can select macro definition information of macro B and macro C from macro A, macro B, and macro C to form the effective macro list information.

[0083] 304. parse the conditional compilation code block information to obtain the macros involved in each conditional branch;

[0084] In an embodiment of the present application, the code migration tool can parse the conditional compilation code block information based on the obtained conditional compilation code block information to obtain the macros involved in each conditional branch. Specifically, the conditional compilation code block information includes the information of the macros involved in each conditional branch. Exemplarily, as in the intermediate result file obtained based on the source file "MacroTest.c" mentioned above, "[]macroList" and "macroName: "CONDITION_ENABLE"" in the intermediate result file indicate that the IF branch of the conditional compilation code block references the macro CONDITION_ENABLE. In actual applications, the code migration tool can parse the macros involved in each conditional branch based on the conditional compilation code block information in the intermediate result file.

[0085] 305. Select a macro that matches the valid macro list information from the macros involved in each conditional branch;

[0086] In an embodiment of the present application, the code migration tool can select a macro that matches the valid macro list information from the macros involved in each conditional branch. Exemplarily, the macros involved in each conditional branch include macro B, macro D, and macro E, and the macro definition information in the valid macro list information is the macro information corresponding to macro B and macro C, then the code migration tool can select macro B from macro B, macro D, and macro E as the macro that matches the valid macro list information.

[0087] 306. Locate the first source code position according to the macro reference context information and the macro matching the valid macro list information;

[0088] In an embodiment of the present application, the code migration tool can locate the first source code position based on the macro reference context information and the macro that matches the effective macro list information. Specifically, the code migration tool can search for the reference position of the macro that matches the effective macro list information from the macro reference context information, and then locate the first source code position based on the position. Exemplarily, macro B is a macro that matches the effective macro list information, and according to the macro reference context information, the reference position of macro B is in the 8th line of the source file, then the code migration tool can locate the first source code position as the 8th line.

[0089] 307. After locating the first source code location, migrate the code or generate a migration suggestion corresponding to the first source code location.

[0090] In an embodiment of the present application, the code migration tool can migrate the code after locating the first source code position. Exemplarily, after the code migration tool finds the reference of macro B in line 8 of the source file, it can insert the code related to the reference of macro B into the code migration report (and indicate that it is the reference code of macro B), and can even automatically migrate the related code.

[0091] In other embodiments, the code migration tool can generate a migration suggestion corresponding to the first source code location after locating the first source code location. For example, after the code migration tool finds the reference of macro B in line 8 of the source file, it can generate a migration suggestion of "migrate the code related to the reference of macro B in line 8 of the source file." In actual applications, the text of the migration suggestion can also have other forms, which are not limited in the embodiments of the present application.

[0092] It is understandable that, for macros in the conditional compilation code block that do not match the valid macro list information, the code migration tool can also give relevant suggestions, or insert a preset code segment. For example, if macro X is a macro in the conditional compilation code block that does not match the valid macro list information, the code migration tool can give relevant suggestions for macro C as "this macro is not related to the CPU architecture, please make a reasonable conversion", or perform code conversion according to preset rules, or insert the code corresponding to the macro into the code migration report for the user to view, which is not limited in the embodiments of the present application.

[0093] In some embodiments, the code migration tool can also scan the code between the code segments corresponding to the conditional compilation code block information, and confirm whether code migration is required according to preset rules. The code between the conditional compilation code blocks is relatively common, so the code migration tool can use the original processing logic to scan line by line, and it is necessary to confirm whether code migration is required. Exemplarily, the code migration tool can select macros that match the valid macro list information from the macros in the code between the code segments; according to the macro reference context information and the macros that match the valid macro list information, the code is migrated after locating the second source code position or the transplantation suggestion corresponding to the second source code position is generated. For example, the macros in the code between the code segments are macro X and macro Y, where macro X matches the valid macro list information, then the code migration tool can determine the macro X in the XX line of the source file (the second source code position) according to the macro reference context information, thereby migrating the code or generating the transplantation suggestion corresponding to the second source code position.

[0094] In some embodiments, the code migration tool may scan the source codes of multiple source files through an intermediate result file, and after the scanning is completed, generate a code migration report for the user to download and guide the completion of the code migration.

[0095] In view of the problem that the code migration tools of the existing solutions do not fully record information: the existing code migration tools are aimed at platform-related macro definitions, and macro replacement is completed based on whitelists or specific regular matching. It is processed on text lines, lacking specific context information for guidance. Some macro definitions are in header files and are included and introduced through header files. In addition, some macros can be defined or undefined repeatedly. These need to be judged based on the preprocessing context, whether the reference to a macro in a certain line of a certain file is valid, the value information of the macro, etc., all of which need to be fully recorded. During the preprocessing of C / C++ source code, the embodiments of the present application will collect the effective scope of each macro, the reference position of the macro in each file, the conditional compilation code block, and all macro information involved in each conditional branch.

[0096] Specifically, compared with traditional code migration tools, the code migration tool of the embodiment of the present application has the following advantages:

[0097] (1) Traditional code migration tools obtain some macros based on the whitelist of macro definitions. Without macro context information, all macro information cannot be identified, which is one of the reasons for the tool's false positives. The code migration tool of the embodiment of the present application can parse all macro context information, and the code migration tool can give accurate code migration suggestions based on the complete macro information.

[0098] (2) The recognition of macro definitions corresponding to traditional code migration tools is based on regular expression matching, and the macro definitions contained in the header files cannot be recognized, which ultimately leads to the inability to obtain the complete macro list information, which will also be one of the causes of tool false alarms. The preprocessor in the code migration tool of the embodiment of the present application can expand the included header files and recognize valid macro information.

[0099] (3) Traditional code migration tools scan conditional compilation code segments line by line based on regular matching, and are unable to see the context information of the entire conditional compilation code segment, and are unable to quickly locate the conditional compilation code block that needs to be migrated. The structured intermediate file generated by the preprocessor in the code migration tool of the embodiment of the present application can provide all the conditional compilation code block context information in the source file, can quickly locate the location of the source code scan, and improve the efficiency of code scanning and migration.

[0100] Figure 4 An example diagram of the code migration tool provided in the embodiment of the present application. The code migration tool can first obtain a list of scanned source files, which can include source file 1, source file 2, source file 3, etc. The code migration tool can scan source files one by one, or scan multiple source files at the same time, which is not limited in the embodiment of the present application. When all source files are scanned, the code migration tool can generate a scan migration report based on the scan results for the user to download and guide the completion of the code migration.

[0101] The code migration tool can then be used by Figure 2 Similar logical processing units are used to generate intermediate result files (or structured format files). The generation process is the same as the above Figure 1 The corresponding method embodiments generate intermediate result files according to the method similarly or similarly to the above Figure 2 The process of generating the intermediate result file by the logic processing unit in each corresponding embodiment is similar and will not be repeated here.

[0102] After the code migration tool obtains the intermediate result file from the logic processing unit, it can parse the macro definition list information, macro reference context information, and conditional compilation code block information. Then, the code migration tool can extract the valid macro list information related to the specified CPU architecture from the macro definition list information based on the whitelist macro information list. This process is the same as the above. Figure 3 Step 303 in the corresponding embodiments is similar and will not be described again here.

[0103] The code migration tool can scan source files according to the conditional compilation code segment. Specifically, the code migration tool can analyze whether the macro in the conditional compilation code segment supports the expected CPU architecture through the effective macro list information. If the macro in the conditional compilation code segment supports the expected CPU architecture, then according to the situation and the macro reference context information, locate the source code position that needs to be scanned, and give a transplantation suggestion. If the macro in the conditional compilation code segment does not support the expected CPU architecture, then according to the situation and the macro reference context information, insert the branch code (for example, insert the branch code corresponding to the macro into the code migration report) or generate a transplantation suggestion. For the code between each conditional code block, the code migration tool uses the original processing logic to scan line by line, and it is necessary to confirm whether code migration is needed.

[0104] After the code migration tool completes the entire source code scan, it generates a code migration report for users to download and guide them to complete the code migration.

[0105] The code migration tool provided in the embodiment of the present application can identify the valid macro information defined in the context of each file through the self-developed C / C++ scanning tool, and format it and output it to the intermediate result file. In addition, the code migration tool provided in the embodiment of the present application can identify the context information (such as line number, macro replacement content, whether CPU architecture is related, etc.) referenced by the macro definition in the source file, and format it and output it to the formatted intermediate file. The subsequent code migration tool gives accurate transplantation suggestions based on the context information of the recorded macro definition. Moreover, the code migration tool provided in the embodiment of the present application can identify the conditional compilation code block, record the type of conditional compilation, the starting line number, the nesting level and the validity of each conditional branch, and format it and output it to the intermediate file. The code migration tool can quickly locate the conditional compilation code block, and judge whether to insert the code or give a transplantation suggestion according to the recorded conditional compilation information. Further, the code migration tool provided in the embodiment of the present application can further identify the macro list information on which each branch depends. The code migration tool can judge whether the conditional compilation code segment needs to be transplanted based on the macros used by each branch. If it is not necessary, the scanning code segment can be directly skipped, which can improve the efficiency of code transplantation.

[0106] Figure 5 A schematic diagram of a code scanning device provided in an embodiment of the present application. The code scanning device 500 includes:

[0107] An acquisition module 501 is used to acquire a source file;

[0108] Processing module 502, used to execute the above Figure 1 Corresponding to step 101 and step 102 in each embodiment.

[0109] Figure 6A schematic diagram of a code migration device provided in an embodiment of the present application. The code migration device 600 includes:

[0110] Acquisition module 601, used to execute the above Figure 3 This corresponds to step 301 in each embodiment.

[0111] Processing module 602 is used to execute the above Figure 3 The corresponding steps are step 301, step 302, step 303, step 304, step 305, step 306 and step 307 in each embodiment.

[0112] Figure 7 A schematic diagram of a code processing device provided in an embodiment of the present application. The code processing device 700 includes: one or more processors 701, a memory 703 and a communication interface 704, and the processor 701, the memory 703 and the communication interface 704 can be connected via a communication bus 702. The memory 703 is used to store one or more programs; the one or more processors 701 are used to run the one or more programs, so that the code processing device 700 performs the methods corresponding to the above-mentioned various method embodiments.

[0113] The processor 701 may be a general-purpose central processing unit (CPU), a network processor (NP), a microprocessor, or may be one or more integrated circuits for implementing the solution of the present application, such as an application-specific integrated circuit (ASIC), a programmable logic device (PLD) or a combination thereof. The above-mentioned PLD may be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL) or any combination thereof.

[0114] The communication bus 702 is used to transmit information between the above components. The communication bus 702 can be divided into an address bus, a data bus, a control bus, etc. Figure 7 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0115] The memory 703 may be a read-only memory (ROM) or other types of static storage devices that can store static information and instructions, or a random access memory (RAM) or other types of dynamic storage devices that can store information and instructions, or an electrically erasable programmable read-only memory (EEPROM), a compact disc read-only memory (CD-ROM) or other optical disc storage, optical disc storage (including compressed optical disc, laser disc, optical disc, digital versatile disc, Blu-ray disc, etc.), a magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto. The memory 703 may exist independently and be connected to the processor 701 through the communication bus 702. The memory 703 may also be integrated with the processor 701.

[0116] The communication interface 704 uses any transceiver-like device for communicating with other devices or communication networks. The communication interface 704 includes a wired communication interface and may also include a wireless communication interface. Among them, the wired communication interface may be, for example, an Ethernet interface. The Ethernet interface may be an optical interface, an electrical interface, or a combination thereof. The wireless communication interface may be a wireless local area network (WLAN) interface, a cellular network communication interface, or a combination thereof, etc.

[0117] In a specific implementation, as an embodiment, the processor 701 may include one or more CPUs, such as the attached Figure 7 CPU0 and CPU1 are shown in the figure.

[0118] In a specific implementation, as an embodiment, the code processing device 700 may include multiple processors, such as the attached Figure 7 1 and 10. Each of these processors may be a single-CPU or a multi-CPU. A processor herein may refer to one or more devices, circuits, and / or processing cores for processing data (such as computer program instructions).

[0119] In a specific implementation, as an embodiment, the code processing device 700 may also include an output device and an input device. The output device communicates with the processor 701 and can display information in a variety of ways. For example, the output device may be a liquid crystal display (LCD), a light emitting diode (LED) display device, a cathode ray tube (CRT) display device, or a projector. The input device communicates with the processor 701 and can receive user input in a variety of ways. For example, the input device may be a mouse, a keyboard, a touch screen device, or a sensor device.

[0120] In some embodiments, the memory 703 is used to store the program code 710 for executing the solution of the present application, and the processor 701 can execute the program code 710 stored in the memory 703. That is, the code processing device 700 can implement the code scanning method or code migration method provided by the method embodiment through the processor 701 and the program code 710 in the memory 703.

[0121] It should be understood that the various units in the code processing device 700 and the above-mentioned other operations and / or functions are used to implement the above-mentioned Figure 1 The method shown or above Figure 3 The method shown, or used to implement the above Figure 2 The steps and methods implemented by the logic processing unit shown in the figure, or used to implement the above Figure 2 The steps and methods implemented by the code migration tool shown are described in detail in the above method embodiment, and will not be described again here for the sake of brevity.

[0122] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0123] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.

[0124] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0125] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0126] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk.

[0127] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, a person skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.

Claims

1. A method for code scanning, It is characterized in that include: Scanning the source code in the source file to obtain macro definition list information, macro reference context information and conditional compilation code block information, wherein the macro definition list information includes macro definition information of the macro in the source code, the macro reference context information includes the reference position of the macro in the source file, and the conditional compilation code block information includes the macros involved in all conditional branches; An intermediate result file is generated according to the macro definition list information, the macro reference context information and the conditional compilation code block information.

2. The method according to claim 1, It is characterized in that The scanning of the source code in the source file includes: If the source file includes a header file, the header file is expanded and scanned to obtain the macro definition list information, the macro reference context information and the conditional compilation code block information corresponding to the header file.

3. The method according to claim 1 or 2, It is characterized in that The scanning of the source code in the source file to obtain the macro definition list information, macro reference context information and conditional compilation code block information includes: If the scanned text of the source code includes a macro, the macro definition information of the macro is recorded in the macro definition list information; If a macro in the text of the source code is scanned and referenced, the line number corresponding to the reference position and the corresponding file identifier are recorded in the macro reference context information; If a conditional compilation code block in the text of the source code is scanned, the codes involved in all conditional branches in the conditional compilation code block are recorded in the conditional compilation code block information.

4. The method according to any one of claims 1 to 2, It is characterized in that Before scanning the source code in the source file, the method further includes: Get the preset macro definitions and whitelist macro information list related to the CPU architecture.

5. A method for code migration, It is characterized in that include: Obtaining an intermediate result file obtained by the code scanning method according to any one of claims 1 to 4 and a whitelist macro information list related to a preset central processing unit CPU architecture; Parsing the intermediate result file to obtain macro definition list information, macro reference context information and conditional compilation code block information; According to the whitelist macro information list, macro definition information related to a preset CPU architecture is selected from the macro definition list information to form valid macro list information; Parsing the conditional compilation code block information to obtain macros involved in each conditional branch; Selecting a macro that matches the valid macro list information from the macros involved in each conditional branch; Locating a first source code position according to the macro reference context information and a macro matching the valid macro list information; After locating the first source code location, migrate the code or generate a migration suggestion corresponding to the first source code location.

6. The method according to claim 5, It is characterized in that The method further comprises: Scan the codes between the code segments corresponding to the conditional compilation code block information, and confirm whether code migration is required according to preset rules.

7. The method according to claim 6, It is characterized in that The step of confirming whether code migration is required according to preset rules includes: Selecting a macro matching the valid macro list information from macros in the code between the code segments; After locating the second source code position according to the macro reference context information and the macro matching the valid macro list information, the code is migrated or a migration suggestion corresponding to the second source code position is generated.

8. The method according to claim 5, It is characterized in that The method further comprises: Generate a code migration report based on the scan results of all source codes.

9. A device for scanning a code, It is characterized in that include: Acquisition module, used to obtain source files; A processing module, wherein a user of the processing module scans the source code in the source file to obtain macro definition list information, macro reference context information and conditional compilation code block information, wherein the macro definition list information includes macro definition information of the macro in the source code, the macro reference context information includes a reference position of the macro in the source file, and the conditional compilation code block information includes macros involved in all conditional branches; The processing module is also used to generate an intermediate result file according to the macro definition list information, the macro reference context information and the conditional compilation code block information.

10. The device according to claim 9, It is characterized in that The processing module is used for: If the source file includes a header file, the header file is expanded and scanned to obtain the macro definition list information, the macro reference context information and the conditional compilation code block information corresponding to the header file.

11. The device according to claim 9 or 10, It is characterized in that The processing module is used for: If the scanned text of the source code includes a macro, the macro definition information of the macro is recorded in the macro definition list information; If a macro in the text of the source code is scanned and referenced, the line number corresponding to the reference position and the corresponding file identifier are recorded in the macro reference context information; If a conditional compilation code block in the text of the source code is scanned, the codes involved in all conditional branches in the conditional compilation code block are recorded in the conditional compilation code block information.

12. The device according to any one of claims 9 to 10, It is characterized in that The acquisition module is used for: Get the preset macro definitions and the whitelist macro information list related to the CPU architecture.

13. A device for code migration, It is characterized in that include: An acquisition module, used to acquire an intermediate result file obtained by the code scanning device according to any one of claims 9 to 12 and a whitelist macro information list related to a preset central processing unit CPU architecture; A processing module, used for parsing the intermediate result file to obtain macro definition list information, macro reference context information and conditional compilation code block information; The processing module is further used to select macro definition information related to a preset CPU architecture from the macro definition list information to form effective macro list information according to the whitelist macro information list; The processing module is further used to parse the conditional compilation code block information to obtain macros involved in each conditional branch; The processing module is further used to select a macro matching the valid macro list information from the macros involved in each conditional branch; The processing module is further used to locate the first source code position according to the macro reference context information and the macro matching the valid macro list information; The processing module is further configured to migrate the code after locating the first source code position or generate a migration suggestion corresponding to the first source code position.

14. The device according to claim 13, It is characterized in that The processing module is used for: Scan the codes between the code segments corresponding to the conditional compilation code block information, and confirm whether code migration is required according to preset rules.

15. The device according to claim 14, It is characterized in that The processing module is used for: Selecting a macro matching the valid macro list information from macros in the code between the code segments; After locating the second source code position according to the macro reference context information and the macro matching the valid macro list information, the code is migrated or a migration suggestion corresponding to the second source code position is generated.

16. The device according to any one of claims 13 to 14, It is characterized in that The processing module is used for: Generate a code migration report based on the scan results of all source codes.

17. A device for code processing, It is characterized in that include: One or more central processing units, memories, communication buses, and communication interfaces; The memory is a temporary storage memory or a permanent storage memory, and a program code is stored in the memory; The communication interface is used to send and receive data; The central processing unit communicates with the memory via the communication bus, and executes the program code in the memory on the code processing device to perform the method according to any one of claims 1 to 8.