From e0f9a8bc5052bfceb3e52ccae774024c3d071f20 Mon Sep 17 00:00:00 2001 From: Mark Mitchell Date: Wed, 19 Aug 1998 18:48:58 +0000 Subject: [PATCH] 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 --- gcc/cp/ChangeLog | 8 +++++- gcc/cp/except.c | 6 ++-- gcc/cp/typeck.c | 29 ++++++++++++++------ 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 65e8eb8baa7..7cd9e51bcd9 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,11 @@ 1998-08-19 Mark Mitchell + * 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 * cp-tree.h (enter_scope_of): New function. diff --git a/gcc/cp/except.c b/gcc/cp/except.c index d294497e4c1..9a239485213 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 2c5529afe83..00a1add42b3 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 4bdb048f1ce..9965a4387a4 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 } -- 2.30.2