scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 2cba9e1b69528c16908d8cd28854211a604cd45b
parent c946d13235a8460a8c7f2aa86e705202d77231a7
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 22 Jun 2022 18:17:01 +0200

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.

Diffstat:
Msrc/cmd/cc/cc1/decl.c | 11+++++++++--
Msrc/cmd/cc/cc1/types.c | 3+++
2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/cmd/cc/cc1/decl.c b/src/cmd/cc/cc1/decl.c @@ -917,8 +917,15 @@ field(struct decl *dcl) } 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); diff --git a/src/cmd/cc/cc1/types.c b/src/cmd/cc/cc1/types.c @@ -266,6 +266,9 @@ newtype(Type *base) 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) {