shithub: scc

Download patch

ref: 2cba9e1b69528c16908d8cd28854211a604cd45b
parent: c946d13235a8460a8c7f2aa86e705202d77231a7
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Jun 22 14:17:01 EDT 2022

cc1: Add support for flexible array members

Flexible array members are allowed in structs when
there is at least one member in the struct and it is
the last member in the struct.

--- a/src/cmd/cc/cc1/decl.c
+++ b/src/cmd/cc/cc1/decl.c
@@ -917,8 +917,15 @@
 	}
 
 	if ((tp->prop & TDEFINED) == 0) {
-		errorp("field '%s' has incomplete type", name);
-		tp = inttype;
+		if (tp->op == ARY && tp->n.elem == 0) {
+			if (n == 0)
+				errorp("flexible array member in a struct with no named members");
+			if (ahead() != '}')
+				errorp("flexible array member not at end of struct");
+		} else {
+			errorp("field '%s' has incomplete type", name);
+			tp = inttype;
+		}
 	}
 	if (tp->op == FTN) {
 		errorp("field '%s' declared as a function", name);
--- a/src/cmd/cc/cc1/types.c
+++ b/src/cmd/cc/cc1/types.c
@@ -266,6 +266,9 @@
 	if (tp->op == FTN) {
 		siz = tp->n.elem * sizeof(Type *);
 		tp->p.pars = memcpy(xmalloc(siz), tp->p.pars, siz);
+	} else if (tp->op == ARY) {
+		/* We need alignment for flexible array members */
+		tp->align = tp->type->align;
 	}
 
 	if (curfun) {