This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Website patch: update projects to remove attribute nonnull


Kaveh R. Ghazi wrote:
1.  What does the C standard say about the definition of the NULL
    macro and its type?

NULL is a macro that expands to a null pointer constant. A null pointer constant is an integer constant expression with the value 0, or such an expression cast to (void *). [7.17, 6.3.2.3].

2.  What does the C standard say about pointer zero and integer zero
    of the same width?

It says everything about casting integers to pointers or vice-versa is implementations defined.

>     Are they guaranteed to have the same
>     representation, especially when passed through stdarg?

No.

3.  Do gcc supported (primary) platforms define NULL as plain "0" when
    pointers are the same width as int?  (Yes)

4. Is NULL commonly used as a variadic terminator? (Yes)

This only is required to work correctly if NULL is defined as (void *)0, so it is non-portable, and even then it is only required to work correctly if the variadic function expects a (void *) or a pointer to character type. [7.15.1.2]

5.  If NULL is defined as "0", would gcc users find it useful to warn
    about it?  (No)

Only people who are not interested in portability would not find this warning useful.

The answers to these questions will tell us what the best thing for
users is.  I suspect we'll find that the best thing to do is *not*
warn about integer zero if it is the same width as a pointer, because
NULL will be in this category.

The best thing to do is to educate the users, not to make it easier for them to continue their sloppy practices.

I realize that on platforms where the size of int is the same as that
of pointers, if the user uses a naked zero then they won't get a
warning.  However they will get a warning when they move this explicit
zero usage to a 64-bit platform.  I say, if you intend your code to
run on a 64-bit box, >test it there<.

The problem is not only with 64-bit platforms; there are lots of other platforms where a null pointer does not have the same bit representation as an integer zero, or where not all object pointers have the same representation as (char *) has.

You might argue that such platforms are not supported by GCC
[is that true, btw?], but that doesn't matter: GCC should warn
about all non-portable constructs.

> We have the same issue with
HOST_WIDE_INT.  If the user hardcodes a specifier instead of using
HOST_WIDE_INT_PRINT_DEC, you only get format warning when you try the
hardcoded specifier on another box where it is wrong.  The integer
zero variadic terminator is in the same category IMHO.

No. With printf specifiers, the compiler can't detect there's something wrong. With the integer zero terminator, it can.


Segher




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]