我正在编写一个工具来解析 C 系列源代码项目,基本上遵循ubuntu 12.04 上的 clang 3.4(主干 192426)上的这两个教程1 2 。
根据官方教程,它说我可以通过compile_commands.json
,-p
但是,如果我只输入$ ./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 的常见问题解答,但它说明了为什么会发生这种情况,但没有说明在使用 libtooling 时如何解决这个问题。传递clang -###
for 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 会错过很多东西。