Teach classify_type to handle typeof
[smatch.git] / pre-process.c
blobcf538930fa30b0c68140659637f26ed4794f3983
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 * Licensed under the Open Software License version 1.1
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <time.h>
23 #include "lib.h"
24 #include "allocate.h"
25 #include "parse.h"
26 #include "token.h"
27 #include "symbol.h"
28 #include "expression.h"
29 #include "scope.h"
31 static int false_nesting = 0;
33 #define INCLUDEPATHS 300
34 const char *includepath[INCLUDEPATHS+1] = {
35 "",
36 "/usr/include",
37 "/usr/local/include",
38 NULL
41 static const char **quote_includepath = includepath;
42 static const char **angle_includepath = includepath + 1;
43 static const char **isys_includepath = includepath + 1;
44 static const char **sys_includepath = includepath + 1;
45 static const char **dirafter_includepath = includepath + 3;
47 #define dirty_stream(stream) \
48 do { \
49 if (!stream->dirty) { \
50 stream->dirty = 1; \
51 if (!stream->ifndef) \
52 stream->protect = NULL; \
53 } \
54 } while(0)
56 #define end_group(stream) \
57 do { \
58 if (stream->ifndef == stream->top_if) { \
59 stream->ifndef = NULL; \
60 if (!stream->dirty) \
61 stream->protect = NULL; \
62 else if (stream->protect) \
63 stream->dirty = 0; \
64 } \
65 } while(0)
67 #define nesting_error(stream) \
68 do { \
69 stream->dirty = 1; \
70 stream->ifndef = NULL; \
71 stream->protect = NULL; \
72 } while(0)
74 static struct token *alloc_token(struct position *pos)
76 struct token *token = __alloc_token(0);
78 token->pos.stream = pos->stream;
79 token->pos.line = pos->line;
80 token->pos.pos = pos->pos;
81 token->pos.whitespace = 1;
82 return token;
85 static const char *show_token_sequence(struct token *token);
87 /* Expand symbol 'sym' at '*list' */
88 static int expand(struct token **, struct symbol *);
90 static void replace_with_string(struct token *token, const char *str)
92 int size = strlen(str) + 1;
93 struct string *s = __alloc_string(size);
95 s->length = size;
96 memcpy(s->data, str, size);
97 token_type(token) = TOKEN_STRING;
98 token->string = s;
101 static void replace_with_integer(struct token *token, unsigned int val)
103 char *buf = __alloc_bytes(11);
104 sprintf(buf, "%u", val);
105 token_type(token) = TOKEN_NUMBER;
106 token->number = buf;
109 static struct symbol *lookup_macro(struct ident *ident)
111 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
112 if (sym && sym->namespace != NS_MACRO)
113 sym = NULL;
114 return sym;
117 static int token_defined(struct token *token)
119 if (token_type(token) == TOKEN_IDENT) {
120 struct symbol *sym = lookup_macro(token->ident);
121 if (sym) {
122 sym->used_in = file_scope;
123 return 1;
125 return 0;
128 sparse_error(token->pos, "expected preprocessor identifier");
129 return 0;
132 static void replace_with_defined(struct token *token)
134 static const char *string[] = { "0", "1" };
135 int defined = token_defined(token);
137 token_type(token) = TOKEN_NUMBER;
138 token->number = string[defined];
141 static int expand_one_symbol(struct token **list)
143 struct token *token = *list;
144 struct symbol *sym;
145 static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
146 static time_t t = 0;
148 if (token->pos.noexpand)
149 return 1;
151 sym = lookup_macro(token->ident);
152 if (sym) {
153 sym->used_in = file_scope;
154 return expand(list, sym);
156 if (token->ident == &__LINE___ident) {
157 replace_with_integer(token, token->pos.line);
158 } else if (token->ident == &__FILE___ident) {
159 replace_with_string(token, stream_name(token->pos.stream));
160 } else if (token->ident == &__DATE___ident) {
161 if (!t)
162 time(&t);
163 strftime(buffer, 12, "%b %e %Y", localtime(&t));
164 replace_with_string(token, buffer);
165 } else if (token->ident == &__TIME___ident) {
166 if (!t)
167 time(&t);
168 strftime(buffer, 9, "%T", localtime(&t));
169 replace_with_string(token, buffer);
171 return 1;
174 static inline struct token *scan_next(struct token **where)
176 struct token *token = *where;
177 if (token_type(token) != TOKEN_UNTAINT)
178 return token;
179 do {
180 token->ident->tainted = 0;
181 token = token->next;
182 } while (token_type(token) == TOKEN_UNTAINT);
183 *where = token;
184 return token;
187 static void expand_list(struct token **list)
189 struct token *next;
190 while (!eof_token(next = scan_next(list))) {
191 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
192 list = &next->next;
196 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
198 struct token **p = &prev->next;
199 struct token *next;
200 int nesting = 0;
202 while (!eof_token(next = scan_next(p))) {
203 if (match_op(next, '(')) {
204 nesting++;
205 } else if (match_op(next, ')')) {
206 if (!nesting--)
207 break;
208 } else if (match_op(next, ',') && !nesting && !vararg) {
209 break;
211 next->pos.stream = pos->stream;
212 next->pos.line = pos->line;
213 next->pos.pos = pos->pos;
214 p = &next->next;
216 *p = &eof_token_entry;
217 return next;
221 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
224 struct arg {
225 struct token *arg;
226 struct token *expanded;
227 struct token *str;
228 int n_normal;
229 int n_quoted;
230 int n_str;
233 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
235 int wanted = arglist->count.normal;
236 struct token *next = NULL;
237 int count = 0;
239 arglist = arglist->next; /* skip counter */
241 if (!wanted) {
242 next = collect_arg(start, 0, &what->pos);
243 if (eof_token(next))
244 goto Eclosing;
245 if (!eof_token(start->next) || !match_op(next, ')')) {
246 count++;
247 goto Emany;
249 } else {
250 for (count = 0; count < wanted; count++) {
251 struct argcount *p = &arglist->next->count;
252 next = collect_arg(start, p->vararg, &what->pos);
253 arglist = arglist->next->next;
254 if (eof_token(next))
255 goto Eclosing;
256 args[count].arg = start->next;
257 args[count].n_normal = p->normal;
258 args[count].n_quoted = p->quoted;
259 args[count].n_str = p->str;
260 if (match_op(next, ')')) {
261 count++;
262 break;
264 start = next;
266 if (count == wanted && !match_op(next, ')'))
267 goto Emany;
268 if (count == wanted - 1) {
269 struct argcount *p = &arglist->next->count;
270 if (!p->vararg)
271 goto Efew;
272 args[count].arg = NULL;
273 args[count].n_normal = p->normal;
274 args[count].n_quoted = p->quoted;
275 args[count].n_str = p->str;
277 if (count < wanted - 1)
278 goto Efew;
280 what->next = next->next;
281 return 1;
283 Efew:
284 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
285 show_token(what), wanted, count);
286 goto out;
287 Emany:
288 while (match_op(next, ',')) {
289 next = collect_arg(next, 0, &what->pos);
290 count++;
292 if (eof_token(next))
293 goto Eclosing;
294 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
295 show_token(what), count, wanted);
296 goto out;
297 Eclosing:
298 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
299 show_token(what));
300 out:
301 what->next = next->next;
302 return 0;
305 static struct token *dup_list(struct token *list)
307 struct token *res = NULL;
308 struct token **p = &res;
310 while (!eof_token(list)) {
311 struct token *newtok = __alloc_token(0);
312 *newtok = *list;
313 *p = newtok;
314 p = &newtok->next;
315 list = list->next;
317 return res;
320 static struct token *stringify(struct token *arg)
322 const char *s = show_token_sequence(arg);
323 int size = strlen(s)+1;
324 struct token *token = __alloc_token(0);
325 struct string *string = __alloc_string(size);
327 memcpy(string->data, s, size);
328 string->length = size;
329 token->pos = arg->pos;
330 token_type(token) = TOKEN_STRING;
331 token->string = string;
332 token->next = &eof_token_entry;
333 return token;
336 static void expand_arguments(int count, struct arg *args)
338 int i;
339 for (i = 0; i < count; i++) {
340 struct token *arg = args[i].arg;
341 if (!arg)
342 arg = &eof_token_entry;
343 if (args[i].n_str)
344 args[i].str = stringify(arg);
345 if (args[i].n_normal) {
346 if (!args[i].n_quoted) {
347 args[i].expanded = arg;
348 args[i].arg = NULL;
349 } else if (eof_token(arg)) {
350 args[i].expanded = arg;
351 } else {
352 args[i].expanded = dup_list(arg);
354 expand_list(&args[i].expanded);
360 * Possibly valid combinations:
361 * - ident + ident -> ident
362 * - ident + number -> ident unless number contains '.', '+' or '-'.
363 * - number + number -> number
364 * - number + ident -> number
365 * - number + '.' -> number
366 * - number + '+' or '-' -> number, if number used to end on [eEpP].
367 * - '.' + number -> number, if number used to start with a digit.
368 * - special + special -> either special or an error.
370 static enum token_type combine(struct token *left, struct token *right, char *p)
372 int len;
373 enum token_type t1 = token_type(left), t2 = token_type(right);
375 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
376 return TOKEN_ERROR;
378 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
379 return TOKEN_ERROR;
381 strcpy(p, show_token(left));
382 strcat(p, show_token(right));
383 len = strlen(p);
385 if (len >= 256)
386 return TOKEN_ERROR;
388 if (t1 == TOKEN_IDENT) {
389 if (t2 == TOKEN_SPECIAL)
390 return TOKEN_ERROR;
391 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
392 return TOKEN_ERROR;
393 return TOKEN_IDENT;
396 if (t1 == TOKEN_NUMBER) {
397 if (t2 == TOKEN_SPECIAL) {
398 switch (right->special) {
399 case '.':
400 break;
401 case '+': case '-':
402 if (strchr("eEpP", p[len - 2]))
403 break;
404 default:
405 return TOKEN_ERROR;
408 return TOKEN_NUMBER;
411 if (p[0] == '.' && isdigit((unsigned char)p[1]))
412 return TOKEN_NUMBER;
414 return TOKEN_SPECIAL;
417 static int merge(struct token *left, struct token *right)
419 static char buffer[512];
420 int n;
422 switch (combine(left, right, buffer)) {
423 case TOKEN_IDENT:
424 left->ident = built_in_ident(buffer);
425 left->pos.noexpand = 0;
426 return 1;
428 case TOKEN_NUMBER: {
429 char *number = __alloc_bytes(strlen(buffer) + 1);
430 memcpy(number, buffer, strlen(buffer) + 1);
431 token_type(left) = TOKEN_NUMBER; /* could be . + num */
432 left->number = number;
433 return 1;
436 case TOKEN_SPECIAL:
437 if (buffer[2] && buffer[3])
438 break;
439 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
440 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
441 left->special = n;
442 return 1;
445 default:
448 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
449 return 0;
452 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
454 struct token *alloc = alloc_token(streampos);
455 token_type(alloc) = token_type(token);
456 alloc->pos.newline = pos->newline;
457 alloc->pos.whitespace = pos->whitespace;
458 alloc->number = token->number;
459 alloc->pos.noexpand = token->pos.noexpand;
460 return alloc;
463 static struct token **copy(struct token **where, struct token *list, int *count)
465 int need_copy = --*count;
466 while (!eof_token(list)) {
467 struct token *token;
468 if (need_copy)
469 token = dup_token(list, &list->pos, &list->pos);
470 else
471 token = list;
472 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
473 token->pos.noexpand = 1;
474 *where = token;
475 where = &token->next;
476 list = list->next;
478 *where = &eof_token_entry;
479 return where;
482 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
484 struct token *token = *list;
485 struct position *base_pos = &token->pos;
486 struct position *pos = base_pos;
487 int *count;
488 enum {Normal, Placeholder, Concat} state = Normal;
490 for (; !eof_token(body); body = body->next, pos = &body->pos) {
491 struct token *added, *arg;
492 struct token **tail;
494 switch (token_type(body)) {
495 case TOKEN_GNU_KLUDGE:
497 * GNU kludge: if we had <comma>##<vararg>, behaviour
498 * depends on whether we had enough arguments to have
499 * a vararg. If we did, ## is just ignored. Otherwise
500 * both , and ## are ignored. Comma should come from
501 * the body of macro and not be an argument of earlier
502 * concatenation.
504 if (!args[body->next->argnum].arg)
505 continue;
506 added = dup_token(body, base_pos, pos);
507 token_type(added) = TOKEN_SPECIAL;
508 tail = &added->next;
509 break;
511 case TOKEN_STR_ARGUMENT:
512 arg = args[body->argnum].str;
513 count = &args[body->argnum].n_str;
514 goto copy_arg;
516 case TOKEN_QUOTED_ARGUMENT:
517 arg = args[body->argnum].arg;
518 count = &args[body->argnum].n_quoted;
519 if (!arg || eof_token(arg)) {
520 if (state == Concat)
521 state = Normal;
522 else
523 state = Placeholder;
524 continue;
526 goto copy_arg;
528 case TOKEN_MACRO_ARGUMENT:
529 arg = args[body->argnum].expanded;
530 count = &args[body->argnum].n_normal;
531 if (eof_token(arg)) {
532 state = Normal;
533 continue;
535 copy_arg:
536 tail = copy(&added, arg, count);
537 added->pos.newline = pos->newline;
538 added->pos.whitespace = pos->whitespace;
539 break;
541 case TOKEN_CONCAT:
542 if (state == Placeholder)
543 state = Normal;
544 else
545 state = Concat;
546 continue;
548 case TOKEN_IDENT:
549 added = dup_token(body, base_pos, pos);
550 if (added->ident->tainted)
551 added->pos.noexpand = 1;
552 tail = &added->next;
553 break;
555 default:
556 added = dup_token(body, base_pos, pos);
557 tail = &added->next;
558 break;
562 * if we got to doing real concatenation, we already have
563 * added something into the list, so containing_token() is OK.
565 if (state == Concat && merge(containing_token(list), added)) {
566 *list = added->next;
567 if (tail != &added->next)
568 list = tail;
569 } else {
570 *list = added;
571 list = tail;
573 state = Normal;
575 *list = &eof_token_entry;
576 return list;
579 static int expand(struct token **list, struct symbol *sym)
581 struct token *last;
582 struct token *token = *list;
583 struct ident *expanding = token->ident;
584 struct token **tail;
585 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
586 struct arg args[nargs];
588 if (expanding->tainted) {
589 token->pos.noexpand = 1;
590 return 1;
593 if (sym->arglist) {
594 if (!match_op(scan_next(&token->next), '('))
595 return 1;
596 if (!collect_arguments(token->next, sym->arglist, args, token))
597 return 1;
598 expand_arguments(nargs, args);
601 expanding->tainted = 1;
603 last = token->next;
604 tail = substitute(list, sym->expansion, args);
605 *tail = last;
607 return 0;
610 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
612 struct token *last;
613 static char buffer[256];
614 char *ptr = buffer;
616 last = token;
617 while (!eof_token(token) && !match_op(token, endop)) {
618 int len;
619 const char *val = token->string->data;
620 if (token_type(token) != TOKEN_STRING)
621 val = show_token(token);
622 len = strlen(val);
623 memcpy(ptr, val, len);
624 ptr += len;
625 token = token->next;
627 *ptr = 0;
628 if (endop && !match_op(token, endop))
629 sparse_error(start->pos, "expected '>' at end of filename");
630 return buffer;
633 static int already_tokenized(const char *path)
635 int i;
636 struct stream *s = input_streams;
638 for (i = input_stream_nr; --i >= 0; s++) {
639 if (s->constant != CONSTANT_FILE_YES)
640 continue;
641 if (strcmp(path, s->name))
642 continue;
643 if (s->protect && !lookup_macro(s->protect))
644 continue;
645 return 1;
647 return 0;
650 /* Handle include of header files.
651 * The relevant options are made compatible with gcc. The only options that
652 * are not supported is -withprefix and friends.
654 * Three set of include paths are known:
655 * quote_includepath: Path to search when using #include "file.h"
656 * angle_includepath: Paths to search when using #include <file.h>
657 * isys_includepath: Paths specified with -isystem, come before the
658 * built-in system include paths. Gcc would suppress
659 * warnings from system headers. Here we separate
660 * them from the angle_ ones to keep search ordering.
662 * sys_includepath: Built-in include paths.
663 * dirafter_includepath Paths added with -dirafter.
665 * The above is implemented as one array with pointers
666 * +--------------+
667 * quote_includepath ---> | |
668 * +--------------+
669 * | |
670 * +--------------+
671 * angle_includepath ---> | |
672 * +--------------+
673 * isys_includepath ---> | |
674 * +--------------+
675 * sys_includepath ---> | |
676 * +--------------+
677 * dirafter_includepath -> | |
678 * +--------------+
680 * -I dir insert dir just before isys_includepath and move the rest
681 * -I- makes all dirs specified with -I before to quote dirs only and
682 * angle_includepath is set equal to isys_includepath.
683 * -nostdinc removes all sys dirs by storing NULL in entry pointed
684 * to by * sys_includepath. Note that this will reset all dirs built-in
685 * and added before -nostdinc by -isystem and -idirafter.
686 * -isystem dir adds dir where isys_includepath points adding this dir as
687 * first systemdir
688 * -idirafter dir adds dir to the end of the list
691 static void set_stream_include_path(struct stream *stream)
693 const char *path = stream->path;
694 if (!path) {
695 const char *p = strrchr(stream->name, '/');
696 path = "";
697 if (p) {
698 int len = p - stream->name + 1;
699 char *m = malloc(len+1);
700 /* This includes the final "/" */
701 memcpy(m, stream->name, len);
702 m[len] = 0;
703 path = m;
705 stream->path = path;
707 includepath[0] = path;
710 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
712 int fd;
713 int plen = strlen(path);
714 static char fullname[PATH_MAX];
716 memcpy(fullname, path, plen);
717 if (plen && path[plen-1] != '/') {
718 fullname[plen] = '/';
719 plen++;
721 memcpy(fullname+plen, filename, flen);
722 if (already_tokenized(fullname))
723 return 1;
724 fd = open(fullname, O_RDONLY);
725 if (fd >= 0) {
726 char * streamname = __alloc_bytes(plen + flen);
727 memcpy(streamname, fullname, plen + flen);
728 *where = tokenize(streamname, fd, *where, next_path);
729 close(fd);
730 return 1;
732 return 0;
735 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
737 const char *path;
739 while ((path = *pptr++) != NULL) {
740 if (!try_include(path, filename, flen, list, pptr))
741 continue;
742 return 1;
744 return 0;
747 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
749 int flen = strlen(filename) + 1;
751 /* Absolute path? */
752 if (filename[0] == '/') {
753 if (try_include("", filename, flen, list, includepath))
754 return;
755 goto out;
758 /* Dir of input file is first dir to search for quoted includes */
759 set_stream_include_path(stream);
761 if (!path)
762 /* Do not search quote include if <> is in use */
763 path = local ? quote_includepath : angle_includepath;
765 /* Check the standard include paths.. */
766 if (do_include_path(path, list, token, filename, flen))
767 return;
768 out:
769 error_die(token->pos, "unable to open '%s'", filename);
772 static int free_preprocessor_line(struct token *token)
774 while (token_type(token) != TOKEN_EOF) {
775 struct token *free = token;
776 token = token->next;
777 __free_token(free);
779 return 1;
782 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
784 const char *filename;
785 struct token *next;
786 int expect;
788 next = token->next;
789 expect = '>';
790 if (!match_op(next, '<')) {
791 expand_list(&token->next);
792 expect = 0;
793 next = token;
794 if (match_op(token->next, '<')) {
795 next = token->next;
796 expect = '>';
799 token = next->next;
800 filename = token_name_sequence(token, expect, token);
801 do_include(!expect, stream, list, token, filename, path);
802 return 0;
805 static int handle_include(struct stream *stream, struct token **list, struct token *token)
807 return handle_include_path(stream, list, token, NULL);
810 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
812 return handle_include_path(stream, list, token, stream->next_path);
815 static int token_different(struct token *t1, struct token *t2)
817 int different;
819 if (token_type(t1) != token_type(t2))
820 return 1;
822 switch (token_type(t1)) {
823 case TOKEN_IDENT:
824 different = t1->ident != t2->ident;
825 break;
826 case TOKEN_ARG_COUNT:
827 case TOKEN_UNTAINT:
828 case TOKEN_CONCAT:
829 case TOKEN_GNU_KLUDGE:
830 different = 0;
831 break;
832 case TOKEN_NUMBER:
833 different = strcmp(t1->number, t2->number);
834 break;
835 case TOKEN_SPECIAL:
836 different = t1->special != t2->special;
837 break;
838 case TOKEN_MACRO_ARGUMENT:
839 case TOKEN_QUOTED_ARGUMENT:
840 case TOKEN_STR_ARGUMENT:
841 different = t1->argnum != t2->argnum;
842 break;
843 case TOKEN_CHAR:
844 different = t1->character != t2->character;
845 break;
846 case TOKEN_STRING: {
847 struct string *s1, *s2;
849 s1 = t1->string;
850 s2 = t2->string;
851 different = 1;
852 if (s1->length != s2->length)
853 break;
854 different = memcmp(s1->data, s2->data, s1->length);
855 break;
857 default:
858 different = 1;
859 break;
861 return different;
864 static int token_list_different(struct token *list1, struct token *list2)
866 for (;;) {
867 if (list1 == list2)
868 return 0;
869 if (!list1 || !list2)
870 return 1;
871 if (token_different(list1, list2))
872 return 1;
873 list1 = list1->next;
874 list2 = list2->next;
878 static inline void set_arg_count(struct token *token)
880 token_type(token) = TOKEN_ARG_COUNT;
881 token->count.normal = token->count.quoted =
882 token->count.str = token->count.vararg = 0;
885 static struct token *parse_arguments(struct token *list)
887 struct token *arg = list->next, *next = list;
888 struct argcount *count = &list->count;
890 set_arg_count(list);
892 if (match_op(arg, ')')) {
893 next = arg->next;
894 list->next = &eof_token_entry;
895 return next;
898 while (token_type(arg) == TOKEN_IDENT) {
899 if (arg->ident == &__VA_ARGS___ident)
900 goto Eva_args;
901 if (!++count->normal)
902 goto Eargs;
903 next = arg->next;
905 if (match_op(next, ',')) {
906 set_arg_count(next);
907 arg = next->next;
908 continue;
911 if (match_op(next, ')')) {
912 set_arg_count(next);
913 next = next->next;
914 arg->next->next = &eof_token_entry;
915 return next;
918 /* normal cases are finished here */
920 if (match_op(next, SPECIAL_ELLIPSIS)) {
921 if (match_op(next->next, ')')) {
922 set_arg_count(next);
923 next->count.vararg = 1;
924 next = next->next;
925 arg->next->next = &eof_token_entry;
926 return next->next;
929 arg = next;
930 goto Enotclosed;
933 if (eof_token(next)) {
934 goto Enotclosed;
935 } else {
936 arg = next;
937 goto Ebadstuff;
941 if (match_op(arg, SPECIAL_ELLIPSIS)) {
942 next = arg->next;
943 token_type(arg) = TOKEN_IDENT;
944 arg->ident = &__VA_ARGS___ident;
945 if (!match_op(next, ')'))
946 goto Enotclosed;
947 if (!++count->normal)
948 goto Eargs;
949 set_arg_count(next);
950 next->count.vararg = 1;
951 next = next->next;
952 arg->next->next = &eof_token_entry;
953 return next;
956 if (eof_token(arg)) {
957 arg = next;
958 goto Enotclosed;
960 if (match_op(arg, ','))
961 goto Emissing;
962 else
963 goto Ebadstuff;
966 Emissing:
967 sparse_error(arg->pos, "parameter name missing");
968 return NULL;
969 Ebadstuff:
970 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
971 show_token(arg));
972 return NULL;
973 Enotclosed:
974 sparse_error(arg->pos, "missing ')' in macro parameter list");
975 return NULL;
976 Eva_args:
977 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
978 return NULL;
979 Eargs:
980 sparse_error(arg->pos, "too many arguments in macro definition");
981 return NULL;
984 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
986 struct ident *ident = token->ident;
987 int nr;
989 if (!arglist || token_type(token) != TOKEN_IDENT)
990 return 0;
992 arglist = arglist->next;
994 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
995 if (arglist->ident == ident) {
996 struct argcount *count = &arglist->next->count;
997 int n;
999 token->argnum = nr;
1000 token_type(token) = type;
1001 switch (type) {
1002 case TOKEN_MACRO_ARGUMENT:
1003 n = ++count->normal;
1004 break;
1005 case TOKEN_QUOTED_ARGUMENT:
1006 n = ++count->quoted;
1007 break;
1008 default:
1009 n = ++count->str;
1011 if (n)
1012 return count->vararg ? 2 : 1;
1013 token_type(token) = TOKEN_ERROR;
1014 return -1;
1017 return 0;
1020 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1022 struct token *token = expansion;
1023 struct token **p;
1024 struct token *last = NULL;
1026 if (match_op(token, SPECIAL_HASHHASH))
1027 goto Econcat;
1029 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1030 if (match_op(token, '#')) {
1031 if (arglist) {
1032 struct token *next = token->next;
1033 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1034 goto Equote;
1035 next->pos.whitespace = token->pos.whitespace;
1036 token = *p = next;
1037 } else {
1038 token->pos.noexpand = 1;
1040 } else if (match_op(token, SPECIAL_HASHHASH)) {
1041 struct token *next = token->next;
1042 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
1043 token_type(token) = TOKEN_CONCAT;
1044 if (arg) {
1045 token = next;
1046 /* GNU kludge */
1047 if (arg == 2 && last && match_op(last, ',')) {
1048 token_type(last) = TOKEN_GNU_KLUDGE;
1049 last->next = token;
1051 } else if (match_op(next, SPECIAL_HASHHASH))
1052 token = next;
1053 else if (eof_token(next))
1054 goto Econcat;
1055 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
1056 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1057 } else {
1058 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1060 if (token_type(token) == TOKEN_ERROR)
1061 goto Earg;
1062 last = token;
1064 token = alloc_token(&expansion->pos);
1065 token_type(token) = TOKEN_UNTAINT;
1066 token->ident = name;
1067 token->next = *p;
1068 *p = token;
1069 return expansion;
1071 Equote:
1072 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1073 return NULL;
1075 Econcat:
1076 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1077 return NULL;
1078 Earg:
1079 sparse_error(token->pos, "too many instances of argument in body");
1080 return NULL;
1083 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1085 struct token *arglist, *expansion;
1086 struct token *left = token->next;
1087 struct symbol *sym;
1088 struct ident *name;
1089 int ret;
1091 if (token_type(left) != TOKEN_IDENT) {
1092 sparse_error(token->pos, "expected identifier to 'define'");
1093 return 1;
1096 name = left->ident;
1098 arglist = NULL;
1099 expansion = left->next;
1100 if (!expansion->pos.whitespace) {
1101 if (match_op(expansion, '(')) {
1102 arglist = expansion;
1103 expansion = parse_arguments(expansion);
1104 if (!expansion)
1105 return 1;
1106 } else if (!eof_token(expansion)) {
1107 warning(expansion->pos,
1108 "no whitespace before object-like macro body");
1112 expansion = parse_expansion(expansion, arglist, name);
1113 if (!expansion)
1114 return 1;
1116 ret = 1;
1117 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1118 if (sym) {
1119 int clean;
1121 if (attr < sym->attr)
1122 goto out;
1124 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1126 if (token_list_different(sym->expansion, expansion) ||
1127 token_list_different(sym->arglist, arglist)) {
1128 ret = 0;
1129 if ((clean && attr == SYM_ATTR_NORMAL)
1130 || sym->used_in == file_scope) {
1131 warning(left->pos, "preprocessor token %.*s redefined",
1132 name->len, name->name);
1133 info(sym->pos, "this was the original definition");
1135 } else if (clean)
1136 goto out;
1139 if (!sym || sym->scope != file_scope) {
1140 sym = alloc_symbol(left->pos, SYM_NODE);
1141 bind_symbol(sym, name, NS_MACRO);
1142 ret = 0;
1145 if (!ret) {
1146 sym->expansion = expansion;
1147 sym->arglist = arglist;
1148 __free_token(token); /* Free the "define" token, but not the rest of the line */
1151 sym->namespace = NS_MACRO;
1152 sym->used_in = NULL;
1153 sym->attr = attr;
1154 out:
1155 return ret;
1158 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1160 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1163 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1165 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1168 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1170 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1173 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1175 struct token *left = token->next;
1176 struct symbol *sym;
1178 if (token_type(left) != TOKEN_IDENT) {
1179 sparse_error(token->pos, "expected identifier to 'undef'");
1180 return 1;
1183 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1184 if (sym) {
1185 if (attr < sym->attr)
1186 return 1;
1187 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1188 return 1;
1189 } else if (attr <= SYM_ATTR_NORMAL)
1190 return 1;
1192 if (!sym || sym->scope != file_scope) {
1193 sym = alloc_symbol(left->pos, SYM_NODE);
1194 bind_symbol(sym, left->ident, NS_MACRO);
1197 sym->namespace = NS_UNDEF;
1198 sym->used_in = NULL;
1199 sym->attr = attr;
1201 return 1;
1204 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1206 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1209 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1211 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1214 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1216 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1217 free_preprocessor_line(token->next);
1218 token->next = stream->top_if;
1219 stream->top_if = token;
1220 if (false_nesting || true != 1)
1221 false_nesting++;
1222 return 0;
1225 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1227 struct token *next = token->next;
1228 int arg;
1229 if (token_type(next) == TOKEN_IDENT) {
1230 arg = token_defined(next);
1231 } else {
1232 dirty_stream(stream);
1233 if (!false_nesting)
1234 sparse_error(token->pos, "expected preprocessor identifier");
1235 arg = -1;
1237 return preprocessor_if(stream, token, arg);
1240 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1242 struct token *next = token->next;
1243 int arg;
1244 if (token_type(next) == TOKEN_IDENT) {
1245 if (!stream->dirty && !stream->ifndef) {
1246 if (!stream->protect) {
1247 stream->ifndef = token;
1248 stream->protect = next->ident;
1249 } else if (stream->protect == next->ident) {
1250 stream->ifndef = token;
1251 stream->dirty = 1;
1254 arg = !token_defined(next);
1255 } else {
1256 dirty_stream(stream);
1257 if (!false_nesting)
1258 sparse_error(token->pos, "expected preprocessor identifier");
1259 arg = -1;
1262 return preprocessor_if(stream, token, arg);
1266 * Expression handling for #if and #elif; it differs from normal expansion
1267 * due to special treatment of "defined".
1269 static int expression_value(struct token **where)
1271 struct expression *expr;
1272 struct token *p;
1273 struct token **list = where, **beginning = NULL;
1274 long long value;
1275 int state = 0;
1277 while (!eof_token(p = scan_next(list))) {
1278 switch (state) {
1279 case 0:
1280 if (token_type(p) != TOKEN_IDENT)
1281 break;
1282 if (p->ident == &defined_ident) {
1283 state = 1;
1284 beginning = list;
1285 break;
1287 if (!expand_one_symbol(list))
1288 continue;
1289 if (token_type(p) != TOKEN_IDENT)
1290 break;
1291 token_type(p) = TOKEN_ZERO_IDENT;
1292 break;
1293 case 1:
1294 if (match_op(p, '(')) {
1295 state = 2;
1296 } else {
1297 state = 0;
1298 replace_with_defined(p);
1299 *beginning = p;
1301 break;
1302 case 2:
1303 if (token_type(p) == TOKEN_IDENT)
1304 state = 3;
1305 else
1306 state = 0;
1307 replace_with_defined(p);
1308 *beginning = p;
1309 break;
1310 case 3:
1311 state = 0;
1312 if (!match_op(p, ')'))
1313 sparse_error(p->pos, "missing ')' after \"defined\"");
1314 *list = p->next;
1315 continue;
1317 list = &p->next;
1320 p = constant_expression(*where, &expr);
1321 if (!eof_token(p))
1322 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p));
1323 value = get_expression_value(expr);
1324 return value != 0;
1327 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1329 int value = 0;
1330 if (!false_nesting)
1331 value = expression_value(&token->next);
1333 dirty_stream(stream);
1334 return preprocessor_if(stream, token, value);
1337 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1339 struct token *top_if = stream->top_if;
1340 end_group(stream);
1342 if (!top_if) {
1343 nesting_error(stream);
1344 sparse_error(token->pos, "unmatched #elif within stream");
1345 return 1;
1348 if (token_type(top_if) == TOKEN_ELSE) {
1349 nesting_error(stream);
1350 sparse_error(token->pos, "#elif after #else");
1351 if (!false_nesting)
1352 false_nesting = 1;
1353 return 1;
1356 dirty_stream(stream);
1357 if (token_type(top_if) != TOKEN_IF)
1358 return 1;
1359 if (false_nesting) {
1360 if (expression_value(&token->next))
1361 false_nesting = 0;
1362 } else {
1363 false_nesting = 1;
1364 token_type(top_if) = TOKEN_SKIP_GROUPS;
1366 return 1;
1369 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1371 struct token *top_if = stream->top_if;
1372 end_group(stream);
1374 if (!top_if) {
1375 nesting_error(stream);
1376 sparse_error(token->pos, "unmatched #else within stream");
1377 return 1;
1380 if (token_type(top_if) == TOKEN_ELSE) {
1381 nesting_error(stream);
1382 sparse_error(token->pos, "#else after #else");
1384 if (false_nesting) {
1385 if (token_type(top_if) == TOKEN_IF)
1386 false_nesting = 0;
1387 } else {
1388 false_nesting = 1;
1390 token_type(top_if) = TOKEN_ELSE;
1391 return 1;
1394 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1396 struct token *top_if = stream->top_if;
1397 end_group(stream);
1398 if (!top_if) {
1399 nesting_error(stream);
1400 sparse_error(token->pos, "unmatched #endif in stream");
1401 return 1;
1403 if (false_nesting)
1404 false_nesting--;
1405 stream->top_if = top_if->next;
1406 __free_token(top_if);
1407 return 1;
1410 static const char *show_token_sequence(struct token *token)
1412 static char buffer[1024];
1413 char *ptr = buffer;
1414 int whitespace = 0;
1416 if (!token)
1417 return "<none>";
1418 while (!eof_token(token)) {
1419 const char *val = show_token(token);
1420 int len = strlen(val);
1422 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1423 sparse_error(token->pos, "too long token expansion");
1424 break;
1427 if (whitespace)
1428 *ptr++ = ' ';
1429 memcpy(ptr, val, len);
1430 ptr += len;
1431 token = token->next;
1432 whitespace = token->pos.whitespace;
1434 *ptr = 0;
1435 return buffer;
1438 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1440 warning(token->pos, "%s", show_token_sequence(token->next));
1441 return 1;
1444 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1446 sparse_error(token->pos, "%s", show_token_sequence(token->next));
1447 return 1;
1450 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1453 * Do we have any non-system includes?
1454 * Clear them out if so..
1456 *sys_includepath = NULL;
1457 return 1;
1460 static inline void update_inc_ptrs(const char ***where)
1463 if (*where <= dirafter_includepath) {
1464 dirafter_includepath++;
1465 /* If this was the entry that we prepend, don't
1466 * rise the lower entries, even if they are at
1467 * the same level. */
1468 if (where == &dirafter_includepath)
1469 return;
1471 if (*where <= sys_includepath) {
1472 sys_includepath++;
1473 if (where == &sys_includepath)
1474 return;
1476 if (*where <= isys_includepath) {
1477 isys_includepath++;
1478 if (where == &isys_includepath)
1479 return;
1482 /* angle_includepath is actually never updated, since we
1483 * don't suppport -iquote rught now. May change some day. */
1484 if (*where <= angle_includepath) {
1485 angle_includepath++;
1486 if (where == &angle_includepath)
1487 return;
1491 /* Add a path before 'where' and update the pointers associated with the
1492 * includepath array */
1493 static void add_path_entry(struct token *token, const char *path,
1494 const char ***where)
1496 const char **dst;
1497 const char *next;
1499 /* Need one free entry.. */
1500 if (includepath[INCLUDEPATHS-2])
1501 error_die(token->pos, "too many include path entries");
1503 /* check that this is not a duplicate */
1504 dst = includepath;
1505 while (*dst) {
1506 if (strcmp(*dst, path) == 0)
1507 return;
1508 dst++;
1510 next = path;
1511 dst = *where;
1513 update_inc_ptrs(where);
1516 * Move them all up starting at dst,
1517 * insert the new entry..
1519 do {
1520 const char *tmp = *dst;
1521 *dst = next;
1522 next = tmp;
1523 dst++;
1524 } while (next);
1527 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1529 for (;;) {
1530 token = token->next;
1531 if (eof_token(token))
1532 return 1;
1533 if (token_type(token) != TOKEN_STRING) {
1534 warning(token->pos, "expected path string");
1535 return 1;
1537 add_path_entry(token, token->string->data, &isys_includepath);
1541 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1543 for (;;) {
1544 token = token->next;
1545 if (eof_token(token))
1546 return 1;
1547 if (token_type(token) != TOKEN_STRING) {
1548 sparse_error(token->pos, "expected path string");
1549 return 1;
1551 add_path_entry(token, token->string->data, &sys_includepath);
1555 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1557 for (;;) {
1558 token = token->next;
1559 if (eof_token(token))
1560 return 1;
1561 if (token_type(token) != TOKEN_STRING) {
1562 sparse_error(token->pos, "expected path string");
1563 return 1;
1565 add_path_entry(token, token->string->data, &dirafter_includepath);
1569 /* Add to end on includepath list - no pointer updates */
1570 static void add_dirafter_entry(struct token *token, const char *path)
1572 const char **dst = includepath;
1574 /* Need one free entry.. */
1575 if (includepath[INCLUDEPATHS-2])
1576 error_die(token->pos, "too many include path entries");
1578 /* Add to the end */
1579 while (*dst)
1580 dst++;
1581 *dst = path;
1582 dst++;
1583 *dst = NULL;
1586 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1588 for (;;) {
1589 token = token->next;
1590 if (eof_token(token))
1591 return 1;
1592 if (token_type(token) != TOKEN_STRING) {
1593 sparse_error(token->pos, "expected path string");
1594 return 1;
1596 add_dirafter_entry(token, token->string->data);
1600 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1603 * -I-
1604 * From info gcc:
1605 * Split the include path. Any directories specified with `-I'
1606 * options before `-I-' are searched only for headers requested with
1607 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1608 * If additional directories are specified with `-I' options after
1609 * the `-I-', those directories are searched for all `#include'
1610 * directives.
1611 * In addition, `-I-' inhibits the use of the directory of the current
1612 * file directory as the first search directory for `#include "FILE"'.
1614 quote_includepath = includepath+1;
1615 angle_includepath = sys_includepath;
1616 return 1;
1620 * We replace "#pragma xxx" with "__pragma__" in the token
1621 * stream. Just as an example.
1623 * We'll just #define that away for now, but the theory here
1624 * is that we can use this to insert arbitrary token sequences
1625 * to turn the pragmas into internal front-end sequences for
1626 * when we actually start caring about them.
1628 * So eventually this will turn into some kind of extended
1629 * __attribute__() like thing, except called __pragma__(xxx).
1631 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1633 struct token *next = *line;
1635 token->ident = &pragma_ident;
1636 token->pos.newline = 1;
1637 token->pos.whitespace = 1;
1638 token->pos.pos = 1;
1639 *line = token;
1640 token->next = next;
1641 return 0;
1645 * We ignore #line for now.
1647 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1649 return 1;
1652 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1654 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1655 return 1;
1659 static void init_preprocessor(void)
1661 int i;
1662 int stream = init_stream("preprocessor", -1, includepath);
1663 static struct {
1664 const char *name;
1665 int (*handler)(struct stream *, struct token **, struct token *);
1666 } normal[] = {
1667 { "define", handle_define },
1668 { "weak_define", handle_weak_define },
1669 { "strong_define", handle_strong_define },
1670 { "undef", handle_undef },
1671 { "strong_undef", handle_strong_undef },
1672 { "warning", handle_warning },
1673 { "error", handle_error },
1674 { "include", handle_include },
1675 { "include_next", handle_include_next },
1676 { "pragma", handle_pragma },
1677 { "line", handle_line },
1679 // our internal preprocessor tokens
1680 { "nostdinc", handle_nostdinc },
1681 { "add_include", handle_add_include },
1682 { "add_isystem", handle_add_isystem },
1683 { "add_system", handle_add_system },
1684 { "add_dirafter", handle_add_dirafter },
1685 { "split_include", handle_split_include },
1686 }, special[] = {
1687 { "ifdef", handle_ifdef },
1688 { "ifndef", handle_ifndef },
1689 { "else", handle_else },
1690 { "endif", handle_endif },
1691 { "if", handle_if },
1692 { "elif", handle_elif },
1695 for (i = 0; i < (sizeof (normal) / sizeof (normal[0])); i++) {
1696 struct symbol *sym;
1697 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1698 sym->handler = normal[i].handler;
1699 sym->normal = 1;
1701 for (i = 0; i < (sizeof (special) / sizeof (special[0])); i++) {
1702 struct symbol *sym;
1703 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1704 sym->handler = special[i].handler;
1705 sym->normal = 0;
1710 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1712 int (*handler)(struct stream *, struct token **, struct token *);
1713 struct token *token = start->next;
1714 int is_normal = 1;
1716 if (eof_token(token))
1717 return;
1719 if (token_type(token) == TOKEN_IDENT) {
1720 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1721 if (sym) {
1722 handler = sym->handler;
1723 is_normal = sym->normal;
1724 } else {
1725 handler = handle_nondirective;
1727 } else if (token_type(token) == TOKEN_NUMBER) {
1728 handler = handle_line;
1729 } else {
1730 handler = handle_nondirective;
1733 if (is_normal) {
1734 dirty_stream(stream);
1735 if (false_nesting)
1736 goto out;
1738 if (!handler(stream, line, token)) /* all set */
1739 return;
1741 out:
1742 free_preprocessor_line(token);
1745 static void preprocessor_line(struct stream *stream, struct token **line)
1747 struct token *start = *line, *next;
1748 struct token **tp = &start->next;
1750 for (;;) {
1751 next = *tp;
1752 if (next->pos.newline)
1753 break;
1754 tp = &next->next;
1756 *line = next;
1757 *tp = &eof_token_entry;
1758 handle_preprocessor_line(stream, line, start);
1761 static void do_preprocess(struct token **list)
1763 struct token *next;
1765 while (!eof_token(next = scan_next(list))) {
1766 struct stream *stream = input_streams + next->pos.stream;
1768 if (next->pos.newline && match_op(next, '#')) {
1769 if (!next->pos.noexpand) {
1770 preprocessor_line(stream, list);
1771 __free_token(next); /* Free the '#' token */
1772 continue;
1776 switch (token_type(next)) {
1777 case TOKEN_STREAMEND:
1778 if (stream->top_if) {
1779 nesting_error(stream);
1780 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1781 stream->top_if = NULL;
1782 false_nesting = 0;
1784 if (!stream->dirty)
1785 stream->constant = CONSTANT_FILE_YES;
1786 *list = next->next;
1787 continue;
1788 case TOKEN_STREAMBEGIN:
1789 *list = next->next;
1790 continue;
1792 default:
1793 dirty_stream(stream);
1794 if (false_nesting) {
1795 *list = next->next;
1796 __free_token(next);
1797 continue;
1800 if (token_type(next) != TOKEN_IDENT ||
1801 expand_one_symbol(list))
1802 list = &next->next;
1807 struct token * preprocess(struct token *token)
1809 preprocessing = 1;
1810 init_preprocessor();
1811 do_preprocess(&token);
1813 // Drop all expressions from preprocessing, they're not used any more.
1814 // This is not true when we have multiple files, though ;/
1815 // clear_expression_alloc();
1816 preprocessing = 0;
1818 return token;