Make "sparse()" handle multiple input files on the command line
[smatch.git] / pre-process.c
blob52dc10090132b901305e8e7a3bc34631e41cb34b
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 position unmatched_if_pos;
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 "",
42 "/usr/include",
43 "/usr/local/include",
44 GCC_INTERNAL_INCLUDE,
45 NULL
48 static const char **quote_includepath = includepath;
49 static const char **angle_includepath = includepath + 1;
50 static const char **sys_includepath = includepath + 1;
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)
61 static struct token *alloc_token(struct position *pos)
63 struct token *token = __alloc_token(0);
65 token->pos.stream = pos->stream;
66 token->pos.line = pos->line;
67 token->pos.pos = pos->pos;
68 token->pos.whitespace = 1;
69 return token;
72 static const char *show_token_sequence(struct token *token);
74 /* Expand symbol 'sym' at '*list' */
75 static int expand(struct token **, struct symbol *);
77 static void replace_with_string(struct token *token, const char *str)
79 int size = strlen(str) + 1;
80 struct string *s = __alloc_string(size);
82 s->length = size;
83 memcpy(s->data, str, size);
84 token_type(token) = TOKEN_STRING;
85 token->string = s;
88 static void replace_with_integer(struct token *token, unsigned int val)
90 char *buf = __alloc_bytes(11);
91 sprintf(buf, "%u", val);
92 token_type(token) = TOKEN_NUMBER;
93 token->number = buf;
96 static int token_defined(struct token *token)
98 if (token_type(token) == TOKEN_IDENT) {
99 struct symbol *sym = lookup_symbol(token->ident, NS_MACRO);
100 if (sym) {
101 sym->weak = 0;
102 return 1;
104 return 0;
107 warning(token->pos, "expected preprocessor identifier");
108 return 0;
111 static void replace_with_defined(struct token *token)
113 static const char *string[] = { "0", "1" };
114 int defined = token_defined(token);
116 token_type(token) = TOKEN_NUMBER;
117 token->number = string[defined];
120 static int expand_one_symbol(struct token **list)
122 struct token *token = *list;
123 struct symbol *sym;
125 if (token->pos.noexpand)
126 return 1;
128 sym = lookup_symbol(token->ident, NS_MACRO);
129 if (sym) {
130 sym->weak = 0;
131 return expand(list, sym);
133 if (token->ident == &__LINE___ident) {
134 replace_with_integer(token, token->pos.line);
135 } else if (token->ident == &__FILE___ident) {
136 replace_with_string(token, stream_name(token->pos.stream));
138 return 1;
141 static inline struct token *scan_next(struct token **where)
143 struct token *token = *where;
144 if (token_type(token) != TOKEN_UNTAINT)
145 return token;
146 do {
147 token->ident->tainted = 0;
148 token = token->next;
149 } while (token_type(token) == TOKEN_UNTAINT);
150 *where = token;
151 return token;
154 static void expand_list(struct token **list)
156 struct token *next;
157 while (!eof_token(next = scan_next(list))) {
158 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
159 list = &next->next;
163 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
165 struct token **p = &prev->next;
166 struct token *next;
167 int nesting = 0;
169 while (!eof_token(next = scan_next(p))) {
170 if (match_op(next, '(')) {
171 nesting++;
172 } else if (match_op(next, ')')) {
173 if (!nesting--)
174 break;
175 } else if (match_op(next, ',') && !nesting && !vararg) {
176 break;
178 next->pos.stream = pos->stream;
179 next->pos.line = pos->line;
180 next->pos.pos = pos->pos;
181 p = &next->next;
183 *p = &eof_token_entry;
184 return next;
188 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
191 struct arg {
192 struct token *arg;
193 struct token *expanded;
194 struct token *str;
195 int n_normal;
196 int n_quoted;
197 int n_str;
200 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
202 int wanted = arglist->count.normal;
203 struct token *next = NULL;
204 int count = 0;
206 arglist = arglist->next; /* skip counter */
208 if (!wanted) {
209 next = collect_arg(start, 0, &what->pos);
210 if (eof_token(next))
211 goto Eclosing;
212 if (!eof_token(start->next) || !match_op(next, ')')) {
213 count++;
214 goto Emany;
216 } else {
217 for (count = 0; count < wanted; count++) {
218 struct argcount *p = &arglist->next->count;
219 next = collect_arg(start, p->vararg, &what->pos);
220 arglist = arglist->next->next;
221 if (eof_token(next))
222 goto Eclosing;
223 args[count].arg = start->next;
224 args[count].n_normal = p->normal;
225 args[count].n_quoted = p->quoted;
226 args[count].n_str = p->str;
227 if (match_op(next, ')')) {
228 count++;
229 break;
231 start = next;
233 if (count == wanted && !match_op(next, ')'))
234 goto Emany;
235 if (count == wanted - 1) {
236 struct argcount *p = &arglist->next->count;
237 if (!p->vararg)
238 goto Efew;
239 args[count].arg = NULL;
240 args[count].n_normal = p->normal;
241 args[count].n_quoted = p->quoted;
242 args[count].n_str = p->str;
244 if (count < wanted - 1)
245 goto Efew;
247 what->next = next->next;
248 return 1;
250 Efew:
251 warning(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
252 show_token(what), wanted, count);
253 goto out;
254 Emany:
255 while (match_op(next, ',')) {
256 next = collect_arg(next, 0, &what->pos);
257 count++;
259 if (eof_token(next))
260 goto Eclosing;
261 warning(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
262 show_token(what), count, wanted);
263 goto out;
264 Eclosing:
265 warning(what->pos, "unterminated argument list invoking macro \"%s\"",
266 show_token(what));
267 out:
268 what->next = next->next;
269 return 0;
272 static struct token *dup_list(struct token *list)
274 struct token *res;
275 struct token **p = &res;
277 while (!eof_token(list)) {
278 struct token *newtok = __alloc_token(0);
279 *newtok = *list;
280 *p = newtok;
281 p = &newtok->next;
282 list = list->next;
284 return res;
287 static struct token *stringify(struct token *arg)
289 const char *s = show_token_sequence(arg);
290 int size = strlen(s)+1;
291 struct token *token = __alloc_token(0);
292 struct string *string = __alloc_string(size);
294 memcpy(string->data, s, size);
295 string->length = size;
296 token->pos = arg->pos;
297 token_type(token) = TOKEN_STRING;
298 token->string = string;
299 token->next = &eof_token_entry;
300 return token;
303 static void expand_arguments(int count, struct arg *args)
305 int i;
306 for (i = 0; i < count; i++) {
307 struct token *arg = args[i].arg;
308 if (!arg)
309 arg = &eof_token_entry;
310 if (args[i].n_str)
311 args[i].str = stringify(arg);
312 if (args[i].n_normal) {
313 if (!args[i].n_quoted) {
314 args[i].expanded = arg;
315 args[i].arg = NULL;
316 } else if (eof_token(arg)) {
317 args[i].expanded = arg;
318 } else {
319 args[i].expanded = dup_list(arg);
321 expand_list(&args[i].expanded);
327 * Possibly valid combinations:
328 * - ident + ident -> ident
329 * - ident + number -> ident unless number contains '.', '+' or '-'.
330 * - number + number -> number
331 * - number + ident -> number
332 * - number + '.' -> number
333 * - number + '+' or '-' -> number, if number used to end on [eEpP].
334 * - '.' + number -> number, if number used to start with a digit.
335 * - special + special -> either special or an error.
337 static enum token_type combine(struct token *left, struct token *right, char *p)
339 int len;
340 enum token_type t1 = token_type(left), t2 = token_type(right);
342 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
343 return TOKEN_ERROR;
345 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
346 return TOKEN_ERROR;
348 strcpy(p, show_token(left));
349 strcat(p, show_token(right));
350 len = strlen(p);
352 if (len >= 256)
353 return TOKEN_ERROR;
355 if (t1 == TOKEN_IDENT) {
356 if (t2 == TOKEN_SPECIAL)
357 return TOKEN_ERROR;
358 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
359 return TOKEN_ERROR;
360 return TOKEN_IDENT;
363 if (t1 == TOKEN_NUMBER) {
364 if (t2 == TOKEN_SPECIAL) {
365 switch (right->special) {
366 case '.':
367 break;
368 case '+': case '-':
369 if (strchr("eEpP", p[len - 2]))
370 break;
371 default:
372 return TOKEN_ERROR;
375 return TOKEN_NUMBER;
378 if (p[0] == '.' && isdigit((unsigned char)p[1]))
379 return TOKEN_NUMBER;
381 return TOKEN_SPECIAL;
384 static int merge(struct token *left, struct token *right)
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 char *number = __alloc_bytes(strlen(buffer) + 1);
397 memcpy(number, buffer, strlen(buffer) + 1);
398 token_type(left) = TOKEN_NUMBER; /* could be . + num */
399 left->number = number;
400 return 1;
403 case TOKEN_SPECIAL:
404 if (buffer[2] && buffer[3])
405 break;
406 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
407 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
408 left->special = n;
409 return 1;
412 default:
415 warning(left->pos, "'##' failed: concatenation is not a valid token");
416 return 0;
419 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
421 struct token *alloc = alloc_token(streampos);
422 token_type(alloc) = token_type(token);
423 alloc->pos.newline = pos->newline;
424 alloc->pos.whitespace = pos->whitespace;
425 alloc->number = token->number;
426 alloc->pos.noexpand = token->pos.noexpand;
427 return alloc;
430 static struct token **copy(struct token **where, struct token *list, int *count)
432 int need_copy = --*count;
433 while (!eof_token(list)) {
434 struct token *token;
435 if (need_copy)
436 token = dup_token(list, &list->pos, &list->pos);
437 else
438 token = list;
439 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
440 token->pos.noexpand = 1;
441 *where = token;
442 where = &token->next;
443 list = list->next;
445 *where = &eof_token_entry;
446 return where;
449 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
451 struct token *token = *list;
452 struct position *base_pos = &token->pos;
453 struct position *pos = base_pos;
454 int *count;
455 enum {Normal, Placeholder, Concat} state = Normal;
457 for (; !eof_token(body); body = body->next, pos = &body->pos) {
458 struct token *added, *arg;
459 struct token **tail;
461 switch (token_type(body)) {
462 case TOKEN_GNU_KLUDGE:
464 * GNU kludge: if we had <comma>##<vararg>, behaviour
465 * depends on whether we had enough arguments to have
466 * a vararg. If we did, ## is just ignored. Otherwise
467 * both , and ## are ignored. Comma should come from
468 * the body of macro and not be an argument of earlier
469 * concatenation.
471 if (!args[body->next->argnum].arg)
472 continue;
473 added = dup_token(body, base_pos, pos);
474 token_type(added) = TOKEN_SPECIAL;
475 tail = &added->next;
476 break;
478 case TOKEN_STR_ARGUMENT:
479 arg = args[body->argnum].str;
480 count = &args[body->argnum].n_str;
481 goto copy_arg;
483 case TOKEN_QUOTED_ARGUMENT:
484 arg = args[body->argnum].arg;
485 count = &args[body->argnum].n_quoted;
486 if (!arg || eof_token(arg)) {
487 if (state == Concat)
488 state = Normal;
489 else
490 state = Placeholder;
491 continue;
493 goto copy_arg;
495 case TOKEN_MACRO_ARGUMENT:
496 arg = args[body->argnum].expanded;
497 count = &args[body->argnum].n_normal;
498 if (eof_token(arg)) {
499 state = Normal;
500 continue;
502 copy_arg:
503 tail = copy(&added, arg, count);
504 added->pos.newline = pos->newline;
505 added->pos.whitespace = pos->whitespace;
506 break;
508 case TOKEN_CONCAT:
509 if (state == Placeholder)
510 state = Normal;
511 else
512 state = Concat;
513 continue;
515 case TOKEN_IDENT:
516 added = dup_token(body, base_pos, pos);
517 if (added->ident->tainted)
518 added->pos.noexpand = 1;
519 tail = &added->next;
520 break;
522 default:
523 added = dup_token(body, base_pos, pos);
524 tail = &added->next;
525 break;
529 * if we got to doing real concatenation, we already have
530 * added something into the list, so containing_token() is OK.
532 if (state == Concat && merge(containing_token(list), added)) {
533 *list = added->next;
534 if (tail != &added->next)
535 list = tail;
536 } else {
537 *list = added;
538 list = tail;
540 state = Normal;
542 *list = &eof_token_entry;
543 return list;
546 static int expand(struct token **list, struct symbol *sym)
548 struct token *last;
549 struct token *token = *list;
550 struct ident *expanding = token->ident;
551 struct token **tail;
552 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
553 struct arg args[nargs];
555 if (expanding->tainted) {
556 token->pos.noexpand = 1;
557 return 1;
560 if (sym->arglist) {
561 if (!match_op(scan_next(&token->next), '('))
562 return 1;
563 if (!collect_arguments(token->next, sym->arglist, args, token))
564 return 1;
565 expand_arguments(nargs, args);
568 expanding->tainted = 1;
570 last = token->next;
571 tail = substitute(list, sym->expansion, args);
572 *tail = last;
574 return 0;
577 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
579 struct token *last;
580 static char buffer[256];
581 char *ptr = buffer;
583 last = token;
584 while (!eof_token(token) && !match_op(token, endop)) {
585 int len;
586 const char *val = token->string->data;
587 if (token_type(token) != TOKEN_STRING)
588 val = show_token(token);
589 len = strlen(val);
590 memcpy(ptr, val, len);
591 ptr += len;
592 token = token->next;
594 *ptr = 0;
595 if (endop && !match_op(token, endop))
596 warning(start->pos, "expected '>' at end of filename");
597 return buffer;
600 static int already_tokenized(const char *path)
602 int i;
603 struct stream *s = input_streams;
605 for (i = input_stream_nr; --i >= 0; s++) {
606 if (s->constant != CONSTANT_FILE_YES)
607 continue;
608 if (strcmp(path, s->name))
609 continue;
610 if (!lookup_symbol(s->protect, NS_MACRO))
611 continue;
612 return 1;
614 return 0;
617 /* Hande include of header files.
618 * The relevant options are made compatible with gcc. The only options that
619 * are not supported is -withprefix and friends.
621 * Three set of include paths are known:
622 * quote_includepath: Path to search when using #include "file.h"
623 * angle_includepath: Path to search when using #include <file.h>
624 * sys_includepath: Built-in include paths
626 * The above is implmented as one array with pointes
627 * +--------------+
628 * quote_includepath ---> | |
629 * +--------------+
630 * | |
631 * +--------------+
632 * angle_includepath ---> | |
633 * +--------------+
634 * sys_includepath ---> | |
635 * +--------------+
636 * | |
637 * +--------------+
639 * -I dir insert dir just before sys_includepath and move the rest
640 * -I- makes all dirs specified with -I before to quote dirs only and
641 * angle_includepath is set equal to sys_includepath.
642 * -nostdinc removes all sys dirs be storing NULL in entry pointed
643 * to by * sys_includepath. Note this will reset all dirs built-in and added
644 * before -nostdinc by -isystem and -dirafter
645 * -isystem dir adds dir where sys_includepath points adding this dir as
646 * first systemdir
647 * -dirafter dir adds dir to the end of the list
650 static void set_stream_include_path(struct stream *stream)
652 const char *path = stream->path;
653 if (!path) {
654 const char *p = strrchr(stream->name, '/');
655 path = "";
656 if (p) {
657 int len = p - stream->name + 1;
658 char *m = malloc(len+1);
659 /* This includes the final "/" */
660 memcpy(m, stream->name, len);
661 m[len] = 0;
662 path = m;
664 stream->path = path;
666 includepath[0] = path;
669 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
671 int fd;
672 int plen = strlen(path);
673 static char fullname[PATH_MAX];
675 memcpy(fullname, path, plen);
676 if (plen && path[plen-1] != '/') {
677 fullname[plen] = '/';
678 plen++;
680 memcpy(fullname+plen, filename, flen);
681 if (already_tokenized(fullname))
682 return 1;
683 fd = open(fullname, O_RDONLY);
684 if (fd >= 0) {
685 char * streamname = __alloc_bytes(plen + flen);
686 memcpy(streamname, fullname, plen + flen);
687 *where = tokenize(streamname, fd, *where, next_path);
688 close(fd);
689 return 1;
691 return 0;
694 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
696 const char *path;
698 while ((path = *pptr++) != NULL) {
699 if (!try_include(path, filename, flen, list, pptr))
700 continue;
701 return 1;
703 return 0;
706 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
708 int flen = strlen(filename) + 1;
710 /* Absolute path? */
711 if (filename[0] == '/') {
712 if (try_include("", filename, flen, list, includepath))
713 return;
714 goto out;
717 /* Dir of inputfile is first dir to search for quoted includes */
718 set_stream_include_path(stream);
720 if (!path)
721 /* Do not search quote include if <> is in use */
722 path = local ? quote_includepath : angle_includepath;
724 /* Check the standard include paths.. */
725 if (do_include_path(path, list, token, filename, flen))
726 return;
727 out:
728 error_die(token->pos, "unable to open '%s'", filename);
731 static int free_preprocessor_line(struct token *token)
733 do {
734 struct token *free = token;
735 token = token->next;
736 __free_token(free);
737 } while (token_type(token) != TOKEN_EOF);
738 return 1;
741 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
743 const char *filename;
744 struct token *next;
745 int expect;
747 if (false_nesting)
748 return free_preprocessor_line(token);
750 if (stream->constant == CONSTANT_FILE_MAYBE)
751 MARK_STREAM_NONCONST(token->pos);
753 next = token->next;
754 expect = '>';
755 if (!match_op(next, '<')) {
756 expand_list(&token->next);
757 expect = 0;
758 next = token;
759 if (match_op(token->next, '<')) {
760 next = token->next;
761 expect = '>';
764 token = next->next;
765 filename = token_name_sequence(token, expect, token);
766 do_include(!expect, stream, list, token, filename, path);
767 return 1;
770 static int handle_include(struct stream *stream, struct token **list, struct token *token)
772 return handle_include_path(stream, list, token, NULL);
775 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
777 return handle_include_path(stream, list, token, stream->next_path);
780 static int token_different(struct token *t1, struct token *t2)
782 int different;
784 if (token_type(t1) != token_type(t2))
785 return 1;
787 switch (token_type(t1)) {
788 case TOKEN_IDENT:
789 different = t1->ident != t2->ident;
790 break;
791 case TOKEN_ARG_COUNT:
792 case TOKEN_UNTAINT:
793 case TOKEN_CONCAT:
794 case TOKEN_GNU_KLUDGE:
795 different = 0;
796 break;
797 case TOKEN_NUMBER:
798 different = strcmp(t1->number, t2->number);
799 break;
800 case TOKEN_SPECIAL:
801 different = t1->special != t2->special;
802 break;
803 case TOKEN_MACRO_ARGUMENT:
804 case TOKEN_QUOTED_ARGUMENT:
805 case TOKEN_STR_ARGUMENT:
806 different = t1->argnum != t2->argnum;
807 break;
808 case TOKEN_CHAR:
809 different = t1->character != t2->character;
810 break;
811 case TOKEN_STRING: {
812 struct string *s1, *s2;
814 s1 = t1->string;
815 s2 = t2->string;
816 different = 1;
817 if (s1->length != s2->length)
818 break;
819 different = memcmp(s1->data, s2->data, s1->length);
820 break;
822 default:
823 different = 1;
824 break;
826 return different;
829 static int token_list_different(struct token *list1, struct token *list2)
831 for (;;) {
832 if (list1 == list2)
833 return 0;
834 if (!list1 || !list2)
835 return 1;
836 if (token_different(list1, list2))
837 return 1;
838 list1 = list1->next;
839 list2 = list2->next;
843 static inline void set_arg_count(struct token *token)
845 token_type(token) = TOKEN_ARG_COUNT;
846 token->count.normal = token->count.quoted =
847 token->count.str = token->count.vararg = 0;
850 static struct token *parse_arguments(struct token *list)
852 struct token *arg = list->next, *next = list;
853 struct argcount *count = &list->count;
855 set_arg_count(list);
857 if (match_op(arg, ')')) {
858 next = arg->next;
859 list->next = &eof_token_entry;
860 return next;
863 while (token_type(arg) == TOKEN_IDENT) {
864 if (arg->ident == &__VA_ARGS___ident)
865 goto Eva_args;
866 if (!++count->normal)
867 goto Eargs;
868 next = arg->next;
870 if (match_op(next, ',')) {
871 set_arg_count(next);
872 arg = next->next;
873 continue;
876 if (match_op(next, ')')) {
877 set_arg_count(next);
878 next = next->next;
879 arg->next->next = &eof_token_entry;
880 return next;
883 /* normal cases are finished here */
885 if (match_op(next, SPECIAL_ELLIPSIS)) {
886 if (match_op(next->next, ')')) {
887 set_arg_count(next);
888 next->count.vararg = 1;
889 next = next->next;
890 arg->next->next = &eof_token_entry;
891 return next->next;
894 arg = next;
895 goto Enotclosed;
898 if (eof_token(next)) {
899 goto Enotclosed;
900 } else {
901 arg = next;
902 goto Ebadstuff;
906 if (match_op(arg, SPECIAL_ELLIPSIS)) {
907 next = arg->next;
908 token_type(arg) = TOKEN_IDENT;
909 arg->ident = &__VA_ARGS___ident;
910 if (!match_op(next, ')'))
911 goto Enotclosed;
912 if (!++count->normal)
913 goto Eargs;
914 set_arg_count(next);
915 next->count.vararg = 1;
916 next = next->next;
917 arg->next->next = &eof_token_entry;
918 return next;
921 if (eof_token(arg)) {
922 arg = next;
923 goto Enotclosed;
925 if (match_op(arg, ','))
926 goto Emissing;
927 else
928 goto Ebadstuff;
931 Emissing:
932 warning(arg->pos, "parameter name missing");
933 return NULL;
934 Ebadstuff:
935 warning(arg->pos, "\"%s\" may not appear in macro parameter list",
936 show_token(arg));
937 return NULL;
938 Enotclosed:
939 warning(arg->pos, "missing ')' in macro parameter list");
940 return NULL;
941 Eva_args:
942 warning(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
943 return NULL;
944 Eargs:
945 warning(arg->pos, "too many arguments in macro definition");
946 return NULL;
949 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
951 struct ident *ident = token->ident;
952 int nr;
954 if (!arglist || token_type(token) != TOKEN_IDENT)
955 return 0;
957 arglist = arglist->next;
959 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
960 if (arglist->ident == ident) {
961 struct argcount *count = &arglist->next->count;
962 int n;
964 token->argnum = nr;
965 token_type(token) = type;
966 switch (type) {
967 case TOKEN_MACRO_ARGUMENT:
968 n = ++count->normal;
969 break;
970 case TOKEN_QUOTED_ARGUMENT:
971 n = ++count->quoted;
972 break;
973 default:
974 n = ++count->str;
976 if (n)
977 return count->vararg ? 2 : 1;
978 token_type(token) = TOKEN_ERROR;
979 return -1;
982 return 0;
985 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
987 struct token *token = expansion;
988 struct token **p;
989 struct token *last = NULL;
991 if (match_op(token, SPECIAL_HASHHASH))
992 goto Econcat;
994 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
995 if (match_op(token, '#')) {
996 if (arglist) {
997 struct token *next = token->next;
998 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
999 goto Equote;
1000 next->pos.whitespace = token->pos.whitespace;
1001 token = *p = next;
1002 } else {
1003 token->pos.noexpand = 1;
1005 } else if (match_op(token, SPECIAL_HASHHASH)) {
1006 struct token *next = token->next;
1007 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
1008 token_type(token) = TOKEN_CONCAT;
1009 if (arg) {
1010 token = next;
1011 /* GNU kludge */
1012 if (arg == 2 && last && match_op(last, ',')) {
1013 token_type(last) = TOKEN_GNU_KLUDGE;
1014 last->next = token;
1016 } else if (match_op(next, SPECIAL_HASHHASH))
1017 token = next;
1018 else if (match_op(next, ','))
1019 token = next;
1020 else if (eof_token(next))
1021 goto Econcat;
1022 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
1023 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1024 } else {
1025 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1027 if (token_type(token) == TOKEN_ERROR)
1028 goto Earg;
1029 last = token;
1031 token = alloc_token(&expansion->pos);
1032 token_type(token) = TOKEN_UNTAINT;
1033 token->ident = name;
1034 token->next = *p;
1035 *p = token;
1036 return expansion;
1038 Equote:
1039 warning(token->pos, "'#' is not followed by a macro parameter");
1040 return NULL;
1042 Econcat:
1043 warning(token->pos, "'##' cannot appear at the ends of macro expansion");
1044 return NULL;
1045 Earg:
1046 warning(token->pos, "too many instances of argument in body");
1047 return NULL;
1050 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int weak)
1052 struct token *arglist, *expansion;
1053 struct token *left = token->next;
1054 struct symbol *sym;
1055 struct ident *name;
1057 if (token_type(left) != TOKEN_IDENT) {
1058 warning(token->pos, "expected identifier to 'define'");
1059 return 0;
1061 if (false_nesting)
1062 return free_preprocessor_line(token);
1064 if (stream->constant == CONSTANT_FILE_MAYBE)
1065 MARK_STREAM_NONCONST(token->pos);
1067 __free_token(token); /* Free the "define" token, but not the rest of the line */
1068 name = left->ident;
1070 arglist = NULL;
1071 expansion = left->next;
1072 if (!expansion->pos.whitespace && match_op(expansion, '(')) {
1073 arglist = expansion;
1074 expansion = parse_arguments(expansion);
1075 if (!expansion)
1076 return 1;
1079 expansion = parse_expansion(expansion, arglist, name);
1080 if (!expansion)
1081 return 1;
1083 sym = lookup_symbol(name, NS_MACRO);
1084 if (sym) {
1085 if (token_list_different(sym->expansion, expansion) ||
1086 token_list_different(sym->arglist, arglist)) {
1087 if (sym->weak)
1088 goto replace_it;
1089 if (weak)
1090 return 1;
1091 warning(left->pos, "preprocessor token %.*s redefined",
1092 name->len, name->name);
1093 info(sym->pos, "this was the original definition");
1094 sym->expansion = expansion;
1095 sym->arglist = arglist;
1097 return 1;
1099 sym = alloc_symbol(left->pos, SYM_NODE);
1100 bind_symbol(sym, name, NS_MACRO);
1102 replace_it:
1103 sym->expansion = expansion;
1104 sym->arglist = arglist;
1105 sym->weak = weak;
1106 return 1;
1109 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1111 return do_handle_define(stream, line, token, 0);
1114 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1116 return do_handle_define(stream, line, token, 1);
1119 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1121 struct token *left = token->next;
1122 struct symbol **sym;
1124 if (token_type(left) != TOKEN_IDENT) {
1125 warning(token->pos, "expected identifier to 'undef'");
1126 return 0;
1128 if (false_nesting)
1129 return free_preprocessor_line(token);
1131 if (stream->constant == CONSTANT_FILE_MAYBE)
1132 MARK_STREAM_NONCONST(token->pos);
1134 sym = &left->ident->symbols;
1135 while (*sym) {
1136 struct symbol *t = *sym;
1137 if (t->namespace == NS_MACRO) {
1138 *sym = t->next_id;
1139 return 1;
1141 sym = &t->next_id;
1143 return free_preprocessor_line(token);
1146 static int preprocessor_if(struct token *token, int true)
1148 if (if_nesting == 0)
1149 unmatched_if_pos = token->pos;
1150 if (if_nesting >= MAX_NEST)
1151 error_die(token->pos, "Maximum preprocessor conditional level exhausted");
1152 elif_ignore[if_nesting] = (false_nesting || true) ? ELIF_IGNORE : 0;
1153 if (false_nesting || !true) {
1154 false_nesting++;
1155 return free_preprocessor_line(token);
1157 true_nesting++;
1158 return free_preprocessor_line(token);
1161 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1163 return preprocessor_if(token, token_defined(token->next));
1166 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1168 struct token *next = token->next;
1169 if (stream->constant == CONSTANT_FILE_MAYBE) {
1170 if (token_type(next) == TOKEN_IDENT &&
1171 (!stream->protect || stream->protect == next->ident)) {
1172 stream->constant = CONSTANT_FILE_IFNDEF;
1173 stream->protect = next->ident;
1174 } else
1175 MARK_STREAM_NONCONST(token->pos);
1177 return preprocessor_if(token, !token_defined(next));
1181 * Expression handling for #if and #elif; it differs from normal expansion
1182 * due to special treatment of "defined".
1184 static int expression_value(struct token **where)
1186 struct expression *expr;
1187 struct token *p;
1188 struct token **list = where, **beginning = NULL;
1189 long long value;
1190 int state = 0;
1192 while (!eof_token(p = scan_next(list))) {
1193 switch (state) {
1194 case 0:
1195 if (token_type(p) != TOKEN_IDENT)
1196 break;
1197 if (p->ident == &defined_ident) {
1198 state = 1;
1199 beginning = list;
1200 break;
1202 if (!expand_one_symbol(list))
1203 continue;
1204 if (token_type(p) != TOKEN_IDENT)
1205 break;
1206 if (Wundefined_preprocessor)
1207 warning(p->pos, "undefined preprocessor identifier '%s'", show_ident(p->ident));
1208 replace_with_integer(p, 0);
1209 break;
1210 case 1:
1211 if (match_op(p, '(')) {
1212 state = 2;
1213 } else {
1214 state = 0;
1215 replace_with_defined(p);
1216 *beginning = p;
1218 break;
1219 case 2:
1220 if (token_type(p) == TOKEN_IDENT)
1221 state = 3;
1222 else
1223 state = 0;
1224 replace_with_defined(p);
1225 *beginning = p;
1226 break;
1227 case 3:
1228 state = 0;
1229 if (!match_op(p, ')'))
1230 warning(p->pos, "missing ')' after \"defined\"");
1231 *list = p->next;
1232 continue;
1234 list = &p->next;
1237 p = constant_expression(*where, &expr);
1238 if (!eof_token(p))
1239 warning(p->pos, "garbage at end: %s", show_token_sequence(p));
1240 value = get_expression_value(expr);
1241 return value != 0;
1244 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1246 int value = 0;
1247 if (!false_nesting)
1248 value = expression_value(&token->next);
1250 // This is an approximation. We really only need this if the
1251 // condition does depends on a pre-processor symbol. Note, that
1252 // the important #ifndef case has already changed ->constant.
1253 if (stream->constant == CONSTANT_FILE_MAYBE)
1254 MARK_STREAM_NONCONST(token->pos);
1256 return preprocessor_if(token, value);
1259 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1261 if (stream->nesting == if_nesting)
1262 MARK_STREAM_NONCONST(token->pos);
1264 if (stream->nesting > if_nesting)
1265 warning(token->pos, "unmatched #elif within stream");
1267 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1268 warning(token->pos, "#elif after #else");
1270 if (false_nesting) {
1271 /* If this whole if-thing is if'ed out, an elif cannot help */
1272 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1273 return 1;
1274 if (expression_value(&token->next)) {
1275 false_nesting = 0;
1276 true_nesting++;
1277 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1279 } else {
1280 false_nesting = 1;
1281 true_nesting--;
1283 return free_preprocessor_line(token);
1286 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1288 if (stream->nesting == if_nesting)
1289 MARK_STREAM_NONCONST(token->pos);
1291 if (stream->nesting > if_nesting)
1292 warning(token->pos, "unmatched #else within stream");
1294 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1295 warning(token->pos, "#else after #else");
1296 else
1297 elif_ignore[if_nesting-1] |= ELIF_SEEN_ELSE;
1299 if (false_nesting) {
1300 /* If this whole if-thing is if'ed out, an else cannot help */
1301 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1302 return 1;
1303 false_nesting = 0;
1304 true_nesting++;
1305 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1306 } else {
1307 true_nesting--;
1308 false_nesting = 1;
1310 return free_preprocessor_line(token);
1313 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1315 if (stream->constant == CONSTANT_FILE_IFNDEF && stream->nesting == if_nesting)
1316 stream->constant = CONSTANT_FILE_MAYBE;
1318 if (stream->nesting > if_nesting)
1319 warning(token->pos, "unmatched #endif in stream");
1320 if (false_nesting)
1321 false_nesting--;
1322 else
1323 true_nesting--;
1324 if (!token)
1325 return 1;
1326 return free_preprocessor_line(token);
1329 static const char *show_token_sequence(struct token *token)
1331 static char buffer[1024];
1332 char *ptr = buffer;
1333 int whitespace = 0;
1335 if (!token)
1336 return "<none>";
1337 while (!eof_token(token)) {
1338 const char *val = show_token(token);
1339 int len = strlen(val);
1341 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1342 warning(token->pos, "too long token expansion");
1343 break;
1346 if (whitespace)
1347 *ptr++ = ' ';
1348 memcpy(ptr, val, len);
1349 ptr += len;
1350 token = token->next;
1351 whitespace = token->pos.whitespace;
1353 *ptr = 0;
1354 return buffer;
1357 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1359 if (false_nesting)
1360 return free_preprocessor_line(token);
1361 if (stream->constant == CONSTANT_FILE_MAYBE)
1362 MARK_STREAM_NONCONST(token->pos);
1363 warning(token->pos, "%s", show_token_sequence(token->next));
1364 return free_preprocessor_line(token);
1367 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1369 if (false_nesting)
1370 return free_preprocessor_line(token);
1371 if (stream->constant == CONSTANT_FILE_MAYBE)
1372 MARK_STREAM_NONCONST(token->pos);
1373 warning(token->pos, "%s", show_token_sequence(token->next));
1374 return free_preprocessor_line(token);
1377 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1379 if (false_nesting)
1380 return free_preprocessor_line(token);
1383 * Do we have any non-system includes?
1384 * Clear them out if so..
1386 *sys_includepath = NULL;
1387 return free_preprocessor_line(token);
1390 static void add_path_entry(struct token *token, const char *path, const char ***where, const char **new_path)
1392 const char **dst;
1393 const char *next;
1395 /* Need one free entry.. */
1396 if (includepath[INCLUDEPATHS-2])
1397 error_die(token->pos, "too many include path entries");
1399 /* check that this is not a duplicate */
1400 dst = includepath;
1401 while (*dst) {
1402 if (strcmp(*dst, path) == 0)
1403 return;
1404 dst++;
1406 next = path;
1407 dst = *where;
1408 *where = new_path;
1411 * Move them all up starting at dst,
1412 * insert the new entry..
1414 for (;;) {
1415 const char *tmp = *dst;
1416 *dst = next;
1417 if (!next)
1418 break;
1419 next = tmp;
1420 dst++;
1424 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1426 for (;;) {
1427 token = token->next;
1428 if (eof_token(token))
1429 return 1;
1430 if (token_type(token) != TOKEN_STRING) {
1431 warning(token->pos, "expected path string");
1432 return 1;
1434 add_path_entry(token, token->string->data, &sys_includepath, sys_includepath + 1);
1438 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1440 for (;;) {
1441 token = token->next;
1442 if (eof_token(token))
1443 return 1;
1444 if (token_type(token) != TOKEN_STRING) {
1445 warning(token->pos, "expected path string");
1446 return 1;
1448 add_path_entry(token, token->string->data, &sys_includepath, sys_includepath);
1452 /* Add to end on includepath list - no pointer updates */
1453 static void add_dirafter_entry(struct token *token, const char *path)
1455 const char **dst = includepath;
1457 /* Need one free entry.. */
1458 if (includepath[INCLUDEPATHS-2])
1459 error_die(token->pos, "too many include path entries");
1461 /* Add to the end */
1462 while (*dst)
1463 dst++;
1464 *dst = path;
1465 dst++;
1466 *dst = NULL;
1469 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1471 for (;;) {
1472 token = token->next;
1473 if (eof_token(token))
1474 return 1;
1475 if (token_type(token) != TOKEN_STRING) {
1476 warning(token->pos, "expected path string");
1477 return 1;
1479 add_dirafter_entry(token, token->string->data);
1483 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1485 if (false_nesting)
1486 return free_preprocessor_line(token);
1489 * -I-
1490 * From info gcc:
1491 * Split the include path. Any directories specified with `-I'
1492 * options before `-I-' are searched only for headers requested with
1493 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1494 * If additional directories are specified with `-I' options after
1495 * the `-I-', those directories are searched for all `#include'
1496 * directives.
1497 * In addition, `-I-' inhibits the use of the directory of the current
1498 * file directory as the first search directory for `#include "FILE"'.
1500 quote_includepath = includepath+1;
1501 angle_includepath = sys_includepath;
1502 return free_preprocessor_line(token);
1506 * We replace "#pragma xxx" with "__pragma__" in the token
1507 * stream. Just as an example.
1509 * We'll just #define that away for now, but the theory here
1510 * is that we can use this to insert arbitrary token sequences
1511 * to turn the pragma's into internal front-end sequences for
1512 * when we actually start caring about them.
1514 * So eventually this will turn into some kind of extended
1515 * __attribute__() like thing, except called __pragma__(xxx).
1517 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1519 struct token *next = *line;
1521 token->ident = &pragma_ident;
1522 token->pos.newline = 1;
1523 token->pos.whitespace = 1;
1524 token->pos.pos = 1;
1525 *line = token;
1526 token->next = next;
1527 return 1;
1531 * We ignore #line for now.
1533 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1535 return 1;
1539 static void init_preprocessor(void)
1541 int i;
1542 int stream = init_stream("preprocessor", -1, includepath);
1543 static struct {
1544 const char *name;
1545 int (*handler)(struct stream *, struct token **, struct token *);
1546 } handlers[] = {
1547 { "define", handle_define },
1548 { "weak_define",handle_weak_define },
1549 { "undef", handle_undef },
1550 { "ifdef", handle_ifdef },
1551 { "ifndef", handle_ifndef },
1552 { "else", handle_else },
1553 { "endif", handle_endif },
1554 { "if", handle_if },
1555 { "elif", handle_elif },
1556 { "warning", handle_warning },
1557 { "error", handle_error },
1558 { "include", handle_include },
1559 { "include_next",handle_include_next },
1560 { "pragma", handle_pragma },
1561 { "line", handle_line },
1563 // our internal preprocessor tokens
1564 { "nostdinc", handle_nostdinc },
1565 { "add_include", handle_add_include },
1566 { "add_isystem", handle_add_isystem },
1567 { "add_dirafter", handle_add_dirafter },
1568 { "split_include", handle_split_include },
1571 for (i = 0; i < (sizeof (handlers) / sizeof (handlers[0])); i++) {
1572 struct symbol *sym;
1573 sym = create_symbol(stream, handlers[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1574 sym->handler = handlers[i].handler;
1578 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1580 struct token *token = start->next;
1582 if (!token)
1583 return;
1585 if (token_type(token) == TOKEN_NUMBER)
1586 if (handle_line(stream, line, start))
1587 return;
1589 if (token_type(token) == TOKEN_IDENT) {
1590 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1591 if (sym && sym->handler(stream, line, token))
1592 return;
1595 warning(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1598 static void preprocessor_line(struct stream *stream, struct token **line)
1600 struct token *start = *line, *next;
1601 struct token **tp = &start->next;
1603 for (;;) {
1604 next = *tp;
1605 if (next->pos.newline)
1606 break;
1607 tp = &next->next;
1609 *line = next;
1610 *tp = &eof_token_entry;
1611 handle_preprocessor_line(stream, line, start);
1614 static void do_preprocess(struct token **list)
1616 struct token *next;
1618 while (!eof_token(next = scan_next(list))) {
1619 struct stream *stream = input_streams + next->pos.stream;
1621 if (next->pos.newline && match_op(next, '#')) {
1622 if (!next->pos.noexpand) {
1623 preprocessor_line(stream, list);
1624 __free_token(next); /* Free the '#' token */
1625 continue;
1629 switch (token_type(next)) {
1630 case TOKEN_STREAMEND:
1631 if (stream->nesting < if_nesting + 1) {
1632 warning(unmatched_if_pos, "unterminated preprocessor conditional");
1633 // Pretend to see a series of #endifs
1634 MARK_STREAM_NONCONST(next->pos);
1635 do {
1636 handle_endif (stream, NULL, NULL);
1637 } while (stream->nesting < if_nesting + 1);
1639 if (stream->constant == CONSTANT_FILE_MAYBE && stream->protect) {
1640 stream->constant = CONSTANT_FILE_YES;
1642 *list = next->next;
1643 continue;
1644 case TOKEN_STREAMBEGIN:
1645 stream->nesting = if_nesting + 1;
1646 *list = next->next;
1647 continue;
1649 default:
1650 if (false_nesting) {
1651 *list = next->next;
1652 __free_token(next);
1653 continue;
1656 if (token_type(next) != TOKEN_IDENT ||
1657 expand_one_symbol(list))
1658 list = &next->next;
1661 if (stream->constant == CONSTANT_FILE_MAYBE) {
1663 * Any token expansion (even if it ended up being an
1664 * empty expansion) in this stream implies it can't
1665 * be constant.
1667 MARK_STREAM_NONCONST(next->pos);
1672 struct token * preprocess(struct token *token)
1674 preprocessing = 1;
1675 init_preprocessor();
1676 do_preprocess(&token);
1678 // Drop all expressions from pre-processing, they're not used any more.
1679 clear_expression_alloc();
1680 preprocessing = 0;
1682 return token;