public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/51440] New: C++ compiler produces warning for an unnamed struct member: TYPE has a field FIELD whose type uses the anonymous namespace
@ 2011-12-06 19:37 blue.dragan+gcc at gmail dot com
  2011-12-07  1:32 ` [Bug c++/51440] " redi at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: blue.dragan+gcc at gmail dot com @ 2011-12-06 19:37 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 51440
           Summary: C++ compiler produces warning for an unnamed struct
                    member: TYPE has a field FIELD whose type uses the
                    anonymous namespace
    Classification: Unclassified
           Product: gcc
           Version: 4.6.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: blue.dragan+gcc@gmail.com


Repro steps:

File test.h:
========================================
typedef volatile struct {
} Unnamed;

typedef struct __Named {
    Unnamed u;
} Named;
========================================

File test.cpp:
========================================
#include "test.h"
========================================

Outcome:
========================================
$ g++ -c -o test.o test.cpp
In file included from test.cpp:1:0:
test.h:4:16: warning: '__Named' has a field '__Named::u' whose type uses the
anonymous namespace [enabled by default]
========================================

Details about the repro scenario:

1) Only C++ compiler (C compiler does not generate the warning).

2) Only if the structures are in header files. When the contents of test.h are
moved to test.cpp, the warning disappears.

3) Only if the unnamed structure is volatile. When volatile keyword is removed,
the warning disappears.

4) GCC 4.6.1 hits this problem, but GCC 4.2.4 and 4.4.3 don't have this
problem. Haven't tested any other version of GCC.

5) I'm using GCC for armv7a-unknown-linux-gnueabi target, haven't tried it with
any other target, although the problem "seems" target-independent.

6) The warning is especially annoying, because there appears to be no way to
disable it.

I don't see anything wrong with defining the structures like this. This is a
common way for defining hardware registers which helps to prevent users from
mistakenly dropping the volatile keyword.


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

* [Bug c++/51440] C++ compiler produces warning for an unnamed struct member: TYPE has a field FIELD whose type uses the anonymous namespace
  2011-12-06 19:37 [Bug c++/51440] New: C++ compiler produces warning for an unnamed struct member: TYPE has a field FIELD whose type uses the anonymous namespace blue.dragan+gcc at gmail dot com
@ 2011-12-07  1:32 ` redi at gcc dot gnu.org
  2011-12-08  1:04 ` blue.dragan+gcc at gmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2011-12-07  1:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-12-07 01:32:03 UTC ---
(In reply to comment #0)
> In file included from test.cpp:1:0:
> test.h:4:16: warning: '__Named' has a field '__Named::u' whose type uses the
> anonymous namespace [enabled by default]
> ========================================

The error is a bit misleading, it should say that the type has internal linkage
(which it would have if it was declared in an anonymous namespace, but that's
not the case here.)

> Details about the repro scenario:
> 
> 1) Only C++ compiler (C compiler does not generate the warning).

C supports the idea of "compatible types" which C++ doesn't

> 2) Only if the structures are in header files. When the contents of test.h are
> moved to test.cpp, the warning disappears.

In C++ a type with internal linkage defined in a header will be a different
type in every file that includes the header, which is usually a mistake so you
get a warning.

The same type defined in a source file cannot be referred to by more than one
file anyway, so there's no need to warn.

> 3) Only if the unnamed structure is volatile. When volatile keyword is removed,
> the warning disappears.

'const' has the same effect

Without const or volatile, the unnamed class has the name "Unnamed" for linkage
purpopses, and so has external linkage.  I'm not sure if the cv-qualifiers mean
the unnamed class doesn't have the typedef name 'Unnamed' for linkage purposes,
or if it's a bug


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

* [Bug c++/51440] C++ compiler produces warning for an unnamed struct member: TYPE has a field FIELD whose type uses the anonymous namespace
  2011-12-06 19:37 [Bug c++/51440] New: C++ compiler produces warning for an unnamed struct member: TYPE has a field FIELD whose type uses the anonymous namespace blue.dragan+gcc at gmail dot com
  2011-12-07  1:32 ` [Bug c++/51440] " redi at gcc dot gnu.org
@ 2011-12-08  1:04 ` blue.dragan+gcc at gmail dot com
  2013-03-26 20:06 ` gcc at martinien dot de
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: blue.dragan+gcc at gmail dot com @ 2011-12-08  1:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Chris <blue.dragan+gcc at gmail dot com> 2011-12-08 01:04:18 UTC ---
When narrowing down the issue I stripped a detail which may be important. All
contents of the test.h file are in fact in extern "C" { } range. When the repro
case from my original report is modified to add extern "C" to the test.h file,
then the warning still repros.

If the warning about the unnamed structure turns out to be legitimate, then I
would indeed expect it to be signaled for non-cv-qualified types as well.

Regardless of whether the warning is legitimate or not, it would be nice to
have a way to turn it off.


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

* [Bug c++/51440] C++ compiler produces warning for an unnamed struct member: TYPE has a field FIELD whose type uses the anonymous namespace
  2011-12-06 19:37 [Bug c++/51440] New: C++ compiler produces warning for an unnamed struct member: TYPE has a field FIELD whose type uses the anonymous namespace blue.dragan+gcc at gmail dot com
  2011-12-07  1:32 ` [Bug c++/51440] " redi at gcc dot gnu.org
  2011-12-08  1:04 ` blue.dragan+gcc at gmail dot com
@ 2013-03-26 20:06 ` gcc at martinien dot de
  2013-03-30 22:06 ` [Bug c++/51440] Improve message and add an option for " gcc at martinien dot de
  2015-08-19 14:44 ` paolo.carlini at oracle dot com
  4 siblings, 0 replies; 6+ messages in thread
From: gcc at martinien dot de @ 2013-03-26 20:06 UTC (permalink / raw)
  To: gcc-bugs


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

Martin Richtarsky <gcc at martinien dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gcc at martinien dot de

--- Comment #3 from Martin Richtarsky <gcc at martinien dot de> 2013-03-26 20:06:11 UTC ---
+1 for the ability to disable this warning.

I am compiling a big project with gcc4.8 and this warning is triggered by a
header file. Many of the cpp files that include the header are compiled with
-Werror. Since I have no way to selectively disable this warning with pragmas
in the header the only workaround is removing -Werror from all the cpp files
that include the header.

If there are any other solutions please let me know.


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

* [Bug c++/51440] Improve message and add an option for warning for an unnamed struct member: TYPE has a field FIELD whose type uses the anonymous namespace
  2011-12-06 19:37 [Bug c++/51440] New: C++ compiler produces warning for an unnamed struct member: TYPE has a field FIELD whose type uses the anonymous namespace blue.dragan+gcc at gmail dot com
                   ` (2 preceding siblings ...)
  2013-03-26 20:06 ` gcc at martinien dot de
@ 2013-03-30 22:06 ` gcc at martinien dot de
  2015-08-19 14:44 ` paolo.carlini at oracle dot com
  4 siblings, 0 replies; 6+ messages in thread
From: gcc at martinien dot de @ 2013-03-30 22:06 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #5 from Martin Richtarsky <gcc at martinien dot de> 2013-03-30 22:06:48 UTC ---
Hi Manuel,

thanks for the pointers how to add the optional warning. I will try to submit a
patch, however first I would like to understand what is going on exactly:

- the volatile qualifier (could also be const) leads to internal linkage
- C++ has a default namespace for each translation unit where any code will be
located that has no explicit namespace, and the warning here says that this
structure will be local to this namespace for each translation unit that
includes it the header

Is this correct?

Would there be any kind of workaround to change this without removing the
volatile keyword?

I tried these:
- wrapping the include in extern "C": doesn't work

- wrapping the include in an explicit namespace statement: works


Best regards,
Martin


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

* [Bug c++/51440] Improve message and add an option for warning for an unnamed struct member: TYPE has a field FIELD whose type uses the anonymous namespace
  2011-12-06 19:37 [Bug c++/51440] New: C++ compiler produces warning for an unnamed struct member: TYPE has a field FIELD whose type uses the anonymous namespace blue.dragan+gcc at gmail dot com
                   ` (3 preceding siblings ...)
  2013-03-30 22:06 ` [Bug c++/51440] Improve message and add an option for " gcc at martinien dot de
@ 2015-08-19 14:44 ` paolo.carlini at oracle dot com
  4 siblings, 0 replies; 6+ messages in thread
From: paolo.carlini at oracle dot com @ 2015-08-19 14:44 UTC (permalink / raw)
  To: gcc-bugs

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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #6 from Paolo Carlini <paolo.carlini at oracle dot com> ---
Dup.

*** This bug has been marked as a duplicate of bug 53184 ***


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

end of thread, other threads:[~2015-08-19 14:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-06 19:37 [Bug c++/51440] New: C++ compiler produces warning for an unnamed struct member: TYPE has a field FIELD whose type uses the anonymous namespace blue.dragan+gcc at gmail dot com
2011-12-07  1:32 ` [Bug c++/51440] " redi at gcc dot gnu.org
2011-12-08  1:04 ` blue.dragan+gcc at gmail dot com
2013-03-26 20:06 ` gcc at martinien dot de
2013-03-30 22:06 ` [Bug c++/51440] Improve message and add an option for " gcc at martinien dot de
2015-08-19 14:44 ` paolo.carlini at oracle dot com

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).