小编use*_*083的帖子

如何使用Python绑定解析单个文件到Clang?

我正在编写一个简单的工具来帮助重构我们的应用程序的源代码.我想解析基于wxWidgets库的C++代码,它定义了GUI并生成.ui用于Qt的XML 文件.我需要获取所有函数调用和参数值.

目前我正在使用Python绑定到Clang,使用下面的示例代码我得到了令牌及其种类和位置,但光标种类总是如此CursorKind.INVALID_FILE.

import sys
import clang.cindex

def find_typerefs(node):
    """ Find all references to the type named 'typename'
    """

    for t in node.get_tokens():
        if not node.location.file != sys.argv[1]:
            continue
        if t.kind.value != 0 and t.kind.value != 1 and t.kind.value != 4:
            print t.spelling
            print t.location
            print t.cursor.kind
            print t.kind
            print "\n"

index = clang.cindex.Index.create()
tu = index.parse(sys.argv[1])
print 'Translation unit:', tu.spelling
find_typerefs(tu.cursor)
Run Code Online (Sandbox Code Playgroud)

确定光标种类的正确方法是什么?

除了很少的博客文章,我找不到任何文档,但它们已经过时或者没有涵盖这个主题.我无法从Clang附带的例子中解决这个问题.

c++ python parsing clang

6
推荐指数
1
解决办法
4926
查看次数

标签 统计

c++ ×1

clang ×1

parsing ×1

python ×1