<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - debug info fail -- range based for loops and other stuff"
   href="http://llvm.org/bugs/show_bug.cgi?id=19864">19864</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>debug info fail -- range based for loops and other stuff
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>chandlerc@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>So, I came here to file a bug about debug info for range-based for loops. I
wrote the following test case:

----
#include <vector>
#include <iostream>

int main() {
  std::vector<int> v = {13, 21, 8, 3, 34, 1, 5, 2};
  std::cout << "v = {" << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3]
            << ", " << v[4] << ", " << v[5] << ", " << v[6] << ", " << v[7]
            << "}\n";

  int size = v.size();
  if (v.size() > 3) {
    int a = 0, b = 0;
    for (int x : v)
      if (x >= size)
        ++b;
      else if (x >= 0)
        ++a;
    std::cout << "a = " << a << "\n";
    std::cout << "b = " << b << "\n";
  }
}
----

Then I compiled and debugged it:

% clang++ -std=c++11 -g -o x x.cc
% gdb --args ./x
GNU gdb (GDB) 7.6.50.20130820-cvs
<snip>
Reading symbols from .../x...done.
(gdb) break main
Breakpoint 1 at 0x400b92: file x.cc, line 5.
(gdb) r
Starting program: .../x
<snip>

Breakpoint 1, main () at x.cc:5
5         std::vector<int> v = {13, 21, 8, 3, 34, 1, 5, 2};
(gdb) n
6         std::cout << "v = {" << v[0] << ", " << v[1] << ", " << v[2] << ", "
<< v[3]
(gdb) 
7                   << ", " << v[4] << ", " << v[5] << ", " << v[6] << ", " <<
v[7]
(gdb) 
6         std::cout << "v = {" << v[0] << ", " << v[1] << ", " << v[2] << ", "
<< v[3]
(gdb) 
7                   << ", " << v[4] << ", " << v[5] << ", " << v[6] << ", " <<
v[7]
(gdb) 
6         std::cout << "v = {" << v[0] << ", " << v[1] << ", " << v[2] << ", "
<< v[3]
(gdb) 
7                   << ", " << v[4] << ", " << v[5] << ", " << v[6] << ", " <<
v[7]
(gdb) 
6         std::cout << "v = {" << v[0] << ", " << v[1] << ", " << v[2] << ", "
<< v[3]
(gdb) 
7                   << ", " << v[4] << ", " << v[5] << ", " << v[6] << ", " <<
v[7]
(gdb) 
6         std::cout << "v = {" << v[0] << ", " << v[1] << ", " << v[2] << ", "
<< v[3]


WAT. I have no idea what happened here. But again, this is not why I wanted to
file the bug, its just something we should fix. Continuing with GDB...


(gdb) 
v = {13, 21, 8, 3, 34, 1, 5, 2}
10        int size = v.size();
(gdb) 
11        if (v.size() > 3) {
(gdb) 
12          int a = 0, b = 0;
(gdb) 
13          for (int x : v)
(gdb) 
14            if (x >= size)
(gdb) 
15              ++b;
(gdb) 
17              ++a;

BUG: We never execute both ++b and ++a. This is a bogus line to jump to, I
think it's actually trying to jump to the "end" of the body of the range based
for loop, and has this as the source location or something. The *behavior* of
this program is entirely correct, but the debugger is lying to me.

(gdb) 
13          for (int x : v)
(gdb) 
14            if (x >= size)
(gdb) 
15              ++b;
(gdb) 
17              ++a;

And it happens on every loop where we execute "++b".

(gdb) 
13          for (int x : v)
(gdb) 
14            if (x >= size)
(gdb) 
15              ++b;
(gdb) 
17              ++a;
(gdb) 
13          for (int x : v)
(gdb) 
14            if (x >= size)
(gdb) 
16            else if (x >= 0)
(gdb) 
17              ++a;
(gdb) 
13          for (int x : v)

But we only see one stop at that line when we execute "++a".

Let me know what else I can provide to help fix!</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>