fix extern inline handling, implement __builtin_huge_val
[cparser] / diagnostic.c
index b495bd2..3e83b0b 100644 (file)
 #include <stdarg.h>
 #include <stdio.h>
 
-#include "adt/error.h"
-#include "ast.h"
 #include "diagnostic.h"
+#include "adt/error.h"
+#include "symbol_t.h"
 #include "token_t.h"
+#include "ast.h"
 #include "type.h"
 #include "warning.h"
 
@@ -36,6 +37,17 @@ unsigned warning_count    = 0;
 /** true if warnings should be inhibited */
 bool inhibit_all_warnings = false;
 
+static const source_position_t *curr_pos = NULL;
+
+/**
+ * prints an additional source position
+ */
+static void print_source_position(FILE *out, const source_position_t *pos) {
+       fprintf(out, "at line %u", pos->linenr);
+       if (curr_pos == NULL || curr_pos->input_name != pos->input_name)
+               fprintf(out, " of \"%s\"", pos->input_name);
+}
+
 /**
  * Issue a diagnostic message.
  */
@@ -116,7 +128,7 @@ static void diagnosticvf(const char *const fmt, va_list ap)
 
                                case 'k': {
                                        if (extended) {
-                                               bool              first     = false;
+                                               bool              first     = true;
                                                va_list*          toks      = va_arg(ap, va_list*);
                                                const char* const delimiter = va_arg(ap, const char*);
                                                for (;;) {
@@ -137,6 +149,12 @@ static void diagnosticvf(const char *const fmt, va_list ap)
                                        break;
                                }
 
+                               case 'P': {
+                                       const source_position_t *pos = va_arg(ap, const source_position_t *);
+                                       print_source_position(stderr, pos);
+                                       break;
+                               }
+
                                default:
                                        panic("unknown format specifier");
                        }
@@ -151,15 +169,17 @@ void diagnosticf(const char *const fmt, ...)
        va_list ap;
        va_start(ap, fmt);
        ++diagnostic_count;
+       curr_pos = NULL;
        diagnosticvf(fmt, ap);
        va_end(ap);
 }
 
-static void errorvf(const source_position_t pos,
+static void errorvf(const source_position_t *pos,
                     const char *const fmt, va_list ap)
 {
-       fprintf(stderr, "%s:%u: error: ", pos.input_name, pos.linenr);
+       fprintf(stderr, "%s:%u: error: ", pos->input_name, pos->linenr);
        ++error_count;
+       curr_pos = pos;
        diagnosticvf(fmt, ap);
        fputc('\n', stderr);
 
@@ -167,30 +187,33 @@ static void errorvf(const source_position_t pos,
                exit(EXIT_FAILURE);
 }
 
-void errorf(const source_position_t pos, const char *const fmt, ...)
+void errorf(const source_position_t *pos, const char *const fmt, ...)
 {
        va_list ap;
        va_start(ap, fmt);
+       curr_pos = pos;
        errorvf(pos, fmt, ap);
        va_end(ap);
 }
 
-static void warningvf(const source_position_t pos,
+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);
+       fprintf(stderr, "%s:%u: warning: ", pos->input_name, pos->linenr);
        ++warning_count;
+       curr_pos = pos;
        diagnosticvf(fmt, ap);
        fputc('\n', stderr);
 }
 
-void warningf(const source_position_t pos, const char *const fmt, ...)
+void warningf(const source_position_t *pos, const char *const fmt, ...)
 {
        if (inhibit_all_warnings)
                return;
 
        va_list ap;
        va_start(ap, fmt);
+       curr_pos = pos;
        if (warning.s_are_errors) {
                errorvf(pos, fmt, ap);
        } else {
@@ -198,3 +221,22 @@ void warningf(const source_position_t pos, const char *const fmt, ...)
        }
        va_end(ap);
 }
+
+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);
+}
+
+void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
+{
+       va_list ap;
+       va_start(ap, fmt);
+       curr_pos = pos;
+       internal_errorvf(pos, fmt, ap);
+       va_end(ap);
+       abort();
+}