assigned_expr: remove debug code
[smatch.git] / pre-process.c
blob9b8e40ed721200ef8e6e37e1effe249cacc05c0a
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 int false_nesting = 0;
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 store_macro_pos(token);
170 sym->used_in = file_scope;
171 return expand(list, sym);
173 if (token->ident == &__LINE___ident) {
174 replace_with_integer(token, token->pos.line);
175 } else if (token->ident == &__FILE___ident) {
176 replace_with_string(token, stream_name(token->pos.stream));
177 } else if (token->ident == &__DATE___ident) {
178 if (!t)
179 time(&t);
180 strftime(buffer, 12, "%b %e %Y", localtime(&t));
181 replace_with_string(token, buffer);
182 } else if (token->ident == &__TIME___ident) {
183 if (!t)
184 time(&t);
185 strftime(buffer, 9, "%T", localtime(&t));
186 replace_with_string(token, buffer);
188 return 1;
191 static inline struct token *scan_next(struct token **where)
193 struct token *token = *where;
194 if (token_type(token) != TOKEN_UNTAINT)
195 return token;
196 do {
197 token->ident->tainted = 0;
198 token = token->next;
199 } while (token_type(token) == TOKEN_UNTAINT);
200 *where = token;
201 return token;
204 static void expand_list(struct token **list)
206 struct token *next;
207 while (!eof_token(next = scan_next(list))) {
208 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
209 list = &next->next;
213 static void preprocessor_line(struct stream *stream, struct token **line);
215 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
217 struct stream *stream = input_streams + prev->pos.stream;
218 struct token **p = &prev->next;
219 struct token *next;
220 int nesting = 0;
222 while (!eof_token(next = scan_next(p))) {
223 if (next->pos.newline && match_op(next, '#')) {
224 if (!next->pos.noexpand) {
225 sparse_error(next->pos,
226 "directive in argument list");
227 preprocessor_line(stream, p);
228 __free_token(next); /* Free the '#' token */
229 continue;
232 switch (token_type(next)) {
233 case TOKEN_STREAMEND:
234 case TOKEN_STREAMBEGIN:
235 *p = &eof_token_entry;
236 return next;
238 if (false_nesting) {
239 *p = next->next;
240 __free_token(next);
241 continue;
243 if (match_op(next, '(')) {
244 nesting++;
245 } else if (match_op(next, ')')) {
246 if (!nesting--)
247 break;
248 } else if (match_op(next, ',') && !nesting && !vararg) {
249 break;
251 next->pos.stream = pos->stream;
252 next->pos.line = pos->line;
253 next->pos.pos = pos->pos;
254 p = &next->next;
256 *p = &eof_token_entry;
257 return next;
261 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
264 struct arg {
265 struct token *arg;
266 struct token *expanded;
267 struct token *str;
268 int n_normal;
269 int n_quoted;
270 int n_str;
273 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
275 int wanted = arglist->count.normal;
276 struct token *next = NULL;
277 int count = 0;
279 arglist = arglist->next; /* skip counter */
281 if (!wanted) {
282 next = collect_arg(start, 0, &what->pos);
283 if (eof_token(next))
284 goto Eclosing;
285 if (!eof_token(start->next) || !match_op(next, ')')) {
286 count++;
287 goto Emany;
289 } else {
290 for (count = 0; count < wanted; count++) {
291 struct argcount *p = &arglist->next->count;
292 next = collect_arg(start, p->vararg, &what->pos);
293 arglist = arglist->next->next;
294 if (eof_token(next))
295 goto Eclosing;
296 args[count].arg = start->next;
297 args[count].n_normal = p->normal;
298 args[count].n_quoted = p->quoted;
299 args[count].n_str = p->str;
300 if (match_op(next, ')')) {
301 count++;
302 break;
304 start = next;
306 if (count == wanted && !match_op(next, ')'))
307 goto Emany;
308 if (count == wanted - 1) {
309 struct argcount *p = &arglist->next->count;
310 if (!p->vararg)
311 goto Efew;
312 args[count].arg = NULL;
313 args[count].n_normal = p->normal;
314 args[count].n_quoted = p->quoted;
315 args[count].n_str = p->str;
317 if (count < wanted - 1)
318 goto Efew;
320 what->next = next->next;
321 return 1;
323 Efew:
324 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
325 show_token(what), wanted, count);
326 goto out;
327 Emany:
328 while (match_op(next, ',')) {
329 next = collect_arg(next, 0, &what->pos);
330 count++;
332 if (eof_token(next))
333 goto Eclosing;
334 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
335 show_token(what), count, wanted);
336 goto out;
337 Eclosing:
338 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
339 show_token(what));
340 out:
341 what->next = next->next;
342 return 0;
345 static struct token *dup_list(struct token *list)
347 struct token *res = NULL;
348 struct token **p = &res;
350 while (!eof_token(list)) {
351 struct token *newtok = __alloc_token(0);
352 *newtok = *list;
353 *p = newtok;
354 p = &newtok->next;
355 list = list->next;
357 return res;
360 static const char *show_token_sequence(struct token *token, int quote)
362 static char buffer[MAX_STRING];
363 char *ptr = buffer;
364 int whitespace = 0;
366 if (!token && !quote)
367 return "<none>";
368 while (!eof_token(token)) {
369 const char *val = quote ? quote_token(token) : show_token(token);
370 int len = strlen(val);
372 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
373 sparse_error(token->pos, "too long token expansion");
374 break;
377 if (whitespace)
378 *ptr++ = ' ';
379 memcpy(ptr, val, len);
380 ptr += len;
381 token = token->next;
382 whitespace = token->pos.whitespace;
384 *ptr = 0;
385 return buffer;
388 static struct token *stringify(struct token *arg)
390 const char *s = show_token_sequence(arg, 1);
391 int size = strlen(s)+1;
392 struct token *token = __alloc_token(0);
393 struct string *string = __alloc_string(size);
395 memcpy(string->data, s, size);
396 string->length = size;
397 token->pos = arg->pos;
398 token_type(token) = TOKEN_STRING;
399 token->string = string;
400 token->next = &eof_token_entry;
401 return token;
404 static void expand_arguments(int count, struct arg *args)
406 int i;
407 for (i = 0; i < count; i++) {
408 struct token *arg = args[i].arg;
409 if (!arg)
410 arg = &eof_token_entry;
411 if (args[i].n_str)
412 args[i].str = stringify(arg);
413 if (args[i].n_normal) {
414 if (!args[i].n_quoted) {
415 args[i].expanded = arg;
416 args[i].arg = NULL;
417 } else if (eof_token(arg)) {
418 args[i].expanded = arg;
419 } else {
420 args[i].expanded = dup_list(arg);
422 expand_list(&args[i].expanded);
428 * Possibly valid combinations:
429 * - ident + ident -> ident
430 * - ident + number -> ident unless number contains '.', '+' or '-'.
431 * - 'L' + char constant -> wide char constant
432 * - 'L' + string literal -> wide string literal
433 * - number + number -> number
434 * - number + ident -> number
435 * - number + '.' -> number
436 * - number + '+' or '-' -> number, if number used to end on [eEpP].
437 * - '.' + number -> number, if number used to start with a digit.
438 * - special + special -> either special or an error.
440 static enum token_type combine(struct token *left, struct token *right, char *p)
442 int len;
443 enum token_type t1 = token_type(left), t2 = token_type(right);
445 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
446 return TOKEN_ERROR;
448 if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
449 if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
450 return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
451 if (t2 == TOKEN_STRING)
452 return TOKEN_WIDE_STRING;
455 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
456 return TOKEN_ERROR;
458 strcpy(p, show_token(left));
459 strcat(p, show_token(right));
460 len = strlen(p);
462 if (len >= 256)
463 return TOKEN_ERROR;
465 if (t1 == TOKEN_IDENT) {
466 if (t2 == TOKEN_SPECIAL)
467 return TOKEN_ERROR;
468 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
469 return TOKEN_ERROR;
470 return TOKEN_IDENT;
473 if (t1 == TOKEN_NUMBER) {
474 if (t2 == TOKEN_SPECIAL) {
475 switch (right->special) {
476 case '.':
477 break;
478 case '+': case '-':
479 if (strchr("eEpP", p[len - 2]))
480 break;
481 default:
482 return TOKEN_ERROR;
485 return TOKEN_NUMBER;
488 if (p[0] == '.' && isdigit((unsigned char)p[1]))
489 return TOKEN_NUMBER;
491 return TOKEN_SPECIAL;
494 static int merge(struct token *left, struct token *right)
496 static char buffer[512];
497 enum token_type res = combine(left, right, buffer);
498 int n;
500 switch (res) {
501 case TOKEN_IDENT:
502 left->ident = built_in_ident(buffer);
503 left->pos.noexpand = 0;
504 return 1;
506 case TOKEN_NUMBER: {
507 char *number = __alloc_bytes(strlen(buffer) + 1);
508 memcpy(number, buffer, strlen(buffer) + 1);
509 token_type(left) = TOKEN_NUMBER; /* could be . + num */
510 left->number = number;
511 return 1;
514 case TOKEN_SPECIAL:
515 if (buffer[2] && buffer[3])
516 break;
517 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
518 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
519 left->special = n;
520 return 1;
523 break;
525 case TOKEN_WIDE_CHAR:
526 case TOKEN_WIDE_STRING:
527 token_type(left) = res;
528 left->pos.noexpand = 0;
529 left->string = right->string;
530 return 1;
532 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
533 token_type(left) = res;
534 left->pos.noexpand = 0;
535 memcpy(left->embedded, right->embedded, 4);
536 return 1;
538 default:
541 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
542 return 0;
545 static struct token *dup_token(struct token *token, struct position *streampos)
547 struct token *alloc = alloc_token(streampos);
548 token_type(alloc) = token_type(token);
549 alloc->pos.newline = token->pos.newline;
550 alloc->pos.whitespace = token->pos.whitespace;
551 alloc->number = token->number;
552 alloc->pos.noexpand = token->pos.noexpand;
553 return alloc;
556 static struct token **copy(struct token **where, struct token *list, int *count)
558 int need_copy = --*count;
559 while (!eof_token(list)) {
560 struct token *token;
561 if (need_copy)
562 token = dup_token(list, &list->pos);
563 else
564 token = list;
565 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
566 token->pos.noexpand = 1;
567 *where = token;
568 where = &token->next;
569 list = list->next;
571 *where = &eof_token_entry;
572 return where;
575 static int handle_kludge(struct token **p, struct arg *args)
577 struct token *t = (*p)->next->next;
578 while (1) {
579 struct arg *v = &args[t->argnum];
580 if (token_type(t->next) != TOKEN_CONCAT) {
581 if (v->arg) {
582 /* ignore the first ## */
583 *p = (*p)->next;
584 return 0;
586 /* skip the entire thing */
587 *p = t;
588 return 1;
590 if (v->arg && !eof_token(v->arg))
591 return 0; /* no magic */
592 t = t->next->next;
596 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
598 struct position *base_pos = &(*list)->pos;
599 int *count;
600 enum {Normal, Placeholder, Concat} state = Normal;
602 for (; !eof_token(body); body = body->next) {
603 struct token *added, *arg;
604 struct token **tail;
605 struct token *t;
607 switch (token_type(body)) {
608 case TOKEN_GNU_KLUDGE:
610 * GNU kludge: if we had <comma>##<vararg>, behaviour
611 * depends on whether we had enough arguments to have
612 * a vararg. If we did, ## is just ignored. Otherwise
613 * both , and ## are ignored. Worse, there can be
614 * an arbitrary number of ##<arg> in between; if all of
615 * those are empty, we act as if they hadn't been there,
616 * otherwise we act as if the kludge didn't exist.
618 t = body;
619 if (handle_kludge(&body, args)) {
620 if (state == Concat)
621 state = Normal;
622 else
623 state = Placeholder;
624 continue;
626 added = dup_token(t, base_pos);
627 token_type(added) = TOKEN_SPECIAL;
628 tail = &added->next;
629 break;
631 case TOKEN_STR_ARGUMENT:
632 arg = args[body->argnum].str;
633 count = &args[body->argnum].n_str;
634 goto copy_arg;
636 case TOKEN_QUOTED_ARGUMENT:
637 arg = args[body->argnum].arg;
638 count = &args[body->argnum].n_quoted;
639 if (!arg || eof_token(arg)) {
640 if (state == Concat)
641 state = Normal;
642 else
643 state = Placeholder;
644 continue;
646 goto copy_arg;
648 case TOKEN_MACRO_ARGUMENT:
649 arg = args[body->argnum].expanded;
650 count = &args[body->argnum].n_normal;
651 if (eof_token(arg)) {
652 state = Normal;
653 continue;
655 copy_arg:
656 tail = copy(&added, arg, count);
657 added->pos.newline = body->pos.newline;
658 added->pos.whitespace = body->pos.whitespace;
659 break;
661 case TOKEN_CONCAT:
662 if (state == Placeholder)
663 state = Normal;
664 else
665 state = Concat;
666 continue;
668 case TOKEN_IDENT:
669 added = dup_token(body, base_pos);
670 if (added->ident->tainted)
671 added->pos.noexpand = 1;
672 tail = &added->next;
673 break;
675 default:
676 added = dup_token(body, base_pos);
677 tail = &added->next;
678 break;
682 * if we got to doing real concatenation, we already have
683 * added something into the list, so containing_token() is OK.
685 if (state == Concat && merge(containing_token(list), added)) {
686 *list = added->next;
687 if (tail != &added->next)
688 list = tail;
689 } else {
690 *list = added;
691 list = tail;
693 state = Normal;
695 *list = &eof_token_entry;
696 return list;
699 static int expand(struct token **list, struct symbol *sym)
701 struct token *last;
702 struct token *token = *list;
703 struct ident *expanding = token->ident;
704 struct token **tail;
705 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
706 struct arg args[nargs];
708 if (expanding->tainted) {
709 token->pos.noexpand = 1;
710 return 1;
713 if (sym->arglist) {
714 if (!match_op(scan_next(&token->next), '('))
715 return 1;
716 if (!collect_arguments(token->next, sym->arglist, args, token))
717 return 1;
718 expand_arguments(nargs, args);
721 expanding->tainted = 1;
723 last = token->next;
724 tail = substitute(list, sym->expansion, args);
726 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
727 * We still can lose the newline flag if the sucker expands to nothing,
728 * but the price of dealing with that is probably too high (we'd need
729 * to collect the flags during scan_next())
731 (*list)->pos.newline = token->pos.newline;
732 (*list)->pos.whitespace = token->pos.whitespace;
733 *tail = last;
735 return 0;
738 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
740 static char buffer[256];
741 char *ptr = buffer;
743 while (!eof_token(token) && !match_op(token, endop)) {
744 int len;
745 const char *val = token->string->data;
746 if (token_type(token) != TOKEN_STRING)
747 val = show_token(token);
748 len = strlen(val);
749 memcpy(ptr, val, len);
750 ptr += len;
751 token = token->next;
753 *ptr = 0;
754 if (endop && !match_op(token, endop))
755 sparse_error(start->pos, "expected '>' at end of filename");
756 return buffer;
759 static int already_tokenized(const char *path)
761 int stream, next;
763 for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
764 struct stream *s = input_streams + stream;
766 next = s->next_stream;
767 if (s->once) {
768 if (strcmp(path, s->name))
769 continue;
770 return 1;
772 if (s->constant != CONSTANT_FILE_YES)
773 continue;
774 if (strcmp(path, s->name))
775 continue;
776 if (s->protect && !lookup_macro(s->protect))
777 continue;
778 return 1;
780 return 0;
783 /* Handle include of header files.
784 * The relevant options are made compatible with gcc. The only options that
785 * are not supported is -withprefix and friends.
787 * Three set of include paths are known:
788 * quote_includepath: Path to search when using #include "file.h"
789 * angle_includepath: Paths to search when using #include <file.h>
790 * isys_includepath: Paths specified with -isystem, come before the
791 * built-in system include paths. Gcc would suppress
792 * warnings from system headers. Here we separate
793 * them from the angle_ ones to keep search ordering.
795 * sys_includepath: Built-in include paths.
796 * dirafter_includepath Paths added with -dirafter.
798 * The above is implemented as one array with pointers
799 * +--------------+
800 * quote_includepath ---> | |
801 * +--------------+
802 * | |
803 * +--------------+
804 * angle_includepath ---> | |
805 * +--------------+
806 * isys_includepath ---> | |
807 * +--------------+
808 * sys_includepath ---> | |
809 * +--------------+
810 * dirafter_includepath -> | |
811 * +--------------+
813 * -I dir insert dir just before isys_includepath and move the rest
814 * -I- makes all dirs specified with -I before to quote dirs only and
815 * angle_includepath is set equal to isys_includepath.
816 * -nostdinc removes all sys dirs by storing NULL in entry pointed
817 * to by * sys_includepath. Note that this will reset all dirs built-in
818 * and added before -nostdinc by -isystem and -idirafter.
819 * -isystem dir adds dir where isys_includepath points adding this dir as
820 * first systemdir
821 * -idirafter dir adds dir to the end of the list
824 static void set_stream_include_path(struct stream *stream)
826 const char *path = stream->path;
827 if (!path) {
828 const char *p = strrchr(stream->name, '/');
829 path = "";
830 if (p) {
831 int len = p - stream->name + 1;
832 char *m = malloc(len+1);
833 /* This includes the final "/" */
834 memcpy(m, stream->name, len);
835 m[len] = 0;
836 path = m;
838 stream->path = path;
840 includepath[0] = path;
843 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
845 int fd;
846 int plen = strlen(path);
847 static char fullname[PATH_MAX];
849 memcpy(fullname, path, plen);
850 if (plen && path[plen-1] != '/') {
851 fullname[plen] = '/';
852 plen++;
854 memcpy(fullname+plen, filename, flen);
855 if (already_tokenized(fullname))
856 return 1;
857 fd = open(fullname, O_RDONLY);
858 if (fd >= 0) {
859 char * streamname = __alloc_bytes(plen + flen);
860 memcpy(streamname, fullname, plen + flen);
861 *where = tokenize(streamname, fd, *where, next_path);
862 close(fd);
863 return 1;
865 return 0;
868 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
870 const char *path;
872 while ((path = *pptr++) != NULL) {
873 if (!try_include(path, filename, flen, list, pptr))
874 continue;
875 return 1;
877 return 0;
880 static int free_preprocessor_line(struct token *token)
882 while (token_type(token) != TOKEN_EOF) {
883 struct token *free = token;
884 token = token->next;
885 __free_token(free);
887 return 1;
890 const char *find_include(const char *skip, const char *look_for)
892 DIR *dp;
893 struct dirent *entry;
894 struct stat statbuf;
895 const char *ret;
896 char cwd[PATH_MAX];
897 static char buf[PATH_MAX + 1];
899 dp = opendir(".");
900 if (!dp)
901 return NULL;
903 if (!getcwd(cwd, sizeof(cwd)))
904 return NULL;
906 while ((entry = readdir(dp))) {
907 lstat(entry->d_name, &statbuf);
909 if (strcmp(entry->d_name, look_for) == 0) {
910 snprintf(buf, sizeof(buf), "%s/%s", cwd, entry->d_name);
911 return buf;
914 if (S_ISDIR(statbuf.st_mode)) {
915 /* Found a directory, but ignore . and .. */
916 if (strcmp(".", entry->d_name) == 0 ||
917 strcmp("..", entry->d_name) == 0 ||
918 strcmp(skip, entry->d_name) == 0)
919 continue;
921 chdir(entry->d_name);
922 ret = find_include("", look_for);
923 chdir("..");
924 if (ret)
925 return ret;
928 closedir(dp);
930 return NULL;
933 const char *search_dir(const char *stop, const char *look_for)
935 char cwd[PATH_MAX];
936 int len;
937 const char *ret;
938 int cnt = 0;
940 if (!getcwd(cwd, sizeof(cwd)))
941 return NULL;
943 len = strlen(cwd);
944 while (len >= 0) {
945 ret = find_include(cnt++ ? cwd + len + 1 : "", look_for);
946 if (ret)
947 return ret;
949 if (strcmp(cwd, stop) == 0 ||
950 strcmp(cwd, "/usr/include") == 0 ||
951 strcmp(cwd, "/usr/local/include") == 0 ||
952 strlen(cwd) <= 10 || /* heck... don't search /usr/lib/ */
953 strcmp(cwd, "/") == 0)
954 return NULL;
956 while (--len >= 0) {
957 if (cwd[len] == '/') {
958 cwd[len] = '\0';
959 break;
963 chdir("..");
965 return NULL;
968 static void use_best_guess_header_file(struct token *token, const char *filename, struct token **list)
970 char cwd[PATH_MAX];
971 char dir_part[PATH_MAX];
972 const char *file_part;
973 const char *include_name;
974 int len;
976 if (!filename || filename[0] == '\0')
977 return;
979 file_part = filename;
980 while ((filename = strchr(filename, '/'))) {
981 ++filename;
982 if (filename[0])
983 file_part = filename;
986 snprintf(dir_part, sizeof(dir_part), "%s", stream_name(token->pos.stream));
987 len = strlen(dir_part);
988 while (--len >= 0) {
989 if (dir_part[len] == '/') {
990 dir_part[len] = '\0';
991 break;
994 if (len < 0)
995 sprintf(dir_part, ".");
997 if (!getcwd(cwd, sizeof(cwd)))
998 return;
1000 chdir(dir_part);
1001 include_name = search_dir(cwd, file_part);
1002 chdir(cwd);
1003 if (!include_name)
1004 return;
1005 sparse_error(token->pos, "using '%s'", include_name);
1007 try_include("", include_name, strlen(include_name), list, includepath);
1010 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how)
1012 const char *filename;
1013 struct token *next;
1014 const char **path;
1015 int expect;
1016 int flen;
1018 next = token->next;
1019 expect = '>';
1020 if (!match_op(next, '<')) {
1021 expand_list(&token->next);
1022 expect = 0;
1023 next = token;
1024 if (match_op(token->next, '<')) {
1025 next = token->next;
1026 expect = '>';
1030 token = next->next;
1031 filename = token_name_sequence(token, expect, token);
1032 flen = strlen(filename) + 1;
1034 /* Absolute path? */
1035 if (filename[0] == '/') {
1036 if (try_include("", filename, flen, list, includepath))
1037 return 0;
1038 goto out;
1041 switch (how) {
1042 case 1:
1043 path = stream->next_path;
1044 break;
1045 case 2:
1046 includepath[0] = "";
1047 path = includepath;
1048 break;
1049 default:
1050 /* Dir of input file is first dir to search for quoted includes */
1051 set_stream_include_path(stream);
1052 path = expect ? angle_includepath : quote_includepath;
1053 break;
1055 /* Check the standard include paths.. */
1056 if (do_include_path(path, list, token, filename, flen))
1057 return 0;
1058 out:
1059 sparse_error(token->pos, "unable to open '%s'", filename);
1060 use_best_guess_header_file(token, filename, list);
1061 return 0;
1064 static int handle_include(struct stream *stream, struct token **list, struct token *token)
1066 return handle_include_path(stream, list, token, 0);
1069 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
1071 return handle_include_path(stream, list, token, 1);
1074 static int handle_argv_include(struct stream *stream, struct token **list, struct token *token)
1076 return handle_include_path(stream, list, token, 2);
1079 static int token_different(struct token *t1, struct token *t2)
1081 int different;
1083 if (token_type(t1) != token_type(t2))
1084 return 1;
1086 switch (token_type(t1)) {
1087 case TOKEN_IDENT:
1088 different = t1->ident != t2->ident;
1089 break;
1090 case TOKEN_ARG_COUNT:
1091 case TOKEN_UNTAINT:
1092 case TOKEN_CONCAT:
1093 case TOKEN_GNU_KLUDGE:
1094 different = 0;
1095 break;
1096 case TOKEN_NUMBER:
1097 different = strcmp(t1->number, t2->number);
1098 break;
1099 case TOKEN_SPECIAL:
1100 different = t1->special != t2->special;
1101 break;
1102 case TOKEN_MACRO_ARGUMENT:
1103 case TOKEN_QUOTED_ARGUMENT:
1104 case TOKEN_STR_ARGUMENT:
1105 different = t1->argnum != t2->argnum;
1106 break;
1107 case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
1108 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
1109 different = memcmp(t1->embedded, t2->embedded, 4);
1110 break;
1111 case TOKEN_CHAR:
1112 case TOKEN_WIDE_CHAR:
1113 case TOKEN_STRING:
1114 case TOKEN_WIDE_STRING: {
1115 struct string *s1, *s2;
1117 s1 = t1->string;
1118 s2 = t2->string;
1119 different = 1;
1120 if (s1->length != s2->length)
1121 break;
1122 different = memcmp(s1->data, s2->data, s1->length);
1123 break;
1125 default:
1126 different = 1;
1127 break;
1129 return different;
1132 static int token_list_different(struct token *list1, struct token *list2)
1134 for (;;) {
1135 if (list1 == list2)
1136 return 0;
1137 if (!list1 || !list2)
1138 return 1;
1139 if (token_different(list1, list2))
1140 return 1;
1141 list1 = list1->next;
1142 list2 = list2->next;
1146 static inline void set_arg_count(struct token *token)
1148 token_type(token) = TOKEN_ARG_COUNT;
1149 token->count.normal = token->count.quoted =
1150 token->count.str = token->count.vararg = 0;
1153 static struct token *parse_arguments(struct token *list)
1155 struct token *arg = list->next, *next = list;
1156 struct argcount *count = &list->count;
1158 set_arg_count(list);
1160 if (match_op(arg, ')')) {
1161 next = arg->next;
1162 list->next = &eof_token_entry;
1163 return next;
1166 while (token_type(arg) == TOKEN_IDENT) {
1167 if (arg->ident == &__VA_ARGS___ident)
1168 goto Eva_args;
1169 if (!++count->normal)
1170 goto Eargs;
1171 next = arg->next;
1173 if (match_op(next, ',')) {
1174 set_arg_count(next);
1175 arg = next->next;
1176 continue;
1179 if (match_op(next, ')')) {
1180 set_arg_count(next);
1181 next = next->next;
1182 arg->next->next = &eof_token_entry;
1183 return next;
1186 /* normal cases are finished here */
1188 if (match_op(next, SPECIAL_ELLIPSIS)) {
1189 if (match_op(next->next, ')')) {
1190 set_arg_count(next);
1191 next->count.vararg = 1;
1192 next = next->next;
1193 arg->next->next = &eof_token_entry;
1194 return next->next;
1197 arg = next;
1198 goto Enotclosed;
1201 if (eof_token(next)) {
1202 goto Enotclosed;
1203 } else {
1204 arg = next;
1205 goto Ebadstuff;
1209 if (match_op(arg, SPECIAL_ELLIPSIS)) {
1210 next = arg->next;
1211 token_type(arg) = TOKEN_IDENT;
1212 arg->ident = &__VA_ARGS___ident;
1213 if (!match_op(next, ')'))
1214 goto Enotclosed;
1215 if (!++count->normal)
1216 goto Eargs;
1217 set_arg_count(next);
1218 next->count.vararg = 1;
1219 next = next->next;
1220 arg->next->next = &eof_token_entry;
1221 return next;
1224 if (eof_token(arg)) {
1225 arg = next;
1226 goto Enotclosed;
1228 if (match_op(arg, ','))
1229 goto Emissing;
1230 else
1231 goto Ebadstuff;
1234 Emissing:
1235 sparse_error(arg->pos, "parameter name missing");
1236 return NULL;
1237 Ebadstuff:
1238 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
1239 show_token(arg));
1240 return NULL;
1241 Enotclosed:
1242 sparse_error(arg->pos, "missing ')' in macro parameter list");
1243 return NULL;
1244 Eva_args:
1245 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1246 return NULL;
1247 Eargs:
1248 sparse_error(arg->pos, "too many arguments in macro definition");
1249 return NULL;
1252 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1254 struct ident *ident = token->ident;
1255 int nr;
1257 if (!arglist || token_type(token) != TOKEN_IDENT)
1258 return 0;
1260 arglist = arglist->next;
1262 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1263 if (arglist->ident == ident) {
1264 struct argcount *count = &arglist->next->count;
1265 int n;
1267 token->argnum = nr;
1268 token_type(token) = type;
1269 switch (type) {
1270 case TOKEN_MACRO_ARGUMENT:
1271 n = ++count->normal;
1272 break;
1273 case TOKEN_QUOTED_ARGUMENT:
1274 n = ++count->quoted;
1275 break;
1276 default:
1277 n = ++count->str;
1279 if (n)
1280 return count->vararg ? 2 : 1;
1282 * XXX - need saner handling of that
1283 * (>= 1024 instances of argument)
1285 token_type(token) = TOKEN_ERROR;
1286 return -1;
1289 return 0;
1292 static struct token *handle_hash(struct token **p, struct token *arglist)
1294 struct token *token = *p;
1295 if (arglist) {
1296 struct token *next = token->next;
1297 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1298 goto Equote;
1299 next->pos.whitespace = token->pos.whitespace;
1300 __free_token(token);
1301 token = *p = next;
1302 } else {
1303 token->pos.noexpand = 1;
1305 return token;
1307 Equote:
1308 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1309 return NULL;
1312 /* token->next is ## */
1313 static struct token *handle_hashhash(struct token *token, struct token *arglist)
1315 struct token *last = token;
1316 struct token *concat;
1317 int state = match_op(token, ',');
1319 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1321 while (1) {
1322 struct token *t;
1323 int is_arg;
1325 /* eat duplicate ## */
1326 concat = token->next;
1327 while (match_op(t = concat->next, SPECIAL_HASHHASH)) {
1328 token->next = t;
1329 __free_token(concat);
1330 concat = t;
1332 token_type(concat) = TOKEN_CONCAT;
1334 if (eof_token(t))
1335 goto Econcat;
1337 if (match_op(t, '#')) {
1338 t = handle_hash(&concat->next, arglist);
1339 if (!t)
1340 return NULL;
1343 is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
1345 if (state == 1 && is_arg) {
1346 state = is_arg;
1347 } else {
1348 last = t;
1349 state = match_op(t, ',');
1352 token = t;
1353 if (!match_op(token->next, SPECIAL_HASHHASH))
1354 break;
1356 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1357 if (state == 2)
1358 token_type(last) = TOKEN_GNU_KLUDGE;
1359 return token;
1361 Econcat:
1362 sparse_error(concat->pos, "'##' cannot appear at the ends of macro expansion");
1363 return NULL;
1366 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1368 struct token *token = expansion;
1369 struct token **p;
1371 if (match_op(token, SPECIAL_HASHHASH))
1372 goto Econcat;
1374 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1375 if (match_op(token, '#')) {
1376 token = handle_hash(p, arglist);
1377 if (!token)
1378 return NULL;
1380 if (match_op(token->next, SPECIAL_HASHHASH)) {
1381 token = handle_hashhash(token, arglist);
1382 if (!token)
1383 return NULL;
1384 } else {
1385 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1387 if (token_type(token) == TOKEN_ERROR)
1388 goto Earg;
1390 token = alloc_token(&expansion->pos);
1391 token_type(token) = TOKEN_UNTAINT;
1392 token->ident = name;
1393 token->next = *p;
1394 *p = token;
1395 return expansion;
1397 Econcat:
1398 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1399 return NULL;
1400 Earg:
1401 sparse_error(token->pos, "too many instances of argument in body");
1402 return NULL;
1405 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1407 struct token *arglist, *expansion;
1408 struct token *left = token->next;
1409 struct symbol *sym;
1410 struct ident *name;
1411 int ret;
1413 if (token_type(left) != TOKEN_IDENT) {
1414 sparse_error(token->pos, "expected identifier to 'define'");
1415 return 1;
1418 name = left->ident;
1420 arglist = NULL;
1421 expansion = left->next;
1422 if (!expansion->pos.whitespace) {
1423 if (match_op(expansion, '(')) {
1424 arglist = expansion;
1425 expansion = parse_arguments(expansion);
1426 if (!expansion)
1427 return 1;
1428 } else if (!eof_token(expansion)) {
1429 warning(expansion->pos,
1430 "no whitespace before object-like macro body");
1434 expansion = parse_expansion(expansion, arglist, name);
1435 if (!expansion)
1436 return 1;
1438 ret = 1;
1439 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1440 if (sym) {
1441 int clean;
1443 if (attr < sym->attr)
1444 goto out;
1446 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1448 if (token_list_different(sym->expansion, expansion) ||
1449 token_list_different(sym->arglist, arglist)) {
1450 ret = 0;
1451 if ((clean && attr == SYM_ATTR_NORMAL)
1452 || sym->used_in == file_scope) {
1453 warning(left->pos, "preprocessor token %.*s redefined",
1454 name->len, name->name);
1455 info(sym->pos, "this was the original definition");
1457 } else if (clean)
1458 goto out;
1461 if (!sym || sym->scope != file_scope) {
1462 sym = alloc_symbol(left->pos, SYM_NODE);
1463 bind_symbol(sym, name, NS_MACRO);
1464 ret = 0;
1467 if (!ret) {
1468 sym->expansion = expansion;
1469 sym->arglist = arglist;
1470 __free_token(token); /* Free the "define" token, but not the rest of the line */
1473 sym->namespace = NS_MACRO;
1474 sym->used_in = NULL;
1475 sym->attr = attr;
1476 out:
1477 return ret;
1480 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1482 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1485 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1487 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1490 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1492 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1495 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1497 struct token *left = token->next;
1498 struct symbol *sym;
1500 if (token_type(left) != TOKEN_IDENT) {
1501 sparse_error(token->pos, "expected identifier to 'undef'");
1502 return 1;
1505 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1506 if (sym) {
1507 if (attr < sym->attr)
1508 return 1;
1509 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1510 return 1;
1511 } else if (attr <= SYM_ATTR_NORMAL)
1512 return 1;
1514 if (!sym || sym->scope != file_scope) {
1515 sym = alloc_symbol(left->pos, SYM_NODE);
1516 bind_symbol(sym, left->ident, NS_MACRO);
1519 sym->namespace = NS_UNDEF;
1520 sym->used_in = NULL;
1521 sym->attr = attr;
1523 return 1;
1526 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1528 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1531 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1533 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1536 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1538 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1539 free_preprocessor_line(token->next);
1540 token->next = stream->top_if;
1541 stream->top_if = token;
1542 if (false_nesting || true != 1)
1543 false_nesting++;
1544 return 0;
1547 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1549 struct token *next = token->next;
1550 int arg;
1551 if (token_type(next) == TOKEN_IDENT) {
1552 arg = token_defined(next);
1553 } else {
1554 dirty_stream(stream);
1555 if (!false_nesting)
1556 sparse_error(token->pos, "expected preprocessor identifier");
1557 arg = -1;
1559 return preprocessor_if(stream, token, arg);
1562 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1564 struct token *next = token->next;
1565 int arg;
1566 if (token_type(next) == TOKEN_IDENT) {
1567 if (!stream->dirty && !stream->ifndef) {
1568 if (!stream->protect) {
1569 stream->ifndef = token;
1570 stream->protect = next->ident;
1571 } else if (stream->protect == next->ident) {
1572 stream->ifndef = token;
1573 stream->dirty = 1;
1576 arg = !token_defined(next);
1577 } else {
1578 dirty_stream(stream);
1579 if (!false_nesting)
1580 sparse_error(token->pos, "expected preprocessor identifier");
1581 arg = -1;
1584 return preprocessor_if(stream, token, arg);
1587 static const char *show_token_sequence(struct token *token, int quote);
1590 * Expression handling for #if and #elif; it differs from normal expansion
1591 * due to special treatment of "defined".
1593 static int expression_value(struct token **where)
1595 struct expression *expr;
1596 struct token *p;
1597 struct token **list = where, **beginning = NULL;
1598 long long value;
1599 int state = 0;
1601 while (!eof_token(p = scan_next(list))) {
1602 switch (state) {
1603 case 0:
1604 if (token_type(p) != TOKEN_IDENT)
1605 break;
1606 if (p->ident == &defined_ident) {
1607 state = 1;
1608 beginning = list;
1609 break;
1611 if (!expand_one_symbol(list))
1612 continue;
1613 if (token_type(p) != TOKEN_IDENT)
1614 break;
1615 token_type(p) = TOKEN_ZERO_IDENT;
1616 break;
1617 case 1:
1618 if (match_op(p, '(')) {
1619 state = 2;
1620 } else {
1621 state = 0;
1622 replace_with_defined(p);
1623 *beginning = p;
1625 break;
1626 case 2:
1627 if (token_type(p) == TOKEN_IDENT)
1628 state = 3;
1629 else
1630 state = 0;
1631 replace_with_defined(p);
1632 *beginning = p;
1633 break;
1634 case 3:
1635 state = 0;
1636 if (!match_op(p, ')'))
1637 sparse_error(p->pos, "missing ')' after \"defined\"");
1638 *list = p->next;
1639 continue;
1641 list = &p->next;
1644 p = constant_expression(*where, &expr);
1645 if (!eof_token(p))
1646 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p, 0));
1647 value = get_expression_value(expr);
1648 return value != 0;
1651 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1653 int value = 0;
1654 if (!false_nesting)
1655 value = expression_value(&token->next);
1657 dirty_stream(stream);
1658 return preprocessor_if(stream, token, value);
1661 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1663 struct token *top_if = stream->top_if;
1664 end_group(stream);
1666 if (!top_if) {
1667 nesting_error(stream);
1668 sparse_error(token->pos, "unmatched #elif within stream");
1669 return 1;
1672 if (token_type(top_if) == TOKEN_ELSE) {
1673 nesting_error(stream);
1674 sparse_error(token->pos, "#elif after #else");
1675 if (!false_nesting)
1676 false_nesting = 1;
1677 return 1;
1680 dirty_stream(stream);
1681 if (token_type(top_if) != TOKEN_IF)
1682 return 1;
1683 if (false_nesting) {
1684 false_nesting = 0;
1685 if (!expression_value(&token->next))
1686 false_nesting = 1;
1687 } else {
1688 false_nesting = 1;
1689 token_type(top_if) = TOKEN_SKIP_GROUPS;
1691 return 1;
1694 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1696 struct token *top_if = stream->top_if;
1697 end_group(stream);
1699 if (!top_if) {
1700 nesting_error(stream);
1701 sparse_error(token->pos, "unmatched #else within stream");
1702 return 1;
1705 if (token_type(top_if) == TOKEN_ELSE) {
1706 nesting_error(stream);
1707 sparse_error(token->pos, "#else after #else");
1709 if (false_nesting) {
1710 if (token_type(top_if) == TOKEN_IF)
1711 false_nesting = 0;
1712 } else {
1713 false_nesting = 1;
1715 token_type(top_if) = TOKEN_ELSE;
1716 return 1;
1719 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1721 struct token *top_if = stream->top_if;
1722 end_group(stream);
1723 if (!top_if) {
1724 nesting_error(stream);
1725 sparse_error(token->pos, "unmatched #endif in stream");
1726 return 1;
1728 if (false_nesting)
1729 false_nesting--;
1730 stream->top_if = top_if->next;
1731 __free_token(top_if);
1732 return 1;
1735 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1737 warning(token->pos, "%s", show_token_sequence(token->next, 0));
1738 return 1;
1741 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1743 sparse_error(token->pos, "%s", show_token_sequence(token->next, 0));
1744 return 1;
1747 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1750 * Do we have any non-system includes?
1751 * Clear them out if so..
1753 *sys_includepath = NULL;
1754 return 1;
1757 static inline void update_inc_ptrs(const char ***where)
1760 if (*where <= dirafter_includepath) {
1761 dirafter_includepath++;
1762 /* If this was the entry that we prepend, don't
1763 * rise the lower entries, even if they are at
1764 * the same level. */
1765 if (where == &dirafter_includepath)
1766 return;
1768 if (*where <= sys_includepath) {
1769 sys_includepath++;
1770 if (where == &sys_includepath)
1771 return;
1773 if (*where <= isys_includepath) {
1774 isys_includepath++;
1775 if (where == &isys_includepath)
1776 return;
1779 /* angle_includepath is actually never updated, since we
1780 * don't suppport -iquote rught now. May change some day. */
1781 if (*where <= angle_includepath) {
1782 angle_includepath++;
1783 if (where == &angle_includepath)
1784 return;
1788 /* Add a path before 'where' and update the pointers associated with the
1789 * includepath array */
1790 static void add_path_entry(struct token *token, const char *path,
1791 const char ***where)
1793 const char **dst;
1794 const char *next;
1796 /* Need one free entry.. */
1797 if (includepath[INCLUDEPATHS-2])
1798 error_die(token->pos, "too many include path entries");
1800 /* check that this is not a duplicate */
1801 dst = includepath;
1802 while (*dst) {
1803 if (strcmp(*dst, path) == 0)
1804 return;
1805 dst++;
1807 next = path;
1808 dst = *where;
1810 update_inc_ptrs(where);
1813 * Move them all up starting at dst,
1814 * insert the new entry..
1816 do {
1817 const char *tmp = *dst;
1818 *dst = next;
1819 next = tmp;
1820 dst++;
1821 } while (next);
1824 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1826 for (;;) {
1827 token = token->next;
1828 if (eof_token(token))
1829 return 1;
1830 if (token_type(token) != TOKEN_STRING) {
1831 warning(token->pos, "expected path string");
1832 return 1;
1834 add_path_entry(token, token->string->data, &isys_includepath);
1838 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1840 for (;;) {
1841 token = token->next;
1842 if (eof_token(token))
1843 return 1;
1844 if (token_type(token) != TOKEN_STRING) {
1845 sparse_error(token->pos, "expected path string");
1846 return 1;
1848 add_path_entry(token, token->string->data, &sys_includepath);
1852 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1854 for (;;) {
1855 token = token->next;
1856 if (eof_token(token))
1857 return 1;
1858 if (token_type(token) != TOKEN_STRING) {
1859 sparse_error(token->pos, "expected path string");
1860 return 1;
1862 add_path_entry(token, token->string->data, &dirafter_includepath);
1866 /* Add to end on includepath list - no pointer updates */
1867 static void add_dirafter_entry(struct token *token, const char *path)
1869 const char **dst = includepath;
1871 /* Need one free entry.. */
1872 if (includepath[INCLUDEPATHS-2])
1873 error_die(token->pos, "too many include path entries");
1875 /* Add to the end */
1876 while (*dst)
1877 dst++;
1878 *dst = path;
1879 dst++;
1880 *dst = NULL;
1883 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1885 for (;;) {
1886 token = token->next;
1887 if (eof_token(token))
1888 return 1;
1889 if (token_type(token) != TOKEN_STRING) {
1890 sparse_error(token->pos, "expected path string");
1891 return 1;
1893 add_dirafter_entry(token, token->string->data);
1897 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1900 * -I-
1901 * From info gcc:
1902 * Split the include path. Any directories specified with `-I'
1903 * options before `-I-' are searched only for headers requested with
1904 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1905 * If additional directories are specified with `-I' options after
1906 * the `-I-', those directories are searched for all `#include'
1907 * directives.
1908 * In addition, `-I-' inhibits the use of the directory of the current
1909 * file directory as the first search directory for `#include "FILE"'.
1911 quote_includepath = includepath+1;
1912 angle_includepath = sys_includepath;
1913 return 1;
1917 * We replace "#pragma xxx" with "__pragma__" in the token
1918 * stream. Just as an example.
1920 * We'll just #define that away for now, but the theory here
1921 * is that we can use this to insert arbitrary token sequences
1922 * to turn the pragmas into internal front-end sequences for
1923 * when we actually start caring about them.
1925 * So eventually this will turn into some kind of extended
1926 * __attribute__() like thing, except called __pragma__(xxx).
1928 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1930 struct token *next = *line;
1932 if (match_ident(token->next, &once_ident) && eof_token(token->next->next)) {
1933 stream->once = 1;
1934 return 1;
1936 token->ident = &pragma_ident;
1937 token->pos.newline = 1;
1938 token->pos.whitespace = 1;
1939 token->pos.pos = 1;
1940 *line = token;
1941 token->next = next;
1942 return 0;
1946 * We ignore #line for now.
1948 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1950 return 1;
1953 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1955 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token, 0));
1956 return 1;
1960 static void init_preprocessor(void)
1962 int i;
1963 int stream = init_stream("preprocessor", -1, includepath);
1964 static struct {
1965 const char *name;
1966 int (*handler)(struct stream *, struct token **, struct token *);
1967 } normal[] = {
1968 { "define", handle_define },
1969 { "weak_define", handle_weak_define },
1970 { "strong_define", handle_strong_define },
1971 { "undef", handle_undef },
1972 { "strong_undef", handle_strong_undef },
1973 { "warning", handle_warning },
1974 { "error", handle_error },
1975 { "include", handle_include },
1976 { "include_next", handle_include_next },
1977 { "pragma", handle_pragma },
1978 { "line", handle_line },
1980 // our internal preprocessor tokens
1981 { "nostdinc", handle_nostdinc },
1982 { "add_include", handle_add_include },
1983 { "add_isystem", handle_add_isystem },
1984 { "add_system", handle_add_system },
1985 { "add_dirafter", handle_add_dirafter },
1986 { "split_include", handle_split_include },
1987 { "argv_include", handle_argv_include },
1988 }, special[] = {
1989 { "ifdef", handle_ifdef },
1990 { "ifndef", handle_ifndef },
1991 { "else", handle_else },
1992 { "endif", handle_endif },
1993 { "if", handle_if },
1994 { "elif", handle_elif },
1997 for (i = 0; i < ARRAY_SIZE(normal); i++) {
1998 struct symbol *sym;
1999 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
2000 sym->handler = normal[i].handler;
2001 sym->normal = 1;
2003 for (i = 0; i < ARRAY_SIZE(special); i++) {
2004 struct symbol *sym;
2005 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
2006 sym->handler = special[i].handler;
2007 sym->normal = 0;
2012 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
2014 int (*handler)(struct stream *, struct token **, struct token *);
2015 struct token *token = start->next;
2016 int is_normal = 1;
2018 if (eof_token(token))
2019 return;
2021 if (token_type(token) == TOKEN_IDENT) {
2022 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
2023 if (sym) {
2024 handler = sym->handler;
2025 is_normal = sym->normal;
2026 } else {
2027 handler = handle_nondirective;
2029 } else if (token_type(token) == TOKEN_NUMBER) {
2030 handler = handle_line;
2031 } else {
2032 handler = handle_nondirective;
2035 if (is_normal) {
2036 dirty_stream(stream);
2037 if (false_nesting)
2038 goto out;
2040 if (!handler(stream, line, token)) /* all set */
2041 return;
2043 out:
2044 free_preprocessor_line(token);
2047 static void preprocessor_line(struct stream *stream, struct token **line)
2049 struct token *start = *line, *next;
2050 struct token **tp = &start->next;
2052 for (;;) {
2053 next = *tp;
2054 if (next->pos.newline)
2055 break;
2056 tp = &next->next;
2058 *line = next;
2059 *tp = &eof_token_entry;
2060 handle_preprocessor_line(stream, line, start);
2063 static void do_preprocess(struct token **list)
2065 struct token *next;
2067 while (!eof_token(next = scan_next(list))) {
2068 struct stream *stream = input_streams + next->pos.stream;
2070 if (next->pos.newline && match_op(next, '#')) {
2071 if (!next->pos.noexpand) {
2072 preprocessor_line(stream, list);
2073 __free_token(next); /* Free the '#' token */
2074 continue;
2078 switch (token_type(next)) {
2079 case TOKEN_STREAMEND:
2080 if (stream->top_if) {
2081 nesting_error(stream);
2082 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
2083 stream->top_if = NULL;
2084 false_nesting = 0;
2086 if (!stream->dirty)
2087 stream->constant = CONSTANT_FILE_YES;
2088 *list = next->next;
2089 continue;
2090 case TOKEN_STREAMBEGIN:
2091 *list = next->next;
2092 continue;
2094 default:
2095 dirty_stream(stream);
2096 if (false_nesting) {
2097 *list = next->next;
2098 __free_token(next);
2099 continue;
2102 if (token_type(next) != TOKEN_IDENT ||
2103 expand_one_symbol(list))
2104 list = &next->next;
2109 void init_include_path(void)
2111 FILE *fp;
2112 char path[256];
2113 char arch[32];
2114 char os[32];
2116 fp = popen("/bin/uname -m", "r");
2117 if (!fp)
2118 return;
2119 if (!fgets(arch, sizeof(arch) - 1, fp))
2120 return;
2121 pclose(fp);
2122 if (arch[strlen(arch) - 1] == '\n')
2123 arch[strlen(arch) - 1] = '\0';
2125 fp = popen("/bin/uname -o", "r");
2126 if (!fp)
2127 return;
2128 fgets(os, sizeof(os) - 1, fp);
2129 pclose(fp);
2131 if (strcmp(os, "GNU/Linux\n") != 0)
2132 return;
2133 strcpy(os, "linux-gnu");
2135 snprintf(path, sizeof(path), "/usr/include/%s-%s/", arch, os);
2136 add_pre_buffer("#add_system \"%s/\"\n", path);
2139 struct token * preprocess(struct token *token)
2141 preprocessing = 1;
2142 init_preprocessor();
2143 do_preprocess(&token);
2145 // Drop all expressions from preprocessing, they're not used any more.
2146 // This is not true when we have multiple files, though ;/
2147 // clear_expression_alloc();
2148 preprocessing = 0;
2150 return token;