From: Jason Merrill Date: Wed, 26 Feb 2020 18:03:23 +0000 (-0500) Subject: c++: Preserve location in maybe_constant_value. X-Git-Tag: releases/gcc-9.3.0~71 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b6678d67c4b7471c25130b6c60a9087e02f31179;p=thirdparty%2Fgcc.git c++: Preserve location in maybe_constant_value. If cxx_eval_outermost_constant_expr doesn't change the argument, we really shouldn't unshare it when we try to fold it again. gcc/cp/ChangeLog 2020-02-26 Jason Merrill PR c++/92852 * constexpr.c (maybe_constant_value): Don't unshare if the cached value is the same as the argument. --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 6f62352a18e0..ca4bc4cc1b50 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2020-02-26 Jason Merrill + + PR c++/92852 + * constexpr.c (maybe_constant_value): Don't unshare if the cached + value is the same as the argument. + 2020-02-14 Jakub Jelinek PR c++/61414 diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index f0d4cb5e8e77..a7728285dc51 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -5582,7 +5582,15 @@ maybe_constant_value (tree t, tree decl, bool manifestly_const_eval) if (cv_cache == NULL) cv_cache = hash_map::create_ggc (101); if (tree *cached = cv_cache->get (t)) - return unshare_expr_without_location (*cached); + { + r = *cached; + if (r != t) + { + r = unshare_expr_without_location (r); + protected_set_expr_location (r, EXPR_LOCATION (t)); + } + return r; + } r = cxx_eval_outermost_constant_expr (t, true, true, false, decl); gcc_checking_assert (r == t diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp48.C b/gcc/testsuite/g++.dg/cpp1z/decomp48.C index 35413c79a9da..3c50b02a6c24 100644 --- a/gcc/testsuite/g++.dg/cpp1z/decomp48.C +++ b/gcc/testsuite/g++.dg/cpp1z/decomp48.C @@ -18,7 +18,7 @@ f2 () { S v {1, 2}; auto& [s, t] = v; // { dg-warning "structured bindings only available with" "" { target c++14_down } } - return s; // { dg-warning "reference to local variable 'v' returned" } + return s; // { dg-warning "reference to local variable 'v' returned" "" { target *-*-* } .-1 } } int & @@ -33,7 +33,7 @@ f4 () { int a[3] = {1, 2, 3}; auto& [s, t, u] = a; // { dg-warning "structured bindings only available with" "" { target c++14_down } } - return s; // { dg-warning "reference to local variable 'a' returned" } + return s; // { dg-warning "reference to local variable 'a' returned" "" { target *-*-* } .-1 } } int & @@ -78,7 +78,7 @@ f10 () { S v {1, 2}; auto& [s, t] = v; // { dg-warning "structured bindings only available with" "" { target c++14_down } } - return &s; // { dg-warning "address of local variable 'v' returned" } + return &s; // { dg-warning "address of local variable 'v' returned" "" { target *-*-* } .-1 } } int * @@ -93,7 +93,7 @@ f12 () { int a[3] = {1, 2, 3}; auto& [s, t, u] = a; // { dg-warning "structured bindings only available with" "" { target c++14_down } } - return &s; // { dg-warning "address of local variable 'a' returned" } + return &s; // { dg-warning "address of local variable 'a' returned" "" { target *-*-* } .-1 } } int *