[Bug c/45977] New: "warning: 'i' initialized and declared 'extern'" is spurious

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

[Bug c/45977] New: "warning: 'i' initialized and declared 'extern'" is spurious

seurer at gcc dot gnu.org
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: [hidden email]
        ReportedBy: [hidden email]


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
Reply | Threaded
Open this post in threaded view
|

[Bug c/45977] "warning: 'i' initialized and declared 'extern'" is spurious

seurer at gcc dot gnu.org
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.
Reply | Threaded
Open this post in threaded view
|

[Bug c/45977] "warning: 'i' initialized and declared 'extern'" is spurious

seurer at gcc dot gnu.org
In reply to this post by seurer at gcc dot gnu.org
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.
Reply | Threaded
Open this post in threaded view
|

[Bug c/45977] "warning: 'i' initialized and declared 'extern'" is spurious

seurer at gcc dot gnu.org
In reply to this post by seurer at gcc dot gnu.org
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.
Reply | Threaded
Open this post in threaded view
|

[Bug c/45977] "warning: 'i' initialized and declared 'extern'" is spurious

seurer at gcc dot gnu.org
In reply to this post by seurer at gcc dot gnu.org
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?
Reply | Threaded
Open this post in threaded view
|

[Bug c/45977] "warning: 'i' initialized and declared 'extern'" could use a separate warning flag controlling it

seurer at gcc dot gnu.org
In reply to this post by seurer at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=45977

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egallager at gcc dot gnu.org
            Summary|"warning: 'i' initialized   |"warning: 'i' initialized
                   |and declared 'extern'" is   |and declared 'extern'"
                   |spurious                    |could use a separate
                   |                            |warning flag controlling it
           Severity|normal                      |enhancement

--- Comment #5 from Eric Gallager <egallager at gcc dot gnu.org> ---
Confirming on the basis that a separate warning flag here would be nice
Reply | Threaded
Open this post in threaded view
|

[Bug c/45977] "warning: 'i' initialized and declared 'extern'" could use a separate warning flag controlling it

seurer at gcc dot gnu.org
In reply to this post by seurer at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=45977

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2017-07-25
     Ever confirmed|0                           |1

--- Comment #6 from Eric Gallager <egallager at gcc dot gnu.org> ---
(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)
Reply | Threaded
Open this post in threaded view
|

[Bug c/45977] "warning: 'i' initialized and declared 'extern'" could use a separate warning flag controlling it

seurer at gcc dot gnu.org
In reply to this post by seurer at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=45977

--- Comment #7 from Eric Gallager <egallager at gcc dot gnu.org> ---
This bug provoked this StackOverflow question:
https://stackoverflow.com/questions/57957168/how-do-i-disable-a-gcc-warning-which-has-no-command-line-switch