shithub: scc

Download patch

ref: 7d32c5733f318b04aefa28e3738bebcad8323b6a
parent: 7953f6f46ef27e1ab5bdb5cd543bba523ac3c0b3
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Jul 9 10:59:18 EDT 2014

Check type of expressions in case statement

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

--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -230,23 +230,20 @@
 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;
 }