libclang の Python binding を使用する 〜 Cursor 編〜
前回は SourceLocation に関する記事を書きました。
今回は Cursor について簡単に書きます。
Cursor は指定したファイル位置の情報を取得する事ができます。
[ソース]
# -*- coding: utf-8 -* import clang.cindex from clang.cindex import Cursor # source code source = """\ void func(int, float){ } int main(){ int value; value = 10; return; } """ index = clang.cindex.Index.create() tu = index.parse("test.cpp", unsaved_files = [ ("test.cpp", source)]) def print_cursor(file, line, col): # location を定義 location = tu.get_location(file, (line, col)) # location からその位置の cursor を取得 cursor = Cursor.from_location(tu, location) # カーソル位置の情報を出力 print cursor.displayname print cursor.spelling print cursor.kind print cursor.location print_cursor("test.cpp", 3, 2) print_cursor("test.cpp", 9, 2)
[出力]
func(int, float) func CursorKind.FUNCTION_DECL <SourceLocation file 'test.cpp', line 3, column 1> value value CursorKind.VAR_DECL <SourceLocation file 'test.cpp', line 9, column 6>
前回書いた SourceLocation から Cursor の生成を行います。
この Cursor を利用して任意の位置の様々な情報を取得します。
次回は型情報について書く予定です。
[Clang]
- clang++ (LLVM) 3.3