title: Traversing the Clang AST using Python
body: <p>I am working on using clang bingings python to travers c/c++ code into AST,how can I get a tree based AST structure?
  Some pointers on where to start, tutorials or anything in this regard will be of great help!!!</p>
  
  <p>I found a very useful work(If you want to check this out ,here is the link:<a href="https://www.chess.com/blog/lockijazz/using-python-to-traverse-and-modify-clang-s-ast-tree" rel="nofollow noreferrer">https://www.chess.com/blog/lockijazz/using-python-to-traverse-and-modify-clang-s-ast-tree</a>) and tried his code,unfortunately I didn't get a useful output.</p>
  
  <pre><code>function_calls = []           
  function_declarations = []     
  def traverse(node):
  
      for child in node.get_children():
          traverse(child)
  
      if node.type == clang.cindex.CursorKind.CALL_EXPR:
          function_calls.append(node)
  
      if node.type == clang.cindex.CursorKind.FUNCTION_DECL:
          function_declarations.append(node)
  
  
      print 'Found %s [line=%s, col=%s]' % (node.displayname, node.location.line, node.location.column)
  
  clang.cindex.Config.set_library_path("/Users/tomgong/Desktop/build/lib")
  index = clang.cindex.Index.create()
  
  tu = index.parse(sys.argv[1])
  
  root = tu.cursor        
  traverse(root)
  </code></pre>