【发布时间】:2016-10-27 19:24:41
【问题描述】:
我需要解析一个 C++ 代码文件并找到其中所有具有完全限定名称的函数调用。我正在使用 libclang 的 Python 绑定,因为它似乎比编写我自己的 C++ 解析器更容易,即使文档很稀疏。
示例 C++ 代码:
namespace a {
namespace b {
class Thing {
public:
Thing();
void DoSomething();
int DoAnotherThing();
private:
int thisThing;
};
}
}
int main()
{
a::b::Thing *thing = new a::b::Thing();
thing->DoSomething();
return 0;
}
Python 脚本:
import clang.cindex
import sys
def find_function_calls(node):
if node.kind == clang.cindex.CursorKind.CALL_EXPR:
# What do I do here?
pass
for child in node.get_children():
find_function_calls(child)
index = clang.cindex.Index.create()
tu = index.parse(sys.argv[1])
find_function_calls(tu.cursor)
我正在寻找的输出是被调用函数的完全限定名称列表:
a::b::Thing::Thing
a::b::Thing::DoSomething
我可以使用node.spelling 获得函数的“短”名称,但我不知道如何找到它所属的类/命名空间。
【问题讨论】: