type: export get_pointer_type()
[smatch.git] / pre-process.c
blobd55356b05015362eb75787ea037cbb81398cbb7d
1 /*
2 * Do C preprocessing, based on a token list gathered by
3 * the tokenizer.
5 * This may not be the smartest preprocessor on the planet.
7 * Copyright (C) 2003 Transmeta Corp.
8 * 2003-2004 Linus Torvalds
10 * Licensed under the Open Software License version 1.1
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <time.h>
23 #include "lib.h"
24 #include "allocate.h"
25 #include "parse.h"
26 #include "token.h"
27 #include "symbol.h"
28 #include "expression.h"
29 #include "scope.h"
31 static int false_nesting = 0;
33 #define INCLUDEPATHS 300
34 const char *includepath[INCLUDEPATHS+1] = {
35 "",
36 "/usr/include",
37 "/usr/local/include",
38 NULL
41 static const char **quote_includepath = includepath;
42 static const char **angle_includepath = includepath + 1;
43 static const char **isys_includepath = includepath + 1;
44 static const char **sys_includepath = includepath + 1;
45 static const char **dirafter_includepath = includepath + 3;
47 #define dirty_stream(stream) \
48 do { \
49 if (!stream->dirty) { \
50 stream->dirty = 1; \
51 if (!stream->ifndef) \
52 stream->protect = NULL; \
53 } \
54 } while(0)
56 #define end_group(stream) \
57 do { \
58 if (stream->ifndef == stream->top_if) { \
59 stream->ifndef = NULL; \
60 if (!stream->dirty) \
61 stream->protect = NULL; \
62 else if (stream->protect) \
63 stream->dirty = 0; \
64 } \
65 } while(0)
67 #define nesting_error(stream) \
68 do { \
69 stream->dirty = 1; \
70 stream->ifndef = NULL; \
71 stream->protect = NULL; \
72 } while(0)
74 static struct token *alloc_token(struct position *pos)
76 struct token *token = __alloc_token(0);
78 token->pos.stream = pos->stream;
79 token->pos.line = pos->line;
80 token->pos.pos = pos->pos;
81 token->pos.whitespace = 1;
82 return token;
85 static const char *show_token_sequence(struct token *token);
87 /* Expand symbol 'sym' at '*list' */
88 static int expand(struct token **, struct symbol *);
90 static void replace_with_string(struct token *token, const char *str)
92 int size = strlen(str) + 1;
93 struct string *s = __alloc_string(size);
95 s->length = size;
96 memcpy(s->data, str, size);
97 token_type(token) = TOKEN_STRING;
98 token->string = s;
101 static void replace_with_integer(struct token *token, unsigned int val)
103 char *buf = __alloc_bytes(11);
104 sprintf(buf, "%u", val);
105 token_type(token) = TOKEN_NUMBER;
106 token->number = buf;
109 static struct symbol *lookup_macro(struct ident *ident)
111 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
112 if (sym && sym->namespace != NS_MACRO)
113 sym = NULL;
114 return sym;
117 static int token_defined(struct token *token)
119 if (token_type(token) == TOKEN_IDENT) {
120 struct symbol *sym = lookup_macro(token->ident);
121 if (sym) {
122 sym->used_in = file_scope;
123 return 1;
125 return 0;
128 sparse_error(token->pos, "expected preprocessor identifier");
129 return 0;
132 static void replace_with_defined(struct token *token)
134 static const char *string[] = { "0", "1" };
135 int defined = token_defined(token);
137 token_type(token) = TOKEN_NUMBER;
138 token->number = string[defined];
141 static int expand_one_symbol(struct token **list)
143 struct token *token = *list;
144 struct symbol *sym;
145 static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
146 static time_t t = 0;
148 if (token->pos.noexpand)
149 return 1;
151 sym = lookup_macro(token->ident);
152 if (sym) {
153 store_macro_pos(token);
154 sym->used_in = file_scope;
155 return expand(list, sym);
157 if (token->ident == &__LINE___ident) {
158 replace_with_integer(token, token->pos.line);
159 } else if (token->ident == &__FILE___ident) {
160 replace_with_string(token, stream_name(token->pos.stream));
161 } else if (token->ident == &__DATE___ident) {
162 if (!t)
163 time(&t);
164 strftime(buffer, 12, "%b %e %Y", localtime(&t));
165 replace_with_string(token, buffer);
166 } else if (token->ident == &__TIME___ident) {
167 if (!t)
168 time(&t);
169 strftime(buffer, 9, "%T", localtime(&t));
170 replace_with_string(token, buffer);
172 return 1;
175 static inline struct token *scan_next(struct token **where)
177 struct token *token = *where;
178 if (token_type(token) != TOKEN_UNTAINT)
179 return token;
180 do {
181 token->ident->tainted = 0;
182 token = token->next;
183 } while (token_type(token) == TOKEN_UNTAINT);
184 *where = token;
185 return token;
188 static void expand_list(struct token **list)
190 struct token *next;
191 while (!eof_token(next = scan_next(list))) {
192 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
193 list = &next->next;
197 static void preprocessor_line(struct stream *stream, struct token **line);
199 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
201 struct stream *stream = input_streams + prev->pos.stream;
202 struct token **p = &prev->next;
203 struct token *next;
204 int nesting = 0;
206 while (!eof_token(next = scan_next(p))) {
207 if (next->pos.newline && match_op(next, '#')) {
208 if (!next->pos.noexpand) {
209 sparse_error(next->pos,
210 "directive in argument list");
211 preprocessor_line(stream, p);
212 __free_token(next); /* Free the '#' token */
213 continue;
216 switch (token_type(next)) {
217 case TOKEN_STREAMEND:
218 case TOKEN_STREAMBEGIN:
219 *p = &eof_token_entry;
220 return next;
222 if (false_nesting) {
223 *p = next->next;
224 __free_token(next);
225 continue;
227 if (match_op(next, '(')) {
228 nesting++;
229 } else if (match_op(next, ')')) {
230 if (!nesting--)
231 break;
232 } else if (match_op(next, ',') && !nesting && !vararg) {
233 break;
235 next->pos.stream = pos->stream;
236 next->pos.line = pos->line;
237 next->pos.pos = pos->pos;
238 p = &next->next;
240 *p = &eof_token_entry;
241 return next;
245 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
248 struct arg {
249 struct token *arg;
250 struct token *expanded;
251 struct token *str;
252 int n_normal;
253 int n_quoted;
254 int n_str;
257 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
259 int wanted = arglist->count.normal;
260 struct token *next = NULL;
261 int count = 0;
263 arglist = arglist->next; /* skip counter */
265 if (!wanted) {
266 next = collect_arg(start, 0, &what->pos);
267 if (eof_token(next))
268 goto Eclosing;
269 if (!eof_token(start->next) || !match_op(next, ')')) {
270 count++;
271 goto Emany;
273 } else {
274 for (count = 0; count < wanted; count++) {
275 struct argcount *p = &arglist->next->count;
276 next = collect_arg(start, p->vararg, &what->pos);
277 arglist = arglist->next->next;
278 if (eof_token(next))
279 goto Eclosing;
280 args[count].arg = start->next;
281 args[count].n_normal = p->normal;
282 args[count].n_quoted = p->quoted;
283 args[count].n_str = p->str;
284 if (match_op(next, ')')) {
285 count++;
286 break;
288 start = next;
290 if (count == wanted && !match_op(next, ')'))
291 goto Emany;
292 if (count == wanted - 1) {
293 struct argcount *p = &arglist->next->count;
294 if (!p->vararg)
295 goto Efew;
296 args[count].arg = NULL;
297 args[count].n_normal = p->normal;
298 args[count].n_quoted = p->quoted;
299 args[count].n_str = p->str;
301 if (count < wanted - 1)
302 goto Efew;
304 what->next = next->next;
305 return 1;
307 Efew:
308 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
309 show_token(what), wanted, count);
310 goto out;
311 Emany:
312 while (match_op(next, ',')) {
313 next = collect_arg(next, 0, &what->pos);
314 count++;
316 if (eof_token(next))
317 goto Eclosing;
318 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
319 show_token(what), count, wanted);
320 goto out;
321 Eclosing:
322 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
323 show_token(what));
324 out:
325 what->next = next->next;
326 return 0;
329 static struct token *dup_list(struct token *list)
331 struct token *res = NULL;
332 struct token **p = &res;
334 while (!eof_token(list)) {
335 struct token *newtok = __alloc_token(0);
336 *newtok = *list;
337 *p = newtok;
338 p = &newtok->next;
339 list = list->next;
341 return res;
344 static struct token *stringify(struct token *arg)
346 const char *s = show_token_sequence(arg);
347 int size = strlen(s)+1;
348 struct token *token = __alloc_token(0);
349 struct string *string = __alloc_string(size);
351 memcpy(string->data, s, size);
352 string->length = size;
353 token->pos = arg->pos;
354 token_type(token) = TOKEN_STRING;
355 token->string = string;
356 token->next = &eof_token_entry;
357 return token;
360 static void expand_arguments(int count, struct arg *args)
362 int i;
363 for (i = 0; i < count; i++) {
364 struct token *arg = args[i].arg;
365 if (!arg)
366 arg = &eof_token_entry;
367 if (args[i].n_str)
368 args[i].str = stringify(arg);
369 if (args[i].n_normal) {
370 if (!args[i].n_quoted) {
371 args[i].expanded = arg;
372 args[i].arg = NULL;
373 } else if (eof_token(arg)) {
374 args[i].expanded = arg;
375 } else {
376 args[i].expanded = dup_list(arg);
378 expand_list(&args[i].expanded);
384 * Possibly valid combinations:
385 * - ident + ident -> ident
386 * - ident + number -> ident unless number contains '.', '+' or '-'.
387 * - number + number -> number
388 * - number + ident -> number
389 * - number + '.' -> number
390 * - number + '+' or '-' -> number, if number used to end on [eEpP].
391 * - '.' + number -> number, if number used to start with a digit.
392 * - special + special -> either special or an error.
394 static enum token_type combine(struct token *left, struct token *right, char *p)
396 int len;
397 enum token_type t1 = token_type(left), t2 = token_type(right);
399 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
400 return TOKEN_ERROR;
402 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
403 return TOKEN_ERROR;
405 strcpy(p, show_token(left));
406 strcat(p, show_token(right));
407 len = strlen(p);
409 if (len >= 256)
410 return TOKEN_ERROR;
412 if (t1 == TOKEN_IDENT) {
413 if (t2 == TOKEN_SPECIAL)
414 return TOKEN_ERROR;
415 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
416 return TOKEN_ERROR;
417 return TOKEN_IDENT;
420 if (t1 == TOKEN_NUMBER) {
421 if (t2 == TOKEN_SPECIAL) {
422 switch (right->special) {
423 case '.':
424 break;
425 case '+': case '-':
426 if (strchr("eEpP", p[len - 2]))
427 break;
428 default:
429 return TOKEN_ERROR;
432 return TOKEN_NUMBER;
435 if (p[0] == '.' && isdigit((unsigned char)p[1]))
436 return TOKEN_NUMBER;
438 return TOKEN_SPECIAL;
441 static int merge(struct token *left, struct token *right)
443 static char buffer[512];
444 int n;
446 switch (combine(left, right, buffer)) {
447 case TOKEN_IDENT:
448 left->ident = built_in_ident(buffer);
449 left->pos.noexpand = 0;
450 return 1;
452 case TOKEN_NUMBER: {
453 char *number = __alloc_bytes(strlen(buffer) + 1);
454 memcpy(number, buffer, strlen(buffer) + 1);
455 token_type(left) = TOKEN_NUMBER; /* could be . + num */
456 left->number = number;
457 return 1;
460 case TOKEN_SPECIAL:
461 if (buffer[2] && buffer[3])
462 break;
463 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
464 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
465 left->special = n;
466 return 1;
469 default:
472 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
473 return 0;
476 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
478 struct token *alloc = alloc_token(streampos);
479 token_type(alloc) = token_type(token);
480 alloc->pos.newline = pos->newline;
481 alloc->pos.whitespace = pos->whitespace;
482 alloc->number = token->number;
483 alloc->pos.noexpand = token->pos.noexpand;
484 return alloc;
487 static struct token **copy(struct token **where, struct token *list, int *count)
489 int need_copy = --*count;
490 while (!eof_token(list)) {
491 struct token *token;
492 if (need_copy)
493 token = dup_token(list, &list->pos, &list->pos);
494 else
495 token = list;
496 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
497 token->pos.noexpand = 1;
498 *where = token;
499 where = &token->next;
500 list = list->next;
502 *where = &eof_token_entry;
503 return where;
506 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
508 struct token *token = *list;
509 struct position *base_pos = &token->pos;
510 struct position *pos = base_pos;
511 int *count;
512 enum {Normal, Placeholder, Concat} state = Normal;
514 for (; !eof_token(body); body = body->next, pos = &body->pos) {
515 struct token *added, *arg;
516 struct token **tail;
518 switch (token_type(body)) {
519 case TOKEN_GNU_KLUDGE:
521 * GNU kludge: if we had <comma>##<vararg>, behaviour
522 * depends on whether we had enough arguments to have
523 * a vararg. If we did, ## is just ignored. Otherwise
524 * both , and ## are ignored. Comma should come from
525 * the body of macro and not be an argument of earlier
526 * concatenation.
528 if (!args[body->next->argnum].arg)
529 continue;
530 added = dup_token(body, base_pos, pos);
531 token_type(added) = TOKEN_SPECIAL;
532 tail = &added->next;
533 break;
535 case TOKEN_STR_ARGUMENT:
536 arg = args[body->argnum].str;
537 count = &args[body->argnum].n_str;
538 goto copy_arg;
540 case TOKEN_QUOTED_ARGUMENT:
541 arg = args[body->argnum].arg;
542 count = &args[body->argnum].n_quoted;
543 if (!arg || eof_token(arg)) {
544 if (state == Concat)
545 state = Normal;
546 else
547 state = Placeholder;
548 continue;
550 goto copy_arg;
552 case TOKEN_MACRO_ARGUMENT:
553 arg = args[body->argnum].expanded;
554 count = &args[body->argnum].n_normal;
555 if (eof_token(arg)) {
556 state = Normal;
557 continue;
559 copy_arg:
560 tail = copy(&added, arg, count);
561 added->pos.newline = pos->newline;
562 added->pos.whitespace = pos->whitespace;
563 break;
565 case TOKEN_CONCAT:
566 if (state == Placeholder)
567 state = Normal;
568 else
569 state = Concat;
570 continue;
572 case TOKEN_IDENT:
573 added = dup_token(body, base_pos, pos);
574 if (added->ident->tainted)
575 added->pos.noexpand = 1;
576 tail = &added->next;
577 break;
579 default:
580 added = dup_token(body, base_pos, pos);
581 tail = &added->next;
582 break;
586 * if we got to doing real concatenation, we already have
587 * added something into the list, so containing_token() is OK.
589 if (state == Concat && merge(containing_token(list), added)) {
590 *list = added->next;
591 if (tail != &added->next)
592 list = tail;
593 } else {
594 *list = added;
595 list = tail;
597 state = Normal;
599 *list = &eof_token_entry;
600 return list;
603 static int expand(struct token **list, struct symbol *sym)
605 struct token *last;
606 struct token *token = *list;
607 struct ident *expanding = token->ident;
608 struct token **tail;
609 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
610 struct arg args[nargs];
612 if (expanding->tainted) {
613 token->pos.noexpand = 1;
614 return 1;
617 if (sym->arglist) {
618 if (!match_op(scan_next(&token->next), '('))
619 return 1;
620 if (!collect_arguments(token->next, sym->arglist, args, token))
621 return 1;
622 expand_arguments(nargs, args);
625 expanding->tainted = 1;
627 last = token->next;
628 tail = substitute(list, sym->expansion, args);
629 *tail = last;
631 return 0;
634 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
636 struct token *last;
637 static char buffer[256];
638 char *ptr = buffer;
640 last = token;
641 while (!eof_token(token) && !match_op(token, endop)) {
642 int len;
643 const char *val = token->string->data;
644 if (token_type(token) != TOKEN_STRING)
645 val = show_token(token);
646 len = strlen(val);
647 memcpy(ptr, val, len);
648 ptr += len;
649 token = token->next;
651 *ptr = 0;
652 if (endop && !match_op(token, endop))
653 sparse_error(start->pos, "expected '>' at end of filename");
654 return buffer;
657 static int already_tokenized(const char *path)
659 int i;
660 struct stream *s = input_streams;
662 for (i = input_stream_nr; --i >= 0; s++) {
663 if (s->constant != CONSTANT_FILE_YES)
664 continue;
665 if (strcmp(path, s->name))
666 continue;
667 if (s->protect && !lookup_macro(s->protect))
668 continue;
669 return 1;
671 return 0;
674 /* Handle include of header files.
675 * The relevant options are made compatible with gcc. The only options that
676 * are not supported is -withprefix and friends.
678 * Three set of include paths are known:
679 * quote_includepath: Path to search when using #include "file.h"
680 * angle_includepath: Paths to search when using #include <file.h>
681 * isys_includepath: Paths specified with -isystem, come before the
682 * built-in system include paths. Gcc would suppress
683 * warnings from system headers. Here we separate
684 * them from the angle_ ones to keep search ordering.
686 * sys_includepath: Built-in include paths.
687 * dirafter_includepath Paths added with -dirafter.
689 * The above is implemented as one array with pointers
690 * +--------------+
691 * quote_includepath ---> | |
692 * +--------------+
693 * | |
694 * +--------------+
695 * angle_includepath ---> | |
696 * +--------------+
697 * isys_includepath ---> | |
698 * +--------------+
699 * sys_includepath ---> | |
700 * +--------------+
701 * dirafter_includepath -> | |
702 * +--------------+
704 * -I dir insert dir just before isys_includepath and move the rest
705 * -I- makes all dirs specified with -I before to quote dirs only and
706 * angle_includepath is set equal to isys_includepath.
707 * -nostdinc removes all sys dirs by storing NULL in entry pointed
708 * to by * sys_includepath. Note that this will reset all dirs built-in
709 * and added before -nostdinc by -isystem and -idirafter.
710 * -isystem dir adds dir where isys_includepath points adding this dir as
711 * first systemdir
712 * -idirafter dir adds dir to the end of the list
715 static void set_stream_include_path(struct stream *stream)
717 const char *path = stream->path;
718 if (!path) {
719 const char *p = strrchr(stream->name, '/');
720 path = "";
721 if (p) {
722 int len = p - stream->name + 1;
723 char *m = malloc(len+1);
724 /* This includes the final "/" */
725 memcpy(m, stream->name, len);
726 m[len] = 0;
727 path = m;
729 stream->path = path;
731 includepath[0] = path;
734 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
736 int fd;
737 int plen = strlen(path);
738 static char fullname[PATH_MAX];
740 memcpy(fullname, path, plen);
741 if (plen && path[plen-1] != '/') {
742 fullname[plen] = '/';
743 plen++;
745 memcpy(fullname+plen, filename, flen);
746 if (already_tokenized(fullname))
747 return 1;
748 fd = open(fullname, O_RDONLY);
749 if (fd >= 0) {
750 char * streamname = __alloc_bytes(plen + flen);
751 memcpy(streamname, fullname, plen + flen);
752 *where = tokenize(streamname, fd, *where, next_path);
753 close(fd);
754 return 1;
756 return 0;
759 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
761 const char *path;
763 while ((path = *pptr++) != NULL) {
764 if (!try_include(path, filename, flen, list, pptr))
765 continue;
766 return 1;
768 return 0;
771 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
773 int flen = strlen(filename) + 1;
775 /* Absolute path? */
776 if (filename[0] == '/') {
777 if (try_include("", filename, flen, list, includepath))
778 return;
779 goto out;
782 /* Dir of input file is first dir to search for quoted includes */
783 set_stream_include_path(stream);
785 if (!path)
786 /* Do not search quote include if <> is in use */
787 path = local ? quote_includepath : angle_includepath;
789 /* Check the standard include paths.. */
790 if (do_include_path(path, list, token, filename, flen))
791 return;
792 out:
793 error_die(token->pos, "unable to open '%s'", filename);
796 static int free_preprocessor_line(struct token *token)
798 while (token_type(token) != TOKEN_EOF) {
799 struct token *free = token;
800 token = token->next;
801 __free_token(free);
803 return 1;
806 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
808 const char *filename;
809 struct token *next;
810 int expect;
812 next = token->next;
813 expect = '>';
814 if (!match_op(next, '<')) {
815 expand_list(&token->next);
816 expect = 0;
817 next = token;
818 if (match_op(token->next, '<')) {
819 next = token->next;
820 expect = '>';
823 token = next->next;
824 filename = token_name_sequence(token, expect, token);
825 do_include(!expect, stream, list, token, filename, path);
826 return 0;
829 static int handle_include(struct stream *stream, struct token **list, struct token *token)
831 return handle_include_path(stream, list, token, NULL);
834 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
836 return handle_include_path(stream, list, token, stream->next_path);
839 static int token_different(struct token *t1, struct token *t2)
841 int different;
843 if (token_type(t1) != token_type(t2))
844 return 1;
846 switch (token_type(t1)) {
847 case TOKEN_IDENT:
848 different = t1->ident != t2->ident;
849 break;
850 case TOKEN_ARG_COUNT:
851 case TOKEN_UNTAINT:
852 case TOKEN_CONCAT:
853 case TOKEN_GNU_KLUDGE:
854 different = 0;
855 break;
856 case TOKEN_NUMBER:
857 different = strcmp(t1->number, t2->number);
858 break;
859 case TOKEN_SPECIAL:
860 different = t1->special != t2->special;
861 break;
862 case TOKEN_MACRO_ARGUMENT:
863 case TOKEN_QUOTED_ARGUMENT:
864 case TOKEN_STR_ARGUMENT:
865 different = t1->argnum != t2->argnum;
866 break;
867 case TOKEN_CHAR:
868 case TOKEN_WIDE_CHAR:
869 different = t1->character != t2->character;
870 break;
871 case TOKEN_STRING:
872 case TOKEN_WIDE_STRING: {
873 struct string *s1, *s2;
875 s1 = t1->string;
876 s2 = t2->string;
877 different = 1;
878 if (s1->length != s2->length)
879 break;
880 different = memcmp(s1->data, s2->data, s1->length);
881 break;
883 default:
884 different = 1;
885 break;
887 return different;
890 static int token_list_different(struct token *list1, struct token *list2)
892 for (;;) {
893 if (list1 == list2)
894 return 0;
895 if (!list1 || !list2)
896 return 1;
897 if (token_different(list1, list2))
898 return 1;
899 list1 = list1->next;
900 list2 = list2->next;
904 static inline void set_arg_count(struct token *token)
906 token_type(token) = TOKEN_ARG_COUNT;
907 token->count.normal = token->count.quoted =
908 token->count.str = token->count.vararg = 0;
911 static struct token *parse_arguments(struct token *list)
913 struct token *arg = list->next, *next = list;
914 struct argcount *count = &list->count;
916 set_arg_count(list);
918 if (match_op(arg, ')')) {
919 next = arg->next;
920 list->next = &eof_token_entry;
921 return next;
924 while (token_type(arg) == TOKEN_IDENT) {
925 if (arg->ident == &__VA_ARGS___ident)
926 goto Eva_args;
927 if (!++count->normal)
928 goto Eargs;
929 next = arg->next;
931 if (match_op(next, ',')) {
932 set_arg_count(next);
933 arg = next->next;
934 continue;
937 if (match_op(next, ')')) {
938 set_arg_count(next);
939 next = next->next;
940 arg->next->next = &eof_token_entry;
941 return next;
944 /* normal cases are finished here */
946 if (match_op(next, SPECIAL_ELLIPSIS)) {
947 if (match_op(next->next, ')')) {
948 set_arg_count(next);
949 next->count.vararg = 1;
950 next = next->next;
951 arg->next->next = &eof_token_entry;
952 return next->next;
955 arg = next;
956 goto Enotclosed;
959 if (eof_token(next)) {
960 goto Enotclosed;
961 } else {
962 arg = next;
963 goto Ebadstuff;
967 if (match_op(arg, SPECIAL_ELLIPSIS)) {
968 next = arg->next;
969 token_type(arg) = TOKEN_IDENT;
970 arg->ident = &__VA_ARGS___ident;
971 if (!match_op(next, ')'))
972 goto Enotclosed;
973 if (!++count->normal)
974 goto Eargs;
975 set_arg_count(next);
976 next->count.vararg = 1;
977 next = next->next;
978 arg->next->next = &eof_token_entry;
979 return next;
982 if (eof_token(arg)) {
983 arg = next;
984 goto Enotclosed;
986 if (match_op(arg, ','))
987 goto Emissing;
988 else
989 goto Ebadstuff;
992 Emissing:
993 sparse_error(arg->pos, "parameter name missing");
994 return NULL;
995 Ebadstuff:
996 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
997 show_token(arg));
998 return NULL;
999 Enotclosed:
1000 sparse_error(arg->pos, "missing ')' in macro parameter list");
1001 return NULL;
1002 Eva_args:
1003 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1004 return NULL;
1005 Eargs:
1006 sparse_error(arg->pos, "too many arguments in macro definition");
1007 return NULL;
1010 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1012 struct ident *ident = token->ident;
1013 int nr;
1015 if (!arglist || token_type(token) != TOKEN_IDENT)
1016 return 0;
1018 arglist = arglist->next;
1020 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1021 if (arglist->ident == ident) {
1022 struct argcount *count = &arglist->next->count;
1023 int n;
1025 token->argnum = nr;
1026 token_type(token) = type;
1027 switch (type) {
1028 case TOKEN_MACRO_ARGUMENT:
1029 n = ++count->normal;
1030 break;
1031 case TOKEN_QUOTED_ARGUMENT:
1032 n = ++count->quoted;
1033 break;
1034 default:
1035 n = ++count->str;
1037 if (n)
1038 return count->vararg ? 2 : 1;
1039 token_type(token) = TOKEN_ERROR;
1040 return -1;
1043 return 0;
1046 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1048 struct token *token = expansion;
1049 struct token **p;
1050 struct token *last = NULL;
1052 if (match_op(token, SPECIAL_HASHHASH))
1053 goto Econcat;
1055 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1056 if (match_op(token, '#')) {
1057 if (arglist) {
1058 struct token *next = token->next;
1059 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1060 goto Equote;
1061 next->pos.whitespace = token->pos.whitespace;
1062 token = *p = next;
1063 } else {
1064 token->pos.noexpand = 1;
1066 } else if (match_op(token, SPECIAL_HASHHASH)) {
1067 struct token *next = token->next;
1068 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
1069 token_type(token) = TOKEN_CONCAT;
1070 if (arg) {
1071 token = next;
1072 /* GNU kludge */
1073 if (arg == 2 && last && match_op(last, ',')) {
1074 token_type(last) = TOKEN_GNU_KLUDGE;
1075 last->next = token;
1077 } else if (match_op(next, SPECIAL_HASHHASH))
1078 token = next;
1079 else if (eof_token(next))
1080 goto Econcat;
1081 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
1082 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1083 } else {
1084 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1086 if (token_type(token) == TOKEN_ERROR)
1087 goto Earg;
1088 last = token;
1090 token = alloc_token(&expansion->pos);
1091 token_type(token) = TOKEN_UNTAINT;
1092 token->ident = name;
1093 token->next = *p;
1094 *p = token;
1095 return expansion;
1097 Equote:
1098 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1099 return NULL;
1101 Econcat:
1102 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1103 return NULL;
1104 Earg:
1105 sparse_error(token->pos, "too many instances of argument in body");
1106 return NULL;
1109 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1111 struct token *arglist, *expansion;
1112 struct token *left = token->next;
1113 struct symbol *sym;
1114 struct ident *name;
1115 int ret;
1117 if (token_type(left) != TOKEN_IDENT) {
1118 sparse_error(token->pos, "expected identifier to 'define'");
1119 return 1;
1122 name = left->ident;
1124 arglist = NULL;
1125 expansion = left->next;
1126 if (!expansion->pos.whitespace) {
1127 if (match_op(expansion, '(')) {
1128 arglist = expansion;
1129 expansion = parse_arguments(expansion);
1130 if (!expansion)
1131 return 1;
1132 } else if (!eof_token(expansion)) {
1133 warning(expansion->pos,
1134 "no whitespace before object-like macro body");
1138 expansion = parse_expansion(expansion, arglist, name);
1139 if (!expansion)
1140 return 1;
1142 ret = 1;
1143 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1144 if (sym) {
1145 int clean;
1147 if (attr < sym->attr)
1148 goto out;
1150 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1152 if (token_list_different(sym->expansion, expansion) ||
1153 token_list_different(sym->arglist, arglist)) {
1154 ret = 0;
1155 if ((clean && attr == SYM_ATTR_NORMAL)
1156 || sym->used_in == file_scope) {
1157 warning(left->pos, "preprocessor token %.*s redefined",
1158 name->len, name->name);
1159 info(sym->pos, "this was the original definition");
1161 } else if (clean)
1162 goto out;
1165 if (!sym || sym->scope != file_scope) {
1166 sym = alloc_symbol(left->pos, SYM_NODE);
1167 bind_symbol(sym, name, NS_MACRO);
1168 ret = 0;
1171 if (!ret) {
1172 sym->expansion = expansion;
1173 sym->arglist = arglist;
1174 __free_token(token); /* Free the "define" token, but not the rest of the line */
1177 sym->namespace = NS_MACRO;
1178 sym->used_in = NULL;
1179 sym->attr = attr;
1180 out:
1181 return ret;
1184 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1186 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1189 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1191 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1194 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1196 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1199 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1201 struct token *left = token->next;
1202 struct symbol *sym;
1204 if (token_type(left) != TOKEN_IDENT) {
1205 sparse_error(token->pos, "expected identifier to 'undef'");
1206 return 1;
1209 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1210 if (sym) {
1211 if (attr < sym->attr)
1212 return 1;
1213 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1214 return 1;
1215 } else if (attr <= SYM_ATTR_NORMAL)
1216 return 1;
1218 if (!sym || sym->scope != file_scope) {
1219 sym = alloc_symbol(left->pos, SYM_NODE);
1220 bind_symbol(sym, left->ident, NS_MACRO);
1223 sym->namespace = NS_UNDEF;
1224 sym->used_in = NULL;
1225 sym->attr = attr;
1227 return 1;
1230 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1232 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1235 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1237 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1240 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1242 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1243 free_preprocessor_line(token->next);
1244 token->next = stream->top_if;
1245 stream->top_if = token;
1246 if (false_nesting || true != 1)
1247 false_nesting++;
1248 return 0;
1251 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1253 struct token *next = token->next;
1254 int arg;
1255 if (token_type(next) == TOKEN_IDENT) {
1256 arg = token_defined(next);
1257 } else {
1258 dirty_stream(stream);
1259 if (!false_nesting)
1260 sparse_error(token->pos, "expected preprocessor identifier");
1261 arg = -1;
1263 return preprocessor_if(stream, token, arg);
1266 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1268 struct token *next = token->next;
1269 int arg;
1270 if (token_type(next) == TOKEN_IDENT) {
1271 if (!stream->dirty && !stream->ifndef) {
1272 if (!stream->protect) {
1273 stream->ifndef = token;
1274 stream->protect = next->ident;
1275 } else if (stream->protect == next->ident) {
1276 stream->ifndef = token;
1277 stream->dirty = 1;
1280 arg = !token_defined(next);
1281 } else {
1282 dirty_stream(stream);
1283 if (!false_nesting)
1284 sparse_error(token->pos, "expected preprocessor identifier");
1285 arg = -1;
1288 return preprocessor_if(stream, token, arg);
1292 * Expression handling for #if and #elif; it differs from normal expansion
1293 * due to special treatment of "defined".
1295 static int expression_value(struct token **where)
1297 struct expression *expr;
1298 struct token *p;
1299 struct token **list = where, **beginning = NULL;
1300 long long value;
1301 int state = 0;
1303 while (!eof_token(p = scan_next(list))) {
1304 switch (state) {
1305 case 0:
1306 if (token_type(p) != TOKEN_IDENT)
1307 break;
1308 if (p->ident == &defined_ident) {
1309 state = 1;
1310 beginning = list;
1311 break;
1313 if (!expand_one_symbol(list))
1314 continue;
1315 if (token_type(p) != TOKEN_IDENT)
1316 break;
1317 token_type(p) = TOKEN_ZERO_IDENT;
1318 break;
1319 case 1:
1320 if (match_op(p, '(')) {
1321 state = 2;
1322 } else {
1323 state = 0;
1324 replace_with_defined(p);
1325 *beginning = p;
1327 break;
1328 case 2:
1329 if (token_type(p) == TOKEN_IDENT)
1330 state = 3;
1331 else
1332 state = 0;
1333 replace_with_defined(p);
1334 *beginning = p;
1335 break;
1336 case 3:
1337 state = 0;
1338 if (!match_op(p, ')'))
1339 sparse_error(p->pos, "missing ')' after \"defined\"");
1340 *list = p->next;
1341 continue;
1343 list = &p->next;
1346 p = constant_expression(*where, &expr);
1347 if (!eof_token(p))
1348 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p));
1349 value = get_expression_value(expr);
1350 return value != 0;
1353 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1355 int value = 0;
1356 if (!false_nesting)
1357 value = expression_value(&token->next);
1359 dirty_stream(stream);
1360 return preprocessor_if(stream, token, value);
1363 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1365 struct token *top_if = stream->top_if;
1366 end_group(stream);
1368 if (!top_if) {
1369 nesting_error(stream);
1370 sparse_error(token->pos, "unmatched #elif within stream");
1371 return 1;
1374 if (token_type(top_if) == TOKEN_ELSE) {
1375 nesting_error(stream);
1376 sparse_error(token->pos, "#elif after #else");
1377 if (!false_nesting)
1378 false_nesting = 1;
1379 return 1;
1382 dirty_stream(stream);
1383 if (token_type(top_if) != TOKEN_IF)
1384 return 1;
1385 if (false_nesting) {
1386 false_nesting = 0;
1387 if (!expression_value(&token->next))
1388 false_nesting = 1;
1389 } else {
1390 false_nesting = 1;
1391 token_type(top_if) = TOKEN_SKIP_GROUPS;
1393 return 1;
1396 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1398 struct token *top_if = stream->top_if;
1399 end_group(stream);
1401 if (!top_if) {
1402 nesting_error(stream);
1403 sparse_error(token->pos, "unmatched #else within stream");
1404 return 1;
1407 if (token_type(top_if) == TOKEN_ELSE) {
1408 nesting_error(stream);
1409 sparse_error(token->pos, "#else after #else");
1411 if (false_nesting) {
1412 if (token_type(top_if) == TOKEN_IF)
1413 false_nesting = 0;
1414 } else {
1415 false_nesting = 1;
1417 token_type(top_if) = TOKEN_ELSE;
1418 return 1;
1421 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1423 struct token *top_if = stream->top_if;
1424 end_group(stream);
1425 if (!top_if) {
1426 nesting_error(stream);
1427 sparse_error(token->pos, "unmatched #endif in stream");
1428 return 1;
1430 if (false_nesting)
1431 false_nesting--;
1432 stream->top_if = top_if->next;
1433 __free_token(top_if);
1434 return 1;
1437 static const char *show_token_sequence(struct token *token)
1439 static char buffer[1024];
1440 char *ptr = buffer;
1441 int whitespace = 0;
1443 if (!token)
1444 return "<none>";
1445 while (!eof_token(token)) {
1446 const char *val = show_token(token);
1447 int len = strlen(val);
1449 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1450 sparse_error(token->pos, "too long token expansion");
1451 break;
1454 if (whitespace)
1455 *ptr++ = ' ';
1456 memcpy(ptr, val, len);
1457 ptr += len;
1458 token = token->next;
1459 whitespace = token->pos.whitespace;
1461 *ptr = 0;
1462 return buffer;
1465 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1467 warning(token->pos, "%s", show_token_sequence(token->next));
1468 return 1;
1471 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1473 sparse_error(token->pos, "%s", show_token_sequence(token->next));
1474 return 1;
1477 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1480 * Do we have any non-system includes?
1481 * Clear them out if so..
1483 *sys_includepath = NULL;
1484 return 1;
1487 static inline void update_inc_ptrs(const char ***where)
1490 if (*where <= dirafter_includepath) {
1491 dirafter_includepath++;
1492 /* If this was the entry that we prepend, don't
1493 * rise the lower entries, even if they are at
1494 * the same level. */
1495 if (where == &dirafter_includepath)
1496 return;
1498 if (*where <= sys_includepath) {
1499 sys_includepath++;
1500 if (where == &sys_includepath)
1501 return;
1503 if (*where <= isys_includepath) {
1504 isys_includepath++;
1505 if (where == &isys_includepath)
1506 return;
1509 /* angle_includepath is actually never updated, since we
1510 * don't suppport -iquote rught now. May change some day. */
1511 if (*where <= angle_includepath) {
1512 angle_includepath++;
1513 if (where == &angle_includepath)
1514 return;
1518 /* Add a path before 'where' and update the pointers associated with the
1519 * includepath array */
1520 static void add_path_entry(struct token *token, const char *path,
1521 const char ***where)
1523 const char **dst;
1524 const char *next;
1526 /* Need one free entry.. */
1527 if (includepath[INCLUDEPATHS-2])
1528 error_die(token->pos, "too many include path entries");
1530 /* check that this is not a duplicate */
1531 dst = includepath;
1532 while (*dst) {
1533 if (strcmp(*dst, path) == 0)
1534 return;
1535 dst++;
1537 next = path;
1538 dst = *where;
1540 update_inc_ptrs(where);
1543 * Move them all up starting at dst,
1544 * insert the new entry..
1546 do {
1547 const char *tmp = *dst;
1548 *dst = next;
1549 next = tmp;
1550 dst++;
1551 } while (next);
1554 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1556 for (;;) {
1557 token = token->next;
1558 if (eof_token(token))
1559 return 1;
1560 if (token_type(token) != TOKEN_STRING) {
1561 warning(token->pos, "expected path string");
1562 return 1;
1564 add_path_entry(token, token->string->data, &isys_includepath);
1568 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1570 for (;;) {
1571 token = token->next;
1572 if (eof_token(token))
1573 return 1;
1574 if (token_type(token) != TOKEN_STRING) {
1575 sparse_error(token->pos, "expected path string");
1576 return 1;
1578 add_path_entry(token, token->string->data, &sys_includepath);
1582 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1584 for (;;) {
1585 token = token->next;
1586 if (eof_token(token))
1587 return 1;
1588 if (token_type(token) != TOKEN_STRING) {
1589 sparse_error(token->pos, "expected path string");
1590 return 1;
1592 add_path_entry(token, token->string->data, &dirafter_includepath);
1596 /* Add to end on includepath list - no pointer updates */
1597 static void add_dirafter_entry(struct token *token, const char *path)
1599 const char **dst = includepath;
1601 /* Need one free entry.. */
1602 if (includepath[INCLUDEPATHS-2])
1603 error_die(token->pos, "too many include path entries");
1605 /* Add to the end */
1606 while (*dst)
1607 dst++;
1608 *dst = path;
1609 dst++;
1610 *dst = NULL;
1613 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1615 for (;;) {
1616 token = token->next;
1617 if (eof_token(token))
1618 return 1;
1619 if (token_type(token) != TOKEN_STRING) {
1620 sparse_error(token->pos, "expected path string");
1621 return 1;
1623 add_dirafter_entry(token, token->string->data);
1627 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1630 * -I-
1631 * From info gcc:
1632 * Split the include path. Any directories specified with `-I'
1633 * options before `-I-' are searched only for headers requested with
1634 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1635 * If additional directories are specified with `-I' options after
1636 * the `-I-', those directories are searched for all `#include'
1637 * directives.
1638 * In addition, `-I-' inhibits the use of the directory of the current
1639 * file directory as the first search directory for `#include "FILE"'.
1641 quote_includepath = includepath+1;
1642 angle_includepath = sys_includepath;
1643 return 1;
1647 * We replace "#pragma xxx" with "__pragma__" in the token
1648 * stream. Just as an example.
1650 * We'll just #define that away for now, but the theory here
1651 * is that we can use this to insert arbitrary token sequences
1652 * to turn the pragmas into internal front-end sequences for
1653 * when we actually start caring about them.
1655 * So eventually this will turn into some kind of extended
1656 * __attribute__() like thing, except called __pragma__(xxx).
1658 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1660 struct token *next = *line;
1662 token->ident = &pragma_ident;
1663 token->pos.newline = 1;
1664 token->pos.whitespace = 1;
1665 token->pos.pos = 1;
1666 *line = token;
1667 token->next = next;
1668 return 0;
1672 * We ignore #line for now.
1674 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1676 return 1;
1679 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1681 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1682 return 1;
1686 static void init_preprocessor(void)
1688 int i;
1689 int stream = init_stream("preprocessor", -1, includepath);
1690 static struct {
1691 const char *name;
1692 int (*handler)(struct stream *, struct token **, struct token *);
1693 } normal[] = {
1694 { "define", handle_define },
1695 { "weak_define", handle_weak_define },
1696 { "strong_define", handle_strong_define },
1697 { "undef", handle_undef },
1698 { "strong_undef", handle_strong_undef },
1699 { "warning", handle_warning },
1700 { "error", handle_error },
1701 { "include", handle_include },
1702 { "include_next", handle_include_next },
1703 { "pragma", handle_pragma },
1704 { "line", handle_line },
1706 // our internal preprocessor tokens
1707 { "nostdinc", handle_nostdinc },
1708 { "add_include", handle_add_include },
1709 { "add_isystem", handle_add_isystem },
1710 { "add_system", handle_add_system },
1711 { "add_dirafter", handle_add_dirafter },
1712 { "split_include", handle_split_include },
1713 }, special[] = {
1714 { "ifdef", handle_ifdef },
1715 { "ifndef", handle_ifndef },
1716 { "else", handle_else },
1717 { "endif", handle_endif },
1718 { "if", handle_if },
1719 { "elif", handle_elif },
1722 for (i = 0; i < (sizeof (normal) / sizeof (normal[0])); i++) {
1723 struct symbol *sym;
1724 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1725 sym->handler = normal[i].handler;
1726 sym->normal = 1;
1728 for (i = 0; i < (sizeof (special) / sizeof (special[0])); i++) {
1729 struct symbol *sym;
1730 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1731 sym->handler = special[i].handler;
1732 sym->normal = 0;
1737 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1739 int (*handler)(struct stream *, struct token **, struct token *);
1740 struct token *token = start->next;
1741 int is_normal = 1;
1743 if (eof_token(token))
1744 return;
1746 if (token_type(token) == TOKEN_IDENT) {
1747 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1748 if (sym) {
1749 handler = sym->handler;
1750 is_normal = sym->normal;
1751 } else {
1752 handler = handle_nondirective;
1754 } else if (token_type(token) == TOKEN_NUMBER) {
1755 handler = handle_line;
1756 } else {
1757 handler = handle_nondirective;
1760 if (is_normal) {
1761 dirty_stream(stream);
1762 if (false_nesting)
1763 goto out;
1765 if (!handler(stream, line, token)) /* all set */
1766 return;
1768 out:
1769 free_preprocessor_line(token);
1772 static void preprocessor_line(struct stream *stream, struct token **line)
1774 struct token *start = *line, *next;
1775 struct token **tp = &start->next;
1777 for (;;) {
1778 next = *tp;
1779 if (next->pos.newline)
1780 break;
1781 tp = &next->next;
1783 *line = next;
1784 *tp = &eof_token_entry;
1785 handle_preprocessor_line(stream, line, start);
1788 static void do_preprocess(struct token **list)
1790 struct token *next;
1792 while (!eof_token(next = scan_next(list))) {
1793 struct stream *stream = input_streams + next->pos.stream;
1795 if (next->pos.newline && match_op(next, '#')) {
1796 if (!next->pos.noexpand) {
1797 preprocessor_line(stream, list);
1798 __free_token(next); /* Free the '#' token */
1799 continue;
1803 switch (token_type(next)) {
1804 case TOKEN_STREAMEND:
1805 if (stream->top_if) {
1806 nesting_error(stream);
1807 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1808 stream->top_if = NULL;
1809 false_nesting = 0;
1811 if (!stream->dirty)
1812 stream->constant = CONSTANT_FILE_YES;
1813 *list = next->next;
1814 continue;
1815 case TOKEN_STREAMBEGIN:
1816 *list = next->next;
1817 continue;
1819 default:
1820 dirty_stream(stream);
1821 if (false_nesting) {
1822 *list = next->next;
1823 __free_token(next);
1824 continue;
1827 if (token_type(next) != TOKEN_IDENT ||
1828 expand_one_symbol(list))
1829 list = &next->next;
1834 struct token * preprocess(struct token *token)
1836 preprocessing = 1;
1837 init_preprocessor();
1838 do_preprocess(&token);
1840 // Drop all expressions from preprocessing, they're not used any more.
1841 // This is not true when we have multiple files, though ;/
1842 // clear_expression_alloc();
1843 preprocessing = 0;
1845 return token;