libclang の Python binding を使用する 〜 diagnostics 編〜

前回は unsave_files に関する記事を書きました。
今回は diagnostics について簡単に書きます。
diagnostics はパースした結果のエラーや警告などを取得する事ができます。

[ソース]

import clang.cindex

# source code
source = """\
struct X{
    const int value = 10;
};

int
main(){
    X x{};
    x.value = 10;

    return
}
"""

index = clang.cindex.Index.create()

tu = index.parse("test.cpp", unsaved_files = [ ("test.cpp", source)])

severity  = ['Ignored', 'Note', 'Warning', 'Error', 'Fatal']
for diag in tu.diagnostics:
    print severity[diag.severity]
    print diag.spelling
    print ""

[出力]

Warning
in-class initialization of non-static data member is a C++11 extension

Error
expected ';' at end of declaration

Error
read-only variable is not assignable

Error
expected expression

Error
expected '}'


次回は location について書く予定です。

[Clang]

  • clang++ (LLVM) 3.3