re PR c++/53184 (Unnecessary anonymous namespace warnings)
authorPaolo Carlini <paolo.carlini@oracle.com>
Wed, 9 Sep 2015 16:48:13 +0000 (16:48 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Wed, 9 Sep 2015 16:48:13 +0000 (16:48 +0000)
2015-09-09  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/53184
* doc/invoke.texi ([Wsubobject-linkage]): Document.

/c-family
2015-09-09  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/53184
* c.opt ([Wsubobject-linkage]): Add.

/cp
2015-09-09  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/53184
* decl2.c (constrain_class_visibility): Use Wsubobject-linkage.

/testsuite
2015-09-09  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/53184
* g++.dg/warn/Wsubobject-linkage-1.C: New.
* g++.dg/warn/Wsubobject-linkage-2.C: Likewise.
* g++.dg/warn/Wsubobject-linkage-3.C: Likewise.
* g++.dg/warn/Wsubobject-linkage-4.C: Likewise.

From-SVN: r227593

gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c.opt
gcc/cp/ChangeLog
gcc/cp/decl2.c
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wsubobject-linkage-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wsubobject-linkage-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wsubobject-linkage-3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wsubobject-linkage-4.C [new file with mode: 0644]

index c00848ca57f2856342a1516d0d712f043a090e4d..44bf040fe513ffc91ee205562f5ad5f8afba6fb7 100644 (file)
@@ -1,3 +1,8 @@
+2015-09-09  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/53184
+       * doc/invoke.texi ([Wsubobject-linkage]): Document.
+
 2015-09-09  Tom de Vries  <tom@codesourcery.com>
 
        * params-list.h: Add missing copyright notice.
index 964f6dda7623e0b991bf7c9ce415afd5cb0f4da0..3acc84f433b3b03201464e93260958e741d6af25 100644 (file)
@@ -1,3 +1,8 @@
+2015-09-09  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/53184
+       * c.opt ([Wsubobject-linkage]): Add.
+
 2015-09-03  Martin Sebor  <msebor@redhat.com>
 
        PR c/66516
index 050dcb0273463fc333fb4c883aed53c23f942a94..d519d7a000bf0c9aa15e436ff4c177fe74a2412c 100644 (file)
@@ -944,6 +944,11 @@ Wuseless-cast
 C++ ObjC++ Var(warn_useless_cast) Warning
 Warn about useless casts
 
+Wsubobject-linkage
+C++ ObjC++ Var(warn_subobject_linkage) Warning Init(1)
+Warn if a class type has a base or a field whose type uses the anonymous
+namespace or depends on a type with no linkage
+
 ansi
 C ObjC C++ ObjC++
 A synonym for -std=c89 (for C) or -std=c++98 (for C++)
index 515a1e88a9d86c21d203eade651f94d4b42b949a..1cc576a3f22943a123804e7d18c3c61ddf34b189 100644 (file)
@@ -1,3 +1,8 @@
+2015-09-09  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/53184
+       * decl2.c (constrain_class_visibility): Use Wsubobject-linkage.
+
 2015-09-09  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/67504
index 74ba380c44df0db2d44939deea6b1a7c146a4f98..6c1f0842331790a2f6582b7ae5e05ea59ebd1d6b 100644 (file)
@@ -2564,10 +2564,25 @@ constrain_class_visibility (tree type)
 
        if (subvis == VISIBILITY_ANON)
          {
-           if (!in_main_input_context ())
-             warning (0, "\
+           if (!in_main_input_context())
+             {
+               tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
+               if (nlt)
+                 {
+                   if (same_type_p (TREE_TYPE (t), nlt))
+                     warning (OPT_Wsubobject_linkage, "\
+%qT has a field %qD whose type has no linkage",
+                              type, t);
+                   else
+                     warning (OPT_Wsubobject_linkage, "\
+%qT has a field %qD whose type depends on the type %qT which has no linkage",
+                              type, t, nlt);
+                 }
+               else
+                 warning (OPT_Wsubobject_linkage, "\
 %qT has a field %qD whose type uses the anonymous namespace",
-                      type, t);
+                          type, t);
+             }
          }
        else if (MAYBE_CLASS_TYPE_P (ftype)
                 && vis < VISIBILITY_HIDDEN
@@ -2585,9 +2600,24 @@ constrain_class_visibility (tree type)
       if (subvis == VISIBILITY_ANON)
         {
          if (!in_main_input_context())
-           warning (0, "\
+           {
+             tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
+             if (nlt)
+               {
+                 if (same_type_p (TREE_TYPE (t), nlt))
+                   warning (OPT_Wsubobject_linkage, "\
+%qT has a base %qT whose type has no linkage",
+                            type, TREE_TYPE (t));
+                 else
+                   warning (OPT_Wsubobject_linkage, "\
+%qT has a base %qT whose type depends on the type %qT which has no linkage",
+                            type, TREE_TYPE (t), nlt);
+               }
+             else
+               warning (OPT_Wsubobject_linkage, "\
 %qT has a base %qT whose type uses the anonymous namespace",
-                    type, TREE_TYPE (t));
+                        type, TREE_TYPE (t));
+           }
        }
       else if (vis < VISIBILITY_HIDDEN
               && subvis >= VISIBILITY_HIDDEN)
index 76e5e29450e3416877200754dd8fa96aa7fa7c03..403cebe2c15d55bf25893f8f86b3ad3bd794d867 100644 (file)
@@ -282,7 +282,7 @@ Objective-C and Objective-C++ Dialects}.
 -Wstrict-aliasing=n @gol -Wstrict-overflow -Wstrict-overflow=@var{n} @gol
 -Wsuggest-attribute=@r{[}pure@r{|}const@r{|}noreturn@r{|}format@r{]} @gol
 -Wsuggest-final-types @gol -Wsuggest-final-methods -Wsuggest-override @gol
--Wmissing-format-attribute @gol
+-Wmissing-format-attribute -Wsubobject-linkage @gol
 -Wswitch  -Wswitch-default  -Wswitch-enum -Wswitch-bool -Wsync-nand @gol
 -Wsystem-headers  -Wtautological-compare  -Wtrampolines  -Wtrigraphs @gol
 -Wtype-limits  -Wundef @gol
@@ -4923,6 +4923,13 @@ types. @option{-Wconversion-null} is enabled by default.
 Warn when a literal '0' is used as null pointer constant.  This can
 be useful to facilitate the conversion to @code{nullptr} in C++11.
 
+@item -Wsubobject-linkage @r{(C++ and Objective-C++ only)}
+@opindex Wsubobject-linkage
+@opindex Wno-subobject-linkage
+Warn if a class type has a base or a field whose type uses the anonymous
+namespace or depends on a type with no linkage.  This warning is
+enabled by default.
+
 @item -Wdate-time
 @opindex Wdate-time
 @opindex Wno-date-time
index 360fe70b8d09a7f10c2d5c44f10ed6417497182b..8215fbea23f23e7622a6d9bfa7f133a11b2dd4ef 100644 (file)
@@ -1,3 +1,11 @@
+2015-09-09  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/53184
+       * g++.dg/warn/Wsubobject-linkage-1.C: New.
+       * g++.dg/warn/Wsubobject-linkage-2.C: Likewise.
+       * g++.dg/warn/Wsubobject-linkage-3.C: Likewise.
+       * g++.dg/warn/Wsubobject-linkage-4.C: Likewise.
+
 2015-09-09  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>
 
        * gcc.target/aarch64/mod_2.x: New file.
diff --git a/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-1.C b/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-1.C
new file mode 100644 (file)
index 0000000..adcaa6d
--- /dev/null
@@ -0,0 +1,9 @@
+// PR c++/53184
+
+typedef volatile struct { } Foo;
+
+#line 6 "foo.C"
+struct Bar { Foo foo; };   // { dg-warning "no linkage" }
+// { dg-bogus "anonymous namespace" "" { target *-*-* } 6 }
+struct Bar2 : Foo { };     // { dg-warning "no linkage" }
+// { dg-bogus "anonymous namespace" "" { target *-*-* } 8 }
diff --git a/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-2.C b/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-2.C
new file mode 100644 (file)
index 0000000..4bb255c
--- /dev/null
@@ -0,0 +1,8 @@
+// PR c++/53184
+// { dg-options "-Wno-subobject-linkage" }
+
+typedef volatile struct { } Foo;
+
+#line 7 "foo.C"
+struct Bar { Foo foo; };
+struct Bar2 : Foo { };
diff --git a/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-3.C b/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-3.C
new file mode 100644 (file)
index 0000000..e9acb63
--- /dev/null
@@ -0,0 +1,9 @@
+// PR c++/53184
+
+namespace { struct Foo { }; }
+
+#line 6 "foo.C"
+struct Bar { Foo foo; };   // { dg-warning "anonymous namespace" }
+// { dg-bogus "no linkage" "" { target *-*-* } 6 }
+struct Bar2 : Foo { };     // { dg-warning "anonymous namespace" }
+// { dg-bogus "no linkage" "" { target *-*-* } 8 }
diff --git a/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-4.C b/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-4.C
new file mode 100644 (file)
index 0000000..033bc47
--- /dev/null
@@ -0,0 +1,8 @@
+// PR c++/53184
+// { dg-options "-Wno-subobject-linkage" }
+
+namespace { struct Foo { }; }
+
+#line 7 "foo.C"
+struct Bar { Foo foo; };
+struct Bar2 : Foo { };