What is the expected behavior of attribute nonnull for C++
Steven Bosscher
stevenb.gcc@gmail.com
Fri Apr 28 21:36:00 GMT 2006
On 4/28/06, Mark Mitchell <mark@codesourcery.com> wrote:
> Steven Bosscher wrote:
>
> > The documentation of the nonnull attribute says:
> >
> > `nonnull (ARG-INDEX, ...)'
> > The `nonnull' attribute specifies that some function parameters
> > should be non-null pointers. For instance, the declaration:
> >
> > extern void *
> > my_memcpy (void *dest, const void *src, size_t len)
> > __attribute__((nonnull (1, 2)));
> >
> > causes the compiler to check that, in calls to `my_memcpy',
> > arguments DEST and SRC are non-null.
> >
> >
> > So do we expect our users to know that they should add 1 to every
> > ARG-INDEX they pass? That would make this a documentation bug. Or
> > is this a "real" bug in G++, and should the compiler correct the
> > ARG-INDEX numbers so that the middle-end doesn't get confused?
>
> I think this is a documentation bug.
OK. Andrew Pinski already pointed out http://gcc.gnu.org/PR1607 as
another example.
> (Well, really, I think it's a design bug; the attribute should be
> applied to the parameters, rather than to the entire function type, so
> that users didn't have to count. In other words, it should be:
>
> void f(__attribute((nonnull)) int *);
>
> But, that's just me being wishful.)
:-)
> The reason I think this is a documentation bug is that (a) it's always
> been this way, ergo we'd be breaking backwards compatibility to change
> it,
OK, Richard G. also mentioned this. I'll prepare a documentation patch.
> and (b) some of these kinds of attributes could usefully apply to
> the "this" pointer.
That is why I was looking at this. We have http://gcc.gnu.org/PR27336,
and part of the fix could be to make the 'this' pointer always
non-NULL. So far I haven't found anyone who can think of a situation
where 'this' can be NULL...
Thanks,
Gr.
Steven
More information about the Gcc
mailing list