From b9edb4b11c99720e10625b41d37dc440bd468f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20L=C3=B3pez-Ib=C3=A1=C3=B1ez?= Date: Tue, 30 Jan 2007 22:29:11 +0000 Subject: [PATCH] re PR c++/24745 (unpleasant warning for "if (NULL)") 2007-01-30 Manuel Lopez-Ibanez PR c++/24745 * doc/invoke.texi (Wpointer-arith): Document warning. cp/ * typeck.c (build_binary_op): Fix logic for warning. Move warning to -Wpointer-arith. * call.c (convert_like_real): Don't warn when converting to boolean type. testsuite/ * g++.dg/warn/null4.C: New. From-SVN: r121361 --- gcc/ChangeLog | 5 +++++ gcc/cp/ChangeLog | 8 ++++++++ gcc/cp/call.c | 2 +- gcc/cp/typeck.c | 32 +++++++++++++++---------------- gcc/doc/invoke.texi | 3 ++- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/warn/null4.C | 29 ++++++++++++++++++++++++++++ 7 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/null4.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 4b3a903ad2b..a58b1dfa0ea 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2007-01-30 Manuel Lopez-Ibanez + + PR c++/24745 + * doc/invoke.texi (Wpointer-arith): Document warning. + 2007-01-30 Janis Johnson * doc/extend.texi (Decimal Floating Types): Remove decfloat.h from diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 0746d6af30b..ad008bf1aa7 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,11 @@ +2007-01-30 Manuel Lopez-Ibanez + + PR c++/24745 + * typeck.c (build_binary_op): Fix logic for warning. Move warning + to -Wpointer-arith. + * call.c (convert_like_real): Don't warn when converting to + boolean type. + 2007-01-29 Manuel Lopez-Ibanez * decl.c (pop_label): Replace warning with call to diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 55ae284787c..ac29ecdbcef 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -4250,7 +4250,7 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum, tree t = non_reference (totype); /* Issue warnings about peculiar, but valid, uses of NULL. */ - if (ARITHMETIC_TYPE_P (t) && expr == null_node) + if (expr == null_node && TREE_CODE (t) != BOOLEAN_TYPE && ARITHMETIC_TYPE_P (t)) { if (fn) warning (OPT_Wconversion, "passing NULL to non-pointer argument %P of %qD", diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 709d25b4dd2..829b8673a74 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -3828,30 +3828,28 @@ build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1, } } - /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE. - Then the expression will be built. - It will be given type FINAL_TYPE if that is nonzero; - otherwise, it will be given type RESULT_TYPE. */ - /* Issue warnings about peculiar, but valid, uses of NULL. */ - if (/* It's reasonable to use pointer values as operands of && + if ((orig_op0 == null_node || orig_op1 == null_node) + /* It's reasonable to use pointer values as operands of && and ||, so NULL is no exception. */ - !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR) - && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */ - (orig_op0 == null_node - && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE) - /* Or vice versa. */ - || (orig_op1 == null_node - && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE) - /* Or, both are NULL and the operation was not a comparison. */ - || (orig_op0 == null_node && orig_op1 == null_node - && code != EQ_EXPR && code != NE_EXPR))) + && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR + && ( /* Both are NULL (or 0) and the operation was not a comparison. */ + (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1) + && code != EQ_EXPR && code != NE_EXPR) + /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */ + || (!null_ptr_cst_p (orig_op0) && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE) + || (!null_ptr_cst_p (orig_op1) && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE))) /* Some sort of arithmetic operation involving NULL was performed. Note that pointer-difference and pointer-addition have already been handled above, and so we don't end up here in that case. */ - warning (0, "NULL used in arithmetic"); + warning (OPT_Wpointer_arith, "NULL used in arithmetic"); + + /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE. + Then the expression will be built. + It will be given type FINAL_TYPE if that is nonzero; + otherwise, it will be given type RESULT_TYPE. */ if (! converted) { if (TREE_TYPE (op0) != result_type) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index bd4bca6c9b4..31490b8f687 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -3153,7 +3153,8 @@ such assumptions. Warn about anything that depends on the ``size of'' a function type or of @code{void}. GNU C assigns these types a size of 1, for convenience in calculations with @code{void *} pointers and pointers -to functions. +to functions. In C++, warn also when an arithmetic operation involves +@code{NULL}. This warning is also enabled by @option{-pedantic}. @item -Wbad-function-cast @r{(C only)} @opindex Wbad-function-cast diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 68eb3dac791..dccd3b9c13b 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2007-01-30 Manuel Lopez-Ibanez + + PR c++/24745 + * g++.dg/warn/null4.C: New. + 2001-01-30 Roger Sayle Uros Bizjak diff --git a/gcc/testsuite/g++.dg/warn/null4.C b/gcc/testsuite/g++.dg/warn/null4.C new file mode 100644 index 00000000000..785f2790488 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/null4.C @@ -0,0 +1,29 @@ +// PR c++/24745 : warnings for NULL constant. +// { dg-do compile } +// { dg-options "-Wpointer-arith -Wconversion " } + +#include + +int foo (void) +{ + if (NULL == 1) return -1; // { dg-warning "NULL used in arithmetic" } + if (NULL > NULL) return -1; // { dg-warning "NULL used in arithmetic" } + if (NULL < NULL) return -1; // { dg-warning "NULL used in arithmetic" } + if (NULL >= 0) return -1; // { dg-warning "NULL used in arithmetic" } + if (NULL <= 0) return -1; // { dg-warning "NULL used in arithmetic" } + return 0; +} + +int bar (void) +{ + if (NULL) return -1; + if (!NULL) return -1; + if (!NULL == 1) return -1; + if (NULL || NULL) return -1; + if (!NULL && NULL) return -1; + if (NULL == NULL) return -1; + if (NULL != NULL) return -1; + if (NULL == 0) return -1; + if (NULL != 0) return -1; + return 0; +}