flow: revert previous commit
[smatch.git] / pre-process.c
blob457685c0f585fec175a8bc3f4e9b288dd5c190bf
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>
38 #include <dirent.h>
39 #include <sys/stat.h>
41 #include "lib.h"
42 #include "allocate.h"
43 #include "parse.h"
44 #include "token.h"
45 #include "symbol.h"
46 #include "expression.h"
47 #include "scope.h"
49 static struct ident_list *macros; // only needed for -dD
50 static int false_nesting = 0;
51 static int counter_macro = 0; // __COUNTER__ expansion
52 static int include_level = 0;
53 static int expanding = 0;
55 #define INCLUDEPATHS 300
56 const char *includepath[INCLUDEPATHS+1] = {
57 "",
58 "/usr/include",
59 "/usr/local/include",
60 NULL
63 static const char **quote_includepath = includepath;
64 static const char **angle_includepath = includepath + 1;
65 static const char **isys_includepath = includepath + 1;
66 static const char **sys_includepath = includepath + 1;
67 static const char **dirafter_includepath = includepath + 3;
69 #define dirty_stream(stream) \
70 do { \
71 if (!stream->dirty) { \
72 stream->dirty = 1; \
73 if (!stream->ifndef) \
74 stream->protect = NULL; \
75 } \
76 } while(0)
78 #define end_group(stream) \
79 do { \
80 if (stream->ifndef == stream->top_if) { \
81 stream->ifndef = NULL; \
82 if (!stream->dirty) \
83 stream->protect = NULL; \
84 else if (stream->protect) \
85 stream->dirty = 0; \
86 } \
87 } while(0)
89 #define nesting_error(stream) \
90 do { \
91 stream->dirty = 1; \
92 stream->ifndef = NULL; \
93 stream->protect = NULL; \
94 } while(0)
96 static struct token *alloc_token(struct position *pos)
98 struct token *token = __alloc_token(0);
100 token->pos.stream = pos->stream;
101 token->pos.line = pos->line;
102 token->pos.pos = pos->pos;
103 token->pos.whitespace = 1;
104 return token;
107 /* Expand symbol 'sym' at '*list' */
108 static int expand(struct token **, struct symbol *);
110 static void replace_with_string(struct token *token, const char *str)
112 int size = strlen(str) + 1;
113 struct string *s = __alloc_string(size);
115 s->length = size;
116 memcpy(s->data, str, size);
117 token_type(token) = TOKEN_STRING;
118 token->string = s;
121 static void replace_with_integer(struct token *token, unsigned int val)
123 char *buf = __alloc_bytes(11);
124 sprintf(buf, "%u", val);
125 token_type(token) = TOKEN_NUMBER;
126 token->number = buf;
129 static struct symbol *lookup_macro(struct ident *ident)
131 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
132 if (sym && sym->namespace != NS_MACRO)
133 sym = NULL;
134 return sym;
137 static int token_defined(struct token *token)
139 if (token_type(token) == TOKEN_IDENT) {
140 struct symbol *sym = lookup_macro(token->ident);
141 if (sym) {
142 sym->used_in = file_scope;
143 return 1;
145 return 0;
148 sparse_error(token->pos, "expected preprocessor identifier");
149 return 0;
152 static void replace_with_bool(struct token *token, bool val)
154 static const char *string[] = { "0", "1" };
156 token_type(token) = TOKEN_NUMBER;
157 token->number = string[val];
160 static void replace_with_defined(struct token *token)
162 replace_with_bool(token, token_defined(token));
165 static void expand_line(struct token *token)
167 replace_with_integer(token, token->pos.line);
170 static void expand_file(struct token *token)
172 replace_with_string(token, stream_name(token->pos.stream));
175 static void expand_basefile(struct token *token)
177 replace_with_string(token, base_filename);
180 static time_t t = 0;
181 static void expand_date(struct token *token)
183 static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
185 if (!t)
186 time(&t);
187 strftime(buffer, 12, "%b %e %Y", localtime(&t));
188 replace_with_string(token, buffer);
191 static void expand_time(struct token *token)
193 static char buffer[9]; /* __TIME__: 2 + ':' + 2 + ':' + 2 + '\0' */
195 if (!t)
196 time(&t);
197 strftime(buffer, 9, "%T", localtime(&t));
198 replace_with_string(token, buffer);
201 static void expand_counter(struct token *token)
203 replace_with_integer(token, counter_macro++);
206 static void expand_include_level(struct token *token)
208 replace_with_integer(token, include_level - 1);
211 static int expand_one_symbol(struct token **list)
213 struct token *token = *list;
214 struct symbol *sym;
216 if (token->pos.noexpand)
217 return 1;
219 sym = lookup_macro(token->ident);
220 if (!sym)
221 return 1;
222 store_macro_pos(token);
223 if (sym->expand_simple) {
224 sym->expand_simple(token);
225 return 1;
226 } else {
227 int rc;
229 sym->used_in = file_scope;
230 expanding = 1;
231 rc = expand(list, sym);
232 expanding = 0;
233 return rc;
237 static inline struct token *scan_next(struct token **where)
239 struct token *token = *where;
240 if (token_type(token) != TOKEN_UNTAINT)
241 return token;
242 do {
243 token->ident->tainted = 0;
244 token = token->next;
245 } while (token_type(token) == TOKEN_UNTAINT);
246 *where = token;
247 return token;
250 static void expand_list(struct token **list)
252 struct token *next;
253 while (!eof_token(next = scan_next(list))) {
254 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
255 list = &next->next;
259 static void preprocessor_line(struct stream *stream, struct token **line);
261 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos, int count)
263 struct stream *stream = input_streams + prev->pos.stream;
264 struct token **p = &prev->next;
265 struct token *next;
266 int nesting = 0;
268 while (!eof_token(next = scan_next(p))) {
269 if (next->pos.newline && match_op(next, '#')) {
270 if (!next->pos.noexpand) {
271 preprocessor_line(stream, p);
272 __free_token(next); /* Free the '#' token */
273 continue;
276 switch (token_type(next)) {
277 case TOKEN_STREAMEND:
278 case TOKEN_STREAMBEGIN:
279 *p = &eof_token_entry;
280 return next;
281 case TOKEN_STRING:
282 case TOKEN_WIDE_STRING:
283 if (count > 1)
284 next->string->immutable = 1;
285 break;
287 if (false_nesting) {
288 *p = next->next;
289 __free_token(next);
290 continue;
292 if (match_op(next, '(')) {
293 nesting++;
294 } else if (match_op(next, ')')) {
295 if (!nesting--)
296 break;
297 } else if (match_op(next, ',') && !nesting && !vararg) {
298 break;
300 next->pos.stream = pos->stream;
301 next->pos.line = pos->line;
302 next->pos.pos = pos->pos;
303 next->pos.newline = 0;
304 p = &next->next;
306 *p = &eof_token_entry;
307 return next;
311 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
314 struct arg {
315 struct token *arg;
316 struct token *expanded;
317 struct token *str;
318 int n_normal;
319 int n_quoted;
320 int n_str;
323 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
325 int wanted = arglist->count.normal;
326 struct token *next = NULL;
327 int count = 0;
329 arglist = arglist->next; /* skip counter */
331 if (!wanted) {
332 next = collect_arg(start, 0, &what->pos, 0);
333 if (eof_token(next))
334 goto Eclosing;
335 if (!eof_token(start->next) || !match_op(next, ')')) {
336 count++;
337 goto Emany;
339 } else {
340 for (count = 0; count < wanted; count++) {
341 struct argcount *p = &arglist->next->count;
342 next = collect_arg(start, p->vararg, &what->pos, p->normal);
343 if (eof_token(next))
344 goto Eclosing;
345 if (p->vararg && wanted == 1 && eof_token(start->next))
346 break;
347 arglist = arglist->next->next;
348 args[count].arg = start->next;
349 args[count].n_normal = p->normal;
350 args[count].n_quoted = p->quoted;
351 args[count].n_str = p->str;
352 if (match_op(next, ')')) {
353 count++;
354 break;
356 start = next;
358 if (count == wanted && !match_op(next, ')'))
359 goto Emany;
360 if (count == wanted - 1) {
361 struct argcount *p = &arglist->next->count;
362 if (!p->vararg)
363 goto Efew;
364 args[count].arg = NULL;
365 args[count].n_normal = p->normal;
366 args[count].n_quoted = p->quoted;
367 args[count].n_str = p->str;
369 if (count < wanted - 1)
370 goto Efew;
372 what->next = next->next;
373 return 1;
375 Efew:
376 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
377 show_token(what), wanted, count);
378 goto out;
379 Emany:
380 while (match_op(next, ',')) {
381 next = collect_arg(next, 0, &what->pos, 0);
382 count++;
384 if (eof_token(next))
385 goto Eclosing;
386 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
387 show_token(what), count, wanted);
388 goto out;
389 Eclosing:
390 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
391 show_token(what));
392 out:
393 what->next = next->next;
394 return 0;
397 static struct token *dup_list(struct token *list)
399 struct token *res = NULL;
400 struct token **p = &res;
402 while (!eof_token(list)) {
403 struct token *newtok = __alloc_token(0);
404 *newtok = *list;
405 *p = newtok;
406 p = &newtok->next;
407 list = list->next;
409 return res;
412 static const char *show_token_sequence(struct token *token, int quote)
414 static char buffer[MAX_STRING];
415 char *ptr = buffer;
416 int whitespace = 0;
418 if (!token && !quote)
419 return "<none>";
420 while (!eof_token(token)) {
421 const char *val = quote ? quote_token(token) : show_token(token);
422 int len = strlen(val);
424 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
425 sparse_error(token->pos, "too long token expansion");
426 break;
429 if (whitespace)
430 *ptr++ = ' ';
431 memcpy(ptr, val, len);
432 ptr += len;
433 token = token->next;
434 whitespace = token->pos.whitespace;
436 *ptr = 0;
437 return buffer;
440 static struct token *stringify(struct token *arg)
442 const char *s = show_token_sequence(arg, 1);
443 int size = strlen(s)+1;
444 struct token *token = __alloc_token(0);
445 struct string *string = __alloc_string(size);
447 memcpy(string->data, s, size);
448 string->length = size;
449 token->pos = arg->pos;
450 token_type(token) = TOKEN_STRING;
451 token->string = string;
452 token->next = &eof_token_entry;
453 return token;
456 static void expand_arguments(int count, struct arg *args)
458 int i;
459 for (i = 0; i < count; i++) {
460 struct token *arg = args[i].arg;
461 if (!arg)
462 arg = &eof_token_entry;
463 if (args[i].n_str)
464 args[i].str = stringify(arg);
465 if (args[i].n_normal) {
466 if (!args[i].n_quoted) {
467 args[i].expanded = arg;
468 args[i].arg = NULL;
469 } else if (eof_token(arg)) {
470 args[i].expanded = arg;
471 } else {
472 args[i].expanded = dup_list(arg);
474 expand_list(&args[i].expanded);
480 * Possibly valid combinations:
481 * - ident + ident -> ident
482 * - ident + number -> ident unless number contains '.', '+' or '-'.
483 * - 'L' + char constant -> wide char constant
484 * - 'L' + string literal -> wide string literal
485 * - number + number -> number
486 * - number + ident -> number
487 * - number + '.' -> number
488 * - number + '+' or '-' -> number, if number used to end on [eEpP].
489 * - '.' + number -> number, if number used to start with a digit.
490 * - special + special -> either special or an error.
492 static enum token_type combine(struct token *left, struct token *right, char *p)
494 int len;
495 enum token_type t1 = token_type(left), t2 = token_type(right);
497 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
498 return TOKEN_ERROR;
500 if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
501 if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
502 return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
503 if (t2 == TOKEN_STRING)
504 return TOKEN_WIDE_STRING;
507 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
508 return TOKEN_ERROR;
510 strcpy(p, show_token(left));
511 strcat(p, show_token(right));
512 len = strlen(p);
514 if (len >= 256)
515 return TOKEN_ERROR;
517 if (t1 == TOKEN_IDENT) {
518 if (t2 == TOKEN_SPECIAL)
519 return TOKEN_ERROR;
520 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
521 return TOKEN_ERROR;
522 return TOKEN_IDENT;
525 if (t1 == TOKEN_NUMBER) {
526 if (t2 == TOKEN_SPECIAL) {
527 switch (right->special) {
528 case '.':
529 break;
530 case '+': case '-':
531 if (strchr("eEpP", p[len - 2]))
532 break;
533 default:
534 return TOKEN_ERROR;
537 return TOKEN_NUMBER;
540 if (p[0] == '.' && isdigit((unsigned char)p[1]))
541 return TOKEN_NUMBER;
543 return TOKEN_SPECIAL;
546 static int merge(struct token *left, struct token *right)
548 static char buffer[512];
549 enum token_type res = combine(left, right, buffer);
550 int n;
552 switch (res) {
553 case TOKEN_IDENT:
554 left->ident = built_in_ident(buffer);
555 left->pos.noexpand = 0;
556 return 1;
558 case TOKEN_NUMBER:
559 token_type(left) = TOKEN_NUMBER; /* could be . + num */
560 left->number = xstrdup(buffer);
561 return 1;
563 case TOKEN_SPECIAL:
564 if (buffer[2] && buffer[3])
565 break;
566 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
567 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
568 left->special = n;
569 return 1;
572 break;
574 case TOKEN_WIDE_CHAR:
575 case TOKEN_WIDE_STRING:
576 token_type(left) = res;
577 left->pos.noexpand = 0;
578 left->string = right->string;
579 return 1;
581 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
582 token_type(left) = res;
583 left->pos.noexpand = 0;
584 memcpy(left->embedded, right->embedded, 4);
585 return 1;
587 default:
590 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
591 return 0;
594 static struct token *dup_token(struct token *token, struct position *streampos)
596 struct token *alloc = alloc_token(streampos);
597 token_type(alloc) = token_type(token);
598 alloc->pos.newline = token->pos.newline;
599 alloc->pos.whitespace = token->pos.whitespace;
600 alloc->number = token->number;
601 alloc->pos.noexpand = token->pos.noexpand;
602 return alloc;
605 static struct token **copy(struct token **where, struct token *list, int *count)
607 int need_copy = --*count;
608 while (!eof_token(list)) {
609 struct token *token;
610 if (need_copy)
611 token = dup_token(list, &list->pos);
612 else
613 token = list;
614 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
615 token->pos.noexpand = 1;
616 *where = token;
617 where = &token->next;
618 list = list->next;
620 *where = &eof_token_entry;
621 return where;
624 static int handle_kludge(struct token **p, struct arg *args)
626 struct token *t = (*p)->next->next;
627 while (1) {
628 struct arg *v = &args[t->argnum];
629 if (token_type(t->next) != TOKEN_CONCAT) {
630 if (v->arg) {
631 /* ignore the first ## */
632 *p = (*p)->next;
633 return 0;
635 /* skip the entire thing */
636 *p = t;
637 return 1;
639 if (v->arg && !eof_token(v->arg))
640 return 0; /* no magic */
641 t = t->next->next;
645 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
647 struct position *base_pos = &(*list)->pos;
648 int *count;
649 enum {Normal, Placeholder, Concat} state = Normal;
651 for (; !eof_token(body); body = body->next) {
652 struct token *added, *arg;
653 struct token **tail;
654 struct token *t;
656 switch (token_type(body)) {
657 case TOKEN_GNU_KLUDGE:
659 * GNU kludge: if we had <comma>##<vararg>, behaviour
660 * depends on whether we had enough arguments to have
661 * a vararg. If we did, ## is just ignored. Otherwise
662 * both , and ## are ignored. Worse, there can be
663 * an arbitrary number of ##<arg> in between; if all of
664 * those are empty, we act as if they hadn't been there,
665 * otherwise we act as if the kludge didn't exist.
667 t = body;
668 if (handle_kludge(&body, args)) {
669 if (state == Concat)
670 state = Normal;
671 else
672 state = Placeholder;
673 continue;
675 added = dup_token(t, base_pos);
676 token_type(added) = TOKEN_SPECIAL;
677 tail = &added->next;
678 break;
680 case TOKEN_STR_ARGUMENT:
681 arg = args[body->argnum].str;
682 count = &args[body->argnum].n_str;
683 goto copy_arg;
685 case TOKEN_QUOTED_ARGUMENT:
686 arg = args[body->argnum].arg;
687 count = &args[body->argnum].n_quoted;
688 if (!arg || eof_token(arg)) {
689 if (state == Concat)
690 state = Normal;
691 else
692 state = Placeholder;
693 continue;
695 goto copy_arg;
697 case TOKEN_MACRO_ARGUMENT:
698 arg = args[body->argnum].expanded;
699 count = &args[body->argnum].n_normal;
700 if (eof_token(arg)) {
701 state = Normal;
702 continue;
704 copy_arg:
705 tail = copy(&added, arg, count);
706 added->pos.newline = body->pos.newline;
707 added->pos.whitespace = body->pos.whitespace;
708 break;
710 case TOKEN_CONCAT:
711 if (state == Placeholder)
712 state = Normal;
713 else
714 state = Concat;
715 continue;
717 case TOKEN_IDENT:
718 added = dup_token(body, base_pos);
719 if (added->ident->tainted)
720 added->pos.noexpand = 1;
721 tail = &added->next;
722 break;
724 default:
725 added = dup_token(body, base_pos);
726 tail = &added->next;
727 break;
731 * if we got to doing real concatenation, we already have
732 * added something into the list, so containing_token() is OK.
734 if (state == Concat && merge(containing_token(list), added)) {
735 *list = added->next;
736 if (tail != &added->next)
737 list = tail;
738 } else {
739 *list = added;
740 list = tail;
742 state = Normal;
744 *list = &eof_token_entry;
745 return list;
748 static int expand(struct token **list, struct symbol *sym)
750 struct token *last;
751 struct token *token = *list;
752 struct ident *expanding = token->ident;
753 struct token **tail;
754 struct token *expansion = sym->expansion;
755 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
756 struct arg args[nargs];
758 if (expanding->tainted) {
759 token->pos.noexpand = 1;
760 return 1;
763 if (sym->arglist) {
764 if (!match_op(scan_next(&token->next), '('))
765 return 1;
766 if (!collect_arguments(token->next, sym->arglist, args, token))
767 return 1;
768 expand_arguments(nargs, args);
771 if (sym->expand)
772 return sym->expand(token, args) ? 0 : 1;
774 expanding->tainted = 1;
776 last = token->next;
777 tail = substitute(list, expansion, args);
779 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
780 * We still can lose the newline flag if the sucker expands to nothing,
781 * but the price of dealing with that is probably too high (we'd need
782 * to collect the flags during scan_next())
784 (*list)->pos.newline = token->pos.newline;
785 (*list)->pos.whitespace = token->pos.whitespace;
786 *tail = last;
788 return 0;
791 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
793 static char buffer[256];
794 char *ptr = buffer;
796 while (!eof_token(token) && !match_op(token, endop)) {
797 int len;
798 const char *val = token->string->data;
799 if (token_type(token) != TOKEN_STRING)
800 val = show_token(token);
801 len = strlen(val);
802 memcpy(ptr, val, len);
803 ptr += len;
804 token = token->next;
806 *ptr = 0;
807 if (endop && !match_op(token, endop))
808 sparse_error(start->pos, "expected '>' at end of filename");
809 return buffer;
812 static int already_tokenized(const char *path)
814 int stream, next;
816 for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
817 struct stream *s = input_streams + stream;
819 next = s->next_stream;
820 if (s->once) {
821 if (strcmp(path, s->name))
822 continue;
823 return 1;
825 if (s->constant != CONSTANT_FILE_YES)
826 continue;
827 if (strcmp(path, s->name))
828 continue;
829 if (s->protect && !lookup_macro(s->protect))
830 continue;
831 return 1;
833 return 0;
836 /* Handle include of header files.
837 * The relevant options are made compatible with gcc. The only options that
838 * are not supported is -withprefix and friends.
840 * Three set of include paths are known:
841 * quote_includepath: Path to search when using #include "file.h"
842 * angle_includepath: Paths to search when using #include <file.h>
843 * isys_includepath: Paths specified with -isystem, come before the
844 * built-in system include paths. Gcc would suppress
845 * warnings from system headers. Here we separate
846 * them from the angle_ ones to keep search ordering.
848 * sys_includepath: Built-in include paths.
849 * dirafter_includepath Paths added with -dirafter.
851 * The above is implemented as one array with pointers
852 * +--------------+
853 * quote_includepath ---> | |
854 * +--------------+
855 * | |
856 * +--------------+
857 * angle_includepath ---> | |
858 * +--------------+
859 * isys_includepath ---> | |
860 * +--------------+
861 * sys_includepath ---> | |
862 * +--------------+
863 * dirafter_includepath -> | |
864 * +--------------+
866 * -I dir insert dir just before isys_includepath and move the rest
867 * -I- makes all dirs specified with -I before to quote dirs only and
868 * angle_includepath is set equal to isys_includepath.
869 * -nostdinc removes all sys dirs by storing NULL in entry pointed
870 * to by * sys_includepath. Note that this will reset all dirs built-in
871 * and added before -nostdinc by -isystem and -idirafter.
872 * -isystem dir adds dir where isys_includepath points adding this dir as
873 * first systemdir
874 * -idirafter dir adds dir to the end of the list
877 static void set_stream_include_path(struct stream *stream)
879 const char *path = stream->path;
880 if (!path) {
881 const char *p = strrchr(stream->name, '/');
882 path = "";
883 if (p) {
884 int len = p - stream->name + 1;
885 char *m = malloc(len+1);
886 /* This includes the final "/" */
887 memcpy(m, stream->name, len);
888 m[len] = 0;
889 path = m;
890 /* normalize this path */
891 while (path[0] == '.' && path[1] == '/') {
892 path += 2;
893 while (path[0] == '/')
894 path++;
897 stream->path = path;
899 includepath[0] = path;
902 static int try_include(struct position pos, const char *path, const char *filename, int flen, struct token **where, const char **next_path)
904 int fd;
905 int plen = strlen(path);
906 static char fullname[PATH_MAX];
908 memcpy(fullname, path, plen);
909 if (plen && path[plen-1] != '/') {
910 fullname[plen] = '/';
911 plen++;
913 memcpy(fullname+plen, filename, flen);
914 if (already_tokenized(fullname))
915 return 1;
916 fd = open(fullname, O_RDONLY);
917 if (fd >= 0) {
918 char *streamname = xmemdup(fullname, plen + flen);
919 *where = tokenize(&pos, streamname, fd, *where, next_path);
920 close(fd);
921 return 1;
923 return 0;
926 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
928 const char *path;
930 while ((path = *pptr++) != NULL) {
931 if (!try_include(token->pos, path, filename, flen, list, pptr))
932 continue;
933 return 1;
935 return 0;
938 static int free_preprocessor_line(struct token *token)
940 while (token_type(token) != TOKEN_EOF) {
941 struct token *free = token;
942 token = token->next;
943 __free_token(free);
945 return 1;
948 const char *find_include(const char *skip, const char *look_for)
950 DIR *dp;
951 struct dirent *entry;
952 struct stat statbuf;
953 const char *ret;
954 char cwd[PATH_MAX];
955 static char buf[PATH_MAX + 1];
957 dp = opendir(".");
958 if (!dp)
959 return NULL;
961 if (!getcwd(cwd, sizeof(cwd)))
962 goto close;
964 while ((entry = readdir(dp))) {
965 lstat(entry->d_name, &statbuf);
967 if (strcmp(entry->d_name, look_for) == 0) {
968 snprintf(buf, sizeof(buf), "%s/%s", cwd, entry->d_name);
969 closedir(dp);
970 return buf;
973 if (S_ISDIR(statbuf.st_mode)) {
974 /* Found a directory, but ignore . and .. */
975 if (strcmp(".", entry->d_name) == 0 ||
976 strcmp("..", entry->d_name) == 0 ||
977 strcmp(skip, entry->d_name) == 0)
978 continue;
980 chdir(entry->d_name);
981 ret = find_include("", look_for);
982 chdir("..");
983 if (ret) {
984 closedir(dp);
985 return ret;
989 close:
990 closedir(dp);
992 return NULL;
995 const char *search_dir(const char *stop, const char *look_for)
997 char cwd[PATH_MAX];
998 int len;
999 const char *ret;
1000 int cnt = 0;
1002 if (!getcwd(cwd, sizeof(cwd)))
1003 return NULL;
1005 len = strlen(cwd);
1006 while (len >= 0) {
1007 ret = find_include(cnt++ ? cwd + len + 1 : "", look_for);
1008 if (ret)
1009 return ret;
1011 if (strcmp(cwd, stop) == 0 ||
1012 strcmp(cwd, "/usr/include") == 0 ||
1013 strcmp(cwd, "/usr/local/include") == 0 ||
1014 strlen(cwd) <= 10 || /* heck... don't search /usr/lib/ */
1015 strcmp(cwd, "/") == 0)
1016 return NULL;
1018 while (--len >= 0) {
1019 if (cwd[len] == '/') {
1020 cwd[len] = '\0';
1021 break;
1025 chdir("..");
1027 return NULL;
1030 static void use_best_guess_header_file(struct token *token, const char *filename, struct token **list)
1032 char cwd[PATH_MAX];
1033 char dir_part[PATH_MAX];
1034 const char *file_part;
1035 const char *include_name;
1036 static int cnt;
1037 int len;
1039 /* Avoid guessing includes recursively. */
1040 if (cnt++ > 1000)
1041 return;
1043 if (!filename || filename[0] == '\0')
1044 return;
1046 file_part = filename;
1047 while ((filename = strchr(filename, '/'))) {
1048 ++filename;
1049 if (filename[0])
1050 file_part = filename;
1053 snprintf(dir_part, sizeof(dir_part), "%s", stream_name(token->pos.stream));
1054 len = strlen(dir_part);
1055 while (--len >= 0) {
1056 if (dir_part[len] == '/') {
1057 dir_part[len] = '\0';
1058 break;
1061 if (len < 0)
1062 sprintf(dir_part, ".");
1064 if (!getcwd(cwd, sizeof(cwd)))
1065 return;
1067 chdir(dir_part);
1068 include_name = search_dir(cwd, file_part);
1069 chdir(cwd);
1070 if (!include_name)
1071 return;
1072 sparse_error(token->pos, "using '%s'", include_name);
1074 try_include(token->pos, "", include_name, strlen(include_name), list, includepath);
1077 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how)
1079 const char *filename;
1080 struct token *next;
1081 const char **path;
1082 int expect;
1083 int flen;
1085 next = token->next;
1086 expect = '>';
1087 if (!match_op(next, '<')) {
1088 expand_list(&token->next);
1089 expect = 0;
1090 next = token;
1091 if (match_op(token->next, '<')) {
1092 next = token->next;
1093 expect = '>';
1097 token = next->next;
1098 filename = token_name_sequence(token, expect, token);
1099 flen = strlen(filename) + 1;
1101 /* Absolute path? */
1102 if (filename[0] == '/') {
1103 if (try_include(token->pos, "", filename, flen, list, includepath))
1104 return 0;
1105 goto out;
1108 switch (how) {
1109 case 1:
1110 path = stream->next_path;
1111 break;
1112 case 2:
1113 includepath[0] = "";
1114 path = includepath;
1115 break;
1116 default:
1117 /* Dir of input file is first dir to search for quoted includes */
1118 set_stream_include_path(stream);
1119 path = expect ? angle_includepath : quote_includepath;
1120 break;
1122 /* Check the standard include paths.. */
1123 if (do_include_path(path, list, token, filename, flen))
1124 return 0;
1125 out:
1126 sparse_error(token->pos, "unable to open '%s'", filename);
1127 use_best_guess_header_file(token, filename, list);
1128 return 0;
1131 static int handle_include(struct stream *stream, struct token **list, struct token *token)
1133 return handle_include_path(stream, list, token, 0);
1136 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
1138 return handle_include_path(stream, list, token, 1);
1141 static int handle_argv_include(struct stream *stream, struct token **list, struct token *token)
1143 return handle_include_path(stream, list, token, 2);
1146 static int token_different(struct token *t1, struct token *t2)
1148 int different;
1150 if (token_type(t1) != token_type(t2))
1151 return 1;
1153 switch (token_type(t1)) {
1154 case TOKEN_IDENT:
1155 different = t1->ident != t2->ident;
1156 break;
1157 case TOKEN_ARG_COUNT:
1158 case TOKEN_UNTAINT:
1159 case TOKEN_CONCAT:
1160 case TOKEN_GNU_KLUDGE:
1161 different = 0;
1162 break;
1163 case TOKEN_NUMBER:
1164 different = strcmp(t1->number, t2->number);
1165 break;
1166 case TOKEN_SPECIAL:
1167 different = t1->special != t2->special;
1168 break;
1169 case TOKEN_MACRO_ARGUMENT:
1170 case TOKEN_QUOTED_ARGUMENT:
1171 case TOKEN_STR_ARGUMENT:
1172 different = t1->argnum != t2->argnum;
1173 break;
1174 case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
1175 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
1176 different = memcmp(t1->embedded, t2->embedded, 4);
1177 break;
1178 case TOKEN_CHAR:
1179 case TOKEN_WIDE_CHAR:
1180 case TOKEN_STRING:
1181 case TOKEN_WIDE_STRING: {
1182 struct string *s1, *s2;
1184 s1 = t1->string;
1185 s2 = t2->string;
1186 different = 1;
1187 if (s1->length != s2->length)
1188 break;
1189 different = memcmp(s1->data, s2->data, s1->length);
1190 break;
1192 default:
1193 different = 1;
1194 break;
1196 return different;
1199 static int token_list_different(struct token *list1, struct token *list2)
1201 for (;;) {
1202 if (list1 == list2)
1203 return 0;
1204 if (!list1 || !list2)
1205 return 1;
1206 if (token_different(list1, list2))
1207 return 1;
1208 list1 = list1->next;
1209 list2 = list2->next;
1213 static inline void set_arg_count(struct token *token)
1215 token_type(token) = TOKEN_ARG_COUNT;
1216 token->count.normal = token->count.quoted =
1217 token->count.str = token->count.vararg = 0;
1220 static struct token *parse_arguments(struct token *list)
1222 struct token *arg = list->next, *next = list;
1223 struct argcount *count = &list->count;
1225 set_arg_count(list);
1227 if (match_op(arg, ')')) {
1228 next = arg->next;
1229 list->next = &eof_token_entry;
1230 return next;
1233 while (token_type(arg) == TOKEN_IDENT) {
1234 if (arg->ident == &__VA_ARGS___ident)
1235 goto Eva_args;
1236 if (!++count->normal)
1237 goto Eargs;
1238 next = arg->next;
1240 if (match_op(next, ',')) {
1241 set_arg_count(next);
1242 arg = next->next;
1243 continue;
1246 if (match_op(next, ')')) {
1247 set_arg_count(next);
1248 next = next->next;
1249 arg->next->next = &eof_token_entry;
1250 return next;
1253 /* normal cases are finished here */
1255 if (match_op(next, SPECIAL_ELLIPSIS)) {
1256 if (match_op(next->next, ')')) {
1257 set_arg_count(next);
1258 next->count.vararg = 1;
1259 next = next->next;
1260 arg->next->next = &eof_token_entry;
1261 return next->next;
1264 arg = next;
1265 goto Enotclosed;
1268 if (eof_token(next)) {
1269 goto Enotclosed;
1270 } else {
1271 arg = next;
1272 goto Ebadstuff;
1276 if (match_op(arg, SPECIAL_ELLIPSIS)) {
1277 next = arg->next;
1278 token_type(arg) = TOKEN_IDENT;
1279 arg->ident = &__VA_ARGS___ident;
1280 if (!match_op(next, ')'))
1281 goto Enotclosed;
1282 if (!++count->normal)
1283 goto Eargs;
1284 set_arg_count(next);
1285 next->count.vararg = 1;
1286 next = next->next;
1287 arg->next->next = &eof_token_entry;
1288 return next;
1291 if (eof_token(arg)) {
1292 arg = next;
1293 goto Enotclosed;
1295 if (match_op(arg, ','))
1296 goto Emissing;
1297 else
1298 goto Ebadstuff;
1301 Emissing:
1302 sparse_error(arg->pos, "parameter name missing");
1303 return NULL;
1304 Ebadstuff:
1305 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
1306 show_token(arg));
1307 return NULL;
1308 Enotclosed:
1309 sparse_error(arg->pos, "missing ')' in macro parameter list");
1310 return NULL;
1311 Eva_args:
1312 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1313 return NULL;
1314 Eargs:
1315 sparse_error(arg->pos, "too many arguments in macro definition");
1316 return NULL;
1319 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1321 struct ident *ident = token->ident;
1322 int nr;
1324 if (!arglist || token_type(token) != TOKEN_IDENT)
1325 return 0;
1327 arglist = arglist->next;
1329 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1330 if (arglist->ident == ident) {
1331 struct argcount *count = &arglist->next->count;
1332 int n;
1334 token->argnum = nr;
1335 token_type(token) = type;
1336 switch (type) {
1337 case TOKEN_MACRO_ARGUMENT:
1338 n = ++count->normal;
1339 break;
1340 case TOKEN_QUOTED_ARGUMENT:
1341 n = ++count->quoted;
1342 break;
1343 default:
1344 n = ++count->str;
1346 if (n)
1347 return count->vararg ? 2 : 1;
1349 * XXX - need saner handling of that
1350 * (>= 1024 instances of argument)
1352 token_type(token) = TOKEN_ERROR;
1353 return -1;
1356 return 0;
1359 static struct token *handle_hash(struct token **p, struct token *arglist)
1361 struct token *token = *p;
1362 if (arglist) {
1363 struct token *next = token->next;
1364 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1365 goto Equote;
1366 next->pos.whitespace = token->pos.whitespace;
1367 __free_token(token);
1368 token = *p = next;
1369 } else {
1370 token->pos.noexpand = 1;
1372 return token;
1374 Equote:
1375 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1376 return NULL;
1379 /* token->next is ## */
1380 static struct token *handle_hashhash(struct token *token, struct token *arglist)
1382 struct token *last = token;
1383 struct token *concat;
1384 int state = match_op(token, ',');
1386 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1388 while (1) {
1389 struct token *t;
1390 int is_arg;
1392 /* eat duplicate ## */
1393 concat = token->next;
1394 while (match_op(t = concat->next, SPECIAL_HASHHASH)) {
1395 token->next = t;
1396 __free_token(concat);
1397 concat = t;
1399 token_type(concat) = TOKEN_CONCAT;
1401 if (eof_token(t))
1402 goto Econcat;
1404 if (match_op(t, '#')) {
1405 t = handle_hash(&concat->next, arglist);
1406 if (!t)
1407 return NULL;
1410 is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
1412 if (state == 1 && is_arg) {
1413 state = is_arg;
1414 } else {
1415 last = t;
1416 state = match_op(t, ',');
1419 token = t;
1420 if (!match_op(token->next, SPECIAL_HASHHASH))
1421 break;
1423 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1424 if (state == 2)
1425 token_type(last) = TOKEN_GNU_KLUDGE;
1426 return token;
1428 Econcat:
1429 sparse_error(concat->pos, "'##' cannot appear at the ends of macro expansion");
1430 return NULL;
1433 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1435 struct token *token = expansion;
1436 struct token **p;
1438 if (match_op(token, SPECIAL_HASHHASH))
1439 goto Econcat;
1441 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1442 if (match_op(token, '#')) {
1443 token = handle_hash(p, arglist);
1444 if (!token)
1445 return NULL;
1447 if (match_op(token->next, SPECIAL_HASHHASH)) {
1448 token = handle_hashhash(token, arglist);
1449 if (!token)
1450 return NULL;
1451 } else {
1452 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1454 switch (token_type(token)) {
1455 case TOKEN_ERROR:
1456 goto Earg;
1458 case TOKEN_STRING:
1459 case TOKEN_WIDE_STRING:
1460 token->string->immutable = 1;
1461 break;
1464 token = alloc_token(&expansion->pos);
1465 token_type(token) = TOKEN_UNTAINT;
1466 token->ident = name;
1467 token->next = *p;
1468 *p = token;
1469 return expansion;
1471 Econcat:
1472 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1473 return NULL;
1474 Earg:
1475 sparse_error(token->pos, "too many instances of argument in body");
1476 return NULL;
1479 static int do_define(struct position pos, struct token *token, struct ident *name,
1480 struct token *arglist, struct token *expansion, int attr)
1482 struct symbol *sym;
1483 int ret = 1;
1485 expansion = parse_expansion(expansion, arglist, name);
1486 if (!expansion)
1487 return 1;
1489 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1490 if (sym) {
1491 int clean;
1493 if (attr < sym->attr)
1494 goto out;
1496 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1498 if (token_list_different(sym->expansion, expansion) ||
1499 token_list_different(sym->arglist, arglist)) {
1500 ret = 0;
1501 if ((clean && attr == SYM_ATTR_NORMAL)
1502 || sym->used_in == file_scope) {
1503 warning(pos, "preprocessor token %.*s redefined",
1504 name->len, name->name);
1505 info(sym->pos, "this was the original definition");
1507 } else if (clean)
1508 goto out;
1511 if (!sym || sym->scope != file_scope) {
1512 sym = alloc_symbol(pos, SYM_NODE);
1513 bind_symbol(sym, name, NS_MACRO);
1514 add_ident(&macros, name);
1515 ret = 0;
1518 if (!ret) {
1519 sym->expansion = expansion;
1520 sym->arglist = arglist;
1521 if (token) /* Free the "define" token, but not the rest of the line */
1522 __free_token(token);
1525 sym->namespace = NS_MACRO;
1526 sym->used_in = NULL;
1527 sym->attr = attr;
1528 out:
1529 return ret;
1533 // predefine a macro with a printf-formatted value
1534 // @name: the name of the macro
1535 // @weak: 0/1 for a normal or a weak define
1536 // @fmt: the printf format followed by it's arguments.
1538 // The type of the value is automatically infered:
1539 // TOKEN_NUMBER if it starts by a digit, TOKEN_IDENT otherwise.
1540 // If @fmt is null or empty, the macro is defined with an empty definition.
1541 void predefine(const char *name, int weak, const char *fmt, ...)
1543 struct ident *ident = built_in_ident(name);
1544 struct token *value = &eof_token_entry;
1545 int attr = weak ? SYM_ATTR_WEAK : SYM_ATTR_NORMAL;
1547 if (fmt && fmt[0]) {
1548 static char buf[256];
1549 va_list ap;
1551 va_start(ap, fmt);
1552 vsnprintf(buf, sizeof(buf), fmt, ap);
1553 va_end(ap);
1555 value = __alloc_token(0);
1556 if (isdigit((unsigned char)buf[0])) {
1557 token_type(value) = TOKEN_NUMBER;
1558 value->number = xstrdup(buf);
1559 } else {
1560 token_type(value) = TOKEN_IDENT;
1561 value->ident = built_in_ident(buf);
1563 value->pos.whitespace = 1;
1564 value->next = &eof_token_entry;
1567 do_define(value->pos, NULL, ident, NULL, value, attr);
1571 // like predefine() but only if one of the non-standard dialect is chosen
1572 void predefine_nostd(const char *name)
1574 if ((standard & STANDARD_GNU) || (standard == STANDARD_NONE))
1575 predefine(name, 1, "1");
1578 static void predefine_fmt(const char *fmt, int weak, va_list ap)
1580 static char buf[256];
1582 vsnprintf(buf, sizeof(buf), fmt, ap);
1583 predefine(buf, weak, "1");
1586 void predefine_strong(const char *fmt, ...)
1588 va_list ap;
1590 va_start(ap, fmt);
1591 predefine_fmt(fmt, 0, ap);
1592 va_end(ap);
1595 void predefine_weak(const char *fmt, ...)
1597 va_list ap;
1599 va_start(ap, fmt);
1600 predefine_fmt(fmt, 1, ap);
1601 va_end(ap);
1604 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1606 struct token *arglist, *expansion;
1607 struct token *left = token->next;
1608 struct ident *name;
1610 if (token_type(left) != TOKEN_IDENT) {
1611 sparse_error(token->pos, "expected identifier to 'define'");
1612 return 1;
1615 name = left->ident;
1617 arglist = NULL;
1618 expansion = left->next;
1619 if (!expansion->pos.whitespace) {
1620 if (match_op(expansion, '(')) {
1621 arglist = expansion;
1622 expansion = parse_arguments(expansion);
1623 if (!expansion)
1624 return 1;
1625 } else if (!eof_token(expansion)) {
1626 warning(expansion->pos,
1627 "no whitespace before object-like macro body");
1631 return do_define(left->pos, token, name, arglist, expansion, attr);
1634 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1636 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1639 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1641 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1644 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1646 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1649 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1651 struct token *left = token->next;
1652 struct symbol *sym;
1654 if (token_type(left) != TOKEN_IDENT) {
1655 sparse_error(token->pos, "expected identifier to 'undef'");
1656 return 1;
1659 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1660 if (sym) {
1661 if (attr < sym->attr)
1662 return 1;
1663 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1664 return 1;
1665 } else if (attr <= SYM_ATTR_NORMAL)
1666 return 1;
1668 if (!sym || sym->scope != file_scope) {
1669 sym = alloc_symbol(left->pos, SYM_NODE);
1670 bind_symbol(sym, left->ident, NS_MACRO);
1673 sym->namespace = NS_UNDEF;
1674 sym->used_in = NULL;
1675 sym->attr = attr;
1677 return 1;
1680 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1682 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1685 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1687 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1690 static int preprocessor_if(struct stream *stream, struct token *token, int cond)
1692 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1693 free_preprocessor_line(token->next);
1694 token->next = stream->top_if;
1695 stream->top_if = token;
1696 if (false_nesting || cond != 1)
1697 false_nesting++;
1698 return 0;
1701 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1703 struct token *next = token->next;
1704 int arg;
1705 if (token_type(next) == TOKEN_IDENT) {
1706 arg = token_defined(next);
1707 } else {
1708 dirty_stream(stream);
1709 if (!false_nesting)
1710 sparse_error(token->pos, "expected preprocessor identifier");
1711 arg = -1;
1713 return preprocessor_if(stream, token, arg);
1716 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1718 struct token *next = token->next;
1719 int arg;
1720 if (token_type(next) == TOKEN_IDENT) {
1721 if (!stream->dirty && !stream->ifndef) {
1722 if (!stream->protect) {
1723 stream->ifndef = token;
1724 stream->protect = next->ident;
1725 } else if (stream->protect == next->ident) {
1726 stream->ifndef = token;
1727 stream->dirty = 1;
1730 arg = !token_defined(next);
1731 } else {
1732 dirty_stream(stream);
1733 if (!false_nesting)
1734 sparse_error(token->pos, "expected preprocessor identifier");
1735 arg = -1;
1738 return preprocessor_if(stream, token, arg);
1742 * Expression handling for #if and #elif; it differs from normal expansion
1743 * due to special treatment of "defined".
1745 static int expression_value(struct token **where)
1747 struct expression *expr;
1748 struct token *p;
1749 struct token **list = where, **beginning = NULL;
1750 long long value;
1751 int state = 0;
1753 while (!eof_token(p = scan_next(list))) {
1754 switch (state) {
1755 case 0:
1756 if (token_type(p) != TOKEN_IDENT)
1757 break;
1758 if (p->ident == &defined_ident) {
1759 state = 1;
1760 beginning = list;
1761 break;
1763 if (!expand_one_symbol(list))
1764 continue;
1765 if (token_type(p) != TOKEN_IDENT)
1766 break;
1767 token_type(p) = TOKEN_ZERO_IDENT;
1768 break;
1769 case 1:
1770 if (match_op(p, '(')) {
1771 state = 2;
1772 } else {
1773 state = 0;
1774 replace_with_defined(p);
1775 *beginning = p;
1777 break;
1778 case 2:
1779 if (token_type(p) == TOKEN_IDENT)
1780 state = 3;
1781 else
1782 state = 0;
1783 replace_with_defined(p);
1784 *beginning = p;
1785 break;
1786 case 3:
1787 state = 0;
1788 if (!match_op(p, ')'))
1789 sparse_error(p->pos, "missing ')' after \"defined\"");
1790 *list = p->next;
1791 continue;
1793 list = &p->next;
1796 p = constant_expression(*where, &expr);
1797 if (!eof_token(p))
1798 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p, 0));
1799 value = get_expression_value(expr);
1800 return value != 0;
1803 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1805 int value = 0;
1806 if (!false_nesting)
1807 value = expression_value(&token->next);
1809 dirty_stream(stream);
1810 return preprocessor_if(stream, token, value);
1813 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1815 struct token *top_if = stream->top_if;
1816 end_group(stream);
1818 if (!top_if) {
1819 nesting_error(stream);
1820 sparse_error(token->pos, "unmatched #elif within stream");
1821 return 1;
1824 if (token_type(top_if) == TOKEN_ELSE) {
1825 nesting_error(stream);
1826 sparse_error(token->pos, "#elif after #else");
1827 if (!false_nesting)
1828 false_nesting = 1;
1829 return 1;
1832 dirty_stream(stream);
1833 if (token_type(top_if) != TOKEN_IF)
1834 return 1;
1835 if (false_nesting) {
1836 false_nesting = 0;
1837 if (!expression_value(&token->next))
1838 false_nesting = 1;
1839 } else {
1840 false_nesting = 1;
1841 token_type(top_if) = TOKEN_SKIP_GROUPS;
1843 return 1;
1846 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1848 struct token *top_if = stream->top_if;
1849 end_group(stream);
1851 if (!top_if) {
1852 nesting_error(stream);
1853 sparse_error(token->pos, "unmatched #else within stream");
1854 return 1;
1857 if (token_type(top_if) == TOKEN_ELSE) {
1858 nesting_error(stream);
1859 sparse_error(token->pos, "#else after #else");
1861 if (false_nesting) {
1862 if (token_type(top_if) == TOKEN_IF)
1863 false_nesting = 0;
1864 } else {
1865 false_nesting = 1;
1867 token_type(top_if) = TOKEN_ELSE;
1868 return 1;
1871 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1873 struct token *top_if = stream->top_if;
1874 end_group(stream);
1875 if (!top_if) {
1876 nesting_error(stream);
1877 sparse_error(token->pos, "unmatched #endif in stream");
1878 return 1;
1880 if (false_nesting)
1881 false_nesting--;
1882 stream->top_if = top_if->next;
1883 __free_token(top_if);
1884 return 1;
1887 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1889 warning(token->pos, "%s", show_token_sequence(token->next, 0));
1890 return 1;
1893 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1895 sparse_error(token->pos, "%s", show_token_sequence(token->next, 0));
1896 return 1;
1899 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1902 * Do we have any non-system includes?
1903 * Clear them out if so..
1905 *sys_includepath = NULL;
1906 return 1;
1909 static inline void update_inc_ptrs(const char ***where)
1912 if (*where <= dirafter_includepath) {
1913 dirafter_includepath++;
1914 /* If this was the entry that we prepend, don't
1915 * rise the lower entries, even if they are at
1916 * the same level. */
1917 if (where == &dirafter_includepath)
1918 return;
1920 if (*where <= sys_includepath) {
1921 sys_includepath++;
1922 if (where == &sys_includepath)
1923 return;
1925 if (*where <= isys_includepath) {
1926 isys_includepath++;
1927 if (where == &isys_includepath)
1928 return;
1931 /* angle_includepath is actually never updated, since we
1932 * don't suppport -iquote rught now. May change some day. */
1933 if (*where <= angle_includepath) {
1934 angle_includepath++;
1935 if (where == &angle_includepath)
1936 return;
1940 /* Add a path before 'where' and update the pointers associated with the
1941 * includepath array */
1942 static void add_path_entry(struct token *token, const char *path,
1943 const char ***where)
1945 const char **dst;
1946 const char *next;
1948 /* Need one free entry.. */
1949 if (includepath[INCLUDEPATHS-2])
1950 error_die(token->pos, "too many include path entries");
1952 /* check that this is not a duplicate */
1953 dst = includepath;
1954 while (*dst) {
1955 if (strcmp(*dst, path) == 0)
1956 return;
1957 dst++;
1959 next = path;
1960 dst = *where;
1962 update_inc_ptrs(where);
1965 * Move them all up starting at dst,
1966 * insert the new entry..
1968 do {
1969 const char *tmp = *dst;
1970 *dst = next;
1971 next = tmp;
1972 dst++;
1973 } while (next);
1976 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1978 for (;;) {
1979 token = token->next;
1980 if (eof_token(token))
1981 return 1;
1982 if (token_type(token) != TOKEN_STRING) {
1983 warning(token->pos, "expected path string");
1984 return 1;
1986 add_path_entry(token, token->string->data, &isys_includepath);
1990 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1992 for (;;) {
1993 token = token->next;
1994 if (eof_token(token))
1995 return 1;
1996 if (token_type(token) != TOKEN_STRING) {
1997 sparse_error(token->pos, "expected path string");
1998 return 1;
2000 add_path_entry(token, token->string->data, &sys_includepath);
2004 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
2006 for (;;) {
2007 token = token->next;
2008 if (eof_token(token))
2009 return 1;
2010 if (token_type(token) != TOKEN_STRING) {
2011 sparse_error(token->pos, "expected path string");
2012 return 1;
2014 add_path_entry(token, token->string->data, &dirafter_includepath);
2018 /* Add to end on includepath list - no pointer updates */
2019 static void add_dirafter_entry(struct token *token, const char *path)
2021 const char **dst = includepath;
2023 /* Need one free entry.. */
2024 if (includepath[INCLUDEPATHS-2])
2025 error_die(token->pos, "too many include path entries");
2027 /* Add to the end */
2028 while (*dst)
2029 dst++;
2030 *dst = path;
2031 dst++;
2032 *dst = NULL;
2035 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
2037 for (;;) {
2038 token = token->next;
2039 if (eof_token(token))
2040 return 1;
2041 if (token_type(token) != TOKEN_STRING) {
2042 sparse_error(token->pos, "expected path string");
2043 return 1;
2045 add_dirafter_entry(token, token->string->data);
2049 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
2052 * -I-
2053 * From info gcc:
2054 * Split the include path. Any directories specified with `-I'
2055 * options before `-I-' are searched only for headers requested with
2056 * `#include "FILE"'; they are not searched for `#include <FILE>'.
2057 * If additional directories are specified with `-I' options after
2058 * the `-I-', those directories are searched for all `#include'
2059 * directives.
2060 * In addition, `-I-' inhibits the use of the directory of the current
2061 * file directory as the first search directory for `#include "FILE"'.
2063 quote_includepath = includepath+1;
2064 angle_includepath = sys_includepath;
2065 return 1;
2069 * We replace "#pragma xxx" with "__pragma__" in the token
2070 * stream. Just as an example.
2072 * We'll just #define that away for now, but the theory here
2073 * is that we can use this to insert arbitrary token sequences
2074 * to turn the pragmas into internal front-end sequences for
2075 * when we actually start caring about them.
2077 * So eventually this will turn into some kind of extended
2078 * __attribute__() like thing, except called __pragma__(xxx).
2080 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
2082 struct token *next = *line;
2084 if (match_ident(token->next, &once_ident) && eof_token(token->next->next)) {
2085 stream->once = 1;
2086 return 1;
2088 token->ident = &pragma_ident;
2089 token->pos.newline = 1;
2090 token->pos.whitespace = 1;
2091 token->pos.pos = 1;
2092 *line = token;
2093 token->next = next;
2094 return 0;
2098 * We ignore #line for now.
2100 static int handle_line(struct stream *stream, struct token **line, struct token *token)
2102 return 1;
2105 static int handle_ident(struct stream *stream, struct token **line, struct token *token)
2107 return 1;
2110 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
2112 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token, 0));
2113 return 1;
2116 static bool expand_has_attribute(struct token *token, struct arg *args)
2118 struct token *arg = args[0].expanded;
2119 struct symbol *sym;
2121 if (token_type(arg) != TOKEN_IDENT) {
2122 sparse_error(arg->pos, "identifier expected");
2123 return false;
2126 sym = lookup_symbol(arg->ident, NS_KEYWORD);
2127 replace_with_bool(token, sym && sym->op && sym->op->attribute);
2128 return true;
2131 static bool expand_has_builtin(struct token *token, struct arg *args)
2133 struct token *arg = args[0].expanded;
2134 struct symbol *sym;
2136 if (token_type(arg) != TOKEN_IDENT) {
2137 sparse_error(arg->pos, "identifier expected");
2138 return false;
2141 sym = lookup_symbol(arg->ident, NS_SYMBOL);
2142 replace_with_bool(token, sym && sym->builtin);
2143 return true;
2146 static bool expand_has_extension(struct token *token, struct arg *args)
2148 struct token *arg = args[0].expanded;
2149 struct ident *ident;
2150 bool val = false;
2152 if (token_type(arg) != TOKEN_IDENT) {
2153 sparse_error(arg->pos, "identifier expected");
2154 return false;
2157 ident = arg->ident;
2158 if (ident == &c_alignas_ident)
2159 val = true;
2160 else if (ident == &c_alignof_ident)
2161 val = true;
2162 else if (ident == &c_generic_selections_ident)
2163 val = true;
2164 else if (ident == &c_static_assert_ident)
2165 val = true;
2167 replace_with_bool(token, val);
2168 return 1;
2171 static bool expand_has_feature(struct token *token, struct arg *args)
2173 struct token *arg = args[0].expanded;
2174 struct ident *ident;
2175 bool val = false;
2177 if (token_type(arg) != TOKEN_IDENT) {
2178 sparse_error(arg->pos, "identifier expected");
2179 return false;
2182 ident = arg->ident;
2183 if (standard >= STANDARD_C11) {
2184 if (ident == &c_alignas_ident)
2185 val = true;
2186 else if (ident == &c_alignof_ident)
2187 val = true;
2188 else if (ident == &c_generic_selections_ident)
2189 val = true;
2190 else if (ident == &c_static_assert_ident)
2191 val = true;
2194 replace_with_bool(token, val);
2195 return 1;
2198 static void create_arglist(struct symbol *sym, int count)
2200 struct token *token;
2201 struct token **next;
2203 if (!count)
2204 return;
2206 token = __alloc_token(0);
2207 token_type(token) = TOKEN_ARG_COUNT;
2208 token->count.normal = count;
2209 sym->arglist = token;
2210 next = &token->next;
2212 while (count--) {
2213 struct token *id, *uses;
2214 id = __alloc_token(0);
2215 token_type(id) = TOKEN_IDENT;
2216 uses = __alloc_token(0);
2217 token_type(uses) = TOKEN_ARG_COUNT;
2218 uses->count.normal = 1;
2220 *next = id;
2221 id->next = uses;
2222 next = &uses->next;
2224 *next = &eof_token_entry;
2227 static void init_preprocessor(void)
2229 int i;
2230 int stream = init_stream(NULL, "preprocessor", -1, includepath);
2231 static struct {
2232 const char *name;
2233 int (*handler)(struct stream *, struct token **, struct token *);
2234 } normal[] = {
2235 { "define", handle_define },
2236 { "weak_define", handle_weak_define },
2237 { "strong_define", handle_strong_define },
2238 { "undef", handle_undef },
2239 { "strong_undef", handle_strong_undef },
2240 { "warning", handle_warning },
2241 { "error", handle_error },
2242 { "include", handle_include },
2243 { "include_next", handle_include_next },
2244 { "pragma", handle_pragma },
2245 { "line", handle_line },
2246 { "ident", handle_ident },
2248 // our internal preprocessor tokens
2249 { "nostdinc", handle_nostdinc },
2250 { "add_include", handle_add_include },
2251 { "add_isystem", handle_add_isystem },
2252 { "add_system", handle_add_system },
2253 { "add_dirafter", handle_add_dirafter },
2254 { "split_include", handle_split_include },
2255 { "argv_include", handle_argv_include },
2256 }, special[] = {
2257 { "ifdef", handle_ifdef },
2258 { "ifndef", handle_ifndef },
2259 { "else", handle_else },
2260 { "endif", handle_endif },
2261 { "if", handle_if },
2262 { "elif", handle_elif },
2264 static struct {
2265 const char *name;
2266 void (*expand_simple)(struct token *);
2267 bool (*expand)(struct token *, struct arg *args);
2268 } dynamic[] = {
2269 { "__LINE__", expand_line },
2270 { "__FILE__", expand_file },
2271 { "__BASE_FILE__", expand_basefile },
2272 { "__DATE__", expand_date },
2273 { "__TIME__", expand_time },
2274 { "__COUNTER__", expand_counter },
2275 { "__INCLUDE_LEVEL__", expand_include_level },
2276 { "__has_attribute", NULL, expand_has_attribute },
2277 { "__has_builtin", NULL, expand_has_builtin },
2278 { "__has_extension", NULL, expand_has_extension },
2279 { "__has_feature", NULL, expand_has_feature },
2282 for (i = 0; i < ARRAY_SIZE(normal); i++) {
2283 struct symbol *sym;
2284 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
2285 sym->handler = normal[i].handler;
2286 sym->normal = 1;
2288 for (i = 0; i < ARRAY_SIZE(special); i++) {
2289 struct symbol *sym;
2290 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
2291 sym->handler = special[i].handler;
2292 sym->normal = 0;
2294 for (i = 0; i < ARRAY_SIZE(dynamic); i++) {
2295 struct symbol *sym;
2296 sym = create_symbol(stream, dynamic[i].name, SYM_NODE, NS_MACRO);
2297 sym->expand_simple = dynamic[i].expand_simple;
2298 if ((sym->expand = dynamic[i].expand) != NULL)
2299 create_arglist(sym, 1);
2302 counter_macro = 0;
2305 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
2307 int (*handler)(struct stream *, struct token **, struct token *);
2308 struct token *token = start->next;
2309 int is_normal = 1;
2310 int is_cond = 0; // is one of {is,ifdef,ifndef,elif,else,endif}
2312 if (eof_token(token))
2313 return;
2315 if (token_type(token) == TOKEN_IDENT) {
2316 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
2317 if (sym) {
2318 handler = sym->handler;
2319 is_normal = sym->normal;
2320 is_cond = !sym->normal;
2321 } else {
2322 handler = handle_nondirective;
2324 } else if (token_type(token) == TOKEN_NUMBER) {
2325 handler = handle_line;
2326 } else {
2327 handler = handle_nondirective;
2330 if (is_normal) {
2331 dirty_stream(stream);
2332 if (false_nesting)
2333 goto out;
2336 if (expanding) {
2337 if (!is_cond || Wpedantic)
2338 warning(start->pos, "directive in macro's argument list");
2340 if (!handler(stream, line, token)) /* all set */
2341 return;
2343 out:
2344 free_preprocessor_line(token);
2347 static void preprocessor_line(struct stream *stream, struct token **line)
2349 struct token *start = *line, *next;
2350 struct token **tp = &start->next;
2352 for (;;) {
2353 next = *tp;
2354 if (next->pos.newline)
2355 break;
2356 tp = &next->next;
2358 *line = next;
2359 *tp = &eof_token_entry;
2360 handle_preprocessor_line(stream, line, start);
2363 static void do_preprocess(struct token **list)
2365 struct token *next;
2367 while (!eof_token(next = scan_next(list))) {
2368 struct stream *stream = input_streams + next->pos.stream;
2370 if (next->pos.newline && match_op(next, '#')) {
2371 if (!next->pos.noexpand) {
2372 preprocessor_line(stream, list);
2373 __free_token(next); /* Free the '#' token */
2374 continue;
2378 switch (token_type(next)) {
2379 case TOKEN_STREAMEND:
2380 if (stream->top_if) {
2381 nesting_error(stream);
2382 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
2383 stream->top_if = NULL;
2384 false_nesting = 0;
2386 if (!stream->dirty)
2387 stream->constant = CONSTANT_FILE_YES;
2388 *list = next->next;
2389 include_level--;
2390 continue;
2391 case TOKEN_STREAMBEGIN:
2392 *list = next->next;
2393 include_level++;
2394 continue;
2396 default:
2397 dirty_stream(stream);
2398 if (false_nesting) {
2399 *list = next->next;
2400 __free_token(next);
2401 continue;
2404 if (token_type(next) != TOKEN_IDENT ||
2405 expand_one_symbol(list))
2406 list = &next->next;
2411 void init_include_path(void)
2413 FILE *fp;
2414 char path[256];
2415 char arch[32];
2416 char os[32];
2418 fp = popen("/bin/uname -m", "r");
2419 if (!fp)
2420 return;
2421 if (!fgets(arch, sizeof(arch) - 1, fp))
2422 return;
2423 pclose(fp);
2424 if (arch[strlen(arch) - 1] == '\n')
2425 arch[strlen(arch) - 1] = '\0';
2427 fp = popen("/bin/uname -o", "r");
2428 if (!fp)
2429 return;
2430 fgets(os, sizeof(os) - 1, fp);
2431 pclose(fp);
2433 if (strcmp(os, "GNU/Linux\n") != 0)
2434 return;
2435 strcpy(os, "linux-gnu");
2437 snprintf(path, sizeof(path), "/usr/include/%s-%s/", arch, os);
2438 add_pre_buffer("#add_system \"%s/\"\n", path);
2441 struct token * preprocess(struct token *token)
2443 preprocessing = 1;
2444 init_preprocessor();
2445 do_preprocess(&token);
2447 // Drop all expressions from preprocessing, they're not used any more.
2448 // This is not true when we have multiple files, though ;/
2449 // clear_expression_alloc();
2450 preprocessing = 0;
2452 return token;
2455 static int is_VA_ARGS_token(struct token *token)
2457 return (token_type(token) == TOKEN_IDENT) &&
2458 (token->ident == &__VA_ARGS___ident);
2461 static void dump_macro(struct symbol *sym)
2463 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
2464 struct token *args[nargs];
2465 struct token *token;
2467 printf("#define %s", show_ident(sym->ident));
2468 token = sym->arglist;
2469 if (token) {
2470 const char *sep = "";
2471 int narg = 0;
2472 putchar('(');
2473 for (; !eof_token(token); token = token->next) {
2474 if (token_type(token) == TOKEN_ARG_COUNT)
2475 continue;
2476 if (is_VA_ARGS_token(token))
2477 printf("%s...", sep);
2478 else
2479 printf("%s%s", sep, show_token(token));
2480 args[narg++] = token;
2481 sep = ",";
2483 putchar(')');
2486 token = sym->expansion;
2487 while (token_type(token) != TOKEN_UNTAINT) {
2488 struct token *next = token->next;
2489 if (token->pos.whitespace)
2490 putchar(' ');
2491 switch (token_type(token)) {
2492 case TOKEN_CONCAT:
2493 printf("##");
2494 break;
2495 case TOKEN_STR_ARGUMENT:
2496 printf("#");
2497 /* fall-through */
2498 case TOKEN_QUOTED_ARGUMENT:
2499 case TOKEN_MACRO_ARGUMENT:
2500 token = args[token->argnum];
2501 /* fall-through */
2502 default:
2503 printf("%s", show_token(token));
2505 token = next;
2507 putchar('\n');
2510 void dump_macro_definitions(void)
2512 struct ident *name;
2514 FOR_EACH_PTR(macros, name) {
2515 struct symbol *sym = lookup_macro(name);
2516 if (sym)
2517 dump_macro(sym);
2518 } END_FOR_EACH_PTR(name);