<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 - “extern int i;” declaration inside function is allowed to shadow “static int i;” at file scope despite automatic variable “i” in function"
   href="https://bugs.llvm.org/show_bug.cgi?id=41921">41921</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>“extern int i;” declaration inside function is allowed to shadow “static int i;” at file scope despite automatic variable “i” in function
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>8.0
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Frontend
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>cuoq@trust-in-soft.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This report is related to but different from
<a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED FIXED - clang incorrectly rejects "static int a; extern int a;""
   href="show_bug.cgi?id=3645">https://bugs.llvm.org/show_bug.cgi?id=3645</a>

The C program below is accepted by Clang 8.0.0 and compiled to the obvious
result (p1 and p2 are equal after the execution of f()).
__________

int *p1, *p2;

static int i = 1;

void f(void) {
    p1 = &i;
    int i = 2;
    {
        extern int i;
        p2 = &i;
    }
}
__________

Compiler Explorer link: <a href="https://gcc.godbolt.org/z/mVK_8H">https://gcc.godbolt.org/z/mVK_8H</a>

You may notice in the link that GCC rejects this same C program BECAUSE of the
line “int i = 2;” (changing i to j in this line makes GCC accept it)

Joseph Myers says that rejecting this program when f() declares the automatic
variable “i” is on purpose and is what the C standard intends:
<a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90472#c3">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90472#c3</a>

Joseph's argument is (I think) that in the C11 clause 6.2.2:4
(<a href="https://port70.net/~nsz/c/c11/n1570.html#6.2.2p4">https://port70.net/~nsz/c/c11/n1570.html#6.2.2p4</a> and quoted below), the part
that makes the program valid without the automatic variable “i” does not apply
to this program because the static variable “i” is not “visible”, being hidden
by the automatic variable “i”. I have to admit that footnote 31 appears to make
his interpretation correct.
__________

For an identifier declared with the storage-class specifier extern in a scope
in which a prior declaration of that identifier is visible,31) if the prior
declaration specifies internal or external linkage, the linkage of the
identifier at the later declaration is the same as the linkage specified at the
prior declaration. If no prior declaration is visible, or if the prior
declaration specifies no linkage, then the identifier has external linkage.


footnote 31) As specified in 6.2.1, the later declaration might hide the prior
declaration.
___________

If you disagree with Joseph's interpretation, I can attempt to file a DR for
clarification of this part of the C standard.</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>