diff options
author | mpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4> | 2019-08-15 18:33:43 +0000 |
---|---|---|
committer | mpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4> | 2019-08-15 18:33:43 +0000 |
commit | 81380ecb806001bc1d80132dfd307c1415e838d4 (patch) | |
tree | b2398b07cc0c7bded5829a7ab5d36bb4d7f15562 | |
parent | 900edf070bc6bbd08291abb284f35433ea34ccb1 (diff) |
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.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/gcc-9-branch@274546 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r-- | gcc/cp/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/cp/call.c | 5 | ||||
-rw-r--r-- | gcc/cp/decl.c | 4 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp0x/nullptr42.C | 18 |
4 files changed, 30 insertions, 4 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 42952c68f23..6e9cb982478 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -13,6 +13,13 @@ * 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 diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 9e22a146a16..c728a9abedc 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -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; diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 83a7baac470..92e58af8c14 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -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 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 (); +} |