SDL_shader_tools: Re-use current_define instead of custom expanding_macro struct member

From 3c896484f5ce57148bf3e77cb54f09917cdb979e Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Fri, 22 Sep 2023 19:25:33 +0200
Subject: [PATCH] Re-use current_define instead of custom expanding_macro
 struct member

Also fix error message.
---
 SDL_shader_internal.h                            | 1 -
 SDL_shader_lexer.c                               | 2 +-
 SDL_shader_lexer.re                              | 2 +-
 SDL_shader_preprocessor.c                        | 4 +---
 unit_tests/preprocessor/errors/unknown-directive | 1 +
 5 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/SDL_shader_internal.h b/SDL_shader_internal.h
index bc608a5..0fe9c02 100644
--- a/SDL_shader_internal.h
+++ b/SDL_shader_internal.h
@@ -250,7 +250,6 @@ typedef struct IncludeState
     const unsigned char *lexer_marker;
     SDL_bool report_whitespace;
     SDL_bool asm_comments;
-    SDL_bool expanding_macro;
     size_t orig_length;
     size_t bytes_left;
     Sint32 line;
diff --git a/SDL_shader_lexer.c b/SDL_shader_lexer.c
index 214686d..d0db009 100644
--- a/SDL_shader_lexer.c
+++ b/SDL_shader_lexer.c
@@ -65,7 +65,7 @@ Token preprocessor_lexer(IncludeState *s)
 
 
     // preprocessor directives are only valid at start of line, and when not expanding a macro.
-    if (s->tokenval == ((Token) '\n') && !s->expanding_macro) {
+    if (s->tokenval == ((Token) '\n') && s->current_define == NULL) {
         goto ppdirective;  // may jump back to scanner_loop.
     }
 
diff --git a/SDL_shader_lexer.re b/SDL_shader_lexer.re
index bcb9235..8b82c40 100644
--- a/SDL_shader_lexer.re
+++ b/SDL_shader_lexer.re
@@ -76,7 +76,7 @@ Token preprocessor_lexer(IncludeState *s)
 */
 
     // preprocessor directives are only valid at start of line, and when not expanding a macro.
-    if (s->tokenval == ((Token) '\n') && !s->expanding_macro) {
+    if (s->tokenval == ((Token) '\n') && s->current_define == NULL) {
         goto ppdirective;  // may jump back to scanner_loop.
     }
 
diff --git a/SDL_shader_preprocessor.c b/SDL_shader_preprocessor.c
index 558d29c..7f90aa8 100644
--- a/SDL_shader_preprocessor.c
+++ b/SDL_shader_preprocessor.c
@@ -939,7 +939,7 @@ static void handle_pp_bad(Context *ctx)
 {
     IncludeState *state = ctx->include_stack;
 
-    failf(ctx, "unknown directive \"%s\"", state->token);
+    failf(ctx, "unknown directive \"%.*s\"", state->tokenlen, state->token);
 }
 
 
@@ -1274,7 +1274,6 @@ static SDL_bool replace_and_push_macro(Context *ctx, const Define *def, const De
     }
 
     state = ctx->include_stack;
-    state->expanding_macro = SDL_TRUE;
     while (lexer(state) != TOKEN_EOI) {
         SDL_bool wantorig = SDL_FALSE;
         const Define *arg = NULL;
@@ -1341,7 +1340,6 @@ static SDL_bool replace_and_push_macro(Context *ctx, const Define *def, const De
             goto replace_and_push_macro_failed;
         }
     }
-    state->expanding_macro = SDL_FALSE;
 
     final = buffer_flatten(buffer);
     if (!final) {
diff --git a/unit_tests/preprocessor/errors/unknown-directive b/unit_tests/preprocessor/errors/unknown-directive
index 5589088..def77e2 100644
--- a/unit_tests/preprocessor/errors/unknown-directive
+++ b/unit_tests/preprocessor/errors/unknown-directive
@@ -1 +1,2 @@
 #something
+code