如何使用Python绑定到Clang来解析单个文件
How to parse single file using Python bindings to Clang?
我正在编写一个简单的工具,以帮助重构我们应用程序的源代码。我想基于WXWIDGETS库来解析C 代码,该库定义GUI并产生XML .ui
文件以与QT一起使用。我需要获取所有函数调用和参数的价值。
目前,我正在使用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)
确定光标类型的正确方法是什么?
我找不到任何文档,除了少数几篇博客文章,但是它们已经过时或没有涵盖此主题。我既不能从Clang随附的示例中解决这个问题。
对于光标对象,应该只使用光标。也许问题是您正在走上代币而不是儿童光标对象(对此不确定)。您可以使用get_children而不是get_tokens来行走AST。
为了查看AST的外观,当我想编写AST步行功能时,我使用此脚本:https://gist.github.com/2503232。这只是显示了光标,并在我的系统上提供明智的输出。无CursorKind.INVALID_FILE.
相关文章:
- 如何将"cout"、"cerr"、"stdout"和"stderr"从C++程序定向到单个文件
- C Visual Studio代码不使用工作区中的单个文件夹编译
- 从存储库读取单个文件时出现问题
- 获取单个文件的 Clang AST
- 无法在 Netbeans 中编译单个C++文件,但能够生成完整的项目
- g++在多个文件上比使用谷歌模拟的单片单个文件慢得多
- 在 netbeans 中编译项目中的单个文件
- 从许多并行可执行文件写入单个文件的最佳方法
- 覆盖单个文件的编译标志
- 多个内核写入单个文件
- 如何使用Python绑定到Clang来解析单个文件
- C++类,使用具有不同用户的单个文件
- 确定何时更改单个文件
- Qt 5.2.0 生成单个 .o 文件
- 如何在代码折叠之外的单个文件中管理长代码
- Cmake用于具有单个.h文件的库
- 如何检测单个文件的文件系统大小限制
- 分叉服务器,单个文件 - r/w 访问
- 接受单个文件或单个目录的 QFileDialog
- 单个文件代码中未定义的引用错误