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 9b0d3e51d78f211a99c79ad96e6849481372f4e1
parent df5c3cf3c53f91e4135508cd9925784b4b2d842c
Author: Michael Forney <mforney@mforney.org>
Date:   Wed, 18 Mar 2020 00:29:20 -0700

pp: Finish '#' operator

Diffstat:
Mpp.c | 13+++++++++++--
Atest/preprocess-macro-stringize-2.c | 5+++++
Atest/preprocess-macro-stringize-2.pp | 3+++
3 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/pp.c b/pp.c @@ -386,9 +386,18 @@ stringize(struct array *buf, struct token *t) { const char *lit; + if ((t->space || t->kind == TNEWLINE) && ((char *)buf->val)[buf->len - 1] != ' ') + arrayaddbuf(buf, " ", 1); lit = t->lit ? t->lit : tokstr[t->kind]; - /* XXX: double escape string literal */ - arrayaddbuf(buf, lit, strlen(lit)); + if (t->kind == TSTRINGLIT || t->kind == TCHARCONST) { + for (; *lit; ++lit) { + if (*lit == '\\' || *lit == '"') + arrayaddbuf(buf, "\\", 1); + arrayaddbuf(buf, lit, 1); + } + } else if (lit) { + arrayaddbuf(buf, lit, strlen(lit)); + } } static bool diff --git a/test/preprocess-macro-stringize-2.c b/test/preprocess-macro-stringize-2.c @@ -0,0 +1,5 @@ +#define str(x) # x +str(@) +str(12 x- 3 + abc) +str('"' "\\") diff --git a/test/preprocess-macro-stringize-2.pp b/test/preprocess-macro-stringize-2.pp @@ -0,0 +1,3 @@ +"@" +"12 x- 3 abc" +"'\"' \"\\\\\""