Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added error position information to MOJOSHADER_parseData.
  • Loading branch information
icculus committed Dec 10, 2008
1 parent 16caf77 commit 95e6c63
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion finderrors.c
Expand Up @@ -96,7 +96,7 @@ static int do_file(const char *profile, const char *dname, const char *fn, int *
#else
const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, buf, rc, NULL, 0, NULL, NULL, NULL);
if (pd->error != NULL)
report("FAIL: %s %s\n", fname, pd->error);
report("FAIL: %s (position %d) %s\n", fname, pd->error_position, pd->error);
else
report("PASS: %s\n", fname);
MOJOSHADER_freeParseData(pd);
Expand Down
13 changes: 12 additions & 1 deletion mojoshader.c
Expand Up @@ -161,8 +161,10 @@ struct Context
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
const uint32 *orig_tokens;
const uint32 *tokens;
uint32 tokencount;
int started_parsing;
const MOJOSHADER_swizzle *swizzles;
unsigned int swizzles_count;
OutputList *output;
Expand Down Expand Up @@ -234,7 +236,7 @@ struct Context
// Convenience functions for allocators...

MOJOSHADER_parseData out_of_mem_data = {
"Out of memory", 0, 0, 0, 0, MOJOSHADER_TYPE_UNKNOWN, 0, 0, 0, 0
"Out of memory", -1, 0, 0, 0, 0, MOJOSHADER_TYPE_UNKNOWN, 0, 0, 0, 0
};

const char *out_of_mem_str = "Out of memory";
Expand Down Expand Up @@ -6627,6 +6629,7 @@ static Context *build_context(const char *profile,
ctx->free = f;
ctx->malloc_data = d;
ctx->tokens = (const uint32 *) tokenbuf;
ctx->orig_tokens = (const uint32 *) tokenbuf;
ctx->tokencount = bufsize / sizeof (uint32);
ctx->swizzles = swiz;
ctx->swizzles_count = swizcount;
Expand Down Expand Up @@ -7083,11 +7086,16 @@ static MOJOSHADER_parseData *build_parsedata(Context *ctx)
Free(ctx, samplers);
} // if

if (ctx->started_parsing)
retval->error_position = (ctx->tokens - ctx->orig_tokens) * sizeof (uint32);
else
retval->error_position = -1;
retval->error = ctx->failstr; // we recycle. :)
ctx->failstr = NULL; // don't let this get free()'d too soon.
} // if
else
{
retval->error_position = -2;
retval->profile = ctx->profile->name;
retval->output = output;
retval->output_len = ctx->output_len;
Expand Down Expand Up @@ -7297,7 +7305,10 @@ const MOJOSHADER_parseData *MOJOSHADER_parse(const char *profile,

// Version token always comes first.
if (!isfail(ctx))
{
ctx->started_parsing = 1;
rc = parse_version_token(ctx, profile);
} // if

// parse out the rest of the tokens after the version token...
while ( (rc > 0) && (!isfail(ctx)) )
Expand Down
9 changes: 9 additions & 0 deletions mojoshader.h
Expand Up @@ -251,6 +251,15 @@ typedef struct MOJOSHADER_parseData
*/
const char *error;

/*
* Position of error, if there is one. Will be -2 if there was no
* error, and -1 if there was an error before processing started. If >= 0,
* MOJOSHADER_parse() sets this to the byte offset (starting at zero) into
* the bytecode you supplied, and MOJOSHADER_assemble() sets this to a
* a line number in the source code you supplied (starting at zero).
*/
int error_position;

/*
* The name of the profile used to parse the shader. Will be NULL on error.
*/
Expand Down
2 changes: 1 addition & 1 deletion testoutput.c
Expand Up @@ -18,7 +18,7 @@ static int do_parse(const unsigned char *buf, const int len, const char *prof)

pd = MOJOSHADER_parse(prof, buf, len, NULL, 0, NULL, NULL, NULL);
if (pd->error != NULL)
printf("ERROR: %s\n", pd->error);
printf("ERROR: (position %d) %s\n", pd->error_position, pd->error);
else
{
retval = 1;
Expand Down
2 changes: 1 addition & 1 deletion testparse.c
Expand Up @@ -62,7 +62,7 @@ static int do_parse(const unsigned char *buf, const int len, const char *prof)
pd = MOJOSHADER_parse(prof, buf, len, NULL, 0, Malloc, Free, NULL);
printf("PROFILE: %s\n", prof);
if (pd->error != NULL)
printf("ERROR: %s\n", pd->error);
printf("ERROR: (position %d) %s\n", pd->error_position, pd->error);
else
{
retval = 1;
Expand Down

0 comments on commit 95e6c63

Please sign in to comment.