allocate.h: Stop needlessly returning a void value in __DO_ALLOCATOR
[smatch.git] / pre-process.c
blobca1d8ef25a73f27de50c1210b6c0d17bc46086ab
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>
21 #include <time.h>
23 #include "pre-process.h"
24 #include "lib.h"
25 #include "allocate.h"
26 #include "parse.h"
27 #include "token.h"
28 #include "symbol.h"
29 #include "expression.h"
30 #include "scope.h"
32 static int false_nesting = 0;
34 #define INCLUDEPATHS 300
35 const char *includepath[INCLUDEPATHS+1] = {
36 "",
37 "/usr/include",
38 "/usr/local/include",
39 GCC_INTERNAL_INCLUDE,
40 NULL
43 static const char **quote_includepath = includepath;
44 static const char **angle_includepath = includepath + 1;
45 static const char **sys_includepath = includepath + 1;
47 #define dirty_stream(stream) \
48 do { \
49 if (!stream->dirty) { \
50 stream->dirty = 1; \
51 if (!stream->ifndef) \
52 stream->protect = NULL; \
53 } \
54 } while(0)
56 #define end_group(stream) \
57 do { \
58 if (stream->ifndef == stream->top_if) { \
59 stream->ifndef = NULL; \
60 if (!stream->dirty) \
61 stream->protect = NULL; \
62 else if (stream->protect) \
63 stream->dirty = 0; \
64 } \
65 } while(0)
67 #define nesting_error(stream) \
68 do { \
69 stream->dirty = 1; \
70 stream->ifndef = NULL; \
71 stream->protect = NULL; \
72 } while(0)
74 static struct token *alloc_token(struct position *pos)
76 struct token *token = __alloc_token(0);
78 token->pos.stream = pos->stream;
79 token->pos.line = pos->line;
80 token->pos.pos = pos->pos;
81 token->pos.whitespace = 1;
82 return token;
85 static const char *show_token_sequence(struct token *token);
87 /* Expand symbol 'sym' at '*list' */
88 static int expand(struct token **, struct symbol *);
90 static void replace_with_string(struct token *token, const char *str)
92 int size = strlen(str) + 1;
93 struct string *s = __alloc_string(size);
95 s->length = size;
96 memcpy(s->data, str, size);
97 token_type(token) = TOKEN_STRING;
98 token->string = s;
101 static void replace_with_integer(struct token *token, unsigned int val)
103 char *buf = __alloc_bytes(11);
104 sprintf(buf, "%u", val);
105 token_type(token) = TOKEN_NUMBER;
106 token->number = buf;
109 static struct symbol *lookup_macro(struct ident *ident)
111 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
112 if (sym && sym->namespace != NS_MACRO)
113 sym = NULL;
114 return sym;
117 static int token_defined(struct token *token)
119 if (token_type(token) == TOKEN_IDENT) {
120 struct symbol *sym = lookup_macro(token->ident);
121 if (sym) {
122 sym->used_in = file_scope;
123 return 1;
125 return 0;
128 sparse_error(token->pos, "expected preprocessor identifier");
129 return 0;
132 static void replace_with_defined(struct token *token)
134 static const char *string[] = { "0", "1" };
135 int defined = token_defined(token);
137 token_type(token) = TOKEN_NUMBER;
138 token->number = string[defined];
141 static int expand_one_symbol(struct token **list)
143 struct token *token = *list;
144 struct symbol *sym;
145 static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
146 static time_t t = 0;
148 if (token->pos.noexpand)
149 return 1;
151 sym = lookup_macro(token->ident);
152 if (sym) {
153 sym->used_in = file_scope;
154 return expand(list, sym);
156 if (token->ident == &__LINE___ident) {
157 replace_with_integer(token, token->pos.line);
158 } else if (token->ident == &__FILE___ident) {
159 replace_with_string(token, stream_name(token->pos.stream));
160 } else if (token->ident == &__DATE___ident) {
161 if (!t)
162 time(&t);
163 strftime(buffer, 12, "%b %e %Y", localtime(&t));
164 replace_with_string(token, buffer);
165 } else if (token->ident == &__TIME___ident) {
166 if (!t)
167 time(&t);
168 strftime(buffer, 9, "%T", localtime(&t));
169 replace_with_string(token, buffer);
171 return 1;
174 static inline struct token *scan_next(struct token **where)
176 struct token *token = *where;
177 if (token_type(token) != TOKEN_UNTAINT)
178 return token;
179 do {
180 token->ident->tainted = 0;
181 token = token->next;
182 } while (token_type(token) == TOKEN_UNTAINT);
183 *where = token;
184 return token;
187 static void expand_list(struct token **list)
189 struct token *next;
190 while (!eof_token(next = scan_next(list))) {
191 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
192 list = &next->next;
196 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
198 struct token **p = &prev->next;
199 struct token *next;
200 int nesting = 0;
202 while (!eof_token(next = scan_next(p))) {
203 if (match_op(next, '(')) {
204 nesting++;
205 } else if (match_op(next, ')')) {
206 if (!nesting--)
207 break;
208 } else if (match_op(next, ',') && !nesting && !vararg) {
209 break;
211 next->pos.stream = pos->stream;
212 next->pos.line = pos->line;
213 next->pos.pos = pos->pos;
214 p = &next->next;
216 *p = &eof_token_entry;
217 return next;
221 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
224 struct arg {
225 struct token *arg;
226 struct token *expanded;
227 struct token *str;
228 int n_normal;
229 int n_quoted;
230 int n_str;
233 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
235 int wanted = arglist->count.normal;
236 struct token *next = NULL;
237 int count = 0;
239 arglist = arglist->next; /* skip counter */
241 if (!wanted) {
242 next = collect_arg(start, 0, &what->pos);
243 if (eof_token(next))
244 goto Eclosing;
245 if (!eof_token(start->next) || !match_op(next, ')')) {
246 count++;
247 goto Emany;
249 } else {
250 for (count = 0; count < wanted; count++) {
251 struct argcount *p = &arglist->next->count;
252 next = collect_arg(start, p->vararg, &what->pos);
253 arglist = arglist->next->next;
254 if (eof_token(next))
255 goto Eclosing;
256 args[count].arg = start->next;
257 args[count].n_normal = p->normal;
258 args[count].n_quoted = p->quoted;
259 args[count].n_str = p->str;
260 if (match_op(next, ')')) {
261 count++;
262 break;
264 start = next;
266 if (count == wanted && !match_op(next, ')'))
267 goto Emany;
268 if (count == wanted - 1) {
269 struct argcount *p = &arglist->next->count;
270 if (!p->vararg)
271 goto Efew;
272 args[count].arg = NULL;
273 args[count].n_normal = p->normal;
274 args[count].n_quoted = p->quoted;
275 args[count].n_str = p->str;
277 if (count < wanted - 1)
278 goto Efew;
280 what->next = next->next;
281 return 1;
283 Efew:
284 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
285 show_token(what), wanted, count);
286 goto out;
287 Emany:
288 while (match_op(next, ',')) {
289 next = collect_arg(next, 0, &what->pos);
290 count++;
292 if (eof_token(next))
293 goto Eclosing;
294 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
295 show_token(what), count, wanted);
296 goto out;
297 Eclosing:
298 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
299 show_token(what));
300 out:
301 what->next = next->next;
302 return 0;
305 static struct token *dup_list(struct token *list)
307 struct token *res = NULL;
308 struct token **p = &res;
310 while (!eof_token(list)) {
311 struct token *newtok = __alloc_token(0);
312 *newtok = *list;
313 *p = newtok;
314 p = &newtok->next;
315 list = list->next;
317 return res;
320 static struct token *stringify(struct token *arg)
322 const char *s = show_token_sequence(arg);
323 int size = strlen(s)+1;
324 struct token *token = __alloc_token(0);
325 struct string *string = __alloc_string(size);
327 memcpy(string->data, s, size);
328 string->length = size;
329 token->pos = arg->pos;
330 token_type(token) = TOKEN_STRING;
331 token->string = string;
332 token->next = &eof_token_entry;
333 return token;
336 static void expand_arguments(int count, struct arg *args)
338 int i;
339 for (i = 0; i < count; i++) {
340 struct token *arg = args[i].arg;
341 if (!arg)
342 arg = &eof_token_entry;
343 if (args[i].n_str)
344 args[i].str = stringify(arg);
345 if (args[i].n_normal) {
346 if (!args[i].n_quoted) {
347 args[i].expanded = arg;
348 args[i].arg = NULL;
349 } else if (eof_token(arg)) {
350 args[i].expanded = arg;
351 } else {
352 args[i].expanded = dup_list(arg);
354 expand_list(&args[i].expanded);
360 * Possibly valid combinations:
361 * - ident + ident -> ident
362 * - ident + number -> ident unless number contains '.', '+' or '-'.
363 * - number + number -> number
364 * - number + ident -> number
365 * - number + '.' -> number
366 * - number + '+' or '-' -> number, if number used to end on [eEpP].
367 * - '.' + number -> number, if number used to start with a digit.
368 * - special + special -> either special or an error.
370 static enum token_type combine(struct token *left, struct token *right, char *p)
372 int len;
373 enum token_type t1 = token_type(left), t2 = token_type(right);
375 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
376 return TOKEN_ERROR;
378 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
379 return TOKEN_ERROR;
381 strcpy(p, show_token(left));
382 strcat(p, show_token(right));
383 len = strlen(p);
385 if (len >= 256)
386 return TOKEN_ERROR;
388 if (t1 == TOKEN_IDENT) {
389 if (t2 == TOKEN_SPECIAL)
390 return TOKEN_ERROR;
391 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
392 return TOKEN_ERROR;
393 return TOKEN_IDENT;
396 if (t1 == TOKEN_NUMBER) {
397 if (t2 == TOKEN_SPECIAL) {
398 switch (right->special) {
399 case '.':
400 break;
401 case '+': case '-':
402 if (strchr("eEpP", p[len - 2]))
403 break;
404 default:
405 return TOKEN_ERROR;
408 return TOKEN_NUMBER;
411 if (p[0] == '.' && isdigit((unsigned char)p[1]))
412 return TOKEN_NUMBER;
414 return TOKEN_SPECIAL;
417 static int merge(struct token *left, struct token *right)
419 static char buffer[512];
420 int n;
422 switch (combine(left, right, buffer)) {
423 case TOKEN_IDENT:
424 left->ident = built_in_ident(buffer);
425 left->pos.noexpand = 0;
426 return 1;
428 case TOKEN_NUMBER: {
429 char *number = __alloc_bytes(strlen(buffer) + 1);
430 memcpy(number, buffer, strlen(buffer) + 1);
431 token_type(left) = TOKEN_NUMBER; /* could be . + num */
432 left->number = number;
433 return 1;
436 case TOKEN_SPECIAL:
437 if (buffer[2] && buffer[3])
438 break;
439 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
440 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
441 left->special = n;
442 return 1;
445 default:
448 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
449 return 0;
452 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
454 struct token *alloc = alloc_token(streampos);
455 token_type(alloc) = token_type(token);
456 alloc->pos.newline = pos->newline;
457 alloc->pos.whitespace = pos->whitespace;
458 alloc->number = token->number;
459 alloc->pos.noexpand = token->pos.noexpand;
460 return alloc;
463 static struct token **copy(struct token **where, struct token *list, int *count)
465 int need_copy = --*count;
466 while (!eof_token(list)) {
467 struct token *token;
468 if (need_copy)
469 token = dup_token(list, &list->pos, &list->pos);
470 else
471 token = list;
472 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
473 token->pos.noexpand = 1;
474 *where = token;
475 where = &token->next;
476 list = list->next;
478 *where = &eof_token_entry;
479 return where;
482 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
484 struct token *token = *list;
485 struct position *base_pos = &token->pos;
486 struct position *pos = base_pos;
487 int *count;
488 enum {Normal, Placeholder, Concat} state = Normal;
490 for (; !eof_token(body); body = body->next, pos = &body->pos) {
491 struct token *added, *arg;
492 struct token **tail;
494 switch (token_type(body)) {
495 case TOKEN_GNU_KLUDGE:
497 * GNU kludge: if we had <comma>##<vararg>, behaviour
498 * depends on whether we had enough arguments to have
499 * a vararg. If we did, ## is just ignored. Otherwise
500 * both , and ## are ignored. Comma should come from
501 * the body of macro and not be an argument of earlier
502 * concatenation.
504 if (!args[body->next->argnum].arg)
505 continue;
506 added = dup_token(body, base_pos, pos);
507 token_type(added) = TOKEN_SPECIAL;
508 tail = &added->next;
509 break;
511 case TOKEN_STR_ARGUMENT:
512 arg = args[body->argnum].str;
513 count = &args[body->argnum].n_str;
514 goto copy_arg;
516 case TOKEN_QUOTED_ARGUMENT:
517 arg = args[body->argnum].arg;
518 count = &args[body->argnum].n_quoted;
519 if (!arg || eof_token(arg)) {
520 if (state == Concat)
521 state = Normal;
522 else
523 state = Placeholder;
524 continue;
526 goto copy_arg;
528 case TOKEN_MACRO_ARGUMENT:
529 arg = args[body->argnum].expanded;
530 count = &args[body->argnum].n_normal;
531 if (eof_token(arg)) {
532 state = Normal;
533 continue;
535 copy_arg:
536 tail = copy(&added, arg, count);
537 added->pos.newline = pos->newline;
538 added->pos.whitespace = pos->whitespace;
539 break;
541 case TOKEN_CONCAT:
542 if (state == Placeholder)
543 state = Normal;
544 else
545 state = Concat;
546 continue;
548 case TOKEN_IDENT:
549 added = dup_token(body, base_pos, pos);
550 if (added->ident->tainted)
551 added->pos.noexpand = 1;
552 tail = &added->next;
553 break;
555 default:
556 added = dup_token(body, base_pos, pos);
557 tail = &added->next;
558 break;
562 * if we got to doing real concatenation, we already have
563 * added something into the list, so containing_token() is OK.
565 if (state == Concat && merge(containing_token(list), added)) {
566 *list = added->next;
567 if (tail != &added->next)
568 list = tail;
569 } else {
570 *list = added;
571 list = tail;
573 state = Normal;
575 *list = &eof_token_entry;
576 return list;
579 static int expand(struct token **list, struct symbol *sym)
581 struct token *last;
582 struct token *token = *list;
583 struct ident *expanding = token->ident;
584 struct token **tail;
585 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
586 struct arg args[nargs];
588 if (expanding->tainted) {
589 token->pos.noexpand = 1;
590 return 1;
593 if (sym->arglist) {
594 if (!match_op(scan_next(&token->next), '('))
595 return 1;
596 if (!collect_arguments(token->next, sym->arglist, args, token))
597 return 1;
598 expand_arguments(nargs, args);
601 expanding->tainted = 1;
603 last = token->next;
604 tail = substitute(list, sym->expansion, args);
605 *tail = last;
607 return 0;
610 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
612 struct token *last;
613 static char buffer[256];
614 char *ptr = buffer;
616 last = token;
617 while (!eof_token(token) && !match_op(token, endop)) {
618 int len;
619 const char *val = token->string->data;
620 if (token_type(token) != TOKEN_STRING)
621 val = show_token(token);
622 len = strlen(val);
623 memcpy(ptr, val, len);
624 ptr += len;
625 token = token->next;
627 *ptr = 0;
628 if (endop && !match_op(token, endop))
629 sparse_error(start->pos, "expected '>' at end of filename");
630 return buffer;
633 static int already_tokenized(const char *path)
635 int i;
636 struct stream *s = input_streams;
638 for (i = input_stream_nr; --i >= 0; s++) {
639 if (s->constant != CONSTANT_FILE_YES)
640 continue;
641 if (strcmp(path, s->name))
642 continue;
643 if (s->protect && !lookup_macro(s->protect))
644 continue;
645 return 1;
647 return 0;
650 /* Handle include of header files.
651 * The relevant options are made compatible with gcc. The only options that
652 * are not supported is -withprefix and friends.
654 * Three set of include paths are known:
655 * quote_includepath: Path to search when using #include "file.h"
656 * angle_includepath: Path to search when using #include <file.h>
657 * sys_includepath: Built-in include paths
659 * The above is implemented as one array with pointers
660 * +--------------+
661 * quote_includepath ---> | |
662 * +--------------+
663 * | |
664 * +--------------+
665 * angle_includepath ---> | |
666 * +--------------+
667 * sys_includepath ---> | |
668 * +--------------+
669 * | |
670 * +--------------+
672 * -I dir insert dir just before sys_includepath and move the rest
673 * -I- makes all dirs specified with -I before to quote dirs only and
674 * angle_includepath is set equal to sys_includepath.
675 * -nostdinc removes all sys dirs be storing NULL in entry pointed
676 * to by * sys_includepath. Note this will reset all dirs built-in and added
677 * before -nostdinc by -isystem and -dirafter
678 * -isystem dir adds dir where sys_includepath points adding this dir as
679 * first systemdir
680 * -dirafter dir adds dir to the end of the list
683 static void set_stream_include_path(struct stream *stream)
685 const char *path = stream->path;
686 if (!path) {
687 const char *p = strrchr(stream->name, '/');
688 path = "";
689 if (p) {
690 int len = p - stream->name + 1;
691 char *m = malloc(len+1);
692 /* This includes the final "/" */
693 memcpy(m, stream->name, len);
694 m[len] = 0;
695 path = m;
697 stream->path = path;
699 includepath[0] = path;
702 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
704 int fd;
705 int plen = strlen(path);
706 static char fullname[PATH_MAX];
708 memcpy(fullname, path, plen);
709 if (plen && path[plen-1] != '/') {
710 fullname[plen] = '/';
711 plen++;
713 memcpy(fullname+plen, filename, flen);
714 if (already_tokenized(fullname))
715 return 1;
716 fd = open(fullname, O_RDONLY);
717 if (fd >= 0) {
718 char * streamname = __alloc_bytes(plen + flen);
719 memcpy(streamname, fullname, plen + flen);
720 *where = tokenize(streamname, fd, *where, next_path);
721 close(fd);
722 return 1;
724 return 0;
727 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
729 const char *path;
731 while ((path = *pptr++) != NULL) {
732 if (!try_include(path, filename, flen, list, pptr))
733 continue;
734 return 1;
736 return 0;
739 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
741 int flen = strlen(filename) + 1;
743 /* Absolute path? */
744 if (filename[0] == '/') {
745 if (try_include("", filename, flen, list, includepath))
746 return;
747 goto out;
750 /* Dir of input file is first dir to search for quoted includes */
751 set_stream_include_path(stream);
753 if (!path)
754 /* Do not search quote include if <> is in use */
755 path = local ? quote_includepath : angle_includepath;
757 /* Check the standard include paths.. */
758 if (do_include_path(path, list, token, filename, flen))
759 return;
760 out:
761 error_die(token->pos, "unable to open '%s'", filename);
764 static int free_preprocessor_line(struct token *token)
766 while (token_type(token) != TOKEN_EOF) {
767 struct token *free = token;
768 token = token->next;
769 __free_token(free);
771 return 1;
774 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
776 const char *filename;
777 struct token *next;
778 int expect;
780 next = token->next;
781 expect = '>';
782 if (!match_op(next, '<')) {
783 expand_list(&token->next);
784 expect = 0;
785 next = token;
786 if (match_op(token->next, '<')) {
787 next = token->next;
788 expect = '>';
791 token = next->next;
792 filename = token_name_sequence(token, expect, token);
793 do_include(!expect, stream, list, token, filename, path);
794 return 0;
797 static int handle_include(struct stream *stream, struct token **list, struct token *token)
799 return handle_include_path(stream, list, token, NULL);
802 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
804 return handle_include_path(stream, list, token, stream->next_path);
807 static int token_different(struct token *t1, struct token *t2)
809 int different;
811 if (token_type(t1) != token_type(t2))
812 return 1;
814 switch (token_type(t1)) {
815 case TOKEN_IDENT:
816 different = t1->ident != t2->ident;
817 break;
818 case TOKEN_ARG_COUNT:
819 case TOKEN_UNTAINT:
820 case TOKEN_CONCAT:
821 case TOKEN_GNU_KLUDGE:
822 different = 0;
823 break;
824 case TOKEN_NUMBER:
825 different = strcmp(t1->number, t2->number);
826 break;
827 case TOKEN_SPECIAL:
828 different = t1->special != t2->special;
829 break;
830 case TOKEN_MACRO_ARGUMENT:
831 case TOKEN_QUOTED_ARGUMENT:
832 case TOKEN_STR_ARGUMENT:
833 different = t1->argnum != t2->argnum;
834 break;
835 case TOKEN_CHAR:
836 different = t1->character != t2->character;
837 break;
838 case TOKEN_STRING: {
839 struct string *s1, *s2;
841 s1 = t1->string;
842 s2 = t2->string;
843 different = 1;
844 if (s1->length != s2->length)
845 break;
846 different = memcmp(s1->data, s2->data, s1->length);
847 break;
849 default:
850 different = 1;
851 break;
853 return different;
856 static int token_list_different(struct token *list1, struct token *list2)
858 for (;;) {
859 if (list1 == list2)
860 return 0;
861 if (!list1 || !list2)
862 return 1;
863 if (token_different(list1, list2))
864 return 1;
865 list1 = list1->next;
866 list2 = list2->next;
870 static inline void set_arg_count(struct token *token)
872 token_type(token) = TOKEN_ARG_COUNT;
873 token->count.normal = token->count.quoted =
874 token->count.str = token->count.vararg = 0;
877 static struct token *parse_arguments(struct token *list)
879 struct token *arg = list->next, *next = list;
880 struct argcount *count = &list->count;
882 set_arg_count(list);
884 if (match_op(arg, ')')) {
885 next = arg->next;
886 list->next = &eof_token_entry;
887 return next;
890 while (token_type(arg) == TOKEN_IDENT) {
891 if (arg->ident == &__VA_ARGS___ident)
892 goto Eva_args;
893 if (!++count->normal)
894 goto Eargs;
895 next = arg->next;
897 if (match_op(next, ',')) {
898 set_arg_count(next);
899 arg = next->next;
900 continue;
903 if (match_op(next, ')')) {
904 set_arg_count(next);
905 next = next->next;
906 arg->next->next = &eof_token_entry;
907 return next;
910 /* normal cases are finished here */
912 if (match_op(next, SPECIAL_ELLIPSIS)) {
913 if (match_op(next->next, ')')) {
914 set_arg_count(next);
915 next->count.vararg = 1;
916 next = next->next;
917 arg->next->next = &eof_token_entry;
918 return next->next;
921 arg = next;
922 goto Enotclosed;
925 if (eof_token(next)) {
926 goto Enotclosed;
927 } else {
928 arg = next;
929 goto Ebadstuff;
933 if (match_op(arg, SPECIAL_ELLIPSIS)) {
934 next = arg->next;
935 token_type(arg) = TOKEN_IDENT;
936 arg->ident = &__VA_ARGS___ident;
937 if (!match_op(next, ')'))
938 goto Enotclosed;
939 if (!++count->normal)
940 goto Eargs;
941 set_arg_count(next);
942 next->count.vararg = 1;
943 next = next->next;
944 arg->next->next = &eof_token_entry;
945 return next;
948 if (eof_token(arg)) {
949 arg = next;
950 goto Enotclosed;
952 if (match_op(arg, ','))
953 goto Emissing;
954 else
955 goto Ebadstuff;
958 Emissing:
959 sparse_error(arg->pos, "parameter name missing");
960 return NULL;
961 Ebadstuff:
962 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
963 show_token(arg));
964 return NULL;
965 Enotclosed:
966 sparse_error(arg->pos, "missing ')' in macro parameter list");
967 return NULL;
968 Eva_args:
969 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
970 return NULL;
971 Eargs:
972 sparse_error(arg->pos, "too many arguments in macro definition");
973 return NULL;
976 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
978 struct ident *ident = token->ident;
979 int nr;
981 if (!arglist || token_type(token) != TOKEN_IDENT)
982 return 0;
984 arglist = arglist->next;
986 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
987 if (arglist->ident == ident) {
988 struct argcount *count = &arglist->next->count;
989 int n;
991 token->argnum = nr;
992 token_type(token) = type;
993 switch (type) {
994 case TOKEN_MACRO_ARGUMENT:
995 n = ++count->normal;
996 break;
997 case TOKEN_QUOTED_ARGUMENT:
998 n = ++count->quoted;
999 break;
1000 default:
1001 n = ++count->str;
1003 if (n)
1004 return count->vararg ? 2 : 1;
1005 token_type(token) = TOKEN_ERROR;
1006 return -1;
1009 return 0;
1012 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1014 struct token *token = expansion;
1015 struct token **p;
1016 struct token *last = NULL;
1018 if (match_op(token, SPECIAL_HASHHASH))
1019 goto Econcat;
1021 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1022 if (match_op(token, '#')) {
1023 if (arglist) {
1024 struct token *next = token->next;
1025 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1026 goto Equote;
1027 next->pos.whitespace = token->pos.whitespace;
1028 token = *p = next;
1029 } else {
1030 token->pos.noexpand = 1;
1032 } else if (match_op(token, SPECIAL_HASHHASH)) {
1033 struct token *next = token->next;
1034 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
1035 token_type(token) = TOKEN_CONCAT;
1036 if (arg) {
1037 token = next;
1038 /* GNU kludge */
1039 if (arg == 2 && last && match_op(last, ',')) {
1040 token_type(last) = TOKEN_GNU_KLUDGE;
1041 last->next = token;
1043 } else if (match_op(next, SPECIAL_HASHHASH))
1044 token = next;
1045 else if (eof_token(next))
1046 goto Econcat;
1047 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
1048 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1049 } else {
1050 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1052 if (token_type(token) == TOKEN_ERROR)
1053 goto Earg;
1054 last = token;
1056 token = alloc_token(&expansion->pos);
1057 token_type(token) = TOKEN_UNTAINT;
1058 token->ident = name;
1059 token->next = *p;
1060 *p = token;
1061 return expansion;
1063 Equote:
1064 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1065 return NULL;
1067 Econcat:
1068 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1069 return NULL;
1070 Earg:
1071 sparse_error(token->pos, "too many instances of argument in body");
1072 return NULL;
1075 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1077 struct token *arglist, *expansion;
1078 struct token *left = token->next;
1079 struct symbol *sym;
1080 struct ident *name;
1081 int ret;
1083 if (token_type(left) != TOKEN_IDENT) {
1084 sparse_error(token->pos, "expected identifier to 'define'");
1085 return 1;
1088 name = left->ident;
1090 arglist = NULL;
1091 expansion = left->next;
1092 if (!expansion->pos.whitespace) {
1093 if (match_op(expansion, '(')) {
1094 arglist = expansion;
1095 expansion = parse_arguments(expansion);
1096 if (!expansion)
1097 return 1;
1098 } else if (!eof_token(expansion)) {
1099 warning(expansion->pos,
1100 "no whitespace before object-like macro body");
1104 expansion = parse_expansion(expansion, arglist, name);
1105 if (!expansion)
1106 return 1;
1108 ret = 1;
1109 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1110 if (sym) {
1111 int clean;
1113 if (attr < sym->attr)
1114 goto out;
1116 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1118 if (token_list_different(sym->expansion, expansion) ||
1119 token_list_different(sym->arglist, arglist)) {
1120 ret = 0;
1121 if ((clean && attr == SYM_ATTR_NORMAL)
1122 || sym->used_in == file_scope) {
1123 warning(left->pos, "preprocessor token %.*s redefined",
1124 name->len, name->name);
1125 info(sym->pos, "this was the original definition");
1127 } else if (clean)
1128 goto out;
1131 if (!sym || sym->scope != file_scope) {
1132 sym = alloc_symbol(left->pos, SYM_NODE);
1133 bind_symbol(sym, name, NS_MACRO);
1134 ret = 0;
1137 if (!ret) {
1138 sym->expansion = expansion;
1139 sym->arglist = arglist;
1140 __free_token(token); /* Free the "define" token, but not the rest of the line */
1143 sym->namespace = NS_MACRO;
1144 sym->used_in = NULL;
1145 sym->attr = attr;
1146 out:
1147 return ret;
1150 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1152 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1155 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1157 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1160 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1162 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1165 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1167 struct token *left = token->next;
1168 struct symbol *sym;
1170 if (token_type(left) != TOKEN_IDENT) {
1171 sparse_error(token->pos, "expected identifier to 'undef'");
1172 return 1;
1175 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1176 if (sym) {
1177 if (attr < sym->attr)
1178 return 1;
1179 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1180 return 1;
1181 } else if (attr <= SYM_ATTR_NORMAL)
1182 return 1;
1184 if (!sym || sym->scope != file_scope) {
1185 sym = alloc_symbol(left->pos, SYM_NODE);
1186 bind_symbol(sym, left->ident, NS_MACRO);
1189 sym->namespace = NS_UNDEF;
1190 sym->used_in = NULL;
1191 sym->attr = attr;
1193 return 1;
1196 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1198 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1201 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1203 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1206 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1208 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1209 free_preprocessor_line(token->next);
1210 token->next = stream->top_if;
1211 stream->top_if = token;
1212 if (false_nesting || true != 1)
1213 false_nesting++;
1214 return 0;
1217 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1219 struct token *next = token->next;
1220 int arg;
1221 if (token_type(next) == TOKEN_IDENT) {
1222 arg = token_defined(next);
1223 } else {
1224 dirty_stream(stream);
1225 if (!false_nesting)
1226 sparse_error(token->pos, "expected preprocessor identifier");
1227 arg = -1;
1229 return preprocessor_if(stream, token, arg);
1232 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1234 struct token *next = token->next;
1235 int arg;
1236 if (token_type(next) == TOKEN_IDENT) {
1237 if (!stream->dirty && !stream->ifndef) {
1238 if (!stream->protect) {
1239 stream->ifndef = token;
1240 stream->protect = next->ident;
1241 } else if (stream->protect == next->ident) {
1242 stream->ifndef = token;
1243 stream->dirty = 1;
1246 arg = !token_defined(next);
1247 } else {
1248 dirty_stream(stream);
1249 if (!false_nesting)
1250 sparse_error(token->pos, "expected preprocessor identifier");
1251 arg = -1;
1254 return preprocessor_if(stream, token, arg);
1258 * Expression handling for #if and #elif; it differs from normal expansion
1259 * due to special treatment of "defined".
1261 static int expression_value(struct token **where)
1263 struct expression *expr;
1264 struct token *p;
1265 struct token **list = where, **beginning = NULL;
1266 long long value;
1267 int state = 0;
1269 while (!eof_token(p = scan_next(list))) {
1270 switch (state) {
1271 case 0:
1272 if (token_type(p) != TOKEN_IDENT)
1273 break;
1274 if (p->ident == &defined_ident) {
1275 state = 1;
1276 beginning = list;
1277 break;
1279 if (!expand_one_symbol(list))
1280 continue;
1281 if (token_type(p) != TOKEN_IDENT)
1282 break;
1283 token_type(p) = TOKEN_ZERO_IDENT;
1284 break;
1285 case 1:
1286 if (match_op(p, '(')) {
1287 state = 2;
1288 } else {
1289 state = 0;
1290 replace_with_defined(p);
1291 *beginning = p;
1293 break;
1294 case 2:
1295 if (token_type(p) == TOKEN_IDENT)
1296 state = 3;
1297 else
1298 state = 0;
1299 replace_with_defined(p);
1300 *beginning = p;
1301 break;
1302 case 3:
1303 state = 0;
1304 if (!match_op(p, ')'))
1305 sparse_error(p->pos, "missing ')' after \"defined\"");
1306 *list = p->next;
1307 continue;
1309 list = &p->next;
1312 p = constant_expression(*where, &expr);
1313 if (!eof_token(p))
1314 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p));
1315 value = get_expression_value(expr);
1316 return value != 0;
1319 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1321 int value = 0;
1322 if (!false_nesting)
1323 value = expression_value(&token->next);
1325 dirty_stream(stream);
1326 return preprocessor_if(stream, token, value);
1329 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1331 struct token *top_if = stream->top_if;
1332 end_group(stream);
1334 if (!top_if) {
1335 nesting_error(stream);
1336 sparse_error(token->pos, "unmatched #elif within stream");
1337 return 1;
1340 if (token_type(top_if) == TOKEN_ELSE) {
1341 nesting_error(stream);
1342 sparse_error(token->pos, "#elif after #else");
1343 if (!false_nesting)
1344 false_nesting = 1;
1345 return 1;
1348 dirty_stream(stream);
1349 if (token_type(top_if) != TOKEN_IF)
1350 return 1;
1351 if (false_nesting) {
1352 if (expression_value(&token->next))
1353 false_nesting = 0;
1354 } else {
1355 false_nesting = 1;
1356 token_type(top_if) = TOKEN_SKIP_GROUPS;
1358 return 1;
1361 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1363 struct token *top_if = stream->top_if;
1364 end_group(stream);
1366 if (!top_if) {
1367 nesting_error(stream);
1368 sparse_error(token->pos, "unmatched #else within stream");
1369 return 1;
1372 if (token_type(top_if) == TOKEN_ELSE) {
1373 nesting_error(stream);
1374 sparse_error(token->pos, "#else after #else");
1376 if (false_nesting) {
1377 if (token_type(top_if) == TOKEN_IF)
1378 false_nesting = 0;
1379 } else {
1380 false_nesting = 1;
1382 token_type(top_if) = TOKEN_ELSE;
1383 return 1;
1386 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1388 struct token *top_if = stream->top_if;
1389 end_group(stream);
1390 if (!top_if) {
1391 nesting_error(stream);
1392 sparse_error(token->pos, "unmatched #endif in stream");
1393 return 1;
1395 if (false_nesting)
1396 false_nesting--;
1397 stream->top_if = top_if->next;
1398 __free_token(top_if);
1399 return 1;
1402 static const char *show_token_sequence(struct token *token)
1404 static char buffer[1024];
1405 char *ptr = buffer;
1406 int whitespace = 0;
1408 if (!token)
1409 return "<none>";
1410 while (!eof_token(token)) {
1411 const char *val = show_token(token);
1412 int len = strlen(val);
1414 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1415 sparse_error(token->pos, "too long token expansion");
1416 break;
1419 if (whitespace)
1420 *ptr++ = ' ';
1421 memcpy(ptr, val, len);
1422 ptr += len;
1423 token = token->next;
1424 whitespace = token->pos.whitespace;
1426 *ptr = 0;
1427 return buffer;
1430 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1432 warning(token->pos, "%s", show_token_sequence(token->next));
1433 return 1;
1436 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1438 sparse_error(token->pos, "%s", show_token_sequence(token->next));
1439 return 1;
1442 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1445 * Do we have any non-system includes?
1446 * Clear them out if so..
1448 *sys_includepath = NULL;
1449 return 1;
1452 static void add_path_entry(struct token *token, const char *path, const char ***where, const char **new_path)
1454 const char **dst;
1455 const char *next;
1457 /* Need one free entry.. */
1458 if (includepath[INCLUDEPATHS-2])
1459 error_die(token->pos, "too many include path entries");
1461 /* check that this is not a duplicate */
1462 dst = includepath;
1463 while (*dst) {
1464 if (strcmp(*dst, path) == 0)
1465 return;
1466 dst++;
1468 next = path;
1469 dst = *where;
1470 *where = new_path;
1473 * Move them all up starting at dst,
1474 * insert the new entry..
1476 for (;;) {
1477 const char *tmp = *dst;
1478 *dst = next;
1479 if (!next)
1480 break;
1481 next = tmp;
1482 dst++;
1486 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1488 for (;;) {
1489 token = token->next;
1490 if (eof_token(token))
1491 return 1;
1492 if (token_type(token) != TOKEN_STRING) {
1493 warning(token->pos, "expected path string");
1494 return 1;
1496 add_path_entry(token, token->string->data, &sys_includepath, sys_includepath + 1);
1500 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1502 for (;;) {
1503 token = token->next;
1504 if (eof_token(token))
1505 return 1;
1506 if (token_type(token) != TOKEN_STRING) {
1507 sparse_error(token->pos, "expected path string");
1508 return 1;
1510 add_path_entry(token, token->string->data, &sys_includepath, sys_includepath);
1514 /* Add to end on includepath list - no pointer updates */
1515 static void add_dirafter_entry(struct token *token, const char *path)
1517 const char **dst = includepath;
1519 /* Need one free entry.. */
1520 if (includepath[INCLUDEPATHS-2])
1521 error_die(token->pos, "too many include path entries");
1523 /* Add to the end */
1524 while (*dst)
1525 dst++;
1526 *dst = path;
1527 dst++;
1528 *dst = NULL;
1531 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1533 for (;;) {
1534 token = token->next;
1535 if (eof_token(token))
1536 return 1;
1537 if (token_type(token) != TOKEN_STRING) {
1538 sparse_error(token->pos, "expected path string");
1539 return 1;
1541 add_dirafter_entry(token, token->string->data);
1545 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1548 * -I-
1549 * From info gcc:
1550 * Split the include path. Any directories specified with `-I'
1551 * options before `-I-' are searched only for headers requested with
1552 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1553 * If additional directories are specified with `-I' options after
1554 * the `-I-', those directories are searched for all `#include'
1555 * directives.
1556 * In addition, `-I-' inhibits the use of the directory of the current
1557 * file directory as the first search directory for `#include "FILE"'.
1559 quote_includepath = includepath+1;
1560 angle_includepath = sys_includepath;
1561 return 1;
1565 * We replace "#pragma xxx" with "__pragma__" in the token
1566 * stream. Just as an example.
1568 * We'll just #define that away for now, but the theory here
1569 * is that we can use this to insert arbitrary token sequences
1570 * to turn the pragmas into internal front-end sequences for
1571 * when we actually start caring about them.
1573 * So eventually this will turn into some kind of extended
1574 * __attribute__() like thing, except called __pragma__(xxx).
1576 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1578 struct token *next = *line;
1580 token->ident = &pragma_ident;
1581 token->pos.newline = 1;
1582 token->pos.whitespace = 1;
1583 token->pos.pos = 1;
1584 *line = token;
1585 token->next = next;
1586 return 0;
1590 * We ignore #line for now.
1592 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1594 return 1;
1597 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1599 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1600 return 1;
1604 static void init_preprocessor(void)
1606 int i;
1607 int stream = init_stream("preprocessor", -1, includepath);
1608 static struct {
1609 const char *name;
1610 int (*handler)(struct stream *, struct token **, struct token *);
1611 } normal[] = {
1612 { "define", handle_define },
1613 { "weak_define", handle_weak_define },
1614 { "strong_define", handle_strong_define },
1615 { "undef", handle_undef },
1616 { "strong_undef", handle_strong_undef },
1617 { "warning", handle_warning },
1618 { "error", handle_error },
1619 { "include", handle_include },
1620 { "include_next", handle_include_next },
1621 { "pragma", handle_pragma },
1622 { "line", handle_line },
1624 // our internal preprocessor tokens
1625 { "nostdinc", handle_nostdinc },
1626 { "add_include", handle_add_include },
1627 { "add_isystem", handle_add_isystem },
1628 { "add_dirafter", handle_add_dirafter },
1629 { "split_include", handle_split_include },
1630 }, special[] = {
1631 { "ifdef", handle_ifdef },
1632 { "ifndef", handle_ifndef },
1633 { "else", handle_else },
1634 { "endif", handle_endif },
1635 { "if", handle_if },
1636 { "elif", handle_elif },
1639 for (i = 0; i < (sizeof (normal) / sizeof (normal[0])); i++) {
1640 struct symbol *sym;
1641 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1642 sym->handler = normal[i].handler;
1643 sym->normal = 1;
1645 for (i = 0; i < (sizeof (special) / sizeof (special[0])); i++) {
1646 struct symbol *sym;
1647 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1648 sym->handler = special[i].handler;
1649 sym->normal = 0;
1654 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1656 int (*handler)(struct stream *, struct token **, struct token *);
1657 struct token *token = start->next;
1658 int is_normal = 1;
1660 if (eof_token(token))
1661 return;
1663 if (token_type(token) == TOKEN_IDENT) {
1664 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1665 if (sym) {
1666 handler = sym->handler;
1667 is_normal = sym->normal;
1668 } else {
1669 handler = handle_nondirective;
1671 } else if (token_type(token) == TOKEN_NUMBER) {
1672 handler = handle_line;
1673 } else {
1674 handler = handle_nondirective;
1677 if (is_normal) {
1678 dirty_stream(stream);
1679 if (false_nesting)
1680 goto out;
1682 if (!handler(stream, line, token)) /* all set */
1683 return;
1685 out:
1686 free_preprocessor_line(token);
1689 static void preprocessor_line(struct stream *stream, struct token **line)
1691 struct token *start = *line, *next;
1692 struct token **tp = &start->next;
1694 for (;;) {
1695 next = *tp;
1696 if (next->pos.newline)
1697 break;
1698 tp = &next->next;
1700 *line = next;
1701 *tp = &eof_token_entry;
1702 handle_preprocessor_line(stream, line, start);
1705 static void do_preprocess(struct token **list)
1707 struct token *next;
1709 while (!eof_token(next = scan_next(list))) {
1710 struct stream *stream = input_streams + next->pos.stream;
1712 if (next->pos.newline && match_op(next, '#')) {
1713 if (!next->pos.noexpand) {
1714 preprocessor_line(stream, list);
1715 __free_token(next); /* Free the '#' token */
1716 continue;
1720 switch (token_type(next)) {
1721 case TOKEN_STREAMEND:
1722 if (stream->top_if) {
1723 nesting_error(stream);
1724 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1725 stream->top_if = NULL;
1726 false_nesting = 0;
1728 if (!stream->dirty)
1729 stream->constant = CONSTANT_FILE_YES;
1730 *list = next->next;
1731 continue;
1732 case TOKEN_STREAMBEGIN:
1733 *list = next->next;
1734 continue;
1736 default:
1737 dirty_stream(stream);
1738 if (false_nesting) {
1739 *list = next->next;
1740 __free_token(next);
1741 continue;
1744 if (token_type(next) != TOKEN_IDENT ||
1745 expand_one_symbol(list))
1746 list = &next->next;
1751 struct token * preprocess(struct token *token)
1753 preprocessing = 1;
1754 init_preprocessor();
1755 do_preprocess(&token);
1757 // Drop all expressions from preprocessing, they're not used any more.
1758 // This is not true when we have multiple files, though ;/
1759 // clear_expression_alloc();
1760 preprocessing = 0;
1762 return token;