From 4264a97f7b08ada4925d67880625a98d30017516 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Thu, 12 Feb 2009 02:41:43 -0500 Subject: [PATCH] Allow app to specify a base filename for error messages. --- assemble.c | 9 +++++---- finderrors.c | 2 +- mojoshader.h | 29 ++++++++++++++++++++++------- mojoshader_assembler.c | 11 ++++++----- mojoshader_preprocessor.c | 7 +++---- preprocess.c | 7 ++++--- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/assemble.c b/assemble.c index 15ca9bda..16fb21c5 100644 --- a/assemble.c +++ b/assemble.c @@ -11,7 +11,8 @@ #include #include "mojoshader.h" -static int assemble(const char *buf, int len, const char *outfile) +static int assemble(const char *fname, const char *buf, int len, + const char *outfile) { FILE *io = fopen(outfile, "wb"); if (io == NULL) @@ -23,8 +24,8 @@ static int assemble(const char *buf, int len, const char *outfile) const MOJOSHADER_parseData *pd; int retval = 0; - pd = MOJOSHADER_assemble(buf, len, NULL, 0, NULL, 0, NULL, 0, - NULL, NULL, NULL, NULL, NULL); + pd = MOJOSHADER_assemble(fname, buf, len, NULL, 0, NULL, 0, + NULL, 0, NULL, NULL, NULL, NULL, NULL); if (pd->error_count > 0) { int i; @@ -76,7 +77,7 @@ int main(int argc, char **argv) printf(" ... fread('%s') failed.\n", infile); else { - if (assemble(buf, rc, outfile)) + if (assemble(infile, buf, rc, outfile)) retval = 0; else remove(outfile); diff --git a/finderrors.c b/finderrors.c index ac89a002..3ae823d9 100644 --- a/finderrors.c +++ b/finderrors.c @@ -84,7 +84,7 @@ static int do_file(const char *profile, const char *dname, const char *fn, int * const MOJOSHADER_parseData *a; buf[rc] = '\0'; // make sure the source is null-terminated. - a = MOJOSHADER_assemble((char *) buf, rc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + a = MOJOSHADER_assemble(fname, (char *) buf, rc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); if (a->error_count > 0) { diff --git a/mojoshader.h b/mojoshader.h index a6f8269f..ecd83fba 100644 --- a/mojoshader.h +++ b/mojoshader.h @@ -329,7 +329,8 @@ typedef struct MOJOSHADER_error const char *error; /* - * Filename where error happened. + * Filename where error happened. This can be NULL if the information + * isn't available. */ const char *filename; @@ -747,7 +748,14 @@ typedef void (*MOJOSHADER_includeClose)(const char *data, * * This function maps to D3DXPreprocessShader(). * - * (source) is an ASCII string of text to preprocess. It does not need to be + * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not + * actually access this file, as we obtain our data from (source). This + * string is copied when we need to report errors while processing (source), + * as opposed to errors in a file referenced via the #include directive in + * (source). If this is NULL, then errors will report the filename as NULL, + * too. + * + * (source) is an string of UTF-8 text to preprocess. It does not need to be * NULL-terminated. * * (sourcelen) is the length of the string pointed to by (source), in bytes. @@ -781,8 +789,8 @@ typedef void (*MOJOSHADER_includeClose)(const char *data, * call. This allows you to preprocess several shaders on separate CPU cores * at the same time. */ -const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *source, - unsigned int sourcelen, +const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *filename, + const char *source, unsigned int sourcelen, const MOJOSHADER_preprocessorDefine **defines, unsigned int define_count, MOJOSHADER_includeOpen include_open, @@ -808,7 +816,14 @@ void MOJOSHADER_freePreprocessData(const MOJOSHADER_preprocessData *data); * This function is optional. Use this to convert Direct3D shader assembly * language into bytecode, which can be handled by MOJOSHADER_parse(). * - * (source) is an ASCII string of valid Direct3D shader assembly source code. + * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not + * actually access this file, as we obtain our data from (source). This + * string is copied when we need to report errors while processing (source), + * as opposed to errors in a file referenced via the #include directive in + * (source). If this is NULL, then errors will report the filename as NULL, + * too. + * + * (source) is an UTF-8 string of valid Direct3D shader assembly source code. * It does not need to be NULL-terminated. * * (sourcelen) is the length of the string pointed to by (source), in bytes. @@ -854,8 +869,8 @@ void MOJOSHADER_freePreprocessData(const MOJOSHADER_preprocessData *data); * call. This allows you to assemble several shaders on separate CPU cores * at the same time. */ -const MOJOSHADER_parseData *MOJOSHADER_assemble(const char *source, - unsigned int sourcelen, +const MOJOSHADER_parseData *MOJOSHADER_assemble(const char *filename, + const char *source, unsigned int sourcelen, const char **comments, unsigned int comment_count, const MOJOSHADER_symbol *symbols, unsigned int symbol_count, diff --git a/mojoshader_assembler.c b/mojoshader_assembler.c index c4fee399..7caeb163 100644 --- a/mojoshader_assembler.c +++ b/mojoshader_assembler.c @@ -1514,7 +1514,8 @@ static void parse_token(Context *ctx, const Token token) } // parse_token -static Context *build_context(const char *source, unsigned int sourcelen, +static Context *build_context(const char *filename, + const char *source, unsigned int sourcelen, const MOJOSHADER_preprocessorDefine **defines, unsigned int define_count, MOJOSHADER_includeOpen include_open, @@ -1537,7 +1538,7 @@ static Context *build_context(const char *source, unsigned int sourcelen, ctx->free = f; ctx->malloc_data = d; ctx->parse_phase = MOJOSHADER_PARSEPHASE_NOTSTARTED; - ctx->preprocessor = preprocessor_start(NULL, source, sourcelen, + ctx->preprocessor = preprocessor_start(filename, source, sourcelen, include_open, include_close, defines, define_count, m, f, d); @@ -1817,8 +1818,8 @@ static void output_comments(Context *ctx, const char **comments, // API entry point... -const MOJOSHADER_parseData *MOJOSHADER_assemble(const char *source, - unsigned int sourcelen, +const MOJOSHADER_parseData *MOJOSHADER_assemble(const char *filename, + const char *source, unsigned int sourcelen, const char **comments, unsigned int comment_count, const MOJOSHADER_symbol *symbols, unsigned int symbol_count, @@ -1834,7 +1835,7 @@ const MOJOSHADER_parseData *MOJOSHADER_assemble(const char *source, if ( ((m == NULL) && (f != NULL)) || ((m != NULL) && (f == NULL)) ) return &MOJOSHADER_out_of_mem_data; // supply both or neither. - ctx = build_context(source, sourcelen, defines, define_count, + ctx = build_context(filename, source, sourcelen, defines, define_count, include_open, include_close, m, f, d); if (ctx == NULL) return &MOJOSHADER_out_of_mem_data; diff --git a/mojoshader_preprocessor.c b/mojoshader_preprocessor.c index 5f79a64e..6f09fe0e 100644 --- a/mojoshader_preprocessor.c +++ b/mojoshader_preprocessor.c @@ -603,8 +603,8 @@ static MOJOSHADER_error *build_errors(ErrorList **errors, const int count, } // build_errors -const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *source, - unsigned int sourcelen, +const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *filename, + const char *source, unsigned int sourcelen, const MOJOSHADER_preprocessorDefine **defines, unsigned int define_count, MOJOSHADER_includeOpen include_open, @@ -626,8 +626,7 @@ const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *source, include_open = (MOJOSHADER_includeOpen) 0x1; include_close = (MOJOSHADER_includeClose) 0x1; - const char *fname = "*"; // !!! FIXME - Preprocessor *pp = preprocessor_start(fname, source, sourcelen, + Preprocessor *pp = preprocessor_start(filename, source, sourcelen, include_open, include_close, defines, define_count, m, f, d); diff --git a/preprocess.c b/preprocess.c index 0a6cb8b2..3c337453 100644 --- a/preprocess.c +++ b/preprocess.c @@ -11,7 +11,8 @@ #include #include "mojoshader.h" -static int preprocess(const char *buf, int len, const char *outfile) +static int preprocess(const char *fname, const char *buf, int len, + const char *outfile) { FILE *io = fopen(outfile, "wb"); if (io == NULL) @@ -23,7 +24,7 @@ static int preprocess(const char *buf, int len, const char *outfile) const MOJOSHADER_preprocessData *pd; int retval = 0; - pd = MOJOSHADER_preprocess(buf, len, NULL, 0, NULL, + pd = MOJOSHADER_preprocess(fname, buf, len, NULL, 0, NULL, NULL, NULL, NULL, NULL); if (pd->error_count > 0) @@ -77,7 +78,7 @@ int main(int argc, char **argv) printf(" ... fread('%s') failed.\n", infile); else { - if (preprocess(buf, rc, outfile)) + if (preprocess(infile, buf, rc, outfile)) retval = 0; else remove(outfile);