From cb0a83f3437be73075fba17db68abbf240d17a36 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Tue, 13 Aug 2019 15:05:48 +0000 Subject: [PATCH] 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. * g++.dg/cpp0x/nullptr42.C: New test. From-SVN: r274382 --- gcc/cp/ChangeLog | 7 +++++++ gcc/cp/call.c | 5 ++--- gcc/cp/decl.c | 4 +++- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/cpp0x/nullptr42.C | 18 ++++++++++++++++++ 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/nullptr42.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 5f1dff89c0d..28bc2a4cec4 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2019-08-13 Marek Polacek + + 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-13 Marek Polacek * cp-tree.h (DECL_MUTABLE_P): Use FIELD_DECL_CHECK. diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 61334a16248..01a25ad6e1e 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -529,9 +529,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; diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index b8806e4628d..d91f25183fb 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -13177,7 +13177,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/ChangeLog b/gcc/testsuite/ChangeLog index a24386608a4..b8814989545 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2019-08-13 Marek Polacek + + PR c++/90473 - wrong code with nullptr in default argument. + * g++.dg/cpp0x/nullptr42.C: New test. + 2019-08-13 Olivier Hainque * gnat.dg/casesi.ad[bs], test_casesi.adb: New test. diff --git a/gcc/testsuite/g++.dg/cpp0x/nullptr42.C b/gcc/testsuite/g++.dg/cpp0x/nullptr42.C new file mode 100644 index 00000000000..2fb628df6d7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/nullptr42.C @@ -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 (); +} -- 2.30.2