diff options
author | Mark Mitchell <mark@markmitchell.com> | 1998-08-19 18:48:58 +0000 |
---|---|---|
committer | Mark Mitchell <mmitchel@gcc.gnu.org> | 1998-08-19 18:48:58 +0000 |
commit | e0f9a8bc5052bfceb3e52ccae774024c3d071f20 (patch) | |
tree | 7407d7664089d119c0f773a50038ffdfd571132a | |
parent | 893779cc020424b6555cb851fd2659597174461a (diff) | |
download | gcc-e0f9a8bc5052bfceb3e52ccae774024c3d071f20.zip gcc-e0f9a8bc5052bfceb3e52ccae774024c3d071f20.tar.gz gcc-e0f9a8bc5052bfceb3e52ccae774024c3d071f20.tar.bz2 |
typeck.c (build_binary_op_nodefault): Warn on use of NULL in arithmetic.
* typeck.c (build_binary_op_nodefault): Warn on use of NULL in
arithmetic.
* except.c (build_throw): Warn when NULL is thrown, even with
-ansi. Use ansi_null_node, rather than integer_zero_node, in the
thrown expression.
From-SVN: r21863
-rw-r--r-- | gcc/cp/ChangeLog | 8 | ||||
-rw-r--r-- | gcc/cp/except.c | 6 | ||||
-rw-r--r-- | gcc/cp/typeck.c | 29 | ||||
-rw-r--r-- | gcc/testsuite/g++.old-deja/g++.other/null1.C | 4 |
4 files changed, 33 insertions, 14 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 65e8eb8..7cd9e51 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,11 @@ 1998-08-19 Mark Mitchell <mark@markmitchell.com> + * typeck.c (build_binary_op_nodefault): Warn on use of NULL in + arithmetic. + * except.c (build_throw): Warn when NULL is thrown, even with + -ansi. Use ansi_null_node, rather than integer_zero_node, in the + thrown expression. + * cp-tree.h (ansi_null_node): New variable. * decl.c (ansi_null_node): New variable. (init_decl_processing): Initialize its type. @@ -7,7 +13,7 @@ for null_node in non-ANSI mode. * typeck.c (build_binary_op_nodefault): Use ansi_null_node in place of null_node to avoid spurious errors. - + 1998-08-17 Mark Mitchell <mark@markmitchell.com> * cp-tree.h (enter_scope_of): New function. diff --git a/gcc/cp/except.c b/gcc/cp/except.c index d294497..9a23948 100644 --- a/gcc/cp/except.c +++ b/gcc/cp/except.c @@ -1277,10 +1277,10 @@ build_throw (e) if (processing_template_decl) return build_min (THROW_EXPR, void_type_node, e); - if (! flag_ansi && e == null_node) + if (e == null_node) { - cp_warning ("throwing NULL"); - e = integer_zero_node; + cp_warning ("throwing NULL, which has integral, not pointer type"); + e = ansi_null_node; } e = build1 (THROW_EXPR, void_type_node, e); diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 2c5529a..00a1add 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -3248,23 +3248,21 @@ build_binary_op_nodefault (code, orig_op0, orig_op1, error_code) things like `7 != NULL' result in errors about comparisons between pointers and integers. So, here, we replace __null with an appropriate null pointer constant. */ - if (orig_op0 == null_node) - orig_op0 = ansi_null_node; - if (orig_op1 == null_node) - orig_op1 = ansi_null_node; + op0 = (orig_op0 == null_node) ? ansi_null_node : orig_op0; + op1 = (orig_op1 == null_node) ? ansi_null_node : orig_op1; /* Apply default conversions. */ if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR || code == TRUTH_XOR_EXPR) { - op0 = decay_conversion (orig_op0); - op1 = decay_conversion (orig_op1); + op0 = decay_conversion (op0); + op1 = decay_conversion (op1); } else { - op0 = default_conversion (orig_op0); - op1 = default_conversion (orig_op1); + op0 = default_conversion (op0); + op1 = default_conversion (op1); } type0 = TREE_TYPE (op0); @@ -3963,6 +3961,21 @@ build_binary_op_nodefault (code, orig_op0, orig_op1, error_code) return error_mark_node; } + if (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */ + (orig_op0 == null_node + && TREE_CODE (TREE_TYPE (orig_op1)) != POINTER_TYPE) + /* Or vice versa. */ + || (orig_op1 == null_node + && TREE_CODE (TREE_TYPE (orig_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)) + /* 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. */ + cp_warning ("NULL used in arithmetic"); + if (! converted) { if (TREE_TYPE (op0) != result_type) diff --git a/gcc/testsuite/g++.old-deja/g++.other/null1.C b/gcc/testsuite/g++.old-deja/g++.other/null1.C index 4bdb048..9965a43 100644 --- a/gcc/testsuite/g++.old-deja/g++.other/null1.C +++ b/gcc/testsuite/g++.old-deja/g++.other/null1.C @@ -7,6 +7,6 @@ void f() int i; float f; - i != NULL; - f != NULL; + i != NULL; // WARNING - NULL used in arithmetic + f != NULL; // WARNING - NULL used in arithmetic } |