shithub: scc

Download patch

ref: e35c19c1a268f0d66bdb676a5e576da8b5e8d177
parent: be7f4b5b23cbdaedac70f2743b1cc0d4f517ef8d
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Aug 25 11:18:47 EDT 2015

Allow assignation of 0 to pointers

The standard allows that a 0 constant is also a valid NULL pointer constant,
so it can be assigned and compared against pointers.

--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -413,11 +413,11 @@
 		break;
 	case PTR:
 		switch (newtp->op) {
-		case ENUM:  /* TODO: allow p = 0 */
+		case ENUM:
 		case INT:
 		case VOID:
 			if (!iscast)
-				return NULL;;
+				return NULL;
 			break;
 		case PTR:
 			if (iscast ||
@@ -627,8 +627,12 @@
 	lp = eval(lp);
 	rp = eval(rp);
 
-	if ((rp = convert(rp, lp->type, 0)) == NULL)
-		error("incompatible types when assigning");
+	if (BTYPE(rp) == INT && BTYPE(lp) == PTR &&
+	    rp->constant && SYMICMP(rp->sym, 0)) {
+		rp = node(OCAST, pvoidtype, rp, NULL);
+	} else if ((rp = convert(rp, lp->type, 0)) == NULL) {
+		errorp("incompatible types when assigning");
+	}
 
 	return node(op, lp->type, lp, rp);
 }