如果我在一些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)
我想我正在寻找的是一种方法来解析我的宏_FOO()的名称的原始AST,但我不确定.有人可以提供一些代码,让我以一个宏的名字传递,并从Clang中获取更多的数据或者数据?