[cfe-commits] [patch] Warn on strange null pointers

David Blaikie dblaikie at gmail.com
Fri Jul 27 13:57:47 PDT 2012


While we currently warn on code like this:

  bool *b = false; // or any other pointer type, for that matter, but
bool makes it clear how someone might accidentally write this

We don't warn on code like this

  char *c = '\0'; // again, applicable to any pointer type

So I figured I would generalize the former warning & use a whitelist
rather than a blacklist, only allowing expressions of nullptr_t type
or literal zeros of integer type (not char type). This catches a few
odd null pointer constructs in the test suite and in the wild
including things like this:

  template<typename T> T *foo() { return T(); } // found in the test
suite, valid for any integral type T, but a tad surprising

& a lot of things like

  strtol(x, '\0', base)

plus a few more subtle things.

In the interests of not breaking all over gunit, the warning is
suppressed for unevaluated contexts (gunit does some sfinae fun to see
if certain expressions are valid null pointers) & refactored the
unevaluated context check across all the current callers.

This helps pave the way for codebases to migrate for a DR Richard
Smith mentioned to me that would actually make this an actual standard
requirement rather than just a warning (the gunit case would no longer
be a problem then because the SFINAE would find it as an error & take
a different path anyway).

Thoughts? Naming & diagnostic text are certainly open to improvements...

PS: you'll see we're actually missing some cases of this (test case
TODO in the patch) that even apply to the bool case we thought we were
catching. I'll work on fixing that hole next (probably move this check
to SemaChecking where it presumably belongs).

- David
-------------- next part --------------
A non-text attachment was scrubbed...
Name: weird_null_pointers.diff
Type: application/octet-stream
Size: 11910 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120727/ac636466/attachment.obj>


More information about the cfe-commits mailing list