This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
PATCH for NULL handling
- To: egcs-patches at cygnus dot com
- Subject: PATCH for NULL handling
- From: Mark Mitchell <mark at markmitchell dot com>
- Date: Wed, 19 Aug 1998 15:16:29 -0700
- Cc: Jason Merrill <jason at cygnus dot com>
- Reply-to: mark at markmitchell dot com
This patch avoids spurious errors on legal code when comparing
integers with NULL. The practice is questionable, but certainly
legal.
--
Mark Mitchell mark@markmitchell.com
Mark Mitchell Consulting http://www.markmitchell.com
1998-08-19 Mark Mitchell <mark@markmitchell.com>
* cp-tree.h (ansi_null_node): New variable.
* decl.c (ansi_null_node): New variable.
(init_decl_processing): Initialize its type.
* lex.c (init_parse): Initialize its value. Use ansi_null_node
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.
Index: testsuite/g++.old-deja/g++.other/null1.C
===================================================================
RCS file: null1.C
diff -N null1.C
*** /dev/null Mon Dec 31 20:00:00 1979
--- null1.C Wed Aug 19 15:06:29 1998
***************
*** 0 ****
--- 1,12 ----
+ // Build don't link:
+
+ #include <cstddef>
+
+ void f()
+ {
+ int i;
+ float f;
+
+ i != NULL;
+ f != NULL;
+ }
cvs server: Diffing cp
Index: cp/cp-tree.h
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/cp-tree.h,v
retrieving revision 1.110
diff -c -p -r1.110 cp-tree.h
*** cp-tree.h 1998/08/17 22:33:49 1.110
--- cp-tree.h 1998/08/19 22:06:55
*************** extern tree long_long_integer_type_node,
*** 1918,1923 ****
--- 1918,1924 ----
extern tree integer_two_node, integer_three_node;
extern tree boolean_type_node, boolean_true_node, boolean_false_node;
+ extern tree ansi_null_node;
extern tree null_node;
/* in pt.c */
Index: cp/decl.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/decl.c,v
retrieving revision 1.181
diff -c -p -r1.181 decl.c
*** decl.c 1998/08/17 19:29:45 1.181
--- decl.c 1998/08/19 22:07:40
*************** tree static_aggregates;
*** 429,437 ****
tree integer_zero_node;
tree null_pointer_node;
! /* The value for __null (NULL), either of type `void *' or, with -ansi,
! an integer type of the same size. */
!
tree null_node;
/* A node for the integer constants 1, 2, and 3. */
--- 429,441 ----
tree integer_zero_node;
tree null_pointer_node;
! /* The value for __null (NULL), when -ansi is specified. As per the
! standard, this is an implementation-defined null pointer constant. */
! tree ansi_null_node;
!
! /* The value for __null (NULL). With -ansi, this is just
! ansi_null_node. Without -ansi, this is a zero-valued pointer
! constant of type `{unknown type}*'. */
tree null_node;
/* A node for the integer constants 1, 2, and 3. */
*************** init_decl_processing ()
*** 6030,6038 ****
/* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
TREE_TYPE (unknown_type_node) = unknown_type_node;
! if (flag_ansi)
! TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0);
! else
TREE_TYPE (null_node) = build_pointer_type (unknown_type_node);
/* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
--- 6034,6041 ----
/* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
TREE_TYPE (unknown_type_node) = unknown_type_node;
! TREE_TYPE (ansi_null_node) = type_for_size (POINTER_SIZE, 0);
! if (!flag_ansi)
TREE_TYPE (null_node) = build_pointer_type (unknown_type_node);
/* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
Index: cp/lex.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/lex.c,v
retrieving revision 1.62
diff -c -p -r1.62 lex.c
*** lex.c 1998/08/17 16:29:16 1.62
--- lex.c 1998/08/19 22:07:54
*************** init_parse (filename)
*** 777,783 ****
TREE_TYPE (signature_type_node) = signature_type_node;
ridpointers[(int) RID_SIGNATURE] = signature_type_node;
! null_node = build_int_2 (0, 0);
ridpointers[RID_NULL] = null_node;
opname_tab[(int) COMPONENT_REF] = "->";
--- 777,791 ----
TREE_TYPE (signature_type_node) = signature_type_node;
ridpointers[(int) RID_SIGNATURE] = signature_type_node;
! /* Create the built-in __null node. Note that we can't yet call for
! type_for_size here because integer_type_node and so forth are not
! set up. Therefore, we don't set the type of these nodes until
! init_decl_processing. */
! ansi_null_node = build_int_2 (0, 0);
! if (flag_ansi)
! null_node = ansi_null_node;
! else
! null_node = build_int_2 (0, 0);
ridpointers[RID_NULL] = null_node;
opname_tab[(int) COMPONENT_REF] = "->";
Index: cp/typeck.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/typeck.c,v
retrieving revision 1.94
diff -c -p -r1.94 typeck.c
*** typeck.c 1998/08/17 17:13:42 1.94
--- typeck.c 1998/08/19 22:08:41
*************** build_binary_op_nodefault (code, orig_op
*** 3244,3249 ****
--- 3244,3258 ----
/* Nonzero means set RESULT_TYPE to the common type of the args. */
int common = 0;
+ /* Unless -ansi is specified, __null has pointer type. But, then,
+ 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;
+
/* Apply default conversions. */
if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
|| code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
cvs server: Diffing cp/inc