14

基本的に、ubuntu 12.04 の clang 3.4 (trunk 192426) に関するこれらの 2 つのチュートリアル1 2に従って、C ファミリのソース コード プロジェクトを解析するツールを作成しています。

公式のチュートリアルに基づいて、私は通り過ぎることができると言っていますがcompile_commands.json-p入力しただけでは、$ ./main -p [path of compile_commands.json]位置引数が欠落していると不平を言います。すべてのファイル名を引数として渡す必要があるようですが、プロジェクトが本当に巨大な場合は実用的ではありません。指定されたすべてのファイルを確認せずに単純に解析できると思いますcompile_commands.jsonが、それを有効にする方法がわかりません。

CommonOptionsParserをカスタマイズするためのチュートリアルが見つからないため、代わりにCompilationDatabaseクラスを使用します。に戻るダミーの訪問者がいるtrueのでVisitStmt、スキップしますVisitDecl。関数は非常に単純ですVisitTypemain

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;
}

opencvcmakeを使用することで、compile_commands.json(正しい?)の正確性が保証されるため、解析することを選択しました。ただし、多くのエラーが表示されます(最後に添付)。stdarg.hLibTooling は、stddef.hも も見つからないと文句を言いますemmintrin.h。それはclangのFAQですが、なぜそれが起こるのかは述べていますが、libtoolingを使用しているときにそれを解決する方法は述べていません。for 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 にこれらのオプションを渡す方法に縮小されますか? 解析したいファイルごとにシェルコマンドを呼び出す必要がありますか?

====更新====

compile_commands.json を変更する方が簡単だと思います。その CMakeList.txt によって生成された Makefile は正しくコンパイルできるため、CMake によって生成された compile_commands.json にシステム ヘッダー ファイル フォルダーが正しく含まれない理由がわかりません。

4

5 に答える 5

9

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

ここでの考え方は、clang 実行可能ファイル自体も含む bin ディレクトリからツールが実行されていることが期待されるということです。通常、これはシステム ディレクトリであるため、そこから 1 つ上に行くと、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"])

これによりtranslation_unit.diagnostics、空のリストが作成されました。

于 2014-04-26T18:27:58.957 に答える
1

編集データベースは自己完結型であるべきだという返事がありました。最初に、compile_commands.json が clang を使用して生成されていることを確認する必要があります。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 

次に、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 

タイプの競合または 2 つのシステム ヘッダー ファイルが原因で、clang を使用して opencv をビルドできません。これを解決する方法がわかりませんでした。

于 2013-10-30T03:55:20.433 に答える