This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [C++ patch] accept __null as sentinel
Hi,
On Fri, 29 Apr 2005, Joseph S. Myers wrote:
> > While strictly speaking this is indeed not correct (and hence PR19542
> > was closed as invalid) we invented the special __null node in C++ (to
> > which NULL is expanded) exactly to make it possible to write uncasted
> > NULL and be sure that its size is exactly as large as a pointer type.
> > So I
>
> I thought the purpose of __null was actually to allow warnings when NULL
> was used in an integer context (e.g. for "int x = __null;"), not to make
> writing uncast NULL valid in varargs (in programs for which only
> portability to G++ matters).
I don't know exactly the history of __null, but it's size-dependendness on
the pointer size (it's set exactly to POINTER_SIZE), makes my
interpretation at least not fully off limits ;)
> I also thought that stylistically C++ programmers would generally be
> more careful about including casts and would expect to need them here.
You couldn't be more wrong I fear. The casual C++ programmer (and for
that matter C programmer) knows absolutely nothing about the difference of
a null pointer constant and a null pointer. And even less of them know
that NULL in C++ is only a null pointer constant. This can be seen in
many packages. And if you ask me, it's also understandable. This aspect
of C++ is IMHO just an artifact of its type system.
To that comes the fact that varargs functions are not that common in C++.
Usually they are from C interfaces, used in C++ code (glib/gtk and X11
comes to mind). Much of such code is just copy&pasted from C source to
C++ source. This is no wonder, as everything continues to work fine
afterwards. But as soon as you add the sentinel attributes in the common
header you get "bogus" warnings in C++, but not in C code: "I've written
NULL there, see? As you told me I couldn't use plain 0. - Huh? I have to
cast NULL to a pointer type? Why, NULL is the null pointer already? - To
what type, char* or void*? - Any? Who the hell designed this language?",
and I could understand this hypothetical developer.
> On that basis, I think a -Wno-format-null-sentinel or similar option to
> allow this warning to be disabled for __null would be better than
> removing this warning from -Wall.
Would you be satisfied with moving it to -pedantic? I really don't want
this to be in -Wall. We are not talking about a standard mandated warning
btw., we are talking about a warning which is supposed to help developers
keep their code clean, for which you explicitely have to add attributes to
prototypes.
Ciao,
Michael.