6

I am writing a simple tool to help with refactoring the source code of our application. I would like to parse C++ code based on wxWidgets library, which defines GUI and produce XML .ui file to use with Qt. I need to get all function calls and value of arguments.

Currently I am toying with Python bindings to Clang, using the example code below I get the tokens and their kind and location, but the cursor kind is always 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)

What is the correct way to determine the cursor kind?

I couldn't find any documentation except few blog posts, but they were outdated or not covering this topic. I was neither unable to work it out from examples that came with Clang .

4

1 回答 1

5

对于游标对象,应该可以只使用 cursor.kind。也许问题是您正在行走令牌而不是子光标对象(不确定)。您可以使用 get_children 代替 get_tokens 来遍历 AST。

为了看看 AST 的样子,当我想写一个 AST 步行函数时,我使用这个脚本:https ://gist.github.com/2503232 。这只是在我的系统上显示 cursor.kind 并提供合理的输出。不CursorKind.INVALID_FILE.

于 2013-10-22T22:05:49.803 回答