From 2e84cc0b418500a57d95315c0376c13a10d60142 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Thu, 15 Nov 2012 00:28:46 +0000 Subject: [PATCH] Use new console system everywhere. --- Makefile | 1 - ast.c | 4 ++-- con.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ error.c | 70 ------------------------------------------------------- gmqcc.h | 50 +++++++++++++++++++-------------------- ir.c | 10 ++++---- lexer.c | 4 ++-- parser.c | 6 ++--- test | Bin 0 -> 9665 bytes 9 files changed, 92 insertions(+), 109 deletions(-) delete mode 100644 error.c create mode 100755 test 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 0000000000000000000000000000000000000000..e859b477cf7a42d4539d26524613ddcc4e8b8e54 GIT binary patch literal 9665 zcmeHNeQaDuwV&Pf+D_uco1}rHNnt52#L#S=v`w78w%mL;xh|VF6sHYR((8?Pofy`; zX7_IE^d-E2W9ZeAU@3^H>-QkoZtDFGiT<`?EUz!uH=?*D5Ru>)t!pC-l(M^b5?42v1S?T zRL#m#?@-HCBakQ#OG}8Fn$U4}Ewq{SBA^Xa4>J(9Ooi#ShEqZ%LZZ~(OtH9XXv+e_ z3RNnu0kWf5RZO#>y{5v{<~Saac-W3uvRle_OIcs&1UW_#D*dCr(XoR4tq5D-h_{HS z&H6k;buNHyf7wk_eR!Tx7@lY8iqOLbRzk(@L$K5RWBLC^B+i?%1AA`DrdMSLa;4!_ z!<}nat-U2t%qP~s0r{qGQx~`1y#r@6T+~h+2g$F&L1Rhs`)_Vp-}mB`qmTdoj-T7# zX}>ME#l3^N--LsF==i7-iiHWI7}~GiNI>D)@VWFI3Ca_C>v@nLZG`6$jxv1GsXWy@UCjQ*=`W*Krim-ADOdtNQapnVcFZ zrd)TwD!S=>$yNP**?ciWT&7TfDQUCmU}_+z9_$|~4CLH?wKdtjag(zqu_i$c<07J; zP^yLSqp_3eUb=Q*4$1EaMrCqguckC6GKUC|e$h2XV!uPQCv0n5oxAduZZjBWuG`pap`k3Dk7iQVV_6DH>$#A$4dT-fC$%V3Q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