<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - [DebugInfo] Static variables declared inside functions always have the function scope"
   href="https://bugs.llvm.org/show_bug.cgi?id=44695">44695</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[DebugInfo] Static variables declared inside functions always have the function scope
          </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>All
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </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>ch.bessonova@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>aprantl@apple.com, jdevlieghere@apple.com, keith.walker@arm.com, llvm-bugs@lists.llvm.org, paul_robinson@playstation.sony.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>If a static variable is defined inside of some bracketed block in a function,
clang/llvm will handle it as it has the function scope, not the corresponded
bracketed block scope.
Conside the following example:

 1  int foo() { return 42; };
 2  int main() {
 3    int a = foo();
 4    switch(a) {
 5    case 1: {
 6      static const int a = 1;
 7      return a;
 8    }
 9    case 2: {
10      static const int a = 2;
11      return a;
12    }
13    }
14  }

Here are 3 'a' variables, all of them have function scope according to the
dwarfdump:

0x0000002a:   DW_TAG_subprogram
                DW_AT_low_pc  (0x0000000000401120)
                DW_AT_high_pc (0x000000000040117a)
                DW_AT_frame_base  (DW_OP_reg6 RBP)
                DW_AT_name  ("main")
                DW_AT_decl_file ("temp.cpp")
                DW_AT_decl_line (3) 
                DW_AT_type  (0x00000081 "int")
                DW_AT_external  (true)

0x00000043:     DW_TAG_variable
                  DW_AT_name  ("a")
                  DW_AT_type  (0x0000007c "const int")
                  DW_AT_decl_file ("temp.cpp")
                  DW_AT_decl_line (7) 
                  DW_AT_location  (DW_OP_addr 0x402004)

0x00000058:     DW_TAG_variable
                  DW_AT_name  ("a")
                  DW_AT_type  (0x0000007c "const int")
                  DW_AT_decl_file ("temp.cpp")
                  DW_AT_decl_line (11)
                  DW_AT_location  (DW_OP_addr 0x402008)

0x0000006d:     DW_TAG_variable
                  DW_AT_location  (DW_OP_fbreg -8) 
                  DW_AT_name  ("a")
                  DW_AT_decl_file ("temp.cpp")
                  DW_AT_decl_line (4) 
                  DW_AT_type  (0x00000081 "int")


This confuses debuggers: gdb will print 1 for 'a' even on line 4.

Note: gcc-9.2 generates DW_TAG_lexical_block's for the bracketed blocks and
puts static 'a's there, so gdb prints 42 for 'a' on line 4 as expected.</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>