libclang解析多个文件时可以共享工作吗? [英] possible to share work when parsing multiple files with libclang?

查看:166
本文介绍了libclang解析多个文件时可以共享工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在一个大型项目中有多个文件,所有这些文件共享大量包含的头文件,是否有任何方法可以共享头文件的解析工作?我曾希望创建一个索引,然后向其中添加多个translationUnits可能会导致某些工作被共享-但是,即使是沿着(pseudocode)的代码进行编码,

If I have multiple files in a large project, all of which share a large number of included header files, is there any way to share the work of parsing the header files? I had hoped that creating one Index and then adding multiple translationUnits to it could cause some work to be shared - however even code along the lines of (pseudocode)

index = clang_createIndex();
clang_parseTranslationUnit(index, "myfile");
clang_parseTranslationUnit(index, "myfile");

似乎每次调用parseTranslationUnit都要花费全部时间,没有比这更好的了

seems to take the full amount of time for each call to parseTranslationUnit, performing no better than

index1 = clang_createIndex();
clang_parseTranslationUnit(index1, "myfile");
index2 = clang_createIndex();
clang_parseTranslationUnit(index2, "myfile");

我知道有一些专门的功能可以解析完全相同的文件;但是,我真正想要的是解析"myfile1"和"myfile2"可以共享解析"myheader.h"的工作,而特定于解析的功能对此无济于事.

I am aware that there are specialized functions for reparsing the exact same file; however what I really want is that parsing "myfile1" and "myfile2" can share the work of parsing "myheader.h", and reparsing-specific functions won't help there.

作为一个子问题,重用索引和为每个翻译单元创建新索引之间有有意义的区别吗?

As a sub-question, is there any meaningful difference between reusing an index and creating a new index for each translation unit?

推荐答案

一种方法是从项目中的共享标头创建预编译标头(PCH文件).

One way of doing this consists in creating Precompiled Headers (PCH file) from the shared header in your project.

遵循这些思路似乎可行(您可以在此处看到整个示例):

Something along these lines seems to work (you can see the whole example here):

  auto Idx = clang_createIndex (0, 0);
  CXTranslationUnit TU;
  Timer t;

  {
    char const *args[] = { "-xc++", "foo.hxx" };
    int nargs = 2;

    t.reset();
    TU = clang_parseTranslationUnit(Idx, 0, args, nargs, 0, 0, CXTranslationUnit_ForSerialization);
    std::cerr << "PCH parse time: " << t.get() << std::endl;
    displayDiagnostics (TU);
    clang_saveTranslationUnit (TU, "foo.pch", clang_defaultSaveOptions(TU));
    clang_disposeTranslationUnit (TU);
  }

  {
    char const *args[] = { "-include-pch", "foo.pch", "foo.cxx" };
    int nargs = 3;

    t.reset();
    TU = clang_createTranslationUnitFromSourceFile(Idx, 0, nargs, args, 0, 0);
    std::cerr << "foo.cxx parse time: " << t.get() << std::endl;
    displayDiagnostics (TU);
    clang_disposeTranslationUnit (TU);
  }

  {
    char const *args[] = { "-include-pch", "foo.pch", "foo2.cxx" };
    int nargs = 3;

    t.reset();
    TU = clang_createTranslationUnitFromSourceFile(Idx, 0, nargs, args, 0, 0);
    std::cerr << "foo2.cxx parse time: " << t.get() << std::endl;
    displayDiagnostics (TU);
    clang_disposeTranslationUnit (TU);
  }

产生以下输出:

PCH parse time: 5.35074
0 diagnostics

foo1.cxx parse time: 0.158232
0 diagnostics

foo2.cxx parse time: 0.143654
0 diagnostics

我在API文档中没有找到有关libclang和预编译头的很多信息,但是以下几页显示了关键字: TRANSLATION_UNIT

I did not find much information about libclang and precompiled headers in the API documentation, but here are a few pages where the keyword appears: CINDEX and TRANSLATION_UNIT

请注意,该解决方案无论如何都不是最佳选择.我期待看到更好的答案.特别是:

Please note that this solution is not optimal by any ways. I'm looking forward to seeing better answers. In particular:

  1. 每个源文件最多可以具有一个预编译头文件
  2. 这里没有什么是libclang特定的;这与使用标准clang命令行进行构建时间优化所使用的策略完全相同.
  3. 它并不是真正的自动化,因为您必须显式创建预编译的头文件(因此必须知道共享头文件的名称)
  4. 我认为在这里使用不同的CXIndex对象不会有什么不同
  1. each source file can have at most one precompiled header
  2. nothing here is libclang-specific ; this is the exact same strategy that is used for build time optimization using the standard clang command lines.
  3. it is not really automated, in that you have to explicitly create the precompiled header (and must thus know the name of the shared header file)
  4. I don't think using different CXIndex objects would have made any difference here

这篇关于libclang解析多个文件时可以共享工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆