Warn about implicit casts to/from "nocast" types
[smatch.git] / pre-process.c
blobebc689106d53c21676f325dbeabf8bcb7cb86f20
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 "",
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 extern unsigned char combinations[][3];
387 static char buffer[512];
388 int n;
390 switch (combine(left, right, buffer)) {
391 case TOKEN_IDENT:
392 left->ident = built_in_ident(buffer);
393 left->pos.noexpand = 0;
394 return 1;
396 case TOKEN_NUMBER: {
397 char *number = __alloc_bytes(strlen(buffer) + 1);
398 memcpy(number, buffer, strlen(buffer) + 1);
399 token_type(left) = TOKEN_NUMBER; /* could be . + num */
400 left->number = number;
401 return 1;
404 case TOKEN_SPECIAL:
405 if (buffer[2] && buffer[3])
406 break;
407 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
408 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
409 left->special = n;
410 return 1;
413 default:
416 warning(left->pos, "'##' failed: concatenation is not a valid token");
417 return 0;
420 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
422 struct token *alloc = alloc_token(streampos);
423 token_type(alloc) = token_type(token);
424 alloc->pos.newline = pos->newline;
425 alloc->pos.whitespace = pos->whitespace;
426 alloc->number = token->number;
427 alloc->pos.noexpand = token->pos.noexpand;
428 return alloc;
431 static struct token **copy(struct token **where, struct token *list, int *count)
433 int need_copy = --*count;
434 while (!eof_token(list)) {
435 struct token *token;
436 if (need_copy)
437 token = dup_token(list, &list->pos, &list->pos);
438 else
439 token = list;
440 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
441 token->pos.noexpand = 1;
442 *where = token;
443 where = &token->next;
444 list = list->next;
446 *where = &eof_token_entry;
447 return where;
450 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
452 struct token *token = *list;
453 struct position *base_pos = &token->pos;
454 struct position *pos = base_pos;
455 int *count;
456 enum {Normal, Placeholder, Concat} state = Normal;
458 for (; !eof_token(body); body = body->next, pos = &body->pos) {
459 struct token *added, *arg;
460 struct token **tail;
462 switch (token_type(body)) {
463 case TOKEN_GNU_KLUDGE:
465 * GNU kludge: if we had <comma>##<vararg>, behaviour
466 * depends on whether we had enough arguments to have
467 * a vararg. If we did, ## is just ignored. Otherwise
468 * both , and ## are ignored. Comma should come from
469 * the body of macro and not be an argument of earlier
470 * concatenation.
472 if (!args[body->next->argnum].arg)
473 continue;
474 added = dup_token(body, base_pos, pos);
475 token_type(added) = TOKEN_SPECIAL;
476 tail = &added->next;
477 break;
479 case TOKEN_STR_ARGUMENT:
480 arg = args[body->argnum].str;
481 count = &args[body->argnum].n_str;
482 goto copy_arg;
484 case TOKEN_QUOTED_ARGUMENT:
485 arg = args[body->argnum].arg;
486 count = &args[body->argnum].n_quoted;
487 if (!arg || eof_token(arg)) {
488 if (state == Concat)
489 state = Normal;
490 else
491 state = Placeholder;
492 continue;
494 goto copy_arg;
496 case TOKEN_MACRO_ARGUMENT:
497 arg = args[body->argnum].expanded;
498 count = &args[body->argnum].n_normal;
499 if (eof_token(arg)) {
500 state = Normal;
501 continue;
503 copy_arg:
504 tail = copy(&added, arg, count);
505 added->pos.newline = pos->newline;
506 added->pos.whitespace = pos->whitespace;
507 break;
509 case TOKEN_CONCAT:
510 if (state == Placeholder)
511 state = Normal;
512 else
513 state = Concat;
514 continue;
516 case TOKEN_IDENT:
517 added = dup_token(body, base_pos, pos);
518 if (added->ident->tainted)
519 added->pos.noexpand = 1;
520 tail = &added->next;
521 break;
523 default:
524 added = dup_token(body, base_pos, pos);
525 tail = &added->next;
526 break;
530 * if we got to doing real concatenation, we already have
531 * added something into the list, so containing_token() is OK.
533 if (state == Concat && merge(containing_token(list), added)) {
534 *list = added->next;
535 if (tail != &added->next)
536 list = tail;
537 } else {
538 *list = added;
539 list = tail;
541 state = Normal;
543 *list = &eof_token_entry;
544 return list;
547 static int expand(struct token **list, struct symbol *sym)
549 struct token *last;
550 struct token *token = *list;
551 struct ident *expanding = token->ident;
552 struct token **tail;
553 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
554 struct arg args[nargs];
556 if (expanding->tainted) {
557 token->pos.noexpand = 1;
558 return 1;
561 if (sym->arglist) {
562 if (!match_op(scan_next(&token->next), '('))
563 return 1;
564 if (!collect_arguments(token->next, sym->arglist, args, token))
565 return 1;
566 expand_arguments(nargs, args);
569 expanding->tainted = 1;
571 last = token->next;
572 tail = substitute(list, sym->expansion, args);
573 *tail = last;
575 return 0;
578 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
580 struct token *last;
581 static char buffer[256];
582 char *ptr = buffer;
584 last = token;
585 while (!eof_token(token) && !match_op(token, endop)) {
586 int len;
587 const char *val = token->string->data;
588 if (token_type(token) != TOKEN_STRING)
589 val = show_token(token);
590 len = strlen(val);
591 memcpy(ptr, val, len);
592 ptr += len;
593 token = token->next;
595 *ptr = 0;
596 if (endop && !match_op(token, endop))
597 warning(start->pos, "expected '>' at end of filename");
598 return buffer;
601 static int already_tokenized(const char *path)
603 int i;
604 struct stream *s = input_streams;
606 for (i = input_stream_nr; --i >= 0; s++) {
607 if (s->constant != CONSTANT_FILE_YES)
608 continue;
609 if (strcmp(path, s->name))
610 continue;
611 if (!lookup_symbol(s->protect, NS_MACRO))
612 continue;
613 return 1;
615 return 0;
618 /* Hande include of header files.
619 * The relevant options are made compatible with gcc. The only options that
620 * are not supported is -withprefix and friends.
622 * Three set of include paths are known:
623 * quote_includepath: Path to search when using #include "file.h"
624 * angle_includepath: Path to search when using #include <file.h>
625 * sys_includepath: Built-in include paths
627 * The above is implmented as one array with pointes
628 * +--------------+
629 * quote_includepath ---> | |
630 * +--------------+
631 * | |
632 * +--------------+
633 * angle_includepath ---> | |
634 * +--------------+
635 * sys_includepath ---> | |
636 * +--------------+
637 * | |
638 * +--------------+
640 * -I dir insert dir just before sys_includepath and move the rest
641 * -I- makes all dirs specified with -I before to quote dirs only and
642 * angle_includepath is set equal to sys_includepath.
643 * -nostdinc removes all sys dirs be storing NULL in entry pointed
644 * to by * sys_includepath. Note this will reset all dirs built-in and added
645 * before -nostdinc by -isystem and -dirafter
646 * -isystem dir adds dir where sys_includepath points adding this dir as
647 * first systemdir
648 * -dirafter dir adds dir to the end of the list
651 static void set_stream_include_path(struct stream *stream)
653 const char *path = stream->path;
654 if (!path) {
655 const char *p = strrchr(stream->name, '/');
656 path = "";
657 if (p) {
658 int len = p - stream->name + 1;
659 char *m = malloc(len+1);
660 /* This includes the final "/" */
661 memcpy(m, stream->name, len);
662 m[len] = 0;
663 path = m;
665 stream->path = path;
667 includepath[0] = path;
670 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
672 int fd;
673 int plen = strlen(path);
674 static char fullname[PATH_MAX];
676 memcpy(fullname, path, plen);
677 if (plen && path[plen-1] != '/') {
678 fullname[plen] = '/';
679 plen++;
681 memcpy(fullname+plen, filename, flen);
682 if (already_tokenized(fullname))
683 return 1;
684 fd = open(fullname, O_RDONLY);
685 if (fd >= 0) {
686 char * streamname = __alloc_bytes(plen + flen);
687 memcpy(streamname, fullname, plen + flen);
688 *where = tokenize(streamname, fd, *where, next_path);
689 close(fd);
690 return 1;
692 return 0;
695 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
697 const char *path;
699 while ((path = *pptr++) != NULL) {
700 if (!try_include(path, filename, flen, list, pptr))
701 continue;
702 return 1;
704 return 0;
707 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
709 int flen = strlen(filename) + 1;
711 /* Absolute path? */
712 if (filename[0] == '/') {
713 if (try_include("", filename, flen, list, includepath))
714 return;
715 goto out;
718 /* Dir of inputfile is first dir to search for quoted includes */
719 set_stream_include_path(stream);
721 if (!path)
722 /* Do not search quote include if <> is in use */
723 path = local ? quote_includepath : angle_includepath;
725 /* Check the standard include paths.. */
726 if (do_include_path(path, list, token, filename, flen))
727 return;
728 out:
729 error_die(token->pos, "unable to open '%s'", filename);
732 static int free_preprocessor_line(struct token *token)
734 do {
735 struct token *free = token;
736 token = token->next;
737 __free_token(free);
738 } while (token_type(token) != TOKEN_EOF);
739 return 1;
742 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
744 const char *filename;
745 struct token *next;
746 int expect;
748 if (false_nesting)
749 return free_preprocessor_line(token);
751 if (stream->constant == CONSTANT_FILE_MAYBE)
752 MARK_STREAM_NONCONST(token->pos);
754 next = token->next;
755 expect = '>';
756 if (!match_op(next, '<')) {
757 expand_list(&token->next);
758 expect = 0;
759 next = token;
760 if (match_op(token->next, '<')) {
761 next = token->next;
762 expect = '>';
765 token = next->next;
766 filename = token_name_sequence(token, expect, token);
767 do_include(!expect, stream, list, token, filename, path);
768 return 1;
771 static int handle_include(struct stream *stream, struct token **list, struct token *token)
773 return handle_include_path(stream, list, token, NULL);
776 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
778 return handle_include_path(stream, list, token, stream->next_path);
781 static int token_different(struct token *t1, struct token *t2)
783 int different;
785 if (token_type(t1) != token_type(t2))
786 return 1;
788 switch (token_type(t1)) {
789 case TOKEN_IDENT:
790 different = t1->ident != t2->ident;
791 break;
792 case TOKEN_ARG_COUNT:
793 case TOKEN_UNTAINT:
794 case TOKEN_CONCAT:
795 case TOKEN_GNU_KLUDGE:
796 different = 0;
797 break;
798 case TOKEN_NUMBER:
799 different = strcmp(t1->number, t2->number);
800 break;
801 case TOKEN_SPECIAL:
802 different = t1->special != t2->special;
803 break;
804 case TOKEN_MACRO_ARGUMENT:
805 case TOKEN_QUOTED_ARGUMENT:
806 case TOKEN_STR_ARGUMENT:
807 different = t1->argnum != t2->argnum;
808 break;
809 case TOKEN_CHAR:
810 different = t1->character != t2->character;
811 break;
812 case TOKEN_STRING: {
813 struct string *s1, *s2;
815 s1 = t1->string;
816 s2 = t2->string;
817 different = 1;
818 if (s1->length != s2->length)
819 break;
820 different = memcmp(s1->data, s2->data, s1->length);
821 break;
823 default:
824 different = 1;
825 break;
827 return different;
830 static int token_list_different(struct token *list1, struct token *list2)
832 for (;;) {
833 if (list1 == list2)
834 return 0;
835 if (!list1 || !list2)
836 return 1;
837 if (token_different(list1, list2))
838 return 1;
839 list1 = list1->next;
840 list2 = list2->next;
844 static inline void set_arg_count(struct token *token)
846 token_type(token) = TOKEN_ARG_COUNT;
847 token->count.normal = token->count.quoted =
848 token->count.str = token->count.vararg = 0;
851 static struct token *parse_arguments(struct token *list)
853 struct token *arg = list->next, *next = list;
854 struct argcount *count = &list->count;
856 set_arg_count(list);
858 if (match_op(arg, ')')) {
859 next = arg->next;
860 list->next = &eof_token_entry;
861 return next;
864 while (token_type(arg) == TOKEN_IDENT) {
865 if (arg->ident == &__VA_ARGS___ident)
866 goto Eva_args;
867 if (!++count->normal)
868 goto Eargs;
869 next = arg->next;
871 if (match_op(next, ',')) {
872 set_arg_count(next);
873 arg = next->next;
874 continue;
877 if (match_op(next, ')')) {
878 set_arg_count(next);
879 next = next->next;
880 arg->next->next = &eof_token_entry;
881 return next;
884 /* normal cases are finished here */
886 if (match_op(next, SPECIAL_ELLIPSIS)) {
887 if (match_op(next->next, ')')) {
888 set_arg_count(next);
889 next->count.vararg = 1;
890 next = next->next;
891 arg->next->next = &eof_token_entry;
892 return next->next;
895 arg = next;
896 goto Enotclosed;
899 if (eof_token(next)) {
900 goto Enotclosed;
901 } else {
902 arg = next;
903 goto Ebadstuff;
907 if (match_op(arg, SPECIAL_ELLIPSIS)) {
908 next = arg->next;
909 token_type(arg) = TOKEN_IDENT;
910 arg->ident = &__VA_ARGS___ident;
911 if (!match_op(next, ')'))
912 goto Enotclosed;
913 if (!++count->normal)
914 goto Eargs;
915 set_arg_count(next);
916 next->count.vararg = 1;
917 next = next->next;
918 arg->next->next = &eof_token_entry;
919 return next;
922 if (eof_token(arg)) {
923 arg = next;
924 goto Enotclosed;
926 if (match_op(arg, ','))
927 goto Emissing;
928 else
929 goto Ebadstuff;
932 Emissing:
933 warning(arg->pos, "parameter name missing");
934 return NULL;
935 Ebadstuff:
936 warning(arg->pos, "\"%s\" may not appear in macro parameter list",
937 show_token(arg));
938 return NULL;
939 Enotclosed:
940 warning(arg->pos, "missing ')' in macro parameter list");
941 return NULL;
942 Eva_args:
943 warning(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
944 return NULL;
945 Eargs:
946 warning(arg->pos, "too many arguments in macro definition");
947 return NULL;
950 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
952 struct ident *ident = token->ident;
953 int nr;
955 if (!arglist || token_type(token) != TOKEN_IDENT)
956 return 0;
958 arglist = arglist->next;
960 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
961 if (arglist->ident == ident) {
962 struct argcount *count = &arglist->next->count;
963 int n;
965 token->argnum = nr;
966 token_type(token) = type;
967 switch (type) {
968 case TOKEN_MACRO_ARGUMENT:
969 n = ++count->normal;
970 break;
971 case TOKEN_QUOTED_ARGUMENT:
972 n = ++count->quoted;
973 break;
974 default:
975 n = ++count->str;
977 if (n)
978 return count->vararg ? 2 : 1;
979 token_type(token) = TOKEN_ERROR;
980 return -1;
983 return 0;
986 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
988 struct token *token = expansion;
989 struct token **p;
990 struct token *last = NULL;
992 if (match_op(token, SPECIAL_HASHHASH))
993 goto Econcat;
995 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
996 if (match_op(token, '#')) {
997 if (arglist) {
998 struct token *next = token->next;
999 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1000 goto Equote;
1001 next->pos.whitespace = token->pos.whitespace;
1002 token = *p = next;
1003 } else {
1004 token->pos.noexpand = 1;
1006 } else if (match_op(token, SPECIAL_HASHHASH)) {
1007 struct token *next = token->next;
1008 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
1009 token_type(token) = TOKEN_CONCAT;
1010 if (arg) {
1011 token = next;
1012 /* GNU kludge */
1013 if (arg == 2 && last && match_op(last, ',')) {
1014 token_type(last) = TOKEN_GNU_KLUDGE;
1015 last->next = token;
1017 } else if (match_op(next, SPECIAL_HASHHASH))
1018 token = next;
1019 else if (match_op(next, ','))
1020 token = next;
1021 else if (eof_token(next))
1022 goto Econcat;
1023 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
1024 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1025 } else {
1026 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1028 if (token_type(token) == TOKEN_ERROR)
1029 goto Earg;
1030 last = token;
1032 token = alloc_token(&expansion->pos);
1033 token_type(token) = TOKEN_UNTAINT;
1034 token->ident = name;
1035 token->next = *p;
1036 *p = token;
1037 return expansion;
1039 Equote:
1040 warning(token->pos, "'#' is not followed by a macro parameter");
1041 return NULL;
1043 Econcat:
1044 warning(token->pos, "'##' cannot appear at the ends of macro expansion");
1045 return NULL;
1046 Earg:
1047 warning(token->pos, "too many instances of argument in body");
1048 return NULL;
1051 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int weak)
1053 struct token *arglist, *expansion;
1054 struct token *left = token->next;
1055 struct symbol *sym;
1056 struct ident *name;
1058 if (token_type(left) != TOKEN_IDENT) {
1059 warning(token->pos, "expected identifier to 'define'");
1060 return 0;
1062 if (false_nesting)
1063 return free_preprocessor_line(token);
1065 if (stream->constant == CONSTANT_FILE_MAYBE)
1066 MARK_STREAM_NONCONST(token->pos);
1068 __free_token(token); /* Free the "define" token, but not the rest of the line */
1069 name = left->ident;
1071 arglist = NULL;
1072 expansion = left->next;
1073 if (!expansion->pos.whitespace && match_op(expansion, '(')) {
1074 arglist = expansion;
1075 expansion = parse_arguments(expansion);
1076 if (!expansion)
1077 return 1;
1080 expansion = parse_expansion(expansion, arglist, name);
1081 if (!expansion)
1082 return 1;
1084 sym = lookup_symbol(name, NS_MACRO);
1085 if (sym) {
1086 if (token_list_different(sym->expansion, expansion) ||
1087 token_list_different(sym->arglist, arglist)) {
1088 if (sym->weak)
1089 goto replace_it;
1090 if (weak)
1091 return 1;
1092 warning(left->pos, "preprocessor token %.*s redefined",
1093 name->len, name->name);
1094 info(sym->pos, "this was the original definition");
1095 sym->expansion = expansion;
1096 sym->arglist = arglist;
1098 return 1;
1100 sym = alloc_symbol(left->pos, SYM_NODE);
1101 bind_symbol(sym, name, NS_MACRO);
1103 replace_it:
1104 sym->expansion = expansion;
1105 sym->arglist = arglist;
1106 sym->weak = weak;
1107 return 1;
1110 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1112 return do_handle_define(stream, line, token, 0);
1115 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1117 return do_handle_define(stream, line, token, 1);
1120 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1122 struct token *left = token->next;
1123 struct symbol **sym;
1125 if (token_type(left) != TOKEN_IDENT) {
1126 warning(token->pos, "expected identifier to 'undef'");
1127 return 0;
1129 if (false_nesting)
1130 return free_preprocessor_line(token);
1132 if (stream->constant == CONSTANT_FILE_MAYBE)
1133 MARK_STREAM_NONCONST(token->pos);
1135 sym = &left->ident->symbols;
1136 while (*sym) {
1137 struct symbol *t = *sym;
1138 if (t->namespace == NS_MACRO) {
1139 *sym = t->next_id;
1140 return 1;
1142 sym = &t->next_id;
1144 return free_preprocessor_line(token);
1147 static int preprocessor_if(struct token *token, int true)
1149 if (if_nesting == 0)
1150 unmatched_if = token;
1151 if (if_nesting >= MAX_NEST)
1152 error_die(token->pos, "Maximum preprocessor conditional level exhausted");
1153 elif_ignore[if_nesting] = (false_nesting || true) ? ELIF_IGNORE : 0;
1154 if (false_nesting || !true) {
1155 false_nesting++;
1156 return free_preprocessor_line(token);
1158 true_nesting++;
1159 return free_preprocessor_line(token);
1162 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1164 return preprocessor_if(token, token_defined(token->next));
1167 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1169 struct token *next = token->next;
1170 if (stream->constant == CONSTANT_FILE_MAYBE) {
1171 if (token_type(next) == TOKEN_IDENT &&
1172 (!stream->protect || stream->protect == next->ident)) {
1173 stream->constant = CONSTANT_FILE_IFNDEF;
1174 stream->protect = next->ident;
1175 } else
1176 MARK_STREAM_NONCONST(token->pos);
1178 return preprocessor_if(token, !token_defined(next));
1182 * Expression handling for #if and #elif; it differs from normal expansion
1183 * due to special treatment of "defined".
1185 static int expression_value(struct token **where)
1187 struct expression *expr;
1188 struct token *p;
1189 struct token **list = where, **beginning = NULL;
1190 long long value;
1191 int state = 0;
1193 while (!eof_token(p = scan_next(list))) {
1194 switch (state) {
1195 case 0:
1196 if (token_type(p) != TOKEN_IDENT)
1197 break;
1198 if (p->ident == &defined_ident) {
1199 state = 1;
1200 beginning = list;
1201 break;
1203 if (!expand_one_symbol(list))
1204 continue;
1205 if (token_type(p) != TOKEN_IDENT)
1206 break;
1207 if (Wundefined_preprocessor)
1208 warning(p->pos, "undefined preprocessor identifier '%s'", show_ident(p->ident));
1209 replace_with_integer(p, 0);
1210 break;
1211 case 1:
1212 if (match_op(p, '(')) {
1213 state = 2;
1214 } else {
1215 state = 0;
1216 replace_with_defined(p);
1217 *beginning = p;
1219 break;
1220 case 2:
1221 if (token_type(p) == TOKEN_IDENT)
1222 state = 3;
1223 else
1224 state = 0;
1225 replace_with_defined(p);
1226 *beginning = p;
1227 break;
1228 case 3:
1229 state = 0;
1230 if (!match_op(p, ')'))
1231 warning(p->pos, "missing ')' after \"defined\"");
1232 *list = p->next;
1233 continue;
1235 list = &p->next;
1238 p = constant_expression(*where, &expr);
1239 if (!eof_token(p))
1240 warning(p->pos, "garbage at end: %s", show_token_sequence(p));
1241 value = get_expression_value(expr);
1242 return value != 0;
1245 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1247 int value = 0;
1248 if (!false_nesting)
1249 value = expression_value(&token->next);
1251 // This is an approximation. We really only need this if the
1252 // condition does depends on a pre-processor symbol. Note, that
1253 // the important #ifndef case has already changed ->constant.
1254 if (stream->constant == CONSTANT_FILE_MAYBE)
1255 MARK_STREAM_NONCONST(token->pos);
1257 return preprocessor_if(token, value);
1260 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1262 if (stream->nesting == if_nesting)
1263 MARK_STREAM_NONCONST(token->pos);
1265 if (stream->nesting > if_nesting)
1266 warning(token->pos, "unmatched #elif within stream");
1268 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1269 warning(token->pos, "#elif after #else");
1271 if (false_nesting) {
1272 /* If this whole if-thing is if'ed out, an elif cannot help */
1273 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1274 return 1;
1275 if (expression_value(&token->next)) {
1276 false_nesting = 0;
1277 true_nesting++;
1278 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1280 } else {
1281 false_nesting = 1;
1282 true_nesting--;
1284 return free_preprocessor_line(token);
1287 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1289 if (stream->nesting == if_nesting)
1290 MARK_STREAM_NONCONST(token->pos);
1292 if (stream->nesting > if_nesting)
1293 warning(token->pos, "unmatched #else within stream");
1295 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1296 warning(token->pos, "#else after #else");
1297 else
1298 elif_ignore[if_nesting-1] |= ELIF_SEEN_ELSE;
1300 if (false_nesting) {
1301 /* If this whole if-thing is if'ed out, an else cannot help */
1302 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1303 return 1;
1304 false_nesting = 0;
1305 true_nesting++;
1306 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1307 } else {
1308 true_nesting--;
1309 false_nesting = 1;
1311 return free_preprocessor_line(token);
1314 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1316 if (stream->constant == CONSTANT_FILE_IFNDEF && stream->nesting == if_nesting)
1317 stream->constant = CONSTANT_FILE_MAYBE;
1319 if (stream->nesting > if_nesting)
1320 warning(token->pos, "unmatched #endif in stream");
1321 if (false_nesting)
1322 false_nesting--;
1323 else
1324 true_nesting--;
1325 return free_preprocessor_line(token);
1328 static const char *show_token_sequence(struct token *token)
1330 static char buffer[1024];
1331 char *ptr = buffer;
1332 int whitespace = 0;
1334 if (!token)
1335 return "<none>";
1336 while (!eof_token(token)) {
1337 const char *val = show_token(token);
1338 int len = strlen(val);
1340 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1341 warning(token->pos, "too long token expansion");
1342 break;
1345 if (whitespace)
1346 *ptr++ = ' ';
1347 memcpy(ptr, val, len);
1348 ptr += len;
1349 token = token->next;
1350 whitespace = token->pos.whitespace;
1352 *ptr = 0;
1353 return buffer;
1356 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1358 if (false_nesting)
1359 return free_preprocessor_line(token);
1360 if (stream->constant == CONSTANT_FILE_MAYBE)
1361 MARK_STREAM_NONCONST(token->pos);
1362 warning(token->pos, "%s", show_token_sequence(token->next));
1363 return free_preprocessor_line(token);
1366 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1368 if (false_nesting)
1369 return free_preprocessor_line(token);
1370 if (stream->constant == CONSTANT_FILE_MAYBE)
1371 MARK_STREAM_NONCONST(token->pos);
1372 warning(token->pos, "%s", show_token_sequence(token->next));
1373 return free_preprocessor_line(token);
1376 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1378 if (false_nesting)
1379 return free_preprocessor_line(token);
1382 * Do we have any non-system includes?
1383 * Clear them out if so..
1385 *sys_includepath = NULL;
1386 return free_preprocessor_line(token);
1389 static void add_path_entry(struct token *token, const char *path, const char ***where, const char **new_path)
1391 const char **dst;
1392 const char *next;
1394 /* Need one free entry.. */
1395 if (includepath[INCLUDEPATHS-2])
1396 error_die(token->pos, "too many include path entries");
1398 /* check that this is not a duplicate */
1399 dst = includepath;
1400 while (*dst) {
1401 if (strcmp(*dst, path) == 0)
1402 return;
1403 dst++;
1405 next = path;
1406 dst = *where;
1407 *where = new_path;
1410 * Move them all up starting at dst,
1411 * insert the new entry..
1413 for (;;) {
1414 const char *tmp = *dst;
1415 *dst = next;
1416 if (!next)
1417 break;
1418 next = tmp;
1419 dst++;
1423 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1425 for (;;) {
1426 token = token->next;
1427 if (eof_token(token))
1428 return 1;
1429 if (token_type(token) != TOKEN_STRING) {
1430 warning(token->pos, "expected path string");
1431 return 1;
1433 add_path_entry(token, token->string->data, &sys_includepath, sys_includepath + 1);
1437 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1439 for (;;) {
1440 token = token->next;
1441 if (eof_token(token))
1442 return 1;
1443 if (token_type(token) != TOKEN_STRING) {
1444 warning(token->pos, "expected path string");
1445 return 1;
1447 add_path_entry(token, token->string->data, &sys_includepath, sys_includepath);
1451 /* Add to end on includepath list - no pointer updates */
1452 static void add_dirafter_entry(struct token *token, const char *path)
1454 const char **dst = includepath;
1456 /* Need one free entry.. */
1457 if (includepath[INCLUDEPATHS-2])
1458 error_die(token->pos, "too many include path entries");
1460 /* Add to the end */
1461 while (*dst)
1462 dst++;
1463 *dst = path;
1464 dst++;
1465 *dst = NULL;
1468 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1470 for (;;) {
1471 token = token->next;
1472 if (eof_token(token))
1473 return 1;
1474 if (token_type(token) != TOKEN_STRING) {
1475 warning(token->pos, "expected path string");
1476 return 1;
1478 add_dirafter_entry(token, token->string->data);
1482 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1484 if (false_nesting)
1485 return free_preprocessor_line(token);
1488 * -I-
1489 * From info gcc:
1490 * Split the include path. Any directories specified with `-I'
1491 * options before `-I-' are searched only for headers requested with
1492 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1493 * If additional directories are specified with `-I' options after
1494 * the `-I-', those directories are searched for all `#include'
1495 * directives.
1496 * In addition, `-I-' inhibits the use of the directory of the current
1497 * file directory as the first search directory for `#include "FILE"'.
1499 quote_includepath = includepath+1;
1500 angle_includepath = sys_includepath;
1501 return free_preprocessor_line(token);
1505 * We replace "#pragma xxx" with "__pragma__" in the token
1506 * stream. Just as an example.
1508 * We'll just #define that away for now, but the theory here
1509 * is that we can use this to insert arbitrary token sequences
1510 * to turn the pragma's into internal front-end sequences for
1511 * when we actually start caring about them.
1513 * So eventually this will turn into some kind of extended
1514 * __attribute__() like thing, except called __pragma__(xxx).
1516 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1518 struct token *next = *line;
1520 token->ident = &pragma_ident;
1521 token->pos.newline = 1;
1522 token->pos.whitespace = 1;
1523 token->pos.pos = 1;
1524 *line = token;
1525 token->next = next;
1526 return 1;
1530 * We ignore #line for now.
1532 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1534 return 1;
1538 void init_preprocessor(void)
1540 int i;
1541 int stream = init_stream("preprocessor", -1, includepath);
1542 static struct {
1543 const char *name;
1544 int (*handler)(struct stream *, struct token **, struct token *);
1545 } handlers[] = {
1546 { "define", handle_define },
1547 { "weak_define",handle_weak_define },
1548 { "undef", handle_undef },
1549 { "ifdef", handle_ifdef },
1550 { "ifndef", handle_ifndef },
1551 { "else", handle_else },
1552 { "endif", handle_endif },
1553 { "if", handle_if },
1554 { "elif", handle_elif },
1555 { "warning", handle_warning },
1556 { "error", handle_error },
1557 { "include", handle_include },
1558 { "include_next",handle_include_next },
1559 { "pragma", handle_pragma },
1560 { "line", handle_line },
1562 // our internal preprocessor tokens
1563 { "nostdinc", handle_nostdinc },
1564 { "add_include", handle_add_include },
1565 { "add_isystem", handle_add_isystem },
1566 { "add_dirafter", handle_add_dirafter },
1567 { "split_include", handle_split_include },
1570 for (i = 0; i < (sizeof (handlers) / sizeof (handlers[0])); i++) {
1571 struct symbol *sym;
1572 sym = create_symbol(stream, handlers[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1573 sym->handler = handlers[i].handler;
1577 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1579 struct token *token = start->next;
1581 if (!token)
1582 return;
1584 if (token_type(token) == TOKEN_NUMBER)
1585 if (handle_line(stream, line, start))
1586 return;
1588 if (token_type(token) == TOKEN_IDENT) {
1589 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1590 if (sym && sym->handler(stream, line, token))
1591 return;
1594 warning(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1597 static void preprocessor_line(struct stream *stream, struct token **line)
1599 struct token *start = *line, *next;
1600 struct token **tp = &start->next;
1602 for (;;) {
1603 next = *tp;
1604 if (next->pos.newline)
1605 break;
1606 tp = &next->next;
1608 *line = next;
1609 *tp = &eof_token_entry;
1610 handle_preprocessor_line(stream, line, start);
1613 static void do_preprocess(struct token **list)
1615 struct token *next;
1617 while (!eof_token(next = scan_next(list))) {
1618 struct stream *stream = input_streams + next->pos.stream;
1620 if (next->pos.newline && match_op(next, '#')) {
1621 if (!next->pos.noexpand) {
1622 preprocessor_line(stream, list);
1623 __free_token(next); /* Free the '#' token */
1624 continue;
1628 switch (token_type(next)) {
1629 case TOKEN_STREAMEND:
1630 if (stream->nesting < if_nesting + 1) {
1631 warning(unmatched_if->pos, "unterminated preprocessor conditional");
1632 // Pretend to see a series of #endifs
1633 MARK_STREAM_NONCONST(next->pos);
1634 do {
1635 handle_endif (stream, NULL, NULL);
1636 } while (stream->nesting < if_nesting + 1);
1638 if (stream->constant == CONSTANT_FILE_MAYBE && stream->protect) {
1639 stream->constant = CONSTANT_FILE_YES;
1641 *list = next->next;
1642 continue;
1643 case TOKEN_STREAMBEGIN:
1644 stream->nesting = if_nesting + 1;
1645 *list = next->next;
1646 continue;
1648 default:
1649 if (false_nesting) {
1650 *list = next->next;
1651 __free_token(next);
1652 continue;
1655 if (token_type(next) != TOKEN_IDENT ||
1656 expand_one_symbol(list))
1657 list = &next->next;
1660 if (stream->constant == CONSTANT_FILE_MAYBE) {
1662 * Any token expansion (even if it ended up being an
1663 * empty expansion) in this stream implies it can't
1664 * be constant.
1666 MARK_STREAM_NONCONST(next->pos);
1671 struct token * preprocess(struct token *token)
1673 preprocessing = 1;
1674 init_preprocessor();
1675 do_preprocess(&token);
1677 // Drop all expressions from pre-processing, they're not used any more.
1678 clear_expression_alloc();
1679 preprocessing = 0;
1681 return token;