From 0b6269f60712cb727956ad1be1114e7208a829a4 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Thu, 30 May 2013 19:36:01 +0000 Subject: [PATCH] column printing for warnings and errors now --- conout.c | 18 +++++++++--------- ftepp.c | 2 +- gmqcc.h | 5 +++-- lexer.c | 49 ++++++++++++++++++++++++++++++++----------------- lexer.h | 1 + opts.c | 2 +- test.c | 14 +++++++------- 7 files changed, 54 insertions(+), 37 deletions(-) diff --git a/conout.c b/conout.c index 2f72076..687fdf4 100644 --- a/conout.c +++ b/conout.c @@ -333,7 +333,7 @@ int con_out(const char *fmt, ...) { * for reporting of file:line based on lexer context, These are used * heavily in the parser/ir/ast. */ -static void con_vprintmsg_c(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap, const char *condname) { +static void con_vprintmsg_c(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap, const char *condname) { /* color selection table */ static int sel[] = { CON_WHITE, @@ -347,9 +347,9 @@ static void con_vprintmsg_c(int level, const char *name, size_t line, const char int (*vprint)(const char *, va_list) = (err) ? &con_verr : &con_vout; if (color) - print("\033[0;%dm%s:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, sel[level], msgtype); + print("\033[0;%dm%s:%d:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, (int)column, sel[level], msgtype); else - print("%s:%d: %s: ", name, (int)line, msgtype); + print("%s:%d:%d: %s: ", name, (int)line, (int)column, msgtype); vprint(msg, ap); if (condname) @@ -358,19 +358,19 @@ static void con_vprintmsg_c(int level, const char *name, size_t line, const char print("\n"); } -void con_vprintmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, va_list ap) { - con_vprintmsg_c(level, name, line, msgtype, msg, ap, NULL); +void con_vprintmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap) { + con_vprintmsg_c(level, name, line, column, msgtype, msg, ap, NULL); } -void con_printmsg(int level, const char *name, size_t line, const char *msgtype, const char *msg, ...) { +void con_printmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, ...) { va_list va; va_start(va, msg); - con_vprintmsg(level, name, line, msgtype, msg, va); + con_vprintmsg(level, name, line, column, 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); + con_vprintmsg(lvl, ((lex_ctx*)ctx)->file, ((lex_ctx*)ctx)->line, ((lex_ctx*)ctx)->column, msgtype, msg, ap); } void con_cprintmsg (void *ctx, int lvl, const char *msgtype, const char *msg, ...) { @@ -432,7 +432,7 @@ bool GMQCC_WARN vcompile_warning(lex_ctx ctx, int warntype, const char *fmt, va_ lvl = LVL_ERROR; } - con_vprintmsg_c(lvl, ctx.file, ctx.line, msgtype, fmt, ap, warn_name); + con_vprintmsg_c(lvl, ctx.file, ctx.line, ctx.column, msgtype, fmt, ap, warn_name); return OPTS_WERROR(warntype) && OPTS_FLAG(BAIL_ON_WERROR); } diff --git a/ftepp.c b/ftepp.c index f8d41d9..5348dfb 100644 --- a/ftepp.c +++ b/ftepp.c @@ -1881,7 +1881,7 @@ ftepp_t *ftepp_create() void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name) { ppmacro *macro; - lex_ctx ctx = { "__builtin__", 0 }; + lex_ctx ctx = { "__builtin__", 0, 0 }; ctx.file = source; macro = ppmacro_new(ctx, name); /*vec_push(ftepp->macros, macro);*/ diff --git a/gmqcc.h b/gmqcc.h index 3b221e9..a92e109 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -749,6 +749,7 @@ void code_pop_statement (code_t *); typedef struct { const char *file; size_t line; + size_t column; } lex_ctx; /*===================================================================*/ @@ -775,8 +776,8 @@ enum { FILE *con_default_out(); FILE *con_default_err(); -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_vprintmsg (int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap); +void con_printmsg (int level, const char *name, size_t line, size_t column, 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, ...); diff --git a/lexer.c b/lexer.c index 035dc1b..862131e 100644 --- a/lexer.c +++ b/lexer.c @@ -66,9 +66,9 @@ static void lexerror(lex_file *lex, const char *fmt, ...) va_start(ap, fmt); if (lex) - con_vprintmsg(LVL_ERROR, lex->name, lex->sline, "parse error", fmt, ap); + con_vprintmsg(LVL_ERROR, lex->name, lex->sline, lex->column, "parse error", fmt, ap); else - con_vprintmsg(LVL_ERROR, "", 0, "parse error", fmt, ap); + con_vprintmsg(LVL_ERROR, "", 0, 0, "parse error", fmt, ap); va_end(ap); } @@ -174,9 +174,11 @@ static void lex_token_new(lex_file *lex) #else if (lex->tok.value) vec_shrinkto(lex->tok.value, 0); + lex->tok.constval.t = 0; - lex->tok.ctx.line = lex->sline; - lex->tok.ctx.file = lex->name; + lex->tok.ctx.line = lex->sline; + lex->tok.ctx.file = lex->name; + lex->tok.ctx.column = lex->column; #endif } #endif @@ -200,12 +202,12 @@ lex_file* lex_open(const char *file) memset(lex, 0, sizeof(*lex)); - lex->file = in; - lex->name = util_strdup(file); - lex->line = 1; /* we start counting at 1 */ - + lex->file = in; + lex->name = util_strdup(file); + lex->line = 1; /* we start counting at 1 */ + lex->column = 0; lex->peekpos = 0; - lex->eof = false; + lex->eof = false; vec_push(lex_filenames, lex->name); return lex; @@ -228,11 +230,11 @@ lex_file* lex_open_string(const char *str, size_t len, const char *name) lex->open_string_length = len; lex->open_string_pos = 0; - lex->name = util_strdup(name ? name : ""); - lex->line = 1; /* we start counting at 1 */ - + lex->name = util_strdup(name ? name : ""); + lex->line = 1; /* we start counting at 1 */ lex->peekpos = 0; - lex->eof = false; + lex->eof = false; + lex->column = 0; vec_push(lex_filenames, lex->name); @@ -271,11 +273,14 @@ void lex_close(lex_file *lex) static int lex_fgetc(lex_file *lex) { - if (lex->file) + if (lex->file) { + lex->column++; return fs_file_getc(lex->file); + } if (lex->open_string) { if (lex->open_string_pos >= lex->open_string_length) return EOF; + lex->column++; return lex->open_string[lex->open_string_pos++]; } return EOF; @@ -291,16 +296,22 @@ static int lex_try_trigraph(lex_file *lex, int old) { int c2, c3; c2 = lex_fgetc(lex); - if (!lex->push_line && c2 == '\n') + if (!lex->push_line && c2 == '\n') { lex->line++; + lex->column = 0; + } + if (c2 != '?') { lex_ungetch(lex, c2); return old; } c3 = lex_fgetc(lex); - if (!lex->push_line && c3 == '\n') + if (!lex->push_line && c3 == '\n') { lex->line++; + lex->column = 0; + } + switch (c3) { case '=': return '#'; case '/': return '\\'; @@ -365,8 +376,11 @@ static int lex_getch(lex_file *lex) static void lex_ungetch(lex_file *lex, int ch) { lex->peek[lex->peekpos++] = ch; - if (!lex->push_line && ch == '\n') + lex->column--; + if (!lex->push_line && ch == '\n') { lex->line--; + lex->column = 0; + } } /* classify characters @@ -872,6 +886,7 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote) ch = 0; else { --u8len; + lex->column += u8len; for (uc = 0; uc < u8len; ++uc) lex_tokench(lex, u8buf[uc]); /* the last character will be inserted with the tokench() call diff --git a/lexer.h b/lexer.h index 5f0bb63..cde0863 100644 --- a/lexer.h +++ b/lexer.h @@ -114,6 +114,7 @@ typedef struct lex_file_s { char *name; size_t line; size_t sline; /* line at the start of a token */ + size_t column; int peek[256]; size_t peekpos; diff --git a/opts.c b/opts.c index 77aa9c0..abd012f 100644 --- a/opts.c +++ b/opts.c @@ -361,7 +361,7 @@ void opts_ini_init(const char *file) { if ((line = opts_ini_parse(ini, &opts_ini_load, &error)) != 0) { /* there was a parse error with the ini file */ - con_printmsg(LVL_ERROR, file, line, "error", error); + con_printmsg(LVL_ERROR, file, line, 0 /*TODO: column for ini error*/, "error", error); vec_free(error); } diff --git a/test.c b/test.c index 2febbff..6682983 100644 --- a/test.c +++ b/test.c @@ -297,7 +297,7 @@ static bool task_template_generate(task_template_t *tmpl, char tag, const char * case 'I': destval = &tmpl->sourcefile; break; case 'F': destval = &tmpl->testflags; break; default: - con_printmsg(LVL_ERROR, __FILE__, __LINE__, "internal error", + con_printmsg(LVL_ERROR, __FILE__, __LINE__, 0, "internal error", "invalid tag `%c:` during code generation\n", tag ); @@ -309,7 +309,7 @@ static bool task_template_generate(task_template_t *tmpl, char tag, const char * * assigned value. */ if (*destval) { - con_printmsg(LVL_ERROR, file, line, "compile error", + con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "compile error", "tag `%c:` already assigned value: %s\n", tag, *destval ); @@ -377,7 +377,7 @@ static bool task_template_parse(const char *file, task_template_t *tmpl, FILE *f */ case '/': if (data[1] != '/') { - con_printmsg(LVL_ERROR, file, line, "tmpl parse error", + con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error", "invalid character `/`, perhaps you meant `//` ?"); mem_d(back); @@ -407,14 +407,14 @@ static bool task_template_parse(const char *file, task_template_t *tmpl, FILE *f case 'I': case 'F': if (data[1] != ':') { - con_printmsg(LVL_ERROR, file, line, "tmpl parse error", + con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error", "expected `:` after `%c`", *data ); goto failure; } if (!task_template_generate(tmpl, *data, file, line, &data[3], pad)) { - con_printmsg(LVL_ERROR, file, line, "tmpl compile error", + con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl compile error", "failed to generate for given task\n" ); goto failure; @@ -429,7 +429,7 @@ static bool task_template_parse(const char *file, task_template_t *tmpl, FILE *f { char *value = &data[3]; if (data[1] != ':') { - con_printmsg(LVL_ERROR, file, line, "tmpl parse error", + con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error", "expected `:` after `%c`", *data ); @@ -454,7 +454,7 @@ static bool task_template_parse(const char *file, task_template_t *tmpl, FILE *f } default: - con_printmsg(LVL_ERROR, file, line, "tmpl parse error", + con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error", "invalid tag `%c`", *data ); goto failure; -- 2.39.2