2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 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 /** true if warnings should be inhibited */
38 bool inhibit_all_warnings = false;
40 static const source_position_t *curr_pos = NULL;
43 * prints an additional source position
45 static void print_source_position(FILE *out, const source_position_t *pos) {
46 fprintf(out, "at line %u", pos->linenr);
47 if (curr_pos == NULL || curr_pos->input_name != pos->input_name)
48 fprintf(out, " of \"%s\"", pos->input_name);
52 * Issue a diagnostic message.
54 static void diagnosticvf(const char *const fmt, va_list ap)
56 for (const char* f = fmt; *f != '\0'; ++f) {
60 bool extended = false;
72 const wint_t val = va_arg(ap, wint_t);
78 const unsigned char val = (unsigned char) va_arg(ap, int);
84 const int val = va_arg(ap, int);
85 fprintf(stderr, "%d", val);
90 const char* const str = va_arg(ap, const char*);
96 const unsigned int val = va_arg(ap, unsigned int);
97 fprintf(stderr, "%u", val);
102 const symbol_t *const symbol = va_arg(ap, const symbol_t*);
104 fputs("(null)", stderr);
106 fputs(symbol->string, stderr);
111 const expression_t* const expr = va_arg(ap, const expression_t*);
112 print_expression(expr);
117 const unsigned qualifiers = va_arg(ap, unsigned);
118 print_type_qualifiers(qualifiers);
123 const type_t* const type = va_arg(ap, const type_t*);
124 const symbol_t* sym = NULL;
126 sym = va_arg(ap, const symbol_t*);
128 print_type_ext(type, sym, NULL);
133 const token_t* const token = va_arg(ap, const token_t*);
134 print_token(stderr, token);
141 va_list* toks = va_arg(ap, va_list*);
142 const char* const delimiter = va_arg(ap, const char*);
144 const token_type_t tok = va_arg(*toks, token_type_t);
150 fputs(delimiter, stderr);
152 print_token_type(stderr, tok);
155 const token_type_t token = va_arg(ap, token_type_t);
156 print_token_type(stderr, token);
162 const source_position_t *pos = va_arg(ap, const source_position_t *);
163 print_source_position(stderr, pos);
168 panic("unknown format specifier");
176 void diagnosticf(const char *const fmt, ...)
182 diagnosticvf(fmt, ap);
186 static void errorvf(const source_position_t *pos,
187 const char *const fmt, va_list ap)
189 fprintf(stderr, "%s:%u: error: ", pos->input_name, pos->linenr);
192 diagnosticvf(fmt, ap);
195 if (warning.fatal_errors)
199 void errorf(const source_position_t *pos, const char *const fmt, ...)
204 errorvf(pos, fmt, ap);
208 static void warningvf(const source_position_t *pos,
209 const char *const fmt, va_list ap)
211 fprintf(stderr, "%s:%u: warning: ", pos->input_name, pos->linenr);
214 diagnosticvf(fmt, ap);
218 void warningf(const source_position_t *pos, const char *const fmt, ...)
220 if (inhibit_all_warnings)
226 if (warning.s_are_errors) {
227 errorvf(pos, fmt, ap);
229 warningvf(pos, fmt, ap);
234 static void internal_errorvf(const source_position_t *pos,
235 const char *const fmt, va_list ap)
237 fprintf(stderr, "%s:%u: internal error: ", pos->input_name, pos->linenr);
239 diagnosticvf(fmt, ap);
243 void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
248 internal_errorvf(pos, fmt, ap);