Simplify constant unops
[smatch.git] / pre-process.c
blob950aea370b535a7290267b656b1194def557d03f
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 "allocate.h"
25 #include "parse.h"
26 #include "token.h"
27 #include "symbol.h"
28 #include "expression.h"
30 static int true_nesting = 0;
31 static int false_nesting = 0;
32 static struct token *unmatched_if = NULL;
33 #define if_nesting (true_nesting + false_nesting)
35 #define MAX_NEST (256)
36 static unsigned char elif_ignore[MAX_NEST];
37 enum { ELIF_IGNORE = 1, ELIF_SEEN_ELSE = 2 };
39 #define INCLUDEPATHS 300
40 const char *includepath[INCLUDEPATHS+1] = {
41 "/usr/include",
42 "/usr/local/include",
43 GCC_INTERNAL_INCLUDE,
44 NULL
47 static const char **sys_includepath = includepath + 0;
48 static const char **gcc_includepath = includepath + 2;
50 #define MARK_STREAM_NONCONST(pos) do { \
51 if (stream->constant != CONSTANT_FILE_NOPE) { \
52 if (0) \
53 info(pos, "%s triggers non-const", __func__); \
54 stream->constant = CONSTANT_FILE_NOPE; \
55 } \
56 } while (0)
59 static struct token *alloc_token(struct position *pos)
61 struct token *token = __alloc_token(0);
63 token->pos.stream = pos->stream;
64 token->pos.line = pos->line;
65 token->pos.pos = pos->pos;
66 token->pos.whitespace = 1;
67 return token;
70 static const char *show_token_sequence(struct token *token);
72 /* Expand symbol 'sym' at '*list' */
73 static int expand(struct token **, struct symbol *);
75 static void replace_with_string(struct token *token, const char *str)
77 int size = strlen(str) + 1;
78 struct string *s = __alloc_string(size);
80 s->length = size;
81 memcpy(s->data, str, size);
82 token_type(token) = TOKEN_STRING;
83 token->string = s;
86 static void replace_with_integer(struct token *token, unsigned int val)
88 char *buf = __alloc_bytes(11);
89 sprintf(buf, "%u", val);
90 token_type(token) = TOKEN_NUMBER;
91 token->number = buf;
94 static int token_defined(struct token *token)
96 if (token_type(token) == TOKEN_IDENT) {
97 struct symbol *sym = lookup_symbol(token->ident, NS_MACRO);
98 if (sym) {
99 sym->weak = 0;
100 return 1;
102 return 0;
105 warning(token->pos, "expected preprocessor identifier");
106 return 0;
109 static void replace_with_defined(struct token *token)
111 static const char *string[] = { "0", "1" };
112 int defined = token_defined(token);
114 token_type(token) = TOKEN_NUMBER;
115 token->number = string[defined];
118 static int expand_one_symbol(struct token **list)
120 struct token *token = *list;
121 struct symbol *sym;
123 if (token->pos.noexpand)
124 return 1;
126 sym = lookup_symbol(token->ident, NS_MACRO);
127 if (sym) {
128 sym->weak = 0;
129 return expand(list, sym);
131 if (token->ident == &__LINE___ident) {
132 replace_with_integer(token, token->pos.line);
133 } else if (token->ident == &__FILE___ident) {
134 replace_with_string(token, (input_streams + token->pos.stream)->name);
136 return 1;
139 static inline struct token *scan_next(struct token **where)
141 struct token *token = *where;
142 if (token_type(token) != TOKEN_UNTAINT)
143 return token;
144 do {
145 token->ident->tainted = 0;
146 token = token->next;
147 } while (token_type(token) == TOKEN_UNTAINT);
148 *where = token;
149 return token;
152 static void expand_list(struct token **list)
154 struct token *next;
155 while (!eof_token(next = scan_next(list))) {
156 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
157 list = &next->next;
161 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
163 struct token **p = &prev->next;
164 struct token *next;
165 int nesting = 0;
167 while (!eof_token(next = scan_next(p))) {
168 if (match_op(next, '(')) {
169 nesting++;
170 } else if (match_op(next, ')')) {
171 if (!nesting--)
172 break;
173 } else if (match_op(next, ',') && !nesting && !vararg) {
174 break;
176 next->pos.stream = pos->stream;
177 next->pos.line = pos->line;
178 next->pos.pos = pos->pos;
179 p = &next->next;
181 *p = &eof_token_entry;
182 return next;
186 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
189 struct arg {
190 struct token *arg;
191 struct token *expanded;
192 struct token *str;
193 int n_normal;
194 int n_quoted;
195 int n_str;
198 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
200 int wanted = arglist->count.normal;
201 struct token *next = NULL;
202 int count = 0;
204 arglist = arglist->next; /* skip counter */
206 if (!wanted) {
207 next = collect_arg(start, 0, &what->pos);
208 if (eof_token(next))
209 goto Eclosing;
210 if (!eof_token(start->next) || !match_op(next, ')')) {
211 count++;
212 goto Emany;
214 } else {
215 for (count = 0; count < wanted; count++) {
216 struct argcount *p = &arglist->next->count;
217 next = collect_arg(start, p->vararg, &what->pos);
218 arglist = arglist->next->next;
219 if (eof_token(next))
220 goto Eclosing;
221 args[count].arg = start->next;
222 args[count].n_normal = p->normal;
223 args[count].n_quoted = p->quoted;
224 args[count].n_str = p->str;
225 if (match_op(next, ')')) {
226 count++;
227 break;
229 start = next;
231 if (count == wanted && !match_op(next, ')'))
232 goto Emany;
233 if (count == wanted - 1) {
234 struct argcount *p = &arglist->next->count;
235 if (!p->vararg)
236 goto Efew;
237 args[count].arg = NULL;
238 args[count].n_normal = p->normal;
239 args[count].n_quoted = p->quoted;
240 args[count].n_str = p->str;
242 if (count < wanted - 1)
243 goto Efew;
245 what->next = next->next;
246 return 1;
248 Efew:
249 warning(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
250 show_token(what), wanted, count);
251 goto out;
252 Emany:
253 while (match_op(next, ',')) {
254 next = collect_arg(next, 0, &what->pos);
255 count++;
257 if (eof_token(next))
258 goto Eclosing;
259 warning(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
260 show_token(what), count, wanted);
261 goto out;
262 Eclosing:
263 warning(what->pos, "unterminated argument list invoking macro \"%s\"",
264 show_token(what));
265 out:
266 what->next = next->next;
267 return 0;
270 static struct token *dup_list(struct token *list)
272 struct token *res;
273 struct token **p = &res;
275 while (!eof_token(list)) {
276 struct token *newtok = __alloc_token(0);
277 *newtok = *list;
278 *p = newtok;
279 p = &newtok->next;
280 list = list->next;
282 return res;
285 static struct token *stringify(struct token *arg)
287 const char *s = show_token_sequence(arg);
288 int size = strlen(s)+1;
289 struct token *token = __alloc_token(0);
290 struct string *string = __alloc_string(size);
292 memcpy(string->data, s, size);
293 string->length = size;
294 token->pos = arg->pos;
295 token_type(token) = TOKEN_STRING;
296 token->string = string;
297 token->next = &eof_token_entry;
298 return token;
301 static void expand_arguments(int count, struct arg *args)
303 int i;
304 for (i = 0; i < count; i++) {
305 struct token *arg = args[i].arg;
306 if (!arg)
307 arg = &eof_token_entry;
308 if (args[i].n_str)
309 args[i].str = stringify(arg);
310 if (args[i].n_normal) {
311 if (!args[i].n_quoted) {
312 args[i].expanded = arg;
313 args[i].arg = NULL;
314 } else if (eof_token(arg)) {
315 args[i].expanded = arg;
316 } else {
317 args[i].expanded = dup_list(arg);
319 expand_list(&args[i].expanded);
325 * Possibly valid combinations:
326 * - ident + ident -> ident
327 * - ident + number -> ident unless number contains '.', '+' or '-'.
328 * - number + number -> number
329 * - number + ident -> number
330 * - number + '.' -> number
331 * - number + '+' or '-' -> number, if number used to end on [eEpP].
332 * - '.' + number -> number, if number used to start with a digit.
333 * - special + special -> either special or an error.
335 static enum token_type combine(struct token *left, struct token *right, char *p)
337 int len;
338 enum token_type t1 = token_type(left), t2 = token_type(right);
340 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
341 return TOKEN_ERROR;
343 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
344 return TOKEN_ERROR;
346 strcpy(p, show_token(left));
347 strcat(p, show_token(right));
348 len = strlen(p);
350 if (len >= 256)
351 return TOKEN_ERROR;
353 if (t1 == TOKEN_IDENT) {
354 if (t2 == TOKEN_SPECIAL)
355 return TOKEN_ERROR;
356 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
357 return TOKEN_ERROR;
358 return TOKEN_IDENT;
361 if (t1 == TOKEN_NUMBER) {
362 if (t2 == TOKEN_SPECIAL) {
363 switch (right->special) {
364 case '.':
365 break;
366 case '+': case '-':
367 if (strchr("eEpP", p[len - 2]))
368 break;
369 default:
370 return TOKEN_ERROR;
373 return TOKEN_NUMBER;
376 if (p[0] == '.' && isdigit((unsigned char)p[1]))
377 return TOKEN_NUMBER;
379 return TOKEN_SPECIAL;
382 static int merge(struct token *left, struct token *right)
384 extern unsigned char combinations[][3];
385 static char buffer[512];
386 int n;
388 switch (combine(left, right, buffer)) {
389 case TOKEN_IDENT:
390 left->ident = built_in_ident(buffer);
391 left->pos.noexpand = 0;
392 return 1;
394 case TOKEN_NUMBER: {
395 char *number = __alloc_bytes(strlen(buffer) + 1);
396 memcpy(number, buffer, strlen(buffer) + 1);
397 token_type(left) = TOKEN_NUMBER; /* could be . + num */
398 left->number = number;
399 return 1;
402 case TOKEN_SPECIAL:
403 if (buffer[2] && buffer[3])
404 break;
405 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
406 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
407 left->special = n;
408 return 1;
411 default:
414 warning(left->pos, "'##' failed: concatenation is not a valid token");
415 return 0;
418 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
420 struct token *alloc = alloc_token(streampos);
421 token_type(alloc) = token_type(token);
422 alloc->pos.newline = pos->newline;
423 alloc->pos.whitespace = pos->whitespace;
424 alloc->number = token->number;
425 alloc->pos.noexpand = token->pos.noexpand;
426 return alloc;
429 static struct token **copy(struct token **where, struct token *list, int *count)
431 int need_copy = --*count;
432 while (!eof_token(list)) {
433 struct token *token;
434 if (need_copy)
435 token = dup_token(list, &list->pos, &list->pos);
436 else
437 token = list;
438 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
439 token->pos.noexpand = 1;
440 *where = token;
441 where = &token->next;
442 list = list->next;
444 *where = &eof_token_entry;
445 return where;
448 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
450 struct token *token = *list;
451 struct position *base_pos = &token->pos;
452 struct position *pos = base_pos;
453 int *count;
454 enum {Normal, Placeholder, Concat} state = Normal;
456 for (; !eof_token(body); body = body->next, pos = &body->pos) {
457 struct token *added, *arg;
458 struct token **tail;
460 switch (token_type(body)) {
461 case TOKEN_GNU_KLUDGE:
463 * GNU kludge: if we had <comma>##<vararg>, behaviour
464 * depends on whether we had enough arguments to have
465 * a vararg. If we did, ## is just ignored. Otherwise
466 * both , and ## are ignored. Comma should come from
467 * the body of macro and not be an argument of earlier
468 * concatenation.
470 if (!args[body->next->argnum].arg)
471 continue;
472 added = dup_token(body, base_pos, pos);
473 token_type(added) = TOKEN_SPECIAL;
474 tail = &added->next;
475 break;
477 case TOKEN_STR_ARGUMENT:
478 arg = args[body->argnum].str;
479 count = &args[body->argnum].n_str;
480 goto copy_arg;
482 case TOKEN_QUOTED_ARGUMENT:
483 arg = args[body->argnum].arg;
484 count = &args[body->argnum].n_quoted;
485 if (!arg || eof_token(arg)) {
486 if (state == Concat)
487 state = Normal;
488 else
489 state = Placeholder;
490 continue;
492 goto copy_arg;
494 case TOKEN_MACRO_ARGUMENT:
495 arg = args[body->argnum].expanded;
496 count = &args[body->argnum].n_normal;
497 if (eof_token(arg)) {
498 state = Normal;
499 continue;
501 copy_arg:
502 tail = copy(&added, arg, count);
503 added->pos.newline = pos->newline;
504 added->pos.whitespace = pos->whitespace;
505 break;
507 case TOKEN_CONCAT:
508 if (state == Placeholder)
509 state = Normal;
510 else
511 state = Concat;
512 continue;
514 case TOKEN_IDENT:
515 added = dup_token(body, base_pos, pos);
516 if (added->ident->tainted)
517 added->pos.noexpand = 1;
518 tail = &added->next;
519 break;
521 default:
522 added = dup_token(body, base_pos, pos);
523 tail = &added->next;
524 break;
528 * if we got to doing real concatenation, we already have
529 * added something into the list, so containing_token() is OK.
531 if (state == Concat && merge(containing_token(list), added)) {
532 *list = added->next;
533 if (tail != &added->next)
534 list = tail;
535 } else {
536 *list = added;
537 list = tail;
539 state = Normal;
541 *list = &eof_token_entry;
542 return list;
545 static int expand(struct token **list, struct symbol *sym)
547 struct token *last;
548 struct token *token = *list;
549 struct ident *expanding = token->ident;
550 struct token **tail;
551 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
552 struct arg args[nargs];
554 if (expanding->tainted) {
555 token->pos.noexpand = 1;
556 return 1;
559 if (sym->arglist) {
560 if (!match_op(scan_next(&token->next), '('))
561 return 1;
562 if (!collect_arguments(token->next, sym->arglist, args, token))
563 return 1;
564 expand_arguments(nargs, args);
567 expanding->tainted = 1;
569 last = token->next;
570 tail = substitute(list, sym->expansion, args);
571 *tail = last;
573 return 0;
576 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
578 struct token *last;
579 static char buffer[256];
580 char *ptr = buffer;
582 last = token;
583 while (!eof_token(token) && !match_op(token, endop)) {
584 int len;
585 const char *val = token->string->data;
586 if (token_type(token) != TOKEN_STRING)
587 val = show_token(token);
588 len = strlen(val);
589 memcpy(ptr, val, len);
590 ptr += len;
591 token = token->next;
593 *ptr = 0;
594 if (endop && !match_op(token, endop))
595 warning(start->pos, "expected '>' at end of filename");
596 return buffer;
599 static int try_include(const char *path, int plen, const char *filename, int flen, struct token **where, const char **next_path)
601 int fd;
602 static char fullname[PATH_MAX];
604 memcpy(fullname, path, plen);
605 if (plen && path[plen-1] != '/') {
606 fullname[plen] = '/';
607 plen++;
609 memcpy(fullname+plen, filename, flen);
610 fd = open(fullname, O_RDONLY);
611 if (fd >= 0) {
612 char * streamname = __alloc_bytes(plen + flen);
613 memcpy(streamname, fullname, plen + flen);
614 *where = tokenize(streamname, fd, *where, next_path);
615 close(fd);
616 return 1;
618 return 0;
621 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
623 const char *path;
625 while ((path = *pptr++) != NULL) {
626 if (!try_include(path, strlen(path), filename, flen, list, pptr))
627 continue;
628 return 1;
630 return 0;
634 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
636 int flen = strlen(filename) + 1;
638 /* Absolute path? */
639 if (filename[0] == '/') {
640 if (try_include("", 0, filename, flen, list, includepath))
641 return;
642 goto out;
645 /* Same directory as current stream? */
646 if (local) {
647 const char *path;
648 char *slash;
649 int plen;
651 path = stream->name;
652 slash = strrchr(path, '/');
653 plen = slash ? slash - path : 0;
655 if (try_include(path, plen, filename, flen, list, includepath))
656 return;
659 /* Check the standard include paths.. */
660 if (do_include_path(path, list, token, filename, flen))
661 return;
662 out:
663 error_die(token->pos, "unable to open '%s'", filename);
666 static int free_preprocessor_line(struct token *token)
668 do {
669 struct token *free = token;
670 token = token->next;
671 __free_token(free);
672 } while (token_type(token) != TOKEN_EOF);
673 return 1;
676 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
678 const char *filename;
679 struct token *next;
680 int expect;
682 if (false_nesting)
683 return free_preprocessor_line(token);
685 if (stream->constant == CONSTANT_FILE_MAYBE)
686 MARK_STREAM_NONCONST(token->pos);
688 next = token->next;
689 expect = '>';
690 if (!match_op(next, '<')) {
691 expand_list(&token->next);
692 expect = 0;
693 next = token;
694 if (match_op(token->next, '<')) {
695 next = token->next;
696 expect = '>';
699 token = next->next;
700 filename = token_name_sequence(token, expect, token);
701 do_include(!expect, stream, list, token, filename, path);
702 return 1;
705 static int handle_include(struct stream *stream, struct token **list, struct token *token)
707 return handle_include_path(stream, list, token, includepath);
710 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
712 return handle_include_path(stream, list, token, stream->next_path);
715 static int token_different(struct token *t1, struct token *t2)
717 int different;
719 if (token_type(t1) != token_type(t2))
720 return 1;
722 switch (token_type(t1)) {
723 case TOKEN_IDENT:
724 different = t1->ident != t2->ident;
725 break;
726 case TOKEN_ARG_COUNT:
727 case TOKEN_UNTAINT:
728 case TOKEN_CONCAT:
729 case TOKEN_GNU_KLUDGE:
730 different = 0;
731 break;
732 case TOKEN_NUMBER:
733 different = strcmp(t1->number, t2->number);
734 break;
735 case TOKEN_SPECIAL:
736 different = t1->special != t2->special;
737 break;
738 case TOKEN_MACRO_ARGUMENT:
739 case TOKEN_QUOTED_ARGUMENT:
740 case TOKEN_STR_ARGUMENT:
741 different = t1->argnum != t2->argnum;
742 break;
743 case TOKEN_CHAR:
744 different = t1->character != t2->character;
745 break;
746 case TOKEN_STRING: {
747 struct string *s1, *s2;
749 s1 = t1->string;
750 s2 = t2->string;
751 different = 1;
752 if (s1->length != s2->length)
753 break;
754 different = memcmp(s1->data, s2->data, s1->length);
755 break;
757 default:
758 different = 1;
759 break;
761 return different;
764 static int token_list_different(struct token *list1, struct token *list2)
766 for (;;) {
767 if (list1 == list2)
768 return 0;
769 if (!list1 || !list2)
770 return 1;
771 if (token_different(list1, list2))
772 return 1;
773 list1 = list1->next;
774 list2 = list2->next;
778 static inline void set_arg_count(struct token *token)
780 token_type(token) = TOKEN_ARG_COUNT;
781 token->count.normal = token->count.quoted =
782 token->count.str = token->count.vararg = 0;
785 static struct token *parse_arguments(struct token *list)
787 struct token *arg = list->next, *next = list;
788 struct argcount *count = &list->count;
790 set_arg_count(list);
792 if (match_op(arg, ')')) {
793 next = arg->next;
794 list->next = &eof_token_entry;
795 return next;
798 while (token_type(arg) == TOKEN_IDENT) {
799 if (arg->ident == &__VA_ARGS___ident)
800 goto Eva_args;
801 if (!++count->normal)
802 goto Eargs;
803 next = arg->next;
805 if (match_op(next, ',')) {
806 set_arg_count(next);
807 arg = next->next;
808 continue;
811 if (match_op(next, ')')) {
812 set_arg_count(next);
813 next = next->next;
814 arg->next->next = &eof_token_entry;
815 return next;
818 /* normal cases are finished here */
820 if (match_op(next, SPECIAL_ELLIPSIS)) {
821 if (match_op(next->next, ')')) {
822 set_arg_count(next);
823 next->count.vararg = 1;
824 next = next->next;
825 arg->next->next = &eof_token_entry;
826 return next->next;
829 arg = next;
830 goto Enotclosed;
833 if (eof_token(next)) {
834 goto Enotclosed;
835 } else {
836 arg = next;
837 goto Ebadstuff;
841 if (match_op(arg, SPECIAL_ELLIPSIS)) {
842 next = arg->next;
843 token_type(arg) = TOKEN_IDENT;
844 arg->ident = &__VA_ARGS___ident;
845 if (!match_op(next, ')'))
846 goto Enotclosed;
847 if (!++count->normal)
848 goto Eargs;
849 set_arg_count(next);
850 next->count.vararg = 1;
851 next = next->next;
852 arg->next->next = &eof_token_entry;
853 return next;
856 if (eof_token(arg)) {
857 arg = next;
858 goto Enotclosed;
860 if (match_op(arg, ','))
861 goto Emissing;
862 else
863 goto Ebadstuff;
866 Emissing:
867 warning(arg->pos, "parameter name missing");
868 return NULL;
869 Ebadstuff:
870 warning(arg->pos, "\"%s\" may not appear in macro parameter list",
871 show_token(arg));
872 return NULL;
873 Enotclosed:
874 warning(arg->pos, "missing ')' in macro parameter list");
875 return NULL;
876 Eva_args:
877 warning(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
878 return NULL;
879 Eargs:
880 warning(arg->pos, "too many arguments in macro definition");
881 return NULL;
884 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
886 struct ident *ident = token->ident;
887 int nr;
889 if (!arglist || token_type(token) != TOKEN_IDENT)
890 return 0;
892 arglist = arglist->next;
894 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
895 if (arglist->ident == ident) {
896 struct argcount *count = &arglist->next->count;
897 int n;
899 token->argnum = nr;
900 token_type(token) = type;
901 switch (type) {
902 case TOKEN_MACRO_ARGUMENT:
903 n = ++count->normal;
904 break;
905 case TOKEN_QUOTED_ARGUMENT:
906 n = ++count->quoted;
907 break;
908 default:
909 n = ++count->str;
911 if (n)
912 return count->vararg ? 2 : 1;
913 token_type(token) = TOKEN_ERROR;
914 return -1;
917 return 0;
920 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
922 struct token *token = expansion;
923 struct token **p;
924 struct token *last = NULL;
926 if (match_op(token, SPECIAL_HASHHASH))
927 goto Econcat;
929 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
930 if (match_op(token, '#')) {
931 if (arglist) {
932 struct token *next = token->next;
933 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
934 goto Equote;
935 next->pos.whitespace = token->pos.whitespace;
936 token = *p = next;
937 } else {
938 token->pos.noexpand = 1;
940 } else if (match_op(token, SPECIAL_HASHHASH)) {
941 struct token *next = token->next;
942 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
943 token_type(token) = TOKEN_CONCAT;
944 if (arg) {
945 token = next;
946 /* GNU kludge */
947 if (arg == 2 && last && match_op(last, ',')) {
948 token_type(last) = TOKEN_GNU_KLUDGE;
949 last->next = token;
951 } else if (match_op(next, SPECIAL_HASHHASH))
952 token = next;
953 else if (match_op(next, ','))
954 token = next;
955 else if (eof_token(next))
956 goto Econcat;
957 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
958 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
959 } else {
960 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
962 if (token_type(token) == TOKEN_ERROR)
963 goto Earg;
964 last = token;
966 token = alloc_token(&expansion->pos);
967 token_type(token) = TOKEN_UNTAINT;
968 token->ident = name;
969 token->next = *p;
970 *p = token;
971 return expansion;
973 Equote:
974 warning(token->pos, "'#' is not followed by a macro parameter");
975 return NULL;
977 Econcat:
978 warning(token->pos, "'##' cannot appear at the ends of macro expansion");
979 return NULL;
980 Earg:
981 warning(token->pos, "too many instances of argument in body");
982 return NULL;
985 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int weak)
987 struct token *arglist, *expansion;
988 struct token *left = token->next;
989 struct symbol *sym;
990 struct ident *name;
992 if (token_type(left) != TOKEN_IDENT) {
993 warning(token->pos, "expected identifier to 'define'");
994 return 0;
996 if (false_nesting)
997 return free_preprocessor_line(token);
999 if (stream->constant == CONSTANT_FILE_MAYBE)
1000 MARK_STREAM_NONCONST(token->pos);
1002 __free_token(token); /* Free the "define" token, but not the rest of the line */
1003 name = left->ident;
1005 arglist = NULL;
1006 expansion = left->next;
1007 if (!expansion->pos.whitespace && match_op(expansion, '(')) {
1008 arglist = expansion;
1009 expansion = parse_arguments(expansion);
1010 if (!expansion)
1011 return 1;
1014 expansion = parse_expansion(expansion, arglist, name);
1015 if (!expansion)
1016 return 1;
1018 sym = lookup_symbol(name, NS_MACRO);
1019 if (sym) {
1020 if (token_list_different(sym->expansion, expansion) ||
1021 token_list_different(sym->arglist, arglist)) {
1022 if (sym->weak)
1023 goto replace_it;
1024 if (weak)
1025 return 1;
1026 warning(left->pos, "preprocessor token %.*s redefined",
1027 name->len, name->name);
1028 info(sym->pos, "this was the original definition");
1029 sym->expansion = expansion;
1030 sym->arglist = arglist;
1032 return 1;
1034 sym = alloc_symbol(left->pos, SYM_NODE);
1035 bind_symbol(sym, name, NS_MACRO);
1037 replace_it:
1038 sym->expansion = expansion;
1039 sym->arglist = arglist;
1040 sym->weak = weak;
1041 return 1;
1044 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1046 return do_handle_define(stream, line, token, 0);
1049 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1051 return do_handle_define(stream, line, token, 1);
1054 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1056 struct token *left = token->next;
1057 struct symbol **sym;
1059 if (token_type(left) != TOKEN_IDENT) {
1060 warning(token->pos, "expected identifier to 'undef'");
1061 return 0;
1063 if (false_nesting)
1064 return free_preprocessor_line(token);
1066 if (stream->constant == CONSTANT_FILE_MAYBE)
1067 MARK_STREAM_NONCONST(token->pos);
1069 sym = &left->ident->symbols;
1070 while (*sym) {
1071 struct symbol *t = *sym;
1072 if (t->namespace == NS_MACRO) {
1073 *sym = t->next_id;
1074 return 1;
1076 sym = &t->next_id;
1078 return free_preprocessor_line(token);
1081 static int preprocessor_if(struct token *token, int true)
1083 if (if_nesting == 0)
1084 unmatched_if = token;
1085 if (if_nesting >= MAX_NEST)
1086 error_die(token->pos, "Maximum preprocessor conditional level exhausted");
1087 elif_ignore[if_nesting] = (false_nesting || true) ? ELIF_IGNORE : 0;
1088 if (false_nesting || !true) {
1089 false_nesting++;
1090 return free_preprocessor_line(token);
1092 true_nesting++;
1093 return free_preprocessor_line(token);
1096 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1098 return preprocessor_if(token, token_defined(token->next));
1101 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1103 struct token *next = token->next;
1104 if (stream->constant == CONSTANT_FILE_MAYBE) {
1105 if (token_type(next) == TOKEN_IDENT &&
1106 (!stream->protect || stream->protect == next->ident)) {
1107 stream->constant = CONSTANT_FILE_IFNDEF;
1108 stream->protect = next->ident;
1109 } else
1110 MARK_STREAM_NONCONST(token->pos);
1112 return preprocessor_if(token, !token_defined(next));
1116 * Expression handling for #if and #elif; it differs from normal expansion
1117 * due to special treatment of "defined".
1119 static int expression_value(struct token **where)
1121 struct expression *expr;
1122 struct token *p;
1123 struct token **list = where, **beginning = NULL;
1124 long long value;
1125 int state = 0;
1127 while (!eof_token(p = scan_next(list))) {
1128 switch (state) {
1129 case 0:
1130 if (token_type(p) != TOKEN_IDENT)
1131 break;
1132 if (p->ident == &defined_ident) {
1133 state = 1;
1134 beginning = list;
1135 break;
1137 if (!expand_one_symbol(list))
1138 continue;
1139 if (token_type(p) != TOKEN_IDENT)
1140 break;
1141 if (Wundefined_preprocessor)
1142 warning(p->pos, "undefined preprocessor identifier '%s'", show_ident(p->ident));
1143 replace_with_integer(p, 0);
1144 break;
1145 case 1:
1146 if (match_op(p, '(')) {
1147 state = 2;
1148 } else {
1149 state = 0;
1150 replace_with_defined(p);
1151 *beginning = p;
1153 break;
1154 case 2:
1155 if (token_type(p) == TOKEN_IDENT)
1156 state = 3;
1157 else
1158 state = 0;
1159 replace_with_defined(p);
1160 *beginning = p;
1161 break;
1162 case 3:
1163 state = 0;
1164 if (!match_op(p, ')'))
1165 warning(p->pos, "missing ')' after \"defined\"");
1166 *list = p->next;
1167 continue;
1169 list = &p->next;
1172 p = constant_expression(*where, &expr);
1173 if (!eof_token(p))
1174 warning(p->pos, "garbage at end: %s", show_token_sequence(p));
1175 value = get_expression_value(expr);
1176 return value != 0;
1179 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1181 int value = 0;
1182 if (!false_nesting)
1183 value = expression_value(&token->next);
1185 // This is an approximation. We really only need this if the
1186 // condition does depends on a pre-processor symbol. Note, that
1187 // the important #ifndef case has already changed ->constant.
1188 if (stream->constant == CONSTANT_FILE_MAYBE)
1189 MARK_STREAM_NONCONST(token->pos);
1191 return preprocessor_if(token, value);
1194 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1196 if (stream->nesting == if_nesting)
1197 MARK_STREAM_NONCONST(token->pos);
1199 if (stream->nesting > if_nesting)
1200 warning(token->pos, "unmatched #elif within stream");
1202 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1203 warning(token->pos, "#elif after #else");
1205 if (false_nesting) {
1206 /* If this whole if-thing is if'ed out, an elif cannot help */
1207 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1208 return 1;
1209 if (expression_value(&token->next)) {
1210 false_nesting = 0;
1211 true_nesting++;
1212 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1214 } else {
1215 false_nesting = 1;
1216 true_nesting--;
1218 return free_preprocessor_line(token);
1221 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1223 if (stream->nesting == if_nesting)
1224 MARK_STREAM_NONCONST(token->pos);
1226 if (stream->nesting > if_nesting)
1227 warning(token->pos, "unmatched #else within stream");
1229 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1230 warning(token->pos, "#else after #else");
1231 else
1232 elif_ignore[if_nesting-1] |= ELIF_SEEN_ELSE;
1234 if (false_nesting) {
1235 /* If this whole if-thing is if'ed out, an else cannot help */
1236 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1237 return 1;
1238 false_nesting = 0;
1239 true_nesting++;
1240 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1241 } else {
1242 true_nesting--;
1243 false_nesting = 1;
1245 return free_preprocessor_line(token);
1248 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1250 if (stream->constant == CONSTANT_FILE_IFNDEF && stream->nesting == if_nesting)
1251 stream->constant = CONSTANT_FILE_MAYBE;
1253 if (stream->nesting > if_nesting)
1254 warning(token->pos, "unmatched #endif in stream");
1255 if (false_nesting)
1256 false_nesting--;
1257 else
1258 true_nesting--;
1259 return free_preprocessor_line(token);
1262 static const char *show_token_sequence(struct token *token)
1264 static char buffer[1024];
1265 char *ptr = buffer;
1266 int whitespace = 0;
1268 if (!token)
1269 return "<none>";
1270 while (!eof_token(token)) {
1271 const char *val = show_token(token);
1272 int len = strlen(val);
1274 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1275 warning(token->pos, "too long token expansion");
1276 break;
1279 if (whitespace)
1280 *ptr++ = ' ';
1281 memcpy(ptr, val, len);
1282 ptr += len;
1283 token = token->next;
1284 whitespace = token->pos.whitespace;
1286 *ptr = 0;
1287 return buffer;
1290 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1292 if (false_nesting)
1293 return free_preprocessor_line(token);
1294 if (stream->constant == CONSTANT_FILE_MAYBE)
1295 MARK_STREAM_NONCONST(token->pos);
1296 warning(token->pos, "%s", show_token_sequence(token->next));
1297 return free_preprocessor_line(token);
1300 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1302 if (false_nesting)
1303 return free_preprocessor_line(token);
1304 if (stream->constant == CONSTANT_FILE_MAYBE)
1305 MARK_STREAM_NONCONST(token->pos);
1306 warning(token->pos, "%s", show_token_sequence(token->next));
1307 return free_preprocessor_line(token);
1310 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1312 int stdinc;
1314 if (false_nesting)
1315 return free_preprocessor_line(token);
1318 * Do we have any non-system includes?
1319 * Clear them out if so..
1321 stdinc = gcc_includepath - sys_includepath;
1322 if (stdinc) {
1323 const char **src = gcc_includepath;
1324 const char **dst = sys_includepath;
1325 for (;;) {
1326 if (!(*dst = *src))
1327 break;
1328 dst++;
1329 src++;
1331 gcc_includepath -= stdinc;
1333 return free_preprocessor_line(token);
1336 static void add_path_entry(struct token *token, const char *path)
1338 const char **dst;
1339 const char *next;
1341 /* Need one free entry.. */
1342 if (includepath[INCLUDEPATHS-2])
1343 error_die(token->pos, "too many include path entries");
1345 next = path;
1346 dst = sys_includepath;
1347 sys_includepath++;
1348 gcc_includepath++;
1351 * Move them all up starting at "sys_includepath",
1352 * insert the new entry..
1354 for (;;) {
1355 const char *tmp = *dst;
1356 *dst = next;
1357 if (!next)
1358 break;
1359 next = tmp;
1360 dst++;
1364 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1366 for (;;) {
1367 token = token->next;
1368 if (eof_token(token))
1369 return 1;
1370 if (token_type(token) != TOKEN_STRING) {
1371 warning(token->pos, "expected path string");
1372 return 1;
1374 add_path_entry(token, token->string->data);
1379 * We replace "#pragma xxx" with "__pragma__" in the token
1380 * stream. Just as an example.
1382 * We'll just #define that away for now, but the theory here
1383 * is that we can use this to insert arbitrary token sequences
1384 * to turn the pragma's into internal front-end sequences for
1385 * when we actually start caring about them.
1387 * So eventually this will turn into some kind of extended
1388 * __attribute__() like thing, except called __pragma__(xxx).
1390 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1392 struct token *next = *line;
1394 token->ident = &pragma_ident;
1395 token->pos.newline = 1;
1396 token->pos.whitespace = 1;
1397 token->pos.pos = 1;
1398 *line = token;
1399 token->next = next;
1400 return 1;
1404 * We ignore #line for now.
1406 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1408 return 1;
1412 void init_preprocessor(void)
1414 int i;
1415 int stream = init_stream("preprocessor", -1, includepath);
1416 static struct {
1417 const char *name;
1418 int (*handler)(struct stream *, struct token **, struct token *);
1419 } handlers[] = {
1420 { "define", handle_define },
1421 { "weak_define",handle_weak_define },
1422 { "undef", handle_undef },
1423 { "ifdef", handle_ifdef },
1424 { "ifndef", handle_ifndef },
1425 { "else", handle_else },
1426 { "endif", handle_endif },
1427 { "if", handle_if },
1428 { "elif", handle_elif },
1429 { "warning", handle_warning },
1430 { "error", handle_error },
1431 { "include", handle_include },
1432 { "include_next",handle_include_next },
1433 { "pragma", handle_pragma },
1434 { "line", handle_line },
1436 // our internal preprocessor tokens
1437 { "nostdinc", handle_nostdinc },
1438 { "add_include", handle_add_include },
1441 for (i = 0; i < (sizeof (handlers) / sizeof (handlers[0])); i++) {
1442 struct symbol *sym;
1443 sym = create_symbol(stream, handlers[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1444 sym->handler = handlers[i].handler;
1448 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1450 struct token *token = start->next;
1452 if (!token)
1453 return;
1455 if (token_type(token) == TOKEN_NUMBER)
1456 if (handle_line(stream, line, start))
1457 return;
1459 if (token_type(token) == TOKEN_IDENT) {
1460 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1461 if (sym && sym->handler(stream, line, token))
1462 return;
1465 warning(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1468 static void preprocessor_line(struct stream *stream, struct token **line)
1470 struct token *start = *line, *next;
1471 struct token **tp = &start->next;
1473 for (;;) {
1474 next = *tp;
1475 if (next->pos.newline)
1476 break;
1477 tp = &next->next;
1479 *line = next;
1480 *tp = &eof_token_entry;
1481 handle_preprocessor_line(stream, line, start);
1484 static void do_preprocess(struct token **list)
1486 struct token *next;
1488 while (!eof_token(next = scan_next(list))) {
1489 struct stream *stream = input_streams + next->pos.stream;
1491 if (next->pos.newline && match_op(next, '#')) {
1492 if (!next->pos.noexpand) {
1493 preprocessor_line(stream, list);
1494 __free_token(next); /* Free the '#' token */
1495 continue;
1499 switch (token_type(next)) {
1500 case TOKEN_STREAMEND:
1501 if (stream->nesting < if_nesting + 1) {
1502 warning(unmatched_if->pos, "unterminated preprocessor conditional");
1503 // Pretend to see a series of #endifs
1504 MARK_STREAM_NONCONST(next->pos);
1505 do {
1506 handle_endif (stream, NULL, NULL);
1507 } while (stream->nesting < if_nesting + 1);
1509 if (stream->constant == CONSTANT_FILE_MAYBE && stream->protect) {
1510 stream->constant = CONSTANT_FILE_YES;
1512 *list = next->next;
1513 continue;
1514 case TOKEN_STREAMBEGIN:
1515 stream->nesting = if_nesting + 1;
1516 *list = next->next;
1517 continue;
1519 default:
1520 if (false_nesting) {
1521 *list = next->next;
1522 __free_token(next);
1523 continue;
1526 if (token_type(next) != TOKEN_IDENT ||
1527 expand_one_symbol(list))
1528 list = &next->next;
1531 if (stream->constant == CONSTANT_FILE_MAYBE) {
1533 * Any token expansion (even if it ended up being an
1534 * empty expansion) in this stream implies it can't
1535 * be constant.
1537 MARK_STREAM_NONCONST(next->pos);
1542 struct token * preprocess(struct token *token)
1544 preprocessing = 1;
1545 init_preprocessor();
1546 do_preprocess(&token);
1548 // Drop all expressions from pre-processing, they're not used any more.
1549 clear_expression_alloc();
1550 preprocessing = 0;
1552 return token;