fix crash in rewrite_branch()
[smatch.git] / pre-process.c
blob74414dfeb93620b9e0cf43af88fc6a0beede879b
1 /*
2 * Do C preprocessing, based on a token list gathered by
3 * the tokenizer.
5 * This may not be the smartest preprocessor on the planet.
7 * Copyright (C) 2003 Transmeta Corp.
8 * 2003-2004 Linus Torvalds
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stddef.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <limits.h>
37 #include <time.h>
39 #include "lib.h"
40 #include "allocate.h"
41 #include "parse.h"
42 #include "token.h"
43 #include "symbol.h"
44 #include "expression.h"
45 #include "scope.h"
47 static struct ident_list *macros; // only needed for -dD
48 static int false_nesting = 0;
49 static int counter_macro = 0; // __COUNTER__ expansion
51 #define INCLUDEPATHS 300
52 const char *includepath[INCLUDEPATHS+1] = {
53 "",
54 "/usr/include",
55 "/usr/local/include",
56 NULL
59 static const char **quote_includepath = includepath;
60 static const char **angle_includepath = includepath + 1;
61 static const char **isys_includepath = includepath + 1;
62 static const char **sys_includepath = includepath + 1;
63 static const char **dirafter_includepath = includepath + 3;
65 #define dirty_stream(stream) \
66 do { \
67 if (!stream->dirty) { \
68 stream->dirty = 1; \
69 if (!stream->ifndef) \
70 stream->protect = NULL; \
71 } \
72 } while(0)
74 #define end_group(stream) \
75 do { \
76 if (stream->ifndef == stream->top_if) { \
77 stream->ifndef = NULL; \
78 if (!stream->dirty) \
79 stream->protect = NULL; \
80 else if (stream->protect) \
81 stream->dirty = 0; \
82 } \
83 } while(0)
85 #define nesting_error(stream) \
86 do { \
87 stream->dirty = 1; \
88 stream->ifndef = NULL; \
89 stream->protect = NULL; \
90 } while(0)
92 static struct token *alloc_token(struct position *pos)
94 struct token *token = __alloc_token(0);
96 token->pos.stream = pos->stream;
97 token->pos.line = pos->line;
98 token->pos.pos = pos->pos;
99 token->pos.whitespace = 1;
100 return token;
103 /* Expand symbol 'sym' at '*list' */
104 static int expand(struct token **, struct symbol *);
106 static void replace_with_string(struct token *token, const char *str)
108 int size = strlen(str) + 1;
109 struct string *s = __alloc_string(size);
111 s->length = size;
112 memcpy(s->data, str, size);
113 token_type(token) = TOKEN_STRING;
114 token->string = s;
117 static void replace_with_integer(struct token *token, unsigned int val)
119 char *buf = __alloc_bytes(11);
120 sprintf(buf, "%u", val);
121 token_type(token) = TOKEN_NUMBER;
122 token->number = buf;
125 static struct symbol *lookup_macro(struct ident *ident)
127 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
128 if (sym && sym->namespace != NS_MACRO)
129 sym = NULL;
130 return sym;
133 static int token_defined(struct token *token)
135 if (token_type(token) == TOKEN_IDENT) {
136 struct symbol *sym = lookup_macro(token->ident);
137 if (sym) {
138 sym->used_in = file_scope;
139 return 1;
141 return 0;
144 sparse_error(token->pos, "expected preprocessor identifier");
145 return 0;
148 static void replace_with_defined(struct token *token)
150 static const char *string[] = { "0", "1" };
151 int defined = token_defined(token);
153 token_type(token) = TOKEN_NUMBER;
154 token->number = string[defined];
157 static int expand_one_symbol(struct token **list)
159 struct token *token = *list;
160 struct symbol *sym;
161 static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
162 static time_t t = 0;
164 if (token->pos.noexpand)
165 return 1;
167 sym = lookup_macro(token->ident);
168 if (sym) {
169 sym->used_in = file_scope;
170 return expand(list, sym);
172 if (token->ident == &__LINE___ident) {
173 replace_with_integer(token, token->pos.line);
174 } else if (token->ident == &__FILE___ident) {
175 replace_with_string(token, stream_name(token->pos.stream));
176 } else if (token->ident == &__DATE___ident) {
177 if (!t)
178 time(&t);
179 strftime(buffer, 12, "%b %e %Y", localtime(&t));
180 replace_with_string(token, buffer);
181 } else if (token->ident == &__TIME___ident) {
182 if (!t)
183 time(&t);
184 strftime(buffer, 9, "%T", localtime(&t));
185 replace_with_string(token, buffer);
186 } else if (token->ident == &__COUNTER___ident) {
187 replace_with_integer(token, counter_macro++);
189 return 1;
192 static inline struct token *scan_next(struct token **where)
194 struct token *token = *where;
195 if (token_type(token) != TOKEN_UNTAINT)
196 return token;
197 do {
198 token->ident->tainted = 0;
199 token = token->next;
200 } while (token_type(token) == TOKEN_UNTAINT);
201 *where = token;
202 return token;
205 static void expand_list(struct token **list)
207 struct token *next;
208 while (!eof_token(next = scan_next(list))) {
209 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
210 list = &next->next;
214 static void preprocessor_line(struct stream *stream, struct token **line);
216 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos, int count)
218 struct stream *stream = input_streams + prev->pos.stream;
219 struct token **p = &prev->next;
220 struct token *next;
221 int nesting = 0;
223 while (!eof_token(next = scan_next(p))) {
224 if (next->pos.newline && match_op(next, '#')) {
225 if (!next->pos.noexpand) {
226 sparse_error(next->pos,
227 "directive in argument list");
228 preprocessor_line(stream, p);
229 __free_token(next); /* Free the '#' token */
230 continue;
233 switch (token_type(next)) {
234 case TOKEN_STREAMEND:
235 case TOKEN_STREAMBEGIN:
236 *p = &eof_token_entry;
237 return next;
238 case TOKEN_STRING:
239 case TOKEN_WIDE_STRING:
240 if (count > 1)
241 next->string->immutable = 1;
242 break;
244 if (false_nesting) {
245 *p = next->next;
246 __free_token(next);
247 continue;
249 if (match_op(next, '(')) {
250 nesting++;
251 } else if (match_op(next, ')')) {
252 if (!nesting--)
253 break;
254 } else if (match_op(next, ',') && !nesting && !vararg) {
255 break;
257 next->pos.stream = pos->stream;
258 next->pos.line = pos->line;
259 next->pos.pos = pos->pos;
260 p = &next->next;
262 *p = &eof_token_entry;
263 return next;
267 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
270 struct arg {
271 struct token *arg;
272 struct token *expanded;
273 struct token *str;
274 int n_normal;
275 int n_quoted;
276 int n_str;
279 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
281 int wanted = arglist->count.normal;
282 struct token *next = NULL;
283 int count = 0;
285 arglist = arglist->next; /* skip counter */
287 if (!wanted) {
288 next = collect_arg(start, 0, &what->pos, 0);
289 if (eof_token(next))
290 goto Eclosing;
291 if (!eof_token(start->next) || !match_op(next, ')')) {
292 count++;
293 goto Emany;
295 } else {
296 for (count = 0; count < wanted; count++) {
297 struct argcount *p = &arglist->next->count;
298 next = collect_arg(start, p->vararg, &what->pos, p->normal);
299 arglist = arglist->next->next;
300 if (eof_token(next))
301 goto Eclosing;
302 args[count].arg = start->next;
303 args[count].n_normal = p->normal;
304 args[count].n_quoted = p->quoted;
305 args[count].n_str = p->str;
306 if (match_op(next, ')')) {
307 count++;
308 break;
310 start = next;
312 if (count == wanted && !match_op(next, ')'))
313 goto Emany;
314 if (count == wanted - 1) {
315 struct argcount *p = &arglist->next->count;
316 if (!p->vararg)
317 goto Efew;
318 args[count].arg = NULL;
319 args[count].n_normal = p->normal;
320 args[count].n_quoted = p->quoted;
321 args[count].n_str = p->str;
323 if (count < wanted - 1)
324 goto Efew;
326 what->next = next->next;
327 return 1;
329 Efew:
330 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
331 show_token(what), wanted, count);
332 goto out;
333 Emany:
334 while (match_op(next, ',')) {
335 next = collect_arg(next, 0, &what->pos, 0);
336 count++;
338 if (eof_token(next))
339 goto Eclosing;
340 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
341 show_token(what), count, wanted);
342 goto out;
343 Eclosing:
344 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
345 show_token(what));
346 out:
347 what->next = next->next;
348 return 0;
351 static struct token *dup_list(struct token *list)
353 struct token *res = NULL;
354 struct token **p = &res;
356 while (!eof_token(list)) {
357 struct token *newtok = __alloc_token(0);
358 *newtok = *list;
359 *p = newtok;
360 p = &newtok->next;
361 list = list->next;
363 return res;
366 static const char *show_token_sequence(struct token *token, int quote)
368 static char buffer[MAX_STRING];
369 char *ptr = buffer;
370 int whitespace = 0;
372 if (!token && !quote)
373 return "<none>";
374 while (!eof_token(token)) {
375 const char *val = quote ? quote_token(token) : show_token(token);
376 int len = strlen(val);
378 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
379 sparse_error(token->pos, "too long token expansion");
380 break;
383 if (whitespace)
384 *ptr++ = ' ';
385 memcpy(ptr, val, len);
386 ptr += len;
387 token = token->next;
388 whitespace = token->pos.whitespace;
390 *ptr = 0;
391 return buffer;
394 static struct token *stringify(struct token *arg)
396 const char *s = show_token_sequence(arg, 1);
397 int size = strlen(s)+1;
398 struct token *token = __alloc_token(0);
399 struct string *string = __alloc_string(size);
401 memcpy(string->data, s, size);
402 string->length = size;
403 token->pos = arg->pos;
404 token_type(token) = TOKEN_STRING;
405 token->string = string;
406 token->next = &eof_token_entry;
407 return token;
410 static void expand_arguments(int count, struct arg *args)
412 int i;
413 for (i = 0; i < count; i++) {
414 struct token *arg = args[i].arg;
415 if (!arg)
416 arg = &eof_token_entry;
417 if (args[i].n_str)
418 args[i].str = stringify(arg);
419 if (args[i].n_normal) {
420 if (!args[i].n_quoted) {
421 args[i].expanded = arg;
422 args[i].arg = NULL;
423 } else if (eof_token(arg)) {
424 args[i].expanded = arg;
425 } else {
426 args[i].expanded = dup_list(arg);
428 expand_list(&args[i].expanded);
434 * Possibly valid combinations:
435 * - ident + ident -> ident
436 * - ident + number -> ident unless number contains '.', '+' or '-'.
437 * - 'L' + char constant -> wide char constant
438 * - 'L' + string literal -> wide string literal
439 * - number + number -> number
440 * - number + ident -> number
441 * - number + '.' -> number
442 * - number + '+' or '-' -> number, if number used to end on [eEpP].
443 * - '.' + number -> number, if number used to start with a digit.
444 * - special + special -> either special or an error.
446 static enum token_type combine(struct token *left, struct token *right, char *p)
448 int len;
449 enum token_type t1 = token_type(left), t2 = token_type(right);
451 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
452 return TOKEN_ERROR;
454 if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
455 if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
456 return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
457 if (t2 == TOKEN_STRING)
458 return TOKEN_WIDE_STRING;
461 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
462 return TOKEN_ERROR;
464 strcpy(p, show_token(left));
465 strcat(p, show_token(right));
466 len = strlen(p);
468 if (len >= 256)
469 return TOKEN_ERROR;
471 if (t1 == TOKEN_IDENT) {
472 if (t2 == TOKEN_SPECIAL)
473 return TOKEN_ERROR;
474 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
475 return TOKEN_ERROR;
476 return TOKEN_IDENT;
479 if (t1 == TOKEN_NUMBER) {
480 if (t2 == TOKEN_SPECIAL) {
481 switch (right->special) {
482 case '.':
483 break;
484 case '+': case '-':
485 if (strchr("eEpP", p[len - 2]))
486 break;
487 default:
488 return TOKEN_ERROR;
491 return TOKEN_NUMBER;
494 if (p[0] == '.' && isdigit((unsigned char)p[1]))
495 return TOKEN_NUMBER;
497 return TOKEN_SPECIAL;
500 static int merge(struct token *left, struct token *right)
502 static char buffer[512];
503 enum token_type res = combine(left, right, buffer);
504 int n;
506 switch (res) {
507 case TOKEN_IDENT:
508 left->ident = built_in_ident(buffer);
509 left->pos.noexpand = 0;
510 return 1;
512 case TOKEN_NUMBER: {
513 char *number = __alloc_bytes(strlen(buffer) + 1);
514 memcpy(number, buffer, strlen(buffer) + 1);
515 token_type(left) = TOKEN_NUMBER; /* could be . + num */
516 left->number = number;
517 return 1;
520 case TOKEN_SPECIAL:
521 if (buffer[2] && buffer[3])
522 break;
523 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
524 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
525 left->special = n;
526 return 1;
529 break;
531 case TOKEN_WIDE_CHAR:
532 case TOKEN_WIDE_STRING:
533 token_type(left) = res;
534 left->pos.noexpand = 0;
535 left->string = right->string;
536 return 1;
538 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
539 token_type(left) = res;
540 left->pos.noexpand = 0;
541 memcpy(left->embedded, right->embedded, 4);
542 return 1;
544 default:
547 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
548 return 0;
551 static struct token *dup_token(struct token *token, struct position *streampos)
553 struct token *alloc = alloc_token(streampos);
554 token_type(alloc) = token_type(token);
555 alloc->pos.newline = token->pos.newline;
556 alloc->pos.whitespace = token->pos.whitespace;
557 alloc->number = token->number;
558 alloc->pos.noexpand = token->pos.noexpand;
559 return alloc;
562 static struct token **copy(struct token **where, struct token *list, int *count)
564 int need_copy = --*count;
565 while (!eof_token(list)) {
566 struct token *token;
567 if (need_copy)
568 token = dup_token(list, &list->pos);
569 else
570 token = list;
571 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
572 token->pos.noexpand = 1;
573 *where = token;
574 where = &token->next;
575 list = list->next;
577 *where = &eof_token_entry;
578 return where;
581 static int handle_kludge(struct token **p, struct arg *args)
583 struct token *t = (*p)->next->next;
584 while (1) {
585 struct arg *v = &args[t->argnum];
586 if (token_type(t->next) != TOKEN_CONCAT) {
587 if (v->arg) {
588 /* ignore the first ## */
589 *p = (*p)->next;
590 return 0;
592 /* skip the entire thing */
593 *p = t;
594 return 1;
596 if (v->arg && !eof_token(v->arg))
597 return 0; /* no magic */
598 t = t->next->next;
602 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
604 struct position *base_pos = &(*list)->pos;
605 int *count;
606 enum {Normal, Placeholder, Concat} state = Normal;
608 for (; !eof_token(body); body = body->next) {
609 struct token *added, *arg;
610 struct token **tail;
611 struct token *t;
613 switch (token_type(body)) {
614 case TOKEN_GNU_KLUDGE:
616 * GNU kludge: if we had <comma>##<vararg>, behaviour
617 * depends on whether we had enough arguments to have
618 * a vararg. If we did, ## is just ignored. Otherwise
619 * both , and ## are ignored. Worse, there can be
620 * an arbitrary number of ##<arg> in between; if all of
621 * those are empty, we act as if they hadn't been there,
622 * otherwise we act as if the kludge didn't exist.
624 t = body;
625 if (handle_kludge(&body, args)) {
626 if (state == Concat)
627 state = Normal;
628 else
629 state = Placeholder;
630 continue;
632 added = dup_token(t, base_pos);
633 token_type(added) = TOKEN_SPECIAL;
634 tail = &added->next;
635 break;
637 case TOKEN_STR_ARGUMENT:
638 arg = args[body->argnum].str;
639 count = &args[body->argnum].n_str;
640 goto copy_arg;
642 case TOKEN_QUOTED_ARGUMENT:
643 arg = args[body->argnum].arg;
644 count = &args[body->argnum].n_quoted;
645 if (!arg || eof_token(arg)) {
646 if (state == Concat)
647 state = Normal;
648 else
649 state = Placeholder;
650 continue;
652 goto copy_arg;
654 case TOKEN_MACRO_ARGUMENT:
655 arg = args[body->argnum].expanded;
656 count = &args[body->argnum].n_normal;
657 if (eof_token(arg)) {
658 state = Normal;
659 continue;
661 copy_arg:
662 tail = copy(&added, arg, count);
663 added->pos.newline = body->pos.newline;
664 added->pos.whitespace = body->pos.whitespace;
665 break;
667 case TOKEN_CONCAT:
668 if (state == Placeholder)
669 state = Normal;
670 else
671 state = Concat;
672 continue;
674 case TOKEN_IDENT:
675 added = dup_token(body, base_pos);
676 if (added->ident->tainted)
677 added->pos.noexpand = 1;
678 tail = &added->next;
679 break;
681 default:
682 added = dup_token(body, base_pos);
683 tail = &added->next;
684 break;
688 * if we got to doing real concatenation, we already have
689 * added something into the list, so containing_token() is OK.
691 if (state == Concat && merge(containing_token(list), added)) {
692 *list = added->next;
693 if (tail != &added->next)
694 list = tail;
695 } else {
696 *list = added;
697 list = tail;
699 state = Normal;
701 *list = &eof_token_entry;
702 return list;
705 static int expand(struct token **list, struct symbol *sym)
707 struct token *last;
708 struct token *token = *list;
709 struct ident *expanding = token->ident;
710 struct token **tail;
711 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
712 struct arg args[nargs];
714 if (expanding->tainted) {
715 token->pos.noexpand = 1;
716 return 1;
719 if (sym->arglist) {
720 if (!match_op(scan_next(&token->next), '('))
721 return 1;
722 if (!collect_arguments(token->next, sym->arglist, args, token))
723 return 1;
724 expand_arguments(nargs, args);
727 expanding->tainted = 1;
729 last = token->next;
730 tail = substitute(list, sym->expansion, args);
732 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
733 * We still can lose the newline flag if the sucker expands to nothing,
734 * but the price of dealing with that is probably too high (we'd need
735 * to collect the flags during scan_next())
737 (*list)->pos.newline = token->pos.newline;
738 (*list)->pos.whitespace = token->pos.whitespace;
739 *tail = last;
741 return 0;
744 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
746 static char buffer[256];
747 char *ptr = buffer;
749 while (!eof_token(token) && !match_op(token, endop)) {
750 int len;
751 const char *val = token->string->data;
752 if (token_type(token) != TOKEN_STRING)
753 val = show_token(token);
754 len = strlen(val);
755 memcpy(ptr, val, len);
756 ptr += len;
757 token = token->next;
759 *ptr = 0;
760 if (endop && !match_op(token, endop))
761 sparse_error(start->pos, "expected '>' at end of filename");
762 return buffer;
765 static int already_tokenized(const char *path)
767 int stream, next;
769 for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
770 struct stream *s = input_streams + stream;
772 next = s->next_stream;
773 if (s->once) {
774 if (strcmp(path, s->name))
775 continue;
776 return 1;
778 if (s->constant != CONSTANT_FILE_YES)
779 continue;
780 if (strcmp(path, s->name))
781 continue;
782 if (s->protect && !lookup_macro(s->protect))
783 continue;
784 return 1;
786 return 0;
789 /* Handle include of header files.
790 * The relevant options are made compatible with gcc. The only options that
791 * are not supported is -withprefix and friends.
793 * Three set of include paths are known:
794 * quote_includepath: Path to search when using #include "file.h"
795 * angle_includepath: Paths to search when using #include <file.h>
796 * isys_includepath: Paths specified with -isystem, come before the
797 * built-in system include paths. Gcc would suppress
798 * warnings from system headers. Here we separate
799 * them from the angle_ ones to keep search ordering.
801 * sys_includepath: Built-in include paths.
802 * dirafter_includepath Paths added with -dirafter.
804 * The above is implemented as one array with pointers
805 * +--------------+
806 * quote_includepath ---> | |
807 * +--------------+
808 * | |
809 * +--------------+
810 * angle_includepath ---> | |
811 * +--------------+
812 * isys_includepath ---> | |
813 * +--------------+
814 * sys_includepath ---> | |
815 * +--------------+
816 * dirafter_includepath -> | |
817 * +--------------+
819 * -I dir insert dir just before isys_includepath and move the rest
820 * -I- makes all dirs specified with -I before to quote dirs only and
821 * angle_includepath is set equal to isys_includepath.
822 * -nostdinc removes all sys dirs by storing NULL in entry pointed
823 * to by * sys_includepath. Note that this will reset all dirs built-in
824 * and added before -nostdinc by -isystem and -idirafter.
825 * -isystem dir adds dir where isys_includepath points adding this dir as
826 * first systemdir
827 * -idirafter dir adds dir to the end of the list
830 static void set_stream_include_path(struct stream *stream)
832 const char *path = stream->path;
833 if (!path) {
834 const char *p = strrchr(stream->name, '/');
835 path = "";
836 if (p) {
837 int len = p - stream->name + 1;
838 char *m = malloc(len+1);
839 /* This includes the final "/" */
840 memcpy(m, stream->name, len);
841 m[len] = 0;
842 path = m;
844 stream->path = path;
846 includepath[0] = path;
849 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
851 int fd;
852 int plen = strlen(path);
853 static char fullname[PATH_MAX];
855 memcpy(fullname, path, plen);
856 if (plen && path[plen-1] != '/') {
857 fullname[plen] = '/';
858 plen++;
860 memcpy(fullname+plen, filename, flen);
861 if (already_tokenized(fullname))
862 return 1;
863 fd = open(fullname, O_RDONLY);
864 if (fd >= 0) {
865 char * streamname = __alloc_bytes(plen + flen);
866 memcpy(streamname, fullname, plen + flen);
867 *where = tokenize(streamname, fd, *where, next_path);
868 close(fd);
869 return 1;
871 return 0;
874 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
876 const char *path;
878 while ((path = *pptr++) != NULL) {
879 if (!try_include(path, filename, flen, list, pptr))
880 continue;
881 return 1;
883 return 0;
886 static int free_preprocessor_line(struct token *token)
888 while (token_type(token) != TOKEN_EOF) {
889 struct token *free = token;
890 token = token->next;
891 __free_token(free);
893 return 1;
896 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how)
898 const char *filename;
899 struct token *next;
900 const char **path;
901 int expect;
902 int flen;
904 next = token->next;
905 expect = '>';
906 if (!match_op(next, '<')) {
907 expand_list(&token->next);
908 expect = 0;
909 next = token;
910 if (match_op(token->next, '<')) {
911 next = token->next;
912 expect = '>';
916 token = next->next;
917 filename = token_name_sequence(token, expect, token);
918 flen = strlen(filename) + 1;
920 /* Absolute path? */
921 if (filename[0] == '/') {
922 if (try_include("", filename, flen, list, includepath))
923 return 0;
924 goto out;
927 switch (how) {
928 case 1:
929 path = stream->next_path;
930 break;
931 case 2:
932 includepath[0] = "";
933 path = includepath;
934 break;
935 default:
936 /* Dir of input file is first dir to search for quoted includes */
937 set_stream_include_path(stream);
938 path = expect ? angle_includepath : quote_includepath;
939 break;
941 /* Check the standard include paths.. */
942 if (do_include_path(path, list, token, filename, flen))
943 return 0;
944 out:
945 error_die(token->pos, "unable to open '%s'", filename);
948 static int handle_include(struct stream *stream, struct token **list, struct token *token)
950 return handle_include_path(stream, list, token, 0);
953 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
955 return handle_include_path(stream, list, token, 1);
958 static int handle_argv_include(struct stream *stream, struct token **list, struct token *token)
960 return handle_include_path(stream, list, token, 2);
963 static int token_different(struct token *t1, struct token *t2)
965 int different;
967 if (token_type(t1) != token_type(t2))
968 return 1;
970 switch (token_type(t1)) {
971 case TOKEN_IDENT:
972 different = t1->ident != t2->ident;
973 break;
974 case TOKEN_ARG_COUNT:
975 case TOKEN_UNTAINT:
976 case TOKEN_CONCAT:
977 case TOKEN_GNU_KLUDGE:
978 different = 0;
979 break;
980 case TOKEN_NUMBER:
981 different = strcmp(t1->number, t2->number);
982 break;
983 case TOKEN_SPECIAL:
984 different = t1->special != t2->special;
985 break;
986 case TOKEN_MACRO_ARGUMENT:
987 case TOKEN_QUOTED_ARGUMENT:
988 case TOKEN_STR_ARGUMENT:
989 different = t1->argnum != t2->argnum;
990 break;
991 case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
992 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
993 different = memcmp(t1->embedded, t2->embedded, 4);
994 break;
995 case TOKEN_CHAR:
996 case TOKEN_WIDE_CHAR:
997 case TOKEN_STRING:
998 case TOKEN_WIDE_STRING: {
999 struct string *s1, *s2;
1001 s1 = t1->string;
1002 s2 = t2->string;
1003 different = 1;
1004 if (s1->length != s2->length)
1005 break;
1006 different = memcmp(s1->data, s2->data, s1->length);
1007 break;
1009 default:
1010 different = 1;
1011 break;
1013 return different;
1016 static int token_list_different(struct token *list1, struct token *list2)
1018 for (;;) {
1019 if (list1 == list2)
1020 return 0;
1021 if (!list1 || !list2)
1022 return 1;
1023 if (token_different(list1, list2))
1024 return 1;
1025 list1 = list1->next;
1026 list2 = list2->next;
1030 static inline void set_arg_count(struct token *token)
1032 token_type(token) = TOKEN_ARG_COUNT;
1033 token->count.normal = token->count.quoted =
1034 token->count.str = token->count.vararg = 0;
1037 static struct token *parse_arguments(struct token *list)
1039 struct token *arg = list->next, *next = list;
1040 struct argcount *count = &list->count;
1042 set_arg_count(list);
1044 if (match_op(arg, ')')) {
1045 next = arg->next;
1046 list->next = &eof_token_entry;
1047 return next;
1050 while (token_type(arg) == TOKEN_IDENT) {
1051 if (arg->ident == &__VA_ARGS___ident)
1052 goto Eva_args;
1053 if (!++count->normal)
1054 goto Eargs;
1055 next = arg->next;
1057 if (match_op(next, ',')) {
1058 set_arg_count(next);
1059 arg = next->next;
1060 continue;
1063 if (match_op(next, ')')) {
1064 set_arg_count(next);
1065 next = next->next;
1066 arg->next->next = &eof_token_entry;
1067 return next;
1070 /* normal cases are finished here */
1072 if (match_op(next, SPECIAL_ELLIPSIS)) {
1073 if (match_op(next->next, ')')) {
1074 set_arg_count(next);
1075 next->count.vararg = 1;
1076 next = next->next;
1077 arg->next->next = &eof_token_entry;
1078 return next->next;
1081 arg = next;
1082 goto Enotclosed;
1085 if (eof_token(next)) {
1086 goto Enotclosed;
1087 } else {
1088 arg = next;
1089 goto Ebadstuff;
1093 if (match_op(arg, SPECIAL_ELLIPSIS)) {
1094 next = arg->next;
1095 token_type(arg) = TOKEN_IDENT;
1096 arg->ident = &__VA_ARGS___ident;
1097 if (!match_op(next, ')'))
1098 goto Enotclosed;
1099 if (!++count->normal)
1100 goto Eargs;
1101 set_arg_count(next);
1102 next->count.vararg = 1;
1103 next = next->next;
1104 arg->next->next = &eof_token_entry;
1105 return next;
1108 if (eof_token(arg)) {
1109 arg = next;
1110 goto Enotclosed;
1112 if (match_op(arg, ','))
1113 goto Emissing;
1114 else
1115 goto Ebadstuff;
1118 Emissing:
1119 sparse_error(arg->pos, "parameter name missing");
1120 return NULL;
1121 Ebadstuff:
1122 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
1123 show_token(arg));
1124 return NULL;
1125 Enotclosed:
1126 sparse_error(arg->pos, "missing ')' in macro parameter list");
1127 return NULL;
1128 Eva_args:
1129 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1130 return NULL;
1131 Eargs:
1132 sparse_error(arg->pos, "too many arguments in macro definition");
1133 return NULL;
1136 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1138 struct ident *ident = token->ident;
1139 int nr;
1141 if (!arglist || token_type(token) != TOKEN_IDENT)
1142 return 0;
1144 arglist = arglist->next;
1146 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1147 if (arglist->ident == ident) {
1148 struct argcount *count = &arglist->next->count;
1149 int n;
1151 token->argnum = nr;
1152 token_type(token) = type;
1153 switch (type) {
1154 case TOKEN_MACRO_ARGUMENT:
1155 n = ++count->normal;
1156 break;
1157 case TOKEN_QUOTED_ARGUMENT:
1158 n = ++count->quoted;
1159 break;
1160 default:
1161 n = ++count->str;
1163 if (n)
1164 return count->vararg ? 2 : 1;
1166 * XXX - need saner handling of that
1167 * (>= 1024 instances of argument)
1169 token_type(token) = TOKEN_ERROR;
1170 return -1;
1173 return 0;
1176 static struct token *handle_hash(struct token **p, struct token *arglist)
1178 struct token *token = *p;
1179 if (arglist) {
1180 struct token *next = token->next;
1181 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1182 goto Equote;
1183 next->pos.whitespace = token->pos.whitespace;
1184 __free_token(token);
1185 token = *p = next;
1186 } else {
1187 token->pos.noexpand = 1;
1189 return token;
1191 Equote:
1192 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1193 return NULL;
1196 /* token->next is ## */
1197 static struct token *handle_hashhash(struct token *token, struct token *arglist)
1199 struct token *last = token;
1200 struct token *concat;
1201 int state = match_op(token, ',');
1203 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1205 while (1) {
1206 struct token *t;
1207 int is_arg;
1209 /* eat duplicate ## */
1210 concat = token->next;
1211 while (match_op(t = concat->next, SPECIAL_HASHHASH)) {
1212 token->next = t;
1213 __free_token(concat);
1214 concat = t;
1216 token_type(concat) = TOKEN_CONCAT;
1218 if (eof_token(t))
1219 goto Econcat;
1221 if (match_op(t, '#')) {
1222 t = handle_hash(&concat->next, arglist);
1223 if (!t)
1224 return NULL;
1227 is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
1229 if (state == 1 && is_arg) {
1230 state = is_arg;
1231 } else {
1232 last = t;
1233 state = match_op(t, ',');
1236 token = t;
1237 if (!match_op(token->next, SPECIAL_HASHHASH))
1238 break;
1240 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1241 if (state == 2)
1242 token_type(last) = TOKEN_GNU_KLUDGE;
1243 return token;
1245 Econcat:
1246 sparse_error(concat->pos, "'##' cannot appear at the ends of macro expansion");
1247 return NULL;
1250 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1252 struct token *token = expansion;
1253 struct token **p;
1255 if (match_op(token, SPECIAL_HASHHASH))
1256 goto Econcat;
1258 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1259 if (match_op(token, '#')) {
1260 token = handle_hash(p, arglist);
1261 if (!token)
1262 return NULL;
1264 if (match_op(token->next, SPECIAL_HASHHASH)) {
1265 token = handle_hashhash(token, arglist);
1266 if (!token)
1267 return NULL;
1268 } else {
1269 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1271 switch (token_type(token)) {
1272 case TOKEN_ERROR:
1273 goto Earg;
1275 case TOKEN_STRING:
1276 case TOKEN_WIDE_STRING:
1277 token->string->immutable = 1;
1278 break;
1281 token = alloc_token(&expansion->pos);
1282 token_type(token) = TOKEN_UNTAINT;
1283 token->ident = name;
1284 token->next = *p;
1285 *p = token;
1286 return expansion;
1288 Econcat:
1289 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1290 return NULL;
1291 Earg:
1292 sparse_error(token->pos, "too many instances of argument in body");
1293 return NULL;
1296 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1298 struct token *arglist, *expansion;
1299 struct token *left = token->next;
1300 struct symbol *sym;
1301 struct ident *name;
1302 int ret;
1304 if (token_type(left) != TOKEN_IDENT) {
1305 sparse_error(token->pos, "expected identifier to 'define'");
1306 return 1;
1309 name = left->ident;
1311 arglist = NULL;
1312 expansion = left->next;
1313 if (!expansion->pos.whitespace) {
1314 if (match_op(expansion, '(')) {
1315 arglist = expansion;
1316 expansion = parse_arguments(expansion);
1317 if (!expansion)
1318 return 1;
1319 } else if (!eof_token(expansion)) {
1320 warning(expansion->pos,
1321 "no whitespace before object-like macro body");
1325 expansion = parse_expansion(expansion, arglist, name);
1326 if (!expansion)
1327 return 1;
1329 ret = 1;
1330 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1331 if (sym) {
1332 int clean;
1334 if (attr < sym->attr)
1335 goto out;
1337 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1339 if (token_list_different(sym->expansion, expansion) ||
1340 token_list_different(sym->arglist, arglist)) {
1341 ret = 0;
1342 if ((clean && attr == SYM_ATTR_NORMAL)
1343 || sym->used_in == file_scope) {
1344 warning(left->pos, "preprocessor token %.*s redefined",
1345 name->len, name->name);
1346 info(sym->pos, "this was the original definition");
1348 } else if (clean)
1349 goto out;
1352 if (!sym || sym->scope != file_scope) {
1353 sym = alloc_symbol(left->pos, SYM_NODE);
1354 bind_symbol(sym, name, NS_MACRO);
1355 add_ident(&macros, name);
1356 ret = 0;
1359 if (!ret) {
1360 sym->expansion = expansion;
1361 sym->arglist = arglist;
1362 __free_token(token); /* Free the "define" token, but not the rest of the line */
1365 sym->namespace = NS_MACRO;
1366 sym->used_in = NULL;
1367 sym->attr = attr;
1368 out:
1369 return ret;
1372 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1374 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1377 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1379 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1382 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1384 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1387 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1389 struct token *left = token->next;
1390 struct symbol *sym;
1392 if (token_type(left) != TOKEN_IDENT) {
1393 sparse_error(token->pos, "expected identifier to 'undef'");
1394 return 1;
1397 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1398 if (sym) {
1399 if (attr < sym->attr)
1400 return 1;
1401 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1402 return 1;
1403 } else if (attr <= SYM_ATTR_NORMAL)
1404 return 1;
1406 if (!sym || sym->scope != file_scope) {
1407 sym = alloc_symbol(left->pos, SYM_NODE);
1408 bind_symbol(sym, left->ident, NS_MACRO);
1411 sym->namespace = NS_UNDEF;
1412 sym->used_in = NULL;
1413 sym->attr = attr;
1415 return 1;
1418 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1420 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1423 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1425 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1428 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1430 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1431 free_preprocessor_line(token->next);
1432 token->next = stream->top_if;
1433 stream->top_if = token;
1434 if (false_nesting || true != 1)
1435 false_nesting++;
1436 return 0;
1439 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1441 struct token *next = token->next;
1442 int arg;
1443 if (token_type(next) == TOKEN_IDENT) {
1444 arg = token_defined(next);
1445 } else {
1446 dirty_stream(stream);
1447 if (!false_nesting)
1448 sparse_error(token->pos, "expected preprocessor identifier");
1449 arg = -1;
1451 return preprocessor_if(stream, token, arg);
1454 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1456 struct token *next = token->next;
1457 int arg;
1458 if (token_type(next) == TOKEN_IDENT) {
1459 if (!stream->dirty && !stream->ifndef) {
1460 if (!stream->protect) {
1461 stream->ifndef = token;
1462 stream->protect = next->ident;
1463 } else if (stream->protect == next->ident) {
1464 stream->ifndef = token;
1465 stream->dirty = 1;
1468 arg = !token_defined(next);
1469 } else {
1470 dirty_stream(stream);
1471 if (!false_nesting)
1472 sparse_error(token->pos, "expected preprocessor identifier");
1473 arg = -1;
1476 return preprocessor_if(stream, token, arg);
1479 static const char *show_token_sequence(struct token *token, int quote);
1482 * Expression handling for #if and #elif; it differs from normal expansion
1483 * due to special treatment of "defined".
1485 static int expression_value(struct token **where)
1487 struct expression *expr;
1488 struct token *p;
1489 struct token **list = where, **beginning = NULL;
1490 long long value;
1491 int state = 0;
1493 while (!eof_token(p = scan_next(list))) {
1494 switch (state) {
1495 case 0:
1496 if (token_type(p) != TOKEN_IDENT)
1497 break;
1498 if (p->ident == &defined_ident) {
1499 state = 1;
1500 beginning = list;
1501 break;
1503 if (!expand_one_symbol(list))
1504 continue;
1505 if (token_type(p) != TOKEN_IDENT)
1506 break;
1507 token_type(p) = TOKEN_ZERO_IDENT;
1508 break;
1509 case 1:
1510 if (match_op(p, '(')) {
1511 state = 2;
1512 } else {
1513 state = 0;
1514 replace_with_defined(p);
1515 *beginning = p;
1517 break;
1518 case 2:
1519 if (token_type(p) == TOKEN_IDENT)
1520 state = 3;
1521 else
1522 state = 0;
1523 replace_with_defined(p);
1524 *beginning = p;
1525 break;
1526 case 3:
1527 state = 0;
1528 if (!match_op(p, ')'))
1529 sparse_error(p->pos, "missing ')' after \"defined\"");
1530 *list = p->next;
1531 continue;
1533 list = &p->next;
1536 p = constant_expression(*where, &expr);
1537 if (!eof_token(p))
1538 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p, 0));
1539 value = get_expression_value(expr);
1540 return value != 0;
1543 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1545 int value = 0;
1546 if (!false_nesting)
1547 value = expression_value(&token->next);
1549 dirty_stream(stream);
1550 return preprocessor_if(stream, token, value);
1553 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1555 struct token *top_if = stream->top_if;
1556 end_group(stream);
1558 if (!top_if) {
1559 nesting_error(stream);
1560 sparse_error(token->pos, "unmatched #elif within stream");
1561 return 1;
1564 if (token_type(top_if) == TOKEN_ELSE) {
1565 nesting_error(stream);
1566 sparse_error(token->pos, "#elif after #else");
1567 if (!false_nesting)
1568 false_nesting = 1;
1569 return 1;
1572 dirty_stream(stream);
1573 if (token_type(top_if) != TOKEN_IF)
1574 return 1;
1575 if (false_nesting) {
1576 false_nesting = 0;
1577 if (!expression_value(&token->next))
1578 false_nesting = 1;
1579 } else {
1580 false_nesting = 1;
1581 token_type(top_if) = TOKEN_SKIP_GROUPS;
1583 return 1;
1586 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1588 struct token *top_if = stream->top_if;
1589 end_group(stream);
1591 if (!top_if) {
1592 nesting_error(stream);
1593 sparse_error(token->pos, "unmatched #else within stream");
1594 return 1;
1597 if (token_type(top_if) == TOKEN_ELSE) {
1598 nesting_error(stream);
1599 sparse_error(token->pos, "#else after #else");
1601 if (false_nesting) {
1602 if (token_type(top_if) == TOKEN_IF)
1603 false_nesting = 0;
1604 } else {
1605 false_nesting = 1;
1607 token_type(top_if) = TOKEN_ELSE;
1608 return 1;
1611 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1613 struct token *top_if = stream->top_if;
1614 end_group(stream);
1615 if (!top_if) {
1616 nesting_error(stream);
1617 sparse_error(token->pos, "unmatched #endif in stream");
1618 return 1;
1620 if (false_nesting)
1621 false_nesting--;
1622 stream->top_if = top_if->next;
1623 __free_token(top_if);
1624 return 1;
1627 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1629 warning(token->pos, "%s", show_token_sequence(token->next, 0));
1630 return 1;
1633 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1635 sparse_error(token->pos, "%s", show_token_sequence(token->next, 0));
1636 return 1;
1639 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1642 * Do we have any non-system includes?
1643 * Clear them out if so..
1645 *sys_includepath = NULL;
1646 return 1;
1649 static inline void update_inc_ptrs(const char ***where)
1652 if (*where <= dirafter_includepath) {
1653 dirafter_includepath++;
1654 /* If this was the entry that we prepend, don't
1655 * rise the lower entries, even if they are at
1656 * the same level. */
1657 if (where == &dirafter_includepath)
1658 return;
1660 if (*where <= sys_includepath) {
1661 sys_includepath++;
1662 if (where == &sys_includepath)
1663 return;
1665 if (*where <= isys_includepath) {
1666 isys_includepath++;
1667 if (where == &isys_includepath)
1668 return;
1671 /* angle_includepath is actually never updated, since we
1672 * don't suppport -iquote rught now. May change some day. */
1673 if (*where <= angle_includepath) {
1674 angle_includepath++;
1675 if (where == &angle_includepath)
1676 return;
1680 /* Add a path before 'where' and update the pointers associated with the
1681 * includepath array */
1682 static void add_path_entry(struct token *token, const char *path,
1683 const char ***where)
1685 const char **dst;
1686 const char *next;
1688 /* Need one free entry.. */
1689 if (includepath[INCLUDEPATHS-2])
1690 error_die(token->pos, "too many include path entries");
1692 /* check that this is not a duplicate */
1693 dst = includepath;
1694 while (*dst) {
1695 if (strcmp(*dst, path) == 0)
1696 return;
1697 dst++;
1699 next = path;
1700 dst = *where;
1702 update_inc_ptrs(where);
1705 * Move them all up starting at dst,
1706 * insert the new entry..
1708 do {
1709 const char *tmp = *dst;
1710 *dst = next;
1711 next = tmp;
1712 dst++;
1713 } while (next);
1716 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1718 for (;;) {
1719 token = token->next;
1720 if (eof_token(token))
1721 return 1;
1722 if (token_type(token) != TOKEN_STRING) {
1723 warning(token->pos, "expected path string");
1724 return 1;
1726 add_path_entry(token, token->string->data, &isys_includepath);
1730 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1732 for (;;) {
1733 token = token->next;
1734 if (eof_token(token))
1735 return 1;
1736 if (token_type(token) != TOKEN_STRING) {
1737 sparse_error(token->pos, "expected path string");
1738 return 1;
1740 add_path_entry(token, token->string->data, &sys_includepath);
1744 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1746 for (;;) {
1747 token = token->next;
1748 if (eof_token(token))
1749 return 1;
1750 if (token_type(token) != TOKEN_STRING) {
1751 sparse_error(token->pos, "expected path string");
1752 return 1;
1754 add_path_entry(token, token->string->data, &dirafter_includepath);
1758 /* Add to end on includepath list - no pointer updates */
1759 static void add_dirafter_entry(struct token *token, const char *path)
1761 const char **dst = includepath;
1763 /* Need one free entry.. */
1764 if (includepath[INCLUDEPATHS-2])
1765 error_die(token->pos, "too many include path entries");
1767 /* Add to the end */
1768 while (*dst)
1769 dst++;
1770 *dst = path;
1771 dst++;
1772 *dst = NULL;
1775 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1777 for (;;) {
1778 token = token->next;
1779 if (eof_token(token))
1780 return 1;
1781 if (token_type(token) != TOKEN_STRING) {
1782 sparse_error(token->pos, "expected path string");
1783 return 1;
1785 add_dirafter_entry(token, token->string->data);
1789 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1792 * -I-
1793 * From info gcc:
1794 * Split the include path. Any directories specified with `-I'
1795 * options before `-I-' are searched only for headers requested with
1796 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1797 * If additional directories are specified with `-I' options after
1798 * the `-I-', those directories are searched for all `#include'
1799 * directives.
1800 * In addition, `-I-' inhibits the use of the directory of the current
1801 * file directory as the first search directory for `#include "FILE"'.
1803 quote_includepath = includepath+1;
1804 angle_includepath = sys_includepath;
1805 return 1;
1809 * We replace "#pragma xxx" with "__pragma__" in the token
1810 * stream. Just as an example.
1812 * We'll just #define that away for now, but the theory here
1813 * is that we can use this to insert arbitrary token sequences
1814 * to turn the pragmas into internal front-end sequences for
1815 * when we actually start caring about them.
1817 * So eventually this will turn into some kind of extended
1818 * __attribute__() like thing, except called __pragma__(xxx).
1820 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1822 struct token *next = *line;
1824 if (match_ident(token->next, &once_ident) && eof_token(token->next->next)) {
1825 stream->once = 1;
1826 return 1;
1828 token->ident = &pragma_ident;
1829 token->pos.newline = 1;
1830 token->pos.whitespace = 1;
1831 token->pos.pos = 1;
1832 *line = token;
1833 token->next = next;
1834 return 0;
1838 * We ignore #line for now.
1840 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1842 return 1;
1845 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1847 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token, 0));
1848 return 1;
1852 static void init_preprocessor(void)
1854 int i;
1855 int stream = init_stream("preprocessor", -1, includepath);
1856 static struct {
1857 const char *name;
1858 int (*handler)(struct stream *, struct token **, struct token *);
1859 } normal[] = {
1860 { "define", handle_define },
1861 { "weak_define", handle_weak_define },
1862 { "strong_define", handle_strong_define },
1863 { "undef", handle_undef },
1864 { "strong_undef", handle_strong_undef },
1865 { "warning", handle_warning },
1866 { "error", handle_error },
1867 { "include", handle_include },
1868 { "include_next", handle_include_next },
1869 { "pragma", handle_pragma },
1870 { "line", handle_line },
1872 // our internal preprocessor tokens
1873 { "nostdinc", handle_nostdinc },
1874 { "add_include", handle_add_include },
1875 { "add_isystem", handle_add_isystem },
1876 { "add_system", handle_add_system },
1877 { "add_dirafter", handle_add_dirafter },
1878 { "split_include", handle_split_include },
1879 { "argv_include", handle_argv_include },
1880 }, special[] = {
1881 { "ifdef", handle_ifdef },
1882 { "ifndef", handle_ifndef },
1883 { "else", handle_else },
1884 { "endif", handle_endif },
1885 { "if", handle_if },
1886 { "elif", handle_elif },
1889 for (i = 0; i < ARRAY_SIZE(normal); i++) {
1890 struct symbol *sym;
1891 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1892 sym->handler = normal[i].handler;
1893 sym->normal = 1;
1895 for (i = 0; i < ARRAY_SIZE(special); i++) {
1896 struct symbol *sym;
1897 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1898 sym->handler = special[i].handler;
1899 sym->normal = 0;
1902 counter_macro = 0;
1905 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1907 int (*handler)(struct stream *, struct token **, struct token *);
1908 struct token *token = start->next;
1909 int is_normal = 1;
1911 if (eof_token(token))
1912 return;
1914 if (token_type(token) == TOKEN_IDENT) {
1915 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1916 if (sym) {
1917 handler = sym->handler;
1918 is_normal = sym->normal;
1919 } else {
1920 handler = handle_nondirective;
1922 } else if (token_type(token) == TOKEN_NUMBER) {
1923 handler = handle_line;
1924 } else {
1925 handler = handle_nondirective;
1928 if (is_normal) {
1929 dirty_stream(stream);
1930 if (false_nesting)
1931 goto out;
1933 if (!handler(stream, line, token)) /* all set */
1934 return;
1936 out:
1937 free_preprocessor_line(token);
1940 static void preprocessor_line(struct stream *stream, struct token **line)
1942 struct token *start = *line, *next;
1943 struct token **tp = &start->next;
1945 for (;;) {
1946 next = *tp;
1947 if (next->pos.newline)
1948 break;
1949 tp = &next->next;
1951 *line = next;
1952 *tp = &eof_token_entry;
1953 handle_preprocessor_line(stream, line, start);
1956 static void do_preprocess(struct token **list)
1958 struct token *next;
1960 while (!eof_token(next = scan_next(list))) {
1961 struct stream *stream = input_streams + next->pos.stream;
1963 if (next->pos.newline && match_op(next, '#')) {
1964 if (!next->pos.noexpand) {
1965 preprocessor_line(stream, list);
1966 __free_token(next); /* Free the '#' token */
1967 continue;
1971 switch (token_type(next)) {
1972 case TOKEN_STREAMEND:
1973 if (stream->top_if) {
1974 nesting_error(stream);
1975 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1976 stream->top_if = NULL;
1977 false_nesting = 0;
1979 if (!stream->dirty)
1980 stream->constant = CONSTANT_FILE_YES;
1981 *list = next->next;
1982 continue;
1983 case TOKEN_STREAMBEGIN:
1984 *list = next->next;
1985 continue;
1987 default:
1988 dirty_stream(stream);
1989 if (false_nesting) {
1990 *list = next->next;
1991 __free_token(next);
1992 continue;
1995 if (token_type(next) != TOKEN_IDENT ||
1996 expand_one_symbol(list))
1997 list = &next->next;
2002 struct token * preprocess(struct token *token)
2004 preprocessing = 1;
2005 init_preprocessor();
2006 do_preprocess(&token);
2008 // Drop all expressions from preprocessing, they're not used any more.
2009 // This is not true when we have multiple files, though ;/
2010 // clear_expression_alloc();
2011 preprocessing = 0;
2013 return token;
2016 static void dump_macro(struct symbol *sym)
2018 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
2019 struct token *args[nargs];
2020 struct token *token;
2022 printf("#define %s", show_ident(sym->ident));
2023 token = sym->arglist;
2024 if (token) {
2025 const char *sep = "";
2026 int narg = 0;
2027 putchar('(');
2028 for (; !eof_token(token); token = token->next) {
2029 if (token_type(token) == TOKEN_ARG_COUNT)
2030 continue;
2031 printf("%s%s", sep, show_token(token));
2032 args[narg++] = token;
2033 sep = ", ";
2035 putchar(')');
2037 putchar(' ');
2039 token = sym->expansion;
2040 while (!eof_token(token)) {
2041 struct token *next = token->next;
2042 switch (token_type(token)) {
2043 case TOKEN_UNTAINT:
2044 break;
2045 case TOKEN_MACRO_ARGUMENT:
2046 token = args[token->argnum];
2047 /* fall-through */
2048 default:
2049 printf("%s", show_token(token));
2050 if (next->pos.whitespace)
2051 putchar(' ');
2053 token = next;
2055 putchar('\n');
2058 void dump_macro_definitions(void)
2060 struct ident *name;
2062 FOR_EACH_PTR(macros, name) {
2063 struct symbol *sym = lookup_macro(name);
2064 if (sym)
2065 dump_macro(sym);
2066 } END_FOR_EACH_PTR(name);