]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR c++/90473 - wrong code with nullptr in default argument.
authorMarek Polacek <polacek@redhat.com>
Thu, 15 Aug 2019 18:33:43 +0000 (18:33 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Thu, 15 Aug 2019 18:33:43 +0000 (18:33 +0000)
* call.c (null_ptr_cst_p): Update quote from the standard.
* decl.c (check_default_argument): Don't return nullptr when the arg
has side-effects.

* g++.dg/cpp0x/nullptr42.C: New test.

From-SVN: r274546

gcc/cp/ChangeLog
gcc/cp/call.c
gcc/cp/decl.c
gcc/testsuite/g++.dg/cpp0x/nullptr42.C [new file with mode: 0644]

index 42952c68f2362e4b59601aa51fc56aee6bad7593..6e9cb982478e6f461af814324fa16c45ed31b87e 100644 (file)
        * typeck.c (cp_build_binary_op): Use same_type_p instead of comparing
        the types directly.
 
+       2019-08-13  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/90473 - wrong code with nullptr in default argument.
+       * call.c (null_ptr_cst_p): Update quote from the standard.
+       * decl.c (check_default_argument): Don't return nullptr when the arg
+       has side-effects.
+
 2019-08-14  Martin Sebor  <msebor@redhat.com>
 
        Backported from mainline
index 9e22a146a16af3888e28cb603c259417f8f9685b..c728a9abedcdd9460947d0a8e86ab10ae61b743b 100644 (file)
@@ -530,9 +530,8 @@ null_ptr_cst_p (tree t)
 
   /* [conv.ptr]
 
-     A null pointer constant is an integral constant expression
-     (_expr.const_) rvalue of integer type that evaluates to zero or
-     an rvalue of type std::nullptr_t. */
+     A null pointer constant is an integer literal ([lex.icon]) with value
+     zero or a prvalue of type std::nullptr_t.  */
   if (NULLPTR_TYPE_P (type))
     return true;
 
index 83a7baac4701eb9df8401b763b18332d71747748..92e58af8c144e31abd13668da7b3e77de365d0f7 100644 (file)
@@ -13043,7 +13043,9 @@ check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
   /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
      the call sites.  */
   if (TYPE_PTR_OR_PTRMEM_P (decl_type)
-      && null_ptr_cst_p (arg))
+      && null_ptr_cst_p (arg)
+      /* Don't lose side-effects as in PR90473.  */
+      && !TREE_SIDE_EFFECTS (arg))
     return nullptr_node;
 
   /* [dcl.fct.default]
diff --git a/gcc/testsuite/g++.dg/cpp0x/nullptr42.C b/gcc/testsuite/g++.dg/cpp0x/nullptr42.C
new file mode 100644 (file)
index 0000000..2fb628d
--- /dev/null
@@ -0,0 +1,18 @@
+// PR c++/90473 - wrong code with nullptr in default argument.
+// { dg-do run { target c++11 } }
+
+int g;
+void f() { g++; }
+
+void fn1 (void* p = (f(), nullptr)) { }
+void fn2 (int p = (f(), 0)) { }
+
+int main()
+{
+  fn1 ();
+  if (g != 1)
+    __builtin_abort ();
+  fn2 ();
+  if (g != 2)
+    __builtin_abort ();
+}