libclang の Python binding を使用する 〜 Index 編〜
前回は Config に関する記事を書きました。
今回は Index 周りについて簡単に書きます。
libclang では Index を使用してソースコードをパースし、TranslationUnit の生成を行います。
[test.cpp]
struct X{ int value1 = 0; float value2 = 0.0f; }; int main(){ auto x = X{}; x.value1; return 0; }
[ソース]
# -*- coding: utf-8 -* import clang.cindex from clang.cindex import Index # index を生成 index = Index.create() # コマンドラインオプションは配列で定義する command_options = ["-std=c++1y", "-Wall"] # ソースコードのパースを行い TranslationUnit を生成する tu = index.parse("test.cpp", args = command_options) print tu.spelling # エラー内容の出力 severity = ['Ignored', 'Note', 'Warning', 'Error', 'Fatal'] for diag in tu.diagnostics: print severity[diag.severity] print diag.spelling
[出力]
test.cpp Error expected ';' after expression Warning expression result unused
上記は test.cpp をパースして TranslationUnit を生成し、その結果を出力しています。
パース時に Clang のコマンドラインオプションを設定しています。
この時に文字列ではなくて配列で1つずつオプションを定義する事に注意して下さい。
次は パース時の unsaved_files について。
[Clang]
- clang++ (LLVM) 3.3