Ping: https://gcc.gnu.org/pipermail/gcc-patches/2020-June/547415.html

Jason already approved the C++ changes (with a couple of minor
tweaks).  I'm still looking for an approval of the corresponding
middle end diff.

On 6/5/20 1:41 PM, Martin Sebor wrote:
The caret location C++ -Wnonnull warnings is in the wrong place:
either under the closing parenthesis of a call to a function
declared nonnull, or under the whole call (when issued from
the middle end).  In addition, for member functions, the one-based
argument number mentioned in the warning starts with the implicit
this pointer, while in non-members it starts with the first argument.
That makes it difficult to tell which argument the warning is
complaining about (see the test + output below).

The attached patch improves things in a few minor ways:

1) by using the argument location when it has one to underline it
    (the location isn't always available at this point but that
    can be improved in a followup)
2) by using 1 for the first explicit argument, and by mentioning
    'this' for the this pointer
3) by changing the message issued by the front-end (and the C++
    member function handling) to match the corresponding message
    (and handling) in the middle end.

Finally, the patch also arranges to treat the C++ this pointer
as implicitly nonnull, regardless of whether the member function
is declared with the attribute.

Tested on x86_64-linux.

Martin

PS For this test case:

__attribute__ ((nonnull)) void f (const char*);

struct S
{
   __attribute__ ((nonnull)) void g (const char*) const;
};

void g (void)
{
   {
     const char* const null = 0;
     f (null);
   }

   {
     const S* const null = 0;
     null->g ("");
   }
}

GCC trunk outputs:

t.C: In function ‘void g()’:
t.C:12:12: warning: null argument where non-null required (argument 1) [-Wnonnull]
    12 |     f (null);
       |            ^
t.C:17:16: warning: null argument where non-null required (argument 1) [-Wnonnull]
    17 |     null->g ("");
       |                ^


With the patch the output is:

t.C: In function ‘void g()’:
t.C:12:8: warning: argument 1 null where non-null expected [-Wnonnull]
    12 |     f (null);
       |        ^~~~
t.C:1:32: note: in a call to function ‘void f(const char*)’ declared ‘nonnull’
     1 | __attribute__ ((nonnull)) void f (const char*);
       |                                ^
t.C:17:5: warning: ‘this’ pointer null [-Wnonnull]
    17 |     null->g ("");
       |     ^~~~
t.C:5:34: note: in a call to non-static member function ‘void S::g(const char*) const’
     5 |   __attribute__ ((nonnull)) void g (const char*) const;
       |                                  ^


Reply via email to