[PATCH] saner handling of #if[n]def syntax errors
[smatch.git] / pre-process.c
blob001687359c3509753b8ade014ab48dcf51a38652
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"
29 #include "scope.h"
31 static int false_nesting = 0;
32 static int if_nesting = 0;
33 static struct position unmatched_if_pos;
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 struct symbol *lookup_macro(struct ident *ident)
98 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_INVISIBLEMACRO);
99 if (sym && sym->namespace != NS_MACRO)
100 sym = NULL;
101 return sym;
104 static int token_defined(struct token *token)
106 if (token_type(token) == TOKEN_IDENT) {
107 struct symbol *sym = lookup_macro(token->ident);
108 if (sym) {
109 sym->weak = 0;
110 return 1;
112 return 0;
115 sparse_error(token->pos, "expected preprocessor identifier");
116 return 0;
119 static void replace_with_defined(struct token *token)
121 static const char *string[] = { "0", "1" };
122 int defined = token_defined(token);
124 token_type(token) = TOKEN_NUMBER;
125 token->number = string[defined];
128 static int expand_one_symbol(struct token **list)
130 struct token *token = *list;
131 struct symbol *sym;
133 if (token->pos.noexpand)
134 return 1;
136 sym = lookup_macro(token->ident);
137 if (sym) {
138 sym->weak = 0;
139 return expand(list, sym);
141 if (token->ident == &__LINE___ident) {
142 replace_with_integer(token, token->pos.line);
143 } else if (token->ident == &__FILE___ident) {
144 replace_with_string(token, stream_name(token->pos.stream));
146 return 1;
149 static inline struct token *scan_next(struct token **where)
151 struct token *token = *where;
152 if (token_type(token) != TOKEN_UNTAINT)
153 return token;
154 do {
155 token->ident->tainted = 0;
156 token = token->next;
157 } while (token_type(token) == TOKEN_UNTAINT);
158 *where = token;
159 return token;
162 static void expand_list(struct token **list)
164 struct token *next;
165 while (!eof_token(next = scan_next(list))) {
166 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
167 list = &next->next;
171 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
173 struct token **p = &prev->next;
174 struct token *next;
175 int nesting = 0;
177 while (!eof_token(next = scan_next(p))) {
178 if (match_op(next, '(')) {
179 nesting++;
180 } else if (match_op(next, ')')) {
181 if (!nesting--)
182 break;
183 } else if (match_op(next, ',') && !nesting && !vararg) {
184 break;
186 next->pos.stream = pos->stream;
187 next->pos.line = pos->line;
188 next->pos.pos = pos->pos;
189 p = &next->next;
191 *p = &eof_token_entry;
192 return next;
196 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
199 struct arg {
200 struct token *arg;
201 struct token *expanded;
202 struct token *str;
203 int n_normal;
204 int n_quoted;
205 int n_str;
208 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
210 int wanted = arglist->count.normal;
211 struct token *next = NULL;
212 int count = 0;
214 arglist = arglist->next; /* skip counter */
216 if (!wanted) {
217 next = collect_arg(start, 0, &what->pos);
218 if (eof_token(next))
219 goto Eclosing;
220 if (!eof_token(start->next) || !match_op(next, ')')) {
221 count++;
222 goto Emany;
224 } else {
225 for (count = 0; count < wanted; count++) {
226 struct argcount *p = &arglist->next->count;
227 next = collect_arg(start, p->vararg, &what->pos);
228 arglist = arglist->next->next;
229 if (eof_token(next))
230 goto Eclosing;
231 args[count].arg = start->next;
232 args[count].n_normal = p->normal;
233 args[count].n_quoted = p->quoted;
234 args[count].n_str = p->str;
235 if (match_op(next, ')')) {
236 count++;
237 break;
239 start = next;
241 if (count == wanted && !match_op(next, ')'))
242 goto Emany;
243 if (count == wanted - 1) {
244 struct argcount *p = &arglist->next->count;
245 if (!p->vararg)
246 goto Efew;
247 args[count].arg = NULL;
248 args[count].n_normal = p->normal;
249 args[count].n_quoted = p->quoted;
250 args[count].n_str = p->str;
252 if (count < wanted - 1)
253 goto Efew;
255 what->next = next->next;
256 return 1;
258 Efew:
259 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
260 show_token(what), wanted, count);
261 goto out;
262 Emany:
263 while (match_op(next, ',')) {
264 next = collect_arg(next, 0, &what->pos);
265 count++;
267 if (eof_token(next))
268 goto Eclosing;
269 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
270 show_token(what), count, wanted);
271 goto out;
272 Eclosing:
273 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
274 show_token(what));
275 out:
276 what->next = next->next;
277 return 0;
280 static struct token *dup_list(struct token *list)
282 struct token *res;
283 struct token **p = &res;
285 while (!eof_token(list)) {
286 struct token *newtok = __alloc_token(0);
287 *newtok = *list;
288 *p = newtok;
289 p = &newtok->next;
290 list = list->next;
292 return res;
295 static struct token *stringify(struct token *arg)
297 const char *s = show_token_sequence(arg);
298 int size = strlen(s)+1;
299 struct token *token = __alloc_token(0);
300 struct string *string = __alloc_string(size);
302 memcpy(string->data, s, size);
303 string->length = size;
304 token->pos = arg->pos;
305 token_type(token) = TOKEN_STRING;
306 token->string = string;
307 token->next = &eof_token_entry;
308 return token;
311 static void expand_arguments(int count, struct arg *args)
313 int i;
314 for (i = 0; i < count; i++) {
315 struct token *arg = args[i].arg;
316 if (!arg)
317 arg = &eof_token_entry;
318 if (args[i].n_str)
319 args[i].str = stringify(arg);
320 if (args[i].n_normal) {
321 if (!args[i].n_quoted) {
322 args[i].expanded = arg;
323 args[i].arg = NULL;
324 } else if (eof_token(arg)) {
325 args[i].expanded = arg;
326 } else {
327 args[i].expanded = dup_list(arg);
329 expand_list(&args[i].expanded);
335 * Possibly valid combinations:
336 * - ident + ident -> ident
337 * - ident + number -> ident unless number contains '.', '+' or '-'.
338 * - number + number -> number
339 * - number + ident -> number
340 * - number + '.' -> number
341 * - number + '+' or '-' -> number, if number used to end on [eEpP].
342 * - '.' + number -> number, if number used to start with a digit.
343 * - special + special -> either special or an error.
345 static enum token_type combine(struct token *left, struct token *right, char *p)
347 int len;
348 enum token_type t1 = token_type(left), t2 = token_type(right);
350 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
351 return TOKEN_ERROR;
353 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
354 return TOKEN_ERROR;
356 strcpy(p, show_token(left));
357 strcat(p, show_token(right));
358 len = strlen(p);
360 if (len >= 256)
361 return TOKEN_ERROR;
363 if (t1 == TOKEN_IDENT) {
364 if (t2 == TOKEN_SPECIAL)
365 return TOKEN_ERROR;
366 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
367 return TOKEN_ERROR;
368 return TOKEN_IDENT;
371 if (t1 == TOKEN_NUMBER) {
372 if (t2 == TOKEN_SPECIAL) {
373 switch (right->special) {
374 case '.':
375 break;
376 case '+': case '-':
377 if (strchr("eEpP", p[len - 2]))
378 break;
379 default:
380 return TOKEN_ERROR;
383 return TOKEN_NUMBER;
386 if (p[0] == '.' && isdigit((unsigned char)p[1]))
387 return TOKEN_NUMBER;
389 return TOKEN_SPECIAL;
392 static int merge(struct token *left, struct token *right)
394 static char buffer[512];
395 int n;
397 switch (combine(left, right, buffer)) {
398 case TOKEN_IDENT:
399 left->ident = built_in_ident(buffer);
400 left->pos.noexpand = 0;
401 return 1;
403 case TOKEN_NUMBER: {
404 char *number = __alloc_bytes(strlen(buffer) + 1);
405 memcpy(number, buffer, strlen(buffer) + 1);
406 token_type(left) = TOKEN_NUMBER; /* could be . + num */
407 left->number = number;
408 return 1;
411 case TOKEN_SPECIAL:
412 if (buffer[2] && buffer[3])
413 break;
414 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
415 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
416 left->special = n;
417 return 1;
420 default:
423 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
424 return 0;
427 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
429 struct token *alloc = alloc_token(streampos);
430 token_type(alloc) = token_type(token);
431 alloc->pos.newline = pos->newline;
432 alloc->pos.whitespace = pos->whitespace;
433 alloc->number = token->number;
434 alloc->pos.noexpand = token->pos.noexpand;
435 return alloc;
438 static struct token **copy(struct token **where, struct token *list, int *count)
440 int need_copy = --*count;
441 while (!eof_token(list)) {
442 struct token *token;
443 if (need_copy)
444 token = dup_token(list, &list->pos, &list->pos);
445 else
446 token = list;
447 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
448 token->pos.noexpand = 1;
449 *where = token;
450 where = &token->next;
451 list = list->next;
453 *where = &eof_token_entry;
454 return where;
457 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
459 struct token *token = *list;
460 struct position *base_pos = &token->pos;
461 struct position *pos = base_pos;
462 int *count;
463 enum {Normal, Placeholder, Concat} state = Normal;
465 for (; !eof_token(body); body = body->next, pos = &body->pos) {
466 struct token *added, *arg;
467 struct token **tail;
469 switch (token_type(body)) {
470 case TOKEN_GNU_KLUDGE:
472 * GNU kludge: if we had <comma>##<vararg>, behaviour
473 * depends on whether we had enough arguments to have
474 * a vararg. If we did, ## is just ignored. Otherwise
475 * both , and ## are ignored. Comma should come from
476 * the body of macro and not be an argument of earlier
477 * concatenation.
479 if (!args[body->next->argnum].arg)
480 continue;
481 added = dup_token(body, base_pos, pos);
482 token_type(added) = TOKEN_SPECIAL;
483 tail = &added->next;
484 break;
486 case TOKEN_STR_ARGUMENT:
487 arg = args[body->argnum].str;
488 count = &args[body->argnum].n_str;
489 goto copy_arg;
491 case TOKEN_QUOTED_ARGUMENT:
492 arg = args[body->argnum].arg;
493 count = &args[body->argnum].n_quoted;
494 if (!arg || eof_token(arg)) {
495 if (state == Concat)
496 state = Normal;
497 else
498 state = Placeholder;
499 continue;
501 goto copy_arg;
503 case TOKEN_MACRO_ARGUMENT:
504 arg = args[body->argnum].expanded;
505 count = &args[body->argnum].n_normal;
506 if (eof_token(arg)) {
507 state = Normal;
508 continue;
510 copy_arg:
511 tail = copy(&added, arg, count);
512 added->pos.newline = pos->newline;
513 added->pos.whitespace = pos->whitespace;
514 break;
516 case TOKEN_CONCAT:
517 if (state == Placeholder)
518 state = Normal;
519 else
520 state = Concat;
521 continue;
523 case TOKEN_IDENT:
524 added = dup_token(body, base_pos, pos);
525 if (added->ident->tainted)
526 added->pos.noexpand = 1;
527 tail = &added->next;
528 break;
530 default:
531 added = dup_token(body, base_pos, pos);
532 tail = &added->next;
533 break;
537 * if we got to doing real concatenation, we already have
538 * added something into the list, so containing_token() is OK.
540 if (state == Concat && merge(containing_token(list), added)) {
541 *list = added->next;
542 if (tail != &added->next)
543 list = tail;
544 } else {
545 *list = added;
546 list = tail;
548 state = Normal;
550 *list = &eof_token_entry;
551 return list;
554 static int expand(struct token **list, struct symbol *sym)
556 struct token *last;
557 struct token *token = *list;
558 struct ident *expanding = token->ident;
559 struct token **tail;
560 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
561 struct arg args[nargs];
563 if (expanding->tainted) {
564 token->pos.noexpand = 1;
565 return 1;
568 if (sym->arglist) {
569 if (!match_op(scan_next(&token->next), '('))
570 return 1;
571 if (!collect_arguments(token->next, sym->arglist, args, token))
572 return 1;
573 expand_arguments(nargs, args);
576 expanding->tainted = 1;
578 last = token->next;
579 tail = substitute(list, sym->expansion, args);
580 *tail = last;
582 return 0;
585 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
587 struct token *last;
588 static char buffer[256];
589 char *ptr = buffer;
591 last = token;
592 while (!eof_token(token) && !match_op(token, endop)) {
593 int len;
594 const char *val = token->string->data;
595 if (token_type(token) != TOKEN_STRING)
596 val = show_token(token);
597 len = strlen(val);
598 memcpy(ptr, val, len);
599 ptr += len;
600 token = token->next;
602 *ptr = 0;
603 if (endop && !match_op(token, endop))
604 sparse_error(start->pos, "expected '>' at end of filename");
605 return buffer;
608 static int already_tokenized(const char *path)
610 int i;
611 struct stream *s = input_streams;
613 for (i = input_stream_nr; --i >= 0; s++) {
614 if (s->constant != CONSTANT_FILE_YES)
615 continue;
616 if (strcmp(path, s->name))
617 continue;
618 if (!lookup_macro(s->protect))
619 continue;
620 return 1;
622 return 0;
625 /* Hande include of header files.
626 * The relevant options are made compatible with gcc. The only options that
627 * are not supported is -withprefix and friends.
629 * Three set of include paths are known:
630 * quote_includepath: Path to search when using #include "file.h"
631 * angle_includepath: Path to search when using #include <file.h>
632 * sys_includepath: Built-in include paths
634 * The above is implmented as one array with pointes
635 * +--------------+
636 * quote_includepath ---> | |
637 * +--------------+
638 * | |
639 * +--------------+
640 * angle_includepath ---> | |
641 * +--------------+
642 * sys_includepath ---> | |
643 * +--------------+
644 * | |
645 * +--------------+
647 * -I dir insert dir just before sys_includepath and move the rest
648 * -I- makes all dirs specified with -I before to quote dirs only and
649 * angle_includepath is set equal to sys_includepath.
650 * -nostdinc removes all sys dirs be storing NULL in entry pointed
651 * to by * sys_includepath. Note this will reset all dirs built-in and added
652 * before -nostdinc by -isystem and -dirafter
653 * -isystem dir adds dir where sys_includepath points adding this dir as
654 * first systemdir
655 * -dirafter dir adds dir to the end of the list
658 static void set_stream_include_path(struct stream *stream)
660 const char *path = stream->path;
661 if (!path) {
662 const char *p = strrchr(stream->name, '/');
663 path = "";
664 if (p) {
665 int len = p - stream->name + 1;
666 char *m = malloc(len+1);
667 /* This includes the final "/" */
668 memcpy(m, stream->name, len);
669 m[len] = 0;
670 path = m;
672 stream->path = path;
674 includepath[0] = path;
677 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
679 int fd;
680 int plen = strlen(path);
681 static char fullname[PATH_MAX];
683 memcpy(fullname, path, plen);
684 if (plen && path[plen-1] != '/') {
685 fullname[plen] = '/';
686 plen++;
688 memcpy(fullname+plen, filename, flen);
689 if (already_tokenized(fullname))
690 return 1;
691 fd = open(fullname, O_RDONLY);
692 if (fd >= 0) {
693 char * streamname = __alloc_bytes(plen + flen);
694 memcpy(streamname, fullname, plen + flen);
695 *where = tokenize(streamname, fd, *where, next_path);
696 close(fd);
697 return 1;
699 return 0;
702 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
704 const char *path;
706 while ((path = *pptr++) != NULL) {
707 if (!try_include(path, filename, flen, list, pptr))
708 continue;
709 return 1;
711 return 0;
714 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
716 int flen = strlen(filename) + 1;
718 /* Absolute path? */
719 if (filename[0] == '/') {
720 if (try_include("", filename, flen, list, includepath))
721 return;
722 goto out;
725 /* Dir of inputfile is first dir to search for quoted includes */
726 set_stream_include_path(stream);
728 if (!path)
729 /* Do not search quote include if <> is in use */
730 path = local ? quote_includepath : angle_includepath;
732 /* Check the standard include paths.. */
733 if (do_include_path(path, list, token, filename, flen))
734 return;
735 out:
736 error_die(token->pos, "unable to open '%s'", filename);
739 static int free_preprocessor_line(struct token *token)
741 do {
742 struct token *free = token;
743 token = token->next;
744 __free_token(free);
745 } while (token_type(token) != TOKEN_EOF);
746 return 1;
749 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
751 const char *filename;
752 struct token *next;
753 int expect;
755 if (false_nesting)
756 return free_preprocessor_line(token);
758 if (stream->constant == CONSTANT_FILE_MAYBE)
759 MARK_STREAM_NONCONST(token->pos);
761 next = token->next;
762 expect = '>';
763 if (!match_op(next, '<')) {
764 expand_list(&token->next);
765 expect = 0;
766 next = token;
767 if (match_op(token->next, '<')) {
768 next = token->next;
769 expect = '>';
772 token = next->next;
773 filename = token_name_sequence(token, expect, token);
774 do_include(!expect, stream, list, token, filename, path);
775 return 1;
778 static int handle_include(struct stream *stream, struct token **list, struct token *token)
780 return handle_include_path(stream, list, token, NULL);
783 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
785 return handle_include_path(stream, list, token, stream->next_path);
788 static int token_different(struct token *t1, struct token *t2)
790 int different;
792 if (token_type(t1) != token_type(t2))
793 return 1;
795 switch (token_type(t1)) {
796 case TOKEN_IDENT:
797 different = t1->ident != t2->ident;
798 break;
799 case TOKEN_ARG_COUNT:
800 case TOKEN_UNTAINT:
801 case TOKEN_CONCAT:
802 case TOKEN_GNU_KLUDGE:
803 different = 0;
804 break;
805 case TOKEN_NUMBER:
806 different = strcmp(t1->number, t2->number);
807 break;
808 case TOKEN_SPECIAL:
809 different = t1->special != t2->special;
810 break;
811 case TOKEN_MACRO_ARGUMENT:
812 case TOKEN_QUOTED_ARGUMENT:
813 case TOKEN_STR_ARGUMENT:
814 different = t1->argnum != t2->argnum;
815 break;
816 case TOKEN_CHAR:
817 different = t1->character != t2->character;
818 break;
819 case TOKEN_STRING: {
820 struct string *s1, *s2;
822 s1 = t1->string;
823 s2 = t2->string;
824 different = 1;
825 if (s1->length != s2->length)
826 break;
827 different = memcmp(s1->data, s2->data, s1->length);
828 break;
830 default:
831 different = 1;
832 break;
834 return different;
837 static int token_list_different(struct token *list1, struct token *list2)
839 for (;;) {
840 if (list1 == list2)
841 return 0;
842 if (!list1 || !list2)
843 return 1;
844 if (token_different(list1, list2))
845 return 1;
846 list1 = list1->next;
847 list2 = list2->next;
851 static inline void set_arg_count(struct token *token)
853 token_type(token) = TOKEN_ARG_COUNT;
854 token->count.normal = token->count.quoted =
855 token->count.str = token->count.vararg = 0;
858 static struct token *parse_arguments(struct token *list)
860 struct token *arg = list->next, *next = list;
861 struct argcount *count = &list->count;
863 set_arg_count(list);
865 if (match_op(arg, ')')) {
866 next = arg->next;
867 list->next = &eof_token_entry;
868 return next;
871 while (token_type(arg) == TOKEN_IDENT) {
872 if (arg->ident == &__VA_ARGS___ident)
873 goto Eva_args;
874 if (!++count->normal)
875 goto Eargs;
876 next = arg->next;
878 if (match_op(next, ',')) {
879 set_arg_count(next);
880 arg = next->next;
881 continue;
884 if (match_op(next, ')')) {
885 set_arg_count(next);
886 next = next->next;
887 arg->next->next = &eof_token_entry;
888 return next;
891 /* normal cases are finished here */
893 if (match_op(next, SPECIAL_ELLIPSIS)) {
894 if (match_op(next->next, ')')) {
895 set_arg_count(next);
896 next->count.vararg = 1;
897 next = next->next;
898 arg->next->next = &eof_token_entry;
899 return next->next;
902 arg = next;
903 goto Enotclosed;
906 if (eof_token(next)) {
907 goto Enotclosed;
908 } else {
909 arg = next;
910 goto Ebadstuff;
914 if (match_op(arg, SPECIAL_ELLIPSIS)) {
915 next = arg->next;
916 token_type(arg) = TOKEN_IDENT;
917 arg->ident = &__VA_ARGS___ident;
918 if (!match_op(next, ')'))
919 goto Enotclosed;
920 if (!++count->normal)
921 goto Eargs;
922 set_arg_count(next);
923 next->count.vararg = 1;
924 next = next->next;
925 arg->next->next = &eof_token_entry;
926 return next;
929 if (eof_token(arg)) {
930 arg = next;
931 goto Enotclosed;
933 if (match_op(arg, ','))
934 goto Emissing;
935 else
936 goto Ebadstuff;
939 Emissing:
940 sparse_error(arg->pos, "parameter name missing");
941 return NULL;
942 Ebadstuff:
943 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
944 show_token(arg));
945 return NULL;
946 Enotclosed:
947 sparse_error(arg->pos, "missing ')' in macro parameter list");
948 return NULL;
949 Eva_args:
950 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
951 return NULL;
952 Eargs:
953 sparse_error(arg->pos, "too many arguments in macro definition");
954 return NULL;
957 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
959 struct ident *ident = token->ident;
960 int nr;
962 if (!arglist || token_type(token) != TOKEN_IDENT)
963 return 0;
965 arglist = arglist->next;
967 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
968 if (arglist->ident == ident) {
969 struct argcount *count = &arglist->next->count;
970 int n;
972 token->argnum = nr;
973 token_type(token) = type;
974 switch (type) {
975 case TOKEN_MACRO_ARGUMENT:
976 n = ++count->normal;
977 break;
978 case TOKEN_QUOTED_ARGUMENT:
979 n = ++count->quoted;
980 break;
981 default:
982 n = ++count->str;
984 if (n)
985 return count->vararg ? 2 : 1;
986 token_type(token) = TOKEN_ERROR;
987 return -1;
990 return 0;
993 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
995 struct token *token = expansion;
996 struct token **p;
997 struct token *last = NULL;
999 if (match_op(token, SPECIAL_HASHHASH))
1000 goto Econcat;
1002 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1003 if (match_op(token, '#')) {
1004 if (arglist) {
1005 struct token *next = token->next;
1006 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1007 goto Equote;
1008 next->pos.whitespace = token->pos.whitespace;
1009 token = *p = next;
1010 } else {
1011 token->pos.noexpand = 1;
1013 } else if (match_op(token, SPECIAL_HASHHASH)) {
1014 struct token *next = token->next;
1015 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
1016 token_type(token) = TOKEN_CONCAT;
1017 if (arg) {
1018 token = next;
1019 /* GNU kludge */
1020 if (arg == 2 && last && match_op(last, ',')) {
1021 token_type(last) = TOKEN_GNU_KLUDGE;
1022 last->next = token;
1024 } else if (match_op(next, SPECIAL_HASHHASH))
1025 token = next;
1026 else if (eof_token(next))
1027 goto Econcat;
1028 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
1029 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1030 } else {
1031 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1033 if (token_type(token) == TOKEN_ERROR)
1034 goto Earg;
1035 last = token;
1037 token = alloc_token(&expansion->pos);
1038 token_type(token) = TOKEN_UNTAINT;
1039 token->ident = name;
1040 token->next = *p;
1041 *p = token;
1042 return expansion;
1044 Equote:
1045 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1046 return NULL;
1048 Econcat:
1049 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1050 return NULL;
1051 Earg:
1052 sparse_error(token->pos, "too many instances of argument in body");
1053 return NULL;
1056 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int weak)
1058 struct token *arglist, *expansion;
1059 struct token *left = token->next;
1060 struct symbol *sym;
1061 struct ident *name;
1063 if (false_nesting)
1064 return free_preprocessor_line(token);
1066 if (token_type(left) != TOKEN_IDENT) {
1067 sparse_error(token->pos, "expected identifier to 'define'");
1068 return 0;
1071 if (stream->constant == CONSTANT_FILE_MAYBE)
1072 MARK_STREAM_NONCONST(token->pos);
1074 __free_token(token); /* Free the "define" token, but not the rest of the line */
1075 name = left->ident;
1077 arglist = NULL;
1078 expansion = left->next;
1079 if (!expansion->pos.whitespace && match_op(expansion, '(')) {
1080 arglist = expansion;
1081 expansion = parse_arguments(expansion);
1082 if (!expansion)
1083 return 1;
1086 expansion = parse_expansion(expansion, arglist, name);
1087 if (!expansion)
1088 return 1;
1090 sym = lookup_macro(name);
1091 if (sym) {
1092 if (token_list_different(sym->expansion, expansion) ||
1093 token_list_different(sym->arglist, arglist)) {
1094 if (sym->weak)
1095 goto replace_it;
1096 if (weak)
1097 return 1;
1098 warning(left->pos, "preprocessor token %.*s redefined",
1099 name->len, name->name);
1100 info(sym->pos, "this was the original definition");
1102 /* Don't overwrite global defs */
1103 if (sym->scope != file_scope)
1104 goto allocate_new;
1105 sym->expansion = expansion;
1106 sym->arglist = arglist;
1108 return 1;
1110 allocate_new:
1111 sym = alloc_symbol(left->pos, SYM_NODE);
1112 bind_symbol(sym, name, NS_MACRO);
1114 replace_it:
1115 sym->expansion = expansion;
1116 sym->arglist = arglist;
1117 sym->weak = weak;
1118 return 1;
1121 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1123 return do_handle_define(stream, line, token, 0);
1126 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1128 return do_handle_define(stream, line, token, 1);
1131 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1133 struct token *left = token->next;
1134 struct symbol **sym;
1136 if (false_nesting)
1137 return free_preprocessor_line(token);
1139 if (token_type(left) != TOKEN_IDENT) {
1140 sparse_error(token->pos, "expected identifier to 'undef'");
1141 return 0;
1144 if (stream->constant == CONSTANT_FILE_MAYBE)
1145 MARK_STREAM_NONCONST(token->pos);
1147 sym = &left->ident->symbols;
1148 while (*sym) {
1149 struct symbol *t = *sym;
1150 if (t->namespace & (NS_MACRO | NS_INVISIBLEMACRO)) {
1151 t->namespace = NS_INVISIBLEMACRO;
1152 return 1;
1154 sym = &t->next_id;
1156 return free_preprocessor_line(token);
1159 static int preprocessor_if(struct token *token, int true)
1161 if (if_nesting == 0)
1162 unmatched_if_pos = token->pos;
1163 if (if_nesting >= MAX_NEST)
1164 error_die(token->pos, "Maximum preprocessor conditional level exhausted");
1165 elif_ignore[if_nesting++] = (false_nesting || true) ? ELIF_IGNORE : 0;
1166 if (false_nesting || true != 1)
1167 false_nesting++;
1168 return free_preprocessor_line(token);
1171 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1173 struct token *next = token->next;
1174 int arg;
1175 if (token_type(next) == TOKEN_IDENT) {
1176 arg = token_defined(next);
1177 } else {
1178 if (!false_nesting)
1179 sparse_error(token->pos, "expected preprocessor identifier");
1180 arg = -1;
1182 return preprocessor_if(token, arg);
1185 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1187 struct token *next = token->next;
1188 int arg;
1189 if (token_type(next) == TOKEN_IDENT) {
1190 if (stream->constant == CONSTANT_FILE_MAYBE) {
1191 if (!stream->protect || stream->protect == next->ident) {
1192 stream->constant = CONSTANT_FILE_IFNDEF;
1193 stream->protect = next->ident;
1194 } else
1195 MARK_STREAM_NONCONST(token->pos);
1197 arg = !token_defined(next);
1198 } else {
1199 if (stream->constant == CONSTANT_FILE_MAYBE)
1200 MARK_STREAM_NONCONST(token->pos);
1201 if (!false_nesting)
1202 sparse_error(token->pos, "expected preprocessor identifier");
1203 arg = -1;
1206 return preprocessor_if(token, arg);
1210 * Expression handling for #if and #elif; it differs from normal expansion
1211 * due to special treatment of "defined".
1213 static int expression_value(struct token **where)
1215 struct expression *expr;
1216 struct token *p;
1217 struct token **list = where, **beginning = NULL;
1218 long long value;
1219 int state = 0;
1221 while (!eof_token(p = scan_next(list))) {
1222 switch (state) {
1223 case 0:
1224 if (token_type(p) != TOKEN_IDENT)
1225 break;
1226 if (p->ident == &defined_ident) {
1227 state = 1;
1228 beginning = list;
1229 break;
1231 if (!expand_one_symbol(list))
1232 continue;
1233 if (token_type(p) != TOKEN_IDENT)
1234 break;
1235 token_type(p) = TOKEN_ZERO_IDENT;
1236 break;
1237 case 1:
1238 if (match_op(p, '(')) {
1239 state = 2;
1240 } else {
1241 state = 0;
1242 replace_with_defined(p);
1243 *beginning = p;
1245 break;
1246 case 2:
1247 if (token_type(p) == TOKEN_IDENT)
1248 state = 3;
1249 else
1250 state = 0;
1251 replace_with_defined(p);
1252 *beginning = p;
1253 break;
1254 case 3:
1255 state = 0;
1256 if (!match_op(p, ')'))
1257 sparse_error(p->pos, "missing ')' after \"defined\"");
1258 *list = p->next;
1259 continue;
1261 list = &p->next;
1264 p = constant_expression(*where, &expr);
1265 if (!eof_token(p))
1266 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p));
1267 value = get_expression_value(expr);
1268 return value != 0;
1271 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1273 int value = 0;
1274 if (!false_nesting)
1275 value = expression_value(&token->next);
1277 // This is an approximation. We really only need this if the
1278 // condition does depends on a pre-processor symbol. Note, that
1279 // the important #ifndef case has already changed ->constant.
1280 if (stream->constant == CONSTANT_FILE_MAYBE)
1281 MARK_STREAM_NONCONST(token->pos);
1283 return preprocessor_if(token, value);
1286 static int handle_elif(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 sparse_error(token->pos, "unmatched #elif within stream");
1294 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1295 sparse_error(token->pos, "#elif after #else");
1297 if (false_nesting) {
1298 /* If this whole if-thing is if'ed out, an elif cannot help */
1299 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1300 return 1;
1301 if (expression_value(&token->next)) {
1302 false_nesting = 0;
1303 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1305 } else {
1306 false_nesting = 1;
1308 return free_preprocessor_line(token);
1311 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1313 if (stream->nesting == if_nesting)
1314 MARK_STREAM_NONCONST(token->pos);
1316 if (stream->nesting > if_nesting)
1317 sparse_error(token->pos, "unmatched #else within stream");
1319 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE)
1320 sparse_error(token->pos, "#else after #else");
1321 else
1322 elif_ignore[if_nesting-1] |= ELIF_SEEN_ELSE;
1324 if (false_nesting) {
1325 /* If this whole if-thing is if'ed out, an else cannot help */
1326 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1327 return 1;
1328 false_nesting = 0;
1329 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1330 } else {
1331 false_nesting = 1;
1333 return free_preprocessor_line(token);
1336 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1338 if (stream->constant == CONSTANT_FILE_IFNDEF && stream->nesting == if_nesting)
1339 stream->constant = CONSTANT_FILE_MAYBE;
1341 if (stream->nesting > if_nesting)
1342 sparse_error(token->pos, "unmatched #endif in stream");
1343 if (false_nesting)
1344 false_nesting--;
1345 if_nesting--;
1346 if (!token)
1347 return 1;
1348 return free_preprocessor_line(token);
1351 static const char *show_token_sequence(struct token *token)
1353 static char buffer[1024];
1354 char *ptr = buffer;
1355 int whitespace = 0;
1357 if (!token)
1358 return "<none>";
1359 while (!eof_token(token)) {
1360 const char *val = show_token(token);
1361 int len = strlen(val);
1363 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1364 sparse_error(token->pos, "too long token expansion");
1365 break;
1368 if (whitespace)
1369 *ptr++ = ' ';
1370 memcpy(ptr, val, len);
1371 ptr += len;
1372 token = token->next;
1373 whitespace = token->pos.whitespace;
1375 *ptr = 0;
1376 return buffer;
1379 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1381 if (false_nesting)
1382 return free_preprocessor_line(token);
1383 if (stream->constant == CONSTANT_FILE_MAYBE)
1384 MARK_STREAM_NONCONST(token->pos);
1385 warning(token->pos, "%s", show_token_sequence(token->next));
1386 return free_preprocessor_line(token);
1389 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1391 if (false_nesting)
1392 return free_preprocessor_line(token);
1393 if (stream->constant == CONSTANT_FILE_MAYBE)
1394 MARK_STREAM_NONCONST(token->pos);
1395 sparse_error(token->pos, "%s", show_token_sequence(token->next));
1396 return free_preprocessor_line(token);
1399 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1401 if (false_nesting)
1402 return free_preprocessor_line(token);
1405 * Do we have any non-system includes?
1406 * Clear them out if so..
1408 *sys_includepath = NULL;
1409 return free_preprocessor_line(token);
1412 static void add_path_entry(struct token *token, const char *path, const char ***where, const char **new_path)
1414 const char **dst;
1415 const char *next;
1417 /* Need one free entry.. */
1418 if (includepath[INCLUDEPATHS-2])
1419 error_die(token->pos, "too many include path entries");
1421 /* check that this is not a duplicate */
1422 dst = includepath;
1423 while (*dst) {
1424 if (strcmp(*dst, path) == 0)
1425 return;
1426 dst++;
1428 next = path;
1429 dst = *where;
1430 *where = new_path;
1433 * Move them all up starting at dst,
1434 * insert the new entry..
1436 for (;;) {
1437 const char *tmp = *dst;
1438 *dst = next;
1439 if (!next)
1440 break;
1441 next = tmp;
1442 dst++;
1446 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1448 for (;;) {
1449 token = token->next;
1450 if (eof_token(token))
1451 return 1;
1452 if (token_type(token) != TOKEN_STRING) {
1453 warning(token->pos, "expected path string");
1454 return 1;
1456 add_path_entry(token, token->string->data, &sys_includepath, sys_includepath + 1);
1460 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1462 for (;;) {
1463 token = token->next;
1464 if (eof_token(token))
1465 return 1;
1466 if (token_type(token) != TOKEN_STRING) {
1467 sparse_error(token->pos, "expected path string");
1468 return 1;
1470 add_path_entry(token, token->string->data, &sys_includepath, sys_includepath);
1474 /* Add to end on includepath list - no pointer updates */
1475 static void add_dirafter_entry(struct token *token, const char *path)
1477 const char **dst = includepath;
1479 /* Need one free entry.. */
1480 if (includepath[INCLUDEPATHS-2])
1481 error_die(token->pos, "too many include path entries");
1483 /* Add to the end */
1484 while (*dst)
1485 dst++;
1486 *dst = path;
1487 dst++;
1488 *dst = NULL;
1491 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1493 for (;;) {
1494 token = token->next;
1495 if (eof_token(token))
1496 return 1;
1497 if (token_type(token) != TOKEN_STRING) {
1498 sparse_error(token->pos, "expected path string");
1499 return 1;
1501 add_dirafter_entry(token, token->string->data);
1505 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1507 if (false_nesting)
1508 return free_preprocessor_line(token);
1511 * -I-
1512 * From info gcc:
1513 * Split the include path. Any directories specified with `-I'
1514 * options before `-I-' are searched only for headers requested with
1515 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1516 * If additional directories are specified with `-I' options after
1517 * the `-I-', those directories are searched for all `#include'
1518 * directives.
1519 * In addition, `-I-' inhibits the use of the directory of the current
1520 * file directory as the first search directory for `#include "FILE"'.
1522 quote_includepath = includepath+1;
1523 angle_includepath = sys_includepath;
1524 return free_preprocessor_line(token);
1528 * We replace "#pragma xxx" with "__pragma__" in the token
1529 * stream. Just as an example.
1531 * We'll just #define that away for now, but the theory here
1532 * is that we can use this to insert arbitrary token sequences
1533 * to turn the pragma's into internal front-end sequences for
1534 * when we actually start caring about them.
1536 * So eventually this will turn into some kind of extended
1537 * __attribute__() like thing, except called __pragma__(xxx).
1539 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1541 struct token *next = *line;
1543 token->ident = &pragma_ident;
1544 token->pos.newline = 1;
1545 token->pos.whitespace = 1;
1546 token->pos.pos = 1;
1547 *line = token;
1548 token->next = next;
1549 return 1;
1553 * We ignore #line for now.
1555 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1557 return 1;
1561 static void init_preprocessor(void)
1563 int i;
1564 int stream = init_stream("preprocessor", -1, includepath);
1565 static struct {
1566 const char *name;
1567 int (*handler)(struct stream *, struct token **, struct token *);
1568 } handlers[] = {
1569 { "define", handle_define },
1570 { "weak_define",handle_weak_define },
1571 { "undef", handle_undef },
1572 { "ifdef", handle_ifdef },
1573 { "ifndef", handle_ifndef },
1574 { "else", handle_else },
1575 { "endif", handle_endif },
1576 { "if", handle_if },
1577 { "elif", handle_elif },
1578 { "warning", handle_warning },
1579 { "error", handle_error },
1580 { "include", handle_include },
1581 { "include_next",handle_include_next },
1582 { "pragma", handle_pragma },
1583 { "line", handle_line },
1585 // our internal preprocessor tokens
1586 { "nostdinc", handle_nostdinc },
1587 { "add_include", handle_add_include },
1588 { "add_isystem", handle_add_isystem },
1589 { "add_dirafter", handle_add_dirafter },
1590 { "split_include", handle_split_include },
1593 for (i = 0; i < (sizeof (handlers) / sizeof (handlers[0])); i++) {
1594 struct symbol *sym;
1595 sym = create_symbol(stream, handlers[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1596 sym->handler = handlers[i].handler;
1600 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1602 struct token *token = start->next;
1604 if (!token)
1605 return;
1607 if (token_type(token) == TOKEN_NUMBER)
1608 if (handle_line(stream, line, start))
1609 return;
1611 if (token_type(token) == TOKEN_IDENT) {
1612 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1613 if (sym && sym->handler(stream, line, token))
1614 return;
1617 if (false_nesting)
1618 return;
1620 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1623 static void preprocessor_line(struct stream *stream, struct token **line)
1625 struct token *start = *line, *next;
1626 struct token **tp = &start->next;
1628 for (;;) {
1629 next = *tp;
1630 if (next->pos.newline)
1631 break;
1632 tp = &next->next;
1634 *line = next;
1635 *tp = &eof_token_entry;
1636 handle_preprocessor_line(stream, line, start);
1639 static void do_preprocess(struct token **list)
1641 struct token *next;
1643 while (!eof_token(next = scan_next(list))) {
1644 struct stream *stream = input_streams + next->pos.stream;
1646 if (next->pos.newline && match_op(next, '#')) {
1647 if (!next->pos.noexpand) {
1648 preprocessor_line(stream, list);
1649 __free_token(next); /* Free the '#' token */
1650 continue;
1654 switch (token_type(next)) {
1655 case TOKEN_STREAMEND:
1656 if (stream->nesting < if_nesting + 1) {
1657 sparse_error(unmatched_if_pos, "unterminated preprocessor conditional");
1658 // Pretend to see a series of #endifs
1659 MARK_STREAM_NONCONST(next->pos);
1660 do {
1661 handle_endif (stream, NULL, NULL);
1662 } while (stream->nesting < if_nesting + 1);
1664 if (stream->constant == CONSTANT_FILE_MAYBE && stream->protect) {
1665 stream->constant = CONSTANT_FILE_YES;
1667 *list = next->next;
1668 continue;
1669 case TOKEN_STREAMBEGIN:
1670 stream->nesting = if_nesting + 1;
1671 *list = next->next;
1672 continue;
1674 default:
1675 if (false_nesting) {
1676 *list = next->next;
1677 __free_token(next);
1678 continue;
1681 if (token_type(next) != TOKEN_IDENT ||
1682 expand_one_symbol(list))
1683 list = &next->next;
1686 if (stream->constant == CONSTANT_FILE_MAYBE) {
1688 * Any token expansion (even if it ended up being an
1689 * empty expansion) in this stream implies it can't
1690 * be constant.
1692 MARK_STREAM_NONCONST(next->pos);
1697 struct token * preprocess(struct token *token)
1699 preprocessing = 1;
1700 init_preprocessor();
1701 do_preprocess(&token);
1703 // Drop all expressions from pre-processing, they're not used any more.
1704 // This is not true when we have multiple files, though ;/
1705 // clear_expression_alloc();
1706 preprocessing = 0;
1708 return token;