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