ast_dump.py
import sys import clang.cindex def traverse(n, i=0): print(' ' * i, end="") print(n.kind, end="") print(" : ", end="") print(n.spelling, end="") print("") for c in n.get_children(): traverse(c, i=i+1) def main(afile): #clang.cindex.Config.set_library_file('C:/Program Files (x86)/LLVM/bin/libclang.dll') index = clang.cindex.Index.create() translation_unit = index.parse(afile, ['-x', 'c++']) traverse(translation_unit.cursor) if __name__ == '__main__': main(sys.argv[1])
sample2.cpp
#include "stdio.h" class myclass { public: void mymethod() { printf("method\n"); } }; void testfunc() { myclass var; var.mymethod(); }
CursorKind.CLASS_DECL : myclass
CursorKind.CXX_ACCESS_SPEC_DECL :
CursorKind.CXX_METHOD : mymethod
CursorKind.COMPOUND_STMT :
CursorKind.CALL_EXPR : printf
CursorKind.UNEXPOSED_EXPR : printf
CursorKind.DECL_REF_EXPR : printf
CursorKind.UNEXPOSED_EXPR :
CursorKind.STRING_LITERAL : "method\n"
CursorKind.FUNCTION_DECL : testfunc
CursorKind.COMPOUND_STMT :
CursorKind.DECL_STMT :
CursorKind.VAR_DECL : var
CursorKind.TYPE_REF : class myclass
CursorKind.CALL_EXPR : myclass
CursorKind.CALL_EXPR : mymethod
CursorKind.MEMBER_REF_EXPR : mymethod
CursorKind.DECL_REF_EXPR : var