2 * Do C preprocessing, based on a token list gathered by
5 * This may not be the smartest preprocessor on the planet.
7 * Copyright (C) 2003 Transmeta Corp.
10 * Licensed under the Open Software License version 1.1
22 #include "pre-process.h"
27 #include "expression.h"
30 int preprocessing
= 0;
32 static int true_nesting
= 0;
33 static int false_nesting
= 0;
34 static struct token
*unmatched_if
= NULL
;
35 #define if_nesting (true_nesting + false_nesting)
37 #define MAX_NEST (256)
38 static unsigned char elif_ignore
[MAX_NEST
];
39 enum { ELIF_IGNORE
= 1, ELIF_SEEN_ELSE
= 2 };
41 #define INCLUDEPATHS 300
42 const char *includepath
[INCLUDEPATHS
+1] = {
49 static const char **sys_includepath
= includepath
+ 0;
50 static const char **gcc_includepath
= includepath
+ 2;
52 #define MARK_STREAM_NONCONST(pos) do { \
53 if (stream->constant != CONSTANT_FILE_NOPE) { \
55 info(pos, "%s triggers non-const", __func__); \
56 stream->constant = CONSTANT_FILE_NOPE; \
63 * This is stupid - the tokenizer already guarantees unique
64 * identifiers, so we should just compare identifier pointers
66 int match_string_ident(struct ident
*ident
, const char *str
)
68 return !strncmp(str
, ident
->name
, ident
->len
) && !str
[ident
->len
];
71 static struct token
*alloc_token(struct position
*pos
)
73 struct token
*token
= __alloc_token(0);
75 token
->pos
.stream
= pos
->stream
;
76 token
->pos
.line
= pos
->line
;
77 token
->pos
.pos
= pos
->pos
;
78 token
->pos
.whitespace
= 1;
82 static const char *show_token_sequence(struct token
*token
);
84 /* Expand symbol 'sym' at '*list' */
85 static struct token
**expand(struct token
**, struct symbol
*);
87 static void replace_with_string(struct token
*token
, const char *str
)
89 int size
= strlen(str
) + 1;
90 struct string
*s
= __alloc_string(size
);
93 memcpy(s
->data
, str
, size
);
94 token_type(token
) = TOKEN_STRING
;
98 static void replace_with_integer(struct token
*token
, unsigned int val
)
100 char *buf
= __alloc_bytes(10);
101 sprintf(buf
, "%d", val
);
102 token_type(token
) = TOKEN_NUMBER
;
106 static void replace_with_defined(struct token
*token
)
108 char *string
[] = { "0", "1" };
110 if (token_type(token
) != TOKEN_IDENT
)
111 warn(token
->pos
, "operator \"defined\" requires an identifier");
112 else if (lookup_symbol(token
->ident
, NS_PREPROCESSOR
))
114 token_type(token
) = TOKEN_NUMBER
;
115 token
->number
= string
[defined
];
118 struct token
**expand_one_symbol(struct token
**list
)
120 struct token
*token
= *list
;
123 if (token
->pos
.noexpand
)
126 sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
128 return expand(list
, sym
);
129 if (token
->ident
== &__LINE___ident
) {
130 replace_with_integer(token
, token
->pos
.line
);
131 } else if (token
->ident
== &__FILE___ident
) {
132 replace_with_string(token
, (input_streams
+ token
->pos
.stream
)->name
);
137 static inline struct token
*scan_next(struct token
**where
)
139 struct token
*token
= *where
;
140 if (token_type(token
) != TOKEN_UNTAINT
)
143 token
->ident
->tainted
= 0;
145 } while (token_type(token
) == TOKEN_UNTAINT
);
150 static struct token
**expand_list(struct token
**list
)
153 while (!eof_token(next
= scan_next(list
))) {
154 if (token_type(next
) == TOKEN_IDENT
)
155 list
= expand_one_symbol(list
);
162 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
164 struct token
**p
= &prev
->next
;
168 while (!eof_token(next
= scan_next(p
))) {
169 if (match_op(next
, '(')) {
171 } else if (match_op(next
, ')')) {
174 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
177 next
->pos
.stream
= pos
->stream
;
178 next
->pos
.line
= pos
->line
;
179 next
->pos
.pos
= pos
->pos
;
182 *p
= &eof_token_entry
;
187 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
192 struct token
*expanded
;
199 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
201 int wanted
= arglist
->count
.normal
;
202 struct token
*next
= NULL
;
205 arglist
= arglist
->next
; /* skip counter */
208 next
= collect_arg(start
, 0, &what
->pos
);
211 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
216 for (count
= 0; count
< wanted
; count
++) {
217 struct argcount
*p
= &arglist
->next
->count
;
218 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
219 arglist
= arglist
->next
->next
;
222 args
[count
].arg
= start
->next
;
223 args
[count
].n_normal
= p
->normal
;
224 args
[count
].n_quoted
= p
->quoted
;
225 args
[count
].n_str
= p
->str
;
226 if (match_op(next
, ')')) {
232 if (count
== wanted
&& !match_op(next
, ')'))
234 if (count
== wanted
- 1) {
235 struct argcount
*p
= &arglist
->next
->count
;
238 args
[count
].arg
= NULL
;
239 args
[count
].n_normal
= p
->normal
;
240 args
[count
].n_quoted
= p
->quoted
;
241 args
[count
].n_str
= p
->str
;
243 if (count
< wanted
- 1)
246 what
->next
= next
->next
;
250 warn(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
251 show_token(what
), wanted
, count
);
254 while (match_op(next
, ',')) {
255 next
= collect_arg(next
, 0, &what
->pos
);
260 warn(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
261 show_token(what
), count
, wanted
);
264 warn(what
->pos
, "unterminated argument list invoking macro \"%s\"",
267 what
->next
= next
->next
;
271 static struct token
*dup_list(struct token
*list
)
274 struct token
**p
= &res
;
276 while (!eof_token(list
)) {
277 struct token
*newtok
= __alloc_token(0);
286 static struct token
*stringify(struct token
*arg
)
288 const char *s
= show_token_sequence(arg
);
289 int size
= strlen(s
)+1;
290 struct token
*token
= __alloc_token(0);
291 struct string
*string
= __alloc_string(size
);
293 memcpy(string
->data
, s
, size
);
294 string
->length
= size
;
295 token
->pos
= arg
->pos
;
296 token_type(token
) = TOKEN_STRING
;
297 token
->string
= string
;
298 token
->next
= &eof_token_entry
;
302 static void expand_arguments(int count
, struct arg
*args
)
305 for (i
= 0; i
< count
; i
++) {
306 struct token
*arg
= args
[i
].arg
;
308 arg
= &eof_token_entry
;
310 args
[i
].str
= stringify(arg
);
311 if (args
[i
].n_normal
) {
312 if (!args
[i
].n_quoted
) {
313 args
[i
].expanded
= arg
;
315 } else if (eof_token(arg
)) {
316 args
[i
].expanded
= arg
;
318 args
[i
].expanded
= dup_list(arg
);
320 expand_list(&args
[i
].expanded
);
326 * Possibly valid combinations:
327 * - ident + ident -> ident
328 * - ident + number -> ident unless number contains '.', '+' or '-'.
329 * - number + number -> number
330 * - number + ident -> number
331 * - number + '.' -> number
332 * - number + '+' or '-' -> number, if number used to end on [eEpP].
333 * - '.' + number -> number, if number used to start with a digit.
334 * - special + special -> either special or an error.
336 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
339 enum token_type t1
= token_type(left
), t2
= token_type(right
);
341 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
344 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
347 strcpy(p
, show_token(left
));
348 strcat(p
, show_token(right
));
354 if (t1
== TOKEN_IDENT
) {
355 if (t2
== TOKEN_SPECIAL
)
357 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
362 if (t1
== TOKEN_NUMBER
) {
363 if (t2
== TOKEN_SPECIAL
) {
364 switch (right
->special
) {
368 if (strchr("eEpP", p
[len
- 2]))
377 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
380 return TOKEN_SPECIAL
;
383 static int merge(struct token
*left
, struct token
*right
)
385 extern unsigned char combinations
[][3];
386 static char buffer
[512];
389 switch (combine(left
, right
, buffer
)) {
391 left
->ident
= built_in_ident(buffer
);
392 left
->pos
.noexpand
= 0;
396 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
397 left
->number
= __alloc_bytes(strlen(buffer
) + 1);
398 memcpy(left
->number
, buffer
, strlen(buffer
) + 1);
402 if (buffer
[2] && buffer
[3])
404 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
405 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
413 warn(left
->pos
, "'##' failed: concatenation is not a valid token");
417 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
419 struct token
*alloc
= alloc_token(streampos
);
420 token_type(alloc
) = token_type(token
);
421 alloc
->pos
.newline
= pos
->newline
;
422 alloc
->pos
.whitespace
= pos
->whitespace
;
423 alloc
->number
= token
->number
;
424 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
428 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
430 int need_copy
= --*count
;
431 while (!eof_token(list
)) {
434 token
= dup_token(list
, &list
->pos
, &list
->pos
);
437 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
438 token
->pos
.noexpand
= 1;
440 where
= &token
->next
;
443 *where
= &eof_token_entry
;
447 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
449 struct token
*token
= *list
;
450 struct position
*base_pos
= &token
->pos
;
451 struct position
*pos
= base_pos
;
453 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
455 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
456 struct token
*added
, *arg
;
459 switch (token_type(body
)) {
460 case TOKEN_GNU_KLUDGE
:
462 * GNU kludge: if we had <comma>##<vararg>, behaviour
463 * depends on whether we had enough arguments to have
464 * a vararg. If we did, ## is just ignored. Otherwise
465 * both , and ## are ignored. Comma should come from
466 * the body of macro and not be an argument of earlier
469 if (!args
[body
->next
->argnum
].arg
)
471 added
= dup_token(body
, base_pos
, pos
);
472 token_type(added
) = TOKEN_SPECIAL
;
476 case TOKEN_STR_ARGUMENT
:
477 arg
= args
[body
->argnum
].str
;
478 count
= &args
[body
->argnum
].n_str
;
481 case TOKEN_QUOTED_ARGUMENT
:
482 arg
= args
[body
->argnum
].arg
;
483 count
= &args
[body
->argnum
].n_quoted
;
484 if (!arg
|| eof_token(arg
)) {
493 case TOKEN_MACRO_ARGUMENT
:
494 arg
= args
[body
->argnum
].expanded
;
495 count
= &args
[body
->argnum
].n_normal
;
496 if (eof_token(arg
)) {
501 tail
= copy(&added
, arg
, count
);
502 added
->pos
.newline
= pos
->newline
;
503 added
->pos
.whitespace
= pos
->whitespace
;
507 if (state
== Placeholder
)
514 added
= dup_token(body
, base_pos
, pos
);
515 if (added
->ident
->tainted
)
516 added
->pos
.noexpand
= 1;
521 added
= dup_token(body
, base_pos
, pos
);
527 * if we got to doing real concatenation, we already have
528 * added something into the list, so containing_token() is OK.
530 if (state
== Concat
&& merge(containing_token(list
), added
)) {
532 if (tail
!= &added
->next
)
540 *list
= &eof_token_entry
;
544 static struct token
**expand(struct token
**list
, struct symbol
*sym
)
547 struct token
*token
= *list
;
548 struct ident
*expanding
= token
->ident
;
550 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
551 struct arg args
[nargs
];
553 if (expanding
->tainted
) {
554 token
->pos
.noexpand
= 1;
559 if (!match_op(scan_next(&token
->next
), '('))
561 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
563 expand_arguments(nargs
, args
);
566 expanding
->tainted
= 1;
569 tail
= substitute(list
, sym
->expansion
, args
);
575 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
578 static char buffer
[256];
582 while (!eof_token(token
) && !match_op(token
, endop
)) {
584 const char *val
= token
->string
->data
;
585 if (token_type(token
) != TOKEN_STRING
)
586 val
= show_token(token
);
588 memcpy(ptr
, val
, len
);
593 if (endop
&& !match_op(token
, endop
))
594 warn(start
->pos
, "expected '>' at end of filename");
598 static int try_include(const char *path
, int plen
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
601 static char fullname
[PATH_MAX
];
603 memcpy(fullname
, path
, plen
);
604 if (plen
&& path
[plen
-1] != '/') {
605 fullname
[plen
] = '/';
608 memcpy(fullname
+plen
, filename
, flen
);
609 fd
= open(fullname
, O_RDONLY
);
611 char * streamname
= __alloc_bytes(plen
+ flen
);
612 memcpy(streamname
, fullname
, plen
+ flen
);
613 *where
= tokenize(streamname
, fd
, *where
, next_path
);
620 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
624 while ((path
= *pptr
++) != NULL
) {
625 if (!try_include(path
, strlen(path
), filename
, flen
, list
, pptr
))
633 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
635 int flen
= strlen(filename
) + 1;
638 if (filename
[0] == '/') {
639 if (try_include("", 0, filename
, flen
, list
, includepath
))
644 /* Same directory as current stream? */
651 slash
= strrchr(path
, '/');
652 plen
= slash
? slash
- path
: 0;
654 if (try_include(path
, plen
, filename
, flen
, list
, includepath
))
658 /* Check the standard include paths.. */
659 if (do_include_path(path
, list
, token
, filename
, flen
))
662 error(token
->pos
, "unable to open '%s'", filename
);
665 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
667 const char *filename
;
674 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
675 MARK_STREAM_NONCONST(token
->pos
);
679 if (!match_op(next
, '<')) {
680 expand_list(&token
->next
);
683 if (match_op(token
->next
, '<')) {
689 filename
= token_name_sequence(token
, expect
, token
);
690 do_include(!expect
, stream
, list
, token
, filename
, path
);
694 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
696 return handle_include_path(stream
, list
, token
, includepath
);
699 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
701 return handle_include_path(stream
, list
, token
, stream
->next_path
);
704 static int token_different(struct token
*t1
, struct token
*t2
)
708 if (token_type(t1
) != token_type(t2
))
711 switch (token_type(t1
)) {
713 different
= t1
->ident
!= t2
->ident
;
715 case TOKEN_ARG_COUNT
:
718 case TOKEN_GNU_KLUDGE
:
722 different
= strcmp(t1
->number
, t2
->number
);
725 different
= t1
->special
!= t2
->special
;
727 case TOKEN_MACRO_ARGUMENT
:
728 case TOKEN_QUOTED_ARGUMENT
:
729 case TOKEN_STR_ARGUMENT
:
730 different
= t1
->argnum
!= t2
->argnum
;
733 different
= t1
->character
!= t2
->character
;
736 struct string
*s1
, *s2
;
741 if (s1
->length
!= s2
->length
)
743 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
753 static int token_list_different(struct token
*list1
, struct token
*list2
)
758 if (!list1
|| !list2
)
760 if (token_different(list1
, list2
))
767 static inline void set_arg_count(struct token
*token
)
769 token_type(token
) = TOKEN_ARG_COUNT
;
770 token
->count
.normal
= token
->count
.quoted
=
771 token
->count
.str
= token
->count
.vararg
= 0;
774 static struct token
*parse_arguments(struct token
*list
)
776 struct token
*arg
= list
->next
, *next
= list
;
777 struct argcount
*count
= &list
->count
;
781 if (match_op(arg
, ')')) {
783 list
->next
= &eof_token_entry
;
787 while (token_type(arg
) == TOKEN_IDENT
) {
788 if (arg
->ident
== &__VA_ARGS___ident
)
790 if (!++count
->normal
)
794 if (match_op(next
, ',')) {
800 if (match_op(next
, ')')) {
803 arg
->next
->next
= &eof_token_entry
;
807 /* normal cases are finished here */
809 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
810 if (match_op(next
->next
, ')')) {
812 next
->count
.vararg
= 1;
814 arg
->next
->next
= &eof_token_entry
;
822 if (eof_token(next
)) {
830 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
832 token_type(arg
) = TOKEN_IDENT
;
833 arg
->ident
= &__VA_ARGS___ident
;
834 if (!match_op(next
, ')'))
836 if (!++count
->normal
)
839 next
->count
.vararg
= 1;
841 arg
->next
->next
= &eof_token_entry
;
845 if (eof_token(arg
)) {
849 if (match_op(arg
, ','))
856 warn(arg
->pos
, "parameter name missing");
859 warn(arg
->pos
, "\"%s\" may not appear in macro parameter list",
863 warn(arg
->pos
, "missing ')' in macro parameter list");
866 warn(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
869 warn(arg
->pos
, "too many arguments in macro definition");
873 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
875 struct ident
*ident
= token
->ident
;
878 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
881 arglist
= arglist
->next
;
883 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
884 if (arglist
->ident
== ident
) {
885 struct argcount
*count
= &arglist
->next
->count
;
889 token_type(token
) = type
;
891 case TOKEN_MACRO_ARGUMENT
:
894 case TOKEN_QUOTED_ARGUMENT
:
901 return count
->vararg
? 2 : 1;
902 token_type(token
) = TOKEN_ERROR
;
909 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
911 struct token
*token
= expansion
;
913 struct token
*last
= NULL
;
915 if (match_op(token
, SPECIAL_HASHHASH
))
918 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
919 if (match_op(token
, '#')) {
921 struct token
*next
= token
->next
;
922 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
924 next
->pos
.whitespace
= token
->pos
.whitespace
;
927 token
->pos
.noexpand
= 1;
929 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
930 struct token
*next
= token
->next
;
931 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
932 token_type(token
) = TOKEN_CONCAT
;
936 if (arg
== 2 && last
&& match_op(last
, ',')) {
937 token_type(last
) = TOKEN_GNU_KLUDGE
;
940 } else if (match_op(next
, SPECIAL_HASHHASH
))
942 else if (match_op(next
, ','))
944 else if (eof_token(next
))
946 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
947 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
949 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
951 if (token_type(token
) == TOKEN_ERROR
)
955 token
= alloc_token(&expansion
->pos
);
956 token_type(token
) = TOKEN_UNTAINT
;
963 warn(token
->pos
, "'#' is not followed by a macro parameter");
967 warn(token
->pos
, "'##' cannot appear at the ends of macro expansion");
970 warn(token
->pos
, "too many instances of argument in body");
974 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
976 struct token
*arglist
, *expansion
;
977 struct token
*left
= token
->next
;
981 if (token_type(left
) != TOKEN_IDENT
) {
982 warn(token
->pos
, "expected identifier to 'define'");
988 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
989 MARK_STREAM_NONCONST(token
->pos
);
994 expansion
= left
->next
;
995 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
997 expansion
= parse_arguments(expansion
);
1002 expansion
= parse_expansion(expansion
, arglist
, name
);
1006 sym
= lookup_symbol(name
, NS_PREPROCESSOR
);
1008 if (token_list_different(sym
->expansion
, expansion
) ||
1009 token_list_different(sym
->arglist
, arglist
)) {
1010 warn(left
->pos
, "preprocessor token %.*s redefined",
1011 name
->len
, name
->name
);
1012 info(sym
->pos
, "this was the original definition");
1016 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1017 bind_symbol(sym
, name
, NS_PREPROCESSOR
);
1019 sym
->expansion
= expansion
;
1020 sym
->arglist
= arglist
;
1024 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1026 struct token
*left
= token
->next
;
1027 struct symbol
**sym
;
1029 if (token_type(left
) != TOKEN_IDENT
) {
1030 warn(token
->pos
, "expected identifier to 'undef'");
1036 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1037 MARK_STREAM_NONCONST(token
->pos
);
1039 sym
= &left
->ident
->symbols
;
1041 struct symbol
*t
= *sym
;
1042 if (t
->namespace == NS_PREPROCESSOR
) {
1051 static int preprocessor_if(struct token
*token
, int true)
1053 if (if_nesting
== 0)
1054 unmatched_if
= token
;
1055 if (if_nesting
>= MAX_NEST
)
1056 error(token
->pos
, "Maximum preprocessor conditional level exhausted");
1057 elif_ignore
[if_nesting
] = (false_nesting
|| true) ? ELIF_IGNORE
: 0;
1058 if (false_nesting
|| !true) {
1066 static int token_defined(struct token
*token
)
1068 if (token_type(token
) == TOKEN_IDENT
)
1069 return lookup_symbol(token
->ident
, NS_PREPROCESSOR
) != NULL
;
1071 warn(token
->pos
, "expected identifier for #if[n]def");
1075 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1077 return preprocessor_if(token
, token_defined(token
->next
));
1080 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1082 struct token
*next
= token
->next
;
1083 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1084 if (token_type(next
) == TOKEN_IDENT
&&
1085 (!stream
->protect
|| stream
->protect
== next
->ident
)) {
1086 stream
->constant
= CONSTANT_FILE_IFNDEF
;
1087 stream
->protect
= next
->ident
;
1089 MARK_STREAM_NONCONST(token
->pos
);
1091 return preprocessor_if(token
, !token_defined(next
));
1095 * Expression handling for #if and #elif; it differs from normal expansion
1096 * due to special treatment of "defined".
1098 static int expression_value(struct token
**where
)
1100 struct expression
*expr
;
1102 struct token
**list
= where
, **beginning
= NULL
;
1106 while (!eof_token(p
= scan_next(list
))) {
1109 if (token_type(p
) == TOKEN_IDENT
) {
1110 if (p
->ident
!= &defined_ident
) {
1111 list
= expand_one_symbol(list
);
1119 if (match_op(p
, '(')) {
1123 replace_with_defined(p
);
1128 if (token_type(p
) == TOKEN_IDENT
)
1132 replace_with_defined(p
);
1137 if (!match_op(p
, ')'))
1138 warn(p
->pos
, "missing ')' after \"defined\"");
1145 p
= constant_expression(*where
, &expr
);
1147 warn(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1148 value
= get_expression_value(expr
);
1152 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1156 value
= expression_value(&token
->next
);
1158 // This is an approximation. We really only need this if the
1159 // condition does depends on a pre-processor symbol. Note, that
1160 // the important #ifndef case has already changed ->constant.
1161 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1162 MARK_STREAM_NONCONST(token
->pos
);
1164 return preprocessor_if(token
, value
);
1167 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1169 if (stream
->nesting
== if_nesting
)
1170 MARK_STREAM_NONCONST(token
->pos
);
1172 if (stream
->nesting
> if_nesting
) {
1173 warn(token
->pos
, "unmatched #elif");
1177 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1178 warn(token
->pos
, "#elif after #else");
1180 if (false_nesting
) {
1181 /* If this whole if-thing is if'ed out, an elif cannot help */
1182 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1184 if (expression_value(&token
->next
)) {
1187 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1196 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1198 if (stream
->nesting
== if_nesting
)
1199 MARK_STREAM_NONCONST(token
->pos
);
1201 if (stream
->nesting
> if_nesting
) {
1202 warn(token
->pos
, "unmatched #else");
1206 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1207 warn(token
->pos
, "#else after #else");
1209 elif_ignore
[if_nesting
-1] |= ELIF_SEEN_ELSE
;
1211 if (false_nesting
) {
1212 /* If this whole if-thing is if'ed out, an else cannot help */
1213 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1217 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1225 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1227 if (stream
->constant
== CONSTANT_FILE_IFNDEF
&& stream
->nesting
== if_nesting
)
1228 stream
->constant
= CONSTANT_FILE_MAYBE
;
1230 if (stream
->nesting
> if_nesting
)
1231 warn(token
->pos
, "unmatched #endif");
1232 else if (false_nesting
)
1239 static const char *show_token_sequence(struct token
*token
)
1241 static char buffer
[1024];
1247 while (!eof_token(token
)) {
1248 const char *val
= show_token(token
);
1249 int len
= strlen(val
);
1251 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1252 warn(token
->pos
, "too long token expansion");
1258 memcpy(ptr
, val
, len
);
1260 token
= token
->next
;
1261 whitespace
= token
->pos
.whitespace
;
1267 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1271 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1272 MARK_STREAM_NONCONST(token
->pos
);
1273 warn(token
->pos
, "%s", show_token_sequence(token
->next
));
1277 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1281 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1282 MARK_STREAM_NONCONST(token
->pos
);
1283 warn(token
->pos
, "%s", show_token_sequence(token
->next
));
1287 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1295 * Do we have any non-system includes?
1296 * Clear them out if so..
1298 stdinc
= gcc_includepath
- sys_includepath
;
1300 const char **src
= gcc_includepath
;
1301 const char **dst
= sys_includepath
;
1308 gcc_includepath
-= stdinc
;
1313 static void add_path_entry(struct token
*token
, const char *path
)
1318 /* Need one free entry.. */
1319 if (includepath
[INCLUDEPATHS
-2])
1320 error(token
->pos
, "too many include path entries");
1323 dst
= sys_includepath
;
1328 * Move them all up starting at "sys_includepath",
1329 * insert the new entry..
1332 const char *tmp
= *dst
;
1341 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1344 token
= token
->next
;
1345 if (eof_token(token
))
1347 if (token_type(token
) != TOKEN_STRING
) {
1348 warn(token
->pos
, "expected path string");
1351 add_path_entry(token
, token
->string
->data
);
1356 * We replace "#pragma xxx" with "__pragma__" in the token
1357 * stream. Just as an example.
1359 * We'll just #define that away for now, but the theory here
1360 * is that we can use this to insert arbitrary token sequences
1361 * to turn the pragma's into internal front-end sequences for
1362 * when we actually start caring about them.
1364 * So eventually this will turn into some kind of extended
1365 * __attribute__() like thing, except called __pragma__(xxx).
1367 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1369 struct token
*next
= *line
;
1371 token
->ident
= &pragma_ident
;
1372 token
->pos
.newline
= 1;
1373 token
->pos
.whitespace
= 1;
1381 * We ignore #line for now.
1383 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1388 static int handle_preprocessor_command(struct stream
*stream
, struct token
**line
, struct ident
*ident
, struct token
*token
)
1393 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1395 { "define", handle_define
},
1396 { "undef", handle_undef
},
1397 { "ifdef", handle_ifdef
},
1398 { "ifndef", handle_ifndef
},
1399 { "else", handle_else
},
1400 { "endif", handle_endif
},
1401 { "if", handle_if
},
1402 { "elif", handle_elif
},
1403 { "warning", handle_warning
},
1404 { "error", handle_error
},
1405 { "include", handle_include
},
1406 { "include_next",handle_include_next
},
1407 { "pragma", handle_pragma
},
1408 { "line", handle_line
},
1410 // our internal preprocessor tokens
1411 { "nostdinc", handle_nostdinc
},
1412 { "add_include", handle_add_include
},
1415 for (i
= 0; i
< (sizeof (handlers
) / sizeof (handlers
[0])); i
++) {
1416 if (match_string_ident(ident
, handlers
[i
].name
))
1417 return handlers
[i
].handler(stream
, line
, token
);
1422 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1424 struct token
*token
= start
->next
;
1429 if (token_type(token
) == TOKEN_NUMBER
)
1430 if (handle_line(stream
, line
, start
))
1433 if (token_type(token
) == TOKEN_IDENT
)
1434 if (handle_preprocessor_command(stream
, line
, token
->ident
, token
))
1437 warn(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1440 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1442 struct token
*start
= *line
, *next
;
1443 struct token
**tp
= &start
->next
;
1447 if (next
->pos
.newline
)
1452 *tp
= &eof_token_entry
;
1453 handle_preprocessor_line(stream
, line
, start
);
1456 static void do_preprocess(struct token
**list
)
1460 while (!eof_token(next
= scan_next(list
))) {
1461 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1463 if (next
->pos
.newline
&& match_op(next
, '#')) {
1464 if (!next
->pos
.noexpand
) {
1465 preprocessor_line(stream
, list
);
1470 switch (token_type(next
)) {
1471 case TOKEN_STREAMEND
:
1472 if (stream
->nesting
< if_nesting
+ 1) {
1473 warn(unmatched_if
->pos
, "unterminated preprocessor conditional");
1474 // Pretend to see a series of #endifs
1475 MARK_STREAM_NONCONST(next
->pos
);
1477 handle_endif (stream
, NULL
, NULL
);
1478 } while (stream
->nesting
< if_nesting
+ 1);
1480 if (stream
->constant
== CONSTANT_FILE_MAYBE
&& stream
->protect
) {
1481 stream
->constant
= CONSTANT_FILE_YES
;
1485 case TOKEN_STREAMBEGIN
:
1486 stream
->nesting
= if_nesting
+ 1;
1491 if (false_nesting
) {
1496 if (token_type(next
) == TOKEN_IDENT
)
1497 list
= expand_one_symbol(list
);
1502 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1504 * Any token expansion (even if it ended up being an
1505 * empty expansion) in this stream implies it can't
1508 MARK_STREAM_NONCONST(next
->pos
);
1513 struct token
* preprocess(struct token
*token
)
1516 do_preprocess(&token
);
1518 // Drop all expressions from pre-processing, they're not used any more.
1519 clear_expression_alloc();