Code language conversion verification method and device, electronic equipment, storage medium and computer program product
By utilizing abstract syntax tree analysis and artificial intelligence correction to transform C language into Rust, a global perspective verification of C language projects is achieved, solving the problem of lacking a global perspective in existing technologies and improving the quality and efficiency of code transformation.
Patent Information
- Application Number
- CN202511299722.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-11
- Publication Date
- 2025-10-17
- Estimated Expiration
- 2045-09-11
AI Technical Summary
Existing technologies lack a global perspective for verification in the conversion of C to Rust code, which increases the difficulty of debugging and problem localization and reduces the quality of project conversion.
By extracting basic transformation units, including data structures and functions, from C language source code files, converting them into Rust language files, and performing layer-by-layer compilation and decommenting, combined with abstract syntax tree analysis and artificial intelligence correction, the overall call relationships and global context are verified.
The ability to capture the macro-semantic impact of function call relationships and parameter types has been enhanced, reducing integration risks and unexpected behaviors, and improving the quality and efficiency of converted code.
Smart Images

Figure CN120803464A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the technical field of code language conversion, and more particularly, to a verification method and device for code language conversion, an electronic device, a storage medium, and a computer program product. BACKGROUND
[0002] Conversion of C language code to Rust language code has become a hot research focus. In the related art, the conversion is mainly realized by a tool C2Rust. However, C2Rust can ensure that the functions before and after conversion are equivalent, but it often generates a large amount of unsafe code, which threatens the security of software. Therefore, it is necessary to verify the conversion result.
[0003] In the related art, the potential syntax errors and type errors in the conversion result are generally corrected by repeatedly compiling; or, the existing test cases of a C project can be used to verify from the bottom function upwards. However, the bottom-up verification based on functions ignores the overall calling relationship and global context of the C language project, which can only ensure that the compilation and local test pass at the level of a single function, and has poor ability to capture the macro semantic influence of function calling relationship and parameter type, which easily leads to integration risks and unexpected behaviors. For example, although some independent functions pass the verification in local test, they may still have problems such as parameter definition error and memory interaction error in a complex environment. This is because the parameter definition of the bottom function may not be suitable for the calling mode of the upper layer, which will lead to logic confusion and syntax confusion of the whole code. Once the whole project has a problem, the verification lacking a global perspective will increase the difficulty of debugging and problem positioning, and thus reduce the conversion quality of the whole project. SUMMARY
[0004] The present disclosure provides a verification method and device for code language conversion, an electronic device, a storage medium, and a computer program product to at least solve the problem in the related art that the verification lacking a global perspective increases the difficulty of debugging and problem positioning, and thus reduces the conversion quality of the whole project.
[0005] According to a first aspect of the embodiments of the present disclosure, a verification method for code language conversion is provided, including: extracting a basic conversion unit from a C language source code file, wherein the basic conversion unit at least contains 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 a function body contained in the second Rust language file is annotated; 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; in the case that the first compilation is passed, performing a de-annotation processing on the annotated function body contained in the Rust project structure file, and performing a second compilation on the Rust project structure file obtained after the de-annotation processing to perform verification.
[0006] Optionally, the function includes a plurality of functions, and the verification method further includes: extracting a calling relationship between the plurality of functions from the C language source code file; and converting the functions into the second Rust language file includes: based on the calling relationship, starting from a leaf function in the plurality of functions, converting functions at each level into a corresponding Rust language file layer by layer, wherein the leaf function is a function that does not call other functions or a function that only calls functions that have been converted into a Rust language file.
[0007] Optionally, the function includes a main function and a sub-function called by the main function, and the de-annotation processing on the annotated function body contained in the Rust project structure file and the second compilation on the Rust project structure file obtained after the de-annotation processing to perform verification includes: performing a first de-annotation processing on the annotated function body of the main function contained in the Rust project structure file, and performing a main function compilation on the Rust project structure file obtained after the first de-annotation processing; in the case that the main function compilation is passed, performing a second de-annotation processing on the annotated function body of the sub-function contained in the Rust project structure file obtained after the first de-annotation processing, and performing a sub-function compilation on the Rust project structure file obtained after the second de-annotation processing to perform verification.
[0008] Optionally, the verification method further includes: in the case that the first compilation is not passed, modifying the first Rust language file and / or the second Rust language file contained in the Rust project structure file and re-performing the first compilation on the Rust project structure file obtained after the modification until the first compilation is passed.
[0009] Optionally, the verification method further comprises: in the case that the second compilation fails, modifying the function body after the de-annotation processing contained in the Rust project structure file and re-compiling the Rust project structure file obtained after the modification until the second compilation passes.
[0010] Optionally, the verification method further comprises: based on a modification result obtained by modifying the function body after the de-annotation processing, synchronously updating a call point of a parent function that calls the function body.
[0011] Optionally, before the first Rust language file and the second Rust language file are combined to obtain a Rust project structure file, the verification method further comprises: third compiling the first Rust language file; in the case that the third compilation fails, modifying the first Rust language file and re-compiling the first Rust language file obtained after the modification until the third compilation passes; and the combining the first Rust language file and the second Rust language file to obtain the Rust project structure file comprises: combining the first Rust language file that passes the third compilation and the second Rust language file to obtain the Rust project structure file.
[0012] Optionally, the extracting the basic conversion unit from the C language source code file comprises: performing abstract syntax tree (AST) analysis on the C language source code file to extract the basic conversion unit.
[0013] According to a second aspect of the embodiments of the present disclosure, a verification device for code language conversion is provided, comprising: a conversion unit extraction module configured to extract a basic conversion unit from a C language source code file, wherein the basic conversion unit at least contains a data structure and a function; a language conversion module configured to convert the data structure into a first Rust language file and convert the function into a second Rust language file, wherein a function body contained in the second Rust language file is annotated; 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 first compile the Rust project structure file; and a second compilation module configured to, in the case that the first compilation passes, perform de-annotation processing on the annotated function body contained in the Rust project structure file and second compile the Rust project structure file obtained after the de-annotation processing to perform verification.
[0014] Optionally, the functions include a plurality of functions, and the verification device further includes a call relationship extraction module configured to extract a call relationship between the plurality of functions from the C language source code file; and the language conversion module is configured to convert each level of function to a corresponding Rust language file starting from a leaf function in the plurality of functions based on the call relationship, wherein the leaf function is a function in the plurality of functions that does not call other functions or a function that only calls functions that have been converted to Rust language files.
[0015] Optionally, the functions include a main function and a sub-function called by the main function, and the second compiling module is configured to perform first de-annotation processing on a function body of the annotated main function included in the Rust project structure file, perform main function compiling on the Rust project structure file obtained after the first de-annotation processing, perform second de-annotation processing on a function body of the annotated sub-function included in the Rust project structure file obtained after the first de-annotation processing in a case where the main function compiling is passed, and perform sub-function compiling on the Rust project structure file obtained after the second de-annotation processing to perform verification.
[0016] Optionally, the verification device further includes a first re-compiling module configured to, in a case where the first compiling fails, modify the first Rust language file and / or the second Rust language file included in the Rust project structure file and re-perform the first compiling on the Rust project structure file obtained after the modification until the first compiling is passed.
[0017] Optionally, the verification device further includes a second re-compiling module configured to, in a case where the second compiling fails, modify the function body after the de-annotation processing included in the Rust project structure file and re-perform the second compiling on the Rust project structure file obtained after the modification until the second compiling is passed.
[0018] Optionally, the verification device further includes a synchronous updating module configured to, based on a modification result obtained by modifying the function body after the de-annotation processing, synchronously update a call point of a parent function that calls the function body.
[0019] Optionally, the verification device further comprises: a third compiling module configured to compile the first Rust language file; a third recompiling module configured to modify the first Rust language file and recompile the modified first Rust language file until the third compilation is passed, if the third compilation fails; the first compiling module is configured to combine the first Rust language file passed the third compilation and the second Rust language file to obtain the Rust project structure file.
[0020] Optionally, the extracting module of the conversion unit is configured to perform abstract syntax tree (AST) analysis on the C language source code file to extract the basic conversion unit.
[0021] According to a third aspect of the embodiments of the present disclosure, an electronic device is provided, comprising: a processor; a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement the verification method for code language conversion according to the present disclosure.
[0022] According to a fourth aspect of the embodiments of the present disclosure, a computer readable storage medium is provided, when instructions in the computer readable storage medium are executed by a processor of an electronic device, the electronic device can execute the verification method for code language conversion according to the present disclosure.
[0023] According to a fifth aspect of the embodiments of the present disclosure, a computer program product is provided, comprising a computer program, when the computer program is executed by a processor, the verification method for code language conversion according to the present disclosure is implemented.
[0024] The technical solutions provided by the embodiments of the present disclosure at least bring the following beneficial effects: In the present disclosure, when verifying the Rust language code converted from the C language code, the overall calling relationship and global context of the C language project are fully considered, which can enhance the ability to capture macro semantic influences such as function calling relationship and parameter passing type, and thus can reduce integration risks and unexpected behaviors. That is, in the present disclosure, the verification from the global perspective can reduce the difficulty of debugging and problem positioning, thereby improving the code quality after language conversion.
[0025] It should be understood that the above general description and the following detailed description are only exemplary and explanatory, and cannot limit the present disclosure. BRIEF DESCRIPTION OF DRAWINGS
[0026] The accompanying drawings incorporated in and forming a part of the specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the disclosure without imposing undue limitations on the disclosure.
[0027] Figure 1 is a flowchart illustrating a verification method of code language conversion according to an example embodiment of the present disclosure; Figure 2 is a flowchart illustrating a verification method of code language conversion according to an example embodiment of the present disclosure; Figure 3 is a flowchart illustrating a verification method of code language conversion according to an example embodiment of the present disclosure; Figure 4 is a flowchart illustrating a verification method of code language conversion according to an example embodiment of the present disclosure; Figure 5 is a flowchart illustrating a verification method of code language conversion according to an example embodiment of the present disclosure; Figure 6 is a block diagram of a verification device of code language conversion according to an example embodiment of the present disclosure; Figure 7 is a block diagram of an electronic device according to an example embodiment of the present disclosure. DETAILED DESCRIPTION
[0028] In order to make the ordinary person skilled in the art better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be described clearly and completely below with reference to the drawings.
[0029] It should be noted that the terms "first", "second", and the like in the specification and claims of the present disclosure and the above-described drawings are used to distinguish similar objects, and do not necessarily have to describe a specific order or a chronological sequence. It should be understood that the data thus used can be interchanged under appropriate circumstances, so that the embodiments of the present disclosure described herein can be implemented in an order other than that illustrated or described herein. The embodiments described in the following embodiments do not represent all embodiments consistent with the present disclosure. Rather, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0030] It should be noted that "at least one of a plurality of items" appearing in the present disclosure means that the three types of alternatives of "any one of the plurality of items", "a combination of any two or more of the plurality of items", and "all of the plurality of items" are included. For example, "including at least one of A and B" includes the following three alternatives: (1) including A; (2) including B; and (3) including A and B. For another example, "performing at least one of step one and step two" means the following three alternatives: (1) performing step one; (2) performing step two; and (3) performing step one and step two.
[0031] Figure 1 is a flowchart illustrating a verification method of code language conversion according to an exemplary embodiment of the present disclosure.
[0032] Reference Figure 1 In step 101, a basic conversion unit may be extracted from a C language source code file, wherein the basic conversion unit may at least include a data structure and a function.
[0033] According to an exemplary embodiment of the present disclosure, an Abstract Syntax Tree (AST) analysis may be performed on a C language source code file to extract basic conversion units.
[0034] For example, by performing a comprehensive AST parsing of the entire C language source code, C language data structure definitions, C language global variables, C language macro definitions, C language function information, and so on can be extracted from the C language source code. C language data structure definitions may include, but are not limited to, structures (structs), unions (unions), enumerations (enums), their members, and memory layout; C language global variables may include, but are not limited to, types, initialization values, and linkage attributes; C language macro definitions may include, but are not limited to, function-like macros that affect types or behaviors; and 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).
[0035] In step 102 , the data structure may be converted into a first Rust language file, and the function may be converted into a second Rust language file, wherein the function body contained in the second Rust language file may be annotated.
[0036] Figure 2 1 is a flowchart illustrating converting a data structure into a Rust language file according to an exemplary embodiment of the present disclosure.
[0037] Reference Figure 2 , the C language data structure definition parsed from the AST can be provided as context to the Large Language Model (LLM), and then the C language data structure can be converted to the equivalent Rust data structure through the LLM. For example, refer to Figure 2A temporary project containing only the Rust data structure can be constructed, and the compilation can be attempted. If the compilation fails, it can be automatically corrected by using artificial intelligence (AI), i.e., using LLM, or it can be manually corrected until the Rust data structure can be successfully compiled.
[0038] According to the example embodiments of the present disclosure, the above function can include a plurality of functions. As described above, the calling relationship between the plurality of functions can also be extracted from the C language source code file, for example, the calling graph between the plurality of functions can be extracted. Then, based on the calling relationship, i.e., the function calling graph, the functions at each level can be converted into the corresponding Rust language file layer by layer starting from the leaf function in the plurality of functions, wherein the leaf function can be a function in the plurality of functions that does not call other functions or a function that only calls functions that have been converted into the Rust language file.
[0039] Figure 3 FIG. 1 is a flowchart illustrating a process of converting a function into a Rust language file according to an example embodiment of the present disclosure.
[0040] Referring to Figure 3 Based on the function calling graph, the leaf function in the C language project can be identified. Then, the source code of the C language function can be provided to the LLM in batches starting from the leaf function for conversion, and the Rust data structure definition and the Rust signature of the sub-functions (as stubs) on which these functions depend can also be provided to the LLM as context. In addition, the converted Rust function body should be completely commented out, and the default value or placeholder value of its corresponding Rust type needs to be returned. In this way, each level of function can be processed layer by layer in the order of dependency between functions (e.g., layer by layer upwards from the leaf function), i.e., the functions can be converted in batches in the order of dependency between functions (stubbed from bottom to top), until all C language functions are converted into Rust functions with annotated stub bodies.
[0041] 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 subjected to a first compilation.
[0042] Figure 4 FIG. 2 is a flowchart illustrating a process of constructing and preliminarily compiling a Rust project structure file according to an example embodiment of the present disclosure.
[0043] Referring to Figure 4For example, all the converted Rust data structures and the annotated function stubs can be assembled into a complete Rust project structure file and can be preliminarily compiled against the assembled Rust project structure file. At this stage, since all the function bodies are annotated, the preliminary compilation mainly verifies the correctness of the Rust data structures and the matching of all the function signatures. If the compilation fails, the data structure conversion stage or the function signature conversion stage can be traced back to make corrections until the preliminary compilation passes.
[0044] According to exemplary embodiments of the present disclosure, the first Rust language file can also be thirdly compiled. In the case where the third compilation fails, the first Rust language file can be corrected and the first Rust language file obtained after the correction can be re-compiled against the third compilation until the third compilation passes. Next, the first Rust language file and the second Rust language file that pass the third compilation can be combined to obtain a Rust project structure file.
[0045] In this way, the Rust language file converted from the data structure of the C language can be compiled before being assembled with the Rust language file converted from the function of the C language. Moreover, the assembly is performed only in the case where the compilation passes, which can ensure the correctness and safety of the Rust project structure file obtained by the assembly.
[0046] According to exemplary embodiments of the present disclosure, in the case where the first compilation fails, i.e., in the case where the preliminary compilation against 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 included in the Rust project structure file obtained by the assembly can be corrected and the Rust project structure file obtained after the correction can be re-compiled against the first compilation until the first compilation passes.
[0047] 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 safety of the Rust project structure file obtained by the combination can be ensured.
[0048] In step 104, in the case where the first compilation passes, the annotated function body included in the Rust project structure file can be de-annotated, and the Rust project structure file obtained after the de-annotation can be secondly compiled for verification.
[0049] According to the example embodiments of the present disclosure, the above functions can include a main function and sub-functions called by the main function.
[0050] First, the first decommenting processing can be performed on the function body of the commented main function included in the Rust project structure file, and the main function compilation can be performed on the Rust project structure file obtained after the first decommenting processing. Next, in the case where the main function compilation is passed, the second decommenting processing can be performed on the function body of the commented sub-function included in the Rust project structure file obtained after the first decommenting processing, and the sub-function compilation can be performed on the Rust project structure file obtained after the second decommenting processing for verification.
[0051] Figure 5 is a flowchart illustrating a step-by-step activation and compilation of a Rust project structure file from top to bottom according to the example embodiments of the present disclosure.
[0052] Referring to Figure 5 The first decommenting processing can be performed on the function body of the commented main function, i.e., the Rust function body of the main function can be first uncommented, starting from the main function (main function) or the equivalent entry point of the Rust project. Then, the main function compilation can be performed on the Rust project structure file obtained after the first decommenting processing. At this stage, since the sub-functions of the main function are still in the commented state and return default values, this compilation mainly verifies whether there is a problem in the logic of the main function itself and whether the call to the data structure and the sub-function stub is correct. If the main function compilation fails, the function body of the main function can be corrected by using AI (e.g., re-prompting the LLM to fix the main function body) or manual intervention until the main function compilation is passed.
[0053] Next, after the main function compilation is passed, the sub-function activation and compilation can be iteratively performed. For example, the second decommenting processing can be performed on the function body of each of the commented sub-functions, i.e., the function body of each of the sub-functions of the main function can be sequentially uncommented. And after the function body of each of the sub-functions is uncommented, the sub-function compilation can be immediately performed on the Rust project structure file obtained after the second decommenting processing, i.e., the entire Rust project can be immediately recompiled. If the sub-function compilation fails, the function body of the currently activated sub-function and its call relationship can be mainly corrected, and the recompiled Rust project structure file obtained after the correction can be recompiled until the sub-function compilation is passed.
[0054] In this way, by canceling the function body of each function one by one from top to bottom along the call chain of the function, and compiling the function body, it can be ensured that no function is missed, and the correctness and safety of the final Rust project can be ensured.
[0055] According to the example embodiment of the present disclosure, in the case where the second compilation fails, the function body after the decommenting processing included in the Rust project structure file can be modified, and the second compilation can be performed again on the Rust project structure file obtained after the modification until the second compilation passes. In this way, by repeatedly compiling and modifying the function body after the decommenting processing, the correctness and safety of the final Rust project structure file can be ensured.
[0056] According to the example embodiment of the present disclosure, the call point of the parent function calling the function body after the decommenting processing can be updated synchronously based on the modification result obtained by modifying the function body. That is, if the parameters of a certain child function need to be adjusted to adapt to the Rust language habits, it is necessary to ensure that the call points of all callers (parent functions) of the child function are also updated synchronously. In this way, after modifying the function body of a certain function, the call points of the parent functions calling the function can also be updated synchronously, and the behavior consistency of each function can be ensured, that is, the accurate calling between each function can be ensured.
[0057] It should be noted that after the entire Rust project is compiled, subsequent functional debugging and integration testing can be performed, and then the project can be put into use after the testing is successful.
[0058] The top-down automatic compilation and verification method provided by the present disclosure fully considers the overall calling relationship and global context of the C language project, and can enhance the ability to capture macro semantic influences such as function calling relationship and parameter type, thereby reducing integration risks and unexpected behaviors. That is, in the present disclosure, the verification from the global perspective can reduce the difficulty of debugging and problem positioning, thereby improving the code quality and conversion efficiency after language conversion.
[0059] Figure 6 is a block diagram of a code language conversion verification device 600 according to an example embodiment of the present disclosure.
[0060] Referring to Figure 6 The code language conversion verification device 600 can include a conversion unit extraction module 601, a language conversion module 602, a first compilation module 603, and a second compilation module 604.
[0061] The conversion unit extraction module 601 can extract a basic conversion unit from the C language source code file, wherein the basic conversion unit can at least include a data structure and a function.
[0062] According to an example embodiment of the present disclosure, the conversion unit extraction module 601 can perform AST analysis on the C language source code file to extract the basic conversion unit.
[0063] The language conversion module 602 can convert the data structure into a first Rust language file, and can convert the function into a second Rust language file, wherein the function body included in the second Rust language file can be annotated.
[0064] According to an example embodiment of the present disclosure, the above-mentioned function can include a plurality of functions, and the code language conversion verification device 600 can further include a call relationship extraction module.
[0065] The call relationship extraction module can extract a call relationship between the plurality of functions from the C language source code file. Then, the language conversion module 602 can convert the functions at each level into a corresponding Rust language file starting from a leaf function in the plurality of functions based on the call relationship, wherein the leaf function can be a function that does not call other functions in the plurality of functions or a function that only calls functions that have been converted into a Rust language file.
[0066] 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 a first compilation on the Rust project structure file.
[0067] According to an example embodiment of the present disclosure, the code language conversion verification device 600 can further include a third compilation module and a third recompilation module.
[0068] The third compilation module can perform a third compilation on the first Rust language file. In the case where the third compilation fails, the third recompilation module can modify the first Rust language file and recompile the modified first Rust language file until the third compilation passes. Next, the first compilation module 603 can combine the first Rust language file that passes the third compilation and the second Rust language file to obtain a Rust project structure file.
[0069] According to an example embodiment of the present disclosure, the code language conversion verification device 600 can further include a first recompilation module.
[0070] In the case that the first compilation fails, i.e. in the case that the preliminary compilation for 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 further modify the first Rust language file and / or the second Rust language file contained in the assembled Rust project structure file and recompile the Rust project structure file obtained after the modification until the first compilation passes.
[0071] In the case that the first compilation passes, the second compilation module 604 can perform a de-annotation process on the annotated function body contained in the Rust project structure file, and can perform a second compilation on the Rust project structure file obtained after the de-annotation process to perform verification.
[0072] According to an example embodiment of the present disclosure, the above-mentioned functions can include a main function and a sub-function called by the main function.
[0073] First, the second compilation module 604 can perform a first de-annotation process on the function body of the annotated main function contained in the Rust project structure file, and can perform a main function compilation on the Rust project structure file obtained after the first de-annotation process. Next, in the case that the main function compilation passes, the second compilation module 604 can perform a second de-annotation process on the function body of the annotated sub-function contained in the Rust project structure file obtained after the first de-annotation process, and can perform a sub-function compilation on the Rust project structure file obtained after the second de-annotation process to perform verification.
[0074] According to an example embodiment of the present disclosure, the above-mentioned verification device 600 for code language conversion can further include a second recompilation module.
[0075] In the case that the second compilation fails, the second recompilation module can modify the function body after the de-annotation process contained in the Rust project structure file and recompile the Rust project structure file obtained after the modification until the second compilation passes.
[0076] According to an example embodiment of the present disclosure, the above-mentioned verification device 600 for code language conversion can further include a synchronization update module.
[0077] The synchronization update module can synchronize the call point of the parent function calling the function body after the de-annotation process based on the modification result obtained by modifying the function body after the de-annotation process. That is, if the parameters of a certain sub-function need to be adjusted to adapt to the Rust language habits, it is necessary to ensure that the call points of all callers (parent functions) of the sub-function are also synchronized.
[0078] Figure 7 is a block diagram illustrating an electronic device 700 according to an example embodiment of the present disclosure.
[0079] Referring to Figure 7 , the electronic device 700 includes at least one memory 701 having instructions stored therein and at least one processor 702 that executes the instructions to perform a method of verifying code language conversion according to an example embodiment of the present disclosure.
[0080] As an example, the electronic device 700 can be a PC computer, a tablet device, a personal digital assistant, a smart phone, or other device capable of executing the above instructions. Here, the electronic device 700 is not necessarily a single electronic device, but can be a collection of any device or circuit capable of executing the above instructions (or instruction sets) alone or in combination. The electronic device 700 can also be a part of an integrated control system or a system manager, or can be configured as a portable electronic device that interfaces with a local or remote (e.g., via wireless transmission).
[0081] In the electronic device 700, the processor 702 can include a central processing unit (CPU), a graphics processing unit (GPU), a programmable logic device, a dedicated processor system, a microcontroller, or a microprocessor. As an example and not a limitation, the processor can also include an analog processor, a digital processor, a microprocessor, a multi-core processor, a processor array, a network processor, etc.
[0082] The processor 702 can execute instructions or code stored in the memory 701, which can also store data. The instructions and data can also be transmitted and received via a network through a network interface device, which can employ any known transmission protocol.
[0083] The memory 701 can be integrated with the processor 702, for example, by arranging RAM or flash memory within an integrated circuit microprocessor, etc. In addition, 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, through an I / O port, a network connection, etc., so that the processor 702 can read files stored in the memory.
[0084] In addition, the electronic device 700 can also include a video display (such as a liquid crystal display) and a user interaction interface (such as a keyboard, a mouse, a touch input device, etc.). All components of the electronic device 700 can be connected to each other via a bus and / or a network.
[0085] According to an example embodiment of the disclosure, a computer-readable storage medium can also be provided, which, when instructions in the computer-readable storage medium are executed by a processor of an electronic device, enables the electronic device to perform the above-described verification method for code language conversion. Examples of the computer-readable storage medium here include a read-only memory (ROM), a random access programmable read-only memory (PROM), an electrically erasable programmable read-only memory (EEPROM), a random access memory (RAM), a dynamic random access memory (DRAM), a static random access memory (SRAM), a flash memory, a non-volatile memory, a CD-ROM, a CD-R, a CD+R, a CD-RW, a CD+RW, a DVD-ROM, a DVD-R, a DVD+R, a DVD-RW, a DVD+RW, a DVD-RAM, a BD-ROM, a BD-R, a BD-RLTH, a BD-RE, a Blu-ray or an optical disc memory, a hard disk drive (HDD), a solid state drive (SSD), a card memory (such as a multimedia card, a secure digital (SD) card, or an extreme digital (XD) card), a magnetic tape, a floppy disk, a magneto-optical data storage device, an optical data storage device, a hard disk, a solid state disk, and any other device configured to store a computer program and any associated data, data files, and data structures in a non-transitory manner and provide the computer program and any associated data, data files, and data structures to a processor or computer so that the processor or computer can execute the computer program. The computer program in the above-described computer-readable storage medium can be run in an environment deployed in a computer device such as a client, a host, an agent device, a server, etc., and in addition, in one example, the computer program and any associated data, data files, and data structures are distributed over a networked computer system so that the computer program and any associated data, data files, and data structures are stored, accessed, and executed by one or more processors or computers in a distributed manner.
[0086] According to an example embodiment of the disclosure, a computer program product can also be provided, which includes a computer program that, when executed by a processor, implements the verification method for code language conversion according to the disclosure.
[0087] The verification method, apparatus, electronic device, storage medium, and computer program product for code language conversion according to the disclosure, when verifying Rust language code converted from C language code, fully consider the overall call relationship and global context of the C language project, can enhance the ability to capture macro semantic influences such as function call relationship and parameter type, and thus can reduce integration risks and unexpected behaviors. That is, in the disclosure, verification from a global perspective can reduce the difficulty of debugging and problem positioning, thereby improving the code quality after language conversion.
[0088] According to the exemplary embodiments of the present disclosure, the Rust language file converted from the data structure of the C language can be compiled before being assembled with the Rust language file converted from the function of the C language. And the assembly is only performed when the compilation is passed, which can ensure the correctness and safety of the Rust project structure file obtained by the assembly.
[0089] According to the exemplary embodiments of the present disclosure, the Rust project structure file obtained by the combination of the first Rust language file and the second Rust language file can be repeatedly compiled and corrected, which can ensure the correctness and safety of the Rust project structure file obtained by the combination.
[0090] According to the exemplary embodiments of the present disclosure, by canceling the function body of the function layer by layer and one by one from top to bottom along the call chain of the function until the function bodies of all functions in the entire Rust project are canceled and successfully compiled, it can be ensured that no function is missed, and the correctness and safety of the final obtained Rust project can be ensured.
[0091] According to the exemplary embodiments of the present disclosure, the Rust project structure file obtained by the combination of the first Rust language file and the second Rust language file can be repeatedly compiled and corrected, which can ensure the correctness and safety of the Rust project structure file obtained by the combination.
[0092] According to the exemplary embodiments of the present disclosure, after the function body of a certain function is corrected, the call point of the parent function calling the function can also be synchronously updated, which can ensure the behavior consistency of each function, that is, the accurate calling between each function can be ensured.
[0093] Other embodiments of the present disclosure will be apparent to those skilled in the art from consideration of the specification and practice of the aspects disclosed herein. The present disclosure is intended to cover any variations, uses or adaptations of the present disclosure following, in general, the principles of the present disclosure and including such
[0094] It should be understood that the present disclosure is not limited to the precise structures described above and illustrated in the drawings and that various modifications and changes can be made without departing from the scope thereof. The scope of the present disclosure is limited only by the claims that follow.
Claims
1. A verification method for code language conversion, characterized in that: include: Extracting a basic conversion unit from a C language source code file, wherein the basic conversion unit at least includes 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 body contained in the second Rust language file is annotated; 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, the annotated function body contained in the Rust project structure file is de-annotated, and the Rust project structure file obtained after the de-annotation is secondly compiled for verification.
2. The verification method according to claim 1, wherein: The function includes multiple functions, and the verification method further includes: Extracting the calling relationship between the multiple functions from the C language source code file; Converting the function into a second Rust language file includes: Based on the calling relationship, starting from the leaf function among the multiple functions, the functions of each level are converted into corresponding Rust language files layer by layer, wherein the leaf function is a function among the multiple functions that does not call other functions or only calls functions that have been converted into Rust language files.
3. The verification method according to claim 1, wherein: The functions include the main function and the sub-functions called by the main function; The de-annotating the annotated function body contained in the Rust project structure file, and performing a second compilation on the Rust project structure file obtained after the de-annotation processing for verification, includes: Performing a first de-annotation process on the function body of the annotated main function contained in the Rust project structure file, and performing main function compilation on the Rust project structure file obtained after the first de-annotation process; When the main function is compiled successfully, a second de-annotation process is performed on the function body of the annotated sub-function contained in the Rust project structure file obtained after the first de-annotation process, and the sub-function compilation is performed on the Rust project structure file obtained after the second de-annotation process for verification.
4. The verification method according to claim 1, wherein: The verification method further comprises: 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 first compilation is re-performed on the corrected Rust project structure file until the first compilation passes.
5. The verification method according to claim 1, wherein: The verification method further comprises: If the second compilation fails, the de-annotated function body contained in the Rust project structure file is corrected and the corrected Rust project structure file is recompiled for the second time until the second compilation passes.
6. The verification method according to claim 5, wherein: The verification method further comprises: Based on the correction result obtained by correcting the de-annotated function body, the calling point of the parent function that calls the function body is synchronously updated.
7. The verification method according to claim 1, wherein: Before combining the first Rust language file and the second Rust language file to obtain a Rust project structure file, the verification method further includes: Performing a third compilation on the first Rust language file; If the third compilation fails, correct the first Rust language file and re-compile the corrected first Rust language file until the third compilation passes. The combining the first Rust language file and the second Rust language file to obtain a Rust project structure file includes: The first Rust language file and the second Rust language file compiled through the third compilation are combined to obtain the Rust project structure file.
8. The verification method according to claim 1, wherein: The step of extracting the basic conversion unit from the C language source code file includes: An abstract syntax tree (AST) analysis is performed on the C language source code file to extract the basic conversion unit.
9. A verification device for code language conversion, characterized in that: include: A conversion unit extraction module is configured to extract a basic conversion unit from a C language source code file, wherein the basic conversion unit at least includes a data structure and a function; a language conversion module configured to convert the data structure into a first Rust language file and convert the function into a second Rust language file, wherein the function body contained in the second Rust language file is annotated; a first compiling module, configured to combine the first Rust language file and the second Rust language file to obtain a Rust project structure file, and perform a first compilation on the Rust project structure file; The second compilation module is configured to, when the first compilation passes, de-annotate the annotated function body contained in the Rust project structure file, and perform a second compilation on the Rust project structure file obtained after the de-annotation processing for verification.
10. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is configured to execute the instructions to implement the code language conversion verification method according to any one of claims 1 to 8.
11. A computer-readable storage medium, characterized in that When the instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to perform the code language conversion verification method according to any one of claims 1 to 8.
12. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the method for verifying code language conversion according to any one of claims 1 to 8 is implemented.
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
Code running method and device for cross-platform programming language, equipment and medium
CN119806636A
Code conversion method and device and computer readable storage medium
CN119847537A
Methods, devices, and media for two-pass source code transformation
US20220357934A1