Handle basic macro replacement in the preprocessor.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 17 Feb 2009 08:41:23 -0500
changeset 644 ea8b50a4edaf
parent 643 20403ca72f54
child 645 9a1f42e8f3f7
Handle basic macro replacement in the preprocessor.
mojoshader_preprocessor.c
--- a/mojoshader_preprocessor.c	Tue Feb 17 08:40:34 2009 -0500
+++ b/mojoshader_preprocessor.c	Tue Feb 17 08:41:23 2009 -0500
@@ -1060,6 +1060,28 @@
 } // handle_pp_endif
 
 
+static int handle_pp_identifier(Context *ctx)
+{
+    IncludeState *state = ctx->include_stack;
+    const unsigned int len = ((unsigned int) (state->source-state->token));
+    char *sym = (char *) alloca(len+1);
+    memcpy(sym, state->token, len);
+    sym[len] = '\0';
+
+    const char *def = find_define(ctx, sym);
+    if (def == NULL)
+        return 0;   // just send the token through unchanged.
+
+    if (!push_source(ctx, state->filename, def, strlen(def), state->line, 0))
+    {
+        assert(ctx->out_of_memory);
+        return 0;
+    } // if
+
+    return 1;
+} // handle_pp_identifier
+
+
 static void unterminated_pp_condition(Context *ctx)
 {
     IncludeState *state = ctx->include_stack;
@@ -1192,6 +1214,12 @@
             continue;  // will return at top of loop.
         } // else if
 
+        else if (token == TOKEN_IDENTIFIER)
+        {
+            if (handle_pp_identifier(ctx))
+                continue;  // pushed the include_stack.
+        } // else if
+
         assert(!skipping);
         *_token = token;
         *_len = (unsigned int) (state->source - state->token);