libclang の Python binding を使用する 〜 SourceLocation 編〜
前回は diagnostics に関する記事を書きました。
今回は SourceLocation について簡単に書きます。
SourceLocation はファイルの位置情報を保持するデータになります。
任意のファイルの位置の情報を取得する場合などに使用します。
前回使用した diagnostics もこの SourceLocation の情報を保持しています。
[ソース]
# -*- coding: utf-8 -* import clang.cindex from clang.cindex import Config Config.set_compatibility_check(False) Config.set_library_path("D:/LLVM/BUILD_3_3/bin") # 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)]) # SourceLocation の出力 def print_location(location): print "filename: %s" % location.file.name print "line : %s" % location.line print "column : %s" % location.column severity = ['Ignored', 'Note', 'Warning', 'Error', 'Fatal'] for diag in tu.diagnostics: print severity[diag.severity] print diag.spelling print_location(diag.location) print ""
[出力]
Warning in-class initialization of non-static data member is a C++11 extension filename: test.cpp line : 2 column : 18 Error expected ';' at end of declaration filename: test.cpp line : 7 column : 5 Error read-only variable is not assignable filename: test.cpp line : 8 column : 10 Error expected expression filename: test.cpp line : 11 column : 1 Error expected '}' filename: test.cpp line : 11 column : 2
次回は Cursor について書く予定です。
[Clang]
- clang++ (LLVM) 3.3