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