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