Code conversion method and device and computer readable storage medium
By generating single file and mapping relationship tables, the problems of repeated definition of data structures, missing dependencies and variable naming conflicts in C2Rust translation are solved, and the accuracy and efficiency of code conversion are improved.
Patent Information
- Application Number
- CN202411889210.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-20
- Publication Date
- 2025-04-18
- Estimated Expiration
- 2044-12-20
AI Technical Summary
During the C2Rust translation process, there may be problems such as duplicate definition of data structures, missing dependencies and variable naming conflicts, which affect the accuracy of code conversion.
By obtaining the code items to be converted, generating a single file and mapping relationship table, performing conversion processing, and reconstructing the target items based on the mapping relationship table, solving the problems of repeated definition of data structures, missing dependencies and variable naming conflicts.
It improves the accuracy of converting C pre-research to Rust language, reduces the work of manual rewriting and reconstruction, saves manpower, material resources and financial resources, and achieves fast and accurate code conversion.
Smart Images

Figure CN119847537A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computer technology, and in particular, to a code conversion method, device, and computer-readable storage medium. Background Art
[0002] Rust is an emerging static programming language featuring memory safety. It introduces the concepts of lifecycle and ownership. Compared with traditional C / C++ programming languages, it can ensure memory safety while achieving similar performance, and avoid memory leaks, dangling pointers, buffer overflows, and data competition in concurrent programming to a great extent. Due to Rust's outstanding performance in memory safety and performance, it is widely used in the construction or reconstruction of high-reliability and high-performance programs. Among them, the C2Rust tool can translate most C language codes into semantically equivalent Rust codes, and has become one of the most mature and popular code conversion tools. However, there is no concept of header files in the Rust language. The C2Rust translation process may have the problem of repeated translation of structures defined in the C project header file, affecting accuracy. Summary of the invention
[0003] The embodiments of the present application provide a code conversion method, device and computer-readable storage medium, which can solve the problems of duplicate definition of data structures, missing dependencies and variable naming conflicts caused by C2Rust translation, and improve the accuracy of converting C pre-research into Rust language.
[0004] In order to achieve the above purpose, the embodiment of the present application adopts the following technical solution:
[0005] In a first aspect, an embodiment of the present application provides a code conversion method, the method comprising:
[0006] Acquire a code project to be converted, wherein the code project to be converted includes at least one first code file written based on a first language;
[0007] Generate a first single file and a mapping relationship table according to the code project to be converted; the mapping relationship table is used to store the correspondence between the first identifier in the first single file and the path of the first code file defining the first identifier, each first identifier in the first single file corresponds to one of the first code files in the code project to be converted;
[0008] Converting the first single file to obtain a second single file; the second single file is a code file written in a second language, and the second single file includes a second identifier converted from the first identifier in the first single file;
[0009] Generate at least one second code file according to the second identifier of the second single file and the mapping relationship table;
[0010] A target project is determined, where the target project at least includes the at least one second code file.
[0011] The code conversion method provided in the embodiment of the present application can solve the problems of duplicate definition of data structure, missing dependencies and variable naming conflicts when translating a first language code project into a second language code project, which helps to improve the code conversion pass rate and efficiency. In addition, the target project reconstructed based on the mapping relationship table has a similar file structure to the original code project to be converted, and there is no need for manual rewriting and reconstruction, which can save manpower, material and financial resources and achieve the purpose of fast and accurate code conversion.
[0012] In a possible implementation, obtaining the code item to be converted includes:
[0013] Obtaining at least one initial code file written in a first language;
[0014] Compile and check the at least one initial code file to obtain the code project to be converted that can pass the compilation.
[0015] Based on this solution, the initial code file is compiled and verified to obtain a code project to be converted that can be compiled, which can reduce compilation errors of the code project to be converted, ensure that the input code project to be converted meets the writing standards of the first language, and help improve the code conversion pass rate and efficiency.
[0016] In a possible implementation, generating a first single file and a mapping relationship table according to the code project to be converted includes:
[0017] Integrate the at least one first code file included in the code project to be converted to obtain the first single file;
[0018] Determine, according to the first single file, a first identifier set corresponding to the first single file, where the first identifier set includes a plurality of first identifiers;
[0019] In the at least one first code file, determining a first code file defining each of the first identifiers;
[0020] The mapping relationship table is generated according to each of the first identifiers and a path of a first code file defining each of the first identifiers.
[0021] Based on this solution, by integrating the first code file to obtain a single file to be converted, it is possible to solve the problems of repeated definition of data structure, missing dependencies and variable naming conflicts when translating from the first language to the second language. When merging, by recording the correspondence between the identifier and the file path, data support is provided for the subsequent reconstruction of the target project with a similar file structure.
[0022] In a possible implementation, the integrating the at least one first code file included in the code project to be converted to obtain the first single file includes:
[0023] Merging the first code files included in the code project to be converted to obtain an initial single file;
[0024] Expanding the macro definition in the initial single file to obtain an intermediate single file;
[0025] The intermediate single file is cleaned to obtain the first single file.
[0026] Based on this solution, the code project to be converted is processed by file merging, macro definition expansion and useless code cleaning, which can solve the problems of duplicate definition of data structure, missing dependencies and variable naming conflicts when translating the first language code project into the second language code project.
[0027] In a possible implementation, before converting the first single file to obtain the second single file, the method further includes:
[0028] Determine an identifier to be repaired in the first single file that meets a first repair condition;
[0029] Repairing processing is performed on the identifier to be repaired in the first single file.
[0030] Based on this solution, before converting the first single file, the identifiers in the first single file that may cause the conversion failure are repaired and then converted, thereby improving the pass rate of successful conversion.
[0031] In a possible implementation, the repairing process on the identifier to be repaired in the first single file includes:
[0032] Determine an abstract syntax tree corresponding to the first single file;
[0033] Determine the enumeration definition in the structure according to the abstract syntax tree corresponding to the first single file;
[0034] Move the enumeration definition in the structure to outside the structure.
[0035] Based on this solution, by preprocessing the first single file to be converted, the enumeration definition in the structure that may cause failure in successful translation is moved outside the structure, which can improve the pass rate of successful translation.
[0036] In a possible implementation, after converting the first single file to obtain the second single file, the method further includes:
[0037] Determine the code to be repaired that meets the second repair condition in the second single file;
[0038] Repair the code to be repaired in the second single file.
[0039] Based on this solution, before reconstructing the second single file, the code in the second single file that may cause compilation failure is repaired, which can improve the pass rate of successful compilation of the target project determined based on the second single file.
[0040] In a possible implementation, the repairing the code to be repaired in the second single file includes:
[0041] Eliminating the naked pointers in the second single file to obtain a preliminary processed single file;
[0042] Performing a compilation check on the preliminary processing single file to obtain a compilation check result;
[0043] When the compilation check result indicates that the preliminary processing single file can pass the compilation, the naked pointer in the preliminary processing single file is repaired.
[0044] Based on this solution, by compiling and verifying the converted second single file, and repairing the naked pointers in the second single file code that may cause failure of successful compilation before and after the compilation verification, the pass rate of successful compilation of the target project determined based on the second single file can be improved.
[0045] In a possible implementation, the generating at least one second code file according to the second identifier of the second single file and the mapping relationship table includes:
[0046] According to the path of the first code file included in the mapping relationship table, create at least one file with the same name based on the second language;
[0047] Determine, according to the second single file, a second identifier set corresponding to the second single file;
[0048] Matching each identifier in the second identifier set with the first identifier included in the mapping relationship table to obtain a matching result;
[0049] Based on the matching result and the at least one file with the same name, the second single file is reconstructed to obtain the at least one second code file.
[0050] Based on this solution, the target project reconstructed according to the mapping relationship table has a file structure similar to that of the original code project to be converted, and there is no need for manual rewriting and reconstruction, which can save manpower, material and financial resources.
[0051] In a possible implementation, the reconstructing the second single file based on the matching result and the at least one file with the same name to obtain the at least one second code file includes:
[0052] In a case where the matching result indicates that the second identifier in the second identifier set matches the target identifier included in the mapping relationship table, determining a definition of the second identifier in the second single file, and determining a target file with the same name as the path of the first code file defining the target identifier in the at least one file with the same name;
[0053] Move the definition of the second identifier in the second single file to the target file;
[0054] A reference to the second identifier is added to a position where the second identifier is used in the at least one file with the same name, so as to obtain the at least one second code file.
[0055] Based on this solution, the definition of the matching identifier is moved to the corresponding file with the same name, and the reference of the identifier is added at the location where the identifier is used, so that the file structure of the target project is similar to that of the original code project to be converted.
[0056] In a second aspect, an embodiment of the present application provides a code conversion device, the device comprising:
[0057] An acquisition module, configured to acquire a code project to be converted, wherein the code project to be converted includes at least one first code file written in a first language;
[0058] A first generating module is used to generate a first single file and a mapping relationship table according to the code project to be converted; the mapping relationship table is used to store the correspondence between the first identifier in the first single file and the path of the first code file defining the first identifier, each first identifier in the first single file corresponds to one of the first code files in the code project to be converted;
[0059] a conversion module, configured to convert the first single file to obtain a second single file; the second single file is a code file written in a second language, and the second single file includes a second identifier converted from the first identifier in the first single file;
[0060] A second generating module, configured to generate at least one second code file according to the second identifier of the second single file and the mapping relationship table;
[0061] The first determination module is used to determine a target project, where the target project at least includes the at least one second code file.
[0062] In a possible implementation, the acquisition module is further used to:
[0063] Obtaining at least one initial code file written in a first language;
[0064] Compile and check the at least one initial code file to obtain the code project to be converted that can pass the compilation.
[0065] In a possible implementation, the first generating module is further configured to:
[0066] Integrate the at least one first code file included in the code project to be converted to obtain the first single file;
[0067] Determine, according to the first single file, a first identifier set corresponding to the first single file, where the first identifier set includes a plurality of first identifiers;
[0068] In the at least one first code file, determining a first code file defining each of the first identifiers;
[0069] The mapping relationship table is generated according to each of the first identifiers and a path of a first code file defining each of the first identifiers.
[0070] In a possible implementation, the first generating module is further configured to:
[0071] Merging the first code files included in the code project to be converted to obtain an initial single file;
[0072] Expanding the macro definition in the initial single file to obtain an intermediate single file;
[0073] The intermediate single file is cleaned to obtain the first single file.
[0074] In a possible implementation, the code conversion device may further include: a second determination module and a first repair processing module;
[0075] The second determining module is used to determine the identifier to be repaired in the first single file that meets the first repair condition;
[0076] The first repair processing module is used to perform repair processing on the identifier to be repaired in the first single file.
[0077] In a possible implementation, the first repair processing module is further configured to:
[0078] Determine an abstract syntax tree corresponding to the first single file;
[0079] Determine the enumeration definition in the structure according to the abstract syntax tree corresponding to the first single file;
[0080] Move the enumeration definition in the structure to outside the structure.
[0081] In a possible implementation, the code conversion device may further include: a third determination module and a second repair processing module;
[0082] The third determining module is used to determine the code to be repaired that meets the second repair condition in the second single file;
[0083] The second repair processing module is used to perform repair processing on the code to be repaired in the second single file.
[0084] In a possible implementation, the second repair processing module is further configured to:
[0085] Eliminating the naked pointers in the second single file to obtain a preliminary processed single file;
[0086] Performing a compilation check on the preliminary processing single file to obtain a compilation check result;
[0087] When the compilation check result indicates that the preliminary processing single file can pass the compilation, the naked pointer in the preliminary processing single file is repaired.
[0088] In a possible implementation, the second generating module is further configured to:
[0089] According to the path of the first code file included in the mapping relationship table, create at least one file with the same name based on the second language;
[0090] Determine, according to the second single file, a second identifier set corresponding to the second single file;
[0091] Matching each identifier in the second identifier set with the first identifier included in the mapping relationship table to obtain a matching result;
[0092] Based on the matching result and the at least one file with the same name, the second single file is reconstructed to obtain the at least one second code file.
[0093] In a possible implementation, the second generating module is further configured to:
[0094] In a case where the matching result indicates that the second identifier in the second identifier set matches the target identifier included in the mapping relationship table, determining a definition of the second identifier in the second single file, and determining a target file with the same name as the path of the first code file defining the target identifier in the at least one file with the same name;
[0095] Move the definition of the second identifier in the second single file to the target file;
[0096] A reference to the second identifier is added to a position where the second identifier is used in the at least one file with the same name, so as to obtain the at least one second code file.
[0097] In a third aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the storage medium stores a computer program, and when the computer program is executed, the code conversion method described in any one of the first aspects above is implemented.
[0098] In a fourth aspect, an embodiment of the present application provides a computing device, comprising: a processor and a memory; the processor and the memory are coupled;
[0099] The memory is used to store executable instructions;
[0100] The processor is used to read the instruction from the memory and execute the instruction to perform the code conversion method described in any one of the first aspects above.
[0101] In a fifth aspect, an embodiment of the present application provides a chip, which is used to execute the code conversion method as described in any one of the first aspects above.
[0102] In a sixth aspect, an embodiment of the present application provides a program product, which, when instructions in the program product are executed, implements the code conversion method described in any one of the first aspects above. BRIEF DESCRIPTION OF THE DRAWINGS
[0103] Figure 1 A schematic diagram of the network architecture of the code conversion system provided in an embodiment of the present application;
[0104] Figure 2 A schematic diagram of an implementation flow of the code conversion method provided in an embodiment of the present application;
[0105] Figure 3 A schematic diagram of another implementation flow of the code conversion method provided in an embodiment of the present application;
[0106] Figure 4 A schematic diagram of another implementation flow of the code conversion method provided in the embodiment of the present application;
[0107] Figure 5 A schematic diagram of another implementation flow of the code conversion method provided in the embodiment of the present application;
[0108] Figure 6 A schematic diagram of a structure of a code conversion device provided in an embodiment of the present application;
[0109] Figure 7 A schematic diagram of the composition structure of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0110] The technical solution in the embodiment of the present application will be described below in conjunction with the drawings in the embodiment of the present application. In order to facilitate the clear description of the technical solution in the embodiment of the present application, the first, second, etc. descriptions appearing in the embodiment of the present application are only used for illustration and distinction of the description objects, and there is no order, nor does it represent a special limitation on the number of devices in the embodiment of the present application, and cannot constitute any limitation on the embodiment of the present application.
[0111] Before further describing the embodiments of the present application in detail, the nouns and terms involved in the embodiments of the present application are first described. The nouns and terms involved in the embodiments of the present application are subject to the following interpretations.
[0112] 1) C2Rust is a tool that automatically converts C code to Rust code, helping developers migrate existing C projects to Rust. It supports multiple operating systems, including Linux, macOS, and Windows, making the migration process more convenient.
[0113] 2) Abstract Syntax Tree (AST) is an abstract representation of the syntax structure of the source code, which displays the syntax structure of the programming language in a tree form. Each node in the AST represents a structure in the source code, such as variable declaration, expression, etc. AST can be applied to front-end development, JavaScript (JS) compilation and execution, keyword matching, scope judgment and other scenarios.
[0114] 3) Low Level Virtual Machine (LLVM) is an open source project that provides efficient compilation, linking, optimization and other functions for multiple programming languages. It is a framework system for building compilers, written in C++, and is used to optimize the compile time, link time, run time and idle time of programs written in any programming language. It is open to developers and compatible with existing scripts.
[0115] 4) Emscripten is a complete WebAssembly (wsam) compilation tool chain based on LLVM that focuses on speed, size and network (Web) platform. It is used to translate C / C++ projects or any other projects compiled with LLVM into wsam, so that C / C++ projects can be executed on browsers, node.js or wsam runtimes.
[0116] Among them, wsam is a virtual instruction set architecture (Instruction Set Architecture, ISA). The overall architecture includes the core ISA definition, binary encoding, definition and execution of program semantics, and application programming interface (Application Programming Interface, API) for different embedded environments (such as Web).
[0117] 5) The Relooper algorithm is an algorithm used to optimize loop structures, especially in compiler design to improve code execution efficiency and performance. It improves program execution speed by reorganizing loop structures, reducing loop nesting depth, and eliminating unnecessary control flow dependencies. The Relooper algorithm is particularly suitable for program codes with complex loops and conditional branches. By optimizing the loop structure, the program is made more efficient, easier to understand and maintain.
[0118] 6) Laertes is a tool that optimizes and repairs unsafe Rust code translated from C2Rust. It mainly consists of two core components: resolve-import and resolve-lifetime. Resolve-import is used to eliminate duplicated data structures and solve the problem of duplicate import of header files (files with the suffix ".h") when expanding code. Resolve-lifetime is used to repair missing dependencies, rewrite raw pointers to safe Rust references, and repair the life cycle of generated variables.
[0119] 7) Crown is a tool that optimizes and repairs raw pointers in Rust code translated from C2Rust. It designs a model for analyzing and reasoning about raw pointer ownership, which can automatically convert raw pointers into Rust's corresponding safety features. Unlike the Laertes tool that relies on the Rust compiler to assist in rewriting raw pointers, the Crown tool models the ownership analysis conversion as a constraint satisfaction problem (such as 3-SAT) and uses a solver to solve it. The analysis and reasoning process does not rely on the Rust compiler and is faster. In addition, the Crown tool based on ownership analysis also has the characteristics of precision and scalability, and can handle complex data structures and nested pointers.
[0120] 8) A raw pointer is a pointer type in Rust that is not managed by the borrow checker. *const T is usually used to represent an immutable raw pointer, and *mut T is used to represent a mutable raw pointer. The characteristics of a raw pointer are as follows: it is unsafe, can point to null or any memory address, and has no life cycle. It is not constrained by Rust's ownership system and is usually used to interact with the underlying operating system or hardware.
[0121] The following describes an exemplary application of the computing device provided in the embodiment of the present application. The computing device provided in the embodiment of the present application can be implemented as a device capable of code conversion, such as a computer. The following describes an exemplary application when the computing device is implemented as a computer.
[0122] See also Figure 1 , Figure 1 A schematic diagram of the network architecture of the code conversion system provided in the embodiment of the present application is shown in FIG. Figure 1 As shown, in the code conversion system 10, a first device 100, a network 200 and a second device 300 are included, wherein the first device 100 is connected to the second device 300 through the network 200. The network 200 can be a wide area network or a local area network, or a combination of the two. The first device 100 and the second device 300 can be directly or indirectly connected to each other through a wired or wireless link to achieve data transmission.
[0123] Exemplarily, the code conversion system 10 provided in the embodiment of the present application can be applied to the scenario of translating a C language project into a Rust language project. Among them, the first device 100 can be a computing device that provides a C language project, such as a server, a laptop, a tablet computer, a desktop computer, a set-top box, a mobile device (such as a mobile phone, a portable music player, a personal digital assistant, a dedicated messaging device, a portable gaming device) and other types of any device that can provide a C language project.
[0124] The second device 300 may be a computing device for code conversion, such as a server, a laptop, a tablet computer, a desktop computer, a set-top box, a mobile device (such as a mobile phone, a portable music player, a personal digital assistant, a dedicated messaging device, a portable gaming device), or any other device capable of translating a C language project provided by the first device 100 into a Rust language project. A code conversion tool is provided on the second device 300. Exemplarily, the code conversion tool may be a C2Rust tool.
[0125] Exemplarily, in an application scenario of the code conversion system 10, an original project written by a developer based on a first language is stored on the first device 100, and the original project includes at least one initial code file. When code conversion is required, the first device 100 sends the at least one initial code file included in the original project to the second device 300 through the network 200.
[0126] After the second device 300 obtains at least one initial code file included in the original project written in the first language transmitted by the first device 100, it can compile and check the original project to obtain a code project to be converted that can be compiled, and the code project to be converted includes at least one first code file written in the first language that can be compiled; then, according to the code project to be converted, determine the first single file to be converted and the mapping relationship table; the mapping relationship table is used to store the correspondence between the first identifier in the first single file and the path of the first code file corresponding to the first identifier, wherein the first code file corresponding to the first identifier is the code file that defines the first identifier. Then, the second device 300 converts the first single file to obtain a converted second single file, and the second single file is a code file written in the second language; finally, determine the target project, which includes at least one second code file generated according to the mapping relationship table and the second identifier in the second single file.
[0127] Exemplarily, the first language can be C language, and the corresponding code project to be converted is a C language project; the second language can be Rust language, and the corresponding target project can be a Rust language project. This code conversion system can solve the problems of duplicate definition of data structures, missing dependencies, and variable naming conflicts caused by C2Rust translation, and can quickly translate C language projects into Rust language projects. The file structure of Rust language projects is similar to that of the original C language projects, and there is no need for manual rewriting and reconstruction, which can save manpower, material resources, and financial resources, and achieve the purpose of quickly and accurately translating C language code into Rust language code.
[0128] For example, in another application scenario of the code conversion system 10, the first device 100 and the second device 300 can be the same device, that is, the device providing the C language project and the device performing the code conversion are the same device. The implementation principle is similar to the above application scenario and will not be repeated here.
[0129] The code conversion system provided by the embodiment of the present application first obtains a code project to be converted written in a first language; then, according to at least one first code file included in the code project to be converted, generates a first single file and a mapping relationship table, the mapping relationship table is used to store the correspondence between the first identifier in the first single file and the path of the first code file defining the first identifier; then, the first single file is converted to obtain a second single file, the second single file is a code file written in a second language, and the second single file includes a second identifier converted from the first identifier in the first single file; finally, at least one second code file is generated according to the second identifier of the second single file and the mapping relationship table, and the target project is determined based on the at least one second code file. Through the code conversion system, the problems of repeated definition of data structure, missing dependency and variable naming conflict existing when translating from the first language to the second language can be solved, the efficiency of code translation can be improved, and the target project reconstructed based on the mapping relationship table is similar to the file structure of the original code project to be converted, without the need for manual rewriting and reconstruction, which can save manpower, material resources and financial resources, and achieve the purpose of fast and accurate code translation.
[0130] The code conversion method provided in the embodiment of the present application is described below in conjunction with the exemplary application and implementation of the computing device provided in the embodiment of the present application.
[0131] Figure 2 A schematic diagram of an implementation flow of the code conversion method provided in the embodiment of the present application, which is applied to Figure 1 A second device in the code conversion system shown. Figure 2 As shown, the code conversion method includes the following steps:
[0132] Step S201, obtaining a code item to be converted.
[0133] The method provided in the embodiment of the present application can be executed by a computing device, and specifically can be executed by a code conversion device in the computing device. Exemplarily, the code conversion method provided in the embodiment of the present application can be used to convert a first language code into a second language code, wherein the first language can be C language, the second language can be Rust language, and the first language and the second language can also be other programming languages, which are not limited in the embodiment of the present application. Below, taking the first language as C language and the second language as Rust language as an example, the code conversion method provided in the embodiment of the present application is exemplarily described.
[0134] The code project to be converted obtained by the code conversion device may include one or more first code files, where the first code files are code files written based on the first language. For example, the code project to be converted may be a C language project.
[0135] In some embodiments, the code conversion device may obtain the code items to be converted in the following manner: first, obtain at least one initial code file written in the first language; then, perform compilation and inspection processing on each initial code file to obtain the code items to be converted that can pass compilation.
[0136] Exemplarily, the initial code file can be a code file written by a developer, for example, the initial code file of a C language project can be a file with a suffix of .c or .h. The code conversion device can check the initial code file for compiling passability. First, traverse each initial code file to check whether there is a configuration file. If there is a configuration file in the initial code file, it is determined that the initial code file can be successfully compiled. At this time, each initial code file is used as the first code file to form a code project to be converted. Exemplarily, the configuration file can be a CMake configuration file. CMake is a cross-platform automated build tool, which is mainly used to manage the build process of software. It can control the compilation and linking process and generate executable files or libraries that can run on different platforms.
[0137] If there is no CMake configuration file in the initial code file, you can use the CMake tool to organize the project, and then verify the compilation of each initial code file organized by the project. If each initial code file organized by the project can be successfully compiled, the initial code files organized by the project will be determined as the first code file to form the code project to be converted; if the compilation of each initial code file organized by the project fails, it can be repaired according to the compilation error until it can be successfully compiled, and the code files that can be successfully compiled will be formed into the code project to be converted.
[0138] Step S202: Generate a first single file and a mapping relationship table according to the code project to be converted.
[0139] Since C2Rust only supports single-file conversion, directly converting a code project to be converted that includes multiple first code files will generate multiple Rust files, which requires a lot of manpower to reconstruct. In order to reduce the workload and save manpower, in an embodiment of the present application, before conversion, multiple first code files included in the code project to be converted can be merged into a single file.
[0140] When merging multiple first code files, since there is no concept of header files in the Rust language, the C2Rust translation process does not consider the dependencies between the files of the C project, and directly merging will bring about the problem of missing dependencies. In an embodiment of the present application, in order to retain the dependencies between multiple files, when merging, the definition path of each first identifier in the first single file in the code project to be converted before the merge can be determined according to the path of each first code file in the code project to be converted before the merge and the first single file after the merge, and a mapping relationship table is generated, which is used to store the correspondence between the first identifier in the first single file and the path of the first code file that defines the first identifier.
[0141] In order to distinguish the identifier in the first language code from the identifier in the second language code, in the embodiment of the present application, the identifier in the first language code is referred to as the first identifier, and the identifier in the second language code is referred to as the second identifier. The first identifier and the second identifier do not represent the order of the identifiers.
[0142] The first identifier in the first single file generated by the C language project can include three types: keywords, predefined identifiers and user-defined identifiers. Keywords, also known as reserved words, are strings with specific meanings specified by the C language, such as int, char, float, return, etc. Predefined identifiers are identifiers pre-defined by the system, such as library function names (such as printf, scanf), macro definitions, and type aliases. User-defined identifiers are identifiers defined by users according to their needs, and are usually used to name variables, functions, arrays, etc., such as the user-defined function managerialSystem.
[0143] Each first identifier is defined in at least one first code file in the code project to be converted, so each first identifier in the first single file corresponds to a first code file in the code project to be converted. For example: the first identifier managerialSystem is defined in the managerialSystem.c file, and the corresponding relationship between the first identifier managerialSystem and the managerialSystem.c file path is recorded in the mapping relationship table.
[0144] Exemplarily, one or more first identifiers may be defined in a first code file. A first identifier may be defined in a first code file, or may be repeatedly defined in multiple first code files. If a first identifier is repeatedly defined in multiple first code files, redundant codes may exist in the generated first single file. In order to improve conversion efficiency, the redundant codes in the first single file may be repaired before the conversion process is performed.
[0145] Step S203, converting the first single file to obtain a second single file.
[0146] Exemplarily, a code conversion tool may be used to convert the first single file to obtain a second single file. The second single file is a code file written in a second language, and includes a second identifier converted from the first identifier in the first single file. Exemplarily, the code conversion tool for converting a C language project to a Rust language project may be a C2Rust tool.
[0147] For example, during the conversion, the first single file determined in step S202 can be directly input into the code conversion tool for conversion processing to obtain the second single file; or the first single file can be preprocessed first, and then the preprocessed first single file is input into the code conversion tool for conversion processing to obtain the second single file. The preprocessing operation can be a processing operation that can improve the conversion success rate, or can be other operations that can improve the code quality, which is not limited in the embodiments of the present application.
[0148] In the embodiment of the present application, by processing the code project to be converted into a single file and then performing the conversion process, problems such as repeated definition of data structures, missing dependencies, and variable naming conflicts that exist when converting a first language into a second language can be solved, which helps to improve the code conversion pass rate and conversion efficiency.
[0149] Step S204: Generate at least one second code file according to the second identifier of the second single file and the mapping relationship table.
[0150] Step S205, determining the target project.
[0151] The mapping relationship table records the file path defined by the first identifier in the first single file in the code project to be converted. According to the correspondence between the first identifier in the mapping relationship table and the second identifier in the second single file, the second single file is reconstructed to generate at least one second code file corresponding to at least one first code file, and the second code files constitute the target project. Reconstruction based on the mapping relationship table makes the file structure of the target project similar to the file structure of the code project to be converted, which can enhance the code readability.
[0152] The code conversion method provided by the embodiment of the present application first obtains a code project to be converted written in a first language; then, according to at least one code file included in the code project to be converted, generates a first single file and a mapping relationship table, the mapping relationship table is used to store the correspondence between the first identifier in the first single file and the path of the first code file defining the first identifier, and each first identifier in the first single file corresponds to a first code file in the code project to be converted; then, the first single file is converted to obtain a second single file written in a second language, the second single file includes a second identifier converted from the first identifier in the first single file; finally, at least one second code file is generated according to the second identifier of the second single file and the mapping relationship table, and a target project is determined based on at least one second code file. Through the code conversion method, the problems of repeated definition of data structure, missing dependency and variable naming conflict existing when translating from the first language to the second language can be solved, which is helpful to improve the pass rate and efficiency of code conversion, and the target project reconstructed based on the mapping relationship table is similar to the file structure of the original code project to be converted, without the need for manual rewriting and reconstruction, which can save manpower, material resources and financial resources, and achieve the purpose of fast and accurate code conversion.
[0153] In some embodiments, step S202 in the above embodiment, "generating a first single file and a mapping relationship table according to the code project to be converted", can be performed by Figure 3 Steps S2021 to S2024 shown are used to implement:
[0154] Step S2021: integrate at least one first code file included in the code project to be converted to obtain a first single file.
[0155] For example, the Goblint tool can be used to configure CMake parameters for project building to obtain the merged first single file to be converted, and the useless code can be removed at the same time during the merge. Among them, Goblint is a powerful static analysis tool designed for C language, which can detect potential problems in the code before compilation, such as data competition, null pointer dereference, etc., to help developers improve the security and reliability of the code.
[0156] In some embodiments, the code conversion device can obtain the first single file by following the steps:
[0157] Step S20211, merging the first code files included in the code project to be converted to obtain an initial single file.
[0158] The code conversion device first traverses the files in the directory of the code project to be converted to determine the first code file included in the code project to be converted. Generally, the number of first code files included in the code project to be converted is multiple. Taking the C language project as an example, the first code file can be a code file with a suffix of .h or .c.
[0159] After obtaining all the first code files included in the code project to be converted, merge the first code files, merge the codes in the first code files into one file, and obtain an initial single file. Specifically, when the code project to be converted includes one first code file, the one first code file is determined as the initial single file; when the code project to be converted includes multiple first code files, the Goblint tool can be used to configure CMake parameters for project construction to obtain the merged initial single file to be converted.
[0160] Step S20212, expand the macro definition in the initial single file to obtain an intermediate single file.
[0161] After obtaining the initial single file, the macro definition in the initial single file is expanded, that is, the macro definition in the initial single file is replaced with a specific value or code block to obtain an intermediate single file. The macro definition is a preprocessing instruction in C language and C++, which allows the programmer to define an identifier in the source code so that the identifier can be used to replace a specific value or code block in subsequent codes.
[0162] In some embodiments, when the code conversion device expands the macro definition in the initial single file, it can also expand the constant macros in the Rust code that are related to the C language standard library. For example, the constant macro DBL_MAX in the C code can be expanded to: (double)1.79769313486231570814527423731704357e+308L.
[0163] Step S20213, clean the intermediate single file to obtain the first single file.
[0164] There may be useless code in the intermediate single file due to programming redundancy or commented code blocks. Useless code increases the amount of conversion code, reduces conversion efficiency, and also affects code readability. Therefore, before conversion, the useless code in the intermediate single file can be deleted to obtain the first single file.
[0165] Exemplarily, useless code may include code and data. There are two conditions for determining useless code: 1) it cannot be executed, that is, the control flow of the program cannot reach the code, or the data flow cannot reach the data; 2) it cannot be traced, that is, the code or data cannot be traced to any system or software requirement.
[0166] In the embodiment of the present application, by merging all the first code files included in the code project to be converted, expanding the macro definitions in the merged initial single file, and cleaning the useless codes therein, a first single file is obtained, which helps to improve the pass rate and conversion efficiency of the successful conversion and save manpower.
[0167] Step S2022: Determine a first identifier set corresponding to the first single file according to the first single file.
[0168] The first identifier set includes the first identifiers in the first single file. The code conversion device can traverse the code of the first single file line by line to determine all the first identifiers included therein. Exemplarily, the identifiers in the C language can include keywords, predefined identifiers and user-defined identifiers.
[0169] Step S2023: Determine, in at least one first code file, a first code file that defines each first identifier.
[0170] Since there is no concept of header files in the Rust language, the C2Rust translation process does not consider the dependencies between C project files, and the first single file after merging will lose the dependencies. In the embodiment of the present application, when merging, the source of the first identifier can be recorded to obtain a mapping table, so as to achieve the purpose of retaining the dependencies between multiple first code files.
[0171] The code conversion device determines the first code file that defines each first identifier in each first code file included in the code item to be converted, that is, determines in which code file each first identifier is defined in the code item to be converted.
[0172] Step S2024: Generate a mapping relationship table according to each first identifier and the path of the first code file defining each first identifier.
[0173] The code conversion device determines the path of the first code file defining each first identifier. Exemplarily, the first identifier can be represented as name, and the path can be represented as path. The corresponding relationship between the first identifier and the path of the first code file defining the first identifier can be represented as MAP<name,path> The correspondence between all first identifiers and their corresponding paths in the first single file is determined, and recorded in a mapping relationship table to obtain a mapping relationship table.
[0174] In an embodiment of the present application, when merging, the correspondence between each first identifier and its source can be recorded to obtain a mapping relationship table, so as to achieve the purpose of retaining the dependency relationship between multiple first code files and provide data support for subsequent reconstruction.
[0175] In order to improve the success rate of conversion, the code that may cause conversion failure in the first single file may be repaired before conversion. Based on this, before step S203 in the above embodiment, the following steps may be performed: Figure 4 Step S21 and step S22 shown:
[0176] Step S21, determining the identifier to be repaired in the first single file that meets the first repair condition.
[0177] The first repair condition is a condition set according to the code that the code conversion tool does not support translation, and the identifier to be repaired that meets the first repair condition is an identifier that the code conversion tool does not support direct translation. According to the preset first repair condition, the identifier to be repaired that needs to be repaired in the first single file is determined.
[0178] Step S22, repairing the identifier to be repaired in the first single file.
[0179] A repair method for the identifier to be repaired is determined according to the first language and the second language, and each identifier to be repaired is repaired respectively to obtain a first single file after repair.
[0180] For example, the following example uses the fact that C language supports enumeration behavior defined in structures, but Rust language does not support enumeration behavior defined in structures. The specific steps are as follows:
[0181] Step S221, determining the abstract syntax tree corresponding to the first single file.
[0182] Exemplarily, the pycparser tool can be used to analyze the C language code in the first single file obtained after merging and useless code cleaning to obtain the abstract syntax tree AST corresponding to the first single file. Among them, pycparser is a C language parser for parsing C language code and converting it into an abstract syntax tree. It can realize various complex analysis and modification of C language code by traversing and operating AST. It also supports obtaining AST node pointer / position for locating and operating specific nodes.
[0183] Step S222: Determine the enumeration definition in the structure according to the abstract syntax tree corresponding to the first single file.
[0184] Exemplarily, it is possible to determine whether the enumeration definition is located in the structure according to the structure identifier (such as struct, { and}) and the enumeration identifier (such as enum, { and}). When it is determined that the enumeration definition in the first single file is located in the structure, the process proceeds to step S213. When it is determined that no enumeration definition in the first single file is located in the structure, the process continues to repair other identifiers to be repaired. After repairing all identifiers to be repaired that meet the first repair condition, the repaired first single file is obtained, and the process proceeds to step S203.
[0185] Step S223, moving the enumeration definition in the structure to outside the structure.
[0186] Exemplarily, the enumeration definition existing in the structure may be moved to the front of the structure according to the line number of the structure.
[0187] For example, the enumeration definition in the structure in the first single file determined according to the abstract syntax tree is as follows:
[0188]
[0189] All enumeration definitions in the structure of the first single file are moved outside the structure to obtain the repaired first single file. The repaired first single file is converted using a code conversion tool to obtain a second single file.
[0190] In the embodiment of the present application, the success rate of translation can be improved by repairing the identifiers to be repaired that may cause unsuccessful translation in the first single file and then performing conversion processing.
[0191] In order to improve the success rate of compilation, the code that may cause compilation failure in the second single file can be repaired before compilation. Based on this, after step S203 in the above embodiment, Figure 4 Step S23 and step S24 shown:
[0192] Step S23, determining the code to be repaired that meets the second repair condition in the second single file.
[0193] The second repair condition is a condition set according to the second language compilation rule, and the code to be repaired that meets the second repair condition is the code that may cause compilation failure. According to the preset second repair condition, the code to be repaired that needs to be repaired in the second single file is determined.
[0194] Step S24, repairing the code to be repaired in the second single file.
[0195] According to the second language, a repair method for the identifier to be repaired is determined, and each identifier to be repaired is repaired respectively to obtain a first single file after repair.
[0196] For example, the code to be repaired may be a constant macro in the Rust code, and the repair method may be to use regular expressions to fully expand the constant macros in the Rust code related to float.h in the C language standard library. For example, the constant macro DBL_MAX in the C code may be expanded to: (double)1.79769313486231570814527423731704357e+308L.
[0197] Exemplarily, when performing repairs, the syn toolkit can be used to parse the Rust code to obtain an abstract syntax tree, traverse the abstract syntax tree, and match and repair other erroneous codes that may cause the target project to fail to compile after translation.
[0198] The following is an example of repairing the naked pointer in the second single file. The specific steps are as follows:
[0199] Step S241, eliminating the naked pointers in the second single file to obtain a preliminary processed single file.
[0200] Exemplarily, the Crown tool can be used to eliminate unnecessary naked pointers in the Rust language code obtained by the preliminary conversion of C2Rust.
[0201] The core of the Crown tool is the static ownership analysis of variables. In Crown, a model for analyzing and reasoning about raw pointer ownership is designed, which can automatically convert raw pointers into the corresponding safety features of the Rust language. Specifically, Crown applies several static analyses to Rust's internal representation system (Medium Internal Representation, MIR) to infer the properties of pointers, which mainly include three types of analysis: 1) Ownership analysis: calculate and infer the ownership information of pointers at specific locations in the code; 2) Mutability analysis: infer which pointers are used to modify the objects they point to; 3) Fatness analysis: distinguish between array pointers and non-array pointers. In Crown, the ownership analysis problem is modeled as a constraint satisfaction problem (SAT), specifically a 3-SAT problem. A solver (such as the z3 SMT solver) can be used to solve the corresponding 3-SAT problem. If the converted 3-SAT problem has no solution, it means that the original C code has a memory leak (revealing that the proposed Crown can not only be used for analysis and optimization of C2Rust translated code, but also for static analysis and improvement of ordinary Rust projects); if there are multiple solutions, a greedy strategy is adopted to select the first solution to generate ownership information. Finally, the ownership is generated according to the solution and the raw pointer is rewritten. Finally, a single file with preliminary processing is obtained.
[0202] Before the compilation pass verification is performed, the naked pointers in the preliminary processing single file are eliminated to reduce the number of naked pointers, which helps to improve the pass rate of successful compilation of the second single file.
[0203] Step S242, compile and check the preliminary processed single file to obtain a compile and check result.
[0204] Verify the compile passability of the preliminary processing single file. If the preliminary processing single file can be compiled, the compilation check result is determined as the preliminary processing single file can be compiled; if the preliminary processing single file cannot be compiled, the compilation check result is determined as the preliminary processing single file cannot be compiled. At this time, the code that cannot be compiled can be repaired according to compiler suggestions or large language models until it can be compiled.
[0205] Step S243, when the compilation check result indicates that the preliminary processing single file can pass the compilation, the naked pointer in the preliminary processing single file is repaired.
[0206] Exemplarily, the Laertes tool can be used to further eliminate unnecessary naked pointers in the Rust language code that can be compiled.
[0207] Among them, Laertes is a C2Rust post-translation unsafe repair solution, which mainly consists of two core components: 1) resolve-import for resolving repeated imports of header file expansion code; and 2) resolve-lifetime for rewriting raw pointers to safe Rust references and repairing the lifetime of generated variables.
[0208] The resolve-lifetime in the Laertes solution rewrites the raw pointer into a safe Rust application, analyzes and infers its ownership information, and generates a lifetime tag for it. resolve-lifetime uses an optimistic rewriting mechanism to rewrite raw pointers (such as structure fields and function parameters) into Rust reference declarations. During the rewriting process, resolve-lifetime assumes: 1) All pointers can be converted to safe references (which may cause compilation errors); 2) All references are borrowed (in fact, some references cannot be borrowed and may need to be rolled back); 3) Each lifetime in function and type signatures is independent (with different type variables). Under these assumptions, resolve-lifetime rewrites pointers to references, and then calls the Rust compiler as a discriminator to iteratively eliminate compilation errors that may be caused by assumptions. Note that in order to deal with the raw pointer situation during the rewriting process, resolve-lifetime converts the raw pointer into an option reference, and then repeatedly calls the Rust compiler to obtain error information and selectively roll back the rewriting content.
[0209] After the compilation pass verification, the naked pointers in the preliminary processing single file are eliminated to further reduce the number of naked pointers and improve the pass rate of successful compilation of the target project.
[0210] In the embodiment of the present application, after the second single file is translated, the code in the second single file that may cause the failure of successful compilation is repaired, so as to improve the success rate of the second single file compilation.
[0211] In practical applications, based on actual conditions, the first single file can be directly converted, or the first single file can be repaired before conversion; when compiling, the second single file can be directly compiled, or the second single file can be repaired before compilation. This application embodiment is not limited.
[0212] In some embodiments, step S204 in the above embodiment, "generating at least one second code file according to the second identifier and the mapping relationship table of the second single file", can be performed by Figure 5 Steps S2041 to S2044 shown are used to implement:
[0213] Step S2041: Create at least one file with the same name based on the second language according to the path of the first code file included in the mapping relationship table.
[0214] For example, the mapping relationship table MAP<name,path> The file paths included in the path are deduplicated, and corresponding files with the same name are created according to the remaining file paths. For example, after deduplication of the paths, the file paths corresponding to the C language project include two files, path 1.c and path 2.h. Then the files with the same name created for the Rust language can be path 1.rs and path 2.rs. Among them, .rs is the suffix of the Rust language file. The number of files with the same name created is equal to the number of file paths stored in the mapping relationship table. If the C language project does not include redundant first code files, the number of files with the same name is equal to the number of first code files.
[0215] In some embodiments, after creating a file with the same name, a lib.rs file can be created in the root directory of the Rust language project, and the created path1.rs and path2.rs files are added to the lib.rs file as mod declarations.
[0216] Step S2042: Determine a second identifier set corresponding to the second single file according to the second single file.
[0217] Exemplarily, the sys tool may be used to analyze and process the second file to determine an abstract syntax tree corresponding to the second file, traverse the abstract syntax tree corresponding to the second file, determine identifiers in the second file, and obtain a second identifier set corresponding to the second file.
[0218] Step S2043: Match each identifier in the second identifier set with the first identifier included in the mapping relationship table to obtain a matching result.
[0219] Traverse each identifier in the second identifier set and compare each identifier with MAP<name,path> Match the name set in to get the matching result.
[0220] Step S2044: reconstruct the second single file based on the matching result and at least one file with the same name to obtain at least one second code file.
[0221] Determine whether the matching result of each identifier in the second identifier set indicates that the second identifier matches the target identifier included in the mapping relationship table. If the current identifier does not match, continue the judgment process of the next identifier. If it matches, mark the current identifier as the second identifier, and continue the judgment process of the next identifier. Until all identifiers in the second identifier set are judged, all matching second identifiers are obtained. Then, based on all matching second identifiers and the created files with the same name, reconstruct the second single file to obtain at least one second code file. The number of second code files is equal to the number of files with the same name.
[0222] In some embodiments, each second code file may be determined according to the following steps:
[0223] Step S20441, when the matching result indicates that the second identifier in the second identifier set matches the target identifier included in the mapping relationship table, the definition of the second identifier is determined in the second single file, and a target file with the same name as the path of the first code file that defines the target identifier is determined in at least one file with the same name.
[0224] Step S20442, moving the definition of the second identifier in the second single file to the target file.
[0225] Step S20443: Add a reference to the second identifier at a location where the second identifier is used in at least one file with the same name, to obtain at least one second code file.
[0226] Migrate the definition of the second identifier that is successfully matched in the second single file to the file with the same name in the file path corresponding to the first identifier that matches the second identifier. For example, migrate the definition of the identifier 1 that is successfully matched in the Rust single file to the rs file with the same name in the path corresponding to the name that matches the identifier 1. At the same time, add a use reference to the place where the Rust single file uses the identifier 1 to repair the dependency and reconstruct the original project file structure. After the processing is completed, all the second code files included in the target project are obtained.
[0227] In the embodiment of the present application, the target project reconstructed based on the mapping relationship table has a file structure similar to that of the original code project to be converted, and there is no need for manual rewriting and reconstruction, which can save manpower, material and financial resources and achieve the purpose of quickly and accurately translating the code.
[0228] Based on the embodiments of the aforementioned code conversion method, the embodiments of the present application also provide a code conversion device, and the modules included in the device, as well as the units included in the modules, can be implemented by a processor in a computing device; of course, they can also be implemented by a specific logic circuit; in the implementation process, the processor can be a central processing unit (CPU), a microprocessor (MPU), a digital signal processor (DSP) or a field programmable gate array (FPGA), etc.
[0229] Figure 6 A schematic diagram of a structure of a code conversion device provided in an embodiment of the present application is shown in FIG. Figure 6 As shown, the code conversion device 600 may include:
[0230] An acquisition module 601 is used to acquire a code project to be converted, wherein the code project to be converted includes at least one first code file written based on a first language;
[0231] A first generating module 602 is used to generate a first single file and a mapping relationship table according to the code project to be converted; the mapping relationship table is used to store the correspondence between the first identifier in the first single file and the path of the first code file defining the first identifier, and each first identifier in the first single file corresponds to one of the first code files in the code project to be converted;
[0232] A conversion module 603 is used to convert the first single file to obtain a second single file; the second single file is a code file written in a second language, and the second single file includes a second identifier converted from the first identifier in the first single file;
[0233] A second generating module 604, configured to generate at least one second code file according to the second identifier of the second single file and the mapping relationship table;
[0234] The first determination module 605 is used to determine a target project, where the target project at least includes the at least one second code file.
[0235] In a possible implementation, the acquisition module 601 is further configured to:
[0236] Obtaining at least one initial code file written in a first language;
[0237] Compile and check the at least one initial code file to obtain the code project to be converted that can pass the compilation.
[0238] In a possible implementation, the first generating module 602 is further configured to:
[0239] Integrate the at least one first code file included in the code project to be converted to obtain the first single file;
[0240] Determine, according to the first single file, a first identifier set corresponding to the first single file, where the first identifier set includes a plurality of first identifiers;
[0241] In the at least one first code file, determining a first code file defining each of the first identifiers;
[0242] The mapping relationship table is generated according to each of the first identifiers and a path of a first code file defining each of the first identifiers.
[0243] In a possible implementation, the first generating module 602 is further configured to:
[0244] Merging the first code files included in the code project to be converted to obtain an initial single file;
[0245] Expanding the macro definition in the initial single file to obtain an intermediate single file;
[0246] The intermediate single file is cleaned to obtain the first single file.
[0247] In a possible implementation, the code conversion device 600 may further include: a second determination module and a first repair processing module;
[0248] The second determining module is used to determine the identifier to be repaired in the first single file that meets the first repair condition;
[0249] The first repair processing module is used to perform repair processing on the identifier to be repaired in the first single file.
[0250] In a possible implementation, the code conversion device 600 may further include: a third determination module and a second repair processing module;
[0251] The third determining module is used to determine the code to be repaired that meets the second repair condition in the second single file;
[0252] The second repair processing module is used to perform repair processing on the code to be repaired in the second single file.
[0253] In a possible implementation, the second generating module 604 is further configured to:
[0254] According to the path of the first code file included in the mapping relationship table, create at least one file with the same name based on the second language;
[0255] Determine, according to the second single file, a second identifier set corresponding to the second single file;
[0256] Matching each identifier in the second identifier set with the first identifier included in the mapping relationship table to obtain a matching result;
[0257] Based on the matching result and the at least one file with the same name, the second single file is reconstructed to obtain the at least one second code file.
[0258] In a possible implementation, the second generating module 604 is further configured to:
[0259] In a case where the matching result indicates that the second identifier in the second identifier set matches the target identifier included in the mapping relationship table, determining a definition of the second identifier in the second single file, and determining a target file with the same name as the path of the first code file defining the target identifier in the at least one file with the same name;
[0260] Move the definition of the second identifier in the second single file to the target file;
[0261] A reference to the second identifier is added to a position where the second identifier is used in the at least one file with the same name, so as to obtain the at least one second code file.
[0262] It should be noted that the description of the above code conversion device embodiment is similar to the above code conversion method description, and has the same beneficial effects as the method embodiment. For technical details not disclosed in the code conversion device embodiment of the present application, those skilled in the art should refer to the description of the method embodiment of the present application for understanding.
[0263] It should be noted that in the embodiment of the present application, if the above method is implemented in the form of a software function module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which 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 execute all or part of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a U disk, a mobile hard disk, a read-only memory (ROM), a disk or an optical disk. In this way, the embodiment of the present application is not limited to any specific combination of hardware and software.
[0264] Accordingly, an embodiment of the present application provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps in the code conversion method provided in the above embodiment are implemented.
[0265] The present application embodiment provides a computing device, Figure 7 A schematic diagram of a structure of a computing device provided in an embodiment of the present application, which can be implemented as the above Figure 1 The second device in the embodiment shown. Figure 7The exemplary structure of the computing device 700 shown may foresee other exemplary structures of the computing device 700, and thus the structure described here should not be considered as limiting. For example, some components described below may be omitted, or components not described below may be added to meet the special needs of certain applications.
[0266] Figure 7 The computing device 700 shown includes: a processor 701, at least one communication bus 702, a user interface 703, at least one external communication interface 704 and a memory 705. The communication bus 702 is configured to realize the connection and communication between these components. The user interface 703 may include a display screen, and the external communication interface 704 may include a standard wired interface and a wireless interface. The processor 701 is configured to execute the program of the code conversion method stored in the memory to implement the steps in the code conversion method provided in the above embodiment.
[0267] The description of the above computing device and storage medium embodiments is similar to the description of the above method embodiments, and has similar beneficial effects as the method embodiments. For technical details not disclosed in the computing device and storage medium embodiments of this application, please refer to the description of the method embodiments of this application for understanding.
[0268] It should be understood that "one embodiment" or "an embodiment" mentioned throughout the specification means that specific features, structures or characteristics related to the embodiment are included in at least one embodiment of the present application. Therefore, "in one embodiment" or "in an embodiment" appearing throughout the specification does not necessarily refer to the same embodiment. In addition, these specific features, structures or characteristics can be combined in one or more embodiments in any suitable manner. It should be understood that in various embodiments of the present application, the size of the sequence number of the above-mentioned processes does not mean the order of execution, and the execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application. The above-mentioned sequence numbers of the embodiments of the present application are only for description and do not represent the advantages and disadvantages of the embodiments.
[0269] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or 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 device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0270] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. 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 can be integrated into another system, or some features can be ignored or not executed. In addition, the coupling, direct coupling, or communication connection between the components shown or discussed can be through some interfaces, and the indirect coupling or communication connection of the devices or units can be electrical, mechanical or other forms.
[0271] The units described above as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units; 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 present embodiment.
[0272] In addition, all functional units in the embodiments of the present application may be integrated into one processing unit, or each unit may be a separate unit, or two or more units may be integrated into one unit; the above-mentioned integrated units may be implemented in the form of hardware or in the form of hardware plus software functional units.
[0273] A person of ordinary skill in the art can understand that: all or part of the steps of implementing the above-mentioned method embodiment can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps of the above-mentioned method embodiment; and the aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, ROMs, magnetic disks or optical disks.
[0274] Alternatively, if the above-mentioned integrated unit of the present application is implemented in the form of a software function module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application can essentially or in other words, the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a device to execute all or part of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, ROMs, magnetic disks or optical disks.
[0275] The implementation methods described above are only specific implementation methods of the present application and are not intended to limit the scope of protection of the present application. Any modifications, equivalent substitutions, improvements, etc. made on the basis of the technical solution of the present application should be included in the scope of protection of the present application.
Claims
1. A code conversion method, characterized in that: include: Acquire a code project to be converted, wherein the code project to be converted includes at least one first code file written based on a first language; Generate a first single file and a mapping relationship table according to the code project to be converted; the mapping relationship table is used to store the correspondence between the first identifier in the first single file and the path of the first code file defining the first identifier, each first identifier in the first single file corresponds to one of the first code files in the code project to be converted; Converting the first single file to obtain a second single file; the second single file is a code file written in a second language, and the second single file includes a second identifier converted from the first identifier in the first single file; Generate at least one second code file according to the second identifier of the second single file and the mapping relationship table; A target project is determined, where the target project at least includes the at least one second code file.
2. The method according to claim 1, characterized in that The step of obtaining the code item to be converted includes: Obtaining at least one initial code file written in a first language; Compile and check the at least one initial code file to obtain the code project to be converted that can pass the compilation.
3. The method according to claim 1, characterized in that The step of generating a first single file and a mapping relationship table according to the code project to be converted comprises: Integrate the at least one first code file included in the code project to be converted to obtain the first single file; Determine, according to the first single file, a first identifier set corresponding to the first single file, where the first identifier set includes a plurality of first identifiers; In the at least one first code file, determining a first code file defining each of the first identifiers; The mapping relationship table is generated according to each of the first identifiers and a path of a first code file defining each of the first identifiers.
4. The method according to claim 3, characterized in that The step of integrating the at least one first code file included in the code project to be converted to obtain the first single file includes: Merging the first code files included in the code project to be converted to obtain an initial single file; Expanding the macro definition in the initial single file to obtain an intermediate single file; The intermediate single file is cleaned to obtain the first single file.
5. The method according to claim 1, characterized in that Before converting the first single file to obtain the second single file, the method further includes: Determine an identifier to be repaired in the first single file that meets a first repair condition; Repairing processing is performed on the identifier to be repaired in the first single file.
6. The method according to claim 5, characterized in that After converting the first single file to obtain a second single file, the method further includes: Determine the code to be repaired that meets the second repair condition in the second single file; Repair the code to be repaired in the second single file.
7. The method according to any one of claims 1 to 6, characterized in that The generating at least one second code file according to the second identifier of the second single file and the mapping relationship table comprises: According to the path of the first code file included in the mapping relationship table, create at least one file with the same name based on the second language; Determine, according to the second single file, a second identifier set corresponding to the second single file; Matching each identifier in the second identifier set with the first identifier included in the mapping relationship table to obtain a matching result; Based on the matching result and the at least one file with the same name, the second single file is reconstructed to obtain the at least one second code file.
8. The method according to claim 7, characterized in that The reconstructing the second single file based on the matching result and the at least one file with the same name to obtain the at least one second code file includes: In a case where the matching result indicates that the second identifier in the second identifier set matches the target identifier included in the mapping relationship table, determining a definition of the second identifier in the second single file, and determining a target file with the same name as the path of the first code file defining the target identifier in the at least one file with the same name; Move the definition of the second identifier in the second single file to the target file; A reference to the second identifier is added to a position where the second identifier is used in the at least one file with the same name, so as to obtain the at least one second code file.
9. A computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the code conversion method according to any one of claims 1 to 8.
10. A computing device, comprising: Processor and memory; The processor is coupled to the memory; The memory is used to store executable instructions; The processor is used to read the instruction from the memory and execute the instruction to perform the code conversion method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Program language conversion method and device, equipment and storage medium
CN117093228A
Code conversion method and device, electronic equipment and computer readable medium
CN117555542A
Code obfuscation method and device
CN118981756A
Automated merge conflict resolution
US20220164672A1
Methods and systems for shared language framework for composability of software translativity of information
WO2023122344A1
Cited By
Code language conversion verification method and device, electronic equipment, storage medium and computer program product
CN120803464A
Code language conversion method and device, electronic equipment, storage medium and computer program product
CN120803467A