This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [C++ Patch] PR 31988
- From: Paolo Carlini <pcarlini at suse dot de>
- To: Mark Mitchell <mark at codesourcery dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Fri, 26 Oct 2007 13:20:06 +0200
- Subject: Re: [C++ Patch] PR 31988
- References: <46F479D7.70308@suse.de> <47215901.2070102@codesourcery.com>
Hi Mark,
> We can make the code tidier than your version, though:
>
> if (!args || args == void_list_node)
> {
> if (!same_type_p (TREE_VALUE (args), size_type_node)
> {
> e = 2;
> ...
> }
> else if (TREE_PURPOSE (args))
> {
> /* [basic.stc.dynamic.allocation]
>
> The first parameter shall not have an associated default
> argument. */
> error (...);
> /* Throw away the default argument. */
> TREE_PURPOSE (args) = NULL_TREE;
> }
> }
>
> This version avoids checking !args, etc., twice.
>
Good point. Unfortunately, the above, as-is, gets the logic slightly
wrong. I tweaked it as in the below, which indeed seems a definite
improvement on my first try. Tested x86_64-linux.
Ok for mainline?
Paolo.
///////////////////////
cp/
2007-10-26 Paolo Carlini <pcarlini@suse.de>
PR c++/31988
* decl2.c (coerce_new_type): Do not allow a default argument for
the first parameter.
testsuite/
2007-10-26 Paolo Carlini <pcarlini@suse.de>
PR c++/31988
* g++.dg/init/new25.C: New.
Index: testsuite/g++.dg/init/new25.C
===================================================================
*** testsuite/g++.dg/init/new25.C (revision 0)
--- testsuite/g++.dg/init/new25.C (revision 0)
***************
*** 0 ****
--- 1,29 ----
+ // PR c++/31988
+ #include <new>
+
+ class C
+ {
+ public:
+ void* operator new(std::size_t = 32) throw (std::bad_alloc); // { dg-error "first parameter" }
+ void* operator new[](std::size_t = 32) throw (std::bad_alloc); // { dg-error "first parameter" }
+ void* operator new(std::size_t = 32, const std::nothrow_t&) throw(); // { dg-error "first parameter" }
+ void* operator new[](std::size_t = 32, const std::nothrow_t&) throw(); // { dg-error "first parameter" }
+ };
+
+ class D
+ {
+ public:
+ void* operator new(std::size_t,
+ const std::nothrow_t& = std::nothrow_t()) throw();
+ void* operator new[](std::size_t,
+ const std::nothrow_t& = std::nothrow_t()) throw();
+ };
+
+ class E
+ {
+ public:
+ void* operator new(std::size_t = 0,
+ const std::nothrow_t& = std::nothrow_t()) throw(); // { dg-error "first parameter" }
+ void* operator new[](std::size_t = 0,
+ const std::nothrow_t& = std::nothrow_t()) throw(); // { dg-error "first parameter" }
+ };
Index: cp/decl2.c
===================================================================
*** cp/decl2.c (revision 129646)
--- cp/decl2.c (working copy)
*************** coerce_new_type (tree type)
*** 1251,1262 ****
error ("%<operator new%> must return type %qT", ptr_type_node);
}
! if (!args || args == void_list_node
! || !same_type_p (TREE_VALUE (args), size_type_node))
{
e = 2;
- if (args && args != void_list_node)
- args = TREE_CHAIN (args);
pedwarn ("%<operator new%> takes type %<size_t%> (%qT) "
"as first parameter", size_type_node);
}
--- 1251,1281 ----
error ("%<operator new%> must return type %qT", ptr_type_node);
}
! if (args && args != void_list_node)
! {
! if (TREE_PURPOSE (args))
! {
! /* [basic.stc.dynamic.allocation]
!
! The first parameter shall not have an associated default
! argument. */
! error ("the first parameter of %<operator new%> cannot "
! "have a default argument");
! /* Throw away the default argument. */
! TREE_PURPOSE (args) = NULL_TREE;
! }
!
! if (!same_type_p (TREE_VALUE (args), size_type_node))
! {
! e = 2;
! args = TREE_CHAIN (args);
! pedwarn ("%<operator new%> takes type %<size_t%> (%qT) "
! "as first parameter", size_type_node);
! }
! }
! else
{
e = 2;
pedwarn ("%<operator new%> takes type %<size_t%> (%qT) "
"as first parameter", size_type_node);
}