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