Skip to content

Commit

Permalink
Let the preprocessor work more like C, without the Microsoft tools' q…
Browse files Browse the repository at this point in the history
…uirks.
  • Loading branch information
icculus committed Feb 14, 2013
1 parent 56f82d0 commit 72e69e2
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions mojoshader_preprocessor.c
Expand Up @@ -60,6 +60,12 @@ typedef struct Context
void *malloc_data;
} Context;

// Microsoft's preprocessor has some quirks. In some ways, it doesn't work
// like you'd expect a C preprocessor to function.
#ifndef MATCH_MICROSOFT_PREPROCESSOR
#define MATCH_MICROSOFT_PREPROCESSOR 1
#endif


// Convenience functions for allocators...

Expand Down Expand Up @@ -720,6 +726,10 @@ static inline void pushback(IncludeState *state)

static Token lexer(IncludeState *state)
{
#if !MATCH_MICROSOFT_PREPROCESSOR
state->report_whitespace = 1;
#endif

if (!state->pushedback)
return preprocessor_lexer(state);
state->pushedback = 0;
Expand Down Expand Up @@ -2118,8 +2128,10 @@ static inline const char *_preprocessor_nexttoken(Preprocessor *_ctx,
ctx->parsing_pragma = 0;
else
{
#if MATCH_MICROSOFT_PREPROCESSOR
// preprocessor is line-oriented, nothing else gets newlines.
continue; // get the next thing.
#endif
} // else
} // else if

Expand Down Expand Up @@ -2161,6 +2173,7 @@ const char *preprocessor_sourcepos(Preprocessor *_ctx, unsigned int *pos)

static void indent_buffer(Buffer *buffer, int n, const int newline)
{
#if MATCH_MICROSOFT_PREPROCESSOR
static char spaces[4] = { ' ', ' ', ' ', ' ' };
if (newline)
{
Expand All @@ -2175,6 +2188,7 @@ static void indent_buffer(Buffer *buffer, int n, const int newline)
if (!buffer_append(buffer, spaces, 1))
return;
} // else
#endif
} // indent_buffer


Expand Down Expand Up @@ -2241,11 +2255,18 @@ const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *filename,
if (preprocessor_outofmemory(pp))
goto preprocess_out_of_mem;

if (token == ((Token) '\n'))
{
buffer_append(buffer, endline, sizeof (endline));
isnewline = 1;
} // else if

#if MATCH_MICROSOFT_PREPROCESSOR
// Microsoft's preprocessor is weird.
// It ignores newlines, and then inserts its own around certain
// tokens. For example, after a semicolon. This allows HLSL code to
// be mostly readable, instead of a stream of tokens.
if ( (token == ((Token) '}')) || (token == ((Token) ';')) )
else if ( (token == ((Token) '}')) || (token == ((Token) ';')) )
{
if ( (token == ((Token) '}')) && (indent > 0) )
indent--;
Expand All @@ -2257,12 +2278,6 @@ const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *filename,
isnewline = 1;
} // if

else if (token == ((Token) '\n'))
{
buffer_append(buffer, endline, sizeof (endline));
isnewline = 1;
} // else if

else if (token == ((Token) '{'))
{
buffer_append(buffer, endline, sizeof (endline));
Expand All @@ -2272,6 +2287,7 @@ const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *filename,
indent++;
isnewline = 1;
} // else if
#endif

else if (token == TOKEN_PREPROCESSING_ERROR)
{
Expand Down

0 comments on commit 72e69e2

Please sign in to comment.