commit some files I forgot
[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 /**
41  * Issue a diagnostic message.
42  */
43 static void diagnosticvf(const char *const fmt, va_list ap)
44 {
45         for (const char* f = fmt; *f != '\0'; ++f) {
46                 if (*f == '%') {
47                         ++f;
48
49                         bool extended = false;
50                         if (*f == '#') {
51                                 extended = true;
52                                 ++f;
53                         }
54
55                         switch (*f) {
56                                 case '%':
57                                         fputc(*f, stderr);
58                                         break;
59
60                                 case 'C': {
61                                         const wint_t val = va_arg(ap, wint_t);
62                                         fputwc(val, stderr);
63                                         break;
64                                 }
65
66                                 case 'c': {
67                                         const unsigned char val = (unsigned char) va_arg(ap, int);
68                                         fputc(val, stderr);
69                                         break;
70                                 }
71
72                                 case 'd': {
73                                         const int val = va_arg(ap, int);
74                                         fprintf(stderr, "%d", val);
75                                         break;
76                                 }
77
78                                 case 's': {
79                                         const char* const str = va_arg(ap, const char*);
80                                         fputs(str, stderr);
81                                         break;
82                                 }
83
84                                 case 'Y': {
85                                         const symbol_t *const symbol = va_arg(ap, const symbol_t*);
86                                         fputs(symbol->string, stderr);
87                                         break;
88                                 }
89
90 #ifndef PPTEST
91                                 case 'E': {
92                                         const expression_t* const expr = va_arg(ap, const expression_t*);
93                                         print_expression(expr);
94                                         break;
95                                 }
96
97                                 case 'Q': {
98                                         const unsigned qualifiers = va_arg(ap, unsigned);
99                                         print_type_qualifiers(qualifiers);
100                                         break;
101                                 }
102
103                                 case 'T': {
104                                         const type_t* const type = va_arg(ap, const type_t*);
105                                         const symbol_t*     sym  = NULL;
106                                         if (extended) {
107                                                 sym = va_arg(ap, const symbol_t*);
108                                         }
109                                         print_type_ext(type, sym, NULL);
110                                         break;
111                                 }
112 #endif
113
114                                 case 'K': {
115                                         const token_t* const token = va_arg(ap, const token_t*);
116                                         print_token(stderr, token);
117                                         break;
118                                 }
119
120                                 case 'k': {
121                                         if (extended) {
122                                                 bool              first     = false;
123                                                 va_list*          toks      = va_arg(ap, va_list*);
124                                                 const char* const delimiter = va_arg(ap, const char*);
125                                                 for (;;) {
126                                                         const token_type_t tok = va_arg(*toks, token_type_t);
127                                                         if (tok == 0)
128                                                                 break;
129                                                         if (first) {
130                                                                 first = false;
131                                                         } else {
132                                                                 fputs(delimiter, stderr);
133                                                         }
134                                                         print_token_type(stderr, tok);
135                                                 }
136                                         } else {
137                                                 const token_type_t token = va_arg(ap, token_type_t);
138                                                 print_token_type(stderr, token);
139                                         }
140                                         break;
141                                 }
142
143                                 default:
144                                         panic("unknown format specifier");
145                         }
146                 } else {
147                         fputc(*f, stderr);
148                 }
149         }
150 }
151
152 void diagnosticf(const char *const fmt, ...)
153 {
154         va_list ap;
155         va_start(ap, fmt);
156         ++diagnostic_count;
157         diagnosticvf(fmt, ap);
158         va_end(ap);
159 }
160
161 static void errorvf(const source_position_t pos,
162                     const char *const fmt, va_list ap)
163 {
164         fprintf(stderr, "%s:%u: error: ", pos.input_name, pos.linenr);
165         ++error_count;
166         diagnosticvf(fmt, ap);
167         fputc('\n', stderr);
168
169         if (warning.fatal_errors)
170                 exit(EXIT_FAILURE);
171 }
172
173 void errorf(const source_position_t pos, const char *const fmt, ...)
174 {
175         va_list ap;
176         va_start(ap, fmt);
177         errorvf(pos, fmt, ap);
178         va_end(ap);
179 }
180
181 static void warningvf(const source_position_t pos,
182                       const char *const fmt, va_list ap)
183 {
184         fprintf(stderr, "%s:%u: warning: ", pos.input_name, pos.linenr);
185         ++warning_count;
186         diagnosticvf(fmt, ap);
187         fputc('\n', stderr);
188 }
189
190 void warningf(const source_position_t pos, const char *const fmt, ...)
191 {
192         if (inhibit_all_warnings)
193                 return;
194
195         va_list ap;
196         va_start(ap, fmt);
197         if (warning.s_are_errors) {
198                 errorvf(pos, fmt, ap);
199         } else {
200                 warningvf(pos, fmt, ap);
201         }
202         va_end(ap);
203 }