From: Dale Weiler Date: Thu, 15 Nov 2012 00:28:46 +0000 (+0000) Subject: Use new console system everywhere. X-Git-Tag: 0.1~17 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=2e84cc0b418500a57d95315c0376c13a10d60142;p=xonotic%2Fgmqcc.git Use new console system everywhere. --- diff --git a/Makefile b/Makefile index ce0bc86..b2d6580 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,6 @@ OBJ = \ code.o \ ast.o \ ir.o \ - error.o \ con.o OBJ_A = test/ast-test.o OBJ_I = test/ir-test.o diff --git a/ast.c b/ast.c index 3f6c59e..10c177e 100644 --- a/ast.c +++ b/ast.c @@ -40,14 +40,14 @@ static void asterror(lex_ctx ctx, const char *msg, ...) { va_list ap; va_start(ap, msg); - cvprintmsg(ctx, LVL_ERROR, "error", msg, ap); + con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", msg, ap); va_end(ap); } /* It must not be possible to get here. */ static GMQCC_NORETURN void _ast_node_destroy(ast_node *self) { - fprintf(stderr, "ast node missing destroy()\n"); + con_err("ast node missing destroy()\n"); abort(); } diff --git a/con.c b/con.c index 623cb99..7ab5fcb 100644 --- a/con.c +++ b/con.c @@ -291,6 +291,10 @@ int con_vout(const char *fmt, va_list va) { return con_write(console.handle_out, fmt, va); } +/* + * Standard stdout/stderr printf functions used generally where they need + * to be used. + */ int con_err(const char *fmt, ...) { va_list va; int ln = 0; @@ -307,3 +311,55 @@ int con_out(const char *fmt, ...) { va_end (va); return ln; } + + +/* + * Utility console message writes for lexer contexts. These will allow + * for reporting of file:line based on lexer context, These are used + * heavily in the parser/ir/ast. + */ +void con_vprintmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap) { + /* color selection table */ + static int sel[] = { + CON_WHITE, + CON_CYAN, + CON_RED + }; + + int err = !!(level == LVL_ERROR); + int color = (err) ? console.color_err : console.color_out; + + /* this might confuse you :P */ + ((err) ? &con_err : &con_out)( + (color) ? + "\033[0;%dm%s:%d: \033[0;%dm%s: \033[0m" : + "%s:%d: %s: ", + + CON_CYAN, + name, + (int)line, + sel[level], + msgtype + ); + + con_verr(msg, ap); + fprintf (stderr, "\n"); +} + +void con_printmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, ...) { + va_list va; + va_start(va, msg); + con_vprintmsg(level, name, line, msgtype, msg, va); + va_end (va); +} + +void con_cvprintmsg(void *ctx, int lvl, const char *msgtype, const char *msg, va_list ap) { + con_vprintmsg(lvl, ((lex_ctx*)ctx)->file, ((lex_ctx*)ctx)->line, msgtype, msg, ap); +} + +void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ...) { + va_list va; + va_start(va, msg); + con_cvprintmsg(ctx, lvl, msgtype, msg, va); + va_end (va); +} diff --git a/error.c b/error.c deleted file mode 100644 index e6a1c7d..0000000 --- a/error.c +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (C) 2012 - * Dale Weiler - * Wolfgang Bumiller - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do - * so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#include "gmqcc.h" - -/* - * Compiler error system, this handles the error printing, and managing - * such as after so many errors just stop the compilation, and other - * intereting like colors for the console. - */ - -#ifndef WIN32 -int levelcolor[] = { - CON_WHITE, - CON_CYAN, - CON_RED -}; -#endif - -void vprintmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap) -{ -#ifndef WIN32 - fprintf (stderr, "\033[0;%dm%s:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, levelcolor[level], msgtype); -#else - fprintf (stderr, "%s:%d: %s: ", name, line, msgtype); -#endif - vfprintf(stderr, msg, ap); - fprintf (stderr, "\n"); -} - -void printmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, ...) -{ - va_list va; - va_start(va, msg); - vprintmsg(level, name, line, msgtype, msg, va); - va_end (va); -} - -void cvprintmsg(lex_ctx ctx, int lvl, const char *msgtype, const char *msg, va_list ap) -{ - vprintmsg(lvl, ctx.file, ctx.line, msgtype, msg, ap); -} - -void cprintmsg (lex_ctx ctx, int lvl, const char *msgtype, const char *msg, ...) -{ - va_list va; - va_start(va, msg); - cvprintmsg(ctx, lvl, msgtype, msg, va); - va_end (va); -} diff --git a/gmqcc.h b/gmqcc.h index 9770929..c8440cc 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -535,6 +535,30 @@ qcint code_alloc_field (size_t qcsize); /*===================================================================*/ /*============================ con.c ================================*/ /*===================================================================*/ +enum { + CON_BLACK = 30, + CON_RED, + CON_GREEN, + CON_BROWN, + CON_BLUE, + CON_MAGENTA, + CON_CYAN , + CON_WHITE +}; + +/* message level */ +enum { + LVL_MSG, + LVL_WARNING, + LVL_ERROR +}; + + +void con_vprintmsg (int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap); +void con_printmsg (int level, const char *name, size_t line, const char *msgtype, const char *msg, ...); +void con_cvprintmsg(void *ctx, int lvl, const char *msgtype, const char *msg, va_list ap); +void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ...); + void con_close(); void con_color(int state); void con_init (); @@ -923,32 +947,6 @@ prog_section_def* prog_getdef (qc_program *prog, qcint off); qcany* prog_getedict (qc_program *prog, qcint e); qcint prog_tempstring(qc_program *prog, const char *_str); -/*===================================================================*/ -/*===================== error.c message printer =====================*/ -/*===================================================================*/ - -#ifndef WIN32 -enum { - CON_BLACK = 30, - CON_RED, - CON_GREEN, - CON_BROWN, - CON_BLUE, - CON_MAGENTA, - CON_CYAN , - CON_WHITE -}; -#endif -enum { - LVL_MSG, - LVL_WARNING, - LVL_ERROR -}; - -void vprintmsg (int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap); -void printmsg (int level, const char *name, size_t line, const char *msgtype, const char *msg, ...); -void cvprintmsg(lex_ctx ctx, int lvl, const char *msgtype, const char *msg, va_list ap); -void cprintmsg (lex_ctx ctx, int lvl, const char *msgtype, const char *msg, ...); /*===================================================================*/ /*===================== parser.c commandline ========================*/ diff --git a/ir.c b/ir.c index 7af6162..4ae49d9 100644 --- a/ir.c +++ b/ir.c @@ -177,7 +177,7 @@ static void irerror(lex_ctx ctx, const char *msg, ...) { va_list ap; va_start(ap, msg); - cvprintmsg(ctx, LVL_ERROR, "internal error", msg, ap); + con_cvprintmsg((void*)&ctx, LVL_ERROR, "internal error", msg, ap); va_end(ap); } @@ -193,7 +193,7 @@ static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...) lvl = LVL_ERROR; va_start(ap, fmt); - vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap); + con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap); va_end(ap); return opts_werror; @@ -2295,7 +2295,7 @@ static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *change * since this function is run multiple times. */ /* For now: debug info: */ - /* fprintf(stderr, "Value only written %s\n", value->name); */ + /* con_err( "Value only written %s\n", value->name); */ tempbool = ir_value_life_merge(value, instr->eid); *changed = *changed || tempbool; /* @@ -2310,7 +2310,7 @@ static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *change tempbool = ir_value_life_merge(value, instr->eid); /* if (tempbool) - fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid); + con_err( "value added id %s %i\n", value->name, (int)instr->eid); */ *changed = *changed || tempbool; /* Then remove */ @@ -2321,7 +2321,7 @@ static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *change } /* (A) */ tempbool = ir_block_living_add_instr(self, instr->eid); - /*fprintf(stderr, "living added values\n");*/ + /*con_err( "living added values\n");*/ *changed = *changed || tempbool; } diff --git a/lexer.c b/lexer.c index e190da0..0ed938c 100644 --- a/lexer.c +++ b/lexer.c @@ -16,7 +16,7 @@ void lexerror(lex_file *lex, const char *fmt, ...) va_list ap; va_start(ap, fmt); - vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap); + con_vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap); va_end(ap); } @@ -32,7 +32,7 @@ bool lexwarn(lex_file *lex, int warntype, const char *fmt, ...) lvl = LVL_ERROR; va_start(ap, fmt); - vprintmsg(lvl, lex->name, lex->sline, "warning", fmt, ap); + con_vprintmsg(lvl, lex->name, lex->sline, "warning", fmt, ap); va_end(ap); return opts_werror; diff --git a/parser.c b/parser.c index 534f007..59d4a33 100644 --- a/parser.c +++ b/parser.c @@ -65,7 +65,7 @@ static void parseerror(parser_t *parser, const char *fmt, ...) parser->errors++; va_start(ap, fmt); - vprintmsg(LVL_ERROR, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "parse error", fmt, ap); + con_vprintmsg(LVL_ERROR, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "parse error", fmt, ap); va_end(ap); } @@ -84,7 +84,7 @@ static bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char * } va_start(ap, fmt); - vprintmsg(lvl, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "warning", fmt, ap); + con_vprintmsg(lvl, parser->lex->tok.ctx.file, parser->lex->tok.ctx.line, "warning", fmt, ap); va_end(ap); return opts_werror; @@ -102,7 +102,7 @@ static bool GMQCC_WARN genwarning(lex_ctx ctx, int warntype, const char *fmt, .. lvl = LVL_ERROR; va_start(ap, fmt); - vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap); + con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap); va_end(ap); return opts_werror; diff --git a/test b/test new file mode 100755 index 0000000..e859b47 Binary files /dev/null and b/test differ