From 3c393a2c012f7f5f1c3df7d6d7b17a9cf992af17 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Tue, 11 Dec 2018 18:53:03 +0000 Subject: [PATCH] PR c++/86608 - reading constexpr volatile variable. * constexpr.c (potential_constant_expression_1): Check want_rval instead of checking if we have a decl. * decl2.c (decl_maybe_constant_var_p): Don't consider volatile constexpr variables as maybe constant. * g++.dg/cpp0x/constexpr-volatile2.C: New test. * g++.dg/cpp0x/pr65327.C: Add dg-error. From-SVN: r267030 --- gcc/cp/ChangeLog | 8 ++++++++ gcc/cp/constexpr.c | 5 +++-- gcc/cp/decl2.c | 2 +- gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/g++.dg/cpp0x/constexpr-volatile2.C | 13 +++++++++++++ gcc/testsuite/g++.dg/cpp0x/pr65327.C | 2 +- 6 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-volatile2.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 3e31ba60d0c..3e499221582 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,11 @@ +2018-12-11 Marek Polacek + + PR c++/86608 - reading constexpr volatile variable. + * constexpr.c (potential_constant_expression_1): Check want_rval + instead of checking if we have a decl. + * decl2.c (decl_maybe_constant_var_p): Don't consider volatile + constexpr variables as maybe constant. + 2018-12-11 Paolo Carlini * decl.c (grokvardecl): Add location_t parameter and use it diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 1c844a8c2ef..44db38029bf 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -5476,10 +5476,11 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, available, so we don't bother with switch tracking. */ return true; - if (TREE_THIS_VOLATILE (t) && !DECL_P (t)) + if (TREE_THIS_VOLATILE (t) && want_rval) { if (flags & tf_error) - error_at (loc, "expression %qE has side-effects", t); + error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue " + "%qE with type %qT", t, TREE_TYPE (t)); return false; } if (CONSTANT_CLASS_P (t)) diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index a8bf28a0cd9..1b3e758b625 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -4313,7 +4313,7 @@ decl_maybe_constant_var_p (tree decl) tree type = TREE_TYPE (decl); if (!VAR_P (decl)) return false; - if (DECL_DECLARED_CONSTEXPR_P (decl)) + if (DECL_DECLARED_CONSTEXPR_P (decl) && !TREE_THIS_VOLATILE (decl)) return true; if (DECL_HAS_VALUE_EXPR_P (decl)) /* A proxy isn't constant. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 8980dd2b2f1..c6730c234b6 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2018-12-11 Marek Polacek + + PR c++/86608 - reading constexpr volatile variable. + * g++.dg/cpp0x/constexpr-volatile2.C: New test. + * g++.dg/cpp0x/pr65327.C: Add dg-error. + 2018-12-11 Paolo Carlini * g++.dg/pr53037-4.C: Test the first two locations too. diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-volatile2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-volatile2.C new file mode 100644 index 00000000000..0def8d73731 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-volatile2.C @@ -0,0 +1,13 @@ +// PR c++/86608 +// { dg-do compile { target c++11 } } + +template struct X {}; + +int +main () +{ + static constexpr volatile int a = 3; + constexpr volatile int b = 2; + return (sizeof(X) // { dg-error "lvalue-to-rvalue conversion of a volatile lvalue" } + + sizeof(X)); // { dg-error "lvalue-to-rvalue conversion of a volatile lvalue" } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/pr65327.C b/gcc/testsuite/g++.dg/cpp0x/pr65327.C index c6cefaba692..5176b3c3204 100644 --- a/gcc/testsuite/g++.dg/cpp0x/pr65327.C +++ b/gcc/testsuite/g++.dg/cpp0x/pr65327.C @@ -15,4 +15,4 @@ constexpr volatile int bar () { return i; -} +} // { dg-error "lvalue-to-rvalue conversion of a volatile lvalue" } -- 2.30.2