Configurable code cutting method

Through the compiler preprocessing module to extract and process conditional compilation information, combined with the configuration file custom trimming, the problem of low source code trimming efficiency in the existing technology is solved, and efficient and accurate code trimming is achieved to meet the release needs of multiple platforms and versions. .

CN119025119AActive Publication Date: 2024-11-26WUHAN LINGJIU MICROELECTRONICS CO LTD

Patent Information

Application Number
CN202411064920.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-05
Publication Date
2024-11-26
Estimated Expiration
2044-08-05

AI Technical Summary

Technical Problem

It is difficult for the prior art to directly configurable cropping of uncompiled but processed source codes, especially in the release of software that meets different platforms, functional versions and security requirements. Traditional methods are inefficient and error-prone.

Method used

The compiler preprocessing module is used to extract conditional compilation information, customize cropping through configuration files, build conditional compilation data units, and obtain valid code segments based on the true and false values ​​to realize code cropping.

Benefits of technology

It improves the efficiency and accuracy of code cutting, can handle complex conditional compilation structures, ensures the integrity and correctness of the cropped code, and meets the release needs of different platforms and functional versions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119025119A_ABST
    Figure CN119025119A_ABST
Patent Text Reader

Abstract

The invention provides a configurable code cutting method, which adopts a compiler preprocessing module to realize configurable code cutting, can process a complex conditional compiling structure, supports self-defined cutting according to a configuration file, and can better ensure the integrity and correctness of cut codes by cutting under conditional compiling logic, so that the code cutting efficiency is improved. The problem that in the source code publishing process of a large software project, in order to meet the external publishing requirements of different platforms, different function versions and safety requirements is solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computers, and more particularly to a configurable code tailoring method. Background Art

[0002] In the release process of highly complex and large-scale software projects, it is often necessary to meet the external release requirements of different platforms, different functional versions and security, and to perform configurable tailoring of the released source code.

[0003] When designing software, conditional compilation instructions are usually used to handle logic such as platform differences, version differences, and function switches. Although these logics can be identified and tailored in the preprocessing stage of compilation, there is currently no implementation method for directly releasing the source code that has been processed according to the logic but has not been compiled. Summary of the invention

[0004] The present invention aims at solving the technical problems existing in the prior art and provides a configurable code cutting method, comprising:

[0005] Step 1, copy a source code file to generate a copy file, and use a compiler to extract all conditional compilation information in the source code file, each conditional compilation information includes a conditional compilation expression and location information;

[0006] Step 2, matching each conditional compilation expression according to the configuration file, finding the macro and corresponding value configured for each conditional compilation expression from the configuration file; modifying the value in the conditional compilation expression at the same position in the copy file according to the configured macro and the corresponding value;

[0007] Step 3, combining the conditional compilation information at the same nesting level according to all the conditional compilation information obtained in the source code file, constructing a conditional compilation data unit, and writing all the conditional compilation data units into an intermediate file;

[0008] Step 4, read all conditional compilation data units from the intermediate file, obtain the truth value of each conditional compilation expression calculated by the compiler in the modified copy file, and obtain the valid code segment in each conditional compilation data unit according to the truth value of each conditional compilation expression;

[0009] Step 5, determining the line number to be deleted in each conditional compilation data unit according to the valid code segment in each conditional compilation data unit;

[0010] Step 6: based on the line number to be deleted in each conditional compilation data unit, the code of the corresponding line number in the source code file is trimmed to obtain the trimmed code.

[0011] Based on the above technical solution, the present invention can also make the following improvements.

[0012] Optionally, in step 1, a compiler is used to extract all conditional compilation information in the source code file, each piece of conditional compilation information includes a conditional compilation expression and location information, including:

[0013] The compiler finds each conditional compilation identifier in the source code file, and extracts the conditional compilation information corresponding to each conditional compilation identifier, wherein the conditional compilation information includes the expression content of the conditional compilation identifier and the line number of the conditional compilation identifier.

[0014] Optionally, the configuration file configures the value of each macro parameter in each conditional compilation expression in the source code file.

[0015] Optionally, in step 4, in the modified copy file, obtaining the truth value of each conditional compilation expression calculated by the compiler, and obtaining the valid code segment in each conditional compilation data unit according to the truth value of each conditional compilation expression, including:

[0016] According to the value of each conditional compilation expression in the modified copy file, the compiler calculates the true or false value of each conditional compilation expression;

[0017] According to the truth value of each conditional compilation expression in each conditional compilation data unit, the code segment corresponding to the true value is taken as the valid code segment.

[0018] Optionally, the step 5, determining the line number to be deleted in each conditional compilation data unit according to the valid code segment in each conditional compilation data unit, includes:

[0019] The codes other than the valid code segment are regarded as the codes to be deleted, and the line number of the codes to be deleted in each conditional compilation data unit is recorded as the line number to be deleted.

[0020] Optionally, the step 6, based on the line number to be deleted in each conditional compilation data unit, trimming the code of the corresponding line number in the source code file to obtain the trimmed code, includes:

[0021] Summarize and remove duplicates of the line numbers to be deleted in each conditional compilation data unit;

[0022] Sort the line numbers to be deleted after deduplication, and delete the codes of the corresponding line numbers in the source code file in ascending order to obtain the trimmed code.

[0023] The present invention provides a configurable code clipping method, which adopts a compiler preprocessing module to realize configurable code clipping, can process complex conditional compilation structures, and supports customized clipping according to configuration files. At the same time, clipping under conditional compilation logic can also better ensure the integrity and correctness of the clipped code, and solve the external release requirements of large-scale software projects in the source code release process in order to meet different platforms, different functional versions and security requirements. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] Figure 1 A flow chart of a configurable code cutting method provided by the present invention;

[0025] Figure 2 The figure is a flowchart of a configurable code tailoring method. DETAILED DESCRIPTION

[0026] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention. In addition, the technical features in the various embodiments or single embodiments provided by the present invention can be arbitrarily combined with each other to form a feasible technical solution. This combination is not subject to the constraints of the sequence of steps and / or the structural composition mode, but must be based on the ability of ordinary technicians in this field to achieve. When the combination of technical solutions is contradictory or cannot be achieved, it should be considered that such a combination of technical solutions does not exist and is not within the scope of protection required by the present invention.

[0027] The traditional method of manually annotating and deleting code is inefficient and prone to errors. The existing regular expression matching and other trimming logic judgments for text cannot obtain the compilation information of the code and are difficult to handle complex conditional compilation structures. On the other hand, as the complexity of conditional compilation instructions increases, the matching rules will become more cumbersome and prone to errors. It is also impossible to integrate custom configuration into the code trimming process. Trimming during the dynamic library generation stage is also prone to abnormal operation errors of subsequent programs.

[0028] To address these shortcomings, the present invention adopts a configurable code clipping method implemented by a compiler preprocessing module, which can handle complex conditional compilation structures and support customized clipping according to configuration files. At the same time, clipping under conditional compilation logic can also better ensure the integrity and correctness of the clipped code.

[0029] Figure 1A configurable code cutting method method flow chart provided by the present invention is as follows: Figure 1 , methods include:

[0030] Step 1, copy a source code file to generate a copy file, and use a compiler to extract all conditional compilation information in the source code file, each conditional compilation information includes a conditional compilation expression and location information.

[0031] It is understandable that a copy of the source code is made to generate a copy file. Then, a compiler preprocessing module is used to preprocess the source code file, wherein the compiler preprocessing module is a part of the compiler.

[0032] Preprocessing means: finding each conditional compilation identifier in the source code file through the compiler, extracting the conditional compilation information corresponding to each conditional compilation identifier, and the conditional compilation information includes the expression content of the conditional compilation identifier and the line number of the conditional compilation identifier.

[0033] Specifically, for example, for the following code:

[0034]

[0035] When the compiler encounters each conditional compilation identifier (#if, #ifdef, #ifndef, #ifdefined, etc.), it extracts conditional compiler information, such as the expression content (FEATURE_C) and line number (1) of the #if identifier, and obtains all conditional compilation information in the source code file.

[0036] Step 2, match each conditional compilation expression according to the configuration file, find the macro and corresponding value configured for each conditional compilation expression from the configuration file; modify the value in the conditional compilation expression at the same position in the copy file according to the configured macro and corresponding value.

[0037] It is understandable that a custom configuration file can be used, and the configuration file configures the value of each macro parameter in each conditional compilation expression in the source code file. Then, according to the defined configuration file, each conditional compilation expression can be matched in the configuration file, and the value of the parameter in each conditional compilation expression can be found in the configuration file.

[0038] According to the parameter value of each conditional compilation expression found in the configuration file, the parameter value of the conditional compilation expression at the same position in the copy file is modified in text form. For example, if FEATURE_C is configured as 0 in the configuration file, after the compiler processes the content of this part of the source file, FEATURE_C in the copy file is replaced with 0.

[0039] The present invention performs two different preprocessing operations on the source code file and the copy file, obtains the conditional compilation expression value after the user-defined configuration, and realizes configurable code cutting.

[0040] Step 3, based on all the conditional compilation information obtained from the source code file, combine the conditional compilation information at the same nesting level, construct a conditional compilation data unit, and write all the conditional compilation data units into the intermediate file.

[0041] It is understandable that step 1 extracts all conditional compilation information in the source code file through the compiler preprocessing module. Match and combine all the conditional compilation information to be processed, combine according to the conditional compilation information of the same nesting level, build a conditional compilation position information structure, that is, a conditional compilation data unit, and write all conditional compilation data units into the intermediate file, such as #if+#elif+#eles+#endif constitutes a combination of the same nesting level, and the internally nested #if+#else+#endif also constitutes a combination of the same nesting level, and each combination position information constitutes a conditional compilation data unit and is written into the intermediate file.

[0042] For example, the following code snippet:

[0043]

[0044] The code segments from line 1 to line 15 can constitute combination 1, the code segments from line 2 to line 6 can constitute combination 2, and the code segments from line 8 to line 12 can constitute combination 3. Then the above code segments can form 3 conditional compilation data units.

[0045] The present invention matches and processes the conditional compilation information of the compiler, integrates the conditional compilation combination information of the same nested level into a conditional compilation data unit, and calculates the code clipping range based on the data unit, which can better handle nested conditional compilation.

[0046] Step 4, read all conditional compilation data units from the intermediate file, obtain the truth value of each conditional compilation expression calculated by the compiler in the modified copy file, and obtain the valid code segment in each conditional compilation data unit according to the truth value of each conditional compilation expression.

[0047] It is understandable that all conditional compilation data units can be obtained from the intermediate file, and each conditional compilation data unit includes multiple conditional compilation information, such as conditional compilation expressions and line number positions.

[0048] Based on this information, the compiler calculates the true or false value of each conditional compilation expression according to the parameter value in each conditional compilation expression in the modified copy file in step 2. According to the true or false value of each conditional compilation expression in each conditional compilation data unit, the code segment corresponding to the true value is used as the valid code segment.

[0049] Among them, for the conditional compilation expression of the modified copy file, obtain the true and false values ​​of the conditional compilation expression calculated by the compiler, take the code segment where the true value is located as the valid code segment, and build a data structure containing the true and false values ​​of the expression, the valid code segment, and the identifier location information. For the code segment in step 3, if the value of #ifFEATURE_C is true, the valid code segment is 2-6 lines, and the identifier location information is line 1. The valid code segment of #else is line 14, and the identifier location information is line 13. Each conditional compilation data unit corresponds to a valid code segment.

[0050] Step 5: Determine the line number to be deleted in each conditional compilation data unit according to the valid code segment in each conditional compilation data unit.

[0051] It is understandable that step 4 finds the valid code segment of each conditional compilation data unit, and the code outside the valid code segment is the code to be deleted. The line number of the code to be deleted in each conditional compilation data unit is recorded as the line number to be deleted.

[0052] Each time the compiler preprocessing module triggers the #endif callback, the code clipping calculation containing the #endif nesting level combination is performed. The line number to be deleted of the conditional compilation data unit is calculated according to the conditional compilation data structure, such as the following code:

[0053] #if 0

[0054] #if1

[0055] / / Codeblock 1

[0056] #else

[0057] / / Codeblock2

[0058] #endif

[0059] #elif0||1

[0060] #if1

[0061] / / Codeblock 3

[0062] #else

[0063] / / Codeblock4

[0064] #endif

[0065] #else

[0066] / / Codeblock 5

[0067] #endif

[0068] The #endif in lines 6, 12, and 15 trigger the corresponding three nested combination code trimming calculations, where three valid code segments are obtained, namely, line 3, line 9, and lines 8 to 12 are valid code segments, and the rest are codes to be deleted. The line number of the code to be deleted in each nested combination code is recorded.

[0069] Step 6: based on the line number to be deleted in each conditional compilation data unit, the code of the corresponding line number in the source code file is trimmed to obtain the trimmed code.

[0070] It is understandable that the line number of the code to be deleted in each conditional compilation data unit is recorded in step 5. The line numbers to be deleted in each conditional compilation data unit are summarized and deduplicated; the deduplicated line numbers to be deleted are sorted, and the code of the corresponding line number in the source code file is deleted in ascending order to obtain the trimmed code.

[0071] The present invention makes a copy of the source code, first modifies the copy in a textual manner based on the compilation information of the source code, and then modifies the source code in a textual manner based on the compilation information of the copy, thereby indirectly realizing code tailoring in the compiler preprocessing stage.

[0072] See also Figure 2 , which is an overall flow chart of a configurable code tailoring method provided by the present invention. The method copies a source file, processes the two files (source file and copy file) twice, obtains data that can be tailored, and performs a code tailoring calculation with the same nested level conditional compilation as a unit. After the source file processing is completed, the data to be deleted is uniformly deduplicated and sorted, and finally the source code is modified in a text reading and writing manner.

[0073] The present invention provides a configurable code tailoring method, which uses a compiler preprocessing module to implement code tailoring. The method can give full play to the advantages of the compiler in understanding and processing conditional compilation, as well as the advantages of efficiently processing large code bases. The present invention constructs conditional compilation data units with conditional compilation combinations of the same level as units, which can better handle complex nested conditional compilation. At the same time, the present invention is a method for implementing code tailoring that integrates custom configuration information into the compilation process, which can more accurately and comprehensively implement flexible configuration of code tailoring.

[0074] It should be noted that in the above embodiments, the description of each embodiment has its own emphasis, and for parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0075] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0076] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded computer, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0077] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0078] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0079] Although the preferred embodiments of the present invention have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.

[0080] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.

Claims

1. A configurable code tailoring method, characterized in that: include: Step 1, copy a source code file to generate a copy file, and use a compiler preprocessing module to extract all conditional compilation information in the source code file, each conditional compilation information includes a conditional compilation expression and location information; Step 2, matching each conditional compilation expression according to the configuration file, and finding the macro and corresponding value configured for each conditional compilation expression from the configuration file; Modify the value of the conditional compilation expression at the same position in the copy file according to the configured macro and the corresponding value; Step 3, combining the conditional compilation information at the same nesting level according to all the conditional compilation information obtained in the source code file, constructing a conditional compilation data unit, and writing all the conditional compilation data units into an intermediate file; Step 4, read all conditional compilation data units from the intermediate file, obtain the truth value of each conditional compilation expression calculated by the compiler in the modified copy file, and obtain the valid code segment in each conditional compilation data unit according to the truth value of each conditional compilation expression; Step 5, determining the line number to be deleted in each conditional compilation data unit according to the valid code segment in each conditional compilation data unit; Step 6: based on the line number to be deleted in each conditional compilation data unit, the code of the corresponding line number in the source code file is trimmed to obtain the trimmed code.

2. The configurable code tailoring method according to claim 1, characterized in that: In step 1, a compiler preprocessing module is used to extract all conditional compilation information in the source code file, each conditional compilation information includes a conditional compilation expression and location information, including: The compiler finds each conditional compilation identifier in the source code file, and extracts the conditional compilation information corresponding to each conditional compilation identifier, wherein the conditional compilation information includes the expression content of the conditional compilation identifier and the line number of the conditional compilation identifier.

3. The configurable code tailoring method according to claim 1, characterized in that: The configuration file configures the value of each macro parameter in each conditional compilation expression in the source code file.

4. The configurable code tailoring method according to claim 1, characterized in that: In step 4, in the modified copy file, the truth value of each conditional compilation expression calculated by the compiler is obtained, and according to the truth value of each conditional compilation expression, the valid code segment in each conditional compilation data unit is obtained, including: According to the value of each conditional compilation expression in the modified copy file, the compiler calculates the true or false value of each conditional compilation expression; According to the truth value of each conditional compilation expression in each conditional compilation data unit, the code segment corresponding to the true value is taken as the valid code segment.

5. The configurable code tailoring method according to claim 1 or 4, characterized in that: The step 5, determining the line number to be deleted in each conditional compilation data unit according to the valid code segment in each conditional compilation data unit, includes: The codes other than the valid code segment are regarded as the codes to be deleted, and the line number of the codes to be deleted in each conditional compilation data unit is recorded as the line number to be deleted.

6. The configurable code tailoring method according to claim 1, characterized in that: The step 6, based on the line number to be deleted in each conditional compilation data unit, trimming the code of the corresponding line number in the source code file to obtain the trimmed code, includes: Summarize and remove duplicates of the line numbers to be deleted in each conditional compilation data unit; Sort the line numbers to be deleted after deduplication, and delete the codes of the corresponding line numbers in the source code file in ascending order to obtain the trimmed code.

Citation Information

Patent Citations

  • VxWorks operating system scaling and customizing method for aerospace computer

    CN102360305A

  • Java program precompiling method and precompiler

    CN104536797A

  • File-level cutting method and system for Linux kernel source code

    CN115437684A

  • QT dynamic library configuration method and device, electronic equipment and storage medium

    CN117971312A

  • Conditional compilation of intermediate language code based on current environment

    US20060101438A1

Cited By

  • Protocol processing method and device, electronic equipment and readable storage medium

    CN119396539A

  • Code writing method adaptive to multi-model air monitors

    CN119829075A

  • Code writing method for compatibility with multiple air quality monitor models

    CN119829075B