test_kernel.sh: print where the warnings are saved
[smatch.git] / pre-process.c
blobdf6caa11c7d4b704f53fb94ad01ad23201272cba
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;
49 #define INCLUDEPATHS 300
50 const char *includepath[INCLUDEPATHS+1] = {
51 "",
52 "/usr/include",
53 "/usr/local/include",
54 NULL
57 static const char **quote_includepath = includepath;
58 static const char **angle_includepath = includepath + 1;
59 static const char **isys_includepath = includepath + 1;
60 static const char **sys_includepath = includepath + 1;
61 static const char **dirafter_includepath = includepath + 3;
63 #define dirty_stream(stream) \
64 do { \
65 if (!stream->dirty) { \
66 stream->dirty = 1; \
67 if (!stream->ifndef) \
68 stream->protect = NULL; \
69 } \
70 } while(0)
72 #define end_group(stream) \
73 do { \
74 if (stream->ifndef == stream->top_if) { \
75 stream->ifndef = NULL; \
76 if (!stream->dirty) \
77 stream->protect = NULL; \
78 else if (stream->protect) \
79 stream->dirty = 0; \
80 } \
81 } while(0)
83 #define nesting_error(stream) \
84 do { \
85 stream->dirty = 1; \
86 stream->ifndef = NULL; \
87 stream->protect = NULL; \
88 } while(0)
90 static struct token *alloc_token(struct position *pos)
92 struct token *token = __alloc_token(0);
94 token->pos.stream = pos->stream;
95 token->pos.line = pos->line;
96 token->pos.pos = pos->pos;
97 token->pos.whitespace = 1;
98 return token;
101 /* Expand symbol 'sym' at '*list' */
102 static int expand(struct token **, struct symbol *);
104 static void replace_with_string(struct token *token, const char *str)
106 int size = strlen(str) + 1;
107 struct string *s = __alloc_string(size);
109 s->length = size;
110 memcpy(s->data, str, size);
111 token_type(token) = TOKEN_STRING;
112 token->string = s;
115 static void replace_with_integer(struct token *token, unsigned int val)
117 char *buf = __alloc_bytes(11);
118 sprintf(buf, "%u", val);
119 token_type(token) = TOKEN_NUMBER;
120 token->number = buf;
123 static struct symbol *lookup_macro(struct ident *ident)
125 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
126 if (sym && sym->namespace != NS_MACRO)
127 sym = NULL;
128 return sym;
131 static int token_defined(struct token *token)
133 if (token_type(token) == TOKEN_IDENT) {
134 struct symbol *sym = lookup_macro(token->ident);
135 if (sym) {
136 sym->used_in = file_scope;
137 return 1;
139 return 0;
142 sparse_error(token->pos, "expected preprocessor identifier");
143 return 0;
146 static void replace_with_defined(struct token *token)
148 static const char *string[] = { "0", "1" };
149 int defined = token_defined(token);
151 token_type(token) = TOKEN_NUMBER;
152 token->number = string[defined];
155 static int expand_one_symbol(struct token **list)
157 struct token *token = *list;
158 struct symbol *sym;
159 static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
160 static time_t t = 0;
162 if (token->pos.noexpand)
163 return 1;
165 sym = lookup_macro(token->ident);
166 if (sym) {
167 store_macro_pos(token);
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);
186 return 1;
189 static inline struct token *scan_next(struct token **where)
191 struct token *token = *where;
192 if (token_type(token) != TOKEN_UNTAINT)
193 return token;
194 do {
195 token->ident->tainted = 0;
196 token = token->next;
197 } while (token_type(token) == TOKEN_UNTAINT);
198 *where = token;
199 return token;
202 static void expand_list(struct token **list)
204 struct token *next;
205 while (!eof_token(next = scan_next(list))) {
206 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
207 list = &next->next;
211 static void preprocessor_line(struct stream *stream, struct token **line);
213 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
215 struct stream *stream = input_streams + prev->pos.stream;
216 struct token **p = &prev->next;
217 struct token *next;
218 int nesting = 0;
220 while (!eof_token(next = scan_next(p))) {
221 if (next->pos.newline && match_op(next, '#')) {
222 if (!next->pos.noexpand) {
223 sparse_error(next->pos,
224 "directive in argument list");
225 preprocessor_line(stream, p);
226 __free_token(next); /* Free the '#' token */
227 continue;
230 switch (token_type(next)) {
231 case TOKEN_STREAMEND:
232 case TOKEN_STREAMBEGIN:
233 *p = &eof_token_entry;
234 return next;
236 if (false_nesting) {
237 *p = next->next;
238 __free_token(next);
239 continue;
241 if (match_op(next, '(')) {
242 nesting++;
243 } else if (match_op(next, ')')) {
244 if (!nesting--)
245 break;
246 } else if (match_op(next, ',') && !nesting && !vararg) {
247 break;
249 next->pos.stream = pos->stream;
250 next->pos.line = pos->line;
251 next->pos.pos = pos->pos;
252 p = &next->next;
254 *p = &eof_token_entry;
255 return next;
259 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
262 struct arg {
263 struct token *arg;
264 struct token *expanded;
265 struct token *str;
266 int n_normal;
267 int n_quoted;
268 int n_str;
271 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
273 int wanted = arglist->count.normal;
274 struct token *next = NULL;
275 int count = 0;
277 arglist = arglist->next; /* skip counter */
279 if (!wanted) {
280 next = collect_arg(start, 0, &what->pos);
281 if (eof_token(next))
282 goto Eclosing;
283 if (!eof_token(start->next) || !match_op(next, ')')) {
284 count++;
285 goto Emany;
287 } else {
288 for (count = 0; count < wanted; count++) {
289 struct argcount *p = &arglist->next->count;
290 next = collect_arg(start, p->vararg, &what->pos);
291 arglist = arglist->next->next;
292 if (eof_token(next))
293 goto Eclosing;
294 args[count].arg = start->next;
295 args[count].n_normal = p->normal;
296 args[count].n_quoted = p->quoted;
297 args[count].n_str = p->str;
298 if (match_op(next, ')')) {
299 count++;
300 break;
302 start = next;
304 if (count == wanted && !match_op(next, ')'))
305 goto Emany;
306 if (count == wanted - 1) {
307 struct argcount *p = &arglist->next->count;
308 if (!p->vararg)
309 goto Efew;
310 args[count].arg = NULL;
311 args[count].n_normal = p->normal;
312 args[count].n_quoted = p->quoted;
313 args[count].n_str = p->str;
315 if (count < wanted - 1)
316 goto Efew;
318 what->next = next->next;
319 return 1;
321 Efew:
322 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
323 show_token(what), wanted, count);
324 goto out;
325 Emany:
326 while (match_op(next, ',')) {
327 next = collect_arg(next, 0, &what->pos);
328 count++;
330 if (eof_token(next))
331 goto Eclosing;
332 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
333 show_token(what), count, wanted);
334 goto out;
335 Eclosing:
336 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
337 show_token(what));
338 out:
339 what->next = next->next;
340 return 0;
343 static struct token *dup_list(struct token *list)
345 struct token *res = NULL;
346 struct token **p = &res;
348 while (!eof_token(list)) {
349 struct token *newtok = __alloc_token(0);
350 *newtok = *list;
351 *p = newtok;
352 p = &newtok->next;
353 list = list->next;
355 return res;
358 static const char *show_token_sequence(struct token *token, int quote)
360 static char buffer[MAX_STRING];
361 char *ptr = buffer;
362 int whitespace = 0;
364 if (!token && !quote)
365 return "<none>";
366 while (!eof_token(token)) {
367 const char *val = quote ? quote_token(token) : show_token(token);
368 int len = strlen(val);
370 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
371 sparse_error(token->pos, "too long token expansion");
372 break;
375 if (whitespace)
376 *ptr++ = ' ';
377 memcpy(ptr, val, len);
378 ptr += len;
379 token = token->next;
380 whitespace = token->pos.whitespace;
382 *ptr = 0;
383 return buffer;
386 static struct token *stringify(struct token *arg)
388 const char *s = show_token_sequence(arg, 1);
389 int size = strlen(s)+1;
390 struct token *token = __alloc_token(0);
391 struct string *string = __alloc_string(size);
393 memcpy(string->data, s, size);
394 string->length = size;
395 token->pos = arg->pos;
396 token_type(token) = TOKEN_STRING;
397 token->string = string;
398 token->next = &eof_token_entry;
399 return token;
402 static void expand_arguments(int count, struct arg *args)
404 int i;
405 for (i = 0; i < count; i++) {
406 struct token *arg = args[i].arg;
407 if (!arg)
408 arg = &eof_token_entry;
409 if (args[i].n_str)
410 args[i].str = stringify(arg);
411 if (args[i].n_normal) {
412 if (!args[i].n_quoted) {
413 args[i].expanded = arg;
414 args[i].arg = NULL;
415 } else if (eof_token(arg)) {
416 args[i].expanded = arg;
417 } else {
418 args[i].expanded = dup_list(arg);
420 expand_list(&args[i].expanded);
426 * Possibly valid combinations:
427 * - ident + ident -> ident
428 * - ident + number -> ident unless number contains '.', '+' or '-'.
429 * - 'L' + char constant -> wide char constant
430 * - 'L' + string literal -> wide string literal
431 * - number + number -> number
432 * - number + ident -> number
433 * - number + '.' -> number
434 * - number + '+' or '-' -> number, if number used to end on [eEpP].
435 * - '.' + number -> number, if number used to start with a digit.
436 * - special + special -> either special or an error.
438 static enum token_type combine(struct token *left, struct token *right, char *p)
440 int len;
441 enum token_type t1 = token_type(left), t2 = token_type(right);
443 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
444 return TOKEN_ERROR;
446 if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
447 if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
448 return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
449 if (t2 == TOKEN_STRING)
450 return TOKEN_WIDE_STRING;
453 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
454 return TOKEN_ERROR;
456 strcpy(p, show_token(left));
457 strcat(p, show_token(right));
458 len = strlen(p);
460 if (len >= 256)
461 return TOKEN_ERROR;
463 if (t1 == TOKEN_IDENT) {
464 if (t2 == TOKEN_SPECIAL)
465 return TOKEN_ERROR;
466 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
467 return TOKEN_ERROR;
468 return TOKEN_IDENT;
471 if (t1 == TOKEN_NUMBER) {
472 if (t2 == TOKEN_SPECIAL) {
473 switch (right->special) {
474 case '.':
475 break;
476 case '+': case '-':
477 if (strchr("eEpP", p[len - 2]))
478 break;
479 default:
480 return TOKEN_ERROR;
483 return TOKEN_NUMBER;
486 if (p[0] == '.' && isdigit((unsigned char)p[1]))
487 return TOKEN_NUMBER;
489 return TOKEN_SPECIAL;
492 static int merge(struct token *left, struct token *right)
494 static char buffer[512];
495 enum token_type res = combine(left, right, buffer);
496 int n;
498 switch (res) {
499 case TOKEN_IDENT:
500 left->ident = built_in_ident(buffer);
501 left->pos.noexpand = 0;
502 return 1;
504 case TOKEN_NUMBER: {
505 char *number = __alloc_bytes(strlen(buffer) + 1);
506 memcpy(number, buffer, strlen(buffer) + 1);
507 token_type(left) = TOKEN_NUMBER; /* could be . + num */
508 left->number = number;
509 return 1;
512 case TOKEN_SPECIAL:
513 if (buffer[2] && buffer[3])
514 break;
515 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
516 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
517 left->special = n;
518 return 1;
521 break;
523 case TOKEN_WIDE_CHAR:
524 case TOKEN_WIDE_STRING:
525 token_type(left) = res;
526 left->pos.noexpand = 0;
527 left->string = right->string;
528 return 1;
530 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
531 token_type(left) = res;
532 left->pos.noexpand = 0;
533 memcpy(left->embedded, right->embedded, 4);
534 return 1;
536 default:
539 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
540 return 0;
543 static struct token *dup_token(struct token *token, struct position *streampos)
545 struct token *alloc = alloc_token(streampos);
546 token_type(alloc) = token_type(token);
547 alloc->pos.newline = token->pos.newline;
548 alloc->pos.whitespace = token->pos.whitespace;
549 alloc->number = token->number;
550 alloc->pos.noexpand = token->pos.noexpand;
551 return alloc;
554 static struct token **copy(struct token **where, struct token *list, int *count)
556 int need_copy = --*count;
557 while (!eof_token(list)) {
558 struct token *token;
559 if (need_copy)
560 token = dup_token(list, &list->pos);
561 else
562 token = list;
563 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
564 token->pos.noexpand = 1;
565 *where = token;
566 where = &token->next;
567 list = list->next;
569 *where = &eof_token_entry;
570 return where;
573 static int handle_kludge(struct token **p, struct arg *args)
575 struct token *t = (*p)->next->next;
576 while (1) {
577 struct arg *v = &args[t->argnum];
578 if (token_type(t->next) != TOKEN_CONCAT) {
579 if (v->arg) {
580 /* ignore the first ## */
581 *p = (*p)->next;
582 return 0;
584 /* skip the entire thing */
585 *p = t;
586 return 1;
588 if (v->arg && !eof_token(v->arg))
589 return 0; /* no magic */
590 t = t->next->next;
594 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
596 struct position *base_pos = &(*list)->pos;
597 int *count;
598 enum {Normal, Placeholder, Concat} state = Normal;
600 for (; !eof_token(body); body = body->next) {
601 struct token *added, *arg;
602 struct token **tail;
603 struct token *t;
605 switch (token_type(body)) {
606 case TOKEN_GNU_KLUDGE:
608 * GNU kludge: if we had <comma>##<vararg>, behaviour
609 * depends on whether we had enough arguments to have
610 * a vararg. If we did, ## is just ignored. Otherwise
611 * both , and ## are ignored. Worse, there can be
612 * an arbitrary number of ##<arg> in between; if all of
613 * those are empty, we act as if they hadn't been there,
614 * otherwise we act as if the kludge didn't exist.
616 t = body;
617 if (handle_kludge(&body, args)) {
618 if (state == Concat)
619 state = Normal;
620 else
621 state = Placeholder;
622 continue;
624 added = dup_token(t, base_pos);
625 token_type(added) = TOKEN_SPECIAL;
626 tail = &added->next;
627 break;
629 case TOKEN_STR_ARGUMENT:
630 arg = args[body->argnum].str;
631 count = &args[body->argnum].n_str;
632 goto copy_arg;
634 case TOKEN_QUOTED_ARGUMENT:
635 arg = args[body->argnum].arg;
636 count = &args[body->argnum].n_quoted;
637 if (!arg || eof_token(arg)) {
638 if (state == Concat)
639 state = Normal;
640 else
641 state = Placeholder;
642 continue;
644 goto copy_arg;
646 case TOKEN_MACRO_ARGUMENT:
647 arg = args[body->argnum].expanded;
648 count = &args[body->argnum].n_normal;
649 if (eof_token(arg)) {
650 state = Normal;
651 continue;
653 copy_arg:
654 tail = copy(&added, arg, count);
655 added->pos.newline = body->pos.newline;
656 added->pos.whitespace = body->pos.whitespace;
657 break;
659 case TOKEN_CONCAT:
660 if (state == Placeholder)
661 state = Normal;
662 else
663 state = Concat;
664 continue;
666 case TOKEN_IDENT:
667 added = dup_token(body, base_pos);
668 if (added->ident->tainted)
669 added->pos.noexpand = 1;
670 tail = &added->next;
671 break;
673 default:
674 added = dup_token(body, base_pos);
675 tail = &added->next;
676 break;
680 * if we got to doing real concatenation, we already have
681 * added something into the list, so containing_token() is OK.
683 if (state == Concat && merge(containing_token(list), added)) {
684 *list = added->next;
685 if (tail != &added->next)
686 list = tail;
687 } else {
688 *list = added;
689 list = tail;
691 state = Normal;
693 *list = &eof_token_entry;
694 return list;
697 static int expand(struct token **list, struct symbol *sym)
699 struct token *last;
700 struct token *token = *list;
701 struct ident *expanding = token->ident;
702 struct token **tail;
703 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
704 struct arg args[nargs];
706 if (expanding->tainted) {
707 token->pos.noexpand = 1;
708 return 1;
711 if (sym->arglist) {
712 if (!match_op(scan_next(&token->next), '('))
713 return 1;
714 if (!collect_arguments(token->next, sym->arglist, args, token))
715 return 1;
716 expand_arguments(nargs, args);
719 expanding->tainted = 1;
721 last = token->next;
722 tail = substitute(list, sym->expansion, args);
724 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
725 * We still can lose the newline flag if the sucker expands to nothing,
726 * but the price of dealing with that is probably too high (we'd need
727 * to collect the flags during scan_next())
729 (*list)->pos.newline = token->pos.newline;
730 (*list)->pos.whitespace = token->pos.whitespace;
731 *tail = last;
733 return 0;
736 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
738 static char buffer[256];
739 char *ptr = buffer;
741 while (!eof_token(token) && !match_op(token, endop)) {
742 int len;
743 const char *val = token->string->data;
744 if (token_type(token) != TOKEN_STRING)
745 val = show_token(token);
746 len = strlen(val);
747 memcpy(ptr, val, len);
748 ptr += len;
749 token = token->next;
751 *ptr = 0;
752 if (endop && !match_op(token, endop))
753 sparse_error(start->pos, "expected '>' at end of filename");
754 return buffer;
757 static int already_tokenized(const char *path)
759 int stream, next;
761 for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
762 struct stream *s = input_streams + stream;
764 next = s->next_stream;
765 if (s->once) {
766 if (strcmp(path, s->name))
767 continue;
768 return 1;
770 if (s->constant != CONSTANT_FILE_YES)
771 continue;
772 if (strcmp(path, s->name))
773 continue;
774 if (s->protect && !lookup_macro(s->protect))
775 continue;
776 return 1;
778 return 0;
781 /* Handle include of header files.
782 * The relevant options are made compatible with gcc. The only options that
783 * are not supported is -withprefix and friends.
785 * Three set of include paths are known:
786 * quote_includepath: Path to search when using #include "file.h"
787 * angle_includepath: Paths to search when using #include <file.h>
788 * isys_includepath: Paths specified with -isystem, come before the
789 * built-in system include paths. Gcc would suppress
790 * warnings from system headers. Here we separate
791 * them from the angle_ ones to keep search ordering.
793 * sys_includepath: Built-in include paths.
794 * dirafter_includepath Paths added with -dirafter.
796 * The above is implemented as one array with pointers
797 * +--------------+
798 * quote_includepath ---> | |
799 * +--------------+
800 * | |
801 * +--------------+
802 * angle_includepath ---> | |
803 * +--------------+
804 * isys_includepath ---> | |
805 * +--------------+
806 * sys_includepath ---> | |
807 * +--------------+
808 * dirafter_includepath -> | |
809 * +--------------+
811 * -I dir insert dir just before isys_includepath and move the rest
812 * -I- makes all dirs specified with -I before to quote dirs only and
813 * angle_includepath is set equal to isys_includepath.
814 * -nostdinc removes all sys dirs by storing NULL in entry pointed
815 * to by * sys_includepath. Note that this will reset all dirs built-in
816 * and added before -nostdinc by -isystem and -idirafter.
817 * -isystem dir adds dir where isys_includepath points adding this dir as
818 * first systemdir
819 * -idirafter dir adds dir to the end of the list
822 static void set_stream_include_path(struct stream *stream)
824 const char *path = stream->path;
825 if (!path) {
826 const char *p = strrchr(stream->name, '/');
827 path = "";
828 if (p) {
829 int len = p - stream->name + 1;
830 char *m = malloc(len+1);
831 /* This includes the final "/" */
832 memcpy(m, stream->name, len);
833 m[len] = 0;
834 path = m;
836 stream->path = path;
838 includepath[0] = path;
841 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
843 int fd;
844 int plen = strlen(path);
845 static char fullname[PATH_MAX];
847 memcpy(fullname, path, plen);
848 if (plen && path[plen-1] != '/') {
849 fullname[plen] = '/';
850 plen++;
852 memcpy(fullname+plen, filename, flen);
853 if (already_tokenized(fullname))
854 return 1;
855 fd = open(fullname, O_RDONLY);
856 if (fd >= 0) {
857 char * streamname = __alloc_bytes(plen + flen);
858 memcpy(streamname, fullname, plen + flen);
859 *where = tokenize(streamname, fd, *where, next_path);
860 close(fd);
861 return 1;
863 return 0;
866 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
868 const char *path;
870 while ((path = *pptr++) != NULL) {
871 if (!try_include(path, filename, flen, list, pptr))
872 continue;
873 return 1;
875 return 0;
878 static int free_preprocessor_line(struct token *token)
880 while (token_type(token) != TOKEN_EOF) {
881 struct token *free = token;
882 token = token->next;
883 __free_token(free);
885 return 1;
888 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how)
890 const char *filename;
891 struct token *next;
892 const char **path;
893 int expect;
894 int flen;
896 next = token->next;
897 expect = '>';
898 if (!match_op(next, '<')) {
899 expand_list(&token->next);
900 expect = 0;
901 next = token;
902 if (match_op(token->next, '<')) {
903 next = token->next;
904 expect = '>';
908 token = next->next;
909 filename = token_name_sequence(token, expect, token);
910 flen = strlen(filename) + 1;
912 /* Absolute path? */
913 if (filename[0] == '/') {
914 if (try_include("", filename, flen, list, includepath))
915 return 0;
916 goto out;
919 switch (how) {
920 case 1:
921 path = stream->next_path;
922 break;
923 case 2:
924 includepath[0] = "";
925 path = includepath;
926 break;
927 default:
928 /* Dir of input file is first dir to search for quoted includes */
929 set_stream_include_path(stream);
930 path = expect ? angle_includepath : quote_includepath;
931 break;
933 /* Check the standard include paths.. */
934 if (do_include_path(path, list, token, filename, flen))
935 return 0;
936 out:
937 error_die(token->pos, "unable to open '%s'", filename);
940 static int handle_include(struct stream *stream, struct token **list, struct token *token)
942 return handle_include_path(stream, list, token, 0);
945 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
947 return handle_include_path(stream, list, token, 1);
950 static int handle_argv_include(struct stream *stream, struct token **list, struct token *token)
952 return handle_include_path(stream, list, token, 2);
955 static int token_different(struct token *t1, struct token *t2)
957 int different;
959 if (token_type(t1) != token_type(t2))
960 return 1;
962 switch (token_type(t1)) {
963 case TOKEN_IDENT:
964 different = t1->ident != t2->ident;
965 break;
966 case TOKEN_ARG_COUNT:
967 case TOKEN_UNTAINT:
968 case TOKEN_CONCAT:
969 case TOKEN_GNU_KLUDGE:
970 different = 0;
971 break;
972 case TOKEN_NUMBER:
973 different = strcmp(t1->number, t2->number);
974 break;
975 case TOKEN_SPECIAL:
976 different = t1->special != t2->special;
977 break;
978 case TOKEN_MACRO_ARGUMENT:
979 case TOKEN_QUOTED_ARGUMENT:
980 case TOKEN_STR_ARGUMENT:
981 different = t1->argnum != t2->argnum;
982 break;
983 case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
984 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
985 different = memcmp(t1->embedded, t2->embedded, 4);
986 break;
987 case TOKEN_CHAR:
988 case TOKEN_WIDE_CHAR:
989 case TOKEN_STRING:
990 case TOKEN_WIDE_STRING: {
991 struct string *s1, *s2;
993 s1 = t1->string;
994 s2 = t2->string;
995 different = 1;
996 if (s1->length != s2->length)
997 break;
998 different = memcmp(s1->data, s2->data, s1->length);
999 break;
1001 default:
1002 different = 1;
1003 break;
1005 return different;
1008 static int token_list_different(struct token *list1, struct token *list2)
1010 for (;;) {
1011 if (list1 == list2)
1012 return 0;
1013 if (!list1 || !list2)
1014 return 1;
1015 if (token_different(list1, list2))
1016 return 1;
1017 list1 = list1->next;
1018 list2 = list2->next;
1022 static inline void set_arg_count(struct token *token)
1024 token_type(token) = TOKEN_ARG_COUNT;
1025 token->count.normal = token->count.quoted =
1026 token->count.str = token->count.vararg = 0;
1029 static struct token *parse_arguments(struct token *list)
1031 struct token *arg = list->next, *next = list;
1032 struct argcount *count = &list->count;
1034 set_arg_count(list);
1036 if (match_op(arg, ')')) {
1037 next = arg->next;
1038 list->next = &eof_token_entry;
1039 return next;
1042 while (token_type(arg) == TOKEN_IDENT) {
1043 if (arg->ident == &__VA_ARGS___ident)
1044 goto Eva_args;
1045 if (!++count->normal)
1046 goto Eargs;
1047 next = arg->next;
1049 if (match_op(next, ',')) {
1050 set_arg_count(next);
1051 arg = next->next;
1052 continue;
1055 if (match_op(next, ')')) {
1056 set_arg_count(next);
1057 next = next->next;
1058 arg->next->next = &eof_token_entry;
1059 return next;
1062 /* normal cases are finished here */
1064 if (match_op(next, SPECIAL_ELLIPSIS)) {
1065 if (match_op(next->next, ')')) {
1066 set_arg_count(next);
1067 next->count.vararg = 1;
1068 next = next->next;
1069 arg->next->next = &eof_token_entry;
1070 return next->next;
1073 arg = next;
1074 goto Enotclosed;
1077 if (eof_token(next)) {
1078 goto Enotclosed;
1079 } else {
1080 arg = next;
1081 goto Ebadstuff;
1085 if (match_op(arg, SPECIAL_ELLIPSIS)) {
1086 next = arg->next;
1087 token_type(arg) = TOKEN_IDENT;
1088 arg->ident = &__VA_ARGS___ident;
1089 if (!match_op(next, ')'))
1090 goto Enotclosed;
1091 if (!++count->normal)
1092 goto Eargs;
1093 set_arg_count(next);
1094 next->count.vararg = 1;
1095 next = next->next;
1096 arg->next->next = &eof_token_entry;
1097 return next;
1100 if (eof_token(arg)) {
1101 arg = next;
1102 goto Enotclosed;
1104 if (match_op(arg, ','))
1105 goto Emissing;
1106 else
1107 goto Ebadstuff;
1110 Emissing:
1111 sparse_error(arg->pos, "parameter name missing");
1112 return NULL;
1113 Ebadstuff:
1114 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
1115 show_token(arg));
1116 return NULL;
1117 Enotclosed:
1118 sparse_error(arg->pos, "missing ')' in macro parameter list");
1119 return NULL;
1120 Eva_args:
1121 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1122 return NULL;
1123 Eargs:
1124 sparse_error(arg->pos, "too many arguments in macro definition");
1125 return NULL;
1128 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1130 struct ident *ident = token->ident;
1131 int nr;
1133 if (!arglist || token_type(token) != TOKEN_IDENT)
1134 return 0;
1136 arglist = arglist->next;
1138 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1139 if (arglist->ident == ident) {
1140 struct argcount *count = &arglist->next->count;
1141 int n;
1143 token->argnum = nr;
1144 token_type(token) = type;
1145 switch (type) {
1146 case TOKEN_MACRO_ARGUMENT:
1147 n = ++count->normal;
1148 break;
1149 case TOKEN_QUOTED_ARGUMENT:
1150 n = ++count->quoted;
1151 break;
1152 default:
1153 n = ++count->str;
1155 if (n)
1156 return count->vararg ? 2 : 1;
1158 * XXX - need saner handling of that
1159 * (>= 1024 instances of argument)
1161 token_type(token) = TOKEN_ERROR;
1162 return -1;
1165 return 0;
1168 static struct token *handle_hash(struct token **p, struct token *arglist)
1170 struct token *token = *p;
1171 if (arglist) {
1172 struct token *next = token->next;
1173 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1174 goto Equote;
1175 next->pos.whitespace = token->pos.whitespace;
1176 __free_token(token);
1177 token = *p = next;
1178 } else {
1179 token->pos.noexpand = 1;
1181 return token;
1183 Equote:
1184 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1185 return NULL;
1188 /* token->next is ## */
1189 static struct token *handle_hashhash(struct token *token, struct token *arglist)
1191 struct token *last = token;
1192 struct token *concat;
1193 int state = match_op(token, ',');
1195 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1197 while (1) {
1198 struct token *t;
1199 int is_arg;
1201 /* eat duplicate ## */
1202 concat = token->next;
1203 while (match_op(t = concat->next, SPECIAL_HASHHASH)) {
1204 token->next = t;
1205 __free_token(concat);
1206 concat = t;
1208 token_type(concat) = TOKEN_CONCAT;
1210 if (eof_token(t))
1211 goto Econcat;
1213 if (match_op(t, '#')) {
1214 t = handle_hash(&concat->next, arglist);
1215 if (!t)
1216 return NULL;
1219 is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
1221 if (state == 1 && is_arg) {
1222 state = is_arg;
1223 } else {
1224 last = t;
1225 state = match_op(t, ',');
1228 token = t;
1229 if (!match_op(token->next, SPECIAL_HASHHASH))
1230 break;
1232 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1233 if (state == 2)
1234 token_type(last) = TOKEN_GNU_KLUDGE;
1235 return token;
1237 Econcat:
1238 sparse_error(concat->pos, "'##' cannot appear at the ends of macro expansion");
1239 return NULL;
1242 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1244 struct token *token = expansion;
1245 struct token **p;
1247 if (match_op(token, SPECIAL_HASHHASH))
1248 goto Econcat;
1250 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1251 if (match_op(token, '#')) {
1252 token = handle_hash(p, arglist);
1253 if (!token)
1254 return NULL;
1256 if (match_op(token->next, SPECIAL_HASHHASH)) {
1257 token = handle_hashhash(token, arglist);
1258 if (!token)
1259 return NULL;
1260 } else {
1261 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1263 if (token_type(token) == TOKEN_ERROR)
1264 goto Earg;
1266 token = alloc_token(&expansion->pos);
1267 token_type(token) = TOKEN_UNTAINT;
1268 token->ident = name;
1269 token->next = *p;
1270 *p = token;
1271 return expansion;
1273 Econcat:
1274 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1275 return NULL;
1276 Earg:
1277 sparse_error(token->pos, "too many instances of argument in body");
1278 return NULL;
1281 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1283 struct token *arglist, *expansion;
1284 struct token *left = token->next;
1285 struct symbol *sym;
1286 struct ident *name;
1287 int ret;
1289 if (token_type(left) != TOKEN_IDENT) {
1290 sparse_error(token->pos, "expected identifier to 'define'");
1291 return 1;
1294 name = left->ident;
1296 arglist = NULL;
1297 expansion = left->next;
1298 if (!expansion->pos.whitespace) {
1299 if (match_op(expansion, '(')) {
1300 arglist = expansion;
1301 expansion = parse_arguments(expansion);
1302 if (!expansion)
1303 return 1;
1304 } else if (!eof_token(expansion)) {
1305 warning(expansion->pos,
1306 "no whitespace before object-like macro body");
1310 expansion = parse_expansion(expansion, arglist, name);
1311 if (!expansion)
1312 return 1;
1314 ret = 1;
1315 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1316 if (sym) {
1317 int clean;
1319 if (attr < sym->attr)
1320 goto out;
1322 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1324 if (token_list_different(sym->expansion, expansion) ||
1325 token_list_different(sym->arglist, arglist)) {
1326 ret = 0;
1327 if ((clean && attr == SYM_ATTR_NORMAL)
1328 || sym->used_in == file_scope) {
1329 warning(left->pos, "preprocessor token %.*s redefined",
1330 name->len, name->name);
1331 info(sym->pos, "this was the original definition");
1333 } else if (clean)
1334 goto out;
1337 if (!sym || sym->scope != file_scope) {
1338 sym = alloc_symbol(left->pos, SYM_NODE);
1339 bind_symbol(sym, name, NS_MACRO);
1340 ret = 0;
1343 if (!ret) {
1344 sym->expansion = expansion;
1345 sym->arglist = arglist;
1346 __free_token(token); /* Free the "define" token, but not the rest of the line */
1349 sym->namespace = NS_MACRO;
1350 sym->used_in = NULL;
1351 sym->attr = attr;
1352 out:
1353 return ret;
1356 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1358 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1361 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1363 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1366 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1368 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1371 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1373 struct token *left = token->next;
1374 struct symbol *sym;
1376 if (token_type(left) != TOKEN_IDENT) {
1377 sparse_error(token->pos, "expected identifier to 'undef'");
1378 return 1;
1381 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1382 if (sym) {
1383 if (attr < sym->attr)
1384 return 1;
1385 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1386 return 1;
1387 } else if (attr <= SYM_ATTR_NORMAL)
1388 return 1;
1390 if (!sym || sym->scope != file_scope) {
1391 sym = alloc_symbol(left->pos, SYM_NODE);
1392 bind_symbol(sym, left->ident, NS_MACRO);
1395 sym->namespace = NS_UNDEF;
1396 sym->used_in = NULL;
1397 sym->attr = attr;
1399 return 1;
1402 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1404 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1407 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1409 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1412 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1414 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1415 free_preprocessor_line(token->next);
1416 token->next = stream->top_if;
1417 stream->top_if = token;
1418 if (false_nesting || true != 1)
1419 false_nesting++;
1420 return 0;
1423 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1425 struct token *next = token->next;
1426 int arg;
1427 if (token_type(next) == TOKEN_IDENT) {
1428 arg = token_defined(next);
1429 } else {
1430 dirty_stream(stream);
1431 if (!false_nesting)
1432 sparse_error(token->pos, "expected preprocessor identifier");
1433 arg = -1;
1435 return preprocessor_if(stream, token, arg);
1438 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1440 struct token *next = token->next;
1441 int arg;
1442 if (token_type(next) == TOKEN_IDENT) {
1443 if (!stream->dirty && !stream->ifndef) {
1444 if (!stream->protect) {
1445 stream->ifndef = token;
1446 stream->protect = next->ident;
1447 } else if (stream->protect == next->ident) {
1448 stream->ifndef = token;
1449 stream->dirty = 1;
1452 arg = !token_defined(next);
1453 } else {
1454 dirty_stream(stream);
1455 if (!false_nesting)
1456 sparse_error(token->pos, "expected preprocessor identifier");
1457 arg = -1;
1460 return preprocessor_if(stream, token, arg);
1463 static const char *show_token_sequence(struct token *token, int quote);
1466 * Expression handling for #if and #elif; it differs from normal expansion
1467 * due to special treatment of "defined".
1469 static int expression_value(struct token **where)
1471 struct expression *expr;
1472 struct token *p;
1473 struct token **list = where, **beginning = NULL;
1474 long long value;
1475 int state = 0;
1477 while (!eof_token(p = scan_next(list))) {
1478 switch (state) {
1479 case 0:
1480 if (token_type(p) != TOKEN_IDENT)
1481 break;
1482 if (p->ident == &defined_ident) {
1483 state = 1;
1484 beginning = list;
1485 break;
1487 if (!expand_one_symbol(list))
1488 continue;
1489 if (token_type(p) != TOKEN_IDENT)
1490 break;
1491 token_type(p) = TOKEN_ZERO_IDENT;
1492 break;
1493 case 1:
1494 if (match_op(p, '(')) {
1495 state = 2;
1496 } else {
1497 state = 0;
1498 replace_with_defined(p);
1499 *beginning = p;
1501 break;
1502 case 2:
1503 if (token_type(p) == TOKEN_IDENT)
1504 state = 3;
1505 else
1506 state = 0;
1507 replace_with_defined(p);
1508 *beginning = p;
1509 break;
1510 case 3:
1511 state = 0;
1512 if (!match_op(p, ')'))
1513 sparse_error(p->pos, "missing ')' after \"defined\"");
1514 *list = p->next;
1515 continue;
1517 list = &p->next;
1520 p = constant_expression(*where, &expr);
1521 if (!eof_token(p))
1522 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p, 0));
1523 value = get_expression_value(expr);
1524 return value != 0;
1527 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1529 int value = 0;
1530 if (!false_nesting)
1531 value = expression_value(&token->next);
1533 dirty_stream(stream);
1534 return preprocessor_if(stream, token, value);
1537 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1539 struct token *top_if = stream->top_if;
1540 end_group(stream);
1542 if (!top_if) {
1543 nesting_error(stream);
1544 sparse_error(token->pos, "unmatched #elif within stream");
1545 return 1;
1548 if (token_type(top_if) == TOKEN_ELSE) {
1549 nesting_error(stream);
1550 sparse_error(token->pos, "#elif after #else");
1551 if (!false_nesting)
1552 false_nesting = 1;
1553 return 1;
1556 dirty_stream(stream);
1557 if (token_type(top_if) != TOKEN_IF)
1558 return 1;
1559 if (false_nesting) {
1560 false_nesting = 0;
1561 if (!expression_value(&token->next))
1562 false_nesting = 1;
1563 } else {
1564 false_nesting = 1;
1565 token_type(top_if) = TOKEN_SKIP_GROUPS;
1567 return 1;
1570 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1572 struct token *top_if = stream->top_if;
1573 end_group(stream);
1575 if (!top_if) {
1576 nesting_error(stream);
1577 sparse_error(token->pos, "unmatched #else within stream");
1578 return 1;
1581 if (token_type(top_if) == TOKEN_ELSE) {
1582 nesting_error(stream);
1583 sparse_error(token->pos, "#else after #else");
1585 if (false_nesting) {
1586 if (token_type(top_if) == TOKEN_IF)
1587 false_nesting = 0;
1588 } else {
1589 false_nesting = 1;
1591 token_type(top_if) = TOKEN_ELSE;
1592 return 1;
1595 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1597 struct token *top_if = stream->top_if;
1598 end_group(stream);
1599 if (!top_if) {
1600 nesting_error(stream);
1601 sparse_error(token->pos, "unmatched #endif in stream");
1602 return 1;
1604 if (false_nesting)
1605 false_nesting--;
1606 stream->top_if = top_if->next;
1607 __free_token(top_if);
1608 return 1;
1611 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1613 warning(token->pos, "%s", show_token_sequence(token->next, 0));
1614 return 1;
1617 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1619 sparse_error(token->pos, "%s", show_token_sequence(token->next, 0));
1620 return 1;
1623 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1626 * Do we have any non-system includes?
1627 * Clear them out if so..
1629 *sys_includepath = NULL;
1630 return 1;
1633 static inline void update_inc_ptrs(const char ***where)
1636 if (*where <= dirafter_includepath) {
1637 dirafter_includepath++;
1638 /* If this was the entry that we prepend, don't
1639 * rise the lower entries, even if they are at
1640 * the same level. */
1641 if (where == &dirafter_includepath)
1642 return;
1644 if (*where <= sys_includepath) {
1645 sys_includepath++;
1646 if (where == &sys_includepath)
1647 return;
1649 if (*where <= isys_includepath) {
1650 isys_includepath++;
1651 if (where == &isys_includepath)
1652 return;
1655 /* angle_includepath is actually never updated, since we
1656 * don't suppport -iquote rught now. May change some day. */
1657 if (*where <= angle_includepath) {
1658 angle_includepath++;
1659 if (where == &angle_includepath)
1660 return;
1664 /* Add a path before 'where' and update the pointers associated with the
1665 * includepath array */
1666 static void add_path_entry(struct token *token, const char *path,
1667 const char ***where)
1669 const char **dst;
1670 const char *next;
1672 /* Need one free entry.. */
1673 if (includepath[INCLUDEPATHS-2])
1674 error_die(token->pos, "too many include path entries");
1676 /* check that this is not a duplicate */
1677 dst = includepath;
1678 while (*dst) {
1679 if (strcmp(*dst, path) == 0)
1680 return;
1681 dst++;
1683 next = path;
1684 dst = *where;
1686 update_inc_ptrs(where);
1689 * Move them all up starting at dst,
1690 * insert the new entry..
1692 do {
1693 const char *tmp = *dst;
1694 *dst = next;
1695 next = tmp;
1696 dst++;
1697 } while (next);
1700 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1702 for (;;) {
1703 token = token->next;
1704 if (eof_token(token))
1705 return 1;
1706 if (token_type(token) != TOKEN_STRING) {
1707 warning(token->pos, "expected path string");
1708 return 1;
1710 add_path_entry(token, token->string->data, &isys_includepath);
1714 static int handle_add_isystem(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 sparse_error(token->pos, "expected path string");
1722 return 1;
1724 add_path_entry(token, token->string->data, &sys_includepath);
1728 static int handle_add_system(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, &dirafter_includepath);
1742 /* Add to end on includepath list - no pointer updates */
1743 static void add_dirafter_entry(struct token *token, const char *path)
1745 const char **dst = includepath;
1747 /* Need one free entry.. */
1748 if (includepath[INCLUDEPATHS-2])
1749 error_die(token->pos, "too many include path entries");
1751 /* Add to the end */
1752 while (*dst)
1753 dst++;
1754 *dst = path;
1755 dst++;
1756 *dst = NULL;
1759 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1761 for (;;) {
1762 token = token->next;
1763 if (eof_token(token))
1764 return 1;
1765 if (token_type(token) != TOKEN_STRING) {
1766 sparse_error(token->pos, "expected path string");
1767 return 1;
1769 add_dirafter_entry(token, token->string->data);
1773 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1776 * -I-
1777 * From info gcc:
1778 * Split the include path. Any directories specified with `-I'
1779 * options before `-I-' are searched only for headers requested with
1780 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1781 * If additional directories are specified with `-I' options after
1782 * the `-I-', those directories are searched for all `#include'
1783 * directives.
1784 * In addition, `-I-' inhibits the use of the directory of the current
1785 * file directory as the first search directory for `#include "FILE"'.
1787 quote_includepath = includepath+1;
1788 angle_includepath = sys_includepath;
1789 return 1;
1793 * We replace "#pragma xxx" with "__pragma__" in the token
1794 * stream. Just as an example.
1796 * We'll just #define that away for now, but the theory here
1797 * is that we can use this to insert arbitrary token sequences
1798 * to turn the pragmas into internal front-end sequences for
1799 * when we actually start caring about them.
1801 * So eventually this will turn into some kind of extended
1802 * __attribute__() like thing, except called __pragma__(xxx).
1804 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1806 struct token *next = *line;
1808 if (match_ident(token->next, &once_ident) && eof_token(token->next->next)) {
1809 stream->once = 1;
1810 return 1;
1812 token->ident = &pragma_ident;
1813 token->pos.newline = 1;
1814 token->pos.whitespace = 1;
1815 token->pos.pos = 1;
1816 *line = token;
1817 token->next = next;
1818 return 0;
1822 * We ignore #line for now.
1824 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1826 return 1;
1829 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1831 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token, 0));
1832 return 1;
1836 static void init_preprocessor(void)
1838 int i;
1839 int stream = init_stream("preprocessor", -1, includepath);
1840 static struct {
1841 const char *name;
1842 int (*handler)(struct stream *, struct token **, struct token *);
1843 } normal[] = {
1844 { "define", handle_define },
1845 { "weak_define", handle_weak_define },
1846 { "strong_define", handle_strong_define },
1847 { "undef", handle_undef },
1848 { "strong_undef", handle_strong_undef },
1849 { "warning", handle_warning },
1850 { "error", handle_error },
1851 { "include", handle_include },
1852 { "include_next", handle_include_next },
1853 { "pragma", handle_pragma },
1854 { "line", handle_line },
1856 // our internal preprocessor tokens
1857 { "nostdinc", handle_nostdinc },
1858 { "add_include", handle_add_include },
1859 { "add_isystem", handle_add_isystem },
1860 { "add_system", handle_add_system },
1861 { "add_dirafter", handle_add_dirafter },
1862 { "split_include", handle_split_include },
1863 { "argv_include", handle_argv_include },
1864 }, special[] = {
1865 { "ifdef", handle_ifdef },
1866 { "ifndef", handle_ifndef },
1867 { "else", handle_else },
1868 { "endif", handle_endif },
1869 { "if", handle_if },
1870 { "elif", handle_elif },
1873 for (i = 0; i < ARRAY_SIZE(normal); i++) {
1874 struct symbol *sym;
1875 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1876 sym->handler = normal[i].handler;
1877 sym->normal = 1;
1879 for (i = 0; i < ARRAY_SIZE(special); i++) {
1880 struct symbol *sym;
1881 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1882 sym->handler = special[i].handler;
1883 sym->normal = 0;
1888 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1890 int (*handler)(struct stream *, struct token **, struct token *);
1891 struct token *token = start->next;
1892 int is_normal = 1;
1894 if (eof_token(token))
1895 return;
1897 if (token_type(token) == TOKEN_IDENT) {
1898 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1899 if (sym) {
1900 handler = sym->handler;
1901 is_normal = sym->normal;
1902 } else {
1903 handler = handle_nondirective;
1905 } else if (token_type(token) == TOKEN_NUMBER) {
1906 handler = handle_line;
1907 } else {
1908 handler = handle_nondirective;
1911 if (is_normal) {
1912 dirty_stream(stream);
1913 if (false_nesting)
1914 goto out;
1916 if (!handler(stream, line, token)) /* all set */
1917 return;
1919 out:
1920 free_preprocessor_line(token);
1923 static void preprocessor_line(struct stream *stream, struct token **line)
1925 struct token *start = *line, *next;
1926 struct token **tp = &start->next;
1928 for (;;) {
1929 next = *tp;
1930 if (next->pos.newline)
1931 break;
1932 tp = &next->next;
1934 *line = next;
1935 *tp = &eof_token_entry;
1936 handle_preprocessor_line(stream, line, start);
1939 static void do_preprocess(struct token **list)
1941 struct token *next;
1943 while (!eof_token(next = scan_next(list))) {
1944 struct stream *stream = input_streams + next->pos.stream;
1946 if (next->pos.newline && match_op(next, '#')) {
1947 if (!next->pos.noexpand) {
1948 preprocessor_line(stream, list);
1949 __free_token(next); /* Free the '#' token */
1950 continue;
1954 switch (token_type(next)) {
1955 case TOKEN_STREAMEND:
1956 if (stream->top_if) {
1957 nesting_error(stream);
1958 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1959 stream->top_if = NULL;
1960 false_nesting = 0;
1962 if (!stream->dirty)
1963 stream->constant = CONSTANT_FILE_YES;
1964 *list = next->next;
1965 continue;
1966 case TOKEN_STREAMBEGIN:
1967 *list = next->next;
1968 continue;
1970 default:
1971 dirty_stream(stream);
1972 if (false_nesting) {
1973 *list = next->next;
1974 __free_token(next);
1975 continue;
1978 if (token_type(next) != TOKEN_IDENT ||
1979 expand_one_symbol(list))
1980 list = &next->next;
1985 struct token * preprocess(struct token *token)
1987 preprocessing = 1;
1988 init_preprocessor();
1989 do_preprocess(&token);
1991 // Drop all expressions from preprocessing, they're not used any more.
1992 // This is not true when we have multiple files, though ;/
1993 // clear_expression_alloc();
1994 preprocessing = 0;
1996 return token;