2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 #include "diagnostic.h"
24 #include "adt/error.h"
31 /** Number of occurred diagnostics. */
32 unsigned diagnostic_count = 0;
33 /** Number of occurred errors. */
34 unsigned error_count = 0;
35 /** Number of occurred warnings. */
36 unsigned warning_count = 0;
37 bool show_column = true;
39 static const source_position_t *curr_pos = NULL;
42 * prints an additional source position
44 static void print_source_position(FILE *out, const source_position_t *pos)
46 fprintf(out, "at line %u", pos->lineno);
48 fprintf(out, ":%u", pos->colno);
49 if (curr_pos == NULL || curr_pos->input_name != pos->input_name)
50 fprintf(out, " of \"%s\"", pos->input_name);
54 * Issue a diagnostic message.
56 static void diagnosticvf(const char *const fmt, va_list ap)
58 for (const char* f = fmt; *f != '\0'; ++f) {
62 bool extended = false;
74 const unsigned char val = (unsigned char) va_arg(ap, int);
80 const int val = va_arg(ap, int);
81 fprintf(stderr, "%d", val);
86 const char* const str = va_arg(ap, const char*);
92 const string_t *str = va_arg(ap, const string_t*);
93 for (size_t i = 0; i < str->size; ++i) {
94 fputc(str->begin[i], stderr);
100 const unsigned int val = va_arg(ap, unsigned int);
101 fprintf(stderr, "%u", val);
106 const symbol_t *const symbol = va_arg(ap, const symbol_t*);
108 fputs("(null)", stderr);
110 fputs(symbol->string, stderr);
115 const expression_t* const expr = va_arg(ap, const expression_t*);
116 print_expression(expr);
121 const unsigned qualifiers = va_arg(ap, unsigned);
122 print_type_qualifiers(qualifiers, QUAL_SEP_NONE);
127 const type_t* const type = va_arg(ap, const type_t*);
128 const symbol_t* sym = NULL;
130 sym = va_arg(ap, const symbol_t*);
132 print_type_ext(type, sym, NULL);
137 const token_t *const token = va_arg(ap, const token_t*);
138 print_pp_token(stderr, token);
143 const token_t* const token = va_arg(ap, const token_t*);
144 print_token(stderr, token);
151 va_list* toks = va_arg(ap, va_list*);
152 const char* const delimiter = va_arg(ap, const char*);
154 const token_type_t tok = va_arg(*toks, token_type_t);
160 fputs(delimiter, stderr);
162 print_token_type(stderr, tok);
165 const token_type_t token = va_arg(ap, token_type_t);
166 print_token_type(stderr, token);
172 const source_position_t *pos = va_arg(ap, const source_position_t *);
173 print_source_position(stderr, pos);
178 panic("unknown format specifier");
186 void diagnosticf(const char *const fmt, ...)
192 diagnosticvf(fmt, ap);
196 static void diagnosticposvf(source_position_t const *const pos, char const *const kind, char const *const fmt, va_list ap)
198 FILE *const out = stderr;
199 fprintf(out, "%s:%u:", pos->input_name, pos->lineno);
201 fprintf(out, "%u:", pos->colno);
202 fprintf(out, " %s: ", kind);
204 diagnosticvf(fmt, ap);
208 static void errorvf(const source_position_t *pos,
209 const char *const fmt, va_list ap)
212 diagnosticposvf(pos, "error", fmt, ap);
213 if (warning.fatal_errors)
217 void errorf(const source_position_t *pos, const char *const fmt, ...)
222 errorvf(pos, fmt, ap);
226 static void warningvf(const source_position_t *pos,
227 const char *const fmt, va_list ap)
230 diagnosticposvf(pos, "warning", fmt, ap);
233 void warningf(const source_position_t *pos, const char *const fmt, ...)
238 if (warning.s_are_errors) {
239 errorvf(pos, fmt, ap);
241 warningvf(pos, fmt, ap);
246 static void internal_errorvf(const source_position_t *pos,
247 const char *const fmt, va_list ap)
249 diagnosticposvf(pos, "internal error", fmt, ap);
252 void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
257 internal_errorvf(pos, fmt, ap);