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"
32 /** Number of occurred errors. */
33 unsigned error_count = 0;
34 /** Number of occurred warnings. */
35 unsigned warning_count = 0;
36 bool show_column = true;
37 bool diagnostics_show_option = 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", (unsigned)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(char const *fmt, va_list ap)
58 for (char const *f; (f = strchr(fmt, '%')); fmt = f) {
59 fwrite(fmt, sizeof(*fmt), f - fmt, stderr); // Print till '%'.
62 bool extended = false;
63 bool flag_zero = false;
66 case '#': extended = true; break;
67 case '0': flag_zero = true; break;
68 default: goto done_flags;
76 field_width = va_arg(ap, int);
85 const unsigned char val = (unsigned char) va_arg(ap, int);
91 const int val = va_arg(ap, int);
92 fprintf(stderr, "%d", val);
97 const char* const str = va_arg(ap, const char*);
103 const string_t *str = va_arg(ap, const string_t*);
104 for (size_t i = 0; i < str->size; ++i) {
105 fputc(str->begin[i], stderr);
111 const unsigned int val = va_arg(ap, unsigned int);
112 fprintf(stderr, "%u", val);
117 unsigned int const val = va_arg(ap, unsigned int);
118 char const *const fmt = flag_zero ? "%0*X" : "%*X";
119 fprintf(stderr, fmt, field_width, val);
124 const symbol_t *const symbol = va_arg(ap, const symbol_t*);
126 fputs("(null)", stderr);
128 fputs(symbol->string, stderr);
133 const expression_t* const expr = va_arg(ap, const expression_t*);
134 print_expression(expr);
139 const unsigned qualifiers = va_arg(ap, unsigned);
140 print_type_qualifiers(qualifiers, QUAL_SEP_NONE);
145 const type_t* const type = va_arg(ap, const type_t*);
146 const symbol_t* sym = NULL;
148 sym = va_arg(ap, const symbol_t*);
150 print_type_ext(type, sym, NULL);
155 const token_t *const token = va_arg(ap, const token_t*);
156 print_pp_token(stderr, token);
161 const token_t* const token = va_arg(ap, const token_t*);
162 print_token(stderr, token);
169 va_list* toks = va_arg(ap, va_list*);
170 const char* const delimiter = va_arg(ap, const char*);
172 const token_kind_t tok = va_arg(*toks, token_kind_t);
178 fputs(delimiter, stderr);
180 print_token_kind(stderr, tok);
183 const token_kind_t token = va_arg(ap, token_kind_t);
184 print_token_kind(stderr, token);
190 entity_t const *const ent = va_arg(ap, entity_t const*);
191 if (extended && is_declaration(ent)) {
192 print_type_ext(ent->declaration.type, ent->base.symbol, NULL);
194 char const *const kind = get_entity_kind_name(ent->kind);
195 symbol_t const *const sym = ent->base.symbol;
197 fprintf(stderr, "%s %s", kind, sym->string);
199 fprintf(stderr, "anonymous %s", kind);
206 const source_position_t *pos = va_arg(ap, const source_position_t *);
207 print_source_position(stderr, pos);
212 panic("unknown format specifier");
215 fputs(fmt, stderr); // Print rest.
218 void diagnosticf(const char *const fmt, ...)
223 diagnosticvf(fmt, ap);
227 static void diagnosticposvf(source_position_t const *const pos, char const *const kind, char const *const fmt, va_list ap)
229 FILE *const out = stderr;
230 fprintf(out, "%s:%u:", pos->input_name, pos->lineno);
232 fprintf(out, "%u:", (unsigned)pos->colno);
233 fprintf(out, " %s: ", kind);
235 diagnosticvf(fmt, ap);
238 static void errorvf(const source_position_t *pos,
239 const char *const fmt, va_list ap)
242 diagnosticposvf(pos, "error", fmt, ap);
244 if (is_warn_on(WARN_FATAL_ERRORS))
248 void errorf(const source_position_t *pos, const char *const fmt, ...)
252 errorvf(pos, fmt, ap);
256 void warningf(warning_t const warn, source_position_t const* pos, char const *const fmt, ...)
260 warning_switch_t const *const s = get_warn_switch(warn);
261 switch ((unsigned) s->state) {
264 if (is_warn_on(WARN_ERROR)) {
265 case WARN_STATE_ON | WARN_STATE_ERROR:
269 case WARN_STATE_ON | WARN_STATE_NO_ERROR:
273 diagnosticposvf(pos, kind, fmt, ap);
274 if (diagnostics_show_option)
275 fprintf(stderr, " [-W%s]\n", s->name);
284 static void internal_errorvf(const source_position_t *pos,
285 const char *const fmt, va_list ap)
287 diagnosticposvf(pos, "internal error", fmt, ap);
291 void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
295 internal_errorvf(pos, fmt, ap);