我正在编写一个简单的工具来帮助重构我们的应用程序的源代码.我想解析基于wxWidgets库的C代码,它定义了GUI并生成用于Qt的XML .ui文件.我需要获取所有函数调用和参数值.
目前我正在使用Python绑定到Clang,使用下面的示例代码我得到了令牌及其种类和位置,但是游标种类总是CursorKind.INVALID_file.
import sysimport clang.cindexdef 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.spellingfind_typerefs(tu.cursor)
确定光标种类的正确方法是什么?
除了很少的博客文章,我找不到任何文档,但它们已经过时或者没有涵盖这个主题.我无法从Clang附带的例子中解决这个问题.
@H_403_15@最佳答案@H_403_15@对于游标对象,只需使用cursor.kind即可.也许问题是你走的是令牌而不是子游标对象(不确定).您可以使用get_children来转向AST,而不是get_tokens.
为了了解AST的外观,当我想编写AST行走函数时,我使用这个脚本:https://gist.github.com/2503232.
这只是在我的系统上显示了cursor.kind,并给出了合理的输出.没有CursorKind.INVALID_file.@H_403_15@ 总结
以上是内存溢出为你收集整理的c – 如何使用Python绑定解析单个文件到Clang?全部内容,希望文章能够帮你解决c – 如何使用Python绑定解析单个文件到Clang?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)