[cfe-users] libclang: traversing initializers in for-range statements

Дмитрий Ермолов epdmitry at yandex.ru
Sun Feb 15 13:40:31 PST 2015


Hi,

I'm trying to use libclang to parse some C++. When I parse source that includes for-range statement like:

  int main() {
      char a[1000];
      for (auto x: a) { // <--- expect to traverse reference to `a` here
      ++x;
      }
  }

I expect to traverse reference to `a` at some point, but it looks like such traversal never happens.
Am I doing something wrong or such case is not supported by libclang?

Below is the example my traversal code in Python (actually I write in C++ but situation is similar there) and its output on sample C++ source.

Thanks,
Dmitry

 » cat for_range.cpp
int main() {
    char a[1000];
    for (auto x: a) {
        ++x;
    }
}

 » cat traverse.py  
import sys
import clang.cindex
clang.cindex.Config.set_library_path('.')

def traverse(node):
    print node.kind, node.spelling, node.location
    for c in node.get_children():
        traverse(c)

index = clang.cindex.Index.create()
tu = index.parse(sys.argv[1])
traverse(tu.cursor)

 » python traverse.py for_range.cpp
CursorKind.TRANSLATION_UNIT for_range.cpp <SourceLocation file None, line 0, column 0>
CursorKind.TYPEDEF_DECL __int128_t <SourceLocation file None, line 0, column 0>
CursorKind.TYPEDEF_DECL __uint128_t <SourceLocation file None, line 0, column 0>
CursorKind.TYPEDEF_DECL __builtin_va_list <SourceLocation file None, line 0, column 0>
CursorKind.TYPE_REF __va_list_tag <SourceLocation file None, line 0, column 0>
CursorKind.FUNCTION_DECL main <SourceLocation file 'for_range.cpp', line 1, column 5>
CursorKind.COMPOUND_STMT  <SourceLocation file 'for_range.cpp', line 1, column 12>
CursorKind.DECL_STMT  <SourceLocation file 'for_range.cpp', line 2, column 5>
CursorKind.VAR_DECL a <SourceLocation file 'for_range.cpp', line 2, column 10>
CursorKind.INTEGER_LITERAL  <SourceLocation file 'for_range.cpp', line 2, column 12>
CursorKind.CXX_FOR_RANGE_STMT  <SourceLocation file 'for_range.cpp', line 3, column 5>
CursorKind.DECL_STMT  <SourceLocation file 'for_range.cpp', line 3, column 18>
CursorKind.DECL_STMT  <SourceLocation file 'for_range.cpp', line 3, column 16>
CursorKind.BINARY_OPERATOR  <SourceLocation file 'for_range.cpp', line 3, column 16>
CursorKind.UNEXPOSED_EXPR __begin <SourceLocation file 'for_range.cpp', line 3, column 16>
CursorKind.DECL_REF_EXPR __begin <SourceLocation file 'for_range.cpp', line 3, column 16>
CursorKind.UNEXPOSED_EXPR __end <SourceLocation file 'for_range.cpp', line 3, column 16>
CursorKind.DECL_REF_EXPR __end <SourceLocation file 'for_range.cpp', line 3, column 16>
CursorKind.UNARY_OPERATOR  <SourceLocation file 'for_range.cpp', line 3, column 16>
CursorKind.DECL_REF_EXPR __begin <SourceLocation file 'for_range.cpp', line 3, column 16>
CursorKind.DECL_STMT  <SourceLocation file 'for_range.cpp', line 3, column 10>
CursorKind.VAR_DECL x <SourceLocation file 'for_range.cpp', line 3, column 15>
CursorKind.UNEXPOSED_EXPR  <SourceLocation file 'for_range.cpp', line 3, column 16>
CursorKind.UNARY_OPERATOR  <SourceLocation file 'for_range.cpp', line 3, column 16>
CursorKind.UNEXPOSED_EXPR __begin <SourceLocation file 'for_range.cpp', line 3, column 16>
CursorKind.DECL_REF_EXPR __begin <SourceLocation file 'for_range.cpp', line 3, column 16>
CursorKind.COMPOUND_STMT  <SourceLocation file 'for_range.cpp', line 3, column 21>
CursorKind.UNARY_OPERATOR  <SourceLocation file 'for_range.cpp', line 4, column 9>
CursorKind.DECL_REF_EXPR x <SourceLocation file 'for_range.cpp', line 4, column 11>




More information about the cfe-users mailing list