Working towards a saner error-output system, adding -Werror

This commit is contained in:
Wolfgang (Blub) Bumiller 2012-08-14 11:34:07 +02:00
parent c08966a2a4
commit 579ac3dd5d
5 changed files with 57 additions and 75 deletions

View file

@ -23,7 +23,7 @@ OBJ = \
ir.o ir.o
OBJ_A = test/ast-test.o OBJ_A = test/ast-test.o
OBJ_I = test/ir-test.o OBJ_I = test/ir-test.o
OBJ_C = main.o lexer.o parser.o OBJ_C = main.o lexer.o parser.o error.o
OBJ_X = exec-standalone.o util.o OBJ_X = exec-standalone.o util.o
#default is compiler only #default is compiler only

73
error.c
View file

@ -1,6 +1,7 @@
/* /*
* Copyright (C) 2012 * Copyright (C) 2012
* Dale Weiler * Dale Weiler
* Wolfgang Bumiller
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of * 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 * this software and associated documentation files (the "Software"), to deal in
@ -20,7 +21,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
#include <stdarg.h>
#include "gmqcc.h" #include "gmqcc.h"
/* /*
@ -28,65 +28,32 @@
* such as after so many errors just stop the compilation, and other * such as after so many errors just stop the compilation, and other
* intereting like colors for the console. * intereting like colors for the console.
*/ */
#ifndef WIN32 #ifndef WIN32
enum { int levelcolor[] = {
CON_BLACK = 30, CON_WHITE,
CON_RED,
CON_GREEN,
CON_BROWN,
CON_BLUE,
CON_MAGENTA,
CON_CYAN ,
CON_WHITE
};
static const int error_color[] = {
CON_RED,
CON_CYAN, CON_CYAN,
CON_MAGENTA, CON_RED
CON_BLUE,
CON_BROWN,
CON_WHITE
}; };
#endif #endif
int error_total = 0;
int error_max = 10;
static const char *const error_list[] = { void vprintmsg(int level, const char *name, size_t line, char *errtype, const char *msg, va_list ap)
"Parsing Error:", {
"Lexing Error:",
"Internal Error:",
"Compilation Error:",
"Preprocessor Error:"
};
int error(lex_file *file, int status, const char *msg, ...) {
char bu[1024*4]; /* enough? */
char fu[1024*4]; /* enough? */
va_list va; va_list va;
if (error_total + 1 > error_max) { #ifndef WIN32
fprintf(stderr, "%d errors and more following, bailing\n", error_total); fprintf (stderr, "\033[0;%dm%s:%d \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, levelcolor[level], errtype);
exit (-1);
}
error_total ++;
/* color */
# ifndef WIN32
sprintf (bu, "\033[0;%dm%s \033[0;%dm %s:%d ", error_color[status-SHRT_MAX], error_list[status-SHRT_MAX], error_color[(status-1)-SHRT_MAX], file->name, file->line);
#else #else
sprintf (bu, "%s ", error_list[status-SHRT_MAX]); fprintf (stderr, "%s:%d %s: ", name, line, errtype);
#endif #endif
va_start (va, msg); vfprintf(stderr, msg, va);
vsprintf (fu, msg, va); fprintf (stderr, "\n");
va_end (va); }
fputs (bu, stderr);
fputs (fu, stderr); void printmsg(int level, const char *name, size_t line, char *errtype, const char *msg, ...)
{
/* color */ va_list va;
# ifndef WIN32 va_start(va, msg);
fputs ("\033[0m", stderr); vprintmsg(level, name, line, errtype, msg, va);
# endif va_end (va);
fflush (stderr);
return status;
} }

26
gmqcc.h
View file

@ -26,6 +26,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h>
#include <ctype.h> #include <ctype.h>
#define GMQCC_VERSION_MAJOR 0 #define GMQCC_VERSION_MAJOR 0
@ -878,6 +879,31 @@ prog_section_def* prog_getdef (qc_program *prog, qcint off);
qcany* prog_getedict (qc_program *prog, qcint e); qcany* prog_getedict (qc_program *prog, qcint e);
qcint prog_tempstring(qc_program *prog, const char *_str); 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, char *errtype, const char *msg, va_list ap);
void printmsg (int level, const char *name, size_t line, char *errtype, const char *msg, ...);
/*===================================================================*/ /*===================================================================*/
/*======================= main.c commandline ========================*/ /*======================= main.c commandline ========================*/
/*===================================================================*/ /*===================================================================*/

View file

@ -54,13 +54,10 @@ void parseerror(parser_t *parser, const char *fmt, ...)
parser->errors++; parser->errors++;
if (parser)
printf("error %s:%lu: ", parser->lex->tok->ctx.file, (unsigned long)parser->lex->tok->ctx.line); printf("error %s:%lu: ", parser->lex->tok->ctx.file, (unsigned long)parser->lex->tok->ctx.line);
else
printf("error: ");
va_start(ap, fmt); va_start(ap, fmt);
vprintf(fmt, ap); vprintmsg(LVL_ERROR, parser->lex->tok->ctx.file, parser->lex->tok->ctx.line, "parse error", fmt, ap);
va_end(ap); va_end(ap);
printf("\n"); printf("\n");
@ -70,30 +67,21 @@ void parseerror(parser_t *parser, const char *fmt, ...)
bool parsewarning(parser_t *parser, int warntype, const char *fmt, ...) bool parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
{ {
va_list ap; va_list ap;
int lvl = LVL_WARNING;
#if 0
if (OPTS_WARN(WARN_ERROR))
parser->errors++;
#endif
if (!OPTS_WARN(warntype)) if (!OPTS_WARN(warntype))
return false; return false;
if (parser) if (OPTS_WARN(WARN_ERROR)) {
printf("warning %s:%lu: ", parser->lex->tok->ctx.file, (unsigned long)parser->lex->tok->ctx.line); parser->errors++;
else lvl = LVL_ERROR;
printf("warning: "); }
va_start(ap, fmt); va_start(ap, fmt);
vprintf(fmt, ap); vprintmsg(lvl, parser->lex->tok->ctx.file, parser->lex->tok->ctx.line, "warning", fmt, ap);
va_end(ap); va_end(ap);
printf("\n"); return OPTS_WARN(WARN_ERROR);
#if 0
return true;
#else
return false;
#endif
} }
bool parser_next(parser_t *parser) bool parser_next(parser_t *parser)

View file

@ -5,3 +5,4 @@
GMQCC_DEFINE_FLAG(UNUSED_VARIABLE) GMQCC_DEFINE_FLAG(UNUSED_VARIABLE)
GMQCC_DEFINE_FLAG(UNKNOWN_CONTROL_SEQUENCE) GMQCC_DEFINE_FLAG(UNKNOWN_CONTROL_SEQUENCE)
GMQCC_DEFINE_FLAG(EXTENSIONS) GMQCC_DEFINE_FLAG(EXTENSIONS)
GMQCC_DEFINE_FLAG(ERROR)