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

David Blaikie dblaikie at gmail.com
Fri Aug 19 14:05:46 PDT 2011


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



More information about the cfe-dev mailing list