Revert "Fix mistaken comparison that becomes a no-op."
[smatch.git] / pre-process.c
blobd521318669959ed59834376b3e37da2f50bfe6eb
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 * Licensed under the Open Software License version 1.1
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <time.h>
23 #include "lib.h"
24 #include "allocate.h"
25 #include "parse.h"
26 #include "token.h"
27 #include "symbol.h"
28 #include "expression.h"
29 #include "scope.h"
31 static int false_nesting = 0;
33 #define INCLUDEPATHS 300
34 const char *includepath[INCLUDEPATHS+1] = {
35 "",
36 "/usr/include",
37 "/usr/local/include",
38 NULL
41 static const char **quote_includepath = includepath;
42 static const char **angle_includepath = includepath + 1;
43 static const char **isys_includepath = includepath + 1;
44 static const char **sys_includepath = includepath + 1;
45 static const char **dirafter_includepath = includepath + 3;
47 #define dirty_stream(stream) \
48 do { \
49 if (!stream->dirty) { \
50 stream->dirty = 1; \
51 if (!stream->ifndef) \
52 stream->protect = NULL; \
53 } \
54 } while(0)
56 #define end_group(stream) \
57 do { \
58 if (stream->ifndef == stream->top_if) { \
59 stream->ifndef = NULL; \
60 if (!stream->dirty) \
61 stream->protect = NULL; \
62 else if (stream->protect) \
63 stream->dirty = 0; \
64 } \
65 } while(0)
67 #define nesting_error(stream) \
68 do { \
69 stream->dirty = 1; \
70 stream->ifndef = NULL; \
71 stream->protect = NULL; \
72 } while(0)
74 static struct token *alloc_token(struct position *pos)
76 struct token *token = __alloc_token(0);
78 token->pos.stream = pos->stream;
79 token->pos.line = pos->line;
80 token->pos.pos = pos->pos;
81 token->pos.whitespace = 1;
82 return token;
85 /* Expand symbol 'sym' at '*list' */
86 static int expand(struct token **, struct symbol *);
88 static void replace_with_string(struct token *token, const char *str)
90 int size = strlen(str) + 1;
91 struct string *s = __alloc_string(size);
93 s->length = size;
94 memcpy(s->data, str, size);
95 token_type(token) = TOKEN_STRING;
96 token->string = s;
99 static void replace_with_integer(struct token *token, unsigned int val)
101 char *buf = __alloc_bytes(11);
102 sprintf(buf, "%u", val);
103 token_type(token) = TOKEN_NUMBER;
104 token->number = buf;
107 static struct symbol *lookup_macro(struct ident *ident)
109 struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
110 if (sym && sym->namespace != NS_MACRO)
111 sym = NULL;
112 return sym;
115 static int token_defined(struct token *token)
117 if (token_type(token) == TOKEN_IDENT) {
118 struct symbol *sym = lookup_macro(token->ident);
119 if (sym) {
120 sym->used_in = file_scope;
121 return 1;
123 return 0;
126 sparse_error(token->pos, "expected preprocessor identifier");
127 return 0;
130 static void replace_with_defined(struct token *token)
132 static const char *string[] = { "0", "1" };
133 int defined = token_defined(token);
135 token_type(token) = TOKEN_NUMBER;
136 token->number = string[defined];
139 static int expand_one_symbol(struct token **list)
141 struct token *token = *list;
142 struct symbol *sym;
143 static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
144 static time_t t = 0;
146 if (token->pos.noexpand)
147 return 1;
149 sym = lookup_macro(token->ident);
150 if (sym) {
151 sym->used_in = file_scope;
152 return expand(list, sym);
154 if (token->ident == &__LINE___ident) {
155 replace_with_integer(token, token->pos.line);
156 } else if (token->ident == &__FILE___ident) {
157 replace_with_string(token, stream_name(token->pos.stream));
158 } else if (token->ident == &__DATE___ident) {
159 if (!t)
160 time(&t);
161 strftime(buffer, 12, "%b %e %Y", localtime(&t));
162 replace_with_string(token, buffer);
163 } else if (token->ident == &__TIME___ident) {
164 if (!t)
165 time(&t);
166 strftime(buffer, 9, "%T", localtime(&t));
167 replace_with_string(token, buffer);
169 return 1;
172 static inline struct token *scan_next(struct token **where)
174 struct token *token = *where;
175 if (token_type(token) != TOKEN_UNTAINT)
176 return token;
177 do {
178 token->ident->tainted = 0;
179 token = token->next;
180 } while (token_type(token) == TOKEN_UNTAINT);
181 *where = token;
182 return token;
185 static void expand_list(struct token **list)
187 struct token *next;
188 while (!eof_token(next = scan_next(list))) {
189 if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
190 list = &next->next;
194 static void preprocessor_line(struct stream *stream, struct token **line);
196 static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
198 struct stream *stream = input_streams + prev->pos.stream;
199 struct token **p = &prev->next;
200 struct token *next;
201 int nesting = 0;
203 while (!eof_token(next = scan_next(p))) {
204 if (next->pos.newline && match_op(next, '#')) {
205 if (!next->pos.noexpand) {
206 sparse_error(next->pos,
207 "directive in argument list");
208 preprocessor_line(stream, p);
209 __free_token(next); /* Free the '#' token */
210 continue;
213 switch (token_type(next)) {
214 case TOKEN_STREAMEND:
215 case TOKEN_STREAMBEGIN:
216 *p = &eof_token_entry;
217 return next;
219 if (false_nesting) {
220 *p = next->next;
221 __free_token(next);
222 continue;
224 if (match_op(next, '(')) {
225 nesting++;
226 } else if (match_op(next, ')')) {
227 if (!nesting--)
228 break;
229 } else if (match_op(next, ',') && !nesting && !vararg) {
230 break;
232 next->pos.stream = pos->stream;
233 next->pos.line = pos->line;
234 next->pos.pos = pos->pos;
235 p = &next->next;
237 *p = &eof_token_entry;
238 return next;
242 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
245 struct arg {
246 struct token *arg;
247 struct token *expanded;
248 struct token *str;
249 int n_normal;
250 int n_quoted;
251 int n_str;
254 static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
256 int wanted = arglist->count.normal;
257 struct token *next = NULL;
258 int count = 0;
260 arglist = arglist->next; /* skip counter */
262 if (!wanted) {
263 next = collect_arg(start, 0, &what->pos);
264 if (eof_token(next))
265 goto Eclosing;
266 if (!eof_token(start->next) || !match_op(next, ')')) {
267 count++;
268 goto Emany;
270 } else {
271 for (count = 0; count < wanted; count++) {
272 struct argcount *p = &arglist->next->count;
273 next = collect_arg(start, p->vararg, &what->pos);
274 arglist = arglist->next->next;
275 if (eof_token(next))
276 goto Eclosing;
277 args[count].arg = start->next;
278 args[count].n_normal = p->normal;
279 args[count].n_quoted = p->quoted;
280 args[count].n_str = p->str;
281 if (match_op(next, ')')) {
282 count++;
283 break;
285 start = next;
287 if (count == wanted && !match_op(next, ')'))
288 goto Emany;
289 if (count == wanted - 1) {
290 struct argcount *p = &arglist->next->count;
291 if (!p->vararg)
292 goto Efew;
293 args[count].arg = NULL;
294 args[count].n_normal = p->normal;
295 args[count].n_quoted = p->quoted;
296 args[count].n_str = p->str;
298 if (count < wanted - 1)
299 goto Efew;
301 what->next = next->next;
302 return 1;
304 Efew:
305 sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
306 show_token(what), wanted, count);
307 goto out;
308 Emany:
309 while (match_op(next, ',')) {
310 next = collect_arg(next, 0, &what->pos);
311 count++;
313 if (eof_token(next))
314 goto Eclosing;
315 sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
316 show_token(what), count, wanted);
317 goto out;
318 Eclosing:
319 sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
320 show_token(what));
321 out:
322 what->next = next->next;
323 return 0;
326 static struct token *dup_list(struct token *list)
328 struct token *res = NULL;
329 struct token **p = &res;
331 while (!eof_token(list)) {
332 struct token *newtok = __alloc_token(0);
333 *newtok = *list;
334 *p = newtok;
335 p = &newtok->next;
336 list = list->next;
338 return res;
341 static const char *show_token_sequence(struct token *token, int quote)
343 static char buffer[MAX_STRING];
344 char *ptr = buffer;
345 int whitespace = 0;
347 if (!token && !quote)
348 return "<none>";
349 while (!eof_token(token)) {
350 const char *val = quote ? quote_token(token) : show_token(token);
351 int len = strlen(val);
353 if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
354 sparse_error(token->pos, "too long token expansion");
355 break;
358 if (whitespace)
359 *ptr++ = ' ';
360 memcpy(ptr, val, len);
361 ptr += len;
362 token = token->next;
363 whitespace = token->pos.whitespace;
365 *ptr = 0;
366 return buffer;
369 static struct token *stringify(struct token *arg)
371 const char *s = show_token_sequence(arg, 1);
372 int size = strlen(s)+1;
373 struct token *token = __alloc_token(0);
374 struct string *string = __alloc_string(size);
376 memcpy(string->data, s, size);
377 string->length = size;
378 token->pos = arg->pos;
379 token_type(token) = TOKEN_STRING;
380 token->string = string;
381 token->next = &eof_token_entry;
382 return token;
385 static void expand_arguments(int count, struct arg *args)
387 int i;
388 for (i = 0; i < count; i++) {
389 struct token *arg = args[i].arg;
390 if (!arg)
391 arg = &eof_token_entry;
392 if (args[i].n_str)
393 args[i].str = stringify(arg);
394 if (args[i].n_normal) {
395 if (!args[i].n_quoted) {
396 args[i].expanded = arg;
397 args[i].arg = NULL;
398 } else if (eof_token(arg)) {
399 args[i].expanded = arg;
400 } else {
401 args[i].expanded = dup_list(arg);
403 expand_list(&args[i].expanded);
409 * Possibly valid combinations:
410 * - ident + ident -> ident
411 * - ident + number -> ident unless number contains '.', '+' or '-'.
412 * - 'L' + char constant -> wide char constant
413 * - 'L' + string literal -> wide string literal
414 * - number + number -> number
415 * - number + ident -> number
416 * - number + '.' -> number
417 * - number + '+' or '-' -> number, if number used to end on [eEpP].
418 * - '.' + number -> number, if number used to start with a digit.
419 * - special + special -> either special or an error.
421 static enum token_type combine(struct token *left, struct token *right, char *p)
423 int len;
424 enum token_type t1 = token_type(left), t2 = token_type(right);
426 if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
427 return TOKEN_ERROR;
429 if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
430 if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
431 return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
432 if (t2 == TOKEN_STRING)
433 return TOKEN_WIDE_STRING;
436 if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
437 return TOKEN_ERROR;
439 strcpy(p, show_token(left));
440 strcat(p, show_token(right));
441 len = strlen(p);
443 if (len >= 256)
444 return TOKEN_ERROR;
446 if (t1 == TOKEN_IDENT) {
447 if (t2 == TOKEN_SPECIAL)
448 return TOKEN_ERROR;
449 if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
450 return TOKEN_ERROR;
451 return TOKEN_IDENT;
454 if (t1 == TOKEN_NUMBER) {
455 if (t2 == TOKEN_SPECIAL) {
456 switch (right->special) {
457 case '.':
458 break;
459 case '+': case '-':
460 if (strchr("eEpP", p[len - 2]))
461 break;
462 default:
463 return TOKEN_ERROR;
466 return TOKEN_NUMBER;
469 if (p[0] == '.' && isdigit((unsigned char)p[1]))
470 return TOKEN_NUMBER;
472 return TOKEN_SPECIAL;
475 static int merge(struct token *left, struct token *right)
477 static char buffer[512];
478 enum token_type res = combine(left, right, buffer);
479 int n;
481 switch (res) {
482 case TOKEN_IDENT:
483 left->ident = built_in_ident(buffer);
484 left->pos.noexpand = 0;
485 return 1;
487 case TOKEN_NUMBER: {
488 char *number = __alloc_bytes(strlen(buffer) + 1);
489 memcpy(number, buffer, strlen(buffer) + 1);
490 token_type(left) = TOKEN_NUMBER; /* could be . + num */
491 left->number = number;
492 return 1;
495 case TOKEN_SPECIAL:
496 if (buffer[2] && buffer[3])
497 break;
498 for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
499 if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
500 left->special = n;
501 return 1;
504 break;
506 case TOKEN_WIDE_CHAR:
507 case TOKEN_WIDE_STRING:
508 token_type(left) = res;
509 left->pos.noexpand = 0;
510 left->string = right->string;
511 return 1;
513 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
514 token_type(left) = res;
515 left->pos.noexpand = 0;
516 memcpy(left->embedded, right->embedded, 4);
517 return 1;
519 default:
522 sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
523 return 0;
526 static struct token *dup_token(struct token *token, struct position *streampos)
528 struct token *alloc = alloc_token(streampos);
529 token_type(alloc) = token_type(token);
530 alloc->pos.newline = token->pos.newline;
531 alloc->pos.whitespace = token->pos.whitespace;
532 alloc->number = token->number;
533 alloc->pos.noexpand = token->pos.noexpand;
534 return alloc;
537 static struct token **copy(struct token **where, struct token *list, int *count)
539 int need_copy = --*count;
540 while (!eof_token(list)) {
541 struct token *token;
542 if (need_copy)
543 token = dup_token(list, &list->pos);
544 else
545 token = list;
546 if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
547 token->pos.noexpand = 1;
548 *where = token;
549 where = &token->next;
550 list = list->next;
552 *where = &eof_token_entry;
553 return where;
556 static int handle_kludge(struct token **p, struct arg *args)
558 struct token *t = (*p)->next->next;
559 while (1) {
560 struct arg *v = &args[t->argnum];
561 if (token_type(t->next) != TOKEN_CONCAT) {
562 if (v->arg) {
563 /* ignore the first ## */
564 *p = (*p)->next;
565 return 0;
567 /* skip the entire thing */
568 *p = t;
569 return 1;
571 if (v->arg && !eof_token(v->arg))
572 return 0; /* no magic */
573 t = t->next->next;
577 static struct token **substitute(struct token **list, struct token *body, struct arg *args)
579 struct position *base_pos = &(*list)->pos;
580 int *count;
581 enum {Normal, Placeholder, Concat} state = Normal;
583 for (; !eof_token(body); body = body->next) {
584 struct token *added, *arg;
585 struct token **tail;
586 struct token *t;
588 switch (token_type(body)) {
589 case TOKEN_GNU_KLUDGE:
591 * GNU kludge: if we had <comma>##<vararg>, behaviour
592 * depends on whether we had enough arguments to have
593 * a vararg. If we did, ## is just ignored. Otherwise
594 * both , and ## are ignored. Worse, there can be
595 * an arbitrary number of ##<arg> in between; if all of
596 * those are empty, we act as if they hadn't been there,
597 * otherwise we act as if the kludge didn't exist.
599 t = body;
600 if (handle_kludge(&body, args)) {
601 if (state == Concat)
602 state = Normal;
603 else
604 state = Placeholder;
605 continue;
607 added = dup_token(t, base_pos);
608 token_type(added) = TOKEN_SPECIAL;
609 tail = &added->next;
610 break;
612 case TOKEN_STR_ARGUMENT:
613 arg = args[body->argnum].str;
614 count = &args[body->argnum].n_str;
615 goto copy_arg;
617 case TOKEN_QUOTED_ARGUMENT:
618 arg = args[body->argnum].arg;
619 count = &args[body->argnum].n_quoted;
620 if (!arg || eof_token(arg)) {
621 if (state == Concat)
622 state = Normal;
623 else
624 state = Placeholder;
625 continue;
627 goto copy_arg;
629 case TOKEN_MACRO_ARGUMENT:
630 arg = args[body->argnum].expanded;
631 count = &args[body->argnum].n_normal;
632 if (eof_token(arg)) {
633 state = Normal;
634 continue;
636 copy_arg:
637 tail = copy(&added, arg, count);
638 added->pos.newline = body->pos.newline;
639 added->pos.whitespace = body->pos.whitespace;
640 break;
642 case TOKEN_CONCAT:
643 if (state == Placeholder)
644 state = Normal;
645 else
646 state = Concat;
647 continue;
649 case TOKEN_IDENT:
650 added = dup_token(body, base_pos);
651 if (added->ident->tainted)
652 added->pos.noexpand = 1;
653 tail = &added->next;
654 break;
656 default:
657 added = dup_token(body, base_pos);
658 tail = &added->next;
659 break;
663 * if we got to doing real concatenation, we already have
664 * added something into the list, so containing_token() is OK.
666 if (state == Concat && merge(containing_token(list), added)) {
667 *list = added->next;
668 if (tail != &added->next)
669 list = tail;
670 } else {
671 *list = added;
672 list = tail;
674 state = Normal;
676 *list = &eof_token_entry;
677 return list;
680 static int expand(struct token **list, struct symbol *sym)
682 struct token *last;
683 struct token *token = *list;
684 struct ident *expanding = token->ident;
685 struct token **tail;
686 int nargs = sym->arglist ? sym->arglist->count.normal : 0;
687 struct arg args[nargs];
689 if (expanding->tainted) {
690 token->pos.noexpand = 1;
691 return 1;
694 if (sym->arglist) {
695 if (!match_op(scan_next(&token->next), '('))
696 return 1;
697 if (!collect_arguments(token->next, sym->arglist, args, token))
698 return 1;
699 expand_arguments(nargs, args);
702 expanding->tainted = 1;
704 last = token->next;
705 tail = substitute(list, sym->expansion, args);
707 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
708 * We still can lose the newline flag if the sucker expands to nothing,
709 * but the price of dealing with that is probably too high (we'd need
710 * to collect the flags during scan_next())
712 (*list)->pos.newline = token->pos.newline;
713 (*list)->pos.whitespace = token->pos.whitespace;
714 *tail = last;
716 return 0;
719 static const char *token_name_sequence(struct token *token, int endop, struct token *start)
721 static char buffer[256];
722 char *ptr = buffer;
724 while (!eof_token(token) && !match_op(token, endop)) {
725 int len;
726 const char *val = token->string->data;
727 if (token_type(token) != TOKEN_STRING)
728 val = show_token(token);
729 len = strlen(val);
730 memcpy(ptr, val, len);
731 ptr += len;
732 token = token->next;
734 *ptr = 0;
735 if (endop && !match_op(token, endop))
736 sparse_error(start->pos, "expected '>' at end of filename");
737 return buffer;
740 static int already_tokenized(const char *path)
742 int stream, next;
744 for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
745 struct stream *s = input_streams + stream;
747 next = s->next_stream;
748 if (s->once) {
749 if (strcmp(path, s->name))
750 continue;
751 return 1;
753 if (s->constant != CONSTANT_FILE_YES)
754 continue;
755 if (strcmp(path, s->name))
756 continue;
757 if (s->protect && !lookup_macro(s->protect))
758 continue;
759 return 1;
761 return 0;
764 /* Handle include of header files.
765 * The relevant options are made compatible with gcc. The only options that
766 * are not supported is -withprefix and friends.
768 * Three set of include paths are known:
769 * quote_includepath: Path to search when using #include "file.h"
770 * angle_includepath: Paths to search when using #include <file.h>
771 * isys_includepath: Paths specified with -isystem, come before the
772 * built-in system include paths. Gcc would suppress
773 * warnings from system headers. Here we separate
774 * them from the angle_ ones to keep search ordering.
776 * sys_includepath: Built-in include paths.
777 * dirafter_includepath Paths added with -dirafter.
779 * The above is implemented as one array with pointers
780 * +--------------+
781 * quote_includepath ---> | |
782 * +--------------+
783 * | |
784 * +--------------+
785 * angle_includepath ---> | |
786 * +--------------+
787 * isys_includepath ---> | |
788 * +--------------+
789 * sys_includepath ---> | |
790 * +--------------+
791 * dirafter_includepath -> | |
792 * +--------------+
794 * -I dir insert dir just before isys_includepath and move the rest
795 * -I- makes all dirs specified with -I before to quote dirs only and
796 * angle_includepath is set equal to isys_includepath.
797 * -nostdinc removes all sys dirs by storing NULL in entry pointed
798 * to by * sys_includepath. Note that this will reset all dirs built-in
799 * and added before -nostdinc by -isystem and -idirafter.
800 * -isystem dir adds dir where isys_includepath points adding this dir as
801 * first systemdir
802 * -idirafter dir adds dir to the end of the list
805 static void set_stream_include_path(struct stream *stream)
807 const char *path = stream->path;
808 if (!path) {
809 const char *p = strrchr(stream->name, '/');
810 path = "";
811 if (p) {
812 int len = p - stream->name + 1;
813 char *m = malloc(len+1);
814 /* This includes the final "/" */
815 memcpy(m, stream->name, len);
816 m[len] = 0;
817 path = m;
819 stream->path = path;
821 includepath[0] = path;
824 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
826 int fd;
827 int plen = strlen(path);
828 static char fullname[PATH_MAX];
830 memcpy(fullname, path, plen);
831 if (plen && path[plen-1] != '/') {
832 fullname[plen] = '/';
833 plen++;
835 memcpy(fullname+plen, filename, flen);
836 if (already_tokenized(fullname))
837 return 1;
838 fd = open(fullname, O_RDONLY);
839 if (fd >= 0) {
840 char * streamname = __alloc_bytes(plen + flen);
841 memcpy(streamname, fullname, plen + flen);
842 *where = tokenize(streamname, fd, *where, next_path);
843 close(fd);
844 return 1;
846 return 0;
849 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
851 const char *path;
853 while ((path = *pptr++) != NULL) {
854 if (!try_include(path, filename, flen, list, pptr))
855 continue;
856 return 1;
858 return 0;
861 static int free_preprocessor_line(struct token *token)
863 while (token_type(token) != TOKEN_EOF) {
864 struct token *free = token;
865 token = token->next;
866 __free_token(free);
868 return 1;
871 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how)
873 const char *filename;
874 struct token *next;
875 const char **path;
876 int expect;
877 int flen;
879 next = token->next;
880 expect = '>';
881 if (!match_op(next, '<')) {
882 expand_list(&token->next);
883 expect = 0;
884 next = token;
885 if (match_op(token->next, '<')) {
886 next = token->next;
887 expect = '>';
891 token = next->next;
892 filename = token_name_sequence(token, expect, token);
893 flen = strlen(filename) + 1;
895 /* Absolute path? */
896 if (filename[0] == '/') {
897 if (try_include("", filename, flen, list, includepath))
898 return 0;
899 goto out;
902 switch (how) {
903 case 1:
904 path = stream->next_path;
905 break;
906 case 2:
907 includepath[0] = "";
908 path = includepath;
909 break;
910 default:
911 /* Dir of input file is first dir to search for quoted includes */
912 set_stream_include_path(stream);
913 path = expect ? angle_includepath : quote_includepath;
914 break;
916 /* Check the standard include paths.. */
917 if (do_include_path(path, list, token, filename, flen))
918 return 0;
919 out:
920 error_die(token->pos, "unable to open '%s'", filename);
923 static int handle_include(struct stream *stream, struct token **list, struct token *token)
925 return handle_include_path(stream, list, token, 0);
928 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
930 return handle_include_path(stream, list, token, 1);
933 static int handle_argv_include(struct stream *stream, struct token **list, struct token *token)
935 return handle_include_path(stream, list, token, 2);
938 static int token_different(struct token *t1, struct token *t2)
940 int different;
942 if (token_type(t1) != token_type(t2))
943 return 1;
945 switch (token_type(t1)) {
946 case TOKEN_IDENT:
947 different = t1->ident != t2->ident;
948 break;
949 case TOKEN_ARG_COUNT:
950 case TOKEN_UNTAINT:
951 case TOKEN_CONCAT:
952 case TOKEN_GNU_KLUDGE:
953 different = 0;
954 break;
955 case TOKEN_NUMBER:
956 different = strcmp(t1->number, t2->number);
957 break;
958 case TOKEN_SPECIAL:
959 different = t1->special != t2->special;
960 break;
961 case TOKEN_MACRO_ARGUMENT:
962 case TOKEN_QUOTED_ARGUMENT:
963 case TOKEN_STR_ARGUMENT:
964 different = t1->argnum != t2->argnum;
965 break;
966 case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
967 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
968 different = memcmp(t1->embedded, t2->embedded, 4);
969 break;
970 case TOKEN_CHAR:
971 case TOKEN_WIDE_CHAR:
972 case TOKEN_STRING:
973 case TOKEN_WIDE_STRING: {
974 struct string *s1, *s2;
976 s1 = t1->string;
977 s2 = t2->string;
978 different = 1;
979 if (s1->length != s2->length)
980 break;
981 different = memcmp(s1->data, s2->data, s1->length);
982 break;
984 default:
985 different = 1;
986 break;
988 return different;
991 static int token_list_different(struct token *list1, struct token *list2)
993 for (;;) {
994 if (list1 == list2)
995 return 0;
996 if (!list1 || !list2)
997 return 1;
998 if (token_different(list1, list2))
999 return 1;
1000 list1 = list1->next;
1001 list2 = list2->next;
1005 static inline void set_arg_count(struct token *token)
1007 token_type(token) = TOKEN_ARG_COUNT;
1008 token->count.normal = token->count.quoted =
1009 token->count.str = token->count.vararg = 0;
1012 static struct token *parse_arguments(struct token *list)
1014 struct token *arg = list->next, *next = list;
1015 struct argcount *count = &list->count;
1017 set_arg_count(list);
1019 if (match_op(arg, ')')) {
1020 next = arg->next;
1021 list->next = &eof_token_entry;
1022 return next;
1025 while (token_type(arg) == TOKEN_IDENT) {
1026 if (arg->ident == &__VA_ARGS___ident)
1027 goto Eva_args;
1028 if (!++count->normal)
1029 goto Eargs;
1030 next = arg->next;
1032 if (match_op(next, ',')) {
1033 set_arg_count(next);
1034 arg = next->next;
1035 continue;
1038 if (match_op(next, ')')) {
1039 set_arg_count(next);
1040 next = next->next;
1041 arg->next->next = &eof_token_entry;
1042 return next;
1045 /* normal cases are finished here */
1047 if (match_op(next, SPECIAL_ELLIPSIS)) {
1048 if (match_op(next->next, ')')) {
1049 set_arg_count(next);
1050 next->count.vararg = 1;
1051 next = next->next;
1052 arg->next->next = &eof_token_entry;
1053 return next->next;
1056 arg = next;
1057 goto Enotclosed;
1060 if (eof_token(next)) {
1061 goto Enotclosed;
1062 } else {
1063 arg = next;
1064 goto Ebadstuff;
1068 if (match_op(arg, SPECIAL_ELLIPSIS)) {
1069 next = arg->next;
1070 token_type(arg) = TOKEN_IDENT;
1071 arg->ident = &__VA_ARGS___ident;
1072 if (!match_op(next, ')'))
1073 goto Enotclosed;
1074 if (!++count->normal)
1075 goto Eargs;
1076 set_arg_count(next);
1077 next->count.vararg = 1;
1078 next = next->next;
1079 arg->next->next = &eof_token_entry;
1080 return next;
1083 if (eof_token(arg)) {
1084 arg = next;
1085 goto Enotclosed;
1087 if (match_op(arg, ','))
1088 goto Emissing;
1089 else
1090 goto Ebadstuff;
1093 Emissing:
1094 sparse_error(arg->pos, "parameter name missing");
1095 return NULL;
1096 Ebadstuff:
1097 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
1098 show_token(arg));
1099 return NULL;
1100 Enotclosed:
1101 sparse_error(arg->pos, "missing ')' in macro parameter list");
1102 return NULL;
1103 Eva_args:
1104 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1105 return NULL;
1106 Eargs:
1107 sparse_error(arg->pos, "too many arguments in macro definition");
1108 return NULL;
1111 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1113 struct ident *ident = token->ident;
1114 int nr;
1116 if (!arglist || token_type(token) != TOKEN_IDENT)
1117 return 0;
1119 arglist = arglist->next;
1121 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1122 if (arglist->ident == ident) {
1123 struct argcount *count = &arglist->next->count;
1124 int n;
1126 token->argnum = nr;
1127 token_type(token) = type;
1128 switch (type) {
1129 case TOKEN_MACRO_ARGUMENT:
1130 n = ++count->normal;
1131 break;
1132 case TOKEN_QUOTED_ARGUMENT:
1133 n = ++count->quoted;
1134 break;
1135 default:
1136 n = ++count->str;
1138 if (n)
1139 return count->vararg ? 2 : 1;
1141 * XXX - need saner handling of that
1142 * (>= 1024 instances of argument)
1144 token_type(token) = TOKEN_ERROR;
1145 return -1;
1148 return 0;
1151 static struct token *handle_hash(struct token **p, struct token *arglist)
1153 struct token *token = *p;
1154 if (arglist) {
1155 struct token *next = token->next;
1156 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1157 goto Equote;
1158 next->pos.whitespace = token->pos.whitespace;
1159 __free_token(token);
1160 token = *p = next;
1161 } else {
1162 token->pos.noexpand = 1;
1164 return token;
1166 Equote:
1167 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1168 return NULL;
1171 /* token->next is ## */
1172 static struct token *handle_hashhash(struct token *token, struct token *arglist)
1174 struct token *last = token;
1175 struct token *concat;
1176 int state = match_op(token, ',');
1178 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1180 while (1) {
1181 struct token *t;
1182 int is_arg;
1184 /* eat duplicate ## */
1185 concat = token->next;
1186 while (match_op(t = concat->next, SPECIAL_HASHHASH)) {
1187 token->next = t;
1188 __free_token(concat);
1189 concat = t;
1191 token_type(concat) = TOKEN_CONCAT;
1193 if (eof_token(t))
1194 goto Econcat;
1196 if (match_op(t, '#')) {
1197 t = handle_hash(&concat->next, arglist);
1198 if (!t)
1199 return NULL;
1202 is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
1204 if (state == 1 && is_arg) {
1205 state = is_arg;
1206 } else {
1207 last = t;
1208 state = match_op(t, ',');
1211 token = t;
1212 if (!match_op(token->next, SPECIAL_HASHHASH))
1213 break;
1215 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1216 if (state == 2)
1217 token_type(last) = TOKEN_GNU_KLUDGE;
1218 return token;
1220 Econcat:
1221 sparse_error(concat->pos, "'##' cannot appear at the ends of macro expansion");
1222 return NULL;
1225 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1227 struct token *token = expansion;
1228 struct token **p;
1230 if (match_op(token, SPECIAL_HASHHASH))
1231 goto Econcat;
1233 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1234 if (match_op(token, '#')) {
1235 token = handle_hash(p, arglist);
1236 if (!token)
1237 return NULL;
1239 if (match_op(token->next, SPECIAL_HASHHASH)) {
1240 token = handle_hashhash(token, arglist);
1241 if (!token)
1242 return NULL;
1243 } else {
1244 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1246 if (token_type(token) == TOKEN_ERROR)
1247 goto Earg;
1249 token = alloc_token(&expansion->pos);
1250 token_type(token) = TOKEN_UNTAINT;
1251 token->ident = name;
1252 token->next = *p;
1253 *p = token;
1254 return expansion;
1256 Econcat:
1257 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1258 return NULL;
1259 Earg:
1260 sparse_error(token->pos, "too many instances of argument in body");
1261 return NULL;
1264 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1266 struct token *arglist, *expansion;
1267 struct token *left = token->next;
1268 struct symbol *sym;
1269 struct ident *name;
1270 int ret;
1272 if (token_type(left) != TOKEN_IDENT) {
1273 sparse_error(token->pos, "expected identifier to 'define'");
1274 return 1;
1277 name = left->ident;
1279 arglist = NULL;
1280 expansion = left->next;
1281 if (!expansion->pos.whitespace) {
1282 if (match_op(expansion, '(')) {
1283 arglist = expansion;
1284 expansion = parse_arguments(expansion);
1285 if (!expansion)
1286 return 1;
1287 } else if (!eof_token(expansion)) {
1288 warning(expansion->pos,
1289 "no whitespace before object-like macro body");
1293 expansion = parse_expansion(expansion, arglist, name);
1294 if (!expansion)
1295 return 1;
1297 ret = 1;
1298 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1299 if (sym) {
1300 int clean;
1302 if (attr < sym->attr)
1303 goto out;
1305 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1307 if (token_list_different(sym->expansion, expansion) ||
1308 token_list_different(sym->arglist, arglist)) {
1309 ret = 0;
1310 if ((clean && attr == SYM_ATTR_NORMAL)
1311 || sym->used_in == file_scope) {
1312 warning(left->pos, "preprocessor token %.*s redefined",
1313 name->len, name->name);
1314 info(sym->pos, "this was the original definition");
1316 } else if (clean)
1317 goto out;
1320 if (!sym || sym->scope != file_scope) {
1321 sym = alloc_symbol(left->pos, SYM_NODE);
1322 bind_symbol(sym, name, NS_MACRO);
1323 ret = 0;
1326 if (!ret) {
1327 sym->expansion = expansion;
1328 sym->arglist = arglist;
1329 __free_token(token); /* Free the "define" token, but not the rest of the line */
1332 sym->namespace = NS_MACRO;
1333 sym->used_in = NULL;
1334 sym->attr = attr;
1335 out:
1336 return ret;
1339 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1341 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1344 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1346 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1349 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1351 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1354 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1356 struct token *left = token->next;
1357 struct symbol *sym;
1359 if (token_type(left) != TOKEN_IDENT) {
1360 sparse_error(token->pos, "expected identifier to 'undef'");
1361 return 1;
1364 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1365 if (sym) {
1366 if (attr < sym->attr)
1367 return 1;
1368 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1369 return 1;
1370 } else if (attr <= SYM_ATTR_NORMAL)
1371 return 1;
1373 if (!sym || sym->scope != file_scope) {
1374 sym = alloc_symbol(left->pos, SYM_NODE);
1375 bind_symbol(sym, left->ident, NS_MACRO);
1378 sym->namespace = NS_UNDEF;
1379 sym->used_in = NULL;
1380 sym->attr = attr;
1382 return 1;
1385 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1387 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1390 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1392 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1395 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1397 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1398 free_preprocessor_line(token->next);
1399 token->next = stream->top_if;
1400 stream->top_if = token;
1401 if (false_nesting || true != 1)
1402 false_nesting++;
1403 return 0;
1406 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1408 struct token *next = token->next;
1409 int arg;
1410 if (token_type(next) == TOKEN_IDENT) {
1411 arg = token_defined(next);
1412 } else {
1413 dirty_stream(stream);
1414 if (!false_nesting)
1415 sparse_error(token->pos, "expected preprocessor identifier");
1416 arg = -1;
1418 return preprocessor_if(stream, token, arg);
1421 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1423 struct token *next = token->next;
1424 int arg;
1425 if (token_type(next) == TOKEN_IDENT) {
1426 if (!stream->dirty && !stream->ifndef) {
1427 if (!stream->protect) {
1428 stream->ifndef = token;
1429 stream->protect = next->ident;
1430 } else if (stream->protect == next->ident) {
1431 stream->ifndef = token;
1432 stream->dirty = 1;
1435 arg = !token_defined(next);
1436 } else {
1437 dirty_stream(stream);
1438 if (!false_nesting)
1439 sparse_error(token->pos, "expected preprocessor identifier");
1440 arg = -1;
1443 return preprocessor_if(stream, token, arg);
1446 static const char *show_token_sequence(struct token *token, int quote);
1449 * Expression handling for #if and #elif; it differs from normal expansion
1450 * due to special treatment of "defined".
1452 static int expression_value(struct token **where)
1454 struct expression *expr;
1455 struct token *p;
1456 struct token **list = where, **beginning = NULL;
1457 long long value;
1458 int state = 0;
1460 while (!eof_token(p = scan_next(list))) {
1461 switch (state) {
1462 case 0:
1463 if (token_type(p) != TOKEN_IDENT)
1464 break;
1465 if (p->ident == &defined_ident) {
1466 state = 1;
1467 beginning = list;
1468 break;
1470 if (!expand_one_symbol(list))
1471 continue;
1472 if (token_type(p) != TOKEN_IDENT)
1473 break;
1474 token_type(p) = TOKEN_ZERO_IDENT;
1475 break;
1476 case 1:
1477 if (match_op(p, '(')) {
1478 state = 2;
1479 } else {
1480 state = 0;
1481 replace_with_defined(p);
1482 *beginning = p;
1484 break;
1485 case 2:
1486 if (token_type(p) == TOKEN_IDENT)
1487 state = 3;
1488 else
1489 state = 0;
1490 replace_with_defined(p);
1491 *beginning = p;
1492 break;
1493 case 3:
1494 state = 0;
1495 if (!match_op(p, ')'))
1496 sparse_error(p->pos, "missing ')' after \"defined\"");
1497 *list = p->next;
1498 continue;
1500 list = &p->next;
1503 p = constant_expression(*where, &expr);
1504 if (!eof_token(p))
1505 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p, 0));
1506 value = get_expression_value(expr);
1507 return value != 0;
1510 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1512 int value = 0;
1513 if (!false_nesting)
1514 value = expression_value(&token->next);
1516 dirty_stream(stream);
1517 return preprocessor_if(stream, token, value);
1520 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1522 struct token *top_if = stream->top_if;
1523 end_group(stream);
1525 if (!top_if) {
1526 nesting_error(stream);
1527 sparse_error(token->pos, "unmatched #elif within stream");
1528 return 1;
1531 if (token_type(top_if) == TOKEN_ELSE) {
1532 nesting_error(stream);
1533 sparse_error(token->pos, "#elif after #else");
1534 if (!false_nesting)
1535 false_nesting = 1;
1536 return 1;
1539 dirty_stream(stream);
1540 if (token_type(top_if) != TOKEN_IF)
1541 return 1;
1542 if (false_nesting) {
1543 false_nesting = 0;
1544 if (!expression_value(&token->next))
1545 false_nesting = 1;
1546 } else {
1547 false_nesting = 1;
1548 token_type(top_if) = TOKEN_SKIP_GROUPS;
1550 return 1;
1553 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1555 struct token *top_if = stream->top_if;
1556 end_group(stream);
1558 if (!top_if) {
1559 nesting_error(stream);
1560 sparse_error(token->pos, "unmatched #else within stream");
1561 return 1;
1564 if (token_type(top_if) == TOKEN_ELSE) {
1565 nesting_error(stream);
1566 sparse_error(token->pos, "#else after #else");
1568 if (false_nesting) {
1569 if (token_type(top_if) == TOKEN_IF)
1570 false_nesting = 0;
1571 } else {
1572 false_nesting = 1;
1574 token_type(top_if) = TOKEN_ELSE;
1575 return 1;
1578 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1580 struct token *top_if = stream->top_if;
1581 end_group(stream);
1582 if (!top_if) {
1583 nesting_error(stream);
1584 sparse_error(token->pos, "unmatched #endif in stream");
1585 return 1;
1587 if (false_nesting)
1588 false_nesting--;
1589 stream->top_if = top_if->next;
1590 __free_token(top_if);
1591 return 1;
1594 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1596 warning(token->pos, "%s", show_token_sequence(token->next, 0));
1597 return 1;
1600 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1602 sparse_error(token->pos, "%s", show_token_sequence(token->next, 0));
1603 return 1;
1606 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1609 * Do we have any non-system includes?
1610 * Clear them out if so..
1612 *sys_includepath = NULL;
1613 return 1;
1616 static inline void update_inc_ptrs(const char ***where)
1619 if (*where <= dirafter_includepath) {
1620 dirafter_includepath++;
1621 /* If this was the entry that we prepend, don't
1622 * rise the lower entries, even if they are at
1623 * the same level. */
1624 if (where == &dirafter_includepath)
1625 return;
1627 if (*where <= sys_includepath) {
1628 sys_includepath++;
1629 if (where == &sys_includepath)
1630 return;
1632 if (*where <= isys_includepath) {
1633 isys_includepath++;
1634 if (where == &isys_includepath)
1635 return;
1638 /* angle_includepath is actually never updated, since we
1639 * don't suppport -iquote rught now. May change some day. */
1640 if (*where <= angle_includepath) {
1641 angle_includepath++;
1642 if (where == &angle_includepath)
1643 return;
1647 /* Add a path before 'where' and update the pointers associated with the
1648 * includepath array */
1649 static void add_path_entry(struct token *token, const char *path,
1650 const char ***where)
1652 const char **dst;
1653 const char *next;
1655 /* Need one free entry.. */
1656 if (includepath[INCLUDEPATHS-2])
1657 error_die(token->pos, "too many include path entries");
1659 /* check that this is not a duplicate */
1660 dst = includepath;
1661 while (*dst) {
1662 if (strcmp(*dst, path) == 0)
1663 return;
1664 dst++;
1666 next = path;
1667 dst = *where;
1669 update_inc_ptrs(where);
1672 * Move them all up starting at dst,
1673 * insert the new entry..
1675 do {
1676 const char *tmp = *dst;
1677 *dst = next;
1678 next = tmp;
1679 dst++;
1680 } while (next);
1683 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1685 for (;;) {
1686 token = token->next;
1687 if (eof_token(token))
1688 return 1;
1689 if (token_type(token) != TOKEN_STRING) {
1690 warning(token->pos, "expected path string");
1691 return 1;
1693 add_path_entry(token, token->string->data, &isys_includepath);
1697 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1699 for (;;) {
1700 token = token->next;
1701 if (eof_token(token))
1702 return 1;
1703 if (token_type(token) != TOKEN_STRING) {
1704 sparse_error(token->pos, "expected path string");
1705 return 1;
1707 add_path_entry(token, token->string->data, &sys_includepath);
1711 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1713 for (;;) {
1714 token = token->next;
1715 if (eof_token(token))
1716 return 1;
1717 if (token_type(token) != TOKEN_STRING) {
1718 sparse_error(token->pos, "expected path string");
1719 return 1;
1721 add_path_entry(token, token->string->data, &dirafter_includepath);
1725 /* Add to end on includepath list - no pointer updates */
1726 static void add_dirafter_entry(struct token *token, const char *path)
1728 const char **dst = includepath;
1730 /* Need one free entry.. */
1731 if (includepath[INCLUDEPATHS-2])
1732 error_die(token->pos, "too many include path entries");
1734 /* Add to the end */
1735 while (*dst)
1736 dst++;
1737 *dst = path;
1738 dst++;
1739 *dst = NULL;
1742 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1744 for (;;) {
1745 token = token->next;
1746 if (eof_token(token))
1747 return 1;
1748 if (token_type(token) != TOKEN_STRING) {
1749 sparse_error(token->pos, "expected path string");
1750 return 1;
1752 add_dirafter_entry(token, token->string->data);
1756 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1759 * -I-
1760 * From info gcc:
1761 * Split the include path. Any directories specified with `-I'
1762 * options before `-I-' are searched only for headers requested with
1763 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1764 * If additional directories are specified with `-I' options after
1765 * the `-I-', those directories are searched for all `#include'
1766 * directives.
1767 * In addition, `-I-' inhibits the use of the directory of the current
1768 * file directory as the first search directory for `#include "FILE"'.
1770 quote_includepath = includepath+1;
1771 angle_includepath = sys_includepath;
1772 return 1;
1776 * We replace "#pragma xxx" with "__pragma__" in the token
1777 * stream. Just as an example.
1779 * We'll just #define that away for now, but the theory here
1780 * is that we can use this to insert arbitrary token sequences
1781 * to turn the pragmas into internal front-end sequences for
1782 * when we actually start caring about them.
1784 * So eventually this will turn into some kind of extended
1785 * __attribute__() like thing, except called __pragma__(xxx).
1787 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1789 struct token *next = *line;
1791 if (match_ident(token->next, &once_ident) && eof_token(token->next->next)) {
1792 stream->once = 1;
1793 return 1;
1795 token->ident = &pragma_ident;
1796 token->pos.newline = 1;
1797 token->pos.whitespace = 1;
1798 token->pos.pos = 1;
1799 *line = token;
1800 token->next = next;
1801 return 0;
1805 * We ignore #line for now.
1807 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1809 return 1;
1812 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1814 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token, 0));
1815 return 1;
1819 static void init_preprocessor(void)
1821 int i;
1822 int stream = init_stream("preprocessor", -1, includepath);
1823 static struct {
1824 const char *name;
1825 int (*handler)(struct stream *, struct token **, struct token *);
1826 } normal[] = {
1827 { "define", handle_define },
1828 { "weak_define", handle_weak_define },
1829 { "strong_define", handle_strong_define },
1830 { "undef", handle_undef },
1831 { "strong_undef", handle_strong_undef },
1832 { "warning", handle_warning },
1833 { "error", handle_error },
1834 { "include", handle_include },
1835 { "include_next", handle_include_next },
1836 { "pragma", handle_pragma },
1837 { "line", handle_line },
1839 // our internal preprocessor tokens
1840 { "nostdinc", handle_nostdinc },
1841 { "add_include", handle_add_include },
1842 { "add_isystem", handle_add_isystem },
1843 { "add_system", handle_add_system },
1844 { "add_dirafter", handle_add_dirafter },
1845 { "split_include", handle_split_include },
1846 { "argv_include", handle_argv_include },
1847 }, special[] = {
1848 { "ifdef", handle_ifdef },
1849 { "ifndef", handle_ifndef },
1850 { "else", handle_else },
1851 { "endif", handle_endif },
1852 { "if", handle_if },
1853 { "elif", handle_elif },
1856 for (i = 0; i < ARRAY_SIZE(normal); i++) {
1857 struct symbol *sym;
1858 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1859 sym->handler = normal[i].handler;
1860 sym->normal = 1;
1862 for (i = 0; i < ARRAY_SIZE(special); i++) {
1863 struct symbol *sym;
1864 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1865 sym->handler = special[i].handler;
1866 sym->normal = 0;
1871 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1873 int (*handler)(struct stream *, struct token **, struct token *);
1874 struct token *token = start->next;
1875 int is_normal = 1;
1877 if (eof_token(token))
1878 return;
1880 if (token_type(token) == TOKEN_IDENT) {
1881 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1882 if (sym) {
1883 handler = sym->handler;
1884 is_normal = sym->normal;
1885 } else {
1886 handler = handle_nondirective;
1888 } else if (token_type(token) == TOKEN_NUMBER) {
1889 handler = handle_line;
1890 } else {
1891 handler = handle_nondirective;
1894 if (is_normal) {
1895 dirty_stream(stream);
1896 if (false_nesting)
1897 goto out;
1899 if (!handler(stream, line, token)) /* all set */
1900 return;
1902 out:
1903 free_preprocessor_line(token);
1906 static void preprocessor_line(struct stream *stream, struct token **line)
1908 struct token *start = *line, *next;
1909 struct token **tp = &start->next;
1911 for (;;) {
1912 next = *tp;
1913 if (next->pos.newline)
1914 break;
1915 tp = &next->next;
1917 *line = next;
1918 *tp = &eof_token_entry;
1919 handle_preprocessor_line(stream, line, start);
1922 static void do_preprocess(struct token **list)
1924 struct token *next;
1926 while (!eof_token(next = scan_next(list))) {
1927 struct stream *stream = input_streams + next->pos.stream;
1929 if (next->pos.newline && match_op(next, '#')) {
1930 if (!next->pos.noexpand) {
1931 preprocessor_line(stream, list);
1932 __free_token(next); /* Free the '#' token */
1933 continue;
1937 switch (token_type(next)) {
1938 case TOKEN_STREAMEND:
1939 if (stream->top_if) {
1940 nesting_error(stream);
1941 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1942 stream->top_if = NULL;
1943 false_nesting = 0;
1945 if (!stream->dirty)
1946 stream->constant = CONSTANT_FILE_YES;
1947 *list = next->next;
1948 continue;
1949 case TOKEN_STREAMBEGIN:
1950 *list = next->next;
1951 continue;
1953 default:
1954 dirty_stream(stream);
1955 if (false_nesting) {
1956 *list = next->next;
1957 __free_token(next);
1958 continue;
1961 if (token_type(next) != TOKEN_IDENT ||
1962 expand_one_symbol(list))
1963 list = &next->next;
1968 struct token * preprocess(struct token *token)
1970 preprocessing = 1;
1971 init_preprocessor();
1972 do_preprocess(&token);
1974 // Drop all expressions from preprocessing, they're not used any more.
1975 // This is not true when we have multiple files, though ;/
1976 // clear_expression_alloc();
1977 preprocessing = 0;
1979 return token;