From 8692693732e89806058d3b9c91132fb83661b214 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Wed, 28 Aug 2019 02:22:29 +0000 Subject: [PATCH] PR c++/81676 - bogus -Wunused warnings in constexpr if. * semantics.c (maybe_mark_exp_read_r): New function. (finish_if_stmt): Call it on THEN_CLAUSE and ELSE_CLAUSE. * g++.dg/cpp1z/constexpr-if31.C: New test. * g++.dg/cpp1z/constexpr-if32.C: New test. From-SVN: r274982 --- gcc/cp/ChangeLog | 4 ++ gcc/cp/semantics.c | 22 ++++++ gcc/testsuite/ChangeLog | 4 ++ gcc/testsuite/g++.dg/cpp1z/constexpr-if31.C | 79 +++++++++++++++++++++ gcc/testsuite/g++.dg/cpp1z/constexpr-if32.C | 16 +++++ 5 files changed, 125 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-if31.C create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-if32.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index c2f19670a24..b735dab6a51 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,9 @@ 2019-08-27 Marek Polacek + PR c++/81676 - bogus -Wunused warnings in constexpr if. + * semantics.c (maybe_mark_exp_read_r): New function. + (finish_if_stmt): Call it on THEN_CLAUSE and ELSE_CLAUSE. + PR c++/91428 - warn about std::is_constant_evaluated in if constexpr. * cp-tree.h (decl_in_std_namespace_p): Declare. * semantics.c (is_std_constant_evaluated_p): New. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 8603e57e7f7..b61d86f94b9 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -810,6 +810,18 @@ finish_else_clause (tree if_stmt) ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt)); } +/* Callback for cp_walk_tree to mark all {VAR,PARM}_DECLs in a tree as + read. */ + +static tree +maybe_mark_exp_read_r (tree *tp, int *, void *) +{ + tree t = *tp; + if (VAR_P (t) || TREE_CODE (t) == PARM_DECL) + mark_exp_read (t); + return NULL_TREE; +} + /* Finish an if-statement. */ void @@ -817,6 +829,16 @@ finish_if_stmt (tree if_stmt) { tree scope = IF_SCOPE (if_stmt); IF_SCOPE (if_stmt) = NULL; + if (IF_STMT_CONSTEXPR_P (if_stmt)) + { + /* Prevent various -Wunused warnings. We might not instantiate + either of these branches, so we would not mark the variables + used in that branch as read. */ + cp_walk_tree_without_duplicates (&THEN_CLAUSE (if_stmt), + maybe_mark_exp_read_r, NULL); + cp_walk_tree_without_duplicates (&ELSE_CLAUSE (if_stmt), + maybe_mark_exp_read_r, NULL); + } add_stmt (do_poplevel (scope)); } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index ade1a69767f..4c58780ff67 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,9 @@ 2019-08-27 Marek Polacek + PR c++/81676 - bogus -Wunused warnings in constexpr if. + * g++.dg/cpp1z/constexpr-if31.C: New test. + * g++.dg/cpp1z/constexpr-if32.C: New test. + PR c++/91428 - warn about std::is_constant_evaluated in if constexpr. * g++.dg/cpp2a/is-constant-evaluated9.C: New test. diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if31.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if31.C new file mode 100644 index 00000000000..02140cff9fd --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if31.C @@ -0,0 +1,79 @@ +// PR c++/81676 - bogus -Wunused warnings in constexpr if. +// { dg-do compile { target c++17 } } +// { dg-options "-Wall -Wextra" } + +template int +f1 (T v) +{ + T x = 0; + if constexpr(sizeof(T) == sizeof(int)) + return v + x; + else + return 0; +} + +template int +f2 (T v) // { dg-warning "unused parameter .v." } +{ + T x = 0; + if constexpr(sizeof(T) == sizeof(int)) + return x; + else + return 0; +} + +template int +f3 (T v) +{ + T x = 0; // { dg-warning "unused variable .x." } + if constexpr(sizeof(T) == sizeof(int)) + return v; + else + return 0; +} + +template int +f4 (T v) +{ + T x = 0; + if constexpr(sizeof(T) == sizeof(int)) + return 0; + else + return v + x; +} + +template int +f5 (T v) // { dg-warning "unused parameter .v." } +{ + T x = 0; + if constexpr(sizeof(T) == sizeof(int)) + return 0; + else + return x; +} + +template int +f6 (T v) +{ + T x = 0; // { dg-warning "unused variable .x." } + if constexpr(sizeof(T) == sizeof(int)) + return 0; + else + return v; +} + +int main() +{ + f1(0); + f1('a'); + f2(0); + f2('a'); + f3(0); + f3('a'); + f4(0); + f4('a'); + f5(0); + f5('a'); + f6(0); + f6('a'); +} diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if32.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if32.C new file mode 100644 index 00000000000..13a6039fce6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if32.C @@ -0,0 +1,16 @@ +// PR c++/81676 - bogus -Wunused warnings in constexpr if. +// { dg-do compile { target c++17 } } +// { dg-options "-Wall -Wextra" } + +int main() +{ + auto f = [](auto a, auto b) { + if constexpr (sizeof(b) == 1) { + return a; + } else { + return b; + } + }; + + return f(1, 1) + f(1, 'a'); +} -- 2.30.2