From 50457b9e2151e9cc3d11da733bd885cf148a29a3 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Tue, 11 Nov 2014 17:34:12 +0000 Subject: [PATCH] re PR c++/63265 (Constexpr variables can trigger spurious compiler warnings) /cp 2014-11-11 Paolo Carlini PR c++/63265 * pt.c (tsubst_copy_and_build, case COND_EXPR): Maybe fold to constant the condition. /testsuite 2014-11-11 Paolo Carlini PR c++/63265 * g++.dg/cpp0x/constexpr-63265.C: New. From-SVN: r217361 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/pt.c | 7 +++++-- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/cpp0x/constexpr-63265.C | 19 +++++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-63265.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 9420908683d..a5513b6cf45 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2014-11-11 Paolo Carlini + + PR c++/63265 + * pt.c (tsubst_copy_and_build, case COND_EXPR): Maybe fold to + constant the condition. + 2014-11-10 Andi Kleen * semantics.c (finish_goto_stmt): Call check_no_cilk. diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index fa9652f748c..21d4039b623 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -15138,11 +15138,13 @@ tsubst_copy_and_build (tree t, case COND_EXPR: { tree cond = RECUR (TREE_OPERAND (t, 0)); + tree folded_cond = (maybe_constant_value + (fold_non_dependent_expr_sfinae (cond, tf_none))); tree exp1, exp2; - if (TREE_CODE (cond) == INTEGER_CST) + if (TREE_CODE (folded_cond) == INTEGER_CST) { - if (integer_zerop (cond)) + if (integer_zerop (folded_cond)) { ++c_inhibit_evaluation_warnings; exp1 = RECUR (TREE_OPERAND (t, 1)); @@ -15156,6 +15158,7 @@ tsubst_copy_and_build (tree t, exp2 = RECUR (TREE_OPERAND (t, 2)); --c_inhibit_evaluation_warnings; } + cond = folded_cond; } else { diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 3ffd69ae1fc..b301e055908 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2014-11-11 Paolo Carlini + + PR c++/63265 + * g++.dg/cpp0x/constexpr-63265.C: New. + 2014-11-11 Evgeny Stupachenko * gcc.target/i386/pr52252-atom-1.c: New. diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-63265.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-63265.C new file mode 100644 index 00000000000..aa0ce5e7ce2 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-63265.C @@ -0,0 +1,19 @@ +// PR c++/63265 +// { dg-do compile { target c++11 } } + +#define LSHIFT (sizeof(unsigned int) * __CHAR_BIT__) + +template +struct SpuriouslyWarns1 { + static constexpr unsigned int v = lshift < LSHIFT ? 1U << lshift : 0; +}; + +static_assert(SpuriouslyWarns1::v == 0, "Impossible occurred"); + +template +struct SpuriouslyWarns2 { + static constexpr bool okay = lshift < LSHIFT; + static constexpr unsigned int v = okay ? 1U << lshift : 0; +}; + +static_assert(SpuriouslyWarns2::v == 0, "Impossible occurred"); -- 2.30.2