小编war*_*ach的帖子

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

如果我在某些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获取范围或数据吗?

c++ python api parsing clang

12
推荐指数
2
解决办法
3279
查看次数

标签 统计

api ×1

c++ ×1

clang ×1

parsing ×1

python ×1