Move constant switch simplification to the instruction
[smatch.git] / pre-process.c
blobd243766ab84dae6070f6f67aed6bbb9b65e0c2c9
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>
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 static int true_nesting = 0;
30 static int false_nesting = 0;
31 static struct token *unmatched_if = NULL;
32 #define if_nesting (true_nesting + false_nesting)
34 #define MAX_NEST (256)
35 static unsigned char elif_ignore[MAX_NEST];
36 enum { ELIF_IGNORE = 1, ELIF_SEEN_ELSE = 2 };
38 #define INCLUDEPATHS 300
39 const char *includepath[INCLUDEPATHS+1] = {
40 "/usr/include",
41 "/usr/local/include",
42 GCC_INTERNAL_INCLUDE,
43 NULL
46 static const char **sys_includepath = includepath + 0;
47 static const char **gcc_includepath = includepath + 2;
49 #define MARK_STREAM_NONCONST(pos) do { \
50 if (stream->constant != CONSTANT_FILE_NOPE) { \
51 if (0) \
52 info(pos, "%s triggers non-const", __func__); \
53 stream->constant = CONSTANT_FILE_NOPE; \
54 } \
55 } while (0)
58 static struct token *alloc_token(struct position *pos)
60 struct token *token = __alloc_token(0);
62 token->pos.stream = pos->stream;
63 token->pos.line = pos->line;
64 token->pos.pos = pos->pos;
65 token->pos.whitespace = 1;
66 return token;
69 static const char *show_token_sequence(struct token *token);
71 /* Expand symbol 'sym' at '*list' */
72 static int expand(struct token **, struct symbol *);
74 static void replace_with_string(struct token *token, const char *str)
76 int size = strlen(str) + 1;
77 struct string *s = __alloc_string(size);
79 s->length = size;
80 memcpy(s->data, str, size);
81 token_type(token) = TOKEN_STRING;
82 token->string = s;
85 static void replace_with_integer(struct token *token, unsigned int val)
87 char *buf = __alloc_bytes(11);
88 sprintf(buf, "%u", val);
89 token_type(token) = TOKEN_NUMBER;
90 token->number = buf;
93 static int token_defined(struct token *token)
95 if (token_type(token) == TOKEN_IDENT) {
96 struct symbol *sym = lookup_symbol(token->ident, NS_MACRO);
97 if (sym) {
98 sym->weak = 0;
99 return 1;
101 return 0;
104 warning(token->pos, "expected preprocessor identifier");
105 return 0;
108 static void replace_with_defined(struct token *token)
110 static const char *string[] = { "0", "1" };
111 int defined = token_defined(token);
113 token_type(token) = TOKEN_NUMBER;
114 token->number = string[defined];
117 static int expand_one_symbol(struct token **list)
119 struct token *token = *list;
120 struct symbol *sym;
122 if (token->pos.noexpand)
123 return 1;
125 sym = lookup_symbol(token->ident, NS_MACRO);
126 if (sym) {
127 sym->weak = 0;
128 return expand(list, sym);
130 if (token->ident == &__LINE___ident) {
131 replace_with_integer(token, token->pos.line);
132 } else if (token->ident == &__FILE___ident) {
133 replace_with_string(token, (input_streams + token->pos.stream)->name);
135 return 1;
138 static inline struct token *scan_next(struct token **where)
140 struct token *token = *where;
141 if (token_type(token) != TOKEN_UNTAINT)
142 return token;
143 do {
144 token->ident->tainted = 0;
145 token = token->next;
146 } while (token_type(token) == TOKEN_UNTAINT);
147 *where = token;
148 return token;
151 static void expand_list(struct token **list)
153 struct token *next;
154 while (!eof_token(next = scan_next(list))) {
155 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
156 list = &next->next;
160 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
162 struct token **p = &prev->next;
163 struct token *next;
164 int nesting = 0;
166 while (!eof_token(next = scan_next(p))) {
167 if (match_op(next, '(')) {
168 nesting++;
169 } else if (match_op(next, ')')) {
170 if (!nesting--)
171 break;
172 } else if (match_op(next, ',') && !nesting && !vararg) {
173 break;
175 next->pos.stream = pos->stream;
176 next->pos.line = pos->line;
177 next->pos.pos = pos->pos;
178 p = &next->next;
180 *p = &eof_token_entry;
181 return next;
185 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
188 struct arg {
189 struct token *arg;
190 struct token *expanded;
191 struct token *str;
192 int n_normal;
193 int n_quoted;
194 int n_str;
197 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
199 int wanted = arglist->count.normal;
200 struct token *next = NULL;
201 int count = 0;
203 arglist = arglist->next; /* skip counter */
205 if (!wanted) {
206 next = collect_arg(start, 0, &what->pos);
207 if (eof_token(next))
208 goto Eclosing;
209 if (!eof_token(start->next) || !match_op(next, ')')) {
210 count++;
211 goto Emany;
213 } else {
214 for (count = 0; count < wanted; count++) {
215 struct argcount *p = &arglist->next->count;
216 next = collect_arg(start, p->vararg, &what->pos);
217 arglist = arglist->next->next;
218 if (eof_token(next))
219 goto Eclosing;
220 args[count].arg = start->next;
221 args[count].n_normal = p->normal;
222 args[count].n_quoted = p->quoted;
223 args[count].n_str = p->str;
224 if (match_op(next, ')')) {
225 count++;
226 break;
228 start = next;
230 if (count == wanted && !match_op(next, ')'))
231 goto Emany;
232 if (count == wanted - 1) {
233 struct argcount *p = &arglist->next->count;
234 if (!p->vararg)
235 goto Efew;
236 args[count].arg = NULL;
237 args[count].n_normal = p->normal;
238 args[count].n_quoted = p->quoted;
239 args[count].n_str = p->str;
241 if (count < wanted - 1)
242 goto Efew;
244 what->next = next->next;
245 return 1;
247 Efew:
248 warning(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
249 show_token(what), wanted, count);
250 goto out;
251 Emany:
252 while (match_op(next, ',')) {
253 next = collect_arg(next, 0, &what->pos);
254 count++;
256 if (eof_token(next))
257 goto Eclosing;
258 warning(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
259 show_token(what), count, wanted);
260 goto out;
261 Eclosing:
262 warning(what->pos, "unterminated argument list invoking macro \"%s\"",
263 show_token(what));
264 out:
265 what->next = next->next;
266 return 0;
269 static struct token *dup_list(struct token *list)
271 struct token *res;
272 struct token **p = &res;
274 while (!eof_token(list)) {
275 struct token *newtok = __alloc_token(0);
276 *newtok = *list;
277 *p = newtok;
278 p = &newtok->next;
279 list = list->next;
281 return res;
284 static struct token *stringify(struct token *arg)
286 const char *s = show_token_sequence(arg);
287 int size = strlen(s)+1;
288 struct token *token = __alloc_token(0);
289 struct string *string = __alloc_string(size);
291 memcpy(string->data, s, size);
292 string->length = size;
293 token->pos = arg->pos;
294 token_type(token) = TOKEN_STRING;
295 token->string = string;
296 token->next = &eof_token_entry;
297 return token;
300 static void expand_arguments(int count, struct arg *args)
302 int i;
303 for (i = 0; i < count; i++) {
304 struct token *arg = args[i].arg;
305 if (!arg)
306 arg = &eof_token_entry;
307 if (args[i].n_str)
308 args[i].str = stringify(arg);
309 if (args[i].n_normal) {
310 if (!args[i].n_quoted) {
311 args[i].expanded = arg;
312 args[i].arg = NULL;
313 } else if (eof_token(arg)) {
314 args[i].expanded = arg;
315 } else {
316 args[i].expanded = dup_list(arg);
318 expand_list(&args[i].expanded);
324 * Possibly valid combinations:
325 * - ident + ident -> ident
326 * - ident + number -> ident unless number contains '.', '+' or '-'.
327 * - number + number -> number
328 * - number + ident -> number
329 * - number + '.' -> number
330 * - number + '+' or '-' -> number, if number used to end on [eEpP].
331 * - '.' + number -> number, if number used to start with a digit.
332 * - special + special -> either special or an error.
334 static enum token_type combine(struct token *left, struct token *right, char *p)
336 int len;
337 enum token_type t1 = token_type(left), t2 = token_type(right);
339 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
340 return TOKEN_ERROR;
342 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
343 return TOKEN_ERROR;
345 strcpy(p, show_token(left));
346 strcat(p, show_token(right));
347 len = strlen(p);
349 if (len >= 256)
350 return TOKEN_ERROR;
352 if (t1 == TOKEN_IDENT) {
353 if (t2 == TOKEN_SPECIAL)
354 return TOKEN_ERROR;
355 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
356 return TOKEN_ERROR;
357 return TOKEN_IDENT;
360 if (t1 == TOKEN_NUMBER) {
361 if (t2 == TOKEN_SPECIAL) {
362 switch (right->special) {
363 case '.':
364 break;
365 case '+': case '-':
366 if (strchr("eEpP", p[len - 2]))
367 break;
368 default:
369 return TOKEN_ERROR;
372 return TOKEN_NUMBER;
375 if (p[0] == '.' && isdigit((unsigned char)p[1]))
376 return TOKEN_NUMBER;
378 return TOKEN_SPECIAL;
381 static int merge(struct token *left, struct token *right)
383 extern unsigned char combinations[][3];
384 static char buffer[512];
385 int n;
387 switch (combine(left, right, buffer)) {
388 case TOKEN_IDENT:
389 left->ident = built_in_ident(buffer);
390 left->pos.noexpand = 0;
391 return 1;
393 case TOKEN_NUMBER: {
394 char *number = __alloc_bytes(strlen(buffer) + 1);
395 memcpy(number, buffer, strlen(buffer) + 1);
396 token_type(left) = TOKEN_NUMBER; /* could be . + num */
397 left->number = number;
398 return 1;
401 case TOKEN_SPECIAL:
402 if (buffer[2] && buffer[3])
403 break;
404 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
405 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
406 left->special = n;
407 return 1;
410 default:
413 warning(left->pos, "'##' failed: concatenation is not a valid token");
414 return 0;
417 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
419 struct token *alloc = alloc_token(streampos);
420 token_type(alloc) = token_type(token);
421 alloc->pos.newline = pos->newline;
422 alloc->pos.whitespace = pos->whitespace;
423 alloc->number = token->number;
424 alloc->pos.noexpand = token->pos.noexpand;
425 return alloc;
428 static struct token **copy(struct token **where, struct token *list, int *count)
430 int need_copy = --*count;
431 while (!eof_token(list)) {
432 struct token *token;
433 if (need_copy)
434 token = dup_token(list, &list->pos, &list->pos);
435 else
436 token = list;
437 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
438 token->pos.noexpand = 1;
439 *where = token;
440 where = &token->next;
441 list = list->next;
443 *where = &eof_token_entry;
444 return where;
447 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
449 struct token *token = *list;
450 struct position *base_pos = &token->pos;
451 struct position *pos = base_pos;
452 int *count;
453 enum {Normal, Placeholder, Concat} state = Normal;
455 for (; !eof_token(body); body = body->next, pos = &body->pos) {
456 struct token *added, *arg;
457 struct token **tail;
459 switch (token_type(body)) {
460 case TOKEN_GNU_KLUDGE:
462 * GNU kludge: if we had <comma>##<vararg>, behaviour
463 * depends on whether we had enough arguments to have
464 * a vararg. If we did, ## is just ignored. Otherwise
465 * both , and ## are ignored. Comma should come from
466 * the body of macro and not be an argument of earlier
467 * concatenation.
469 if (!args[body->next->argnum].arg)
470 continue;
471 added = dup_token(body, base_pos, pos);
472 token_type(added) = TOKEN_SPECIAL;
473 tail = &added->next;
474 break;
476 case TOKEN_STR_ARGUMENT:
477 arg = args[body->argnum].str;
478 count = &args[body->argnum].n_str;
479 goto copy_arg;
481 case TOKEN_QUOTED_ARGUMENT:
482 arg = args[body->argnum].arg;
483 count = &args[body->argnum].n_quoted;
484 if (!arg || eof_token(arg)) {
485 if (state == Concat)
486 state = Normal;
487 else
488 state = Placeholder;
489 continue;
491 goto copy_arg;
493 case TOKEN_MACRO_ARGUMENT:
494 arg = args[body->argnum].expanded;
495 count = &args[body->argnum].n_normal;
496 if (eof_token(arg)) {
497 state = Normal;
498 continue;
500 copy_arg:
501 tail = copy(&added, arg, count);
502 added->pos.newline = pos->newline;
503 added->pos.whitespace = pos->whitespace;
504 break;
506 case TOKEN_CONCAT:
507 if (state == Placeholder)
508 state = Normal;
509 else
510 state = Concat;
511 continue;
513 case TOKEN_IDENT:
514 added = dup_token(body, base_pos, pos);
515 if (added->ident->tainted)
516 added->pos.noexpand = 1;
517 tail = &added->next;
518 break;
520 default:
521 added = dup_token(body, base_pos, pos);
522 tail = &added->next;
523 break;
527 * if we got to doing real concatenation, we already have
528 * added something into the list, so containing_token() is OK.
530 if (state == Concat && merge(containing_token(list), added)) {
531 *list = added->next;
532 if (tail != &added->next)
533 list = tail;
534 } else {
535 *list = added;
536 list = tail;
538 state = Normal;
540 *list = &eof_token_entry;
541 return list;
544 static int expand(struct token **list, struct symbol *sym)
546 struct token *last;
547 struct token *token = *list;
548 struct ident *expanding = token->ident;
549 struct token **tail;
550 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
551 struct arg args[nargs];
553 if (expanding->tainted) {
554 token->pos.noexpand = 1;
555 return 1;
558 if (sym->arglist) {
559 if (!match_op(scan_next(&token->next), '('))
560 return 1;
561 if (!collect_arguments(token->next, sym->arglist, args, token))
562 return 1;
563 expand_arguments(nargs, args);
566 expanding->tainted = 1;
568 last = token->next;
569 tail = substitute(list, sym->expansion, args);
570 *tail = last;
572 return 0;
575 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
577 struct token *last;
578 static char buffer[256];
579 char *ptr = buffer;
581 last = token;
582 while (!eof_token(token) && !match_op(token, endop)) {
583 int len;
584 const char *val = token->string->data;
585 if (token_type(token) != TOKEN_STRING)
586 val = show_token(token);
587 len = strlen(val);
588 memcpy(ptr, val, len);
589 ptr += len;
590 token = token->next;
592 *ptr = 0;
593 if (endop && !match_op(token, endop))
594 warning(start->pos, "expected '>' at end of filename");
595 return buffer;
598 static int try_include(const char *path, int plen, const char *filename, int flen, struct token **where, const char **next_path)
600 int fd;
601 static char fullname[PATH_MAX];
603 memcpy(fullname, path, plen);
604 if (plen && path[plen-1] != '/') {
605 fullname[plen] = '/';
606 plen++;
608 memcpy(fullname+plen, filename, flen);
609 fd = open(fullname, O_RDONLY);
610 if (fd >= 0) {
611 char * streamname = __alloc_bytes(plen + flen);
612 memcpy(streamname, fullname, plen + flen);
613 *where = tokenize(streamname, fd, *where, next_path);
614 close(fd);
615 return 1;
617 return 0;
620 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
622 const char *path;
624 while ((path = *pptr++) != NULL) {
625 if (!try_include(path, strlen(path), filename, flen, list, pptr))
626 continue;
627 return 1;
629 return 0;
633 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
635 int flen = strlen(filename) + 1;
637 /* Absolute path? */
638 if (filename[0] == '/') {
639 if (try_include("", 0, filename, flen, list, includepath))
640 return;
641 goto out;
644 /* Same directory as current stream? */
645 if (local) {
646 const char *path;
647 char *slash;
648 int plen;
650 path = stream->name;
651 slash = strrchr(path, '/');
652 plen = slash ? slash - path : 0;
654 if (try_include(path, plen, filename, flen, list, includepath))
655 return;
658 /* Check the standard include paths.. */
659 if (do_include_path(path, list, token, filename, flen))
660 return;
661 out:
662 error_die(token->pos, "unable to open '%s'", filename);
665 static int free_preprocessor_line(struct token *token)
667 do {
668 struct token *free = token;
669 token = token->next;
670 __free_token(free);
671 } while (token_type(token) != TOKEN_EOF);
672 return 1;
675 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
677 const char *filename;
678 struct token *next;
679 int expect;
681 if (false_nesting)
682 return free_preprocessor_line(token);
684 if (stream->constant == CONSTANT_FILE_MAYBE)
685 MARK_STREAM_NONCONST(token->pos);
687 next = token->next;
688 expect = '>';
689 if (!match_op(next, '<')) {
690 expand_list(&token->next);
691 expect = 0;
692 next = token;
693 if (match_op(token->next, '<')) {
694 next = token->next;
695 expect = '>';
698 token = next->next;
699 filename = token_name_sequence(token, expect, token);
700 do_include(!expect, stream, list, token, filename, path);
701 return 1;
704 static int handle_include(struct stream *stream, struct token **list, struct token *token)
706 return handle_include_path(stream, list, token, includepath);
709 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
711 return handle_include_path(stream, list, token, stream->next_path);
714 static int token_different(struct token *t1, struct token *t2)
716 int different;
718 if (token_type(t1) != token_type(t2))
719 return 1;
721 switch (token_type(t1)) {
722 case TOKEN_IDENT:
723 different = t1->ident != t2->ident;
724 break;
725 case TOKEN_ARG_COUNT:
726 case TOKEN_UNTAINT:
727 case TOKEN_CONCAT:
728 case TOKEN_GNU_KLUDGE:
729 different = 0;
730 break;
731 case TOKEN_NUMBER:
732 different = strcmp(t1->number, t2->number);
733 break;
734 case TOKEN_SPECIAL:
735 different = t1->special != t2->special;
736 break;
737 case TOKEN_MACRO_ARGUMENT:
738 case TOKEN_QUOTED_ARGUMENT:
739 case TOKEN_STR_ARGUMENT:
740 different = t1->argnum != t2->argnum;
741 break;
742 case TOKEN_CHAR:
743 different = t1->character != t2->character;
744 break;
745 case TOKEN_STRING: {
746 struct string *s1, *s2;
748 s1 = t1->string;
749 s2 = t2->string;
750 different = 1;
751 if (s1->length != s2->length)
752 break;
753 different = memcmp(s1->data, s2->data, s1->length);
754 break;
756 default:
757 different = 1;
758 break;
760 return different;
763 static int token_list_different(struct token *list1, struct token *list2)
765 for (;;) {
766 if (list1 == list2)
767 return 0;
768 if (!list1 || !list2)
769 return 1;
770 if (token_different(list1, list2))
771 return 1;
772 list1 = list1->next;
773 list2 = list2->next;
777 static inline void set_arg_count(struct token *token)
779 token_type(token) = TOKEN_ARG_COUNT;
780 token->count.normal = token->count.quoted =
781 token->count.str = token->count.vararg = 0;
784 static struct token *parse_arguments(struct token *list)
786 struct token *arg = list->next, *next = list;
787 struct argcount *count = &list->count;
789 set_arg_count(list);
791 if (match_op(arg, ')')) {
792 next = arg->next;
793 list->next = &eof_token_entry;
794 return next;
797 while (token_type(arg) == TOKEN_IDENT) {
798 if (arg->ident == &__VA_ARGS___ident)
799 goto Eva_args;
800 if (!++count->normal)
801 goto Eargs;
802 next = arg->next;
804 if (match_op(next, ',')) {
805 set_arg_count(next);
806 arg = next->next;
807 continue;
810 if (match_op(next, ')')) {
811 set_arg_count(next);
812 next = next->next;
813 arg->next->next = &eof_token_entry;
814 return next;
817 /* normal cases are finished here */
819 if (match_op(next, SPECIAL_ELLIPSIS)) {
820 if (match_op(next->next, ')')) {
821 set_arg_count(next);
822 next->count.vararg = 1;
823 next = next->next;
824 arg->next->next = &eof_token_entry;
825 return next->next;
828 arg = next;
829 goto Enotclosed;
832 if (eof_token(next)) {
833 goto Enotclosed;
834 } else {
835 arg = next;
836 goto Ebadstuff;
840 if (match_op(arg, SPECIAL_ELLIPSIS)) {
841 next = arg->next;
842 token_type(arg) = TOKEN_IDENT;
843 arg->ident = &__VA_ARGS___ident;
844 if (!match_op(next, ')'))
845 goto Enotclosed;
846 if (!++count->normal)
847 goto Eargs;
848 set_arg_count(next);
849 next->count.vararg = 1;
850 next = next->next;
851 arg->next->next = &eof_token_entry;
852 return next;
855 if (eof_token(arg)) {
856 arg = next;
857 goto Enotclosed;
859 if (match_op(arg, ','))
860 goto Emissing;
861 else
862 goto Ebadstuff;
865 Emissing:
866 warning(arg->pos, "parameter name missing");
867 return NULL;
868 Ebadstuff:
869 warning(arg->pos, "\"%s\" may not appear in macro parameter list",
870 show_token(arg));
871 return NULL;
872 Enotclosed:
873 warning(arg->pos, "missing ')' in macro parameter list");
874 return NULL;
875 Eva_args:
876 warning(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
877 return NULL;
878 Eargs:
879 warning(arg->pos, "too many arguments in macro definition");
880 return NULL;
883 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
885 struct ident *ident = token->ident;
886 int nr;
888 if (!arglist || token_type(token) != TOKEN_IDENT)
889 return 0;
891 arglist = arglist->next;
893 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
894 if (arglist->ident == ident) {
895 struct argcount *count = &arglist->next->count;
896 int n;
898 token->argnum = nr;
899 token_type(token) = type;
900 switch (type) {
901 case TOKEN_MACRO_ARGUMENT:
902 n = ++count->normal;
903 break;
904 case TOKEN_QUOTED_ARGUMENT:
905 n = ++count->quoted;
906 break;
907 default:
908 n = ++count->str;
910 if (n)
911 return count->vararg ? 2 : 1;
912 token_type(token) = TOKEN_ERROR;
913 return -1;
916 return 0;
919 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
921 struct token *token = expansion;
922 struct token **p;
923 struct token *last = NULL;
925 if (match_op(token, SPECIAL_HASHHASH))
926 goto Econcat;
928 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
929 if (match_op(token, '#')) {
930 if (arglist) {
931 struct token *next = token->next;
932 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
933 goto Equote;
934 next->pos.whitespace = token->pos.whitespace;
935 token = *p = next;
936 } else {
937 token->pos.noexpand = 1;
939 } else if (match_op(token, SPECIAL_HASHHASH)) {
940 struct token *next = token->next;
941 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
942 token_type(token) = TOKEN_CONCAT;
943 if (arg) {
944 token = next;
945 /* GNU kludge */
946 if (arg == 2 && last && match_op(last, ',')) {
947 token_type(last) = TOKEN_GNU_KLUDGE;
948 last->next = token;
950 } else if (match_op(next, SPECIAL_HASHHASH))
951 token = next;
952 else if (match_op(next, ','))
953 token = next;
954 else if (eof_token(next))
955 goto Econcat;
956 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
957 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
958 } else {
959 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
961 if (token_type(token) == TOKEN_ERROR)
962 goto Earg;
963 last = token;
965 token = alloc_token(&expansion->pos);
966 token_type(token) = TOKEN_UNTAINT;
967 token->ident = name;
968 token->next = *p;
969 *p = token;
970 return expansion;
972 Equote:
973 warning(token->pos, "'#' is not followed by a macro parameter");
974 return NULL;
976 Econcat:
977 warning(token->pos, "'##' cannot appear at the ends of macro expansion");
978 return NULL;
979 Earg:
980 warning(token->pos, "too many instances of argument in body");
981 return NULL;
984 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int weak)
986 struct token *arglist, *expansion;
987 struct token *left = token->next;
988 struct symbol *sym;
989 struct ident *name;
991 if (token_type(left) != TOKEN_IDENT) {
992 warning(token->pos, "expected identifier to 'define'");
993 return 0;
995 if (false_nesting)
996 return free_preprocessor_line(token);
998 if (stream->constant == CONSTANT_FILE_MAYBE)
999 MARK_STREAM_NONCONST(token->pos);
1001 __free_token(token); /* Free the "define" token, but not the rest of the line */
1002 name = left->ident;
1004 arglist = NULL;
1005 expansion = left->next;
1006 if (!expansion->pos.whitespace && match_op(expansion, '(')) {
1007 arglist = expansion;
1008 expansion = parse_arguments(expansion);
1009 if (!expansion)
1010 return 1;
1013 expansion = parse_expansion(expansion, arglist, name);
1014 if (!expansion)
1015 return 1;
1017 sym = lookup_symbol(name, NS_MACRO);
1018 if (sym) {
1019 if (token_list_different(sym->expansion, expansion) ||
1020 token_list_different(sym->arglist, arglist)) {
1021 if (sym->weak)
1022 goto replace_it;
1023 if (weak)
1024 return 1;
1025 warning(left->pos, "preprocessor token %.*s redefined",
1026 name->len, name->name);
1027 info(sym->pos, "this was the original definition");
1028 sym->expansion = expansion;
1029 sym->arglist = arglist;
1031 return 1;
1033 sym = alloc_symbol(left->pos, SYM_NODE);
1034 bind_symbol(sym, name, NS_MACRO);
1036 replace_it:
1037 sym->expansion = expansion;
1038 sym->arglist = arglist;
1039 sym->weak = weak;
1040 return 1;
1043 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1045 return do_handle_define(stream, line, token, 0);
1048 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1050 return do_handle_define(stream, line, token, 1);
1053 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1055 struct token *left = token->next;
1056 struct symbol **sym;
1058 if (token_type(left) != TOKEN_IDENT) {
1059 warning(token->pos, "expected identifier to 'undef'");
1060 return 0;
1062 if (false_nesting)
1063 return free_preprocessor_line(token);
1065 if (stream->constant == CONSTANT_FILE_MAYBE)
1066 MARK_STREAM_NONCONST(token->pos);
1068 sym = &left->ident->symbols;
1069 while (*sym) {
1070 struct symbol *t = *sym;
1071 if (t->namespace == NS_MACRO) {
1072 *sym = t->next_id;
1073 return 1;
1075 sym = &t->next_id;
1077 return free_preprocessor_line(token);
1080 static int preprocessor_if(struct token *token, int true)
1082 if (if_nesting == 0)
1083 unmatched_if = token;
1084 if (if_nesting >= MAX_NEST)
1085 error_die(token->pos, "Maximum preprocessor conditional level exhausted");
1086 elif_ignore[if_nesting] = (false_nesting || true) ? ELIF_IGNORE : 0;
1087 if (false_nesting || !true) {
1088 false_nesting++;
1089 return free_preprocessor_line(token);
1091 true_nesting++;
1092 return free_preprocessor_line(token);
1095 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1097 return preprocessor_if(token, token_defined(token->next));
1100 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1102 struct token *next = token->next;
1103 if (stream->constant == CONSTANT_FILE_MAYBE) {
1104 if (token_type(next) == TOKEN_IDENT &&
1105 (!stream->protect || stream->protect == next->ident)) {
1106 stream->constant = CONSTANT_FILE_IFNDEF;
1107 stream->protect = next->ident;
1108 } else
1109 MARK_STREAM_NONCONST(token->pos);
1111 return preprocessor_if(token, !token_defined(next));
1115 * Expression handling for #if and #elif; it differs from normal expansion
1116 * due to special treatment of "defined".
1118 static int expression_value(struct token **where)
1120 struct expression *expr;
1121 struct token *p;
1122 struct token **list = where, **beginning = NULL;
1123 long long value;
1124 int state = 0;
1126 while (!eof_token(p = scan_next(list))) {
1127 switch (state) {
1128 case 0:
1129 if (token_type(p) != TOKEN_IDENT)
1130 break;
1131 if (p->ident == &defined_ident) {
1132 state = 1;
1133 beginning = list;
1134 break;
1136 if (!expand_one_symbol(list))
1137 continue;
1138 if (token_type(p) != TOKEN_IDENT)
1139 break;
1140 if (Wundefined_preprocessor)
1141 warning(p->pos, "undefined preprocessor identifier '%s'", show_ident(p->ident));
1142 replace_with_integer(p, 0);
1143 break;
1144 case 1:
1145 if (match_op(p, '(')) {
1146 state = 2;
1147 } else {
1148 state = 0;
1149 replace_with_defined(p);
1150 *beginning = p;
1152 break;
1153 case 2:
1154 if (token_type(p) == TOKEN_IDENT)
1155 state = 3;
1156 else
1157 state = 0;
1158 replace_with_defined(p);
1159 *beginning = p;
1160 break;
1161 case 3:
1162 state = 0;
1163 if (!match_op(p, ')'))
1164 warning(p->pos, "missing ')' after \"defined\"");
1165 *list = p->next;
1166 continue;
1168 list = &p->next;
1171 p = constant_expression(*where, &expr);
1172 if (!eof_token(p))
1173 warning(p->pos, "garbage at end: %s", show_token_sequence(p));
1174 value = get_expression_value(expr);
1175 return value != 0;
1178 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1180 int value = 0;
1181 if (!false_nesting)
1182 value = expression_value(&token->next);
1184 // This is an approximation. We really only need this if the
1185 // condition does depends on a pre-processor symbol. Note, that
1186 // the important #ifndef case has already changed ->constant.
1187 if (stream->constant == CONSTANT_FILE_MAYBE)
1188 MARK_STREAM_NONCONST(token->pos);
1190 return preprocessor_if(token, value);
1193 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1195 if (stream->nesting == if_nesting)
1196 MARK_STREAM_NONCONST(token->pos);
1198 if (stream->nesting > if_nesting)
1199 warning(token->pos, "unmatched #elif within stream");
1201 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1202 warning(token->pos, "#elif after #else");
1204 if (false_nesting) {
1205 /* If this whole if-thing is if'ed out, an elif cannot help */
1206 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1207 return 1;
1208 if (expression_value(&token->next)) {
1209 false_nesting = 0;
1210 true_nesting++;
1211 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1213 } else {
1214 false_nesting = 1;
1215 true_nesting--;
1217 return free_preprocessor_line(token);
1220 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1222 if (stream->nesting == if_nesting)
1223 MARK_STREAM_NONCONST(token->pos);
1225 if (stream->nesting > if_nesting)
1226 warning(token->pos, "unmatched #else within stream");
1228 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1229 warning(token->pos, "#else after #else");
1230 else
1231 elif_ignore[if_nesting-1] |= ELIF_SEEN_ELSE;
1233 if (false_nesting) {
1234 /* If this whole if-thing is if'ed out, an else cannot help */
1235 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1236 return 1;
1237 false_nesting = 0;
1238 true_nesting++;
1239 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1240 } else {
1241 true_nesting--;
1242 false_nesting = 1;
1244 return free_preprocessor_line(token);
1247 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1249 if (stream->constant == CONSTANT_FILE_IFNDEF && stream->nesting == if_nesting)
1250 stream->constant = CONSTANT_FILE_MAYBE;
1252 if (stream->nesting > if_nesting)
1253 warning(token->pos, "unmatched #endif in stream");
1254 if (false_nesting)
1255 false_nesting--;
1256 else
1257 true_nesting--;
1258 return free_preprocessor_line(token);
1261 static const char *show_token_sequence(struct token *token)
1263 static char buffer[1024];
1264 char *ptr = buffer;
1265 int whitespace = 0;
1267 if (!token)
1268 return "<none>";
1269 while (!eof_token(token)) {
1270 const char *val = show_token(token);
1271 int len = strlen(val);
1273 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1274 warning(token->pos, "too long token expansion");
1275 break;
1278 if (whitespace)
1279 *ptr++ = ' ';
1280 memcpy(ptr, val, len);
1281 ptr += len;
1282 token = token->next;
1283 whitespace = token->pos.whitespace;
1285 *ptr = 0;
1286 return buffer;
1289 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1291 if (false_nesting)
1292 return free_preprocessor_line(token);
1293 if (stream->constant == CONSTANT_FILE_MAYBE)
1294 MARK_STREAM_NONCONST(token->pos);
1295 warning(token->pos, "%s", show_token_sequence(token->next));
1296 return free_preprocessor_line(token);
1299 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1301 if (false_nesting)
1302 return free_preprocessor_line(token);
1303 if (stream->constant == CONSTANT_FILE_MAYBE)
1304 MARK_STREAM_NONCONST(token->pos);
1305 warning(token->pos, "%s", show_token_sequence(token->next));
1306 return free_preprocessor_line(token);
1309 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1311 int stdinc;
1313 if (false_nesting)
1314 return free_preprocessor_line(token);
1317 * Do we have any non-system includes?
1318 * Clear them out if so..
1320 stdinc = gcc_includepath - sys_includepath;
1321 if (stdinc) {
1322 const char **src = gcc_includepath;
1323 const char **dst = sys_includepath;
1324 for (;;) {
1325 if (!(*dst = *src))
1326 break;
1327 dst++;
1328 src++;
1330 gcc_includepath -= stdinc;
1332 return free_preprocessor_line(token);
1335 static void add_path_entry(struct token *token, const char *path)
1337 const char **dst;
1338 const char *next;
1340 /* Need one free entry.. */
1341 if (includepath[INCLUDEPATHS-2])
1342 error_die(token->pos, "too many include path entries");
1344 next = path;
1345 dst = sys_includepath;
1346 sys_includepath++;
1347 gcc_includepath++;
1350 * Move them all up starting at "sys_includepath",
1351 * insert the new entry..
1353 for (;;) {
1354 const char *tmp = *dst;
1355 *dst = next;
1356 if (!next)
1357 break;
1358 next = tmp;
1359 dst++;
1363 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1365 for (;;) {
1366 token = token->next;
1367 if (eof_token(token))
1368 return 1;
1369 if (token_type(token) != TOKEN_STRING) {
1370 warning(token->pos, "expected path string");
1371 return 1;
1373 add_path_entry(token, token->string->data);
1378 * We replace "#pragma xxx" with "__pragma__" in the token
1379 * stream. Just as an example.
1381 * We'll just #define that away for now, but the theory here
1382 * is that we can use this to insert arbitrary token sequences
1383 * to turn the pragma's into internal front-end sequences for
1384 * when we actually start caring about them.
1386 * So eventually this will turn into some kind of extended
1387 * __attribute__() like thing, except called __pragma__(xxx).
1389 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1391 struct token *next = *line;
1393 token->ident = &pragma_ident;
1394 token->pos.newline = 1;
1395 token->pos.whitespace = 1;
1396 token->pos.pos = 1;
1397 *line = token;
1398 token->next = next;
1399 return 1;
1403 * We ignore #line for now.
1405 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1407 return 1;
1411 void init_preprocessor(void)
1413 int i;
1414 int stream = init_stream("preprocessor", -1, includepath);
1415 static struct {
1416 const char *name;
1417 int (*handler)(struct stream *, struct token **, struct token *);
1418 } handlers[] = {
1419 { "define", handle_define },
1420 { "weak_define",handle_weak_define },
1421 { "undef", handle_undef },
1422 { "ifdef", handle_ifdef },
1423 { "ifndef", handle_ifndef },
1424 { "else", handle_else },
1425 { "endif", handle_endif },
1426 { "if", handle_if },
1427 { "elif", handle_elif },
1428 { "warning", handle_warning },
1429 { "error", handle_error },
1430 { "include", handle_include },
1431 { "include_next",handle_include_next },
1432 { "pragma", handle_pragma },
1433 { "line", handle_line },
1435 // our internal preprocessor tokens
1436 { "nostdinc", handle_nostdinc },
1437 { "add_include", handle_add_include },
1440 for (i = 0; i < (sizeof (handlers) / sizeof (handlers[0])); i++) {
1441 struct symbol *sym;
1442 sym = create_symbol(stream, handlers[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1443 sym->handler = handlers[i].handler;
1447 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1449 struct token *token = start->next;
1451 if (!token)
1452 return;
1454 if (token_type(token) == TOKEN_NUMBER)
1455 if (handle_line(stream, line, start))
1456 return;
1458 if (token_type(token) == TOKEN_IDENT) {
1459 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1460 if (sym && sym->handler(stream, line, token))
1461 return;
1464 warning(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1467 static void preprocessor_line(struct stream *stream, struct token **line)
1469 struct token *start = *line, *next;
1470 struct token **tp = &start->next;
1472 for (;;) {
1473 next = *tp;
1474 if (next->pos.newline)
1475 break;
1476 tp = &next->next;
1478 *line = next;
1479 *tp = &eof_token_entry;
1480 handle_preprocessor_line(stream, line, start);
1483 static void do_preprocess(struct token **list)
1485 struct token *next;
1487 while (!eof_token(next = scan_next(list))) {
1488 struct stream *stream = input_streams + next->pos.stream;
1490 if (next->pos.newline && match_op(next, '#')) {
1491 if (!next->pos.noexpand) {
1492 preprocessor_line(stream, list);
1493 __free_token(next); /* Free the '#' token */
1494 continue;
1498 switch (token_type(next)) {
1499 case TOKEN_STREAMEND:
1500 if (stream->nesting < if_nesting + 1) {
1501 warning(unmatched_if->pos, "unterminated preprocessor conditional");
1502 // Pretend to see a series of #endifs
1503 MARK_STREAM_NONCONST(next->pos);
1504 do {
1505 handle_endif (stream, NULL, NULL);
1506 } while (stream->nesting < if_nesting + 1);
1508 if (stream->constant == CONSTANT_FILE_MAYBE && stream->protect) {
1509 stream->constant = CONSTANT_FILE_YES;
1511 *list = next->next;
1512 continue;
1513 case TOKEN_STREAMBEGIN:
1514 stream->nesting = if_nesting + 1;
1515 *list = next->next;
1516 continue;
1518 default:
1519 if (false_nesting) {
1520 *list = next->next;
1521 __free_token(next);
1522 continue;
1525 if (token_type(next) != TOKEN_IDENT ||
1526 expand_one_symbol(list))
1527 list = &next->next;
1530 if (stream->constant == CONSTANT_FILE_MAYBE) {
1532 * Any token expansion (even if it ended up being an
1533 * empty expansion) in this stream implies it can't
1534 * be constant.
1536 MARK_STREAM_NONCONST(next->pos);
1541 struct token * preprocess(struct token *token)
1543 preprocessing = 1;
1544 init_preprocessor();
1545 do_preprocess(&token);
1547 // Drop all expressions from pre-processing, they're not used any more.
1548 clear_expression_alloc();
1549 preprocessing = 0;
1551 return token;