move OP_MUL simplification in a separate function
[smatch.git] / pre-process.c
blob7c57ba1cddfed6e85975d4f9b109024794174e84
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 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stddef.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <limits.h>
37 #include <time.h>
39 #include "lib.h"
40 #include "allocate.h"
41 #include "parse.h"
42 #include "token.h"
43 #include "symbol.h"
44 #include "expression.h"
45 #include "scope.h"
47 static int false_nesting = 0;
48 static int counter_macro = 0; // __COUNTER__ expansion
50 #define INCLUDEPATHS 300
51 const char *includepath[INCLUDEPATHS+1] = {
52 "",
53 "/usr/include",
54 "/usr/local/include",
55 NULL
58 static const char **quote_includepath = includepath;
59 static const char **angle_includepath = includepath + 1;
60 static const char **isys_includepath = includepath + 1;
61 static const char **sys_includepath = includepath + 1;
62 static const char **dirafter_includepath = includepath + 3;
64 #define dirty_stream(stream) \
65 do { \
66 if (!stream->dirty) { \
67 stream->dirty = 1; \
68 if (!stream->ifndef) \
69 stream->protect = NULL; \
70 } \
71 } while(0)
73 #define end_group(stream) \
74 do { \
75 if (stream->ifndef == stream->top_if) { \
76 stream->ifndef = NULL; \
77 if (!stream->dirty) \
78 stream->protect = NULL; \
79 else if (stream->protect) \
80 stream->dirty = 0; \
81 } \
82 } while(0)
84 #define nesting_error(stream) \
85 do { \
86 stream->dirty = 1; \
87 stream->ifndef = NULL; \
88 stream->protect = NULL; \
89 } while(0)
91 static struct token *alloc_token(struct position *pos)
93 struct token *token = __alloc_token(0);
95 token->pos.stream = pos->stream;
96 token->pos.line = pos->line;
97 token->pos.pos = pos->pos;
98 token->pos.whitespace = 1;
99 return token;
102 /* Expand symbol 'sym' at '*list' */
103 static int expand(struct token **, struct symbol *);
105 static void replace_with_string(struct token *token, const char *str)
107 int size = strlen(str) + 1;
108 struct string *s = __alloc_string(size);
110 s->length = size;
111 memcpy(s->data, str, size);
112 token_type(token) = TOKEN_STRING;
113 token->string = s;
116 static void replace_with_integer(struct token *token, unsigned int val)
118 char *buf = __alloc_bytes(11);
119 sprintf(buf, "%u", val);
120 token_type(token) = TOKEN_NUMBER;
121 token->number = buf;
124 static struct symbol *lookup_macro(struct ident *ident)
126 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
127 if (sym && sym->namespace != NS_MACRO)
128 sym = NULL;
129 return sym;
132 static int token_defined(struct token *token)
134 if (token_type(token) == TOKEN_IDENT) {
135 struct symbol *sym = lookup_macro(token->ident);
136 if (sym) {
137 sym->used_in = file_scope;
138 return 1;
140 return 0;
143 sparse_error(token->pos, "expected preprocessor identifier");
144 return 0;
147 static void replace_with_defined(struct token *token)
149 static const char *string[] = { "0", "1" };
150 int defined = token_defined(token);
152 token_type(token) = TOKEN_NUMBER;
153 token->number = string[defined];
156 static int expand_one_symbol(struct token **list)
158 struct token *token = *list;
159 struct symbol *sym;
160 static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
161 static time_t t = 0;
163 if (token->pos.noexpand)
164 return 1;
166 sym = lookup_macro(token->ident);
167 if (sym) {
168 sym->used_in = file_scope;
169 return expand(list, sym);
171 if (token->ident == &__LINE___ident) {
172 replace_with_integer(token, token->pos.line);
173 } else if (token->ident == &__FILE___ident) {
174 replace_with_string(token, stream_name(token->pos.stream));
175 } else if (token->ident == &__DATE___ident) {
176 if (!t)
177 time(&t);
178 strftime(buffer, 12, "%b %e %Y", localtime(&t));
179 replace_with_string(token, buffer);
180 } else if (token->ident == &__TIME___ident) {
181 if (!t)
182 time(&t);
183 strftime(buffer, 9, "%T", localtime(&t));
184 replace_with_string(token, buffer);
185 } else if (token->ident == &__COUNTER___ident) {
186 replace_with_integer(token, counter_macro++);
188 return 1;
191 static inline struct token *scan_next(struct token **where)
193 struct token *token = *where;
194 if (token_type(token) != TOKEN_UNTAINT)
195 return token;
196 do {
197 token->ident->tainted = 0;
198 token = token->next;
199 } while (token_type(token) == TOKEN_UNTAINT);
200 *where = token;
201 return token;
204 static void expand_list(struct token **list)
206 struct token *next;
207 while (!eof_token(next = scan_next(list))) {
208 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
209 list = &next->next;
213 static void preprocessor_line(struct stream *stream, struct token **line);
215 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos, int count)
217 struct stream *stream = input_streams + prev->pos.stream;
218 struct token **p = &prev->next;
219 struct token *next;
220 int nesting = 0;
222 while (!eof_token(next = scan_next(p))) {
223 if (next->pos.newline && match_op(next, '#')) {
224 if (!next->pos.noexpand) {
225 sparse_error(next->pos,
226 "directive in argument list");
227 preprocessor_line(stream, p);
228 __free_token(next); /* Free the '#' token */
229 continue;
232 switch (token_type(next)) {
233 case TOKEN_STREAMEND:
234 case TOKEN_STREAMBEGIN:
235 *p = &eof_token_entry;
236 return next;
237 case TOKEN_STRING:
238 case TOKEN_WIDE_STRING:
239 if (count > 1)
240 next->string->immutable = 1;
241 break;
243 if (false_nesting) {
244 *p = next->next;
245 __free_token(next);
246 continue;
248 if (match_op(next, '(')) {
249 nesting++;
250 } else if (match_op(next, ')')) {
251 if (!nesting--)
252 break;
253 } else if (match_op(next, ',') && !nesting && !vararg) {
254 break;
256 next->pos.stream = pos->stream;
257 next->pos.line = pos->line;
258 next->pos.pos = pos->pos;
259 p = &next->next;
261 *p = &eof_token_entry;
262 return next;
266 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
269 struct arg {
270 struct token *arg;
271 struct token *expanded;
272 struct token *str;
273 int n_normal;
274 int n_quoted;
275 int n_str;
278 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
280 int wanted = arglist->count.normal;
281 struct token *next = NULL;
282 int count = 0;
284 arglist = arglist->next; /* skip counter */
286 if (!wanted) {
287 next = collect_arg(start, 0, &what->pos, 0);
288 if (eof_token(next))
289 goto Eclosing;
290 if (!eof_token(start->next) || !match_op(next, ')')) {
291 count++;
292 goto Emany;
294 } else {
295 for (count = 0; count < wanted; count++) {
296 struct argcount *p = &arglist->next->count;
297 next = collect_arg(start, p->vararg, &what->pos, p->normal);
298 arglist = arglist->next->next;
299 if (eof_token(next))
300 goto Eclosing;
301 args[count].arg = start->next;
302 args[count].n_normal = p->normal;
303 args[count].n_quoted = p->quoted;
304 args[count].n_str = p->str;
305 if (match_op(next, ')')) {
306 count++;
307 break;
309 start = next;
311 if (count == wanted && !match_op(next, ')'))
312 goto Emany;
313 if (count == wanted - 1) {
314 struct argcount *p = &arglist->next->count;
315 if (!p->vararg)
316 goto Efew;
317 args[count].arg = NULL;
318 args[count].n_normal = p->normal;
319 args[count].n_quoted = p->quoted;
320 args[count].n_str = p->str;
322 if (count < wanted - 1)
323 goto Efew;
325 what->next = next->next;
326 return 1;
328 Efew:
329 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
330 show_token(what), wanted, count);
331 goto out;
332 Emany:
333 while (match_op(next, ',')) {
334 next = collect_arg(next, 0, &what->pos, 0);
335 count++;
337 if (eof_token(next))
338 goto Eclosing;
339 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
340 show_token(what), count, wanted);
341 goto out;
342 Eclosing:
343 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
344 show_token(what));
345 out:
346 what->next = next->next;
347 return 0;
350 static struct token *dup_list(struct token *list)
352 struct token *res = NULL;
353 struct token **p = &res;
355 while (!eof_token(list)) {
356 struct token *newtok = __alloc_token(0);
357 *newtok = *list;
358 *p = newtok;
359 p = &newtok->next;
360 list = list->next;
362 return res;
365 static const char *show_token_sequence(struct token *token, int quote)
367 static char buffer[MAX_STRING];
368 char *ptr = buffer;
369 int whitespace = 0;
371 if (!token && !quote)
372 return "<none>";
373 while (!eof_token(token)) {
374 const char *val = quote ? quote_token(token) : show_token(token);
375 int len = strlen(val);
377 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
378 sparse_error(token->pos, "too long token expansion");
379 break;
382 if (whitespace)
383 *ptr++ = ' ';
384 memcpy(ptr, val, len);
385 ptr += len;
386 token = token->next;
387 whitespace = token->pos.whitespace;
389 *ptr = 0;
390 return buffer;
393 static struct token *stringify(struct token *arg)
395 const char *s = show_token_sequence(arg, 1);
396 int size = strlen(s)+1;
397 struct token *token = __alloc_token(0);
398 struct string *string = __alloc_string(size);
400 memcpy(string->data, s, size);
401 string->length = size;
402 token->pos = arg->pos;
403 token_type(token) = TOKEN_STRING;
404 token->string = string;
405 token->next = &eof_token_entry;
406 return token;
409 static void expand_arguments(int count, struct arg *args)
411 int i;
412 for (i = 0; i < count; i++) {
413 struct token *arg = args[i].arg;
414 if (!arg)
415 arg = &eof_token_entry;
416 if (args[i].n_str)
417 args[i].str = stringify(arg);
418 if (args[i].n_normal) {
419 if (!args[i].n_quoted) {
420 args[i].expanded = arg;
421 args[i].arg = NULL;
422 } else if (eof_token(arg)) {
423 args[i].expanded = arg;
424 } else {
425 args[i].expanded = dup_list(arg);
427 expand_list(&args[i].expanded);
433 * Possibly valid combinations:
434 * - ident + ident -> ident
435 * - ident + number -> ident unless number contains '.', '+' or '-'.
436 * - 'L' + char constant -> wide char constant
437 * - 'L' + string literal -> wide string literal
438 * - number + number -> number
439 * - number + ident -> number
440 * - number + '.' -> number
441 * - number + '+' or '-' -> number, if number used to end on [eEpP].
442 * - '.' + number -> number, if number used to start with a digit.
443 * - special + special -> either special or an error.
445 static enum token_type combine(struct token *left, struct token *right, char *p)
447 int len;
448 enum token_type t1 = token_type(left), t2 = token_type(right);
450 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
451 return TOKEN_ERROR;
453 if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
454 if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
455 return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
456 if (t2 == TOKEN_STRING)
457 return TOKEN_WIDE_STRING;
460 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
461 return TOKEN_ERROR;
463 strcpy(p, show_token(left));
464 strcat(p, show_token(right));
465 len = strlen(p);
467 if (len >= 256)
468 return TOKEN_ERROR;
470 if (t1 == TOKEN_IDENT) {
471 if (t2 == TOKEN_SPECIAL)
472 return TOKEN_ERROR;
473 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
474 return TOKEN_ERROR;
475 return TOKEN_IDENT;
478 if (t1 == TOKEN_NUMBER) {
479 if (t2 == TOKEN_SPECIAL) {
480 switch (right->special) {
481 case '.':
482 break;
483 case '+': case '-':
484 if (strchr("eEpP", p[len - 2]))
485 break;
486 default:
487 return TOKEN_ERROR;
490 return TOKEN_NUMBER;
493 if (p[0] == '.' && isdigit((unsigned char)p[1]))
494 return TOKEN_NUMBER;
496 return TOKEN_SPECIAL;
499 static int merge(struct token *left, struct token *right)
501 static char buffer[512];
502 enum token_type res = combine(left, right, buffer);
503 int n;
505 switch (res) {
506 case TOKEN_IDENT:
507 left->ident = built_in_ident(buffer);
508 left->pos.noexpand = 0;
509 return 1;
511 case TOKEN_NUMBER: {
512 char *number = __alloc_bytes(strlen(buffer) + 1);
513 memcpy(number, buffer, strlen(buffer) + 1);
514 token_type(left) = TOKEN_NUMBER; /* could be . + num */
515 left->number = number;
516 return 1;
519 case TOKEN_SPECIAL:
520 if (buffer[2] && buffer[3])
521 break;
522 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
523 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
524 left->special = n;
525 return 1;
528 break;
530 case TOKEN_WIDE_CHAR:
531 case TOKEN_WIDE_STRING:
532 token_type(left) = res;
533 left->pos.noexpand = 0;
534 left->string = right->string;
535 return 1;
537 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
538 token_type(left) = res;
539 left->pos.noexpand = 0;
540 memcpy(left->embedded, right->embedded, 4);
541 return 1;
543 default:
546 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
547 return 0;
550 static struct token *dup_token(struct token *token, struct position *streampos)
552 struct token *alloc = alloc_token(streampos);
553 token_type(alloc) = token_type(token);
554 alloc->pos.newline = token->pos.newline;
555 alloc->pos.whitespace = token->pos.whitespace;
556 alloc->number = token->number;
557 alloc->pos.noexpand = token->pos.noexpand;
558 return alloc;
561 static struct token **copy(struct token **where, struct token *list, int *count)
563 int need_copy = --*count;
564 while (!eof_token(list)) {
565 struct token *token;
566 if (need_copy)
567 token = dup_token(list, &list->pos);
568 else
569 token = list;
570 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
571 token->pos.noexpand = 1;
572 *where = token;
573 where = &token->next;
574 list = list->next;
576 *where = &eof_token_entry;
577 return where;
580 static int handle_kludge(struct token **p, struct arg *args)
582 struct token *t = (*p)->next->next;
583 while (1) {
584 struct arg *v = &args[t->argnum];
585 if (token_type(t->next) != TOKEN_CONCAT) {
586 if (v->arg) {
587 /* ignore the first ## */
588 *p = (*p)->next;
589 return 0;
591 /* skip the entire thing */
592 *p = t;
593 return 1;
595 if (v->arg && !eof_token(v->arg))
596 return 0; /* no magic */
597 t = t->next->next;
601 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
603 struct position *base_pos = &(*list)->pos;
604 int *count;
605 enum {Normal, Placeholder, Concat} state = Normal;
607 for (; !eof_token(body); body = body->next) {
608 struct token *added, *arg;
609 struct token **tail;
610 struct token *t;
612 switch (token_type(body)) {
613 case TOKEN_GNU_KLUDGE:
615 * GNU kludge: if we had <comma>##<vararg>, behaviour
616 * depends on whether we had enough arguments to have
617 * a vararg. If we did, ## is just ignored. Otherwise
618 * both , and ## are ignored. Worse, there can be
619 * an arbitrary number of ##<arg> in between; if all of
620 * those are empty, we act as if they hadn't been there,
621 * otherwise we act as if the kludge didn't exist.
623 t = body;
624 if (handle_kludge(&body, args)) {
625 if (state == Concat)
626 state = Normal;
627 else
628 state = Placeholder;
629 continue;
631 added = dup_token(t, base_pos);
632 token_type(added) = TOKEN_SPECIAL;
633 tail = &added->next;
634 break;
636 case TOKEN_STR_ARGUMENT:
637 arg = args[body->argnum].str;
638 count = &args[body->argnum].n_str;
639 goto copy_arg;
641 case TOKEN_QUOTED_ARGUMENT:
642 arg = args[body->argnum].arg;
643 count = &args[body->argnum].n_quoted;
644 if (!arg || eof_token(arg)) {
645 if (state == Concat)
646 state = Normal;
647 else
648 state = Placeholder;
649 continue;
651 goto copy_arg;
653 case TOKEN_MACRO_ARGUMENT:
654 arg = args[body->argnum].expanded;
655 count = &args[body->argnum].n_normal;
656 if (eof_token(arg)) {
657 state = Normal;
658 continue;
660 copy_arg:
661 tail = copy(&added, arg, count);
662 added->pos.newline = body->pos.newline;
663 added->pos.whitespace = body->pos.whitespace;
664 break;
666 case TOKEN_CONCAT:
667 if (state == Placeholder)
668 state = Normal;
669 else
670 state = Concat;
671 continue;
673 case TOKEN_IDENT:
674 added = dup_token(body, base_pos);
675 if (added->ident->tainted)
676 added->pos.noexpand = 1;
677 tail = &added->next;
678 break;
680 default:
681 added = dup_token(body, base_pos);
682 tail = &added->next;
683 break;
687 * if we got to doing real concatenation, we already have
688 * added something into the list, so containing_token() is OK.
690 if (state == Concat && merge(containing_token(list), added)) {
691 *list = added->next;
692 if (tail != &added->next)
693 list = tail;
694 } else {
695 *list = added;
696 list = tail;
698 state = Normal;
700 *list = &eof_token_entry;
701 return list;
704 static int expand(struct token **list, struct symbol *sym)
706 struct token *last;
707 struct token *token = *list;
708 struct ident *expanding = token->ident;
709 struct token **tail;
710 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
711 struct arg args[nargs];
713 if (expanding->tainted) {
714 token->pos.noexpand = 1;
715 return 1;
718 if (sym->arglist) {
719 if (!match_op(scan_next(&token->next), '('))
720 return 1;
721 if (!collect_arguments(token->next, sym->arglist, args, token))
722 return 1;
723 expand_arguments(nargs, args);
726 expanding->tainted = 1;
728 last = token->next;
729 tail = substitute(list, sym->expansion, args);
731 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
732 * We still can lose the newline flag if the sucker expands to nothing,
733 * but the price of dealing with that is probably too high (we'd need
734 * to collect the flags during scan_next())
736 (*list)->pos.newline = token->pos.newline;
737 (*list)->pos.whitespace = token->pos.whitespace;
738 *tail = last;
740 return 0;
743 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
745 static char buffer[256];
746 char *ptr = buffer;
748 while (!eof_token(token) && !match_op(token, endop)) {
749 int len;
750 const char *val = token->string->data;
751 if (token_type(token) != TOKEN_STRING)
752 val = show_token(token);
753 len = strlen(val);
754 memcpy(ptr, val, len);
755 ptr += len;
756 token = token->next;
758 *ptr = 0;
759 if (endop && !match_op(token, endop))
760 sparse_error(start->pos, "expected '>' at end of filename");
761 return buffer;
764 static int already_tokenized(const char *path)
766 int stream, next;
768 for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
769 struct stream *s = input_streams + stream;
771 next = s->next_stream;
772 if (s->once) {
773 if (strcmp(path, s->name))
774 continue;
775 return 1;
777 if (s->constant != CONSTANT_FILE_YES)
778 continue;
779 if (strcmp(path, s->name))
780 continue;
781 if (s->protect && !lookup_macro(s->protect))
782 continue;
783 return 1;
785 return 0;
788 /* Handle include of header files.
789 * The relevant options are made compatible with gcc. The only options that
790 * are not supported is -withprefix and friends.
792 * Three set of include paths are known:
793 * quote_includepath: Path to search when using #include "file.h"
794 * angle_includepath: Paths to search when using #include <file.h>
795 * isys_includepath: Paths specified with -isystem, come before the
796 * built-in system include paths. Gcc would suppress
797 * warnings from system headers. Here we separate
798 * them from the angle_ ones to keep search ordering.
800 * sys_includepath: Built-in include paths.
801 * dirafter_includepath Paths added with -dirafter.
803 * The above is implemented as one array with pointers
804 * +--------------+
805 * quote_includepath ---> | |
806 * +--------------+
807 * | |
808 * +--------------+
809 * angle_includepath ---> | |
810 * +--------------+
811 * isys_includepath ---> | |
812 * +--------------+
813 * sys_includepath ---> | |
814 * +--------------+
815 * dirafter_includepath -> | |
816 * +--------------+
818 * -I dir insert dir just before isys_includepath and move the rest
819 * -I- makes all dirs specified with -I before to quote dirs only and
820 * angle_includepath is set equal to isys_includepath.
821 * -nostdinc removes all sys dirs by storing NULL in entry pointed
822 * to by * sys_includepath. Note that this will reset all dirs built-in
823 * and added before -nostdinc by -isystem and -idirafter.
824 * -isystem dir adds dir where isys_includepath points adding this dir as
825 * first systemdir
826 * -idirafter dir adds dir to the end of the list
829 static void set_stream_include_path(struct stream *stream)
831 const char *path = stream->path;
832 if (!path) {
833 const char *p = strrchr(stream->name, '/');
834 path = "";
835 if (p) {
836 int len = p - stream->name + 1;
837 char *m = malloc(len+1);
838 /* This includes the final "/" */
839 memcpy(m, stream->name, len);
840 m[len] = 0;
841 path = m;
843 stream->path = path;
845 includepath[0] = path;
848 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
850 int fd;
851 int plen = strlen(path);
852 static char fullname[PATH_MAX];
854 memcpy(fullname, path, plen);
855 if (plen && path[plen-1] != '/') {
856 fullname[plen] = '/';
857 plen++;
859 memcpy(fullname+plen, filename, flen);
860 if (already_tokenized(fullname))
861 return 1;
862 fd = open(fullname, O_RDONLY);
863 if (fd >= 0) {
864 char * streamname = __alloc_bytes(plen + flen);
865 memcpy(streamname, fullname, plen + flen);
866 *where = tokenize(streamname, fd, *where, next_path);
867 close(fd);
868 return 1;
870 return 0;
873 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
875 const char *path;
877 while ((path = *pptr++) != NULL) {
878 if (!try_include(path, filename, flen, list, pptr))
879 continue;
880 return 1;
882 return 0;
885 static int free_preprocessor_line(struct token *token)
887 while (token_type(token) != TOKEN_EOF) {
888 struct token *free = token;
889 token = token->next;
890 __free_token(free);
892 return 1;
895 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how)
897 const char *filename;
898 struct token *next;
899 const char **path;
900 int expect;
901 int flen;
903 next = token->next;
904 expect = '>';
905 if (!match_op(next, '<')) {
906 expand_list(&token->next);
907 expect = 0;
908 next = token;
909 if (match_op(token->next, '<')) {
910 next = token->next;
911 expect = '>';
915 token = next->next;
916 filename = token_name_sequence(token, expect, token);
917 flen = strlen(filename) + 1;
919 /* Absolute path? */
920 if (filename[0] == '/') {
921 if (try_include("", filename, flen, list, includepath))
922 return 0;
923 goto out;
926 switch (how) {
927 case 1:
928 path = stream->next_path;
929 break;
930 case 2:
931 includepath[0] = "";
932 path = includepath;
933 break;
934 default:
935 /* Dir of input file is first dir to search for quoted includes */
936 set_stream_include_path(stream);
937 path = expect ? angle_includepath : quote_includepath;
938 break;
940 /* Check the standard include paths.. */
941 if (do_include_path(path, list, token, filename, flen))
942 return 0;
943 out:
944 error_die(token->pos, "unable to open '%s'", filename);
947 static int handle_include(struct stream *stream, struct token **list, struct token *token)
949 return handle_include_path(stream, list, token, 0);
952 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
954 return handle_include_path(stream, list, token, 1);
957 static int handle_argv_include(struct stream *stream, struct token **list, struct token *token)
959 return handle_include_path(stream, list, token, 2);
962 static int token_different(struct token *t1, struct token *t2)
964 int different;
966 if (token_type(t1) != token_type(t2))
967 return 1;
969 switch (token_type(t1)) {
970 case TOKEN_IDENT:
971 different = t1->ident != t2->ident;
972 break;
973 case TOKEN_ARG_COUNT:
974 case TOKEN_UNTAINT:
975 case TOKEN_CONCAT:
976 case TOKEN_GNU_KLUDGE:
977 different = 0;
978 break;
979 case TOKEN_NUMBER:
980 different = strcmp(t1->number, t2->number);
981 break;
982 case TOKEN_SPECIAL:
983 different = t1->special != t2->special;
984 break;
985 case TOKEN_MACRO_ARGUMENT:
986 case TOKEN_QUOTED_ARGUMENT:
987 case TOKEN_STR_ARGUMENT:
988 different = t1->argnum != t2->argnum;
989 break;
990 case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
991 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
992 different = memcmp(t1->embedded, t2->embedded, 4);
993 break;
994 case TOKEN_CHAR:
995 case TOKEN_WIDE_CHAR:
996 case TOKEN_STRING:
997 case TOKEN_WIDE_STRING: {
998 struct string *s1, *s2;
1000 s1 = t1->string;
1001 s2 = t2->string;
1002 different = 1;
1003 if (s1->length != s2->length)
1004 break;
1005 different = memcmp(s1->data, s2->data, s1->length);
1006 break;
1008 default:
1009 different = 1;
1010 break;
1012 return different;
1015 static int token_list_different(struct token *list1, struct token *list2)
1017 for (;;) {
1018 if (list1 == list2)
1019 return 0;
1020 if (!list1 || !list2)
1021 return 1;
1022 if (token_different(list1, list2))
1023 return 1;
1024 list1 = list1->next;
1025 list2 = list2->next;
1029 static inline void set_arg_count(struct token *token)
1031 token_type(token) = TOKEN_ARG_COUNT;
1032 token->count.normal = token->count.quoted =
1033 token->count.str = token->count.vararg = 0;
1036 static struct token *parse_arguments(struct token *list)
1038 struct token *arg = list->next, *next = list;
1039 struct argcount *count = &list->count;
1041 set_arg_count(list);
1043 if (match_op(arg, ')')) {
1044 next = arg->next;
1045 list->next = &eof_token_entry;
1046 return next;
1049 while (token_type(arg) == TOKEN_IDENT) {
1050 if (arg->ident == &__VA_ARGS___ident)
1051 goto Eva_args;
1052 if (!++count->normal)
1053 goto Eargs;
1054 next = arg->next;
1056 if (match_op(next, ',')) {
1057 set_arg_count(next);
1058 arg = next->next;
1059 continue;
1062 if (match_op(next, ')')) {
1063 set_arg_count(next);
1064 next = next->next;
1065 arg->next->next = &eof_token_entry;
1066 return next;
1069 /* normal cases are finished here */
1071 if (match_op(next, SPECIAL_ELLIPSIS)) {
1072 if (match_op(next->next, ')')) {
1073 set_arg_count(next);
1074 next->count.vararg = 1;
1075 next = next->next;
1076 arg->next->next = &eof_token_entry;
1077 return next->next;
1080 arg = next;
1081 goto Enotclosed;
1084 if (eof_token(next)) {
1085 goto Enotclosed;
1086 } else {
1087 arg = next;
1088 goto Ebadstuff;
1092 if (match_op(arg, SPECIAL_ELLIPSIS)) {
1093 next = arg->next;
1094 token_type(arg) = TOKEN_IDENT;
1095 arg->ident = &__VA_ARGS___ident;
1096 if (!match_op(next, ')'))
1097 goto Enotclosed;
1098 if (!++count->normal)
1099 goto Eargs;
1100 set_arg_count(next);
1101 next->count.vararg = 1;
1102 next = next->next;
1103 arg->next->next = &eof_token_entry;
1104 return next;
1107 if (eof_token(arg)) {
1108 arg = next;
1109 goto Enotclosed;
1111 if (match_op(arg, ','))
1112 goto Emissing;
1113 else
1114 goto Ebadstuff;
1117 Emissing:
1118 sparse_error(arg->pos, "parameter name missing");
1119 return NULL;
1120 Ebadstuff:
1121 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
1122 show_token(arg));
1123 return NULL;
1124 Enotclosed:
1125 sparse_error(arg->pos, "missing ')' in macro parameter list");
1126 return NULL;
1127 Eva_args:
1128 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1129 return NULL;
1130 Eargs:
1131 sparse_error(arg->pos, "too many arguments in macro definition");
1132 return NULL;
1135 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1137 struct ident *ident = token->ident;
1138 int nr;
1140 if (!arglist || token_type(token) != TOKEN_IDENT)
1141 return 0;
1143 arglist = arglist->next;
1145 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1146 if (arglist->ident == ident) {
1147 struct argcount *count = &arglist->next->count;
1148 int n;
1150 token->argnum = nr;
1151 token_type(token) = type;
1152 switch (type) {
1153 case TOKEN_MACRO_ARGUMENT:
1154 n = ++count->normal;
1155 break;
1156 case TOKEN_QUOTED_ARGUMENT:
1157 n = ++count->quoted;
1158 break;
1159 default:
1160 n = ++count->str;
1162 if (n)
1163 return count->vararg ? 2 : 1;
1165 * XXX - need saner handling of that
1166 * (>= 1024 instances of argument)
1168 token_type(token) = TOKEN_ERROR;
1169 return -1;
1172 return 0;
1175 static struct token *handle_hash(struct token **p, struct token *arglist)
1177 struct token *token = *p;
1178 if (arglist) {
1179 struct token *next = token->next;
1180 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1181 goto Equote;
1182 next->pos.whitespace = token->pos.whitespace;
1183 __free_token(token);
1184 token = *p = next;
1185 } else {
1186 token->pos.noexpand = 1;
1188 return token;
1190 Equote:
1191 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1192 return NULL;
1195 /* token->next is ## */
1196 static struct token *handle_hashhash(struct token *token, struct token *arglist)
1198 struct token *last = token;
1199 struct token *concat;
1200 int state = match_op(token, ',');
1202 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1204 while (1) {
1205 struct token *t;
1206 int is_arg;
1208 /* eat duplicate ## */
1209 concat = token->next;
1210 while (match_op(t = concat->next, SPECIAL_HASHHASH)) {
1211 token->next = t;
1212 __free_token(concat);
1213 concat = t;
1215 token_type(concat) = TOKEN_CONCAT;
1217 if (eof_token(t))
1218 goto Econcat;
1220 if (match_op(t, '#')) {
1221 t = handle_hash(&concat->next, arglist);
1222 if (!t)
1223 return NULL;
1226 is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
1228 if (state == 1 && is_arg) {
1229 state = is_arg;
1230 } else {
1231 last = t;
1232 state = match_op(t, ',');
1235 token = t;
1236 if (!match_op(token->next, SPECIAL_HASHHASH))
1237 break;
1239 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1240 if (state == 2)
1241 token_type(last) = TOKEN_GNU_KLUDGE;
1242 return token;
1244 Econcat:
1245 sparse_error(concat->pos, "'##' cannot appear at the ends of macro expansion");
1246 return NULL;
1249 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1251 struct token *token = expansion;
1252 struct token **p;
1254 if (match_op(token, SPECIAL_HASHHASH))
1255 goto Econcat;
1257 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1258 if (match_op(token, '#')) {
1259 token = handle_hash(p, arglist);
1260 if (!token)
1261 return NULL;
1263 if (match_op(token->next, SPECIAL_HASHHASH)) {
1264 token = handle_hashhash(token, arglist);
1265 if (!token)
1266 return NULL;
1267 } else {
1268 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1270 switch (token_type(token)) {
1271 case TOKEN_ERROR:
1272 goto Earg;
1274 case TOKEN_STRING:
1275 case TOKEN_WIDE_STRING:
1276 token->string->immutable = 1;
1277 break;
1280 token = alloc_token(&expansion->pos);
1281 token_type(token) = TOKEN_UNTAINT;
1282 token->ident = name;
1283 token->next = *p;
1284 *p = token;
1285 return expansion;
1287 Econcat:
1288 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1289 return NULL;
1290 Earg:
1291 sparse_error(token->pos, "too many instances of argument in body");
1292 return NULL;
1295 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1297 struct token *arglist, *expansion;
1298 struct token *left = token->next;
1299 struct symbol *sym;
1300 struct ident *name;
1301 int ret;
1303 if (token_type(left) != TOKEN_IDENT) {
1304 sparse_error(token->pos, "expected identifier to 'define'");
1305 return 1;
1308 name = left->ident;
1310 arglist = NULL;
1311 expansion = left->next;
1312 if (!expansion->pos.whitespace) {
1313 if (match_op(expansion, '(')) {
1314 arglist = expansion;
1315 expansion = parse_arguments(expansion);
1316 if (!expansion)
1317 return 1;
1318 } else if (!eof_token(expansion)) {
1319 warning(expansion->pos,
1320 "no whitespace before object-like macro body");
1324 expansion = parse_expansion(expansion, arglist, name);
1325 if (!expansion)
1326 return 1;
1328 ret = 1;
1329 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1330 if (sym) {
1331 int clean;
1333 if (attr < sym->attr)
1334 goto out;
1336 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1338 if (token_list_different(sym->expansion, expansion) ||
1339 token_list_different(sym->arglist, arglist)) {
1340 ret = 0;
1341 if ((clean && attr == SYM_ATTR_NORMAL)
1342 || sym->used_in == file_scope) {
1343 warning(left->pos, "preprocessor token %.*s redefined",
1344 name->len, name->name);
1345 info(sym->pos, "this was the original definition");
1347 } else if (clean)
1348 goto out;
1351 if (!sym || sym->scope != file_scope) {
1352 sym = alloc_symbol(left->pos, SYM_NODE);
1353 bind_symbol(sym, name, NS_MACRO);
1354 ret = 0;
1357 if (!ret) {
1358 sym->expansion = expansion;
1359 sym->arglist = arglist;
1360 __free_token(token); /* Free the "define" token, but not the rest of the line */
1363 sym->namespace = NS_MACRO;
1364 sym->used_in = NULL;
1365 sym->attr = attr;
1366 out:
1367 return ret;
1370 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1372 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1375 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1377 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1380 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1382 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1385 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1387 struct token *left = token->next;
1388 struct symbol *sym;
1390 if (token_type(left) != TOKEN_IDENT) {
1391 sparse_error(token->pos, "expected identifier to 'undef'");
1392 return 1;
1395 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1396 if (sym) {
1397 if (attr < sym->attr)
1398 return 1;
1399 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1400 return 1;
1401 } else if (attr <= SYM_ATTR_NORMAL)
1402 return 1;
1404 if (!sym || sym->scope != file_scope) {
1405 sym = alloc_symbol(left->pos, SYM_NODE);
1406 bind_symbol(sym, left->ident, NS_MACRO);
1409 sym->namespace = NS_UNDEF;
1410 sym->used_in = NULL;
1411 sym->attr = attr;
1413 return 1;
1416 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1418 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1421 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1423 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1426 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1428 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1429 free_preprocessor_line(token->next);
1430 token->next = stream->top_if;
1431 stream->top_if = token;
1432 if (false_nesting || true != 1)
1433 false_nesting++;
1434 return 0;
1437 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1439 struct token *next = token->next;
1440 int arg;
1441 if (token_type(next) == TOKEN_IDENT) {
1442 arg = token_defined(next);
1443 } else {
1444 dirty_stream(stream);
1445 if (!false_nesting)
1446 sparse_error(token->pos, "expected preprocessor identifier");
1447 arg = -1;
1449 return preprocessor_if(stream, token, arg);
1452 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1454 struct token *next = token->next;
1455 int arg;
1456 if (token_type(next) == TOKEN_IDENT) {
1457 if (!stream->dirty && !stream->ifndef) {
1458 if (!stream->protect) {
1459 stream->ifndef = token;
1460 stream->protect = next->ident;
1461 } else if (stream->protect == next->ident) {
1462 stream->ifndef = token;
1463 stream->dirty = 1;
1466 arg = !token_defined(next);
1467 } else {
1468 dirty_stream(stream);
1469 if (!false_nesting)
1470 sparse_error(token->pos, "expected preprocessor identifier");
1471 arg = -1;
1474 return preprocessor_if(stream, token, arg);
1477 static const char *show_token_sequence(struct token *token, int quote);
1480 * Expression handling for #if and #elif; it differs from normal expansion
1481 * due to special treatment of "defined".
1483 static int expression_value(struct token **where)
1485 struct expression *expr;
1486 struct token *p;
1487 struct token **list = where, **beginning = NULL;
1488 long long value;
1489 int state = 0;
1491 while (!eof_token(p = scan_next(list))) {
1492 switch (state) {
1493 case 0:
1494 if (token_type(p) != TOKEN_IDENT)
1495 break;
1496 if (p->ident == &defined_ident) {
1497 state = 1;
1498 beginning = list;
1499 break;
1501 if (!expand_one_symbol(list))
1502 continue;
1503 if (token_type(p) != TOKEN_IDENT)
1504 break;
1505 token_type(p) = TOKEN_ZERO_IDENT;
1506 break;
1507 case 1:
1508 if (match_op(p, '(')) {
1509 state = 2;
1510 } else {
1511 state = 0;
1512 replace_with_defined(p);
1513 *beginning = p;
1515 break;
1516 case 2:
1517 if (token_type(p) == TOKEN_IDENT)
1518 state = 3;
1519 else
1520 state = 0;
1521 replace_with_defined(p);
1522 *beginning = p;
1523 break;
1524 case 3:
1525 state = 0;
1526 if (!match_op(p, ')'))
1527 sparse_error(p->pos, "missing ')' after \"defined\"");
1528 *list = p->next;
1529 continue;
1531 list = &p->next;
1534 p = constant_expression(*where, &expr);
1535 if (!eof_token(p))
1536 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p, 0));
1537 value = get_expression_value(expr);
1538 return value != 0;
1541 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1543 int value = 0;
1544 if (!false_nesting)
1545 value = expression_value(&token->next);
1547 dirty_stream(stream);
1548 return preprocessor_if(stream, token, value);
1551 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1553 struct token *top_if = stream->top_if;
1554 end_group(stream);
1556 if (!top_if) {
1557 nesting_error(stream);
1558 sparse_error(token->pos, "unmatched #elif within stream");
1559 return 1;
1562 if (token_type(top_if) == TOKEN_ELSE) {
1563 nesting_error(stream);
1564 sparse_error(token->pos, "#elif after #else");
1565 if (!false_nesting)
1566 false_nesting = 1;
1567 return 1;
1570 dirty_stream(stream);
1571 if (token_type(top_if) != TOKEN_IF)
1572 return 1;
1573 if (false_nesting) {
1574 false_nesting = 0;
1575 if (!expression_value(&token->next))
1576 false_nesting = 1;
1577 } else {
1578 false_nesting = 1;
1579 token_type(top_if) = TOKEN_SKIP_GROUPS;
1581 return 1;
1584 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1586 struct token *top_if = stream->top_if;
1587 end_group(stream);
1589 if (!top_if) {
1590 nesting_error(stream);
1591 sparse_error(token->pos, "unmatched #else within stream");
1592 return 1;
1595 if (token_type(top_if) == TOKEN_ELSE) {
1596 nesting_error(stream);
1597 sparse_error(token->pos, "#else after #else");
1599 if (false_nesting) {
1600 if (token_type(top_if) == TOKEN_IF)
1601 false_nesting = 0;
1602 } else {
1603 false_nesting = 1;
1605 token_type(top_if) = TOKEN_ELSE;
1606 return 1;
1609 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1611 struct token *top_if = stream->top_if;
1612 end_group(stream);
1613 if (!top_if) {
1614 nesting_error(stream);
1615 sparse_error(token->pos, "unmatched #endif in stream");
1616 return 1;
1618 if (false_nesting)
1619 false_nesting--;
1620 stream->top_if = top_if->next;
1621 __free_token(top_if);
1622 return 1;
1625 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1627 warning(token->pos, "%s", show_token_sequence(token->next, 0));
1628 return 1;
1631 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1633 sparse_error(token->pos, "%s", show_token_sequence(token->next, 0));
1634 return 1;
1637 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1640 * Do we have any non-system includes?
1641 * Clear them out if so..
1643 *sys_includepath = NULL;
1644 return 1;
1647 static inline void update_inc_ptrs(const char ***where)
1650 if (*where <= dirafter_includepath) {
1651 dirafter_includepath++;
1652 /* If this was the entry that we prepend, don't
1653 * rise the lower entries, even if they are at
1654 * the same level. */
1655 if (where == &dirafter_includepath)
1656 return;
1658 if (*where <= sys_includepath) {
1659 sys_includepath++;
1660 if (where == &sys_includepath)
1661 return;
1663 if (*where <= isys_includepath) {
1664 isys_includepath++;
1665 if (where == &isys_includepath)
1666 return;
1669 /* angle_includepath is actually never updated, since we
1670 * don't suppport -iquote rught now. May change some day. */
1671 if (*where <= angle_includepath) {
1672 angle_includepath++;
1673 if (where == &angle_includepath)
1674 return;
1678 /* Add a path before 'where' and update the pointers associated with the
1679 * includepath array */
1680 static void add_path_entry(struct token *token, const char *path,
1681 const char ***where)
1683 const char **dst;
1684 const char *next;
1686 /* Need one free entry.. */
1687 if (includepath[INCLUDEPATHS-2])
1688 error_die(token->pos, "too many include path entries");
1690 /* check that this is not a duplicate */
1691 dst = includepath;
1692 while (*dst) {
1693 if (strcmp(*dst, path) == 0)
1694 return;
1695 dst++;
1697 next = path;
1698 dst = *where;
1700 update_inc_ptrs(where);
1703 * Move them all up starting at dst,
1704 * insert the new entry..
1706 do {
1707 const char *tmp = *dst;
1708 *dst = next;
1709 next = tmp;
1710 dst++;
1711 } while (next);
1714 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1716 for (;;) {
1717 token = token->next;
1718 if (eof_token(token))
1719 return 1;
1720 if (token_type(token) != TOKEN_STRING) {
1721 warning(token->pos, "expected path string");
1722 return 1;
1724 add_path_entry(token, token->string->data, &isys_includepath);
1728 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1730 for (;;) {
1731 token = token->next;
1732 if (eof_token(token))
1733 return 1;
1734 if (token_type(token) != TOKEN_STRING) {
1735 sparse_error(token->pos, "expected path string");
1736 return 1;
1738 add_path_entry(token, token->string->data, &sys_includepath);
1742 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1744 for (;;) {
1745 token = token->next;
1746 if (eof_token(token))
1747 return 1;
1748 if (token_type(token) != TOKEN_STRING) {
1749 sparse_error(token->pos, "expected path string");
1750 return 1;
1752 add_path_entry(token, token->string->data, &dirafter_includepath);
1756 /* Add to end on includepath list - no pointer updates */
1757 static void add_dirafter_entry(struct token *token, const char *path)
1759 const char **dst = includepath;
1761 /* Need one free entry.. */
1762 if (includepath[INCLUDEPATHS-2])
1763 error_die(token->pos, "too many include path entries");
1765 /* Add to the end */
1766 while (*dst)
1767 dst++;
1768 *dst = path;
1769 dst++;
1770 *dst = NULL;
1773 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1775 for (;;) {
1776 token = token->next;
1777 if (eof_token(token))
1778 return 1;
1779 if (token_type(token) != TOKEN_STRING) {
1780 sparse_error(token->pos, "expected path string");
1781 return 1;
1783 add_dirafter_entry(token, token->string->data);
1787 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1790 * -I-
1791 * From info gcc:
1792 * Split the include path. Any directories specified with `-I'
1793 * options before `-I-' are searched only for headers requested with
1794 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1795 * If additional directories are specified with `-I' options after
1796 * the `-I-', those directories are searched for all `#include'
1797 * directives.
1798 * In addition, `-I-' inhibits the use of the directory of the current
1799 * file directory as the first search directory for `#include "FILE"'.
1801 quote_includepath = includepath+1;
1802 angle_includepath = sys_includepath;
1803 return 1;
1807 * We replace "#pragma xxx" with "__pragma__" in the token
1808 * stream. Just as an example.
1810 * We'll just #define that away for now, but the theory here
1811 * is that we can use this to insert arbitrary token sequences
1812 * to turn the pragmas into internal front-end sequences for
1813 * when we actually start caring about them.
1815 * So eventually this will turn into some kind of extended
1816 * __attribute__() like thing, except called __pragma__(xxx).
1818 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1820 struct token *next = *line;
1822 if (match_ident(token->next, &once_ident) && eof_token(token->next->next)) {
1823 stream->once = 1;
1824 return 1;
1826 token->ident = &pragma_ident;
1827 token->pos.newline = 1;
1828 token->pos.whitespace = 1;
1829 token->pos.pos = 1;
1830 *line = token;
1831 token->next = next;
1832 return 0;
1836 * We ignore #line for now.
1838 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1840 return 1;
1843 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1845 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token, 0));
1846 return 1;
1850 static void init_preprocessor(void)
1852 int i;
1853 int stream = init_stream("preprocessor", -1, includepath);
1854 static struct {
1855 const char *name;
1856 int (*handler)(struct stream *, struct token **, struct token *);
1857 } normal[] = {
1858 { "define", handle_define },
1859 { "weak_define", handle_weak_define },
1860 { "strong_define", handle_strong_define },
1861 { "undef", handle_undef },
1862 { "strong_undef", handle_strong_undef },
1863 { "warning", handle_warning },
1864 { "error", handle_error },
1865 { "include", handle_include },
1866 { "include_next", handle_include_next },
1867 { "pragma", handle_pragma },
1868 { "line", handle_line },
1870 // our internal preprocessor tokens
1871 { "nostdinc", handle_nostdinc },
1872 { "add_include", handle_add_include },
1873 { "add_isystem", handle_add_isystem },
1874 { "add_system", handle_add_system },
1875 { "add_dirafter", handle_add_dirafter },
1876 { "split_include", handle_split_include },
1877 { "argv_include", handle_argv_include },
1878 }, special[] = {
1879 { "ifdef", handle_ifdef },
1880 { "ifndef", handle_ifndef },
1881 { "else", handle_else },
1882 { "endif", handle_endif },
1883 { "if", handle_if },
1884 { "elif", handle_elif },
1887 for (i = 0; i < ARRAY_SIZE(normal); i++) {
1888 struct symbol *sym;
1889 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1890 sym->handler = normal[i].handler;
1891 sym->normal = 1;
1893 for (i = 0; i < ARRAY_SIZE(special); i++) {
1894 struct symbol *sym;
1895 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1896 sym->handler = special[i].handler;
1897 sym->normal = 0;
1900 counter_macro = 0;
1903 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1905 int (*handler)(struct stream *, struct token **, struct token *);
1906 struct token *token = start->next;
1907 int is_normal = 1;
1909 if (eof_token(token))
1910 return;
1912 if (token_type(token) == TOKEN_IDENT) {
1913 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1914 if (sym) {
1915 handler = sym->handler;
1916 is_normal = sym->normal;
1917 } else {
1918 handler = handle_nondirective;
1920 } else if (token_type(token) == TOKEN_NUMBER) {
1921 handler = handle_line;
1922 } else {
1923 handler = handle_nondirective;
1926 if (is_normal) {
1927 dirty_stream(stream);
1928 if (false_nesting)
1929 goto out;
1931 if (!handler(stream, line, token)) /* all set */
1932 return;
1934 out:
1935 free_preprocessor_line(token);
1938 static void preprocessor_line(struct stream *stream, struct token **line)
1940 struct token *start = *line, *next;
1941 struct token **tp = &start->next;
1943 for (;;) {
1944 next = *tp;
1945 if (next->pos.newline)
1946 break;
1947 tp = &next->next;
1949 *line = next;
1950 *tp = &eof_token_entry;
1951 handle_preprocessor_line(stream, line, start);
1954 static void do_preprocess(struct token **list)
1956 struct token *next;
1958 while (!eof_token(next = scan_next(list))) {
1959 struct stream *stream = input_streams + next->pos.stream;
1961 if (next->pos.newline && match_op(next, '#')) {
1962 if (!next->pos.noexpand) {
1963 preprocessor_line(stream, list);
1964 __free_token(next); /* Free the '#' token */
1965 continue;
1969 switch (token_type(next)) {
1970 case TOKEN_STREAMEND:
1971 if (stream->top_if) {
1972 nesting_error(stream);
1973 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1974 stream->top_if = NULL;
1975 false_nesting = 0;
1977 if (!stream->dirty)
1978 stream->constant = CONSTANT_FILE_YES;
1979 *list = next->next;
1980 continue;
1981 case TOKEN_STREAMBEGIN:
1982 *list = next->next;
1983 continue;
1985 default:
1986 dirty_stream(stream);
1987 if (false_nesting) {
1988 *list = next->next;
1989 __free_token(next);
1990 continue;
1993 if (token_type(next) != TOKEN_IDENT ||
1994 expand_one_symbol(list))
1995 list = &next->next;
2000 struct token * preprocess(struct token *token)
2002 preprocessing = 1;
2003 init_preprocessor();
2004 do_preprocess(&token);
2006 // Drop all expressions from preprocessing, they're not used any more.
2007 // This is not true when we have multiple files, though ;/
2008 // clear_expression_alloc();
2009 preprocessing = 0;
2011 return token;