fix for error44
[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, 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);
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                                         if (symbol == NULL)
98                                                 fputs("(null)", stderr);
99                                         else
100                                                 fputs(symbol->string, stderr);
101                                         break;
102                                 }
103
104                                 case 'E': {
105                                         const expression_t* const expr = va_arg(ap, const expression_t*);
106                                         print_expression(expr);
107                                         break;
108                                 }
109
110                                 case 'Q': {
111                                         const unsigned qualifiers = va_arg(ap, unsigned);
112                                         print_type_qualifiers(qualifiers);
113                                         break;
114                                 }
115
116                                 case 'T': {
117                                         const type_t* const type = va_arg(ap, const type_t*);
118                                         const symbol_t*     sym  = NULL;
119                                         if (extended) {
120                                                 sym = va_arg(ap, const symbol_t*);
121                                         }
122                                         print_type_ext(type, sym, NULL);
123                                         break;
124                                 }
125
126                                 case 'K': {
127                                         const token_t* const token = va_arg(ap, const token_t*);
128                                         print_token(stderr, token);
129                                         break;
130                                 }
131
132                                 case 'k': {
133                                         if (extended) {
134                                                 bool              first     = true;
135                                                 va_list*          toks      = va_arg(ap, va_list*);
136                                                 const char* const delimiter = va_arg(ap, const char*);
137                                                 for (;;) {
138                                                         const token_type_t tok = va_arg(*toks, token_type_t);
139                                                         if (tok == 0)
140                                                                 break;
141                                                         if (first) {
142                                                                 first = false;
143                                                         } else {
144                                                                 fputs(delimiter, stderr);
145                                                         }
146                                                         print_token_type(stderr, tok);
147                                                 }
148                                         } else {
149                                                 const token_type_t token = va_arg(ap, token_type_t);
150                                                 print_token_type(stderr, token);
151                                         }
152                                         break;
153                                 }
154
155                                 case 'P': {
156                                         const source_position_t *pos = va_arg(ap, const source_position_t *);
157                                         print_source_position(stderr, pos);
158                                         break;
159                                 }
160
161                                 default:
162                                         panic("unknown format specifier");
163                         }
164                 } else {
165                         fputc(*f, stderr);
166                 }
167         }
168 }
169
170 void diagnosticf(const char *const fmt, ...)
171 {
172         va_list ap;
173         va_start(ap, fmt);
174         ++diagnostic_count;
175         curr_pos = NULL;
176         diagnosticvf(fmt, ap);
177         va_end(ap);
178 }
179
180 static void errorvf(const source_position_t *pos,
181                     const char *const fmt, va_list ap)
182 {
183         fprintf(stderr, "%s:%u: error: ", pos->input_name, pos->linenr);
184         ++error_count;
185         curr_pos = pos;
186         diagnosticvf(fmt, ap);
187         fputc('\n', stderr);
188
189         if (warning.fatal_errors)
190                 exit(EXIT_FAILURE);
191 }
192
193 void errorf(const source_position_t *pos, const char *const fmt, ...)
194 {
195         va_list ap;
196         va_start(ap, fmt);
197         curr_pos = pos;
198         errorvf(pos, fmt, ap);
199         va_end(ap);
200 }
201
202 static void warningvf(const source_position_t *pos,
203                       const char *const fmt, va_list ap)
204 {
205         fprintf(stderr, "%s:%u: warning: ", pos->input_name, pos->linenr);
206         ++warning_count;
207         curr_pos = pos;
208         diagnosticvf(fmt, ap);
209         fputc('\n', stderr);
210 }
211
212 void warningf(const source_position_t *pos, const char *const fmt, ...)
213 {
214         if (inhibit_all_warnings)
215                 return;
216
217         va_list ap;
218         va_start(ap, fmt);
219         curr_pos = pos;
220         if (warning.s_are_errors) {
221                 errorvf(pos, fmt, ap);
222         } else {
223                 warningvf(pos, fmt, ap);
224         }
225         va_end(ap);
226 }
227
228 static void internal_errorvf(const source_position_t *pos,
229                     const char *const fmt, va_list ap)
230 {
231         fprintf(stderr, "%s:%u: internal error: ", pos->input_name, pos->linenr);
232         curr_pos = pos;
233         diagnosticvf(fmt, ap);
234         fputc('\n', stderr);
235 }
236
237 void internal_errorf(const source_position_t *pos, const char *const fmt, ...)
238 {
239         va_list ap;
240         va_start(ap, fmt);
241         curr_pos = pos;
242         internal_errorvf(pos, fmt, ap);
243         va_end(ap);
244         abort();
245 }