From 64f4b00d342417fd49fbdebe0759bb85b8db4ec7 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Mon, 11 Jul 2022 15:27:51 +0200 Subject: [PATCH] tccpp: Fix #ifdef in macro args see testcase. We have to "empty" the macro-stack string on end_macro, as it may be 'tokstr_buf' which is going to be used twice in recursive invocations of macro_subst_tok. The uses aren't overlapping but the first one needs to be properly finalized. --- tccpp.c | 1 + tests/pp/22.c | 12 ++++++++++++ tests/pp/22.expect | 2 ++ 3 files changed, 15 insertions(+) create mode 100644 tests/pp/22.c create mode 100644 tests/pp/22.expect diff --git a/tccpp.c b/tccpp.c index a5f6d7af..057da007 100644 --- a/tccpp.c +++ b/tccpp.c @@ -1189,6 +1189,7 @@ ST_FUNC void end_macro(void) macro_stack = str->prev; macro_ptr = str->prev_ptr; file->line_num = str->save_line_num; + str->len = 0; /* matters if str not alloced, may be tokstr_buf */ if (str->alloc != 0) { if (str->alloc == 2) str->str = NULL; /* don't free */ diff --git a/tests/pp/22.c b/tests/pp/22.c new file mode 100644 index 00000000..76e23154 --- /dev/null +++ b/tests/pp/22.c @@ -0,0 +1,12 @@ +#define TRACE(a,b,c) X a X b X c X +#define rettrue(x) 1 +A rettrue(bla) B +TRACE( + ARG_1, +#if rettrue(bla) + ARG_2, +#else + ARG_2_wrong, +#endif + ARG_3 +); diff --git a/tests/pp/22.expect b/tests/pp/22.expect new file mode 100644 index 00000000..ceaea030 --- /dev/null +++ b/tests/pp/22.expect @@ -0,0 +1,2 @@ +A 1 B +X ARG_1 X ARG_2 X ARG_3 X;