Transcoding method, device and computer readable storage medium
By generating a mapping table and merging processes, the problems of duplicate data structure definitions, missing dependencies, and variable naming conflicts in C2Rust translation are resolved, achieving fast and accurate conversion from C to Rust, improving conversion efficiency and reducing manual rewriting work.
Patent Information
- Application Number
- CN202411889210.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-20
- Publication Date
- 2026-01-13
- Estimated Expiration
- 2044-12-20
AI Technical Summary
The C2Rust translation process suffers from issues such as duplicated data structure definitions, missing dependencies, and variable naming conflicts, which affect the accuracy of code conversion.
The above problems are solved by generating a mapping table to record the path relationship between the first identifier and the code file that defines it, merging the code files, performing transformation processing, and refactoring the target project.
It improves the pass rate and efficiency of code conversion, ensures that the file structure of the target project is similar to that of the original code project, saves manpower and resources, and achieves fast and accurate code conversion.
Smart Images

Figure CN119847537B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] Embodiments of the present application relate to the technical field of computer, and particularly relate to a code conversion method, device and computer readable storage medium. BACKGROUND
[0002] Rust is a new static programming language characterized by memory safety. Compared with traditional C / C++ programming languages, Rust introduces the concepts of lifetime and ownership, can ensure memory safety on the premise of obtaining approximate performance, and greatly avoids memory leaks, dangling pointers, buffer overflows, and data races in concurrent programming. Due to the excellent performance of Rust in memory safety and performance, Rust 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 most popular code conversion tools. However, there is no concept of header file in Rust language, and the C2Rust translation process may have the problem of repeated translation of structures defined in the header file of the C project, which affects the accuracy. SUMMARY
[0003] Embodiments of the present application provide a code conversion method, device and computer readable storage medium, which can solve the problems of repeated definition of data structure, missing dependencies and variable naming conflicts caused by C2Rust translation, and improve the accuracy of converting C pre-research into Rust language.
[0004] To achieve the above object, the embodiments of the present application adopt the following technical solutions:
[0005] In a first aspect, the embodiments of the present application provide a code conversion method, which comprises:
[0006] obtaining a code project to be converted, the code project to be converted comprising at least one first code file written in a first language;
[0007] generating 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 corresponding relationship between a first identifier in the first single file and the path of a first code file defining the first identifier, and each first identifier in the first single file corresponds to one first code file in the code project to be converted;
[0008] performing conversion processing on 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 comprises a second identifier converted from a first identifier in the first single file;
[0009] generating at least one second code file according to the second identifier of the second single file and the mapping relationship table;
[0010] determining a target project, the target project at least including the at least one second code file.
[0011] The code conversion method provided by the embodiments of the present application can solve the problems of repeated definition of data structure, missing dependency and variable naming conflict when converting the first language code project to the second language code project, and help to improve the code conversion pass rate and efficiency. The target project obtained based on the mapping relationship table is similar to the file structure of the original code project to be converted, and does not need to be manually rewritten and reconstructed, which can save manpower, material resources and financial resources, and achieve the purpose of quickly and accurately converting the code.
[0012] In a possible implementation, the obtaining the code project to be converted includes:
[0013] obtaining at least one initial code file written based on a first language;
[0014] performing a compilation checking process on the at least one initial code file to obtain the code project to be converted which can pass the compilation.
[0015] According to the present solution, the initial code file is verified by compilation to obtain the code project to be converted which can pass the compilation, which can reduce the compilation errors of the code project to be converted, ensures that the input code project to be converted meets the writing standard of the first language, and helps to improve the code conversion pass rate and efficiency.
[0016] In a possible implementation, the generating the first single file and the mapping relationship table according to the code project to be converted includes:
[0017] performing an integration process on the at least one first code file included in the code project to be converted to obtain the first single file;
[0018] determining a first identifier set corresponding to the first single file according to the first single file, the first identifier set including a plurality of first identifiers;
[0019] determining, in the at least one first code file, a first code file defining each of the first identifiers;
[0020] generating the mapping relationship table according to each of the first identifiers and the path of the first code file defining each of the first identifiers.
[0021] Based on the scheme, the first code file is integrated to obtain the single file to be converted, which can solve the problems of repeated definition of data structure, missing dependency and variable naming conflict when the first language is translated into the second language. The correspondence between the identifier and the file path is recorded during the merging, which provides data support for subsequent reconstruction of target projects with similar file structures.
[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 comprises:
[0023] The first code files included in the code project to be converted are merged to obtain an initial single file;
[0024] The macro definition in the initial single file is expanded to obtain an intermediate single file;
[0025] The intermediate single file is cleaned to obtain the first single file.
[0026] Based on the scheme, the code project to be converted is processed by file merging, macro definition expansion and useless code cleaning, which can solve the problems of repeated definition of data structure, missing dependency and variable naming conflict when the first language code project is translated into the second language code project.
[0027] In a possible implementation, before the first single file is converted to obtain a second single file, the method further comprises:
[0028] The identifier to be repaired in the first single file that meets the first repair condition is determined;
[0029] The identifier to be repaired in the first single file is repaired.
[0030] Based on the scheme, before the first single file is converted, the identifier that may cause conversion failure in the first single file is repaired, and then the conversion is performed, which can improve the success rate of conversion.
[0031] In a possible implementation, the repairing the identifier to be repaired in the first single file comprises:
[0032] The abstract syntax tree corresponding to the first single file is determined;
[0033] The enumeration definition located in the structure is determined according to the abstract syntax tree corresponding to the first single file;
[0034] The enumeration definition located in the structure is moved out of the structure.
[0035] Based on the scheme, the enumeration definition in the structure that may cause unsuccessful translation in the first single file to be converted is moved out of the structure through preprocessing, so that the success rate of translation is improved.
[0036] In a possible implementation, after the first single file is converted to obtain the second single file, the method further includes:
[0037] Determining the to-be-repaired code in the second single file that meets a second repair condition;
[0038] Repairing the to-be-repaired code in the second single file.
[0039] Based on the scheme, the code that may cause compilation failure in the second single file is repaired before the second single file is reconstructed, so that the success rate of successful compilation of the target project determined based on the second single file is improved.
[0040] In a possible implementation, the repairing the to-be-repaired code in the second single file includes:
[0041] Eliminating the naked pointer in the second single file to obtain a preliminary processing single file;
[0042] Performing a compilation check on the preliminary processing single file to obtain a compilation check result;
[0043] In a case where the compilation check result indicates that the preliminary processing single file is compilable, repairing the naked pointer in the preliminary processing single file.
[0044] Based on the scheme, the second single file obtained through conversion is verified by compiling, and the naked pointer in the second single file that may cause unsuccessful compilation is repaired before and after the verification, so that the success rate of successful compilation of the target project determined based on the second single file is improved.
[0045] In a possible implementation, the generating at least one second code file according to the second identifiers of the second single file and the mapping relationship table includes:
[0046] Creating at least one same-named file based on a second language according to the path of the first code file included in the mapping relationship table;
[0047] Determining a second identifier set corresponding to the second single file according 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] Reconstruct the second single file based on the matching result and the at least one same-name file to obtain the at least one second code file.
[0050] According to the scheme, the target project reconstructed based on the mapping relationship table is similar to the file structure of the original code project to be converted, and manual rewriting and reconstruction are not required, thereby saving manpower, material resources and financial resources.
[0051] In a possible implementation, the reconstructing the second single file based on the matching result and the at least one same-name file to obtain the at least one second code file includes:
[0052] In a case where the matching result indicates that a second identifier in the second identifier set matches a 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 that is same in name as a path of a first code file defining the target identifier in the at least one same-name file;
[0053] moving the definition of the second identifier in the second single file to the target file;
[0054] adding a reference of the second identifier to a position where the second identifier is used in the at least one same-name file to obtain the at least one second code file.
[0055] According to the scheme, the definition of the matched identifier is moved to the corresponding same-name file, and a reference of the identifier is added to a position where the identifier is used, so that the target project is similar to the file structure of the original code project to be converted.
[0056] In a second aspect, an embodiment of the present application provides a code conversion device, and the device includes:
[0057] The obtaining module is configured to obtain a code project to be converted, the code project to be converted including at least one first code file written based on a first language;
[0058] The first generating module is configured 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 a corresponding relationship between a first identifier in the first single file and a path of a first code file defining the first identifier, and each first identifier in the first single file corresponds to one first code file in the code project to be converted;
[0059] a conversion module, configured to perform conversion processing on the first single file to obtain a second single file; the second single file is a code file written based on a second language, and the second single file includes a second identifier converted from a first identifier in the first single file;
[0060] a second generation 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] a first determination module, configured to determine a target item, the target item including at least the at least one second code file.
[0062] In a possible implementation, the acquisition module is further configured to:
[0063] acquire at least one initial code file written based on a first language;
[0064] perform compilation checking processing on the at least one initial code file to obtain the code item to be converted that can pass the compilation.
[0065] In a possible implementation, the first generation module is further configured to:
[0066] perform integration processing on the at least one first code file included in the code item 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, the first identifier set including a plurality of first identifiers;
[0068] determine, in the at least one first code file, a first code file defining each of the first identifiers;
[0069] generate the mapping relationship table according to each of the first identifiers and a path of the first code file defining each of the first identifiers.
[0070] In a possible implementation, the first generation module is further configured to:
[0071] perform merging processing on each first code file included in the code item to be converted to obtain an initial single file;
[0072] perform expansion processing on a macro definition in the initial single file to obtain an intermediate single file;
[0073] perform cleaning processing on the intermediate single file to obtain the first single file.
[0074] In a possible implementation, the code conversion apparatus can further include a second determination module and a first repair processing module.
[0075] The second determining module is configured to determine identifiers to be repaired in the first single file that meet a first repair condition.
[0076] The first repair processing module is configured to perform repair processing on the identifiers 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 an enumeration definition located in a structure according to the abstract syntax tree corresponding to the first single file;
[0080] move the enumeration definition located in the structure to outside the structure.
[0081] In a possible implementation, the code conversion apparatus can further include a third determining module and a second repair processing module.
[0082] The third determining module is configured to determine code to be repaired in the second single file that meets a second repair condition.
[0083] The second repair processing module is configured 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] perform elimination processing on a naked pointer in the second single file to obtain a preliminary processing single file;
[0086] perform a compilation check on the preliminary processing single file to obtain a compilation check result;
[0087] perform repair processing on the naked pointer in the preliminary processing single file in a case where the compilation check result represents that the preliminary processing single file is compilable.
[0088] In a possible implementation, the second generating module is further configured to:
[0089] create at least one same-name file based on a second language according to a path of a first code file included in the mapping relationship table;
[0090] determine a second identifier set corresponding to the second single file according to the second single file;
[0091] perform matching on each identifier in the second identifier set and a first identifier included in the mapping relationship table to obtain a matching result.
[0092] reconstruct the second single file based on the matching result and the at least one same-name file, 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 a second identifier in the second identifier set matches a 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 that is same in name as a path of a first code file defining the target identifier in the at least one same-name file;
[0095] moving the definition of the second identifier in the second single file to the target file;
[0096] adding a reference of the second identifier to a position where the second identifier is used in the at least one same-name file, 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, the storage medium storing a computer program, the computer program being executed to implement the code conversion method in any one of the first aspect.
[0098] In a fourth aspect, an embodiment of the present application provides a computing device, including a processor and a memory; the processor and the memory are coupled;
[0099] The memory is configured to store executable instructions;
[0100] The processor is configured to read the instructions from the memory, and execute the instructions to perform the code conversion method in any one of the first aspect.
[0101] In a fifth aspect, an embodiment of the present application provides a chip, the chip being configured to perform the code conversion method in any one of the first aspect.
[0102] In a sixth aspect, an embodiment of the present application provides a program product, when instructions in the program product are executed, the code conversion method in any one of the first aspect is implemented. BRIEF DESCRIPTION OF DRAWINGS
[0103] Figure 1 a network architecture schematic diagram of a code conversion system provided by an embodiment of the present application;
[0104] Figure 2 a flowchart schematic diagram of an implementation of a code conversion method provided by an embodiment of the present application;
[0105] Figure 3 Another implementation flowchart of the code conversion method provided by the embodiment of the present application is shown in the figure.
[0106] Figure 4 Another implementation flowchart of the code conversion method provided by the embodiment of the present application is shown in the figure.
[0107] Figure 5 Another implementation flowchart of the code conversion method provided by the embodiment of the present application is shown in the figure.
[0108] Figure 6 A component structure diagram of the code conversion device provided by the embodiment of the present application is shown in the figure.
[0109] Figure 7 A component structure diagram of the computing device provided by the embodiment of the present application is shown in the figure. DETAILED DESCRIPTION
[0110] The technical solutions in the embodiments of the present application will be described below with reference to the drawings in the embodiments of the present application. In order to clearly describe the technical solutions in the embodiments of the present application, the first, second, etc. descriptions in the embodiments of the present application are only used for indicating and distinguishing the description objects, and do not have the order, nor represent the special limitation of the number of devices in the embodiments of the present application, which cannot constitute any limitation on the embodiments of the present application.
[0111] Before the embodiments of the present application are further described in detail, the terms and phrases involved in the embodiments of the present application are first described, which are applicable to the following explanations.
[0112] 1) C2Rust is a tool that automatically converts C language code to Rust language code, helping developers to 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 source code, which presents the syntax structure of programming language in 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, etc.
[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 compilation toolchain based on LLVM that focuses on speed, size, and Web platforms, and is used to translate C / C++ projects or any other project compiled using LLVM into WebAssembly (wsam), allowing C / C++ projects to run on browsers, node.js, or wsam runtime.
[0116] Among them, wsam is a virtual instruction set architecture (ISA), and the overall architecture includes the definition of core ISA, binary encoding, program semantics definition and execution, and application programming interface (API) for different embedded environments (such as Web).
[0117] 5) Relooper algorithm is an algorithm for optimizing loop structures, especially in compiler design to improve code execution efficiency and performance. It reorganizes loop structures, reduces the nesting depth of loops, and eliminates unnecessary control flow dependencies to improve program execution speed. Relooper algorithm is particularly suitable for program code with complex loops and conditional branches, and through optimizing loop structures, the program becomes more efficient, easy to understand and maintain.
[0118] 6) Laertes is a tool for optimizing and repairing unsafe in Rust code translated from C2Rust, which consists of two core components: resolve-import and resolve-lifetime. resolve-import is used to eliminate duplicate definitions of data structures and solve the problem of repeated import of header file (suffix name ".h") expansion code, and resolve-lifetime is used to repair missing dependencies, rewrite naked pointers to safe Rust references, and repair variable lifetime generation.
[0119] 7) Crown is a tool for optimizing and repairing raw pointers in Rust code transpiled from C2Rust. It employs a model to analyze and infer ownership of raw pointers, automatically converting them to Rust's corresponding security features. Unlike Laertes, which relies on the Rust compiler for raw pointer rewriting, Crown models ownership analysis as a constraint satisfaction problem (such as 3-SAT) and solves it using a solver. The analysis and inference process is independent of the Rust compiler, resulting in faster speed. Furthermore, Crown, based on ownership analysis, is accurate and scalable, capable of handling 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. It is typically represented by *const T for an immutable raw pointer and *mut T for a mutable raw pointer. Raw pointers are characterized as follows: unsafe, can point to null or arbitrary memory addresses, have no lifetime, and are not bound by Rust's ownership system. They are usually used for interacting with the underlying operating system or hardware.
[0121] The following describes exemplary applications of the computing device provided in the embodiments of this application. The computing device provided in the embodiments of this application can be implemented as a device capable of code conversion, such as a computer. Exemplary applications when the computing device is implemented as a computer will be described below.
[0122] See Figure 1 , Figure 1 This is a schematic diagram of the network architecture of the code conversion system provided in the embodiments of this application, such as... Figure 1 As shown, the code conversion system 10 includes a first device 100, a network 200, and a second device 300. The first device 100 is communicatively connected to the second device 300 via the network 200. The network 200 can be a wide area network (WAN), a local area network (LAN), or a combination of both. The first device 100 and the second device 300 can communicate directly or indirectly via wired or wireless links to achieve data transmission.
[0123] For example, the code conversion system 10 provided in this application embodiment can be applied to scenarios where C language projects are translated into Rust language projects. The first device 100 can be any type of computing device capable of providing C language projects, such as a server, laptop, tablet, desktop computer, set-top box, mobile device (e.g., mobile phone, portable music player, personal digital assistant, dedicated messaging device, portable gaming device), etc.
[0124] The second device 300 can be a computing device for code conversion, such as a server, a notebook computer, 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 game device), and various types of arbitrary devices capable of translating the C language project provided by the first device 100 into a Rust language project. Among them, the second device 300 is provided with a code conversion tool. Exemplarily, the code conversion tool can be a C2Rust tool.
[0125] Exemplarily, in one application scenario of the code conversion system 10, the first device 100 stores a original project written by a developer based on a first language, and the original project includes at least one initial code file. When code conversion is needed, 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 the at least one initial code file included in the original project written based on the first language transmitted by the first device 100, the second device 300 can perform a compilation checking process on the original project to obtain a convertible code project that can pass compilation, and the convertible code project includes at least one first code file that can pass compilation and is written based on the first language. Then, according to the convertible code project, the second device 300 determines a first single file to be converted and a mapping relationship table. The mapping relationship table is used to store a correspondence between a first identifier in the first single file and a path of a first code file corresponding to the first identifier, and the first code file corresponding to the first identifier is a code file defining the first identifier. Then, the second device 300 performs a conversion process on the first single file to obtain a converted second single file, and the second single file is a code file written based on a second language. Finally, the second device 300 determines a target project, and the target project includes at least one second code file generated according to the mapping relationship table and a second identifier in the second single file.
[0127] Exemplarily, the first language can be C language, and the corresponding convertible code project is a C language project; the second language can be Rust language, and the corresponding target project can be a Rust language project. Through the code conversion system, the problems of repeated definition of data structure, missing dependencies, and variable naming conflicts caused by C2Rust translation can be solved, the C language project can be quickly translated into a Rust language project, and the file structure of the Rust language project is similar to that of the original C language project, without the need for manual rewriting and refactoring, etc. The code conversion system can save manpower, material resources and financial resources, and achieve the purpose of quickly and accurately translating C language code to Rust language code.
[0128] Exemplarily, 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 code conversion are the same device. The implementation principle is similar to the above-mentioned application scenario, which will not be described here again.
[0129] The code conversion system provided by the embodiment of the present application firstly acquires a code project to be converted written based on a first language; then generates a first single file and a mapping relationship table according to at least one first code file included in the code project to be converted, the mapping relationship table being used to store a corresponding relationship between a first identifier in the first single file and a path of a first code file defining the first identifier; further performs conversion processing on the first single file to obtain a second single file, the second single file being a code file written based on a second language, the second single file including a second identifier converted from the first identifier in the first single file; finally generates at least one second code file according to the second identifier of the second single file and the mapping relationship table, and determines a target project 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 the first language is translated into the second language can be solved, the code translation efficiency 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, so that manual rewriting and reconstruction and the like are not needed, manpower, material resources and financial resources can be saved, and the purpose of quickly and accurately translating code is achieved.
[0130] The code conversion method provided by the embodiment of the present application will be described below in combination with an exemplary application and implementation of a computing device provided by the embodiment of the present application.
[0131] Figure 2 An implementation flowchart of the code conversion method provided by the embodiment of the present application is applied to Figure 1 The second device in the code conversion system shown in FIG. 1. As shown in Figure 2 The code conversion method includes the following steps:
[0132] Step S201, acquiring a code project to be converted.
[0133] The method provided by the embodiment of the present application can be executed by a computing device, and specifically can be executed by a code conversion apparatus in the computing device. Exemplarily, the code conversion method provided by 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, and 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 by the embodiment of the present application. The code conversion method provided by the embodiment of the present application will be exemplarily described below taking the first language as C language and the second language as Rust language.
[0134] The code item to be converted obtained by the code conversion device can include one or more first code files, and the first code files are code files written based on a first language. For example, the code item to be converted can be a C language item.
[0135] In some embodiments, the code conversion device can obtain the code item to be converted by first obtaining at least one initial code file written based on a first language, and then performing a compilation check process on each initial code file to obtain a code item to be converted that can pass compilation.
[0136] For example, the initial code file can be a code file written by a developer, and for example, the initial code file of a C language item can be a file with a suffix of.c or.h. The code conversion device can perform a compilation pass check on the initial code file. 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 successfully pass compilation, and at this time, each initial code file is used as a first code file to form a code item to be converted. For example, the configuration file can be a CMake configuration file. CMake is a cross-platform automated build tool, mainly used to manage the build process of software, which can control the compilation and linking process to generate executable files or libraries that can run on different platforms.
[0137] If there is no CMake configuration file in the initial code file, the CMake tool can be used to organize the project, and then the compilation pass of each initial code file after project organization is verified. If each initial code file after project organization can successfully pass compilation, each initial code file after project organization is determined as a first code file to form a code item to be converted. If each initial code file after project organization fails to pass compilation, it can be repaired according to the compilation error until it can successfully pass compilation, and the finally obtained code file that can successfully pass compilation is used to form a code item to be converted.
[0138] Step S202, generating a first single file and a mapping relationship table according to the code item to be converted.
[0139] Since C2Rust only supports single file conversion, direct conversion of the code item to be converted including multiple first code files will generate multiple Rust files, which requires a lot of manpower for reconstruction. In order to reduce the workload and save manpower, in the embodiments of the present application, before conversion, the multiple first code files included in the code item 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 direct merging may cause a problem of missing dependencies. In the embodiment of the application, in order to preserve the dependencies between multiple files, when merging, the definition path of each first identifier in the first single file in the pre-merged code project to be converted can be determined according to the path of each first code file in the pre-merged code project to be converted and the first single file after merging, and a mapping relationship table is generated, which is used to store the corresponding relationship between the first identifier in the first single file and the path of the first code file defining the first identifier.
[0141] In order to distinguish between identifiers in the first language code and identifiers in the second language code, in the embodiment of the application, the identifiers in the first language code are referred to as first identifiers, and the identifiers in the second language code are referred to as second identifiers. The first identifiers and the second identifiers do not represent the order of the identifiers.
[0142] The first identifiers in the first single file generated by the C language project can include three types: keywords, pre-defined identifiers, and user-defined identifiers. The keyword, also known as a reserved word, is a string with a specific meaning specified by the C language, such as int, char, float, return, etc. The pre-defined identifier is a system pre-defined identifier, for example, a library function name (such as printf, scanf), a macro definition, and a type alias, etc. The user-defined identifier is an identifier defined by the user as needed, which is 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] For example, one or more first identifiers can be defined in one first code file. One first identifier can be defined in one first code file or repeatedly defined in multiple first code files. If one first identifier is repeatedly defined in multiple first code files, redundant code will exist in the generated first single file. In order to improve the conversion efficiency, the redundant code in the first single file can be repaired before the conversion processing.
[0145] Step S203, the first single file is converted to obtain a second single file.
[0146] Exemplarily, the first single file can be converted by a code conversion tool to obtain the second single file. The second single file is a code file written in a second language, and includes second identifiers converted from the first identifiers in the first single file. Exemplarily, the code conversion tool for converting a C language project to a Rust language project can be a C2Rust tool.
[0147] Exemplarily, in the conversion, the first single file determined in step S202 can be directly input into the code conversion tool for conversion to obtain the second single file. Alternatively, the first single file can be preprocessed first, and then the preprocessed first single file is input into the code conversion tool for conversion to obtain the second single file. The preprocessing operation can be an operation that can improve the conversion success rate, or other operations that can improve the code quality, which are not limited in the embodiments of the present application.
[0148] In the embodiments of the present application, by processing the code project to be converted into a single file and then converting, the problems of repeated definition of data structure, missing dependencies and variable naming conflicts in the conversion from the first language to the second language can be solved, which helps to improve the code conversion pass rate and conversion efficiency.
[0149] Step S204, at least one second code file is generated according to the second identifiers of the second single file and the mapping relationship table.
[0150] Step S205, a target project is determined.
[0151] The mapping relationship table records the file path defined by the first identifiers in the first single file in the code project to be converted. According to the corresponding relationship between the first identifiers in the mapping relationship table and the second identifiers in the second single file, the second single file is reconstructed to generate at least one second code file corresponding to the at least one first code file, and the second code files constitute the target project. The 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 in the embodiments of the present application first acquires a code project to be converted written in a first language; then generates a first single file and a mapping relationship table according to at least one code file included in the code project to be converted, the mapping relationship table is used to store the correspondence between a first identifier in the first single file and the path of a first code file defining the first identifier, and each first identifier in the first single file corresponds to one first code file in the code project to be converted; then performs conversion processing on the first single file 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 generates at least one second code file according to the second identifier of the second single file and the mapping relationship table, and determines a target project based on the 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 when the first language is translated into the second language can be solved, which helps to improve the code conversion pass rate and efficiency, 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 quickly and accurately converting code.
[0153] In some embodiments, the step S202 "generating a first single file and a mapping relationship table according to the code project to be converted" in the above embodiments can be implemented by the steps S2021 to S2024 as shown in the following table: Figure 3
[0154] The step S2021 integrates at least one first code file included in the code project to be converted to obtain a first single file.
[0155] For example, the CMake parameter can be configured by using the Goblint tool to build the project to obtain the merged first single file to be converted, and the removal of useless code can be completed at the same time. The Goblint is a powerful static analysis tool specially designed for C language, which can detect potential problems in the code before compilation, such as data race, null pointer dereference, etc., to help developers improve the safety and reliability of the code.
[0156] In some embodiments, the code conversion device can obtain the first single file by the following steps:
[0157] The step S20211 merges each first code file included in the code project to be converted to obtain an initial single file.
[0158] The code conversion apparatus first traverses the files under the directory in the code item to be converted to determine the first code files included in the code item to be converted. Generally, the number of the first code files included in the code item to be converted is multiple. Taking a 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 item to be converted, the code in each first code file is merged into one file to obtain an initial single file. Specifically, when the code item to be converted includes one first code file, the one first code file is determined as the initial single file; when the code item to be converted includes multiple first code files, the Goblint tool can be used to configure the CMake parameter to build the project to obtain the initial single file to be converted after merging.
[0160] In step S20212, the macro definition in the initial single file is expanded to obtain an intermediate single file.
[0161] After obtaining the initial single file, the macro definition in the initial single file is expanded, i.e., the macro definition in the initial single file is replaced by a specific value or code block to obtain an intermediate single file. The macro definition is a preprocessor directive in C and C++, which allows programmers to define an identifier in the source code to replace a specific value or code block with the identifier in subsequent code.
[0162] In some embodiments, when the code conversion apparatus expands the macro definition in the initial single file, the constant macro related to the C language standard library in the Rust code can also be expanded. For example, the constant macro DBL_MAX in the C code can be expanded to: (double) 1.79769313486231570814527423731704357e+308L.
[0163] In step S20213, the intermediate single file is cleaned to obtain a first single file.
[0164] Due to programming redundancy or code blocks of annotations, etc., there can be useless code in the intermediate single file. The useless code increases the amount of converted code, reduces the conversion efficiency, and also affects the readability of the code, so the useless code in the intermediate single file can be deleted before conversion to obtain a first single file.
[0165] For example, the useless code can include code and data. There are two conditions for determining useless code: 1) not executable, i.e., the control flow of the program cannot reach the code, or the data flow cannot reach the data; 2) not traceable, i.e., from the code or data, any system or software requirement cannot be traced.
[0166] In the embodiment of the application, by performing merging processing on all first code files included in the to-be-converted code project, expanding the macro definition in the merged initial single file, and cleaning the useless code therein, the first single file is obtained, which helps to improve the success conversion pass rate and conversion efficiency, and saves manpower.
[0167] In step S2022, the first identifier set corresponding to the first single file is determined according to the first single file.
[0168] The first identifiers in the first single file are included in the first identifier set. The code conversion device can perform line-by-line traversal on the code of the first single file to determine all the first identifiers included therein. For example, in C language, identifiers can include keywords, predefined identifiers, and user-defined identifiers.
[0169] In step S2023, the first code file defining each first identifier is determined in the at least one first code file.
[0170] Since there is no concept of header file in Rust language, the C2Rust translation process does not consider the dependency relationship between C project files, and the merged first single file loses the dependency relationship. In the embodiment of the application, when merging, the mapping relationship table can be recorded by recording the origin of the first identifier, so as to achieve the purpose of preserving the dependency relationship between multiple first code files.
[0171] The code conversion device determines the first code file defining each first identifier in each first code file included in the to-be-converted code project, that is, determines which code file in the to-be-converted code project defines each first identifier.
[0172] In step S2024, the mapping relationship table is generated 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. For example, the first identifier can be represented as name, and the path can be represented as path. The correspondence 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 the first identifiers in the first single file and the corresponding paths is determined, and recorded in the mapping relationship table to obtain the mapping relationship table.
[0174] In the embodiment of the application, when merging, the mapping relationship table can be recorded by recording the correspondence between each first identifier and its origin, so as to achieve the purpose of preserving the dependency relationship between multiple first code files and provide data support for subsequent reconstruction.
[0175] To improve the success conversion rate, the code in the first single file that may cause conversion failure can be repaired before conversion. Based on this, before step S203 in the above embodiment, steps S21 and S22 can be performed Figure 4 Steps S21 and S22 are shown as follows:
[0176] Step S21, determine the to-be-repaired identifier in the first single file that meets the first repair condition.
[0177] The first repair condition is a condition set according to the code conversion tool that does not support translation. The to-be-repaired identifier 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, determine the to-be-repaired identifier in the first single file that needs to be repaired.
[0178] Step S22, repair the to-be-repaired identifier in the first single file.
[0179] According to the first language and the second language, determine the repair method of the to-be-repaired identifier, and repair each to-be-repaired identifier respectively to obtain the repaired first single file.
[0180] For example, the following takes the C language to support the definition of enumeration in the structure as an example, but the Rust language does not support the definition of enumeration in the structure. The specific steps are as follows:
[0181] Step S221, determine the abstract syntax tree corresponding to the first single file.
[0182] For example, the C language code in the first single file obtained after merging and useless code cleaning can be analyzed using the pycparser tool to obtain the abstract syntax tree AST corresponding to the first single file. The pycparser is a C language parser that is used to parse C language code and convert 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 pointers / positions for positioning and operating specific nodes.
[0183] Step S222, determine the enumeration definition located in the structure according to the abstract syntax tree corresponding to the first single file.
[0184] Exemplarily, it can be determined according to struct identifier (such as struct, { and}) and enum identifier (such as enum, { and}) whether the enum definition is located in the struct. When it is determined that there is an enum definition located in the struct in the first single file, step S213 is entered. When it is determined that there is no enum definition located in the struct in the first single file, the repair of other to-be-repaired identifiers is continued. After the repair of all to-be-repaired identifiers satisfying the first repair condition is completed, the repaired first single file is obtained, and step S203 is entered.
[0185] In step S223, the enum definition located in the struct is moved out of the struct.
[0186] Exemplarily, the enum definition existing in the struct can be moved to the front of the struct according to the line number of the struct.
[0187] For example, the enum definition located in the struct in the first single file determined according to the abstract syntax tree is as follows:
[0188]
[0189] All enum definitions located in the struct in the first single file are moved out of the struct to obtain the repaired first single file. The repaired first single file is converted by using a code conversion tool to obtain a second single file.
[0190] In the embodiment, the to-be-repaired identifiers that can cause unsuccessful translation in the first single file are repaired before conversion processing, so that the success rate of translation is improved.
[0191] In order to improve the success rate of compilation, the code that can cause compilation failure in the second single file can be repaired before compilation. Based on this, after step S203 in the above embodiment, steps S23 and S24 shown in the following table can be executed. Figure 4
[0192] In step S23, to-be-repaired code satisfying a second repair condition in the second single file is determined.
[0193] The second repair condition is a condition set according to a second language compilation rule, and the to-be-repaired code satisfying the second repair condition is code that can cause compilation failure. According to the preset second repair condition, to-be-repaired code that needs to be repaired in the second single file is determined.
[0194] In step S24, the to-be-repaired code in the second single file is repaired.
[0195] According to the second language, the repair manner of the to-be-repaired identifier is determined, and each to-be-repaired identifier is repaired to obtain the repaired first single file.
[0196] For example, the code to be repaired can be a constant macro in Rust code, and the repair method can be to use a regular expression to expand the constant macro related to float.h in the C language standard library in Rust code. For example, the constant macro DBL_MAX in C code can be expanded to: (double) 1.79769313486231570814527423731704357e+308L.
[0197] For example, when repairing, the syn toolkit can be used to parse the Rust code to obtain an abstract syntax tree, and the abstract syntax tree can be traversed to match and repair other error codes that can cause the translated target project to fail to compile.
[0198] The following is an example of repairing a naked pointer in a second single file. The specific steps are as follows:
[0199] Step S241, the naked pointer in the second single file is eliminated to obtain a preliminary processed single file.
[0200] For example, the Crown tool can be used to eliminate unnecessary naked pointers in the Rust language code obtained by C2Rust preliminary conversion.
[0201] The core of the Crown tool is the static ownership analysis of variables. In Crown, a model for analyzing and reasoning about the ownership of naked pointers is designed, which can automatically convert naked pointers to the corresponding safety features of the Rust language. Specifically, Crown applies several static analyses to the Medium Internal Representation (MIR) of Rust to infer the properties of pointers, which mainly include three analyses: 1) Ownership analysis: calculate and infer the ownership information of pointers at a specific location in the code; 2) Variability 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), which can be a 3-SAT problem. The corresponding 3-SAT problem can be solved using a solver (such as the z3 SMT solver). 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 be used not only for C2Rust translation code analysis and optimization, but also for static analysis and improvement of ordinary Rust projects); if there are multiple solutions, a greedy strategy is used to select the first solution to generate ownership information. Finally, the ownership is generated according to the solution, and the naked pointer is rewritten. The final preliminary processed single file is obtained.
[0202] Before the compilation passability verification is performed, the number of naked pointers in the preliminary processing single file is reduced by eliminating the naked pointers, which helps to improve the success rate of the second single file compilation.
[0203] In step S242, the preliminary processing single file is subjected to compilation checking, and a compilation checking result is obtained.
[0204] The compilation passability of the preliminary processing single file is verified. If the preliminary processing single file can pass the compilation, the compilation checking result is determined as that the preliminary processing single file can pass the compilation; if the preliminary processing single file cannot pass the compilation, the compilation checking result is determined as that the preliminary processing single file cannot pass the compilation. At this time, the code that cannot pass the compilation can be repaired according to the compiler suggestion or a large language model, until it can pass the compilation.
[0205] In step S243, in the case that the compilation checking result represents that the preliminary processing single file can pass the compilation, the naked pointers in the preliminary processing single file are subjected to repair processing.
[0206] For example, the Laertes tool can be used to further eliminate unnecessary naked pointers in the Rust language code that can pass the compilation.
[0207] Laertes is a C2Rust translation post-unsafe repair scheme, which mainly consists of two core components: 1) resolve-import for solving the repeated import of header file expansion code; and 2) resolve-lifetime for rewriting naked pointers to safe Rust references and repairing the generated variable lifetime.
[0208] resolve-lifetime in Laertes scheme, which rewrites naked pointers to safe Rust applications, analyzes and infers their ownership information, and generates lifetime labels for them. resolve-lifetime adopts an optimistic rewriting mechanism to rewrite naked pointers (such as structure fields and function parameters) to Rust reference declarations. In the rewriting process, resolve-lifetime makes the following assumptions: 1) all pointers can be converted to safe references (which may cause a compilation error); 2) all references are borrowed (in fact, some references are not borrowable, and may need to be modified back); 3) each lifetime in the function and type signature is independent (has a different type variable). resolve-lifetime rewrites the pointers to references under these assumptions, and then calls the Rust compiler as a discriminator to eliminate the compilation errors that may be caused by the assumptions in an iterative loop. It is noted that, in order to deal with the naked pointer case in the rewriting process, resolve-lifetime converts the naked pointer to an option reference, and then repeatedly calls the Rust compiler to obtain error information and selectively modify back the rewritten content.
[0209] After the compilation pass verification, the number of naked pointers can be further reduced by eliminating the naked pointers in the preliminary processing single file, and the success rate of successful compilation of the target project can be improved.
[0210] In the embodiments of the present application, after the second single file is obtained by translation, the success rate of successful compilation of the second single file can be improved by repairing the code in the second single file that may cause unsuccessful compilation.
[0211] In actual application, the first single file can be directly converted for processing, or the first single file can be repaired and then converted for processing according to actual conditions. When compiling, the second single file can be directly compiled for processing, or the second single file can be repaired and then compiled for processing. The embodiments of the present application are not limited.
[0212] In some embodiments, the step S204 "generating at least one second code file according to the second identifier of the second single file and the mapping relationship table" in the above embodiments can be implemented by steps S2041 to S2044 as shown in the figure: Figure 5
[0213] In step S2041, at least one same-named file based on a second language is created according to the path of the first code file included in the mapping relationship table.
[0214] Exemplarily, the file paths path included in the mapping relationship table MAP<name, path> can be deduplicated, and corresponding same-name files are created according to the remaining file paths. For example, after path deduplication, the file paths corresponding to the C language project include two files of path 1.c and path 2.h, and the created same-name files corresponding to the Rust language can be path 1.rs and path 2.rs. Wherein,.rs is the suffix name of the Rust language file. The number of the created same-name files is equal to the number of the file paths stored in the mapping relationship table. If the C language project does not include redundant first code files, the number of the same-name files is equal to the number of the first code files.
[0215] In some embodiments, after creating the same-name files, a lib.rs file can be created in the root directory of the Rust language project, and the created path 1.rs and path 2.rs files are added to the lib.rs file as mod declarations.
[0216] Step S2042, determining a second identifier set corresponding to the second single file according to the second single file.
[0217] Exemplarily, the sys tool can be used to analyze and process the second single file to determine the abstract syntax tree corresponding to the second single file. The abstract syntax tree corresponding to the second single file is traversed to determine the identifiers in the second single file, and the second identifier set corresponding to the second single file is obtained.
[0218] Step S2043, matching each identifier in the second identifier set with the first identifier included in the mapping relationship table to obtain a matching result.
[0219] Each identifier in the second identifier set is matched with the name set in MAP<name, path> to obtain a matching result.
[0220] Step S2044, reconstructing the second single file based on the matching result and the at least one same-name file to obtain at least one second code file.
[0221] It is determined whether the matching result of each identifier in the second identifier set represents that the second identifier matches the target identifier included in the mapping relationship table. If the current identifier does not match, the judgment process of the next identifier is continued. If it matches, the current identifier is marked as a second identifier, and the judgment process of the next identifier is continued. Until all identifiers in the second identifier set are judged, all matched second identifiers are obtained. Then, the second single file is reconstructed based on all matched second identifiers and the created same-name files to obtain at least one second code file. Wherein, the number of the second code files is equal to the number of the same-name files.
[0222] In some embodiments, each second code file can be determined according to the following steps:
[0223] Step S20441, in the case where the matching result represents that the second identifier in the second identifier set matches the target identifier included in the mapping relationship table, determining the definition of the second identifier in the second single file, and determining a target file which is the same name as the path of the first code file defining the target identifier in the at least one same name file.
[0224] Step S20442, moving the definition of the second identifier in the second single file to the target file.
[0225] Step S20443, adding a reference of the second identifier to the position where the second identifier is used in the at least one same name file, to obtain at least one second code file.
[0226] The definition of the successfully matched second identifier in the second single file is migrated to the same name file of the file path corresponding to the first identifier matched with the second identifier, for example, the definition of the identifier 1 successfully matched in the Rust single file is migrated to the same name rs file of the path corresponding to the name of the identifier 1 matched. At the same time, a use reference is added to the place where the identifier 1 is used in the Rust single file, to repair the dependency and reconstruct the original project file structure. After the processing is completed, all second code files included in the target project are obtained.
[0227] In the embodiments of the present application, 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, etc., which can save manpower, material resources and financial resources, and achieve the purpose of quickly and accurately translating the code.
[0228] Based on the embodiments of the foregoing code conversion method, the embodiments of the present application also provide a code conversion device. Each module included in the device and each unit included in the modules can be implemented by a processor in a computing device. Of course, it can also be implemented by a specific logic circuit. In the implementation process, the processor can be a central processing unit (CPU), a microprocessor unit (MPU), a digital signal processor (DSP), or a field programmable gate array (FPGA), etc.
[0229] Figure 6 A schematic diagram of a component structure of the code conversion device provided in the embodiments of the present application is shown in FIG. 6, which can include: Figure 6
[0230] The obtaining module 601 is configured to obtain a code project to be converted, the code project to be converted comprising at least one first code file written in a first language;
[0231] The first generating module 602 is configured to generate a first single file and a mapping relationship table according to the code project to be converted; the mapping relationship table is configured to store a correspondence between a first identifier in the first single file and a path of a first code file defining the first identifier, each first identifier in the first single file corresponding to one first code file in the code project to be converted;
[0232] The converting module 603 is configured to perform conversion processing on 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 comprises a second identifier converted from the first identifier in the first single file;
[0233] The second generating module 604 is 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 determining module 605 is configured to determine a target project, the target project comprising at least the at least one second code file.
[0235] In a possible implementation, the obtaining module 601 is further configured to:
[0236] obtain at least one initial code file written in the first language;
[0237] perform compilation checking processing on 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] perform integration processing on the at least one first code file included in the code project to be converted to obtain the first single file;
[0240] determine a first identifier set corresponding to the first single file according to the first single file, the first identifier set comprising a plurality of first identifiers;
[0241] determine, in the at least one first code file, a first code file defining each first identifier;
[0242] generate the mapping relationship table according to each first identifier and a path of the first code file defining each first identifier.
[0243] In a possible implementation, the first generating module 602 is further configured to:
[0244] perform merging processing on each first code file included in the code item to be converted, to obtain an initial single file;
[0245] perform expansion processing on macro definitions in the initial single file, to obtain an intermediate single file;
[0246] perform cleaning processing on the intermediate single file, to obtain the first single file.
[0247] In a possible implementation, the code conversion apparatus 600 can further include a second determining module and a first repair processing module.
[0248] The second determining module is configured to determine a to-be-repaired identifier in the first single file that meets a first repair condition.
[0249] The first repair processing module is configured to perform repair processing on the to-be-repaired identifier in the first single file.
[0250] In a possible implementation, the code conversion apparatus 600 can further include a third determining module and a second repair processing module.
[0251] The third determining module is configured to determine to-be-repaired code in the second single file that meets a second repair condition.
[0252] The second repair processing module is configured to perform repair processing on the to-be-repaired code in the second single file.
[0253] In a possible implementation, the second generating module 604 is further configured to:
[0254] create at least one same-name file based on a second language according to a path of a first code file included in the mapping relationship table;
[0255] determine a second identifier set corresponding to the second single file according to the second single file;
[0256] perform matching on each identifier in the second identifier set and a first identifier included in the mapping relationship table, to obtain a matching result;
[0257] perform reconstruction processing on the second single file based on the matching result and the at least one same-name file, 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 that the matching result represents that a second identifier in the second identifier set matches a 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 which is same as a path of a first code file defining the target identifier in the at least one same-name file;
[0260] moving the definition of the second identifier in the second single file to the target file;
[0261] adding a reference of the second identifier to a position of the second identifier in the at least one same-name file, 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 description of the above code conversion method, 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 can refer to the description of the method embodiment of the present application for understanding.
[0263] It should be noted that in the embodiments 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 this understanding, the technical solutions of the embodiments of the present application can be embodied in the form of a software product in essence or the part that contributes to the prior art. The computer software product is stored in a storage medium and includes a plurality of instructions for causing 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 the embodiments of the present application. The aforementioned storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM), a magnetic disk or an optical disk, and various media that can store program codes. Thus, the embodiments of the present application are not limited to any specific combination of hardware and software.
[0264] Correspondingly, the embodiments of the present application provide a computer readable storage medium, which stores a computer program. The computer program is executed by a processor to implement the steps of the code conversion method provided in the above embodiments.
[0265] The embodiments of the present application provide a computing device, Figure 7 A schematic diagram of a component structure of the computing device provided in the embodiments of the present application is shown in the above Figure 1 The computing device can be implemented as the second device in the above Figure 7The exemplary structure of the computing device 700 is shown, and other exemplary structures of the computing device 700 are contemplated, and thus the structure described herein should not be considered limiting. For example, some components described below can be omitted in some embodiments, or additional components not recited below can be included in some embodiments to suit particular needs of some embodiments.
[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 enable communication between the components. The user interface 703 can include a display screen, and the external communication interface 704 can include standard wired and wireless interfaces. The processor 701 is configured to execute a program stored in the memory to implement the steps of the transcoding method provided in the above embodiments.
[0267] The descriptions of the computing device and the storage medium embodiments above are similar to the descriptions of the method embodiments above, and have similar beneficial effects as the method embodiments. For technical details not disclosed in the computing device and the storage medium embodiments of the present application, please refer to the descriptions of the method embodiments of the present application.
[0268] It should be understood that the reference to “one embodiment” or “an embodiment” throughout the specification means that a particular feature, structure, or characteristic described in connection with the embodiment is included in at least one embodiment of the application. Therefore, appearances of the phrase “in one embodiment” or “in an embodiment” at various places throughout the specification are not necessarily referring to the same embodiment. Furthermore, the particular features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. It should be understood that the sequence of the processes described above does not mean the execution order, and the execution order of the processes should be determined according to their functions and inherent logic, and should not constitute any limitation on the implementation process of the embodiments of the present application. The sequence of the embodiments of the present application described above is only for description, and does not represent the advantages or disadvantages of the embodiments.
[0269] It should be noted that, in this document, the terms “comprises”, “comprising”, or any other variation thereof are intended to cover a non-exclusive inclusion, so that a process, method, article, or apparatus that comprises a list of elements does not only include those elements, but also includes other elements not expressly listed or inherent to such process, method, article, or apparatus. Without more limitations, an element defined by the phrase “comprises a” does not exclude the presence of additional identical elements in the process, method, article, or apparatus that includes the element.
[0270] In several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other manners. The described device embodiments are merely schematic. For example, the division of the units is only a logical function division. There can be another division manner for the actual implementation, for example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the displayed or discussed coupling, or direct coupling or communication connection between the components can be indirect coupling or communication connection through some interfaces, devices, or units, and can be electrical, mechanical, or in other forms.
[0271] The units described as separate components can or can not be physically separate, and the components shown as units can or can not be physical units; they can be located in one place, or distributed on multiple network units; and some or all of the units can be selected according to actual needs to achieve the purpose of the embodiment.
[0272] In addition, each functional unit in each embodiment of the present application can be integrated into one processing unit, or each unit can be a separate unit, or two or more units can be integrated into one unit; the integrated unit can be realized in the form of hardware or in the form of hardware plus software functional units.
[0273] Those of ordinary skill in the art can understand that all or part of the steps of the above-mentioned method embodiments can be completed by a program instruction relevant hardware, and the aforementioned program can be stored in a computer readable storage medium, and the program executes the steps including the above-mentioned method embodiments when executed; and the aforementioned storage medium includes mobile storage devices, ROM, magnetic discs or optical discs, and various storage medium that can store program codes.
[0274] Alternatively, the integrated units of the present application, if implemented in the form of software functional modules and sold or used as independent products, can also be stored in a computer readable storage medium. Based on this understanding, the technical solutions of the embodiments of the present application can be embodied in the form of a software product, and the computer software product is stored in a storage medium, and includes several instructions for causing a device to execute all or part of the methods described in the embodiments of the present application. The aforementioned storage medium includes mobile storage devices, ROM, magnetic discs or optical discs, and various storage medium that can store program codes.
[0275] The above embodiments are merely specific embodiments of the present application and are not intended to limit the protection scope of the present application, and any modification, equivalent replacement, improvement, etc. made on the basis of the technical solutions of the present application shall be included in the protection scope of the present application.
Claims
1. A transcoding method, characterized by, The method comprises: acquiring a code project to be converted, the code project to be converted comprising at least one first code file written in a first language; performing merging processing on each first code file included in the code project to be converted to obtain an initial single file; performing expansion processing on macro definitions in the initial single file to obtain an intermediate single file; performing cleaning processing on the intermediate single file to obtain a first single file; determining a first identifier set corresponding to the first single file according to the first single file, the first identifier set comprising a plurality of first identifiers; determining, in the at least one first code file, a first code file defining each first identifier; generating a mapping relationship table according to each first identifier and a path of the first code file defining each first identifier; wherein the mapping relationship table is used to store a corresponding relationship between a first identifier in the first single file and a path of the first code file defining the first identifier, and each first identifier in the first single file corresponds to one first code file in the code project to be converted; performing conversion processing on 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 comprises second identifiers converted from the first identifiers in the first single file; generating at least one second code file according to second identifiers of the second single file and the mapping relationship table; determining a target project, the target project comprising at least the at least one second code file.
2. The method of claim 1, wherein, The acquiring a code project to be converted comprises: acquiring at least one initial code file written in a first language; performing compilation checking processing on the at least one initial code file to obtain the code project to be converted that can pass the compilation.
3. The method of claim 1, wherein, Before performing conversion processing on the first single file to obtain a second single file, the method further comprises: determining a to-be-repaired identifier in the first single file that satisfies a first repair condition; performing repair processing on the to-be-repaired identifier in the first single file.
4. The method of claim 3, wherein, After performing conversion processing on the first single file to obtain a second single file, the method further comprises: determining to-be-repaired code in the second single file that satisfies a second repair condition; performing repair processing on the to-be-repaired code in the second single file.
5. The method according to any one of claims 1-4, characterized in that, The generating at least one second code file according to second identifiers of the second single file and the mapping relationship table comprises: creating at least one same-name file based on a second language according to a path of a first code file included in the mapping relationship table; determining a second identifier set corresponding to the second single file according to the second single file; performing matching on each identifier in the second identifier set and a first identifier included in the mapping relationship table to obtain a matching result; performing reconstruction processing on the second single file based on the matching result and the at least one same-name file to obtain the at least one second code file.
6. The method of claim 5, wherein, The performing reconstruction processing on the second single file based on the matching result and the at least one same-name file to obtain the at least one second code file comprises: In a case where the matching result represents that a second identifier in the second identifier set matches a 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 that is same-named with a path of a first code file defining the target identifier in the at least one same-named file; moving the definition of the second identifier in the second single file to the target file; adding a reference of the second identifier using a position to the second identifier in the at least one same-named file to obtain the at least one second code file. 7.A computer readable storage medium, the storage medium storing a computer program, the computer program being executed by a processor to implement the code conversion method in any one of claims 1-6.
8. A computing device comprising: a processor and a memory; the processor and the memory are coupled; the memory is configured to store executable instructions; the processor is configured to read the instructions from the memory and execute the instructions to perform the code conversion method in any one of claims 1-6.
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