shithub: scc

Download patch

ref: 60593515d461f8665b59f7c51eca7556fff69256
parent: b0ffc63f8087baa7e5c77911b7b6fb14c49ec306
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Jul 10 09:49:48 EDT 2014

Check storage class in initializer

Only defined elements can be be initialized. I am not sure if is an
error initialize an extern symbol, or it is only a warning. I
suspect it should be an error, but gcc only emits a warning.

--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -240,12 +240,18 @@
 	error("invalid type specification");
 }
 
-/* TODO: check storage class and correctness of the initializator  */
+/* TODO: check correctness of the initializator  */
+/* TODO: emit initializer */
 static struct node *
-initializer(register Type *tp)
+initializer(Symbol *sym)
 {
+	register Type *tp = sym->type;
+
+	if (!sym->s.isdefined)
+		error("'%s' initialized and declared extern", sym->name);
+
 	if (accept('{')) {
-		initializer(tp);
+		initializer(sym);
 		expect('}');
 	} else {
 		do {
@@ -375,7 +381,7 @@
 		sym = newiden();
 		sym->type = inttype;
 		if (accept('='))
-			initializer(inttype);
+			initializer(sym);
 		sym->u.i = val++;
 		newfield(tp, sym);
 		if (!accept(','))
@@ -405,7 +411,7 @@
 			case AUTO: default: sym->s.isauto = 1;
 			}
 			if (accept('='))
-				initializer(sym->type); /* TODO: emit initializer */
+				initializer(sym);
 			emitdcl(sym);
 		} while (accept(','));
 	}
@@ -453,7 +459,7 @@
 
 			if (BTYPE(tp) != FTN) {
 				if (accept('='))
-					initializer(tp);
+					initializer(sym);
 				emitdcl(sym);
 			} else if (yytoken == '{') {
 				curfun = sym;