https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86568

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-06-03
     Ever confirmed|0                           |1
                 CC|                            |msebor at gcc dot gnu.org
      Known to fail|                            |10.1.0, 11.0, 9.2.0
             Blocks|                            |95507
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
Confirmed.  The problem isn't unique to -Wnonnull but affects other warnings as
well.  An example involving -Walloc-size-larger-than is below.

The middle end warnings use the location of the call when they should instead
be using the location of the argument (when it has one).

$ cat z.c && gcc -O2 -S -Wall -Wpedantic z.c
typedef __SIZE_TYPE__ size_t;

__attribute__ ((alloc_size (2))) void* f (const char*, size_t, int);

void* g (void)
{
  size_t n = (size_t)-1 / 2;

  return f ("foo", n + 1, 0);
}
z.c: In function ‘g’:
z.c:9:10: warning: argument 2 value ‘9223372036854775808’ exceeds maximum
object size 9223372036854775807 [-Walloc-size-larger-than=]
    9 |   return f ("foo", n + 1, 0);
      |          ^~~~~~~~~~~~~~~~~~~
z.c:3:40: note: in a call to allocation function ‘f’ declared here
    3 | __attribute__ ((alloc_size (2))) void* f (const char*, size_t, int);
      |                                        ^

I'm not too familiar with the front end implementation but it too looks like it
uses the location of the call.  The patch below produces the (almost) expected
output for the simple test case.  The argument numbers are still off.

index b1379faa412..9c5e76c1e59 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -5497,7 +5497,10 @@ check_nonnull_arg (void *ctx, tree param, unsigned
HOST_WIDE_INT param_num)
   /* Diagnose the simple cases of null arguments.  */
   if (integer_zerop (fold_for_warn (param)))
     {
-      warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null "
+      location_t loc
+       = EXPR_HAS_LOCATION (param) ? EXPR_LOCATION (param) : pctx->loc;
+
+      warning_at (loc, OPT_Wnonnull, "null argument where non-null "
                  "required (argument %lu)", (unsigned long) param_num);
       pctx->warned_p = true;
     }


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95507
[Bug 95507] [meta-bug] bogus/missing -Wnonnull

Reply via email to