re PR c++/20175 (Warnings are issued when initializing struct members with "strings")
authorJakub Jelinek <jakub@redhat.com>
Thu, 24 Feb 2005 21:21:28 +0000 (22:21 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Thu, 24 Feb 2005 21:21:28 +0000 (22:21 +0100)
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.

From-SVN: r95512

gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wbraces2.C [new file with mode: 0644]

index 0905273..bc9e1ea 100644 (file)
@@ -1,3 +1,9 @@
+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.
+
 2005-02-23  Mark Mitchell  <mark@codesourcery.com>
 
        PR c++/19878
index cea13b8..d767125 100644 (file)
@@ -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");
     }
 
index 6f590f3..1b1a9c4 100644 (file)
@@ -1,3 +1,8 @@
+2005-02-24  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/20175
+       * g++.dg/warn/Wbraces2.C: New test.
+
 2005-02-23  Mark Mitchell  <mark@codesourcery.com>
 
        PR c++/19878
diff --git a/gcc/testsuite/g++.dg/warn/Wbraces2.C b/gcc/testsuite/g++.dg/warn/Wbraces2.C
new file mode 100644 (file)
index 0000000..b51d5ca
--- /dev/null
@@ -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" }