[PATCH] Warning for mixing enums of different types
[smatch.git] / pre-process.c
blob08d1a2eedc26e5389a032d83bba32848a6177acd
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;
33 #define INCLUDEPATHS 300
34 const char *includepath[INCLUDEPATHS+1] = {
35 "",
36 "/usr/include",
37 "/usr/local/include",
38 GCC_INTERNAL_INCLUDE,
39 NULL
42 static const char **quote_includepath = includepath;
43 static const char **angle_includepath = includepath + 1;
44 static const char **sys_includepath = includepath + 1;
46 #define dirty_stream(stream) \
47 do if (!stream->dirty) { \
48 stream->dirty = 1; \
49 if (!stream->ifndef) \
50 stream->protect = NULL; \
51 } while(0)
53 #define end_group(stream) \
54 do if (stream->ifndef == stream->top_if) { \
55 stream->ifndef = NULL; \
56 if (!stream->dirty) \
57 stream->protect = NULL; \
58 else if (stream->protect) \
59 stream->dirty = 0; \
60 } while(0)
62 #define nesting_error(stream) \
63 do { \
64 stream->dirty = 1; \
65 stream->ifndef = NULL; \
66 stream->protect = NULL; \
67 } while(0)
69 static struct token *alloc_token(struct position *pos)
71 struct token *token = __alloc_token(0);
73 token->pos.stream = pos->stream;
74 token->pos.line = pos->line;
75 token->pos.pos = pos->pos;
76 token->pos.whitespace = 1;
77 return token;
80 static const char *show_token_sequence(struct token *token);
82 /* Expand symbol 'sym' at '*list' */
83 static int expand(struct token **, struct symbol *);
85 static void replace_with_string(struct token *token, const char *str)
87 int size = strlen(str) + 1;
88 struct string *s = __alloc_string(size);
90 s->length = size;
91 memcpy(s->data, str, size);
92 token_type(token) = TOKEN_STRING;
93 token->string = s;
96 static void replace_with_integer(struct token *token, unsigned int val)
98 char *buf = __alloc_bytes(11);
99 sprintf(buf, "%u", val);
100 token_type(token) = TOKEN_NUMBER;
101 token->number = buf;
104 static struct symbol *lookup_macro(struct ident *ident)
106 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_INVISIBLEMACRO);
107 if (sym && sym->namespace != NS_MACRO)
108 sym = NULL;
109 return sym;
112 static int token_defined(struct token *token)
114 if (token_type(token) == TOKEN_IDENT) {
115 struct symbol *sym = lookup_macro(token->ident);
116 if (sym) {
117 sym->weak = 0;
118 return 1;
120 return 0;
123 sparse_error(token->pos, "expected preprocessor identifier");
124 return 0;
127 static void replace_with_defined(struct token *token)
129 static const char *string[] = { "0", "1" };
130 int defined = token_defined(token);
132 token_type(token) = TOKEN_NUMBER;
133 token->number = string[defined];
136 static int expand_one_symbol(struct token **list)
138 struct token *token = *list;
139 struct symbol *sym;
141 if (token->pos.noexpand)
142 return 1;
144 sym = lookup_macro(token->ident);
145 if (sym) {
146 sym->weak = 0;
147 return expand(list, sym);
149 if (token->ident == &__LINE___ident) {
150 replace_with_integer(token, token->pos.line);
151 } else if (token->ident == &__FILE___ident) {
152 replace_with_string(token, stream_name(token->pos.stream));
154 return 1;
157 static inline struct token *scan_next(struct token **where)
159 struct token *token = *where;
160 if (token_type(token) != TOKEN_UNTAINT)
161 return token;
162 do {
163 token->ident->tainted = 0;
164 token = token->next;
165 } while (token_type(token) == TOKEN_UNTAINT);
166 *where = token;
167 return token;
170 static void expand_list(struct token **list)
172 struct token *next;
173 while (!eof_token(next = scan_next(list))) {
174 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
175 list = &next->next;
179 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
181 struct token **p = &prev->next;
182 struct token *next;
183 int nesting = 0;
185 while (!eof_token(next = scan_next(p))) {
186 if (match_op(next, '(')) {
187 nesting++;
188 } else if (match_op(next, ')')) {
189 if (!nesting--)
190 break;
191 } else if (match_op(next, ',') && !nesting && !vararg) {
192 break;
194 next->pos.stream = pos->stream;
195 next->pos.line = pos->line;
196 next->pos.pos = pos->pos;
197 p = &next->next;
199 *p = &eof_token_entry;
200 return next;
204 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
207 struct arg {
208 struct token *arg;
209 struct token *expanded;
210 struct token *str;
211 int n_normal;
212 int n_quoted;
213 int n_str;
216 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
218 int wanted = arglist->count.normal;
219 struct token *next = NULL;
220 int count = 0;
222 arglist = arglist->next; /* skip counter */
224 if (!wanted) {
225 next = collect_arg(start, 0, &what->pos);
226 if (eof_token(next))
227 goto Eclosing;
228 if (!eof_token(start->next) || !match_op(next, ')')) {
229 count++;
230 goto Emany;
232 } else {
233 for (count = 0; count < wanted; count++) {
234 struct argcount *p = &arglist->next->count;
235 next = collect_arg(start, p->vararg, &what->pos);
236 arglist = arglist->next->next;
237 if (eof_token(next))
238 goto Eclosing;
239 args[count].arg = start->next;
240 args[count].n_normal = p->normal;
241 args[count].n_quoted = p->quoted;
242 args[count].n_str = p->str;
243 if (match_op(next, ')')) {
244 count++;
245 break;
247 start = next;
249 if (count == wanted && !match_op(next, ')'))
250 goto Emany;
251 if (count == wanted - 1) {
252 struct argcount *p = &arglist->next->count;
253 if (!p->vararg)
254 goto Efew;
255 args[count].arg = NULL;
256 args[count].n_normal = p->normal;
257 args[count].n_quoted = p->quoted;
258 args[count].n_str = p->str;
260 if (count < wanted - 1)
261 goto Efew;
263 what->next = next->next;
264 return 1;
266 Efew:
267 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
268 show_token(what), wanted, count);
269 goto out;
270 Emany:
271 while (match_op(next, ',')) {
272 next = collect_arg(next, 0, &what->pos);
273 count++;
275 if (eof_token(next))
276 goto Eclosing;
277 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
278 show_token(what), count, wanted);
279 goto out;
280 Eclosing:
281 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
282 show_token(what));
283 out:
284 what->next = next->next;
285 return 0;
288 static struct token *dup_list(struct token *list)
290 struct token *res;
291 struct token **p = &res;
293 while (!eof_token(list)) {
294 struct token *newtok = __alloc_token(0);
295 *newtok = *list;
296 *p = newtok;
297 p = &newtok->next;
298 list = list->next;
300 return res;
303 static struct token *stringify(struct token *arg)
305 const char *s = show_token_sequence(arg);
306 int size = strlen(s)+1;
307 struct token *token = __alloc_token(0);
308 struct string *string = __alloc_string(size);
310 memcpy(string->data, s, size);
311 string->length = size;
312 token->pos = arg->pos;
313 token_type(token) = TOKEN_STRING;
314 token->string = string;
315 token->next = &eof_token_entry;
316 return token;
319 static void expand_arguments(int count, struct arg *args)
321 int i;
322 for (i = 0; i < count; i++) {
323 struct token *arg = args[i].arg;
324 if (!arg)
325 arg = &eof_token_entry;
326 if (args[i].n_str)
327 args[i].str = stringify(arg);
328 if (args[i].n_normal) {
329 if (!args[i].n_quoted) {
330 args[i].expanded = arg;
331 args[i].arg = NULL;
332 } else if (eof_token(arg)) {
333 args[i].expanded = arg;
334 } else {
335 args[i].expanded = dup_list(arg);
337 expand_list(&args[i].expanded);
343 * Possibly valid combinations:
344 * - ident + ident -> ident
345 * - ident + number -> ident unless number contains '.', '+' or '-'.
346 * - number + number -> number
347 * - number + ident -> number
348 * - number + '.' -> number
349 * - number + '+' or '-' -> number, if number used to end on [eEpP].
350 * - '.' + number -> number, if number used to start with a digit.
351 * - special + special -> either special or an error.
353 static enum token_type combine(struct token *left, struct token *right, char *p)
355 int len;
356 enum token_type t1 = token_type(left), t2 = token_type(right);
358 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
359 return TOKEN_ERROR;
361 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
362 return TOKEN_ERROR;
364 strcpy(p, show_token(left));
365 strcat(p, show_token(right));
366 len = strlen(p);
368 if (len >= 256)
369 return TOKEN_ERROR;
371 if (t1 == TOKEN_IDENT) {
372 if (t2 == TOKEN_SPECIAL)
373 return TOKEN_ERROR;
374 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
375 return TOKEN_ERROR;
376 return TOKEN_IDENT;
379 if (t1 == TOKEN_NUMBER) {
380 if (t2 == TOKEN_SPECIAL) {
381 switch (right->special) {
382 case '.':
383 break;
384 case '+': case '-':
385 if (strchr("eEpP", p[len - 2]))
386 break;
387 default:
388 return TOKEN_ERROR;
391 return TOKEN_NUMBER;
394 if (p[0] == '.' && isdigit((unsigned char)p[1]))
395 return TOKEN_NUMBER;
397 return TOKEN_SPECIAL;
400 static int merge(struct token *left, struct token *right)
402 static char buffer[512];
403 int n;
405 switch (combine(left, right, buffer)) {
406 case TOKEN_IDENT:
407 left->ident = built_in_ident(buffer);
408 left->pos.noexpand = 0;
409 return 1;
411 case TOKEN_NUMBER: {
412 char *number = __alloc_bytes(strlen(buffer) + 1);
413 memcpy(number, buffer, strlen(buffer) + 1);
414 token_type(left) = TOKEN_NUMBER; /* could be . + num */
415 left->number = number;
416 return 1;
419 case TOKEN_SPECIAL:
420 if (buffer[2] && buffer[3])
421 break;
422 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
423 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
424 left->special = n;
425 return 1;
428 default:
431 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
432 return 0;
435 static struct token *dup_token(struct token *token, struct position *streampos, struct position *pos)
437 struct token *alloc = alloc_token(streampos);
438 token_type(alloc) = token_type(token);
439 alloc->pos.newline = pos->newline;
440 alloc->pos.whitespace = pos->whitespace;
441 alloc->number = token->number;
442 alloc->pos.noexpand = token->pos.noexpand;
443 return alloc;
446 static struct token **copy(struct token **where, struct token *list, int *count)
448 int need_copy = --*count;
449 while (!eof_token(list)) {
450 struct token *token;
451 if (need_copy)
452 token = dup_token(list, &list->pos, &list->pos);
453 else
454 token = list;
455 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
456 token->pos.noexpand = 1;
457 *where = token;
458 where = &token->next;
459 list = list->next;
461 *where = &eof_token_entry;
462 return where;
465 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
467 struct token *token = *list;
468 struct position *base_pos = &token->pos;
469 struct position *pos = base_pos;
470 int *count;
471 enum {Normal, Placeholder, Concat} state = Normal;
473 for (; !eof_token(body); body = body->next, pos = &body->pos) {
474 struct token *added, *arg;
475 struct token **tail;
477 switch (token_type(body)) {
478 case TOKEN_GNU_KLUDGE:
480 * GNU kludge: if we had <comma>##<vararg>, behaviour
481 * depends on whether we had enough arguments to have
482 * a vararg. If we did, ## is just ignored. Otherwise
483 * both , and ## are ignored. Comma should come from
484 * the body of macro and not be an argument of earlier
485 * concatenation.
487 if (!args[body->next->argnum].arg)
488 continue;
489 added = dup_token(body, base_pos, pos);
490 token_type(added) = TOKEN_SPECIAL;
491 tail = &added->next;
492 break;
494 case TOKEN_STR_ARGUMENT:
495 arg = args[body->argnum].str;
496 count = &args[body->argnum].n_str;
497 goto copy_arg;
499 case TOKEN_QUOTED_ARGUMENT:
500 arg = args[body->argnum].arg;
501 count = &args[body->argnum].n_quoted;
502 if (!arg || eof_token(arg)) {
503 if (state == Concat)
504 state = Normal;
505 else
506 state = Placeholder;
507 continue;
509 goto copy_arg;
511 case TOKEN_MACRO_ARGUMENT:
512 arg = args[body->argnum].expanded;
513 count = &args[body->argnum].n_normal;
514 if (eof_token(arg)) {
515 state = Normal;
516 continue;
518 copy_arg:
519 tail = copy(&added, arg, count);
520 added->pos.newline = pos->newline;
521 added->pos.whitespace = pos->whitespace;
522 break;
524 case TOKEN_CONCAT:
525 if (state == Placeholder)
526 state = Normal;
527 else
528 state = Concat;
529 continue;
531 case TOKEN_IDENT:
532 added = dup_token(body, base_pos, pos);
533 if (added->ident->tainted)
534 added->pos.noexpand = 1;
535 tail = &added->next;
536 break;
538 default:
539 added = dup_token(body, base_pos, pos);
540 tail = &added->next;
541 break;
545 * if we got to doing real concatenation, we already have
546 * added something into the list, so containing_token() is OK.
548 if (state == Concat && merge(containing_token(list), added)) {
549 *list = added->next;
550 if (tail != &added->next)
551 list = tail;
552 } else {
553 *list = added;
554 list = tail;
556 state = Normal;
558 *list = &eof_token_entry;
559 return list;
562 static int expand(struct token **list, struct symbol *sym)
564 struct token *last;
565 struct token *token = *list;
566 struct ident *expanding = token->ident;
567 struct token **tail;
568 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
569 struct arg args[nargs];
571 if (expanding->tainted) {
572 token->pos.noexpand = 1;
573 return 1;
576 if (sym->arglist) {
577 if (!match_op(scan_next(&token->next), '('))
578 return 1;
579 if (!collect_arguments(token->next, sym->arglist, args, token))
580 return 1;
581 expand_arguments(nargs, args);
584 expanding->tainted = 1;
586 last = token->next;
587 tail = substitute(list, sym->expansion, args);
588 *tail = last;
590 return 0;
593 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
595 struct token *last;
596 static char buffer[256];
597 char *ptr = buffer;
599 last = token;
600 while (!eof_token(token) && !match_op(token, endop)) {
601 int len;
602 const char *val = token->string->data;
603 if (token_type(token) != TOKEN_STRING)
604 val = show_token(token);
605 len = strlen(val);
606 memcpy(ptr, val, len);
607 ptr += len;
608 token = token->next;
610 *ptr = 0;
611 if (endop && !match_op(token, endop))
612 sparse_error(start->pos, "expected '>' at end of filename");
613 return buffer;
616 static int already_tokenized(const char *path)
618 int i;
619 struct stream *s = input_streams;
621 for (i = input_stream_nr; --i >= 0; s++) {
622 if (s->constant != CONSTANT_FILE_YES)
623 continue;
624 if (strcmp(path, s->name))
625 continue;
626 if (s->protect && !lookup_macro(s->protect))
627 continue;
628 return 1;
630 return 0;
633 /* Hande include of header files.
634 * The relevant options are made compatible with gcc. The only options that
635 * are not supported is -withprefix and friends.
637 * Three set of include paths are known:
638 * quote_includepath: Path to search when using #include "file.h"
639 * angle_includepath: Path to search when using #include <file.h>
640 * sys_includepath: Built-in include paths
642 * The above is implmented as one array with pointes
643 * +--------------+
644 * quote_includepath ---> | |
645 * +--------------+
646 * | |
647 * +--------------+
648 * angle_includepath ---> | |
649 * +--------------+
650 * sys_includepath ---> | |
651 * +--------------+
652 * | |
653 * +--------------+
655 * -I dir insert dir just before sys_includepath and move the rest
656 * -I- makes all dirs specified with -I before to quote dirs only and
657 * angle_includepath is set equal to sys_includepath.
658 * -nostdinc removes all sys dirs be storing NULL in entry pointed
659 * to by * sys_includepath. Note this will reset all dirs built-in and added
660 * before -nostdinc by -isystem and -dirafter
661 * -isystem dir adds dir where sys_includepath points adding this dir as
662 * first systemdir
663 * -dirafter dir adds dir to the end of the list
666 static void set_stream_include_path(struct stream *stream)
668 const char *path = stream->path;
669 if (!path) {
670 const char *p = strrchr(stream->name, '/');
671 path = "";
672 if (p) {
673 int len = p - stream->name + 1;
674 char *m = malloc(len+1);
675 /* This includes the final "/" */
676 memcpy(m, stream->name, len);
677 m[len] = 0;
678 path = m;
680 stream->path = path;
682 includepath[0] = path;
685 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
687 int fd;
688 int plen = strlen(path);
689 static char fullname[PATH_MAX];
691 memcpy(fullname, path, plen);
692 if (plen && path[plen-1] != '/') {
693 fullname[plen] = '/';
694 plen++;
696 memcpy(fullname+plen, filename, flen);
697 if (already_tokenized(fullname))
698 return 1;
699 fd = open(fullname, O_RDONLY);
700 if (fd >= 0) {
701 char * streamname = __alloc_bytes(plen + flen);
702 memcpy(streamname, fullname, plen + flen);
703 *where = tokenize(streamname, fd, *where, next_path);
704 close(fd);
705 return 1;
707 return 0;
710 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
712 const char *path;
714 while ((path = *pptr++) != NULL) {
715 if (!try_include(path, filename, flen, list, pptr))
716 continue;
717 return 1;
719 return 0;
722 static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path)
724 int flen = strlen(filename) + 1;
726 /* Absolute path? */
727 if (filename[0] == '/') {
728 if (try_include("", filename, flen, list, includepath))
729 return;
730 goto out;
733 /* Dir of inputfile is first dir to search for quoted includes */
734 set_stream_include_path(stream);
736 if (!path)
737 /* Do not search quote include if <> is in use */
738 path = local ? quote_includepath : angle_includepath;
740 /* Check the standard include paths.. */
741 if (do_include_path(path, list, token, filename, flen))
742 return;
743 out:
744 error_die(token->pos, "unable to open '%s'", filename);
747 static int free_preprocessor_line(struct token *token)
749 do {
750 struct token *free = token;
751 token = token->next;
752 __free_token(free);
753 } while (token_type(token) != TOKEN_EOF);
754 return 1;
757 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path)
759 const char *filename;
760 struct token *next;
761 int expect;
763 next = token->next;
764 expect = '>';
765 if (!match_op(next, '<')) {
766 expand_list(&token->next);
767 expect = 0;
768 next = token;
769 if (match_op(token->next, '<')) {
770 next = token->next;
771 expect = '>';
774 token = next->next;
775 filename = token_name_sequence(token, expect, token);
776 do_include(!expect, stream, list, token, filename, path);
777 return 0;
780 static int handle_include(struct stream *stream, struct token **list, struct token *token)
782 return handle_include_path(stream, list, token, NULL);
785 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
787 return handle_include_path(stream, list, token, stream->next_path);
790 static int token_different(struct token *t1, struct token *t2)
792 int different;
794 if (token_type(t1) != token_type(t2))
795 return 1;
797 switch (token_type(t1)) {
798 case TOKEN_IDENT:
799 different = t1->ident != t2->ident;
800 break;
801 case TOKEN_ARG_COUNT:
802 case TOKEN_UNTAINT:
803 case TOKEN_CONCAT:
804 case TOKEN_GNU_KLUDGE:
805 different = 0;
806 break;
807 case TOKEN_NUMBER:
808 different = strcmp(t1->number, t2->number);
809 break;
810 case TOKEN_SPECIAL:
811 different = t1->special != t2->special;
812 break;
813 case TOKEN_MACRO_ARGUMENT:
814 case TOKEN_QUOTED_ARGUMENT:
815 case TOKEN_STR_ARGUMENT:
816 different = t1->argnum != t2->argnum;
817 break;
818 case TOKEN_CHAR:
819 different = t1->character != t2->character;
820 break;
821 case TOKEN_STRING: {
822 struct string *s1, *s2;
824 s1 = t1->string;
825 s2 = t2->string;
826 different = 1;
827 if (s1->length != s2->length)
828 break;
829 different = memcmp(s1->data, s2->data, s1->length);
830 break;
832 default:
833 different = 1;
834 break;
836 return different;
839 static int token_list_different(struct token *list1, struct token *list2)
841 for (;;) {
842 if (list1 == list2)
843 return 0;
844 if (!list1 || !list2)
845 return 1;
846 if (token_different(list1, list2))
847 return 1;
848 list1 = list1->next;
849 list2 = list2->next;
853 static inline void set_arg_count(struct token *token)
855 token_type(token) = TOKEN_ARG_COUNT;
856 token->count.normal = token->count.quoted =
857 token->count.str = token->count.vararg = 0;
860 static struct token *parse_arguments(struct token *list)
862 struct token *arg = list->next, *next = list;
863 struct argcount *count = &list->count;
865 set_arg_count(list);
867 if (match_op(arg, ')')) {
868 next = arg->next;
869 list->next = &eof_token_entry;
870 return next;
873 while (token_type(arg) == TOKEN_IDENT) {
874 if (arg->ident == &__VA_ARGS___ident)
875 goto Eva_args;
876 if (!++count->normal)
877 goto Eargs;
878 next = arg->next;
880 if (match_op(next, ',')) {
881 set_arg_count(next);
882 arg = next->next;
883 continue;
886 if (match_op(next, ')')) {
887 set_arg_count(next);
888 next = next->next;
889 arg->next->next = &eof_token_entry;
890 return next;
893 /* normal cases are finished here */
895 if (match_op(next, SPECIAL_ELLIPSIS)) {
896 if (match_op(next->next, ')')) {
897 set_arg_count(next);
898 next->count.vararg = 1;
899 next = next->next;
900 arg->next->next = &eof_token_entry;
901 return next->next;
904 arg = next;
905 goto Enotclosed;
908 if (eof_token(next)) {
909 goto Enotclosed;
910 } else {
911 arg = next;
912 goto Ebadstuff;
916 if (match_op(arg, SPECIAL_ELLIPSIS)) {
917 next = arg->next;
918 token_type(arg) = TOKEN_IDENT;
919 arg->ident = &__VA_ARGS___ident;
920 if (!match_op(next, ')'))
921 goto Enotclosed;
922 if (!++count->normal)
923 goto Eargs;
924 set_arg_count(next);
925 next->count.vararg = 1;
926 next = next->next;
927 arg->next->next = &eof_token_entry;
928 return next;
931 if (eof_token(arg)) {
932 arg = next;
933 goto Enotclosed;
935 if (match_op(arg, ','))
936 goto Emissing;
937 else
938 goto Ebadstuff;
941 Emissing:
942 sparse_error(arg->pos, "parameter name missing");
943 return NULL;
944 Ebadstuff:
945 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
946 show_token(arg));
947 return NULL;
948 Enotclosed:
949 sparse_error(arg->pos, "missing ')' in macro parameter list");
950 return NULL;
951 Eva_args:
952 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
953 return NULL;
954 Eargs:
955 sparse_error(arg->pos, "too many arguments in macro definition");
956 return NULL;
959 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
961 struct ident *ident = token->ident;
962 int nr;
964 if (!arglist || token_type(token) != TOKEN_IDENT)
965 return 0;
967 arglist = arglist->next;
969 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
970 if (arglist->ident == ident) {
971 struct argcount *count = &arglist->next->count;
972 int n;
974 token->argnum = nr;
975 token_type(token) = type;
976 switch (type) {
977 case TOKEN_MACRO_ARGUMENT:
978 n = ++count->normal;
979 break;
980 case TOKEN_QUOTED_ARGUMENT:
981 n = ++count->quoted;
982 break;
983 default:
984 n = ++count->str;
986 if (n)
987 return count->vararg ? 2 : 1;
988 token_type(token) = TOKEN_ERROR;
989 return -1;
992 return 0;
995 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
997 struct token *token = expansion;
998 struct token **p;
999 struct token *last = NULL;
1001 if (match_op(token, SPECIAL_HASHHASH))
1002 goto Econcat;
1004 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1005 if (match_op(token, '#')) {
1006 if (arglist) {
1007 struct token *next = token->next;
1008 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1009 goto Equote;
1010 next->pos.whitespace = token->pos.whitespace;
1011 token = *p = next;
1012 } else {
1013 token->pos.noexpand = 1;
1015 } else if (match_op(token, SPECIAL_HASHHASH)) {
1016 struct token *next = token->next;
1017 int arg = try_arg(next, TOKEN_QUOTED_ARGUMENT, arglist);
1018 token_type(token) = TOKEN_CONCAT;
1019 if (arg) {
1020 token = next;
1021 /* GNU kludge */
1022 if (arg == 2 && last && match_op(last, ',')) {
1023 token_type(last) = TOKEN_GNU_KLUDGE;
1024 last->next = token;
1026 } else if (match_op(next, SPECIAL_HASHHASH))
1027 token = next;
1028 else if (eof_token(next))
1029 goto Econcat;
1030 } else if (match_op(token->next, SPECIAL_HASHHASH)) {
1031 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1032 } else {
1033 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1035 if (token_type(token) == TOKEN_ERROR)
1036 goto Earg;
1037 last = token;
1039 token = alloc_token(&expansion->pos);
1040 token_type(token) = TOKEN_UNTAINT;
1041 token->ident = name;
1042 token->next = *p;
1043 *p = token;
1044 return expansion;
1046 Equote:
1047 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1048 return NULL;
1050 Econcat:
1051 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1052 return NULL;
1053 Earg:
1054 sparse_error(token->pos, "too many instances of argument in body");
1055 return NULL;
1058 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int weak)
1060 struct token *arglist, *expansion;
1061 struct token *left = token->next;
1062 struct symbol *sym;
1063 struct ident *name;
1065 if (token_type(left) != TOKEN_IDENT) {
1066 sparse_error(token->pos, "expected identifier to 'define'");
1067 return 1;
1070 name = left->ident;
1072 arglist = NULL;
1073 expansion = left->next;
1074 if (!expansion->pos.whitespace && match_op(expansion, '(')) {
1075 arglist = expansion;
1076 expansion = parse_arguments(expansion);
1077 if (!expansion)
1078 return 1;
1081 expansion = parse_expansion(expansion, arglist, name);
1082 if (!expansion)
1083 return 1;
1085 sym = lookup_macro(name);
1086 if (sym) {
1087 if (token_list_different(sym->expansion, expansion) ||
1088 token_list_different(sym->arglist, arglist)) {
1089 if (sym->weak)
1090 goto replace_it;
1091 if (weak)
1092 return 1;
1093 warning(left->pos, "preprocessor token %.*s redefined",
1094 name->len, name->name);
1095 info(sym->pos, "this was the original definition");
1097 /* Don't overwrite global defs */
1098 if (sym->scope != file_scope)
1099 goto allocate_new;
1100 goto replace_it;
1102 return 1;
1104 allocate_new:
1105 sym = alloc_symbol(left->pos, SYM_NODE);
1106 bind_symbol(sym, name, NS_MACRO);
1108 replace_it:
1109 sym->expansion = expansion;
1110 sym->arglist = arglist;
1111 sym->weak = weak;
1112 __free_token(token); /* Free the "define" token, but not the rest of the line */
1113 return 0;
1116 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1118 return do_handle_define(stream, line, token, 0);
1121 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1123 return do_handle_define(stream, line, token, 1);
1126 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1128 struct token *left = token->next;
1129 struct symbol **sym;
1131 if (token_type(left) != TOKEN_IDENT) {
1132 sparse_error(token->pos, "expected identifier to 'undef'");
1133 return 1;
1136 sym = &left->ident->symbols;
1137 while (*sym) {
1138 struct symbol *t = *sym;
1139 if (t->namespace & (NS_MACRO | NS_INVISIBLEMACRO)) {
1140 t->namespace = NS_INVISIBLEMACRO;
1141 return 1;
1143 sym = &t->next_id;
1145 return 1;
1148 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1150 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1151 free_preprocessor_line(token->next);
1152 token->next = stream->top_if;
1153 stream->top_if = token;
1154 if (false_nesting || true != 1)
1155 false_nesting++;
1156 return 0;
1159 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1161 struct token *next = token->next;
1162 int arg;
1163 if (token_type(next) == TOKEN_IDENT) {
1164 arg = token_defined(next);
1165 } else {
1166 dirty_stream(stream);
1167 if (!false_nesting)
1168 sparse_error(token->pos, "expected preprocessor identifier");
1169 arg = -1;
1171 return preprocessor_if(stream, token, arg);
1174 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1176 struct token *next = token->next;
1177 int arg;
1178 if (token_type(next) == TOKEN_IDENT) {
1179 if (!stream->dirty && !stream->ifndef) {
1180 if (!stream->protect) {
1181 stream->ifndef = token;
1182 stream->protect = next->ident;
1183 } else if (stream->protect == next->ident) {
1184 stream->ifndef = token;
1185 stream->dirty = 1;
1188 arg = !token_defined(next);
1189 } else {
1190 dirty_stream(stream);
1191 if (!false_nesting)
1192 sparse_error(token->pos, "expected preprocessor identifier");
1193 arg = -1;
1196 return preprocessor_if(stream, token, arg);
1200 * Expression handling for #if and #elif; it differs from normal expansion
1201 * due to special treatment of "defined".
1203 static int expression_value(struct token **where)
1205 struct expression *expr;
1206 struct token *p;
1207 struct token **list = where, **beginning = NULL;
1208 long long value;
1209 int state = 0;
1211 while (!eof_token(p = scan_next(list))) {
1212 switch (state) {
1213 case 0:
1214 if (token_type(p) != TOKEN_IDENT)
1215 break;
1216 if (p->ident == &defined_ident) {
1217 state = 1;
1218 beginning = list;
1219 break;
1221 if (!expand_one_symbol(list))
1222 continue;
1223 if (token_type(p) != TOKEN_IDENT)
1224 break;
1225 token_type(p) = TOKEN_ZERO_IDENT;
1226 break;
1227 case 1:
1228 if (match_op(p, '(')) {
1229 state = 2;
1230 } else {
1231 state = 0;
1232 replace_with_defined(p);
1233 *beginning = p;
1235 break;
1236 case 2:
1237 if (token_type(p) == TOKEN_IDENT)
1238 state = 3;
1239 else
1240 state = 0;
1241 replace_with_defined(p);
1242 *beginning = p;
1243 break;
1244 case 3:
1245 state = 0;
1246 if (!match_op(p, ')'))
1247 sparse_error(p->pos, "missing ')' after \"defined\"");
1248 *list = p->next;
1249 continue;
1251 list = &p->next;
1254 p = constant_expression(*where, &expr);
1255 if (!eof_token(p))
1256 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p));
1257 value = get_expression_value(expr);
1258 return value != 0;
1261 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1263 int value = 0;
1264 if (!false_nesting)
1265 value = expression_value(&token->next);
1267 dirty_stream(stream);
1268 return preprocessor_if(stream, token, value);
1271 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1273 struct token *top_if = stream->top_if;
1274 end_group(stream);
1276 if (!top_if) {
1277 nesting_error(stream);
1278 sparse_error(token->pos, "unmatched #elif within stream");
1279 return 1;
1282 if (token_type(top_if) == TOKEN_ELSE) {
1283 nesting_error(stream);
1284 sparse_error(token->pos, "#elif after #else");
1285 if (!false_nesting)
1286 false_nesting = 1;
1287 return 1;
1290 dirty_stream(stream);
1291 if (token_type(top_if) != TOKEN_IF)
1292 return 1;
1293 if (false_nesting) {
1294 if (expression_value(&token->next))
1295 false_nesting = 0;
1296 } else {
1297 false_nesting = 1;
1298 token_type(top_if) = TOKEN_SKIP_GROUPS;
1300 return 1;
1303 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1305 struct token *top_if = stream->top_if;
1306 end_group(stream);
1308 if (!top_if) {
1309 nesting_error(stream);
1310 sparse_error(token->pos, "unmatched #else within stream");
1311 return 1;
1314 if (token_type(top_if) == TOKEN_ELSE) {
1315 nesting_error(stream);
1316 sparse_error(token->pos, "#else after #else");
1318 if (false_nesting) {
1319 if (token_type(top_if) == TOKEN_IF)
1320 false_nesting = 0;
1321 } else {
1322 false_nesting = 1;
1324 token_type(top_if) = TOKEN_ELSE;
1325 return 1;
1328 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1330 struct token *top_if = stream->top_if;
1331 end_group(stream);
1332 if (!top_if) {
1333 nesting_error(stream);
1334 sparse_error(token->pos, "unmatched #endif in stream");
1335 return 1;
1337 if (false_nesting)
1338 false_nesting--;
1339 stream->top_if = top_if->next;
1340 __free_token(top_if);
1341 return 1;
1344 static const char *show_token_sequence(struct token *token)
1346 static char buffer[1024];
1347 char *ptr = buffer;
1348 int whitespace = 0;
1350 if (!token)
1351 return "<none>";
1352 while (!eof_token(token)) {
1353 const char *val = show_token(token);
1354 int len = strlen(val);
1356 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1357 sparse_error(token->pos, "too long token expansion");
1358 break;
1361 if (whitespace)
1362 *ptr++ = ' ';
1363 memcpy(ptr, val, len);
1364 ptr += len;
1365 token = token->next;
1366 whitespace = token->pos.whitespace;
1368 *ptr = 0;
1369 return buffer;
1372 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1374 warning(token->pos, "%s", show_token_sequence(token->next));
1375 return 1;
1378 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1380 sparse_error(token->pos, "%s", show_token_sequence(token->next));
1381 return 1;
1384 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1387 * Do we have any non-system includes?
1388 * Clear them out if so..
1390 *sys_includepath = NULL;
1391 return 1;
1394 static void add_path_entry(struct token *token, const char *path, const char ***where, const char **new_path)
1396 const char **dst;
1397 const char *next;
1399 /* Need one free entry.. */
1400 if (includepath[INCLUDEPATHS-2])
1401 error_die(token->pos, "too many include path entries");
1403 /* check that this is not a duplicate */
1404 dst = includepath;
1405 while (*dst) {
1406 if (strcmp(*dst, path) == 0)
1407 return;
1408 dst++;
1410 next = path;
1411 dst = *where;
1412 *where = new_path;
1415 * Move them all up starting at dst,
1416 * insert the new entry..
1418 for (;;) {
1419 const char *tmp = *dst;
1420 *dst = next;
1421 if (!next)
1422 break;
1423 next = tmp;
1424 dst++;
1428 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1430 for (;;) {
1431 token = token->next;
1432 if (eof_token(token))
1433 return 1;
1434 if (token_type(token) != TOKEN_STRING) {
1435 warning(token->pos, "expected path string");
1436 return 1;
1438 add_path_entry(token, token->string->data, &sys_includepath, sys_includepath + 1);
1442 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1444 for (;;) {
1445 token = token->next;
1446 if (eof_token(token))
1447 return 1;
1448 if (token_type(token) != TOKEN_STRING) {
1449 sparse_error(token->pos, "expected path string");
1450 return 1;
1452 add_path_entry(token, token->string->data, &sys_includepath, sys_includepath);
1456 /* Add to end on includepath list - no pointer updates */
1457 static void add_dirafter_entry(struct token *token, const char *path)
1459 const char **dst = includepath;
1461 /* Need one free entry.. */
1462 if (includepath[INCLUDEPATHS-2])
1463 error_die(token->pos, "too many include path entries");
1465 /* Add to the end */
1466 while (*dst)
1467 dst++;
1468 *dst = path;
1469 dst++;
1470 *dst = NULL;
1473 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1475 for (;;) {
1476 token = token->next;
1477 if (eof_token(token))
1478 return 1;
1479 if (token_type(token) != TOKEN_STRING) {
1480 sparse_error(token->pos, "expected path string");
1481 return 1;
1483 add_dirafter_entry(token, token->string->data);
1487 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1490 * -I-
1491 * From info gcc:
1492 * Split the include path. Any directories specified with `-I'
1493 * options before `-I-' are searched only for headers requested with
1494 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1495 * If additional directories are specified with `-I' options after
1496 * the `-I-', those directories are searched for all `#include'
1497 * directives.
1498 * In addition, `-I-' inhibits the use of the directory of the current
1499 * file directory as the first search directory for `#include "FILE"'.
1501 quote_includepath = includepath+1;
1502 angle_includepath = sys_includepath;
1503 return 1;
1507 * We replace "#pragma xxx" with "__pragma__" in the token
1508 * stream. Just as an example.
1510 * We'll just #define that away for now, but the theory here
1511 * is that we can use this to insert arbitrary token sequences
1512 * to turn the pragma's into internal front-end sequences for
1513 * when we actually start caring about them.
1515 * So eventually this will turn into some kind of extended
1516 * __attribute__() like thing, except called __pragma__(xxx).
1518 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1520 struct token *next = *line;
1522 token->ident = &pragma_ident;
1523 token->pos.newline = 1;
1524 token->pos.whitespace = 1;
1525 token->pos.pos = 1;
1526 *line = token;
1527 token->next = next;
1528 return 0;
1532 * We ignore #line for now.
1534 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1536 return 1;
1539 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1541 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1542 return 1;
1546 static void init_preprocessor(void)
1548 int i;
1549 int stream = init_stream("preprocessor", -1, includepath);
1550 static struct {
1551 const char *name;
1552 int (*handler)(struct stream *, struct token **, struct token *);
1553 } normal[] = {
1554 { "define", handle_define },
1555 { "weak_define",handle_weak_define },
1556 { "undef", handle_undef },
1557 { "warning", handle_warning },
1558 { "error", handle_error },
1559 { "include", handle_include },
1560 { "include_next",handle_include_next },
1561 { "pragma", handle_pragma },
1562 { "line", handle_line },
1564 // our internal preprocessor tokens
1565 { "nostdinc", handle_nostdinc },
1566 { "add_include", handle_add_include },
1567 { "add_isystem", handle_add_isystem },
1568 { "add_dirafter", handle_add_dirafter },
1569 { "split_include", handle_split_include },
1570 }, special[] = {
1571 { "ifdef", handle_ifdef },
1572 { "ifndef", handle_ifndef },
1573 { "else", handle_else },
1574 { "endif", handle_endif },
1575 { "if", handle_if },
1576 { "elif", handle_elif },
1579 for (i = 0; i < (sizeof (normal) / sizeof (normal[0])); i++) {
1580 struct symbol *sym;
1581 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1582 sym->handler = normal[i].handler;
1583 sym->normal = 1;
1585 for (i = 0; i < (sizeof (special) / sizeof (special[0])); i++) {
1586 struct symbol *sym;
1587 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1588 sym->handler = special[i].handler;
1589 sym->normal = 0;
1594 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1596 int (*handler)(struct stream *, struct token **, struct token *);
1597 struct token *token = start->next;
1598 int is_normal = 1;
1600 if (eof_token(token))
1601 return;
1603 if (token_type(token) == TOKEN_IDENT) {
1604 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1605 if (sym) {
1606 handler = sym->handler;
1607 is_normal = sym->normal;
1608 } else {
1609 handler = handle_nondirective;
1611 } else if (token_type(token) == TOKEN_NUMBER) {
1612 handler = handle_line;
1613 } else {
1614 handler = handle_nondirective;
1617 if (is_normal) {
1618 dirty_stream(stream);
1619 if (false_nesting)
1620 goto out;
1622 if (!handler(stream, line, token)) /* all set */
1623 return;
1625 out:
1626 free_preprocessor_line(token);
1629 static void preprocessor_line(struct stream *stream, struct token **line)
1631 struct token *start = *line, *next;
1632 struct token **tp = &start->next;
1634 for (;;) {
1635 next = *tp;
1636 if (next->pos.newline)
1637 break;
1638 tp = &next->next;
1640 *line = next;
1641 *tp = &eof_token_entry;
1642 handle_preprocessor_line(stream, line, start);
1645 static void do_preprocess(struct token **list)
1647 struct token *next;
1649 while (!eof_token(next = scan_next(list))) {
1650 struct stream *stream = input_streams + next->pos.stream;
1652 if (next->pos.newline && match_op(next, '#')) {
1653 if (!next->pos.noexpand) {
1654 preprocessor_line(stream, list);
1655 __free_token(next); /* Free the '#' token */
1656 continue;
1660 switch (token_type(next)) {
1661 case TOKEN_STREAMEND:
1662 if (stream->top_if) {
1663 nesting_error(stream);
1664 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1665 stream->top_if = NULL;
1666 false_nesting = 0;
1668 if (!stream->dirty)
1669 stream->constant = CONSTANT_FILE_YES;
1670 *list = next->next;
1671 continue;
1672 case TOKEN_STREAMBEGIN:
1673 *list = next->next;
1674 continue;
1676 default:
1677 dirty_stream(stream);
1678 if (false_nesting) {
1679 *list = next->next;
1680 __free_token(next);
1681 continue;
1684 if (token_type(next) != TOKEN_IDENT ||
1685 expand_one_symbol(list))
1686 list = &next->next;
1691 struct token * preprocess(struct token *token)
1693 preprocessing = 1;
1694 init_preprocessor();
1695 do_preprocess(&token);
1697 // Drop all expressions from pre-processing, they're not used any more.
1698 // This is not true when we have multiple files, though ;/
1699 // clear_expression_alloc();
1700 preprocessing = 0;
1702 return token;