public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/45977] New: "warning: 'i' initialized and declared 'extern'" is spurious
@ 2010-10-12  7:14 konrad.schwarz at siemens dot com
  2010-10-12 11:32 ` [Bug c/45977] " joseph at codesourcery dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: konrad.schwarz at siemens dot com @ 2010-10-12  7:14 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45977

           Summary: "warning: 'i' initialized and declared 'extern'" is
                    spurious
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: konrad.schwarz@siemens.com


Created attachment 22020
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=22020
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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/45977] "warning: 'i' initialized and declared 'extern'" is spurious
  2010-10-12  7:14 [Bug c/45977] New: "warning: 'i' initialized and declared 'extern'" is spurious konrad.schwarz at siemens dot com
@ 2010-10-12 11:32 ` joseph at codesourcery dot com
  2010-10-12 15:27 ` konrad.schwarz at siemens dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: joseph at codesourcery dot com @ 2010-10-12 11:32 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45977

--- Comment #1 from joseph at codesourcery dot com <joseph at codesourcery dot com> 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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/45977] "warning: 'i' initialized and declared 'extern'" is spurious
  2010-10-12  7:14 [Bug c/45977] New: "warning: 'i' initialized and declared 'extern'" is spurious konrad.schwarz at siemens dot com
  2010-10-12 11:32 ` [Bug c/45977] " joseph at codesourcery dot com
@ 2010-10-12 15:27 ` konrad.schwarz at siemens dot com
  2010-10-12 15:47 ` joseph at codesourcery dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: konrad.schwarz at siemens dot com @ 2010-10-12 15:27 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45977

--- Comment #2 from Konrad Schwarz <konrad.schwarz at siemens dot com> 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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/45977] "warning: 'i' initialized and declared 'extern'" is spurious
  2010-10-12  7:14 [Bug c/45977] New: "warning: 'i' initialized and declared 'extern'" is spurious konrad.schwarz at siemens dot com
  2010-10-12 11:32 ` [Bug c/45977] " joseph at codesourcery dot com
  2010-10-12 15:27 ` konrad.schwarz at siemens dot com
@ 2010-10-12 15:47 ` joseph at codesourcery dot com
  2011-05-09  4:01 ` cascardo at holoscopio dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: joseph at codesourcery dot com @ 2010-10-12 15:47 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45977

--- Comment #3 from joseph at codesourcery dot com <joseph at codesourcery dot com> 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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/45977] "warning: 'i' initialized and declared 'extern'" is spurious
  2010-10-12  7:14 [Bug c/45977] New: "warning: 'i' initialized and declared 'extern'" is spurious konrad.schwarz at siemens dot com
                   ` (2 preceding siblings ...)
  2010-10-12 15:47 ` joseph at codesourcery dot com
@ 2011-05-09  4:01 ` cascardo at holoscopio dot com
  2022-03-23 12:15 ` [Bug c/45977] "warning: 'i' initialized and declared 'extern'" could use a separate warning flag controlling it manx-bugzilla at problemloesungsmaschine dot de
  2022-12-07 19:36 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cascardo at holoscopio dot com @ 2011-05-09  4:01 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45977

Thadeu Lima de Souza Cascardo <cascardo at holoscopio dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |cascardo at holoscopio dot
                   |                            |com

--- Comment #4 from Thadeu Lima de Souza Cascardo <cascardo at holoscopio dot com> 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?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/45977] "warning: 'i' initialized and declared 'extern'" could use a separate warning flag controlling it
  2010-10-12  7:14 [Bug c/45977] New: "warning: 'i' initialized and declared 'extern'" is spurious konrad.schwarz at siemens dot com
                   ` (3 preceding siblings ...)
  2011-05-09  4:01 ` cascardo at holoscopio dot com
@ 2022-03-23 12:15 ` manx-bugzilla at problemloesungsmaschine dot de
  2022-12-07 19:36 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: manx-bugzilla at problemloesungsmaschine dot de @ 2022-03-23 12:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=45977

Jörn Heusipp <manx-bugzilla at problemloesungsmaschine dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manx-bugzilla@problemloesun
                   |                            |gsmaschine.de

--- Comment #8 from Jörn Heusipp <manx-bugzilla at problemloesungsmaschine dot de> ---
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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/45977] "warning: 'i' initialized and declared 'extern'" could use a separate warning flag controlling it
  2010-10-12  7:14 [Bug c/45977] New: "warning: 'i' initialized and declared 'extern'" is spurious konrad.schwarz at siemens dot com
                   ` (4 preceding siblings ...)
  2022-03-23 12:15 ` [Bug c/45977] "warning: 'i' initialized and declared 'extern'" could use a separate warning flag controlling it manx-bugzilla at problemloesungsmaschine dot de
@ 2022-12-07 19:36 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-07 19:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=45977

--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-12-07 19:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-12  7:14 [Bug c/45977] New: "warning: 'i' initialized and declared 'extern'" is spurious konrad.schwarz at siemens dot com
2010-10-12 11:32 ` [Bug c/45977] " joseph at codesourcery dot com
2010-10-12 15:27 ` konrad.schwarz at siemens dot com
2010-10-12 15:47 ` joseph at codesourcery dot com
2011-05-09  4:01 ` cascardo at holoscopio dot com
2022-03-23 12:15 ` [Bug c/45977] "warning: 'i' initialized and declared 'extern'" could use a separate warning flag controlling it manx-bugzilla at problemloesungsmaschine dot de
2022-12-07 19:36 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).