[PATCH] evaluate_sign() typo
[smatch.git] / pre-process.c
blobd49accddd8a97a1e9e9b0fc202a2faf004d962a4
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 static int true_nesting = 0;
33 static int false_nesting = 0;
34 static struct token *unmatched_if = NULL;
35 #define if_nesting (true_nesting + false_nesting)
37 #define MAX_NEST (256)
38 static unsigned char elif_ignore[MAX_NEST];
39 enum { ELIF_IGNORE = 1, ELIF_SEEN_ELSE = 2 };
41 #define INCLUDEPATHS 300
42 const char *includepath[INCLUDEPATHS+1] = {
43 "/usr/include",
44 "/usr/local/include",
45 GCC_INTERNAL_INCLUDE,
46 NULL
49 static const char **sys_includepath = includepath + 0;
50 static const char **gcc_includepath = includepath + 2;
52 #define MARK_STREAM_NONCONST(pos) do { \
53 if (stream->constant != CONSTANT_FILE_NOPE) { \
54 if (0) \
55 info(pos, "%s triggers non-const", __func__); \
56 stream->constant = CONSTANT_FILE_NOPE; \
57 } \
58 } while (0)
63 * This is stupid - the tokenizer already guarantees unique
64 * identifiers, so we should just compare identifier pointers
66 int match_string_ident(struct ident *ident, const char *str)
68 return !strncmp(str, ident->name, ident->len) && !str[ident->len];
71 static struct token *alloc_token(struct position *pos)
73 struct token *token = __alloc_token(0);
75 token->pos.stream = pos->stream;
76 token->pos.line = pos->line;
77 token->pos.pos = pos->pos;
78 token->pos.whitespace = 1;
79 return token;
82 static const char *show_token_sequence(struct token *token);
84 /* Expand symbol 'sym' at '*list' */
85 static struct token **expand(struct token **, struct symbol *);
87 static void replace_with_string(struct token *token, const char *str)
89 int size = strlen(str) + 1;
90 struct string *s = __alloc_string(size);
92 s->length = size;
93 memcpy(s->data, str, size);
94 token_type(token) = TOKEN_STRING;
95 token->string = s;
98 static void replace_with_integer(struct token *token, unsigned int val)
100 char *buf = __alloc_bytes(10);
101 sprintf(buf, "%d", val);
102 token_type(token) = TOKEN_NUMBER;
103 token->number = buf;
106 static void replace_with_defined(struct token *token)
108 char *string[] = { "0", "1" };
109 int defined = 0;
110 if (token_type(token) != TOKEN_IDENT)
111 warn(token->pos, "operator \"defined\" requires an identifier");
112 else if (lookup_symbol(token->ident, NS_PREPROCESSOR))
113 defined = 1;
114 token_type(token) = TOKEN_NUMBER;
115 token->number = string[defined];
118 struct token **expand_one_symbol(struct token **list)
120 struct token *token = *list;
121 struct symbol *sym;
123 if (token->pos.noexpand)
124 return &token->next;
126 sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
127 if (sym)
128 return expand(list, sym);
129 if (token->ident == &__LINE___ident) {
130 replace_with_integer(token, token->pos.line);
131 } else if (token->ident == &__FILE___ident) {
132 replace_with_string(token, (input_streams + token->pos.stream)->name);
134 return &token->next;
137 static inline struct token *scan_next(struct token **where)
139 struct token *token = *where;
140 if (token_type(token) != TOKEN_UNTAINT)
141 return token;
142 do {
143 token->ident->tainted = 0;
144 token = token->next;
145 } while (token_type(token) == TOKEN_UNTAINT);
146 *where = token;
147 return token;
150 static struct token **expand_list(struct token **list)
152 struct token *next;
153 while (!eof_token(next = scan_next(list))) {
154 if (token_type(next) == TOKEN_IDENT)
155 list = expand_one_symbol(list);
156 else
157 list = &next->next;
159 return list;
162 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
164 struct token **p = &prev->next;
165 struct token *next;
166 int nesting = 0;
168 while (!eof_token(next = scan_next(p))) {
169 if (match_op(next, '(')) {
170 nesting++;
171 } else if (match_op(next, ')')) {
172 if (!nesting--)
173 break;
174 } else if (match_op(next, ',') && !nesting && !vararg) {
175 break;
177 next->pos.stream = pos->stream;
178 next->pos.line = pos->line;
179 next->pos.pos = pos->pos;
180 p = &next->next;
182 *p = &eof_token_entry;
183 return next;
187 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
190 struct arg {
191 struct token *arg;
192 struct token *expanded;
193 struct token *str;
194 int n_normal;
195 int n_quoted;
196 int n_str;
199 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
201 int wanted = arglist->count.normal;
202 struct token *next = NULL;
203 int count = 0;
205 arglist = arglist->next; /* skip counter */
207 if (!wanted) {
208 next = collect_arg(start, 0, &what->pos);
209 if (eof_token(next))
210 goto Eclosing;
211 if (!eof_token(start->next) || !match_op(next, ')')) {
212 count++;
213 goto Emany;
215 } else {
216 for (count = 0; count < wanted; count++) {
217 struct argcount *p = &arglist->next->count;
218 next = collect_arg(start, p->vararg, &what->pos);
219 arglist = arglist->next->next;
220 if (eof_token(next))
221 goto Eclosing;
222 args[count].arg = start->next;
223 args[count].n_normal = p->normal;
224 args[count].n_quoted = p->quoted;
225 args[count].n_str = p->str;
226 if (match_op(next, ')')) {
227 count++;
228 break;
230 start = next;
232 if (count == wanted && !match_op(next, ')'))
233 goto Emany;
234 if (count == wanted - 1) {
235 struct argcount *p = &arglist->next->count;
236 if (!p->vararg)
237 goto Efew;
238 args[count].arg = NULL;
239 args[count].n_normal = p->normal;
240 args[count].n_quoted = p->quoted;
241 args[count].n_str = p->str;
243 if (count < wanted - 1)
244 goto Efew;
246 what->next = next->next;
247 return 1;
249 Efew:
250 warn(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
251 show_token(what), wanted, count);
252 goto out;
253 Emany:
254 while (match_op(next, ',')) {
255 next = collect_arg(next, 0, &what->pos);
256 count++;
258 if (eof_token(next))
259 goto Eclosing;
260 warn(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
261 show_token(what), count, wanted);
262 goto out;
263 Eclosing:
264 warn(what->pos, "unterminated argument list invoking macro \"%s\"",
265 show_token(what));
266 out:
267 what->next = next->next;
268 return 0;
271 static struct token *dup_list(struct token *list)
273 struct token *res;
274 struct token **p = &res;
276 while (!eof_token(list)) {
277 struct token *newtok = __alloc_token(0);
278 *newtok = *list;
279 *p = newtok;
280 p = &newtok->next;
281 list = list->next;
283 return res;
286 static struct token *stringify(struct token *arg)
288 const char *s = show_token_sequence(arg);
289 int size = strlen(s)+1;
290 struct token *token = __alloc_token(0);
291 struct string *string = __alloc_string(size);
293 memcpy(string->data, s, size);
294 string->length = size;
295 token->pos = arg->pos;
296 token_type(token) = TOKEN_STRING;
297 token->string = string;
298 token->next = &eof_token_entry;
299 return token;
302 static void expand_arguments(int count, struct arg *args)
304 int i;
305 for (i = 0; i < count; i++) {
306 struct token *arg = args[i].arg;
307 if (!arg)
308 arg = &eof_token_entry;
309 if (args[i].n_str)
310 args[i].str = stringify(arg);
311 if (args[i].n_normal) {
312 if (!args[i].n_quoted) {
313 args[i].expanded = arg;
314 args[i].arg = NULL;
315 } else if (eof_token(arg)) {
316 args[i].expanded = arg;
317 } else {
318 args[i].expanded = dup_list(arg);
320 expand_list(&args[i].expanded);
326 * Possibly valid combinations:
327 * - ident + ident -> ident
328 * - ident + number -> ident unless number contains '.', '+' or '-'.
329 * - number + number -> number
330 * - number + ident -> number
331 * - number + '.' -> number
332 * - number + '+' or '-' -> number, if number used to end on [eEpP].
333 * - '.' + number -> number, if number used to start with a digit.
334 * - special + special -> either special or an error.
336 static enum token_type combine(struct token *left, struct token *right, char *p)
338 int len;
339 enum token_type t1 = token_type(left), t2 = token_type(right);
341 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
342 return TOKEN_ERROR;
344 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
345 return TOKEN_ERROR;
347 strcpy(p, show_token(left));
348 strcat(p, show_token(right));
349 len = strlen(p);
351 if (len >= 256)
352 return TOKEN_ERROR;
354 if (t1 == TOKEN_IDENT) {
355 if (t2 == TOKEN_SPECIAL)
356 return TOKEN_ERROR;
357 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
358 return TOKEN_ERROR;
359 return TOKEN_IDENT;
362 if (t1 == TOKEN_NUMBER) {
363 if (t2 == TOKEN_SPECIAL) {
364 switch (right->special) {
365 case '.':
366 break;
367 case '+': case '-':
368 if (strchr("eEpP", p[len - 2]))
369 break;
370 default:
371 return TOKEN_ERROR;
374 return TOKEN_NUMBER;
377 if (p[0] == '.' && isdigit((unsigned char)p[1]))
378 return TOKEN_NUMBER;
380 return TOKEN_SPECIAL;
383 static int merge(struct token *left, struct token *right)
385 extern unsigned char combinations[][3];
386 static char buffer[512];
387 int n;
389 switch (combine(left, right, buffer)) {
390 case TOKEN_IDENT:
391 left->ident = built_in_ident(buffer);
392 left->pos.noexpand = 0;
393 return 1;
395 case TOKEN_NUMBER:
396 token_type(left) = TOKEN_NUMBER; /* could be . + num */
397 left->number = __alloc_bytes(strlen(buffer) + 1);
398 memcpy(left->number, buffer, strlen(buffer) + 1);
399 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 warn(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 struct token **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 &token->next;
558 if (sym->arglist) {
559 if (!match_op(scan_next(&token->next), '('))
560 return &token->next;
561 if (!collect_arguments(token->next, sym->arglist, args, token))
562 return &token->next;
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 list;
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 warn(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(token->pos, "unable to open '%s'", filename);
665 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
667 const char *filename;
668 struct token *next;
669 int expect;
671 if (false_nesting)
672 return 1;
674 if (stream->constant == CONSTANT_FILE_MAYBE)
675 MARK_STREAM_NONCONST(token->pos);
677 next = token->next;
678 expect = '>';
679 if (!match_op(next, '<')) {
680 expand_list(&token->next);
681 expect = 0;
682 next = token;
683 if (match_op(token->next, '<')) {
684 next = token->next;
685 expect = '>';
688 token = next->next;
689 filename = token_name_sequence(token, expect, token);
690 do_include(!expect, stream, list, token, filename, path);
691 return 1;
694 static int handle_include(struct stream *stream, struct token **list, struct token *token)
696 return handle_include_path(stream, list, token, includepath);
699 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
701 return handle_include_path(stream, list, token, stream->next_path);
704 static int token_different(struct token *t1, struct token *t2)
706 int different;
708 if (token_type(t1) != token_type(t2))
709 return 1;
711 switch (token_type(t1)) {
712 case TOKEN_IDENT:
713 different = t1->ident != t2->ident;
714 break;
715 case TOKEN_ARG_COUNT:
716 case TOKEN_UNTAINT:
717 case TOKEN_CONCAT:
718 case TOKEN_GNU_KLUDGE:
719 different = 0;
720 break;
721 case TOKEN_NUMBER:
722 different = strcmp(t1->number, t2->number);
723 break;
724 case TOKEN_SPECIAL:
725 different = t1->special != t2->special;
726 break;
727 case TOKEN_MACRO_ARGUMENT:
728 case TOKEN_QUOTED_ARGUMENT:
729 case TOKEN_STR_ARGUMENT:
730 different = t1->argnum != t2->argnum;
731 break;
732 case TOKEN_CHAR:
733 different = t1->character != t2->character;
734 break;
735 case TOKEN_STRING: {
736 struct string *s1, *s2;
738 s1 = t1->string;
739 s2 = t2->string;
740 different = 1;
741 if (s1->length != s2->length)
742 break;
743 different = memcmp(s1->data, s2->data, s1->length);
744 break;
746 default:
747 different = 1;
748 break;
750 return different;
753 static int token_list_different(struct token *list1, struct token *list2)
755 for (;;) {
756 if (list1 == list2)
757 return 0;
758 if (!list1 || !list2)
759 return 1;
760 if (token_different(list1, list2))
761 return 1;
762 list1 = list1->next;
763 list2 = list2->next;
767 static inline void set_arg_count(struct token *token)
769 token_type(token) = TOKEN_ARG_COUNT;
770 token->count.normal = token->count.quoted =
771 token->count.str = token->count.vararg = 0;
774 static struct token *parse_arguments(struct token *list)
776 struct token *arg = list->next, *next = list;
777 struct argcount *count = &list->count;
779 set_arg_count(list);
781 if (match_op(arg, ')')) {
782 next = arg->next;
783 list->next = &eof_token_entry;
784 return next;
787 while (token_type(arg) == TOKEN_IDENT) {
788 if (arg->ident == &__VA_ARGS___ident)
789 goto Eva_args;
790 if (!++count->normal)
791 goto Eargs;
792 next = arg->next;
794 if (match_op(next, ',')) {
795 set_arg_count(next);
796 arg = next->next;
797 continue;
800 if (match_op(next, ')')) {
801 set_arg_count(next);
802 next = next->next;
803 arg->next->next = &eof_token_entry;
804 return next;
807 /* normal cases are finished here */
809 if (match_op(next, SPECIAL_ELLIPSIS)) {
810 if (match_op(next->next, ')')) {
811 set_arg_count(next);
812 next->count.vararg = 1;
813 next = next->next;
814 arg->next->next = &eof_token_entry;
815 return next->next;
818 arg = next;
819 goto Enotclosed;
822 if (eof_token(next)) {
823 goto Enotclosed;
824 } else {
825 arg = next;
826 goto Ebadstuff;
830 if (match_op(arg, SPECIAL_ELLIPSIS)) {
831 next = arg->next;
832 token_type(arg) = TOKEN_IDENT;
833 arg->ident = &__VA_ARGS___ident;
834 if (!match_op(next, ')'))
835 goto Enotclosed;
836 if (!++count->normal)
837 goto Eargs;
838 set_arg_count(next);
839 next->count.vararg = 1;
840 next = next->next;
841 arg->next->next = &eof_token_entry;
842 return next;
845 if (eof_token(arg)) {
846 arg = next;
847 goto Enotclosed;
849 if (match_op(arg, ','))
850 goto Emissing;
851 else
852 goto Ebadstuff;
855 Emissing:
856 warn(arg->pos, "parameter name missing");
857 return NULL;
858 Ebadstuff:
859 warn(arg->pos, "\"%s\" may not appear in macro parameter list",
860 show_token(arg));
861 return NULL;
862 Enotclosed:
863 warn(arg->pos, "missing ')' in macro parameter list");
864 return NULL;
865 Eva_args:
866 warn(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
867 return NULL;
868 Eargs:
869 warn(arg->pos, "too many arguments in macro definition");
870 return NULL;
873 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
875 struct ident *ident = token->ident;
876 int nr;
878 if (!arglist || token_type(token) != TOKEN_IDENT)
879 return 0;
881 arglist = arglist->next;
883 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
884 if (arglist->ident == ident) {
885 struct argcount *count = &arglist->next->count;
886 int n;
888 token->argnum = nr;
889 token_type(token) = type;
890 switch (type) {
891 case TOKEN_MACRO_ARGUMENT:
892 n = ++count->normal;
893 break;
894 case TOKEN_QUOTED_ARGUMENT:
895 n = ++count->quoted;
896 break;
897 default:
898 n = ++count->str;
900 if (n)
901 return count->vararg ? 2 : 1;
902 token_type(token) = TOKEN_ERROR;
903 return -1;
906 return 0;
909 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
911 struct token *token = expansion;
912 struct token **p;
913 struct token *last = NULL;
915 if (match_op(token, SPECIAL_HASHHASH))
916 goto Econcat;
918 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
919 if (match_op(token, '#')) {
920 if (arglist) {
921 struct token *next = token->next;
922 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
923 goto Equote;
924 next->pos.whitespace = token->pos.whitespace;
925 token = *p = next;
926 } else {
927 token->pos.noexpand = 1;
929 } else if (match_op(token, SPECIAL_HASHHASH)) {
930 struct token *next = token->next;
931 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
932 token_type(token) = TOKEN_CONCAT;
933 if (arg) {
934 token = next;
935 /* GNU kludge */
936 if (arg == 2 && last && match_op(last, ',')) {
937 token_type(last) = TOKEN_GNU_KLUDGE;
938 last->next = token;
940 } else if (match_op(next, SPECIAL_HASHHASH))
941 token = next;
942 else if (match_op(next, ','))
943 token = next;
944 else if (eof_token(next))
945 goto Econcat;
946 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
947 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
948 } else {
949 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
951 if (token_type(token) == TOKEN_ERROR)
952 goto Earg;
953 last = token;
955 token = alloc_token(&expansion->pos);
956 token_type(token) = TOKEN_UNTAINT;
957 token->ident = name;
958 token->next = *p;
959 *p = token;
960 return expansion;
962 Equote:
963 warn(token->pos, "'#' is not followed by a macro parameter");
964 return NULL;
966 Econcat:
967 warn(token->pos, "'##' cannot appear at the ends of macro expansion");
968 return NULL;
969 Earg:
970 warn(token->pos, "too many instances of argument in body");
971 return NULL;
974 static int handle_define(struct stream *stream, struct token **line, struct token *token)
976 struct token *arglist, *expansion;
977 struct token *left = token->next;
978 struct symbol *sym;
979 struct ident *name;
981 if (token_type(left) != TOKEN_IDENT) {
982 warn(token->pos, "expected identifier to 'define'");
983 return 0;
985 if (false_nesting)
986 return 1;
988 if (stream->constant == CONSTANT_FILE_MAYBE)
989 MARK_STREAM_NONCONST(token->pos);
991 name = left->ident;
993 arglist = NULL;
994 expansion = left->next;
995 if (!expansion->pos.whitespace && match_op(expansion, '(')) {
996 arglist = expansion;
997 expansion = parse_arguments(expansion);
998 if (!expansion)
999 return 1;
1002 expansion = parse_expansion(expansion, arglist, name);
1003 if (!expansion)
1004 return 1;
1006 sym = lookup_symbol(name, NS_PREPROCESSOR);
1007 if (sym) {
1008 if (token_list_different(sym->expansion, expansion) ||
1009 token_list_different(sym->arglist, arglist)) {
1010 warn(left->pos, "preprocessor token %.*s redefined",
1011 name->len, name->name);
1012 info(sym->pos, "this was the original definition");
1014 return 1;
1016 sym = alloc_symbol(left->pos, SYM_NODE);
1017 bind_symbol(sym, name, NS_PREPROCESSOR);
1019 sym->expansion = expansion;
1020 sym->arglist = arglist;
1021 return 1;
1024 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1026 struct token *left = token->next;
1027 struct symbol **sym;
1029 if (token_type(left) != TOKEN_IDENT) {
1030 warn(token->pos, "expected identifier to 'undef'");
1031 return 0;
1033 if (false_nesting)
1034 return 1;
1036 if (stream->constant == CONSTANT_FILE_MAYBE)
1037 MARK_STREAM_NONCONST(token->pos);
1039 sym = &left->ident->symbols;
1040 while (*sym) {
1041 struct symbol *t = *sym;
1042 if (t->namespace == NS_PREPROCESSOR) {
1043 *sym = t->next_id;
1044 return 1;
1046 sym = &t->next_id;
1048 return 1;
1051 static int preprocessor_if(struct token *token, int true)
1053 if (if_nesting == 0)
1054 unmatched_if = token;
1055 if (if_nesting >= MAX_NEST)
1056 error(token->pos, "Maximum preprocessor conditional level exhausted");
1057 elif_ignore[if_nesting] = (false_nesting || true) ? ELIF_IGNORE : 0;
1058 if (false_nesting || !true) {
1059 false_nesting++;
1060 return 1;
1062 true_nesting++;
1063 return 1;
1066 static int token_defined(struct token *token)
1068 if (token_type(token) == TOKEN_IDENT)
1069 return lookup_symbol(token->ident, NS_PREPROCESSOR) != NULL;
1071 warn(token->pos, "expected identifier for #if[n]def");
1072 return 0;
1075 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1077 return preprocessor_if(token, token_defined(token->next));
1080 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1082 struct token *next = token->next;
1083 if (stream->constant == CONSTANT_FILE_MAYBE) {
1084 if (token_type(next) == TOKEN_IDENT &&
1085 (!stream->protect || stream->protect == next->ident)) {
1086 stream->constant = CONSTANT_FILE_IFNDEF;
1087 stream->protect = next->ident;
1088 } else
1089 MARK_STREAM_NONCONST(token->pos);
1091 return preprocessor_if(token, !token_defined(next));
1095 * Expression handling for #if and #elif; it differs from normal expansion
1096 * due to special treatment of "defined".
1098 static int expression_value(struct token **where)
1100 struct expression *expr;
1101 struct token *p;
1102 struct token **list = where, **beginning = NULL;
1103 long long value;
1104 int state = 0;
1106 while (!eof_token(p = scan_next(list))) {
1107 switch (state) {
1108 case 0:
1109 if (token_type(p) == TOKEN_IDENT) {
1110 if (p->ident != &defined_ident) {
1111 list = expand_one_symbol(list);
1112 continue;
1114 state = 1;
1115 beginning = list;
1117 break;
1118 case 1:
1119 if (match_op(p, '(')) {
1120 state = 2;
1121 } else {
1122 state = 0;
1123 replace_with_defined(p);
1124 *beginning = p;
1126 break;
1127 case 2:
1128 if (token_type(p) == TOKEN_IDENT)
1129 state = 3;
1130 else
1131 state = 0;
1132 replace_with_defined(p);
1133 *beginning = p;
1134 break;
1135 case 3:
1136 state = 0;
1137 if (!match_op(p, ')'))
1138 warn(p->pos, "missing ')' after \"defined\"");
1139 *list = p->next;
1140 continue;
1142 list = &p->next;
1145 p = constant_expression(*where, &expr);
1146 if (!eof_token(p))
1147 warn(p->pos, "garbage at end: %s", show_token_sequence(p));
1148 value = get_expression_value(expr);
1149 return value != 0;
1152 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1154 int value = 0;
1155 if (!false_nesting)
1156 value = expression_value(&token->next);
1158 // This is an approximation. We really only need this if the
1159 // condition does depends on a pre-processor symbol. Note, that
1160 // the important #ifndef case has already changed ->constant.
1161 if (stream->constant == CONSTANT_FILE_MAYBE)
1162 MARK_STREAM_NONCONST(token->pos);
1164 return preprocessor_if(token, value);
1167 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1169 if (stream->nesting == if_nesting)
1170 MARK_STREAM_NONCONST(token->pos);
1172 if (stream->nesting > if_nesting) {
1173 warn(token->pos, "unmatched #elif");
1174 return 1;
1177 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1178 warn(token->pos, "#elif after #else");
1180 if (false_nesting) {
1181 /* If this whole if-thing is if'ed out, an elif cannot help */
1182 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1183 return 1;
1184 if (expression_value(&token->next)) {
1185 false_nesting = 0;
1186 true_nesting++;
1187 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1189 } else {
1190 false_nesting = 1;
1191 true_nesting--;
1193 return 1;
1196 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1198 if (stream->nesting == if_nesting)
1199 MARK_STREAM_NONCONST(token->pos);
1201 if (stream->nesting > if_nesting) {
1202 warn(token->pos, "unmatched #else");
1203 return 1;
1206 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1207 warn(token->pos, "#else after #else");
1208 else
1209 elif_ignore[if_nesting-1] |= ELIF_SEEN_ELSE;
1211 if (false_nesting) {
1212 /* If this whole if-thing is if'ed out, an else cannot help */
1213 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1214 return 1;
1215 false_nesting = 0;
1216 true_nesting++;
1217 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1218 } else {
1219 true_nesting--;
1220 false_nesting = 1;
1222 return 1;
1225 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1227 if (stream->constant == CONSTANT_FILE_IFNDEF && stream->nesting == if_nesting)
1228 stream->constant = CONSTANT_FILE_MAYBE;
1230 if (stream->nesting > if_nesting)
1231 warn(token->pos, "unmatched #endif");
1232 else if (false_nesting)
1233 false_nesting--;
1234 else
1235 true_nesting--;
1236 return 1;
1239 static const char *show_token_sequence(struct token *token)
1241 static char buffer[1024];
1242 char *ptr = buffer;
1243 int whitespace = 0;
1245 if (!token)
1246 return "<none>";
1247 while (!eof_token(token)) {
1248 const char *val = show_token(token);
1249 int len = strlen(val);
1251 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1252 warn(token->pos, "too long token expansion");
1253 break;
1256 if (whitespace)
1257 *ptr++ = ' ';
1258 memcpy(ptr, val, len);
1259 ptr += len;
1260 token = token->next;
1261 whitespace = token->pos.whitespace;
1263 *ptr = 0;
1264 return buffer;
1267 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1269 if (false_nesting)
1270 return 1;
1271 if (stream->constant == CONSTANT_FILE_MAYBE)
1272 MARK_STREAM_NONCONST(token->pos);
1273 warn(token->pos, "%s", show_token_sequence(token->next));
1274 return 1;
1277 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1279 if (false_nesting)
1280 return 1;
1281 if (stream->constant == CONSTANT_FILE_MAYBE)
1282 MARK_STREAM_NONCONST(token->pos);
1283 warn(token->pos, "%s", show_token_sequence(token->next));
1284 return 1;
1287 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1289 int stdinc;
1291 if (false_nesting)
1292 return 1;
1295 * Do we have any non-system includes?
1296 * Clear them out if so..
1298 stdinc = gcc_includepath - sys_includepath;
1299 if (stdinc) {
1300 const char **src = gcc_includepath;
1301 const char **dst = sys_includepath;
1302 for (;;) {
1303 if (!(*dst = *src))
1304 break;
1305 dst++;
1306 src++;
1308 gcc_includepath -= stdinc;
1310 return 1;
1313 static void add_path_entry(struct token *token, const char *path)
1315 const char **dst;
1316 const char *next;
1318 /* Need one free entry.. */
1319 if (includepath[INCLUDEPATHS-2])
1320 error(token->pos, "too many include path entries");
1322 next = path;
1323 dst = sys_includepath;
1324 sys_includepath++;
1325 gcc_includepath++;
1328 * Move them all up starting at "sys_includepath",
1329 * insert the new entry..
1331 for (;;) {
1332 const char *tmp = *dst;
1333 *dst = next;
1334 if (!next)
1335 break;
1336 next = tmp;
1337 dst++;
1341 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1343 for (;;) {
1344 token = token->next;
1345 if (eof_token(token))
1346 return 1;
1347 if (token_type(token) != TOKEN_STRING) {
1348 warn(token->pos, "expected path string");
1349 return 1;
1351 add_path_entry(token, token->string->data);
1356 * We replace "#pragma xxx" with "__pragma__" in the token
1357 * stream. Just as an example.
1359 * We'll just #define that away for now, but the theory here
1360 * is that we can use this to insert arbitrary token sequences
1361 * to turn the pragma's into internal front-end sequences for
1362 * when we actually start caring about them.
1364 * So eventually this will turn into some kind of extended
1365 * __attribute__() like thing, except called __pragma__(xxx).
1367 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1369 struct token *next = *line;
1371 token->ident = &pragma_ident;
1372 token->pos.newline = 1;
1373 token->pos.whitespace = 1;
1374 token->pos.pos = 1;
1375 *line = token;
1376 token->next = next;
1377 return 1;
1381 * We ignore #line for now.
1383 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1385 return 1;
1388 static int handle_preprocessor_command(struct stream *stream, struct token **line, struct ident *ident, struct token *token)
1390 int i;
1391 static struct {
1392 const char *name;
1393 int (*handler)(struct stream *, struct token **, struct token *);
1394 } handlers[] = {
1395 { "define", handle_define },
1396 { "undef", handle_undef },
1397 { "ifdef", handle_ifdef },
1398 { "ifndef", handle_ifndef },
1399 { "else", handle_else },
1400 { "endif", handle_endif },
1401 { "if", handle_if },
1402 { "elif", handle_elif },
1403 { "warning", handle_warning },
1404 { "error", handle_error },
1405 { "include", handle_include },
1406 { "include_next",handle_include_next },
1407 { "pragma", handle_pragma },
1408 { "line", handle_line },
1410 // our internal preprocessor tokens
1411 { "nostdinc", handle_nostdinc },
1412 { "add_include", handle_add_include },
1415 for (i = 0; i < (sizeof (handlers) / sizeof (handlers[0])); i++) {
1416 if (match_string_ident(ident, handlers[i].name))
1417 return handlers[i].handler(stream, line, token);
1419 return 0;
1422 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1424 struct token *token = start->next;
1426 if (!token)
1427 return;
1429 if (token_type(token) == TOKEN_NUMBER)
1430 if (handle_line(stream, line, start))
1431 return;
1433 if (token_type(token) == TOKEN_IDENT)
1434 if (handle_preprocessor_command(stream, line, token->ident, token))
1435 return;
1437 warn(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1440 static void preprocessor_line(struct stream *stream, struct token **line)
1442 struct token *start = *line, *next;
1443 struct token **tp = &start->next;
1445 for (;;) {
1446 next = *tp;
1447 if (next->pos.newline)
1448 break;
1449 tp = &next->next;
1451 *line = next;
1452 *tp = &eof_token_entry;
1453 handle_preprocessor_line(stream, line, start);
1456 static void do_preprocess(struct token **list)
1458 struct token *next;
1460 while (!eof_token(next = scan_next(list))) {
1461 struct stream *stream = input_streams + next->pos.stream;
1463 if (next->pos.newline && match_op(next, '#')) {
1464 if (!next->pos.noexpand) {
1465 preprocessor_line(stream, list);
1466 continue;
1470 switch (token_type(next)) {
1471 case TOKEN_STREAMEND:
1472 if (stream->nesting < if_nesting + 1) {
1473 warn(unmatched_if->pos, "unterminated preprocessor conditional");
1474 // Pretend to see a series of #endifs
1475 MARK_STREAM_NONCONST(next->pos);
1476 do {
1477 handle_endif (stream, NULL, NULL);
1478 } while (stream->nesting < if_nesting + 1);
1480 if (stream->constant == CONSTANT_FILE_MAYBE && stream->protect) {
1481 stream->constant = CONSTANT_FILE_YES;
1483 *list = next->next;
1484 continue;
1485 case TOKEN_STREAMBEGIN:
1486 stream->nesting = if_nesting + 1;
1487 *list = next->next;
1488 continue;
1490 default:
1491 if (false_nesting) {
1492 *list = next->next;
1493 continue;
1496 if (token_type(next) == TOKEN_IDENT)
1497 list = expand_one_symbol(list);
1498 else
1499 list = &next->next;
1502 if (stream->constant == CONSTANT_FILE_MAYBE) {
1504 * Any token expansion (even if it ended up being an
1505 * empty expansion) in this stream implies it can't
1506 * be constant.
1508 MARK_STREAM_NONCONST(next->pos);
1513 struct token * preprocess(struct token *token)
1515 preprocessing = 1;
1516 do_preprocess(&token);
1518 // Drop all expressions from pre-processing, they're not used any more.
1519 clear_expression_alloc();
1520 preprocessing = 0;
1522 return token;