2013-11-27 5 views
1

http://eli.thegreenplace.net/2011/07/03/parsing-c-in-python-with-clang/ 에서 파이썬 바인딩의 제한을 보여주는 예제를 따르고 있습니다. "libclang visitation 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] 

내가 사용하는 버전은 윈도우와 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) 

많은 검색 및 시험 후에이 동작에 대한 이유를 얻을 수 없습니다. 누구든지 설명해 주시겠습니까? 감사합니다.

답변

0

나는이 이유를 발견했다고 생각합니다.

'foo'함수의 반환 유형을 'bool'에서 'int'로 변경하면 예상되는 결과가 나타납니다.

'bool'은 'c'의 키워드가 아니기 때문일 수 있습니다. 간단했습니다.