From 59fbdb3a871585b020b21632c5c775d1ad8bc94f Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Mon, 18 Nov 2024 15:00:32 +0100 Subject: [PATCH] c++: reduce redundant deprecated warnings If a template uses a deprecated function, we should warn there and not also whenever the template is instantiated. I implement this by suppressing the warning at the location; then to make this also work with modules, I need to make sure to set TREE_NO_WARNING so that the warning spec for this location gets recorded. And then I noticed that has_warning_spec was broken such that if it returned true than get_nowarn_spec would always return null. gcc/cp/ChangeLog: * decl2.cc (cp_handle_deprecated_or_unavailable): Avoid redundant warning. * call.cc (build_over_call): Set TREE_NO_WARNING for calls to deprecated functions. * semantics.cc (finish_call_expr): Propagate TREE_NO_WARNING. gcc/ChangeLog: * warning-control.cc (has_warning_spec): Fix handling of get_no_warning_bit. gcc/testsuite/ChangeLog: * g++.dg/warn/deprecated-21.C: New test. * g++.dg/modules/warn-spec-2_a.C: New test. * g++.dg/modules/warn-spec-2_b.C: New test. --- gcc/cp/call.cc | 13 +++++++++++++ gcc/cp/decl2.cc | 7 ++++++- gcc/cp/semantics.cc | 9 +++++---- gcc/testsuite/g++.dg/modules/warn-spec-2_a.C | 11 +++++++++++ gcc/testsuite/g++.dg/modules/warn-spec-2_b.C | 8 ++++++++ gcc/testsuite/g++.dg/warn/deprecated-21.C | 12 ++++++++++++ gcc/warning-control.cc | 2 +- 7 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/warn-spec-2_a.C create mode 100644 gcc/testsuite/g++.dg/modules/warn-spec-2_b.C create mode 100644 gcc/testsuite/g++.dg/warn/deprecated-21.C diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 220ac130b0b..3033be47c7d 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -10004,6 +10004,11 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) SET_EXPR_LOCATION (expr, input_location); if (TREE_THIS_VOLATILE (fn) && cfun) current_function_returns_abnormally = 1; + if (TREE_DEPRECATED (fn) + && warning_suppressed_at (input_location, + OPT_Wdeprecated_declarations)) + /* Make the expr consistent with the location. */ + TREE_NO_WARNING (expr) = true; if (immediate_invocation_p (fn)) { tree obj_arg = NULL_TREE, exprimm = expr; @@ -10704,6 +10709,14 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) if (TREE_CODE (c) == CALL_EXPR) suppress_warning (c /* Suppress all warnings. */); } + else if (TREE_DEPRECATED (fn) + && warning_suppressed_at (input_location, + OPT_Wdeprecated_declarations)) + { + tree c = extract_call_expr (call); + if (TREE_CODE (c) == CALL_EXPR) + TREE_NO_WARNING (c) = true; + } return call; } diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc index fdcd67d5406..5f176168309 100644 --- a/gcc/cp/decl2.cc +++ b/gcc/cp/decl2.cc @@ -5943,7 +5943,12 @@ cp_handle_deprecated_or_unavailable (tree decl, tsubst_flags_t complain) } } else - warned = warn_deprecated_use (decl, NULL_TREE); + { + if (!warning_suppressed_at (input_location, + OPT_Wdeprecated_declarations)) + warned = warn_deprecated_use (decl, NULL_TREE); + suppress_warning_at (input_location, OPT_Wdeprecated_declarations); + } return warned; } diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index cb2b1543462..ecdd3da9ea2 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -3313,11 +3313,12 @@ finish_call_expr (tree fn, vec **args, bool disallow_virtual, orig_fn = sel_fn; } - result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args); - SET_EXPR_LOCATION (result, input_location); - KOENIG_LOOKUP_P (result) = koenig_p; + tree r = build_call_vec (TREE_TYPE (result), orig_fn, orig_args); + SET_EXPR_LOCATION (r, input_location); + KOENIG_LOOKUP_P (r) = koenig_p; + TREE_NO_WARNING (r) = TREE_NO_WARNING (result); release_tree_vector (orig_args); - result = convert_from_reference (result); + result = convert_from_reference (r); } return result; diff --git a/gcc/testsuite/g++.dg/modules/warn-spec-2_a.C b/gcc/testsuite/g++.dg/modules/warn-spec-2_a.C new file mode 100644 index 00000000000..fbfa668b2c7 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/warn-spec-2_a.C @@ -0,0 +1,11 @@ +// { dg-additional-options -fmodules } + +export module M; + +[[deprecated]] void depr_fn() {} + +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + +export { + template void f(T) { depr_fn(); } +} diff --git a/gcc/testsuite/g++.dg/modules/warn-spec-2_b.C b/gcc/testsuite/g++.dg/modules/warn-spec-2_b.C new file mode 100644 index 00000000000..437748b1332 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/warn-spec-2_b.C @@ -0,0 +1,8 @@ +// { dg-additional-options -fmodules } + +import M; + +int main() +{ + f(42); +} diff --git a/gcc/testsuite/g++.dg/warn/deprecated-21.C b/gcc/testsuite/g++.dg/warn/deprecated-21.C new file mode 100644 index 00000000000..7454a12c57b --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/deprecated-21.C @@ -0,0 +1,12 @@ +// { dg-do compile { target c++11 } } + +[[deprecated]] void depr_fn() {} + +template void f(T) { + depr_fn(); // { dg-warning "deprecated" } +} + +int main() +{ + f(42); +} diff --git a/gcc/warning-control.cc b/gcc/warning-control.cc index 11ca5db69c3..a13c3143897 100644 --- a/gcc/warning-control.cc +++ b/gcc/warning-control.cc @@ -262,7 +262,7 @@ copy_warning (gimple *to, const gimple *from) bool has_warning_spec (const_tree t) { const location_t loc = get_location (t); - return !RESERVED_LOCATION_P (loc) && !get_no_warning_bit (t); + return !RESERVED_LOCATION_P (loc) && get_no_warning_bit (t); } /* Retrieve warning dispostion bitmap for tree streaming. */