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
OBJ_A = test/ast-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
#default is compiler only

73
error.c
View file

@ -1,6 +1,7 @@
/*
* 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
@ -20,7 +21,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdarg.h>
#include "gmqcc.h"
/*
@ -28,65 +28,32 @@
* such as after so many errors just stop the compilation, and other
* intereting like colors for the console.
*/
#ifndef WIN32
enum {
CON_BLACK = 30,
CON_RED,
CON_GREEN,
CON_BROWN,
CON_BLUE,
CON_MAGENTA,
CON_CYAN ,
CON_WHITE
};
static const int error_color[] = {
CON_RED,
int levelcolor[] = {
CON_WHITE,
CON_CYAN,
CON_MAGENTA,
CON_BLUE,
CON_BROWN,
CON_WHITE
CON_RED
};
#endif
int error_total = 0;
int error_max = 10;
static const char *const error_list[] = {
"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? */
void vprintmsg(int level, const char *name, size_t line, char *errtype, const char *msg, va_list ap)
{
va_list va;
if (error_total + 1 > error_max) {
fprintf(stderr, "%d errors and more following, bailing\n", error_total);
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);
#ifndef WIN32
fprintf (stderr, "\033[0;%dm%s:%d \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, levelcolor[level], errtype);
#else
sprintf (bu, "%s ", error_list[status-SHRT_MAX]);
fprintf (stderr, "%s:%d %s: ", name, line, errtype);
#endif
va_start (va, msg);
vsprintf (fu, msg, va);
va_end (va);
fputs (bu, stderr);
fputs (fu, stderr);
/* color */
# ifndef WIN32
fputs ("\033[0m", stderr);
# endif
fflush (stderr);
return status;
vfprintf(stderr, msg, va);
fprintf (stderr, "\n");
}
void printmsg(int level, const char *name, size_t line, char *errtype, const char *msg, ...)
{
va_list va;
va_start(va, msg);
vprintmsg(level, name, line, errtype, msg, va);
va_end (va);
}

26
gmqcc.h
View file

@ -26,6 +26,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#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);
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 ========================*/
/*===================================================================*/

View file

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

View file

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