<div>Hi,</div><div><br></div><div>Richard Smith suggested that I try to improve the diagnostic emitted when Clang encounters certain kinds of invalid C+11 ranged-based for loops.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div>When a pointer to a container is used as the range in a range-based for, Clang's diagnostic is not awesome:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
struct S { int *begin(); int *end(); };<br>void f(S *p) {<br> for (auto i : p) {}<br>}<br>tmp.cpp:3:15: error: use of undeclared identifier 'begin'<br> for (auto i : p) {} }<br>             ^<br>tmp.cpp:3:15: note: range has type 'S *'</blockquote>
<div><br></div><div>We should do better than that, and suggest inserting the missing '*'.</div></blockquote><div><br></div><div>This patch replaces the errors complaining about undeclared identifiers with an error specific to ranged-for loops, along with an explanatory note. I also updated the existing test cases to reflect the change. For example, the above code now generates this:</div>
<div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">test.cpp:3:15: error: invalid range expression of type 'S *'<br>
 for (auto i : p) {}<br>             ^ ~<br>test.cpp:3:16: note: range expression is of type 'S *'; did you<br>      mean to dereference it with '*'?<br> for (auto i : p) {}<br>               ^<br>               *<br>
test.cpp:3:14: note: no viable 'end' function for range of type<br>      'S *'<br> for (auto i : p) {}</blockquote><div><br></div><div>I hope that this is helpful, and comments are always welcome.</div><div>
<br></div><div>-Sam </div></div>