[cfe-dev] [PATCH] -Wconversion-null
Lubos Lunak
l.lunak at suse.cz
Tue Mar 13 15:04:35 PDT 2012
Hello,
the attached patch adds option -Wconversion-null . It is pretty much the same
like http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35669 .
The purpose of the option can be seen in this testcase:
#include <stdlib.h>
void foo( int a )
{
}
int main()
{
foo( NULL );
int a = NULL;
int b = 1;
short c = b + 1;
(void)a;
(void)c;
return 0;
}
$ clang++ -Wconversion null.cpp -c -fno-caret-diagnostics
null.cpp:9:10: warning: implicit conversion of NULL constant to integer
[-Wconversion]
null.cpp:10:13: warning: implicit conversion of NULL constant to integer
[-Wconversion]
null.cpp:12:15: warning: implicit conversion loses integer precision: 'int'
to 'short' [-Wconversion]
There are two obviously incorrect uses of NULL in this testcase. This is
currently warned about only with -Wconversion, which however also triggers
other conversion warnings that are not obviously incorrect. There are
probably no realistic usage scenarios where a conversion from NULL to integer
would be intended, but e.g. short<->int conversions may be wanted for example
for space saving reasons and casts that would silence those warnings may not
be deemed worth the decreased readability. In short, the benefits
of -Wconversion may be questionable depending on the codebase, but incorrect
usage of NULL should not (and it may not be as obvious as in this testcase).
This can be solved by introduction of -Wconversion-null, which only warns
about most probably incorrect usage of NULL, and is enabled by -Wconversion
or -Wall.
The attached patch should implement the change. This is my first clang
contribution, so I don't know if there is something more necessary. I also
couldn't find if/where options are documented.
--
Lubos Lunak
l.lunak at suse.cz
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clang-conversion-null.patch
Type: text/x-diff
Size: 4121 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120313/ae25bc55/attachment.patch>
More information about the cfe-dev
mailing list