no more multi-line errors/warnings
[cparser] / diagnostic.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
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.
9  *
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.
14  *
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
18  * 02111-1307, USA.
19  */
20 #include <stdarg.h>
21 #include <stdio.h>
22
23 #include "diagnostic.h"
24 #include "adt/error.h"
25 #include "symbol_t.h"
26 #include "token_t.h"
27 #include "ast.h"
28 #include "type.h"
29 #include "warning.h"
30
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;
39
40 static const source_position_t *curr_pos = NULL;
41
42 /**
43  * prints an additional source position
44  */
45 static void print_source_position(FILE *out, 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);
49 }
50
51 /**
52  * Issue a diagnostic message.
53  */
54 static void diagnosticvf(const char *const fmt, va_list ap)
55 {
56         for (const char* f = fmt; *f != '\0'; ++f) {
57                 if (*f == '%') {
58                         ++f;
59
60                         bool extended = false;
61                         if (*f == '#') {
62                                 extended = true;
63                                 ++f;
64                         }
65
66                         switch (*f) {
67                                 case '%':
68                                         fputc(*f, stderr);
69                                         break;
70
71                                 case 'C': {
72                                         const wint_t val = va_arg(ap, wint_t);
73                                         fputwc(val, stderr);
74                                         break;
75                                 }
76
77                                 case 'c': {
78                                         const unsigned char val = (unsigned char) va_arg(ap, int);
79                                         fputc(val, stderr);
80                                         break;
81                                 }
82
83                                 case 'd': {
84                                         const int val = va_arg(ap, int);
85                                         fprintf(stderr, "%d", val);
86                                         break;
87                                 }
88
89                                 case 's': {
90                                         const char* const str = va_arg(ap, const char*);
91                                         fputs(str, stderr);
92                                         break;
93                                 }
94
95                                 case 'Y': {
96                                         const symbol_t *const symbol = va_arg(ap, const symbol_t*);
97                                         fputs(symbol->string, stderr);
98                                         break;
99                                 }
100
101                                 case 'E': {
102                                         const expression_t* const expr = va_arg(ap, const expression_t*);
103                                         print_expression(expr);
104                                         break;
105                                 }
106
107                                 case 'Q': {
108                                         const unsigned qualifiers = va_arg(ap, unsigned);
109                                         print_type_qualifiers(qualifiers);
110                                         break;
111                                 }
112
113                                 case 'T': {
114                                         const type_t* const type = va_arg(ap, const type_t*);
115                                         const symbol_t*     sym  = NULL;
116                                         if (extended) {
117                                                 sym = va_arg(ap, const symbol_t*);
118                                         }
119                                         print_type_ext(type, sym, NULL);
120                                         break;
121                                 }
122
123                                 case 'K': {
124                                         const token_t* const token = va_arg(ap, const token_t*);
125                                         print_token(stderr, token);
126                                         break;
127                                 }
128
129                                 case 'k': {
130                                         if (extended) {
131                                                 bool              first     = false;
132                                                 va_list*          toks      = va_arg(ap, va_list*);
133                                                 const char* const delimiter = va_arg(ap, const char*);
134                                                 for (;;) {
135                                                         const token_type_t tok = va_arg(*toks, token_type_t);
136                                                         if (tok == 0)
137                                                                 break;
138                                                         if (first) {
139                                                                 first = false;
140                                                         } else {
141                                                                 fputs(delimiter, stderr);
142                                                         }
143                                                         print_token_type(stderr, tok);
144                                                 }
145                                         } else {
146                                                 const token_type_t token = va_arg(ap, token_type_t);
147                                                 print_token_type(stderr, token);
148                                         }
149                                         break;
150                                 }
151
152                                 case 'P': {
153                                         source_position_t pos = va_arg(ap, source_position_t);
154                                         print_source_position(stderr, pos);
155                                         break;
156                                 }
157
158                                 default:
159                                         panic("unknown format specifier");
160                         }
161                 } else {
162                         fputc(*f, stderr);
163                 }
164         }
165 }
166
167 void diagnosticf(const char *const fmt, ...)
168 {
169         va_list ap;
170         va_start(ap, fmt);
171         ++diagnostic_count;
172         curr_pos = NULL;
173         diagnosticvf(fmt, ap);
174         va_end(ap);
175 }
176
177 static void errorvf(const source_position_t pos,
178                     const char *const fmt, va_list ap)
179 {
180         fprintf(stderr, "%s:%u: error: ", pos.input_name, pos.linenr);
181         ++error_count;
182         curr_pos = &pos;
183         diagnosticvf(fmt, ap);
184         fputc('\n', stderr);
185
186         if (warning.fatal_errors)
187                 exit(EXIT_FAILURE);
188 }
189
190 void errorf(const source_position_t pos, const char *const fmt, ...)
191 {
192         va_list ap;
193         va_start(ap, fmt);
194         curr_pos = &pos;
195         errorvf(pos, fmt, ap);
196         va_end(ap);
197 }
198
199 static void warningvf(const source_position_t pos,
200                       const char *const fmt, va_list ap)
201 {
202         fprintf(stderr, "%s:%u: warning: ", pos.input_name, pos.linenr);
203         ++warning_count;
204         curr_pos = &pos;
205         diagnosticvf(fmt, ap);
206         fputc('\n', stderr);
207 }
208
209 void warningf(const source_position_t pos, const char *const fmt, ...)
210 {
211         if (inhibit_all_warnings)
212                 return;
213
214         va_list ap;
215         va_start(ap, fmt);
216         curr_pos = &pos;
217         if (warning.s_are_errors) {
218                 errorvf(pos, fmt, ap);
219         } else {
220                 warningvf(pos, fmt, ap);
221         }
222         va_end(ap);
223 }
224
225 static void internal_errorvf(const source_position_t pos,
226                     const char *const fmt, va_list ap)
227 {
228         fprintf(stderr, "%s:%u: internal error: ", pos.input_name, pos.linenr);
229         curr_pos = &pos;
230         diagnosticvf(fmt, ap);
231         fputc('\n', stderr);
232 }
233
234 void internal_errorf(const source_position_t pos, const char *const fmt, ...)
235 {
236         va_list ap;
237         va_start(ap, fmt);
238         curr_pos = &pos;
239         internal_errorvf(pos, fmt, ap);
240         va_end(ap);
241         abort();
242 }