From 95e6c633d83f10b5f9bac4e7258b5dafa95f5fc7 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 10 Dec 2008 03:48:38 -0500 Subject: [PATCH] Added error position information to MOJOSHADER_parseData. --- finderrors.c | 2 +- mojoshader.c | 13 ++++++++++++- mojoshader.h | 9 +++++++++ testoutput.c | 2 +- testparse.c | 2 +- 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/finderrors.c b/finderrors.c index f71eec8a..b364570d 100644 --- a/finderrors.c +++ b/finderrors.c @@ -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); diff --git a/mojoshader.c b/mojoshader.c index 310d31c2..cf1b832e 100644 --- a/mojoshader.c +++ b/mojoshader.c @@ -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; @@ -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"; @@ -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; @@ -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; @@ -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)) ) diff --git a/mojoshader.h b/mojoshader.h index 541028c3..cbac5a19 100644 --- a/mojoshader.h +++ b/mojoshader.h @@ -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. */ diff --git a/testoutput.c b/testoutput.c index a4d80905..33097493 100644 --- a/testoutput.c +++ b/testoutput.c @@ -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; diff --git a/testparse.c b/testparse.c index 0c565782..b2c89986 100644 --- a/testparse.c +++ b/testparse.c @@ -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;