如果我在某些C ++代码中有以下宏:
_Foo(arg1, arg2)
我想使用Python找到我使用Clang和cindex.py提供的Python绑定的宏的所有实例和范围。我不想直接在代码上使用Python的正则表达式,因为这样可以获得99%的方式,但不是100%。在我看来,要达到100%,你需要使用像Clang这样的真正的C ++解析器来处理人们做语法正确和编译的愚蠢事情的所有情况,但对正则表达式没有意义。我需要处理100%的情况,因为我们使用Clang作为我们的编译器之一,所以将它用作此任务的解析器也是有意义的。
鉴于以下Python代码,我能够找到Clang python绑定所知道的预定义类型,而不是宏:
def find_typerefs(node):
ref_node = clang.cindex.Cursor_ref(node)
if ref_node:
print 'Found %s Type %s DATA %s Extent %s [line=%s, col=%s]' % (
ref_node.spelling, ref_node.kind, node.data, node.extent, node.location.line, node.location.column)
# Recurse for children of this node
for c in node.get_children():
find_typerefs(c)
index = clang.cindex.Index.create()
tu = index.parse(sys.argv[1])
find_typerefs(tu.cursor)
我认为我正在寻找的是一种解析原始AST {宏} _FOO()
名称的方法,但我不确定。有人可以提供一些代码,允许我传入宏的名称并从Clang获取范围或数据吗?
答案 0 :(得分:9)
您需要将相应的options
标记传递给Index.parse
:
tu = index.parse(sys.argv[1], options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD)
光标访问者的其余部分可能如下所示:
def visit(node):
if node.kind in (clang.cindex.CursorKind.MACRO_INSTANTIATION, clang.cindex.CursorKind.MACRO_DEFINITION):
print 'Found %s Type %s DATA %s Extent %s [line=%s, col=%s]' % (node.displayname, node.kind, node.data, node.extent, node.location.line, node.location.column)
for c in node.get_children():
visit(c)
答案 1 :(得分:0)
我曾经写过一个脚本来重新绘制你从libclang获得的整个AST,以便查看在哪里可以找到哪些信息。