fix cgcc ELF version for ppc64/pcc64le
[smatch.git] / pre-process.c
blob8800dce5367725b5c5f679277fc6fa045db97cb5
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 struct ident_list *macros; // only needed for -dD
48 static int false_nesting = 0;
49 static int counter_macro = 0; // __COUNTER__ expansion
51 #define INCLUDEPATHS 300
52 const char *includepath[INCLUDEPATHS+1] = {
53 "",
54 "/usr/include",
55 "/usr/local/include",
56 NULL
59 static const char **quote_includepath = includepath;
60 static const char **angle_includepath = includepath + 1;
61 static const char **isys_includepath = includepath + 1;
62 static const char **sys_includepath = includepath + 1;
63 static const char **dirafter_includepath = includepath + 3;
65 #define dirty_stream(stream) \
66 do { \
67 if (!stream->dirty) { \
68 stream->dirty = 1; \
69 if (!stream->ifndef) \
70 stream->protect = NULL; \
71 } \
72 } while(0)
74 #define end_group(stream) \
75 do { \
76 if (stream->ifndef == stream->top_if) { \
77 stream->ifndef = NULL; \
78 if (!stream->dirty) \
79 stream->protect = NULL; \
80 else if (stream->protect) \
81 stream->dirty = 0; \
82 } \
83 } while(0)
85 #define nesting_error(stream) \
86 do { \
87 stream->dirty = 1; \
88 stream->ifndef = NULL; \
89 stream->protect = NULL; \
90 } while(0)
92 static struct token *alloc_token(struct position *pos)
94 struct token *token = __alloc_token(0);
96 token->pos.stream = pos->stream;
97 token->pos.line = pos->line;
98 token->pos.pos = pos->pos;
99 token->pos.whitespace = 1;
100 return token;
103 /* Expand symbol 'sym' at '*list' */
104 static int expand(struct token **, struct symbol *);
106 static void replace_with_string(struct token *token, const char *str)
108 int size = strlen(str) + 1;
109 struct string *s = __alloc_string(size);
111 s->length = size;
112 memcpy(s->data, str, size);
113 token_type(token) = TOKEN_STRING;
114 token->string = s;
117 static void replace_with_integer(struct token *token, unsigned int val)
119 char *buf = __alloc_bytes(11);
120 sprintf(buf, "%u", val);
121 token_type(token) = TOKEN_NUMBER;
122 token->number = buf;
125 static struct symbol *lookup_macro(struct ident *ident)
127 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
128 if (sym && sym->namespace != NS_MACRO)
129 sym = NULL;
130 return sym;
133 static int token_defined(struct token *token)
135 if (token_type(token) == TOKEN_IDENT) {
136 struct symbol *sym = lookup_macro(token->ident);
137 if (sym) {
138 sym->used_in = file_scope;
139 return 1;
141 return 0;
144 sparse_error(token->pos, "expected preprocessor identifier");
145 return 0;
148 static void replace_with_defined(struct token *token)
150 static const char *string[] = { "0", "1" };
151 int defined = token_defined(token);
153 token_type(token) = TOKEN_NUMBER;
154 token->number = string[defined];
157 static int expand_one_symbol(struct token **list)
159 struct token *token = *list;
160 struct symbol *sym;
161 static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
162 static time_t t = 0;
164 if (token->pos.noexpand)
165 return 1;
167 sym = lookup_macro(token->ident);
168 if (sym) {
169 sym->used_in = file_scope;
170 return expand(list, sym);
172 if (token->ident == &__LINE___ident) {
173 replace_with_integer(token, token->pos.line);
174 } else if (token->ident == &__FILE___ident) {
175 replace_with_string(token, stream_name(token->pos.stream));
176 } else if (token->ident == &__DATE___ident) {
177 if (!t)
178 time(&t);
179 strftime(buffer, 12, "%b %e %Y", localtime(&t));
180 replace_with_string(token, buffer);
181 } else if (token->ident == &__TIME___ident) {
182 if (!t)
183 time(&t);
184 strftime(buffer, 9, "%T", localtime(&t));
185 replace_with_string(token, buffer);
186 } else if (token->ident == &__COUNTER___ident) {
187 replace_with_integer(token, counter_macro++);
189 return 1;
192 static inline struct token *scan_next(struct token **where)
194 struct token *token = *where;
195 if (token_type(token) != TOKEN_UNTAINT)
196 return token;
197 do {
198 token->ident->tainted = 0;
199 token = token->next;
200 } while (token_type(token) == TOKEN_UNTAINT);
201 *where = token;
202 return token;
205 static void expand_list(struct token **list)
207 struct token *next;
208 while (!eof_token(next = scan_next(list))) {
209 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
210 list = &next->next;
214 static void preprocessor_line(struct stream *stream, struct token **line);
216 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos, int count)
218 struct stream *stream = input_streams + prev->pos.stream;
219 struct token **p = &prev->next;
220 struct token *next;
221 int nesting = 0;
223 while (!eof_token(next = scan_next(p))) {
224 if (next->pos.newline && match_op(next, '#')) {
225 if (!next->pos.noexpand) {
226 sparse_error(next->pos,
227 "directive in argument list");
228 preprocessor_line(stream, p);
229 __free_token(next); /* Free the '#' token */
230 continue;
233 switch (token_type(next)) {
234 case TOKEN_STREAMEND:
235 case TOKEN_STREAMBEGIN:
236 *p = &eof_token_entry;
237 return next;
238 case TOKEN_STRING:
239 case TOKEN_WIDE_STRING:
240 if (count > 1)
241 next->string->immutable = 1;
242 break;
244 if (false_nesting) {
245 *p = next->next;
246 __free_token(next);
247 continue;
249 if (match_op(next, '(')) {
250 nesting++;
251 } else if (match_op(next, ')')) {
252 if (!nesting--)
253 break;
254 } else if (match_op(next, ',') && !nesting && !vararg) {
255 break;
257 next->pos.stream = pos->stream;
258 next->pos.line = pos->line;
259 next->pos.pos = pos->pos;
260 p = &next->next;
262 *p = &eof_token_entry;
263 return next;
267 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
270 struct arg {
271 struct token *arg;
272 struct token *expanded;
273 struct token *str;
274 int n_normal;
275 int n_quoted;
276 int n_str;
279 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
281 int wanted = arglist->count.normal;
282 struct token *next = NULL;
283 int count = 0;
285 arglist = arglist->next; /* skip counter */
287 if (!wanted) {
288 next = collect_arg(start, 0, &what->pos, 0);
289 if (eof_token(next))
290 goto Eclosing;
291 if (!eof_token(start->next) || !match_op(next, ')')) {
292 count++;
293 goto Emany;
295 } else {
296 for (count = 0; count < wanted; count++) {
297 struct argcount *p = &arglist->next->count;
298 next = collect_arg(start, p->vararg, &what->pos, p->normal);
299 if (eof_token(next))
300 goto Eclosing;
301 if (p->vararg && wanted == 1 && eof_token(start->next))
302 break;
303 arglist = arglist->next->next;
304 args[count].arg = start->next;
305 args[count].n_normal = p->normal;
306 args[count].n_quoted = p->quoted;
307 args[count].n_str = p->str;
308 if (match_op(next, ')')) {
309 count++;
310 break;
312 start = next;
314 if (count == wanted && !match_op(next, ')'))
315 goto Emany;
316 if (count == wanted - 1) {
317 struct argcount *p = &arglist->next->count;
318 if (!p->vararg)
319 goto Efew;
320 args[count].arg = NULL;
321 args[count].n_normal = p->normal;
322 args[count].n_quoted = p->quoted;
323 args[count].n_str = p->str;
325 if (count < wanted - 1)
326 goto Efew;
328 what->next = next->next;
329 return 1;
331 Efew:
332 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
333 show_token(what), wanted, count);
334 goto out;
335 Emany:
336 while (match_op(next, ',')) {
337 next = collect_arg(next, 0, &what->pos, 0);
338 count++;
340 if (eof_token(next))
341 goto Eclosing;
342 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
343 show_token(what), count, wanted);
344 goto out;
345 Eclosing:
346 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
347 show_token(what));
348 out:
349 what->next = next->next;
350 return 0;
353 static struct token *dup_list(struct token *list)
355 struct token *res = NULL;
356 struct token **p = &res;
358 while (!eof_token(list)) {
359 struct token *newtok = __alloc_token(0);
360 *newtok = *list;
361 *p = newtok;
362 p = &newtok->next;
363 list = list->next;
365 return res;
368 static const char *show_token_sequence(struct token *token, int quote)
370 static char buffer[MAX_STRING];
371 char *ptr = buffer;
372 int whitespace = 0;
374 if (!token && !quote)
375 return "<none>";
376 while (!eof_token(token)) {
377 const char *val = quote ? quote_token(token) : show_token(token);
378 int len = strlen(val);
380 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
381 sparse_error(token->pos, "too long token expansion");
382 break;
385 if (whitespace)
386 *ptr++ = ' ';
387 memcpy(ptr, val, len);
388 ptr += len;
389 token = token->next;
390 whitespace = token->pos.whitespace;
392 *ptr = 0;
393 return buffer;
396 static struct token *stringify(struct token *arg)
398 const char *s = show_token_sequence(arg, 1);
399 int size = strlen(s)+1;
400 struct token *token = __alloc_token(0);
401 struct string *string = __alloc_string(size);
403 memcpy(string->data, s, size);
404 string->length = size;
405 token->pos = arg->pos;
406 token_type(token) = TOKEN_STRING;
407 token->string = string;
408 token->next = &eof_token_entry;
409 return token;
412 static void expand_arguments(int count, struct arg *args)
414 int i;
415 for (i = 0; i < count; i++) {
416 struct token *arg = args[i].arg;
417 if (!arg)
418 arg = &eof_token_entry;
419 if (args[i].n_str)
420 args[i].str = stringify(arg);
421 if (args[i].n_normal) {
422 if (!args[i].n_quoted) {
423 args[i].expanded = arg;
424 args[i].arg = NULL;
425 } else if (eof_token(arg)) {
426 args[i].expanded = arg;
427 } else {
428 args[i].expanded = dup_list(arg);
430 expand_list(&args[i].expanded);
436 * Possibly valid combinations:
437 * - ident + ident -> ident
438 * - ident + number -> ident unless number contains '.', '+' or '-'.
439 * - 'L' + char constant -> wide char constant
440 * - 'L' + string literal -> wide string literal
441 * - number + number -> number
442 * - number + ident -> number
443 * - number + '.' -> number
444 * - number + '+' or '-' -> number, if number used to end on [eEpP].
445 * - '.' + number -> number, if number used to start with a digit.
446 * - special + special -> either special or an error.
448 static enum token_type combine(struct token *left, struct token *right, char *p)
450 int len;
451 enum token_type t1 = token_type(left), t2 = token_type(right);
453 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
454 return TOKEN_ERROR;
456 if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
457 if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
458 return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
459 if (t2 == TOKEN_STRING)
460 return TOKEN_WIDE_STRING;
463 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
464 return TOKEN_ERROR;
466 strcpy(p, show_token(left));
467 strcat(p, show_token(right));
468 len = strlen(p);
470 if (len >= 256)
471 return TOKEN_ERROR;
473 if (t1 == TOKEN_IDENT) {
474 if (t2 == TOKEN_SPECIAL)
475 return TOKEN_ERROR;
476 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
477 return TOKEN_ERROR;
478 return TOKEN_IDENT;
481 if (t1 == TOKEN_NUMBER) {
482 if (t2 == TOKEN_SPECIAL) {
483 switch (right->special) {
484 case '.':
485 break;
486 case '+': case '-':
487 if (strchr("eEpP", p[len - 2]))
488 break;
489 default:
490 return TOKEN_ERROR;
493 return TOKEN_NUMBER;
496 if (p[0] == '.' && isdigit((unsigned char)p[1]))
497 return TOKEN_NUMBER;
499 return TOKEN_SPECIAL;
502 static int merge(struct token *left, struct token *right)
504 static char buffer[512];
505 enum token_type res = combine(left, right, buffer);
506 int n;
508 switch (res) {
509 case TOKEN_IDENT:
510 left->ident = built_in_ident(buffer);
511 left->pos.noexpand = 0;
512 return 1;
514 case TOKEN_NUMBER: {
515 char *number = __alloc_bytes(strlen(buffer) + 1);
516 memcpy(number, buffer, strlen(buffer) + 1);
517 token_type(left) = TOKEN_NUMBER; /* could be . + num */
518 left->number = number;
519 return 1;
522 case TOKEN_SPECIAL:
523 if (buffer[2] && buffer[3])
524 break;
525 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
526 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
527 left->special = n;
528 return 1;
531 break;
533 case TOKEN_WIDE_CHAR:
534 case TOKEN_WIDE_STRING:
535 token_type(left) = res;
536 left->pos.noexpand = 0;
537 left->string = right->string;
538 return 1;
540 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
541 token_type(left) = res;
542 left->pos.noexpand = 0;
543 memcpy(left->embedded, right->embedded, 4);
544 return 1;
546 default:
549 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
550 return 0;
553 static struct token *dup_token(struct token *token, struct position *streampos)
555 struct token *alloc = alloc_token(streampos);
556 token_type(alloc) = token_type(token);
557 alloc->pos.newline = token->pos.newline;
558 alloc->pos.whitespace = token->pos.whitespace;
559 alloc->number = token->number;
560 alloc->pos.noexpand = token->pos.noexpand;
561 return alloc;
564 static struct token **copy(struct token **where, struct token *list, int *count)
566 int need_copy = --*count;
567 while (!eof_token(list)) {
568 struct token *token;
569 if (need_copy)
570 token = dup_token(list, &list->pos);
571 else
572 token = list;
573 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
574 token->pos.noexpand = 1;
575 *where = token;
576 where = &token->next;
577 list = list->next;
579 *where = &eof_token_entry;
580 return where;
583 static int handle_kludge(struct token **p, struct arg *args)
585 struct token *t = (*p)->next->next;
586 while (1) {
587 struct arg *v = &args[t->argnum];
588 if (token_type(t->next) != TOKEN_CONCAT) {
589 if (v->arg) {
590 /* ignore the first ## */
591 *p = (*p)->next;
592 return 0;
594 /* skip the entire thing */
595 *p = t;
596 return 1;
598 if (v->arg && !eof_token(v->arg))
599 return 0; /* no magic */
600 t = t->next->next;
604 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
606 struct position *base_pos = &(*list)->pos;
607 int *count;
608 enum {Normal, Placeholder, Concat} state = Normal;
610 for (; !eof_token(body); body = body->next) {
611 struct token *added, *arg;
612 struct token **tail;
613 struct token *t;
615 switch (token_type(body)) {
616 case TOKEN_GNU_KLUDGE:
618 * GNU kludge: if we had <comma>##<vararg>, behaviour
619 * depends on whether we had enough arguments to have
620 * a vararg. If we did, ## is just ignored. Otherwise
621 * both , and ## are ignored. Worse, there can be
622 * an arbitrary number of ##<arg> in between; if all of
623 * those are empty, we act as if they hadn't been there,
624 * otherwise we act as if the kludge didn't exist.
626 t = body;
627 if (handle_kludge(&body, args)) {
628 if (state == Concat)
629 state = Normal;
630 else
631 state = Placeholder;
632 continue;
634 added = dup_token(t, base_pos);
635 token_type(added) = TOKEN_SPECIAL;
636 tail = &added->next;
637 break;
639 case TOKEN_STR_ARGUMENT:
640 arg = args[body->argnum].str;
641 count = &args[body->argnum].n_str;
642 goto copy_arg;
644 case TOKEN_QUOTED_ARGUMENT:
645 arg = args[body->argnum].arg;
646 count = &args[body->argnum].n_quoted;
647 if (!arg || eof_token(arg)) {
648 if (state == Concat)
649 state = Normal;
650 else
651 state = Placeholder;
652 continue;
654 goto copy_arg;
656 case TOKEN_MACRO_ARGUMENT:
657 arg = args[body->argnum].expanded;
658 count = &args[body->argnum].n_normal;
659 if (eof_token(arg)) {
660 state = Normal;
661 continue;
663 copy_arg:
664 tail = copy(&added, arg, count);
665 added->pos.newline = body->pos.newline;
666 added->pos.whitespace = body->pos.whitespace;
667 break;
669 case TOKEN_CONCAT:
670 if (state == Placeholder)
671 state = Normal;
672 else
673 state = Concat;
674 continue;
676 case TOKEN_IDENT:
677 added = dup_token(body, base_pos);
678 if (added->ident->tainted)
679 added->pos.noexpand = 1;
680 tail = &added->next;
681 break;
683 default:
684 added = dup_token(body, base_pos);
685 tail = &added->next;
686 break;
690 * if we got to doing real concatenation, we already have
691 * added something into the list, so containing_token() is OK.
693 if (state == Concat && merge(containing_token(list), added)) {
694 *list = added->next;
695 if (tail != &added->next)
696 list = tail;
697 } else {
698 *list = added;
699 list = tail;
701 state = Normal;
703 *list = &eof_token_entry;
704 return list;
707 static int expand(struct token **list, struct symbol *sym)
709 struct token *last;
710 struct token *token = *list;
711 struct ident *expanding = token->ident;
712 struct token **tail;
713 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
714 struct arg args[nargs];
716 if (expanding->tainted) {
717 token->pos.noexpand = 1;
718 return 1;
721 if (sym->arglist) {
722 if (!match_op(scan_next(&token->next), '('))
723 return 1;
724 if (!collect_arguments(token->next, sym->arglist, args, token))
725 return 1;
726 expand_arguments(nargs, args);
729 expanding->tainted = 1;
731 last = token->next;
732 tail = substitute(list, sym->expansion, args);
734 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
735 * We still can lose the newline flag if the sucker expands to nothing,
736 * but the price of dealing with that is probably too high (we'd need
737 * to collect the flags during scan_next())
739 (*list)->pos.newline = token->pos.newline;
740 (*list)->pos.whitespace = token->pos.whitespace;
741 *tail = last;
743 return 0;
746 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
748 static char buffer[256];
749 char *ptr = buffer;
751 while (!eof_token(token) && !match_op(token, endop)) {
752 int len;
753 const char *val = token->string->data;
754 if (token_type(token) != TOKEN_STRING)
755 val = show_token(token);
756 len = strlen(val);
757 memcpy(ptr, val, len);
758 ptr += len;
759 token = token->next;
761 *ptr = 0;
762 if (endop && !match_op(token, endop))
763 sparse_error(start->pos, "expected '>' at end of filename");
764 return buffer;
767 static int already_tokenized(const char *path)
769 int stream, next;
771 for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
772 struct stream *s = input_streams + stream;
774 next = s->next_stream;
775 if (s->once) {
776 if (strcmp(path, s->name))
777 continue;
778 return 1;
780 if (s->constant != CONSTANT_FILE_YES)
781 continue;
782 if (strcmp(path, s->name))
783 continue;
784 if (s->protect && !lookup_macro(s->protect))
785 continue;
786 return 1;
788 return 0;
791 /* Handle include of header files.
792 * The relevant options are made compatible with gcc. The only options that
793 * are not supported is -withprefix and friends.
795 * Three set of include paths are known:
796 * quote_includepath: Path to search when using #include "file.h"
797 * angle_includepath: Paths to search when using #include <file.h>
798 * isys_includepath: Paths specified with -isystem, come before the
799 * built-in system include paths. Gcc would suppress
800 * warnings from system headers. Here we separate
801 * them from the angle_ ones to keep search ordering.
803 * sys_includepath: Built-in include paths.
804 * dirafter_includepath Paths added with -dirafter.
806 * The above is implemented as one array with pointers
807 * +--------------+
808 * quote_includepath ---> | |
809 * +--------------+
810 * | |
811 * +--------------+
812 * angle_includepath ---> | |
813 * +--------------+
814 * isys_includepath ---> | |
815 * +--------------+
816 * sys_includepath ---> | |
817 * +--------------+
818 * dirafter_includepath -> | |
819 * +--------------+
821 * -I dir insert dir just before isys_includepath and move the rest
822 * -I- makes all dirs specified with -I before to quote dirs only and
823 * angle_includepath is set equal to isys_includepath.
824 * -nostdinc removes all sys dirs by storing NULL in entry pointed
825 * to by * sys_includepath. Note that this will reset all dirs built-in
826 * and added before -nostdinc by -isystem and -idirafter.
827 * -isystem dir adds dir where isys_includepath points adding this dir as
828 * first systemdir
829 * -idirafter dir adds dir to the end of the list
832 static void set_stream_include_path(struct stream *stream)
834 const char *path = stream->path;
835 if (!path) {
836 const char *p = strrchr(stream->name, '/');
837 path = "";
838 if (p) {
839 int len = p - stream->name + 1;
840 char *m = malloc(len+1);
841 /* This includes the final "/" */
842 memcpy(m, stream->name, len);
843 m[len] = 0;
844 path = m;
846 stream->path = path;
848 includepath[0] = path;
851 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
853 int fd;
854 int plen = strlen(path);
855 static char fullname[PATH_MAX];
857 memcpy(fullname, path, plen);
858 if (plen && path[plen-1] != '/') {
859 fullname[plen] = '/';
860 plen++;
862 memcpy(fullname+plen, filename, flen);
863 if (already_tokenized(fullname))
864 return 1;
865 fd = open(fullname, O_RDONLY);
866 if (fd >= 0) {
867 char * streamname = __alloc_bytes(plen + flen);
868 memcpy(streamname, fullname, plen + flen);
869 *where = tokenize(streamname, fd, *where, next_path);
870 close(fd);
871 return 1;
873 return 0;
876 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
878 const char *path;
880 while ((path = *pptr++) != NULL) {
881 if (!try_include(path, filename, flen, list, pptr))
882 continue;
883 return 1;
885 return 0;
888 static int free_preprocessor_line(struct token *token)
890 while (token_type(token) != TOKEN_EOF) {
891 struct token *free = token;
892 token = token->next;
893 __free_token(free);
895 return 1;
898 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how)
900 const char *filename;
901 struct token *next;
902 const char **path;
903 int expect;
904 int flen;
906 next = token->next;
907 expect = '>';
908 if (!match_op(next, '<')) {
909 expand_list(&token->next);
910 expect = 0;
911 next = token;
912 if (match_op(token->next, '<')) {
913 next = token->next;
914 expect = '>';
918 token = next->next;
919 filename = token_name_sequence(token, expect, token);
920 flen = strlen(filename) + 1;
922 /* Absolute path? */
923 if (filename[0] == '/') {
924 if (try_include("", filename, flen, list, includepath))
925 return 0;
926 goto out;
929 switch (how) {
930 case 1:
931 path = stream->next_path;
932 break;
933 case 2:
934 includepath[0] = "";
935 path = includepath;
936 break;
937 default:
938 /* Dir of input file is first dir to search for quoted includes */
939 set_stream_include_path(stream);
940 path = expect ? angle_includepath : quote_includepath;
941 break;
943 /* Check the standard include paths.. */
944 if (do_include_path(path, list, token, filename, flen))
945 return 0;
946 out:
947 error_die(token->pos, "unable to open '%s'", filename);
950 static int handle_include(struct stream *stream, struct token **list, struct token *token)
952 return handle_include_path(stream, list, token, 0);
955 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
957 return handle_include_path(stream, list, token, 1);
960 static int handle_argv_include(struct stream *stream, struct token **list, struct token *token)
962 return handle_include_path(stream, list, token, 2);
965 static int token_different(struct token *t1, struct token *t2)
967 int different;
969 if (token_type(t1) != token_type(t2))
970 return 1;
972 switch (token_type(t1)) {
973 case TOKEN_IDENT:
974 different = t1->ident != t2->ident;
975 break;
976 case TOKEN_ARG_COUNT:
977 case TOKEN_UNTAINT:
978 case TOKEN_CONCAT:
979 case TOKEN_GNU_KLUDGE:
980 different = 0;
981 break;
982 case TOKEN_NUMBER:
983 different = strcmp(t1->number, t2->number);
984 break;
985 case TOKEN_SPECIAL:
986 different = t1->special != t2->special;
987 break;
988 case TOKEN_MACRO_ARGUMENT:
989 case TOKEN_QUOTED_ARGUMENT:
990 case TOKEN_STR_ARGUMENT:
991 different = t1->argnum != t2->argnum;
992 break;
993 case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
994 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
995 different = memcmp(t1->embedded, t2->embedded, 4);
996 break;
997 case TOKEN_CHAR:
998 case TOKEN_WIDE_CHAR:
999 case TOKEN_STRING:
1000 case TOKEN_WIDE_STRING: {
1001 struct string *s1, *s2;
1003 s1 = t1->string;
1004 s2 = t2->string;
1005 different = 1;
1006 if (s1->length != s2->length)
1007 break;
1008 different = memcmp(s1->data, s2->data, s1->length);
1009 break;
1011 default:
1012 different = 1;
1013 break;
1015 return different;
1018 static int token_list_different(struct token *list1, struct token *list2)
1020 for (;;) {
1021 if (list1 == list2)
1022 return 0;
1023 if (!list1 || !list2)
1024 return 1;
1025 if (token_different(list1, list2))
1026 return 1;
1027 list1 = list1->next;
1028 list2 = list2->next;
1032 static inline void set_arg_count(struct token *token)
1034 token_type(token) = TOKEN_ARG_COUNT;
1035 token->count.normal = token->count.quoted =
1036 token->count.str = token->count.vararg = 0;
1039 static struct token *parse_arguments(struct token *list)
1041 struct token *arg = list->next, *next = list;
1042 struct argcount *count = &list->count;
1044 set_arg_count(list);
1046 if (match_op(arg, ')')) {
1047 next = arg->next;
1048 list->next = &eof_token_entry;
1049 return next;
1052 while (token_type(arg) == TOKEN_IDENT) {
1053 if (arg->ident == &__VA_ARGS___ident)
1054 goto Eva_args;
1055 if (!++count->normal)
1056 goto Eargs;
1057 next = arg->next;
1059 if (match_op(next, ',')) {
1060 set_arg_count(next);
1061 arg = next->next;
1062 continue;
1065 if (match_op(next, ')')) {
1066 set_arg_count(next);
1067 next = next->next;
1068 arg->next->next = &eof_token_entry;
1069 return next;
1072 /* normal cases are finished here */
1074 if (match_op(next, SPECIAL_ELLIPSIS)) {
1075 if (match_op(next->next, ')')) {
1076 set_arg_count(next);
1077 next->count.vararg = 1;
1078 next = next->next;
1079 arg->next->next = &eof_token_entry;
1080 return next->next;
1083 arg = next;
1084 goto Enotclosed;
1087 if (eof_token(next)) {
1088 goto Enotclosed;
1089 } else {
1090 arg = next;
1091 goto Ebadstuff;
1095 if (match_op(arg, SPECIAL_ELLIPSIS)) {
1096 next = arg->next;
1097 token_type(arg) = TOKEN_IDENT;
1098 arg->ident = &__VA_ARGS___ident;
1099 if (!match_op(next, ')'))
1100 goto Enotclosed;
1101 if (!++count->normal)
1102 goto Eargs;
1103 set_arg_count(next);
1104 next->count.vararg = 1;
1105 next = next->next;
1106 arg->next->next = &eof_token_entry;
1107 return next;
1110 if (eof_token(arg)) {
1111 arg = next;
1112 goto Enotclosed;
1114 if (match_op(arg, ','))
1115 goto Emissing;
1116 else
1117 goto Ebadstuff;
1120 Emissing:
1121 sparse_error(arg->pos, "parameter name missing");
1122 return NULL;
1123 Ebadstuff:
1124 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
1125 show_token(arg));
1126 return NULL;
1127 Enotclosed:
1128 sparse_error(arg->pos, "missing ')' in macro parameter list");
1129 return NULL;
1130 Eva_args:
1131 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1132 return NULL;
1133 Eargs:
1134 sparse_error(arg->pos, "too many arguments in macro definition");
1135 return NULL;
1138 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1140 struct ident *ident = token->ident;
1141 int nr;
1143 if (!arglist || token_type(token) != TOKEN_IDENT)
1144 return 0;
1146 arglist = arglist->next;
1148 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1149 if (arglist->ident == ident) {
1150 struct argcount *count = &arglist->next->count;
1151 int n;
1153 token->argnum = nr;
1154 token_type(token) = type;
1155 switch (type) {
1156 case TOKEN_MACRO_ARGUMENT:
1157 n = ++count->normal;
1158 break;
1159 case TOKEN_QUOTED_ARGUMENT:
1160 n = ++count->quoted;
1161 break;
1162 default:
1163 n = ++count->str;
1165 if (n)
1166 return count->vararg ? 2 : 1;
1168 * XXX - need saner handling of that
1169 * (>= 1024 instances of argument)
1171 token_type(token) = TOKEN_ERROR;
1172 return -1;
1175 return 0;
1178 static struct token *handle_hash(struct token **p, struct token *arglist)
1180 struct token *token = *p;
1181 if (arglist) {
1182 struct token *next = token->next;
1183 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1184 goto Equote;
1185 next->pos.whitespace = token->pos.whitespace;
1186 __free_token(token);
1187 token = *p = next;
1188 } else {
1189 token->pos.noexpand = 1;
1191 return token;
1193 Equote:
1194 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1195 return NULL;
1198 /* token->next is ## */
1199 static struct token *handle_hashhash(struct token *token, struct token *arglist)
1201 struct token *last = token;
1202 struct token *concat;
1203 int state = match_op(token, ',');
1205 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1207 while (1) {
1208 struct token *t;
1209 int is_arg;
1211 /* eat duplicate ## */
1212 concat = token->next;
1213 while (match_op(t = concat->next, SPECIAL_HASHHASH)) {
1214 token->next = t;
1215 __free_token(concat);
1216 concat = t;
1218 token_type(concat) = TOKEN_CONCAT;
1220 if (eof_token(t))
1221 goto Econcat;
1223 if (match_op(t, '#')) {
1224 t = handle_hash(&concat->next, arglist);
1225 if (!t)
1226 return NULL;
1229 is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
1231 if (state == 1 && is_arg) {
1232 state = is_arg;
1233 } else {
1234 last = t;
1235 state = match_op(t, ',');
1238 token = t;
1239 if (!match_op(token->next, SPECIAL_HASHHASH))
1240 break;
1242 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1243 if (state == 2)
1244 token_type(last) = TOKEN_GNU_KLUDGE;
1245 return token;
1247 Econcat:
1248 sparse_error(concat->pos, "'##' cannot appear at the ends of macro expansion");
1249 return NULL;
1252 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1254 struct token *token = expansion;
1255 struct token **p;
1257 if (match_op(token, SPECIAL_HASHHASH))
1258 goto Econcat;
1260 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1261 if (match_op(token, '#')) {
1262 token = handle_hash(p, arglist);
1263 if (!token)
1264 return NULL;
1266 if (match_op(token->next, SPECIAL_HASHHASH)) {
1267 token = handle_hashhash(token, arglist);
1268 if (!token)
1269 return NULL;
1270 } else {
1271 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1273 switch (token_type(token)) {
1274 case TOKEN_ERROR:
1275 goto Earg;
1277 case TOKEN_STRING:
1278 case TOKEN_WIDE_STRING:
1279 token->string->immutable = 1;
1280 break;
1283 token = alloc_token(&expansion->pos);
1284 token_type(token) = TOKEN_UNTAINT;
1285 token->ident = name;
1286 token->next = *p;
1287 *p = token;
1288 return expansion;
1290 Econcat:
1291 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1292 return NULL;
1293 Earg:
1294 sparse_error(token->pos, "too many instances of argument in body");
1295 return NULL;
1298 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1300 struct token *arglist, *expansion;
1301 struct token *left = token->next;
1302 struct symbol *sym;
1303 struct ident *name;
1304 int ret;
1306 if (token_type(left) != TOKEN_IDENT) {
1307 sparse_error(token->pos, "expected identifier to 'define'");
1308 return 1;
1311 name = left->ident;
1313 arglist = NULL;
1314 expansion = left->next;
1315 if (!expansion->pos.whitespace) {
1316 if (match_op(expansion, '(')) {
1317 arglist = expansion;
1318 expansion = parse_arguments(expansion);
1319 if (!expansion)
1320 return 1;
1321 } else if (!eof_token(expansion)) {
1322 warning(expansion->pos,
1323 "no whitespace before object-like macro body");
1327 expansion = parse_expansion(expansion, arglist, name);
1328 if (!expansion)
1329 return 1;
1331 ret = 1;
1332 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1333 if (sym) {
1334 int clean;
1336 if (attr < sym->attr)
1337 goto out;
1339 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1341 if (token_list_different(sym->expansion, expansion) ||
1342 token_list_different(sym->arglist, arglist)) {
1343 ret = 0;
1344 if ((clean && attr == SYM_ATTR_NORMAL)
1345 || sym->used_in == file_scope) {
1346 warning(left->pos, "preprocessor token %.*s redefined",
1347 name->len, name->name);
1348 info(sym->pos, "this was the original definition");
1350 } else if (clean)
1351 goto out;
1354 if (!sym || sym->scope != file_scope) {
1355 sym = alloc_symbol(left->pos, SYM_NODE);
1356 bind_symbol(sym, name, NS_MACRO);
1357 add_ident(&macros, name);
1358 ret = 0;
1361 if (!ret) {
1362 sym->expansion = expansion;
1363 sym->arglist = arglist;
1364 __free_token(token); /* Free the "define" token, but not the rest of the line */
1367 sym->namespace = NS_MACRO;
1368 sym->used_in = NULL;
1369 sym->attr = attr;
1370 out:
1371 return ret;
1374 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1376 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1379 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1381 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1384 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1386 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1389 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1391 struct token *left = token->next;
1392 struct symbol *sym;
1394 if (token_type(left) != TOKEN_IDENT) {
1395 sparse_error(token->pos, "expected identifier to 'undef'");
1396 return 1;
1399 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1400 if (sym) {
1401 if (attr < sym->attr)
1402 return 1;
1403 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1404 return 1;
1405 } else if (attr <= SYM_ATTR_NORMAL)
1406 return 1;
1408 if (!sym || sym->scope != file_scope) {
1409 sym = alloc_symbol(left->pos, SYM_NODE);
1410 bind_symbol(sym, left->ident, NS_MACRO);
1413 sym->namespace = NS_UNDEF;
1414 sym->used_in = NULL;
1415 sym->attr = attr;
1417 return 1;
1420 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1422 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1425 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1427 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1430 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1432 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1433 free_preprocessor_line(token->next);
1434 token->next = stream->top_if;
1435 stream->top_if = token;
1436 if (false_nesting || true != 1)
1437 false_nesting++;
1438 return 0;
1441 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1443 struct token *next = token->next;
1444 int arg;
1445 if (token_type(next) == TOKEN_IDENT) {
1446 arg = token_defined(next);
1447 } else {
1448 dirty_stream(stream);
1449 if (!false_nesting)
1450 sparse_error(token->pos, "expected preprocessor identifier");
1451 arg = -1;
1453 return preprocessor_if(stream, token, arg);
1456 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1458 struct token *next = token->next;
1459 int arg;
1460 if (token_type(next) == TOKEN_IDENT) {
1461 if (!stream->dirty && !stream->ifndef) {
1462 if (!stream->protect) {
1463 stream->ifndef = token;
1464 stream->protect = next->ident;
1465 } else if (stream->protect == next->ident) {
1466 stream->ifndef = token;
1467 stream->dirty = 1;
1470 arg = !token_defined(next);
1471 } else {
1472 dirty_stream(stream);
1473 if (!false_nesting)
1474 sparse_error(token->pos, "expected preprocessor identifier");
1475 arg = -1;
1478 return preprocessor_if(stream, token, arg);
1481 static const char *show_token_sequence(struct token *token, int quote);
1484 * Expression handling for #if and #elif; it differs from normal expansion
1485 * due to special treatment of "defined".
1487 static int expression_value(struct token **where)
1489 struct expression *expr;
1490 struct token *p;
1491 struct token **list = where, **beginning = NULL;
1492 long long value;
1493 int state = 0;
1495 while (!eof_token(p = scan_next(list))) {
1496 switch (state) {
1497 case 0:
1498 if (token_type(p) != TOKEN_IDENT)
1499 break;
1500 if (p->ident == &defined_ident) {
1501 state = 1;
1502 beginning = list;
1503 break;
1505 if (!expand_one_symbol(list))
1506 continue;
1507 if (token_type(p) != TOKEN_IDENT)
1508 break;
1509 token_type(p) = TOKEN_ZERO_IDENT;
1510 break;
1511 case 1:
1512 if (match_op(p, '(')) {
1513 state = 2;
1514 } else {
1515 state = 0;
1516 replace_with_defined(p);
1517 *beginning = p;
1519 break;
1520 case 2:
1521 if (token_type(p) == TOKEN_IDENT)
1522 state = 3;
1523 else
1524 state = 0;
1525 replace_with_defined(p);
1526 *beginning = p;
1527 break;
1528 case 3:
1529 state = 0;
1530 if (!match_op(p, ')'))
1531 sparse_error(p->pos, "missing ')' after \"defined\"");
1532 *list = p->next;
1533 continue;
1535 list = &p->next;
1538 p = constant_expression(*where, &expr);
1539 if (!eof_token(p))
1540 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p, 0));
1541 value = get_expression_value(expr);
1542 return value != 0;
1545 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1547 int value = 0;
1548 if (!false_nesting)
1549 value = expression_value(&token->next);
1551 dirty_stream(stream);
1552 return preprocessor_if(stream, token, value);
1555 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1557 struct token *top_if = stream->top_if;
1558 end_group(stream);
1560 if (!top_if) {
1561 nesting_error(stream);
1562 sparse_error(token->pos, "unmatched #elif within stream");
1563 return 1;
1566 if (token_type(top_if) == TOKEN_ELSE) {
1567 nesting_error(stream);
1568 sparse_error(token->pos, "#elif after #else");
1569 if (!false_nesting)
1570 false_nesting = 1;
1571 return 1;
1574 dirty_stream(stream);
1575 if (token_type(top_if) != TOKEN_IF)
1576 return 1;
1577 if (false_nesting) {
1578 false_nesting = 0;
1579 if (!expression_value(&token->next))
1580 false_nesting = 1;
1581 } else {
1582 false_nesting = 1;
1583 token_type(top_if) = TOKEN_SKIP_GROUPS;
1585 return 1;
1588 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1590 struct token *top_if = stream->top_if;
1591 end_group(stream);
1593 if (!top_if) {
1594 nesting_error(stream);
1595 sparse_error(token->pos, "unmatched #else within stream");
1596 return 1;
1599 if (token_type(top_if) == TOKEN_ELSE) {
1600 nesting_error(stream);
1601 sparse_error(token->pos, "#else after #else");
1603 if (false_nesting) {
1604 if (token_type(top_if) == TOKEN_IF)
1605 false_nesting = 0;
1606 } else {
1607 false_nesting = 1;
1609 token_type(top_if) = TOKEN_ELSE;
1610 return 1;
1613 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1615 struct token *top_if = stream->top_if;
1616 end_group(stream);
1617 if (!top_if) {
1618 nesting_error(stream);
1619 sparse_error(token->pos, "unmatched #endif in stream");
1620 return 1;
1622 if (false_nesting)
1623 false_nesting--;
1624 stream->top_if = top_if->next;
1625 __free_token(top_if);
1626 return 1;
1629 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1631 warning(token->pos, "%s", show_token_sequence(token->next, 0));
1632 return 1;
1635 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1637 sparse_error(token->pos, "%s", show_token_sequence(token->next, 0));
1638 return 1;
1641 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1644 * Do we have any non-system includes?
1645 * Clear them out if so..
1647 *sys_includepath = NULL;
1648 return 1;
1651 static inline void update_inc_ptrs(const char ***where)
1654 if (*where <= dirafter_includepath) {
1655 dirafter_includepath++;
1656 /* If this was the entry that we prepend, don't
1657 * rise the lower entries, even if they are at
1658 * the same level. */
1659 if (where == &dirafter_includepath)
1660 return;
1662 if (*where <= sys_includepath) {
1663 sys_includepath++;
1664 if (where == &sys_includepath)
1665 return;
1667 if (*where <= isys_includepath) {
1668 isys_includepath++;
1669 if (where == &isys_includepath)
1670 return;
1673 /* angle_includepath is actually never updated, since we
1674 * don't suppport -iquote rught now. May change some day. */
1675 if (*where <= angle_includepath) {
1676 angle_includepath++;
1677 if (where == &angle_includepath)
1678 return;
1682 /* Add a path before 'where' and update the pointers associated with the
1683 * includepath array */
1684 static void add_path_entry(struct token *token, const char *path,
1685 const char ***where)
1687 const char **dst;
1688 const char *next;
1690 /* Need one free entry.. */
1691 if (includepath[INCLUDEPATHS-2])
1692 error_die(token->pos, "too many include path entries");
1694 /* check that this is not a duplicate */
1695 dst = includepath;
1696 while (*dst) {
1697 if (strcmp(*dst, path) == 0)
1698 return;
1699 dst++;
1701 next = path;
1702 dst = *where;
1704 update_inc_ptrs(where);
1707 * Move them all up starting at dst,
1708 * insert the new entry..
1710 do {
1711 const char *tmp = *dst;
1712 *dst = next;
1713 next = tmp;
1714 dst++;
1715 } while (next);
1718 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1720 for (;;) {
1721 token = token->next;
1722 if (eof_token(token))
1723 return 1;
1724 if (token_type(token) != TOKEN_STRING) {
1725 warning(token->pos, "expected path string");
1726 return 1;
1728 add_path_entry(token, token->string->data, &isys_includepath);
1732 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1734 for (;;) {
1735 token = token->next;
1736 if (eof_token(token))
1737 return 1;
1738 if (token_type(token) != TOKEN_STRING) {
1739 sparse_error(token->pos, "expected path string");
1740 return 1;
1742 add_path_entry(token, token->string->data, &sys_includepath);
1746 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1748 for (;;) {
1749 token = token->next;
1750 if (eof_token(token))
1751 return 1;
1752 if (token_type(token) != TOKEN_STRING) {
1753 sparse_error(token->pos, "expected path string");
1754 return 1;
1756 add_path_entry(token, token->string->data, &dirafter_includepath);
1760 /* Add to end on includepath list - no pointer updates */
1761 static void add_dirafter_entry(struct token *token, const char *path)
1763 const char **dst = includepath;
1765 /* Need one free entry.. */
1766 if (includepath[INCLUDEPATHS-2])
1767 error_die(token->pos, "too many include path entries");
1769 /* Add to the end */
1770 while (*dst)
1771 dst++;
1772 *dst = path;
1773 dst++;
1774 *dst = NULL;
1777 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1779 for (;;) {
1780 token = token->next;
1781 if (eof_token(token))
1782 return 1;
1783 if (token_type(token) != TOKEN_STRING) {
1784 sparse_error(token->pos, "expected path string");
1785 return 1;
1787 add_dirafter_entry(token, token->string->data);
1791 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1794 * -I-
1795 * From info gcc:
1796 * Split the include path. Any directories specified with `-I'
1797 * options before `-I-' are searched only for headers requested with
1798 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1799 * If additional directories are specified with `-I' options after
1800 * the `-I-', those directories are searched for all `#include'
1801 * directives.
1802 * In addition, `-I-' inhibits the use of the directory of the current
1803 * file directory as the first search directory for `#include "FILE"'.
1805 quote_includepath = includepath+1;
1806 angle_includepath = sys_includepath;
1807 return 1;
1811 * We replace "#pragma xxx" with "__pragma__" in the token
1812 * stream. Just as an example.
1814 * We'll just #define that away for now, but the theory here
1815 * is that we can use this to insert arbitrary token sequences
1816 * to turn the pragmas into internal front-end sequences for
1817 * when we actually start caring about them.
1819 * So eventually this will turn into some kind of extended
1820 * __attribute__() like thing, except called __pragma__(xxx).
1822 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1824 struct token *next = *line;
1826 if (match_ident(token->next, &once_ident) && eof_token(token->next->next)) {
1827 stream->once = 1;
1828 return 1;
1830 token->ident = &pragma_ident;
1831 token->pos.newline = 1;
1832 token->pos.whitespace = 1;
1833 token->pos.pos = 1;
1834 *line = token;
1835 token->next = next;
1836 return 0;
1840 * We ignore #line for now.
1842 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1844 return 1;
1847 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1849 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token, 0));
1850 return 1;
1854 static void init_preprocessor(void)
1856 int i;
1857 int stream = init_stream("preprocessor", -1, includepath);
1858 static struct {
1859 const char *name;
1860 int (*handler)(struct stream *, struct token **, struct token *);
1861 } normal[] = {
1862 { "define", handle_define },
1863 { "weak_define", handle_weak_define },
1864 { "strong_define", handle_strong_define },
1865 { "undef", handle_undef },
1866 { "strong_undef", handle_strong_undef },
1867 { "warning", handle_warning },
1868 { "error", handle_error },
1869 { "include", handle_include },
1870 { "include_next", handle_include_next },
1871 { "pragma", handle_pragma },
1872 { "line", handle_line },
1874 // our internal preprocessor tokens
1875 { "nostdinc", handle_nostdinc },
1876 { "add_include", handle_add_include },
1877 { "add_isystem", handle_add_isystem },
1878 { "add_system", handle_add_system },
1879 { "add_dirafter", handle_add_dirafter },
1880 { "split_include", handle_split_include },
1881 { "argv_include", handle_argv_include },
1882 }, special[] = {
1883 { "ifdef", handle_ifdef },
1884 { "ifndef", handle_ifndef },
1885 { "else", handle_else },
1886 { "endif", handle_endif },
1887 { "if", handle_if },
1888 { "elif", handle_elif },
1891 for (i = 0; i < ARRAY_SIZE(normal); i++) {
1892 struct symbol *sym;
1893 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1894 sym->handler = normal[i].handler;
1895 sym->normal = 1;
1897 for (i = 0; i < ARRAY_SIZE(special); i++) {
1898 struct symbol *sym;
1899 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1900 sym->handler = special[i].handler;
1901 sym->normal = 0;
1904 counter_macro = 0;
1907 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1909 int (*handler)(struct stream *, struct token **, struct token *);
1910 struct token *token = start->next;
1911 int is_normal = 1;
1913 if (eof_token(token))
1914 return;
1916 if (token_type(token) == TOKEN_IDENT) {
1917 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1918 if (sym) {
1919 handler = sym->handler;
1920 is_normal = sym->normal;
1921 } else {
1922 handler = handle_nondirective;
1924 } else if (token_type(token) == TOKEN_NUMBER) {
1925 handler = handle_line;
1926 } else {
1927 handler = handle_nondirective;
1930 if (is_normal) {
1931 dirty_stream(stream);
1932 if (false_nesting)
1933 goto out;
1935 if (!handler(stream, line, token)) /* all set */
1936 return;
1938 out:
1939 free_preprocessor_line(token);
1942 static void preprocessor_line(struct stream *stream, struct token **line)
1944 struct token *start = *line, *next;
1945 struct token **tp = &start->next;
1947 for (;;) {
1948 next = *tp;
1949 if (next->pos.newline)
1950 break;
1951 tp = &next->next;
1953 *line = next;
1954 *tp = &eof_token_entry;
1955 handle_preprocessor_line(stream, line, start);
1958 static void do_preprocess(struct token **list)
1960 struct token *next;
1962 while (!eof_token(next = scan_next(list))) {
1963 struct stream *stream = input_streams + next->pos.stream;
1965 if (next->pos.newline && match_op(next, '#')) {
1966 if (!next->pos.noexpand) {
1967 preprocessor_line(stream, list);
1968 __free_token(next); /* Free the '#' token */
1969 continue;
1973 switch (token_type(next)) {
1974 case TOKEN_STREAMEND:
1975 if (stream->top_if) {
1976 nesting_error(stream);
1977 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1978 stream->top_if = NULL;
1979 false_nesting = 0;
1981 if (!stream->dirty)
1982 stream->constant = CONSTANT_FILE_YES;
1983 *list = next->next;
1984 continue;
1985 case TOKEN_STREAMBEGIN:
1986 *list = next->next;
1987 continue;
1989 default:
1990 dirty_stream(stream);
1991 if (false_nesting) {
1992 *list = next->next;
1993 __free_token(next);
1994 continue;
1997 if (token_type(next) != TOKEN_IDENT ||
1998 expand_one_symbol(list))
1999 list = &next->next;
2004 struct token * preprocess(struct token *token)
2006 preprocessing = 1;
2007 init_preprocessor();
2008 do_preprocess(&token);
2010 // Drop all expressions from preprocessing, they're not used any more.
2011 // This is not true when we have multiple files, though ;/
2012 // clear_expression_alloc();
2013 preprocessing = 0;
2015 return token;
2018 static void dump_macro(struct symbol *sym)
2020 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
2021 struct token *args[nargs];
2022 struct token *token;
2024 printf("#define %s", show_ident(sym->ident));
2025 token = sym->arglist;
2026 if (token) {
2027 const char *sep = "";
2028 int narg = 0;
2029 putchar('(');
2030 for (; !eof_token(token); token = token->next) {
2031 if (token_type(token) == TOKEN_ARG_COUNT)
2032 continue;
2033 printf("%s%s", sep, show_token(token));
2034 args[narg++] = token;
2035 sep = ", ";
2037 putchar(')');
2039 putchar(' ');
2041 token = sym->expansion;
2042 while (!eof_token(token)) {
2043 struct token *next = token->next;
2044 switch (token_type(token)) {
2045 case TOKEN_UNTAINT:
2046 break;
2047 case TOKEN_MACRO_ARGUMENT:
2048 token = args[token->argnum];
2049 /* fall-through */
2050 default:
2051 printf("%s", show_token(token));
2052 if (next->pos.whitespace)
2053 putchar(' ');
2055 token = next;
2057 putchar('\n');
2060 void dump_macro_definitions(void)
2062 struct ident *name;
2064 FOR_EACH_PTR(macros, name) {
2065 struct symbol *sym = lookup_macro(name);
2066 if (sym)
2067 dump_macro(sym);
2068 } END_FOR_EACH_PTR(name);