Make error correction more robust when the parser encounters a storage class where...
[cparser] / diagnostic.c
index 01773f3..782c657 100644 (file)
@@ -34,6 +34,7 @@ unsigned diagnostic_count = 0;
 unsigned error_count      = 0;
 /** Number of occurred warnings. */
 unsigned warning_count    = 0;
+bool     show_column      = true;
 
 static const source_position_t *curr_pos = NULL;
 
@@ -42,7 +43,9 @@ static const source_position_t *curr_pos = NULL;
  */
 static void print_source_position(FILE *out, const source_position_t *pos)
 {
-       fprintf(out, "at line %u", pos->linenr);
+       fprintf(out, "at line %u", pos->lineno);
+       if (show_column)
+               fprintf(out, ":%u", pos->colno);
        if (curr_pos == NULL || curr_pos->input_name != pos->input_name)
                fprintf(out, " of \"%s\"", pos->input_name);
 }
@@ -190,15 +193,23 @@ void diagnosticf(const char *const fmt, ...)
        va_end(ap);
 }
 
-static void errorvf(const source_position_t *pos,
-                    const char *const fmt, va_list ap)
+static void diagnosticposvf(source_position_t const *const pos, char const *const kind, char const *const fmt, va_list ap)
 {
-       fprintf(stderr, "%s:%u: error: ", pos->input_name, pos->linenr);
-       ++error_count;
+       FILE *const out = stderr;
+       fprintf(out, "%s:%u:", pos->input_name, pos->lineno);
+       if (show_column)
+               fprintf(out, "%u:", pos->colno);
+       fprintf(out, " %s: ", kind);
        curr_pos = pos;
        diagnosticvf(fmt, ap);
-       fputc('\n', stderr);
+       fputc('\n', out);
+}
 
+static void errorvf(const source_position_t *pos,
+                    const char *const fmt, va_list ap)
+{
+       ++error_count;
+       diagnosticposvf(pos, "error", fmt, ap);
        if (warning.fatal_errors)
                exit(EXIT_FAILURE);
 }
@@ -215,11 +226,8 @@ void errorf(const source_position_t *pos, const char *const fmt, ...)
 static void warningvf(const source_position_t *pos,
                       const char *const fmt, va_list ap)
 {
-       fprintf(stderr, "%s:%u: warning: ", pos->input_name, pos->linenr);
        ++warning_count;
-       curr_pos = pos;
-       diagnosticvf(fmt, ap);
-       fputc('\n', stderr);
+       diagnosticposvf(pos, "warning", fmt, ap);
 }
 
 void warningf(const source_position_t *pos, const char *const fmt, ...)
@@ -238,10 +246,7 @@ void warningf(const source_position_t *pos, const char *const fmt, ...)
 static void internal_errorvf(const source_position_t *pos,
                     const char *const fmt, va_list ap)
 {
-       fprintf(stderr, "%s:%u: internal error: ", pos->input_name, pos->linenr);
-       curr_pos = pos;
-       diagnosticvf(fmt, ap);
-       fputc('\n', stderr);
+       diagnosticposvf(pos, "internal error", fmt, ap);
 }
 
 void internal_errorf(const source_position_t *pos, const char *const fmt, ...)