[cfe-dev] null pointer literals, warnings, and fixups

Douglas Gregor dgregor at apple.com
Fri Aug 26 15:58:11 PDT 2011


On Aug 26, 2011, at 3:40 PM, David Blaikie wrote:
> You're advocating a coding style that favors nullptr over 0. That's fine, and for a purely C++0x code base I *might* agree with you (although 0 is so much shorter than nullptr). However, the use of 0 rather than nullptr is not a good indication that there is actually a problem with the code, so we're outside the domain of what a compiler should do.
> 
> 
> That seems to me to be more than a stylistic issue - that's a "we're not sure what you intended here so you're not getting the helpful diagnostics you could be if you were providing more semantics to the compiler" (the helpful diagnostics we're already providing for nullptr and NULL usage where we know what you meant - so we already have precedent that shows these things are helpful/have some value).
> 
> I know GCC and Clang provide great diagnostics for NULL (could be better, we agree & we can fix that) and nullptr usage - but all the value of that is lost without any warning as soon as someone uses a 0 null pointer literal. Seems to very easily defeat the value we agree exists in those diagnostics.

We're going in circles. I think we agree that preferring nullptr to 0:

  (1) May prevent some mistakes arising from confusion between 0 being both an integer value and a null pointer , and
  (2) Is not in widespread use in C++ code today.

Your suggested warning about the use of 0 in a pointer context only makes sense if a particular code base follows this convention to start with, or the user wants to establish this convention. The former case is too rare now to consider (per (2)), and the latter case is a style-checking issue that should not be a compiler warning.

> Lots of code bases will be compiling both as C++98 and as C++0x for several years. I build Clang as C++0x with libc++; do you want me changing all of the 0's to nullptr's? The buildbots sure don't want me to.
> 
> Indeed - in that case I think it'd be reasonable to suppress any warnings about "old" C++98 constructs that have better C++0x equivalents, such as nullptr.

Clang's purpose is not to migrate code from C++98 to C++0x. That's a separate tool one could build with Clang.

> Of course I can try implementing this myself & running it on llvm/clang, though it'll be hard to say if it finds any bugs since replacing 0 with NULL won't tell me if the developer originally thought they were passing an integer parameter. 

This is what one has to do to introduce a warning: implement it and see what the fallout is. With this warning, you're extremely likely to get an extremely low signal/noise ratio, especially because some coding standards actively encourage the use of 0 over NULL.

> This seems quite different from stylistic issues such as bracing, line length, naming, or even the other thread about boolean testability (which way should you test a null pointer: if (x), if(x == NULL), etc...).
>  
>  
> Well, you can compile LLVM and Clang as C++0x. 
> 
> Do you just set CXXFLAGS to -std=c++0x when you run make, then? I'll have to give that a go.

Yes.

	- Doug

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110826/41374316/attachment.html>


More information about the cfe-dev mailing list