From 6fc3ae2b5c3230eecee19dd7b4e94368c1ecfa20 Mon Sep 17 00:00:00 2001 From: Zebediah Figura Date: Sat, 29 Jul 2023 18:15:14 -0500 Subject: [PATCH] vkd3d-shader/preproc: Stringify text immediately in macro invocations. --- libs/vkd3d-shader/preproc.l | 15 +++++++++++++++ tests/hlsl_d3d12.c | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/libs/vkd3d-shader/preproc.l b/libs/vkd3d-shader/preproc.l index 60cd662e..5e8ed2eb 100644 --- a/libs/vkd3d-shader/preproc.l +++ b/libs/vkd3d-shader/preproc.l @@ -664,6 +664,21 @@ int yylex(YYSTYPE *lval, YYLTYPE *lloc, yyscan_t scanner) break; } + /* Stringification is another special case. Unsurprisingly, + * we need to stringify if this is an argument. More + * surprisingly, we need to stringify even if it's not. */ + case T_HASHSTRING: + { + struct vkd3d_string_buffer buffer; + + vkd3d_string_buffer_init(&buffer); + preproc_stringify(ctx, &buffer, text); + if (current_arg) + preproc_text_add(current_arg, buffer.buffer); + vkd3d_string_buffer_cleanup(&buffer); + break; + } + case T_NEWLINE: if (current_arg) preproc_text_add(current_arg, " "); diff --git a/tests/hlsl_d3d12.c b/tests/hlsl_d3d12.c index 85a1548f..ef41235e 100644 --- a/tests/hlsl_d3d12.c +++ b/tests/hlsl_d3d12.c @@ -236,6 +236,27 @@ static void test_preprocess(void) "#", }, + { + "#define KEY2(x) x\n" + "#define KEY(a) KEY2(#a)\n" + "KEY(apple)", + + "\"apple\"", + }, + { + "#define KEY2(x) #x\n" + "#define KEY(a) KEY2(#a)\n" + "KEY(apple)", + + "\"\\\"apple\\\"\"", + }, + { + "#define KEY2(x) #x\n" + "#define KEY(a) KEY2(#x)\n" + "KEY(apple)", + + "\"\\\"x\\\"\"", + }, /* #pragma is preserved. */ {