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