[PATCH] two-arguments ?:
[smatch.git] / pre-process.c
blob63274cae76b1daf61ddf08230fbab1007fa04e0f
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)
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 void replace_with_defined(struct token *token)
98 static const char *string[] = { "0", "1" };
99 int defined = 0;
100 if (token_type(token) != TOKEN_IDENT)
101 warning(token->pos, "operator \"defined\" requires an identifier");
102 else if (lookup_symbol(token->ident, NS_MACRO))
103 defined = 1;
104 token_type(token) = TOKEN_NUMBER;
105 token->number = string[defined];
108 static int expand_one_symbol(struct token **list)
110 struct token *token = *list;
111 struct symbol *sym;
113 if (token->pos.noexpand)
114 return 1;
116 sym = lookup_symbol(token->ident, NS_MACRO);
117 if (sym)
118 return expand(list, sym);
119 if (token->ident == &__LINE___ident) {
120 replace_with_integer(token, token->pos.line);
121 } else if (token->ident == &__FILE___ident) {
122 replace_with_string(token, (input_streams + token->pos.stream)->name);
124 return 1;
127 static inline struct token *scan_next(struct token **where)
129 struct token *token = *where;
130 if (token_type(token) != TOKEN_UNTAINT)
131 return token;
132 do {
133 token->ident->tainted = 0;
134 token = token->next;
135 } while (token_type(token) == TOKEN_UNTAINT);
136 *where = token;
137 return token;
140 static void expand_list(struct token **list)
142 struct token *next;
143 while (!eof_token(next = scan_next(list))) {
144 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
145 list = &next->next;
149 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
151 struct token **p = &prev->next;
152 struct token *next;
153 int nesting = 0;
155 while (!eof_token(next = scan_next(p))) {
156 if (match_op(next, '(')) {
157 nesting++;
158 } else if (match_op(next, ')')) {
159 if (!nesting--)
160 break;
161 } else if (match_op(next, ',') && !nesting && !vararg) {
162 break;
164 next->pos.stream = pos->stream;
165 next->pos.line = pos->line;
166 next->pos.pos = pos->pos;
167 p = &next->next;
169 *p = &eof_token_entry;
170 return next;
174 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
177 struct arg {
178 struct token *arg;
179 struct token *expanded;
180 struct token *str;
181 int n_normal;
182 int n_quoted;
183 int n_str;
186 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
188 int wanted = arglist->count.normal;
189 struct token *next = NULL;
190 int count = 0;
192 arglist = arglist->next; /* skip counter */
194 if (!wanted) {
195 next = collect_arg(start, 0, &what->pos);
196 if (eof_token(next))
197 goto Eclosing;
198 if (!eof_token(start->next) || !match_op(next, ')')) {
199 count++;
200 goto Emany;
202 } else {
203 for (count = 0; count < wanted; count++) {
204 struct argcount *p = &arglist->next->count;
205 next = collect_arg(start, p->vararg, &what->pos);
206 arglist = arglist->next->next;
207 if (eof_token(next))
208 goto Eclosing;
209 args[count].arg = start->next;
210 args[count].n_normal = p->normal;
211 args[count].n_quoted = p->quoted;
212 args[count].n_str = p->str;
213 if (match_op(next, ')')) {
214 count++;
215 break;
217 start = next;
219 if (count == wanted && !match_op(next, ')'))
220 goto Emany;
221 if (count == wanted - 1) {
222 struct argcount *p = &arglist->next->count;
223 if (!p->vararg)
224 goto Efew;
225 args[count].arg = NULL;
226 args[count].n_normal = p->normal;
227 args[count].n_quoted = p->quoted;
228 args[count].n_str = p->str;
230 if (count < wanted - 1)
231 goto Efew;
233 what->next = next->next;
234 return 1;
236 Efew:
237 warning(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
238 show_token(what), wanted, count);
239 goto out;
240 Emany:
241 while (match_op(next, ',')) {
242 next = collect_arg(next, 0, &what->pos);
243 count++;
245 if (eof_token(next))
246 goto Eclosing;
247 warning(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
248 show_token(what), count, wanted);
249 goto out;
250 Eclosing:
251 warning(what->pos, "unterminated argument list invoking macro \"%s\"",
252 show_token(what));
253 out:
254 what->next = next->next;
255 return 0;
258 static struct token *dup_list(struct token *list)
260 struct token *res;
261 struct token **p = &res;
263 while (!eof_token(list)) {
264 struct token *newtok = __alloc_token(0);
265 *newtok = *list;
266 *p = newtok;
267 p = &newtok->next;
268 list = list->next;
270 return res;
273 static struct token *stringify(struct token *arg)
275 const char *s = show_token_sequence(arg);
276 int size = strlen(s)+1;
277 struct token *token = __alloc_token(0);
278 struct string *string = __alloc_string(size);
280 memcpy(string->data, s, size);
281 string->length = size;
282 token->pos = arg->pos;
283 token_type(token) = TOKEN_STRING;
284 token->string = string;
285 token->next = &eof_token_entry;
286 return token;
289 static void expand_arguments(int count, struct arg *args)
291 int i;
292 for (i = 0; i < count; i++) {
293 struct token *arg = args[i].arg;
294 if (!arg)
295 arg = &eof_token_entry;
296 if (args[i].n_str)
297 args[i].str = stringify(arg);
298 if (args[i].n_normal) {
299 if (!args[i].n_quoted) {
300 args[i].expanded = arg;
301 args[i].arg = NULL;
302 } else if (eof_token(arg)) {
303 args[i].expanded = arg;
304 } else {
305 args[i].expanded = dup_list(arg);
307 expand_list(&args[i].expanded);
313 * Possibly valid combinations:
314 * - ident + ident -> ident
315 * - ident + number -> ident unless number contains '.', '+' or '-'.
316 * - number + number -> number
317 * - number + ident -> number
318 * - number + '.' -> number
319 * - number + '+' or '-' -> number, if number used to end on [eEpP].
320 * - '.' + number -> number, if number used to start with a digit.
321 * - special + special -> either special or an error.
323 static enum token_type combine(struct token *left, struct token *right, char *p)
325 int len;
326 enum token_type t1 = token_type(left), t2 = token_type(right);
328 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
329 return TOKEN_ERROR;
331 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
332 return TOKEN_ERROR;
334 strcpy(p, show_token(left));
335 strcat(p, show_token(right));
336 len = strlen(p);
338 if (len >= 256)
339 return TOKEN_ERROR;
341 if (t1 == TOKEN_IDENT) {
342 if (t2 == TOKEN_SPECIAL)
343 return TOKEN_ERROR;
344 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
345 return TOKEN_ERROR;
346 return TOKEN_IDENT;
349 if (t1 == TOKEN_NUMBER) {
350 if (t2 == TOKEN_SPECIAL) {
351 switch (right->special) {
352 case '.':
353 break;
354 case '+': case '-':
355 if (strchr("eEpP", p[len - 2]))
356 break;
357 default:
358 return TOKEN_ERROR;
361 return TOKEN_NUMBER;
364 if (p[0] == '.' && isdigit((unsigned char)p[1]))
365 return TOKEN_NUMBER;
367 return TOKEN_SPECIAL;
370 static int merge(struct token *left, struct token *right)
372 extern unsigned char combinations[][3];
373 static char buffer[512];
374 int n;
376 switch (combine(left, right, buffer)) {
377 case TOKEN_IDENT:
378 left->ident = built_in_ident(buffer);
379 left->pos.noexpand = 0;
380 return 1;
382 case TOKEN_NUMBER: {
383 char *number = __alloc_bytes(strlen(buffer) + 1);
384 memcpy(number, buffer, strlen(buffer) + 1);
385 token_type(left) = TOKEN_NUMBER; /* could be . + num */
386 left->number = number;
387 return 1;
390 case TOKEN_SPECIAL:
391 if (buffer[2] && buffer[3])
392 break;
393 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
394 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
395 left->special = n;
396 return 1;
399 default:
402 warning(left->pos, "'##' failed: concatenation is not a valid token");
403 return 0;
406 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
408 struct token *alloc = alloc_token(streampos);
409 token_type(alloc) = token_type(token);
410 alloc->pos.newline = pos->newline;
411 alloc->pos.whitespace = pos->whitespace;
412 alloc->number = token->number;
413 alloc->pos.noexpand = token->pos.noexpand;
414 return alloc;
417 static struct token **copy(struct token **where, struct token *list, int *count)
419 int need_copy = --*count;
420 while (!eof_token(list)) {
421 struct token *token;
422 if (need_copy)
423 token = dup_token(list, &list->pos, &list->pos);
424 else
425 token = list;
426 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
427 token->pos.noexpand = 1;
428 *where = token;
429 where = &token->next;
430 list = list->next;
432 *where = &eof_token_entry;
433 return where;
436 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
438 struct token *token = *list;
439 struct position *base_pos = &token->pos;
440 struct position *pos = base_pos;
441 int *count;
442 enum {Normal, Placeholder, Concat} state = Normal;
444 for (; !eof_token(body); body = body->next, pos = &body->pos) {
445 struct token *added, *arg;
446 struct token **tail;
448 switch (token_type(body)) {
449 case TOKEN_GNU_KLUDGE:
451 * GNU kludge: if we had <comma>##<vararg>, behaviour
452 * depends on whether we had enough arguments to have
453 * a vararg. If we did, ## is just ignored. Otherwise
454 * both , and ## are ignored. Comma should come from
455 * the body of macro and not be an argument of earlier
456 * concatenation.
458 if (!args[body->next->argnum].arg)
459 continue;
460 added = dup_token(body, base_pos, pos);
461 token_type(added) = TOKEN_SPECIAL;
462 tail = &added->next;
463 break;
465 case TOKEN_STR_ARGUMENT:
466 arg = args[body->argnum].str;
467 count = &args[body->argnum].n_str;
468 goto copy_arg;
470 case TOKEN_QUOTED_ARGUMENT:
471 arg = args[body->argnum].arg;
472 count = &args[body->argnum].n_quoted;
473 if (!arg || eof_token(arg)) {
474 if (state == Concat)
475 state = Normal;
476 else
477 state = Placeholder;
478 continue;
480 goto copy_arg;
482 case TOKEN_MACRO_ARGUMENT:
483 arg = args[body->argnum].expanded;
484 count = &args[body->argnum].n_normal;
485 if (eof_token(arg)) {
486 state = Normal;
487 continue;
489 copy_arg:
490 tail = copy(&added, arg, count);
491 added->pos.newline = pos->newline;
492 added->pos.whitespace = pos->whitespace;
493 break;
495 case TOKEN_CONCAT:
496 if (state == Placeholder)
497 state = Normal;
498 else
499 state = Concat;
500 continue;
502 case TOKEN_IDENT:
503 added = dup_token(body, base_pos, pos);
504 if (added->ident->tainted)
505 added->pos.noexpand = 1;
506 tail = &added->next;
507 break;
509 default:
510 added = dup_token(body, base_pos, pos);
511 tail = &added->next;
512 break;
516 * if we got to doing real concatenation, we already have
517 * added something into the list, so containing_token() is OK.
519 if (state == Concat && merge(containing_token(list), added)) {
520 *list = added->next;
521 if (tail != &added->next)
522 list = tail;
523 } else {
524 *list = added;
525 list = tail;
527 state = Normal;
529 *list = &eof_token_entry;
530 return list;
533 static int expand(struct token **list, struct symbol *sym)
535 struct token *last;
536 struct token *token = *list;
537 struct ident *expanding = token->ident;
538 struct token **tail;
539 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
540 struct arg args[nargs];
542 if (expanding->tainted) {
543 token->pos.noexpand = 1;
544 return 1;
547 if (sym->arglist) {
548 if (!match_op(scan_next(&token->next), '('))
549 return 1;
550 if (!collect_arguments(token->next, sym->arglist, args, token))
551 return 1;
552 expand_arguments(nargs, args);
555 expanding->tainted = 1;
557 last = token->next;
558 tail = substitute(list, sym->expansion, args);
559 *tail = last;
561 return 0;
564 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
566 struct token *last;
567 static char buffer[256];
568 char *ptr = buffer;
570 last = token;
571 while (!eof_token(token) && !match_op(token, endop)) {
572 int len;
573 const char *val = token->string->data;
574 if (token_type(token) != TOKEN_STRING)
575 val = show_token(token);
576 len = strlen(val);
577 memcpy(ptr, val, len);
578 ptr += len;
579 token = token->next;
581 *ptr = 0;
582 if (endop && !match_op(token, endop))
583 warning(start->pos, "expected '>' at end of filename");
584 return buffer;
587 static int try_include(const char *path, int plen, const char *filename, int flen, struct token **where, const char **next_path)
589 int fd;
590 static char fullname[PATH_MAX];
592 memcpy(fullname, path, plen);
593 if (plen && path[plen-1] != '/') {
594 fullname[plen] = '/';
595 plen++;
597 memcpy(fullname+plen, filename, flen);
598 fd = open(fullname, O_RDONLY);
599 if (fd >= 0) {
600 char * streamname = __alloc_bytes(plen + flen);
601 memcpy(streamname, fullname, plen + flen);
602 *where = tokenize(streamname, fd, *where, next_path);
603 close(fd);
604 return 1;
606 return 0;
609 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
611 const char *path;
613 while ((path = *pptr++) != NULL) {
614 if (!try_include(path, strlen(path), filename, flen, list, pptr))
615 continue;
616 return 1;
618 return 0;
622 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
624 int flen = strlen(filename) + 1;
626 /* Absolute path? */
627 if (filename[0] == '/') {
628 if (try_include("", 0, filename, flen, list, includepath))
629 return;
630 goto out;
633 /* Same directory as current stream? */
634 if (local) {
635 const char *path;
636 char *slash;
637 int plen;
639 path = stream->name;
640 slash = strrchr(path, '/');
641 plen = slash ? slash - path : 0;
643 if (try_include(path, plen, filename, flen, list, includepath))
644 return;
647 /* Check the standard include paths.. */
648 if (do_include_path(path, list, token, filename, flen))
649 return;
650 out:
651 error_die(token->pos, "unable to open '%s'", filename);
654 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
656 const char *filename;
657 struct token *next;
658 int expect;
660 if (false_nesting)
661 return 1;
663 if (stream->constant == CONSTANT_FILE_MAYBE)
664 MARK_STREAM_NONCONST(token->pos);
666 next = token->next;
667 expect = '>';
668 if (!match_op(next, '<')) {
669 expand_list(&token->next);
670 expect = 0;
671 next = token;
672 if (match_op(token->next, '<')) {
673 next = token->next;
674 expect = '>';
677 token = next->next;
678 filename = token_name_sequence(token, expect, token);
679 do_include(!expect, stream, list, token, filename, path);
680 return 1;
683 static int handle_include(struct stream *stream, struct token **list, struct token *token)
685 return handle_include_path(stream, list, token, includepath);
688 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
690 return handle_include_path(stream, list, token, stream->next_path);
693 static int token_different(struct token *t1, struct token *t2)
695 int different;
697 if (token_type(t1) != token_type(t2))
698 return 1;
700 switch (token_type(t1)) {
701 case TOKEN_IDENT:
702 different = t1->ident != t2->ident;
703 break;
704 case TOKEN_ARG_COUNT:
705 case TOKEN_UNTAINT:
706 case TOKEN_CONCAT:
707 case TOKEN_GNU_KLUDGE:
708 different = 0;
709 break;
710 case TOKEN_NUMBER:
711 different = strcmp(t1->number, t2->number);
712 break;
713 case TOKEN_SPECIAL:
714 different = t1->special != t2->special;
715 break;
716 case TOKEN_MACRO_ARGUMENT:
717 case TOKEN_QUOTED_ARGUMENT:
718 case TOKEN_STR_ARGUMENT:
719 different = t1->argnum != t2->argnum;
720 break;
721 case TOKEN_CHAR:
722 different = t1->character != t2->character;
723 break;
724 case TOKEN_STRING: {
725 struct string *s1, *s2;
727 s1 = t1->string;
728 s2 = t2->string;
729 different = 1;
730 if (s1->length != s2->length)
731 break;
732 different = memcmp(s1->data, s2->data, s1->length);
733 break;
735 default:
736 different = 1;
737 break;
739 return different;
742 static int token_list_different(struct token *list1, struct token *list2)
744 for (;;) {
745 if (list1 == list2)
746 return 0;
747 if (!list1 || !list2)
748 return 1;
749 if (token_different(list1, list2))
750 return 1;
751 list1 = list1->next;
752 list2 = list2->next;
756 static inline void set_arg_count(struct token *token)
758 token_type(token) = TOKEN_ARG_COUNT;
759 token->count.normal = token->count.quoted =
760 token->count.str = token->count.vararg = 0;
763 static struct token *parse_arguments(struct token *list)
765 struct token *arg = list->next, *next = list;
766 struct argcount *count = &list->count;
768 set_arg_count(list);
770 if (match_op(arg, ')')) {
771 next = arg->next;
772 list->next = &eof_token_entry;
773 return next;
776 while (token_type(arg) == TOKEN_IDENT) {
777 if (arg->ident == &__VA_ARGS___ident)
778 goto Eva_args;
779 if (!++count->normal)
780 goto Eargs;
781 next = arg->next;
783 if (match_op(next, ',')) {
784 set_arg_count(next);
785 arg = next->next;
786 continue;
789 if (match_op(next, ')')) {
790 set_arg_count(next);
791 next = next->next;
792 arg->next->next = &eof_token_entry;
793 return next;
796 /* normal cases are finished here */
798 if (match_op(next, SPECIAL_ELLIPSIS)) {
799 if (match_op(next->next, ')')) {
800 set_arg_count(next);
801 next->count.vararg = 1;
802 next = next->next;
803 arg->next->next = &eof_token_entry;
804 return next->next;
807 arg = next;
808 goto Enotclosed;
811 if (eof_token(next)) {
812 goto Enotclosed;
813 } else {
814 arg = next;
815 goto Ebadstuff;
819 if (match_op(arg, SPECIAL_ELLIPSIS)) {
820 next = arg->next;
821 token_type(arg) = TOKEN_IDENT;
822 arg->ident = &__VA_ARGS___ident;
823 if (!match_op(next, ')'))
824 goto Enotclosed;
825 if (!++count->normal)
826 goto Eargs;
827 set_arg_count(next);
828 next->count.vararg = 1;
829 next = next->next;
830 arg->next->next = &eof_token_entry;
831 return next;
834 if (eof_token(arg)) {
835 arg = next;
836 goto Enotclosed;
838 if (match_op(arg, ','))
839 goto Emissing;
840 else
841 goto Ebadstuff;
844 Emissing:
845 warning(arg->pos, "parameter name missing");
846 return NULL;
847 Ebadstuff:
848 warning(arg->pos, "\"%s\" may not appear in macro parameter list",
849 show_token(arg));
850 return NULL;
851 Enotclosed:
852 warning(arg->pos, "missing ')' in macro parameter list");
853 return NULL;
854 Eva_args:
855 warning(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
856 return NULL;
857 Eargs:
858 warning(arg->pos, "too many arguments in macro definition");
859 return NULL;
862 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
864 struct ident *ident = token->ident;
865 int nr;
867 if (!arglist || token_type(token) != TOKEN_IDENT)
868 return 0;
870 arglist = arglist->next;
872 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
873 if (arglist->ident == ident) {
874 struct argcount *count = &arglist->next->count;
875 int n;
877 token->argnum = nr;
878 token_type(token) = type;
879 switch (type) {
880 case TOKEN_MACRO_ARGUMENT:
881 n = ++count->normal;
882 break;
883 case TOKEN_QUOTED_ARGUMENT:
884 n = ++count->quoted;
885 break;
886 default:
887 n = ++count->str;
889 if (n)
890 return count->vararg ? 2 : 1;
891 token_type(token) = TOKEN_ERROR;
892 return -1;
895 return 0;
898 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
900 struct token *token = expansion;
901 struct token **p;
902 struct token *last = NULL;
904 if (match_op(token, SPECIAL_HASHHASH))
905 goto Econcat;
907 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
908 if (match_op(token, '#')) {
909 if (arglist) {
910 struct token *next = token->next;
911 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
912 goto Equote;
913 next->pos.whitespace = token->pos.whitespace;
914 token = *p = next;
915 } else {
916 token->pos.noexpand = 1;
918 } else if (match_op(token, SPECIAL_HASHHASH)) {
919 struct token *next = token->next;
920 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
921 token_type(token) = TOKEN_CONCAT;
922 if (arg) {
923 token = next;
924 /* GNU kludge */
925 if (arg == 2 && last && match_op(last, ',')) {
926 token_type(last) = TOKEN_GNU_KLUDGE;
927 last->next = token;
929 } else if (match_op(next, SPECIAL_HASHHASH))
930 token = next;
931 else if (match_op(next, ','))
932 token = next;
933 else if (eof_token(next))
934 goto Econcat;
935 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
936 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
937 } else {
938 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
940 if (token_type(token) == TOKEN_ERROR)
941 goto Earg;
942 last = token;
944 token = alloc_token(&expansion->pos);
945 token_type(token) = TOKEN_UNTAINT;
946 token->ident = name;
947 token->next = *p;
948 *p = token;
949 return expansion;
951 Equote:
952 warning(token->pos, "'#' is not followed by a macro parameter");
953 return NULL;
955 Econcat:
956 warning(token->pos, "'##' cannot appear at the ends of macro expansion");
957 return NULL;
958 Earg:
959 warning(token->pos, "too many instances of argument in body");
960 return NULL;
963 static int handle_define(struct stream *stream, struct token **line, struct token *token)
965 struct token *arglist, *expansion;
966 struct token *left = token->next;
967 struct symbol *sym;
968 struct ident *name;
970 if (token_type(left) != TOKEN_IDENT) {
971 warning(token->pos, "expected identifier to 'define'");
972 return 0;
974 if (false_nesting)
975 return 1;
977 if (stream->constant == CONSTANT_FILE_MAYBE)
978 MARK_STREAM_NONCONST(token->pos);
980 name = left->ident;
982 arglist = NULL;
983 expansion = left->next;
984 if (!expansion->pos.whitespace && match_op(expansion, '(')) {
985 arglist = expansion;
986 expansion = parse_arguments(expansion);
987 if (!expansion)
988 return 1;
991 expansion = parse_expansion(expansion, arglist, name);
992 if (!expansion)
993 return 1;
995 sym = lookup_symbol(name, NS_MACRO);
996 if (sym) {
997 if (token_list_different(sym->expansion, expansion) ||
998 token_list_different(sym->arglist, arglist)) {
999 warning(left->pos, "preprocessor token %.*s redefined",
1000 name->len, name->name);
1001 info(sym->pos, "this was the original definition");
1003 return 1;
1005 sym = alloc_symbol(left->pos, SYM_NODE);
1006 bind_symbol(sym, name, NS_MACRO);
1008 sym->expansion = expansion;
1009 sym->arglist = arglist;
1010 return 1;
1013 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1015 struct token *left = token->next;
1016 struct symbol **sym;
1018 if (token_type(left) != TOKEN_IDENT) {
1019 warning(token->pos, "expected identifier to 'undef'");
1020 return 0;
1022 if (false_nesting)
1023 return 1;
1025 if (stream->constant == CONSTANT_FILE_MAYBE)
1026 MARK_STREAM_NONCONST(token->pos);
1028 sym = &left->ident->symbols;
1029 while (*sym) {
1030 struct symbol *t = *sym;
1031 if (t->namespace == NS_MACRO) {
1032 *sym = t->next_id;
1033 return 1;
1035 sym = &t->next_id;
1037 return 1;
1040 static int preprocessor_if(struct token *token, int true)
1042 if (if_nesting == 0)
1043 unmatched_if = token;
1044 if (if_nesting >= MAX_NEST)
1045 error_die(token->pos, "Maximum preprocessor conditional level exhausted");
1046 elif_ignore[if_nesting] = (false_nesting || true) ? ELIF_IGNORE : 0;
1047 if (false_nesting || !true) {
1048 false_nesting++;
1049 return 1;
1051 true_nesting++;
1052 return 1;
1055 static int token_defined(struct token *token)
1057 if (token_type(token) == TOKEN_IDENT)
1058 return lookup_symbol(token->ident, NS_MACRO) != NULL;
1060 warning(token->pos, "expected identifier for #if[n]def");
1061 return 0;
1064 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1066 return preprocessor_if(token, token_defined(token->next));
1069 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1071 struct token *next = token->next;
1072 if (stream->constant == CONSTANT_FILE_MAYBE) {
1073 if (token_type(next) == TOKEN_IDENT &&
1074 (!stream->protect || stream->protect == next->ident)) {
1075 stream->constant = CONSTANT_FILE_IFNDEF;
1076 stream->protect = next->ident;
1077 } else
1078 MARK_STREAM_NONCONST(token->pos);
1080 return preprocessor_if(token, !token_defined(next));
1084 * Expression handling for #if and #elif; it differs from normal expansion
1085 * due to special treatment of "defined".
1087 static int expression_value(struct token **where)
1089 struct expression *expr;
1090 struct token *p;
1091 struct token **list = where, **beginning = NULL;
1092 long long value;
1093 int state = 0;
1095 while (!eof_token(p = scan_next(list))) {
1096 switch (state) {
1097 case 0:
1098 if (token_type(p) != TOKEN_IDENT)
1099 break;
1100 if (p->ident == &defined_ident) {
1101 state = 1;
1102 beginning = list;
1103 break;
1105 if (!expand_one_symbol(list))
1106 continue;
1107 if (token_type(p) != TOKEN_IDENT)
1108 break;
1109 if (Wundefined_preprocessor)
1110 warning(p->pos, "undefined preprocessor identifier '%s'", show_ident(p->ident));
1111 replace_with_integer(p, 0);
1112 break;
1113 case 1:
1114 if (match_op(p, '(')) {
1115 state = 2;
1116 } else {
1117 state = 0;
1118 replace_with_defined(p);
1119 *beginning = p;
1121 break;
1122 case 2:
1123 if (token_type(p) == TOKEN_IDENT)
1124 state = 3;
1125 else
1126 state = 0;
1127 replace_with_defined(p);
1128 *beginning = p;
1129 break;
1130 case 3:
1131 state = 0;
1132 if (!match_op(p, ')'))
1133 warning(p->pos, "missing ')' after \"defined\"");
1134 *list = p->next;
1135 continue;
1137 list = &p->next;
1140 p = constant_expression(*where, &expr);
1141 if (!eof_token(p))
1142 warning(p->pos, "garbage at end: %s", show_token_sequence(p));
1143 value = get_expression_value(expr);
1144 return value != 0;
1147 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1149 int value = 0;
1150 if (!false_nesting)
1151 value = expression_value(&token->next);
1153 // This is an approximation. We really only need this if the
1154 // condition does depends on a pre-processor symbol. Note, that
1155 // the important #ifndef case has already changed ->constant.
1156 if (stream->constant == CONSTANT_FILE_MAYBE)
1157 MARK_STREAM_NONCONST(token->pos);
1159 return preprocessor_if(token, value);
1162 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1164 if (stream->nesting == if_nesting)
1165 MARK_STREAM_NONCONST(token->pos);
1167 if (stream->nesting > if_nesting) {
1168 warning(token->pos, "unmatched #elif");
1169 return 1;
1172 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1173 warning(token->pos, "#elif after #else");
1175 if (false_nesting) {
1176 /* If this whole if-thing is if'ed out, an elif cannot help */
1177 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1178 return 1;
1179 if (expression_value(&token->next)) {
1180 false_nesting = 0;
1181 true_nesting++;
1182 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1184 } else {
1185 false_nesting = 1;
1186 true_nesting--;
1188 return 1;
1191 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1193 if (stream->nesting == if_nesting)
1194 MARK_STREAM_NONCONST(token->pos);
1196 if (stream->nesting > if_nesting) {
1197 warning(token->pos, "unmatched #else");
1198 return 1;
1201 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1202 warning(token->pos, "#else after #else");
1203 else
1204 elif_ignore[if_nesting-1] |= ELIF_SEEN_ELSE;
1206 if (false_nesting) {
1207 /* If this whole if-thing is if'ed out, an else cannot help */
1208 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1209 return 1;
1210 false_nesting = 0;
1211 true_nesting++;
1212 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1213 } else {
1214 true_nesting--;
1215 false_nesting = 1;
1217 return 1;
1220 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1222 if (stream->constant == CONSTANT_FILE_IFNDEF && stream->nesting == if_nesting)
1223 stream->constant = CONSTANT_FILE_MAYBE;
1225 if (stream->nesting > if_nesting)
1226 warning(token->pos, "unmatched #endif");
1227 else if (false_nesting)
1228 false_nesting--;
1229 else
1230 true_nesting--;
1231 return 1;
1234 static const char *show_token_sequence(struct token *token)
1236 static char buffer[1024];
1237 char *ptr = buffer;
1238 int whitespace = 0;
1240 if (!token)
1241 return "<none>";
1242 while (!eof_token(token)) {
1243 const char *val = show_token(token);
1244 int len = strlen(val);
1246 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1247 warning(token->pos, "too long token expansion");
1248 break;
1251 if (whitespace)
1252 *ptr++ = ' ';
1253 memcpy(ptr, val, len);
1254 ptr += len;
1255 token = token->next;
1256 whitespace = token->pos.whitespace;
1258 *ptr = 0;
1259 return buffer;
1262 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1264 if (false_nesting)
1265 return 1;
1266 if (stream->constant == CONSTANT_FILE_MAYBE)
1267 MARK_STREAM_NONCONST(token->pos);
1268 warning(token->pos, "%s", show_token_sequence(token->next));
1269 return 1;
1272 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1274 if (false_nesting)
1275 return 1;
1276 if (stream->constant == CONSTANT_FILE_MAYBE)
1277 MARK_STREAM_NONCONST(token->pos);
1278 warning(token->pos, "%s", show_token_sequence(token->next));
1279 return 1;
1282 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1284 int stdinc;
1286 if (false_nesting)
1287 return 1;
1290 * Do we have any non-system includes?
1291 * Clear them out if so..
1293 stdinc = gcc_includepath - sys_includepath;
1294 if (stdinc) {
1295 const char **src = gcc_includepath;
1296 const char **dst = sys_includepath;
1297 for (;;) {
1298 if (!(*dst = *src))
1299 break;
1300 dst++;
1301 src++;
1303 gcc_includepath -= stdinc;
1305 return 1;
1308 static void add_path_entry(struct token *token, const char *path)
1310 const char **dst;
1311 const char *next;
1313 /* Need one free entry.. */
1314 if (includepath[INCLUDEPATHS-2])
1315 error_die(token->pos, "too many include path entries");
1317 next = path;
1318 dst = sys_includepath;
1319 sys_includepath++;
1320 gcc_includepath++;
1323 * Move them all up starting at "sys_includepath",
1324 * insert the new entry..
1326 for (;;) {
1327 const char *tmp = *dst;
1328 *dst = next;
1329 if (!next)
1330 break;
1331 next = tmp;
1332 dst++;
1336 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1338 for (;;) {
1339 token = token->next;
1340 if (eof_token(token))
1341 return 1;
1342 if (token_type(token) != TOKEN_STRING) {
1343 warning(token->pos, "expected path string");
1344 return 1;
1346 add_path_entry(token, token->string->data);
1351 * We replace "#pragma xxx" with "__pragma__" in the token
1352 * stream. Just as an example.
1354 * We'll just #define that away for now, but the theory here
1355 * is that we can use this to insert arbitrary token sequences
1356 * to turn the pragma's into internal front-end sequences for
1357 * when we actually start caring about them.
1359 * So eventually this will turn into some kind of extended
1360 * __attribute__() like thing, except called __pragma__(xxx).
1362 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1364 struct token *next = *line;
1366 token->ident = &pragma_ident;
1367 token->pos.newline = 1;
1368 token->pos.whitespace = 1;
1369 token->pos.pos = 1;
1370 *line = token;
1371 token->next = next;
1372 return 1;
1376 * We ignore #line for now.
1378 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1380 return 1;
1384 void init_preprocessor(void)
1386 int i;
1387 int stream = init_stream("preprocessor", -1, includepath);
1388 static struct {
1389 const char *name;
1390 int (*handler)(struct stream *, struct token **, struct token *);
1391 } handlers[] = {
1392 { "define", handle_define },
1393 { "undef", handle_undef },
1394 { "ifdef", handle_ifdef },
1395 { "ifndef", handle_ifndef },
1396 { "else", handle_else },
1397 { "endif", handle_endif },
1398 { "if", handle_if },
1399 { "elif", handle_elif },
1400 { "warning", handle_warning },
1401 { "error", handle_error },
1402 { "include", handle_include },
1403 { "include_next",handle_include_next },
1404 { "pragma", handle_pragma },
1405 { "line", handle_line },
1407 // our internal preprocessor tokens
1408 { "nostdinc", handle_nostdinc },
1409 { "add_include", handle_add_include },
1412 for (i = 0; i < (sizeof (handlers) / sizeof (handlers[0])); i++) {
1413 struct symbol *sym;
1414 sym = create_symbol(stream, handlers[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1415 sym->handler = handlers[i].handler;
1419 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1421 struct token *token = start->next;
1423 if (!token)
1424 return;
1426 if (token_type(token) == TOKEN_NUMBER)
1427 if (handle_line(stream, line, start))
1428 return;
1430 if (token_type(token) == TOKEN_IDENT) {
1431 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1432 if (sym && sym->handler(stream, line, token))
1433 return;
1436 warning(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1439 static void preprocessor_line(struct stream *stream, struct token **line)
1441 struct token *start = *line, *next;
1442 struct token **tp = &start->next;
1444 for (;;) {
1445 next = *tp;
1446 if (next->pos.newline)
1447 break;
1448 tp = &next->next;
1450 *line = next;
1451 *tp = &eof_token_entry;
1452 handle_preprocessor_line(stream, line, start);
1455 static void do_preprocess(struct token **list)
1457 struct token *next;
1459 while (!eof_token(next = scan_next(list))) {
1460 struct stream *stream = input_streams + next->pos.stream;
1462 if (next->pos.newline && match_op(next, '#')) {
1463 if (!next->pos.noexpand) {
1464 preprocessor_line(stream, list);
1465 continue;
1469 switch (token_type(next)) {
1470 case TOKEN_STREAMEND:
1471 if (stream->nesting < if_nesting + 1) {
1472 warning(unmatched_if->pos, "unterminated preprocessor conditional");
1473 // Pretend to see a series of #endifs
1474 MARK_STREAM_NONCONST(next->pos);
1475 do {
1476 handle_endif (stream, NULL, NULL);
1477 } while (stream->nesting < if_nesting + 1);
1479 if (stream->constant == CONSTANT_FILE_MAYBE && stream->protect) {
1480 stream->constant = CONSTANT_FILE_YES;
1482 *list = next->next;
1483 continue;
1484 case TOKEN_STREAMBEGIN:
1485 stream->nesting = if_nesting + 1;
1486 *list = next->next;
1487 continue;
1489 default:
1490 if (false_nesting) {
1491 *list = next->next;
1492 continue;
1495 if (token_type(next) != TOKEN_IDENT ||
1496 expand_one_symbol(list))
1497 list = &next->next;
1500 if (stream->constant == CONSTANT_FILE_MAYBE) {
1502 * Any token expansion (even if it ended up being an
1503 * empty expansion) in this stream implies it can't
1504 * be constant.
1506 MARK_STREAM_NONCONST(next->pos);
1511 struct token * preprocess(struct token *token)
1513 preprocessing = 1;
1514 init_preprocessor();
1515 do_preprocess(&token);
1517 // Drop all expressions from pre-processing, they're not used any more.
1518 clear_expression_alloc();
1519 preprocessing = 0;
1521 return token;