From df5cf47a978aaeb53fc2b18ff0b22eb4531a27d8 Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Fri, 31 Jul 2020 10:27:33 -0600 Subject: [PATCH] Set and test no-warning bit to avoid -Wnonnull for synthesized expressions. Resolves: PR c++/96003 spurious -Wnonnull calling a member on the result of static_cast gcc/c-family/ChangeLog: PR c++/96003 * c-common.c (check_function_arguments_recurse): Return early when no-warning bit is set. gcc/cp/ChangeLog: PR c++/96003 * class.c (build_base_path): Set no-warning bit on the synthesized conditional expression in static_cast. gcc/testsuite/ChangeLog: PR c++/96003 * g++.dg/warn/Wnonnull7.C: New test. --- gcc/c-family/c-common.c | 3 +++ gcc/cp/class.c | 10 ++++++-- gcc/testsuite/g++.dg/warn/Wnonnull7.C | 36 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wnonnull7.C diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index b97539c0c2a..96ed2334863 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -5822,6 +5822,9 @@ check_function_arguments_recurse (void (*callback) void *ctx, tree param, unsigned HOST_WIDE_INT param_num) { + if (TREE_NO_WARNING (param)) + return; + if (CONVERT_EXPR_P (param) && (TYPE_PRECISION (TREE_TYPE (param)) == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0))))) diff --git a/gcc/cp/class.c b/gcc/cp/class.c index 7a25d8fc76c..b39bdaaa3ab 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -516,8 +516,14 @@ build_base_path (enum tree_code code, out: if (null_test) - expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr, - build_zero_cst (target_type)); + { + expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, + expr, build_zero_cst (target_type)); + /* Avoid warning for the whole conditional expression (in addition + to NULL_TEST itself -- see above) in case the result is used in + a nonnull context that the front end -Wnonnull checks. */ + TREE_NO_WARNING (expr) = 1; + } return expr; } diff --git a/gcc/testsuite/g++.dg/warn/Wnonnull7.C b/gcc/testsuite/g++.dg/warn/Wnonnull7.C new file mode 100644 index 00000000000..7611c18d948 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wnonnull7.C @@ -0,0 +1,36 @@ +/* PR c++/96003 - spurious -Wnonnull calling a member on the result + of static_cast + { dg-do compile } + { dg-options "-Wall" } */ + +struct D; +struct B +{ + B* next; + D* Next (); +}; + +struct D: B +{ + virtual ~D (); +}; + +struct Iterator +{ + D* p; + void advance () + { + p = static_cast(p)->Next (); // { dg-bogus "\\\[-Wnonnull" } + } +}; + +// Test case from comment #11. + +struct S1 { virtual ~S1 (); }; +struct S2 { virtual ~S2 (); }; +struct S3: S1, S2 { void f (); }; + +void f (S2 *p) +{ + static_cast(p)->f (); // { dg-bogus "\\\[-Wnonnull" } +} -- 2.30.2