Re: C++ PATCH for c++/81676 - bogus -Wunused warnings in constexpr if
On Tue, Aug 27, 2019 at 4:10 PM Marek Polacek wrote: > On Fri, Aug 23, 2019 at 02:20:53PM -0700, Jason Merrill wrote: > > On 8/19/19 11:28 AM, Marek Polacek wrote: > > > On Fri, Aug 16, 2019 at 06:20:20PM -0700, Jason Merrill wrote: > > > > On 8/16/19 2:29 PM, Marek Polacek wrote: > > > > > This patch is an attempt to fix the annoying -Wunused-but-set-* > > > > > warnings that > > > > > tend to occur with constexpr if. When we have something like > > > > > > > > > > template < typename T > > > > > > int f(T v){ > > > > > if constexpr(sizeof(T) == sizeof(int)){ > > > > > return v; > > > > > }else{ > > > > > return 0; > > > > > } > > > > > } > > > > > > > > > > and call f('a'), then the condition is false, meaning that we won't > > > > > instantiate > > > > > the then-branch, as per tsubst_expr/IF_STMT: > > > > > 17284 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp)) > > > > > 17285 /* Don't instantiate the THEN_CLAUSE. */; > > > > > so we'll never get round to mark_exp_read-ing the decls used in the > > > > > then-branch, causing finish_function to emit "parameter set but not > > > > > used" > > > > > warnings. > > > > > > > > > > It's unclear how to best deal with this. Marking the decls > > > > > DECL_READ_P while > > > > > parsing doesn't seem like a viable approach > > > > > > > > Why not? > > > > > > Well, while parsing, we're in a template and so the condition won't be > > > evaluated until tsubst_expr. So we can't tell which branch is dead. > > > > But if a decl is used on one branch, we shouldn't warn even if it isn't used > > on the selected branch. > > I didn't want to mark the decls as read multiple times but we do it anyway > so that's no longer my concern. So... > > Bootstrapped/regtested on x86_64-linux, ok for trunk? > > 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. I was thinking of adding mark_exp_read to places where we currently take a shortcut in templates, like check_return_expr or build_x_binary_op, but this is probably simpler. The patch is OK. Jason
Re: C++ PATCH for c++/81676 - bogus -Wunused warnings in constexpr if
On Fri, Aug 23, 2019 at 02:20:53PM -0700, Jason Merrill wrote: > On 8/19/19 11:28 AM, Marek Polacek wrote: > > On Fri, Aug 16, 2019 at 06:20:20PM -0700, Jason Merrill wrote: > > > On 8/16/19 2:29 PM, Marek Polacek wrote: > > > > This patch is an attempt to fix the annoying -Wunused-but-set-* > > > > warnings that > > > > tend to occur with constexpr if. When we have something like > > > > > > > > template < typename T > > > > > int f(T v){ > > > > if constexpr(sizeof(T) == sizeof(int)){ > > > > return v; > > > > }else{ > > > > return 0; > > > > } > > > > } > > > > > > > > and call f('a'), then the condition is false, meaning that we won't > > > > instantiate > > > > the then-branch, as per tsubst_expr/IF_STMT: > > > > 17284 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp)) > > > > 17285 /* Don't instantiate the THEN_CLAUSE. */; > > > > so we'll never get round to mark_exp_read-ing the decls used in the > > > > then-branch, causing finish_function to emit "parameter set but not > > > > used" > > > > warnings. > > > > > > > > It's unclear how to best deal with this. Marking the decls DECL_READ_P > > > > while > > > > parsing doesn't seem like a viable approach > > > > > > Why not? > > > > Well, while parsing, we're in a template and so the condition won't be > > evaluated until tsubst_expr. So we can't tell which branch is dead. > > But if a decl is used on one branch, we shouldn't warn even if it isn't used > on the selected branch. I didn't want to mark the decls as read multiple times but we do it anyway so that's no longer my concern. So... Bootstrapped/regtested on x86_64-linux, ok for trunk? 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. * g++.dg/cpp1z/constexpr-if31.C: New test. * g++.dg/cpp1z/constexpr-if32.C: New test. diff --git gcc/cp/semantics.c gcc/cp/semantics.c index 1f7745933f9..2ba8e635ab7 100644 --- gcc/cp/semantics.c +++ gcc/cp/semantics.c @@ -774,6 +774,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 @@ -781,6 +793,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 (_CLAUSE (if_stmt), + maybe_mark_exp_read_r, NULL); + cp_walk_tree_without_duplicates (_CLAUSE (if_stmt), + maybe_mark_exp_read_r, NULL); +} add_stmt (do_poplevel (scope)); } diff --git gcc/testsuite/g++.dg/cpp1z/constexpr-if31.C gcc/testsuite/g++.dg/cpp1z/constexpr-if31.C new file mode 100644 index 000..02140cff9fd --- /dev/null +++ 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 gcc/testsuite/g++.dg/cpp1z/constexpr-if32.C gcc/testsuite/g++.dg/cpp1z/constexpr-if32.C new file mode 100644 index 000..13a6039fce6 --- /dev/null +++ gcc/testsuite/g++.dg/cpp1z/constexpr-if32.C @@ -0,0 +1,16 @@ +// PR c++/81676 - bogus -Wunused warnings in constexpr if. +// { dg-do
Re: C++ PATCH for c++/81676 - bogus -Wunused warnings in constexpr if
On 8/19/19 11:28 AM, Marek Polacek wrote: On Fri, Aug 16, 2019 at 06:20:20PM -0700, Jason Merrill wrote: On 8/16/19 2:29 PM, Marek Polacek wrote: This patch is an attempt to fix the annoying -Wunused-but-set-* warnings that tend to occur with constexpr if. When we have something like template < typename T > int f(T v){ if constexpr(sizeof(T) == sizeof(int)){ return v; }else{ return 0; } } and call f('a'), then the condition is false, meaning that we won't instantiate the then-branch, as per tsubst_expr/IF_STMT: 17284 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp)) 17285 /* Don't instantiate the THEN_CLAUSE. */; so we'll never get round to mark_exp_read-ing the decls used in the then-branch, causing finish_function to emit "parameter set but not used" warnings. It's unclear how to best deal with this. Marking the decls DECL_READ_P while parsing doesn't seem like a viable approach Why not? Well, while parsing, we're in a template and so the condition won't be evaluated until tsubst_expr. So we can't tell which branch is dead. But if a decl is used on one branch, we shouldn't warn even if it isn't used on the selected branch. Jason
Re: C++ PATCH for c++/81676 - bogus -Wunused warnings in constexpr if
On Fri, Aug 16, 2019 at 06:20:20PM -0700, Jason Merrill wrote: > On 8/16/19 2:29 PM, Marek Polacek wrote: > > This patch is an attempt to fix the annoying -Wunused-but-set-* warnings > > that > > tend to occur with constexpr if. When we have something like > > > >template < typename T > > >int f(T v){ > >if constexpr(sizeof(T) == sizeof(int)){ > > return v; > >}else{ > > return 0; > >} > >} > > > > and call f('a'), then the condition is false, meaning that we won't > > instantiate > > the then-branch, as per tsubst_expr/IF_STMT: > > 17284 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp)) > > 17285 /* Don't instantiate the THEN_CLAUSE. */; > > so we'll never get round to mark_exp_read-ing the decls used in the > > then-branch, causing finish_function to emit "parameter set but not used" > > warnings. > > > > It's unclear how to best deal with this. Marking the decls DECL_READ_P > > while > > parsing doesn't seem like a viable approach > > Why not? Well, while parsing, we're in a template and so the condition won't be evaluated until tsubst_expr. So we can't tell which branch is dead. I suppose we could still mark all the _DECLs in then/else branches as read, in finish_if_stmt if IF_STMT_CONSTEXPR_P, but that seems really really ugly. -- Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA
Re: C++ PATCH for c++/81676 - bogus -Wunused warnings in constexpr if
On 8/16/19 2:29 PM, Marek Polacek wrote: This patch is an attempt to fix the annoying -Wunused-but-set-* warnings that tend to occur with constexpr if. When we have something like template < typename T > int f(T v){ if constexpr(sizeof(T) == sizeof(int)){ return v; }else{ return 0; } } and call f('a'), then the condition is false, meaning that we won't instantiate the then-branch, as per tsubst_expr/IF_STMT: 17284 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp)) 17285 /* Don't instantiate the THEN_CLAUSE. */; so we'll never get round to mark_exp_read-ing the decls used in the then-branch, causing finish_function to emit "parameter set but not used" warnings. It's unclear how to best deal with this. Marking the decls DECL_READ_P while parsing doesn't seem like a viable approach Why not? Jason
C++ PATCH for c++/81676 - bogus -Wunused warnings in constexpr if
This patch is an attempt to fix the annoying -Wunused-but-set-* warnings that tend to occur with constexpr if. When we have something like template < typename T > int f(T v){ if constexpr(sizeof(T) == sizeof(int)){ return v; }else{ return 0; } } and call f('a'), then the condition is false, meaning that we won't instantiate the then-branch, as per tsubst_expr/IF_STMT: 17284 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp)) 17285 /* Don't instantiate the THEN_CLAUSE. */; so we'll never get round to mark_exp_read-ing the decls used in the then-branch, causing finish_function to emit "parameter set but not used" warnings. It's unclear how to best deal with this. Marking the decls DECL_READ_P while parsing doesn't seem like a viable approach, and since in this testcase the decl is type-dependent, doing something similiar to the fix for c++/85827 is out too. One option is the below, still don't instantiate anything in such a dead clause, but mark any use of a decl in it as a read. Which means that "set but unused" won't be warned about, but that's still better than bogus warnings. (Clang doesn't seem to handle that, either.) retrieve_local_specialization is there because we need to set the flag on the aready instantiated PARM/VAR_DECL, not the template itself. The good news is that when a param/decl is really unused, we still get the warning. And it works with lambdas too. Better ideas, anyone? (If anyone's wondering, constexpr function templates don't have this problem.) Bootstrapped/regtested on x86_64-linux, ok for trunk? 2019-08-16 Marek Polacek PR c++/81676 - bogus -Wunused warnings in constexpr if. * pt.c (maybe_mark_exp_read_r): New function. (tsubst_expr) : Call it for an uninstantiated THEN_CLAUSE and ELSE_CLAUSE. * g++.dg/cpp1z/constexpr-if30.C: New test. * g++.dg/cpp1z/constexpr-if31.C: New test. diff --git gcc/cp/pt.c gcc/cp/pt.c index 17585119bce..e146ad38443 100644 --- gcc/cp/pt.c +++ gcc/cp/pt.c @@ -16995,6 +16995,23 @@ lookup_init_capture_pack (tree decl) return r; } +/* Callback for cp_walk_tree to mark all instantiations of {VAR,PARM}_DECLs + in a tree as read. The point is to mark the parameters or local variables + of a template function as read to prevent various -Wunused warnings. */ + +static tree +maybe_mark_exp_read_r (tree *tp, int *, void *) +{ + tree t = *tp; + if (VAR_P (t) || TREE_CODE (t) == PARM_DECL) +{ + tree s = retrieve_local_specialization (t); + if (s) + mark_exp_read (s); +} + return NULL_TREE; +} + /* Like tsubst_copy for expressions, etc. but also does semantic processing. */ @@ -17282,7 +17299,10 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, break; } if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp)) - /* Don't instantiate the THEN_CLAUSE. */; + /* Don't instantiate the THEN_CLAUSE. But mark the PARM_DECLs and + VAR_DECLs used in the THEN_CLAUSE as read, so as to suppress + -Wunused-but-set-{variable,parameter} warnings. */ + cp_walk_tree (_CLAUSE (t), maybe_mark_exp_read_r, NULL, NULL); else { tree folded = fold_non_dependent_expr (tmp, complain); @@ -17296,7 +17316,10 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, finish_then_clause (stmt); if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp)) - /* Don't instantiate the ELSE_CLAUSE. */; + /* Don't instantiate the ELSE_CLAUSE. But mark the PARM_DECLs and + VAR_DECLs used in the ELSE_CLAUSE as read, so as to suppress + -Wunused-but-set-{variable,parameter} warnings. */ + cp_walk_tree (_CLAUSE (t), maybe_mark_exp_read_r, NULL, NULL); else if (ELSE_CLAUSE (t)) { tree folded = fold_non_dependent_expr (tmp, complain); diff --git gcc/testsuite/g++.dg/cpp1z/constexpr-if30.C gcc/testsuite/g++.dg/cpp1z/constexpr-if30.C new file mode 100644 index 000..26e0c6f39a6 --- /dev/null +++ gcc/testsuite/g++.dg/cpp1z/constexpr-if30.C @@ -0,0 +1,79 @@ +// PR c++/81676 - bogus -Wunused warnings in constexpr if. +// { dg-do compile { target c++17 } } +// { dg-options "-Wunused-variable -Wunused-parameter" } + +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