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

2024-05-14 08:18:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个简单的工具来帮助重构应用程序的源代码。我想解析基于WxWIDGoS库的C++代码,它定义了GUI并生成了与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)

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

我找不到任何文档,除了一些博客文章,但他们是过时的或没有涵盖这个主题。我也不能从叮当的例子中找出答案。


Tags: 代码importnodeifvaluesyslocationfind

热门问题