https://bugs.llvm.org/show_bug.cgi?id=41921

            Bug ID: 41921
           Summary: “extern int i;” declaration inside function is allowed
                    to shadow “static int i;” at file scope despite
                    automatic variable “i” in function
           Product: clang
           Version: 8.0
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Frontend
          Assignee: unassignedclangb...@nondot.org
          Reporter: c...@trust-in-soft.com
                CC: llvm-bugs@lists.llvm.org, neeil...@live.com,
                    richard-l...@metafoo.co.uk

This report is related to but different from
https://bugs.llvm.org/show_bug.cgi?id=3645

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: https://gcc.godbolt.org/z/mVK_8H

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:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90472#c3

Joseph's argument is (I think) that in the C11 clause 6.2.2:4
(https://port70.net/~nsz/c/c11/n1570.html#6.2.2p4 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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to