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

David Blaikie dblaikie at gmail.com
Sun Aug 21 21:18:07 PDT 2011


As a small indirect first attempt I've included a patch that:

1) changes "NULL" to "null" in "initialization of pointer of type %0
to NULL from a constant boolean expression" - since NULL is a specific
macro but the general concept is simply a "null pointer".

2) adds a fixup for this warning in C++0x to suggest using nullptr

I think there's still a bunch of things worth discussing about when to
suggest nullptr and what to suggest in C++98 (0, NULL, nothing at
all?) & I'd like to apply this kind of suggestion/fixup in a much more
general way (to all null pointer literals that aren't nullptr, not
just the false boolean literal) - but I thought this might be a
moderately easy experiment, even if it's throwaway, and might be a
more concrete thing to start such discussions.

- David

On Fri, Aug 19, 2011 at 2:05 PM, David Blaikie <dblaikie at gmail.com> wrote:
> It came up as a coding convention question recently on llvm-dev
> regarding which null pointer literal to use (NULL or 0) (&
> additionally, what was the preferred way to do null pointer tests) &
> got me thinking:
>
> What would be the appropriate way to make some warnings (including
> fixups) to, primarily, help people migrate to nullptr and,
> secondarily, get consistent use of NULL (there are some warnings about
> NULL usage in non-pointer contexts (NULL < 3, for example) but not all
> (int i = NULL, for example) - and no warning to help you add NULL
> where you've currently used 0 as a null pointer literal).
>
> Should these be compiler warnings or static analysis (like the LLVM
> coding conventions static analysis)? The last one I mentioned for
> C++98 might be a bit tricky. Some people prefer NULL, some prefer 0.
> Can we have mutually disagreeing warnings in clang?
> The C++0x case seems easier & to be clear what I'm proposing is adding
> two warnings: One for any use of NULL that is in a non-pointer
> context, with a fixit to switch to the relevant integer literal.
> (could this be powered by macro detection semantics (could catch other
> definitions of NULL, would fail to catch indirect use of NULL if
> someone defined their own MY_NULL macro), or only by GNU's special
> __null type?)
> And another to uses of NULL in pointer contexts with a fixit to switch
> to nullptr.
>
> Side question: what tools currently exist that can actually apply
> fixit instructions to code? Is there any existing simple program (that
> would probably work like scan-build, or similar) that could be
> instructed to actually apply all instances of a particular warning
> fixup?
>
> - David
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bool-pointer-nullptr.diff
Type: text/x-patch
Size: 1951 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20110821/c84ed969/attachment.bin>


More information about the cfe-commits mailing list