This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PATCH RFA: -Wc++-compat warning about duplicate decls


In C, it's OK to write
  int i;
  int i;
In C++, it is not.  This patch adds a -Wc++-compat warning for this
case.  This patch requires approval from the C frontend maintainers.

This patch also fixes the single remaining case in the gcc sources where
duplicate decls occurs.  This is a mildly interesting case resulting
from the fact that options listed in the various *.opt files are sorted
by name.  When an option has a VarExists clause, but the variable in
question has not been seen, the option code emits a definition for the
variable.  It also does see when the option has an Init claus, even if
the variable was seen previously.  Because the options in
config/linux.opt are not in alphabetical order, the VarExists is seen
first, which leads to a duplicate definition of the variable.  I fixed
this by swapping the order of the options.

Bootstrapped and tested on x86_64-unknown-linux-gnu.  OK for mainline?

Ian


gcc/ChangeLog:

2009-06-19  Ian Lance Taylor  <iant@google.com>

	* config/linux.opt: Put mglibc ahead of muclibc.

	* c-decl.c (diagnose_mismatched_decls): Add -Wc++-compat warning
	for duplicate decls.

testsuite/ChangeLog:

2009-06-19  Ian Lance Taylor  <iant@google.com>

	* gcc.dg/Wcxx-compat-19.c: New testcase.


Index: c-decl.c
===================================================================
--- c-decl.c	(revision 148719)
+++ c-decl.c	(working copy)
@@ -1991,6 +1991,18 @@ diagnose_mismatched_decls (tree newdecl,
 
 	  return false;
 	}
+
+      /* C++ does not permit a decl to appear multiple times at file
+	 scope.  */
+      if (warn_cxx_compat
+	  && DECL_FILE_SCOPE_P (newdecl)
+	  && !DECL_EXTERNAL (newdecl)
+	  && !DECL_EXTERNAL (olddecl))
+	warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
+			      OPT_Wc___compat,
+			      ("duplicate declaration of %qD is "
+			       "invalid in C++"),
+			      newdecl);
     }
 
   /* warnings */
Index: config/linux.opt
===================================================================
--- config/linux.opt	(revision 148719)
+++ config/linux.opt	(working copy)
@@ -1,6 +1,6 @@
 ; Processor-independent options for GNU/Linux.
 ;
-; Copyright (C) 2006, 2007 Free Software Foundation, Inc.
+; Copyright (C) 2006, 2007, 2009 Free Software Foundation, Inc.
 ; Contributed by CodeSourcery.
 ;
 ; This file is part of GCC.
@@ -19,10 +19,10 @@
 ; along with GCC; see the file COPYING3.  If not see
 ; <http://www.gnu.org/licenses/>.
 
-muclibc
-Target RejectNegative Report Mask(UCLIBC) Var(linux_uclibc) Init(UCLIBC_DEFAULT ? OPTION_MASK_UCLIBC : 0)
-Use uClibc instead of GNU libc
-
 mglibc
-Target RejectNegative Report InverseMask(UCLIBC, GLIBC) Var(linux_uclibc) VarExists
+Target RejectNegative Report InverseMask(UCLIBC, GLIBC) Var(linux_uclibc) Init(UCLIBC_DEFAULT ? OPTION_MASK_UCLIBC : 0)
 Use GNU libc instead of uClibc
+
+muclibc
+Target RejectNegative Report Mask(UCLIBC) Var(linux_uclibc) VarExists
+Use uClibc instead of GNU libc
Index: testsuite/gcc.dg/Wcxx-compat-19.c
===================================================================
--- testsuite/gcc.dg/Wcxx-compat-19.c	(revision 0)
+++ testsuite/gcc.dg/Wcxx-compat-19.c	(revision 0)
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-Wc++-compat" } */
+int v1;			/* { dg-message "previous declaration" } */
+int v1;			/* { dg-warning "invalid in C\[+\]\[+\]" } */
+int v2;			/* { dg-message "previous declaration" } */
+int v2 = 1;		/* { dg-warning "invalid in C\[+\]\[+\]" } */
+extern int v3;
+int v3;			/* { dg-message "previous declaration" } */
+int v3 = 1;		/* { dg-warning "invalid in C\[+\]\[+\]" } */
+extern int v4;
+int v4 = 1;
+static int v5;		/* { dg-message "previous declaration" } */
+static int v5;		/* { dg-warning "invalid in C\[+\]\[+\]" } */
+static int v6;		/* { dg-message "previous declaration" } */
+static int v6 = 1;	/* { dg-warning "invalid in C\[+\]\[+\]" } */
+int v7;
+extern int v7;
+int v8 = 1;
+extern int v8;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]