diff options
author | Michael Forney <mforney@mforney.org> | 2020-03-17 15:19:24 -0700 |
---|---|---|
committer | Michael Forney <mforney@mforney.org> | 2020-03-17 15:22:51 -0700 |
commit | 46db27cb5171a91d49fa653b6c4bc429efa9e695 (patch) | |
tree | 5e619273f6d21b5c35144b614ed7461012991a2d | |
parent | bad98e94caf37db71f8114bdb8562a7caa4fb256 (diff) |
pp: Fix some whitespace after preprocessing
-rw-r--r-- | pp.c | 20 | ||||
-rw-r--r-- | test/preprocess-standard-example-2.pp | 8 |
2 files changed, 17 insertions, 11 deletions
@@ -129,7 +129,7 @@ framenext(struct frame *f) /* push a new context frame */ static struct frame * -ctxpush(struct token *t, size_t n, struct macro *m) +ctxpush(struct token *t, size_t n, struct macro *m, bool space) { struct frame *f; @@ -137,6 +137,8 @@ ctxpush(struct token *t, size_t n, struct macro *m) f->token = t; f->ntoken = n; f->macro = m; + if (n > 0) + t[0].space = space; return f; } @@ -162,6 +164,7 @@ ctxnext(void) struct frame *f; struct token *t; struct macro *m; + bool space; size_t i; again: @@ -171,6 +174,7 @@ again: m = f->macro; if (m && m->kind == MACROFUNC) { /* try to expand macro parameter */ + space = f->token->space; switch (f->token->kind) { case THASH: framenext(f); @@ -178,7 +182,7 @@ again: assert(t && t->kind == TIDENT); i = macroparam(m, t->lit); assert(i != -1); - f = ctxpush(&m->arg[i].str, 1, NULL); + f = ctxpush(&m->arg[i].str, 1, NULL, space); break; case TIDENT: i = macroparam(m, f->token->lit); @@ -187,7 +191,7 @@ again: framenext(f); if (m->arg[i].ntoken == 0) goto again; - f = ctxpush(m->arg[i].token, m->arg[i].ntoken, NULL); + f = ctxpush(m->arg[i].token, m->arg[i].ntoken, NULL, space); break; } /* XXX: token concatenation */ @@ -234,7 +238,6 @@ define(void) /* read macro body */ m->param = params.val; m->nparam = params.len / sizeof(m->param[0]); - t->space = false; while (t->kind != TNEWLINE) { if (t->kind == THASHHASH) error(&t->loc, "'##' operator is not yet implemented"); @@ -377,7 +380,8 @@ peekparen(void) while (t->kind == TNEWLINE); if (t->kind == TLPAREN) return true; - ctxpush(pending.val, pending.len / sizeof(*t), NULL); + t = pending.val; + ctxpush(t, pending.len / sizeof(*t), NULL, t[0].space); return false; } @@ -398,6 +402,7 @@ expand(struct token *t) struct macroarg *arg; struct array str, tok; size_t i, depth, paren; + bool space; if (t->kind != TIDENT) return false; @@ -406,6 +411,7 @@ expand(struct token *t) t->hide = true; return false; } + space = t->space; if (m->kind == MACROFUNC) { if (!peekparen()) return false; @@ -464,7 +470,7 @@ expand(struct token *t) } m->arg = arg; } - ctxpush(m->token, m->ntoken, m); + ctxpush(m->token, m->ntoken, m, space); m->hide = true; ++macrodepth; return true; @@ -579,7 +585,7 @@ peek(int kind) } pending = tok; tok = old; - ctxpush(&pending, 1, NULL); + ctxpush(&pending, 1, NULL, pending.space); return false; } diff --git a/test/preprocess-standard-example-2.pp b/test/preprocess-standard-example-2.pp index 3a52eb0..de72fb1 100644 --- a/test/preprocess-standard-example-2.pp +++ b/test/preprocess-standard-example-2.pp @@ -1,4 +1,4 @@ -f(2 * (y+1)) +f(2 * (f(2 * (z[0])))) %f(2 * (0)) + t(1); -f(2 * (2+(3,4)-0,1)) |f(2 * (~ 5)) &f(2 * (0,1))^m(0,1); -int i[] = {1, }; -char c[2][6] = {"hello","" }; +f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1); +f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1); +int i[] = { 1, }; +char c[2][6] = { "hello", "" }; |