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 uninitialized const variables


C++ requires that const variables be initialized.  C does not--it
permits them to be initialized to zero by default.  This patch
improves the -Wc++-compat warning to warn about uninitialized const
variables.  It also fixes the two cases in the gcc source code where
such cases occur.  This patch requires approval from the C 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>

	* ggc-page.c (ggc_pch_write_object): Initialize emptyBytes.
	* sdbout.c (sdb_debug_hooks): Initialize non-SDB_DEBUGGING_INFO
	version.

	* c-decl.c (finish_decl): If -Wc++-compat, warn about
	uninitialized const.

gcc/testsuite/ChangeLog:

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

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


Index: c-decl.c
===================================================================
--- c-decl.c	(revision 148549)
+++ c-decl.c	(working copy)
@@ -4332,6 +4332,14 @@ finish_decl (tree decl, location_t init_
 	  push_cleanup (decl, cleanup, false);
 	}
     }
+
+  if (warn_cxx_compat
+      && TREE_CODE (decl) == VAR_DECL
+      && TREE_READONLY (decl)
+      && !DECL_EXTERNAL (decl)
+      && DECL_INITIAL (decl) == NULL_TREE)
+    warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
+		"uninitialized const %qD is invalid in C++", decl);
 }
 
 /* Given a parsed parameter declaration, decode it into a PARM_DECL.  */
Index: sdbout.c
===================================================================
--- sdbout.c	(revision 148549)
+++ sdbout.c	(working copy)
@@ -1697,7 +1697,37 @@ sdbout_init (const char *input_file_name
 #else  /* SDB_DEBUGGING_INFO */
 
 /* This should never be used, but its address is needed for comparisons.  */
-const struct gcc_debug_hooks sdb_debug_hooks;
+const struct gcc_debug_hooks sdb_debug_hooks =
+{
+  0,		/* init */
+  0,		/* finish */
+  0,		/* define */
+  0,		/* undef */
+  0,		/* start_source_file */
+  0,		/* end_source_file */
+  0,		/* begin_block */
+  0,		/* end_block */
+  0,		/* ignore_block */
+  0,		/* source_line */
+  0,		/* begin_prologue */
+  0,		/* end_prologue */
+  0,		/* end_epilogue */
+  0,		/* begin_function */
+  0,		/* end_function */
+  0,		/* function_decl */
+  0,		/* global_decl */
+  0,		/* type_decl */
+  0,		/* imported_module_or_decl */
+  0,		/* deferred_inline_function */
+  0,		/* outlining_inline_function */
+  0,		/* label */
+  0,		/* handle_pch */
+  0,		/* var_location */
+  0,		/* switch_text_section */
+  0,		/* set_name */
+  0		/* start_end_main_source_file */
+};
+
 
 #endif /* SDB_DEBUGGING_INFO */
 
Index: ggc-page.c
===================================================================
--- ggc-page.c	(revision 148549)
+++ ggc-page.c	(working copy)
@@ -2165,7 +2165,7 @@ ggc_pch_write_object (struct ggc_pch_dat
 		      size_t size, bool is_string ATTRIBUTE_UNUSED)
 {
   unsigned order;
-  static const char emptyBytes[256];
+  static const char emptyBytes[256] = { 0 };
 
   if (size < NUM_SIZE_LOOKUP)
     order = size_lookup[size];
Index: testsuite/gcc.dg/Wcxx-compat-17.c
===================================================================
--- testsuite/gcc.dg/Wcxx-compat-17.c	(revision 0)
+++ testsuite/gcc.dg/Wcxx-compat-17.c	(revision 0)
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "-Wc++-compat" } */
+const int v1;			/* { dg-warning "invalid in C\[+\]\[+\]" } */
+const char * const v2;		/* { dg-warning "invalid in C\[+\]\[+\]" } */
+struct s { int f1; int f2; };
+const struct s v3;		/* { dg-warning "invalid in C\[+\]\[+\]" } */
+const int v4 = 1;
+const char * const v5 = 0;
+const struct s v6 = { 0, 0 };
+const struct s v7 = { 0 };
+void
+f()
+{
+  const int v11;		/* { dg-warning "invalid in C\[+\]\[+\]" } */
+  const char * const v12;	/* { dg-warning "invalid in C\[+\]\[+\]" } */
+  const struct s v13;		/* { dg-warning "invalid in C\[+\]\[+\]" } */
+  const int v14 = 1;
+  const char * const v15 = 0;
+  const struct s v16 = { 0, 0 };
+  const struct s v17 = { 0 };
+}

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