smatch_clear_buffer: memcpy() should set the destination to unknown
[smatch.git] / pre-process.c
blob1f25c50a85adca9077c6b1bb51e62f29caa378f2
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 "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 NULL
41 static const char **quote_includepath = includepath;
42 static const char **angle_includepath = includepath + 1;
43 static const char **isys_includepath = includepath + 1;
44 static const char **sys_includepath = includepath + 1;
45 static const char **dirafter_includepath = includepath + 3;
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 /* Expand symbol 'sym' at '*list' */
86 static int expand(struct token **, struct symbol *);
88 static void replace_with_string(struct token *token, const char *str)
90 int size = strlen(str) + 1;
91 struct string *s = __alloc_string(size);
93 s->length = size;
94 memcpy(s->data, str, size);
95 token_type(token) = TOKEN_STRING;
96 token->string = s;
99 static void replace_with_integer(struct token *token, unsigned int val)
101 char *buf = __alloc_bytes(11);
102 sprintf(buf, "%u", val);
103 token_type(token) = TOKEN_NUMBER;
104 token->number = buf;
107 static struct symbol *lookup_macro(struct ident *ident)
109 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
110 if (sym && sym->namespace != NS_MACRO)
111 sym = NULL;
112 return sym;
115 static int token_defined(struct token *token)
117 if (token_type(token) == TOKEN_IDENT) {
118 struct symbol *sym = lookup_macro(token->ident);
119 if (sym) {
120 sym->used_in = file_scope;
121 return 1;
123 return 0;
126 sparse_error(token->pos, "expected preprocessor identifier");
127 return 0;
130 static void replace_with_defined(struct token *token)
132 static const char *string[] = { "0", "1" };
133 int defined = token_defined(token);
135 token_type(token) = TOKEN_NUMBER;
136 token->number = string[defined];
139 static int expand_one_symbol(struct token **list)
141 struct token *token = *list;
142 struct symbol *sym;
143 static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
144 static time_t t = 0;
146 if (token->pos.noexpand)
147 return 1;
149 sym = lookup_macro(token->ident);
150 if (sym) {
151 store_macro_pos(token);
152 sym->used_in = file_scope;
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));
159 } else if (token->ident == &__DATE___ident) {
160 if (!t)
161 time(&t);
162 strftime(buffer, 12, "%b %e %Y", localtime(&t));
163 replace_with_string(token, buffer);
164 } else if (token->ident == &__TIME___ident) {
165 if (!t)
166 time(&t);
167 strftime(buffer, 9, "%T", localtime(&t));
168 replace_with_string(token, buffer);
170 return 1;
173 static inline struct token *scan_next(struct token **where)
175 struct token *token = *where;
176 if (token_type(token) != TOKEN_UNTAINT)
177 return token;
178 do {
179 token->ident->tainted = 0;
180 token = token->next;
181 } while (token_type(token) == TOKEN_UNTAINT);
182 *where = token;
183 return token;
186 static void expand_list(struct token **list)
188 struct token *next;
189 while (!eof_token(next = scan_next(list))) {
190 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
191 list = &next->next;
195 static void preprocessor_line(struct stream *stream, struct token **line);
197 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
199 struct stream *stream = input_streams + prev->pos.stream;
200 struct token **p = &prev->next;
201 struct token *next;
202 int nesting = 0;
204 while (!eof_token(next = scan_next(p))) {
205 if (next->pos.newline && match_op(next, '#')) {
206 if (!next->pos.noexpand) {
207 sparse_error(next->pos,
208 "directive in argument list");
209 preprocessor_line(stream, p);
210 __free_token(next); /* Free the '#' token */
211 continue;
214 switch (token_type(next)) {
215 case TOKEN_STREAMEND:
216 case TOKEN_STREAMBEGIN:
217 *p = &eof_token_entry;
218 return next;
220 if (false_nesting) {
221 *p = next->next;
222 __free_token(next);
223 continue;
225 if (match_op(next, '(')) {
226 nesting++;
227 } else if (match_op(next, ')')) {
228 if (!nesting--)
229 break;
230 } else if (match_op(next, ',') && !nesting && !vararg) {
231 break;
233 next->pos.stream = pos->stream;
234 next->pos.line = pos->line;
235 next->pos.pos = pos->pos;
236 p = &next->next;
238 *p = &eof_token_entry;
239 return next;
243 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
246 struct arg {
247 struct token *arg;
248 struct token *expanded;
249 struct token *str;
250 int n_normal;
251 int n_quoted;
252 int n_str;
255 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
257 int wanted = arglist->count.normal;
258 struct token *next = NULL;
259 int count = 0;
261 arglist = arglist->next; /* skip counter */
263 if (!wanted) {
264 next = collect_arg(start, 0, &what->pos);
265 if (eof_token(next))
266 goto Eclosing;
267 if (!eof_token(start->next) || !match_op(next, ')')) {
268 count++;
269 goto Emany;
271 } else {
272 for (count = 0; count < wanted; count++) {
273 struct argcount *p = &arglist->next->count;
274 next = collect_arg(start, p->vararg, &what->pos);
275 arglist = arglist->next->next;
276 if (eof_token(next))
277 goto Eclosing;
278 args[count].arg = start->next;
279 args[count].n_normal = p->normal;
280 args[count].n_quoted = p->quoted;
281 args[count].n_str = p->str;
282 if (match_op(next, ')')) {
283 count++;
284 break;
286 start = next;
288 if (count == wanted && !match_op(next, ')'))
289 goto Emany;
290 if (count == wanted - 1) {
291 struct argcount *p = &arglist->next->count;
292 if (!p->vararg)
293 goto Efew;
294 args[count].arg = NULL;
295 args[count].n_normal = p->normal;
296 args[count].n_quoted = p->quoted;
297 args[count].n_str = p->str;
299 if (count < wanted - 1)
300 goto Efew;
302 what->next = next->next;
303 return 1;
305 Efew:
306 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
307 show_token(what), wanted, count);
308 goto out;
309 Emany:
310 while (match_op(next, ',')) {
311 next = collect_arg(next, 0, &what->pos);
312 count++;
314 if (eof_token(next))
315 goto Eclosing;
316 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
317 show_token(what), count, wanted);
318 goto out;
319 Eclosing:
320 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
321 show_token(what));
322 out:
323 what->next = next->next;
324 return 0;
327 static struct token *dup_list(struct token *list)
329 struct token *res = NULL;
330 struct token **p = &res;
332 while (!eof_token(list)) {
333 struct token *newtok = __alloc_token(0);
334 *newtok = *list;
335 *p = newtok;
336 p = &newtok->next;
337 list = list->next;
339 return res;
342 static const char *quote_token_sequence(struct token *token)
344 static char buffer[1024];
345 char *ptr = buffer;
346 int whitespace = 0;
348 while (!eof_token(token)) {
349 const char *val = quote_token(token);
350 int len = strlen(val);
352 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
353 sparse_error(token->pos, "too long token expansion");
354 break;
357 if (whitespace)
358 *ptr++ = ' ';
359 memcpy(ptr, val, len);
360 ptr += len;
361 token = token->next;
362 whitespace = token->pos.whitespace;
364 *ptr = 0;
365 return buffer;
368 static struct token *stringify(struct token *arg)
370 const char *s = quote_token_sequence(arg);
371 int size = strlen(s)+1;
372 struct token *token = __alloc_token(0);
373 struct string *string = __alloc_string(size);
375 memcpy(string->data, s, size);
376 string->length = size;
377 token->pos = arg->pos;
378 token_type(token) = TOKEN_STRING;
379 token->string = string;
380 token->next = &eof_token_entry;
381 return token;
384 static void expand_arguments(int count, struct arg *args)
386 int i;
387 for (i = 0; i < count; i++) {
388 struct token *arg = args[i].arg;
389 if (!arg)
390 arg = &eof_token_entry;
391 if (args[i].n_str)
392 args[i].str = stringify(arg);
393 if (args[i].n_normal) {
394 if (!args[i].n_quoted) {
395 args[i].expanded = arg;
396 args[i].arg = NULL;
397 } else if (eof_token(arg)) {
398 args[i].expanded = arg;
399 } else {
400 args[i].expanded = dup_list(arg);
402 expand_list(&args[i].expanded);
408 * Possibly valid combinations:
409 * - ident + ident -> ident
410 * - ident + number -> ident unless number contains '.', '+' or '-'.
411 * - 'L' + char constant -> wide char constant
412 * - 'L' + string literal -> wide string literal
413 * - number + number -> number
414 * - number + ident -> number
415 * - number + '.' -> number
416 * - number + '+' or '-' -> number, if number used to end on [eEpP].
417 * - '.' + number -> number, if number used to start with a digit.
418 * - special + special -> either special or an error.
420 static enum token_type combine(struct token *left, struct token *right, char *p)
422 int len;
423 enum token_type t1 = token_type(left), t2 = token_type(right);
425 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
426 return TOKEN_ERROR;
428 if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
429 if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
430 return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
431 if (t2 == TOKEN_STRING)
432 return TOKEN_WIDE_STRING;
435 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
436 return TOKEN_ERROR;
438 strcpy(p, show_token(left));
439 strcat(p, show_token(right));
440 len = strlen(p);
442 if (len >= 256)
443 return TOKEN_ERROR;
445 if (t1 == TOKEN_IDENT) {
446 if (t2 == TOKEN_SPECIAL)
447 return TOKEN_ERROR;
448 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
449 return TOKEN_ERROR;
450 return TOKEN_IDENT;
453 if (t1 == TOKEN_NUMBER) {
454 if (t2 == TOKEN_SPECIAL) {
455 switch (right->special) {
456 case '.':
457 break;
458 case '+': case '-':
459 if (strchr("eEpP", p[len - 2]))
460 break;
461 default:
462 return TOKEN_ERROR;
465 return TOKEN_NUMBER;
468 if (p[0] == '.' && isdigit((unsigned char)p[1]))
469 return TOKEN_NUMBER;
471 return TOKEN_SPECIAL;
474 static int merge(struct token *left, struct token *right)
476 static char buffer[512];
477 enum token_type res = combine(left, right, buffer);
478 int n;
480 switch (res) {
481 case TOKEN_IDENT:
482 left->ident = built_in_ident(buffer);
483 left->pos.noexpand = 0;
484 return 1;
486 case TOKEN_NUMBER: {
487 char *number = __alloc_bytes(strlen(buffer) + 1);
488 memcpy(number, buffer, strlen(buffer) + 1);
489 token_type(left) = TOKEN_NUMBER; /* could be . + num */
490 left->number = number;
491 return 1;
494 case TOKEN_SPECIAL:
495 if (buffer[2] && buffer[3])
496 break;
497 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
498 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
499 left->special = n;
500 return 1;
503 break;
505 case TOKEN_WIDE_CHAR:
506 case TOKEN_WIDE_STRING:
507 token_type(left) = res;
508 left->pos.noexpand = 0;
509 left->string = right->string;
510 return 1;
512 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
513 token_type(left) = res;
514 left->pos.noexpand = 0;
515 memcpy(left->embedded, right->embedded, 4);
516 return 1;
518 default:
521 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
522 return 0;
525 static struct token *dup_token(struct token *token, struct position *streampos)
527 struct token *alloc = alloc_token(streampos);
528 token_type(alloc) = token_type(token);
529 alloc->pos.newline = token->pos.newline;
530 alloc->pos.whitespace = token->pos.whitespace;
531 alloc->number = token->number;
532 alloc->pos.noexpand = token->pos.noexpand;
533 return alloc;
536 static struct token **copy(struct token **where, struct token *list, int *count)
538 int need_copy = --*count;
539 while (!eof_token(list)) {
540 struct token *token;
541 if (need_copy)
542 token = dup_token(list, &list->pos);
543 else
544 token = list;
545 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
546 token->pos.noexpand = 1;
547 *where = token;
548 where = &token->next;
549 list = list->next;
551 *where = &eof_token_entry;
552 return where;
555 static int handle_kludge(struct token **p, struct arg *args)
557 struct token *t = (*p)->next->next;
558 while (1) {
559 struct arg *v = &args[t->argnum];
560 if (token_type(t->next) != TOKEN_CONCAT) {
561 if (v->arg) {
562 /* ignore the first ## */
563 *p = (*p)->next;
564 return 0;
566 /* skip the entire thing */
567 *p = t;
568 return 1;
570 if (v->arg && !eof_token(v->arg))
571 return 0; /* no magic */
572 t = t->next->next;
576 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
578 struct position *base_pos = &(*list)->pos;
579 int *count;
580 enum {Normal, Placeholder, Concat} state = Normal;
582 for (; !eof_token(body); body = body->next) {
583 struct token *added, *arg;
584 struct token **tail;
585 struct token *t;
587 switch (token_type(body)) {
588 case TOKEN_GNU_KLUDGE:
590 * GNU kludge: if we had <comma>##<vararg>, behaviour
591 * depends on whether we had enough arguments to have
592 * a vararg. If we did, ## is just ignored. Otherwise
593 * both , and ## are ignored. Worse, there can be
594 * an arbitrary number of ##<arg> in between; if all of
595 * those are empty, we act as if they hadn't been there,
596 * otherwise we act as if the kludge didn't exist.
598 t = body;
599 if (handle_kludge(&body, args)) {
600 if (state == Concat)
601 state = Normal;
602 else
603 state = Placeholder;
604 continue;
606 added = dup_token(t, base_pos);
607 token_type(added) = TOKEN_SPECIAL;
608 tail = &added->next;
609 break;
611 case TOKEN_STR_ARGUMENT:
612 arg = args[body->argnum].str;
613 count = &args[body->argnum].n_str;
614 goto copy_arg;
616 case TOKEN_QUOTED_ARGUMENT:
617 arg = args[body->argnum].arg;
618 count = &args[body->argnum].n_quoted;
619 if (!arg || eof_token(arg)) {
620 if (state == Concat)
621 state = Normal;
622 else
623 state = Placeholder;
624 continue;
626 goto copy_arg;
628 case TOKEN_MACRO_ARGUMENT:
629 arg = args[body->argnum].expanded;
630 count = &args[body->argnum].n_normal;
631 if (eof_token(arg)) {
632 state = Normal;
633 continue;
635 copy_arg:
636 tail = copy(&added, arg, count);
637 added->pos.newline = body->pos.newline;
638 added->pos.whitespace = body->pos.whitespace;
639 break;
641 case TOKEN_CONCAT:
642 if (state == Placeholder)
643 state = Normal;
644 else
645 state = Concat;
646 continue;
648 case TOKEN_IDENT:
649 added = dup_token(body, base_pos);
650 if (added->ident->tainted)
651 added->pos.noexpand = 1;
652 tail = &added->next;
653 break;
655 default:
656 added = dup_token(body, base_pos);
657 tail = &added->next;
658 break;
662 * if we got to doing real concatenation, we already have
663 * added something into the list, so containing_token() is OK.
665 if (state == Concat && merge(containing_token(list), added)) {
666 *list = added->next;
667 if (tail != &added->next)
668 list = tail;
669 } else {
670 *list = added;
671 list = tail;
673 state = Normal;
675 *list = &eof_token_entry;
676 return list;
679 static int expand(struct token **list, struct symbol *sym)
681 struct token *last;
682 struct token *token = *list;
683 struct ident *expanding = token->ident;
684 struct token **tail;
685 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
686 struct arg args[nargs];
688 if (expanding->tainted) {
689 token->pos.noexpand = 1;
690 return 1;
693 if (sym->arglist) {
694 if (!match_op(scan_next(&token->next), '('))
695 return 1;
696 if (!collect_arguments(token->next, sym->arglist, args, token))
697 return 1;
698 expand_arguments(nargs, args);
701 expanding->tainted = 1;
703 last = token->next;
704 tail = substitute(list, sym->expansion, args);
706 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
707 * We still can lose the newline flag if the sucker expands to nothing,
708 * but the price of dealing with that is probably too high (we'd need
709 * to collect the flags during scan_next())
711 (*list)->pos.newline = token->pos.newline;
712 (*list)->pos.whitespace = token->pos.whitespace;
713 *tail = last;
715 return 0;
718 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
720 static char buffer[256];
721 char *ptr = buffer;
723 while (!eof_token(token) && !match_op(token, endop)) {
724 int len;
725 const char *val = token->string->data;
726 if (token_type(token) != TOKEN_STRING)
727 val = show_token(token);
728 len = strlen(val);
729 memcpy(ptr, val, len);
730 ptr += len;
731 token = token->next;
733 *ptr = 0;
734 if (endop && !match_op(token, endop))
735 sparse_error(start->pos, "expected '>' at end of filename");
736 return buffer;
739 static int already_tokenized(const char *path)
741 int stream, next;
743 for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
744 struct stream *s = input_streams + stream;
746 next = s->next_stream;
747 if (s->constant != CONSTANT_FILE_YES)
748 continue;
749 if (strcmp(path, s->name))
750 continue;
751 if (s->protect && !lookup_macro(s->protect))
752 continue;
753 return 1;
755 return 0;
758 /* Handle include of header files.
759 * The relevant options are made compatible with gcc. The only options that
760 * are not supported is -withprefix and friends.
762 * Three set of include paths are known:
763 * quote_includepath: Path to search when using #include "file.h"
764 * angle_includepath: Paths to search when using #include <file.h>
765 * isys_includepath: Paths specified with -isystem, come before the
766 * built-in system include paths. Gcc would suppress
767 * warnings from system headers. Here we separate
768 * them from the angle_ ones to keep search ordering.
770 * sys_includepath: Built-in include paths.
771 * dirafter_includepath Paths added with -dirafter.
773 * The above is implemented as one array with pointers
774 * +--------------+
775 * quote_includepath ---> | |
776 * +--------------+
777 * | |
778 * +--------------+
779 * angle_includepath ---> | |
780 * +--------------+
781 * isys_includepath ---> | |
782 * +--------------+
783 * sys_includepath ---> | |
784 * +--------------+
785 * dirafter_includepath -> | |
786 * +--------------+
788 * -I dir insert dir just before isys_includepath and move the rest
789 * -I- makes all dirs specified with -I before to quote dirs only and
790 * angle_includepath is set equal to isys_includepath.
791 * -nostdinc removes all sys dirs by storing NULL in entry pointed
792 * to by * sys_includepath. Note that this will reset all dirs built-in
793 * and added before -nostdinc by -isystem and -idirafter.
794 * -isystem dir adds dir where isys_includepath points adding this dir as
795 * first systemdir
796 * -idirafter dir adds dir to the end of the list
799 static void set_stream_include_path(struct stream *stream)
801 const char *path = stream->path;
802 if (!path) {
803 const char *p = strrchr(stream->name, '/');
804 path = "";
805 if (p) {
806 int len = p - stream->name + 1;
807 char *m = malloc(len+1);
808 /* This includes the final "/" */
809 memcpy(m, stream->name, len);
810 m[len] = 0;
811 path = m;
813 stream->path = path;
815 includepath[0] = path;
818 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
820 int fd;
821 int plen = strlen(path);
822 static char fullname[PATH_MAX];
824 memcpy(fullname, path, plen);
825 if (plen && path[plen-1] != '/') {
826 fullname[plen] = '/';
827 plen++;
829 memcpy(fullname+plen, filename, flen);
830 if (already_tokenized(fullname))
831 return 1;
832 fd = open(fullname, O_RDONLY);
833 if (fd >= 0) {
834 char * streamname = __alloc_bytes(plen + flen);
835 memcpy(streamname, fullname, plen + flen);
836 *where = tokenize(streamname, fd, *where, next_path);
837 close(fd);
838 return 1;
840 return 0;
843 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
845 const char *path;
847 while ((path = *pptr++) != NULL) {
848 if (!try_include(path, filename, flen, list, pptr))
849 continue;
850 return 1;
852 return 0;
855 static int free_preprocessor_line(struct token *token)
857 while (token_type(token) != TOKEN_EOF) {
858 struct token *free = token;
859 token = token->next;
860 __free_token(free);
862 return 1;
865 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how)
867 const char *filename;
868 struct token *next;
869 const char **path;
870 int expect;
871 int flen;
873 next = token->next;
874 expect = '>';
875 if (!match_op(next, '<')) {
876 expand_list(&token->next);
877 expect = 0;
878 next = token;
879 if (match_op(token->next, '<')) {
880 next = token->next;
881 expect = '>';
885 token = next->next;
886 filename = token_name_sequence(token, expect, token);
887 flen = strlen(filename) + 1;
889 /* Absolute path? */
890 if (filename[0] == '/') {
891 if (try_include("", filename, flen, list, includepath))
892 return 0;
893 goto out;
896 switch (how) {
897 case 1:
898 path = stream->next_path;
899 break;
900 case 2:
901 includepath[0] = "";
902 path = includepath;
903 break;
904 default:
905 /* Dir of input file is first dir to search for quoted includes */
906 set_stream_include_path(stream);
907 path = expect ? angle_includepath : quote_includepath;
908 break;
910 /* Check the standard include paths.. */
911 if (do_include_path(path, list, token, filename, flen))
912 return 0;
913 out:
914 error_die(token->pos, "unable to open '%s'", filename);
917 static int handle_include(struct stream *stream, struct token **list, struct token *token)
919 return handle_include_path(stream, list, token, 0);
922 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
924 return handle_include_path(stream, list, token, 1);
927 static int handle_argv_include(struct stream *stream, struct token **list, struct token *token)
929 return handle_include_path(stream, list, token, 2);
932 static int token_different(struct token *t1, struct token *t2)
934 int different;
936 if (token_type(t1) != token_type(t2))
937 return 1;
939 switch (token_type(t1)) {
940 case TOKEN_IDENT:
941 different = t1->ident != t2->ident;
942 break;
943 case TOKEN_ARG_COUNT:
944 case TOKEN_UNTAINT:
945 case TOKEN_CONCAT:
946 case TOKEN_GNU_KLUDGE:
947 different = 0;
948 break;
949 case TOKEN_NUMBER:
950 different = strcmp(t1->number, t2->number);
951 break;
952 case TOKEN_SPECIAL:
953 different = t1->special != t2->special;
954 break;
955 case TOKEN_MACRO_ARGUMENT:
956 case TOKEN_QUOTED_ARGUMENT:
957 case TOKEN_STR_ARGUMENT:
958 different = t1->argnum != t2->argnum;
959 break;
960 case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
961 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
962 different = memcmp(t1->embedded, t2->embedded, 4);
963 break;
964 case TOKEN_CHAR:
965 case TOKEN_WIDE_CHAR:
966 case TOKEN_STRING:
967 case TOKEN_WIDE_STRING: {
968 struct string *s1, *s2;
970 s1 = t1->string;
971 s2 = t2->string;
972 different = 1;
973 if (s1->length != s2->length)
974 break;
975 different = memcmp(s1->data, s2->data, s1->length);
976 break;
978 default:
979 different = 1;
980 break;
982 return different;
985 static int token_list_different(struct token *list1, struct token *list2)
987 for (;;) {
988 if (list1 == list2)
989 return 0;
990 if (!list1 || !list2)
991 return 1;
992 if (token_different(list1, list2))
993 return 1;
994 list1 = list1->next;
995 list2 = list2->next;
999 static inline void set_arg_count(struct token *token)
1001 token_type(token) = TOKEN_ARG_COUNT;
1002 token->count.normal = token->count.quoted =
1003 token->count.str = token->count.vararg = 0;
1006 static struct token *parse_arguments(struct token *list)
1008 struct token *arg = list->next, *next = list;
1009 struct argcount *count = &list->count;
1011 set_arg_count(list);
1013 if (match_op(arg, ')')) {
1014 next = arg->next;
1015 list->next = &eof_token_entry;
1016 return next;
1019 while (token_type(arg) == TOKEN_IDENT) {
1020 if (arg->ident == &__VA_ARGS___ident)
1021 goto Eva_args;
1022 if (!++count->normal)
1023 goto Eargs;
1024 next = arg->next;
1026 if (match_op(next, ',')) {
1027 set_arg_count(next);
1028 arg = next->next;
1029 continue;
1032 if (match_op(next, ')')) {
1033 set_arg_count(next);
1034 next = next->next;
1035 arg->next->next = &eof_token_entry;
1036 return next;
1039 /* normal cases are finished here */
1041 if (match_op(next, SPECIAL_ELLIPSIS)) {
1042 if (match_op(next->next, ')')) {
1043 set_arg_count(next);
1044 next->count.vararg = 1;
1045 next = next->next;
1046 arg->next->next = &eof_token_entry;
1047 return next->next;
1050 arg = next;
1051 goto Enotclosed;
1054 if (eof_token(next)) {
1055 goto Enotclosed;
1056 } else {
1057 arg = next;
1058 goto Ebadstuff;
1062 if (match_op(arg, SPECIAL_ELLIPSIS)) {
1063 next = arg->next;
1064 token_type(arg) = TOKEN_IDENT;
1065 arg->ident = &__VA_ARGS___ident;
1066 if (!match_op(next, ')'))
1067 goto Enotclosed;
1068 if (!++count->normal)
1069 goto Eargs;
1070 set_arg_count(next);
1071 next->count.vararg = 1;
1072 next = next->next;
1073 arg->next->next = &eof_token_entry;
1074 return next;
1077 if (eof_token(arg)) {
1078 arg = next;
1079 goto Enotclosed;
1081 if (match_op(arg, ','))
1082 goto Emissing;
1083 else
1084 goto Ebadstuff;
1087 Emissing:
1088 sparse_error(arg->pos, "parameter name missing");
1089 return NULL;
1090 Ebadstuff:
1091 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
1092 show_token(arg));
1093 return NULL;
1094 Enotclosed:
1095 sparse_error(arg->pos, "missing ')' in macro parameter list");
1096 return NULL;
1097 Eva_args:
1098 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1099 return NULL;
1100 Eargs:
1101 sparse_error(arg->pos, "too many arguments in macro definition");
1102 return NULL;
1105 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1107 struct ident *ident = token->ident;
1108 int nr;
1110 if (!arglist || token_type(token) != TOKEN_IDENT)
1111 return 0;
1113 arglist = arglist->next;
1115 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1116 if (arglist->ident == ident) {
1117 struct argcount *count = &arglist->next->count;
1118 int n;
1120 token->argnum = nr;
1121 token_type(token) = type;
1122 switch (type) {
1123 case TOKEN_MACRO_ARGUMENT:
1124 n = ++count->normal;
1125 break;
1126 case TOKEN_QUOTED_ARGUMENT:
1127 n = ++count->quoted;
1128 break;
1129 default:
1130 n = ++count->str;
1132 if (n)
1133 return count->vararg ? 2 : 1;
1135 * XXX - need saner handling of that
1136 * (>= 1024 instances of argument)
1138 token_type(token) = TOKEN_ERROR;
1139 return -1;
1142 return 0;
1145 static struct token *handle_hash(struct token **p, struct token *arglist)
1147 struct token *token = *p;
1148 if (arglist) {
1149 struct token *next = token->next;
1150 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1151 goto Equote;
1152 next->pos.whitespace = token->pos.whitespace;
1153 __free_token(token);
1154 token = *p = next;
1155 } else {
1156 token->pos.noexpand = 1;
1158 return token;
1160 Equote:
1161 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1162 return NULL;
1165 /* token->next is ## */
1166 static struct token *handle_hashhash(struct token *token, struct token *arglist)
1168 struct token *last = token;
1169 struct token *concat;
1170 int state = match_op(token, ',');
1172 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1174 while (1) {
1175 struct token *t;
1176 int is_arg;
1178 /* eat duplicate ## */
1179 concat = token->next;
1180 while (match_op(t = concat->next, SPECIAL_HASHHASH)) {
1181 token->next = t;
1182 __free_token(concat);
1183 concat = t;
1185 token_type(concat) = TOKEN_CONCAT;
1187 if (eof_token(t))
1188 goto Econcat;
1190 if (match_op(t, '#')) {
1191 t = handle_hash(&concat->next, arglist);
1192 if (!t)
1193 return NULL;
1196 is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
1198 if (state == 1 && is_arg) {
1199 state = is_arg;
1200 } else {
1201 last = t;
1202 state = match_op(t, ',');
1205 token = t;
1206 if (!match_op(token->next, SPECIAL_HASHHASH))
1207 break;
1209 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1210 if (state == 2)
1211 token_type(last) = TOKEN_GNU_KLUDGE;
1212 return token;
1214 Econcat:
1215 sparse_error(concat->pos, "'##' cannot appear at the ends of macro expansion");
1216 return NULL;
1219 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1221 struct token *token = expansion;
1222 struct token **p;
1224 if (match_op(token, SPECIAL_HASHHASH))
1225 goto Econcat;
1227 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1228 if (match_op(token, '#')) {
1229 token = handle_hash(p, arglist);
1230 if (!token)
1231 return NULL;
1233 if (match_op(token->next, SPECIAL_HASHHASH)) {
1234 token = handle_hashhash(token, arglist);
1235 if (!token)
1236 return NULL;
1237 } else {
1238 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1240 if (token_type(token) == TOKEN_ERROR)
1241 goto Earg;
1243 token = alloc_token(&expansion->pos);
1244 token_type(token) = TOKEN_UNTAINT;
1245 token->ident = name;
1246 token->next = *p;
1247 *p = token;
1248 return expansion;
1250 Econcat:
1251 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1252 return NULL;
1253 Earg:
1254 sparse_error(token->pos, "too many instances of argument in body");
1255 return NULL;
1258 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1260 struct token *arglist, *expansion;
1261 struct token *left = token->next;
1262 struct symbol *sym;
1263 struct ident *name;
1264 int ret;
1266 if (token_type(left) != TOKEN_IDENT) {
1267 sparse_error(token->pos, "expected identifier to 'define'");
1268 return 1;
1271 name = left->ident;
1273 arglist = NULL;
1274 expansion = left->next;
1275 if (!expansion->pos.whitespace) {
1276 if (match_op(expansion, '(')) {
1277 arglist = expansion;
1278 expansion = parse_arguments(expansion);
1279 if (!expansion)
1280 return 1;
1281 } else if (!eof_token(expansion)) {
1282 warning(expansion->pos,
1283 "no whitespace before object-like macro body");
1287 expansion = parse_expansion(expansion, arglist, name);
1288 if (!expansion)
1289 return 1;
1291 ret = 1;
1292 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1293 if (sym) {
1294 int clean;
1296 if (attr < sym->attr)
1297 goto out;
1299 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1301 if (token_list_different(sym->expansion, expansion) ||
1302 token_list_different(sym->arglist, arglist)) {
1303 ret = 0;
1304 if ((clean && attr == SYM_ATTR_NORMAL)
1305 || sym->used_in == file_scope) {
1306 warning(left->pos, "preprocessor token %.*s redefined",
1307 name->len, name->name);
1308 info(sym->pos, "this was the original definition");
1310 } else if (clean)
1311 goto out;
1314 if (!sym || sym->scope != file_scope) {
1315 sym = alloc_symbol(left->pos, SYM_NODE);
1316 bind_symbol(sym, name, NS_MACRO);
1317 ret = 0;
1320 if (!ret) {
1321 sym->expansion = expansion;
1322 sym->arglist = arglist;
1323 __free_token(token); /* Free the "define" token, but not the rest of the line */
1326 sym->namespace = NS_MACRO;
1327 sym->used_in = NULL;
1328 sym->attr = attr;
1329 out:
1330 return ret;
1333 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1335 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1338 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1340 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1343 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1345 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1348 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1350 struct token *left = token->next;
1351 struct symbol *sym;
1353 if (token_type(left) != TOKEN_IDENT) {
1354 sparse_error(token->pos, "expected identifier to 'undef'");
1355 return 1;
1358 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1359 if (sym) {
1360 if (attr < sym->attr)
1361 return 1;
1362 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1363 return 1;
1364 } else if (attr <= SYM_ATTR_NORMAL)
1365 return 1;
1367 if (!sym || sym->scope != file_scope) {
1368 sym = alloc_symbol(left->pos, SYM_NODE);
1369 bind_symbol(sym, left->ident, NS_MACRO);
1372 sym->namespace = NS_UNDEF;
1373 sym->used_in = NULL;
1374 sym->attr = attr;
1376 return 1;
1379 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1381 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1384 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1386 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1389 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1391 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1392 free_preprocessor_line(token->next);
1393 token->next = stream->top_if;
1394 stream->top_if = token;
1395 if (false_nesting || true != 1)
1396 false_nesting++;
1397 return 0;
1400 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1402 struct token *next = token->next;
1403 int arg;
1404 if (token_type(next) == TOKEN_IDENT) {
1405 arg = token_defined(next);
1406 } else {
1407 dirty_stream(stream);
1408 if (!false_nesting)
1409 sparse_error(token->pos, "expected preprocessor identifier");
1410 arg = -1;
1412 return preprocessor_if(stream, token, arg);
1415 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1417 struct token *next = token->next;
1418 int arg;
1419 if (token_type(next) == TOKEN_IDENT) {
1420 if (!stream->dirty && !stream->ifndef) {
1421 if (!stream->protect) {
1422 stream->ifndef = token;
1423 stream->protect = next->ident;
1424 } else if (stream->protect == next->ident) {
1425 stream->ifndef = token;
1426 stream->dirty = 1;
1429 arg = !token_defined(next);
1430 } else {
1431 dirty_stream(stream);
1432 if (!false_nesting)
1433 sparse_error(token->pos, "expected preprocessor identifier");
1434 arg = -1;
1437 return preprocessor_if(stream, token, arg);
1440 static const char *show_token_sequence(struct token *token);
1443 * Expression handling for #if and #elif; it differs from normal expansion
1444 * due to special treatment of "defined".
1446 static int expression_value(struct token **where)
1448 struct expression *expr;
1449 struct token *p;
1450 struct token **list = where, **beginning = NULL;
1451 long long value;
1452 int state = 0;
1454 while (!eof_token(p = scan_next(list))) {
1455 switch (state) {
1456 case 0:
1457 if (token_type(p) != TOKEN_IDENT)
1458 break;
1459 if (p->ident == &defined_ident) {
1460 state = 1;
1461 beginning = list;
1462 break;
1464 if (!expand_one_symbol(list))
1465 continue;
1466 if (token_type(p) != TOKEN_IDENT)
1467 break;
1468 token_type(p) = TOKEN_ZERO_IDENT;
1469 break;
1470 case 1:
1471 if (match_op(p, '(')) {
1472 state = 2;
1473 } else {
1474 state = 0;
1475 replace_with_defined(p);
1476 *beginning = p;
1478 break;
1479 case 2:
1480 if (token_type(p) == TOKEN_IDENT)
1481 state = 3;
1482 else
1483 state = 0;
1484 replace_with_defined(p);
1485 *beginning = p;
1486 break;
1487 case 3:
1488 state = 0;
1489 if (!match_op(p, ')'))
1490 sparse_error(p->pos, "missing ')' after \"defined\"");
1491 *list = p->next;
1492 continue;
1494 list = &p->next;
1497 p = constant_expression(*where, &expr);
1498 if (!eof_token(p))
1499 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p));
1500 value = get_expression_value(expr);
1501 return value != 0;
1504 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1506 int value = 0;
1507 if (!false_nesting)
1508 value = expression_value(&token->next);
1510 dirty_stream(stream);
1511 return preprocessor_if(stream, token, value);
1514 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1516 struct token *top_if = stream->top_if;
1517 end_group(stream);
1519 if (!top_if) {
1520 nesting_error(stream);
1521 sparse_error(token->pos, "unmatched #elif within stream");
1522 return 1;
1525 if (token_type(top_if) == TOKEN_ELSE) {
1526 nesting_error(stream);
1527 sparse_error(token->pos, "#elif after #else");
1528 if (!false_nesting)
1529 false_nesting = 1;
1530 return 1;
1533 dirty_stream(stream);
1534 if (token_type(top_if) != TOKEN_IF)
1535 return 1;
1536 if (false_nesting) {
1537 false_nesting = 0;
1538 if (!expression_value(&token->next))
1539 false_nesting = 1;
1540 } else {
1541 false_nesting = 1;
1542 token_type(top_if) = TOKEN_SKIP_GROUPS;
1544 return 1;
1547 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1549 struct token *top_if = stream->top_if;
1550 end_group(stream);
1552 if (!top_if) {
1553 nesting_error(stream);
1554 sparse_error(token->pos, "unmatched #else within stream");
1555 return 1;
1558 if (token_type(top_if) == TOKEN_ELSE) {
1559 nesting_error(stream);
1560 sparse_error(token->pos, "#else after #else");
1562 if (false_nesting) {
1563 if (token_type(top_if) == TOKEN_IF)
1564 false_nesting = 0;
1565 } else {
1566 false_nesting = 1;
1568 token_type(top_if) = TOKEN_ELSE;
1569 return 1;
1572 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1574 struct token *top_if = stream->top_if;
1575 end_group(stream);
1576 if (!top_if) {
1577 nesting_error(stream);
1578 sparse_error(token->pos, "unmatched #endif in stream");
1579 return 1;
1581 if (false_nesting)
1582 false_nesting--;
1583 stream->top_if = top_if->next;
1584 __free_token(top_if);
1585 return 1;
1588 static const char *show_token_sequence(struct token *token)
1590 static char buffer[1024];
1591 char *ptr = buffer;
1592 int whitespace = 0;
1594 if (!token)
1595 return "<none>";
1596 while (!eof_token(token)) {
1597 const char *val = show_token(token);
1598 int len = strlen(val);
1600 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
1601 sparse_error(token->pos, "too long token expansion");
1602 break;
1605 if (whitespace)
1606 *ptr++ = ' ';
1607 memcpy(ptr, val, len);
1608 ptr += len;
1609 token = token->next;
1610 whitespace = token->pos.whitespace;
1612 *ptr = 0;
1613 return buffer;
1616 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1618 warning(token->pos, "%s", show_token_sequence(token->next));
1619 return 1;
1622 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1624 sparse_error(token->pos, "%s", show_token_sequence(token->next));
1625 return 1;
1628 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1631 * Do we have any non-system includes?
1632 * Clear them out if so..
1634 *sys_includepath = NULL;
1635 return 1;
1638 static inline void update_inc_ptrs(const char ***where)
1641 if (*where <= dirafter_includepath) {
1642 dirafter_includepath++;
1643 /* If this was the entry that we prepend, don't
1644 * rise the lower entries, even if they are at
1645 * the same level. */
1646 if (where == &dirafter_includepath)
1647 return;
1649 if (*where <= sys_includepath) {
1650 sys_includepath++;
1651 if (where == &sys_includepath)
1652 return;
1654 if (*where <= isys_includepath) {
1655 isys_includepath++;
1656 if (where == &isys_includepath)
1657 return;
1660 /* angle_includepath is actually never updated, since we
1661 * don't suppport -iquote rught now. May change some day. */
1662 if (*where <= angle_includepath) {
1663 angle_includepath++;
1664 if (where == &angle_includepath)
1665 return;
1669 /* Add a path before 'where' and update the pointers associated with the
1670 * includepath array */
1671 static void add_path_entry(struct token *token, const char *path,
1672 const char ***where)
1674 const char **dst;
1675 const char *next;
1677 /* Need one free entry.. */
1678 if (includepath[INCLUDEPATHS-2])
1679 error_die(token->pos, "too many include path entries");
1681 /* check that this is not a duplicate */
1682 dst = includepath;
1683 while (*dst) {
1684 if (strcmp(*dst, path) == 0)
1685 return;
1686 dst++;
1688 next = path;
1689 dst = *where;
1691 update_inc_ptrs(where);
1694 * Move them all up starting at dst,
1695 * insert the new entry..
1697 do {
1698 const char *tmp = *dst;
1699 *dst = next;
1700 next = tmp;
1701 dst++;
1702 } while (next);
1705 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1707 for (;;) {
1708 token = token->next;
1709 if (eof_token(token))
1710 return 1;
1711 if (token_type(token) != TOKEN_STRING) {
1712 warning(token->pos, "expected path string");
1713 return 1;
1715 add_path_entry(token, token->string->data, &isys_includepath);
1719 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1721 for (;;) {
1722 token = token->next;
1723 if (eof_token(token))
1724 return 1;
1725 if (token_type(token) != TOKEN_STRING) {
1726 sparse_error(token->pos, "expected path string");
1727 return 1;
1729 add_path_entry(token, token->string->data, &sys_includepath);
1733 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1735 for (;;) {
1736 token = token->next;
1737 if (eof_token(token))
1738 return 1;
1739 if (token_type(token) != TOKEN_STRING) {
1740 sparse_error(token->pos, "expected path string");
1741 return 1;
1743 add_path_entry(token, token->string->data, &dirafter_includepath);
1747 /* Add to end on includepath list - no pointer updates */
1748 static void add_dirafter_entry(struct token *token, const char *path)
1750 const char **dst = includepath;
1752 /* Need one free entry.. */
1753 if (includepath[INCLUDEPATHS-2])
1754 error_die(token->pos, "too many include path entries");
1756 /* Add to the end */
1757 while (*dst)
1758 dst++;
1759 *dst = path;
1760 dst++;
1761 *dst = NULL;
1764 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1766 for (;;) {
1767 token = token->next;
1768 if (eof_token(token))
1769 return 1;
1770 if (token_type(token) != TOKEN_STRING) {
1771 sparse_error(token->pos, "expected path string");
1772 return 1;
1774 add_dirafter_entry(token, token->string->data);
1778 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1781 * -I-
1782 * From info gcc:
1783 * Split the include path. Any directories specified with `-I'
1784 * options before `-I-' are searched only for headers requested with
1785 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1786 * If additional directories are specified with `-I' options after
1787 * the `-I-', those directories are searched for all `#include'
1788 * directives.
1789 * In addition, `-I-' inhibits the use of the directory of the current
1790 * file directory as the first search directory for `#include "FILE"'.
1792 quote_includepath = includepath+1;
1793 angle_includepath = sys_includepath;
1794 return 1;
1798 * We replace "#pragma xxx" with "__pragma__" in the token
1799 * stream. Just as an example.
1801 * We'll just #define that away for now, but the theory here
1802 * is that we can use this to insert arbitrary token sequences
1803 * to turn the pragmas into internal front-end sequences for
1804 * when we actually start caring about them.
1806 * So eventually this will turn into some kind of extended
1807 * __attribute__() like thing, except called __pragma__(xxx).
1809 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1811 struct token *next = *line;
1813 token->ident = &pragma_ident;
1814 token->pos.newline = 1;
1815 token->pos.whitespace = 1;
1816 token->pos.pos = 1;
1817 *line = token;
1818 token->next = next;
1819 return 0;
1823 * We ignore #line for now.
1825 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1827 return 1;
1830 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1832 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
1833 return 1;
1837 static void init_preprocessor(void)
1839 int i;
1840 int stream = init_stream("preprocessor", -1, includepath);
1841 static struct {
1842 const char *name;
1843 int (*handler)(struct stream *, struct token **, struct token *);
1844 } normal[] = {
1845 { "define", handle_define },
1846 { "weak_define", handle_weak_define },
1847 { "strong_define", handle_strong_define },
1848 { "undef", handle_undef },
1849 { "strong_undef", handle_strong_undef },
1850 { "warning", handle_warning },
1851 { "error", handle_error },
1852 { "include", handle_include },
1853 { "include_next", handle_include_next },
1854 { "pragma", handle_pragma },
1855 { "line", handle_line },
1857 // our internal preprocessor tokens
1858 { "nostdinc", handle_nostdinc },
1859 { "add_include", handle_add_include },
1860 { "add_isystem", handle_add_isystem },
1861 { "add_system", handle_add_system },
1862 { "add_dirafter", handle_add_dirafter },
1863 { "split_include", handle_split_include },
1864 { "argv_include", handle_argv_include },
1865 }, special[] = {
1866 { "ifdef", handle_ifdef },
1867 { "ifndef", handle_ifndef },
1868 { "else", handle_else },
1869 { "endif", handle_endif },
1870 { "if", handle_if },
1871 { "elif", handle_elif },
1874 for (i = 0; i < ARRAY_SIZE(normal); i++) {
1875 struct symbol *sym;
1876 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1877 sym->handler = normal[i].handler;
1878 sym->normal = 1;
1880 for (i = 0; i < ARRAY_SIZE(special); i++) {
1881 struct symbol *sym;
1882 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1883 sym->handler = special[i].handler;
1884 sym->normal = 0;
1889 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1891 int (*handler)(struct stream *, struct token **, struct token *);
1892 struct token *token = start->next;
1893 int is_normal = 1;
1895 if (eof_token(token))
1896 return;
1898 if (token_type(token) == TOKEN_IDENT) {
1899 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1900 if (sym) {
1901 handler = sym->handler;
1902 is_normal = sym->normal;
1903 } else {
1904 handler = handle_nondirective;
1906 } else if (token_type(token) == TOKEN_NUMBER) {
1907 handler = handle_line;
1908 } else {
1909 handler = handle_nondirective;
1912 if (is_normal) {
1913 dirty_stream(stream);
1914 if (false_nesting)
1915 goto out;
1917 if (!handler(stream, line, token)) /* all set */
1918 return;
1920 out:
1921 free_preprocessor_line(token);
1924 static void preprocessor_line(struct stream *stream, struct token **line)
1926 struct token *start = *line, *next;
1927 struct token **tp = &start->next;
1929 for (;;) {
1930 next = *tp;
1931 if (next->pos.newline)
1932 break;
1933 tp = &next->next;
1935 *line = next;
1936 *tp = &eof_token_entry;
1937 handle_preprocessor_line(stream, line, start);
1940 static void do_preprocess(struct token **list)
1942 struct token *next;
1944 while (!eof_token(next = scan_next(list))) {
1945 struct stream *stream = input_streams + next->pos.stream;
1947 if (next->pos.newline && match_op(next, '#')) {
1948 if (!next->pos.noexpand) {
1949 preprocessor_line(stream, list);
1950 __free_token(next); /* Free the '#' token */
1951 continue;
1955 switch (token_type(next)) {
1956 case TOKEN_STREAMEND:
1957 if (stream->top_if) {
1958 nesting_error(stream);
1959 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1960 stream->top_if = NULL;
1961 false_nesting = 0;
1963 if (!stream->dirty)
1964 stream->constant = CONSTANT_FILE_YES;
1965 *list = next->next;
1966 continue;
1967 case TOKEN_STREAMBEGIN:
1968 *list = next->next;
1969 continue;
1971 default:
1972 dirty_stream(stream);
1973 if (false_nesting) {
1974 *list = next->next;
1975 __free_token(next);
1976 continue;
1979 if (token_type(next) != TOKEN_IDENT ||
1980 expand_one_symbol(list))
1981 list = &next->next;
1986 struct token * preprocess(struct token *token)
1988 preprocessing = 1;
1989 init_preprocessor();
1990 do_preprocess(&token);
1992 // Drop all expressions from preprocessing, they're not used any more.
1993 // This is not true when we have multiple files, though ;/
1994 // clear_expression_alloc();
1995 preprocessing = 0;
1997 return token;