re PR c++/31988 (new operator should not permit default first parameter)
authorPaolo Carlini <pcarlini@suse.de>
Fri, 26 Oct 2007 18:32:41 +0000 (18:32 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Fri, 26 Oct 2007 18:32:41 +0000 (18:32 +0000)
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.

From-SVN: r129657

gcc/cp/ChangeLog
gcc/cp/decl2.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/init/new25.C [new file with mode: 0644]

index 191836d5da6559d9d67f8e29e75740cceffcfb96..99a2bc57538b8c75d84559bcbed7edc2b969a77d 100644 (file)
@@ -1,3 +1,9 @@
+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.
+
 2007-10-26  Douglas Gregor  <doug.gregor@gmail.com>
 
        PR c++/33839
index ce5e52816edce7a2154c4c049e311d9e9df88c25..0a978344fa13ddee00f85c9da7e9a2aa90878326 100644 (file)
@@ -1251,15 +1251,33 @@ coerce_new_type (tree type)
       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))
+  if (args && args != void_list_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);
+      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);
+       }
     }
+  else
+    e = 2;
+
+  if (e == 2)
+    pedwarn ("%<operator new%> takes type %<size_t%> (%qT) "
+            "as first parameter", size_type_node);
+
   switch (e)
   {
     case 2:
index 1257d0b1348b96d3080308aba1ca6953e3947816..02339a0c64faf196d712304c7e17ed1ed4ca7623 100644 (file)
@@ -1,3 +1,8 @@
+2007-10-26  Paolo Carlini  <pcarlini@suse.de>
+
+       PR c++/31988
+       * g++.dg/init/new25.C: New.
+
 2007-10-26  Douglas Gregor  <doug.gregor@gmail.com>
 
        * g++.dg/cpp0x/pr33839.C: New.
diff --git a/gcc/testsuite/g++.dg/init/new25.C b/gcc/testsuite/g++.dg/init/new25.C
new file mode 100644 (file)
index 0000000..2d447f8
--- /dev/null
@@ -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" }
+};