scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit e35c19c1a268f0d66bdb676a5e576da8b5e8d177
parent be7f4b5b23cbdaedac70f2743b1cc0d4f517ef8d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 25 Aug 2015 15:18:47 +0200

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.

Diffstat:
cc1/expr.c | 12++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/cc1/expr.c b/cc1/expr.c @@ -413,11 +413,11 @@ convert(Node *np, Type *newtp, char iscast) 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 @@ assignop(char op, Node *lp, Node *rp) 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); }