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] Fix -Wmissing-braces (PR c++/20175)


Hi!

The PR c++/19755 fix introduced this regression where we now warn about
missing braces even if a char (resp. wchar_t) array is initialized
by a string (resp. wide string) literal, while the standard in
[dcl.init.string]/1 for this states that the (wide) string literal is
only optionally enclosed in braces.
This worked properly in 3.2 (and I assume 3.3).

Ok for trunk/3.4?

2005-02-24  Jakub Jelinek  <jakub@redhat.com>

	PR c++/20175
	* decl.c (reshape_init): Don't warn about missing braces if STRING_CST
	initializes a char/wchar_t array.

	* g++.dg/warn/Wbraces2.C: New test.

--- gcc/cp/decl.c.jj	2005-02-24 10:29:52.000000000 +0100
+++ gcc/cp/decl.c	2005-02-24 12:44:35.376652158 +0100
@@ -4176,6 +4176,7 @@ reshape_init (tree type, tree *initp)
   tree old_init_value;
   tree new_init;
   bool brace_enclosed_p;
+  bool string_init_p;
 
   old_init = *initp;
   old_init_value = (TREE_CODE (*initp) == TREE_LIST
@@ -4239,6 +4240,7 @@ reshape_init (tree type, tree *initp)
       return old_init;
     }
 
+  string_init_p = false;
   if (TREE_CODE (old_init_value) == STRING_CST
       && TREE_CODE (type) == ARRAY_TYPE
       && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
@@ -4253,6 +4255,7 @@ reshape_init (tree type, tree *initp)
       /* Move past the initializer.  */
       *initp = TREE_CHAIN (old_init);
       TREE_CHAIN (old_init) = NULL_TREE;
+      string_init_p = true;
     }
   else
     {
@@ -4359,7 +4362,7 @@ reshape_init (tree type, tree *initp)
     {
       if (brace_enclosed_p)
 	error ("too many initializers for %qT", type);
-      else if (warn_missing_braces)
+      else if (warn_missing_braces && !string_init_p)
 	warning ("missing braces around initializer");
     }
 
--- gcc/testsuite/g++.dg/warn/Wbraces2.C.jj	2005-02-24 12:54:02.894548808 +0100
+++ gcc/testsuite/g++.dg/warn/Wbraces2.C	2005-02-24 12:53:58.162391650 +0100
@@ -0,0 +1,15 @@
+// PR c++/20175
+// { dg-options "-Wmissing-braces" }
+int a[2][2] = { 0, 1, 2, 3 };			 // { dg-warning "missing braces" }
+int b[2][2] = { { 0, 1 }, { 2, 3 } };
+int c[2][2] = { { { 0 }, 1 }, { 2, 3 } };	 // { dg-error "brace-enclosed" }
+struct S { char s[6]; int i; };
+S d = { "hello", 1 };
+S e = { { "hello" }, 1 };
+S f = { { { "hello" } }, 1 };			 // { dg-error "brace-enclosed" }
+S g = { 'h', 'e', 'l', 'l', 'o', '\0', 1 };	 // { dg-warning "missing braces" }
+struct T { wchar_t s[6]; int i; };
+T i = { L"hello", 1 };
+T j = { { L"hello" }, 1 };
+T k = { { { L"hello" } }, 1 };			 // { dg-error "brace-enclosed" }
+T l = { L'h', L'e', L'l', L'l', L'o', L'\0', 1 };// { dg-warning "missing braces" }

	Jakub


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