如何使用CLANG作为解析器并使用Python作为脚本语言来解析C++代码中的宏?

war*_*ach 12 c++ python api parsing clang

如果我在某些C++代码中有以下宏:

_Foo(arg1, arg2)
Run Code Online (Sandbox Code Playgroud)

我想使用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)
Run Code Online (Sandbox Code Playgroud)

我认为我正在寻找的方法是解析原始AST的宏名称_FOO(),但我不确定.有人可以提供一些代码,允许我传入宏的名称并从Clang获取范围或数据吗?

thp*_*ani 9

您需要将适当的options标志传递给Index.parse:

tu = index.parse(sys.argv[1], options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD)
Run Code Online (Sandbox Code Playgroud)

游标访问者的其余部分可能如下所示:

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)
Run Code Online (Sandbox Code Playgroud)


Seb*_*ian 0

我曾经写过一个脚本来漂亮地打印从 libclang 获得的整个 AST,以便查看在哪里可以找到哪些信息。

这是: https: //gist.github.com/2503232