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

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

问题描述

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

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

_Foo(arg1, arg2)

我想使用Python使用Clang和cindex提供的Python绑定来查找该宏的所有实例和范围。 py。我不想直接使用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)


$ b b

我认为我正在寻找的是一种解析原始的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?

推荐答案

您需要将相应的选项标记传递给 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天全站免登陆