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.
8 * 2003-2004 Linus Torvalds
10 * Licensed under the Open Software License version 1.1
22 #include "pre-process.h"
27 #include "expression.h"
29 static int true_nesting
= 0;
30 static int false_nesting
= 0;
31 static struct token
*unmatched_if
= NULL
;
32 #define if_nesting (true_nesting + false_nesting)
34 #define MAX_NEST (256)
35 static unsigned char elif_ignore
[MAX_NEST
];
36 enum { ELIF_IGNORE
= 1, ELIF_SEEN_ELSE
= 2 };
38 #define INCLUDEPATHS 300
39 const char *includepath
[INCLUDEPATHS
+1] = {
46 static const char **sys_includepath
= includepath
+ 0;
47 static const char **gcc_includepath
= includepath
+ 2;
49 #define MARK_STREAM_NONCONST(pos) do { \
50 if (stream->constant != CONSTANT_FILE_NOPE) { \
52 info(pos, "%s triggers non-const", __func__); \
53 stream->constant = CONSTANT_FILE_NOPE; \
58 static struct token
*alloc_token(struct position
*pos
)
60 struct token
*token
= __alloc_token(0);
62 token
->pos
.stream
= pos
->stream
;
63 token
->pos
.line
= pos
->line
;
64 token
->pos
.pos
= pos
->pos
;
65 token
->pos
.whitespace
= 1;
69 static const char *show_token_sequence(struct token
*token
);
71 /* Expand symbol 'sym' at '*list' */
72 static int expand(struct token
**, struct symbol
*);
74 static void replace_with_string(struct token
*token
, const char *str
)
76 int size
= strlen(str
) + 1;
77 struct string
*s
= __alloc_string(size
);
80 memcpy(s
->data
, str
, size
);
81 token_type(token
) = TOKEN_STRING
;
85 static void replace_with_integer(struct token
*token
, unsigned int val
)
87 char *buf
= __alloc_bytes(11);
88 sprintf(buf
, "%u", val
);
89 token_type(token
) = TOKEN_NUMBER
;
93 static int token_defined(struct token
*token
)
95 if (token_type(token
) == TOKEN_IDENT
) {
96 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_MACRO
);
104 warning(token
->pos
, "expected preprocessor identifier");
108 static void replace_with_defined(struct token
*token
)
110 static const char *string
[] = { "0", "1" };
111 int defined
= token_defined(token
);
113 token_type(token
) = TOKEN_NUMBER
;
114 token
->number
= string
[defined
];
117 static int expand_one_symbol(struct token
**list
)
119 struct token
*token
= *list
;
122 if (token
->pos
.noexpand
)
125 sym
= lookup_symbol(token
->ident
, NS_MACRO
);
128 return expand(list
, sym
);
130 if (token
->ident
== &__LINE___ident
) {
131 replace_with_integer(token
, token
->pos
.line
);
132 } else if (token
->ident
== &__FILE___ident
) {
133 replace_with_string(token
, (input_streams
+ token
->pos
.stream
)->name
);
138 static inline struct token
*scan_next(struct token
**where
)
140 struct token
*token
= *where
;
141 if (token_type(token
) != TOKEN_UNTAINT
)
144 token
->ident
->tainted
= 0;
146 } while (token_type(token
) == TOKEN_UNTAINT
);
151 static void expand_list(struct token
**list
)
154 while (!eof_token(next
= scan_next(list
))) {
155 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
160 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
162 struct token
**p
= &prev
->next
;
166 while (!eof_token(next
= scan_next(p
))) {
167 if (match_op(next
, '(')) {
169 } else if (match_op(next
, ')')) {
172 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
175 next
->pos
.stream
= pos
->stream
;
176 next
->pos
.line
= pos
->line
;
177 next
->pos
.pos
= pos
->pos
;
180 *p
= &eof_token_entry
;
185 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
190 struct token
*expanded
;
197 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
199 int wanted
= arglist
->count
.normal
;
200 struct token
*next
= NULL
;
203 arglist
= arglist
->next
; /* skip counter */
206 next
= collect_arg(start
, 0, &what
->pos
);
209 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
214 for (count
= 0; count
< wanted
; count
++) {
215 struct argcount
*p
= &arglist
->next
->count
;
216 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
217 arglist
= arglist
->next
->next
;
220 args
[count
].arg
= start
->next
;
221 args
[count
].n_normal
= p
->normal
;
222 args
[count
].n_quoted
= p
->quoted
;
223 args
[count
].n_str
= p
->str
;
224 if (match_op(next
, ')')) {
230 if (count
== wanted
&& !match_op(next
, ')'))
232 if (count
== wanted
- 1) {
233 struct argcount
*p
= &arglist
->next
->count
;
236 args
[count
].arg
= NULL
;
237 args
[count
].n_normal
= p
->normal
;
238 args
[count
].n_quoted
= p
->quoted
;
239 args
[count
].n_str
= p
->str
;
241 if (count
< wanted
- 1)
244 what
->next
= next
->next
;
248 warning(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
249 show_token(what
), wanted
, count
);
252 while (match_op(next
, ',')) {
253 next
= collect_arg(next
, 0, &what
->pos
);
258 warning(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
259 show_token(what
), count
, wanted
);
262 warning(what
->pos
, "unterminated argument list invoking macro \"%s\"",
265 what
->next
= next
->next
;
269 static struct token
*dup_list(struct token
*list
)
272 struct token
**p
= &res
;
274 while (!eof_token(list
)) {
275 struct token
*newtok
= __alloc_token(0);
284 static struct token
*stringify(struct token
*arg
)
286 const char *s
= show_token_sequence(arg
);
287 int size
= strlen(s
)+1;
288 struct token
*token
= __alloc_token(0);
289 struct string
*string
= __alloc_string(size
);
291 memcpy(string
->data
, s
, size
);
292 string
->length
= size
;
293 token
->pos
= arg
->pos
;
294 token_type(token
) = TOKEN_STRING
;
295 token
->string
= string
;
296 token
->next
= &eof_token_entry
;
300 static void expand_arguments(int count
, struct arg
*args
)
303 for (i
= 0; i
< count
; i
++) {
304 struct token
*arg
= args
[i
].arg
;
306 arg
= &eof_token_entry
;
308 args
[i
].str
= stringify(arg
);
309 if (args
[i
].n_normal
) {
310 if (!args
[i
].n_quoted
) {
311 args
[i
].expanded
= arg
;
313 } else if (eof_token(arg
)) {
314 args
[i
].expanded
= arg
;
316 args
[i
].expanded
= dup_list(arg
);
318 expand_list(&args
[i
].expanded
);
324 * Possibly valid combinations:
325 * - ident + ident -> ident
326 * - ident + number -> ident unless number contains '.', '+' or '-'.
327 * - number + number -> number
328 * - number + ident -> number
329 * - number + '.' -> number
330 * - number + '+' or '-' -> number, if number used to end on [eEpP].
331 * - '.' + number -> number, if number used to start with a digit.
332 * - special + special -> either special or an error.
334 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
337 enum token_type t1
= token_type(left
), t2
= token_type(right
);
339 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
342 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
345 strcpy(p
, show_token(left
));
346 strcat(p
, show_token(right
));
352 if (t1
== TOKEN_IDENT
) {
353 if (t2
== TOKEN_SPECIAL
)
355 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
360 if (t1
== TOKEN_NUMBER
) {
361 if (t2
== TOKEN_SPECIAL
) {
362 switch (right
->special
) {
366 if (strchr("eEpP", p
[len
- 2]))
375 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
378 return TOKEN_SPECIAL
;
381 static int merge(struct token
*left
, struct token
*right
)
383 extern unsigned char combinations
[][3];
384 static char buffer
[512];
387 switch (combine(left
, right
, buffer
)) {
389 left
->ident
= built_in_ident(buffer
);
390 left
->pos
.noexpand
= 0;
394 char *number
= __alloc_bytes(strlen(buffer
) + 1);
395 memcpy(number
, buffer
, strlen(buffer
) + 1);
396 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
397 left
->number
= number
;
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 warning(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 int 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 warning(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_die(token
->pos
, "unable to open '%s'", filename
);
665 static int free_preprocessor_line(struct token
*token
)
668 struct token
*free
= token
;
671 } while (token_type(token
) != TOKEN_EOF
);
675 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
677 const char *filename
;
682 return free_preprocessor_line(token
);
684 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
685 MARK_STREAM_NONCONST(token
->pos
);
689 if (!match_op(next
, '<')) {
690 expand_list(&token
->next
);
693 if (match_op(token
->next
, '<')) {
699 filename
= token_name_sequence(token
, expect
, token
);
700 do_include(!expect
, stream
, list
, token
, filename
, path
);
704 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
706 return handle_include_path(stream
, list
, token
, includepath
);
709 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
711 return handle_include_path(stream
, list
, token
, stream
->next_path
);
714 static int token_different(struct token
*t1
, struct token
*t2
)
718 if (token_type(t1
) != token_type(t2
))
721 switch (token_type(t1
)) {
723 different
= t1
->ident
!= t2
->ident
;
725 case TOKEN_ARG_COUNT
:
728 case TOKEN_GNU_KLUDGE
:
732 different
= strcmp(t1
->number
, t2
->number
);
735 different
= t1
->special
!= t2
->special
;
737 case TOKEN_MACRO_ARGUMENT
:
738 case TOKEN_QUOTED_ARGUMENT
:
739 case TOKEN_STR_ARGUMENT
:
740 different
= t1
->argnum
!= t2
->argnum
;
743 different
= t1
->character
!= t2
->character
;
746 struct string
*s1
, *s2
;
751 if (s1
->length
!= s2
->length
)
753 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
763 static int token_list_different(struct token
*list1
, struct token
*list2
)
768 if (!list1
|| !list2
)
770 if (token_different(list1
, list2
))
777 static inline void set_arg_count(struct token
*token
)
779 token_type(token
) = TOKEN_ARG_COUNT
;
780 token
->count
.normal
= token
->count
.quoted
=
781 token
->count
.str
= token
->count
.vararg
= 0;
784 static struct token
*parse_arguments(struct token
*list
)
786 struct token
*arg
= list
->next
, *next
= list
;
787 struct argcount
*count
= &list
->count
;
791 if (match_op(arg
, ')')) {
793 list
->next
= &eof_token_entry
;
797 while (token_type(arg
) == TOKEN_IDENT
) {
798 if (arg
->ident
== &__VA_ARGS___ident
)
800 if (!++count
->normal
)
804 if (match_op(next
, ',')) {
810 if (match_op(next
, ')')) {
813 arg
->next
->next
= &eof_token_entry
;
817 /* normal cases are finished here */
819 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
820 if (match_op(next
->next
, ')')) {
822 next
->count
.vararg
= 1;
824 arg
->next
->next
= &eof_token_entry
;
832 if (eof_token(next
)) {
840 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
842 token_type(arg
) = TOKEN_IDENT
;
843 arg
->ident
= &__VA_ARGS___ident
;
844 if (!match_op(next
, ')'))
846 if (!++count
->normal
)
849 next
->count
.vararg
= 1;
851 arg
->next
->next
= &eof_token_entry
;
855 if (eof_token(arg
)) {
859 if (match_op(arg
, ','))
866 warning(arg
->pos
, "parameter name missing");
869 warning(arg
->pos
, "\"%s\" may not appear in macro parameter list",
873 warning(arg
->pos
, "missing ')' in macro parameter list");
876 warning(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
879 warning(arg
->pos
, "too many arguments in macro definition");
883 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
885 struct ident
*ident
= token
->ident
;
888 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
891 arglist
= arglist
->next
;
893 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
894 if (arglist
->ident
== ident
) {
895 struct argcount
*count
= &arglist
->next
->count
;
899 token_type(token
) = type
;
901 case TOKEN_MACRO_ARGUMENT
:
904 case TOKEN_QUOTED_ARGUMENT
:
911 return count
->vararg
? 2 : 1;
912 token_type(token
) = TOKEN_ERROR
;
919 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
921 struct token
*token
= expansion
;
923 struct token
*last
= NULL
;
925 if (match_op(token
, SPECIAL_HASHHASH
))
928 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
929 if (match_op(token
, '#')) {
931 struct token
*next
= token
->next
;
932 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
934 next
->pos
.whitespace
= token
->pos
.whitespace
;
937 token
->pos
.noexpand
= 1;
939 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
940 struct token
*next
= token
->next
;
941 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
942 token_type(token
) = TOKEN_CONCAT
;
946 if (arg
== 2 && last
&& match_op(last
, ',')) {
947 token_type(last
) = TOKEN_GNU_KLUDGE
;
950 } else if (match_op(next
, SPECIAL_HASHHASH
))
952 else if (match_op(next
, ','))
954 else if (eof_token(next
))
956 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
957 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
959 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
961 if (token_type(token
) == TOKEN_ERROR
)
965 token
= alloc_token(&expansion
->pos
);
966 token_type(token
) = TOKEN_UNTAINT
;
973 warning(token
->pos
, "'#' is not followed by a macro parameter");
977 warning(token
->pos
, "'##' cannot appear at the ends of macro expansion");
980 warning(token
->pos
, "too many instances of argument in body");
984 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int weak
)
986 struct token
*arglist
, *expansion
;
987 struct token
*left
= token
->next
;
991 if (token_type(left
) != TOKEN_IDENT
) {
992 warning(token
->pos
, "expected identifier to 'define'");
996 return free_preprocessor_line(token
);
998 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
999 MARK_STREAM_NONCONST(token
->pos
);
1001 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1005 expansion
= left
->next
;
1006 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
1007 arglist
= expansion
;
1008 expansion
= parse_arguments(expansion
);
1013 expansion
= parse_expansion(expansion
, arglist
, name
);
1017 sym
= lookup_symbol(name
, NS_MACRO
);
1019 if (token_list_different(sym
->expansion
, expansion
) ||
1020 token_list_different(sym
->arglist
, arglist
)) {
1025 warning(left
->pos
, "preprocessor token %.*s redefined",
1026 name
->len
, name
->name
);
1027 info(sym
->pos
, "this was the original definition");
1028 sym
->expansion
= expansion
;
1029 sym
->arglist
= arglist
;
1033 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1034 bind_symbol(sym
, name
, NS_MACRO
);
1037 sym
->expansion
= expansion
;
1038 sym
->arglist
= arglist
;
1043 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1045 return do_handle_define(stream
, line
, token
, 0);
1048 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1050 return do_handle_define(stream
, line
, token
, 1);
1053 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1055 struct token
*left
= token
->next
;
1056 struct symbol
**sym
;
1058 if (token_type(left
) != TOKEN_IDENT
) {
1059 warning(token
->pos
, "expected identifier to 'undef'");
1063 return free_preprocessor_line(token
);
1065 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1066 MARK_STREAM_NONCONST(token
->pos
);
1068 sym
= &left
->ident
->symbols
;
1070 struct symbol
*t
= *sym
;
1071 if (t
->namespace == NS_MACRO
) {
1077 return free_preprocessor_line(token
);
1080 static int preprocessor_if(struct token
*token
, int true)
1082 if (if_nesting
== 0)
1083 unmatched_if
= token
;
1084 if (if_nesting
>= MAX_NEST
)
1085 error_die(token
->pos
, "Maximum preprocessor conditional level exhausted");
1086 elif_ignore
[if_nesting
] = (false_nesting
|| true) ? ELIF_IGNORE
: 0;
1087 if (false_nesting
|| !true) {
1089 return free_preprocessor_line(token
);
1092 return free_preprocessor_line(token
);
1095 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1097 return preprocessor_if(token
, token_defined(token
->next
));
1100 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1102 struct token
*next
= token
->next
;
1103 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1104 if (token_type(next
) == TOKEN_IDENT
&&
1105 (!stream
->protect
|| stream
->protect
== next
->ident
)) {
1106 stream
->constant
= CONSTANT_FILE_IFNDEF
;
1107 stream
->protect
= next
->ident
;
1109 MARK_STREAM_NONCONST(token
->pos
);
1111 return preprocessor_if(token
, !token_defined(next
));
1115 * Expression handling for #if and #elif; it differs from normal expansion
1116 * due to special treatment of "defined".
1118 static int expression_value(struct token
**where
)
1120 struct expression
*expr
;
1122 struct token
**list
= where
, **beginning
= NULL
;
1126 while (!eof_token(p
= scan_next(list
))) {
1129 if (token_type(p
) != TOKEN_IDENT
)
1131 if (p
->ident
== &defined_ident
) {
1136 if (!expand_one_symbol(list
))
1138 if (token_type(p
) != TOKEN_IDENT
)
1140 if (Wundefined_preprocessor
)
1141 warning(p
->pos
, "undefined preprocessor identifier '%s'", show_ident(p
->ident
));
1142 replace_with_integer(p
, 0);
1145 if (match_op(p
, '(')) {
1149 replace_with_defined(p
);
1154 if (token_type(p
) == TOKEN_IDENT
)
1158 replace_with_defined(p
);
1163 if (!match_op(p
, ')'))
1164 warning(p
->pos
, "missing ')' after \"defined\"");
1171 p
= constant_expression(*where
, &expr
);
1173 warning(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1174 value
= get_expression_value(expr
);
1178 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1182 value
= expression_value(&token
->next
);
1184 // This is an approximation. We really only need this if the
1185 // condition does depends on a pre-processor symbol. Note, that
1186 // the important #ifndef case has already changed ->constant.
1187 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1188 MARK_STREAM_NONCONST(token
->pos
);
1190 return preprocessor_if(token
, value
);
1193 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1195 if (stream
->nesting
== if_nesting
)
1196 MARK_STREAM_NONCONST(token
->pos
);
1198 if (stream
->nesting
> if_nesting
)
1199 warning(token
->pos
, "unmatched #elif within stream");
1201 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1202 warning(token
->pos
, "#elif after #else");
1204 if (false_nesting
) {
1205 /* If this whole if-thing is if'ed out, an elif cannot help */
1206 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1208 if (expression_value(&token
->next
)) {
1211 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1217 return free_preprocessor_line(token
);
1220 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1222 if (stream
->nesting
== if_nesting
)
1223 MARK_STREAM_NONCONST(token
->pos
);
1225 if (stream
->nesting
> if_nesting
)
1226 warning(token
->pos
, "unmatched #else within stream");
1228 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1229 warning(token
->pos
, "#else after #else");
1231 elif_ignore
[if_nesting
-1] |= ELIF_SEEN_ELSE
;
1233 if (false_nesting
) {
1234 /* If this whole if-thing is if'ed out, an else cannot help */
1235 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1239 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1244 return free_preprocessor_line(token
);
1247 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1249 if (stream
->constant
== CONSTANT_FILE_IFNDEF
&& stream
->nesting
== if_nesting
)
1250 stream
->constant
= CONSTANT_FILE_MAYBE
;
1252 if (stream
->nesting
> if_nesting
)
1253 warning(token
->pos
, "unmatched #endif in stream");
1258 return free_preprocessor_line(token
);
1261 static const char *show_token_sequence(struct token
*token
)
1263 static char buffer
[1024];
1269 while (!eof_token(token
)) {
1270 const char *val
= show_token(token
);
1271 int len
= strlen(val
);
1273 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1274 warning(token
->pos
, "too long token expansion");
1280 memcpy(ptr
, val
, len
);
1282 token
= token
->next
;
1283 whitespace
= token
->pos
.whitespace
;
1289 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1292 return free_preprocessor_line(token
);
1293 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1294 MARK_STREAM_NONCONST(token
->pos
);
1295 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1296 return free_preprocessor_line(token
);
1299 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1302 return free_preprocessor_line(token
);
1303 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1304 MARK_STREAM_NONCONST(token
->pos
);
1305 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1306 return free_preprocessor_line(token
);
1309 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1314 return free_preprocessor_line(token
);
1317 * Do we have any non-system includes?
1318 * Clear them out if so..
1320 stdinc
= gcc_includepath
- sys_includepath
;
1322 const char **src
= gcc_includepath
;
1323 const char **dst
= sys_includepath
;
1330 gcc_includepath
-= stdinc
;
1332 return free_preprocessor_line(token
);
1335 static void add_path_entry(struct token
*token
, const char *path
)
1340 /* Need one free entry.. */
1341 if (includepath
[INCLUDEPATHS
-2])
1342 error_die(token
->pos
, "too many include path entries");
1345 dst
= sys_includepath
;
1350 * Move them all up starting at "sys_includepath",
1351 * insert the new entry..
1354 const char *tmp
= *dst
;
1363 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1366 token
= token
->next
;
1367 if (eof_token(token
))
1369 if (token_type(token
) != TOKEN_STRING
) {
1370 warning(token
->pos
, "expected path string");
1373 add_path_entry(token
, token
->string
->data
);
1378 * We replace "#pragma xxx" with "__pragma__" in the token
1379 * stream. Just as an example.
1381 * We'll just #define that away for now, but the theory here
1382 * is that we can use this to insert arbitrary token sequences
1383 * to turn the pragma's into internal front-end sequences for
1384 * when we actually start caring about them.
1386 * So eventually this will turn into some kind of extended
1387 * __attribute__() like thing, except called __pragma__(xxx).
1389 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1391 struct token
*next
= *line
;
1393 token
->ident
= &pragma_ident
;
1394 token
->pos
.newline
= 1;
1395 token
->pos
.whitespace
= 1;
1403 * We ignore #line for now.
1405 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1411 void init_preprocessor(void)
1414 int stream
= init_stream("preprocessor", -1, includepath
);
1417 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1419 { "define", handle_define
},
1420 { "weak_define",handle_weak_define
},
1421 { "undef", handle_undef
},
1422 { "ifdef", handle_ifdef
},
1423 { "ifndef", handle_ifndef
},
1424 { "else", handle_else
},
1425 { "endif", handle_endif
},
1426 { "if", handle_if
},
1427 { "elif", handle_elif
},
1428 { "warning", handle_warning
},
1429 { "error", handle_error
},
1430 { "include", handle_include
},
1431 { "include_next",handle_include_next
},
1432 { "pragma", handle_pragma
},
1433 { "line", handle_line
},
1435 // our internal preprocessor tokens
1436 { "nostdinc", handle_nostdinc
},
1437 { "add_include", handle_add_include
},
1440 for (i
= 0; i
< (sizeof (handlers
) / sizeof (handlers
[0])); i
++) {
1442 sym
= create_symbol(stream
, handlers
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1443 sym
->handler
= handlers
[i
].handler
;
1447 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1449 struct token
*token
= start
->next
;
1454 if (token_type(token
) == TOKEN_NUMBER
)
1455 if (handle_line(stream
, line
, start
))
1458 if (token_type(token
) == TOKEN_IDENT
) {
1459 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1460 if (sym
&& sym
->handler(stream
, line
, token
))
1464 warning(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1467 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1469 struct token
*start
= *line
, *next
;
1470 struct token
**tp
= &start
->next
;
1474 if (next
->pos
.newline
)
1479 *tp
= &eof_token_entry
;
1480 handle_preprocessor_line(stream
, line
, start
);
1483 static void do_preprocess(struct token
**list
)
1487 while (!eof_token(next
= scan_next(list
))) {
1488 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1490 if (next
->pos
.newline
&& match_op(next
, '#')) {
1491 if (!next
->pos
.noexpand
) {
1492 preprocessor_line(stream
, list
);
1493 __free_token(next
); /* Free the '#' token */
1498 switch (token_type(next
)) {
1499 case TOKEN_STREAMEND
:
1500 if (stream
->nesting
< if_nesting
+ 1) {
1501 warning(unmatched_if
->pos
, "unterminated preprocessor conditional");
1502 // Pretend to see a series of #endifs
1503 MARK_STREAM_NONCONST(next
->pos
);
1505 handle_endif (stream
, NULL
, NULL
);
1506 } while (stream
->nesting
< if_nesting
+ 1);
1508 if (stream
->constant
== CONSTANT_FILE_MAYBE
&& stream
->protect
) {
1509 stream
->constant
= CONSTANT_FILE_YES
;
1513 case TOKEN_STREAMBEGIN
:
1514 stream
->nesting
= if_nesting
+ 1;
1519 if (false_nesting
) {
1525 if (token_type(next
) != TOKEN_IDENT
||
1526 expand_one_symbol(list
))
1530 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1532 * Any token expansion (even if it ended up being an
1533 * empty expansion) in this stream implies it can't
1536 MARK_STREAM_NONCONST(next
->pos
);
1541 struct token
* preprocess(struct token
*token
)
1544 init_preprocessor();
1545 do_preprocess(&token
);
1547 // Drop all expressions from pre-processing, they're not used any more.
1548 clear_expression_alloc();