我正在关注一个显示 python 绑定限制的示例,来自站点http://eli.thegreenplace.net/2011/07/03/parsing-c-in-python-with-clang/ 它直接使用“libclang 访问 API” .
import sys
import clang.cindex
def callexpr_visitor(node, parent, userdata):
if node.kind == clang.cindex.CursorKind.CALL_EXPR:
print 'Found %s [line=%s, col=%s]' % (
node.spelling, node.location.line, node.location.column)
return 2 # means continue visiting recursively
index = clang.cindex.Index.create()
tu = index.parse(sys.argv[1])
clang.cindex.Cursor_visit(
tu.cursor,
clang.cindex.Cursor_visit_callback(callexpr_visitor),
None)
输出显示所有调用的函数及其行号。
Found foo [line=8, col=5]
Found foo [line=10, col=9]
Found bar [line=15, col=5]
Found foo [line=16, col=9]
Found bar [line=17, col=9]
当我运行相同的代码时,我只得到输出
Found bar [line=15, col=5]
我使用的版本是带有 windows 的 llvm3.1(链接中建议的更改)。
我觉得,返回 2 不是再次调用回调函数。我什至尝试在节点上使用'get_children'并在没有回调的情况下遍历,我得到了相同的结果。
import sys
import clang.cindex
#def callexpr_visitor(node, parent, userdata):
def callexpr_visitor(node):
if node.kind == clang.cindex.CursorKind.CALL_EXPR:
print 'Found %s [line=%s, col=%s]' % (
clang.cindex.Cursor_displayname(node), node.location.line, node.location.column)
for c in node.get_children():
callexpr_visitor(c)
#return 2 # means continue visiting recursively
index = clang.cindex.Index.create()
tu = index.parse(sys.argv[1])
#clang.cindex.Cursor_visit(
# tu.cursor,
# clang.cindex.Cursor_visit_callback(callexpr_visitor),
# None)
callexpr_visitor(tu.cursor)
经过大量搜索和试验,我无法得到这种行为的原因。谁能解释一下?问候。