Bug 45977 - "warning: 'i' initialized and declared 'extern'" could use a separate warning flag controlling it
Summary: "warning: 'i' initialized and declared 'extern'" could use a separate warning...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, easyhack
Depends on:
Blocks: 44209
  Show dependency treegraph
 
Reported: 2010-10-12 07:14 UTC by Konrad Schwarz
Modified: 2022-12-07 19:36 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-07-25 00:00:00


Attachments
file "external.c", a short test case exhibiting the problem (78 bytes, application/octet-stream)
2010-10-12 07:14 UTC, Konrad Schwarz
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Konrad Schwarz 2010-10-12 07:14:46 UTC
Created attachment 22020 [details]
file "external.c", a short test case exhibiting the problem

Given the attached source file, GCC (powerpc-eabi-gcc.exe (Sourcery G++ Lite 4.4-79) 4.4.1), invoked as:
powerpc-eabi-gcc -te500v1 -mcall-sysv-noeabi -MMD -Wno-parentheses      -c -o ex
ternal.o external.c

reports:
external.c:3: warning: 'i' initialized and declared 'extern'

The relevant lines in the file are:

# if    1
static int i;
# endif

extern int i = 3; 

The warning occurs irrespectively whether the # if 1 is changed to # if 0.

Standard C gives the above code a well defined meaning.  The following reference is from the C89 specification:

6.1.2.2, Linkage of Identifiers:
If the declaration of an identifier for an object or a function contains the storage-class specifier <b>extern</b>, the identifier has the same linkage as any visible declaration of the identifier with file scope.  If there is no visible declaration with file scope, the identifier has external linkage.

Thus, the primary meaning of "extern" for external definitions (that is, definitions with file scope) is to use whatever linkage specification is already in force.  Only if no linkage specification has been specified previously does "extern" mean assign external linkage.

This is in contrast with "static", which means assign internal linkage, and no storage-class specifier, which means external linkage for objects (but means the same thing as "extern" for functions, i.e., use existing linkage if possible).

6.1.2.2 goes on to specify that only one type of linkage may be specified for a n identifier in a translation unit.

Consider a code generation scenario using the C preprocessor (include directives), where a generic code generation component is being employed.  Standard C allows a design where the (documented) definitions supplied by the component are marked "extern", giving the user of the component the ability to override the linkage by providing a tentative definition marked "static" before including the component's header file.

The warning emitted by GCC undermines this design.

So what I'd like is for:
* this warning to be turned off by default
* (possibly) enable this warning with -Wextra
Comment 1 jsm-csl@polyomino.org.uk 2010-10-12 11:32:10 UTC
This is a coding style warning - the code is valid, but extremely 
unidiomatic for C since "extern" is generally expected to mean that the 
declaration is not providing a definition of the object.  Following static 
by extern, though valid, is also a C feature of doubtful value.
Comment 2 Konrad Schwarz 2010-10-12 15:27:15 UTC
(In reply to comment #1)
> This is a coding style warning - the code is valid, but extremely 
> unidiomatic for C since "extern" is generally expected to mean that the 
> declaration is not providing a definition of the object.  Following static 
> by extern, though valid, is also a C feature of doubtful value.

I see the value of following static by extern -- the bug report provides an example.  To restate, using extern in a definition allows overriding an object's or function's linkage, which can be useful in a translation unit consisting of files a user can change and files a user cannot.

Whether or not this is idiomatic usage, or corresponds to what is generally expected, is not sufficient grounds for a warning.

To the contrary, this warning promulgates incorrect assumptions about "extern".  

At the very least, there must be a way of turning this warning off.
Comment 3 jsm-csl@polyomino.org.uk 2010-10-12 15:47:45 UTC
On Tue, 12 Oct 2010, konrad.schwarz at siemens dot com wrote:

> Whether or not this is idiomatic usage, or corresponds to what is generally
> expected, is not sufficient grounds for a warning.

That is the whole point of warnings: to diagnose dubious usages that 
cannot be given hard errors because they are formally valid according to 
the standard.

> At the very least, there must be a way of turning this warning off.

That is the only actual bug I see here: all warnings should have options 
controlling them.
Comment 4 Thadeu Lima de Souza Cascardo 2011-05-09 03:49:25 UTC
What seemed really strange to me is that the warning would be emitted even when the static declaration was inside #if 0/#endif pair, since the pre-processor would have removed the code entirely.

And reading the warning also told me the problem was assigning a value in the extern declaration.

So

extern int i = 3;

is not fine. While

static int i = 3;
extern int i;

is perfectly OK. That is, without any warning on/off flags, there is no warning. Should there be such a warning as Joseph says? -Wall -Wextra emits no warning for static int i; extern int i; case. In fact, there is -Wredundant-decls, but it only works if there is no initialization in the extern declaration.

I can turn off this warning using -w, but there is no particular flag for this warning. Should one of the existing flags be used or a new one be created? Better yet, should gcc not warn when this initialization happens in this particular case, when the variable has already been declared as static?
Comment 5 Eric Gallager 2017-07-25 19:18:03 UTC
Confirming on the basis that a separate warning flag here would be nice
Comment 6 Eric Gallager 2017-07-25 19:20:31 UTC
(In reply to Eric Gallager from comment #5)
> Confirming on the basis that a separate warning flag here would be nice

Oops I retitled it but forgot to actually confirm it like I said I was doing (sorry; hopefully I've got it this time)
Comment 7 Eric Gallager 2019-09-17 03:26:05 UTC
This bug provoked this StackOverflow question: https://stackoverflow.com/questions/57957168/how-do-i-disable-a-gcc-warning-which-has-no-command-line-switch
Comment 8 Jörn Heusipp 2022-03-23 12:15:12 UTC
I am seeing the same warning, however in C++, when trying to set DJGPP's CRT startup flags (see <http://www.delorie.com/djgpp/v2faq/faq18_9.html>):

```
manx@appendix:~/tmp$ cat djgpp-main.cpp
#include <crt0.h>
extern "C" int _crt0_startup_flags = 0 | _CRT0_FLAG_LOCK_MEMORY;
int main(int argc, char * argv[]) {
    _crt0_startup_flags &= ~_CRT0_FLAG_LOCK_MEMORY;
    static_cast<void>(argc);
    static_cast<void>(argv);
    return 0;
}
manx@appendix:~/tmp$ i386-pc-msdosdjgpp-g++ -c -std=gnu++17 -O2 -Wall -Wextra -Wpedantic djgpp-main.cpp
djgpp-main.cpp:2:16: warning: '_crt0_startup_flags' initialized and declared 'extern'
    2 | extern "C" int _crt0_startup_flags = 0 | _CRT0_FLAG_LOCK_MEMORY;
      |                ^~~~~~~~~~~~~~~~~~~
manx@appendix:~/tmp$
```

minimal test case:
```
manx@appendix:~/tmp$ cat warn.cpp
extern "C" {
extern int foo;
}
extern "C" int foo = 23;
manx@appendix:~/tmp$ g++ -c -std=c++17 -O2 -Wall -Wextra -Wpedantic warn.cpp
warn.cpp:4:16: warning: ‘foo’ initialized and declared ‘extern’
    4 | extern "C" int foo = 23;
      |                ^~~
manx@appendix:~/tmp$
```

also happens with no -W flags:
```
manx@appendix:~/tmp$ g++ -c -std=c++17 -O2 warn.cpp
warn.cpp:4:16: warning: ‘foo’ initialized and declared ‘extern’
    4 | extern "C" int foo = 23;
      |                ^~~
```

However, I am not seeing the warning in C code:
```
manx@appendix:~/tmp$ cat warn.c
extern int foo;
int foo = 23;
manx@appendix:~/tmp$ gcc -c -std=c17 -O2 -Wall -Wextra -Wpedantic warn.c
manx@appendix:~/tmp$
```

I would really appreciate an option to disable this warning in C++.

I am not 100% sure if my issue is really identical, but it certainly looks related.

Also, as the original issue was about C, do you want me to report a separate issue for C++?

GCC versions:

manx@appendix:~/tmp$ gcc --version
gcc (Debian 11.2.0-18) 11.2.0

manx@appendix:~/tmp$ i386-pc-msdosdjgpp-gcc --version
i386-pc-msdosdjgpp-gcc (GCC) 10.3.0
Comment 9 Andrew Pinski 2022-12-07 19:36:16 UTC
(In reply to Jörn Heusipp from comment #8)
> I am seeing the same warning, however in C++, when trying to set DJGPP's CRT
> startup flags (see <http://www.delorie.com/djgpp/v2faq/faq18_9.html>):

I filed PR 108013 for that case since it is a different issue all together really.