简体   繁体   English

Eclipse CDT:获得C ++项目的AST

[英]Eclipse CDT: Get AST of a C++ project

The ITranslationUnit and IASTTranslationUnit interfaces represent the translation unit and AST of a single C/C++ source file, respectively. ITranslationUnit和IASTTranslationUnit接口分别表示单个C / C ++源文件的转换单位和AST。

Is there any way to get the AST of an entire C++ project or do I need to start from the AST of the main file and navigate through the include directives and produce a separate AST for each source unit? 有什么方法可以获取整个C ++项目的AST,还是我需要从主文件的AST开始并浏览包含指令并为每个源单元生成单独的AST?

Thanks. 谢谢。

CDT's AST is not designed to scale to an entire project. CDT的AST并非旨在扩展到整个项目。 Once you start getting into the 10000+ LOC range, it's likely to start performing pretty badly. 一旦您开始进入10000+ LOC范围,它就可能开始表现很差。

For cross-file analysis purposes, CDT has an indexer , which parses each file in the project (one at a time), and builds a database of information about the code in the project as a whole (called the index ). 为了进行跨文件分析,CDT具有一个索引器 ,该索引器解析项目中的每个文件(一次解析一个文件),并构建一个有关整个项目中的代码信息的数据库(称为index )。 The index is accessed via the interface IIndex , an instance of which can be obtained (for example) by calling IASTTranslationUnit.getIndex() on any AST. 索引是通过接口IIndex访问的,该接口的实例可以通过在任何AST上调用IASTTranslationUnit.getIndex()来获取。

Most code analysis and manipulation use cases fall into one of the following workflows: 大多数代码分析和处理用例属于以下工作流程之一:

  • Just use the index. 只需使用索引。 IIndex gives you a lot to work with, such as: IIndex给您很多帮助,例如:

    • various overloads of findBindings() to find bindings matching a name or name prefix findBindings()各种重载以查找与名称或名称前缀匹配的绑定
    • findReferences(binding) to give you all references to a binding findReferences(binding)为您提供对绑定的所有引用
    • findDeclarations(binding) to give you all declarations of a binding findDeclarations(binding)为您提供绑定的所有声明


    and many others. 还有很多其他 This is how editor navigation features like Open Declaration and Call Hierarchy work. 这就是打开声明和呼叫层次结构之类的编辑器导航功能的工作方式。

  • Use the index to identify a small set of source files for which you need ASTs, and then parse those. 使用索引可以识别需要AST的少量源文件,然后对其进行解析。 This is how refactorings work. 重构就是这样工作的。 For example, the rename refactoring uses the index to locate uses of the binding being renamed, and then creates ASTs for the files containing those uses to perform the refactoring on. 例如,重命名重构使用索引来定位要重命名的绑定的用途,然后为包含那些用于执行重构的用途的文件创建AST。

  • If neither of the above is good enough and you really need AST-level information for every file in the project, create an AST for every file in the project, one at a time, and extract the information you need from each one. 如果以上两种方法都不够好,并且您确实需要项目中每个文件的AST级信息,请一次为项目中的每个文件创建一个AST,然后从每个文件中提取所需的信息。 This is how the indexer itself works. 这就是索引器本身的工作方式。 (Note that, if you choose this option, you don't need to navigate includes to list all the files you need to parse. Instead, you can just enumerate all the files in the project. See PDOMRebuildTask.createDelegate() for an example.) (请注意,如果选择此选项,则无需导航包含以列出需要解析的所有文件。相反,您可以枚举项目中的所有文件。有关示例,请参见PDOMRebuildTask.createDelegate() 。)

If you say more about what your use case is, I may be able to provide more specific suggestions. 如果您对用例有更多的了解,我也许可以提供更具体的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM