【发布时间】:2013-10-28 18:40:21
【问题描述】:
我正在编写一个工具来解析 C 系列源代码项目,基本上遵循这两个教程 1 2 on clang 3.4 (trunk 192426) on ubuntu 12.04。
基于offical tutorial,它说我可以通过-p 传递compile_commands.json,但是,如果我只输入$ ./main -p [path of compile_commands.json],它会抱怨缺少位置参数。似乎我仍然需要将所有文件名作为参数传递,如果项目真的很大,这是不切实际的。我更喜欢它可以简单地解析compile_commands.json 中指定的所有文件而不询问但无法找到如何打开它。
由于我找不到 CommonOptionsParser 的教程来做任何自定义的事情,我改用 CompilationDatabase 类。有一个虚拟访客返回true 为VisitStmt、VisitDecl 和VisitType,所以我将跳过它。 main 函数非常简单:
int main(int argc, const char **argv) {
string errorMsg = "";
CompilationDatabase *cd = CompilationDatabase::autoDetectFromDirectory (argv[1], errorMsg);
ClangTool Tool(*cd, cd->getAllFiles());
int result = Tool.run(newFrontendActionFactory<ExampleFrontendAction>());
return result;
}
我选择opencv来解析,因为使用cmake保证了compile_commands.json的正确性(对吗?)。但是,出现了很多错误(最后附上)。 LibTooling 抱怨找不到stdarg.h、stddef.h 和emmintrin.h。这是clang的FAQ,但它说明了为什么会发生这种情况,但没有说明如何在使用libtooling时解决这个问题。传递 clang -### 的所有参数,因为 clang 可以解决这个问题,但是如何在使用 libtooling 时传递这些参数?
# include <stdarg.h>
^
1 error generated.
Error while processing /home/jcwu/repos/opencv/3rdparty/openexr/IlmImf/ImfCompressionAttribute.cpp.
In file included from /home/jcwu/repos/opencv/3rdparty/libjpeg/jmemansi.c:16:
/home/jcwu/repos/opencv/3rdparty/libjpeg/jinclude.h:35:10: fatal error: 'stddef.h' file not found
#include <stddef.h>
^
1 error generated.
Error while processing /home/jcwu/repos/opencv/3rdparty/libjpeg/jmemansi.c.
error: no suitable precompiled header file found in directory '/home/jcwu/repos/opencv/modules/legacy/precomp.hpp.gch'
1 error generated.
Error while processing /home/jcwu/repos/opencv/modules/legacy/src/hmmobs.cpp.
In file included from /home/jcwu/repos/opencv/3rdparty/libwebp/enc/quant.c:17:
In file included from /home/jcwu/repos/opencv/3rdparty/libwebp/enc/../dsp/../enc/vp8enci.h:17:
/usr/include/string.h:34:10: fatal error: 'stddef.h' file not found
#include <stddef.h>
^
1 error generated.
Error while processing /home/jcwu/repos/opencv/3rdparty/libwebp/enc/quant.c.
In file included from /home/jcwu/repos/opencv/modules/imgproc/opencv_test_imgproc_pch_dephelp.cxx:1:
In file included from /home/jcwu/repos/opencv/modules/imgproc/test/test_precomp.hpp:12:
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../include/c++/4.6/iostream:40:
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../include/c++/4.6/ostream:40:
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../include/c++/4.6/ios:39:
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../include/c++/4.6/iosfwd:42:
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../include/c++/4.6/bits/postypes.h:42:
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../include/c++/4.6/cwchar:46:
/usr/include/wchar.h:40:11: fatal error: 'stdarg.h' file not found
# include <stdarg.h>
==== 更新 ====
阅读 CommonOptionsParser.cpp 源代码。它使用 FixedCompilationDatabase 通过 -- 之后的参数来猜测 CompilationDatabase,然后在 -- 之前传递参数以用于自定义(仅 CommonOptionParser 中的 -p)选项。在我的情况下 compile_commands.json 是必需的,所以我可以跳过使用 CommonOptionsParser。
因此,当我有 compile_commands.json 时,我的问题归结为如何将这些选项从“clang -###”传递给 LibTooling?我应该为每个要解析的文件调用 shell 命令吗?
==== 更新 ====
我认为修改 compile_commands.json 可能更容易。我不确定为什么 CMake 生成的 compile_commands.json 不会正确包含我的系统头文件文件夹,因为 CMakeList.txt 生成的 Makefile 可以正确编译,为什么 compile_commands.json 会错过很多东西。
【问题讨论】:
标签: clang