Code language conversion verification method and device, electronic equipment, storage medium and computer program product
By employing a global perspective verification method for C-to-Rust code conversion, this approach addresses the lack of a global perspective in existing technologies, achieving a higher quality and more efficient code conversion process and ensuring the correctness and security of Rust projects.
Patent Information
- Application Number
- CN202511299722.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-11
- Publication Date
- 2025-11-21
- Estimated Expiration
- 2045-09-11
AI Technical Summary
现有技术在C语言到Rust语言代码转换中缺乏全局视角的验证,导致调试和问题定位难度增加,降低了项目转换质量。
By extracting basic transformation units, including data structures and functions, from C language source code, converting them into commented Rust language files, and performing layer-by-layer compilation and de-commenting, combined with abstract syntax tree analysis and large language models for automatic or manual correction, the correctness of overall call relationships and global context is ensured.
增强了对函数调用关系和传参类型的宏观语义影响的捕捉能力,减少了集成风险和意外行为,提高了代码转换后的质量和效率。
Smart Images

Figure CN120803464B_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of code-language conversion technology, and more specifically, to verification methods, apparatus, electronic devices, storage media, and computer program products for code-language conversion. Background Technology
[0002] The conversion from C to Rust code has become a hot research topic. The primary technology used is C2Rust. However, while C2Rust ensures functional equivalence before and after conversion, it often generates a large amount of unsafe code, posing a threat to software security. Therefore, verifying the conversion results is essential.
[0003] In related technologies, potential syntax and type errors in the conversion results are typically corrected through repeated compilation; alternatively, existing test cases from the C project can be used to verify from the bottom-level functions upwards. However, this function-based bottom-up verification approach neglects the overall call relationships and global context of the C project. This means it can only ensure compilation and local testing pass at the individual function level, while its ability to capture macro-level semantic effects such as function call relationships and parameter types is poor. This limitation can easily lead to integration risks and unexpected behavior. For example, some independent functions may pass verification in local tests, but they may still have problems such as incorrect parameter definitions and memory interaction errors in complex environments. This is because the parameter definitions of the lower-level functions may not be suitable for the calling methods of the upper levels, leading to logical and syntactic chaos in the overall code. Once problems occur in the entire project, the lack of verification from a global perspective increases the difficulty of debugging and problem localization, thereby reducing the overall conversion quality of the project. Summary of the Invention
[0004] This disclosure provides verification methods, apparatus, electronic devices, storage media, and computer program products for code language conversion, to at least address the problem in the aforementioned related technologies that the lack of a global perspective in verification increases the difficulty of debugging and problem localization, thereby reducing the overall conversion quality of the project.
[0005] According to a first aspect of the present disclosure, a method for verifying code language conversion is provided, comprising: extracting basic conversion units from a C language source code file, wherein the basic conversion unit includes at least a data structure and a function; converting the data structure into a first Rust language file and converting the function into a second Rust language file, wherein the function bodies contained in the second Rust language file are commented out; combining the first Rust language file and the second Rust language file to obtain a Rust project structure file, and performing a first compilation on the Rust project structure file; if the first compilation passes, removing the comments from the commented function bodies contained in the Rust project structure file, and performing a second compilation on the Rust project structure file obtained after the removal of comments for verification.
[0006] Optionally, the function includes multiple functions, and the verification method further includes: extracting the call relationship between the multiple functions from the C language source code file; the step of converting the function into a second Rust language file includes: based on the call relationship, converting the functions at each level into corresponding Rust language files layer by layer, starting from the leaf functions among the multiple functions, wherein the leaf functions are functions among the multiple functions that do not call other functions or only call functions that have already been converted into Rust language files.
[0007] Optionally, the function includes a main function and sub-functions called by the main function; the step of de-commenting the commented function bodies contained in the Rust project structure file and performing a second compilation on the Rust project structure file obtained after de-commenting for verification includes: performing a first de-commenting process on the function bodies of the commented main function contained in the Rust project structure file, and compiling the main function on the Rust project structure file obtained after the first de-commenting; if the main function compiles successfully, performing a second de-commenting process on the function bodies of the commented sub-functions contained in the Rust project structure file obtained after the first de-commenting, and compiling the sub-functions on the Rust project structure file obtained after the second de-commenting for verification.
[0008] Optionally, the verification method further includes: if the first compilation fails, correcting the first Rust language file and / or the second Rust language file contained in the Rust project structure file and recompiling the corrected Rust project structure file until the first compilation succeeds.
[0009] Optionally, the verification method further includes: if the second compilation fails, correcting the function bodies in the Rust project structure file after the comment-removal process and recompiling the corrected Rust project structure file until the second compilation succeeds.
[0010] Optionally, the verification method further includes: synchronously updating the call point of the parent function that calls the function body based on the correction result obtained by correcting the function body after the comment-removal process.
[0011] Optionally, before combining the first Rust language file and the second Rust language file to obtain the Rust project structure file, the verification method further includes: performing a third compilation on the first Rust language file; if the third compilation fails, correcting the first Rust language file and recompiling the corrected first Rust language file until the third compilation succeeds; the step of combining the first Rust language file and the second Rust language file to obtain the Rust project structure file includes: combining the first Rust language file and the second Rust language file that have passed the third compilation to obtain the Rust project structure file.
[0012] Optionally, the step of extracting basic transformation units from the C language source code file includes: performing Abstract Syntax Tree (AST) analysis on the C language source code file to extract the basic transformation units.
[0013] According to a second aspect of the present disclosure, a verification apparatus for code language conversion is provided, comprising: a conversion unit extraction module configured to extract basic conversion units from a C language source code file, wherein the basic conversion unit includes at least a data structure and a function; a language conversion module configured to convert the data structure into a first Rust language file and the function into a second Rust language file, wherein the function bodies contained in the second Rust language file are commented; a first compilation module configured to combine the first Rust language file and the second Rust language file to obtain a Rust project structure file, and to perform a first compilation on the Rust project structure file; and a second compilation module configured, if the first compilation passes, to perform de-commenting processing on the commented function bodies contained in the Rust project structure file, and to perform a second compilation on the Rust project structure file obtained after de-commenting processing for verification.
[0014] Optionally, the function includes multiple functions, and the verification device further includes: a call relationship extraction module configured to extract the call relationship between the multiple functions from the C language source code file; the language conversion module is configured to: based on the call relationship, convert the functions at each level into corresponding Rust language files layer by layer, starting from the leaf functions among the multiple functions, wherein the leaf functions are functions among the multiple functions that do not call other functions or only call functions that have already been converted into Rust language files.
[0015] Optionally, the function includes a main function and sub-functions called by the main function; the second compilation module is configured to: perform a first de-commenting process on the function body of the commented main function included in the Rust project structure file, and compile the main function in the Rust project structure file obtained after the first de-commenting process; if the main function compiles successfully, perform a second de-commenting process on the function body of the commented sub-functions included in the Rust project structure file obtained after the first de-commenting process, and compile the sub-functions in the Rust project structure file obtained after the second de-commenting process for verification.
[0016] Optionally, the verification device further includes: a first recompilation module, configured to, if the first compilation fails, correct the first Rust language file and / or the second Rust language file contained in the Rust project structure file and recompile the corrected Rust project structure file until the first compilation succeeds.
[0017] Optionally, the verification device further includes a second recompilation module, configured to, if the second compilation fails, correct the function bodies in the Rust project structure file after the comment-removal process and recompile the corrected Rust project structure file until the second compilation succeeds.
[0018] Optionally, the verification device further includes a synchronization update module, configured to synchronize the call point of the parent function that calls the function body based on the correction result obtained by correcting the function body after the comment-removal process.
[0019] Optionally, the verification device further includes: a third compilation module configured to perform a third compilation on the first Rust language file; a third recompilation module configured to, if the third compilation fails, correct the first Rust language file and recompile the corrected first Rust language file until the third compilation succeeds; the first compilation module is configured to combine the first Rust language file that has passed the third compilation and the second Rust language file to obtain the Rust project structure file.
[0020] Optionally, the conversion unit extraction module is configured to perform abstract syntax tree (AST) analysis on the C language source code file to extract the basic conversion units.
[0021] According to a third aspect of the present disclosure, an electronic device is provided, comprising: a processor; and a memory for storing processor-executable instructions; wherein the processor is configured to execute the instructions to implement a verification method for code language conversion according to the present disclosure.
[0022] According to a fourth aspect of the present disclosure, a computer-readable storage medium is provided that, when instructions in the computer-readable storage medium are executed by a processor of an electronic device, enables the electronic device to perform a verification method based on code language conversion according to the present disclosure.
[0023] According to a fifth aspect of the present disclosure, a computer program product is provided, including a computer program that, when executed by a processor, implements a verification method for code language conversion according to the present disclosure.
[0024] The technical solutions provided by the embodiments of this disclosure have at least the following beneficial effects:
[0025] In this disclosure, when verifying Rust code converted from C code, the overall call relationships and global context of the C project are fully considered. This enhances the ability to capture macro-level semantic effects such as function call relationships and parameter types, thereby reducing integration risks and unexpected behaviors. In other words, this global perspective verification reduces the difficulty of debugging and problem localization, thus improving the quality of the converted code.
[0026] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this disclosure. Attached Figure Description
[0027] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure, and are not intended to unduly limit this disclosure.
[0028] Figure 1 This is a flowchart illustrating a verification method for code language conversion according to exemplary embodiments of the present disclosure;
[0029] Figure 2 This is a schematic diagram illustrating the process of converting a data structure into a Rust language file according to an exemplary embodiment of the present disclosure;
[0030] Figure 3 This is a schematic diagram illustrating the process of converting a function into a Rust language file according to an exemplary embodiment of the present disclosure;
[0031] Figure 4 This is a schematic diagram illustrating the process of constructing and initially compiling Rust project structure files according to exemplary embodiments of the present disclosure;
[0032] Figure 5 This is a schematic diagram illustrating a top-down process for activating and compiling Rust project structure files according to exemplary embodiments of the present disclosure;
[0033] Figure 6 This is a block diagram illustrating a verification apparatus for code language conversion according to an exemplary embodiment of the present disclosure;
[0034] Figure 7 This is a block diagram illustrating an electronic device according to exemplary embodiments of the present disclosure. Detailed Implementation
[0035] To enable those skilled in the art to better understand the technical solutions of this disclosure, the technical solutions in the embodiments of this disclosure will be clearly and completely described below with reference to the accompanying drawings.
[0036] It should be noted that the terms "first," "second," etc., used in the specification, claims, and accompanying drawings of this disclosure are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this disclosure described herein can be implemented in orders other than those illustrated or described herein. The embodiments described in the following examples do not represent all embodiments consistent with this disclosure. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this disclosure as detailed in the appended claims.
[0037] It should be noted that the phrase "at least one of several items" in this disclosure refers to three parallel cases: "any one of the several items", "a combination of any number of the several items", and "all of the several items". For example, "including at least one of A and B" includes the following three parallel cases: (1) including A; (2) including B; (3) including A and B. Another example is "performing at least one of step one and step two", which means the following three parallel cases: (1) performing step one; (2) performing step two; (3) performing both step one and step two.
[0038] Figure 1 This is a flowchart illustrating a verification method for code language conversion according to an exemplary embodiment of the present disclosure.
[0039] Reference Figure 1 In step 101, basic conversion units can be extracted from the C language source code file, wherein the basic conversion unit can at least contain data structures and functions.
[0040] According to exemplary embodiments of this disclosure, an Abstract Syntax Tree (AST) analysis can be performed on a C language source code file to extract basic transformation units.
[0041] For example, by performing a comprehensive AST analysis on the entire C language source code, one can extract C language data structure definitions, global variables, macro definitions, function information, and so on. C language data structure definitions may include, but are not limited to: structs, unions, enumerations, and their members and memory layout; C language global variables may include, but are not limited to: types, initial values, and linkage attributes; C language macro definitions may include, but are not limited to: function-like macros that affect type or behavior; C language function information may include, but is not limited to: function signatures (e.g., parameters, return type) and internal call relationships (e.g., which function calls which other functions).
[0042] In step 102, the data structure can be converted into a first Rust language file, and the function can be converted into a second Rust language file, wherein the function body contained in the second Rust language file can be commented.
[0043] Figure 2 This is a schematic diagram illustrating a process for converting a data structure into a Rust language file according to an exemplary embodiment of the present disclosure.
[0044] Reference Figure 2The C language data structure definitions parsed from the AST can be provided as context to the Large Language Model (LLM), which can then convert the C language data structures into equivalent Rust data structures. For example, refer to... Figure 2 You can build a temporary project containing only Rust data structures and attempt to compile it. If compilation fails, you can use artificial intelligence (AI) for automatic correction, i.e., use LLM for automatic correction; or you can manually intervene until the Rust data structures can be successfully compiled.
[0045] According to exemplary embodiments of this disclosure, the above-mentioned function may include multiple functions. As mentioned above, the call relationships between multiple functions can also be extracted from the C language source code file; for example, a call graph between multiple functions can be extracted. Then, based on the call relationships, i.e., the function call graph, the functions at each level can be converted into corresponding Rust language files, starting from the leaf functions among the multiple functions. The leaf functions can be functions among the multiple functions that do not call other functions or functions that only call functions that have already been converted into Rust language files.
[0046] Figure 3 This is a schematic diagram illustrating the process of converting a function into a Rust language file according to an exemplary embodiment of the present disclosure.
[0047] Reference Figure 3 Based on the function call graph, leaf functions in a C project can be identified. Then, starting with the leaf functions, the source code of the C functions can be provided to the LLM for conversion in batches. Simultaneously, the Rust data structure definitions that these functions depend on, and the Rust signatures of their subfunctions (as stubs), can be provided to the LLM as context. Furthermore, the converted Rust function bodies should be fully commented out, and they should return their corresponding Rust type default values or placeholder values. In this way, functions at each level can be processed layer by layer according to the dependency order (e.g., from leaf functions upwards), i.e., functions can be converted in batches according to their dependencies (bottom-up stubification), until all C functions are converted into Rust functions with commented stub bodies.
[0048] In step 103, the first Rust language file and the second Rust language file can be combined to obtain a Rust project structure file, and the Rust project structure file can be compiled for the first time.
[0049] Figure 4 This is a schematic diagram illustrating the process of constructing and initially compiling Rust project structure files according to exemplary embodiments of the present disclosure.
[0050] Reference Figure 4 For example, all the converted Rust data structures and annotated function stubs can be assembled into a complete Rust project structure file, which can then be preliminarily compiled. At this stage, since all function bodies are commented out, this preliminary compilation primarily verifies the correctness of the Rust data structures and the matching of all function signatures. If compilation fails, it can be backtracked to the data structure conversion stage or the function signature conversion stage for correction until the preliminary compilation succeeds.
[0051] According to an exemplary embodiment of this disclosure, the first Rust language file can also be compiled a third time. If the third compilation fails, the first Rust language file can be corrected and the corrected first Rust language file can be recompiled a third time until the third compilation succeeds. Next, the first Rust language file that has passed the third compilation and the second Rust language file can be combined to obtain the Rust project structure file.
[0052] In this way, before assembling the Rust language files converted from C language data structures and C language functions, the Rust language files converted from C language data structures can be compiled first. Furthermore, assembly will only proceed if compilation succeeds, ensuring the correctness and security of the assembled Rust project structure file.
[0053] According to an exemplary embodiment of this disclosure, if the first compilation fails, that is, if the initial compilation of the Rust project structure file obtained by combining the first Rust language file and the second Rust language file fails, the first Rust language file and / or the second Rust language file contained in the assembled Rust project structure file can be corrected and the corrected Rust project structure file can be recompiled until the first compilation succeeds.
[0054] In this way, by repeatedly compiling and correcting the Rust project structure file obtained by combining the first Rust language file and the second Rust language file, the correctness and security of the combined Rust project structure file can be guaranteed.
[0055] In step 104, if the first compilation is successful, the commented-out function bodies contained in the Rust project structure file can be de-commented, and the Rust project structure file obtained after de-commenting can be compiled a second time for verification.
[0056] According to an exemplary embodiment of this disclosure, the above function may include a main function and sub-functions called by the main function.
[0057] First, the function body of the commented-out main function in the Rust project structure file can be de-commented, and the main function can be compiled into the Rust project structure file obtained after the first de-commenting process. Next, if the main function compiles successfully, the function bodies of the commented-out sub-functions in the Rust project structure file obtained after the first de-commenting process can be de-commented a second time, and the sub-functions can be compiled into the Rust project structure file obtained after the second de-commenting process for verification.
[0058] Figure 5 This is a schematic diagram illustrating a top-down process for activating and compiling Rust project structure files according to exemplary embodiments of the present disclosure.
[0059] Reference Figure 5 You can start from the main function (or an equivalent entry point) of the Rust project and first uncomment the commented-out function body of the main function. Then, you can compile the main function from the Rust project structure file obtained after this initial uncommenting process. At this stage, since the sub-functions of the main function are still commented out and return default values, this compilation primarily verifies whether there are any problems with the logic of the main function itself and whether the calls to data structures and sub-function stubs are correct. If the main function compilation fails, you can use AI (e.g., prompting LLM to repair the main function body) or manual intervention to correct the main function body until the main function compiles successfully.
[0060] Next, after the `main` function compiles successfully, the activation and compilation of sub-functions can be iteratively executed. For example, a second decommenting process can be performed on the function bodies of each commented-out sub-function, meaning the function bodies of the sub-functions of the `main` function can be uncommented one by one. Furthermore, after uncommenting the function body of each sub-function, the Rust project structure file obtained after the second decommenting process can be immediately recompiled, effectively recompiling the entire Rust project. If a sub-function compilation fails, the function bodies and call relationships of the currently activated sub-functions can be corrected, and the corrected Rust project structure file can be recompiled until the sub-function compiles successfully.
[0061] This process is repeated, uncommenting the function bodies one by one along the function call chain from top to bottom and compiling each function until all function bodies in the entire Rust project are uncommented and successfully compiled. This ensures that no function is missed and guarantees the correctness and security of the final Rust project.
[0062] According to an exemplary embodiment of this disclosure, if the second compilation fails, the function bodies in the Rust project structure file that have undergone de-commenting can be corrected, and the corrected Rust project structure file can be recompiled until the second compilation succeeds. In this way, by repeatedly compiling and correcting the de-commented function bodies, the correctness and security of the final Rust project structure file can be guaranteed.
[0063] According to exemplary embodiments of this disclosure, the call points of parent functions that call a function can be synchronously updated based on the correction results obtained from modifying the function body after de-commenting. That is, if the parameters of a sub-function need to be adjusted to adapt to Rust language conventions, it is necessary to ensure that the call points of all callers (parent functions) of that sub-function are also synchronously updated. In this way, after modifying the function body of a function, the call points of the parent functions that call that function can also be synchronously updated, ensuring the consistency of behavior among functions, i.e., ensuring accurate calls between functions.
[0064] It should be noted that after the entire Rust project is compiled successfully, subsequent functional debugging and integration testing can be carried out, and it can then be put into use after successful testing.
[0065] The top-down automatic compilation and verification method provided in this disclosure fully considers the overall call relationships and global context of C language projects. It enhances the ability to capture macro-level semantic effects such as function call relationships and parameter types, thereby reducing integration risks and unexpected behaviors. In other words, the global perspective verification in this disclosure reduces the difficulty of debugging and problem localization, thus improving the quality and efficiency of the converted code.
[0066] Figure 6 This is a block diagram illustrating a verification apparatus 600 for code-language conversion according to an exemplary embodiment of the present disclosure.
[0067] Reference Figure 6 The verification device 600 for code language conversion may include a conversion unit extraction module 601, a language conversion module 602, a first compilation module 603, and a second compilation module 604.
[0068] The conversion unit extraction module 601 can extract basic conversion units from C language source code files, wherein the basic conversion unit can at least contain data structures and functions.
[0069] According to an exemplary embodiment of the present disclosure, the conversion unit extraction module 601 can perform AST analysis on a C language source code file to extract basic conversion units.
[0070] The language conversion module 602 can convert data structures into a first Rust language file and functions into a second Rust language file, wherein the function bodies contained in the second Rust language file can be commented.
[0071] According to exemplary embodiments of this disclosure, the above-mentioned function may include multiple functions, and the above-mentioned code language conversion verification device 600 may also include a call relationship extraction module.
[0072] The call relationship extraction module can extract the call relationships between multiple functions from a C language source code file. Then, the language conversion module 602 can convert the functions at each level into corresponding Rust language files based on the call relationships, starting from the leaf functions among the multiple functions. The leaf functions can be functions among the multiple functions that do not call other functions or functions that only call functions that have already been converted into Rust language files.
[0073] The first compilation module 603 can combine the first Rust language file and the second Rust language file to obtain a Rust project structure file, and can perform the first compilation on the Rust project structure file.
[0074] According to an exemplary embodiment of this disclosure, the verification device 600 for code language conversion described above may further include a third compilation module and a third recompilation module.
[0075] The third compilation module can perform a third compilation on the first Rust language file. If the third compilation fails, the third recompilation module can correct the first Rust language file and recompile it until the third compilation succeeds. Next, the first compilation module 603 can combine the first Rust language file that has passed the third compilation with the second Rust language file to obtain the Rust project structure file.
[0076] According to an exemplary embodiment of this disclosure, the verification device 600 for code language conversion described above may further include a first recompilation module.
[0077] If the first compilation fails, that is, if the initial compilation of the Rust project structure file obtained by combining the first Rust language file and the second Rust language file fails, the first recompilation module can also correct the first Rust language file and / or the second Rust language file contained in the assembled Rust project structure file and recompile the corrected Rust project structure file until the first compilation succeeds.
[0078] If the first compilation passes, the second compilation module 604 can decomment the commented function bodies contained in the Rust project structure file, and can perform a second compilation on the Rust project structure file obtained after decommenting to verify it.
[0079] According to an exemplary embodiment of this disclosure, the above function may include a main function and sub-functions called by the main function.
[0080] First, the second compilation module 604 performs a first decommenting process on the function body of the commented-out main function contained in the Rust project structure file, and then compiles the main function in the Rust project structure file obtained after the first decommenting process. Next, if the main function compiles successfully, the second compilation module 604 performs a second decommenting process on the function bodies of the commented-out sub-functions contained in the Rust project structure file obtained after the first decommenting process, and then compiles the sub-functions in the Rust project structure file obtained after the second decommenting process for verification.
[0081] According to an exemplary embodiment of this disclosure, the verification device 600 for code language conversion described above may further include a second recompilation module.
[0082] If the second compilation fails, the second recompile module can correct the function bodies in the Rust project structure file after they have been decommented and then recompile the Rust project structure file until the second compilation succeeds.
[0083] According to an exemplary embodiment of this disclosure, the verification device 600 for code language conversion described above may further include a synchronization update module.
[0084] The synchronization update module can update the call points of parent functions that call a function body based on the correction results obtained after revising the function body (after removing comments). That is, if the parameters of a sub-function need to be adjusted to conform to Rust language conventions, it is necessary to ensure that the call points of all callers (parent functions) of that sub-function are also updated synchronously.
[0085] Figure 7 This is a block diagram illustrating an electronic device 700 according to an exemplary embodiment of the present disclosure.
[0086] Reference Figure 7 The electronic device 700 includes at least one memory 701 and at least one processor 702. The at least one memory 701 stores instructions that, when executed by the at least one processor 702, perform a code language conversion verification method according to an exemplary embodiment of the present disclosure.
[0087] As an example, electronic device 700 may be a PC, tablet, personal digital assistant, smartphone, or other device capable of executing the aforementioned instructions. Here, electronic device 700 is not necessarily a single electronic device, but may be a collection of any devices or circuits capable of executing the aforementioned instructions (or instruction sets) individually or in combination. Electronic device 700 may also be part of an integrated control system or system manager, or may be configured to interconnect with a portable electronic device locally or remotely (e.g., via wireless transmission) through an interface.
[0088] In electronic device 700, processor 702 may include a central processing unit (CPU), a graphics processing unit (GPU), a programmable logic device, a dedicated processor system, a microcontroller, or a microprocessor. By way of example and not limitation, processor may also include analog processors, digital processors, microprocessors, multi-core processors, processor arrays, network processors, etc.
[0089] The processor 702 can execute instructions or code stored in the memory 701, which can also store data. Instructions and data can also be sent and received via a network through a network interface device, which can employ any known transmission protocol.
[0090] The memory 701 can be integrated with the processor 702, for example, by placing RAM or flash memory within an integrated circuit microprocessor. Alternatively, the memory 701 can include a separate device, such as an external disk drive, a storage array, or other storage device usable by any database system. The memory 701 and the processor 702 can be operatively coupled, or can communicate with each other, for example, via I / O ports, network connections, etc., enabling the processor 702 to read files stored in the memory.
[0091] In addition, the electronic device 700 may also include a video display (such as a liquid crystal display) and a user interaction interface (such as a keyboard, mouse, touch input device, etc.). All components of the electronic device 700 can be interconnected via a bus and / or network.
[0092] According to exemplary embodiments of this disclosure, a computer-readable storage medium may also be provided, which, when executed by a processor of an electronic device, enables the electronic device to perform the aforementioned code language conversion verification method. Examples of computer-readable storage media include: read-only memory (ROM), random access programmable read-only memory (PROM), electrically erasable programmable read-only memory (EEPROM), random access memory (RAM), dynamic random access memory (DRAM), static random access memory (SRAM), flash memory, non-volatile memory, CD-ROM, CD-R, CD+R, CD-RW, CD+RW, DVD-ROM, DVD-R, DVD+R, DVD-RW, DVD+RW, DVD-RAM, BD-ROM, BD-R, BD-R, BD-R The computer program in the aforementioned computer-readable storage medium can run in an environment deployed in computer devices such as clients, hosts, agents, and servers. Furthermore, in one example, the computer program and any associated data, data files, and data structures are distributed across a networked computer system, allowing the computer program and any associated data, data files, and data structures to be stored, accessed, and executed in a distributed manner through one or more processors or computers.
[0093] According to exemplary embodiments of the present disclosure, a computer program product may also be provided, including a computer program that, when executed by a processor, implements a verification method for code language conversion according to the present disclosure.
[0094] According to the verification method, apparatus, electronic device, storage medium, and computer program product for code language conversion disclosed herein, when verifying Rust language code converted from C language code, the overall call relationships and global context of the C language project are fully considered. This enhances the ability to capture macro-level semantic effects such as function call relationships and parameter types, thereby reducing integration risks and unexpected behaviors. In other words, the global perspective verification in this disclosure reduces the difficulty of debugging and problem localization, thus improving the quality of the converted code.
[0095] According to an exemplary embodiment of this disclosure, before assembling the Rust language file converted from C language data structures and the Rust language file converted from C language functions, the Rust language file converted from C language data structures can be compiled first. Furthermore, assembly will only occur if the compilation succeeds, ensuring the correctness and security of the assembled Rust project structure file.
[0096] According to exemplary embodiments of this disclosure, the correctness and security of the combined Rust project structure file can be guaranteed by repeatedly compiling and correcting the Rust project structure file obtained by combining the first Rust language file and the second Rust language file.
[0097] According to an exemplary embodiment of this disclosure, by uncommenting the function bodies of functions one by one from top to bottom along the function call chain and compiling them until the function bodies of all functions in the entire Rust project are uncommented and successfully compiled, it can be guaranteed that no function is missed, and the correctness and security of the final Rust project can also be guaranteed.
[0098] According to exemplary embodiments of this disclosure, by repeatedly compiling and correcting the function bodies after de-annotation, the correctness and security of the final Rust project structure file can be guaranteed.
[0099] According to an exemplary embodiment of this disclosure, after modifying the function body of a function, the call point of the parent function that calls the function can also be updated synchronously, which can ensure the consistency of the behavior of each function, that is, ensure accurate calling between each function.
[0100] Other embodiments of this disclosure will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This disclosure is intended to cover any variations, uses, or adaptations of this disclosure that follow the general principles of this disclosure and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this disclosure are indicated by the following claims.
[0101] It should be understood that this disclosure is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this disclosure is limited only by the appended claims.
Claims
1. A verification method for code language conversion, characterized in that, include: Extract basic transformation units from C language source code files, wherein the basic transformation units contain at least data structures and functions; The data structure is converted into a first Rust language file, and the function is converted into a second Rust language file, wherein the function bodies contained in the second Rust language file are commented out; The first Rust language file and the second Rust language file are combined to obtain a Rust project structure file, and the Rust project structure file is then compiled for the first time. If the first compilation passes, the commented function bodies contained in the Rust project structure file are de-commented, and the Rust project structure file obtained after the de-commenting process is compiled a second time for verification. The step of extracting basic transformation units from C language source code files includes: An Abstract Syntax Tree (AST) analysis is performed on the C language source code file to extract the basic transformation units; The step of removing comments from the commented function bodies in the Rust project structure file and then performing a second compilation on the Rust project structure file obtained after the removal process for verification includes: The Rust project structure file is compiled by uncommenting and compiling each commented-out function body in the Rust project structure file layer by layer and from top to bottom, following the function call chain, until all commented-out function bodies in the Rust project structure file are uncommented and compiled successfully.
2. The verification method as described in claim 1, characterized in that, The function includes multiple functions, and the verification method further includes: Extract the call relationships between the multiple functions from the C language source code file; The process of converting the function into a second Rust language file includes: Based on the calling relationship, starting from the leaf functions among the plurality of functions, the functions at each level are converted into corresponding Rust language files layer by layer. The leaf functions are functions among the plurality of functions that do not call other functions or only call functions that have already been converted into Rust language files.
3. The verification method as described in claim 1, characterized in that, The function includes the main function and the sub-functions called by the main function; The process of removing comments from the commented function bodies contained in the Rust project structure file, and then performing a second compilation on the Rust project structure file obtained after the removal of comments for verification, includes: The function body of the commented main function contained in the Rust project structure file is subjected to a first de-commenting process, and the main function is compiled in the Rust project structure file obtained after the first de-commenting process. If the main function compiles successfully, the function bodies of the commented sub-functions contained in the Rust project structure file obtained after the first de-commenting process are subjected to a second de-commenting process, and the sub-functions in the Rust project structure file obtained after the second de-commenting process are compiled for verification.
4. The verification method as described in claim 1, characterized in that, The verification method further includes: If the first compilation fails, the first Rust language file and / or the second Rust language file contained in the Rust project structure file are corrected, and the Rust project structure file obtained after correction is recompiled until the first compilation succeeds.
5. The verification method as described in claim 1, characterized in that, The verification method further includes: If the second compilation fails, the function bodies in the Rust project structure file that have been de-commented are corrected, and the Rust project structure file obtained after correction is recompiled until the second compilation succeeds.
6. The verification method as described in claim 5, characterized in that, The verification method further includes: Based on the correction results obtained by correcting the function body after the comment-removal process, the call point of the parent function that calls the function body is updated synchronously.
7. The verification method as described in claim 1, characterized in that, Before combining the first Rust language file and the second Rust language file to obtain the Rust project structure file, the verification method further includes: Perform a third compilation on the first Rust language file; If the third compilation fails, the first Rust language file is corrected and the corrected first Rust language file is recompiled in the third compilation until the third compilation succeeds. The step of combining the first Rust language file and the second Rust language file to obtain the Rust project structure file includes: The Rust project structure file is obtained by combining the first Rust language file and the second Rust language file, which are compiled in the third method.
8. A verification device for code language conversion, characterized in that, include: The conversion unit extraction module is configured to extract basic conversion units from C language source code files, wherein the basic conversion unit contains at least a data structure and a function; The language conversion module is configured to convert the data structure into a first Rust language file and the function into a second Rust language file, wherein the function bodies contained in the second Rust language file are commented out; The first compilation module is configured to combine the first Rust language file and the second Rust language file to obtain a Rust project structure file, and to perform a first compilation on the Rust project structure file; The second compilation module is configured to, if the first compilation passes, de-comment the commented function bodies contained in the Rust project structure file, and perform a second compilation on the Rust project structure file obtained after de-commenting to verify it. The conversion unit extraction module is configured as follows: An Abstract Syntax Tree (AST) analysis is performed on the C language source code file to extract the basic transformation units; The second compilation module is configured as follows: The Rust project structure file is compiled by uncommenting and compiling each commented-out function body in the Rust project structure file layer by layer and from top to bottom, following the function call chain, until all commented-out function bodies in the Rust project structure file are uncommented and compiled successfully.
9. An electronic device, characterized in that, include: processor; Memory used to store the processor's executable instructions; The processor is configured to execute the instructions to implement the code language conversion verification method as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, When the instructions in the computer-readable storage medium are executed by the processor of the electronic device, the electronic device is able to perform the code language conversion verification method as described in any one of claims 1 to 7.
11. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the code language conversion verification method as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Project reconstruction method, device and equipment based on AngularJS framework and medium
CN117555587A
Function call display method and device, storage medium and computer equipment
CN117827209A