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 diagnostics. */
33 unsigned diagnostic_count = 0;
34 /** Number of occurred errors. */
35 unsigned error_count = 0;
36 /** Number of occurred warnings. */
37 unsigned warning_count = 0;
38 bool show_column = true;
39 bool diagnostics_show_option = true;
41 static const source_position_t *curr_pos = NULL;
44 * prints an additional source position
46 static void print_source_position(FILE *out, const source_position_t *pos)
48 fprintf(out, "at line %u", pos->lineno);
50 fprintf(out, ":%u", (unsigned)pos->colno);
51 if (curr_pos == NULL || curr_pos->input_name != pos->input_name)
52 fprintf(out, " of \"%s\"", pos->input_name);
56 * Issue a diagnostic message.
58 static void diagnosticvf(const char *const fmt, va_list ap)
60 for (const char* f = fmt; *f != '\0'; ++f) {
64 bool extended = false;
76 const unsigned char val = (unsigned char) va_arg(ap, int);
82 const int val = va_arg(ap, int);
83 fprintf(stderr, "%d", val);
88 const char* const str = va_arg(ap, const char*);
94 const string_t *str = va_arg(ap, const string_t*);
95 for (size_t i = 0; i < str->size; ++i) {
96 fputc(str->begin[i], stderr);
102 const unsigned int val = va_arg(ap, unsigned int);
103 fprintf(stderr, "%u", val);
108 const symbol_t *const symbol = va_arg(ap, const symbol_t*);
110 fputs("(null)", stderr);
112 fputs(symbol->string, stderr);
117 const expression_t* const expr = va_arg(ap, const expression_t*);
118 print_expression(expr);
123 const unsigned qualifiers = va_arg(ap, unsigned);
124 print_type_qualifiers(qualifiers, QUAL_SEP_NONE);
129 const type_t* const type = va_arg(ap, const type_t*);
130 const symbol_t* sym = NULL;
132 sym = va_arg(ap, const symbol_t*);
134 print_type_ext(type, sym, NULL);
139 const token_t *const token = va_arg(ap, const token_t*);
140 print_pp_token(stderr, token);
145 const token_t* const token = va_arg(ap, const token_t*);
146 print_token(stderr, token);
153 va_list* toks = va_arg(ap, va_list*);
154 const char* const delimiter = va_arg(ap, const char*);
156 const token_kind_t tok = va_arg(*toks, token_kind_t);
162 fputs(delimiter, stderr);
164 print_token_kind(stderr, tok);
167 const token_kind_t token = va_arg(ap, token_kind_t);
168 print_token_kind(stderr, token);
174 entity_t const *const ent = va_arg(ap, entity_t const*);
175 if (extended && is_declaration(ent)) {
176 print_type_ext(ent->declaration.type, ent->base.symbol, NULL);
178 char const *const kind = get_entity_kind_name(ent->kind);
179 symbol_t const *const sym = ent->base.symbol;
181 fprintf(stderr, "%s %s", kind, sym->string);
183 fprintf(stderr, "anonymous %s", kind);
190 const source_position_t *pos = va_arg(ap, const source_position_t *);
191 print_source_position(stderr, pos);
196 panic("unknown format specifier");
204 void diagnosticf(const char *const fmt, ...)
210 diagnosticvf(fmt, ap);
214 static void diagnosticposvf(source_position_t const *const pos, char const *const kind, char const *const fmt, va_list ap)
216 FILE *const out = stderr;
217 fprintf(out, "%s:%u:", pos->input_name, pos->lineno);
219 fprintf(out, "%u:", (unsigned)pos->colno);
220 fprintf(out, " %s: ", kind);
222 diagnosticvf(fmt, ap);
225 static void errorvf(const source_position_t *pos,
226 const char *const fmt, va_list ap)
230 diagnosticposvf(pos, "error", fmt, ap);
232 if (is_warn_on(WARN_FATAL_ERRORS))
236 void errorf(const source_position_t *pos, const char *const fmt, ...)
240 errorvf(pos, fmt, ap);
244 void warningf(warning_t const warn, source_position_t const* pos, char const *const fmt, ...)
248 warning_switch_t const *const s = get_warn_switch(warn);
249 switch ((unsigned) s->state) {
252 if (is_warn_on(WARN_ERROR)) {
253 case WARN_STATE_ON | WARN_STATE_ERROR:
257 case WARN_STATE_ON | WARN_STATE_NO_ERROR:
261 diagnosticposvf(pos, kind, fmt, ap);
262 if (diagnostics_show_option)
263 fprintf(stderr, " [-W%s]\n", s->name);
272 static void internal_errorvf(const source_position_t *pos,
273 const char *const fmt, va_list ap)
275 diagnosticposvf(pos, "internal error", fmt, ap);
279 void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
284 internal_errorvf(pos, fmt, ap);