o split handle_switch, to make it more lib friendly
[smatch.git] / pre-process.c
blob4e18cd837e0611d30ed7be3adf7001f45b56039a
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 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>
22 #include "pre-process.h"
23 #include "lib.h"
24 #include "parse.h"
25 #include "token.h"
26 #include "symbol.h"
27 #include "expression.h"
29 int verbose = 0;
30 int preprocessing = 0;
32 #define MAX_NEST (256)
33 static int true_nesting = 0;
34 static int false_nesting = 0;
35 static struct token *unmatched_if = NULL;
36 static char elif_ignore[MAX_NEST];
37 #define if_nesting (true_nesting + false_nesting)
39 #define INCLUDEPATHS 32
40 const char *includepath[INCLUDEPATHS+1] = {
41 NULL
44 const char *sys_includepath[] = {
45 "/usr/include",
46 "/usr/local/include",
47 NULL,
50 const char *gcc_includepath[] = {
51 GCC_INTERNAL_INCLUDE,
52 NULL
57 * This is stupid - the tokenizer already guarantees unique
58 * identifiers, so we should just compare identifier pointers
60 int match_string_ident(struct ident *ident, const char *str)
62 return !str[ident->len] && !memcmp(str, ident->name, ident->len);
65 static struct token *alloc_token(struct position *pos)
67 struct token *token = __alloc_token(0);
69 token->pos.stream = pos->stream;
70 token->pos.line = pos->line;
71 token->pos.pos = pos->pos;
72 token->pos.whitespace = 1;
73 return token;
76 static const char *show_token_sequence(struct token *token);
78 /* Head is one-before-list, and last is one-past-list */
79 static struct token *for_each_ident(struct token *parent, struct token *head, struct token *(*action)(struct token *parent, struct token *head, struct token *))
81 for (;;) {
82 struct token *next = head->next;
84 /* Did we hit the end of the current expansion? */
85 if (eof_token(next))
86 break;
88 if (token_type(next) == TOKEN_IDENT)
89 next = action(parent, head, next);
91 head = next;
93 return head;
96 static struct token *is_defined(struct token *head, struct token *token, struct token *next)
98 char *string[] = { "0", "1" };
99 char *defined = string[lookup_symbol(token->ident, NS_PREPROCESSOR) != NULL];
100 struct token *newtoken = alloc_token(&token->pos);
102 token_type(newtoken) = TOKEN_INTEGER;
103 newtoken->integer = defined;
104 newtoken->next = next;
105 head->next = newtoken;
106 return next;
110 struct token *defined_one_symbol(struct token *head, struct token *next)
112 struct token *token = next->next;
113 struct token *past = token->next;
115 if (match_op(token, '(')) {
116 token = past;
117 past = token->next;
118 if (!match_op(past, ')'))
119 return next;
120 past = past->next;
122 if (token_type(token) == TOKEN_IDENT)
123 return is_defined(head, token, past);
124 return next;
127 struct token variable_argument = { .next = &eof_token_entry };
129 /* Expand symbol 'sym' between 'head->next' and 'head->next->next' */
130 static struct token *expand(struct token *, struct token *, struct symbol *);
132 static void replace_with_string(struct token *token, const char *str)
134 int size = strlen(str) + 1;
135 struct string *s = __alloc_string(size);
137 s->length = size;
138 memcpy(s->data, str, size);
139 token_type(token) = TOKEN_STRING;
140 token->string = s;
143 static void replace_with_integer(struct token *token, unsigned int val)
145 char *buf = __alloc_bytes(10);
146 sprintf(buf, "%d", val);
147 token_type(token) = TOKEN_INTEGER;
148 token->integer = buf;
151 struct token *expand_one_symbol(struct token *parent, struct token *head, struct token *token)
153 struct symbol *sym;
154 struct token *x;
156 /* Avoid recursive expansion */
157 x = token;
158 while ((x = x->parent) != NULL) {
159 if (parent && x->ident == parent->ident)
160 return token;
161 if (x->ident == token->ident)
162 return token;
165 sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
166 if (sym) {
167 if (sym->arglist && !match_op(token->next, '('))
168 return token;
169 return expand(token, head, sym);
171 if (token->ident == &__LINE___ident) {
172 replace_with_integer(token, token->pos.line);
173 } else if (token->ident == &__FILE___ident) {
174 replace_with_string(token, (input_streams + token->pos.stream)->name);
175 } else if (token->ident == &defined_ident) {
176 return defined_one_symbol(head, token);
178 return token;
181 static struct token *expand_list(struct token *parent, struct token *head)
183 return for_each_ident(parent, head, expand_one_symbol);
186 static struct token *find_argument_end(struct token *start, struct token *arglist)
188 int nesting = 0;
190 while (!eof_token(start)) {
191 struct token *next = start->next;
192 if (match_op(next, '('))
193 nesting++;
194 else if (match_op(next, ')')) {
195 if (--nesting < 0) {
196 start->next = &eof_token_entry;
197 return next->next;
199 } else if (!nesting && match_op(next, ',') && arglist->next != &variable_argument
200 && !match_op(arglist, SPECIAL_ELLIPSIS)) {
201 next->special = SPECIAL_ARG_SEPARATOR;
202 arglist = arglist->next;
204 start = next;
206 return start;
209 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
211 struct token *alloc = alloc_token(streampos);
212 token_type(alloc) = token_type(token);
213 alloc->pos.newline = pos->newline;
214 alloc->pos.whitespace = pos->whitespace;
215 alloc->integer = token->integer;
216 return alloc;
219 static void insert(struct token *token, struct token *prev)
221 token->next = prev->next;
222 prev->next = token;
225 static struct token * replace(struct token *parent, struct token *token, struct token *prev, struct token *list)
227 struct position *pos = &token->pos;
229 prev->next = token->next;
230 while (!eof_token(list) && !match_op(list, SPECIAL_ARG_SEPARATOR)) {
231 struct token *newtok = dup_token(list, &token->pos, pos);
232 newtok->parent = parent;
233 insert(newtok, prev);
234 prev = newtok;
235 list = list->next;
236 pos = &list->pos;
238 return prev;
241 static struct token *get_argument(int nr, struct token *args)
243 if (!nr)
244 return args;
245 while (!eof_token(args)) {
246 if (match_op(args, SPECIAL_ARG_SEPARATOR))
247 if (!--nr)
248 return args->next;
249 args = args->next;
252 return args;
255 static struct token *stringify(struct token *token, struct token *arg)
257 const char *s = show_token_sequence(arg);
258 int size = strlen(s)+1;
259 struct token *newtoken = alloc_token(&token->pos);
260 struct string *string = __alloc_string(size);
262 newtoken->pos.newline = token->pos.newline;
263 memcpy(string->data, s, size);
264 string->length = size;
265 token_type(newtoken) = TOKEN_STRING;
266 newtoken->string = string;
267 newtoken->next = &eof_token_entry;
268 return newtoken;
271 static int arg_number(struct token *arglist, struct ident *ident)
273 int nr = 0;
275 while (!eof_token(arglist)) {
276 if (match_op(arglist, SPECIAL_ELLIPSIS) && ident == &__VA_ARGS___ident)
277 return nr;
278 if (arglist->ident == ident)
279 return nr;
280 nr++;
281 arglist = arglist->next;
283 return -1;
286 static struct token empty_arg_token = { .pos = { .type = TOKEN_EOF } };
288 static struct token *expand_one_arg(struct token *parent, struct token *head, struct token *token,
289 struct token *arglist, struct token *arguments)
291 int nr = arg_number(arglist, token->ident);
292 struct token *orig_head = head;
294 if (nr >= 0) {
295 struct token *arg = get_argument(nr, arguments);
296 struct token *last = token->next;
297 token->next = &eof_token_entry;
300 * Special case for gcc 'x ## arg' semantics: if 'arg' is empty
301 * then the 'x' goes away too.
303 if (match_op(head, SPECIAL_HASHHASH) && eof_token(arg)) {
304 arg = &empty_arg_token;
305 empty_arg_token.next = &eof_token_entry;
308 head = replace(NULL, token, head, arg);
309 if (!match_op(orig_head, SPECIAL_HASHHASH) && !match_op(last, SPECIAL_HASHHASH) && !match_op(orig_head, '#'))
310 head = expand_list(parent, orig_head);
311 head->next = last;
312 return head;
314 return token;
317 static void expand_arguments(struct token *parent,
318 struct token *token, struct token *head,
319 struct token *arguments, struct token *arglist)
321 for (;;) {
322 struct token *next = head->next;
324 /* Did we hit the end of the current expansion? */
325 if (eof_token(next))
326 break;
328 if (match_op(next, '#')) {
329 struct token *nextnext = next->next;
330 int nr = arg_number(arglist, nextnext->ident);
331 if (nextnext != head && nr >= 0 && token_type(nextnext) == TOKEN_IDENT) {
332 struct token *newtoken = stringify(nextnext, get_argument(nr, arguments));
333 replace(NULL, nextnext, head, newtoken);
334 continue;
336 warn(next->pos, "'#' operation is not followed by argument name");
339 if (token_type(next) == TOKEN_IDENT)
340 next = expand_one_arg(parent, head, next, arglist, arguments);
342 head = next;
347 * Possibly valid combinations:
348 * - anything + 'empty_arg_token' is empty.
349 * - ident + ident - combine (==ident)
350 * - ident + number - combine (==ident)
351 * - number + number - combine (==number)
352 * - number + ident - combine (==number)
353 * - string + string - leave as is, C will combine them anyway
354 * others cause an error and leave the two tokens as separate tokens.
356 static struct token *hashhash(struct token *head, struct token *first, struct token *second)
358 static char buffer[512], *p;
359 struct token *newtoken;
360 static const char *src;
361 int len;
363 first->next = second;
366 * Special case for gcc 'x ## arg' semantics: if 'arg' is empty
367 * then the 'x' goes away too.
369 * See expand_one_arg.
371 if (token_type(second) == TOKEN_EOF) {
372 head->next = second->next;
373 return head;
376 p = buffer;
377 switch (token_type(first)) {
378 case TOKEN_INTEGER:
379 len = strlen(first->integer);
380 src = first->integer;
381 break;
382 case TOKEN_IDENT:
383 len = first->ident->len;
384 src = first->ident->name;
385 break;
386 default:
387 return second;
389 memcpy(p, src, len);
390 p += len;
392 switch (token_type(second)) {
393 case TOKEN_INTEGER:
394 len = strlen(second->integer);
395 src = second->integer;
396 break;
397 case TOKEN_IDENT:
398 len = second->ident->len;
399 src = second->ident->name;
400 break;
401 default:
402 return second;
404 memcpy(p, src, len);
405 p += len;
406 *p++ = 0;
408 newtoken = alloc_token(&first->pos);
409 head->next = newtoken;
410 token_type(newtoken) = token_type(first);
411 switch (token_type(newtoken)) {
412 case TOKEN_IDENT:
413 newtoken->ident = built_in_ident(buffer);
414 break;
415 case TOKEN_INTEGER:
416 newtoken->integer = __alloc_bytes(p - buffer);
417 memcpy(newtoken->integer, buffer, p - buffer);
418 break;
420 return newtoken;
423 static void retokenize(struct token *head)
425 struct token * next = head->next;
426 struct token * nextnext = next->next;
427 struct token * nextnextnext = nextnext->next;
429 if (eof_token(next) || eof_token(nextnext))
430 return;
432 for (;;) {
433 if (eof_token(nextnextnext))
434 break;
436 if (match_op(nextnext, SPECIAL_HASHHASH)) {
437 struct token *newtoken = hashhash(head, next, nextnextnext);
439 next = newtoken;
440 nextnext = nextnextnext->next;
441 nextnextnext = nextnext->next;
443 newtoken->next = nextnext;
444 if (!eof_token(nextnext))
445 continue;
446 break;
449 head = next;
450 next = nextnext;
451 nextnext = nextnext->next;
452 nextnextnext = nextnextnext->next;
456 static struct token *expand(struct token *parent, struct token *head, struct symbol *sym)
458 struct token *arguments, *token, *last;
460 token = head->next;
461 last = token->next;
463 arguments = NULL;
464 if (sym->arglist) {
465 arguments = last->next;
466 last = find_argument_end(last, sym->arglist);
468 token->next = &eof_token_entry;
470 /* Replace the token with the token expansion */
471 replace(parent, token, head, sym->expansion);
473 /* Then, replace all the arguments with their expansions */
474 if (arguments)
475 expand_arguments(parent, token, head, arguments, sym->arglist);
477 /* Re-tokenize the sequence if any ## token exists.. */
478 retokenize(head);
480 token = head;
481 while (!eof_token(token->next))
482 token = token->next;
483 token->next = last;
484 return head;
487 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
489 struct token *last;
490 static char buffer[256];
491 char *ptr = buffer;
493 last = token;
494 while (!eof_token(token) && !match_op(token, endop)) {
495 int len;
496 const char *val = token->string->data;
497 if (token_type(token) != TOKEN_STRING)
498 val = show_token(token);
499 len = strlen(val);
500 memcpy(ptr, val, len);
501 ptr += len;
502 token = token->next;
504 *ptr = 0;
505 if (endop && !match_op(token, endop))
506 warn(start->pos, "expected '>' at end of filename");
507 return buffer;
510 static int try_include(const char *path, int plen, const char *filename, int flen, struct token *head)
512 int fd;
513 static char fullname[PATH_MAX];
515 memcpy(fullname, path, plen);
516 if (plen && path[plen-1] != '/') {
517 fullname[plen] = '/';
518 plen++;
520 memcpy(fullname+plen, filename, flen);
521 fd = open(fullname, O_RDONLY);
522 if (fd >= 0) {
523 char * streamname = __alloc_bytes(plen + flen);
524 memcpy(streamname, fullname, plen + flen);
525 head->next = tokenize(streamname, fd, head->next);
526 close(fd);
527 return 1;
529 return 0;
532 static int do_include_path(const char **pptr, struct token *head, struct token *token, const char *filename, int flen)
534 const char *path;
536 while ((path = *pptr++) != NULL) {
537 if (!try_include(path, strlen(path), filename, flen, head))
538 continue;
539 return 1;
541 return 0;
545 static void do_include(int local, struct stream *stream, struct token *head, struct token *token, const char *filename)
547 int flen = strlen(filename) + 1;
549 /* Same directory as current stream? */
550 if (local) {
551 const char *path;
552 char *slash;
553 int plen;
555 path = stream->name;
556 slash = strrchr(path, '/');
557 plen = slash ? slash - path : 0;
559 if (try_include(path, plen, filename, flen, head))
560 return;
563 /* Check the standard include paths.. */
564 if (do_include_path(includepath, head, token, filename, flen))
565 return;
566 if (do_include_path(sys_includepath, head, token, filename, flen))
567 return;
568 if (do_include_path(gcc_includepath, head, token, filename, flen))
569 return;
571 error(token->pos, "unable to open '%s'", filename);
574 static int handle_include(struct stream *stream, struct token *head, struct token *token)
576 const char *filename;
577 struct token *next;
578 int expect;
580 if (stream->constant == -1)
581 stream->constant = 0;
582 if (false_nesting)
583 return 1;
584 next = token->next;
585 expect = '>';
586 if (!match_op(next, '<')) {
587 expand_list(NULL, token);
588 expect = 0;
589 next = token;
591 token = next->next;
592 filename = token_name_sequence(token, expect, token);
593 do_include(!expect, stream, head, token, filename);
594 return 1;
597 static int token_list_different(struct token *list1, struct token *list2)
599 for (;;) {
600 if (list1 == list2)
601 return 0;
602 if (!list1 || !list2)
603 return 1;
604 if (token_type(list1) != token_type(list2))
605 return 1;
606 list1 = list1->next;
607 list2 = list2->next;
612 static int handle_define(struct stream *stream, struct token *head, struct token *token)
614 struct token *arglist, *expansion;
615 struct token *left = token->next;
616 struct symbol *sym;
617 struct ident *name;
619 if (token_type(left) != TOKEN_IDENT) {
620 warn(head->pos, "expected identifier to 'define'");
621 return 0;
623 if (false_nesting)
624 return 1;
625 name = left->ident;
627 arglist = NULL;
628 expansion = left->next;
629 if (!expansion->pos.whitespace && match_op(expansion, '(')) {
630 arglist = expansion;
631 while (!eof_token(expansion)) {
632 struct token *next = expansion->next;
633 if (token_type(expansion) == TOKEN_IDENT) {
634 if (expansion->ident == &__VA_ARGS___ident)
635 warn(expansion->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
636 if (match_op(next, SPECIAL_ELLIPSIS)) {
637 expansion->next = &variable_argument;
638 next = next->next;
641 if (match_op(next, ')')) {
642 // Terminate the arglist
643 expansion->next = &eof_token_entry;
644 expansion = next->next;
645 break;
647 if (match_op(next, ','))
648 expansion->next = next->next;
649 expansion = next;
651 arglist = arglist->next;
654 sym = lookup_symbol(name, NS_PREPROCESSOR);
655 if (sym) {
656 if (token_list_different(sym->expansion, expansion) ||
657 token_list_different(sym->arglist, arglist)) {
658 warn(left->pos, "preprocessor token %.*s redefined",
659 name->len, name->name);
660 warn(sym->pos, "this was the original definition");
662 return 1;
664 sym = alloc_symbol(left->pos, SYM_NODE);
665 bind_symbol(sym, name, NS_PREPROCESSOR);
667 sym->expansion = expansion;
668 sym->arglist = arglist;
669 return 1;
672 static int handle_undef(struct stream *stream, struct token *head, struct token *token)
674 struct token *left = token->next;
675 struct symbol **sym;
677 if (token_type(left) != TOKEN_IDENT) {
678 warn(head->pos, "expected identifier to 'undef'");
679 return 0;
681 if (false_nesting)
682 return 1;
683 sym = &left->ident->symbols;
684 while (*sym) {
685 struct symbol *t = *sym;
686 if (t->namespace == NS_PREPROCESSOR) {
687 *sym = t->next_id;
688 return 1;
690 sym = &t->next_id;
692 return 1;
695 static int preprocessor_if(struct token *token, int true)
697 if (if_nesting == 0)
698 unmatched_if = token;
699 if (if_nesting >= MAX_NEST)
700 error(token->pos, "Maximum preprocessor conditional level exhausted");
701 elif_ignore[if_nesting] = false_nesting || true;
702 if (false_nesting || !true) {
703 false_nesting++;
704 return 1;
706 true_nesting++;
707 return 1;
710 static int token_defined(struct token *token)
712 if (token_type(token) == TOKEN_IDENT)
713 return lookup_symbol(token->ident, NS_PREPROCESSOR) != NULL;
715 warn(token->pos, "expected identifier for #if[n]def");
716 return 0;
719 static int handle_ifdef(struct stream *stream, struct token *head, struct token *token)
721 return preprocessor_if(token, token_defined(token->next));
724 static int handle_ifndef(struct stream *stream, struct token *head, struct token *token)
726 struct token *next = token->next;
727 if (stream->constant == -1) {
728 int newconstant = 0;
729 if (token_type(next) == TOKEN_IDENT) {
730 if (!stream->protect || stream->protect == next->ident) {
731 newconstant = -2;
732 stream->protect = next->ident;
733 stream->nesting = if_nesting+1;
736 stream->constant = newconstant;
738 return preprocessor_if(token, !token_defined(next));
741 static int expression_value(struct token *head)
743 struct expression *expr;
744 struct token *token;
745 long long value;
747 expand_list(NULL, head);
748 token = constant_expression(head->next, &expr);
749 if (!eof_token(token))
750 warn(token->pos, "garbage at end: %s", show_token_sequence(token));
751 value = get_expression_value(expr);
752 return value != 0;
755 static int handle_if(struct stream *stream, struct token *head, struct token *token)
757 int value = 0;
758 if (!false_nesting)
759 value = expression_value(token);
760 return preprocessor_if(token, value);
763 static int handle_elif(struct stream * stream, struct token *head, struct token *token)
765 if (stream->nesting == if_nesting)
766 stream->constant = 0;
767 if (false_nesting) {
768 /* If this whole if-thing is if'ed out, an elif cannot help */
769 if (elif_ignore[if_nesting-1])
770 return 1;
771 if (expression_value(token)) {
772 false_nesting--;
773 true_nesting++;
774 elif_ignore[if_nesting-1] = 1;
776 return 1;
778 if (true_nesting) {
779 false_nesting = 1;
780 true_nesting--;
781 return 1;
783 warn(token->pos, "unmatched '#elif'");
784 return 1;
787 static int handle_else(struct stream *stream, struct token *head, struct token *token)
789 if (stream->nesting == if_nesting)
790 stream->constant = 0;
791 if (false_nesting) {
792 /* If this whole if-thing is if'ed out, an else cannot help */
793 if (elif_ignore[if_nesting-1])
794 return 1;
795 false_nesting--;
796 true_nesting++;
797 elif_ignore[if_nesting-1] = 1;
798 return 1;
800 if (true_nesting) {
801 true_nesting--;
802 false_nesting = 1;
803 return 1;
805 warn(token->pos, "unmatched #else");
806 return 1;
809 static int handle_endif(struct stream *stream, struct token *head, struct token *token)
811 if (stream->constant == -2 && stream->nesting == if_nesting)
812 stream->constant = -1;
814 if (false_nesting) {
815 false_nesting--;
816 return 1;
818 if (true_nesting) {
819 true_nesting--;
820 return 1;
822 warn(token->pos, "unmatched #endif");
823 return 1;
826 static const char *show_token_sequence(struct token *token)
828 static char buffer[256];
829 char *ptr = buffer;
830 int whitespace = 0;
832 if (!token)
833 return "<none>";
834 while (!eof_token(token) && !match_op(token, SPECIAL_ARG_SEPARATOR)) {
835 const char *val = show_token(token);
836 int len = strlen(val);
837 if (whitespace)
838 *ptr++ = ' ';
839 memcpy(ptr, val, len);
840 ptr += len;
841 token = token->next;
842 whitespace = token->pos.whitespace;
844 *ptr = 0;
845 return buffer;
848 static int handle_warning(struct stream *stream, struct token *head, struct token *token)
850 if (false_nesting)
851 return 1;
852 warn(token->pos, "%s", show_token_sequence(token->next));
853 return 1;
856 static int handle_error(struct stream *stream, struct token *head, struct token *token)
858 if (false_nesting)
859 return 1;
860 warn(token->pos, "%s", show_token_sequence(token->next));
861 return 1;
864 static int handle_nostdinc(struct stream *stream, struct token *head, struct token *token)
866 if (false_nesting)
867 return 1;
868 includepath[0] = NULL;
869 return 1;
872 static void add_path_entry(struct token *token, const char *path)
874 int i;
876 for (i = 0; i < INCLUDEPATHS; i++) {
877 if (!includepath[i]) {
878 includepath[i] = path;
879 includepath[i+1] = NULL;
880 return;
883 warn(token->pos, "too many include path entries");
886 static int handle_add_include(struct stream *stream, struct token *head, struct token *token)
888 for (;;) {
889 token = token->next;
890 if (eof_token(token))
891 return 1;
892 if (token_type(token) != TOKEN_STRING) {
893 warn(token->pos, "expected path string");
894 return 1;
896 add_path_entry(token, token->string->data);
901 * We replace "#pragma xxx" with "__pragma__" in the token
902 * stream. Just as an example.
904 * We'll just #define that away for now, but the theory here
905 * is that we can use this to insert arbitrary token sequences
906 * to turn the pragma's into internal front-end sequences for
907 * when we actually start caring about them.
909 * So eventually this will turn into some kind of extended
910 * __attribute__() like thing, except called __pragma__(xxx).
912 static int handle_pragma(struct stream *stream, struct token *head, struct token *token)
914 struct token *next = head->next;
916 token->ident = &pragma_ident;
917 token->pos.newline = 1;
918 token->pos.whitespace = 1;
919 token->pos.pos = 1;
920 head->next = token;
921 token->next = next;
922 return 1;
925 static int handle_preprocessor_command(struct stream *stream, struct token *head, struct ident *ident, struct token *token)
927 int i;
928 static struct {
929 const char *name;
930 int (*handler)(struct stream *, struct token *, struct token *);
931 } handlers[] = {
932 { "define", handle_define },
933 { "undef", handle_undef },
934 { "ifdef", handle_ifdef },
935 { "ifndef", handle_ifndef },
936 { "else", handle_else },
937 { "endif", handle_endif },
938 { "if", handle_if },
939 { "elif", handle_elif },
940 { "warning", handle_warning },
941 { "error", handle_error },
942 { "include", handle_include },
943 { "pragma", handle_pragma },
945 // our internal preprocessor tokens
946 { "nostdinc", handle_nostdinc },
947 { "add_include", handle_add_include },
950 for (i = 0; i < (sizeof (handlers) / sizeof (handlers[0])); i++) {
951 if (match_string_ident(ident, handlers[i].name))
952 return handlers[i].handler(stream, head, token);
954 return 0;
957 static void handle_preprocessor_line(struct stream *stream, struct token * head, struct token *token)
959 if (!token)
960 return;
962 if (token_type(token) == TOKEN_IDENT)
963 if (handle_preprocessor_command(stream, head, token->ident, token))
964 return;
965 warn(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
968 static void preprocessor_line(struct stream *stream, struct token * head)
970 struct token *start = head->next, *next;
971 struct token **tp = &start->next;
973 for (;;) {
974 next = *tp;
975 if (next->pos.newline)
976 break;
977 tp = &next->next;
979 head->next = next;
980 *tp = &eof_token_entry;
981 handle_preprocessor_line(stream, head, start->next);
984 static void do_preprocess(struct token *head)
986 do {
987 struct token *next = head->next;
988 struct stream *stream = input_streams + next->pos.stream;
990 if (next->pos.newline && match_op(next, '#')) {
991 preprocessor_line(stream, head);
992 continue;
995 if (false_nesting) {
996 head->next = next->next;
997 continue;
1000 switch (token_type(next)) {
1001 case TOKEN_STREAMEND:
1002 if (stream->constant == -1 && stream->protect) {
1003 stream->constant = 1;
1005 /* fallthrough */
1006 case TOKEN_STREAMBEGIN:
1007 head->next = next->next;
1008 continue;
1010 case TOKEN_IDENT:
1011 next = expand_one_symbol(next, head, next);
1012 /* fallthrough */
1013 default:
1015 * Any token expansion (even if it ended up being an
1016 * empty expansion) in this stream implies it can't
1017 * be constant.
1019 stream->constant = 0;
1022 head = next;
1023 } while (!eof_token(head));
1026 struct token * preprocess(struct token *token)
1028 struct token header = { };
1030 preprocessing = 1;
1031 header.next = token;
1032 do_preprocess(&header);
1033 if (if_nesting)
1034 warn(unmatched_if->pos, "unmatched preprocessor conditional");
1036 // Drop all expressions from pre-processing, they're not used any more.
1037 clear_expression_alloc();
1038 preprocessing = 0;
1040 return header.next;