Define __SIZEOF_POINTER__
[smatch.git] / pre-process.c
blob22ddf02871ef8ee12a20e50937f14f6f22236a4a
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->constant != CONSTANT_FILE_YES)
749 continue;
750 if (strcmp(path, s->name))
751 continue;
752 if (s->protect && !lookup_macro(s->protect))
753 continue;
754 return 1;
756 return 0;
759 /* Handle include of header files.
760 * The relevant options are made compatible with gcc. The only options that
761 * are not supported is -withprefix and friends.
763 * Three set of include paths are known:
764 * quote_includepath: Path to search when using #include "file.h"
765 * angle_includepath: Paths to search when using #include <file.h>
766 * isys_includepath: Paths specified with -isystem, come before the
767 * built-in system include paths. Gcc would suppress
768 * warnings from system headers. Here we separate
769 * them from the angle_ ones to keep search ordering.
771 * sys_includepath: Built-in include paths.
772 * dirafter_includepath Paths added with -dirafter.
774 * The above is implemented as one array with pointers
775 * +--------------+
776 * quote_includepath ---> | |
777 * +--------------+
778 * | |
779 * +--------------+
780 * angle_includepath ---> | |
781 * +--------------+
782 * isys_includepath ---> | |
783 * +--------------+
784 * sys_includepath ---> | |
785 * +--------------+
786 * dirafter_includepath -> | |
787 * +--------------+
789 * -I dir insert dir just before isys_includepath and move the rest
790 * -I- makes all dirs specified with -I before to quote dirs only and
791 * angle_includepath is set equal to isys_includepath.
792 * -nostdinc removes all sys dirs by storing NULL in entry pointed
793 * to by * sys_includepath. Note that this will reset all dirs built-in
794 * and added before -nostdinc by -isystem and -idirafter.
795 * -isystem dir adds dir where isys_includepath points adding this dir as
796 * first systemdir
797 * -idirafter dir adds dir to the end of the list
800 static void set_stream_include_path(struct stream *stream)
802 const char *path = stream->path;
803 if (!path) {
804 const char *p = strrchr(stream->name, '/');
805 path = "";
806 if (p) {
807 int len = p - stream->name + 1;
808 char *m = malloc(len+1);
809 /* This includes the final "/" */
810 memcpy(m, stream->name, len);
811 m[len] = 0;
812 path = m;
814 stream->path = path;
816 includepath[0] = path;
819 static int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
821 int fd;
822 int plen = strlen(path);
823 static char fullname[PATH_MAX];
825 memcpy(fullname, path, plen);
826 if (plen && path[plen-1] != '/') {
827 fullname[plen] = '/';
828 plen++;
830 memcpy(fullname+plen, filename, flen);
831 if (already_tokenized(fullname))
832 return 1;
833 fd = open(fullname, O_RDONLY);
834 if (fd >= 0) {
835 char * streamname = __alloc_bytes(plen + flen);
836 memcpy(streamname, fullname, plen + flen);
837 *where = tokenize(streamname, fd, *where, next_path);
838 close(fd);
839 return 1;
841 return 0;
844 static int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
846 const char *path;
848 while ((path = *pptr++) != NULL) {
849 if (!try_include(path, filename, flen, list, pptr))
850 continue;
851 return 1;
853 return 0;
856 static int free_preprocessor_line(struct token *token)
858 while (token_type(token) != TOKEN_EOF) {
859 struct token *free = token;
860 token = token->next;
861 __free_token(free);
863 return 1;
866 static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how)
868 const char *filename;
869 struct token *next;
870 const char **path;
871 int expect;
872 int flen;
874 next = token->next;
875 expect = '>';
876 if (!match_op(next, '<')) {
877 expand_list(&token->next);
878 expect = 0;
879 next = token;
880 if (match_op(token->next, '<')) {
881 next = token->next;
882 expect = '>';
886 token = next->next;
887 filename = token_name_sequence(token, expect, token);
888 flen = strlen(filename) + 1;
890 /* Absolute path? */
891 if (filename[0] == '/') {
892 if (try_include("", filename, flen, list, includepath))
893 return 0;
894 goto out;
897 switch (how) {
898 case 1:
899 path = stream->next_path;
900 break;
901 case 2:
902 includepath[0] = "";
903 path = includepath;
904 break;
905 default:
906 /* Dir of input file is first dir to search for quoted includes */
907 set_stream_include_path(stream);
908 path = expect ? angle_includepath : quote_includepath;
909 break;
911 /* Check the standard include paths.. */
912 if (do_include_path(path, list, token, filename, flen))
913 return 0;
914 out:
915 error_die(token->pos, "unable to open '%s'", filename);
918 static int handle_include(struct stream *stream, struct token **list, struct token *token)
920 return handle_include_path(stream, list, token, 0);
923 static int handle_include_next(struct stream *stream, struct token **list, struct token *token)
925 return handle_include_path(stream, list, token, 1);
928 static int handle_argv_include(struct stream *stream, struct token **list, struct token *token)
930 return handle_include_path(stream, list, token, 2);
933 static int token_different(struct token *t1, struct token *t2)
935 int different;
937 if (token_type(t1) != token_type(t2))
938 return 1;
940 switch (token_type(t1)) {
941 case TOKEN_IDENT:
942 different = t1->ident != t2->ident;
943 break;
944 case TOKEN_ARG_COUNT:
945 case TOKEN_UNTAINT:
946 case TOKEN_CONCAT:
947 case TOKEN_GNU_KLUDGE:
948 different = 0;
949 break;
950 case TOKEN_NUMBER:
951 different = strcmp(t1->number, t2->number);
952 break;
953 case TOKEN_SPECIAL:
954 different = t1->special != t2->special;
955 break;
956 case TOKEN_MACRO_ARGUMENT:
957 case TOKEN_QUOTED_ARGUMENT:
958 case TOKEN_STR_ARGUMENT:
959 different = t1->argnum != t2->argnum;
960 break;
961 case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
962 case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
963 different = memcmp(t1->embedded, t2->embedded, 4);
964 break;
965 case TOKEN_CHAR:
966 case TOKEN_WIDE_CHAR:
967 case TOKEN_STRING:
968 case TOKEN_WIDE_STRING: {
969 struct string *s1, *s2;
971 s1 = t1->string;
972 s2 = t2->string;
973 different = 1;
974 if (s1->length != s2->length)
975 break;
976 different = memcmp(s1->data, s2->data, s1->length);
977 break;
979 default:
980 different = 1;
981 break;
983 return different;
986 static int token_list_different(struct token *list1, struct token *list2)
988 for (;;) {
989 if (list1 == list2)
990 return 0;
991 if (!list1 || !list2)
992 return 1;
993 if (token_different(list1, list2))
994 return 1;
995 list1 = list1->next;
996 list2 = list2->next;
1000 static inline void set_arg_count(struct token *token)
1002 token_type(token) = TOKEN_ARG_COUNT;
1003 token->count.normal = token->count.quoted =
1004 token->count.str = token->count.vararg = 0;
1007 static struct token *parse_arguments(struct token *list)
1009 struct token *arg = list->next, *next = list;
1010 struct argcount *count = &list->count;
1012 set_arg_count(list);
1014 if (match_op(arg, ')')) {
1015 next = arg->next;
1016 list->next = &eof_token_entry;
1017 return next;
1020 while (token_type(arg) == TOKEN_IDENT) {
1021 if (arg->ident == &__VA_ARGS___ident)
1022 goto Eva_args;
1023 if (!++count->normal)
1024 goto Eargs;
1025 next = arg->next;
1027 if (match_op(next, ',')) {
1028 set_arg_count(next);
1029 arg = next->next;
1030 continue;
1033 if (match_op(next, ')')) {
1034 set_arg_count(next);
1035 next = next->next;
1036 arg->next->next = &eof_token_entry;
1037 return next;
1040 /* normal cases are finished here */
1042 if (match_op(next, SPECIAL_ELLIPSIS)) {
1043 if (match_op(next->next, ')')) {
1044 set_arg_count(next);
1045 next->count.vararg = 1;
1046 next = next->next;
1047 arg->next->next = &eof_token_entry;
1048 return next->next;
1051 arg = next;
1052 goto Enotclosed;
1055 if (eof_token(next)) {
1056 goto Enotclosed;
1057 } else {
1058 arg = next;
1059 goto Ebadstuff;
1063 if (match_op(arg, SPECIAL_ELLIPSIS)) {
1064 next = arg->next;
1065 token_type(arg) = TOKEN_IDENT;
1066 arg->ident = &__VA_ARGS___ident;
1067 if (!match_op(next, ')'))
1068 goto Enotclosed;
1069 if (!++count->normal)
1070 goto Eargs;
1071 set_arg_count(next);
1072 next->count.vararg = 1;
1073 next = next->next;
1074 arg->next->next = &eof_token_entry;
1075 return next;
1078 if (eof_token(arg)) {
1079 arg = next;
1080 goto Enotclosed;
1082 if (match_op(arg, ','))
1083 goto Emissing;
1084 else
1085 goto Ebadstuff;
1088 Emissing:
1089 sparse_error(arg->pos, "parameter name missing");
1090 return NULL;
1091 Ebadstuff:
1092 sparse_error(arg->pos, "\"%s\" may not appear in macro parameter list",
1093 show_token(arg));
1094 return NULL;
1095 Enotclosed:
1096 sparse_error(arg->pos, "missing ')' in macro parameter list");
1097 return NULL;
1098 Eva_args:
1099 sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1100 return NULL;
1101 Eargs:
1102 sparse_error(arg->pos, "too many arguments in macro definition");
1103 return NULL;
1106 static int try_arg(struct token *token, enum token_type type, struct token *arglist)
1108 struct ident *ident = token->ident;
1109 int nr;
1111 if (!arglist || token_type(token) != TOKEN_IDENT)
1112 return 0;
1114 arglist = arglist->next;
1116 for (nr = 0; !eof_token(arglist); nr++, arglist = arglist->next->next) {
1117 if (arglist->ident == ident) {
1118 struct argcount *count = &arglist->next->count;
1119 int n;
1121 token->argnum = nr;
1122 token_type(token) = type;
1123 switch (type) {
1124 case TOKEN_MACRO_ARGUMENT:
1125 n = ++count->normal;
1126 break;
1127 case TOKEN_QUOTED_ARGUMENT:
1128 n = ++count->quoted;
1129 break;
1130 default:
1131 n = ++count->str;
1133 if (n)
1134 return count->vararg ? 2 : 1;
1136 * XXX - need saner handling of that
1137 * (>= 1024 instances of argument)
1139 token_type(token) = TOKEN_ERROR;
1140 return -1;
1143 return 0;
1146 static struct token *handle_hash(struct token **p, struct token *arglist)
1148 struct token *token = *p;
1149 if (arglist) {
1150 struct token *next = token->next;
1151 if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
1152 goto Equote;
1153 next->pos.whitespace = token->pos.whitespace;
1154 __free_token(token);
1155 token = *p = next;
1156 } else {
1157 token->pos.noexpand = 1;
1159 return token;
1161 Equote:
1162 sparse_error(token->pos, "'#' is not followed by a macro parameter");
1163 return NULL;
1166 /* token->next is ## */
1167 static struct token *handle_hashhash(struct token *token, struct token *arglist)
1169 struct token *last = token;
1170 struct token *concat;
1171 int state = match_op(token, ',');
1173 try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
1175 while (1) {
1176 struct token *t;
1177 int is_arg;
1179 /* eat duplicate ## */
1180 concat = token->next;
1181 while (match_op(t = concat->next, SPECIAL_HASHHASH)) {
1182 token->next = t;
1183 __free_token(concat);
1184 concat = t;
1186 token_type(concat) = TOKEN_CONCAT;
1188 if (eof_token(t))
1189 goto Econcat;
1191 if (match_op(t, '#')) {
1192 t = handle_hash(&concat->next, arglist);
1193 if (!t)
1194 return NULL;
1197 is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
1199 if (state == 1 && is_arg) {
1200 state = is_arg;
1201 } else {
1202 last = t;
1203 state = match_op(t, ',');
1206 token = t;
1207 if (!match_op(token->next, SPECIAL_HASHHASH))
1208 break;
1210 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1211 if (state == 2)
1212 token_type(last) = TOKEN_GNU_KLUDGE;
1213 return token;
1215 Econcat:
1216 sparse_error(concat->pos, "'##' cannot appear at the ends of macro expansion");
1217 return NULL;
1220 static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
1222 struct token *token = expansion;
1223 struct token **p;
1225 if (match_op(token, SPECIAL_HASHHASH))
1226 goto Econcat;
1228 for (p = &expansion; !eof_token(token); p = &token->next, token = *p) {
1229 if (match_op(token, '#')) {
1230 token = handle_hash(p, arglist);
1231 if (!token)
1232 return NULL;
1234 if (match_op(token->next, SPECIAL_HASHHASH)) {
1235 token = handle_hashhash(token, arglist);
1236 if (!token)
1237 return NULL;
1238 } else {
1239 try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
1241 if (token_type(token) == TOKEN_ERROR)
1242 goto Earg;
1244 token = alloc_token(&expansion->pos);
1245 token_type(token) = TOKEN_UNTAINT;
1246 token->ident = name;
1247 token->next = *p;
1248 *p = token;
1249 return expansion;
1251 Econcat:
1252 sparse_error(token->pos, "'##' cannot appear at the ends of macro expansion");
1253 return NULL;
1254 Earg:
1255 sparse_error(token->pos, "too many instances of argument in body");
1256 return NULL;
1259 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
1261 struct token *arglist, *expansion;
1262 struct token *left = token->next;
1263 struct symbol *sym;
1264 struct ident *name;
1265 int ret;
1267 if (token_type(left) != TOKEN_IDENT) {
1268 sparse_error(token->pos, "expected identifier to 'define'");
1269 return 1;
1272 name = left->ident;
1274 arglist = NULL;
1275 expansion = left->next;
1276 if (!expansion->pos.whitespace) {
1277 if (match_op(expansion, '(')) {
1278 arglist = expansion;
1279 expansion = parse_arguments(expansion);
1280 if (!expansion)
1281 return 1;
1282 } else if (!eof_token(expansion)) {
1283 warning(expansion->pos,
1284 "no whitespace before object-like macro body");
1288 expansion = parse_expansion(expansion, arglist, name);
1289 if (!expansion)
1290 return 1;
1292 ret = 1;
1293 sym = lookup_symbol(name, NS_MACRO | NS_UNDEF);
1294 if (sym) {
1295 int clean;
1297 if (attr < sym->attr)
1298 goto out;
1300 clean = (attr == sym->attr && sym->namespace == NS_MACRO);
1302 if (token_list_different(sym->expansion, expansion) ||
1303 token_list_different(sym->arglist, arglist)) {
1304 ret = 0;
1305 if ((clean && attr == SYM_ATTR_NORMAL)
1306 || sym->used_in == file_scope) {
1307 warning(left->pos, "preprocessor token %.*s redefined",
1308 name->len, name->name);
1309 info(sym->pos, "this was the original definition");
1311 } else if (clean)
1312 goto out;
1315 if (!sym || sym->scope != file_scope) {
1316 sym = alloc_symbol(left->pos, SYM_NODE);
1317 bind_symbol(sym, name, NS_MACRO);
1318 ret = 0;
1321 if (!ret) {
1322 sym->expansion = expansion;
1323 sym->arglist = arglist;
1324 __free_token(token); /* Free the "define" token, but not the rest of the line */
1327 sym->namespace = NS_MACRO;
1328 sym->used_in = NULL;
1329 sym->attr = attr;
1330 out:
1331 return ret;
1334 static int handle_define(struct stream *stream, struct token **line, struct token *token)
1336 return do_handle_define(stream, line, token, SYM_ATTR_NORMAL);
1339 static int handle_weak_define(struct stream *stream, struct token **line, struct token *token)
1341 return do_handle_define(stream, line, token, SYM_ATTR_WEAK);
1344 static int handle_strong_define(struct stream *stream, struct token **line, struct token *token)
1346 return do_handle_define(stream, line, token, SYM_ATTR_STRONG);
1349 static int do_handle_undef(struct stream *stream, struct token **line, struct token *token, int attr)
1351 struct token *left = token->next;
1352 struct symbol *sym;
1354 if (token_type(left) != TOKEN_IDENT) {
1355 sparse_error(token->pos, "expected identifier to 'undef'");
1356 return 1;
1359 sym = lookup_symbol(left->ident, NS_MACRO | NS_UNDEF);
1360 if (sym) {
1361 if (attr < sym->attr)
1362 return 1;
1363 if (attr == sym->attr && sym->namespace == NS_UNDEF)
1364 return 1;
1365 } else if (attr <= SYM_ATTR_NORMAL)
1366 return 1;
1368 if (!sym || sym->scope != file_scope) {
1369 sym = alloc_symbol(left->pos, SYM_NODE);
1370 bind_symbol(sym, left->ident, NS_MACRO);
1373 sym->namespace = NS_UNDEF;
1374 sym->used_in = NULL;
1375 sym->attr = attr;
1377 return 1;
1380 static int handle_undef(struct stream *stream, struct token **line, struct token *token)
1382 return do_handle_undef(stream, line, token, SYM_ATTR_NORMAL);
1385 static int handle_strong_undef(struct stream *stream, struct token **line, struct token *token)
1387 return do_handle_undef(stream, line, token, SYM_ATTR_STRONG);
1390 static int preprocessor_if(struct stream *stream, struct token *token, int true)
1392 token_type(token) = false_nesting ? TOKEN_SKIP_GROUPS : TOKEN_IF;
1393 free_preprocessor_line(token->next);
1394 token->next = stream->top_if;
1395 stream->top_if = token;
1396 if (false_nesting || true != 1)
1397 false_nesting++;
1398 return 0;
1401 static int handle_ifdef(struct stream *stream, struct token **line, struct token *token)
1403 struct token *next = token->next;
1404 int arg;
1405 if (token_type(next) == TOKEN_IDENT) {
1406 arg = token_defined(next);
1407 } else {
1408 dirty_stream(stream);
1409 if (!false_nesting)
1410 sparse_error(token->pos, "expected preprocessor identifier");
1411 arg = -1;
1413 return preprocessor_if(stream, token, arg);
1416 static int handle_ifndef(struct stream *stream, struct token **line, struct token *token)
1418 struct token *next = token->next;
1419 int arg;
1420 if (token_type(next) == TOKEN_IDENT) {
1421 if (!stream->dirty && !stream->ifndef) {
1422 if (!stream->protect) {
1423 stream->ifndef = token;
1424 stream->protect = next->ident;
1425 } else if (stream->protect == next->ident) {
1426 stream->ifndef = token;
1427 stream->dirty = 1;
1430 arg = !token_defined(next);
1431 } else {
1432 dirty_stream(stream);
1433 if (!false_nesting)
1434 sparse_error(token->pos, "expected preprocessor identifier");
1435 arg = -1;
1438 return preprocessor_if(stream, token, arg);
1441 static const char *show_token_sequence(struct token *token, int quote);
1444 * Expression handling for #if and #elif; it differs from normal expansion
1445 * due to special treatment of "defined".
1447 static int expression_value(struct token **where)
1449 struct expression *expr;
1450 struct token *p;
1451 struct token **list = where, **beginning = NULL;
1452 long long value;
1453 int state = 0;
1455 while (!eof_token(p = scan_next(list))) {
1456 switch (state) {
1457 case 0:
1458 if (token_type(p) != TOKEN_IDENT)
1459 break;
1460 if (p->ident == &defined_ident) {
1461 state = 1;
1462 beginning = list;
1463 break;
1465 if (!expand_one_symbol(list))
1466 continue;
1467 if (token_type(p) != TOKEN_IDENT)
1468 break;
1469 token_type(p) = TOKEN_ZERO_IDENT;
1470 break;
1471 case 1:
1472 if (match_op(p, '(')) {
1473 state = 2;
1474 } else {
1475 state = 0;
1476 replace_with_defined(p);
1477 *beginning = p;
1479 break;
1480 case 2:
1481 if (token_type(p) == TOKEN_IDENT)
1482 state = 3;
1483 else
1484 state = 0;
1485 replace_with_defined(p);
1486 *beginning = p;
1487 break;
1488 case 3:
1489 state = 0;
1490 if (!match_op(p, ')'))
1491 sparse_error(p->pos, "missing ')' after \"defined\"");
1492 *list = p->next;
1493 continue;
1495 list = &p->next;
1498 p = constant_expression(*where, &expr);
1499 if (!eof_token(p))
1500 sparse_error(p->pos, "garbage at end: %s", show_token_sequence(p, 0));
1501 value = get_expression_value(expr);
1502 return value != 0;
1505 static int handle_if(struct stream *stream, struct token **line, struct token *token)
1507 int value = 0;
1508 if (!false_nesting)
1509 value = expression_value(&token->next);
1511 dirty_stream(stream);
1512 return preprocessor_if(stream, token, value);
1515 static int handle_elif(struct stream * stream, struct token **line, struct token *token)
1517 struct token *top_if = stream->top_if;
1518 end_group(stream);
1520 if (!top_if) {
1521 nesting_error(stream);
1522 sparse_error(token->pos, "unmatched #elif within stream");
1523 return 1;
1526 if (token_type(top_if) == TOKEN_ELSE) {
1527 nesting_error(stream);
1528 sparse_error(token->pos, "#elif after #else");
1529 if (!false_nesting)
1530 false_nesting = 1;
1531 return 1;
1534 dirty_stream(stream);
1535 if (token_type(top_if) != TOKEN_IF)
1536 return 1;
1537 if (false_nesting) {
1538 false_nesting = 0;
1539 if (!expression_value(&token->next))
1540 false_nesting = 1;
1541 } else {
1542 false_nesting = 1;
1543 token_type(top_if) = TOKEN_SKIP_GROUPS;
1545 return 1;
1548 static int handle_else(struct stream *stream, struct token **line, struct token *token)
1550 struct token *top_if = stream->top_if;
1551 end_group(stream);
1553 if (!top_if) {
1554 nesting_error(stream);
1555 sparse_error(token->pos, "unmatched #else within stream");
1556 return 1;
1559 if (token_type(top_if) == TOKEN_ELSE) {
1560 nesting_error(stream);
1561 sparse_error(token->pos, "#else after #else");
1563 if (false_nesting) {
1564 if (token_type(top_if) == TOKEN_IF)
1565 false_nesting = 0;
1566 } else {
1567 false_nesting = 1;
1569 token_type(top_if) = TOKEN_ELSE;
1570 return 1;
1573 static int handle_endif(struct stream *stream, struct token **line, struct token *token)
1575 struct token *top_if = stream->top_if;
1576 end_group(stream);
1577 if (!top_if) {
1578 nesting_error(stream);
1579 sparse_error(token->pos, "unmatched #endif in stream");
1580 return 1;
1582 if (false_nesting)
1583 false_nesting--;
1584 stream->top_if = top_if->next;
1585 __free_token(top_if);
1586 return 1;
1589 static int handle_warning(struct stream *stream, struct token **line, struct token *token)
1591 warning(token->pos, "%s", show_token_sequence(token->next, 0));
1592 return 1;
1595 static int handle_error(struct stream *stream, struct token **line, struct token *token)
1597 sparse_error(token->pos, "%s", show_token_sequence(token->next, 0));
1598 return 1;
1601 static int handle_nostdinc(struct stream *stream, struct token **line, struct token *token)
1604 * Do we have any non-system includes?
1605 * Clear them out if so..
1607 *sys_includepath = NULL;
1608 return 1;
1611 static inline void update_inc_ptrs(const char ***where)
1614 if (*where <= dirafter_includepath) {
1615 dirafter_includepath++;
1616 /* If this was the entry that we prepend, don't
1617 * rise the lower entries, even if they are at
1618 * the same level. */
1619 if (where == &dirafter_includepath)
1620 return;
1622 if (*where <= sys_includepath) {
1623 sys_includepath++;
1624 if (where == &sys_includepath)
1625 return;
1627 if (*where <= isys_includepath) {
1628 isys_includepath++;
1629 if (where == &isys_includepath)
1630 return;
1633 /* angle_includepath is actually never updated, since we
1634 * don't suppport -iquote rught now. May change some day. */
1635 if (*where <= angle_includepath) {
1636 angle_includepath++;
1637 if (where == &angle_includepath)
1638 return;
1642 /* Add a path before 'where' and update the pointers associated with the
1643 * includepath array */
1644 static void add_path_entry(struct token *token, const char *path,
1645 const char ***where)
1647 const char **dst;
1648 const char *next;
1650 /* Need one free entry.. */
1651 if (includepath[INCLUDEPATHS-2])
1652 error_die(token->pos, "too many include path entries");
1654 /* check that this is not a duplicate */
1655 dst = includepath;
1656 while (*dst) {
1657 if (strcmp(*dst, path) == 0)
1658 return;
1659 dst++;
1661 next = path;
1662 dst = *where;
1664 update_inc_ptrs(where);
1667 * Move them all up starting at dst,
1668 * insert the new entry..
1670 do {
1671 const char *tmp = *dst;
1672 *dst = next;
1673 next = tmp;
1674 dst++;
1675 } while (next);
1678 static int handle_add_include(struct stream *stream, struct token **line, struct token *token)
1680 for (;;) {
1681 token = token->next;
1682 if (eof_token(token))
1683 return 1;
1684 if (token_type(token) != TOKEN_STRING) {
1685 warning(token->pos, "expected path string");
1686 return 1;
1688 add_path_entry(token, token->string->data, &isys_includepath);
1692 static int handle_add_isystem(struct stream *stream, struct token **line, struct token *token)
1694 for (;;) {
1695 token = token->next;
1696 if (eof_token(token))
1697 return 1;
1698 if (token_type(token) != TOKEN_STRING) {
1699 sparse_error(token->pos, "expected path string");
1700 return 1;
1702 add_path_entry(token, token->string->data, &sys_includepath);
1706 static int handle_add_system(struct stream *stream, struct token **line, struct token *token)
1708 for (;;) {
1709 token = token->next;
1710 if (eof_token(token))
1711 return 1;
1712 if (token_type(token) != TOKEN_STRING) {
1713 sparse_error(token->pos, "expected path string");
1714 return 1;
1716 add_path_entry(token, token->string->data, &dirafter_includepath);
1720 /* Add to end on includepath list - no pointer updates */
1721 static void add_dirafter_entry(struct token *token, const char *path)
1723 const char **dst = includepath;
1725 /* Need one free entry.. */
1726 if (includepath[INCLUDEPATHS-2])
1727 error_die(token->pos, "too many include path entries");
1729 /* Add to the end */
1730 while (*dst)
1731 dst++;
1732 *dst = path;
1733 dst++;
1734 *dst = NULL;
1737 static int handle_add_dirafter(struct stream *stream, struct token **line, struct token *token)
1739 for (;;) {
1740 token = token->next;
1741 if (eof_token(token))
1742 return 1;
1743 if (token_type(token) != TOKEN_STRING) {
1744 sparse_error(token->pos, "expected path string");
1745 return 1;
1747 add_dirafter_entry(token, token->string->data);
1751 static int handle_split_include(struct stream *stream, struct token **line, struct token *token)
1754 * -I-
1755 * From info gcc:
1756 * Split the include path. Any directories specified with `-I'
1757 * options before `-I-' are searched only for headers requested with
1758 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1759 * If additional directories are specified with `-I' options after
1760 * the `-I-', those directories are searched for all `#include'
1761 * directives.
1762 * In addition, `-I-' inhibits the use of the directory of the current
1763 * file directory as the first search directory for `#include "FILE"'.
1765 quote_includepath = includepath+1;
1766 angle_includepath = sys_includepath;
1767 return 1;
1771 * We replace "#pragma xxx" with "__pragma__" in the token
1772 * stream. Just as an example.
1774 * We'll just #define that away for now, but the theory here
1775 * is that we can use this to insert arbitrary token sequences
1776 * to turn the pragmas into internal front-end sequences for
1777 * when we actually start caring about them.
1779 * So eventually this will turn into some kind of extended
1780 * __attribute__() like thing, except called __pragma__(xxx).
1782 static int handle_pragma(struct stream *stream, struct token **line, struct token *token)
1784 struct token *next = *line;
1786 token->ident = &pragma_ident;
1787 token->pos.newline = 1;
1788 token->pos.whitespace = 1;
1789 token->pos.pos = 1;
1790 *line = token;
1791 token->next = next;
1792 return 0;
1796 * We ignore #line for now.
1798 static int handle_line(struct stream *stream, struct token **line, struct token *token)
1800 return 1;
1803 static int handle_nondirective(struct stream *stream, struct token **line, struct token *token)
1805 sparse_error(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token, 0));
1806 return 1;
1810 static void init_preprocessor(void)
1812 int i;
1813 int stream = init_stream("preprocessor", -1, includepath);
1814 static struct {
1815 const char *name;
1816 int (*handler)(struct stream *, struct token **, struct token *);
1817 } normal[] = {
1818 { "define", handle_define },
1819 { "weak_define", handle_weak_define },
1820 { "strong_define", handle_strong_define },
1821 { "undef", handle_undef },
1822 { "strong_undef", handle_strong_undef },
1823 { "warning", handle_warning },
1824 { "error", handle_error },
1825 { "include", handle_include },
1826 { "include_next", handle_include_next },
1827 { "pragma", handle_pragma },
1828 { "line", handle_line },
1830 // our internal preprocessor tokens
1831 { "nostdinc", handle_nostdinc },
1832 { "add_include", handle_add_include },
1833 { "add_isystem", handle_add_isystem },
1834 { "add_system", handle_add_system },
1835 { "add_dirafter", handle_add_dirafter },
1836 { "split_include", handle_split_include },
1837 { "argv_include", handle_argv_include },
1838 }, special[] = {
1839 { "ifdef", handle_ifdef },
1840 { "ifndef", handle_ifndef },
1841 { "else", handle_else },
1842 { "endif", handle_endif },
1843 { "if", handle_if },
1844 { "elif", handle_elif },
1847 for (i = 0; i < ARRAY_SIZE(normal); i++) {
1848 struct symbol *sym;
1849 sym = create_symbol(stream, normal[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1850 sym->handler = normal[i].handler;
1851 sym->normal = 1;
1853 for (i = 0; i < ARRAY_SIZE(special); i++) {
1854 struct symbol *sym;
1855 sym = create_symbol(stream, special[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
1856 sym->handler = special[i].handler;
1857 sym->normal = 0;
1862 static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
1864 int (*handler)(struct stream *, struct token **, struct token *);
1865 struct token *token = start->next;
1866 int is_normal = 1;
1868 if (eof_token(token))
1869 return;
1871 if (token_type(token) == TOKEN_IDENT) {
1872 struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
1873 if (sym) {
1874 handler = sym->handler;
1875 is_normal = sym->normal;
1876 } else {
1877 handler = handle_nondirective;
1879 } else if (token_type(token) == TOKEN_NUMBER) {
1880 handler = handle_line;
1881 } else {
1882 handler = handle_nondirective;
1885 if (is_normal) {
1886 dirty_stream(stream);
1887 if (false_nesting)
1888 goto out;
1890 if (!handler(stream, line, token)) /* all set */
1891 return;
1893 out:
1894 free_preprocessor_line(token);
1897 static void preprocessor_line(struct stream *stream, struct token **line)
1899 struct token *start = *line, *next;
1900 struct token **tp = &start->next;
1902 for (;;) {
1903 next = *tp;
1904 if (next->pos.newline)
1905 break;
1906 tp = &next->next;
1908 *line = next;
1909 *tp = &eof_token_entry;
1910 handle_preprocessor_line(stream, line, start);
1913 static void do_preprocess(struct token **list)
1915 struct token *next;
1917 while (!eof_token(next = scan_next(list))) {
1918 struct stream *stream = input_streams + next->pos.stream;
1920 if (next->pos.newline && match_op(next, '#')) {
1921 if (!next->pos.noexpand) {
1922 preprocessor_line(stream, list);
1923 __free_token(next); /* Free the '#' token */
1924 continue;
1928 switch (token_type(next)) {
1929 case TOKEN_STREAMEND:
1930 if (stream->top_if) {
1931 nesting_error(stream);
1932 sparse_error(stream->top_if->pos, "unterminated preprocessor conditional");
1933 stream->top_if = NULL;
1934 false_nesting = 0;
1936 if (!stream->dirty)
1937 stream->constant = CONSTANT_FILE_YES;
1938 *list = next->next;
1939 continue;
1940 case TOKEN_STREAMBEGIN:
1941 *list = next->next;
1942 continue;
1944 default:
1945 dirty_stream(stream);
1946 if (false_nesting) {
1947 *list = next->next;
1948 __free_token(next);
1949 continue;
1952 if (token_type(next) != TOKEN_IDENT ||
1953 expand_one_symbol(list))
1954 list = &next->next;
1959 struct token * preprocess(struct token *token)
1961 preprocessing = 1;
1962 init_preprocessor();
1963 do_preprocess(&token);
1965 // Drop all expressions from preprocessing, they're not used any more.
1966 // This is not true when we have multiple files, though ;/
1967 // clear_expression_alloc();
1968 preprocessing = 0;
1970 return token;