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]

[C++] Fix 14075


This patch fixes 14075, where we accepted a parenthesized string
literal as an initializer.  As with the C front end, we're only
pick about it when being pedantic.

booted & tested on i686-pc-linux-gnu.

nathan
--
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk

2004-12-16  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.

Index: cp/cp-tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-tree.h,v
retrieving revision 1.1081
diff -c -3 -p -r1.1081 cp-tree.h
*** cp/cp-tree.h	16 Dec 2004 11:03:17 -0000	1.1081
--- cp/cp-tree.h	16 Dec 2004 15:12:59 -0000
*************** struct diagnostic_context;
*** 42,47 ****
--- 42,48 ----
        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).
*************** struct lang_decl GTY(())
*** 2243,2248 ****
--- 2244,2255 ----
     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: cp/decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/decl.c,v
retrieving revision 1.1342
diff -c -3 -p -r1.1342 decl.c
*** cp/decl.c	16 Dec 2004 11:03:21 -0000	1.1342
--- cp/decl.c	16 Dec 2004 15:13:22 -0000
*************** check_initializer (tree decl, tree init,
*** 4596,4601 ****
--- 4596,4607 ----
  	  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: cp/error.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/error.c,v
retrieving revision 1.273
diff -c -3 -p -r1.273 error.c
*** cp/error.c	29 Nov 2004 20:10:11 -0000	1.273
--- cp/error.c	16 Dec 2004 15:13:26 -0000
*************** dump_expr (tree t, int flags)
*** 1288,1295 ****
        dump_decl (t, (flags & ~TFF_DECL_SPECIFIERS) | TFF_NO_FUNCTION_ARGUMENTS);
        break;
  
-     case INTEGER_CST:
      case STRING_CST:
      case REAL_CST:
         pp_c_constant (pp_c_base (cxx_pp), t);
        break;
--- 1288,1302 ----
        dump_decl (t, (flags & ~TFF_DECL_SPECIFIERS) | TFF_NO_FUNCTION_ARGUMENTS);
        break;
  
      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: cp/semantics.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/semantics.c,v
retrieving revision 1.452
diff -c -3 -p -r1.452 semantics.c
*** cp/semantics.c	8 Dec 2004 08:35:45 -0000	1.452
--- cp/semantics.c	16 Dec 2004 15:13:32 -0000
*************** finish_parenthesized_expr (tree expr)
*** 1272,1277 ****
--- 1272,1281 ----
      /* [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;
  }
  
// { dg-do compile }

// Copyright (C) 2004 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 16 Dec 2004 <nathan@codesourcery.com>

// PR 14075.  Failure to diagnose parethesized string literal
// Origin:  porten@kde.org

char a1[] = "one";
char a2[] = ("one"); // { dg-error "parenthesized string literal" "" }

char a3[4] = "one";
char a4[4] = ("one"); // { dg-error "parenthesized string literal" "" }

char a5[] = {"one"};
char a6[] = {("one")}; // { dg-error "parenthesized string literal" "" }

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