30
|
1 /*-
|
|
2 * Copyright (c) 2010 The NetBSD Foundation, Inc.
|
|
3 * All rights reserved.
|
|
4 *
|
|
5 * This code is derived from software contributed to The NetBSD Foundation
|
|
6 * by David A. Holland.
|
|
7 *
|
|
8 * Redistribution and use in source and binary forms, with or without
|
|
9 * modification, are permitted provided that the following conditions
|
|
10 * are met:
|
|
11 * 1. Redistributions of source code must retain the above copyright
|
|
12 * notice, this list of conditions and the following disclaimer.
|
|
13 * 2. Redistributions in binary form must reproduce the above copyright
|
|
14 * notice, this list of conditions and the following disclaimer in the
|
|
15 * documentation and/or other materials provided with the distribution.
|
|
16 *
|
|
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
27 * POSSIBILITY OF SUCH DAMAGE.
|
|
28 */
|
|
29
|
15
|
30 #include <assert.h>
|
|
31 #include <stdbool.h>
|
|
32 #include <stdlib.h>
|
|
33 #include <string.h>
|
|
34
|
|
35 #include "utils.h"
|
|
36 #include "mode.h"
|
|
37 #include "place.h"
|
|
38 #include "files.h"
|
|
39 #include "directive.h"
|
|
40 #include "macro.h"
|
|
41 #include "eval.h"
|
|
42
|
|
43 struct ifstate {
|
|
44 struct ifstate *prev;
|
|
45 struct place startplace;
|
|
46 bool curtrue;
|
|
47 bool evertrue;
|
|
48 bool seenelse;
|
|
49 };
|
|
50
|
|
51 static struct ifstate *ifstate;
|
|
52
|
|
53 ////////////////////////////////////////////////////////////
|
|
54 // common parsing bits
|
|
55
|
|
56 static
|
|
57 void
|
|
58 oneword(const char *what, struct place *p2, char *line)
|
|
59 {
|
|
60 size_t pos;
|
|
61
|
|
62 pos = strcspn(line, ws);
|
|
63 if (line[pos] != '\0') {
|
|
64 p2->column += pos;
|
|
65 complain(p2, "Garbage after %s argument", what);
|
|
66 complain_fail();
|
|
67 line[pos] = '\0';
|
|
68 }
|
|
69 }
|
|
70
|
|
71 ////////////////////////////////////////////////////////////
|
|
72 // if handling
|
|
73
|
|
74 static
|
|
75 struct ifstate *
|
|
76 ifstate_create(struct ifstate *prev, struct place *p, bool startstate)
|
|
77 {
|
|
78 struct ifstate *is;
|
|
79
|
|
80 is = domalloc(sizeof(*is));
|
|
81 is->prev = prev;
|
|
82 if (p != NULL) {
|
|
83 is->startplace = *p;
|
|
84 } else {
|
|
85 place_setbuiltin(&is->startplace, 1);
|
|
86 }
|
|
87 is->curtrue = startstate;
|
|
88 is->evertrue = is->curtrue;
|
|
89 is->seenelse = false;
|
|
90 return is;
|
|
91 }
|
|
92
|
|
93 static
|
|
94 void
|
|
95 ifstate_destroy(struct ifstate *is)
|
|
96 {
|
|
97 free(is);
|
|
98 }
|
|
99
|
|
100 static
|
|
101 void
|
|
102 ifstate_push(struct place *p, bool startstate)
|
|
103 {
|
|
104 ifstate = ifstate_create(ifstate, p, startstate);
|
|
105 }
|
|
106
|
|
107 static
|
|
108 void
|
|
109 ifstate_pop(void)
|
|
110 {
|
|
111 struct ifstate *is;
|
|
112
|
|
113 is = ifstate;
|
|
114 ifstate = ifstate->prev;
|
|
115 ifstate_destroy(is);
|
|
116 }
|
|
117
|
|
118 static
|
|
119 void
|
|
120 d_if(struct place *p, struct place *p2, char *line, size_t len)
|
|
121 {
|
|
122 char *expr;
|
|
123 bool val;
|
16
|
124 struct place p3 = *p2;
|
15
|
125
|
|
126 expr = macroexpand(p2, line, len, true);
|
16
|
127 val = eval(&p3, expr);
|
15
|
128 ifstate_push(p, val);
|
|
129 free(expr);
|
|
130 }
|
|
131
|
|
132 static
|
|
133 void
|
|
134 d_ifdef(struct place *p, struct place *p2, char *line, size_t len)
|
|
135 {
|
|
136 oneword("#ifdef", p2, line);
|
|
137 ifstate_push(p, macro_isdefined(line));
|
|
138 }
|
|
139
|
|
140 static
|
|
141 void
|
|
142 d_ifndef(struct place *p, struct place *p2, char *line, size_t len)
|
|
143 {
|
|
144 oneword("#ifndef", p2, line);
|
|
145 ifstate_push(p, !macro_isdefined(line));
|
|
146 }
|
|
147
|
|
148 static
|
|
149 void
|
|
150 d_elif(struct place *p, struct place *p2, char *line, size_t len)
|
|
151 {
|
|
152 char *expr;
|
16
|
153 struct place p3 = *p2;
|
15
|
154
|
|
155 if (ifstate->seenelse) {
|
|
156 complain(p, "#elif after #else");
|
|
157 complain_fail();
|
|
158 }
|
|
159
|
|
160 if (ifstate->evertrue) {
|
|
161 ifstate->curtrue = false;
|
|
162 } else {
|
|
163 expr = macroexpand(p2, line, len, true);
|
16
|
164 ifstate->curtrue = eval(&p3, expr);
|
15
|
165 ifstate->evertrue = ifstate->curtrue;
|
|
166 free(expr);
|
|
167 }
|
|
168 }
|
|
169
|
|
170 static
|
|
171 void
|
|
172 d_else(struct place *p, struct place *p2, char *line, size_t len)
|
|
173 {
|
|
174 if (ifstate->seenelse) {
|
|
175 complain(p, "Multiple #else directives in one conditional");
|
|
176 complain_fail();
|
|
177 }
|
|
178
|
|
179 ifstate->curtrue = !ifstate->evertrue;
|
|
180 ifstate->evertrue = true;
|
|
181 ifstate->seenelse = true;
|
|
182 }
|
|
183
|
|
184 static
|
|
185 void
|
|
186 d_endif(struct place *p, struct place *p2, char *line, size_t len)
|
|
187 {
|
|
188 if (ifstate->prev == NULL) {
|
|
189 complain(p, "Unmatched #endif");
|
|
190 complain_fail();
|
|
191 } else {
|
|
192 ifstate_pop();
|
|
193 }
|
|
194 }
|
|
195
|
|
196 ////////////////////////////////////////////////////////////
|
|
197 // macros
|
|
198
|
|
199 static
|
|
200 void
|
|
201 d_define(struct place *p, struct place *p2, char *line, size_t len)
|
|
202 {
|
18
|
203 size_t pos, argpos;
|
|
204 struct place p3, p4;
|
15
|
205
|
|
206 /*
|
|
207 * line may be:
|
|
208 * macro expansion
|
|
209 * macro(arg, arg, ...) expansion
|
|
210 */
|
|
211
|
|
212 pos = strcspn(line, " \t\f\v(");
|
|
213 if (line[pos] == '(') {
|
18
|
214 line[pos++] = '\0';
|
|
215 argpos = pos;
|
15
|
216 pos = pos + strcspn(line+pos, "()");
|
|
217 if (line[pos] == '(') {
|
|
218 p2->column += pos;
|
|
219 complain(p2, "Left parenthesis in macro parameters");
|
|
220 complain_fail();
|
|
221 return;
|
|
222 }
|
|
223 if (line[pos] != ')') {
|
|
224 p2->column += pos;
|
|
225 complain(p2, "Unclosed macro parameter list");
|
|
226 complain_fail();
|
|
227 return;
|
|
228 }
|
18
|
229 line[pos++] = '\0';
|
15
|
230 if (!strchr(ws, line[pos])) {
|
|
231 p2->column += pos;
|
|
232 complain(p2, "Trash after macro parameter list");
|
|
233 complain_fail();
|
|
234 return;
|
|
235 }
|
|
236 } else if (line[pos] == '\0') {
|
18
|
237 argpos = 0;
|
15
|
238 } else {
|
|
239 line[pos++] = '\0';
|
18
|
240 argpos = 0;
|
15
|
241 }
|
|
242
|
|
243 pos += strspn(line+pos, ws);
|
|
244
|
|
245 p3 = *p2;
|
18
|
246 p3.column += argpos;
|
|
247
|
|
248 p4 = *p2;
|
|
249 p4.column += pos;
|
|
250
|
|
251 if (argpos) {
|
|
252 macro_define_params(p2, line, &p3,
|
|
253 line + argpos, &p4,
|
|
254 line + pos);
|
|
255 } else {
|
|
256 macro_define_plain(p2, line, &p4, line + pos);
|
|
257 }
|
15
|
258 }
|
|
259
|
|
260 static
|
|
261 void
|
|
262 d_undef(struct place *p, struct place *p2, char *line, size_t len)
|
|
263 {
|
|
264 oneword("#undef", p2, line);
|
|
265 macro_undef(line);
|
|
266 }
|
|
267
|
|
268 ////////////////////////////////////////////////////////////
|
|
269 // includes
|
|
270
|
|
271 static
|
|
272 bool
|
|
273 tryinclude(struct place *p, char *line, size_t len)
|
|
274 {
|
|
275 if (len > 2 && line[0] == '"' && line[len-1] == '"') {
|
|
276 line[len-1] = '\0';
|
|
277 file_readquote(p, line+1);
|
|
278 return true;
|
|
279 }
|
|
280 if (len > 2 && line[0] == '<' && line[len-1] == '>') {
|
|
281 line[len-1] = '\0';
|
|
282 file_readbracket(p, line+1);
|
|
283 return true;
|
|
284 }
|
|
285 return false;
|
|
286 }
|
|
287
|
|
288 static
|
|
289 void
|
|
290 d_include(struct place *p, struct place *p2, char *line, size_t len)
|
|
291 {
|
|
292 char *text;
|
|
293
|
|
294 if (tryinclude(p, line, len)) {
|
|
295 return;
|
|
296 }
|
|
297 text = macroexpand(p2, line, len, false);
|
|
298 if (tryinclude(p, text, strlen(text))) {
|
|
299 free(text);
|
|
300 return;
|
|
301 }
|
|
302 free(text);
|
|
303 complain(p, "Illegal #include directive");
|
|
304 complain_fail();
|
|
305 }
|
|
306
|
|
307 static
|
|
308 void
|
|
309 d_line(struct place *p, struct place *p2, char *line, size_t len)
|
|
310 {
|
|
311 /* XXX */
|
|
312 complain(p, "Sorry, no #line yet");
|
|
313 }
|
|
314
|
|
315 ////////////////////////////////////////////////////////////
|
|
316 // messages
|
|
317
|
|
318 static
|
|
319 void
|
|
320 d_warning(struct place *p, struct place *p2, char *line, size_t len)
|
|
321 {
|
|
322 char *msg;
|
|
323
|
|
324 msg = macroexpand(p2, line, len, false);
|
|
325 complain(p, "#warning: %s", msg);
|
|
326 if (mode.werror) {
|
|
327 complain_fail();
|
|
328 }
|
|
329 free(msg);
|
|
330 }
|
|
331
|
|
332 static
|
|
333 void
|
|
334 d_error(struct place *p, struct place *p2, char *line, size_t len)
|
|
335 {
|
|
336 char *msg;
|
|
337
|
|
338 msg = macroexpand(p2, line, len, false);
|
|
339 complain(p, "#error: %s", msg);
|
|
340 complain_fail();
|
|
341 free(msg);
|
|
342 }
|
|
343
|
|
344 ////////////////////////////////////////////////////////////
|
|
345 // other
|
|
346
|
|
347 static
|
|
348 void
|
|
349 d_pragma(struct place *p, struct place *p2, char *line, size_t len)
|
|
350 {
|
|
351 complain(p, "#pragma %s", line);
|
|
352 complain_fail();
|
|
353 }
|
|
354
|
|
355 ////////////////////////////////////////////////////////////
|
|
356 // directive table
|
|
357
|
|
358 static const struct {
|
|
359 const char *name;
|
|
360 bool ifskip;
|
|
361 void (*func)(struct place *, struct place *, char *line, size_t len);
|
|
362 } directives[] = {
|
|
363 { "define", true, d_define },
|
|
364 { "elif", false, d_elif },
|
|
365 { "else", false, d_else },
|
|
366 { "endif", false, d_endif },
|
|
367 { "error", true, d_error },
|
|
368 { "if", false, d_if },
|
|
369 { "ifdef", false, d_ifdef },
|
|
370 { "ifndef", false, d_ifndef },
|
|
371 { "include", true, d_include },
|
|
372 { "line", true, d_line },
|
|
373 { "pragma", true, d_pragma },
|
|
374 { "undef", true, d_undef },
|
|
375 { "warning", true, d_warning },
|
|
376 };
|
|
377 static const unsigned numdirectives = HOWMANY(directives);
|
|
378
|
|
379 static
|
|
380 void
|
|
381 directive_gotdirective(struct place *p, char *line, size_t linelen)
|
|
382 {
|
|
383 struct place p2;
|
|
384 size_t len, skip;
|
|
385 unsigned i;
|
|
386
|
|
387 p2 = *p;
|
|
388 for (i=0; i<numdirectives; i++) {
|
|
389 len = strlen(directives[i].name);
|
|
390 if (!strncmp(line, directives[i].name, len) &&
|
|
391 strchr(ws, line[len])) {
|
|
392 if (directives[i].ifskip && !ifstate->curtrue) {
|
|
393 return;
|
|
394 }
|
|
395 skip = len + strspn(line+len, ws);
|
|
396 p2.column += skip;
|
|
397 line += skip;
|
|
398 linelen -= skip;
|
|
399 linelen = notrailingws(line, linelen);
|
|
400 directives[i].func(p, &p2, line, linelen);
|
|
401 return;
|
|
402 }
|
|
403 }
|
|
404 skip = strcspn(line, ws);
|
|
405 complain(p, "Unknown directive #%.*s", (int)skip, line);
|
|
406 complain_fail();
|
|
407 }
|
|
408
|
|
409 void
|
|
410 directive_gotline(struct place *p, char *line, size_t len)
|
|
411 {
|
|
412 size_t skip;
|
|
413
|
|
414 /* check if we have a directive line */
|
|
415 skip = strspn(line, ws);
|
|
416 if (line[skip] == '#') {
|
|
417 skip = skip + 1 + strspn(line + skip + 1, ws);
|
|
418 p->column += skip;
|
|
419 directive_gotdirective(p, line+skip, len-skip);
|
|
420 } else if (ifstate->curtrue) {
|
|
421 macro_sendline(p, line, len);
|
|
422 }
|
|
423 }
|
|
424
|
|
425
|
|
426 void
|
|
427 directive_goteof(struct place *p)
|
|
428 {
|
|
429 while (ifstate->prev != NULL) {
|
|
430 complain(p, "Missing #endif");
|
|
431 complain(&ifstate->startplace, "...opened at this point");
|
|
432 complain_failed();
|
|
433 ifstate_pop();
|
|
434 }
|
|
435 macro_sendeof(p);
|
|
436 }
|
|
437
|
|
438 ////////////////////////////////////////////////////////////
|
|
439 // module initialization
|
|
440
|
|
441 void
|
|
442 directive_init(void)
|
|
443 {
|
|
444 ifstate = ifstate_create(NULL, NULL, true);
|
|
445 }
|
|
446
|
|
447 void
|
|
448 directive_cleanup(void)
|
|
449 {
|
|
450 assert(ifstate->prev == NULL);
|
|
451 ifstate_destroy(ifstate);
|
|
452 ifstate = NULL;
|
|
453 }
|