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 globals with anonymous type


The C++ frontend issues a warning for global variables defined with an
anonymous type.  This patch adds a similar warning to the C frontend
when using -Wc++-compat.  It also fixes the one case in the gcc
sources where this occurs, by giving the struct a name.

This patch requires approval from the C frontend maintainers and the
Fortran frontend maintainers.

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

Ian


gcc/ChangeLog:

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

	* c-decl.c (grokdeclarator): If -Wc++-compat, warn about a global
	variable with an anonymous type.

gcc/fortran/ChangeLog:

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

	* cpp.c (struct gfc_cpp_option_data): Give this struct, used for
	the global variable gfc_cpp_option, a name.

gcc/testsuite/ChangeLog:

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

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


Index: testsuite/gcc.dg/Wcxx-compat-16.c
===================================================================
--- testsuite/gcc.dg/Wcxx-compat-16.c	(revision 0)
+++ testsuite/gcc.dg/Wcxx-compat-16.c	(revision 0)
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-Wc++-compat" } */
+
+struct { int f1; } g1;		/* { dg-warning "C\[+\]\[+\]" } */
+static struct { int f2; } g2;
+struct s { int f3; } g3;
+union { int f4; } g4;		/* { dg-warning "C\[+\]\[+\]" } */
+static union { int f5; } g5;
+union u { int f6; } g6;
+enum { A } g7;			/* { dg-warning "C\[+\]\[+\]" } */
+static enum { B } g8;
+enum E { C } g9;
Index: c-decl.c
===================================================================
--- c-decl.c	(revision 148648)
+++ c-decl.c	(working copy)
@@ -5875,6 +5875,19 @@ grokdeclarator (const struct c_declarato
      name of a variable.  Thus, if it's known before this, die horribly.  */
     gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
 
+    if (warn_cxx_compat
+	&& TREE_CODE (decl) == VAR_DECL
+	&& TREE_PUBLIC (decl)
+	&& TREE_STATIC (decl)
+	&& (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
+	    || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
+	    || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
+	&& TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
+      warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
+		  ("non-local variable %qD with anonymous type is "
+		   "questionable in C++"),
+		  decl);
+
     return decl;
   }
 }
Index: fortran/cpp.c
===================================================================
--- fortran/cpp.c	(revision 148648)
+++ fortran/cpp.c	(working copy)
@@ -66,7 +66,7 @@ typedef struct gfc_cpp_macro_queue
 } gfc_cpp_macro_queue;
 static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue;
 
-struct
+struct gfc_cpp_option_data
 {
   /* Argument of -cpp, implied by SPEC;
      if NULL, preprocessing disabled.  */

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