如何解析 C++ 代码中的宏,使用 CLANG 作为解析器,使用 Python 作为脚本语言? [英] How can I parse macros in C++ code, using CLANG as the parser and Python as the scripting language?

查看:52
本文介绍了如何解析 C++ 代码中的宏,使用 CLANG 作为解析器,使用 Python 作为脚本语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have the following macro in some C++ code:

_Foo(arg1, arg2)

我想使用 Python 使用 Clang 和 cindex.py 提供的 Python 绑定来查找该宏的所有实例和范围.我不想直接在代码上使用 Python 中的正则表达式,因为这让我达到了 99%,但不是 100%.在我看来,要达到 100%,您需要使用真正的 C++ 解析器(如 Clang)来处理所有情况,即人们做了语法正确并编译的愚蠢事情,但对正则表达式没有意义.我需要处理 100% 的情况,而且由于我们使用 Clang 作为我们的编译器之一,因此也可以将它用作此任务的解析器.

I would like to use Python to find me all the instances and extents of that macro using Clang and the Python bindings provided with cindex.py. I do not want to use a regular expression from Python on the code directly because that gets me 99% of the way there, but not 100%. It appears to me that to get to 100%, you need to use a real C++ parser like Clang to handle all the cases where people do silly things that are syntactically correct and compile, but don't make sense to a regular expression. I need to handle 100% of the cases and since we use Clang as one of our compilers, it makes sense to use it as the parser for this task as well.

给定以下 Python 代码,我能够找到看起来是 Clang python 绑定知道的预定义类型,但不是宏:

Given the following Python code I am able to find what appear to be predefined types that the Clang python bindings know about, but not macros:

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 取回范围或数据吗?

What I think I am looking for is a way to parse the raw AST for the name of my macro _FOO(), but I am not sure. Can someone provide some code that will allow me to pass in the name of a Macro and get back the extent or data from Clang?

推荐答案

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

You need to pass the appropriate options flag to Index.parse:

tu = index.parse(sys.argv[1], options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD)

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

The rest of the cursor visitor could look like this:

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)

这篇关于如何解析 C++ 代码中的宏,使用 CLANG 作为解析器,使用 Python 作为脚本语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆