The initial -Wnonnull implementation in the middle end took place too late in the pipeline (just before expansion), and as a result was prone to false positives (bug 78817). In an attempt to avoid the worst of those, the warning was moved to the ccp2 pass in r243874. However, as the test case in PR 87489 shows, this is in turn too early and causes other false positives as a result.
A few experiments with running the warning later suggest that just before the mergephi2 pass is a good point to avoid this class of false positives without causing any regressions or introducing any new warnings either in Glibc or in Binutils/GDB. Since PR 87489 is a GCC 8-11 regression I propose to make this change on the trunk as well as on the release branches. Martin
PR middle-end/87489 - Spurious -Wnonnull warning on bitwise arithmetic and inlining gcc/ChangeLog: PR middle-end/87489 * passes.def (pass_post_ipa_warn): Move later. gcc/testsuite/ChangeLog: PR middle-end/87489 * gcc.dg/Wnonnull-6.c: New test. * gcc.dg/Wnonnull-7.c: New test. diff --git a/gcc/passes.def b/gcc/passes.def index e9ed3c7bc57..5e5a012943a 100644 --- a/gcc/passes.def +++ b/gcc/passes.def @@ -194,7 +194,6 @@ along with GCC; see the file COPYING3. If not see They ensure memory accesses are not indirect wherever possible. */ NEXT_PASS (pass_strip_predict_hints, false /* early_p */); NEXT_PASS (pass_ccp, true /* nonzero_p */); - NEXT_PASS (pass_post_ipa_warn); /* After CCP we rewrite no longer addressed locals into SSA form if possible. */ NEXT_PASS (pass_complete_unrolli); @@ -207,6 +206,7 @@ along with GCC; see the file COPYING3. If not see NEXT_PASS (pass_build_alias); NEXT_PASS (pass_return_slot); NEXT_PASS (pass_fre, true /* may_iterate */); + NEXT_PASS (pass_post_ipa_warn); NEXT_PASS (pass_merge_phi); NEXT_PASS (pass_thread_jumps); NEXT_PASS (pass_vrp, true /* warn_array_bounds_p */); @@ -368,7 +368,6 @@ along with GCC; see the file COPYING3. If not see NEXT_PASS (pass_lower_switch); /* Perform simple scalar cleanup which is constant/copy propagation. */ NEXT_PASS (pass_ccp, true /* nonzero_p */); - NEXT_PASS (pass_post_ipa_warn); NEXT_PASS (pass_object_sizes); /* Fold remaining builtins. */ NEXT_PASS (pass_fold_builtins); @@ -377,6 +376,7 @@ along with GCC; see the file COPYING3. If not see to forward object-size and builtin folding results properly. */ NEXT_PASS (pass_copy_prop); NEXT_PASS (pass_dce); + NEXT_PASS (pass_post_ipa_warn); NEXT_PASS (pass_sancov); NEXT_PASS (pass_asan); NEXT_PASS (pass_tsan); diff --git a/gcc/testsuite/gcc.dg/Wnonnull-6.c b/gcc/testsuite/gcc.dg/Wnonnull-6.c new file mode 100644 index 00000000000..507e7979cd0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wnonnull-6.c @@ -0,0 +1,25 @@ +/* PR middle-end/87489 - Spurious -Wnonnull warning on bitwise arithmetic + and inlining + { dg-do compile } + { dg-options "-O1 -Wall" } */ + +extern void f (const void*, int); + +static void g (int i, const char *s) +{ + int j = 0; + + if (i) + j |= 1; + + if (j) + f (&j, 0); + + if (j & 1) + f (s, __builtin_strlen (s)); // { dg-bogus "\\\[-Wnonnull" } +} + +void h (void) +{ + g (0, 0); +} diff --git a/gcc/testsuite/gcc.dg/Wnonnull-7.c b/gcc/testsuite/gcc.dg/Wnonnull-7.c new file mode 100644 index 00000000000..d801ca2329d --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wnonnull-7.c @@ -0,0 +1,6 @@ +/* PR middle-end/87489 - Spurious -Wnonnull warning on bitwise arithmetic + and inlining + { dg-do compile } + { dg-options "-Og -Wall" } */ + +#include "Wnonnull-6.c"