Skip to content

Commit 2cbc103

Browse files
committed
fix(mcr,lex): move processing of comma omission to expansion site (#136)
1 parent f608340 commit 2cbc103

2 files changed

Lines changed: 18 additions & 33 deletions

File tree

‎lib/lex.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ struct lex_t {
2929
unsigned blue: 1; /* true if painted blue */
3030
unsigned noarg: 1; /* true if token came from ## */
3131
unsigned sync: 2; /* 1: #include start, 2: end */
32-
unsigned vaarg: 1; /* true if __VA_ARGS__ diagnosed */
33-
unsigned vaopt: 1; /* true if removed when nothing given for __VA_ARGS__ */
32+
unsigned vaarg: 2; /* 1: __VA_ARGS__ diagnosed, 2: used for extension */
3433
} f;
3534
struct lex_t *next; /* link for token list */
3635
};

‎lib/mcr.c‎

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ static void chkexp(struct pel *l, lex_t *p[])
433433
t = NULL;
434434
break;
435435
} else if (pt->id == LEX_PASTEOP) {
436-
if ((q = pelookup(l, t)) != NULL)
436+
if ((q = pelookup(l, t)) != NULL && t->f.vaarg != 2)
437437
q->expand--;
438438
} else
439439
first = 0;
@@ -632,14 +632,11 @@ lex_t *(mcr_define)(const lmap_t *pos, int cmd)
632632
} else if (t->id == LEX_DSHARP) {
633633
err_dpos(t->pos, ERR_PP_TWODSHARP);
634634
return t;
635-
} else if (main_opt()->extension && v && pt->id == ',' && t->id == LEX_ID) {
635+
} else if (main_opt()->extension && v && pt->id == ',' && t->id == LEX_ID &&
636+
t->spell[0] == '_') {
636637
s = LEX_SPELL(t);
637-
if (MCR_ISVAARGS(s)) {
638-
pt->f.vaopt = 1;
639-
pt->next = l->next, l = pt;
640-
pt = ts;
641-
continue;
642-
}
638+
if (MCR_ISVAARGS(s))
639+
t->f.vaarg = 2;
643640
}
644641
ts->id = LEX_PASTEOP;
645642
sharp = 1;
@@ -989,7 +986,7 @@ static lex_t *stringify(lex_t ***pq, struct pl *pl, const lmap_t *dpos, const lm
989986
*/
990987
static int sharp(lex_t ***pq, lex_t *t1, struct pl *pl, lex_t **ll, int noarg)
991988
{
992-
int nend = 0;
989+
int nend, vaopt;
993990
lex_t *l, *t2;
994991
lex_t *dsl = NULL;
995992
const lmap_t *spos, *ppos, /* # and ## */
@@ -1000,6 +997,7 @@ static int sharp(lex_t ***pq, lex_t *t1, struct pl *pl, lex_t **ll, int noarg)
1000997
assert(t1);
1001998
assert(ll);
1002999

1000+
nend = vaopt = 0;
10031001
spos = ppos = NULL;
10041002
diagds = err_chkwarn(ERR_PP_ORDERDS);
10051003

@@ -1020,6 +1018,11 @@ static int sharp(lex_t ***pq, lex_t *t1, struct pl *pl, lex_t **ll, int noarg)
10201018
if (*r && (*r)->id == LEX_PASTEOP) {
10211019
*pq = nextnsp(r+1) + 1;
10221020
t2 = (*pq)[-1];
1021+
if (t2->f.vaarg == 2) {
1022+
vaopt = noarg;
1023+
(*pq)--; /* to revisit __VA_ARGS__ */
1024+
break;
1025+
}
10231026
if (!nend) {
10241027
l = lst_append(l, lex_make(LEX_MCR, NULL, 0));
10251028
nend = 1;
@@ -1045,13 +1048,14 @@ static int sharp(lex_t ***pq, lex_t *t1, struct pl *pl, lex_t **ll, int noarg)
10451048
if (nend) {
10461049
if (!isempty(t1)) { /* t1 already has correct u->m chain */
10471050
((lmap_t *)tpos)->u.m = lmap_range(fpos, lpos);
1048-
l = lst_append(l, lst_copy(t1, 0, strg_line));
1051+
if (!vaopt)
1052+
l = lst_append(l, lst_copy(t1, 0, strg_line));
10491053
}
10501054
l = lst_append(l, lex_make(LEX_MCR, NULL, 1));
10511055
}
10521056

10531057
*ll = l;
1054-
return nend;
1058+
return nend | vaopt;
10551059
}
10561060

10571061

@@ -1358,9 +1362,7 @@ int (mcr_expand)(lex_t *t)
13581362
}
13591363
for (q = p->rl; *q; ) {
13601364
t = *q++;
1361-
if (t->f.vaopt && noarg)
1362-
continue;
1363-
if (p->f.sharp && sharp(&q, t, pl, &l))
1365+
if (p->f.sharp && sharp(&q, t, pl, &l, noarg))
13641366
continue;
13651367
else if (t->id == LEX_ID) {
13661368
if (pl && (r = plookup(pl, t)) != NULL) {
@@ -1716,7 +1718,6 @@ void (mcr_listdef)(FILE *fp)
17161718

17171719
for (h = 0; h < mtab.n; h++)
17181720
for (p = mtab.t[h]; p; p = p->link) {
1719-
int opt = 0;
17201721
if (p->f.dynamic)
17211722
continue;
17221723
fprintf(fp, "#define %s", p->chn);
@@ -1737,24 +1738,9 @@ void (mcr_listdef)(FILE *fp)
17371738
}
17381739
if (p->rl[0]) {
17391740
putc(' ', fp);
1740-
for (pt = p->rl; *pt; pt++) {
1741-
if ((*pt)->f.vaopt != opt) {
1742-
if ((*pt)->f.vaopt)
1743-
fputs("__VA_OPT__(", fp);
1744-
else {
1745-
fputs(") ", fp);
1746-
if ((*pt)->id == LEX_SPACE) {
1747-
pt++;
1748-
assert(*pt);
1749-
}
1750-
}
1751-
}
1741+
for (pt = p->rl; *pt; pt++)
17521742
fputs(LEX_SPELL(*pt), fp);
1753-
opt = (*pt)->f.vaopt;
1754-
}
17551743
}
1756-
if (opt)
1757-
putc(')', fp);
17581744
putc('\n', fp);
17591745
}
17601746
}

0 commit comments

Comments
 (0)