cproc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/cproc
Log | Files | Refs | Submodules | README | LICENSE

commit 231137aceb65c4b1ca0b54dd599f1f8b8c5c9342
parent bdefdc936a6c2be8f6c814c82ef9b36a432051e4
Author: Michael Forney <mforney@mforney.org>
Date:   Sat, 27 Apr 2019 00:25:13 -0700

init: Allow string literal initializers to be in braces

Diffstat:
Minit.c | 36++++++++++++++++++------------------
Atest/initializer-string-braces.c | 1+
Atest/initializer-string-braces.qbe | 1+
3 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/init.c b/init.c @@ -163,8 +163,6 @@ focus(struct initparser *p) p->sub->mem = p->sub->type->structunion.members; t = p->sub->mem->type; break; - default: - t = p->sub->type; } subobj(p, t, 0); } @@ -224,13 +222,16 @@ parseinit(struct scope *s, struct type *t) designator(s, &p); else if (p.sub != p.cur) advance(&p); - else + else if (p.cur->type->kind == TYPESTRUCT || p.cur->type->kind == TYPEUNION) focus(&p); } - if (tok.kind == TLBRACE) { - next(); - if (p.cur && p.cur->type == p.sub->type) - error(&tok.loc, "nested braces around scalar initializer"); + if (consume(TLBRACE)) { + if (p.cur == p.sub) { + if (p.cur->type->prop & PROPSCALAR) + error(&tok.loc, "nested braces around scalar initializer"); + assert(p.cur->type->kind == TYPEARRAY); + focus(&p); + } p.cur = p.sub; p.cur->iscur = true; continue; @@ -240,17 +241,16 @@ parseinit(struct scope *s, struct type *t) t = p.sub->type; switch (t->kind) { case TYPEARRAY: - if (expr->decayed && expr->base->kind == EXPRSTRING) { - expr = expr->base; - base = t->base; - /* XXX: wide string literals */ - if (!(base->prop & PROPCHAR)) - error(&tok.loc, "array initializer is string literal with incompatible type"); - if (t->incomplete) - updatearray(t, expr->string.size); - goto add; - } - break; + if (!expr->decayed || expr->base->kind != EXPRSTRING) + break; + base = t->base; + /* XXX: wide string literals */ + if (!(base->prop & PROPCHAR)) + break; + expr = expr->base; + if (t->incomplete) + updatearray(t, expr->string.size); + goto add; case TYPESTRUCT: case TYPEUNION: if (typecompatible(expr->type, t)) diff --git a/test/initializer-string-braces.c b/test/initializer-string-braces.c @@ -0,0 +1 @@ +char s[] = {"abc"}; diff --git a/test/initializer-string-braces.qbe b/test/initializer-string-braces.qbe @@ -0,0 +1 @@ +export data $s = align 1 { b "abc", z 1, }