This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
PATCH RFA: Support -Wmissing-declarations in C++
- From: Ian Lance Taylor <iant at google dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: 14 Dec 2006 09:50:07 -0800
- Subject: PATCH RFA: Support -Wmissing-declarations in C++
Currently -Wmissing-declarations only works for C. But it makes just
as much sense for C++ as it does for C. This patch adds support for
-Wmissing-declarations in C++.
Tested with bootstrap and testsuite run on i686-pc-linux-gnu.
OK for mainline?
Ian
gcc/ChangeLog:
2006-12-14 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++.
gcc/cp/ChangeLog:
2006-12-14 Ian Lance Taylor <iant@google.com>
* decl.c (start_preparsed_function): Add support for
-Wmissing-declarations.
gcc/testsuite/ChangeLog:
2006-12-14 Ian Lance Taylor <iant@google.com>
* g++.dg/warn/Wmissing-declarations-1.C: New test.
Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi (revision 119818)
+++ gcc/doc/invoke.texi (working copy)
@@ -3215,7 +3215,7 @@ declaration. This warning is issued eve
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.
Index: gcc/cp/decl.c
===================================================================
--- gcc/cp/decl.c (revision 119818)
+++ gcc/cp/decl.c (working copy)
@@ -10570,9 +10570,40 @@ start_preparsed_function (tree decl1, tr
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))
+ {
+ bool anon;
+ tree context;
+
+ /* Check whether DECL1 is in an anonymous
+ namespace. */
+ anon = false;
+ for (context = DECL_CONTEXT (decl1);
+ context;
+ context = DECL_CONTEXT (context))
+ {
+ if (TREE_CODE (context) == NAMESPACE_DECL
+ && DECL_NAME (context) == NULL_TREE)
+ {
+ anon = true;
+ break;
+ }
+ }
+
+ if (!anon)
+ warning (OPT_Wmissing_declarations,
+ "no previous declaration for %q+D", decl1);
+ }
+
+ decl1 = olddecl;
+ }
}
else
{
Index: gcc/c.opt
===================================================================
--- gcc/c.opt (revision 119818)
+++ gcc/c.opt (working copy)
@@ -260,7 +260,7 @@ C ObjC C++ ObjC++ Var(warn_missing_brace
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: gcc/testsuite/g++.dg/warn/Wmissing-declarations-1.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Wmissing-declarations-1.C (revision 0)
+++ gcc/testsuite/g++.dg/warn/Wmissing-declarations-1.C (revision 0)
@@ -0,0 +1,35 @@
+// { dg-options "-Wmissing-declarations" }
+
+int fn1() { } // { dg-warning "no previous declaration" }
+namespace ns {
+ int fn2() { } // { dg-warning "no previous declaration" }
+}
+namespace {
+ int fn3() { }
+}
+static int fn4() { }
+
+int fn5();
+namespace ns {
+ int fn6();
+}
+
+int fn5() { }
+namespace ns {
+ int fn6() { }
+}
+
+class c {
+ int cfn1() { }
+ static int cfn2() { }
+ int cfn3();
+ static int cfn4();
+};
+
+int c::cfn3() { }
+int c::cfn4() { }
+
+static struct {
+ int sfn1() { }
+ static int sfn2() { }
+} s;