[PATCH] check for !token in handle_preprocessor_line() should be eof_token(token)
[smatch.git] / pre-process.c
blobaf77666593c2b23c32594bb30387a8014a7b69ba
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 dirty_stream(stream) \
53 do if (!stream->dirty) { \
54 stream->dirty = 1; \
55 if (!stream->ifndef) \
56 stream->protect = NULL; \
57 } while(0)
59 #define end_group(stream) \
60 do if (stream->ifndef == if_nesting) { \
61 stream->ifndef = 0; \
62 if (!stream->dirty) \
63 stream->protect = NULL; \
64 else if (stream->protect) \
65 stream->dirty = 0; \
66 } while(0)
68 #define nesting_error(stream) \
69 do { \
70 stream->dirty = 1; \
71 stream->ifndef = 0; \
72 stream->protect = NULL; \
73 } while(0)
75 static struct token *alloc_token(struct position *pos)
77 struct token *token = __alloc_token(0);
79 token->pos.stream = pos->stream;
80 token->pos.line = pos->line;
81 token->pos.pos = pos->pos;
82 token->pos.whitespace = 1;
83 return token;
86 static const char *show_token_sequence(struct token *token);
88 /* Expand symbol 'sym' at '*list' */
89 static int expand(struct token **, struct symbol *);
91 static void replace_with_string(struct token *token, const char *str)
93 int size = strlen(str) + 1;
94 struct string *s = __alloc_string(size);
96 s->length = size;
97 memcpy(s->data, str, size);
98 token_type(token) = TOKEN_STRING;
99 token->string = s;
102 static void replace_with_integer(struct token *token, unsigned int val)
104 char *buf = __alloc_bytes(11);
105 sprintf(buf, "%u", val);
106 token_type(token) = TOKEN_NUMBER;
107 token->number = buf;
110 struct symbol *lookup_macro(struct ident *ident)
112 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_INVISIBLEMACRO);
113 if (sym && sym->namespace != NS_MACRO)
114 sym = NULL;
115 return sym;
118 static int token_defined(struct token *token)
120 if (token_type(token) == TOKEN_IDENT) {
121 struct symbol *sym = lookup_macro(token->ident);
122 if (sym) {
123 sym->weak = 0;
124 return 1;
126 return 0;
129 sparse_error(token->pos, "expected preprocessor identifier");
130 return 0;
133 static void replace_with_defined(struct token *token)
135 static const char *string[] = { "0", "1" };
136 int defined = token_defined(token);
138 token_type(token) = TOKEN_NUMBER;
139 token->number = string[defined];
142 static int expand_one_symbol(struct token **list)
144 struct token *token = *list;
145 struct symbol *sym;
147 if (token->pos.noexpand)
148 return 1;
150 sym = lookup_macro(token->ident);
151 if (sym) {
152 sym->weak = 0;
153 return expand(list, sym);
155 if (token->ident == &__LINE___ident) {
156 replace_with_integer(token, token->pos.line);
157 } else if (token->ident == &__FILE___ident) {
158 replace_with_string(token, stream_name(token->pos.stream));
160 return 1;
163 static inline struct token *scan_next(struct token **where)
165 struct token *token = *where;
166 if (token_type(token) != TOKEN_UNTAINT)
167 return token;
168 do {
169 token->ident->tainted = 0;
170 token = token->next;
171 } while (token_type(token) == TOKEN_UNTAINT);
172 *where = token;
173 return token;
176 static void expand_list(struct token **list)
178 struct token *next;
179 while (!eof_token(next = scan_next(list))) {
180 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
181 list = &next->next;
185 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
187 struct token **p = &prev->next;
188 struct token *next;
189 int nesting = 0;
191 while (!eof_token(next = scan_next(p))) {
192 if (match_op(next, '(')) {
193 nesting++;
194 } else if (match_op(next, ')')) {
195 if (!nesting--)
196 break;
197 } else if (match_op(next, ',') && !nesting && !vararg) {
198 break;
200 next->pos.stream = pos->stream;
201 next->pos.line = pos->line;
202 next->pos.pos = pos->pos;
203 p = &next->next;
205 *p = &eof_token_entry;
206 return next;
210 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
213 struct arg {
214 struct token *arg;
215 struct token *expanded;
216 struct token *str;
217 int n_normal;
218 int n_quoted;
219 int n_str;
222 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
224 int wanted = arglist->count.normal;
225 struct token *next = NULL;
226 int count = 0;
228 arglist = arglist->next; /* skip counter */
230 if (!wanted) {
231 next = collect_arg(start, 0, &what->pos);
232 if (eof_token(next))
233 goto Eclosing;
234 if (!eof_token(start->next) || !match_op(next, ')')) {
235 count++;
236 goto Emany;
238 } else {
239 for (count = 0; count < wanted; count++) {
240 struct argcount *p = &arglist->next->count;
241 next = collect_arg(start, p->vararg, &what->pos);
242 arglist = arglist->next->next;
243 if (eof_token(next))
244 goto Eclosing;
245 args[count].arg = start->next;
246 args[count].n_normal = p->normal;
247 args[count].n_quoted = p->quoted;
248 args[count].n_str = p->str;
249 if (match_op(next, ')')) {
250 count++;
251 break;
253 start = next;
255 if (count == wanted && !match_op(next, ')'))
256 goto Emany;
257 if (count == wanted - 1) {
258 struct argcount *p = &arglist->next->count;
259 if (!p->vararg)
260 goto Efew;
261 args[count].arg = NULL;
262 args[count].n_normal = p->normal;
263 args[count].n_quoted = p->quoted;
264 args[count].n_str = p->str;
266 if (count < wanted - 1)
267 goto Efew;
269 what->next = next->next;
270 return 1;
272 Efew:
273 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
274 show_token(what), wanted, count);
275 goto out;
276 Emany:
277 while (match_op(next, ',')) {
278 next = collect_arg(next, 0, &what->pos);
279 count++;
281 if (eof_token(next))
282 goto Eclosing;
283 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
284 show_token(what), count, wanted);
285 goto out;
286 Eclosing:
287 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
288 show_token(what));
289 out:
290 what->next = next->next;
291 return 0;
294 static struct token *dup_list(struct token *list)
296 struct token *res;
297 struct token **p = &res;
299 while (!eof_token(list)) {
300 struct token *newtok = __alloc_token(0);
301 *newtok = *list;
302 *p = newtok;
303 p = &newtok->next;
304 list = list->next;
306 return res;
309 static struct token *stringify(struct token *arg)
311 const char *s = show_token_sequence(arg);
312 int size = strlen(s)+1;
313 struct token *token = __alloc_token(0);
314 struct string *string = __alloc_string(size);
316 memcpy(string->data, s, size);
317 string->length = size;
318 token->pos = arg->pos;
319 token_type(token) = TOKEN_STRING;
320 token->string = string;
321 token->next = &eof_token_entry;
322 return token;
325 static void expand_arguments(int count, struct arg *args)
327 int i;
328 for (i = 0; i < count; i++) {
329 struct token *arg = args[i].arg;
330 if (!arg)
331 arg = &eof_token_entry;
332 if (args[i].n_str)
333 args[i].str = stringify(arg);
334 if (args[i].n_normal) {
335 if (!args[i].n_quoted) {
336 args[i].expanded = arg;
337 args[i].arg = NULL;
338 } else if (eof_token(arg)) {
339 args[i].expanded = arg;
340 } else {
341 args[i].expanded = dup_list(arg);
343 expand_list(&args[i].expanded);
349 * Possibly valid combinations:
350 * - ident + ident -> ident
351 * - ident + number -> ident unless number contains '.', '+' or '-'.
352 * - number + number -> number
353 * - number + ident -> number
354 * - number + '.' -> number
355 * - number + '+' or '-' -> number, if number used to end on [eEpP].
356 * - '.' + number -> number, if number used to start with a digit.
357 * - special + special -> either special or an error.
359 static enum token_type combine(struct token *left, struct token *right, char *p)
361 int len;
362 enum token_type t1 = token_type(left), t2 = token_type(right);
364 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
365 return TOKEN_ERROR;
367 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
368 return TOKEN_ERROR;
370 strcpy(p, show_token(left));
371 strcat(p, show_token(right));
372 len = strlen(p);
374 if (len >= 256)
375 return TOKEN_ERROR;
377 if (t1 == TOKEN_IDENT) {
378 if (t2 == TOKEN_SPECIAL)
379 return TOKEN_ERROR;
380 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
381 return TOKEN_ERROR;
382 return TOKEN_IDENT;
385 if (t1 == TOKEN_NUMBER) {
386 if (t2 == TOKEN_SPECIAL) {
387 switch (right->special) {
388 case '.':
389 break;
390 case '+': case '-':
391 if (strchr("eEpP", p[len - 2]))
392 break;
393 default:
394 return TOKEN_ERROR;
397 return TOKEN_NUMBER;
400 if (p[0] == '.' && isdigit((unsigned char)p[1]))
401 return TOKEN_NUMBER;
403 return TOKEN_SPECIAL;
406 static int merge(struct token *left, struct token *right)
408 static char buffer[512];
409 int n;
411 switch (combine(left, right, buffer)) {
412 case TOKEN_IDENT:
413 left->ident = built_in_ident(buffer);
414 left->pos.noexpand = 0;
415 return 1;
417 case TOKEN_NUMBER: {
418 char *number = __alloc_bytes(strlen(buffer) + 1);
419 memcpy(number, buffer, strlen(buffer) + 1);
420 token_type(left) = TOKEN_NUMBER; /* could be . + num */
421 left->number = number;
422 return 1;
425 case TOKEN_SPECIAL:
426 if (buffer[2] && buffer[3])
427 break;
428 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
429 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
430 left->special = n;
431 return 1;
434 default:
437 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
438 return 0;
441 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
443 struct token *alloc = alloc_token(streampos);
444 token_type(alloc) = token_type(token);
445 alloc->pos.newline = pos->newline;
446 alloc->pos.whitespace = pos->whitespace;
447 alloc->number = token->number;
448 alloc->pos.noexpand = token->pos.noexpand;
449 return alloc;
452 static struct token **copy(struct token **where, struct token *list, int *count)
454 int need_copy = --*count;
455 while (!eof_token(list)) {
456 struct token *token;
457 if (need_copy)
458 token = dup_token(list, &list->pos, &list->pos);
459 else
460 token = list;
461 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
462 token->pos.noexpand = 1;
463 *where = token;
464 where = &token->next;
465 list = list->next;
467 *where = &eof_token_entry;
468 return where;
471 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
473 struct token *token = *list;
474 struct position *base_pos = &token->pos;
475 struct position *pos = base_pos;
476 int *count;
477 enum {Normal, Placeholder, Concat} state = Normal;
479 for (; !eof_token(body); body = body->next, pos = &body->pos) {
480 struct token *added, *arg;
481 struct token **tail;
483 switch (token_type(body)) {
484 case TOKEN_GNU_KLUDGE:
486 * GNU kludge: if we had <comma>##<vararg>, behaviour
487 * depends on whether we had enough arguments to have
488 * a vararg. If we did, ## is just ignored. Otherwise
489 * both , and ## are ignored. Comma should come from
490 * the body of macro and not be an argument of earlier
491 * concatenation.
493 if (!args[body->next->argnum].arg)
494 continue;
495 added = dup_token(body, base_pos, pos);
496 token_type(added) = TOKEN_SPECIAL;
497 tail = &added->next;
498 break;
500 case TOKEN_STR_ARGUMENT:
501 arg = args[body->argnum].str;
502 count = &args[body->argnum].n_str;
503 goto copy_arg;
505 case TOKEN_QUOTED_ARGUMENT:
506 arg = args[body->argnum].arg;
507 count = &args[body->argnum].n_quoted;
508 if (!arg || eof_token(arg)) {
509 if (state == Concat)
510 state = Normal;
511 else
512 state = Placeholder;
513 continue;
515 goto copy_arg;
517 case TOKEN_MACRO_ARGUMENT:
518 arg = args[body->argnum].expanded;
519 count = &args[body->argnum].n_normal;
520 if (eof_token(arg)) {
521 state = Normal;
522 continue;
524 copy_arg:
525 tail = copy(&added, arg, count);
526 added->pos.newline = pos->newline;
527 added->pos.whitespace = pos->whitespace;
528 break;
530 case TOKEN_CONCAT:
531 if (state == Placeholder)
532 state = Normal;
533 else
534 state = Concat;
535 continue;
537 case TOKEN_IDENT:
538 added = dup_token(body, base_pos, pos);
539 if (added->ident->tainted)
540 added->pos.noexpand = 1;
541 tail = &added->next;
542 break;
544 default:
545 added = dup_token(body, base_pos, pos);
546 tail = &added->next;
547 break;
551 * if we got to doing real concatenation, we already have
552 * added something into the list, so containing_token() is OK.
554 if (state == Concat && merge(containing_token(list), added)) {
555 *list = added->next;
556 if (tail != &added->next)
557 list = tail;
558 } else {
559 *list = added;
560 list = tail;
562 state = Normal;
564 *list = &eof_token_entry;
565 return list;
568 static int expand(struct token **list, struct symbol *sym)
570 struct token *last;
571 struct token *token = *list;
572 struct ident *expanding = token->ident;
573 struct token **tail;
574 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
575 struct arg args[nargs];
577 if (expanding->tainted) {
578 token->pos.noexpand = 1;
579 return 1;
582 if (sym->arglist) {
583 if (!match_op(scan_next(&token->next), '('))
584 return 1;
585 if (!collect_arguments(token->next, sym->arglist, args, token))
586 return 1;
587 expand_arguments(nargs, args);
590 expanding->tainted = 1;
592 last = token->next;
593 tail = substitute(list, sym->expansion, args);
594 *tail = last;
596 return 0;
599 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
601 struct token *last;
602 static char buffer[256];
603 char *ptr = buffer;
605 last = token;
606 while (!eof_token(token) && !match_op(token, endop)) {
607 int len;
608 const char *val = token->string->data;
609 if (token_type(token) != TOKEN_STRING)
610 val = show_token(token);
611 len = strlen(val);
612 memcpy(ptr, val, len);
613 ptr += len;
614 token = token->next;
616 *ptr = 0;
617 if (endop && !match_op(token, endop))
618 sparse_error(start->pos, "expected '>' at end of filename");
619 return buffer;
622 static int already_tokenized(const char *path)
624 int i;
625 struct stream *s = input_streams;
627 for (i = input_stream_nr; --i >= 0; s++) {
628 if (s->constant != CONSTANT_FILE_YES)
629 continue;
630 if (strcmp(path, s->name))
631 continue;
632 if (s->protect && !lookup_macro(s->protect))
633 continue;
634 return 1;
636 return 0;
639 /* Hande include of header files.
640 * The relevant options are made compatible with gcc. The only options that
641 * are not supported is -withprefix and friends.
643 * Three set of include paths are known:
644 * quote_includepath: Path to search when using #include "file.h"
645 * angle_includepath: Path to search when using #include <file.h>
646 * sys_includepath: Built-in include paths
648 * The above is implmented as one array with pointes
649 * +--------------+
650 * quote_includepath ---> | |
651 * +--------------+
652 * | |
653 * +--------------+
654 * angle_includepath ---> | |
655 * +--------------+
656 * sys_includepath ---> | |
657 * +--------------+
658 * | |
659 * +--------------+
661 * -I dir insert dir just before sys_includepath and move the rest
662 * -I- makes all dirs specified with -I before to quote dirs only and
663 * angle_includepath is set equal to sys_includepath.
664 * -nostdinc removes all sys dirs be storing NULL in entry pointed
665 * to by * sys_includepath. Note this will reset all dirs built-in and added
666 * before -nostdinc by -isystem and -dirafter
667 * -isystem dir adds dir where sys_includepath points adding this dir as
668 * first systemdir
669 * -dirafter dir adds dir to the end of the list
672 static void set_stream_include_path(struct stream *stream)
674 const char *path = stream->path;
675 if (!path) {
676 const char *p = strrchr(stream->name, '/');
677 path = "";
678 if (p) {
679 int len = p - stream->name + 1;
680 char *m = malloc(len+1);
681 /* This includes the final "/" */
682 memcpy(m, stream->name, len);
683 m[len] = 0;
684 path = m;
686 stream->path = path;
688 includepath[0] = path;
691 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
693 int fd;
694 int plen = strlen(path);
695 static char fullname[PATH_MAX];
697 memcpy(fullname, path, plen);
698 if (plen && path[plen-1] != '/') {
699 fullname[plen] = '/';
700 plen++;
702 memcpy(fullname+plen, filename, flen);
703 if (already_tokenized(fullname))
704 return 1;
705 fd = open(fullname, O_RDONLY);
706 if (fd >= 0) {
707 char * streamname = __alloc_bytes(plen + flen);
708 memcpy(streamname, fullname, plen + flen);
709 *where = tokenize(streamname, fd, *where, next_path);
710 close(fd);
711 return 1;
713 return 0;
716 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
718 const char *path;
720 while ((path = *pptr++) != NULL) {
721 if (!try_include(path, filename, flen, list, pptr))
722 continue;
723 return 1;
725 return 0;
728 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
730 int flen = strlen(filename) + 1;
732 /* Absolute path? */
733 if (filename[0] == '/') {
734 if (try_include("", filename, flen, list, includepath))
735 return;
736 goto out;
739 /* Dir of inputfile is first dir to search for quoted includes */
740 set_stream_include_path(stream);
742 if (!path)
743 /* Do not search quote include if <> is in use */
744 path = local ? quote_includepath : angle_includepath;
746 /* Check the standard include paths.. */
747 if (do_include_path(path, list, token, filename, flen))
748 return;
749 out:
750 error_die(token->pos, "unable to open '%s'", filename);
753 static int free_preprocessor_line(struct token *token)
755 do {
756 struct token *free = token;
757 token = token->next;
758 __free_token(free);
759 } while (token_type(token) != TOKEN_EOF);
760 return 1;
763 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
765 const char *filename;
766 struct token *next;
767 int expect;
769 next = token->next;
770 expect = '>';
771 if (!match_op(next, '<')) {
772 expand_list(&token->next);
773 expect = 0;
774 next = token;
775 if (match_op(token->next, '<')) {
776 next = token->next;
777 expect = '>';
780 token = next->next;
781 filename = token_name_sequence(token, expect, token);
782 do_include(!expect, stream, list, token, filename, path);
783 return 0;
786 static int handle_include(struct stream *stream, struct token **list, struct token *token)
788 return handle_include_path(stream, list, token, NULL);
791 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
793 return handle_include_path(stream, list, token, stream->next_path);
796 static int token_different(struct token *t1, struct token *t2)
798 int different;
800 if (token_type(t1) != token_type(t2))
801 return 1;
803 switch (token_type(t1)) {
804 case TOKEN_IDENT:
805 different = t1->ident != t2->ident;
806 break;
807 case TOKEN_ARG_COUNT:
808 case TOKEN_UNTAINT:
809 case TOKEN_CONCAT:
810 case TOKEN_GNU_KLUDGE:
811 different = 0;
812 break;
813 case TOKEN_NUMBER:
814 different = strcmp(t1->number, t2->number);
815 break;
816 case TOKEN_SPECIAL:
817 different = t1->special != t2->special;
818 break;
819 case TOKEN_MACRO_ARGUMENT:
820 case TOKEN_QUOTED_ARGUMENT:
821 case TOKEN_STR_ARGUMENT:
822 different = t1->argnum != t2->argnum;
823 break;
824 case TOKEN_CHAR:
825 different = t1->character != t2->character;
826 break;
827 case TOKEN_STRING: {
828 struct string *s1, *s2;
830 s1 = t1->string;
831 s2 = t2->string;
832 different = 1;
833 if (s1->length != s2->length)
834 break;
835 different = memcmp(s1->data, s2->data, s1->length);
836 break;
838 default:
839 different = 1;
840 break;
842 return different;
845 static int token_list_different(struct token *list1, struct token *list2)
847 for (;;) {
848 if (list1 == list2)
849 return 0;
850 if (!list1 || !list2)
851 return 1;
852 if (token_different(list1, list2))
853 return 1;
854 list1 = list1->next;
855 list2 = list2->next;
859 static inline void set_arg_count(struct token *token)
861 token_type(token) = TOKEN_ARG_COUNT;
862 token->count.normal = token->count.quoted =
863 token->count.str = token->count.vararg = 0;
866 static struct token *parse_arguments(struct token *list)
868 struct token *arg = list->next, *next = list;
869 struct argcount *count = &list->count;
871 set_arg_count(list);
873 if (match_op(arg, ')')) {
874 next = arg->next;
875 list->next = &eof_token_entry;
876 return next;
879 while (token_type(arg) == TOKEN_IDENT) {
880 if (arg->ident == &__VA_ARGS___ident)
881 goto Eva_args;
882 if (!++count->normal)
883 goto Eargs;
884 next = arg->next;
886 if (match_op(next, ',')) {
887 set_arg_count(next);
888 arg = next->next;
889 continue;
892 if (match_op(next, ')')) {
893 set_arg_count(next);
894 next = next->next;
895 arg->next->next = &eof_token_entry;
896 return next;
899 /* normal cases are finished here */
901 if (match_op(next, SPECIAL_ELLIPSIS)) {
902 if (match_op(next->next, ')')) {
903 set_arg_count(next);
904 next->count.vararg = 1;
905 next = next->next;
906 arg->next->next = &eof_token_entry;
907 return next->next;
910 arg = next;
911 goto Enotclosed;
914 if (eof_token(next)) {
915 goto Enotclosed;
916 } else {
917 arg = next;
918 goto Ebadstuff;
922 if (match_op(arg, SPECIAL_ELLIPSIS)) {
923 next = arg->next;
924 token_type(arg) = TOKEN_IDENT;
925 arg->ident = &__VA_ARGS___ident;
926 if (!match_op(next, ')'))
927 goto Enotclosed;
928 if (!++count->normal)
929 goto Eargs;
930 set_arg_count(next);
931 next->count.vararg = 1;
932 next = next->next;
933 arg->next->next = &eof_token_entry;
934 return next;
937 if (eof_token(arg)) {
938 arg = next;
939 goto Enotclosed;
941 if (match_op(arg, ','))
942 goto Emissing;
943 else
944 goto Ebadstuff;
947 Emissing:
948 sparse_error(arg->pos, "parameter name missing");
949 return NULL;
950 Ebadstuff:
951 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
952 show_token(arg));
953 return NULL;
954 Enotclosed:
955 sparse_error(arg->pos, "missing ')' in macro parameter list");
956 return NULL;
957 Eva_args:
958 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
959 return NULL;
960 Eargs:
961 sparse_error(arg->pos, "too many arguments in macro definition");
962 return NULL;
965 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
967 struct ident *ident = token->ident;
968 int nr;
970 if (!arglist || token_type(token) != TOKEN_IDENT)
971 return 0;
973 arglist = arglist->next;
975 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
976 if (arglist->ident == ident) {
977 struct argcount *count = &arglist->next->count;
978 int n;
980 token->argnum = nr;
981 token_type(token) = type;
982 switch (type) {
983 case TOKEN_MACRO_ARGUMENT:
984 n = ++count->normal;
985 break;
986 case TOKEN_QUOTED_ARGUMENT:
987 n = ++count->quoted;
988 break;
989 default:
990 n = ++count->str;
992 if (n)
993 return count->vararg ? 2 : 1;
994 token_type(token) = TOKEN_ERROR;
995 return -1;
998 return 0;
1001 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1003 struct token *token = expansion;
1004 struct token **p;
1005 struct token *last = NULL;
1007 if (match_op(token, SPECIAL_HASHHASH))
1008 goto Econcat;
1010 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1011 if (match_op(token, '#')) {
1012 if (arglist) {
1013 struct token *next = token->next;
1014 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1015 goto Equote;
1016 next->pos.whitespace = token->pos.whitespace;
1017 token = *p = next;
1018 } else {
1019 token->pos.noexpand = 1;
1021 } else if (match_op(token, SPECIAL_HASHHASH)) {
1022 struct token *next = token->next;
1023 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
1024 token_type(token) = TOKEN_CONCAT;
1025 if (arg) {
1026 token = next;
1027 /* GNU kludge */
1028 if (arg == 2 && last && match_op(last, ',')) {
1029 token_type(last) = TOKEN_GNU_KLUDGE;
1030 last->next = token;
1032 } else if (match_op(next, SPECIAL_HASHHASH))
1033 token = next;
1034 else if (eof_token(next))
1035 goto Econcat;
1036 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
1037 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1038 } else {
1039 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1041 if (token_type(token) == TOKEN_ERROR)
1042 goto Earg;
1043 last = token;
1045 token = alloc_token(&expansion->pos);
1046 token_type(token) = TOKEN_UNTAINT;
1047 token->ident = name;
1048 token->next = *p;
1049 *p = token;
1050 return expansion;
1052 Equote:
1053 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1054 return NULL;
1056 Econcat:
1057 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1058 return NULL;
1059 Earg:
1060 sparse_error(token->pos, "too many instances of argument in body");
1061 return NULL;
1064 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int weak)
1066 struct token *arglist, *expansion;
1067 struct token *left = token->next;
1068 struct symbol *sym;
1069 struct ident *name;
1071 if (token_type(left) != TOKEN_IDENT) {
1072 sparse_error(token->pos, "expected identifier to 'define'");
1073 return 1;
1076 name = left->ident;
1078 arglist = NULL;
1079 expansion = left->next;
1080 if (!expansion->pos.whitespace && match_op(expansion, '(')) {
1081 arglist = expansion;
1082 expansion = parse_arguments(expansion);
1083 if (!expansion)
1084 return 1;
1087 expansion = parse_expansion(expansion, arglist, name);
1088 if (!expansion)
1089 return 1;
1091 sym = lookup_macro(name);
1092 if (sym) {
1093 if (token_list_different(sym->expansion, expansion) ||
1094 token_list_different(sym->arglist, arglist)) {
1095 if (sym->weak)
1096 goto replace_it;
1097 if (weak)
1098 return 1;
1099 warning(left->pos, "preprocessor token %.*s redefined",
1100 name->len, name->name);
1101 info(sym->pos, "this was the original definition");
1103 /* Don't overwrite global defs */
1104 if (sym->scope != file_scope)
1105 goto allocate_new;
1106 goto replace_it;
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 __free_token(token); /* Free the "define" token, but not the rest of the line */
1119 return 0;
1122 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1124 return do_handle_define(stream, line, token, 0);
1127 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1129 return do_handle_define(stream, line, token, 1);
1132 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1134 struct token *left = token->next;
1135 struct symbol **sym;
1137 if (token_type(left) != TOKEN_IDENT) {
1138 sparse_error(token->pos, "expected identifier to 'undef'");
1139 return 1;
1142 sym = &left->ident->symbols;
1143 while (*sym) {
1144 struct symbol *t = *sym;
1145 if (t->namespace & (NS_MACRO | NS_INVISIBLEMACRO)) {
1146 t->namespace = NS_INVISIBLEMACRO;
1147 return 1;
1149 sym = &t->next_id;
1151 return 1;
1154 static int preprocessor_if(struct token *token, int true)
1156 if (if_nesting == 0)
1157 unmatched_if_pos = token->pos;
1158 if (if_nesting >= MAX_NEST)
1159 error_die(token->pos, "Maximum preprocessor conditional level exhausted");
1160 elif_ignore[if_nesting++] = (false_nesting || true) ? ELIF_IGNORE : 0;
1161 if (false_nesting || true != 1)
1162 false_nesting++;
1163 return 1;
1166 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1168 struct token *next = token->next;
1169 int arg;
1170 if (token_type(next) == TOKEN_IDENT) {
1171 arg = token_defined(next);
1172 } else {
1173 dirty_stream(stream);
1174 if (!false_nesting)
1175 sparse_error(token->pos, "expected preprocessor identifier");
1176 arg = -1;
1178 return preprocessor_if(token, arg);
1181 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1183 struct token *next = token->next;
1184 int arg;
1185 if (token_type(next) == TOKEN_IDENT) {
1186 if (!stream->dirty && !stream->ifndef) {
1187 if (!stream->protect) {
1188 stream->ifndef = if_nesting + 1;
1189 stream->protect = next->ident;
1190 } else if (stream->protect == next->ident) {
1191 stream->ifndef = if_nesting + 1;
1192 stream->dirty = 1;
1195 arg = !token_defined(next);
1196 } else {
1197 dirty_stream(stream);
1198 if (!false_nesting)
1199 sparse_error(token->pos, "expected preprocessor identifier");
1200 arg = -1;
1203 return preprocessor_if(token, arg);
1207 * Expression handling for #if and #elif; it differs from normal expansion
1208 * due to special treatment of "defined".
1210 static int expression_value(struct token **where)
1212 struct expression *expr;
1213 struct token *p;
1214 struct token **list = where, **beginning = NULL;
1215 long long value;
1216 int state = 0;
1218 while (!eof_token(p = scan_next(list))) {
1219 switch (state) {
1220 case 0:
1221 if (token_type(p) != TOKEN_IDENT)
1222 break;
1223 if (p->ident == &defined_ident) {
1224 state = 1;
1225 beginning = list;
1226 break;
1228 if (!expand_one_symbol(list))
1229 continue;
1230 if (token_type(p) != TOKEN_IDENT)
1231 break;
1232 token_type(p) = TOKEN_ZERO_IDENT;
1233 break;
1234 case 1:
1235 if (match_op(p, '(')) {
1236 state = 2;
1237 } else {
1238 state = 0;
1239 replace_with_defined(p);
1240 *beginning = p;
1242 break;
1243 case 2:
1244 if (token_type(p) == TOKEN_IDENT)
1245 state = 3;
1246 else
1247 state = 0;
1248 replace_with_defined(p);
1249 *beginning = p;
1250 break;
1251 case 3:
1252 state = 0;
1253 if (!match_op(p, ')'))
1254 sparse_error(p->pos, "missing ')' after \"defined\"");
1255 *list = p->next;
1256 continue;
1258 list = &p->next;
1261 p = constant_expression(*where, &expr);
1262 if (!eof_token(p))
1263 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p));
1264 value = get_expression_value(expr);
1265 return value != 0;
1268 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1270 int value = 0;
1271 if (!false_nesting)
1272 value = expression_value(&token->next);
1274 dirty_stream(stream);
1275 return preprocessor_if(token, value);
1278 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1280 end_group(stream);
1282 if (stream->nesting > if_nesting) {
1283 nesting_error(stream);
1284 sparse_error(token->pos, "unmatched #elif within stream");
1285 return 1;
1288 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE) {
1289 nesting_error(stream);
1290 sparse_error(token->pos, "#elif after #else");
1291 return 1;
1294 dirty_stream(stream);
1295 if (false_nesting) {
1296 /* If this whole if-thing is if'ed out, an elif cannot help */
1297 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1298 return 1;
1299 if (expression_value(&token->next)) {
1300 false_nesting = 0;
1301 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1303 } else {
1304 false_nesting = 1;
1306 return 1;
1309 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1311 end_group(stream);
1313 if (stream->nesting > if_nesting) {
1314 nesting_error(stream);
1315 sparse_error(token->pos, "unmatched #else within stream");
1316 return 1;
1319 if (elif_ignore[if_nesting-1] & ELIF_SEEN_ELSE) {
1320 nesting_error(stream);
1321 sparse_error(token->pos, "#else after #else");
1322 return 1;
1325 elif_ignore[if_nesting-1] |= ELIF_SEEN_ELSE;
1327 if (false_nesting) {
1328 /* If this whole if-thing is if'ed out, an else cannot help */
1329 if (elif_ignore[if_nesting-1] & ELIF_IGNORE)
1330 return 1;
1331 false_nesting = 0;
1332 elif_ignore[if_nesting-1] |= ELIF_IGNORE;
1333 } else {
1334 false_nesting = 1;
1336 return 1;
1339 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1341 end_group(stream);
1342 if (stream->nesting > if_nesting) {
1343 nesting_error(stream);
1344 sparse_error(token->pos, "unmatched #endif in stream");
1345 return 1;
1347 if (false_nesting)
1348 false_nesting--;
1349 if_nesting--;
1350 return 1;
1353 static const char *show_token_sequence(struct token *token)
1355 static char buffer[1024];
1356 char *ptr = buffer;
1357 int whitespace = 0;
1359 if (!token)
1360 return "<none>";
1361 while (!eof_token(token)) {
1362 const char *val = show_token(token);
1363 int len = strlen(val);
1365 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1366 sparse_error(token->pos, "too long token expansion");
1367 break;
1370 if (whitespace)
1371 *ptr++ = ' ';
1372 memcpy(ptr, val, len);
1373 ptr += len;
1374 token = token->next;
1375 whitespace = token->pos.whitespace;
1377 *ptr = 0;
1378 return buffer;
1381 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1383 warning(token->pos, "%s", show_token_sequence(token->next));
1384 return 1;
1387 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1389 sparse_error(token->pos, "%s", show_token_sequence(token->next));
1390 return 1;
1393 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1396 * Do we have any non-system includes?
1397 * Clear them out if so..
1399 *sys_includepath = NULL;
1400 return 1;
1403 static void add_path_entry(struct token *token, const char *path, const char ***where, const char **new_path)
1405 const char **dst;
1406 const char *next;
1408 /* Need one free entry.. */
1409 if (includepath[INCLUDEPATHS-2])
1410 error_die(token->pos, "too many include path entries");
1412 /* check that this is not a duplicate */
1413 dst = includepath;
1414 while (*dst) {
1415 if (strcmp(*dst, path) == 0)
1416 return;
1417 dst++;
1419 next = path;
1420 dst = *where;
1421 *where = new_path;
1424 * Move them all up starting at dst,
1425 * insert the new entry..
1427 for (;;) {
1428 const char *tmp = *dst;
1429 *dst = next;
1430 if (!next)
1431 break;
1432 next = tmp;
1433 dst++;
1437 static int handle_add_include(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 + 1);
1451 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1453 for (;;) {
1454 token = token->next;
1455 if (eof_token(token))
1456 return 1;
1457 if (token_type(token) != TOKEN_STRING) {
1458 sparse_error(token->pos, "expected path string");
1459 return 1;
1461 add_path_entry(token, token->string->data, &sys_includepath, sys_includepath);
1465 /* Add to end on includepath list - no pointer updates */
1466 static void add_dirafter_entry(struct token *token, const char *path)
1468 const char **dst = includepath;
1470 /* Need one free entry.. */
1471 if (includepath[INCLUDEPATHS-2])
1472 error_die(token->pos, "too many include path entries");
1474 /* Add to the end */
1475 while (*dst)
1476 dst++;
1477 *dst = path;
1478 dst++;
1479 *dst = NULL;
1482 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1484 for (;;) {
1485 token = token->next;
1486 if (eof_token(token))
1487 return 1;
1488 if (token_type(token) != TOKEN_STRING) {
1489 sparse_error(token->pos, "expected path string");
1490 return 1;
1492 add_dirafter_entry(token, token->string->data);
1496 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1499 * -I-
1500 * From info gcc:
1501 * Split the include path. Any directories specified with `-I'
1502 * options before `-I-' are searched only for headers requested with
1503 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1504 * If additional directories are specified with `-I' options after
1505 * the `-I-', those directories are searched for all `#include'
1506 * directives.
1507 * In addition, `-I-' inhibits the use of the directory of the current
1508 * file directory as the first search directory for `#include "FILE"'.
1510 quote_includepath = includepath+1;
1511 angle_includepath = sys_includepath;
1512 return 1;
1516 * We replace "#pragma xxx" with "__pragma__" in the token
1517 * stream. Just as an example.
1519 * We'll just #define that away for now, but the theory here
1520 * is that we can use this to insert arbitrary token sequences
1521 * to turn the pragma's into internal front-end sequences for
1522 * when we actually start caring about them.
1524 * So eventually this will turn into some kind of extended
1525 * __attribute__() like thing, except called __pragma__(xxx).
1527 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1529 struct token *next = *line;
1531 token->ident = &pragma_ident;
1532 token->pos.newline = 1;
1533 token->pos.whitespace = 1;
1534 token->pos.pos = 1;
1535 *line = token;
1536 token->next = next;
1537 return 0;
1541 * We ignore #line for now.
1543 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1545 return 1;
1548 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1550 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1551 return 1;
1555 static void init_preprocessor(void)
1557 int i;
1558 int stream = init_stream("preprocessor", -1, includepath);
1559 static struct {
1560 const char *name;
1561 int (*handler)(struct stream *, struct token **, struct token *);
1562 } normal[] = {
1563 { "define", handle_define },
1564 { "weak_define",handle_weak_define },
1565 { "undef", handle_undef },
1566 { "warning", handle_warning },
1567 { "error", handle_error },
1568 { "include", handle_include },
1569 { "include_next",handle_include_next },
1570 { "pragma", handle_pragma },
1571 { "line", handle_line },
1573 // our internal preprocessor tokens
1574 { "nostdinc", handle_nostdinc },
1575 { "add_include", handle_add_include },
1576 { "add_isystem", handle_add_isystem },
1577 { "add_dirafter", handle_add_dirafter },
1578 { "split_include", handle_split_include },
1579 }, special[] = {
1580 { "ifdef", handle_ifdef },
1581 { "ifndef", handle_ifndef },
1582 { "else", handle_else },
1583 { "endif", handle_endif },
1584 { "if", handle_if },
1585 { "elif", handle_elif },
1588 for (i = 0; i < (sizeof (normal) / sizeof (normal[0])); i++) {
1589 struct symbol *sym;
1590 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1591 sym->handler = normal[i].handler;
1592 sym->normal = 1;
1594 for (i = 0; i < (sizeof (special) / sizeof (special[0])); i++) {
1595 struct symbol *sym;
1596 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1597 sym->handler = special[i].handler;
1598 sym->normal = 0;
1603 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1605 int (*handler)(struct stream *, struct token **, struct token *);
1606 struct token *token = start->next;
1607 int is_normal = 1;
1609 if (eof_token(token))
1610 return;
1612 if (token_type(token) == TOKEN_IDENT) {
1613 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1614 if (sym) {
1615 handler = sym->handler;
1616 is_normal = sym->normal;
1617 } else {
1618 handler = handle_nondirective;
1620 } else if (token_type(token) == TOKEN_NUMBER) {
1621 handler = handle_line;
1622 } else {
1623 handler = handle_nondirective;
1626 if (is_normal) {
1627 dirty_stream(stream);
1628 if (false_nesting)
1629 goto out;
1631 if (!handler(stream, line, token)) /* all set */
1632 return;
1634 out:
1635 free_preprocessor_line(token);
1638 static void preprocessor_line(struct stream *stream, struct token **line)
1640 struct token *start = *line, *next;
1641 struct token **tp = &start->next;
1643 for (;;) {
1644 next = *tp;
1645 if (next->pos.newline)
1646 break;
1647 tp = &next->next;
1649 *line = next;
1650 *tp = &eof_token_entry;
1651 handle_preprocessor_line(stream, line, start);
1654 static void do_preprocess(struct token **list)
1656 struct token *next;
1658 while (!eof_token(next = scan_next(list))) {
1659 struct stream *stream = input_streams + next->pos.stream;
1661 if (next->pos.newline && match_op(next, '#')) {
1662 if (!next->pos.noexpand) {
1663 preprocessor_line(stream, list);
1664 __free_token(next); /* Free the '#' token */
1665 continue;
1669 switch (token_type(next)) {
1670 case TOKEN_STREAMEND:
1671 if (stream->nesting < if_nesting + 1) {
1672 nesting_error(stream);
1673 sparse_error(unmatched_if_pos, "unterminated preprocessor conditional");
1674 if_nesting = stream->nesting - 1;
1675 false_nesting = 0;
1677 if (!stream->dirty)
1678 stream->constant = CONSTANT_FILE_YES;
1679 *list = next->next;
1680 continue;
1681 case TOKEN_STREAMBEGIN:
1682 stream->nesting = if_nesting + 1;
1683 *list = next->next;
1684 continue;
1686 default:
1687 dirty_stream(stream);
1688 if (false_nesting) {
1689 *list = next->next;
1690 __free_token(next);
1691 continue;
1694 if (token_type(next) != TOKEN_IDENT ||
1695 expand_one_symbol(list))
1696 list = &next->next;
1701 struct token * preprocess(struct token *token)
1703 preprocessing = 1;
1704 init_preprocessor();
1705 do_preprocess(&token);
1707 // Drop all expressions from pre-processing, they're not used any more.
1708 // This is not true when we have multiple files, though ;/
1709 // clear_expression_alloc();
1710 preprocessing = 0;
1712 return token;