]> git.rm.cloudns.org Git - xonotic/gmqcc.git/commitdiff
Working towards a saner error-output system, adding -Werror
authorWolfgang (Blub) Bumiller <blub@speed.at>
Tue, 14 Aug 2012 09:34:07 +0000 (11:34 +0200)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Tue, 14 Aug 2012 09:34:07 +0000 (11:34 +0200)
Makefile
error.c
gmqcc.h
parser.c
warns.def

index d326576da571977ee15a368ff4ab61b2f637f2b5..1f584cbab1bd03f8b69dfac4d7df6066d59d3dc0 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/error.c b/error.c
index b9cf740b6b5ddae8383ff718b87a76598dc6a25e..c34abdb1d3eac5df2cf65716bab22d069a23515e 100644 (file)
--- a/error.c
+++ b/error.c
@@ -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"
 
 /*
  * 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);
+    vfprintf(stderr, msg, va);
+    fprintf (stderr, "\n");
+}
 
-    return status;
+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);
 }
diff --git a/gmqcc.h b/gmqcc.h
index 32a899f9cff1f8f3934ad65b9a470cce25a76c96..e985e875f5210d1ef4fb3f896cfbd8f160b2a32e 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -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 ========================*/
 /*===================================================================*/
index 1e754b86a766b3f154a8c32882eafa8650f4a165..7a69276d266ddef38f1388e1d3857835e5fcb322 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -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)
index ba433303dc5a7056bcf91351ba43b42230b30e17..d8656e7caf32a6582773e9bd78a997055b365015 100644 (file)
--- a/warns.def
+++ b/warns.def
@@ -5,3 +5,4 @@
 GMQCC_DEFINE_FLAG(UNUSED_VARIABLE)
 GMQCC_DEFINE_FLAG(UNKNOWN_CONTROL_SEQUENCE)
 GMQCC_DEFINE_FLAG(EXTENSIONS)
+GMQCC_DEFINE_FLAG(ERROR)