introduce in_macro() which returns true if we're in a macro
[smatch.git] / pre-process.c
blob34b21ff02f627ba8b04af3d87d9a548a03c007d8
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 struct token *last;
636 static char buffer[256];
637 char *ptr = buffer;
639 last = token;
640 while (!eof_token(token) && !match_op(token, endop)) {
641 int len;
642 const char *val = token->string->data;
643 if (token_type(token) != TOKEN_STRING)
644 val = show_token(token);
645 len = strlen(val);
646 memcpy(ptr, val, len);
647 ptr += len;
648 token = token->next;
650 *ptr = 0;
651 if (endop && !match_op(token, endop))
652 sparse_error(start->pos, "expected '>' at end of filename");
653 return buffer;
656 static int already_tokenized(const char *path)
658 int i;
659 struct stream *s = input_streams;
661 for (i = input_stream_nr; --i >= 0; s++) {
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 different = t1->character != t2->character;
868 break;
869 case TOKEN_STRING: {
870 struct string *s1, *s2;
872 s1 = t1->string;
873 s2 = t2->string;
874 different = 1;
875 if (s1->length != s2->length)
876 break;
877 different = memcmp(s1->data, s2->data, s1->length);
878 break;
880 default:
881 different = 1;
882 break;
884 return different;
887 static int token_list_different(struct token *list1, struct token *list2)
889 for (;;) {
890 if (list1 == list2)
891 return 0;
892 if (!list1 || !list2)
893 return 1;
894 if (token_different(list1, list2))
895 return 1;
896 list1 = list1->next;
897 list2 = list2->next;
901 static inline void set_arg_count(struct token *token)
903 token_type(token) = TOKEN_ARG_COUNT;
904 token->count.normal = token->count.quoted =
905 token->count.str = token->count.vararg = 0;
908 static struct token *parse_arguments(struct token *list)
910 struct token *arg = list->next, *next = list;
911 struct argcount *count = &list->count;
913 set_arg_count(list);
915 if (match_op(arg, ')')) {
916 next = arg->next;
917 list->next = &eof_token_entry;
918 return next;
921 while (token_type(arg) == TOKEN_IDENT) {
922 if (arg->ident == &__VA_ARGS___ident)
923 goto Eva_args;
924 if (!++count->normal)
925 goto Eargs;
926 next = arg->next;
928 if (match_op(next, ',')) {
929 set_arg_count(next);
930 arg = next->next;
931 continue;
934 if (match_op(next, ')')) {
935 set_arg_count(next);
936 next = next->next;
937 arg->next->next = &eof_token_entry;
938 return next;
941 /* normal cases are finished here */
943 if (match_op(next, SPECIAL_ELLIPSIS)) {
944 if (match_op(next->next, ')')) {
945 set_arg_count(next);
946 next->count.vararg = 1;
947 next = next->next;
948 arg->next->next = &eof_token_entry;
949 return next->next;
952 arg = next;
953 goto Enotclosed;
956 if (eof_token(next)) {
957 goto Enotclosed;
958 } else {
959 arg = next;
960 goto Ebadstuff;
964 if (match_op(arg, SPECIAL_ELLIPSIS)) {
965 next = arg->next;
966 token_type(arg) = TOKEN_IDENT;
967 arg->ident = &__VA_ARGS___ident;
968 if (!match_op(next, ')'))
969 goto Enotclosed;
970 if (!++count->normal)
971 goto Eargs;
972 set_arg_count(next);
973 next->count.vararg = 1;
974 next = next->next;
975 arg->next->next = &eof_token_entry;
976 return next;
979 if (eof_token(arg)) {
980 arg = next;
981 goto Enotclosed;
983 if (match_op(arg, ','))
984 goto Emissing;
985 else
986 goto Ebadstuff;
989 Emissing:
990 sparse_error(arg->pos, "parameter name missing");
991 return NULL;
992 Ebadstuff:
993 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
994 show_token(arg));
995 return NULL;
996 Enotclosed:
997 sparse_error(arg->pos, "missing ')' in macro parameter list");
998 return NULL;
999 Eva_args:
1000 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1001 return NULL;
1002 Eargs:
1003 sparse_error(arg->pos, "too many arguments in macro definition");
1004 return NULL;
1007 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1009 struct ident *ident = token->ident;
1010 int nr;
1012 if (!arglist || token_type(token) != TOKEN_IDENT)
1013 return 0;
1015 arglist = arglist->next;
1017 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1018 if (arglist->ident == ident) {
1019 struct argcount *count = &arglist->next->count;
1020 int n;
1022 token->argnum = nr;
1023 token_type(token) = type;
1024 switch (type) {
1025 case TOKEN_MACRO_ARGUMENT:
1026 n = ++count->normal;
1027 break;
1028 case TOKEN_QUOTED_ARGUMENT:
1029 n = ++count->quoted;
1030 break;
1031 default:
1032 n = ++count->str;
1034 if (n)
1035 return count->vararg ? 2 : 1;
1036 token_type(token) = TOKEN_ERROR;
1037 return -1;
1040 return 0;
1043 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1045 struct token *token = expansion;
1046 struct token **p;
1047 struct token *last = NULL;
1049 if (match_op(token, SPECIAL_HASHHASH))
1050 goto Econcat;
1052 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1053 if (match_op(token, '#')) {
1054 if (arglist) {
1055 struct token *next = token->next;
1056 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1057 goto Equote;
1058 next->pos.whitespace = token->pos.whitespace;
1059 token = *p = next;
1060 } else {
1061 token->pos.noexpand = 1;
1063 } else if (match_op(token, SPECIAL_HASHHASH)) {
1064 struct token *next = token->next;
1065 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
1066 token_type(token) = TOKEN_CONCAT;
1067 if (arg) {
1068 token = next;
1069 /* GNU kludge */
1070 if (arg == 2 && last && match_op(last, ',')) {
1071 token_type(last) = TOKEN_GNU_KLUDGE;
1072 last->next = token;
1074 } else if (match_op(next, SPECIAL_HASHHASH))
1075 token = next;
1076 else if (eof_token(next))
1077 goto Econcat;
1078 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
1079 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1080 } else {
1081 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1083 if (token_type(token) == TOKEN_ERROR)
1084 goto Earg;
1085 last = token;
1087 token = alloc_token(&expansion->pos);
1088 token_type(token) = TOKEN_UNTAINT;
1089 token->ident = name;
1090 token->next = *p;
1091 *p = token;
1092 return expansion;
1094 Equote:
1095 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1096 return NULL;
1098 Econcat:
1099 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1100 return NULL;
1101 Earg:
1102 sparse_error(token->pos, "too many instances of argument in body");
1103 return NULL;
1106 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1108 struct token *arglist, *expansion;
1109 struct token *left = token->next;
1110 struct symbol *sym;
1111 struct ident *name;
1112 int ret;
1114 if (token_type(left) != TOKEN_IDENT) {
1115 sparse_error(token->pos, "expected identifier to 'define'");
1116 return 1;
1119 name = left->ident;
1121 arglist = NULL;
1122 expansion = left->next;
1123 if (!expansion->pos.whitespace) {
1124 if (match_op(expansion, '(')) {
1125 arglist = expansion;
1126 expansion = parse_arguments(expansion);
1127 if (!expansion)
1128 return 1;
1129 } else if (!eof_token(expansion)) {
1130 warning(expansion->pos,
1131 "no whitespace before object-like macro body");
1135 expansion = parse_expansion(expansion, arglist, name);
1136 if (!expansion)
1137 return 1;
1139 ret = 1;
1140 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1141 if (sym) {
1142 int clean;
1144 if (attr < sym->attr)
1145 goto out;
1147 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1149 if (token_list_different(sym->expansion, expansion) ||
1150 token_list_different(sym->arglist, arglist)) {
1151 ret = 0;
1152 if ((clean && attr == SYM_ATTR_NORMAL)
1153 || sym->used_in == file_scope) {
1154 warning(left->pos, "preprocessor token %.*s redefined",
1155 name->len, name->name);
1156 info(sym->pos, "this was the original definition");
1158 } else if (clean)
1159 goto out;
1162 if (!sym || sym->scope != file_scope) {
1163 sym = alloc_symbol(left->pos, SYM_NODE);
1164 bind_symbol(sym, name, NS_MACRO);
1165 ret = 0;
1168 if (!ret) {
1169 sym->expansion = expansion;
1170 sym->arglist = arglist;
1171 __free_token(token); /* Free the "define" token, but not the rest of the line */
1174 sym->namespace = NS_MACRO;
1175 sym->used_in = NULL;
1176 sym->attr = attr;
1177 out:
1178 return ret;
1181 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1183 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1186 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1188 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1191 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1193 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1196 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1198 struct token *left = token->next;
1199 struct symbol *sym;
1201 if (token_type(left) != TOKEN_IDENT) {
1202 sparse_error(token->pos, "expected identifier to 'undef'");
1203 return 1;
1206 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1207 if (sym) {
1208 if (attr < sym->attr)
1209 return 1;
1210 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1211 return 1;
1212 } else if (attr <= SYM_ATTR_NORMAL)
1213 return 1;
1215 if (!sym || sym->scope != file_scope) {
1216 sym = alloc_symbol(left->pos, SYM_NODE);
1217 bind_symbol(sym, left->ident, NS_MACRO);
1220 sym->namespace = NS_UNDEF;
1221 sym->used_in = NULL;
1222 sym->attr = attr;
1224 return 1;
1227 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1229 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1232 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1234 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1237 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1239 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1240 free_preprocessor_line(token->next);
1241 token->next = stream->top_if;
1242 stream->top_if = token;
1243 if (false_nesting || true != 1)
1244 false_nesting++;
1245 return 0;
1248 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1250 struct token *next = token->next;
1251 int arg;
1252 if (token_type(next) == TOKEN_IDENT) {
1253 arg = token_defined(next);
1254 } else {
1255 dirty_stream(stream);
1256 if (!false_nesting)
1257 sparse_error(token->pos, "expected preprocessor identifier");
1258 arg = -1;
1260 return preprocessor_if(stream, token, arg);
1263 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1265 struct token *next = token->next;
1266 int arg;
1267 if (token_type(next) == TOKEN_IDENT) {
1268 if (!stream->dirty && !stream->ifndef) {
1269 if (!stream->protect) {
1270 stream->ifndef = token;
1271 stream->protect = next->ident;
1272 } else if (stream->protect == next->ident) {
1273 stream->ifndef = token;
1274 stream->dirty = 1;
1277 arg = !token_defined(next);
1278 } else {
1279 dirty_stream(stream);
1280 if (!false_nesting)
1281 sparse_error(token->pos, "expected preprocessor identifier");
1282 arg = -1;
1285 return preprocessor_if(stream, token, arg);
1289 * Expression handling for #if and #elif; it differs from normal expansion
1290 * due to special treatment of "defined".
1292 static int expression_value(struct token **where)
1294 struct expression *expr;
1295 struct token *p;
1296 struct token **list = where, **beginning = NULL;
1297 long long value;
1298 int state = 0;
1300 while (!eof_token(p = scan_next(list))) {
1301 switch (state) {
1302 case 0:
1303 if (token_type(p) != TOKEN_IDENT)
1304 break;
1305 if (p->ident == &defined_ident) {
1306 state = 1;
1307 beginning = list;
1308 break;
1310 if (!expand_one_symbol(list))
1311 continue;
1312 if (token_type(p) != TOKEN_IDENT)
1313 break;
1314 token_type(p) = TOKEN_ZERO_IDENT;
1315 break;
1316 case 1:
1317 if (match_op(p, '(')) {
1318 state = 2;
1319 } else {
1320 state = 0;
1321 replace_with_defined(p);
1322 *beginning = p;
1324 break;
1325 case 2:
1326 if (token_type(p) == TOKEN_IDENT)
1327 state = 3;
1328 else
1329 state = 0;
1330 replace_with_defined(p);
1331 *beginning = p;
1332 break;
1333 case 3:
1334 state = 0;
1335 if (!match_op(p, ')'))
1336 sparse_error(p->pos, "missing ')' after \"defined\"");
1337 *list = p->next;
1338 continue;
1340 list = &p->next;
1343 p = constant_expression(*where, &expr);
1344 if (!eof_token(p))
1345 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p));
1346 value = get_expression_value(expr);
1347 return value != 0;
1350 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1352 int value = 0;
1353 if (!false_nesting)
1354 value = expression_value(&token->next);
1356 dirty_stream(stream);
1357 return preprocessor_if(stream, token, value);
1360 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1362 struct token *top_if = stream->top_if;
1363 end_group(stream);
1365 if (!top_if) {
1366 nesting_error(stream);
1367 sparse_error(token->pos, "unmatched #elif within stream");
1368 return 1;
1371 if (token_type(top_if) == TOKEN_ELSE) {
1372 nesting_error(stream);
1373 sparse_error(token->pos, "#elif after #else");
1374 if (!false_nesting)
1375 false_nesting = 1;
1376 return 1;
1379 dirty_stream(stream);
1380 if (token_type(top_if) != TOKEN_IF)
1381 return 1;
1382 if (false_nesting) {
1383 false_nesting = 0;
1384 if (!expression_value(&token->next))
1385 false_nesting = 1;
1386 } else {
1387 false_nesting = 1;
1388 token_type(top_if) = TOKEN_SKIP_GROUPS;
1390 return 1;
1393 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1395 struct token *top_if = stream->top_if;
1396 end_group(stream);
1398 if (!top_if) {
1399 nesting_error(stream);
1400 sparse_error(token->pos, "unmatched #else within stream");
1401 return 1;
1404 if (token_type(top_if) == TOKEN_ELSE) {
1405 nesting_error(stream);
1406 sparse_error(token->pos, "#else after #else");
1408 if (false_nesting) {
1409 if (token_type(top_if) == TOKEN_IF)
1410 false_nesting = 0;
1411 } else {
1412 false_nesting = 1;
1414 token_type(top_if) = TOKEN_ELSE;
1415 return 1;
1418 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1420 struct token *top_if = stream->top_if;
1421 end_group(stream);
1422 if (!top_if) {
1423 nesting_error(stream);
1424 sparse_error(token->pos, "unmatched #endif in stream");
1425 return 1;
1427 if (false_nesting)
1428 false_nesting--;
1429 stream->top_if = top_if->next;
1430 __free_token(top_if);
1431 return 1;
1434 static const char *show_token_sequence(struct token *token)
1436 static char buffer[1024];
1437 char *ptr = buffer;
1438 int whitespace = 0;
1440 if (!token)
1441 return "<none>";
1442 while (!eof_token(token)) {
1443 const char *val = show_token(token);
1444 int len = strlen(val);
1446 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1447 sparse_error(token->pos, "too long token expansion");
1448 break;
1451 if (whitespace)
1452 *ptr++ = ' ';
1453 memcpy(ptr, val, len);
1454 ptr += len;
1455 token = token->next;
1456 whitespace = token->pos.whitespace;
1458 *ptr = 0;
1459 return buffer;
1462 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1464 warning(token->pos, "%s", show_token_sequence(token->next));
1465 return 1;
1468 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1470 sparse_error(token->pos, "%s", show_token_sequence(token->next));
1471 return 1;
1474 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1477 * Do we have any non-system includes?
1478 * Clear them out if so..
1480 *sys_includepath = NULL;
1481 return 1;
1484 static inline void update_inc_ptrs(const char ***where)
1487 if (*where <= dirafter_includepath) {
1488 dirafter_includepath++;
1489 /* If this was the entry that we prepend, don't
1490 * rise the lower entries, even if they are at
1491 * the same level. */
1492 if (where == &dirafter_includepath)
1493 return;
1495 if (*where <= sys_includepath) {
1496 sys_includepath++;
1497 if (where == &sys_includepath)
1498 return;
1500 if (*where <= isys_includepath) {
1501 isys_includepath++;
1502 if (where == &isys_includepath)
1503 return;
1506 /* angle_includepath is actually never updated, since we
1507 * don't suppport -iquote rught now. May change some day. */
1508 if (*where <= angle_includepath) {
1509 angle_includepath++;
1510 if (where == &angle_includepath)
1511 return;
1515 /* Add a path before 'where' and update the pointers associated with the
1516 * includepath array */
1517 static void add_path_entry(struct token *token, const char *path,
1518 const char ***where)
1520 const char **dst;
1521 const char *next;
1523 /* Need one free entry.. */
1524 if (includepath[INCLUDEPATHS-2])
1525 error_die(token->pos, "too many include path entries");
1527 /* check that this is not a duplicate */
1528 dst = includepath;
1529 while (*dst) {
1530 if (strcmp(*dst, path) == 0)
1531 return;
1532 dst++;
1534 next = path;
1535 dst = *where;
1537 update_inc_ptrs(where);
1540 * Move them all up starting at dst,
1541 * insert the new entry..
1543 do {
1544 const char *tmp = *dst;
1545 *dst = next;
1546 next = tmp;
1547 dst++;
1548 } while (next);
1551 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1553 for (;;) {
1554 token = token->next;
1555 if (eof_token(token))
1556 return 1;
1557 if (token_type(token) != TOKEN_STRING) {
1558 warning(token->pos, "expected path string");
1559 return 1;
1561 add_path_entry(token, token->string->data, &isys_includepath);
1565 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1567 for (;;) {
1568 token = token->next;
1569 if (eof_token(token))
1570 return 1;
1571 if (token_type(token) != TOKEN_STRING) {
1572 sparse_error(token->pos, "expected path string");
1573 return 1;
1575 add_path_entry(token, token->string->data, &sys_includepath);
1579 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1581 for (;;) {
1582 token = token->next;
1583 if (eof_token(token))
1584 return 1;
1585 if (token_type(token) != TOKEN_STRING) {
1586 sparse_error(token->pos, "expected path string");
1587 return 1;
1589 add_path_entry(token, token->string->data, &dirafter_includepath);
1593 /* Add to end on includepath list - no pointer updates */
1594 static void add_dirafter_entry(struct token *token, const char *path)
1596 const char **dst = includepath;
1598 /* Need one free entry.. */
1599 if (includepath[INCLUDEPATHS-2])
1600 error_die(token->pos, "too many include path entries");
1602 /* Add to the end */
1603 while (*dst)
1604 dst++;
1605 *dst = path;
1606 dst++;
1607 *dst = NULL;
1610 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1612 for (;;) {
1613 token = token->next;
1614 if (eof_token(token))
1615 return 1;
1616 if (token_type(token) != TOKEN_STRING) {
1617 sparse_error(token->pos, "expected path string");
1618 return 1;
1620 add_dirafter_entry(token, token->string->data);
1624 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1627 * -I-
1628 * From info gcc:
1629 * Split the include path. Any directories specified with `-I'
1630 * options before `-I-' are searched only for headers requested with
1631 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1632 * If additional directories are specified with `-I' options after
1633 * the `-I-', those directories are searched for all `#include'
1634 * directives.
1635 * In addition, `-I-' inhibits the use of the directory of the current
1636 * file directory as the first search directory for `#include "FILE"'.
1638 quote_includepath = includepath+1;
1639 angle_includepath = sys_includepath;
1640 return 1;
1644 * We replace "#pragma xxx" with "__pragma__" in the token
1645 * stream. Just as an example.
1647 * We'll just #define that away for now, but the theory here
1648 * is that we can use this to insert arbitrary token sequences
1649 * to turn the pragmas into internal front-end sequences for
1650 * when we actually start caring about them.
1652 * So eventually this will turn into some kind of extended
1653 * __attribute__() like thing, except called __pragma__(xxx).
1655 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1657 struct token *next = *line;
1659 token->ident = &pragma_ident;
1660 token->pos.newline = 1;
1661 token->pos.whitespace = 1;
1662 token->pos.pos = 1;
1663 *line = token;
1664 token->next = next;
1665 return 0;
1669 * We ignore #line for now.
1671 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1673 return 1;
1676 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1678 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1679 return 1;
1683 static void init_preprocessor(void)
1685 int i;
1686 int stream = init_stream("preprocessor", -1, includepath);
1687 static struct {
1688 const char *name;
1689 int (*handler)(struct stream *, struct token **, struct token *);
1690 } normal[] = {
1691 { "define", handle_define },
1692 { "weak_define", handle_weak_define },
1693 { "strong_define", handle_strong_define },
1694 { "undef", handle_undef },
1695 { "strong_undef", handle_strong_undef },
1696 { "warning", handle_warning },
1697 { "error", handle_error },
1698 { "include", handle_include },
1699 { "include_next", handle_include_next },
1700 { "pragma", handle_pragma },
1701 { "line", handle_line },
1703 // our internal preprocessor tokens
1704 { "nostdinc", handle_nostdinc },
1705 { "add_include", handle_add_include },
1706 { "add_isystem", handle_add_isystem },
1707 { "add_system", handle_add_system },
1708 { "add_dirafter", handle_add_dirafter },
1709 { "split_include", handle_split_include },
1710 }, special[] = {
1711 { "ifdef", handle_ifdef },
1712 { "ifndef", handle_ifndef },
1713 { "else", handle_else },
1714 { "endif", handle_endif },
1715 { "if", handle_if },
1716 { "elif", handle_elif },
1719 for (i = 0; i < (sizeof (normal) / sizeof (normal[0])); i++) {
1720 struct symbol *sym;
1721 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1722 sym->handler = normal[i].handler;
1723 sym->normal = 1;
1725 for (i = 0; i < (sizeof (special) / sizeof (special[0])); i++) {
1726 struct symbol *sym;
1727 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1728 sym->handler = special[i].handler;
1729 sym->normal = 0;
1734 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1736 int (*handler)(struct stream *, struct token **, struct token *);
1737 struct token *token = start->next;
1738 int is_normal = 1;
1740 if (eof_token(token))
1741 return;
1743 if (token_type(token) == TOKEN_IDENT) {
1744 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1745 if (sym) {
1746 handler = sym->handler;
1747 is_normal = sym->normal;
1748 } else {
1749 handler = handle_nondirective;
1751 } else if (token_type(token) == TOKEN_NUMBER) {
1752 handler = handle_line;
1753 } else {
1754 handler = handle_nondirective;
1757 if (is_normal) {
1758 dirty_stream(stream);
1759 if (false_nesting)
1760 goto out;
1762 if (!handler(stream, line, token)) /* all set */
1763 return;
1765 out:
1766 free_preprocessor_line(token);
1769 static void preprocessor_line(struct stream *stream, struct token **line)
1771 struct token *start = *line, *next;
1772 struct token **tp = &start->next;
1774 for (;;) {
1775 next = *tp;
1776 if (next->pos.newline)
1777 break;
1778 tp = &next->next;
1780 *line = next;
1781 *tp = &eof_token_entry;
1782 handle_preprocessor_line(stream, line, start);
1785 static void do_preprocess(struct token **list)
1787 struct token *next;
1789 while (!eof_token(next = scan_next(list))) {
1790 struct stream *stream = input_streams + next->pos.stream;
1792 if (next->pos.newline && match_op(next, '#')) {
1793 if (!next->pos.noexpand) {
1794 preprocessor_line(stream, list);
1795 __free_token(next); /* Free the '#' token */
1796 continue;
1800 switch (token_type(next)) {
1801 case TOKEN_STREAMEND:
1802 if (stream->top_if) {
1803 nesting_error(stream);
1804 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1805 stream->top_if = NULL;
1806 false_nesting = 0;
1808 if (!stream->dirty)
1809 stream->constant = CONSTANT_FILE_YES;
1810 *list = next->next;
1811 continue;
1812 case TOKEN_STREAMBEGIN:
1813 *list = next->next;
1814 continue;
1816 default:
1817 dirty_stream(stream);
1818 if (false_nesting) {
1819 *list = next->next;
1820 __free_token(next);
1821 continue;
1824 if (token_type(next) != TOKEN_IDENT ||
1825 expand_one_symbol(list))
1826 list = &next->next;
1831 struct token * preprocess(struct token *token)
1833 preprocessing = 1;
1834 init_preprocessor();
1835 do_preprocess(&token);
1837 // Drop all expressions from preprocessing, they're not used any more.
1838 // This is not true when we have multiple files, though ;/
1839 // clear_expression_alloc();
1840 preprocessing = 0;
1842 return token;