parser: Remove the unused attribute alignment from struct declaration_specifiers_t.
[cparser] / diagnostic.c
index 27fdda9..3ff3d61 100644 (file)
@@ -1,21 +1,6 @@
 /*
  * This file is part of cparser.
- * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
  */
 #include <stdarg.h>
 #include <stdio.h>
 #include "diagnostic.h"
 #include "adt/error.h"
 #include "entity_t.h"
+#include "separator_t.h"
 #include "symbol_t.h"
-#include "token_t.h"
 #include "ast.h"
 #include "type.h"
-#include "warning.h"
+#include "unicode.h"
 
 /** Number of occurred errors. */
 unsigned error_count             = 0;
@@ -36,12 +21,12 @@ unsigned warning_count           = 0;
 bool     show_column             = true;
 bool     diagnostics_show_option = true;
 
-static const source_position_t *curr_pos = NULL;
+static const position_t *curr_pos = NULL;
 
 /**
  * prints an additional source position
  */
-static void print_source_position(FILE *out, const source_position_t *pos)
+static void print_position(FILE *out, const position_t *pos)
 {
        fprintf(out, "at line %u", pos->lineno);
        if (show_column)
@@ -140,9 +125,9 @@ done_flags:;
                }
 
                case 'X': {
-                       unsigned int const val = va_arg(ap, unsigned int);
-                       char const  *const fmt = flag_zero ? "%0*X" : "%*X";
-                       fprintf(stderr, fmt, field_width, val);
+                       unsigned int const val  = va_arg(ap, unsigned int);
+                       char  const *const xfmt = flag_zero ? "%0*X" : "%*X";
+                       fprintf(stderr, xfmt, field_width, val);
                        break;
                }
 
@@ -185,18 +170,13 @@ done_flags:;
 
                case 'k': {
                        if (extended) {
-                               bool              first     = true;
-                               va_list*          toks      = va_arg(ap, va_list*);
-                               const char* const delimiter = va_arg(ap, const char*);
+                               va_list* const toks = va_arg(ap, va_list*);
+                               separator_t    sep  = { "", va_arg(ap, const char*) };
                                for (;;) {
                                        const token_kind_t tok = (token_kind_t)va_arg(*toks, int);
                                        if (tok == 0)
                                                break;
-                                       if (first) {
-                                               first = false;
-                                       } else {
-                                               fputs(delimiter, stderr);
-                                       }
+                                       fputs(sep_next(&sep), stderr);
                                        print_token_kind(stderr, tok);
                                }
                        } else {
@@ -223,8 +203,8 @@ done_flags:;
                }
 
                case 'P': {
-                       const source_position_t *pos = va_arg(ap, const source_position_t *);
-                       print_source_position(stderr, pos);
+                       const position_t *pos = va_arg(ap, const position_t *);
+                       print_position(stderr, pos);
                        break;
                }
 
@@ -244,18 +224,24 @@ void diagnosticf(const char *const fmt, ...)
        va_end(ap);
 }
 
-static void diagnosticposvf(source_position_t const *const pos, char const *const kind, char const *const fmt, va_list ap)
+static void diagnosticposvf(position_t const *const pos, char const *const kind, char const *const fmt, va_list ap)
 {
        FILE *const out = stderr;
-       fprintf(out, "%s:%u:", pos->input_name, pos->lineno);
-       if (show_column)
-               fprintf(out, "%u:", (unsigned)pos->colno);
-       fprintf(out, " %s: ", kind);
+       if (pos) {
+               fprintf(out, "%s:", pos->input_name);
+               if (pos->lineno != 0) {
+                       fprintf(out, "%u:", pos->lineno);
+                       if (show_column)
+                               fprintf(out, "%u:", (unsigned)pos->colno);
+               }
+               fputc(' ', out);
+       }
+       fprintf(out, "%s: ", kind);
        curr_pos = pos;
        diagnosticvf(fmt, ap);
 }
 
-static void errorvf(const source_position_t *pos,
+static void errorvf(const position_t *pos,
                     const char *const fmt, va_list ap)
 {
        ++error_count;
@@ -265,7 +251,7 @@ 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 position_t *pos, const char *const fmt, ...)
 {
        va_list ap;
        va_start(ap, fmt);
@@ -273,10 +259,11 @@ void errorf(const source_position_t *pos, const char *const fmt, ...)
        va_end(ap);
 }
 
-void warningf(warning_t const warn, source_position_t const* pos, char const *const fmt, ...)
+void warningf(warning_t const warn, position_t const* pos, char const *const fmt, ...)
 {
-       va_list ap;
-       va_start(ap, fmt);
+       if (pos->is_system_header && !is_warn_on(WARN_SYSTEM))
+               return;
+
        warning_switch_t const *const s = get_warn_switch(warn);
        switch ((unsigned) s->state) {
                        char const* kind;
@@ -290,25 +277,29 @@ void warningf(warning_t const warn, source_position_t const* pos, char const *co
                                ++warning_count;
                                kind = "warning";
                        }
+                       va_list ap;
+                       va_start(ap, fmt);
                        diagnosticposvf(pos, kind, fmt, ap);
+                       va_end(ap);
                        if (diagnostics_show_option)
                                fprintf(stderr, " [-W%s]\n", s->name);
+                       else
+                               fputc('\n', stderr);
                        break;
 
                default:
                        break;
        }
-       va_end(ap);
 }
 
-static void internal_errorvf(const source_position_t *pos,
+static void internal_errorvf(const position_t *pos,
                     const char *const fmt, va_list ap)
 {
        diagnosticposvf(pos, "internal error", fmt, ap);
        fputc('\n', stderr);
 }
 
-void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
+void internal_errorf(const position_t *pos, const char *const fmt, ...)
 {
        va_list ap;
        va_start(ap, fmt);