<html>
    <head>
      <base href="https://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 --- - [codeview] properly emit scope for variables"
   href="https://llvm.org/bugs/show_bug.cgi?id=28458">28458</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[codeview] properly emit scope for variables
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Windows NT
          </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>DebugInfo
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>vladimir@pobox.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When compiling something like:

int somefunc(int arg) {
  {
     char scopedChar = 0;
     scopedChar++;
  }

  int n = 0;
  for (int number = 0; number < 5; number++) {
    n += number;
  }
  for (int number = 0; number < 5; number++) {
    n += number;
  }
  return n;
}

the VS debugger shows two entries in the "Locals" view for "number", showing
them and "scopedChar" as "Variable is optimized away and not available." at the
entrance to the function.  Stepping through it, "scopedChar" becomes available,
and then when it leaves scope it sticks around in the locals view.  Same with
the two "number" variables.

When compiled with MSVC, the list of locals properly changes based on what's
actually in scope.  The same behaviour is in windbg, with "tv" (show locals).

With readobj, MSVC generates codeview entries that look like this:

    BlockStart {
      PtrParent: 0x0
      PtrEnd: 0x0
      CodeSize: 0x2D
      CodeOffset: ?somefunc@@YAHH@Z+0x1D
      Segment: 0x0
      BlockName:
      LinkageName: ?somefunc@@YAHH@Z
    }
    RegRelativeSym {
      Offset: 0x8
      Type: int (0x74)
      Register: 0x14F
      VarName: number
    }
    BlockEnd {
    }
[similar for the second one, just with a different CodeOffset]

Whereas clang/llvm generates:

    Local {
      Type: int (0x74)
      Flags [ (0x0)
      ]
      VarName: number
    }
    DefRangeRegisterRel {
      BaseRegister: 335
      HasSpilledUDTMember: No
      OffsetInParent: 0
      BasePointerOffset: 8
      LocalVariableAddrRange {
        OffsetStart: .text+0x1F
        ISectStart: 0x0
        Range: 0x2F
      }
    }
[similar for second, with different OffsetStart]

The IR for these looks like:

!21 = !DILocalVariable(name: "number", scope: !22, file: !1, line: 9, type:
!10)
!22 = distinct !DILexicalBlock(scope: !7, file: !1, line: 9, column: 3)
...
!37 = !DILocalVariable(name: "number", scope: !38, file: !1, line: 12, type:
!10)
!38 = distinct !DILexicalBlock(scope: !7, file: !1, line: 12, column: 3)

It looks like the debuggers (sort of) understand the RangeRegisterRel as a
proper scope for when the local variable is valid, but don't actually limit
visibility to that scope.  Should llvm be generating BlockStart/BlockEnd in CV
for all DILexicalBlocks?</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>