<div dir="ltr">Hi,<div><br></div><div>I'm not sure if I understand your question, but it seems to me you are assuming that the range-for loop will access the values of a by reference. This is not true by default, actually. If you write</div><div><br></div><div>(char x: a) or (auto x: a)</div><div><br></div><div>x will receive a copy of an element of a each iteration. To get access by reference, just change that to:</div><div><br></div><div>(char& x: a) ou (auto& x: a)</div><div><br></div><div>As a reminder, auto<b> </b><i>never</i> translates to a reference type automatically--you must put the &. This is true even in cases such as:</div><div>int a = 3;</div><div>int& b = a;</div><div>auto c = b; // c gets 3, but c is not a reference to a. c is just an int.</div><div><br></div><div>The reason for this is that all expressions that have reference type are implicitly dereferenced, so expression "b" in the third line has int type, not reference of int, because it is automatically dereferenced.</div><div><br></div><div>Hope that helps,</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 15 February 2015 at 19:40, Дмитрий Ермолов <span dir="ltr"><<a href="mailto:epdmitry@yandex.ru" target="_blank">epdmitry@yandex.ru</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I'm trying to use libclang to parse some C++. When I parse source that includes for-range statement like:<br>
<br>
  int main() {<br>
      char a[1000];<br>
      for (auto x: a) { // <--- expect to traverse reference to `a` here<br>
      ++x;<br>
      }<br>
  }<br>
<br>
I expect to traverse reference to `a` at some point, but it looks like such traversal never happens.<br>
Am I doing something wrong or such case is not supported by libclang?<br>
<br>
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.<br>
<br>
Thanks,<br>
Dmitry<br>
<br>
 » cat for_range.cpp<br>
int main() {<br>
    char a[1000];<br>
    for (auto x: a) {<br>
        ++x;<br>
    }<br>
}<br>
<br>
 » cat traverse.py<br>
import sys<br>
import clang.cindex<br>
clang.cindex.Config.set_library_path('.')<br>
<br>
def traverse(node):<br>
    print node.kind, node.spelling, node.location<br>
    for c in node.get_children():<br>
        traverse(c)<br>
<br>
index = clang.cindex.Index.create()<br>
tu = index.parse(sys.argv[1])<br>
traverse(tu.cursor)<br>
<br>
 » python traverse.py for_range.cpp<br>
CursorKind.TRANSLATION_UNIT for_range.cpp <SourceLocation file None, line 0, column 0><br>
CursorKind.TYPEDEF_DECL __int128_t <SourceLocation file None, line 0, column 0><br>
CursorKind.TYPEDEF_DECL __uint128_t <SourceLocation file None, line 0, column 0><br>
CursorKind.TYPEDEF_DECL __builtin_va_list <SourceLocation file None, line 0, column 0><br>
CursorKind.TYPE_REF __va_list_tag <SourceLocation file None, line 0, column 0><br>
CursorKind.FUNCTION_DECL main <SourceLocation file 'for_range.cpp', line 1, column 5><br>
CursorKind.COMPOUND_STMT  <SourceLocation file 'for_range.cpp', line 1, column 12><br>
CursorKind.DECL_STMT  <SourceLocation file 'for_range.cpp', line 2, column 5><br>
CursorKind.VAR_DECL a <SourceLocation file 'for_range.cpp', line 2, column 10><br>
CursorKind.INTEGER_LITERAL  <SourceLocation file 'for_range.cpp', line 2, column 12><br>
CursorKind.CXX_FOR_RANGE_STMT  <SourceLocation file 'for_range.cpp', line 3, column 5><br>
CursorKind.DECL_STMT  <SourceLocation file 'for_range.cpp', line 3, column 18><br>
CursorKind.DECL_STMT  <SourceLocation file 'for_range.cpp', line 3, column 16><br>
CursorKind.BINARY_OPERATOR  <SourceLocation file 'for_range.cpp', line 3, column 16><br>
CursorKind.UNEXPOSED_EXPR __begin <SourceLocation file 'for_range.cpp', line 3, column 16><br>
CursorKind.DECL_REF_EXPR __begin <SourceLocation file 'for_range.cpp', line 3, column 16><br>
CursorKind.UNEXPOSED_EXPR __end <SourceLocation file 'for_range.cpp', line 3, column 16><br>
CursorKind.DECL_REF_EXPR __end <SourceLocation file 'for_range.cpp', line 3, column 16><br>
CursorKind.UNARY_OPERATOR  <SourceLocation file 'for_range.cpp', line 3, column 16><br>
CursorKind.DECL_REF_EXPR __begin <SourceLocation file 'for_range.cpp', line 3, column 16><br>
CursorKind.DECL_STMT  <SourceLocation file 'for_range.cpp', line 3, column 10><br>
CursorKind.VAR_DECL x <SourceLocation file 'for_range.cpp', line 3, column 15><br>
CursorKind.UNEXPOSED_EXPR  <SourceLocation file 'for_range.cpp', line 3, column 16><br>
CursorKind.UNARY_OPERATOR  <SourceLocation file 'for_range.cpp', line 3, column 16><br>
CursorKind.UNEXPOSED_EXPR __begin <SourceLocation file 'for_range.cpp', line 3, column 16><br>
CursorKind.DECL_REF_EXPR __begin <SourceLocation file 'for_range.cpp', line 3, column 16><br>
CursorKind.COMPOUND_STMT  <SourceLocation file 'for_range.cpp', line 3, column 21><br>
CursorKind.UNARY_OPERATOR  <SourceLocation file 'for_range.cpp', line 4, column 9><br>
CursorKind.DECL_REF_EXPR x <SourceLocation file 'for_range.cpp', line 4, column 11><br>
<br>
_______________________________________________<br>
cfe-users mailing list<br>
<a href="mailto:cfe-users@cs.uiuc.edu">cfe-users@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Thiago Caetano<br>Engenharia de Computação Unicamp 2010<br></div></div>
</div>