c.opt (Wmissing-declarations): Add C++ and ObjC++.
authorIan Lance Taylor <iant@google.com>
Mon, 18 Dec 2006 19:56:58 +0000 (19:56 +0000)
committerIan Lance Taylor <ian@gcc.gnu.org>
Mon, 18 Dec 2006 19:56:58 +0000 (19:56 +0000)
./: * c.opt (Wmissing-declarations): Add C++ and ObjC++.
* doc/invoke.texi (Warning Options): -Wmissing-declarations now
works for C++.
cp/:
* decl.c (start_preparsed_function): Add support for
-Wmissing-declarations.
testsuite:
* g++.dg/warn/Wmissing-declarations-1.C: New test.

From-SVN: r120012

gcc/ChangeLog
gcc/c.opt
gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wmissing-declarations-1.C [new file with mode: 0644]

index e9a397e..6e812d4 100644 (file)
@@ -1,3 +1,9 @@
+2006-12-18  Ian Lance Taylor  <iant@google.com>
+
+       * c.opt (Wmissing-declarations): Add C++ and ObjC++.
+       * doc/invoke.texi (Warning Options): -Wmissing-declarations now
+       works for C++.
+
 2006-12-18  Andrew MacLeod  <amacleod@redhat.com>
 
        * tree-ssa-operands.h (struct vdef_optype_d): Rename to voptype_d.
index ab04dc2..7914b6d 100644 (file)
--- a/gcc/c.opt
+++ b/gcc/c.opt
@@ -264,7 +264,7 @@ C ObjC C++ ObjC++ Var(warn_missing_braces)
 Warn about possibly missing braces around initializers
 
 Wmissing-declarations
-C ObjC Var(warn_missing_declarations)
+C ObjC C++ ObjC++ Var(warn_missing_declarations)
 Warn about global functions without previous declarations
 
 Wmissing-field-initializers
index b51ebc0..2eac776 100644 (file)
@@ -1,3 +1,8 @@
+2006-12-18  Ian Lance Taylor  <iant@google.com>
+
+       * decl.c (start_preparsed_function): Add support for
+       -Wmissing-declarations.
+
 2006-12-16  Simon Martin  <simartin@users.sourceforge.net>
 
        PR c++/29475
index 2e6ace3..f23fd48 100644 (file)
@@ -10570,9 +10570,36 @@ start_preparsed_function (tree decl1, tree attrs, int flags)
               parsing the body of the function.  */
            ;
          else
-           /* Otherwise, OLDDECL is either a previous declaration of
-              the same function or DECL1 itself.  */
-           decl1 = olddecl;
+           {
+             /* Otherwise, OLDDECL is either a previous declaration
+                of the same function or DECL1 itself.  */
+
+             if (warn_missing_declarations
+                 && olddecl == decl1
+                 && !DECL_MAIN_P (decl1)
+                 && TREE_PUBLIC (decl1)
+                 && !DECL_DECLARED_INLINE_P (decl1))
+               {
+                 tree context;
+
+                 /* Check whether DECL1 is in an anonymous
+                    namespace.  */
+                 for (context = DECL_CONTEXT (decl1);
+                      context;
+                      context = DECL_CONTEXT (context))
+                   {
+                     if (TREE_CODE (context) == NAMESPACE_DECL
+                         && DECL_NAME (context) == NULL_TREE)
+                       break;
+                   }
+
+                 if (context == NULL)
+                   warning (OPT_Wmissing_declarations,
+                            "no previous declaration for %q+D", decl1);
+               }
+
+             decl1 = olddecl;
+           }
        }
       else
        {
index 52b1daa..290dad0 100644 (file)
@@ -3221,12 +3221,13 @@ declaration.  This warning is issued even if the definition itself
 provides a prototype.  The aim is to detect global functions that fail
 to be declared in header files.
 
-@item -Wmissing-declarations @r{(C only)}
+@item -Wmissing-declarations @r{(C and C++ only)}
 @opindex Wmissing-declarations
 Warn if a global function is defined without a previous declaration.
 Do so even if the definition itself provides a prototype.
 Use this option to detect global functions that are not declared in
-header files.
+header files.  In C++, no warnings are issued for function templates,
+or for inline functions, or for functions in anonymous namespaces.
 
 @item -Wmissing-field-initializers
 @opindex Wmissing-field-initializers
index 56331b7..1737efd 100644 (file)
@@ -1,3 +1,7 @@
+2006-12-18  Ian Lance Taylor  <iant@google.com>
+
+       * g++.dg/warn/Wmissing-declarations-1.C: New test.
+
 2006-12-17  Eric Botcazou  <ebotcazou@libertysurf.fr>
 
        * gcc.c-torture/compile/pr27528.c: Use empty templates.
diff --git a/gcc/testsuite/g++.dg/warn/Wmissing-declarations-1.C b/gcc/testsuite/g++.dg/warn/Wmissing-declarations-1.C
new file mode 100644 (file)
index 0000000..8944325
--- /dev/null
@@ -0,0 +1,45 @@
+// { dg-options "-Wmissing-declarations" }
+
+void fn1() { }         // { dg-warning "no previous declaration" }
+namespace ns {
+  void fn2() { }       // { dg-warning "no previous declaration" }
+}
+namespace {
+  void fn3() { }
+}
+static void fn4() { }
+
+void fn5();
+namespace ns {
+  void fn6();
+}
+
+void fn5() { }
+namespace ns {
+  void fn6() { }
+}
+
+inline void fn7() { }
+
+class c {
+  void cfn1() { }
+  static void cfn2() { }
+  void cfn3();
+  static void cfn4();
+};
+
+void c::cfn3() { }
+void c::cfn4() { }
+
+static struct {
+  void sfn1() { }
+  static void sfn2() { }
+} s;
+
+template<typename C>
+void tfn1() { }
+
+template void tfn1<c>();
+
+class d { };
+template<> void tfn1<d>() { }