From: Nathan Sidwell Date: Tue, 21 Dec 2004 17:54:25 +0000 (+0000) Subject: re PR c++/14075 (("foo") accepted as char[] initializer) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=7a8380aebb80b0f567b218204d6e926105b9d305;p=gcc.git re PR c++/14075 (("foo") accepted as char[] initializer) 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): Add parens, if needed. testsuite: PR c++/14075 * g++.dg/init/string1.C: New. From-SVN: r92464 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index c90f5af67ed..b0c25a7b5d7 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,12 @@ 2004-12-21 Nathan Sidwell + 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): Add parens, if needed. + * cp-tree.def (TEMPLATE_TYPE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM, TYPE_OF_TYPE, TYPENAME_TYPE): Reorder for better code efficiency. diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 82cd13f418f..c6d47c909f4 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -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) \ diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index dbaf23e23c2..515bb2c5041 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -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; } } diff --git a/gcc/cp/error.c b/gcc/cp/error.c index 8599616a3aa..ec332f2273b 100644 --- a/gcc/cp/error.c +++ b/gcc/cp/error.c @@ -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; diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 19c6d5d8910..ab1c028ff5d 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -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; } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 2633376c828..640674a830b 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2004-12-21 Nathan Sidwell + PR c++/14075 + * g++.dg/init/string1.C: New. + PR c++/18975 * g++.dg/other/synth1.C: New.