sparse, llvm: Fix assert() in sparse code
[smatch.git] / pre-process.c
blob8a16f8b383f0a1534650c6b4b72f208888a67722
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 void preprocessor_line(struct stream *stream, struct token **line);
198 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
200 struct stream *stream = input_streams + prev->pos.stream;
201 struct token **p = &prev->next;
202 struct token *next;
203 int nesting = 0;
205 while (!eof_token(next = scan_next(p))) {
206 if (next->pos.newline && match_op(next, '#')) {
207 if (!next->pos.noexpand) {
208 sparse_error(next->pos,
209 "directive in argument list");
210 preprocessor_line(stream, p);
211 __free_token(next); /* Free the '#' token */
212 continue;
215 switch (token_type(next)) {
216 case TOKEN_STREAMEND:
217 case TOKEN_STREAMBEGIN:
218 *p = &eof_token_entry;
219 return next;
221 if (false_nesting) {
222 *p = next->next;
223 __free_token(next);
224 continue;
226 if (match_op(next, '(')) {
227 nesting++;
228 } else if (match_op(next, ')')) {
229 if (!nesting--)
230 break;
231 } else if (match_op(next, ',') && !nesting && !vararg) {
232 break;
234 next->pos.stream = pos->stream;
235 next->pos.line = pos->line;
236 next->pos.pos = pos->pos;
237 p = &next->next;
239 *p = &eof_token_entry;
240 return next;
244 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
247 struct arg {
248 struct token *arg;
249 struct token *expanded;
250 struct token *str;
251 int n_normal;
252 int n_quoted;
253 int n_str;
256 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
258 int wanted = arglist->count.normal;
259 struct token *next = NULL;
260 int count = 0;
262 arglist = arglist->next; /* skip counter */
264 if (!wanted) {
265 next = collect_arg(start, 0, &what->pos);
266 if (eof_token(next))
267 goto Eclosing;
268 if (!eof_token(start->next) || !match_op(next, ')')) {
269 count++;
270 goto Emany;
272 } else {
273 for (count = 0; count < wanted; count++) {
274 struct argcount *p = &arglist->next->count;
275 next = collect_arg(start, p->vararg, &what->pos);
276 arglist = arglist->next->next;
277 if (eof_token(next))
278 goto Eclosing;
279 args[count].arg = start->next;
280 args[count].n_normal = p->normal;
281 args[count].n_quoted = p->quoted;
282 args[count].n_str = p->str;
283 if (match_op(next, ')')) {
284 count++;
285 break;
287 start = next;
289 if (count == wanted && !match_op(next, ')'))
290 goto Emany;
291 if (count == wanted - 1) {
292 struct argcount *p = &arglist->next->count;
293 if (!p->vararg)
294 goto Efew;
295 args[count].arg = NULL;
296 args[count].n_normal = p->normal;
297 args[count].n_quoted = p->quoted;
298 args[count].n_str = p->str;
300 if (count < wanted - 1)
301 goto Efew;
303 what->next = next->next;
304 return 1;
306 Efew:
307 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
308 show_token(what), wanted, count);
309 goto out;
310 Emany:
311 while (match_op(next, ',')) {
312 next = collect_arg(next, 0, &what->pos);
313 count++;
315 if (eof_token(next))
316 goto Eclosing;
317 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
318 show_token(what), count, wanted);
319 goto out;
320 Eclosing:
321 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
322 show_token(what));
323 out:
324 what->next = next->next;
325 return 0;
328 static struct token *dup_list(struct token *list)
330 struct token *res = NULL;
331 struct token **p = &res;
333 while (!eof_token(list)) {
334 struct token *newtok = __alloc_token(0);
335 *newtok = *list;
336 *p = newtok;
337 p = &newtok->next;
338 list = list->next;
340 return res;
343 static struct token *stringify(struct token *arg)
345 const char *s = show_token_sequence(arg);
346 int size = strlen(s)+1;
347 struct token *token = __alloc_token(0);
348 struct string *string = __alloc_string(size);
350 memcpy(string->data, s, size);
351 string->length = size;
352 token->pos = arg->pos;
353 token_type(token) = TOKEN_STRING;
354 token->string = string;
355 token->next = &eof_token_entry;
356 return token;
359 static void expand_arguments(int count, struct arg *args)
361 int i;
362 for (i = 0; i < count; i++) {
363 struct token *arg = args[i].arg;
364 if (!arg)
365 arg = &eof_token_entry;
366 if (args[i].n_str)
367 args[i].str = stringify(arg);
368 if (args[i].n_normal) {
369 if (!args[i].n_quoted) {
370 args[i].expanded = arg;
371 args[i].arg = NULL;
372 } else if (eof_token(arg)) {
373 args[i].expanded = arg;
374 } else {
375 args[i].expanded = dup_list(arg);
377 expand_list(&args[i].expanded);
383 * Possibly valid combinations:
384 * - ident + ident -> ident
385 * - ident + number -> ident unless number contains '.', '+' or '-'.
386 * - number + number -> number
387 * - number + ident -> number
388 * - number + '.' -> number
389 * - number + '+' or '-' -> number, if number used to end on [eEpP].
390 * - '.' + number -> number, if number used to start with a digit.
391 * - special + special -> either special or an error.
393 static enum token_type combine(struct token *left, struct token *right, char *p)
395 int len;
396 enum token_type t1 = token_type(left), t2 = token_type(right);
398 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
399 return TOKEN_ERROR;
401 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
402 return TOKEN_ERROR;
404 strcpy(p, show_token(left));
405 strcat(p, show_token(right));
406 len = strlen(p);
408 if (len >= 256)
409 return TOKEN_ERROR;
411 if (t1 == TOKEN_IDENT) {
412 if (t2 == TOKEN_SPECIAL)
413 return TOKEN_ERROR;
414 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
415 return TOKEN_ERROR;
416 return TOKEN_IDENT;
419 if (t1 == TOKEN_NUMBER) {
420 if (t2 == TOKEN_SPECIAL) {
421 switch (right->special) {
422 case '.':
423 break;
424 case '+': case '-':
425 if (strchr("eEpP", p[len - 2]))
426 break;
427 default:
428 return TOKEN_ERROR;
431 return TOKEN_NUMBER;
434 if (p[0] == '.' && isdigit((unsigned char)p[1]))
435 return TOKEN_NUMBER;
437 return TOKEN_SPECIAL;
440 static int merge(struct token *left, struct token *right)
442 static char buffer[512];
443 int n;
445 switch (combine(left, right, buffer)) {
446 case TOKEN_IDENT:
447 left->ident = built_in_ident(buffer);
448 left->pos.noexpand = 0;
449 return 1;
451 case TOKEN_NUMBER: {
452 char *number = __alloc_bytes(strlen(buffer) + 1);
453 memcpy(number, buffer, strlen(buffer) + 1);
454 token_type(left) = TOKEN_NUMBER; /* could be . + num */
455 left->number = number;
456 return 1;
459 case TOKEN_SPECIAL:
460 if (buffer[2] && buffer[3])
461 break;
462 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
463 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
464 left->special = n;
465 return 1;
468 default:
471 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
472 return 0;
475 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
477 struct token *alloc = alloc_token(streampos);
478 token_type(alloc) = token_type(token);
479 alloc->pos.newline = pos->newline;
480 alloc->pos.whitespace = pos->whitespace;
481 alloc->number = token->number;
482 alloc->pos.noexpand = token->pos.noexpand;
483 return alloc;
486 static struct token **copy(struct token **where, struct token *list, int *count)
488 int need_copy = --*count;
489 while (!eof_token(list)) {
490 struct token *token;
491 if (need_copy)
492 token = dup_token(list, &list->pos, &list->pos);
493 else
494 token = list;
495 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
496 token->pos.noexpand = 1;
497 *where = token;
498 where = &token->next;
499 list = list->next;
501 *where = &eof_token_entry;
502 return where;
505 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
507 struct token *token = *list;
508 struct position *base_pos = &token->pos;
509 struct position *pos = base_pos;
510 int *count;
511 enum {Normal, Placeholder, Concat} state = Normal;
513 for (; !eof_token(body); body = body->next, pos = &body->pos) {
514 struct token *added, *arg;
515 struct token **tail;
517 switch (token_type(body)) {
518 case TOKEN_GNU_KLUDGE:
520 * GNU kludge: if we had <comma>##<vararg>, behaviour
521 * depends on whether we had enough arguments to have
522 * a vararg. If we did, ## is just ignored. Otherwise
523 * both , and ## are ignored. Comma should come from
524 * the body of macro and not be an argument of earlier
525 * concatenation.
527 if (!args[body->next->argnum].arg)
528 continue;
529 added = dup_token(body, base_pos, pos);
530 token_type(added) = TOKEN_SPECIAL;
531 tail = &added->next;
532 break;
534 case TOKEN_STR_ARGUMENT:
535 arg = args[body->argnum].str;
536 count = &args[body->argnum].n_str;
537 goto copy_arg;
539 case TOKEN_QUOTED_ARGUMENT:
540 arg = args[body->argnum].arg;
541 count = &args[body->argnum].n_quoted;
542 if (!arg || eof_token(arg)) {
543 if (state == Concat)
544 state = Normal;
545 else
546 state = Placeholder;
547 continue;
549 goto copy_arg;
551 case TOKEN_MACRO_ARGUMENT:
552 arg = args[body->argnum].expanded;
553 count = &args[body->argnum].n_normal;
554 if (eof_token(arg)) {
555 state = Normal;
556 continue;
558 copy_arg:
559 tail = copy(&added, arg, count);
560 added->pos.newline = pos->newline;
561 added->pos.whitespace = pos->whitespace;
562 break;
564 case TOKEN_CONCAT:
565 if (state == Placeholder)
566 state = Normal;
567 else
568 state = Concat;
569 continue;
571 case TOKEN_IDENT:
572 added = dup_token(body, base_pos, pos);
573 if (added->ident->tainted)
574 added->pos.noexpand = 1;
575 tail = &added->next;
576 break;
578 default:
579 added = dup_token(body, base_pos, pos);
580 tail = &added->next;
581 break;
585 * if we got to doing real concatenation, we already have
586 * added something into the list, so containing_token() is OK.
588 if (state == Concat && merge(containing_token(list), added)) {
589 *list = added->next;
590 if (tail != &added->next)
591 list = tail;
592 } else {
593 *list = added;
594 list = tail;
596 state = Normal;
598 *list = &eof_token_entry;
599 return list;
602 static int expand(struct token **list, struct symbol *sym)
604 struct token *last;
605 struct token *token = *list;
606 struct ident *expanding = token->ident;
607 struct token **tail;
608 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
609 struct arg args[nargs];
611 if (expanding->tainted) {
612 token->pos.noexpand = 1;
613 return 1;
616 if (sym->arglist) {
617 if (!match_op(scan_next(&token->next), '('))
618 return 1;
619 if (!collect_arguments(token->next, sym->arglist, args, token))
620 return 1;
621 expand_arguments(nargs, args);
624 expanding->tainted = 1;
626 last = token->next;
627 tail = substitute(list, sym->expansion, args);
628 *tail = last;
630 return 0;
633 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
635 static char buffer[256];
636 char *ptr = buffer;
638 while (!eof_token(token) && !match_op(token, endop)) {
639 int len;
640 const char *val = token->string->data;
641 if (token_type(token) != TOKEN_STRING)
642 val = show_token(token);
643 len = strlen(val);
644 memcpy(ptr, val, len);
645 ptr += len;
646 token = token->next;
648 *ptr = 0;
649 if (endop && !match_op(token, endop))
650 sparse_error(start->pos, "expected '>' at end of filename");
651 return buffer;
654 static int already_tokenized(const char *path)
656 int stream, next;
658 for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
659 struct stream *s = input_streams + stream;
661 next = s->next_stream;
662 if (s->constant != CONSTANT_FILE_YES)
663 continue;
664 if (strcmp(path, s->name))
665 continue;
666 if (s->protect && !lookup_macro(s->protect))
667 continue;
668 return 1;
670 return 0;
673 /* Handle include of header files.
674 * The relevant options are made compatible with gcc. The only options that
675 * are not supported is -withprefix and friends.
677 * Three set of include paths are known:
678 * quote_includepath: Path to search when using #include "file.h"
679 * angle_includepath: Paths to search when using #include <file.h>
680 * isys_includepath: Paths specified with -isystem, come before the
681 * built-in system include paths. Gcc would suppress
682 * warnings from system headers. Here we separate
683 * them from the angle_ ones to keep search ordering.
685 * sys_includepath: Built-in include paths.
686 * dirafter_includepath Paths added with -dirafter.
688 * The above is implemented as one array with pointers
689 * +--------------+
690 * quote_includepath ---> | |
691 * +--------------+
692 * | |
693 * +--------------+
694 * angle_includepath ---> | |
695 * +--------------+
696 * isys_includepath ---> | |
697 * +--------------+
698 * sys_includepath ---> | |
699 * +--------------+
700 * dirafter_includepath -> | |
701 * +--------------+
703 * -I dir insert dir just before isys_includepath and move the rest
704 * -I- makes all dirs specified with -I before to quote dirs only and
705 * angle_includepath is set equal to isys_includepath.
706 * -nostdinc removes all sys dirs by storing NULL in entry pointed
707 * to by * sys_includepath. Note that this will reset all dirs built-in
708 * and added before -nostdinc by -isystem and -idirafter.
709 * -isystem dir adds dir where isys_includepath points adding this dir as
710 * first systemdir
711 * -idirafter dir adds dir to the end of the list
714 static void set_stream_include_path(struct stream *stream)
716 const char *path = stream->path;
717 if (!path) {
718 const char *p = strrchr(stream->name, '/');
719 path = "";
720 if (p) {
721 int len = p - stream->name + 1;
722 char *m = malloc(len+1);
723 /* This includes the final "/" */
724 memcpy(m, stream->name, len);
725 m[len] = 0;
726 path = m;
728 stream->path = path;
730 includepath[0] = path;
733 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
735 int fd;
736 int plen = strlen(path);
737 static char fullname[PATH_MAX];
739 memcpy(fullname, path, plen);
740 if (plen && path[plen-1] != '/') {
741 fullname[plen] = '/';
742 plen++;
744 memcpy(fullname+plen, filename, flen);
745 if (already_tokenized(fullname))
746 return 1;
747 fd = open(fullname, O_RDONLY);
748 if (fd >= 0) {
749 char * streamname = __alloc_bytes(plen + flen);
750 memcpy(streamname, fullname, plen + flen);
751 *where = tokenize(streamname, fd, *where, next_path);
752 close(fd);
753 return 1;
755 return 0;
758 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
760 const char *path;
762 while ((path = *pptr++) != NULL) {
763 if (!try_include(path, filename, flen, list, pptr))
764 continue;
765 return 1;
767 return 0;
770 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
772 int flen = strlen(filename) + 1;
774 /* Absolute path? */
775 if (filename[0] == '/') {
776 if (try_include("", filename, flen, list, includepath))
777 return;
778 goto out;
781 /* Dir of input file is first dir to search for quoted includes */
782 set_stream_include_path(stream);
784 if (!path)
785 /* Do not search quote include if <> is in use */
786 path = local ? quote_includepath : angle_includepath;
788 /* Check the standard include paths.. */
789 if (do_include_path(path, list, token, filename, flen))
790 return;
791 out:
792 error_die(token->pos, "unable to open '%s'", filename);
795 static int free_preprocessor_line(struct token *token)
797 while (token_type(token) != TOKEN_EOF) {
798 struct token *free = token;
799 token = token->next;
800 __free_token(free);
802 return 1;
805 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
807 const char *filename;
808 struct token *next;
809 int expect;
811 next = token->next;
812 expect = '>';
813 if (!match_op(next, '<')) {
814 expand_list(&token->next);
815 expect = 0;
816 next = token;
817 if (match_op(token->next, '<')) {
818 next = token->next;
819 expect = '>';
822 token = next->next;
823 filename = token_name_sequence(token, expect, token);
824 do_include(!expect, stream, list, token, filename, path);
825 return 0;
828 static int handle_include(struct stream *stream, struct token **list, struct token *token)
830 return handle_include_path(stream, list, token, NULL);
833 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
835 return handle_include_path(stream, list, token, stream->next_path);
838 static int token_different(struct token *t1, struct token *t2)
840 int different;
842 if (token_type(t1) != token_type(t2))
843 return 1;
845 switch (token_type(t1)) {
846 case TOKEN_IDENT:
847 different = t1->ident != t2->ident;
848 break;
849 case TOKEN_ARG_COUNT:
850 case TOKEN_UNTAINT:
851 case TOKEN_CONCAT:
852 case TOKEN_GNU_KLUDGE:
853 different = 0;
854 break;
855 case TOKEN_NUMBER:
856 different = strcmp(t1->number, t2->number);
857 break;
858 case TOKEN_SPECIAL:
859 different = t1->special != t2->special;
860 break;
861 case TOKEN_MACRO_ARGUMENT:
862 case TOKEN_QUOTED_ARGUMENT:
863 case TOKEN_STR_ARGUMENT:
864 different = t1->argnum != t2->argnum;
865 break;
866 case TOKEN_CHAR:
867 case TOKEN_WIDE_CHAR:
868 different = t1->character != t2->character;
869 break;
870 case TOKEN_STRING:
871 case TOKEN_WIDE_STRING: {
872 struct string *s1, *s2;
874 s1 = t1->string;
875 s2 = t2->string;
876 different = 1;
877 if (s1->length != s2->length)
878 break;
879 different = memcmp(s1->data, s2->data, s1->length);
880 break;
882 default:
883 different = 1;
884 break;
886 return different;
889 static int token_list_different(struct token *list1, struct token *list2)
891 for (;;) {
892 if (list1 == list2)
893 return 0;
894 if (!list1 || !list2)
895 return 1;
896 if (token_different(list1, list2))
897 return 1;
898 list1 = list1->next;
899 list2 = list2->next;
903 static inline void set_arg_count(struct token *token)
905 token_type(token) = TOKEN_ARG_COUNT;
906 token->count.normal = token->count.quoted =
907 token->count.str = token->count.vararg = 0;
910 static struct token *parse_arguments(struct token *list)
912 struct token *arg = list->next, *next = list;
913 struct argcount *count = &list->count;
915 set_arg_count(list);
917 if (match_op(arg, ')')) {
918 next = arg->next;
919 list->next = &eof_token_entry;
920 return next;
923 while (token_type(arg) == TOKEN_IDENT) {
924 if (arg->ident == &__VA_ARGS___ident)
925 goto Eva_args;
926 if (!++count->normal)
927 goto Eargs;
928 next = arg->next;
930 if (match_op(next, ',')) {
931 set_arg_count(next);
932 arg = next->next;
933 continue;
936 if (match_op(next, ')')) {
937 set_arg_count(next);
938 next = next->next;
939 arg->next->next = &eof_token_entry;
940 return next;
943 /* normal cases are finished here */
945 if (match_op(next, SPECIAL_ELLIPSIS)) {
946 if (match_op(next->next, ')')) {
947 set_arg_count(next);
948 next->count.vararg = 1;
949 next = next->next;
950 arg->next->next = &eof_token_entry;
951 return next->next;
954 arg = next;
955 goto Enotclosed;
958 if (eof_token(next)) {
959 goto Enotclosed;
960 } else {
961 arg = next;
962 goto Ebadstuff;
966 if (match_op(arg, SPECIAL_ELLIPSIS)) {
967 next = arg->next;
968 token_type(arg) = TOKEN_IDENT;
969 arg->ident = &__VA_ARGS___ident;
970 if (!match_op(next, ')'))
971 goto Enotclosed;
972 if (!++count->normal)
973 goto Eargs;
974 set_arg_count(next);
975 next->count.vararg = 1;
976 next = next->next;
977 arg->next->next = &eof_token_entry;
978 return next;
981 if (eof_token(arg)) {
982 arg = next;
983 goto Enotclosed;
985 if (match_op(arg, ','))
986 goto Emissing;
987 else
988 goto Ebadstuff;
991 Emissing:
992 sparse_error(arg->pos, "parameter name missing");
993 return NULL;
994 Ebadstuff:
995 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
996 show_token(arg));
997 return NULL;
998 Enotclosed:
999 sparse_error(arg->pos, "missing ')' in macro parameter list");
1000 return NULL;
1001 Eva_args:
1002 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1003 return NULL;
1004 Eargs:
1005 sparse_error(arg->pos, "too many arguments in macro definition");
1006 return NULL;
1009 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1011 struct ident *ident = token->ident;
1012 int nr;
1014 if (!arglist || token_type(token) != TOKEN_IDENT)
1015 return 0;
1017 arglist = arglist->next;
1019 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1020 if (arglist->ident == ident) {
1021 struct argcount *count = &arglist->next->count;
1022 int n;
1024 token->argnum = nr;
1025 token_type(token) = type;
1026 switch (type) {
1027 case TOKEN_MACRO_ARGUMENT:
1028 n = ++count->normal;
1029 break;
1030 case TOKEN_QUOTED_ARGUMENT:
1031 n = ++count->quoted;
1032 break;
1033 default:
1034 n = ++count->str;
1036 if (n)
1037 return count->vararg ? 2 : 1;
1038 token_type(token) = TOKEN_ERROR;
1039 return -1;
1042 return 0;
1045 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1047 struct token *token = expansion;
1048 struct token **p;
1049 struct token *last = NULL;
1051 if (match_op(token, SPECIAL_HASHHASH))
1052 goto Econcat;
1054 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1055 if (match_op(token, '#')) {
1056 if (arglist) {
1057 struct token *next = token->next;
1058 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1059 goto Equote;
1060 next->pos.whitespace = token->pos.whitespace;
1061 token = *p = next;
1062 } else {
1063 token->pos.noexpand = 1;
1065 } else if (match_op(token, SPECIAL_HASHHASH)) {
1066 struct token *next = token->next;
1067 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
1068 token_type(token) = TOKEN_CONCAT;
1069 if (arg) {
1070 token = next;
1071 /* GNU kludge */
1072 if (arg == 2 && last && match_op(last, ',')) {
1073 token_type(last) = TOKEN_GNU_KLUDGE;
1074 last->next = token;
1076 } else if (match_op(next, SPECIAL_HASHHASH))
1077 token = next;
1078 else if (eof_token(next))
1079 goto Econcat;
1080 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
1081 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1082 } else {
1083 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1085 if (token_type(token) == TOKEN_ERROR)
1086 goto Earg;
1087 last = token;
1089 token = alloc_token(&expansion->pos);
1090 token_type(token) = TOKEN_UNTAINT;
1091 token->ident = name;
1092 token->next = *p;
1093 *p = token;
1094 return expansion;
1096 Equote:
1097 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1098 return NULL;
1100 Econcat:
1101 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1102 return NULL;
1103 Earg:
1104 sparse_error(token->pos, "too many instances of argument in body");
1105 return NULL;
1108 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1110 struct token *arglist, *expansion;
1111 struct token *left = token->next;
1112 struct symbol *sym;
1113 struct ident *name;
1114 int ret;
1116 if (token_type(left) != TOKEN_IDENT) {
1117 sparse_error(token->pos, "expected identifier to 'define'");
1118 return 1;
1121 name = left->ident;
1123 arglist = NULL;
1124 expansion = left->next;
1125 if (!expansion->pos.whitespace) {
1126 if (match_op(expansion, '(')) {
1127 arglist = expansion;
1128 expansion = parse_arguments(expansion);
1129 if (!expansion)
1130 return 1;
1131 } else if (!eof_token(expansion)) {
1132 warning(expansion->pos,
1133 "no whitespace before object-like macro body");
1137 expansion = parse_expansion(expansion, arglist, name);
1138 if (!expansion)
1139 return 1;
1141 ret = 1;
1142 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1143 if (sym) {
1144 int clean;
1146 if (attr < sym->attr)
1147 goto out;
1149 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1151 if (token_list_different(sym->expansion, expansion) ||
1152 token_list_different(sym->arglist, arglist)) {
1153 ret = 0;
1154 if ((clean && attr == SYM_ATTR_NORMAL)
1155 || sym->used_in == file_scope) {
1156 warning(left->pos, "preprocessor token %.*s redefined",
1157 name->len, name->name);
1158 info(sym->pos, "this was the original definition");
1160 } else if (clean)
1161 goto out;
1164 if (!sym || sym->scope != file_scope) {
1165 sym = alloc_symbol(left->pos, SYM_NODE);
1166 bind_symbol(sym, name, NS_MACRO);
1167 ret = 0;
1170 if (!ret) {
1171 sym->expansion = expansion;
1172 sym->arglist = arglist;
1173 __free_token(token); /* Free the "define" token, but not the rest of the line */
1176 sym->namespace = NS_MACRO;
1177 sym->used_in = NULL;
1178 sym->attr = attr;
1179 out:
1180 return ret;
1183 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1185 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1188 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1190 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1193 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1195 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1198 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1200 struct token *left = token->next;
1201 struct symbol *sym;
1203 if (token_type(left) != TOKEN_IDENT) {
1204 sparse_error(token->pos, "expected identifier to 'undef'");
1205 return 1;
1208 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1209 if (sym) {
1210 if (attr < sym->attr)
1211 return 1;
1212 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1213 return 1;
1214 } else if (attr <= SYM_ATTR_NORMAL)
1215 return 1;
1217 if (!sym || sym->scope != file_scope) {
1218 sym = alloc_symbol(left->pos, SYM_NODE);
1219 bind_symbol(sym, left->ident, NS_MACRO);
1222 sym->namespace = NS_UNDEF;
1223 sym->used_in = NULL;
1224 sym->attr = attr;
1226 return 1;
1229 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1231 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1234 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1236 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1239 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1241 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1242 free_preprocessor_line(token->next);
1243 token->next = stream->top_if;
1244 stream->top_if = token;
1245 if (false_nesting || true != 1)
1246 false_nesting++;
1247 return 0;
1250 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1252 struct token *next = token->next;
1253 int arg;
1254 if (token_type(next) == TOKEN_IDENT) {
1255 arg = token_defined(next);
1256 } else {
1257 dirty_stream(stream);
1258 if (!false_nesting)
1259 sparse_error(token->pos, "expected preprocessor identifier");
1260 arg = -1;
1262 return preprocessor_if(stream, token, arg);
1265 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1267 struct token *next = token->next;
1268 int arg;
1269 if (token_type(next) == TOKEN_IDENT) {
1270 if (!stream->dirty && !stream->ifndef) {
1271 if (!stream->protect) {
1272 stream->ifndef = token;
1273 stream->protect = next->ident;
1274 } else if (stream->protect == next->ident) {
1275 stream->ifndef = token;
1276 stream->dirty = 1;
1279 arg = !token_defined(next);
1280 } else {
1281 dirty_stream(stream);
1282 if (!false_nesting)
1283 sparse_error(token->pos, "expected preprocessor identifier");
1284 arg = -1;
1287 return preprocessor_if(stream, token, arg);
1291 * Expression handling for #if and #elif; it differs from normal expansion
1292 * due to special treatment of "defined".
1294 static int expression_value(struct token **where)
1296 struct expression *expr;
1297 struct token *p;
1298 struct token **list = where, **beginning = NULL;
1299 long long value;
1300 int state = 0;
1302 while (!eof_token(p = scan_next(list))) {
1303 switch (state) {
1304 case 0:
1305 if (token_type(p) != TOKEN_IDENT)
1306 break;
1307 if (p->ident == &defined_ident) {
1308 state = 1;
1309 beginning = list;
1310 break;
1312 if (!expand_one_symbol(list))
1313 continue;
1314 if (token_type(p) != TOKEN_IDENT)
1315 break;
1316 token_type(p) = TOKEN_ZERO_IDENT;
1317 break;
1318 case 1:
1319 if (match_op(p, '(')) {
1320 state = 2;
1321 } else {
1322 state = 0;
1323 replace_with_defined(p);
1324 *beginning = p;
1326 break;
1327 case 2:
1328 if (token_type(p) == TOKEN_IDENT)
1329 state = 3;
1330 else
1331 state = 0;
1332 replace_with_defined(p);
1333 *beginning = p;
1334 break;
1335 case 3:
1336 state = 0;
1337 if (!match_op(p, ')'))
1338 sparse_error(p->pos, "missing ')' after \"defined\"");
1339 *list = p->next;
1340 continue;
1342 list = &p->next;
1345 p = constant_expression(*where, &expr);
1346 if (!eof_token(p))
1347 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p));
1348 value = get_expression_value(expr);
1349 return value != 0;
1352 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1354 int value = 0;
1355 if (!false_nesting)
1356 value = expression_value(&token->next);
1358 dirty_stream(stream);
1359 return preprocessor_if(stream, token, value);
1362 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1364 struct token *top_if = stream->top_if;
1365 end_group(stream);
1367 if (!top_if) {
1368 nesting_error(stream);
1369 sparse_error(token->pos, "unmatched #elif within stream");
1370 return 1;
1373 if (token_type(top_if) == TOKEN_ELSE) {
1374 nesting_error(stream);
1375 sparse_error(token->pos, "#elif after #else");
1376 if (!false_nesting)
1377 false_nesting = 1;
1378 return 1;
1381 dirty_stream(stream);
1382 if (token_type(top_if) != TOKEN_IF)
1383 return 1;
1384 if (false_nesting) {
1385 false_nesting = 0;
1386 if (!expression_value(&token->next))
1387 false_nesting = 1;
1388 } else {
1389 false_nesting = 1;
1390 token_type(top_if) = TOKEN_SKIP_GROUPS;
1392 return 1;
1395 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1397 struct token *top_if = stream->top_if;
1398 end_group(stream);
1400 if (!top_if) {
1401 nesting_error(stream);
1402 sparse_error(token->pos, "unmatched #else within stream");
1403 return 1;
1406 if (token_type(top_if) == TOKEN_ELSE) {
1407 nesting_error(stream);
1408 sparse_error(token->pos, "#else after #else");
1410 if (false_nesting) {
1411 if (token_type(top_if) == TOKEN_IF)
1412 false_nesting = 0;
1413 } else {
1414 false_nesting = 1;
1416 token_type(top_if) = TOKEN_ELSE;
1417 return 1;
1420 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1422 struct token *top_if = stream->top_if;
1423 end_group(stream);
1424 if (!top_if) {
1425 nesting_error(stream);
1426 sparse_error(token->pos, "unmatched #endif in stream");
1427 return 1;
1429 if (false_nesting)
1430 false_nesting--;
1431 stream->top_if = top_if->next;
1432 __free_token(top_if);
1433 return 1;
1436 static const char *show_token_sequence(struct token *token)
1438 static char buffer[1024];
1439 char *ptr = buffer;
1440 int whitespace = 0;
1442 if (!token)
1443 return "<none>";
1444 while (!eof_token(token)) {
1445 const char *val = show_token(token);
1446 int len = strlen(val);
1448 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1449 sparse_error(token->pos, "too long token expansion");
1450 break;
1453 if (whitespace)
1454 *ptr++ = ' ';
1455 memcpy(ptr, val, len);
1456 ptr += len;
1457 token = token->next;
1458 whitespace = token->pos.whitespace;
1460 *ptr = 0;
1461 return buffer;
1464 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1466 warning(token->pos, "%s", show_token_sequence(token->next));
1467 return 1;
1470 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1472 sparse_error(token->pos, "%s", show_token_sequence(token->next));
1473 return 1;
1476 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1479 * Do we have any non-system includes?
1480 * Clear them out if so..
1482 *sys_includepath = NULL;
1483 return 1;
1486 static inline void update_inc_ptrs(const char ***where)
1489 if (*where <= dirafter_includepath) {
1490 dirafter_includepath++;
1491 /* If this was the entry that we prepend, don't
1492 * rise the lower entries, even if they are at
1493 * the same level. */
1494 if (where == &dirafter_includepath)
1495 return;
1497 if (*where <= sys_includepath) {
1498 sys_includepath++;
1499 if (where == &sys_includepath)
1500 return;
1502 if (*where <= isys_includepath) {
1503 isys_includepath++;
1504 if (where == &isys_includepath)
1505 return;
1508 /* angle_includepath is actually never updated, since we
1509 * don't suppport -iquote rught now. May change some day. */
1510 if (*where <= angle_includepath) {
1511 angle_includepath++;
1512 if (where == &angle_includepath)
1513 return;
1517 /* Add a path before 'where' and update the pointers associated with the
1518 * includepath array */
1519 static void add_path_entry(struct token *token, const char *path,
1520 const char ***where)
1522 const char **dst;
1523 const char *next;
1525 /* Need one free entry.. */
1526 if (includepath[INCLUDEPATHS-2])
1527 error_die(token->pos, "too many include path entries");
1529 /* check that this is not a duplicate */
1530 dst = includepath;
1531 while (*dst) {
1532 if (strcmp(*dst, path) == 0)
1533 return;
1534 dst++;
1536 next = path;
1537 dst = *where;
1539 update_inc_ptrs(where);
1542 * Move them all up starting at dst,
1543 * insert the new entry..
1545 do {
1546 const char *tmp = *dst;
1547 *dst = next;
1548 next = tmp;
1549 dst++;
1550 } while (next);
1553 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1555 for (;;) {
1556 token = token->next;
1557 if (eof_token(token))
1558 return 1;
1559 if (token_type(token) != TOKEN_STRING) {
1560 warning(token->pos, "expected path string");
1561 return 1;
1563 add_path_entry(token, token->string->data, &isys_includepath);
1567 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1569 for (;;) {
1570 token = token->next;
1571 if (eof_token(token))
1572 return 1;
1573 if (token_type(token) != TOKEN_STRING) {
1574 sparse_error(token->pos, "expected path string");
1575 return 1;
1577 add_path_entry(token, token->string->data, &sys_includepath);
1581 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1583 for (;;) {
1584 token = token->next;
1585 if (eof_token(token))
1586 return 1;
1587 if (token_type(token) != TOKEN_STRING) {
1588 sparse_error(token->pos, "expected path string");
1589 return 1;
1591 add_path_entry(token, token->string->data, &dirafter_includepath);
1595 /* Add to end on includepath list - no pointer updates */
1596 static void add_dirafter_entry(struct token *token, const char *path)
1598 const char **dst = includepath;
1600 /* Need one free entry.. */
1601 if (includepath[INCLUDEPATHS-2])
1602 error_die(token->pos, "too many include path entries");
1604 /* Add to the end */
1605 while (*dst)
1606 dst++;
1607 *dst = path;
1608 dst++;
1609 *dst = NULL;
1612 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1614 for (;;) {
1615 token = token->next;
1616 if (eof_token(token))
1617 return 1;
1618 if (token_type(token) != TOKEN_STRING) {
1619 sparse_error(token->pos, "expected path string");
1620 return 1;
1622 add_dirafter_entry(token, token->string->data);
1626 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1629 * -I-
1630 * From info gcc:
1631 * Split the include path. Any directories specified with `-I'
1632 * options before `-I-' are searched only for headers requested with
1633 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1634 * If additional directories are specified with `-I' options after
1635 * the `-I-', those directories are searched for all `#include'
1636 * directives.
1637 * In addition, `-I-' inhibits the use of the directory of the current
1638 * file directory as the first search directory for `#include "FILE"'.
1640 quote_includepath = includepath+1;
1641 angle_includepath = sys_includepath;
1642 return 1;
1646 * We replace "#pragma xxx" with "__pragma__" in the token
1647 * stream. Just as an example.
1649 * We'll just #define that away for now, but the theory here
1650 * is that we can use this to insert arbitrary token sequences
1651 * to turn the pragmas into internal front-end sequences for
1652 * when we actually start caring about them.
1654 * So eventually this will turn into some kind of extended
1655 * __attribute__() like thing, except called __pragma__(xxx).
1657 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1659 struct token *next = *line;
1661 token->ident = &pragma_ident;
1662 token->pos.newline = 1;
1663 token->pos.whitespace = 1;
1664 token->pos.pos = 1;
1665 *line = token;
1666 token->next = next;
1667 return 0;
1671 * We ignore #line for now.
1673 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1675 return 1;
1678 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1680 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1681 return 1;
1685 static void init_preprocessor(void)
1687 int i;
1688 int stream = init_stream("preprocessor", -1, includepath);
1689 static struct {
1690 const char *name;
1691 int (*handler)(struct stream *, struct token **, struct token *);
1692 } normal[] = {
1693 { "define", handle_define },
1694 { "weak_define", handle_weak_define },
1695 { "strong_define", handle_strong_define },
1696 { "undef", handle_undef },
1697 { "strong_undef", handle_strong_undef },
1698 { "warning", handle_warning },
1699 { "error", handle_error },
1700 { "include", handle_include },
1701 { "include_next", handle_include_next },
1702 { "pragma", handle_pragma },
1703 { "line", handle_line },
1705 // our internal preprocessor tokens
1706 { "nostdinc", handle_nostdinc },
1707 { "add_include", handle_add_include },
1708 { "add_isystem", handle_add_isystem },
1709 { "add_system", handle_add_system },
1710 { "add_dirafter", handle_add_dirafter },
1711 { "split_include", handle_split_include },
1712 }, special[] = {
1713 { "ifdef", handle_ifdef },
1714 { "ifndef", handle_ifndef },
1715 { "else", handle_else },
1716 { "endif", handle_endif },
1717 { "if", handle_if },
1718 { "elif", handle_elif },
1721 for (i = 0; i < ARRAY_SIZE(normal); i++) {
1722 struct symbol *sym;
1723 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1724 sym->handler = normal[i].handler;
1725 sym->normal = 1;
1727 for (i = 0; i < ARRAY_SIZE(special); i++) {
1728 struct symbol *sym;
1729 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1730 sym->handler = special[i].handler;
1731 sym->normal = 0;
1736 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1738 int (*handler)(struct stream *, struct token **, struct token *);
1739 struct token *token = start->next;
1740 int is_normal = 1;
1742 if (eof_token(token))
1743 return;
1745 if (token_type(token) == TOKEN_IDENT) {
1746 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1747 if (sym) {
1748 handler = sym->handler;
1749 is_normal = sym->normal;
1750 } else {
1751 handler = handle_nondirective;
1753 } else if (token_type(token) == TOKEN_NUMBER) {
1754 handler = handle_line;
1755 } else {
1756 handler = handle_nondirective;
1759 if (is_normal) {
1760 dirty_stream(stream);
1761 if (false_nesting)
1762 goto out;
1764 if (!handler(stream, line, token)) /* all set */
1765 return;
1767 out:
1768 free_preprocessor_line(token);
1771 static void preprocessor_line(struct stream *stream, struct token **line)
1773 struct token *start = *line, *next;
1774 struct token **tp = &start->next;
1776 for (;;) {
1777 next = *tp;
1778 if (next->pos.newline)
1779 break;
1780 tp = &next->next;
1782 *line = next;
1783 *tp = &eof_token_entry;
1784 handle_preprocessor_line(stream, line, start);
1787 static void do_preprocess(struct token **list)
1789 struct token *next;
1791 while (!eof_token(next = scan_next(list))) {
1792 struct stream *stream = input_streams + next->pos.stream;
1794 if (next->pos.newline && match_op(next, '#')) {
1795 if (!next->pos.noexpand) {
1796 preprocessor_line(stream, list);
1797 __free_token(next); /* Free the '#' token */
1798 continue;
1802 switch (token_type(next)) {
1803 case TOKEN_STREAMEND:
1804 if (stream->top_if) {
1805 nesting_error(stream);
1806 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1807 stream->top_if = NULL;
1808 false_nesting = 0;
1810 if (!stream->dirty)
1811 stream->constant = CONSTANT_FILE_YES;
1812 *list = next->next;
1813 continue;
1814 case TOKEN_STREAMBEGIN:
1815 *list = next->next;
1816 continue;
1818 default:
1819 dirty_stream(stream);
1820 if (false_nesting) {
1821 *list = next->next;
1822 __free_token(next);
1823 continue;
1826 if (token_type(next) != TOKEN_IDENT ||
1827 expand_one_symbol(list))
1828 list = &next->next;
1833 struct token * preprocess(struct token *token)
1835 preprocessing = 1;
1836 init_preprocessor();
1837 do_preprocess(&token);
1839 // Drop all expressions from preprocessing, they're not used any more.
1840 // This is not true when we have multiple files, though ;/
1841 // clear_expression_alloc();
1842 preprocessing = 0;
1844 return token;