libclang: traversing initializers in for-range statements

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.

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

Am I doing something wrong or such case is not supported by libclang?

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>

Dmitry,

I don't know about your specific case, but I've found multiple
incomplete cursor traversal issues in libclang and I've reported them
here (along with fixes and test code):

http://llvm.org/bugs/buglist.cgi?quicksearch=incomplete%20traversal&list_id=68006

So far, every single one has been ignored (tried IRC, the mailing list several
months ago, and now these issues since January).

In my experimentation, I did find there were issues with range-based for
loops, as I've documented here:

https://github.com/jeaye/color_coded/issues/2

However, I found that specific issue was fixed some time in January.
Still, I'm hoping that this thread can build momentum for the existing
issues in libclang which have not been resolved for months.

Cheers,
Jeaye

Thanks, for your response.

Will try recent build. As for other problems mentioned on your page, think that I just haven't hit them yet. Hope that your fixes will be accepted.

Thanks,
Dmitry

Sorry to hear this Jaeye, getting stuff in is sometimes a pain but I assure you it’s mostly because people are super busy. Patches in bugzilla usually just rot, sending them to cfe-commits is the way to go. If they go unnoticed for a week feel free to “ping” them, and make sure you CC someone who’s familiar with the code (svn blame).