I'm learning to parse some C++ source file with python-libclang
, so far I have a script that can partially achieve what I hope:
import clang.cindex
idx = clang.cindex.Index.create()
tu = idx.parse('example.cpp', args='-xc++ --std=c++11'.split())
for cursor in tu.cursor.walk_preorder():
print("`{}` --> {}".format(cursor.spelling, cursor.kind))
This script can parse a example.cpp
// example.cpp
// I know this is incomplete but this is all I got: some incomplete files
int main()
{
Thing *thing = new Thing();
thing->DoSomething();
return 0;
}
and print out:
`example.cpp` --> CursorKind.TRANSLATION_UNIT
`main` --> CursorKind.FUNCTION_DECL
`` --> CursorKind.COMPOUND_STMT
`` --> CursorKind.DECL_STMT
`thing` --> CursorKind.VAR_DECL
`` --> CursorKind.RETURN_STMT
`` --> CursorKind.INTEGER_LITERAL
Yet there is no information about function DoSomething()
. How do I locate all function calls (and hopefully their fully-qualified names) from a source file?
E.g. how do I get a Thing::DoSomething
, or at least a DoSomething
?
I have read and tried examples from
- How to retrieve class names and its method names from C++ header using python
- How can I retrieve fully-qualified function names with libclang?
- https://github.com/llvm-mirror/clang/blob/master/bindings/python/examples/cindex/cindex-dump.py
- https://github.com/llvm-mirror/clang/blob/master/bindings/python/examples/cindex/cindex-includes.py
but none of them get what I want.
Recommendation to any other SIMPLE tools to parse C++ source files is welcomed.