我正在编写一个工具来解析C系列源代码项目,基本上是在ubuntu 12.04上的clang 3.4(trunk 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;
}
Run Code Online (Sandbox Code Playgroud)
我选择opencv解析因为使用cmake gaurantee正确性compile_commands.json(对吗?).但是,出现了很多错误(附在最后).LibTooling抱怨它找不到stdarg.h,stddef.h也没有emmintrin.h.这是clang 的FAQ,但它说明了为什么会发生这种情况,但没有说明如何在使用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>
Run Code Online (Sandbox Code Playgroud)
====更新====
阅读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会错过很多东西.
我在使用python绑定时遇到了类似的问题.
[<Diagnostic severity 4, location <SourceLocation file '/usr/include/stdio.h', line 33, column 11>, spelling "'stddef.h' file not found">]
在"提示"部分
http://clang.llvm.org/docs/LibTooling.html
他们提到默认包含路径是
$(dirname /path/to/tool)/../lib/clang/3.3/include
Run Code Online (Sandbox Code Playgroud)
这里的想法似乎是,您的工具正在从包含clang可执行文件本身的bin目录执行.通常情况下,这将是一个系统目录,因此从该目录上升将包含包含clang/3.4/include目录的lib 目录.所以我手动包含$(which clang)../lib/clang/3.4/include在解析器中.在python中,这看起来像
translation_unit = index.parse("test.cc",["-I/home/archgoon/usr/local/lib/clang/3.4/include"])
Run Code Online (Sandbox Code Playgroud)
这导致了translation_unit.diagnostics一个空列表.
在我的情况下,我收到错误已安装clang-tidy但在Ubuntu上没有铿锵声.
有人回复我说编译数据库应该是独立的。首先,我需要确保使用 clang 生成compile_commands.json,并且我可以使用 clang 构建 opencv。
我设置了这些环境变量
export CC=/home/jcwu/repos/llvm-release/Release/bin/clang
export CXX=/home/jcwu/repos/llvm-release/Release/bin/clang++
export C_INCLUDE_PATH=/usr/local/include:/home/jcwu/repos/llvm-release/Release/lib/clang/3.4/include:/usr/include/x86_64-linux-gnu:/usr/include # these are from clang -v -c files.cpp
export CPLUS_INCLUDE_PATH=/usr/local/include:/home/jcwu/repos/llvm-release/Release/lib/clang/3.4/include:/usr/include/x86_64-linux-gnu:/usr/include
Run Code Online (Sandbox Code Playgroud)
然后重新生成compile_commands.json,它可以找到stddef.h,但出现新问题
[ 31%] Building CXX object modules/ts/CMakeFiles/opencv_ts.dir/src/ts.cpp.o
In file included from /home/jcwu/repos/opencv/modules/ts/src/ts.cpp:116:
/usr/include/setjmp.h:60:12: error: conflicting types for '__sigsetjmp'
extern int __sigsetjmp (struct __jmp_buf_tag __env[1], int __savemask) __THROWNL;
^
/usr/include/pthread.h:727:12: note: previous declaration is here
extern int __sigsetjmp (struct __jmp_buf_tag *__env, int __savemask) __THROW;
^
1 error generated.
make[2]: *** [modules/ts/CMakeFiles/opencv_ts.dir/src/ts.cpp.o] Error 1
make[1]: *** [modules/ts/CMakeFiles/opencv_ts.dir/all] Error 2
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)
由于类型冲突或两个系统头文件,我无法使用 clang 构建 opencv。还没想出如何解决这个问题。
| 归档时间: |
|
| 查看次数: |
6416 次 |
| 最近记录: |