I am writing a tool for analyzing C-family source code projects, mainly following these two tutorials 1 2 on clang 3.4 (trunk 192426) on ubuntu 12.04.
Based on the official guide , he says that I could pass compile_commands.json to -p , however, if I only type $ ./main -p [path of compile_commands.json] , he will complain about the lack of positional arguments. It seems like I still need to pass all the file names as arguments, which is impractical if the project is really huge. I prefer it to be able to simply parse all the files specified in compile_commands.json without a request, but cannot find out how to enable this.
Since I cannot find a tutorial for CommonOptionsParser to do any setup, I use CompilationDatabase . There is a dummy visitor returning true for VisitStmt , VisitDecl and VisitType , so I will skip this. The main function is pretty simple:
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; }
I choose opencv for parsing since using cmake gaurantee compile_commands.json is correct (right?). However, many errors appear (attached at the end). LibTooling complains that it cannot find stdarg.h , stddef.h and emmintrin.h . This is a FAQ for clang, but it says why this will happen, but did not say how to solve it when using libtooling. Pass all arguments to clang -### , since clang can solve this, but how do I pass these arguments when using 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>
==== Update ====
Read the source code of CommonOptionsParser.cpp. It uses FixedCompilationDatabase to guess CompilationDatabase by arguments after - and then pass arguments to - for custom (-p only in CommonOptionParser) options. In my case, compile_commands.json is required, so I can skip using CommonOptionsParser.
So my problem boils down to how to pass these parameters from "clang - ###" to LibTooling when I have compile_commands.json? Should I call a shell command for every file I want to parse?
==== Update ====
I think changing compile_commands.json might be easier. I'm not sure why the compile_commands.json generated by CMake will not correctly contain the system header file folder, since the Makefile generated by this CMakeList.txt can compile correctly, why compile_commands.json skips a lot of things.