*new* check_macros: find macro precedence bugs
[smatch.git] / pre-process.c
blobdb26eb111a670bbc7d6c71176a98f0bb780700ff
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 store_macro_pos(token);
154 sym->used_in = file_scope;
155 return expand(list, sym);
157 if (token->ident == &__LINE___ident) {
158 replace_with_integer(token, token->pos.line);
159 } else if (token->ident == &__FILE___ident) {
160 replace_with_string(token, stream_name(token->pos.stream));
161 } else if (token->ident == &__DATE___ident) {
162 if (!t)
163 time(&t);
164 strftime(buffer, 12, "%b %e %Y", localtime(&t));
165 replace_with_string(token, buffer);
166 } else if (token->ident == &__TIME___ident) {
167 if (!t)
168 time(&t);
169 strftime(buffer, 9, "%T", localtime(&t));
170 replace_with_string(token, buffer);
172 return 1;
175 static inline struct token *scan_next(struct token **where)
177 struct token *token = *where;
178 if (token_type(token) != TOKEN_UNTAINT)
179 return token;
180 do {
181 token->ident->tainted = 0;
182 token = token->next;
183 } while (token_type(token) == TOKEN_UNTAINT);
184 *where = token;
185 return token;
188 static void expand_list(struct token **list)
190 struct token *next;
191 while (!eof_token(next = scan_next(list))) {
192 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
193 list = &next->next;
197 static void preprocessor_line(struct stream *stream, struct token **line);
199 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
201 struct stream *stream = input_streams + prev->pos.stream;
202 struct token **p = &prev->next;
203 struct token *next;
204 int nesting = 0;
206 while (!eof_token(next = scan_next(p))) {
207 if (next->pos.newline && match_op(next, '#')) {
208 if (!next->pos.noexpand) {
209 sparse_error(next->pos,
210 "directive in argument list");
211 preprocessor_line(stream, p);
212 __free_token(next); /* Free the '#' token */
213 continue;
216 switch (token_type(next)) {
217 case TOKEN_STREAMEND:
218 case TOKEN_STREAMBEGIN:
219 *p = &eof_token_entry;
220 return next;
222 if (false_nesting) {
223 *p = next->next;
224 __free_token(next);
225 continue;
227 if (match_op(next, '(')) {
228 nesting++;
229 } else if (match_op(next, ')')) {
230 if (!nesting--)
231 break;
232 } else if (match_op(next, ',') && !nesting && !vararg) {
233 break;
235 next->pos.stream = pos->stream;
236 next->pos.line = pos->line;
237 next->pos.pos = pos->pos;
238 p = &next->next;
240 *p = &eof_token_entry;
241 return next;
245 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
248 struct arg {
249 struct token *arg;
250 struct token *expanded;
251 struct token *str;
252 int n_normal;
253 int n_quoted;
254 int n_str;
257 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
259 int wanted = arglist->count.normal;
260 struct token *next = NULL;
261 int count = 0;
263 arglist = arglist->next; /* skip counter */
265 if (!wanted) {
266 next = collect_arg(start, 0, &what->pos);
267 if (eof_token(next))
268 goto Eclosing;
269 if (!eof_token(start->next) || !match_op(next, ')')) {
270 count++;
271 goto Emany;
273 } else {
274 for (count = 0; count < wanted; count++) {
275 struct argcount *p = &arglist->next->count;
276 next = collect_arg(start, p->vararg, &what->pos);
277 arglist = arglist->next->next;
278 if (eof_token(next))
279 goto Eclosing;
280 args[count].arg = start->next;
281 args[count].n_normal = p->normal;
282 args[count].n_quoted = p->quoted;
283 args[count].n_str = p->str;
284 if (match_op(next, ')')) {
285 count++;
286 break;
288 start = next;
290 if (count == wanted && !match_op(next, ')'))
291 goto Emany;
292 if (count == wanted - 1) {
293 struct argcount *p = &arglist->next->count;
294 if (!p->vararg)
295 goto Efew;
296 args[count].arg = NULL;
297 args[count].n_normal = p->normal;
298 args[count].n_quoted = p->quoted;
299 args[count].n_str = p->str;
301 if (count < wanted - 1)
302 goto Efew;
304 what->next = next->next;
305 return 1;
307 Efew:
308 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
309 show_token(what), wanted, count);
310 goto out;
311 Emany:
312 while (match_op(next, ',')) {
313 next = collect_arg(next, 0, &what->pos);
314 count++;
316 if (eof_token(next))
317 goto Eclosing;
318 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
319 show_token(what), count, wanted);
320 goto out;
321 Eclosing:
322 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
323 show_token(what));
324 out:
325 what->next = next->next;
326 return 0;
329 static struct token *dup_list(struct token *list)
331 struct token *res = NULL;
332 struct token **p = &res;
334 while (!eof_token(list)) {
335 struct token *newtok = __alloc_token(0);
336 *newtok = *list;
337 *p = newtok;
338 p = &newtok->next;
339 list = list->next;
341 return res;
344 static struct token *stringify(struct token *arg)
346 const char *s = show_token_sequence(arg);
347 int size = strlen(s)+1;
348 struct token *token = __alloc_token(0);
349 struct string *string = __alloc_string(size);
351 memcpy(string->data, s, size);
352 string->length = size;
353 token->pos = arg->pos;
354 token_type(token) = TOKEN_STRING;
355 token->string = string;
356 token->next = &eof_token_entry;
357 return token;
360 static void expand_arguments(int count, struct arg *args)
362 int i;
363 for (i = 0; i < count; i++) {
364 struct token *arg = args[i].arg;
365 if (!arg)
366 arg = &eof_token_entry;
367 if (args[i].n_str)
368 args[i].str = stringify(arg);
369 if (args[i].n_normal) {
370 if (!args[i].n_quoted) {
371 args[i].expanded = arg;
372 args[i].arg = NULL;
373 } else if (eof_token(arg)) {
374 args[i].expanded = arg;
375 } else {
376 args[i].expanded = dup_list(arg);
378 expand_list(&args[i].expanded);
384 * Possibly valid combinations:
385 * - ident + ident -> ident
386 * - ident + number -> ident unless number contains '.', '+' or '-'.
387 * - number + number -> number
388 * - number + ident -> number
389 * - number + '.' -> number
390 * - number + '+' or '-' -> number, if number used to end on [eEpP].
391 * - '.' + number -> number, if number used to start with a digit.
392 * - special + special -> either special or an error.
394 static enum token_type combine(struct token *left, struct token *right, char *p)
396 int len;
397 enum token_type t1 = token_type(left), t2 = token_type(right);
399 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
400 return TOKEN_ERROR;
402 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
403 return TOKEN_ERROR;
405 strcpy(p, show_token(left));
406 strcat(p, show_token(right));
407 len = strlen(p);
409 if (len >= 256)
410 return TOKEN_ERROR;
412 if (t1 == TOKEN_IDENT) {
413 if (t2 == TOKEN_SPECIAL)
414 return TOKEN_ERROR;
415 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
416 return TOKEN_ERROR;
417 return TOKEN_IDENT;
420 if (t1 == TOKEN_NUMBER) {
421 if (t2 == TOKEN_SPECIAL) {
422 switch (right->special) {
423 case '.':
424 break;
425 case '+': case '-':
426 if (strchr("eEpP", p[len - 2]))
427 break;
428 default:
429 return TOKEN_ERROR;
432 return TOKEN_NUMBER;
435 if (p[0] == '.' && isdigit((unsigned char)p[1]))
436 return TOKEN_NUMBER;
438 return TOKEN_SPECIAL;
441 static int merge(struct token *left, struct token *right)
443 static char buffer[512];
444 int n;
446 switch (combine(left, right, buffer)) {
447 case TOKEN_IDENT:
448 left->ident = built_in_ident(buffer);
449 left->pos.noexpand = 0;
450 return 1;
452 case TOKEN_NUMBER: {
453 char *number = __alloc_bytes(strlen(buffer) + 1);
454 memcpy(number, buffer, strlen(buffer) + 1);
455 token_type(left) = TOKEN_NUMBER; /* could be . + num */
456 left->number = number;
457 return 1;
460 case TOKEN_SPECIAL:
461 if (buffer[2] && buffer[3])
462 break;
463 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
464 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
465 left->special = n;
466 return 1;
469 default:
472 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
473 return 0;
476 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
478 struct token *alloc = alloc_token(streampos);
479 token_type(alloc) = token_type(token);
480 alloc->pos.newline = pos->newline;
481 alloc->pos.whitespace = pos->whitespace;
482 alloc->number = token->number;
483 alloc->pos.noexpand = token->pos.noexpand;
484 return alloc;
487 static struct token **copy(struct token **where, struct token *list, int *count)
489 int need_copy = --*count;
490 while (!eof_token(list)) {
491 struct token *token;
492 if (need_copy)
493 token = dup_token(list, &list->pos, &list->pos);
494 else
495 token = list;
496 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
497 token->pos.noexpand = 1;
498 *where = token;
499 where = &token->next;
500 list = list->next;
502 *where = &eof_token_entry;
503 return where;
506 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
508 struct token *token = *list;
509 struct position *base_pos = &token->pos;
510 struct position *pos = base_pos;
511 int *count;
512 enum {Normal, Placeholder, Concat} state = Normal;
514 for (; !eof_token(body); body = body->next, pos = &body->pos) {
515 struct token *added, *arg;
516 struct token **tail;
518 switch (token_type(body)) {
519 case TOKEN_GNU_KLUDGE:
521 * GNU kludge: if we had <comma>##<vararg>, behaviour
522 * depends on whether we had enough arguments to have
523 * a vararg. If we did, ## is just ignored. Otherwise
524 * both , and ## are ignored. Comma should come from
525 * the body of macro and not be an argument of earlier
526 * concatenation.
528 if (!args[body->next->argnum].arg)
529 continue;
530 added = dup_token(body, base_pos, pos);
531 token_type(added) = TOKEN_SPECIAL;
532 tail = &added->next;
533 break;
535 case TOKEN_STR_ARGUMENT:
536 arg = args[body->argnum].str;
537 count = &args[body->argnum].n_str;
538 goto copy_arg;
540 case TOKEN_QUOTED_ARGUMENT:
541 arg = args[body->argnum].arg;
542 count = &args[body->argnum].n_quoted;
543 if (!arg || eof_token(arg)) {
544 if (state == Concat)
545 state = Normal;
546 else
547 state = Placeholder;
548 continue;
550 goto copy_arg;
552 case TOKEN_MACRO_ARGUMENT:
553 arg = args[body->argnum].expanded;
554 count = &args[body->argnum].n_normal;
555 if (eof_token(arg)) {
556 state = Normal;
557 continue;
559 copy_arg:
560 tail = copy(&added, arg, count);
561 added->pos.newline = pos->newline;
562 added->pos.whitespace = pos->whitespace;
563 break;
565 case TOKEN_CONCAT:
566 if (state == Placeholder)
567 state = Normal;
568 else
569 state = Concat;
570 continue;
572 case TOKEN_IDENT:
573 added = dup_token(body, base_pos, pos);
574 if (added->ident->tainted)
575 added->pos.noexpand = 1;
576 tail = &added->next;
577 break;
579 default:
580 added = dup_token(body, base_pos, pos);
581 tail = &added->next;
582 break;
586 * if we got to doing real concatenation, we already have
587 * added something into the list, so containing_token() is OK.
589 if (state == Concat && merge(containing_token(list), added)) {
590 *list = added->next;
591 if (tail != &added->next)
592 list = tail;
593 } else {
594 *list = added;
595 list = tail;
597 state = Normal;
599 *list = &eof_token_entry;
600 return list;
603 static int expand(struct token **list, struct symbol *sym)
605 struct token *last;
606 struct token *token = *list;
607 struct ident *expanding = token->ident;
608 struct token **tail;
609 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
610 struct arg args[nargs];
612 if (expanding->tainted) {
613 token->pos.noexpand = 1;
614 return 1;
617 if (sym->arglist) {
618 if (!match_op(scan_next(&token->next), '('))
619 return 1;
620 if (!collect_arguments(token->next, sym->arglist, args, token))
621 return 1;
622 expand_arguments(nargs, args);
625 expanding->tainted = 1;
627 last = token->next;
628 tail = substitute(list, sym->expansion, args);
629 *tail = last;
631 return 0;
634 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
636 struct token *last;
637 static char buffer[256];
638 char *ptr = buffer;
640 last = token;
641 while (!eof_token(token) && !match_op(token, endop)) {
642 int len;
643 const char *val = token->string->data;
644 if (token_type(token) != TOKEN_STRING)
645 val = show_token(token);
646 len = strlen(val);
647 memcpy(ptr, val, len);
648 ptr += len;
649 token = token->next;
651 *ptr = 0;
652 if (endop && !match_op(token, endop))
653 sparse_error(start->pos, "expected '>' at end of filename");
654 return buffer;
657 static int already_tokenized(const char *path)
659 int i;
660 struct stream *s = input_streams;
662 for (i = input_stream_nr; --i >= 0; s++) {
663 if (s->constant != CONSTANT_FILE_YES)
664 continue;
665 if (strcmp(path, s->name))
666 continue;
667 if (s->protect && !lookup_macro(s->protect))
668 continue;
669 return 1;
671 return 0;
674 /* Handle include of header files.
675 * The relevant options are made compatible with gcc. The only options that
676 * are not supported is -withprefix and friends.
678 * Three set of include paths are known:
679 * quote_includepath: Path to search when using #include "file.h"
680 * angle_includepath: Paths to search when using #include <file.h>
681 * isys_includepath: Paths specified with -isystem, come before the
682 * built-in system include paths. Gcc would suppress
683 * warnings from system headers. Here we separate
684 * them from the angle_ ones to keep search ordering.
686 * sys_includepath: Built-in include paths.
687 * dirafter_includepath Paths added with -dirafter.
689 * The above is implemented as one array with pointers
690 * +--------------+
691 * quote_includepath ---> | |
692 * +--------------+
693 * | |
694 * +--------------+
695 * angle_includepath ---> | |
696 * +--------------+
697 * isys_includepath ---> | |
698 * +--------------+
699 * sys_includepath ---> | |
700 * +--------------+
701 * dirafter_includepath -> | |
702 * +--------------+
704 * -I dir insert dir just before isys_includepath and move the rest
705 * -I- makes all dirs specified with -I before to quote dirs only and
706 * angle_includepath is set equal to isys_includepath.
707 * -nostdinc removes all sys dirs by storing NULL in entry pointed
708 * to by * sys_includepath. Note that this will reset all dirs built-in
709 * and added before -nostdinc by -isystem and -idirafter.
710 * -isystem dir adds dir where isys_includepath points adding this dir as
711 * first systemdir
712 * -idirafter dir adds dir to the end of the list
715 static void set_stream_include_path(struct stream *stream)
717 const char *path = stream->path;
718 if (!path) {
719 const char *p = strrchr(stream->name, '/');
720 path = "";
721 if (p) {
722 int len = p - stream->name + 1;
723 char *m = malloc(len+1);
724 /* This includes the final "/" */
725 memcpy(m, stream->name, len);
726 m[len] = 0;
727 path = m;
729 stream->path = path;
731 includepath[0] = path;
734 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
736 int fd;
737 int plen = strlen(path);
738 static char fullname[PATH_MAX];
740 memcpy(fullname, path, plen);
741 if (plen && path[plen-1] != '/') {
742 fullname[plen] = '/';
743 plen++;
745 memcpy(fullname+plen, filename, flen);
746 if (already_tokenized(fullname))
747 return 1;
748 fd = open(fullname, O_RDONLY);
749 if (fd >= 0) {
750 char * streamname = __alloc_bytes(plen + flen);
751 memcpy(streamname, fullname, plen + flen);
752 *where = tokenize(streamname, fd, *where, next_path);
753 close(fd);
754 return 1;
756 return 0;
759 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
761 const char *path;
763 while ((path = *pptr++) != NULL) {
764 if (!try_include(path, filename, flen, list, pptr))
765 continue;
766 return 1;
768 return 0;
771 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
773 int flen = strlen(filename) + 1;
775 /* Absolute path? */
776 if (filename[0] == '/') {
777 if (try_include("", filename, flen, list, includepath))
778 return;
779 goto out;
782 /* Dir of input file is first dir to search for quoted includes */
783 set_stream_include_path(stream);
785 if (!path)
786 /* Do not search quote include if <> is in use */
787 path = local ? quote_includepath : angle_includepath;
789 /* Check the standard include paths.. */
790 if (do_include_path(path, list, token, filename, flen))
791 return;
792 out:
793 error_die(token->pos, "unable to open '%s'", filename);
796 static int free_preprocessor_line(struct token *token)
798 while (token_type(token) != TOKEN_EOF) {
799 struct token *free = token;
800 token = token->next;
801 __free_token(free);
803 return 1;
806 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
808 const char *filename;
809 struct token *next;
810 int expect;
812 next = token->next;
813 expect = '>';
814 if (!match_op(next, '<')) {
815 expand_list(&token->next);
816 expect = 0;
817 next = token;
818 if (match_op(token->next, '<')) {
819 next = token->next;
820 expect = '>';
823 token = next->next;
824 filename = token_name_sequence(token, expect, token);
825 do_include(!expect, stream, list, token, filename, path);
826 return 0;
829 static int handle_include(struct stream *stream, struct token **list, struct token *token)
831 return handle_include_path(stream, list, token, NULL);
834 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
836 return handle_include_path(stream, list, token, stream->next_path);
839 static int token_different(struct token *t1, struct token *t2)
841 int different;
843 if (token_type(t1) != token_type(t2))
844 return 1;
846 switch (token_type(t1)) {
847 case TOKEN_IDENT:
848 different = t1->ident != t2->ident;
849 break;
850 case TOKEN_ARG_COUNT:
851 case TOKEN_UNTAINT:
852 case TOKEN_CONCAT:
853 case TOKEN_GNU_KLUDGE:
854 different = 0;
855 break;
856 case TOKEN_NUMBER:
857 different = strcmp(t1->number, t2->number);
858 break;
859 case TOKEN_SPECIAL:
860 different = t1->special != t2->special;
861 break;
862 case TOKEN_MACRO_ARGUMENT:
863 case TOKEN_QUOTED_ARGUMENT:
864 case TOKEN_STR_ARGUMENT:
865 different = t1->argnum != t2->argnum;
866 break;
867 case TOKEN_CHAR:
868 different = t1->character != t2->character;
869 break;
870 case TOKEN_STRING: {
871 struct string *s1, *s2;
873 s1 = t1->string;
874 s2 = t2->string;
875 different = 1;
876 if (s1->length != s2->length)
877 break;
878 different = memcmp(s1->data, s2->data, s1->length);
879 break;
881 default:
882 different = 1;
883 break;
885 return different;
888 static int token_list_different(struct token *list1, struct token *list2)
890 for (;;) {
891 if (list1 == list2)
892 return 0;
893 if (!list1 || !list2)
894 return 1;
895 if (token_different(list1, list2))
896 return 1;
897 list1 = list1->next;
898 list2 = list2->next;
902 static inline void set_arg_count(struct token *token)
904 token_type(token) = TOKEN_ARG_COUNT;
905 token->count.normal = token->count.quoted =
906 token->count.str = token->count.vararg = 0;
909 static struct token *parse_arguments(struct token *list)
911 struct token *arg = list->next, *next = list;
912 struct argcount *count = &list->count;
914 set_arg_count(list);
916 if (match_op(arg, ')')) {
917 next = arg->next;
918 list->next = &eof_token_entry;
919 return next;
922 while (token_type(arg) == TOKEN_IDENT) {
923 if (arg->ident == &__VA_ARGS___ident)
924 goto Eva_args;
925 if (!++count->normal)
926 goto Eargs;
927 next = arg->next;
929 if (match_op(next, ',')) {
930 set_arg_count(next);
931 arg = next->next;
932 continue;
935 if (match_op(next, ')')) {
936 set_arg_count(next);
937 next = next->next;
938 arg->next->next = &eof_token_entry;
939 return next;
942 /* normal cases are finished here */
944 if (match_op(next, SPECIAL_ELLIPSIS)) {
945 if (match_op(next->next, ')')) {
946 set_arg_count(next);
947 next->count.vararg = 1;
948 next = next->next;
949 arg->next->next = &eof_token_entry;
950 return next->next;
953 arg = next;
954 goto Enotclosed;
957 if (eof_token(next)) {
958 goto Enotclosed;
959 } else {
960 arg = next;
961 goto Ebadstuff;
965 if (match_op(arg, SPECIAL_ELLIPSIS)) {
966 next = arg->next;
967 token_type(arg) = TOKEN_IDENT;
968 arg->ident = &__VA_ARGS___ident;
969 if (!match_op(next, ')'))
970 goto Enotclosed;
971 if (!++count->normal)
972 goto Eargs;
973 set_arg_count(next);
974 next->count.vararg = 1;
975 next = next->next;
976 arg->next->next = &eof_token_entry;
977 return next;
980 if (eof_token(arg)) {
981 arg = next;
982 goto Enotclosed;
984 if (match_op(arg, ','))
985 goto Emissing;
986 else
987 goto Ebadstuff;
990 Emissing:
991 sparse_error(arg->pos, "parameter name missing");
992 return NULL;
993 Ebadstuff:
994 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
995 show_token(arg));
996 return NULL;
997 Enotclosed:
998 sparse_error(arg->pos, "missing ')' in macro parameter list");
999 return NULL;
1000 Eva_args:
1001 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1002 return NULL;
1003 Eargs:
1004 sparse_error(arg->pos, "too many arguments in macro definition");
1005 return NULL;
1008 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1010 struct ident *ident = token->ident;
1011 int nr;
1013 if (!arglist || token_type(token) != TOKEN_IDENT)
1014 return 0;
1016 arglist = arglist->next;
1018 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1019 if (arglist->ident == ident) {
1020 struct argcount *count = &arglist->next->count;
1021 int n;
1023 token->argnum = nr;
1024 token_type(token) = type;
1025 switch (type) {
1026 case TOKEN_MACRO_ARGUMENT:
1027 n = ++count->normal;
1028 break;
1029 case TOKEN_QUOTED_ARGUMENT:
1030 n = ++count->quoted;
1031 break;
1032 default:
1033 n = ++count->str;
1035 if (n)
1036 return count->vararg ? 2 : 1;
1037 token_type(token) = TOKEN_ERROR;
1038 return -1;
1041 return 0;
1044 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1046 struct token *token = expansion;
1047 struct token **p;
1048 struct token *last = NULL;
1050 if (match_op(token, SPECIAL_HASHHASH))
1051 goto Econcat;
1053 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1054 if (match_op(token, '#')) {
1055 if (arglist) {
1056 struct token *next = token->next;
1057 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1058 goto Equote;
1059 next->pos.whitespace = token->pos.whitespace;
1060 token = *p = next;
1061 } else {
1062 token->pos.noexpand = 1;
1064 } else if (match_op(token, SPECIAL_HASHHASH)) {
1065 struct token *next = token->next;
1066 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
1067 token_type(token) = TOKEN_CONCAT;
1068 if (arg) {
1069 token = next;
1070 /* GNU kludge */
1071 if (arg == 2 && last && match_op(last, ',')) {
1072 token_type(last) = TOKEN_GNU_KLUDGE;
1073 last->next = token;
1075 } else if (match_op(next, SPECIAL_HASHHASH))
1076 token = next;
1077 else if (eof_token(next))
1078 goto Econcat;
1079 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
1080 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1081 } else {
1082 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1084 if (token_type(token) == TOKEN_ERROR)
1085 goto Earg;
1086 last = token;
1088 token = alloc_token(&expansion->pos);
1089 token_type(token) = TOKEN_UNTAINT;
1090 token->ident = name;
1091 token->next = *p;
1092 *p = token;
1093 return expansion;
1095 Equote:
1096 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1097 return NULL;
1099 Econcat:
1100 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1101 return NULL;
1102 Earg:
1103 sparse_error(token->pos, "too many instances of argument in body");
1104 return NULL;
1107 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1109 struct token *arglist, *expansion;
1110 struct token *left = token->next;
1111 struct symbol *sym;
1112 struct ident *name;
1113 int ret;
1115 if (token_type(left) != TOKEN_IDENT) {
1116 sparse_error(token->pos, "expected identifier to 'define'");
1117 return 1;
1120 name = left->ident;
1122 arglist = NULL;
1123 expansion = left->next;
1124 if (!expansion->pos.whitespace) {
1125 if (match_op(expansion, '(')) {
1126 arglist = expansion;
1127 expansion = parse_arguments(expansion);
1128 if (!expansion)
1129 return 1;
1130 } else if (!eof_token(expansion)) {
1131 warning(expansion->pos,
1132 "no whitespace before object-like macro body");
1136 expansion = parse_expansion(expansion, arglist, name);
1137 if (!expansion)
1138 return 1;
1140 ret = 1;
1141 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1142 if (sym) {
1143 int clean;
1145 if (attr < sym->attr)
1146 goto out;
1148 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1150 if (token_list_different(sym->expansion, expansion) ||
1151 token_list_different(sym->arglist, arglist)) {
1152 ret = 0;
1153 if ((clean && attr == SYM_ATTR_NORMAL)
1154 || sym->used_in == file_scope) {
1155 warning(left->pos, "preprocessor token %.*s redefined",
1156 name->len, name->name);
1157 info(sym->pos, "this was the original definition");
1159 } else if (clean)
1160 goto out;
1163 if (!sym || sym->scope != file_scope) {
1164 sym = alloc_symbol(left->pos, SYM_NODE);
1165 bind_symbol(sym, name, NS_MACRO);
1166 ret = 0;
1169 if (!ret) {
1170 sym->expansion = expansion;
1171 sym->arglist = arglist;
1172 __free_token(token); /* Free the "define" token, but not the rest of the line */
1175 sym->namespace = NS_MACRO;
1176 sym->used_in = NULL;
1177 sym->attr = attr;
1178 out:
1179 return ret;
1182 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1184 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1187 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1189 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1192 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1194 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1197 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1199 struct token *left = token->next;
1200 struct symbol *sym;
1202 if (token_type(left) != TOKEN_IDENT) {
1203 sparse_error(token->pos, "expected identifier to 'undef'");
1204 return 1;
1207 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1208 if (sym) {
1209 if (attr < sym->attr)
1210 return 1;
1211 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1212 return 1;
1213 } else if (attr <= SYM_ATTR_NORMAL)
1214 return 1;
1216 if (!sym || sym->scope != file_scope) {
1217 sym = alloc_symbol(left->pos, SYM_NODE);
1218 bind_symbol(sym, left->ident, NS_MACRO);
1221 sym->namespace = NS_UNDEF;
1222 sym->used_in = NULL;
1223 sym->attr = attr;
1225 return 1;
1228 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1230 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1233 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1235 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1238 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1240 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1241 free_preprocessor_line(token->next);
1242 token->next = stream->top_if;
1243 stream->top_if = token;
1244 if (false_nesting || true != 1)
1245 false_nesting++;
1246 return 0;
1249 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1251 struct token *next = token->next;
1252 int arg;
1253 if (token_type(next) == TOKEN_IDENT) {
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;
1261 return preprocessor_if(stream, token, arg);
1264 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1266 struct token *next = token->next;
1267 int arg;
1268 if (token_type(next) == TOKEN_IDENT) {
1269 if (!stream->dirty && !stream->ifndef) {
1270 if (!stream->protect) {
1271 stream->ifndef = token;
1272 stream->protect = next->ident;
1273 } else if (stream->protect == next->ident) {
1274 stream->ifndef = token;
1275 stream->dirty = 1;
1278 arg = !token_defined(next);
1279 } else {
1280 dirty_stream(stream);
1281 if (!false_nesting)
1282 sparse_error(token->pos, "expected preprocessor identifier");
1283 arg = -1;
1286 return preprocessor_if(stream, token, arg);
1290 * Expression handling for #if and #elif; it differs from normal expansion
1291 * due to special treatment of "defined".
1293 static int expression_value(struct token **where)
1295 struct expression *expr;
1296 struct token *p;
1297 struct token **list = where, **beginning = NULL;
1298 long long value;
1299 int state = 0;
1301 while (!eof_token(p = scan_next(list))) {
1302 switch (state) {
1303 case 0:
1304 if (token_type(p) != TOKEN_IDENT)
1305 break;
1306 if (p->ident == &defined_ident) {
1307 state = 1;
1308 beginning = list;
1309 break;
1311 if (!expand_one_symbol(list))
1312 continue;
1313 if (token_type(p) != TOKEN_IDENT)
1314 break;
1315 token_type(p) = TOKEN_ZERO_IDENT;
1316 break;
1317 case 1:
1318 if (match_op(p, '(')) {
1319 state = 2;
1320 } else {
1321 state = 0;
1322 replace_with_defined(p);
1323 *beginning = p;
1325 break;
1326 case 2:
1327 if (token_type(p) == TOKEN_IDENT)
1328 state = 3;
1329 else
1330 state = 0;
1331 replace_with_defined(p);
1332 *beginning = p;
1333 break;
1334 case 3:
1335 state = 0;
1336 if (!match_op(p, ')'))
1337 sparse_error(p->pos, "missing ')' after \"defined\"");
1338 *list = p->next;
1339 continue;
1341 list = &p->next;
1344 p = constant_expression(*where, &expr);
1345 if (!eof_token(p))
1346 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p));
1347 value = get_expression_value(expr);
1348 return value != 0;
1351 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1353 int value = 0;
1354 if (!false_nesting)
1355 value = expression_value(&token->next);
1357 dirty_stream(stream);
1358 return preprocessor_if(stream, token, value);
1361 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1363 struct token *top_if = stream->top_if;
1364 end_group(stream);
1366 if (!top_if) {
1367 nesting_error(stream);
1368 sparse_error(token->pos, "unmatched #elif within stream");
1369 return 1;
1372 if (token_type(top_if) == TOKEN_ELSE) {
1373 nesting_error(stream);
1374 sparse_error(token->pos, "#elif after #else");
1375 if (!false_nesting)
1376 false_nesting = 1;
1377 return 1;
1380 dirty_stream(stream);
1381 if (token_type(top_if) != TOKEN_IF)
1382 return 1;
1383 if (false_nesting) {
1384 false_nesting = 0;
1385 if (!expression_value(&token->next))
1386 false_nesting = 1;
1387 } else {
1388 false_nesting = 1;
1389 token_type(top_if) = TOKEN_SKIP_GROUPS;
1391 return 1;
1394 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1396 struct token *top_if = stream->top_if;
1397 end_group(stream);
1399 if (!top_if) {
1400 nesting_error(stream);
1401 sparse_error(token->pos, "unmatched #else within stream");
1402 return 1;
1405 if (token_type(top_if) == TOKEN_ELSE) {
1406 nesting_error(stream);
1407 sparse_error(token->pos, "#else after #else");
1409 if (false_nesting) {
1410 if (token_type(top_if) == TOKEN_IF)
1411 false_nesting = 0;
1412 } else {
1413 false_nesting = 1;
1415 token_type(top_if) = TOKEN_ELSE;
1416 return 1;
1419 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1421 struct token *top_if = stream->top_if;
1422 end_group(stream);
1423 if (!top_if) {
1424 nesting_error(stream);
1425 sparse_error(token->pos, "unmatched #endif in stream");
1426 return 1;
1428 if (false_nesting)
1429 false_nesting--;
1430 stream->top_if = top_if->next;
1431 __free_token(top_if);
1432 return 1;
1435 static const char *show_token_sequence(struct token *token)
1437 static char buffer[1024];
1438 char *ptr = buffer;
1439 int whitespace = 0;
1441 if (!token)
1442 return "<none>";
1443 while (!eof_token(token)) {
1444 const char *val = show_token(token);
1445 int len = strlen(val);
1447 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1448 sparse_error(token->pos, "too long token expansion");
1449 break;
1452 if (whitespace)
1453 *ptr++ = ' ';
1454 memcpy(ptr, val, len);
1455 ptr += len;
1456 token = token->next;
1457 whitespace = token->pos.whitespace;
1459 *ptr = 0;
1460 return buffer;
1463 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1465 warning(token->pos, "%s", show_token_sequence(token->next));
1466 return 1;
1469 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1471 sparse_error(token->pos, "%s", show_token_sequence(token->next));
1472 return 1;
1475 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1478 * Do we have any non-system includes?
1479 * Clear them out if so..
1481 *sys_includepath = NULL;
1482 return 1;
1485 static inline void update_inc_ptrs(const char ***where)
1488 if (*where <= dirafter_includepath) {
1489 dirafter_includepath++;
1490 /* If this was the entry that we prepend, don't
1491 * rise the lower entries, even if they are at
1492 * the same level. */
1493 if (where == &dirafter_includepath)
1494 return;
1496 if (*where <= sys_includepath) {
1497 sys_includepath++;
1498 if (where == &sys_includepath)
1499 return;
1501 if (*where <= isys_includepath) {
1502 isys_includepath++;
1503 if (where == &isys_includepath)
1504 return;
1507 /* angle_includepath is actually never updated, since we
1508 * don't suppport -iquote rught now. May change some day. */
1509 if (*where <= angle_includepath) {
1510 angle_includepath++;
1511 if (where == &angle_includepath)
1512 return;
1516 /* Add a path before 'where' and update the pointers associated with the
1517 * includepath array */
1518 static void add_path_entry(struct token *token, const char *path,
1519 const char ***where)
1521 const char **dst;
1522 const char *next;
1524 /* Need one free entry.. */
1525 if (includepath[INCLUDEPATHS-2])
1526 error_die(token->pos, "too many include path entries");
1528 /* check that this is not a duplicate */
1529 dst = includepath;
1530 while (*dst) {
1531 if (strcmp(*dst, path) == 0)
1532 return;
1533 dst++;
1535 next = path;
1536 dst = *where;
1538 update_inc_ptrs(where);
1541 * Move them all up starting at dst,
1542 * insert the new entry..
1544 do {
1545 const char *tmp = *dst;
1546 *dst = next;
1547 next = tmp;
1548 dst++;
1549 } while (next);
1552 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1554 for (;;) {
1555 token = token->next;
1556 if (eof_token(token))
1557 return 1;
1558 if (token_type(token) != TOKEN_STRING) {
1559 warning(token->pos, "expected path string");
1560 return 1;
1562 add_path_entry(token, token->string->data, &isys_includepath);
1566 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1568 for (;;) {
1569 token = token->next;
1570 if (eof_token(token))
1571 return 1;
1572 if (token_type(token) != TOKEN_STRING) {
1573 sparse_error(token->pos, "expected path string");
1574 return 1;
1576 add_path_entry(token, token->string->data, &sys_includepath);
1580 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1582 for (;;) {
1583 token = token->next;
1584 if (eof_token(token))
1585 return 1;
1586 if (token_type(token) != TOKEN_STRING) {
1587 sparse_error(token->pos, "expected path string");
1588 return 1;
1590 add_path_entry(token, token->string->data, &dirafter_includepath);
1594 /* Add to end on includepath list - no pointer updates */
1595 static void add_dirafter_entry(struct token *token, const char *path)
1597 const char **dst = includepath;
1599 /* Need one free entry.. */
1600 if (includepath[INCLUDEPATHS-2])
1601 error_die(token->pos, "too many include path entries");
1603 /* Add to the end */
1604 while (*dst)
1605 dst++;
1606 *dst = path;
1607 dst++;
1608 *dst = NULL;
1611 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1613 for (;;) {
1614 token = token->next;
1615 if (eof_token(token))
1616 return 1;
1617 if (token_type(token) != TOKEN_STRING) {
1618 sparse_error(token->pos, "expected path string");
1619 return 1;
1621 add_dirafter_entry(token, token->string->data);
1625 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1628 * -I-
1629 * From info gcc:
1630 * Split the include path. Any directories specified with `-I'
1631 * options before `-I-' are searched only for headers requested with
1632 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1633 * If additional directories are specified with `-I' options after
1634 * the `-I-', those directories are searched for all `#include'
1635 * directives.
1636 * In addition, `-I-' inhibits the use of the directory of the current
1637 * file directory as the first search directory for `#include "FILE"'.
1639 quote_includepath = includepath+1;
1640 angle_includepath = sys_includepath;
1641 return 1;
1645 * We replace "#pragma xxx" with "__pragma__" in the token
1646 * stream. Just as an example.
1648 * We'll just #define that away for now, but the theory here
1649 * is that we can use this to insert arbitrary token sequences
1650 * to turn the pragmas into internal front-end sequences for
1651 * when we actually start caring about them.
1653 * So eventually this will turn into some kind of extended
1654 * __attribute__() like thing, except called __pragma__(xxx).
1656 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1658 struct token *next = *line;
1660 token->ident = &pragma_ident;
1661 token->pos.newline = 1;
1662 token->pos.whitespace = 1;
1663 token->pos.pos = 1;
1664 *line = token;
1665 token->next = next;
1666 return 0;
1670 * We ignore #line for now.
1672 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1674 return 1;
1677 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1679 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1680 return 1;
1684 static void init_preprocessor(void)
1686 int i;
1687 int stream = init_stream("preprocessor", -1, includepath);
1688 static struct {
1689 const char *name;
1690 int (*handler)(struct stream *, struct token **, struct token *);
1691 } normal[] = {
1692 { "define", handle_define },
1693 { "weak_define", handle_weak_define },
1694 { "strong_define", handle_strong_define },
1695 { "undef", handle_undef },
1696 { "strong_undef", handle_strong_undef },
1697 { "warning", handle_warning },
1698 { "error", handle_error },
1699 { "include", handle_include },
1700 { "include_next", handle_include_next },
1701 { "pragma", handle_pragma },
1702 { "line", handle_line },
1704 // our internal preprocessor tokens
1705 { "nostdinc", handle_nostdinc },
1706 { "add_include", handle_add_include },
1707 { "add_isystem", handle_add_isystem },
1708 { "add_system", handle_add_system },
1709 { "add_dirafter", handle_add_dirafter },
1710 { "split_include", handle_split_include },
1711 }, special[] = {
1712 { "ifdef", handle_ifdef },
1713 { "ifndef", handle_ifndef },
1714 { "else", handle_else },
1715 { "endif", handle_endif },
1716 { "if", handle_if },
1717 { "elif", handle_elif },
1720 for (i = 0; i < (sizeof (normal) / sizeof (normal[0])); i++) {
1721 struct symbol *sym;
1722 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1723 sym->handler = normal[i].handler;
1724 sym->normal = 1;
1726 for (i = 0; i < (sizeof (special) / sizeof (special[0])); i++) {
1727 struct symbol *sym;
1728 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1729 sym->handler = special[i].handler;
1730 sym->normal = 0;
1735 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1737 int (*handler)(struct stream *, struct token **, struct token *);
1738 struct token *token = start->next;
1739 int is_normal = 1;
1741 if (eof_token(token))
1742 return;
1744 if (token_type(token) == TOKEN_IDENT) {
1745 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1746 if (sym) {
1747 handler = sym->handler;
1748 is_normal = sym->normal;
1749 } else {
1750 handler = handle_nondirective;
1752 } else if (token_type(token) == TOKEN_NUMBER) {
1753 handler = handle_line;
1754 } else {
1755 handler = handle_nondirective;
1758 if (is_normal) {
1759 dirty_stream(stream);
1760 if (false_nesting)
1761 goto out;
1763 if (!handler(stream, line, token)) /* all set */
1764 return;
1766 out:
1767 free_preprocessor_line(token);
1770 static void preprocessor_line(struct stream *stream, struct token **line)
1772 struct token *start = *line, *next;
1773 struct token **tp = &start->next;
1775 for (;;) {
1776 next = *tp;
1777 if (next->pos.newline)
1778 break;
1779 tp = &next->next;
1781 *line = next;
1782 *tp = &eof_token_entry;
1783 handle_preprocessor_line(stream, line, start);
1786 static void do_preprocess(struct token **list)
1788 struct token *next;
1790 while (!eof_token(next = scan_next(list))) {
1791 struct stream *stream = input_streams + next->pos.stream;
1793 if (next->pos.newline && match_op(next, '#')) {
1794 if (!next->pos.noexpand) {
1795 preprocessor_line(stream, list);
1796 __free_token(next); /* Free the '#' token */
1797 continue;
1801 switch (token_type(next)) {
1802 case TOKEN_STREAMEND:
1803 if (stream->top_if) {
1804 nesting_error(stream);
1805 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1806 stream->top_if = NULL;
1807 false_nesting = 0;
1809 if (!stream->dirty)
1810 stream->constant = CONSTANT_FILE_YES;
1811 *list = next->next;
1812 continue;
1813 case TOKEN_STREAMBEGIN:
1814 *list = next->next;
1815 continue;
1817 default:
1818 dirty_stream(stream);
1819 if (false_nesting) {
1820 *list = next->next;
1821 __free_token(next);
1822 continue;
1825 if (token_type(next) != TOKEN_IDENT ||
1826 expand_one_symbol(list))
1827 list = &next->next;
1832 struct token * preprocess(struct token *token)
1834 preprocessing = 1;
1835 init_preprocessor();
1836 do_preprocess(&token);
1838 // Drop all expressions from preprocessing, they're not used any more.
1839 // This is not true when we have multiple files, though ;/
1840 // clear_expression_alloc();
1841 preprocessing = 0;
1843 return token;