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++ PATCH] [PR14181] Emit good diagnostic for invalid new expression


Hello,

this code:
-----------------
int* a = new (int)[10];
-----------------

used to be accepted by GCC 3.3.x (unless pedantic). It's now rejected by the
new parser, but the current error message is just a parse error:
-----------------
error: expected `;' before '[' token
-----------------

With this patch, we detect this situation and emit this message:
-----------------
error: use of parenthesis around the type name is not allowed while allocating
an array

-----------------

Tested on i686-pc-linux-gnu, OK for mainline? OK for 3.4 (already 3 users
reported this issue and it's a really trivial patch)?

Giovanni Bajo



cp/
2004-02-14  Giovanni Bajo  <giovannibajo@gcc.gnu.org>

        PR c++/14181
        * parser.c (cp_parser_new_expression): Parse an ill-formed
        direct-new-declared after a parenthesized type-id to emit good
        diagnostic.

testsuite/
2004-02-14  Giovanni Bajo  <giovannibajo@gcc.gnu.org>

        PR c++/14181
        * g++.dg/parser/new2.C: New test.


Index: parser.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/parser.c,v
retrieving revision 1.173
diff -c -3 -p -r1.173 parser.c
*** parser.c 13 Feb 2004 16:11:39 -0000 1.173
--- parser.c 18 Feb 2004 10:47:26 -0000
*************** cp_parser_new_expression (cp_parser* par
*** 4531,4536 ****
--- 4531,4545 ----
        type = cp_parser_type_id (parser);
        /* Look for the closing `)'.  */
        cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
+       /* There should not be a direct-new-declarator in this production,
+          but GCC used to allowed this, so we check and emit a sensible error
+   message for this case.  */
+       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
+  {
+    error ("use of parenthesis around the type name is not allowed "
+   "while allocating an array");
+    cp_parser_direct_new_declarator (parser);
+  }
      }
    /* Otherwise, there must be a new-type-id.  */
    else
Index: htdocs/gcc-3.4/changes.html
===================================================================
RCS file: /cvs/gcc/wwwdocs/htdocs/gcc-3.4/changes.html,v
retrieving revision 1.99
diff -c -3 -p -r1.99 changes.html
*** htdocs/gcc-3.4/changes.html 12 Feb 2004 19:46:44 -0000 1.99
--- htdocs/gcc-3.4/changes.html 18 Feb 2004 10:55:07 -0000
***************
*** 524,529 ****
--- 524,536 ----
   typedef Q&lt;0&gt; R;
   template class R;      // error, no typedef name here
   template class Q&lt;0&gt;;   // OK</pre></li>
+
+     <li>When allocating an array with a new expression, GCC used to allow
+     parenthesis around the type name. This is actually ill-formed and it is
+     now rejected:
+     <pre>
+     int* a = new (int)[10];    // error, not accepted anymore
+     int* a = new int[10];      // OK</pre></li>
    </ul>

  <h3>Objective-C</h3>



// { dg-do compile }
// Contributed by David Daney <daney at gcc dot gnu dot org>
// PR c++/14181: Cryptic error message for ill-formed new expressions

void f1(void)
{
   (void)new (char*)[10];  // { dg-error "use of parenthesis" }
   (void)new char*[10];
}






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