scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit ebd1bfd80b0716ec72b256c6b65c01e9f8d8e2f1
parent 45da2d1eff24fc3c0393d5ffbea087d1507b598c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri,  8 Jan 2016 10:40:55 +0100

Give a meanful error message when non scalar are used

There are contexts, mainly conditions, where only scalar are required,
and the code was giving a very confusing message error related to
comparision, instead of giving this new error (taken from gcc) which
shows to the use what is the problem.

Diffstat:
Mcc1/cc1.h | 1+
Mcc1/expr.c | 4++++
Mcc1/types.c | 3++-
3 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -44,6 +44,7 @@ struct type { bool printed : 1; /* the type already was printed */ bool integer : 1; /* this type is INT or enum */ bool arith : 1; /* this type is INT, ENUM, FLOAT */ + bool aggreg : 1; /* this type is struct or union */ size_t size; /* sizeof the type */ size_t align; /* align of the type */ Type *type; /* base type */ diff --git a/cc1/expr.c b/cc1/expr.c @@ -410,6 +410,10 @@ static Node * exp2cond(Node *np, char neg) { np = decay(np); + if (np->type->aggreg) { + errorp("used struct/union type value where scalar is required"); + np = constnode(zero); + } if (isnodecmp(np->op)) return (neg) ? negate(np) : np; return compare((neg) ? OEQ : ONE, np, constnode(zero)); diff --git a/cc1/types.c b/cc1/types.c @@ -465,9 +465,10 @@ mktype(Type *tp, int op, TINT nelem, Type *pars[]) type.printed = 1; type.integer = 1; type.arith = 1; - /* PASSTROUGH */ + goto no_defined; case STRUCT: case UNION: + type.aggreg = 1; no_defined: type.defined = 0; break;