A method, device, and medium for constructing a C++ code knowledge base
By building detailed compilation configuration files and two-stage persistence strategies, the problem of incomplete information in the C++ code knowledge base is solved, efficient and flexible data management and query are achieved, and the evolution needs of large-scale projects are adapted.
Patent Information
- Application Number
- CN202510413991.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-03
- Publication Date
- 2025-07-25
- Estimated Expiration
- 2045-04-03
AI Technical Summary
Existing C++ code analysis tools are incomplete or misjudgment when dealing with complex syntax features, resulting in incomplete or inaccurate knowledge base information, and manual maintenance code documents are difficult to reflect the latest status of the code base in a timely manner, which is high maintenance cost and is prone to data redundancy or omissions.
By building detailed compilation configuration files, analyzing C++ project files, generating compilation units, obtaining dependency information and abstract syntax trees, performing node filtering and mapping storage to semantic mapping tables, realizing two-stage persistence processing, separating node information and relationship information, and supporting dynamic maintenance and querying.
It improves the accuracy and efficiency of C++ project processing, reduces computing and storage overhead, provides flexible query and update mechanisms, and supports the continuous expansion and dynamic maintenance of large-scale projects.
Smart Images

Figure CN119918641B_ABST
Abstract
Description
Technical Field
[0001] This specification relates to the technical field of knowledge graphs, and in particular, to a method, device, and medium for constructing a C++ code knowledge base. Background Art
[0002] As a high-performance programming language widely used in system-level and complex application development, the code structure of C++ is usually very large and complex, containing a large number of advanced syntax features such as template programming, macro definitions, and multiple inheritances. With the continuous expansion of the scale of software projects, the amount of code has increased exponentially. In order to help developers quickly understand the structure of existing code, quickly locate key code segments, and understand the relationships between codes, it is particularly important to build a comprehensive and intelligent C++ code knowledge base.
[0003] However, most current traditional code analysis tools mainly rely on regular expressions or simple text matching methods. When dealing with complex C++ syntax features, it is easy to have incomplete parsing or misjudgment, resulting in incomplete or inaccurate information in the constructed knowledge base. Moreover, most existing code documentation and knowledge management systems rely on manual maintenance and inefficient update mechanisms, making it difficult to reflect the latest state of the code library in a timely manner, and having high maintenance costs, easy to generate data redundancy or omissions when facing rapid iteration and large-scale code, and difficult to achieve high-quality and comprehensive data support. Summary of the Invention
[0004] To solve the above technical problems, one or more embodiments of this specification provide a method, device, and medium for constructing a C++ code knowledge base.
[0005] One or more embodiments of this specification adopt the following technical solutions:
[0006] One or more embodiments of this specification provide a method for constructing a C++ code knowledge base, the method including:
[0007] Construct a compilation configuration file containing detailed compilation parameters according to the C++ project configuration file of the C++ project source file;
[0008] Parse and obtain the file elements corresponding to the compilation configuration file based on a preset script parsing engine, and preprocess the C++ project source file to obtain a processed C++ project file, so as to generate a compilation unit corresponding to the C++ project file based on the file elements;
[0009] Obtain the dependency information of the C++ project file based on the compilation unit, and perform node filtering on the abstract syntax tree of the C++ project file based on the compilation unit, so as to map and store the filtered nodes and the dependency information into a corresponding semantic mapping table to implement the first persistent processing;
[0010] Determine the association relationships of the C++ project files based on each of the semantic mapping tables, store each of the association relationships into a preset relationship database, implement the second persistent processing, and use the preset relationship database as the C++ code knowledge base.
[0011] Optionally, in one or more embodiments of this specification, after implementing the second persistent processing and using the preset relationship database as the C++ code knowledge base, the method further includes:
[0012] Scan each file of the current C++ project file to generate the current feature values corresponding to each of the files according to the file content and location of each of the files;
[0013] Compare the current feature values with the file feature values stored in the semantic mapping table to obtain a comparison result; wherein, the semantic mapping table includes: a file table, a definition table, a structure table, and a symbol table;
[0014] If it is determined based on the comparison result that there is an update, obtain the corresponding updated feature value and determine the affected files corresponding to the updated feature value;
[0015] Based on the file table, obtain the dependency information corresponding to the affected files and establish a dependency tree of the affected files;
[0016] Identify the associated files related to the affected files according to the dependency tree, delete the content of the affected files and the associated files in the semantic mapping table, and perform a local knowledge base reconstruction on the affected files and the associated files based on the project configuration file corresponding to the updated feature value.
[0017] Optionally, in one or more embodiments of this specification, after implementing the second persistent processing and using the preset relationship database as the C++ code knowledge base, the method further includes:
[0018] Receive the current query request information, and query the relationship type and specific index corresponding to the current query request information in the preset relationship database based on a preset multi-dimensional query interface; wherein, the query request information includes: function name, class name, file name;
[0019] Query each of the semantic mapping tables based on the specific index to obtain the detailed data corresponding to the current query request information.
[0020] Optionally, in one or more embodiments of this specification, construct a compilation configuration file containing detailed compilation parameters according to the C++ project configuration file of the C++ project source file, specifically including:
[0021] Parse the C++ project configuration file of the C++ project source file to obtain the compilation parameters of the C++ project configuration file;
[0022] Complete the compilation parameters of the C++ project configuration file based on the preset default compilation parameters to obtain the detailed completed compilation parameters;
[0023] Store the detailed compilation parameters based on the preset format to obtain a compilation configuration file containing the detailed compilation parameters.
[0024] Optionally, in one or more embodiments of this specification, parse and obtain the file elements corresponding to the compilation configuration file based on a preset script parsing engine, and preprocess the C++ project source file to obtain a processed C++ project file, specifically including:
[0025] Read the compilation configuration file based on the preset script parsing engine to parse the compilation configuration file and obtain the initial file elements corresponding to the compilation configuration file; among them, each file element includes: working directory, compilation command, and compilation file;
[0026] Filter the compilation commands in the initial file elements to obtain the filtered compilation commands and file elements; among them, the filtered compilation commands have preprocessor options and language standard related commands;
[0027] Standardize the C++ project source file to convert the encoding of the C++ project source file into the same character encoding format, obtain the processed C++ project file, and cache the processed C++ project file in memory.
[0028] Optionally, in one or more embodiments of this specification, generate the compilation unit corresponding to the C++ project file based on the file elements, specifically including:
[0029] Use the working directory, compilation command, and compilation file as parsing parameters to construct a corresponding code parsing environment according to the parsing parameters;
[0030] Under the code parsing environment, call the syntax parsing library based on the preset script parsing engine to complement the syntax structure and context semantic information corresponding to the compilation file according to the same directory dependency information of the working directory and other directory dependency information of the compilation command;
[0031] Generate the compilation unit corresponding to the C++ project file based on the complemented syntax structure and context semantic information.
[0032] Optionally, in one or more embodiments of this specification, dependency information of the C++ project file is obtained based on the compilation unit, and node filtering is performed on the abstract syntax tree of the C++ project file obtained based on the compilation unit, so as to map and store the filtered nodes and the dependency information into a corresponding semantic mapping table to implement the first persistent processing, specifically including:
[0033] Determine whether the C++ project file contains a header file based on the compilation unit;
[0034] If so, extract the list of header files directly referenced by the C++ project file, and perform recursive analysis on the list of header files to complete the dependency information;
[0035] If not, obtain the abstract syntax tree of the C++ project file to traverse the abstract syntax tree to complete the dependency information;
[0036] Generate a unique feature code for each abstract syntax tree node, filter duplicate nodes based on the feature code, and map and store the completed dependency information and the filtered nodes into a corresponding semantic mapping table to implement the first persistent processing.
[0037] Optionally, in one or more embodiments of this specification, a unique feature code is generated for each abstract syntax tree node, duplicate nodes are filtered based on the feature code, and the completed dependency information and the filtered nodes are mapped and stored into a corresponding semantic mapping table to implement the first persistent processing, specifically including:
[0038] Perform a hash operation based on the constituent elements and file positions corresponding to each node in the abstract syntax tree to generate a unique feature code for each abstract syntax tree node;
[0039] Determine whether there are duplicate nodes in the abstract syntax tree nodes based on the feature code. If so, do not store the duplicate nodes;
[0040] If not, classify the abstract syntax tree nodes based on their types, and store the completed dependency information and the abstract syntax tree nodes into a corresponding semantic mapping table based on preset field rules.
[0041] One or more embodiments of this specification provide a device for constructing a C++ code knowledge base. The device includes:
[0042] At least one processor; and,
[0043] A memory communicatively connected to the at least one processor; wherein,
[0044] The memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to: execute any one of the above-mentioned methods.
[0045] A non-volatile computer storage medium provided by one or more embodiments of the present specification stores computer-executable instructions, and the computer-executable instructions are configured to: be capable of executing any one of the above-mentioned methods.
[0046] The above at least one technical solution adopted in the embodiments of the present specification can achieve the following beneficial effects:
[0047] By constructing a compilation configuration file with detailed compilation parameters using a C++ project configuration file, the project compilation environment information can be completely retained, parsing errors caused by lack of compilation parameters can be avoided, and the processing ability for complex C++ projects is enhanced. Based on the compilation unit, an abstract syntax tree is obtained and node filtering is performed, and in combination with the node feature code caching mechanism based on the abstract syntax tree, the problem of duplicate nodes caused by dependent files is effectively solved, the data processing efficiency is improved, and unnecessary computing and storage overheads are reduced. A two-stage persistence strategy is adopted. First, the filtered nodes and dependency information are mapped and stored in a semantic mapping table, and then the association relationships are stored in a pre-set relational database, separating the storage of node information and relationship information. This not only ensures data integrity but also provides a flexible query and update mechanism, supports the dynamic maintenance and continuous expansion of the knowledge base, and can meet the evolving needs of large-scale projects. Description of the Drawings
[0048] In order to more clearly illustrate the technical solutions in the embodiments of the present specification or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the drawings in the following description are only some embodiments recorded in the present specification. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings. In the drawings:
[0049] Figure 1 It is a schematic flowchart of a method for constructing a C++ code knowledge base provided by an embodiment of the present specification;
[0050] Figure 2 It is a schematic overall architecture diagram of a C++ code knowledge base provided by an embodiment of the present specification;
[0051] Figure 3 It is a schematic diagram of a set of semantic mapping tables of a C++ code knowledge base provided by an embodiment of the present specification;
[0052] Figure 4Schematic diagram of the query process of a C++ code knowledge base provided by an embodiment of this specification;
[0053] Figure 5 Schematic diagram of the query process of a C++ code knowledge base after reconstruction provided by an embodiment of this specification;
[0054] Figure 6 Schematic diagram of the structure of a device for constructing a C++ code knowledge base provided by an embodiment of this specification;
[0055] Figure 7 Schematic diagram of the structure of a non-volatile memory provided by an embodiment of this specification. Detailed implementation
[0056] An embodiment of this specification provides a method, device, and medium for constructing a C++ code knowledge base.
[0057] In order to enable those skilled in the art to better understand the technical solutions in this specification, the following will clearly and completely describe the technical solutions in the embodiments of this specification with reference to the accompanying drawings in the embodiments of this specification. Obviously, the described embodiments are only a part of the embodiments of this specification, rather than all the embodiments. Based on the embodiments of this specification, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of this specification.
[0058] As Figure 1 shown, an embodiment of this specification provides a schematic diagram of the method flow of a method for constructing a C++ code knowledge base. It can be Figure 1 seen that in one or more embodiments of this specification, a method for constructing a C++ code knowledge base includes the following steps:
[0059] S101: Construct a compilation configuration file containing detailed compilation parameters according to the C++ project configuration file of the C++ project source file.
[0060] When processing C++ code containing advanced syntax features such as a large number of template programming, macro definitions, and multiple inheritances, correct compilation parameters can enable the compiler to accurately understand the code structure and semantics. Therefore, in the embodiments of this specification, in order to ensure the accurate processing of complex C++ projects, a compilation configuration file containing detailed compilation parameters will be constructed according to the C++ project configuration file of the C++ project source file. Specifically, in one or more embodiments of this specification, constructing a compilation configuration file containing detailed compilation parameters according to the C++ project configuration file of the C++ project source file specifically includes:
[0061] In order to provide complex compilation parameters for subsequent steps in a C++ project, the C++ project configuration file of the C++ project source file is parsed to obtain the compilation parameters of the C++ project configuration file. Then, based on the default values of the preset compilation parameters, the compilation parameters of the C++ project configuration file are supplemented to obtain the detailed compilation parameters after supplementation. The detailed compilation parameters are stored based on the preset format to obtain a compilation configuration file containing the detailed compilation parameters. By parsing to obtain the compilation parameters and using the default values for supplementation, the integrity and accuracy of the compilation parameters can be ensured, reducing the compilation failure cases caused by missing or incorrect parameters, and improving the stability of the compilation process. In addition, the supplementation mechanism of the preset compilation parameter default values makes the project more adaptable when facing different development environments or configuration file differences. Even if there are missing parameters in the configuration file, the project can still be compiled based on the default values, reducing the risk of the project being unable to be compiled due to an incomplete configuration file, and facilitating the deployment and operation of the project in different environments. And storing the detailed compilation parameters in the preset format to generate a compilation configuration file makes the compilation configuration management more standardized and unified.
[0062] S102: Parse and obtain the file elements corresponding to the compilation configuration file based on the preset script parsing engine, and preprocess the C++ project source file to obtain the processed C++ project file, so as to generate the compilation unit corresponding to the C++ project file based on the file elements.
[0063] As Figure 2 shown, after the preprocessing is implemented based on step S101 to obtain a compilation configuration file containing detailed compilation parameters, the compilation configuration file is parsed through the preset script parsing engine to obtain the file elements therein, such as the working directory, compilation command, compilation file, etc. These elements are the basis for subsequent compilation and processing, and they specify information such as the working environment of the compiler, the compilation parameters used, and the source files to be compiled. Then, preprocessing the C++ project source file is to eliminate some special instructions and macro definitions in the code to make it easier for the compiler to understand. Then, based on the parsed file elements and the preprocessed C++ project file, the corresponding compilation unit is generated.
[0064] Specifically, in one or more embodiments of this specification, parsing and obtaining the file elements corresponding to the compilation configuration file based on the preset script parsing engine, and preprocessing the C++ project source file to obtain the processed C++ project file specifically include the following processes:
[0065] First, based on a preset script parsing engine, read the compilation configuration file to parse the compilation configuration file and obtain the initial file elements corresponding to the compilation configuration file. Among them, each file element includes: a working directory, a compilation command, and a compilation file. Then, the compilation commands in the initial file elements are filtered to obtain the filtered compilation commands and file elements. Among them, the filtered compilation commands have preprocessor options and language standard-related commands. That is, in a certain application scenario, a script parsing engine is used to read the compilation configuration file in the previous step, and then by parsing the compilation configuration file, each internal element is obtained. Each element contains three parts: a working directory, a compilation command, and a compilation file for subsequent use. Among them, for the compilation command, the preprocessor options and language standard-related commands are retained. Then, the C++ project source files are standardized to convert the encoding of the C++ project source files into the same character encoding format, obtaining the processed C++ project files, and the processed C++ project files are cached in memory. Based on this standardization process, it can be ensured that the abstract syntax tree (AST) can be normally parsed subsequently. In this process, by filtering the compilation commands and only retaining the preprocessor options and language standard-related commands, the compiler can focus more on the key compilation instructions and reduce unnecessary interference. And standardizing the C++ project source files and unifying the character encoding format can effectively solve the compatibility problems caused by different encoding formats. The filtered compilation commands and the source files after standardization provide a good foundation for subsequent operations such as abstract syntax tree parsing.
[0066] Specifically, in one or more embodiments of the present specification, generating a compilation unit corresponding to a C++ project file based on a file element specifically includes:
[0067] After the above preprocessing operations are completed, in the embodiments of the present specification, the working directory, compilation command, and compilation file of the file elements parsed and recognized above are used as parsing parameters, and then a corresponding code parsing environment is constructed according to the parsing parameters to ensure that the syntax structure and context information of the source code are completely retained. Then, in the code parsing environment, a syntax parsing library is called based on a preset script parsing engine, and according to the same-directory dependency information provided by the working directory and other directory dependency information of the compilation command, the syntax structure and context semantic information corresponding to the compilation file are complemented. According to the complemented syntax structure and context semantic information, a compilation unit corresponding to the C++ project file covering the complete syntax structure and context semantic information is generated.
[0068] In large-scale C++ projects, the dependencies between codes are complex, and accurate context information is crucial for correct compilation. In the above content of this specification, by constructing an accurate code parsing environment and complementing the syntax structure and context semantic information, the compiler can understand the source code more comprehensively and accurately. This helps to reduce compilation failures caused by syntax errors, unclear dependencies, etc., and improve the success rate of compilation.
[0069] S103: Obtain the dependency information of the C++ project file based on the compilation unit, and perform node filtering on the abstract syntax tree of the C++ project file obtained based on the compilation unit, so as to map and store the filtered nodes and the dependency information into the corresponding semantic mapping table to achieve the first persistent processing.
[0070] Different from the traditional single storage in the embodiments of this specification, the basic node information will be processed first based on the first persistent processing, and then the relationship information will be processed through the second persistent processing. This is convenient for separating and storing the node information and the relationship information, providing a flexible query and update mechanism while ensuring data integrity, supporting the dynamic maintenance and continuous expansion of the knowledge base, and meeting the evolution requirements of large-scale projects. Specifically, for the first persistent processing, in the embodiments of this specification, the dependency information of the C++ project file will be obtained according to the compilation unit, and node filtering will be performed on the abstract syntax tree of the C++ project file obtained based on the compilation unit, so as to map and store the filtered nodes and the dependency information into the corresponding semantic mapping table to achieve the first persistent processing. By performing node filtering on the abstract syntax tree, the filtering of duplicate nodes is achieved, and then: mapping and storing the filtered nodes and the dependency information can accurately reflect the dependency relationship between C++ project files.
[0071] Specifically, in one or more embodiments of this specification, obtaining the dependency information of the C++ project file based on the compilation unit, and performing node filtering on the abstract syntax tree of the C++ project file obtained based on the compilation unit, so as to map and store the filtered nodes and the dependency information into the corresponding semantic mapping table to achieve the first persistent processing, specifically includes the following processes:
[0072] First, based on the compilation unit, it is determined whether the C++ project file contains header files. If so, then the list of header files directly referenced by the C++ project file is extracted, and the list of header files is recursively analyzed to complete the supplementation of dependency information. If there are no header files, the abstract syntax tree of the C++ project file is obtained, and then the abstract syntax tree is traversed to complete the supplementation of the dependency information. And a unique feature code is generated for each abstract syntax tree node, and duplicate nodes are filtered based on the feature code, so as to map and store the supplemented dependency information and the filtered nodes into the corresponding semantic mapping table to achieve the first persistent processing. That is, since the compilation configuration file may not contain complete header file information, in a certain scenario, the header file dependency network can be supplemented and constructed in the following way: when processing the indirect dependencies of source files, record the involved header file information; analyze the indirect dependencies of each source file and extract the dependency relationships between header files; by traversing the dependency information of all source files, progressively construct a complete header file dependency index structure. In this process, the header file dependency network is supplemented and constructed by analyzing indirect dependencies, rather than relying only on the compilation configuration file, which not only captures the direct dependency relationships between source files, but also supplements and constructs a complete header file dependency index through indirect dependency analysis, overcoming the limitation of incomplete header file dependency analysis in traditional solutions.
[0073] Further, in one or more embodiments of the present specification, a unique feature code is generated for each abstract syntax tree node, and duplicate nodes are filtered based on the feature code, so as to map and store the supplemented dependency information and the filtered nodes into the corresponding semantic mapping table to achieve the first persistent processing, specifically including:
[0074] Perform a hash operation based on the constituent elements and file positions corresponding to each node in the abstract syntax tree to generate a unique feature code for each abstract syntax tree node. Then, determine whether there are duplicate nodes based on the feature code. If so, do not store the duplicate nodes. If not, classify the abstract syntax tree nodes based on their types, and store the supplemented dependency information and the abstract syntax tree nodes into the corresponding semantic mapping table as shown in Figure 3 That is, in a certain application scenario, the node feature codes will be calculated and cached for duplicate node recognition. Due to the information integration of dependent files, the same node may appear multiple times, and fast deduplication is achieved through the feature codes. Then, node filtering and screening are performed. First, filter nodes such as standard library references that can be directly processed, identify the defined type nodes and their location information, identify control structure nodes such as conditional statements and loop structures, and identify symbol reference nodes with associated relationships. Then, as shown in Figure 3 That is, the filtered nodes are respectively mapped and stored into a definition table, a structure table, and a symbol table according to the preset field rules.
[0075] S104: Determine the association relationships of the C++ project files based on each of the semantic mapping tables, store each of the association relationships in a preset relationship database, implement the second persistence process, and use the preset relationship database as the C++ code knowledge base.
[0076] In the embodiments of this specification, after implementing the first persistence process, in order to be able to separate and store node information and relationship information through a two-stage persistence strategy, while ensuring data integrity, a flexible query and update mechanism is provided, supporting the dynamic maintenance and continuous expansion of the knowledge base to meet the evolution requirements of large-scale projects. There will also be a second persistence process, that is, determine the association relationships of the C++ project files according to each semantic mapping table, so as to store each association relationship in a preset relationship database, implement the second persistence process, and use the preset relationship database as the C++ code knowledge base. That is, this step first reads the data stored in the base table before, and establishes data relationships: based on the data in the file table, record the dependency relationships between files; based on the data in the definition table, record the structures and symbols directly included in it; based on the data in the structure table, record the definitions and symbols directly included in it; based on the data in the symbol table, record the definitions, structures, and symbols it references. Then store all the association relationships established through the semantic mapping table set into the relationship database, and this relationship database is the final code knowledge base.
[0077] Further, in one or more embodiments of this specification, after implementing the second persistence process and using the preset relationship database as the C++ code knowledge base, in order to be able to solve the problems of too low update efficiency and too large data processing volume caused by the existing timestamp comparison or full-scale update, the embodiments of this specification will also perform updates based on the following steps:
[0078] First, scan each file of the current C++ project file, and generate the current feature value corresponding to each file according to the file content and location of each file. Then, compare the current feature value with the file feature value stored in the semantic mapping table to obtain a comparison result. Among them, it should be noted that the semantic mapping table includes: file table, definition table, structure table, and symbol table. And if it is determined that there is an update according to the comparison result, obtain the corresponding update feature value, and determine the affected files corresponding to the update feature value. Then obtain the dependency information corresponding to the affected files according to the file table, and establish a dependency tree of the affected files. Then identify the associated files related to the affected files according to the dependency tree, so as to delete the content of the affected files and the associated files in the semantic mapping table, and perform local knowledge base reconstruction on the affected files and the associated files based on the project configuration file corresponding to the update feature value.
[0079] That is, in a certain application scenario such as Figure 4As shown in the figure, for the process of only updating the affected files after code update: The system first scans all the files in the project, calculates and obtains the feature values of each file. This feature value is generated based on the file content and location during the knowledge base construction phase. Then, the system obtains the stored file feature values from the file table and compares them with the newly calculated feature values. If the feature values are the same, it indicates that the file has not changed, and the process ends directly; if the feature values are different, the subsequent update steps are continued; for the files with changed feature values, the following operations are performed: Obtain the direct / indirect dependency information through the file table; establish the dependency tree of the affected files; identify all the associated files that need to be updated; then perform a batch update on all the affected files found from the relationship table to delete the original information of these contents in the corresponding table; after completing the data deletion, perform a partial knowledge base reconstruction and re-execute the knowledge base construction process for the affected files.
[0080] In this process, by generating file feature values and comparing them with the semantic mapping table, it can accurately determine whether a file has been updated and which files have been affected. This method can precisely locate the source and scope of the update, avoiding unnecessary reprocessing of the entire project and improving the processing efficiency. The way of constructing the dependency tree clearly shows the dependency relationship between files, enabling all potentially affected files to be comprehensively considered when processing file updates. This helps ensure that no important associations are missed during the update process, thus guaranteeing the integrity and consistency of the project. Only the affected files and associated files are processed, rather than making large-scale modifications to the entire project. This local processing method reduces the complexity and time cost of processing, and also reduces the risk of introducing new errors. Based on the updated feature values and project configuration files, a partial knowledge base reconstruction can quickly restore the semantic information of the files and ensure the normal operation of the project. In addition, the use of the semantic mapping table helps to centrally manage various semantic information of project files, including file definitions, structures, and symbol references, etc. When the file is updated, the outdated information is deleted in a timely manner and rebuilt, ensuring that the semantic mapping table always reflects the latest state of the project.
[0081] Furthermore, in one or more embodiments of this specification, after implementing the second persistent processing and using the preset relational database as the C++ code knowledge base, in order to be able to query the phase shift code information based on requirements, as Figure 5 The method shown also includes the following process:
[0082] First, receive the current query request information, and then query the relation type and specific index corresponding to the current query request information in the preset relational database based on the preset multi-dimensional query interface; wherein, the query request information includes: function name, class name, and file name. Then, query each semantic mapping table based on the specific index to obtain the detailed data corresponding to the current query request information. For example, the following query functions can be implemented, including but not limited to:
[0083] When the input file name is queried:
[0084] (1)Direct and indirect dependencies between files:
[0085] ->QUERY(DIRECT_INCLUDES|INDIRECT_INCLUDES) FROM file table WHERE full_path = "target file path"
[0086] (2)Definition information included in the file:
[0087] ->QUERY(REFEREE_ID|REFEREE_TYPE) FROM relation table WHERE REFERRER_ID IN(SELECT id FROM file table WHERE full_path = "target file path")
[0088] When the input function name is queried:
[0089] (1)Complete definition information of the function:
[0090] ->QUERY(FULL_PATH|POSITION|KIND|TYPE) FROM definition table WHERE kind = "FUNCTION_DECL" AND spelling = "target function name"
[0091] (2)Call relationship of the function:
[0092] ->QUERY(REFERRER_ID|REFEREE_ID|RELATION) FROM relation table WHERE referee_kind = "FUNCTION_DECL" AND relation = "CALLS" AND referee_id IN (SELECT idFROM definition table WHERE spelling = "target function name")
[0093] (3)Class to which the function belongs:
[0094] ->QUERY(REFERRER_ID|REFERRER_KIND) FROM the relationship table WHERE referee_id IN(SELECT id FROM the definition table WHERE spelling = "target function name") AND relation = "MEMBER_OF"
[0095] (4)Function overloading information:
[0096] ->QUERY(FULL_PATH|POSITION|ARGUMENTS) FROM the definition table WHERE kind = "FUNCTION" AND spelling = "target function name"
[0097] The input class name can be queried:
[0098] (1)Complete definition and inheritance of the class:
[0099] ->QUERY(FULL_PATH|POSITION|RELATION) FROM the definition table, relationship table WHERE kind ="CLASS_DECL" AND spelling = "target class name" AND relation IN ("INHERITS", "INHERITED_BY")
[0100] (2)List of class members:
[0101] ->QUERY(REFEREE_ID|REFEREE_KIND) FROM the relationship table WHERE referrer_id IN(SELECT id FROM the definition table WHERE spelling = "target class name") AND relation = "HAS_MEMBER"
[0102] (3)Usage location of the class:
[0103] ->QUERY(REFERRER_ID|POSITION) FROM the relationship table WHERE referee_id IN (SELECTid FROM the definition table WHERE spelling = "target class name" AND kind = "CLASS_DECL")
[0104] The input variable name can be queried:
[0105] (1)Definition and type of the variable:
[0106] ->QUERY(FULL_PATH|POSITION|KIND|TYPE) FROM symbol table WHERE spelling = "target variable name" AND kind = "TYPE_REF|MEMBER_REF_EXPR|CALL_EXPR"
[0107] (2)Usage location of the variable:
[0108] ->QUERY(FULL_PATH|POSITION|REF_KIND) FROM symbol table WHERE spelling = "target variable name"
[0109] (3)Belonging structure:
[0110] ->QUERY(REFERRER_ID|REFERRER_KIND) FROM relationship table WHERE referee_id IN(SELECT id FROM definition table WHERE spelling = "target variable name") AND relation = "CONTAINED_BY"
[0111] Query for input structure related content:
[0112] (1)Structure scope and content:
[0113] ->QUERY(FULL_PATH|POSITION) FROM structure table WHERE full_path= "directory where the structure is located"
[0114] (2)Symbol references within the structure:
[0115] ->QUERY(REFEREE_ID|REFEREE_KIND) FROM relationship table WHERE referee_kind IN(IF_STMT|WHILE_STMT|FOR_STMT|DO_STMT) AND relation = "CONTAINS"
[0116] (3)Structure nesting relationship:
[0117] ->QUERY(REFERRER_ID|REFEREE_ID) FROM relationship table WHERE relation = "CONTAINS" AND referee_kind IN (IF_STMT|WHILE_STMT|FOR_STMT|DO_STMT) AND referrer_kind IN (IF_STMT|WHILE_STMT|FOR_STMT|DO_STMT)
[0118] Combined query:
[0119] (1)Function-to-file dependency:
[0120] ->QUERY(DIRECT_INCLUDES|INDIRECT_INCLUDES) FROM file table WHERE id IN(SELECT referrer_id FROM relationship table WHERE referee_id IN (SELECT id FROM definition table WHERE spelling = "target function name"))
[0121] (2)Class-to-structure inclusion:
[0122] ->QUERY(REFEREE_ID|RELATION) FROM relationship table WHERE referrer_id IN (SELECT id FROM definition table WHERE spelling = "target class name") AND relation = "CONTAINS"
[0123] (3)Dependency impact analysis:
[0124] ->QUERY(REFERRER_ID|REFEREE_ID|RELATION) FROM relationship table WHERE referee_id IN (SELECT id FROM definition table WHERE spelling = "target name") AND relation IN ("CALLS")
[0125] In the embodiments of this specification, the incremental update solution based on the above process significantly reduces the computational overhead of knowledge base maintenance. For large C++ projects, the update time can be shortened from the hour level of complete reconstruction to the minute level, while ensuring data consistency and integrity. Moreover, by uniformly managing the associations between various types of information through a relationship table, developers can quickly locate all relevant information from any code element, significantly improving the code understanding and maintenance efficiency. Specifically, the parsing mechanism based on the compilation configuration file ensures the complete retention of the compilation environment information of the project, solves the parsing errors caused by the lack of compilation parameters in traditional tools, and improves the processing ability for complex C++ projects. The progressive dependency network construction method not only captures the direct dependency relationships between source files, but also supplements and constructs a complete header file dependency index through indirect dependency analysis, overcoming the limitation of incomplete header file dependency analysis in traditional solutions. The node feature code caching mechanism based on the abstract syntax tree effectively solves the problem of duplicate nodes caused by dependent files, improves the data processing efficiency, and reduces the storage overhead. And based on the multi-dimensional association system of location information and unique identifiers, the precise association between code elements such as definitions, references, and control structures is realized, providing a reliable data basis for code analysis and intelligent applications. Through the two-stage persistence strategy, the node information and relationship information are stored separately, providing a flexible query and update mechanism while ensuring data integrity, supporting the dynamic maintenance and continuous expansion of the knowledge base, and meeting the evolution requirements of large-scale projects. The precise update positioning mechanism based on eigenvalue comparison and dependency tree analysis avoids the redundant processing of unmodified files in traditional solutions and is particularly suitable for development environments with frequent iterations. The centralized query design based on the relationship table, through a unified relationship index mechanism, enables the system to accurately track the evolution of associations between code elements, thereby supporting advanced application scenarios such as incremental updates and change impact analysis, and significantly reducing the maintenance cost and reconstruction risk of large projects.
[0126] As Figure 6 shown, the embodiments of this specification provide a schematic structural diagram of a device for constructing a C++ code knowledge base, which consists of Figure 6 It can be seen that in one or more embodiments of this specification, a device for constructing a C++ code knowledge base includes:
[0127] At least one processor; and,
[0128] A memory communicatively connected to the at least one processor; wherein,
[0129] The memory stores instructions executable by the at least one processor, and when the instructions are executed by the at least one processor, the at least one processor is enabled to: execute any one of the above methods.
[0130] As Figure 7As shown, the embodiments of this specification provide a schematic structural diagram of a non-volatile storage medium, which consists of Figure 7 It can be seen that in one or more embodiments of this specification, a non-volatile storage medium stores computer-executable instructions 701, and the computer-executable instructions 701 can: execute any of the above-mentioned methods.
[0131] The embodiments in this specification are all described in a progressive manner. For the same or similar parts among the embodiments, reference can be made to each other. Each embodiment focuses on the differences from other embodiments. In particular, for the embodiments of the device, equipment, and non-volatile computer storage medium, since they are basically similar to the method embodiments, the description is relatively simple, and reference can be made to the relevant parts of the method embodiments for the relevant parts.
[0132] The above describes specific embodiments of this specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in a different order than in the embodiments and still achieve the desired result. Additionally, the processes depicted in the figures do not necessarily require the particular order or sequential order shown to achieve the desired result. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0133] The above is only one or more embodiments of this specification and is not intended to limit this specification. For those skilled in the art, there can be various changes and modifications to one or more embodiments of this specification. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of one or more embodiments of this specification shall be included within the scope of the claims of this specification.
Claims
1. A method for constructing a C++ code knowledge base, characterized in that The method includes: Construct a compilation configuration file containing detailed compilation parameters according to the C++ project configuration file of the C++ project source file; Based on a preset script parsing engine, parse to obtain the file elements corresponding to the compilation configuration file, and preprocess the C++ project source file to obtain a processed C++ project file, so as to generate a compilation unit corresponding to the C++ project file based on the file elements; Based on the compilation unit, obtain the dependency information of the C++ project file, and perform node filtering on the abstract syntax tree of the C++ project file obtained based on the compilation unit, so as to map and store the filtered nodes and the dependency information into a corresponding semantic mapping table to implement the first persistent processing; Based on each semantic mapping table, determine the association relationships of the C++ project file, and store each association relationship into a preset relational database to implement the second persistent processing and use the preset relational database as a C++ code knowledge base; Based on a preset script parsing engine, parse to obtain the file elements corresponding to the compilation configuration file, and preprocess the C++ project source file to obtain a processed C++ project file. Specifically, it includes: Based on a preset script parsing engine, read the compilation configuration file to parse the compilation configuration file to obtain the initial file elements corresponding to the compilation configuration file; wherein, each file element includes: working directory, compilation command and compilation file; Filter the compilation commands in the initial file elements to obtain the filtered compilation commands and file elements; wherein, the filtered compilation commands have preprocessor options and language standard related commands; Perform standardization processing on the C++ project source file to convert the encoding of the C++ project source file into the same character encoding format, obtain a processed C++ project file, and cache the processed C++ project file in memory; Based on the compilation unit, obtain the dependency information of the C++ project file, and perform node filtering on the abstract syntax tree of the C++ project file obtained based on the compilation unit, so as to map and store the filtered nodes and the dependency information into a corresponding semantic mapping table to implement the first persistent processing. Specifically, it includes: Based on the compilation unit, determine whether the C++ project file contains a header file; If so, extract the list of header files directly referenced by the C++ project file, and perform recursive analysis on the list of header files to complete the dependency information; If not, obtain the abstract syntax tree of the C++ project file, and traverse the abstract syntax tree to complete the dependency information; Generate a unique feature code for each abstract syntax tree node, filter the duplicate nodes based on the feature code, and map and store the completed dependency information and the filtered nodes into a corresponding semantic mapping table to implement the first persistent processing.
2. The construction method of a C++ code knowledge base according to claim 1, characterized in that, After implementing the second persistent processing and using the preset relational database as a C++ code knowledge base, the method further includes: Scan each file of the current C++ project file to generate the current feature value corresponding to each file according to the file content and location of each file; Compare the current feature value with the file feature values stored in the semantic mapping table to obtain a comparison result; wherein, the semantic mapping table includes: a file table, a definition table, a structure table, and a symbol table; If it is determined based on the comparison result that there is an update, obtain the corresponding update feature value and determine the affected files corresponding to the update feature value; Obtain the dependency information corresponding to the affected files based on the file table and establish a dependency tree for the affected files; Identify the associated files related to the affected files according to the dependency tree, delete the content of the affected files and the associated files in the semantic mapping table, and perform local knowledge base reconstruction on the affected files and the associated files based on the project configuration file corresponding to the update feature value.
3. The construction method of a C++ code knowledge base according to claim 1, characterized in that After implementing the second persistent processing and using the preset relational database as the C++ code knowledge base, the method further includes: Receive the current query request information to query the relational type and specific index corresponding to the current query request information in the preset relational database based on the preset multi-dimensional query interface; wherein, the query request information includes: function name, class name, file name; Query each semantic mapping table based on the specific index to obtain the detailed data corresponding to the current query request information.
4. The construction method of a C++ code knowledge base according to claim 1, characterized in that, Construct a compilation configuration file containing detailed compilation parameters according to the C++ project configuration file of the C++ project source file, specifically including: Parse the C++ project configuration file of the C++ project source file to obtain the compilation parameters of the C++ project configuration file; Complete the compilation parameters of the C++ project configuration file based on the preset compilation parameter default values to obtain the completed detailed compilation parameters; Store the detailed compilation parameters based on the preset format to obtain a compilation configuration file containing detailed compilation parameters.
5. A method for constructing a C++ code knowledge base according to claim 1, characterized in that Generate the compilation unit corresponding to the C++ project file based on the file elements, specifically including: Use the working directory, compilation command, and compilation file as parsing parameters to construct a corresponding code parsing environment according to the parsing parameters; Under the code parsing environment, call the syntax parsing library based on the preset script parsing engine to complete the syntax structure and context semantic information corresponding to the compilation file according to the same-directory dependency information of the working directory and the other-directory dependency information of the compilation command; Generate the compilation unit corresponding to the C++ project file based on the completed syntax structure and context semantic information.
6. The construction method of a C++ code knowledge base according to claim 1, characterized in that, Generate a unique feature code for each abstract syntax tree node, filter the duplicate nodes based on the feature code, and map and store the completed dependency information and the filtered nodes into the corresponding semantic mapping tables to implement the first persistent processing, specifically including: Perform a hash operation based on the constituent elements and file locations corresponding to each node in the abstract syntax tree to generate the unique feature code for each abstract syntax tree node; Determine whether there are duplicate nodes in the abstract syntax tree node based on the feature code. If so, do not store the duplicate nodes; If not, classify the abstract syntax tree nodes based on their types, and store the complemented dependency information and the abstract syntax tree nodes into the corresponding semantic mapping table based on the preset field rules.
7. An apparatus for constructing a C++ code knowledge base, characterized in that, The device includes: At least one processor; and, A memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can: execute the method according to any one of claims 1-6 above.
8. A non-volatile storage medium stores computer-executable instructions, characterized in that, The computer-executable instructions can: execute the method according to any one of claims 1-6 above.
Citation Information
Patent Citations
Knowledge graph construction method and device, electronic equipment and readable storage medium
CN111914098A
Code conversion method and device for pre-compiler, medium, and equipment
CN114035805A