re PR c++/14075 (("foo") accepted as char[] initializer)
authorNathan Sidwell <nathan@codesourcery.com>
Tue, 21 Dec 2004 17:54:25 +0000 (17:54 +0000)
committerNathan Sidwell <nathan@gcc.gnu.org>
Tue, 21 Dec 2004 17:54:25 +0000 (17:54 +0000)
cp:
PR c++/14075
* decl.c (check_initializer): Check string initializer of array is
not parenthesized.
* cp-tree.h (PAREN_STRING_LITERAL_P): New.
* semantics.c (finish_parenthesized_expr): Mark a STRING_CST.
* error.c (dump_expr): <STRING_CST case> Add parens, if needed.
testsuite:
PR c++/14075
* g++.dg/init/string1.C: New.

From-SVN: r92464

gcc/cp/ChangeLog
gcc/cp/cp-tree.h
gcc/cp/decl.c
gcc/cp/error.c
gcc/cp/semantics.c
gcc/testsuite/ChangeLog

index c90f5af67ed797afea5f9aee8f8b6176fc4619d2..b0c25a7b5d75d617fc29ea3a7aad0a84a3bfee0c 100644 (file)
@@ -1,5 +1,12 @@
 2004-12-21  Nathan Sidwell  <nathan@codesourcery.com>
 
+       PR c++/14075
+       * decl.c (check_initializer): Check string initializer of array is
+       not parenthesized.
+       * cp-tree.h (PAREN_STRING_LITERAL_P): New.
+       * semantics.c (finish_parenthesized_expr): Mark a STRING_CST.
+       * error.c (dump_expr): <STRING_CST case> Add parens, if needed.
+
        * cp-tree.def (TEMPLATE_TYPE_PARM,
        BOUND_TEMPLATE_TEMPLATE_PARM, TYPE_OF_TYPE, TYPENAME_TYPE): Reorder 
        for better code efficiency.
index 82cd13f418fd622c659abf4402d2e3d8975a5926..c6d47c909f40637a0c3e12071a551516636b22ad 100644 (file)
@@ -42,6 +42,7 @@ struct diagnostic_context;
       CLEANUP_P (in TRY_BLOCK)
       AGGR_INIT_VIA_CTOR_P (in AGGR_INIT_EXPR)
       PTRMEM_OK_P (in ADDR_EXPR, OFFSET_REF)
+      PAREN_STRING_LITERAL (in STRING_CST)
       DECL_PRETTY_FUNCTION_P (in VAR_DECL)
       KOENIG_LOOKUP_P (in CALL_EXPR)
       STATEMENT_LIST_NO_SCOPE (in STATEMENT_LIST).
@@ -2248,6 +2249,12 @@ struct lang_decl GTY(())
    should be performed at instantiation time.  */
 #define KOENIG_LOOKUP_P(NODE) TREE_LANG_FLAG_0 (CALL_EXPR_CHECK (NODE))
 
+/* Indicates whether a string literal has been parenthesized. Such
+   usages are disallowed in certain circumstances.  */
+
+#define PAREN_STRING_LITERAL_P(NODE) \
+  TREE_LANG_FLAG_0 (STRING_CST_CHECK (NODE))
+
 /* Nonzero if this AGGR_INIT_EXPR provides for initialization via a
    constructor call, rather than an ordinary function call.  */
 #define AGGR_INIT_VIA_CTOR_P(NODE) \
index dbaf23e23c2614d0167883678a31fa1e3b825a6d..515bb2c504144ed7a32f45b63ecb7d2a6a2544bd 100644 (file)
@@ -4596,6 +4596,12 @@ check_initializer (tree decl, tree init, int flags, tree *cleanup)
          if (TREE_CODE (init) != TREE_VEC)
            {
              init_code = store_init_value (decl, init);
+             if (pedantic && TREE_CODE (type) == ARRAY_TYPE
+                 && DECL_INITIAL (decl)
+                 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
+                 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
+               warning ("array %qD initialized by parenthesized string literal %qE",
+                        decl, DECL_INITIAL (decl));
              init = NULL;
            }
        }
index 8599616a3aa296bf08b5294fe703bd824988af09..ec332f2273ba1696e6a16847443352e2a9ef1acd 100644 (file)
@@ -1286,8 +1286,15 @@ dump_expr (tree t, int flags)
       dump_decl (t, (flags & ~TFF_DECL_SPECIFIERS) | TFF_NO_FUNCTION_ARGUMENTS);
       break;
 
-    case INTEGER_CST:
     case STRING_CST:
+      if (PAREN_STRING_LITERAL_P (t))
+       pp_cxx_left_paren (cxx_pp);
+      pp_c_constant (pp_c_base (cxx_pp), t);
+      if (PAREN_STRING_LITERAL_P (t))
+       pp_cxx_right_paren (cxx_pp);
+      break;
+      
+    case INTEGER_CST:
     case REAL_CST:
        pp_c_constant (pp_c_base (cxx_pp), t);
       break;
index 19c6d5d89107251ac1e2e4f5e495503fcd4aa043..ab1c028ff5d53885d82f2f0fe49f5ba674bf94cf 100644 (file)
@@ -1272,6 +1272,10 @@ finish_parenthesized_expr (tree expr)
     /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
        enclosed in parentheses.  */
     PTRMEM_OK_P (expr) = 0;
+  
+  if (TREE_CODE (expr) == STRING_CST)
+    PAREN_STRING_LITERAL_P (expr) = 1;
+  
   return expr;
 }
 
index 2633376c828dc3fcc66e8d42e398f67fad4e153e..640674a830b753f527998f9e2de9107f20d2a966 100644 (file)
@@ -1,5 +1,8 @@
 2004-12-21  Nathan Sidwell  <nathan@codesourcery.com>
 
+       PR c++/14075
+       * g++.dg/init/string1.C: New.
+
        PR c++/18975
        * g++.dg/other/synth1.C: New.