Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added support for #error directive to the preprocessor.
  • Loading branch information
icculus committed Feb 13, 2009
1 parent e77fcf3 commit 7da8497
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
4 changes: 0 additions & 4 deletions mojoshader_assembler.c
Expand Up @@ -1254,10 +1254,6 @@ static int parse_condition(Context *ctx, uint32 *controls)
return 0;
} // parse_condition

static inline int Min(const int a, const int b)
{
return ((a < b) ? a : b);
} // Min

static int parse_instruction_token(Context *ctx, Token token)
{
Expand Down
5 changes: 5 additions & 0 deletions mojoshader_internal.h
Expand Up @@ -119,6 +119,11 @@ typedef int32_t int32;
# define SWAP32(x) (x)
#endif

static inline int Min(const int a, const int b)
{
return ((a < b) ? a : b);
} // Min

// This is the ID for a D3DXSHADER_CONSTANTTABLE in the bytecode comments.
#define CTAB_ID 0x42415443 // 0x42415443 == 'CTAB'
#define CTAB_SIZE 28 // sizeof (D3DXSHADER_CONSTANTTABLE).
Expand Down
50 changes: 48 additions & 2 deletions mojoshader_preprocessor.c
Expand Up @@ -27,7 +27,7 @@ typedef struct Context
{
int isfail;
int out_of_memory;
char failstr[128];
char failstr[256];
IncludeState *include_stack;
DefineHash *define_hashtable[256];
int pushedback;
Expand Down Expand Up @@ -392,6 +392,47 @@ int preprocessor_outofmemory(Preprocessor *_ctx)
} // preprocessor_outofmemory


static void handle_pp_error(Context *ctx)
{
IncludeState *state = ctx->include_stack;
const char *data = NULL;
int done = 0;

while (!done)
{
const char *source = state->source;
const Token token = preprocessor_internal_lexer(state);
switch (token)
{
case TOKEN_INCOMPLETE_COMMENT:
state->source = source; // move back so we catch this later.
done = 1;
break;

case ((Token) '\n'):
case TOKEN_EOI:
done = 1;
break;

default:
if (data == NULL)
data = state->token; // skip #error token.
break;
} // switch
} // while

const char *prefix = "#error ";
const size_t prefixlen = strlen(prefix);
const int len = (int) (state->source - data);
const int cpy = Min(len, sizeof (ctx->failstr) - prefixlen);
strcpy(ctx->failstr, prefix);
if (cpy > 0)
memcpy(ctx->failstr + prefixlen, data, cpy);
ctx->failstr[cpy + prefixlen] = '\0';
ctx->isfail = 1;
} // handle_pp_error


static inline const char *_preprocessor_nexttoken(Preprocessor *_ctx,
unsigned int *_len, Token *_token)
{
Expand Down Expand Up @@ -426,7 +467,6 @@ static inline const char *_preprocessor_nexttoken(Preprocessor *_ctx,
// TOKEN_PP_ELSE,
// TOKEN_PP_ELIF,
// TOKEN_PP_ENDIF,
// TOKEN_PP_ERROR,

Token token = preprocessor_internal_lexer(state);
if (token == TOKEN_EOI)
Expand All @@ -442,6 +482,12 @@ static inline const char *_preprocessor_nexttoken(Preprocessor *_ctx,
continue; // will return at top of loop.
} // else if

else if (token == TOKEN_PP_ERROR)
{
handle_pp_error(ctx);
continue; // will return at top of loop.
} // else if

*_token = token;
*_len = (unsigned int) (state->source - state->token);
return state->token;
Expand Down

0 comments on commit 7da8497

Please sign in to comment.