<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/91285>91285</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang produces confusing debug location for end of loops
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mmozeiko
</td>
</tr>
</table>
<pre>
For simple loops like this:
```
int x = 0;
while (x == 0)
{
x = 1;
} // <-- end of loop
```
clang emits branch at end of loop to jump back to loop condition.
Debug information for this branch encodes location of beginning of loop - while location.
Here's the complete program: https://godbolt.org/z/E7x4b5Whb
As you can see `br label %while.cond, !dbg !18, !llvm.loop !23` enodes same `!dbg !18` as other instructions, which means line 4, not line 7. The AST encodes `WhileStmt` location as `<line:4:5, line:7:5>` - so clearly it has information about end of loop at line 7.
This leads to poor debugging experience.
For example, if you put breakpoint on line 4 and try to step (F10) in VisualStudio debugger, then you will need to press F10 two times to get out of while loop. Because first time it hits breakpoint of "jmp" instruction at end of loop.
Or in other words - if you put breakpoint on line 4 and run this program with F5, then it will hit line 4 breakpoint 3 times, when it should do that only 2 times.
cl.exe compiles correct location, line 7, for "jmp" instruction at end of loop.
I think branch emitted for end of loop (in this case WhileStmt, but for other type of loops too) should encode end of loop location, not beginning of loop:
https://github.com/llvm/llvm-project/blob/6e5ed351be68017e46120b8ad1aa89d75b0ca204/clang/lib/CodeGen/CGStmt.cpp#L1066
Here's a screenshot of clang compiled exe when I put breakpoint on line 4. You can see how it automatically puts second breakpoint on `jmp`, and also mixed source-disassembly window shows `while (x == 0)` for `jmp` instruction.
![image](https://github.com/llvm/llvm-project/assets/1665010/1862ed68-9d17-459a-8dd2-6a15f1cee38e)
Here's a screenshot of cl.exe compiled code, showing only one breakpoint on line 4, and proper line 7 location for `jmp` instruction.
![image](https://github.com/llvm/llvm-project/assets/1665010/0d7db6ed-5254-49f7-a055-d4a590b0ecc9)
</pre>
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