Print this page
12257 resync smatch to 0.6.1-rc1-il-4
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/pre-process.c
+++ new/usr/src/tools/smatch/src/pre-process.c
1 1 /*
2 2 * Do C preprocessing, based on a token list gathered by
3 3 * the tokenizer.
4 4 *
5 5 * This may not be the smartest preprocessor on the planet.
6 6 *
7 7 * Copyright (C) 2003 Transmeta Corp.
8 8 * 2003-2004 Linus Torvalds
9 9 *
10 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 11 * of this software and associated documentation files (the "Software"), to deal
12 12 * in the Software without restriction, including without limitation the rights
13 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 14 * copies of the Software, and to permit persons to whom the Software is
15 15 * furnished to do so, subject to the following conditions:
16 16 *
17 17 * The above copyright notice and this permission notice shall be included in
18 18 * all copies or substantial portions of the Software.
19 19 *
20 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 26 * THE SOFTWARE.
27 27 */
28 28 #include <stdio.h>
29 29 #include <stdlib.h>
30 30 #include <stdarg.h>
31 31 #include <stddef.h>
32 32 #include <string.h>
33 33 #include <ctype.h>
34 34 #include <unistd.h>
35 35 #include <fcntl.h>
36 36 #include <limits.h>
37 37 #include <time.h>
38 38 #include <dirent.h>
39 39 #include <sys/stat.h>
40 40
41 41 #include "lib.h"
42 42 #include "allocate.h"
43 43 #include "parse.h"
44 44 #include "token.h"
45 45 #include "symbol.h"
46 46 #include "expression.h"
47 47 #include "scope.h"
48 48
49 49 static struct ident_list *macros; // only needed for -dD
50 50 static int false_nesting = 0;
51 51 static int counter_macro = 0; // __COUNTER__ expansion
52 52 static int include_level = 0;
53 53
54 54 #define INCLUDEPATHS 300
55 55 const char *includepath[INCLUDEPATHS+1] = {
56 56 "",
57 57 "/usr/include",
58 58 "/usr/local/include",
59 59 NULL
60 60 };
61 61
62 62 static const char **quote_includepath = includepath;
63 63 static const char **angle_includepath = includepath + 1;
64 64 static const char **isys_includepath = includepath + 1;
65 65 static const char **sys_includepath = includepath + 1;
66 66 static const char **dirafter_includepath = includepath + 3;
67 67
68 68 #define dirty_stream(stream) \
69 69 do { \
70 70 if (!stream->dirty) { \
71 71 stream->dirty = 1; \
72 72 if (!stream->ifndef) \
73 73 stream->protect = NULL; \
74 74 } \
75 75 } while(0)
76 76
77 77 #define end_group(stream) \
78 78 do { \
79 79 if (stream->ifndef == stream->top_if) { \
80 80 stream->ifndef = NULL; \
81 81 if (!stream->dirty) \
82 82 stream->protect = NULL; \
83 83 else if (stream->protect) \
84 84 stream->dirty = 0; \
85 85 } \
86 86 } while(0)
87 87
88 88 #define nesting_error(stream) \
89 89 do { \
90 90 stream->dirty = 1; \
91 91 stream->ifndef = NULL; \
92 92 stream->protect = NULL; \
93 93 } while(0)
94 94
95 95 static struct token *alloc_token(struct position *pos)
96 96 {
97 97 struct token *token = __alloc_token(0);
98 98
99 99 token->pos.stream = pos->stream;
100 100 token->pos.line = pos->line;
101 101 token->pos.pos = pos->pos;
102 102 token->pos.whitespace = 1;
103 103 return token;
104 104 }
105 105
106 106 /* Expand symbol 'sym' at '*list' */
107 107 static int expand(struct token **, struct symbol *);
108 108
109 109 static void replace_with_string(struct token *token, const char *str)
110 110 {
111 111 int size = strlen(str) + 1;
112 112 struct string *s = __alloc_string(size);
113 113
114 114 s->length = size;
115 115 memcpy(s->data, str, size);
116 116 token_type(token) = TOKEN_STRING;
117 117 token->string = s;
118 118 }
119 119
120 120 static void replace_with_integer(struct token *token, unsigned int val)
121 121 {
122 122 char *buf = __alloc_bytes(11);
123 123 sprintf(buf, "%u", val);
124 124 token_type(token) = TOKEN_NUMBER;
125 125 token->number = buf;
126 126 }
127 127
128 128 static struct symbol *lookup_macro(struct ident *ident)
129 129 {
130 130 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
131 131 if (sym && sym->namespace != NS_MACRO)
132 132 sym = NULL;
133 133 return sym;
134 134 }
135 135
136 136 static int token_defined(struct token *token)
137 137 {
138 138 if (token_type(token) == TOKEN_IDENT) {
139 139 struct symbol *sym = lookup_macro(token->ident);
140 140 if (sym) {
141 141 sym->used_in = file_scope;
142 142 return 1;
143 143 }
144 144 return 0;
145 145 }
146 146
147 147 sparse_error(token->pos, "expected preprocessor identifier");
148 148 return 0;
149 149 }
150 150
151 151 static void replace_with_bool(struct token *token, bool val)
152 152 {
153 153 static const char *string[] = { "0", "1" };
154 154
155 155 token_type(token) = TOKEN_NUMBER;
156 156 token->number = string[val];
157 157 }
158 158
159 159 static void replace_with_defined(struct token *token)
160 160 {
161 161 replace_with_bool(token, token_defined(token));
162 162 }
163 163
164 164 static void replace_with_has_builtin(struct token *token)
165 165 {
166 166 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL);
167 167 replace_with_bool(token, sym && sym->builtin);
168 168 }
169 169
170 170 static void replace_with_has_attribute(struct token *token)
171 171 {
172 172 struct symbol *sym = lookup_symbol(token->ident, NS_KEYWORD);
173 173 replace_with_bool(token, sym && sym->op && sym->op->attribute);
174 174 }
175 175
176 176 static void expand_line(struct token *token)
177 177 {
178 178 replace_with_integer(token, token->pos.line);
179 179 }
180 180
181 181 static void expand_file(struct token *token)
182 182 {
183 183 replace_with_string(token, stream_name(token->pos.stream));
184 184 }
185 185
186 186 static void expand_basefile(struct token *token)
187 187 {
188 188 replace_with_string(token, base_filename);
189 189 }
190 190
191 191 static time_t t = 0;
192 192 static void expand_date(struct token *token)
193 193 {
194 194 static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
195 195
196 196 if (!t)
197 197 time(&t);
198 198 strftime(buffer, 12, "%b %e %Y", localtime(&t));
199 199 replace_with_string(token, buffer);
200 200 }
201 201
202 202 static void expand_time(struct token *token)
203 203 {
204 204 static char buffer[9]; /* __TIME__: 2 + ':' + 2 + ':' + 2 + '\0' */
205 205
206 206 if (!t)
207 207 time(&t);
208 208 strftime(buffer, 9, "%T", localtime(&t));
209 209 replace_with_string(token, buffer);
210 210 }
211 211
212 212 static void expand_counter(struct token *token)
213 213 {
214 214 replace_with_integer(token, counter_macro++);
215 215 }
216 216
217 217 static void expand_include_level(struct token *token)
218 218 {
219 219 replace_with_integer(token, include_level - 1);
220 220 }
221 221
222 222 static int expand_one_symbol(struct token **list)
223 223 {
224 224 struct token *token = *list;
225 225 struct symbol *sym;
226 226
227 227 if (token->pos.noexpand)
228 228 return 1;
229 229
230 230 sym = lookup_macro(token->ident);
231 231 if (!sym)
232 232 return 1;
233 233 store_macro_pos(token);
234 234 if (sym->expander) {
235 235 sym->expander(token);
236 236 return 1;
237 237 } else {
238 238 sym->used_in = file_scope;
239 239 return expand(list, sym);
240 240 }
241 241 }
242 242
243 243 static inline struct token *scan_next(struct token **where)
244 244 {
245 245 struct token *token = *where;
246 246 if (token_type(token) != TOKEN_UNTAINT)
247 247 return token;
248 248 do {
249 249 token->ident->tainted = 0;
250 250 token = token->next;
251 251 } while (token_type(token) == TOKEN_UNTAINT);
252 252 *where = token;
253 253 return token;
254 254 }
255 255
256 256 static void expand_list(struct token **list)
257 257 {
258 258 struct token *next;
259 259 while (!eof_token(next = scan_next(list))) {
260 260 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
261 261 list = &next->next;
262 262 }
263 263 }
264 264
265 265 static void preprocessor_line(struct stream *stream, struct token **line);
266 266
267 267 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos, int count)
268 268 {
269 269 struct stream *stream = input_streams + prev->pos.stream;
270 270 struct token **p = &prev->next;
271 271 struct token *next;
272 272 int nesting = 0;
273 273
274 274 while (!eof_token(next = scan_next(p))) {
275 275 if (next->pos.newline && match_op(next, '#')) {
276 276 if (!next->pos.noexpand) {
277 277 sparse_error(next->pos,
278 278 "directive in argument list");
279 279 preprocessor_line(stream, p);
280 280 __free_token(next); /* Free the '#' token */
281 281 continue;
282 282 }
283 283 }
284 284 switch (token_type(next)) {
285 285 case TOKEN_STREAMEND:
286 286 case TOKEN_STREAMBEGIN:
287 287 *p = &eof_token_entry;
288 288 return next;
289 289 case TOKEN_STRING:
290 290 case TOKEN_WIDE_STRING:
291 291 if (count > 1)
292 292 next->string->immutable = 1;
293 293 break;
294 294 }
295 295 if (false_nesting) {
296 296 *p = next->next;
297 297 __free_token(next);
298 298 continue;
299 299 }
300 300 if (match_op(next, '(')) {
301 301 nesting++;
302 302 } else if (match_op(next, ')')) {
303 303 if (!nesting--)
304 304 break;
305 305 } else if (match_op(next, ',') && !nesting && !vararg) {
306 306 break;
307 307 }
308 308 next->pos.stream = pos->stream;
309 309 next->pos.line = pos->line;
310 310 next->pos.pos = pos->pos;
311 311 p = &next->next;
312 312 }
313 313 *p = &eof_token_entry;
314 314 return next;
315 315 }
316 316
317 317 /*
318 318 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
319 319 */
320 320
321 321 struct arg {
322 322 struct token *arg;
323 323 struct token *expanded;
324 324 struct token *str;
325 325 int n_normal;
326 326 int n_quoted;
327 327 int n_str;
328 328 };
329 329
330 330 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
331 331 {
332 332 int wanted = arglist->count.normal;
333 333 struct token *next = NULL;
334 334 int count = 0;
335 335
336 336 arglist = arglist->next; /* skip counter */
337 337
338 338 if (!wanted) {
339 339 next = collect_arg(start, 0, &what->pos, 0);
340 340 if (eof_token(next))
341 341 goto Eclosing;
342 342 if (!eof_token(start->next) || !match_op(next, ')')) {
343 343 count++;
344 344 goto Emany;
345 345 }
346 346 } else {
347 347 for (count = 0; count < wanted; count++) {
348 348 struct argcount *p = &arglist->next->count;
349 349 next = collect_arg(start, p->vararg, &what->pos, p->normal);
350 350 if (eof_token(next))
351 351 goto Eclosing;
352 352 if (p->vararg && wanted == 1 && eof_token(start->next))
353 353 break;
354 354 arglist = arglist->next->next;
355 355 args[count].arg = start->next;
356 356 args[count].n_normal = p->normal;
357 357 args[count].n_quoted = p->quoted;
358 358 args[count].n_str = p->str;
359 359 if (match_op(next, ')')) {
360 360 count++;
361 361 break;
362 362 }
363 363 start = next;
364 364 }
365 365 if (count == wanted && !match_op(next, ')'))
366 366 goto Emany;
367 367 if (count == wanted - 1) {
368 368 struct argcount *p = &arglist->next->count;
369 369 if (!p->vararg)
370 370 goto Efew;
371 371 args[count].arg = NULL;
372 372 args[count].n_normal = p->normal;
373 373 args[count].n_quoted = p->quoted;
374 374 args[count].n_str = p->str;
375 375 }
376 376 if (count < wanted - 1)
377 377 goto Efew;
378 378 }
379 379 what->next = next->next;
380 380 return 1;
381 381
382 382 Efew:
383 383 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
384 384 show_token(what), wanted, count);
385 385 goto out;
386 386 Emany:
387 387 while (match_op(next, ',')) {
388 388 next = collect_arg(next, 0, &what->pos, 0);
389 389 count++;
390 390 }
391 391 if (eof_token(next))
392 392 goto Eclosing;
393 393 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
394 394 show_token(what), count, wanted);
395 395 goto out;
396 396 Eclosing:
397 397 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
398 398 show_token(what));
399 399 out:
400 400 what->next = next->next;
401 401 return 0;
402 402 }
403 403
404 404 static struct token *dup_list(struct token *list)
405 405 {
406 406 struct token *res = NULL;
407 407 struct token **p = &res;
408 408
409 409 while (!eof_token(list)) {
410 410 struct token *newtok = __alloc_token(0);
411 411 *newtok = *list;
412 412 *p = newtok;
413 413 p = &newtok->next;
414 414 list = list->next;
415 415 }
416 416 return res;
417 417 }
418 418
419 419 static const char *show_token_sequence(struct token *token, int quote)
420 420 {
421 421 static char buffer[MAX_STRING];
422 422 char *ptr = buffer;
423 423 int whitespace = 0;
424 424
425 425 if (!token && !quote)
426 426 return "<none>";
427 427 while (!eof_token(token)) {
428 428 const char *val = quote ? quote_token(token) : show_token(token);
429 429 int len = strlen(val);
430 430
431 431 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
432 432 sparse_error(token->pos, "too long token expansion");
433 433 break;
434 434 }
435 435
436 436 if (whitespace)
437 437 *ptr++ = ' ';
438 438 memcpy(ptr, val, len);
439 439 ptr += len;
440 440 token = token->next;
441 441 whitespace = token->pos.whitespace;
442 442 }
443 443 *ptr = 0;
444 444 return buffer;
445 445 }
446 446
447 447 static struct token *stringify(struct token *arg)
448 448 {
449 449 const char *s = show_token_sequence(arg, 1);
450 450 int size = strlen(s)+1;
451 451 struct token *token = __alloc_token(0);
452 452 struct string *string = __alloc_string(size);
453 453
454 454 memcpy(string->data, s, size);
455 455 string->length = size;
456 456 token->pos = arg->pos;
457 457 token_type(token) = TOKEN_STRING;
458 458 token->string = string;
459 459 token->next = &eof_token_entry;
460 460 return token;
461 461 }
462 462
463 463 static void expand_arguments(int count, struct arg *args)
464 464 {
465 465 int i;
466 466 for (i = 0; i < count; i++) {
467 467 struct token *arg = args[i].arg;
468 468 if (!arg)
469 469 arg = &eof_token_entry;
470 470 if (args[i].n_str)
471 471 args[i].str = stringify(arg);
472 472 if (args[i].n_normal) {
473 473 if (!args[i].n_quoted) {
474 474 args[i].expanded = arg;
475 475 args[i].arg = NULL;
476 476 } else if (eof_token(arg)) {
477 477 args[i].expanded = arg;
478 478 } else {
479 479 args[i].expanded = dup_list(arg);
480 480 }
481 481 expand_list(&args[i].expanded);
482 482 }
483 483 }
484 484 }
485 485
486 486 /*
487 487 * Possibly valid combinations:
488 488 * - ident + ident -> ident
489 489 * - ident + number -> ident unless number contains '.', '+' or '-'.
490 490 * - 'L' + char constant -> wide char constant
491 491 * - 'L' + string literal -> wide string literal
492 492 * - number + number -> number
493 493 * - number + ident -> number
494 494 * - number + '.' -> number
495 495 * - number + '+' or '-' -> number, if number used to end on [eEpP].
496 496 * - '.' + number -> number, if number used to start with a digit.
497 497 * - special + special -> either special or an error.
498 498 */
499 499 static enum token_type combine(struct token *left, struct token *right, char *p)
500 500 {
501 501 int len;
502 502 enum token_type t1 = token_type(left), t2 = token_type(right);
503 503
504 504 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
505 505 return TOKEN_ERROR;
506 506
507 507 if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
508 508 if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
509 509 return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
510 510 if (t2 == TOKEN_STRING)
511 511 return TOKEN_WIDE_STRING;
512 512 }
513 513
514 514 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
515 515 return TOKEN_ERROR;
516 516
517 517 strcpy(p, show_token(left));
518 518 strcat(p, show_token(right));
519 519 len = strlen(p);
520 520
521 521 if (len >= 256)
522 522 return TOKEN_ERROR;
523 523
524 524 if (t1 == TOKEN_IDENT) {
525 525 if (t2 == TOKEN_SPECIAL)
526 526 return TOKEN_ERROR;
527 527 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
528 528 return TOKEN_ERROR;
529 529 return TOKEN_IDENT;
530 530 }
531 531
532 532 if (t1 == TOKEN_NUMBER) {
533 533 if (t2 == TOKEN_SPECIAL) {
534 534 switch (right->special) {
535 535 case '.':
536 536 break;
537 537 case '+': case '-':
538 538 if (strchr("eEpP", p[len - 2]))
539 539 break;
540 540 default:
541 541 return TOKEN_ERROR;
542 542 }
543 543 }
544 544 return TOKEN_NUMBER;
545 545 }
546 546
547 547 if (p[0] == '.' && isdigit((unsigned char)p[1]))
548 548 return TOKEN_NUMBER;
549 549
550 550 return TOKEN_SPECIAL;
551 551 }
552 552
553 553 static int merge(struct token *left, struct token *right)
554 554 {
555 555 static char buffer[512];
556 556 enum token_type res = combine(left, right, buffer);
557 557 int n;
558 558
559 559 switch (res) {
560 560 case TOKEN_IDENT:
561 561 left->ident = built_in_ident(buffer);
562 562 left->pos.noexpand = 0;
563 563 return 1;
564 564
565 565 case TOKEN_NUMBER:
566 566 token_type(left) = TOKEN_NUMBER; /* could be . + num */
567 567 left->number = xstrdup(buffer);
568 568 return 1;
569 569
570 570 case TOKEN_SPECIAL:
571 571 if (buffer[2] && buffer[3])
572 572 break;
573 573 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
574 574 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
575 575 left->special = n;
576 576 return 1;
577 577 }
578 578 }
579 579 break;
580 580
581 581 case TOKEN_WIDE_CHAR:
582 582 case TOKEN_WIDE_STRING:
583 583 token_type(left) = res;
584 584 left->pos.noexpand = 0;
585 585 left->string = right->string;
586 586 return 1;
587 587
588 588 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
589 589 token_type(left) = res;
590 590 left->pos.noexpand = 0;
591 591 memcpy(left->embedded, right->embedded, 4);
592 592 return 1;
593 593
594 594 default:
595 595 ;
596 596 }
597 597 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
598 598 return 0;
599 599 }
600 600
601 601 static struct token *dup_token(struct token *token, struct position *streampos)
602 602 {
603 603 struct token *alloc = alloc_token(streampos);
604 604 token_type(alloc) = token_type(token);
605 605 alloc->pos.newline = token->pos.newline;
606 606 alloc->pos.whitespace = token->pos.whitespace;
607 607 alloc->number = token->number;
608 608 alloc->pos.noexpand = token->pos.noexpand;
609 609 return alloc;
610 610 }
611 611
612 612 static struct token **copy(struct token **where, struct token *list, int *count)
613 613 {
614 614 int need_copy = --*count;
615 615 while (!eof_token(list)) {
616 616 struct token *token;
617 617 if (need_copy)
618 618 token = dup_token(list, &list->pos);
619 619 else
620 620 token = list;
621 621 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
622 622 token->pos.noexpand = 1;
623 623 *where = token;
624 624 where = &token->next;
625 625 list = list->next;
626 626 }
627 627 *where = &eof_token_entry;
628 628 return where;
629 629 }
630 630
631 631 static int handle_kludge(struct token **p, struct arg *args)
632 632 {
633 633 struct token *t = (*p)->next->next;
634 634 while (1) {
635 635 struct arg *v = &args[t->argnum];
636 636 if (token_type(t->next) != TOKEN_CONCAT) {
637 637 if (v->arg) {
638 638 /* ignore the first ## */
639 639 *p = (*p)->next;
640 640 return 0;
641 641 }
642 642 /* skip the entire thing */
643 643 *p = t;
644 644 return 1;
645 645 }
646 646 if (v->arg && !eof_token(v->arg))
647 647 return 0; /* no magic */
648 648 t = t->next->next;
649 649 }
650 650 }
651 651
652 652 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
653 653 {
654 654 struct position *base_pos = &(*list)->pos;
655 655 int *count;
656 656 enum {Normal, Placeholder, Concat} state = Normal;
657 657
658 658 for (; !eof_token(body); body = body->next) {
659 659 struct token *added, *arg;
660 660 struct token **tail;
661 661 struct token *t;
662 662
663 663 switch (token_type(body)) {
664 664 case TOKEN_GNU_KLUDGE:
665 665 /*
666 666 * GNU kludge: if we had <comma>##<vararg>, behaviour
667 667 * depends on whether we had enough arguments to have
668 668 * a vararg. If we did, ## is just ignored. Otherwise
669 669 * both , and ## are ignored. Worse, there can be
670 670 * an arbitrary number of ##<arg> in between; if all of
671 671 * those are empty, we act as if they hadn't been there,
672 672 * otherwise we act as if the kludge didn't exist.
673 673 */
674 674 t = body;
675 675 if (handle_kludge(&body, args)) {
676 676 if (state == Concat)
677 677 state = Normal;
678 678 else
679 679 state = Placeholder;
680 680 continue;
681 681 }
682 682 added = dup_token(t, base_pos);
683 683 token_type(added) = TOKEN_SPECIAL;
684 684 tail = &added->next;
685 685 break;
686 686
687 687 case TOKEN_STR_ARGUMENT:
688 688 arg = args[body->argnum].str;
689 689 count = &args[body->argnum].n_str;
690 690 goto copy_arg;
691 691
692 692 case TOKEN_QUOTED_ARGUMENT:
693 693 arg = args[body->argnum].arg;
694 694 count = &args[body->argnum].n_quoted;
695 695 if (!arg || eof_token(arg)) {
696 696 if (state == Concat)
697 697 state = Normal;
698 698 else
699 699 state = Placeholder;
700 700 continue;
701 701 }
702 702 goto copy_arg;
703 703
704 704 case TOKEN_MACRO_ARGUMENT:
705 705 arg = args[body->argnum].expanded;
706 706 count = &args[body->argnum].n_normal;
707 707 if (eof_token(arg)) {
708 708 state = Normal;
709 709 continue;
710 710 }
711 711 copy_arg:
712 712 tail = copy(&added, arg, count);
713 713 added->pos.newline = body->pos.newline;
714 714 added->pos.whitespace = body->pos.whitespace;
715 715 break;
716 716
717 717 case TOKEN_CONCAT:
718 718 if (state == Placeholder)
719 719 state = Normal;
720 720 else
721 721 state = Concat;
722 722 continue;
723 723
724 724 case TOKEN_IDENT:
725 725 added = dup_token(body, base_pos);
726 726 if (added->ident->tainted)
727 727 added->pos.noexpand = 1;
728 728 tail = &added->next;
729 729 break;
730 730
731 731 default:
732 732 added = dup_token(body, base_pos);
733 733 tail = &added->next;
734 734 break;
735 735 }
736 736
737 737 /*
738 738 * if we got to doing real concatenation, we already have
739 739 * added something into the list, so containing_token() is OK.
740 740 */
741 741 if (state == Concat && merge(containing_token(list), added)) {
742 742 *list = added->next;
743 743 if (tail != &added->next)
744 744 list = tail;
745 745 } else {
746 746 *list = added;
747 747 list = tail;
748 748 }
749 749 state = Normal;
750 750 }
751 751 *list = &eof_token_entry;
752 752 return list;
753 753 }
754 754
755 755 static int expand(struct token **list, struct symbol *sym)
756 756 {
757 757 struct token *last;
758 758 struct token *token = *list;
759 759 struct ident *expanding = token->ident;
760 760 struct token **tail;
761 761 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
762 762 struct arg args[nargs];
763 763
764 764 if (expanding->tainted) {
765 765 token->pos.noexpand = 1;
766 766 return 1;
767 767 }
768 768
769 769 if (sym->arglist) {
770 770 if (!match_op(scan_next(&token->next), '('))
771 771 return 1;
772 772 if (!collect_arguments(token->next, sym->arglist, args, token))
773 773 return 1;
774 774 expand_arguments(nargs, args);
775 775 }
776 776
777 777 expanding->tainted = 1;
778 778
779 779 last = token->next;
780 780 tail = substitute(list, sym->expansion, args);
781 781 /*
782 782 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
783 783 * We still can lose the newline flag if the sucker expands to nothing,
784 784 * but the price of dealing with that is probably too high (we'd need
785 785 * to collect the flags during scan_next())
786 786 */
787 787 (*list)->pos.newline = token->pos.newline;
788 788 (*list)->pos.whitespace = token->pos.whitespace;
789 789 *tail = last;
790 790
791 791 return 0;
792 792 }
793 793
794 794 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
795 795 {
796 796 static char buffer[256];
797 797 char *ptr = buffer;
798 798
799 799 while (!eof_token(token) && !match_op(token, endop)) {
800 800 int len;
801 801 const char *val = token->string->data;
802 802 if (token_type(token) != TOKEN_STRING)
803 803 val = show_token(token);
804 804 len = strlen(val);
805 805 memcpy(ptr, val, len);
806 806 ptr += len;
807 807 token = token->next;
808 808 }
809 809 *ptr = 0;
810 810 if (endop && !match_op(token, endop))
811 811 sparse_error(start->pos, "expected '>' at end of filename");
812 812 return buffer;
813 813 }
814 814
815 815 static int already_tokenized(const char *path)
816 816 {
817 817 int stream, next;
818 818
819 819 for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
820 820 struct stream *s = input_streams + stream;
821 821
822 822 next = s->next_stream;
823 823 if (s->once) {
824 824 if (strcmp(path, s->name))
825 825 continue;
826 826 return 1;
827 827 }
828 828 if (s->constant != CONSTANT_FILE_YES)
829 829 continue;
830 830 if (strcmp(path, s->name))
831 831 continue;
832 832 if (s->protect && !lookup_macro(s->protect))
833 833 continue;
834 834 return 1;
835 835 }
836 836 return 0;
837 837 }
838 838
839 839 /* Handle include of header files.
840 840 * The relevant options are made compatible with gcc. The only options that
841 841 * are not supported is -withprefix and friends.
842 842 *
843 843 * Three set of include paths are known:
844 844 * quote_includepath: Path to search when using #include "file.h"
845 845 * angle_includepath: Paths to search when using #include <file.h>
846 846 * isys_includepath: Paths specified with -isystem, come before the
847 847 * built-in system include paths. Gcc would suppress
848 848 * warnings from system headers. Here we separate
849 849 * them from the angle_ ones to keep search ordering.
850 850 *
851 851 * sys_includepath: Built-in include paths.
852 852 * dirafter_includepath Paths added with -dirafter.
853 853 *
854 854 * The above is implemented as one array with pointers
855 855 * +--------------+
856 856 * quote_includepath ---> | |
857 857 * +--------------+
858 858 * | |
859 859 * +--------------+
860 860 * angle_includepath ---> | |
861 861 * +--------------+
862 862 * isys_includepath ---> | |
863 863 * +--------------+
864 864 * sys_includepath ---> | |
865 865 * +--------------+
866 866 * dirafter_includepath -> | |
867 867 * +--------------+
868 868 *
869 869 * -I dir insert dir just before isys_includepath and move the rest
870 870 * -I- makes all dirs specified with -I before to quote dirs only and
871 871 * angle_includepath is set equal to isys_includepath.
872 872 * -nostdinc removes all sys dirs by storing NULL in entry pointed
873 873 * to by * sys_includepath. Note that this will reset all dirs built-in
874 874 * and added before -nostdinc by -isystem and -idirafter.
875 875 * -isystem dir adds dir where isys_includepath points adding this dir as
876 876 * first systemdir
877 877 * -idirafter dir adds dir to the end of the list
878 878 */
879 879
880 880 static void set_stream_include_path(struct stream *stream)
881 881 {
882 882 const char *path = stream->path;
883 883 if (!path) {
884 884 const char *p = strrchr(stream->name, '/');
885 885 path = "";
886 886 if (p) {
887 887 int len = p - stream->name + 1;
888 888 char *m = malloc(len+1);
889 889 /* This includes the final "/" */
890 890 memcpy(m, stream->name, len);
891 891 m[len] = 0;
892 892 path = m;
893 893 }
894 894 stream->path = path;
895 895 }
896 896 includepath[0] = path;
897 897 }
898 898
899 899 #ifndef PATH_MAX
900 900 #define PATH_MAX 4096 // for Hurd where it's not defined
901 901 #endif
902 902
903 903 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
904 904 {
905 905 int fd;
906 906 int plen = strlen(path);
907 907 static char fullname[PATH_MAX];
908 908
909 909 memcpy(fullname, path, plen);
910 910 if (plen && path[plen-1] != '/') {
911 911 fullname[plen] = '/';
912 912 plen++;
913 913 }
914 914 memcpy(fullname+plen, filename, flen);
915 915 if (already_tokenized(fullname))
916 916 return 1;
917 917 fd = open(fullname, O_RDONLY);
918 918 if (fd >= 0) {
919 919 char *streamname = xmemdup(fullname, plen + flen);
920 920 *where = tokenize(streamname, fd, *where, next_path);
921 921 close(fd);
922 922 return 1;
923 923 }
924 924 return 0;
925 925 }
926 926
927 927 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
928 928 {
929 929 const char *path;
930 930
931 931 while ((path = *pptr++) != NULL) {
932 932 if (!try_include(path, filename, flen, list, pptr))
933 933 continue;
934 934 return 1;
935 935 }
936 936 return 0;
937 937 }
938 938
939 939 static int free_preprocessor_line(struct token *token)
940 940 {
941 941 while (token_type(token) != TOKEN_EOF) {
942 942 struct token *free = token;
943 943 token = token->next;
944 944 __free_token(free);
945 945 };
946 946 return 1;
947 947 }
948 948
949 949 const char *find_include(const char *skip, const char *look_for)
950 950 {
951 951 DIR *dp;
952 952 struct dirent *entry;
953 953 struct stat statbuf;
954 954 const char *ret;
955 955 char cwd[PATH_MAX];
956 956 static char buf[PATH_MAX + 1];
957 957
958 958 dp = opendir(".");
959 959 if (!dp)
960 960 return NULL;
961 961
962 962 if (!getcwd(cwd, sizeof(cwd)))
963 963 goto close;
964 964
965 965 while ((entry = readdir(dp))) {
966 966 lstat(entry->d_name, &statbuf);
967 967
968 968 if (strcmp(entry->d_name, look_for) == 0) {
969 969 snprintf(buf, sizeof(buf), "%s/%s", cwd, entry->d_name);
970 970 closedir(dp);
971 971 return buf;
972 972 }
973 973
974 974 if (S_ISDIR(statbuf.st_mode)) {
975 975 /* Found a directory, but ignore . and .. */
976 976 if (strcmp(".", entry->d_name) == 0 ||
977 977 strcmp("..", entry->d_name) == 0 ||
978 978 strcmp(skip, entry->d_name) == 0)
979 979 continue;
980 980
981 981 chdir(entry->d_name);
982 982 ret = find_include("", look_for);
983 983 chdir("..");
984 984 if (ret) {
985 985 closedir(dp);
986 986 return ret;
987 987 }
988 988 }
989 989 }
990 990 close:
991 991 closedir(dp);
992 992
993 993 return NULL;
994 994 }
995 995
996 996 const char *search_dir(const char *stop, const char *look_for)
997 997 {
998 998 char cwd[PATH_MAX];
999 999 int len;
1000 1000 const char *ret;
1001 1001 int cnt = 0;
1002 1002
1003 1003 if (!getcwd(cwd, sizeof(cwd)))
1004 1004 return NULL;
1005 1005
1006 1006 len = strlen(cwd);
1007 1007 while (len >= 0) {
1008 1008 ret = find_include(cnt++ ? cwd + len + 1 : "", look_for);
1009 1009 if (ret)
1010 1010 return ret;
1011 1011
1012 1012 if (strcmp(cwd, stop) == 0 ||
1013 1013 strcmp(cwd, "/usr/include") == 0 ||
1014 1014 strcmp(cwd, "/usr/local/include") == 0 ||
1015 1015 strlen(cwd) <= 10 || /* heck... don't search /usr/lib/ */
1016 1016 strcmp(cwd, "/") == 0)
1017 1017 return NULL;
1018 1018
1019 1019 while (--len >= 0) {
1020 1020 if (cwd[len] == '/') {
1021 1021 cwd[len] = '\0';
1022 1022 break;
1023 1023 }
1024 1024 }
1025 1025
1026 1026 chdir("..");
1027 1027 }
1028 1028 return NULL;
1029 1029 }
1030 1030
1031 1031 static void use_best_guess_header_file(struct token *token, const char *filename, struct token **list)
1032 1032 {
1033 1033 char cwd[PATH_MAX];
1034 1034 char dir_part[PATH_MAX];
1035 1035 const char *file_part;
1036 1036 const char *include_name;
1037 1037 static int cnt;
1038 1038 int len;
1039 1039
1040 1040 /* Avoid guessing includes recursively. */
1041 1041 if (cnt++ > 1000)
1042 1042 return;
1043 1043
1044 1044 if (!filename || filename[0] == '\0')
1045 1045 return;
1046 1046
1047 1047 file_part = filename;
1048 1048 while ((filename = strchr(filename, '/'))) {
1049 1049 ++filename;
1050 1050 if (filename[0])
1051 1051 file_part = filename;
1052 1052 }
1053 1053
1054 1054 snprintf(dir_part, sizeof(dir_part), "%s", stream_name(token->pos.stream));
1055 1055 len = strlen(dir_part);
1056 1056 while (--len >= 0) {
1057 1057 if (dir_part[len] == '/') {
1058 1058 dir_part[len] = '\0';
1059 1059 break;
1060 1060 }
1061 1061 }
1062 1062 if (len < 0)
1063 1063 sprintf(dir_part, ".");
1064 1064
1065 1065 if (!getcwd(cwd, sizeof(cwd)))
1066 1066 return;
1067 1067
1068 1068 chdir(dir_part);
1069 1069 include_name = search_dir(cwd, file_part);
1070 1070 chdir(cwd);
1071 1071 if (!include_name)
1072 1072 return;
1073 1073 sparse_error(token->pos, "using '%s'", include_name);
1074 1074
1075 1075 try_include("", include_name, strlen(include_name), list, includepath);
1076 1076 }
1077 1077
1078 1078 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how)
1079 1079 {
1080 1080 const char *filename;
1081 1081 struct token *next;
1082 1082 const char **path;
1083 1083 int expect;
1084 1084 int flen;
1085 1085
1086 1086 next = token->next;
1087 1087 expect = '>';
1088 1088 if (!match_op(next, '<')) {
1089 1089 expand_list(&token->next);
1090 1090 expect = 0;
1091 1091 next = token;
1092 1092 if (match_op(token->next, '<')) {
1093 1093 next = token->next;
1094 1094 expect = '>';
1095 1095 }
1096 1096 }
1097 1097
1098 1098 token = next->next;
1099 1099 filename = token_name_sequence(token, expect, token);
1100 1100 flen = strlen(filename) + 1;
1101 1101
1102 1102 /* Absolute path? */
1103 1103 if (filename[0] == '/') {
1104 1104 if (try_include("", filename, flen, list, includepath))
1105 1105 return 0;
1106 1106 goto out;
1107 1107 }
1108 1108
1109 1109 switch (how) {
1110 1110 case 1:
1111 1111 path = stream->next_path;
1112 1112 break;
1113 1113 case 2:
1114 1114 includepath[0] = "";
1115 1115 path = includepath;
1116 1116 break;
1117 1117 default:
1118 1118 /* Dir of input file is first dir to search for quoted includes */
1119 1119 set_stream_include_path(stream);
1120 1120 path = expect ? angle_includepath : quote_includepath;
1121 1121 break;
1122 1122 }
1123 1123 /* Check the standard include paths.. */
1124 1124 if (do_include_path(path, list, token, filename, flen))
1125 1125 return 0;
1126 1126 out:
1127 1127 sparse_error(token->pos, "unable to open '%s'", filename);
1128 1128 use_best_guess_header_file(token, filename, list);
1129 1129 return 0;
1130 1130 }
1131 1131
1132 1132 static int handle_include(struct stream *stream, struct token **list, struct token *token)
1133 1133 {
1134 1134 return handle_include_path(stream, list, token, 0);
1135 1135 }
1136 1136
1137 1137 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
1138 1138 {
1139 1139 return handle_include_path(stream, list, token, 1);
1140 1140 }
1141 1141
1142 1142 static int handle_argv_include(struct stream *stream, struct token **list, struct token *token)
1143 1143 {
1144 1144 return handle_include_path(stream, list, token, 2);
1145 1145 }
1146 1146
1147 1147 static int token_different(struct token *t1, struct token *t2)
1148 1148 {
1149 1149 int different;
1150 1150
1151 1151 if (token_type(t1) != token_type(t2))
1152 1152 return 1;
1153 1153
1154 1154 switch (token_type(t1)) {
1155 1155 case TOKEN_IDENT:
1156 1156 different = t1->ident != t2->ident;
1157 1157 break;
1158 1158 case TOKEN_ARG_COUNT:
1159 1159 case TOKEN_UNTAINT:
1160 1160 case TOKEN_CONCAT:
1161 1161 case TOKEN_GNU_KLUDGE:
1162 1162 different = 0;
1163 1163 break;
1164 1164 case TOKEN_NUMBER:
1165 1165 different = strcmp(t1->number, t2->number);
1166 1166 break;
1167 1167 case TOKEN_SPECIAL:
1168 1168 different = t1->special != t2->special;
1169 1169 break;
1170 1170 case TOKEN_MACRO_ARGUMENT:
1171 1171 case TOKEN_QUOTED_ARGUMENT:
1172 1172 case TOKEN_STR_ARGUMENT:
1173 1173 different = t1->argnum != t2->argnum;
1174 1174 break;
1175 1175 case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
1176 1176 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
1177 1177 different = memcmp(t1->embedded, t2->embedded, 4);
1178 1178 break;
1179 1179 case TOKEN_CHAR:
1180 1180 case TOKEN_WIDE_CHAR:
1181 1181 case TOKEN_STRING:
1182 1182 case TOKEN_WIDE_STRING: {
1183 1183 struct string *s1, *s2;
1184 1184
1185 1185 s1 = t1->string;
1186 1186 s2 = t2->string;
1187 1187 different = 1;
1188 1188 if (s1->length != s2->length)
1189 1189 break;
1190 1190 different = memcmp(s1->data, s2->data, s1->length);
1191 1191 break;
1192 1192 }
1193 1193 default:
1194 1194 different = 1;
1195 1195 break;
1196 1196 }
1197 1197 return different;
1198 1198 }
1199 1199
1200 1200 static int token_list_different(struct token *list1, struct token *list2)
1201 1201 {
1202 1202 for (;;) {
1203 1203 if (list1 == list2)
1204 1204 return 0;
1205 1205 if (!list1 || !list2)
1206 1206 return 1;
1207 1207 if (token_different(list1, list2))
1208 1208 return 1;
1209 1209 list1 = list1->next;
1210 1210 list2 = list2->next;
1211 1211 }
1212 1212 }
1213 1213
1214 1214 static inline void set_arg_count(struct token *token)
1215 1215 {
1216 1216 token_type(token) = TOKEN_ARG_COUNT;
1217 1217 token->count.normal = token->count.quoted =
1218 1218 token->count.str = token->count.vararg = 0;
1219 1219 }
1220 1220
1221 1221 static struct token *parse_arguments(struct token *list)
1222 1222 {
1223 1223 struct token *arg = list->next, *next = list;
1224 1224 struct argcount *count = &list->count;
1225 1225
1226 1226 set_arg_count(list);
1227 1227
1228 1228 if (match_op(arg, ')')) {
1229 1229 next = arg->next;
1230 1230 list->next = &eof_token_entry;
1231 1231 return next;
1232 1232 }
1233 1233
1234 1234 while (token_type(arg) == TOKEN_IDENT) {
1235 1235 if (arg->ident == &__VA_ARGS___ident)
1236 1236 goto Eva_args;
1237 1237 if (!++count->normal)
1238 1238 goto Eargs;
1239 1239 next = arg->next;
1240 1240
1241 1241 if (match_op(next, ',')) {
1242 1242 set_arg_count(next);
1243 1243 arg = next->next;
1244 1244 continue;
1245 1245 }
1246 1246
1247 1247 if (match_op(next, ')')) {
1248 1248 set_arg_count(next);
1249 1249 next = next->next;
1250 1250 arg->next->next = &eof_token_entry;
1251 1251 return next;
1252 1252 }
1253 1253
1254 1254 /* normal cases are finished here */
1255 1255
1256 1256 if (match_op(next, SPECIAL_ELLIPSIS)) {
1257 1257 if (match_op(next->next, ')')) {
1258 1258 set_arg_count(next);
1259 1259 next->count.vararg = 1;
1260 1260 next = next->next;
1261 1261 arg->next->next = &eof_token_entry;
1262 1262 return next->next;
1263 1263 }
1264 1264
1265 1265 arg = next;
1266 1266 goto Enotclosed;
1267 1267 }
1268 1268
1269 1269 if (eof_token(next)) {
1270 1270 goto Enotclosed;
1271 1271 } else {
1272 1272 arg = next;
1273 1273 goto Ebadstuff;
1274 1274 }
1275 1275 }
1276 1276
1277 1277 if (match_op(arg, SPECIAL_ELLIPSIS)) {
1278 1278 next = arg->next;
1279 1279 token_type(arg) = TOKEN_IDENT;
1280 1280 arg->ident = &__VA_ARGS___ident;
1281 1281 if (!match_op(next, ')'))
1282 1282 goto Enotclosed;
1283 1283 if (!++count->normal)
1284 1284 goto Eargs;
1285 1285 set_arg_count(next);
1286 1286 next->count.vararg = 1;
1287 1287 next = next->next;
1288 1288 arg->next->next = &eof_token_entry;
1289 1289 return next;
1290 1290 }
1291 1291
1292 1292 if (eof_token(arg)) {
1293 1293 arg = next;
1294 1294 goto Enotclosed;
1295 1295 }
1296 1296 if (match_op(arg, ','))
1297 1297 goto Emissing;
1298 1298 else
1299 1299 goto Ebadstuff;
1300 1300
1301 1301
1302 1302 Emissing:
1303 1303 sparse_error(arg->pos, "parameter name missing");
1304 1304 return NULL;
1305 1305 Ebadstuff:
1306 1306 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
1307 1307 show_token(arg));
1308 1308 return NULL;
1309 1309 Enotclosed:
1310 1310 sparse_error(arg->pos, "missing ')' in macro parameter list");
1311 1311 return NULL;
1312 1312 Eva_args:
1313 1313 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1314 1314 return NULL;
1315 1315 Eargs:
1316 1316 sparse_error(arg->pos, "too many arguments in macro definition");
1317 1317 return NULL;
1318 1318 }
1319 1319
1320 1320 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1321 1321 {
1322 1322 struct ident *ident = token->ident;
1323 1323 int nr;
1324 1324
1325 1325 if (!arglist || token_type(token) != TOKEN_IDENT)
1326 1326 return 0;
1327 1327
1328 1328 arglist = arglist->next;
1329 1329
1330 1330 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1331 1331 if (arglist->ident == ident) {
1332 1332 struct argcount *count = &arglist->next->count;
1333 1333 int n;
1334 1334
1335 1335 token->argnum = nr;
1336 1336 token_type(token) = type;
1337 1337 switch (type) {
1338 1338 case TOKEN_MACRO_ARGUMENT:
1339 1339 n = ++count->normal;
1340 1340 break;
1341 1341 case TOKEN_QUOTED_ARGUMENT:
1342 1342 n = ++count->quoted;
1343 1343 break;
1344 1344 default:
1345 1345 n = ++count->str;
1346 1346 }
1347 1347 if (n)
1348 1348 return count->vararg ? 2 : 1;
1349 1349 /*
1350 1350 * XXX - need saner handling of that
1351 1351 * (>= 1024 instances of argument)
1352 1352 */
1353 1353 token_type(token) = TOKEN_ERROR;
1354 1354 return -1;
1355 1355 }
1356 1356 }
1357 1357 return 0;
1358 1358 }
1359 1359
1360 1360 static struct token *handle_hash(struct token **p, struct token *arglist)
1361 1361 {
1362 1362 struct token *token = *p;
1363 1363 if (arglist) {
1364 1364 struct token *next = token->next;
1365 1365 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1366 1366 goto Equote;
1367 1367 next->pos.whitespace = token->pos.whitespace;
1368 1368 __free_token(token);
1369 1369 token = *p = next;
1370 1370 } else {
1371 1371 token->pos.noexpand = 1;
1372 1372 }
1373 1373 return token;
1374 1374
1375 1375 Equote:
1376 1376 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1377 1377 return NULL;
1378 1378 }
1379 1379
1380 1380 /* token->next is ## */
1381 1381 static struct token *handle_hashhash(struct token *token, struct token *arglist)
1382 1382 {
1383 1383 struct token *last = token;
1384 1384 struct token *concat;
1385 1385 int state = match_op(token, ',');
1386 1386
1387 1387 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1388 1388
1389 1389 while (1) {
1390 1390 struct token *t;
1391 1391 int is_arg;
1392 1392
1393 1393 /* eat duplicate ## */
1394 1394 concat = token->next;
1395 1395 while (match_op(t = concat->next, SPECIAL_HASHHASH)) {
1396 1396 token->next = t;
1397 1397 __free_token(concat);
1398 1398 concat = t;
1399 1399 }
1400 1400 token_type(concat) = TOKEN_CONCAT;
1401 1401
1402 1402 if (eof_token(t))
1403 1403 goto Econcat;
1404 1404
1405 1405 if (match_op(t, '#')) {
1406 1406 t = handle_hash(&concat->next, arglist);
1407 1407 if (!t)
1408 1408 return NULL;
1409 1409 }
1410 1410
1411 1411 is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
1412 1412
1413 1413 if (state == 1 && is_arg) {
1414 1414 state = is_arg;
1415 1415 } else {
1416 1416 last = t;
1417 1417 state = match_op(t, ',');
1418 1418 }
1419 1419
1420 1420 token = t;
1421 1421 if (!match_op(token->next, SPECIAL_HASHHASH))
1422 1422 break;
1423 1423 }
1424 1424 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1425 1425 if (state == 2)
1426 1426 token_type(last) = TOKEN_GNU_KLUDGE;
1427 1427 return token;
1428 1428
1429 1429 Econcat:
1430 1430 sparse_error(concat->pos, "'##' cannot appear at the ends of macro expansion");
1431 1431 return NULL;
1432 1432 }
1433 1433
1434 1434 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1435 1435 {
1436 1436 struct token *token = expansion;
1437 1437 struct token **p;
1438 1438
1439 1439 if (match_op(token, SPECIAL_HASHHASH))
1440 1440 goto Econcat;
1441 1441
1442 1442 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1443 1443 if (match_op(token, '#')) {
1444 1444 token = handle_hash(p, arglist);
1445 1445 if (!token)
1446 1446 return NULL;
1447 1447 }
1448 1448 if (match_op(token->next, SPECIAL_HASHHASH)) {
1449 1449 token = handle_hashhash(token, arglist);
1450 1450 if (!token)
1451 1451 return NULL;
1452 1452 } else {
1453 1453 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1454 1454 }
1455 1455 switch (token_type(token)) {
1456 1456 case TOKEN_ERROR:
1457 1457 goto Earg;
1458 1458
1459 1459 case TOKEN_STRING:
1460 1460 case TOKEN_WIDE_STRING:
1461 1461 token->string->immutable = 1;
1462 1462 break;
1463 1463 }
1464 1464 }
1465 1465 token = alloc_token(&expansion->pos);
1466 1466 token_type(token) = TOKEN_UNTAINT;
1467 1467 token->ident = name;
1468 1468 token->next = *p;
1469 1469 *p = token;
1470 1470 return expansion;
1471 1471
1472 1472 Econcat:
1473 1473 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1474 1474 return NULL;
1475 1475 Earg:
1476 1476 sparse_error(token->pos, "too many instances of argument in body");
1477 1477 return NULL;
1478 1478 }
1479 1479
1480 1480 static int do_define(struct position pos, struct token *token, struct ident *name,
1481 1481 struct token *arglist, struct token *expansion, int attr)
1482 1482 {
1483 1483 struct symbol *sym;
1484 1484 int ret = 1;
1485 1485
1486 1486 expansion = parse_expansion(expansion, arglist, name);
1487 1487 if (!expansion)
1488 1488 return 1;
1489 1489
1490 1490 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1491 1491 if (sym) {
1492 1492 int clean;
1493 1493
1494 1494 if (attr < sym->attr)
1495 1495 goto out;
1496 1496
1497 1497 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1498 1498
1499 1499 if (token_list_different(sym->expansion, expansion) ||
1500 1500 token_list_different(sym->arglist, arglist)) {
1501 1501 ret = 0;
1502 1502 if ((clean && attr == SYM_ATTR_NORMAL)
1503 1503 || sym->used_in == file_scope) {
1504 1504 warning(pos, "preprocessor token %.*s redefined",
1505 1505 name->len, name->name);
1506 1506 info(sym->pos, "this was the original definition");
1507 1507 }
1508 1508 } else if (clean)
1509 1509 goto out;
1510 1510 }
1511 1511
1512 1512 if (!sym || sym->scope != file_scope) {
1513 1513 sym = alloc_symbol(pos, SYM_NODE);
1514 1514 bind_symbol(sym, name, NS_MACRO);
1515 1515 add_ident(¯os, name);
1516 1516 ret = 0;
1517 1517 }
1518 1518
1519 1519 if (!ret) {
1520 1520 sym->expansion = expansion;
1521 1521 sym->arglist = arglist;
1522 1522 if (token) /* Free the "define" token, but not the rest of the line */
1523 1523 __free_token(token);
1524 1524 }
1525 1525
1526 1526 sym->namespace = NS_MACRO;
1527 1527 sym->used_in = NULL;
1528 1528 sym->attr = attr;
1529 1529 out:
1530 1530 return ret;
1531 1531 }
1532 1532
1533 1533 ///
1534 1534 // predefine a macro with a printf-formatted value
1535 1535 // @name: the name of the macro
1536 1536 // @weak: 0/1 for a normal or a weak define
1537 1537 // @fmt: the printf format followed by it's arguments.
1538 1538 //
1539 1539 // The type of the value is automatically infered:
1540 1540 // TOKEN_NUMBER if it starts by a digit, TOKEN_IDENT otherwise.
1541 1541 // If @fmt is null or empty, the macro is defined with an empty definition.
1542 1542 void predefine(const char *name, int weak, const char *fmt, ...)
1543 1543 {
1544 1544 struct ident *ident = built_in_ident(name);
1545 1545 struct token *value = &eof_token_entry;
1546 1546 int attr = weak ? SYM_ATTR_WEAK : SYM_ATTR_NORMAL;
1547 1547
1548 1548 if (fmt && fmt[0]) {
1549 1549 static char buf[256];
1550 1550 va_list ap;
1551 1551
1552 1552 va_start(ap, fmt);
1553 1553 vsnprintf(buf, sizeof(buf), fmt, ap);
1554 1554 va_end(ap);
1555 1555
1556 1556 value = __alloc_token(0);
1557 1557 if (isdigit(buf[0])) {
1558 1558 token_type(value) = TOKEN_NUMBER;
1559 1559 value->number = xstrdup(buf);
1560 1560 } else {
↓ open down ↓ |
1560 lines elided |
↑ open up ↑ |
1561 1561 token_type(value) = TOKEN_IDENT;
1562 1562 value->ident = built_in_ident(buf);
1563 1563 }
1564 1564 value->pos.whitespace = 1;
1565 1565 value->next = &eof_token_entry;
1566 1566 }
1567 1567
1568 1568 do_define(value->pos, NULL, ident, NULL, value, attr);
1569 1569 }
1570 1570
1571 +///
1572 +// like predefine() but only if one of the non-standard dialect is chosen
1573 +void predefine_nostd(const char *name)
1574 +{
1575 + if ((standard & STANDARD_GNU) || (standard == STANDARD_NONE))
1576 + predefine(name, 1, "1");
1577 +}
1578 +
1571 1579 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1572 1580 {
1573 1581 struct token *arglist, *expansion;
1574 1582 struct token *left = token->next;
1575 1583 struct ident *name;
1576 1584
1577 1585 if (token_type(left) != TOKEN_IDENT) {
1578 1586 sparse_error(token->pos, "expected identifier to 'define'");
1579 1587 return 1;
1580 1588 }
1581 1589
1582 1590 name = left->ident;
1583 1591
1584 1592 arglist = NULL;
1585 1593 expansion = left->next;
1586 1594 if (!expansion->pos.whitespace) {
1587 1595 if (match_op(expansion, '(')) {
1588 1596 arglist = expansion;
1589 1597 expansion = parse_arguments(expansion);
1590 1598 if (!expansion)
1591 1599 return 1;
1592 1600 } else if (!eof_token(expansion)) {
1593 1601 warning(expansion->pos,
1594 1602 "no whitespace before object-like macro body");
1595 1603 }
1596 1604 }
1597 1605
1598 1606 return do_define(left->pos, token, name, arglist, expansion, attr);
1599 1607 }
1600 1608
1601 1609 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1602 1610 {
1603 1611 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1604 1612 }
1605 1613
1606 1614 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1607 1615 {
1608 1616 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1609 1617 }
1610 1618
1611 1619 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1612 1620 {
1613 1621 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1614 1622 }
1615 1623
1616 1624 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1617 1625 {
1618 1626 struct token *left = token->next;
1619 1627 struct symbol *sym;
1620 1628
1621 1629 if (token_type(left) != TOKEN_IDENT) {
1622 1630 sparse_error(token->pos, "expected identifier to 'undef'");
1623 1631 return 1;
1624 1632 }
1625 1633
1626 1634 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1627 1635 if (sym) {
1628 1636 if (attr < sym->attr)
1629 1637 return 1;
1630 1638 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1631 1639 return 1;
1632 1640 } else if (attr <= SYM_ATTR_NORMAL)
1633 1641 return 1;
1634 1642
1635 1643 if (!sym || sym->scope != file_scope) {
1636 1644 sym = alloc_symbol(left->pos, SYM_NODE);
1637 1645 bind_symbol(sym, left->ident, NS_MACRO);
1638 1646 }
1639 1647
1640 1648 sym->namespace = NS_UNDEF;
1641 1649 sym->used_in = NULL;
1642 1650 sym->attr = attr;
1643 1651
1644 1652 return 1;
1645 1653 }
1646 1654
1647 1655 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1648 1656 {
1649 1657 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1650 1658 }
1651 1659
1652 1660 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1653 1661 {
1654 1662 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1655 1663 }
1656 1664
1657 1665 static int preprocessor_if(struct stream *stream, struct token *token, int cond)
1658 1666 {
1659 1667 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1660 1668 free_preprocessor_line(token->next);
1661 1669 token->next = stream->top_if;
1662 1670 stream->top_if = token;
1663 1671 if (false_nesting || cond != 1)
1664 1672 false_nesting++;
1665 1673 return 0;
1666 1674 }
1667 1675
1668 1676 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1669 1677 {
1670 1678 struct token *next = token->next;
1671 1679 int arg;
1672 1680 if (token_type(next) == TOKEN_IDENT) {
1673 1681 arg = token_defined(next);
1674 1682 } else {
1675 1683 dirty_stream(stream);
1676 1684 if (!false_nesting)
1677 1685 sparse_error(token->pos, "expected preprocessor identifier");
1678 1686 arg = -1;
1679 1687 }
1680 1688 return preprocessor_if(stream, token, arg);
1681 1689 }
1682 1690
1683 1691 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1684 1692 {
1685 1693 struct token *next = token->next;
1686 1694 int arg;
1687 1695 if (token_type(next) == TOKEN_IDENT) {
1688 1696 if (!stream->dirty && !stream->ifndef) {
1689 1697 if (!stream->protect) {
1690 1698 stream->ifndef = token;
1691 1699 stream->protect = next->ident;
1692 1700 } else if (stream->protect == next->ident) {
1693 1701 stream->ifndef = token;
1694 1702 stream->dirty = 1;
1695 1703 }
1696 1704 }
1697 1705 arg = !token_defined(next);
1698 1706 } else {
1699 1707 dirty_stream(stream);
1700 1708 if (!false_nesting)
1701 1709 sparse_error(token->pos, "expected preprocessor identifier");
1702 1710 arg = -1;
1703 1711 }
1704 1712
1705 1713 return preprocessor_if(stream, token, arg);
1706 1714 }
1707 1715
1708 1716 static const char *show_token_sequence(struct token *token, int quote);
1709 1717
1710 1718 /*
1711 1719 * Expression handling for #if and #elif; it differs from normal expansion
1712 1720 * due to special treatment of "defined".
1713 1721 */
1714 1722 static int expression_value(struct token **where)
1715 1723 {
1716 1724 struct expression *expr;
1717 1725 struct token *p;
1718 1726 struct token **list = where, **beginning = NULL;
1719 1727 long long value;
1720 1728 int state = 0;
1721 1729
1722 1730 while (!eof_token(p = scan_next(list))) {
1723 1731 switch (state) {
1724 1732 case 0:
1725 1733 if (token_type(p) != TOKEN_IDENT)
1726 1734 break;
1727 1735 if (p->ident == &defined_ident) {
1728 1736 state = 1;
1729 1737 beginning = list;
1730 1738 break;
1731 1739 } else if (p->ident == &__has_builtin_ident) {
1732 1740 state = 4;
1733 1741 beginning = list;
1734 1742 break;
1735 1743 } else if (p->ident == &__has_attribute_ident) {
1736 1744 state = 6;
1737 1745 beginning = list;
1738 1746 break;
1739 1747 }
1740 1748 if (!expand_one_symbol(list))
1741 1749 continue;
1742 1750 if (token_type(p) != TOKEN_IDENT)
1743 1751 break;
1744 1752 token_type(p) = TOKEN_ZERO_IDENT;
1745 1753 break;
1746 1754 case 1:
1747 1755 if (match_op(p, '(')) {
1748 1756 state = 2;
1749 1757 } else {
1750 1758 state = 0;
1751 1759 replace_with_defined(p);
1752 1760 *beginning = p;
1753 1761 }
1754 1762 break;
1755 1763 case 2:
1756 1764 if (token_type(p) == TOKEN_IDENT)
1757 1765 state = 3;
1758 1766 else
1759 1767 state = 0;
1760 1768 replace_with_defined(p);
1761 1769 *beginning = p;
1762 1770 break;
1763 1771 case 3:
1764 1772 state = 0;
1765 1773 if (!match_op(p, ')'))
1766 1774 sparse_error(p->pos, "missing ')' after \"defined\"");
1767 1775 *list = p->next;
1768 1776 continue;
1769 1777
1770 1778 // __has_builtin(x) or __has_attribute(x)
1771 1779 case 4: case 6:
1772 1780 if (match_op(p, '(')) {
1773 1781 state++;
1774 1782 } else {
1775 1783 sparse_error(p->pos, "missing '(' after \"__has_%s\"",
1776 1784 state == 4 ? "builtin" : "attribute");
1777 1785 state = 0;
1778 1786 }
1779 1787 *beginning = p;
1780 1788 break;
1781 1789 case 5: case 7:
1782 1790 if (token_type(p) != TOKEN_IDENT) {
1783 1791 sparse_error(p->pos, "identifier expected");
1784 1792 state = 0;
1785 1793 break;
1786 1794 }
1787 1795 if (!match_op(p->next, ')'))
1788 1796 sparse_error(p->pos, "missing ')' after \"__has_%s\"",
1789 1797 state == 5 ? "builtin" : "attribute");
1790 1798 if (state == 5)
1791 1799 replace_with_has_builtin(p);
1792 1800 else
1793 1801 replace_with_has_attribute(p);
1794 1802 state = 8;
1795 1803 *beginning = p;
1796 1804 break;
1797 1805 case 8:
1798 1806 state = 0;
1799 1807 *list = p->next;
1800 1808 continue;
1801 1809 }
1802 1810 list = &p->next;
1803 1811 }
1804 1812
1805 1813 p = constant_expression(*where, &expr);
1806 1814 if (!eof_token(p))
1807 1815 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p, 0));
1808 1816 value = get_expression_value(expr);
1809 1817 return value != 0;
1810 1818 }
1811 1819
1812 1820 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1813 1821 {
1814 1822 int value = 0;
1815 1823 if (!false_nesting)
1816 1824 value = expression_value(&token->next);
1817 1825
1818 1826 dirty_stream(stream);
1819 1827 return preprocessor_if(stream, token, value);
1820 1828 }
1821 1829
1822 1830 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1823 1831 {
1824 1832 struct token *top_if = stream->top_if;
1825 1833 end_group(stream);
1826 1834
1827 1835 if (!top_if) {
1828 1836 nesting_error(stream);
1829 1837 sparse_error(token->pos, "unmatched #elif within stream");
1830 1838 return 1;
1831 1839 }
1832 1840
1833 1841 if (token_type(top_if) == TOKEN_ELSE) {
1834 1842 nesting_error(stream);
1835 1843 sparse_error(token->pos, "#elif after #else");
1836 1844 if (!false_nesting)
1837 1845 false_nesting = 1;
1838 1846 return 1;
1839 1847 }
1840 1848
1841 1849 dirty_stream(stream);
1842 1850 if (token_type(top_if) != TOKEN_IF)
1843 1851 return 1;
1844 1852 if (false_nesting) {
1845 1853 false_nesting = 0;
1846 1854 if (!expression_value(&token->next))
1847 1855 false_nesting = 1;
1848 1856 } else {
1849 1857 false_nesting = 1;
1850 1858 token_type(top_if) = TOKEN_SKIP_GROUPS;
1851 1859 }
1852 1860 return 1;
1853 1861 }
1854 1862
1855 1863 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1856 1864 {
1857 1865 struct token *top_if = stream->top_if;
1858 1866 end_group(stream);
1859 1867
1860 1868 if (!top_if) {
1861 1869 nesting_error(stream);
1862 1870 sparse_error(token->pos, "unmatched #else within stream");
1863 1871 return 1;
1864 1872 }
1865 1873
1866 1874 if (token_type(top_if) == TOKEN_ELSE) {
1867 1875 nesting_error(stream);
1868 1876 sparse_error(token->pos, "#else after #else");
1869 1877 }
1870 1878 if (false_nesting) {
1871 1879 if (token_type(top_if) == TOKEN_IF)
1872 1880 false_nesting = 0;
1873 1881 } else {
1874 1882 false_nesting = 1;
1875 1883 }
1876 1884 token_type(top_if) = TOKEN_ELSE;
1877 1885 return 1;
1878 1886 }
1879 1887
1880 1888 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1881 1889 {
1882 1890 struct token *top_if = stream->top_if;
1883 1891 end_group(stream);
1884 1892 if (!top_if) {
1885 1893 nesting_error(stream);
1886 1894 sparse_error(token->pos, "unmatched #endif in stream");
1887 1895 return 1;
1888 1896 }
1889 1897 if (false_nesting)
1890 1898 false_nesting--;
1891 1899 stream->top_if = top_if->next;
1892 1900 __free_token(top_if);
1893 1901 return 1;
1894 1902 }
1895 1903
1896 1904 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1897 1905 {
1898 1906 warning(token->pos, "%s", show_token_sequence(token->next, 0));
1899 1907 return 1;
1900 1908 }
1901 1909
1902 1910 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1903 1911 {
1904 1912 sparse_error(token->pos, "%s", show_token_sequence(token->next, 0));
1905 1913 return 1;
1906 1914 }
1907 1915
1908 1916 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1909 1917 {
1910 1918 /*
1911 1919 * Do we have any non-system includes?
1912 1920 * Clear them out if so..
1913 1921 */
1914 1922 *sys_includepath = NULL;
1915 1923 return 1;
1916 1924 }
1917 1925
1918 1926 static inline void update_inc_ptrs(const char ***where)
1919 1927 {
1920 1928
1921 1929 if (*where <= dirafter_includepath) {
1922 1930 dirafter_includepath++;
1923 1931 /* If this was the entry that we prepend, don't
1924 1932 * rise the lower entries, even if they are at
1925 1933 * the same level. */
1926 1934 if (where == &dirafter_includepath)
1927 1935 return;
1928 1936 }
1929 1937 if (*where <= sys_includepath) {
1930 1938 sys_includepath++;
1931 1939 if (where == &sys_includepath)
1932 1940 return;
1933 1941 }
1934 1942 if (*where <= isys_includepath) {
1935 1943 isys_includepath++;
1936 1944 if (where == &isys_includepath)
1937 1945 return;
1938 1946 }
1939 1947
1940 1948 /* angle_includepath is actually never updated, since we
1941 1949 * don't suppport -iquote rught now. May change some day. */
1942 1950 if (*where <= angle_includepath) {
1943 1951 angle_includepath++;
1944 1952 if (where == &angle_includepath)
1945 1953 return;
1946 1954 }
1947 1955 }
1948 1956
1949 1957 /* Add a path before 'where' and update the pointers associated with the
1950 1958 * includepath array */
1951 1959 static void add_path_entry(struct token *token, const char *path,
1952 1960 const char ***where)
1953 1961 {
1954 1962 const char **dst;
1955 1963 const char *next;
1956 1964
1957 1965 /* Need one free entry.. */
1958 1966 if (includepath[INCLUDEPATHS-2])
1959 1967 error_die(token->pos, "too many include path entries");
1960 1968
1961 1969 /* check that this is not a duplicate */
1962 1970 dst = includepath;
1963 1971 while (*dst) {
1964 1972 if (strcmp(*dst, path) == 0)
1965 1973 return;
1966 1974 dst++;
1967 1975 }
1968 1976 next = path;
1969 1977 dst = *where;
1970 1978
1971 1979 update_inc_ptrs(where);
1972 1980
1973 1981 /*
1974 1982 * Move them all up starting at dst,
1975 1983 * insert the new entry..
1976 1984 */
1977 1985 do {
1978 1986 const char *tmp = *dst;
1979 1987 *dst = next;
1980 1988 next = tmp;
1981 1989 dst++;
1982 1990 } while (next);
1983 1991 }
1984 1992
1985 1993 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1986 1994 {
1987 1995 for (;;) {
1988 1996 token = token->next;
1989 1997 if (eof_token(token))
1990 1998 return 1;
1991 1999 if (token_type(token) != TOKEN_STRING) {
1992 2000 warning(token->pos, "expected path string");
1993 2001 return 1;
1994 2002 }
1995 2003 add_path_entry(token, token->string->data, &isys_includepath);
1996 2004 }
1997 2005 }
1998 2006
1999 2007 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
2000 2008 {
2001 2009 for (;;) {
2002 2010 token = token->next;
2003 2011 if (eof_token(token))
2004 2012 return 1;
2005 2013 if (token_type(token) != TOKEN_STRING) {
2006 2014 sparse_error(token->pos, "expected path string");
2007 2015 return 1;
2008 2016 }
2009 2017 add_path_entry(token, token->string->data, &sys_includepath);
2010 2018 }
2011 2019 }
2012 2020
2013 2021 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
2014 2022 {
2015 2023 for (;;) {
2016 2024 token = token->next;
2017 2025 if (eof_token(token))
2018 2026 return 1;
2019 2027 if (token_type(token) != TOKEN_STRING) {
2020 2028 sparse_error(token->pos, "expected path string");
2021 2029 return 1;
2022 2030 }
2023 2031 add_path_entry(token, token->string->data, &dirafter_includepath);
2024 2032 }
2025 2033 }
2026 2034
2027 2035 /* Add to end on includepath list - no pointer updates */
2028 2036 static void add_dirafter_entry(struct token *token, const char *path)
2029 2037 {
2030 2038 const char **dst = includepath;
2031 2039
2032 2040 /* Need one free entry.. */
2033 2041 if (includepath[INCLUDEPATHS-2])
2034 2042 error_die(token->pos, "too many include path entries");
2035 2043
2036 2044 /* Add to the end */
2037 2045 while (*dst)
2038 2046 dst++;
2039 2047 *dst = path;
2040 2048 dst++;
2041 2049 *dst = NULL;
2042 2050 }
2043 2051
2044 2052 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
2045 2053 {
2046 2054 for (;;) {
2047 2055 token = token->next;
2048 2056 if (eof_token(token))
2049 2057 return 1;
2050 2058 if (token_type(token) != TOKEN_STRING) {
2051 2059 sparse_error(token->pos, "expected path string");
2052 2060 return 1;
2053 2061 }
2054 2062 add_dirafter_entry(token, token->string->data);
2055 2063 }
2056 2064 }
2057 2065
2058 2066 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
2059 2067 {
2060 2068 /*
2061 2069 * -I-
2062 2070 * From info gcc:
2063 2071 * Split the include path. Any directories specified with `-I'
2064 2072 * options before `-I-' are searched only for headers requested with
2065 2073 * `#include "FILE"'; they are not searched for `#include <FILE>'.
2066 2074 * If additional directories are specified with `-I' options after
2067 2075 * the `-I-', those directories are searched for all `#include'
2068 2076 * directives.
2069 2077 * In addition, `-I-' inhibits the use of the directory of the current
2070 2078 * file directory as the first search directory for `#include "FILE"'.
2071 2079 */
2072 2080 quote_includepath = includepath+1;
2073 2081 angle_includepath = sys_includepath;
2074 2082 return 1;
2075 2083 }
2076 2084
2077 2085 /*
2078 2086 * We replace "#pragma xxx" with "__pragma__" in the token
2079 2087 * stream. Just as an example.
2080 2088 *
2081 2089 * We'll just #define that away for now, but the theory here
2082 2090 * is that we can use this to insert arbitrary token sequences
2083 2091 * to turn the pragmas into internal front-end sequences for
2084 2092 * when we actually start caring about them.
2085 2093 *
2086 2094 * So eventually this will turn into some kind of extended
2087 2095 * __attribute__() like thing, except called __pragma__(xxx).
2088 2096 */
2089 2097 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
2090 2098 {
2091 2099 struct token *next = *line;
2092 2100
2093 2101 if (match_ident(token->next, &once_ident) && eof_token(token->next->next)) {
2094 2102 stream->once = 1;
2095 2103 return 1;
2096 2104 }
2097 2105 token->ident = &pragma_ident;
2098 2106 token->pos.newline = 1;
2099 2107 token->pos.whitespace = 1;
2100 2108 token->pos.pos = 1;
2101 2109 *line = token;
2102 2110 token->next = next;
2103 2111 return 0;
2104 2112 }
2105 2113
2106 2114 /*
2107 2115 * We ignore #line for now.
2108 2116 */
2109 2117 static int handle_line(struct stream *stream, struct token **line, struct token *token)
2110 2118 {
2111 2119 return 1;
2112 2120 }
2113 2121
2114 2122 static int handle_ident(struct stream *stream, struct token **line, struct token *token)
2115 2123 {
2116 2124 return 1;
2117 2125 }
2118 2126
2119 2127 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
2120 2128 {
2121 2129 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token, 0));
2122 2130 return 1;
2123 2131 }
2124 2132
2125 2133
2126 2134 static void init_preprocessor(void)
2127 2135 {
2128 2136 int i;
2129 2137 int stream = init_stream("preprocessor", -1, includepath);
2130 2138 static struct {
2131 2139 const char *name;
2132 2140 int (*handler)(struct stream *, struct token **, struct token *);
2133 2141 } normal[] = {
2134 2142 { "define", handle_define },
2135 2143 { "weak_define", handle_weak_define },
2136 2144 { "strong_define", handle_strong_define },
2137 2145 { "undef", handle_undef },
2138 2146 { "strong_undef", handle_strong_undef },
2139 2147 { "warning", handle_warning },
2140 2148 { "error", handle_error },
2141 2149 { "include", handle_include },
2142 2150 { "include_next", handle_include_next },
2143 2151 { "pragma", handle_pragma },
2144 2152 { "line", handle_line },
2145 2153 { "ident", handle_ident },
2146 2154
2147 2155 // our internal preprocessor tokens
2148 2156 { "nostdinc", handle_nostdinc },
2149 2157 { "add_include", handle_add_include },
2150 2158 { "add_isystem", handle_add_isystem },
2151 2159 { "add_system", handle_add_system },
2152 2160 { "add_dirafter", handle_add_dirafter },
2153 2161 { "split_include", handle_split_include },
2154 2162 { "argv_include", handle_argv_include },
2155 2163 }, special[] = {
2156 2164 { "ifdef", handle_ifdef },
2157 2165 { "ifndef", handle_ifndef },
2158 2166 { "else", handle_else },
2159 2167 { "endif", handle_endif },
2160 2168 { "if", handle_if },
2161 2169 { "elif", handle_elif },
2162 2170 };
2163 2171 static struct {
2164 2172 const char *name;
2165 2173 void (*expander)(struct token *);
2166 2174 } dynamic[] = {
2167 2175 { "__LINE__", expand_line },
2168 2176 { "__FILE__", expand_file },
2169 2177 { "__BASE_FILE__", expand_basefile },
2170 2178 { "__DATE__", expand_date },
2171 2179 { "__TIME__", expand_time },
2172 2180 { "__COUNTER__", expand_counter },
2173 2181 { "__INCLUDE_LEVEL__", expand_include_level },
2174 2182 };
2175 2183
2176 2184 for (i = 0; i < ARRAY_SIZE(normal); i++) {
2177 2185 struct symbol *sym;
2178 2186 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
2179 2187 sym->handler = normal[i].handler;
2180 2188 sym->normal = 1;
2181 2189 }
2182 2190 for (i = 0; i < ARRAY_SIZE(special); i++) {
2183 2191 struct symbol *sym;
2184 2192 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
2185 2193 sym->handler = special[i].handler;
2186 2194 sym->normal = 0;
2187 2195 }
2188 2196 for (i = 0; i < ARRAY_SIZE(dynamic); i++) {
2189 2197 struct symbol *sym;
2190 2198 sym = create_symbol(stream, dynamic[i].name, SYM_NODE, NS_MACRO);
2191 2199 sym->expander = dynamic[i].expander;
2192 2200 }
2193 2201
2194 2202 counter_macro = 0;
2195 2203 }
2196 2204
2197 2205 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
2198 2206 {
2199 2207 int (*handler)(struct stream *, struct token **, struct token *);
2200 2208 struct token *token = start->next;
2201 2209 int is_normal = 1;
2202 2210
2203 2211 if (eof_token(token))
2204 2212 return;
2205 2213
2206 2214 if (token_type(token) == TOKEN_IDENT) {
2207 2215 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
2208 2216 if (sym) {
2209 2217 handler = sym->handler;
2210 2218 is_normal = sym->normal;
2211 2219 } else {
2212 2220 handler = handle_nondirective;
2213 2221 }
2214 2222 } else if (token_type(token) == TOKEN_NUMBER) {
2215 2223 handler = handle_line;
2216 2224 } else {
2217 2225 handler = handle_nondirective;
2218 2226 }
2219 2227
2220 2228 if (is_normal) {
2221 2229 dirty_stream(stream);
2222 2230 if (false_nesting)
2223 2231 goto out;
2224 2232 }
2225 2233 if (!handler(stream, line, token)) /* all set */
2226 2234 return;
2227 2235
2228 2236 out:
2229 2237 free_preprocessor_line(token);
2230 2238 }
2231 2239
2232 2240 static void preprocessor_line(struct stream *stream, struct token **line)
2233 2241 {
2234 2242 struct token *start = *line, *next;
2235 2243 struct token **tp = &start->next;
2236 2244
2237 2245 for (;;) {
2238 2246 next = *tp;
2239 2247 if (next->pos.newline)
2240 2248 break;
2241 2249 tp = &next->next;
2242 2250 }
2243 2251 *line = next;
2244 2252 *tp = &eof_token_entry;
2245 2253 handle_preprocessor_line(stream, line, start);
2246 2254 }
2247 2255
2248 2256 static void do_preprocess(struct token **list)
2249 2257 {
2250 2258 struct token *next;
2251 2259
2252 2260 while (!eof_token(next = scan_next(list))) {
2253 2261 struct stream *stream = input_streams + next->pos.stream;
2254 2262
2255 2263 if (next->pos.newline && match_op(next, '#')) {
2256 2264 if (!next->pos.noexpand) {
2257 2265 preprocessor_line(stream, list);
2258 2266 __free_token(next); /* Free the '#' token */
2259 2267 continue;
2260 2268 }
2261 2269 }
2262 2270
2263 2271 switch (token_type(next)) {
2264 2272 case TOKEN_STREAMEND:
2265 2273 if (stream->top_if) {
2266 2274 nesting_error(stream);
2267 2275 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
2268 2276 stream->top_if = NULL;
2269 2277 false_nesting = 0;
2270 2278 }
2271 2279 if (!stream->dirty)
2272 2280 stream->constant = CONSTANT_FILE_YES;
2273 2281 *list = next->next;
2274 2282 include_level--;
2275 2283 continue;
2276 2284 case TOKEN_STREAMBEGIN:
2277 2285 *list = next->next;
2278 2286 include_level++;
2279 2287 continue;
2280 2288
2281 2289 default:
2282 2290 dirty_stream(stream);
2283 2291 if (false_nesting) {
2284 2292 *list = next->next;
2285 2293 __free_token(next);
2286 2294 continue;
2287 2295 }
2288 2296
2289 2297 if (token_type(next) != TOKEN_IDENT ||
2290 2298 expand_one_symbol(list))
2291 2299 list = &next->next;
2292 2300 }
2293 2301 }
2294 2302 }
2295 2303
2296 2304 void init_include_path(void)
2297 2305 {
2298 2306 FILE *fp;
2299 2307 char path[256];
2300 2308 char arch[32];
2301 2309 char os[32];
2302 2310
2303 2311 fp = popen("/bin/uname -m", "r");
2304 2312 if (!fp)
2305 2313 return;
2306 2314 if (!fgets(arch, sizeof(arch) - 1, fp))
2307 2315 return;
2308 2316 pclose(fp);
2309 2317 if (arch[strlen(arch) - 1] == '\n')
2310 2318 arch[strlen(arch) - 1] = '\0';
2311 2319
2312 2320 fp = popen("/bin/uname -o", "r");
2313 2321 if (!fp)
2314 2322 return;
2315 2323 fgets(os, sizeof(os) - 1, fp);
2316 2324 pclose(fp);
2317 2325
2318 2326 if (strcmp(os, "GNU/Linux\n") != 0)
2319 2327 return;
2320 2328 strcpy(os, "linux-gnu");
2321 2329
2322 2330 snprintf(path, sizeof(path), "/usr/include/%s-%s/", arch, os);
2323 2331 add_pre_buffer("#add_system \"%s/\"\n", path);
2324 2332 }
2325 2333
2326 2334 struct token * preprocess(struct token *token)
2327 2335 {
2328 2336 preprocessing = 1;
2329 2337 init_preprocessor();
2330 2338 do_preprocess(&token);
2331 2339
2332 2340 // Drop all expressions from preprocessing, they're not used any more.
2333 2341 // This is not true when we have multiple files, though ;/
2334 2342 // clear_expression_alloc();
2335 2343 preprocessing = 0;
2336 2344
2337 2345 return token;
2338 2346 }
2339 2347
2340 2348 static int is_VA_ARGS_token(struct token *token)
2341 2349 {
2342 2350 return (token_type(token) == TOKEN_IDENT) &&
2343 2351 (token->ident == &__VA_ARGS___ident);
2344 2352 }
2345 2353
2346 2354 static void dump_macro(struct symbol *sym)
2347 2355 {
2348 2356 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
2349 2357 struct token *args[nargs];
2350 2358 struct token *token;
2351 2359
2352 2360 printf("#define %s", show_ident(sym->ident));
2353 2361 token = sym->arglist;
2354 2362 if (token) {
2355 2363 const char *sep = "";
2356 2364 int narg = 0;
2357 2365 putchar('(');
2358 2366 for (; !eof_token(token); token = token->next) {
2359 2367 if (token_type(token) == TOKEN_ARG_COUNT)
2360 2368 continue;
2361 2369 if (is_VA_ARGS_token(token))
2362 2370 printf("%s...", sep);
2363 2371 else
2364 2372 printf("%s%s", sep, show_token(token));
2365 2373 args[narg++] = token;
2366 2374 sep = ",";
2367 2375 }
2368 2376 putchar(')');
2369 2377 }
2370 2378
2371 2379 token = sym->expansion;
2372 2380 while (token_type(token) != TOKEN_UNTAINT) {
2373 2381 struct token *next = token->next;
2374 2382 if (token->pos.whitespace)
2375 2383 putchar(' ');
2376 2384 switch (token_type(token)) {
2377 2385 case TOKEN_CONCAT:
2378 2386 printf("##");
2379 2387 break;
2380 2388 case TOKEN_STR_ARGUMENT:
2381 2389 printf("#");
2382 2390 /* fall-through */
2383 2391 case TOKEN_QUOTED_ARGUMENT:
2384 2392 case TOKEN_MACRO_ARGUMENT:
2385 2393 token = args[token->argnum];
2386 2394 /* fall-through */
2387 2395 default:
2388 2396 printf("%s", show_token(token));
2389 2397 }
2390 2398 token = next;
2391 2399 }
2392 2400 putchar('\n');
2393 2401 }
2394 2402
2395 2403 void dump_macro_definitions(void)
2396 2404 {
2397 2405 struct ident *name;
2398 2406
2399 2407 FOR_EACH_PTR(macros, name) {
2400 2408 struct symbol *sym = lookup_macro(name);
2401 2409 if (sym)
2402 2410 dump_macro(sym);
2403 2411 } END_FOR_EACH_PTR(name);
2404 2412 }
↓ open down ↓ |
824 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX