如何使用PCH或PTH提高clang解析性能

How to increase clang parsing performance using PCH or PTH?

本文关键字:clang 性能 提高 PTH 何使用 PCH      更新时间:2023-10-16

我使用Clang C API进行解析和诊断。我注意到我一直在使用相同的头束,所以我决定尝试使用PCH或PTH来提高性能。

但我得到的诊断是,在clang_parseTranslationUnit:时既没有使用PCH也没有使用PTH

  • 编译过程中未使用的参数:'-emit pth'
  • /tmp/system/include/libcxx/iostream.pth:"链接器"输入未使用
  • 编译期间未使用的参数:'-emit pch'
  • /tmp/system/include/libcxx/iostream.pch:"链接器"输入未使用

我很惊讶在解析时没有使用PCH和PTH,那么我如何使用PCH或PTH或任何其他方法来提高解析性能呢?

PS。

PTH是用clang++ -x c++-header iostream -emit-pth -o iostream.pth、PCH以类似的方式生成的

更新1:

我发现PCH的使用示例:

// This will load all the symbols from 'IndexTest.c', excluding symbols
// from 'IndexTest.pch'.
char *args[] = { "-Xclang", "-include-pch=IndexTest.pch" };
TU = clang_createTranslationUnitFromSourceFile(Idx, "IndexTest.c", 2, args, 0, 0);

问题是,现在我得到了另一个诊断警告:

'-pch=/system/include/libcxx/iostream.pch' file not found

命令行参数的分析似乎不正确。。我做错了什么?

需要在每个参数之前包含-Xclang

char *args[] = { "-Xclang", "-include-pch", "-Xclang", "IndexTest.pch" };

请参阅https://reviews.llvm.org/diffusion/L/browse/cfe/trunk/test/Index/c-index-pch.c寻找灵感。