scc

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

commit 7d32c5733f318b04aefa28e3738bebcad8323b6a
parent 7953f6f46ef27e1ab5bdb5cd543bba523ac3c0b3
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  9 Jul 2014 14:59:18 +0200

Check type of expressions in case statement

Case statement can only content integer expressions, or expressions
that can be converted into integers.

Diffstat:
Mcc1/stmt.c | 15++++++---------
1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/cc1/stmt.c b/cc1/stmt.c @@ -230,23 +230,20 @@ static void Case(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) { Node *np; - Symbol *lcase; struct scase *pcase; - lcase = install("", NS_LABEL); + expect(CASE); if (!lswitch) error("case label not within a switch statement"); - expect(CASE); - if (yytoken == ':') - error("expected expression before ':'"); - np = eval(expr()); - /* TODO: check integer type */ + if ((np = expr()) == NULL) + error("expected expression before '%s'", yytext); + if ((np = convert(np, inttype, 0)) == NULL) + error("incorrect type in case statement"); expect(':'); - emitlabel(lcase); pcase = xmalloc(sizeof(*pcase)); pcase->expr = np; - pcase->label = lcase; pcase->next = lswitch->head; + emitlabel(pcase->label = install("", NS_LABEL)); lswitch->head = pcase; ++lswitch->nr; }