evaluate: split out implementation of compatible_assignment_types
[smatch.git] / pre-process.c
blob1aa3d2c4852e0eb255c47081e8829cd150cf0142
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 sym->used_in = file_scope;
168 return expand(list, sym);
170 if (token->ident == &__LINE___ident) {
171 replace_with_integer(token, token->pos.line);
172 } else if (token->ident == &__FILE___ident) {
173 replace_with_string(token, stream_name(token->pos.stream));
174 } else if (token->ident == &__DATE___ident) {
175 if (!t)
176 time(&t);
177 strftime(buffer, 12, "%b %e %Y", localtime(&t));
178 replace_with_string(token, buffer);
179 } else if (token->ident == &__TIME___ident) {
180 if (!t)
181 time(&t);
182 strftime(buffer, 9, "%T", localtime(&t));
183 replace_with_string(token, buffer);
185 return 1;
188 static inline struct token *scan_next(struct token **where)
190 struct token *token = *where;
191 if (token_type(token) != TOKEN_UNTAINT)
192 return token;
193 do {
194 token->ident->tainted = 0;
195 token = token->next;
196 } while (token_type(token) == TOKEN_UNTAINT);
197 *where = token;
198 return token;
201 static void expand_list(struct token **list)
203 struct token *next;
204 while (!eof_token(next = scan_next(list))) {
205 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
206 list = &next->next;
210 static void preprocessor_line(struct stream *stream, struct token **line);
212 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
214 struct stream *stream = input_streams + prev->pos.stream;
215 struct token **p = &prev->next;
216 struct token *next;
217 int nesting = 0;
219 while (!eof_token(next = scan_next(p))) {
220 if (next->pos.newline && match_op(next, '#')) {
221 if (!next->pos.noexpand) {
222 sparse_error(next->pos,
223 "directive in argument list");
224 preprocessor_line(stream, p);
225 __free_token(next); /* Free the '#' token */
226 continue;
229 switch (token_type(next)) {
230 case TOKEN_STREAMEND:
231 case TOKEN_STREAMBEGIN:
232 *p = &eof_token_entry;
233 return next;
235 if (false_nesting) {
236 *p = next->next;
237 __free_token(next);
238 continue;
240 if (match_op(next, '(')) {
241 nesting++;
242 } else if (match_op(next, ')')) {
243 if (!nesting--)
244 break;
245 } else if (match_op(next, ',') && !nesting && !vararg) {
246 break;
248 next->pos.stream = pos->stream;
249 next->pos.line = pos->line;
250 next->pos.pos = pos->pos;
251 p = &next->next;
253 *p = &eof_token_entry;
254 return next;
258 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
261 struct arg {
262 struct token *arg;
263 struct token *expanded;
264 struct token *str;
265 int n_normal;
266 int n_quoted;
267 int n_str;
270 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
272 int wanted = arglist->count.normal;
273 struct token *next = NULL;
274 int count = 0;
276 arglist = arglist->next; /* skip counter */
278 if (!wanted) {
279 next = collect_arg(start, 0, &what->pos);
280 if (eof_token(next))
281 goto Eclosing;
282 if (!eof_token(start->next) || !match_op(next, ')')) {
283 count++;
284 goto Emany;
286 } else {
287 for (count = 0; count < wanted; count++) {
288 struct argcount *p = &arglist->next->count;
289 next = collect_arg(start, p->vararg, &what->pos);
290 arglist = arglist->next->next;
291 if (eof_token(next))
292 goto Eclosing;
293 args[count].arg = start->next;
294 args[count].n_normal = p->normal;
295 args[count].n_quoted = p->quoted;
296 args[count].n_str = p->str;
297 if (match_op(next, ')')) {
298 count++;
299 break;
301 start = next;
303 if (count == wanted && !match_op(next, ')'))
304 goto Emany;
305 if (count == wanted - 1) {
306 struct argcount *p = &arglist->next->count;
307 if (!p->vararg)
308 goto Efew;
309 args[count].arg = NULL;
310 args[count].n_normal = p->normal;
311 args[count].n_quoted = p->quoted;
312 args[count].n_str = p->str;
314 if (count < wanted - 1)
315 goto Efew;
317 what->next = next->next;
318 return 1;
320 Efew:
321 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
322 show_token(what), wanted, count);
323 goto out;
324 Emany:
325 while (match_op(next, ',')) {
326 next = collect_arg(next, 0, &what->pos);
327 count++;
329 if (eof_token(next))
330 goto Eclosing;
331 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
332 show_token(what), count, wanted);
333 goto out;
334 Eclosing:
335 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
336 show_token(what));
337 out:
338 what->next = next->next;
339 return 0;
342 static struct token *dup_list(struct token *list)
344 struct token *res = NULL;
345 struct token **p = &res;
347 while (!eof_token(list)) {
348 struct token *newtok = __alloc_token(0);
349 *newtok = *list;
350 *p = newtok;
351 p = &newtok->next;
352 list = list->next;
354 return res;
357 static const char *show_token_sequence(struct token *token, int quote)
359 static char buffer[MAX_STRING];
360 char *ptr = buffer;
361 int whitespace = 0;
363 if (!token && !quote)
364 return "<none>";
365 while (!eof_token(token)) {
366 const char *val = quote ? quote_token(token) : show_token(token);
367 int len = strlen(val);
369 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
370 sparse_error(token->pos, "too long token expansion");
371 break;
374 if (whitespace)
375 *ptr++ = ' ';
376 memcpy(ptr, val, len);
377 ptr += len;
378 token = token->next;
379 whitespace = token->pos.whitespace;
381 *ptr = 0;
382 return buffer;
385 static struct token *stringify(struct token *arg)
387 const char *s = show_token_sequence(arg, 1);
388 int size = strlen(s)+1;
389 struct token *token = __alloc_token(0);
390 struct string *string = __alloc_string(size);
392 memcpy(string->data, s, size);
393 string->length = size;
394 token->pos = arg->pos;
395 token_type(token) = TOKEN_STRING;
396 token->string = string;
397 token->next = &eof_token_entry;
398 return token;
401 static void expand_arguments(int count, struct arg *args)
403 int i;
404 for (i = 0; i < count; i++) {
405 struct token *arg = args[i].arg;
406 if (!arg)
407 arg = &eof_token_entry;
408 if (args[i].n_str)
409 args[i].str = stringify(arg);
410 if (args[i].n_normal) {
411 if (!args[i].n_quoted) {
412 args[i].expanded = arg;
413 args[i].arg = NULL;
414 } else if (eof_token(arg)) {
415 args[i].expanded = arg;
416 } else {
417 args[i].expanded = dup_list(arg);
419 expand_list(&args[i].expanded);
425 * Possibly valid combinations:
426 * - ident + ident -> ident
427 * - ident + number -> ident unless number contains '.', '+' or '-'.
428 * - 'L' + char constant -> wide char constant
429 * - 'L' + string literal -> wide string literal
430 * - number + number -> number
431 * - number + ident -> number
432 * - number + '.' -> number
433 * - number + '+' or '-' -> number, if number used to end on [eEpP].
434 * - '.' + number -> number, if number used to start with a digit.
435 * - special + special -> either special or an error.
437 static enum token_type combine(struct token *left, struct token *right, char *p)
439 int len;
440 enum token_type t1 = token_type(left), t2 = token_type(right);
442 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
443 return TOKEN_ERROR;
445 if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
446 if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
447 return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
448 if (t2 == TOKEN_STRING)
449 return TOKEN_WIDE_STRING;
452 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
453 return TOKEN_ERROR;
455 strcpy(p, show_token(left));
456 strcat(p, show_token(right));
457 len = strlen(p);
459 if (len >= 256)
460 return TOKEN_ERROR;
462 if (t1 == TOKEN_IDENT) {
463 if (t2 == TOKEN_SPECIAL)
464 return TOKEN_ERROR;
465 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
466 return TOKEN_ERROR;
467 return TOKEN_IDENT;
470 if (t1 == TOKEN_NUMBER) {
471 if (t2 == TOKEN_SPECIAL) {
472 switch (right->special) {
473 case '.':
474 break;
475 case '+': case '-':
476 if (strchr("eEpP", p[len - 2]))
477 break;
478 default:
479 return TOKEN_ERROR;
482 return TOKEN_NUMBER;
485 if (p[0] == '.' && isdigit((unsigned char)p[1]))
486 return TOKEN_NUMBER;
488 return TOKEN_SPECIAL;
491 static int merge(struct token *left, struct token *right)
493 static char buffer[512];
494 enum token_type res = combine(left, right, buffer);
495 int n;
497 switch (res) {
498 case TOKEN_IDENT:
499 left->ident = built_in_ident(buffer);
500 left->pos.noexpand = 0;
501 return 1;
503 case TOKEN_NUMBER: {
504 char *number = __alloc_bytes(strlen(buffer) + 1);
505 memcpy(number, buffer, strlen(buffer) + 1);
506 token_type(left) = TOKEN_NUMBER; /* could be . + num */
507 left->number = number;
508 return 1;
511 case TOKEN_SPECIAL:
512 if (buffer[2] && buffer[3])
513 break;
514 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
515 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
516 left->special = n;
517 return 1;
520 break;
522 case TOKEN_WIDE_CHAR:
523 case TOKEN_WIDE_STRING:
524 token_type(left) = res;
525 left->pos.noexpand = 0;
526 left->string = right->string;
527 return 1;
529 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
530 token_type(left) = res;
531 left->pos.noexpand = 0;
532 memcpy(left->embedded, right->embedded, 4);
533 return 1;
535 default:
538 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
539 return 0;
542 static struct token *dup_token(struct token *token, struct position *streampos)
544 struct token *alloc = alloc_token(streampos);
545 token_type(alloc) = token_type(token);
546 alloc->pos.newline = token->pos.newline;
547 alloc->pos.whitespace = token->pos.whitespace;
548 alloc->number = token->number;
549 alloc->pos.noexpand = token->pos.noexpand;
550 return alloc;
553 static struct token **copy(struct token **where, struct token *list, int *count)
555 int need_copy = --*count;
556 while (!eof_token(list)) {
557 struct token *token;
558 if (need_copy)
559 token = dup_token(list, &list->pos);
560 else
561 token = list;
562 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
563 token->pos.noexpand = 1;
564 *where = token;
565 where = &token->next;
566 list = list->next;
568 *where = &eof_token_entry;
569 return where;
572 static int handle_kludge(struct token **p, struct arg *args)
574 struct token *t = (*p)->next->next;
575 while (1) {
576 struct arg *v = &args[t->argnum];
577 if (token_type(t->next) != TOKEN_CONCAT) {
578 if (v->arg) {
579 /* ignore the first ## */
580 *p = (*p)->next;
581 return 0;
583 /* skip the entire thing */
584 *p = t;
585 return 1;
587 if (v->arg && !eof_token(v->arg))
588 return 0; /* no magic */
589 t = t->next->next;
593 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
595 struct position *base_pos = &(*list)->pos;
596 int *count;
597 enum {Normal, Placeholder, Concat} state = Normal;
599 for (; !eof_token(body); body = body->next) {
600 struct token *added, *arg;
601 struct token **tail;
602 struct token *t;
604 switch (token_type(body)) {
605 case TOKEN_GNU_KLUDGE:
607 * GNU kludge: if we had <comma>##<vararg>, behaviour
608 * depends on whether we had enough arguments to have
609 * a vararg. If we did, ## is just ignored. Otherwise
610 * both , and ## are ignored. Worse, there can be
611 * an arbitrary number of ##<arg> in between; if all of
612 * those are empty, we act as if they hadn't been there,
613 * otherwise we act as if the kludge didn't exist.
615 t = body;
616 if (handle_kludge(&body, args)) {
617 if (state == Concat)
618 state = Normal;
619 else
620 state = Placeholder;
621 continue;
623 added = dup_token(t, base_pos);
624 token_type(added) = TOKEN_SPECIAL;
625 tail = &added->next;
626 break;
628 case TOKEN_STR_ARGUMENT:
629 arg = args[body->argnum].str;
630 count = &args[body->argnum].n_str;
631 goto copy_arg;
633 case TOKEN_QUOTED_ARGUMENT:
634 arg = args[body->argnum].arg;
635 count = &args[body->argnum].n_quoted;
636 if (!arg || eof_token(arg)) {
637 if (state == Concat)
638 state = Normal;
639 else
640 state = Placeholder;
641 continue;
643 goto copy_arg;
645 case TOKEN_MACRO_ARGUMENT:
646 arg = args[body->argnum].expanded;
647 count = &args[body->argnum].n_normal;
648 if (eof_token(arg)) {
649 state = Normal;
650 continue;
652 copy_arg:
653 tail = copy(&added, arg, count);
654 added->pos.newline = body->pos.newline;
655 added->pos.whitespace = body->pos.whitespace;
656 break;
658 case TOKEN_CONCAT:
659 if (state == Placeholder)
660 state = Normal;
661 else
662 state = Concat;
663 continue;
665 case TOKEN_IDENT:
666 added = dup_token(body, base_pos);
667 if (added->ident->tainted)
668 added->pos.noexpand = 1;
669 tail = &added->next;
670 break;
672 default:
673 added = dup_token(body, base_pos);
674 tail = &added->next;
675 break;
679 * if we got to doing real concatenation, we already have
680 * added something into the list, so containing_token() is OK.
682 if (state == Concat && merge(containing_token(list), added)) {
683 *list = added->next;
684 if (tail != &added->next)
685 list = tail;
686 } else {
687 *list = added;
688 list = tail;
690 state = Normal;
692 *list = &eof_token_entry;
693 return list;
696 static int expand(struct token **list, struct symbol *sym)
698 struct token *last;
699 struct token *token = *list;
700 struct ident *expanding = token->ident;
701 struct token **tail;
702 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
703 struct arg args[nargs];
705 if (expanding->tainted) {
706 token->pos.noexpand = 1;
707 return 1;
710 if (sym->arglist) {
711 if (!match_op(scan_next(&token->next), '('))
712 return 1;
713 if (!collect_arguments(token->next, sym->arglist, args, token))
714 return 1;
715 expand_arguments(nargs, args);
718 expanding->tainted = 1;
720 last = token->next;
721 tail = substitute(list, sym->expansion, args);
723 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
724 * We still can lose the newline flag if the sucker expands to nothing,
725 * but the price of dealing with that is probably too high (we'd need
726 * to collect the flags during scan_next())
728 (*list)->pos.newline = token->pos.newline;
729 (*list)->pos.whitespace = token->pos.whitespace;
730 *tail = last;
732 return 0;
735 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
737 static char buffer[256];
738 char *ptr = buffer;
740 while (!eof_token(token) && !match_op(token, endop)) {
741 int len;
742 const char *val = token->string->data;
743 if (token_type(token) != TOKEN_STRING)
744 val = show_token(token);
745 len = strlen(val);
746 memcpy(ptr, val, len);
747 ptr += len;
748 token = token->next;
750 *ptr = 0;
751 if (endop && !match_op(token, endop))
752 sparse_error(start->pos, "expected '>' at end of filename");
753 return buffer;
756 static int already_tokenized(const char *path)
758 int stream, next;
760 for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
761 struct stream *s = input_streams + stream;
763 next = s->next_stream;
764 if (s->once) {
765 if (strcmp(path, s->name))
766 continue;
767 return 1;
769 if (s->constant != CONSTANT_FILE_YES)
770 continue;
771 if (strcmp(path, s->name))
772 continue;
773 if (s->protect && !lookup_macro(s->protect))
774 continue;
775 return 1;
777 return 0;
780 /* Handle include of header files.
781 * The relevant options are made compatible with gcc. The only options that
782 * are not supported is -withprefix and friends.
784 * Three set of include paths are known:
785 * quote_includepath: Path to search when using #include "file.h"
786 * angle_includepath: Paths to search when using #include <file.h>
787 * isys_includepath: Paths specified with -isystem, come before the
788 * built-in system include paths. Gcc would suppress
789 * warnings from system headers. Here we separate
790 * them from the angle_ ones to keep search ordering.
792 * sys_includepath: Built-in include paths.
793 * dirafter_includepath Paths added with -dirafter.
795 * The above is implemented as one array with pointers
796 * +--------------+
797 * quote_includepath ---> | |
798 * +--------------+
799 * | |
800 * +--------------+
801 * angle_includepath ---> | |
802 * +--------------+
803 * isys_includepath ---> | |
804 * +--------------+
805 * sys_includepath ---> | |
806 * +--------------+
807 * dirafter_includepath -> | |
808 * +--------------+
810 * -I dir insert dir just before isys_includepath and move the rest
811 * -I- makes all dirs specified with -I before to quote dirs only and
812 * angle_includepath is set equal to isys_includepath.
813 * -nostdinc removes all sys dirs by storing NULL in entry pointed
814 * to by * sys_includepath. Note that this will reset all dirs built-in
815 * and added before -nostdinc by -isystem and -idirafter.
816 * -isystem dir adds dir where isys_includepath points adding this dir as
817 * first systemdir
818 * -idirafter dir adds dir to the end of the list
821 static void set_stream_include_path(struct stream *stream)
823 const char *path = stream->path;
824 if (!path) {
825 const char *p = strrchr(stream->name, '/');
826 path = "";
827 if (p) {
828 int len = p - stream->name + 1;
829 char *m = malloc(len+1);
830 /* This includes the final "/" */
831 memcpy(m, stream->name, len);
832 m[len] = 0;
833 path = m;
835 stream->path = path;
837 includepath[0] = path;
840 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
842 int fd;
843 int plen = strlen(path);
844 static char fullname[PATH_MAX];
846 memcpy(fullname, path, plen);
847 if (plen && path[plen-1] != '/') {
848 fullname[plen] = '/';
849 plen++;
851 memcpy(fullname+plen, filename, flen);
852 if (already_tokenized(fullname))
853 return 1;
854 fd = open(fullname, O_RDONLY);
855 if (fd >= 0) {
856 char * streamname = __alloc_bytes(plen + flen);
857 memcpy(streamname, fullname, plen + flen);
858 *where = tokenize(streamname, fd, *where, next_path);
859 close(fd);
860 return 1;
862 return 0;
865 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
867 const char *path;
869 while ((path = *pptr++) != NULL) {
870 if (!try_include(path, filename, flen, list, pptr))
871 continue;
872 return 1;
874 return 0;
877 static int free_preprocessor_line(struct token *token)
879 while (token_type(token) != TOKEN_EOF) {
880 struct token *free = token;
881 token = token->next;
882 __free_token(free);
884 return 1;
887 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how)
889 const char *filename;
890 struct token *next;
891 const char **path;
892 int expect;
893 int flen;
895 next = token->next;
896 expect = '>';
897 if (!match_op(next, '<')) {
898 expand_list(&token->next);
899 expect = 0;
900 next = token;
901 if (match_op(token->next, '<')) {
902 next = token->next;
903 expect = '>';
907 token = next->next;
908 filename = token_name_sequence(token, expect, token);
909 flen = strlen(filename) + 1;
911 /* Absolute path? */
912 if (filename[0] == '/') {
913 if (try_include("", filename, flen, list, includepath))
914 return 0;
915 goto out;
918 switch (how) {
919 case 1:
920 path = stream->next_path;
921 break;
922 case 2:
923 includepath[0] = "";
924 path = includepath;
925 break;
926 default:
927 /* Dir of input file is first dir to search for quoted includes */
928 set_stream_include_path(stream);
929 path = expect ? angle_includepath : quote_includepath;
930 break;
932 /* Check the standard include paths.. */
933 if (do_include_path(path, list, token, filename, flen))
934 return 0;
935 out:
936 error_die(token->pos, "unable to open '%s'", filename);
939 static int handle_include(struct stream *stream, struct token **list, struct token *token)
941 return handle_include_path(stream, list, token, 0);
944 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
946 return handle_include_path(stream, list, token, 1);
949 static int handle_argv_include(struct stream *stream, struct token **list, struct token *token)
951 return handle_include_path(stream, list, token, 2);
954 static int token_different(struct token *t1, struct token *t2)
956 int different;
958 if (token_type(t1) != token_type(t2))
959 return 1;
961 switch (token_type(t1)) {
962 case TOKEN_IDENT:
963 different = t1->ident != t2->ident;
964 break;
965 case TOKEN_ARG_COUNT:
966 case TOKEN_UNTAINT:
967 case TOKEN_CONCAT:
968 case TOKEN_GNU_KLUDGE:
969 different = 0;
970 break;
971 case TOKEN_NUMBER:
972 different = strcmp(t1->number, t2->number);
973 break;
974 case TOKEN_SPECIAL:
975 different = t1->special != t2->special;
976 break;
977 case TOKEN_MACRO_ARGUMENT:
978 case TOKEN_QUOTED_ARGUMENT:
979 case TOKEN_STR_ARGUMENT:
980 different = t1->argnum != t2->argnum;
981 break;
982 case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
983 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
984 different = memcmp(t1->embedded, t2->embedded, 4);
985 break;
986 case TOKEN_CHAR:
987 case TOKEN_WIDE_CHAR:
988 case TOKEN_STRING:
989 case TOKEN_WIDE_STRING: {
990 struct string *s1, *s2;
992 s1 = t1->string;
993 s2 = t2->string;
994 different = 1;
995 if (s1->length != s2->length)
996 break;
997 different = memcmp(s1->data, s2->data, s1->length);
998 break;
1000 default:
1001 different = 1;
1002 break;
1004 return different;
1007 static int token_list_different(struct token *list1, struct token *list2)
1009 for (;;) {
1010 if (list1 == list2)
1011 return 0;
1012 if (!list1 || !list2)
1013 return 1;
1014 if (token_different(list1, list2))
1015 return 1;
1016 list1 = list1->next;
1017 list2 = list2->next;
1021 static inline void set_arg_count(struct token *token)
1023 token_type(token) = TOKEN_ARG_COUNT;
1024 token->count.normal = token->count.quoted =
1025 token->count.str = token->count.vararg = 0;
1028 static struct token *parse_arguments(struct token *list)
1030 struct token *arg = list->next, *next = list;
1031 struct argcount *count = &list->count;
1033 set_arg_count(list);
1035 if (match_op(arg, ')')) {
1036 next = arg->next;
1037 list->next = &eof_token_entry;
1038 return next;
1041 while (token_type(arg) == TOKEN_IDENT) {
1042 if (arg->ident == &__VA_ARGS___ident)
1043 goto Eva_args;
1044 if (!++count->normal)
1045 goto Eargs;
1046 next = arg->next;
1048 if (match_op(next, ',')) {
1049 set_arg_count(next);
1050 arg = next->next;
1051 continue;
1054 if (match_op(next, ')')) {
1055 set_arg_count(next);
1056 next = next->next;
1057 arg->next->next = &eof_token_entry;
1058 return next;
1061 /* normal cases are finished here */
1063 if (match_op(next, SPECIAL_ELLIPSIS)) {
1064 if (match_op(next->next, ')')) {
1065 set_arg_count(next);
1066 next->count.vararg = 1;
1067 next = next->next;
1068 arg->next->next = &eof_token_entry;
1069 return next->next;
1072 arg = next;
1073 goto Enotclosed;
1076 if (eof_token(next)) {
1077 goto Enotclosed;
1078 } else {
1079 arg = next;
1080 goto Ebadstuff;
1084 if (match_op(arg, SPECIAL_ELLIPSIS)) {
1085 next = arg->next;
1086 token_type(arg) = TOKEN_IDENT;
1087 arg->ident = &__VA_ARGS___ident;
1088 if (!match_op(next, ')'))
1089 goto Enotclosed;
1090 if (!++count->normal)
1091 goto Eargs;
1092 set_arg_count(next);
1093 next->count.vararg = 1;
1094 next = next->next;
1095 arg->next->next = &eof_token_entry;
1096 return next;
1099 if (eof_token(arg)) {
1100 arg = next;
1101 goto Enotclosed;
1103 if (match_op(arg, ','))
1104 goto Emissing;
1105 else
1106 goto Ebadstuff;
1109 Emissing:
1110 sparse_error(arg->pos, "parameter name missing");
1111 return NULL;
1112 Ebadstuff:
1113 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
1114 show_token(arg));
1115 return NULL;
1116 Enotclosed:
1117 sparse_error(arg->pos, "missing ')' in macro parameter list");
1118 return NULL;
1119 Eva_args:
1120 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1121 return NULL;
1122 Eargs:
1123 sparse_error(arg->pos, "too many arguments in macro definition");
1124 return NULL;
1127 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1129 struct ident *ident = token->ident;
1130 int nr;
1132 if (!arglist || token_type(token) != TOKEN_IDENT)
1133 return 0;
1135 arglist = arglist->next;
1137 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1138 if (arglist->ident == ident) {
1139 struct argcount *count = &arglist->next->count;
1140 int n;
1142 token->argnum = nr;
1143 token_type(token) = type;
1144 switch (type) {
1145 case TOKEN_MACRO_ARGUMENT:
1146 n = ++count->normal;
1147 break;
1148 case TOKEN_QUOTED_ARGUMENT:
1149 n = ++count->quoted;
1150 break;
1151 default:
1152 n = ++count->str;
1154 if (n)
1155 return count->vararg ? 2 : 1;
1157 * XXX - need saner handling of that
1158 * (>= 1024 instances of argument)
1160 token_type(token) = TOKEN_ERROR;
1161 return -1;
1164 return 0;
1167 static struct token *handle_hash(struct token **p, struct token *arglist)
1169 struct token *token = *p;
1170 if (arglist) {
1171 struct token *next = token->next;
1172 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1173 goto Equote;
1174 next->pos.whitespace = token->pos.whitespace;
1175 __free_token(token);
1176 token = *p = next;
1177 } else {
1178 token->pos.noexpand = 1;
1180 return token;
1182 Equote:
1183 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1184 return NULL;
1187 /* token->next is ## */
1188 static struct token *handle_hashhash(struct token *token, struct token *arglist)
1190 struct token *last = token;
1191 struct token *concat;
1192 int state = match_op(token, ',');
1194 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1196 while (1) {
1197 struct token *t;
1198 int is_arg;
1200 /* eat duplicate ## */
1201 concat = token->next;
1202 while (match_op(t = concat->next, SPECIAL_HASHHASH)) {
1203 token->next = t;
1204 __free_token(concat);
1205 concat = t;
1207 token_type(concat) = TOKEN_CONCAT;
1209 if (eof_token(t))
1210 goto Econcat;
1212 if (match_op(t, '#')) {
1213 t = handle_hash(&concat->next, arglist);
1214 if (!t)
1215 return NULL;
1218 is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
1220 if (state == 1 && is_arg) {
1221 state = is_arg;
1222 } else {
1223 last = t;
1224 state = match_op(t, ',');
1227 token = t;
1228 if (!match_op(token->next, SPECIAL_HASHHASH))
1229 break;
1231 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1232 if (state == 2)
1233 token_type(last) = TOKEN_GNU_KLUDGE;
1234 return token;
1236 Econcat:
1237 sparse_error(concat->pos, "'##' cannot appear at the ends of macro expansion");
1238 return NULL;
1241 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1243 struct token *token = expansion;
1244 struct token **p;
1246 if (match_op(token, SPECIAL_HASHHASH))
1247 goto Econcat;
1249 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1250 if (match_op(token, '#')) {
1251 token = handle_hash(p, arglist);
1252 if (!token)
1253 return NULL;
1255 if (match_op(token->next, SPECIAL_HASHHASH)) {
1256 token = handle_hashhash(token, arglist);
1257 if (!token)
1258 return NULL;
1259 } else {
1260 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1262 if (token_type(token) == TOKEN_ERROR)
1263 goto Earg;
1265 token = alloc_token(&expansion->pos);
1266 token_type(token) = TOKEN_UNTAINT;
1267 token->ident = name;
1268 token->next = *p;
1269 *p = token;
1270 return expansion;
1272 Econcat:
1273 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1274 return NULL;
1275 Earg:
1276 sparse_error(token->pos, "too many instances of argument in body");
1277 return NULL;
1280 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1282 struct token *arglist, *expansion;
1283 struct token *left = token->next;
1284 struct symbol *sym;
1285 struct ident *name;
1286 int ret;
1288 if (token_type(left) != TOKEN_IDENT) {
1289 sparse_error(token->pos, "expected identifier to 'define'");
1290 return 1;
1293 name = left->ident;
1295 arglist = NULL;
1296 expansion = left->next;
1297 if (!expansion->pos.whitespace) {
1298 if (match_op(expansion, '(')) {
1299 arglist = expansion;
1300 expansion = parse_arguments(expansion);
1301 if (!expansion)
1302 return 1;
1303 } else if (!eof_token(expansion)) {
1304 warning(expansion->pos,
1305 "no whitespace before object-like macro body");
1309 expansion = parse_expansion(expansion, arglist, name);
1310 if (!expansion)
1311 return 1;
1313 ret = 1;
1314 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1315 if (sym) {
1316 int clean;
1318 if (attr < sym->attr)
1319 goto out;
1321 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1323 if (token_list_different(sym->expansion, expansion) ||
1324 token_list_different(sym->arglist, arglist)) {
1325 ret = 0;
1326 if ((clean && attr == SYM_ATTR_NORMAL)
1327 || sym->used_in == file_scope) {
1328 warning(left->pos, "preprocessor token %.*s redefined",
1329 name->len, name->name);
1330 info(sym->pos, "this was the original definition");
1332 } else if (clean)
1333 goto out;
1336 if (!sym || sym->scope != file_scope) {
1337 sym = alloc_symbol(left->pos, SYM_NODE);
1338 bind_symbol(sym, name, NS_MACRO);
1339 ret = 0;
1342 if (!ret) {
1343 sym->expansion = expansion;
1344 sym->arglist = arglist;
1345 __free_token(token); /* Free the "define" token, but not the rest of the line */
1348 sym->namespace = NS_MACRO;
1349 sym->used_in = NULL;
1350 sym->attr = attr;
1351 out:
1352 return ret;
1355 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1357 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1360 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1362 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1365 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1367 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1370 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1372 struct token *left = token->next;
1373 struct symbol *sym;
1375 if (token_type(left) != TOKEN_IDENT) {
1376 sparse_error(token->pos, "expected identifier to 'undef'");
1377 return 1;
1380 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1381 if (sym) {
1382 if (attr < sym->attr)
1383 return 1;
1384 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1385 return 1;
1386 } else if (attr <= SYM_ATTR_NORMAL)
1387 return 1;
1389 if (!sym || sym->scope != file_scope) {
1390 sym = alloc_symbol(left->pos, SYM_NODE);
1391 bind_symbol(sym, left->ident, NS_MACRO);
1394 sym->namespace = NS_UNDEF;
1395 sym->used_in = NULL;
1396 sym->attr = attr;
1398 return 1;
1401 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1403 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1406 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1408 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1411 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1413 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1414 free_preprocessor_line(token->next);
1415 token->next = stream->top_if;
1416 stream->top_if = token;
1417 if (false_nesting || true != 1)
1418 false_nesting++;
1419 return 0;
1422 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1424 struct token *next = token->next;
1425 int arg;
1426 if (token_type(next) == TOKEN_IDENT) {
1427 arg = token_defined(next);
1428 } else {
1429 dirty_stream(stream);
1430 if (!false_nesting)
1431 sparse_error(token->pos, "expected preprocessor identifier");
1432 arg = -1;
1434 return preprocessor_if(stream, token, arg);
1437 static int handle_ifndef(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 if (!stream->dirty && !stream->ifndef) {
1443 if (!stream->protect) {
1444 stream->ifndef = token;
1445 stream->protect = next->ident;
1446 } else if (stream->protect == next->ident) {
1447 stream->ifndef = token;
1448 stream->dirty = 1;
1451 arg = !token_defined(next);
1452 } else {
1453 dirty_stream(stream);
1454 if (!false_nesting)
1455 sparse_error(token->pos, "expected preprocessor identifier");
1456 arg = -1;
1459 return preprocessor_if(stream, token, arg);
1462 static const char *show_token_sequence(struct token *token, int quote);
1465 * Expression handling for #if and #elif; it differs from normal expansion
1466 * due to special treatment of "defined".
1468 static int expression_value(struct token **where)
1470 struct expression *expr;
1471 struct token *p;
1472 struct token **list = where, **beginning = NULL;
1473 long long value;
1474 int state = 0;
1476 while (!eof_token(p = scan_next(list))) {
1477 switch (state) {
1478 case 0:
1479 if (token_type(p) != TOKEN_IDENT)
1480 break;
1481 if (p->ident == &defined_ident) {
1482 state = 1;
1483 beginning = list;
1484 break;
1486 if (!expand_one_symbol(list))
1487 continue;
1488 if (token_type(p) != TOKEN_IDENT)
1489 break;
1490 token_type(p) = TOKEN_ZERO_IDENT;
1491 break;
1492 case 1:
1493 if (match_op(p, '(')) {
1494 state = 2;
1495 } else {
1496 state = 0;
1497 replace_with_defined(p);
1498 *beginning = p;
1500 break;
1501 case 2:
1502 if (token_type(p) == TOKEN_IDENT)
1503 state = 3;
1504 else
1505 state = 0;
1506 replace_with_defined(p);
1507 *beginning = p;
1508 break;
1509 case 3:
1510 state = 0;
1511 if (!match_op(p, ')'))
1512 sparse_error(p->pos, "missing ')' after \"defined\"");
1513 *list = p->next;
1514 continue;
1516 list = &p->next;
1519 p = constant_expression(*where, &expr);
1520 if (!eof_token(p))
1521 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p, 0));
1522 value = get_expression_value(expr);
1523 return value != 0;
1526 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1528 int value = 0;
1529 if (!false_nesting)
1530 value = expression_value(&token->next);
1532 dirty_stream(stream);
1533 return preprocessor_if(stream, token, value);
1536 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1538 struct token *top_if = stream->top_if;
1539 end_group(stream);
1541 if (!top_if) {
1542 nesting_error(stream);
1543 sparse_error(token->pos, "unmatched #elif within stream");
1544 return 1;
1547 if (token_type(top_if) == TOKEN_ELSE) {
1548 nesting_error(stream);
1549 sparse_error(token->pos, "#elif after #else");
1550 if (!false_nesting)
1551 false_nesting = 1;
1552 return 1;
1555 dirty_stream(stream);
1556 if (token_type(top_if) != TOKEN_IF)
1557 return 1;
1558 if (false_nesting) {
1559 false_nesting = 0;
1560 if (!expression_value(&token->next))
1561 false_nesting = 1;
1562 } else {
1563 false_nesting = 1;
1564 token_type(top_if) = TOKEN_SKIP_GROUPS;
1566 return 1;
1569 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1571 struct token *top_if = stream->top_if;
1572 end_group(stream);
1574 if (!top_if) {
1575 nesting_error(stream);
1576 sparse_error(token->pos, "unmatched #else within stream");
1577 return 1;
1580 if (token_type(top_if) == TOKEN_ELSE) {
1581 nesting_error(stream);
1582 sparse_error(token->pos, "#else after #else");
1584 if (false_nesting) {
1585 if (token_type(top_if) == TOKEN_IF)
1586 false_nesting = 0;
1587 } else {
1588 false_nesting = 1;
1590 token_type(top_if) = TOKEN_ELSE;
1591 return 1;
1594 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1596 struct token *top_if = stream->top_if;
1597 end_group(stream);
1598 if (!top_if) {
1599 nesting_error(stream);
1600 sparse_error(token->pos, "unmatched #endif in stream");
1601 return 1;
1603 if (false_nesting)
1604 false_nesting--;
1605 stream->top_if = top_if->next;
1606 __free_token(top_if);
1607 return 1;
1610 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1612 warning(token->pos, "%s", show_token_sequence(token->next, 0));
1613 return 1;
1616 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1618 sparse_error(token->pos, "%s", show_token_sequence(token->next, 0));
1619 return 1;
1622 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1625 * Do we have any non-system includes?
1626 * Clear them out if so..
1628 *sys_includepath = NULL;
1629 return 1;
1632 static inline void update_inc_ptrs(const char ***where)
1635 if (*where <= dirafter_includepath) {
1636 dirafter_includepath++;
1637 /* If this was the entry that we prepend, don't
1638 * rise the lower entries, even if they are at
1639 * the same level. */
1640 if (where == &dirafter_includepath)
1641 return;
1643 if (*where <= sys_includepath) {
1644 sys_includepath++;
1645 if (where == &sys_includepath)
1646 return;
1648 if (*where <= isys_includepath) {
1649 isys_includepath++;
1650 if (where == &isys_includepath)
1651 return;
1654 /* angle_includepath is actually never updated, since we
1655 * don't suppport -iquote rught now. May change some day. */
1656 if (*where <= angle_includepath) {
1657 angle_includepath++;
1658 if (where == &angle_includepath)
1659 return;
1663 /* Add a path before 'where' and update the pointers associated with the
1664 * includepath array */
1665 static void add_path_entry(struct token *token, const char *path,
1666 const char ***where)
1668 const char **dst;
1669 const char *next;
1671 /* Need one free entry.. */
1672 if (includepath[INCLUDEPATHS-2])
1673 error_die(token->pos, "too many include path entries");
1675 /* check that this is not a duplicate */
1676 dst = includepath;
1677 while (*dst) {
1678 if (strcmp(*dst, path) == 0)
1679 return;
1680 dst++;
1682 next = path;
1683 dst = *where;
1685 update_inc_ptrs(where);
1688 * Move them all up starting at dst,
1689 * insert the new entry..
1691 do {
1692 const char *tmp = *dst;
1693 *dst = next;
1694 next = tmp;
1695 dst++;
1696 } while (next);
1699 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1701 for (;;) {
1702 token = token->next;
1703 if (eof_token(token))
1704 return 1;
1705 if (token_type(token) != TOKEN_STRING) {
1706 warning(token->pos, "expected path string");
1707 return 1;
1709 add_path_entry(token, token->string->data, &isys_includepath);
1713 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1715 for (;;) {
1716 token = token->next;
1717 if (eof_token(token))
1718 return 1;
1719 if (token_type(token) != TOKEN_STRING) {
1720 sparse_error(token->pos, "expected path string");
1721 return 1;
1723 add_path_entry(token, token->string->data, &sys_includepath);
1727 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1729 for (;;) {
1730 token = token->next;
1731 if (eof_token(token))
1732 return 1;
1733 if (token_type(token) != TOKEN_STRING) {
1734 sparse_error(token->pos, "expected path string");
1735 return 1;
1737 add_path_entry(token, token->string->data, &dirafter_includepath);
1741 /* Add to end on includepath list - no pointer updates */
1742 static void add_dirafter_entry(struct token *token, const char *path)
1744 const char **dst = includepath;
1746 /* Need one free entry.. */
1747 if (includepath[INCLUDEPATHS-2])
1748 error_die(token->pos, "too many include path entries");
1750 /* Add to the end */
1751 while (*dst)
1752 dst++;
1753 *dst = path;
1754 dst++;
1755 *dst = NULL;
1758 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1760 for (;;) {
1761 token = token->next;
1762 if (eof_token(token))
1763 return 1;
1764 if (token_type(token) != TOKEN_STRING) {
1765 sparse_error(token->pos, "expected path string");
1766 return 1;
1768 add_dirafter_entry(token, token->string->data);
1772 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1775 * -I-
1776 * From info gcc:
1777 * Split the include path. Any directories specified with `-I'
1778 * options before `-I-' are searched only for headers requested with
1779 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1780 * If additional directories are specified with `-I' options after
1781 * the `-I-', those directories are searched for all `#include'
1782 * directives.
1783 * In addition, `-I-' inhibits the use of the directory of the current
1784 * file directory as the first search directory for `#include "FILE"'.
1786 quote_includepath = includepath+1;
1787 angle_includepath = sys_includepath;
1788 return 1;
1792 * We replace "#pragma xxx" with "__pragma__" in the token
1793 * stream. Just as an example.
1795 * We'll just #define that away for now, but the theory here
1796 * is that we can use this to insert arbitrary token sequences
1797 * to turn the pragmas into internal front-end sequences for
1798 * when we actually start caring about them.
1800 * So eventually this will turn into some kind of extended
1801 * __attribute__() like thing, except called __pragma__(xxx).
1803 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1805 struct token *next = *line;
1807 if (match_ident(token->next, &once_ident) && eof_token(token->next->next)) {
1808 stream->once = 1;
1809 return 1;
1811 token->ident = &pragma_ident;
1812 token->pos.newline = 1;
1813 token->pos.whitespace = 1;
1814 token->pos.pos = 1;
1815 *line = token;
1816 token->next = next;
1817 return 0;
1821 * We ignore #line for now.
1823 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1825 return 1;
1828 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1830 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token, 0));
1831 return 1;
1835 static void init_preprocessor(void)
1837 int i;
1838 int stream = init_stream("preprocessor", -1, includepath);
1839 static struct {
1840 const char *name;
1841 int (*handler)(struct stream *, struct token **, struct token *);
1842 } normal[] = {
1843 { "define", handle_define },
1844 { "weak_define", handle_weak_define },
1845 { "strong_define", handle_strong_define },
1846 { "undef", handle_undef },
1847 { "strong_undef", handle_strong_undef },
1848 { "warning", handle_warning },
1849 { "error", handle_error },
1850 { "include", handle_include },
1851 { "include_next", handle_include_next },
1852 { "pragma", handle_pragma },
1853 { "line", handle_line },
1855 // our internal preprocessor tokens
1856 { "nostdinc", handle_nostdinc },
1857 { "add_include", handle_add_include },
1858 { "add_isystem", handle_add_isystem },
1859 { "add_system", handle_add_system },
1860 { "add_dirafter", handle_add_dirafter },
1861 { "split_include", handle_split_include },
1862 { "argv_include", handle_argv_include },
1863 }, special[] = {
1864 { "ifdef", handle_ifdef },
1865 { "ifndef", handle_ifndef },
1866 { "else", handle_else },
1867 { "endif", handle_endif },
1868 { "if", handle_if },
1869 { "elif", handle_elif },
1872 for (i = 0; i < ARRAY_SIZE(normal); i++) {
1873 struct symbol *sym;
1874 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1875 sym->handler = normal[i].handler;
1876 sym->normal = 1;
1878 for (i = 0; i < ARRAY_SIZE(special); i++) {
1879 struct symbol *sym;
1880 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1881 sym->handler = special[i].handler;
1882 sym->normal = 0;
1887 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1889 int (*handler)(struct stream *, struct token **, struct token *);
1890 struct token *token = start->next;
1891 int is_normal = 1;
1893 if (eof_token(token))
1894 return;
1896 if (token_type(token) == TOKEN_IDENT) {
1897 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1898 if (sym) {
1899 handler = sym->handler;
1900 is_normal = sym->normal;
1901 } else {
1902 handler = handle_nondirective;
1904 } else if (token_type(token) == TOKEN_NUMBER) {
1905 handler = handle_line;
1906 } else {
1907 handler = handle_nondirective;
1910 if (is_normal) {
1911 dirty_stream(stream);
1912 if (false_nesting)
1913 goto out;
1915 if (!handler(stream, line, token)) /* all set */
1916 return;
1918 out:
1919 free_preprocessor_line(token);
1922 static void preprocessor_line(struct stream *stream, struct token **line)
1924 struct token *start = *line, *next;
1925 struct token **tp = &start->next;
1927 for (;;) {
1928 next = *tp;
1929 if (next->pos.newline)
1930 break;
1931 tp = &next->next;
1933 *line = next;
1934 *tp = &eof_token_entry;
1935 handle_preprocessor_line(stream, line, start);
1938 static void do_preprocess(struct token **list)
1940 struct token *next;
1942 while (!eof_token(next = scan_next(list))) {
1943 struct stream *stream = input_streams + next->pos.stream;
1945 if (next->pos.newline && match_op(next, '#')) {
1946 if (!next->pos.noexpand) {
1947 preprocessor_line(stream, list);
1948 __free_token(next); /* Free the '#' token */
1949 continue;
1953 switch (token_type(next)) {
1954 case TOKEN_STREAMEND:
1955 if (stream->top_if) {
1956 nesting_error(stream);
1957 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1958 stream->top_if = NULL;
1959 false_nesting = 0;
1961 if (!stream->dirty)
1962 stream->constant = CONSTANT_FILE_YES;
1963 *list = next->next;
1964 continue;
1965 case TOKEN_STREAMBEGIN:
1966 *list = next->next;
1967 continue;
1969 default:
1970 dirty_stream(stream);
1971 if (false_nesting) {
1972 *list = next->next;
1973 __free_token(next);
1974 continue;
1977 if (token_type(next) != TOKEN_IDENT ||
1978 expand_one_symbol(list))
1979 list = &next->next;
1984 struct token * preprocess(struct token *token)
1986 preprocessing = 1;
1987 init_preprocessor();
1988 do_preprocess(&token);
1990 // Drop all expressions from preprocessing, they're not used any more.
1991 // This is not true when we have multiple files, though ;/
1992 // clear_expression_alloc();
1993 preprocessing = 0;
1995 return token;