ref: e4e130d48a89642db29f4aefbb8a416bbfe60abe
parent: fbd7247cc8cf0a6829d6a9a1e7e9f26f30e4005d
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Jan 19 12:39:48 EST 2016
Allow initializatio of arrays without braces This patch allows something like: int m[4][2] = {1,2,3,4,5,6,7,8};
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -186,6 +186,7 @@
IDEN,
SCLASS,
CONSTANT,
+ STRING,
SIZEOF,
INDIR,
INC,
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -734,7 +734,7 @@
char c, *s, *t;
for (next(); yytoken != EOFTOK; next()) {
- if (yytoken != CONSTANT || *yytext != '"') {
+ if (yytoken != STRING) {
printf("%s ", yytext);
continue;
}
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -616,6 +616,7 @@
sym = yylval.sym;
switch (yytoken) {
+ case STRING:
case CONSTANT:
np = constnode(sym);
next();
--- a/cc1/init.c
+++ b/cc1/init.c
@@ -96,8 +96,14 @@
Type *btp;
size_t len;
- np = (accept('{')) ? initlist(tp) : assign();
+ if ((tp->op == ARY || tp->op == STRUCT) &&
+ yytoken != '{' && yytoken != STRING) {
+ return initlist(tp);
+ }
+
+ np = (yytoken == '{') ? initlist(tp) : assign();
sym = np->sym;
+
if (sym && sym->flags&ISSTRING && tp->op == ARY) {
btp = tp->type;
if (btp != chartype &&
@@ -123,8 +129,8 @@
return np;
if ((aux = convert(decay(np), tp, 0)) != NULL)
return aux;
-
errorp("incorrect initializer");
+
return_zero:
return constnode(zero);
}
@@ -168,9 +174,6 @@
{
struct designator *dp;
- if (ip->pos > ip->max)
- ip->max = ip->pos;
-
dp = xmalloc(sizeof(*dp));
dp->pos = ip->pos;
dp->expr = np;
@@ -188,7 +191,7 @@
initlist(Type *tp)
{
Init in;
- int toomany = 0, outbound;
+ int braces, scalar, toomany, outbound;
Type *newtp;
Node *np;
@@ -196,7 +199,11 @@
in.type = tp;
in.pos = 0;
in.max = 0;
+ braces = scalar = toomany = 0;
+ if (accept('{'))
+ braces = 1;
+
do {
if (yytoken == '}')
break;
@@ -226,7 +233,9 @@
break;
default:
newtp = tp;
- warn("braces around scalar initializer");
+ if (!scalar)
+ warn("braces around scalar initializer");
+ scalar = 1;
if (in.pos == 0)
break;
if (!toomany)
@@ -242,15 +251,19 @@
else
newdesig(&in, np);
+ if (in.pos > in.max)
+ in.max = in.pos;
if (++in.pos == 0)
errorp("compound literal too big");
-
+ if (tp->n.elem == in.pos && !braces)
+ break;
} while (accept(','));
- expect('}');
+ if (braces)
+ expect('}');
if (tp->op == ARY && !tp->defined) {
- tp->n.elem = in.pos;
+ tp->n.elem = in.max;
tp->defined = 1;
}
if (tp->op == ARY || tp->op == STRUCT)
--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -486,7 +486,7 @@
yylval.sym = newstring(yytext+1, yylen-1);
*bp++ = '"';
*bp = '\0';
- return CONSTANT;
+ return STRING;
}
static unsigned