typeck.c (build_binary_op_nodefault): Warn on use of NULL in arithmetic.
authorMark Mitchell <mark@markmitchell.com>
Wed, 19 Aug 1998 18:48:58 +0000 (18:48 +0000)
committerMark Mitchell <mmitchel@gcc.gnu.org>
Wed, 19 Aug 1998 18:48:58 +0000 (18:48 +0000)
* 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
gcc/cp/except.c
gcc/cp/typeck.c
gcc/testsuite/g++.old-deja/g++.other/null1.C

index 65e8eb8baa72420ff58f15b4fb31fe84da8dda25..7cd9e51bcd96a339eb37d589d58d677677612702 100644 (file)
@@ -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.
index d294497e4c1fb83ca979f14537e42a4ae5102cea..9a23948521338085ef01e2611e54d951b9ad1305 100644 (file)
@@ -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);
index 2c5529afe83cd25c7efac58f3804a7ee41d4702f..00a1add42b365d50dbaacff2ec75a6b419f1bd87 100644 (file)
@@ -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)
index 4bdb048f1cecccfc89263a26805f731d6976cbea..9965a4387a47071d543a1737799b1153216e48b9 100644 (file)
@@ -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
 }