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"
28 #include "expression.h"
30 static int true_nesting
= 0;
31 static int false_nesting
= 0;
32 static struct token
*unmatched_if
= NULL
;
33 #define if_nesting (true_nesting + false_nesting)
35 #define MAX_NEST (256)
36 static unsigned char elif_ignore
[MAX_NEST
];
37 enum { ELIF_IGNORE
= 1, ELIF_SEEN_ELSE
= 2 };
39 #define INCLUDEPATHS 300
40 const char *includepath
[INCLUDEPATHS
+1] = {
47 static const char **sys_includepath
= includepath
+ 0;
48 static const char **gcc_includepath
= includepath
+ 2;
50 #define MARK_STREAM_NONCONST(pos) do { \
51 if (stream->constant != CONSTANT_FILE_NOPE) { \
53 info(pos, "%s triggers non-const", __func__); \
54 stream->constant = CONSTANT_FILE_NOPE; \
59 static struct token
*alloc_token(struct position
*pos
)
61 struct token
*token
= __alloc_token(0);
63 token
->pos
.stream
= pos
->stream
;
64 token
->pos
.line
= pos
->line
;
65 token
->pos
.pos
= pos
->pos
;
66 token
->pos
.whitespace
= 1;
70 static const char *show_token_sequence(struct token
*token
);
72 /* Expand symbol 'sym' at '*list' */
73 static int expand(struct token
**, struct symbol
*);
75 static void replace_with_string(struct token
*token
, const char *str
)
77 int size
= strlen(str
) + 1;
78 struct string
*s
= __alloc_string(size
);
81 memcpy(s
->data
, str
, size
);
82 token_type(token
) = TOKEN_STRING
;
86 static void replace_with_integer(struct token
*token
, unsigned int val
)
88 char *buf
= __alloc_bytes(11);
89 sprintf(buf
, "%u", val
);
90 token_type(token
) = TOKEN_NUMBER
;
94 static int token_defined(struct token
*token
)
96 if (token_type(token
) == TOKEN_IDENT
) {
97 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_MACRO
);
105 warning(token
->pos
, "expected preprocessor identifier");
109 static void replace_with_defined(struct token
*token
)
111 static const char *string
[] = { "0", "1" };
112 int defined
= token_defined(token
);
114 token_type(token
) = TOKEN_NUMBER
;
115 token
->number
= string
[defined
];
118 static int expand_one_symbol(struct token
**list
)
120 struct token
*token
= *list
;
123 if (token
->pos
.noexpand
)
126 sym
= lookup_symbol(token
->ident
, NS_MACRO
);
129 return expand(list
, sym
);
131 if (token
->ident
== &__LINE___ident
) {
132 replace_with_integer(token
, token
->pos
.line
);
133 } else if (token
->ident
== &__FILE___ident
) {
134 replace_with_string(token
, stream_name(token
->pos
.stream
));
139 static inline struct token
*scan_next(struct token
**where
)
141 struct token
*token
= *where
;
142 if (token_type(token
) != TOKEN_UNTAINT
)
145 token
->ident
->tainted
= 0;
147 } while (token_type(token
) == TOKEN_UNTAINT
);
152 static void expand_list(struct token
**list
)
155 while (!eof_token(next
= scan_next(list
))) {
156 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
161 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
163 struct token
**p
= &prev
->next
;
167 while (!eof_token(next
= scan_next(p
))) {
168 if (match_op(next
, '(')) {
170 } else if (match_op(next
, ')')) {
173 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
176 next
->pos
.stream
= pos
->stream
;
177 next
->pos
.line
= pos
->line
;
178 next
->pos
.pos
= pos
->pos
;
181 *p
= &eof_token_entry
;
186 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
191 struct token
*expanded
;
198 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
200 int wanted
= arglist
->count
.normal
;
201 struct token
*next
= NULL
;
204 arglist
= arglist
->next
; /* skip counter */
207 next
= collect_arg(start
, 0, &what
->pos
);
210 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
215 for (count
= 0; count
< wanted
; count
++) {
216 struct argcount
*p
= &arglist
->next
->count
;
217 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
218 arglist
= arglist
->next
->next
;
221 args
[count
].arg
= start
->next
;
222 args
[count
].n_normal
= p
->normal
;
223 args
[count
].n_quoted
= p
->quoted
;
224 args
[count
].n_str
= p
->str
;
225 if (match_op(next
, ')')) {
231 if (count
== wanted
&& !match_op(next
, ')'))
233 if (count
== wanted
- 1) {
234 struct argcount
*p
= &arglist
->next
->count
;
237 args
[count
].arg
= NULL
;
238 args
[count
].n_normal
= p
->normal
;
239 args
[count
].n_quoted
= p
->quoted
;
240 args
[count
].n_str
= p
->str
;
242 if (count
< wanted
- 1)
245 what
->next
= next
->next
;
249 warning(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
250 show_token(what
), wanted
, count
);
253 while (match_op(next
, ',')) {
254 next
= collect_arg(next
, 0, &what
->pos
);
259 warning(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
260 show_token(what
), count
, wanted
);
263 warning(what
->pos
, "unterminated argument list invoking macro \"%s\"",
266 what
->next
= next
->next
;
270 static struct token
*dup_list(struct token
*list
)
273 struct token
**p
= &res
;
275 while (!eof_token(list
)) {
276 struct token
*newtok
= __alloc_token(0);
285 static struct token
*stringify(struct token
*arg
)
287 const char *s
= show_token_sequence(arg
);
288 int size
= strlen(s
)+1;
289 struct token
*token
= __alloc_token(0);
290 struct string
*string
= __alloc_string(size
);
292 memcpy(string
->data
, s
, size
);
293 string
->length
= size
;
294 token
->pos
= arg
->pos
;
295 token_type(token
) = TOKEN_STRING
;
296 token
->string
= string
;
297 token
->next
= &eof_token_entry
;
301 static void expand_arguments(int count
, struct arg
*args
)
304 for (i
= 0; i
< count
; i
++) {
305 struct token
*arg
= args
[i
].arg
;
307 arg
= &eof_token_entry
;
309 args
[i
].str
= stringify(arg
);
310 if (args
[i
].n_normal
) {
311 if (!args
[i
].n_quoted
) {
312 args
[i
].expanded
= arg
;
314 } else if (eof_token(arg
)) {
315 args
[i
].expanded
= arg
;
317 args
[i
].expanded
= dup_list(arg
);
319 expand_list(&args
[i
].expanded
);
325 * Possibly valid combinations:
326 * - ident + ident -> ident
327 * - ident + number -> ident unless number contains '.', '+' or '-'.
328 * - number + number -> number
329 * - number + ident -> number
330 * - number + '.' -> number
331 * - number + '+' or '-' -> number, if number used to end on [eEpP].
332 * - '.' + number -> number, if number used to start with a digit.
333 * - special + special -> either special or an error.
335 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
338 enum token_type t1
= token_type(left
), t2
= token_type(right
);
340 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
343 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
346 strcpy(p
, show_token(left
));
347 strcat(p
, show_token(right
));
353 if (t1
== TOKEN_IDENT
) {
354 if (t2
== TOKEN_SPECIAL
)
356 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
361 if (t1
== TOKEN_NUMBER
) {
362 if (t2
== TOKEN_SPECIAL
) {
363 switch (right
->special
) {
367 if (strchr("eEpP", p
[len
- 2]))
376 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
379 return TOKEN_SPECIAL
;
382 static int merge(struct token
*left
, struct token
*right
)
384 extern unsigned char combinations
[][3];
385 static char buffer
[512];
388 switch (combine(left
, right
, buffer
)) {
390 left
->ident
= built_in_ident(buffer
);
391 left
->pos
.noexpand
= 0;
395 char *number
= __alloc_bytes(strlen(buffer
) + 1);
396 memcpy(number
, buffer
, strlen(buffer
) + 1);
397 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
398 left
->number
= number
;
403 if (buffer
[2] && buffer
[3])
405 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
406 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
414 warning(left
->pos
, "'##' failed: concatenation is not a valid token");
418 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
420 struct token
*alloc
= alloc_token(streampos
);
421 token_type(alloc
) = token_type(token
);
422 alloc
->pos
.newline
= pos
->newline
;
423 alloc
->pos
.whitespace
= pos
->whitespace
;
424 alloc
->number
= token
->number
;
425 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
429 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
431 int need_copy
= --*count
;
432 while (!eof_token(list
)) {
435 token
= dup_token(list
, &list
->pos
, &list
->pos
);
438 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
439 token
->pos
.noexpand
= 1;
441 where
= &token
->next
;
444 *where
= &eof_token_entry
;
448 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
450 struct token
*token
= *list
;
451 struct position
*base_pos
= &token
->pos
;
452 struct position
*pos
= base_pos
;
454 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
456 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
457 struct token
*added
, *arg
;
460 switch (token_type(body
)) {
461 case TOKEN_GNU_KLUDGE
:
463 * GNU kludge: if we had <comma>##<vararg>, behaviour
464 * depends on whether we had enough arguments to have
465 * a vararg. If we did, ## is just ignored. Otherwise
466 * both , and ## are ignored. Comma should come from
467 * the body of macro and not be an argument of earlier
470 if (!args
[body
->next
->argnum
].arg
)
472 added
= dup_token(body
, base_pos
, pos
);
473 token_type(added
) = TOKEN_SPECIAL
;
477 case TOKEN_STR_ARGUMENT
:
478 arg
= args
[body
->argnum
].str
;
479 count
= &args
[body
->argnum
].n_str
;
482 case TOKEN_QUOTED_ARGUMENT
:
483 arg
= args
[body
->argnum
].arg
;
484 count
= &args
[body
->argnum
].n_quoted
;
485 if (!arg
|| eof_token(arg
)) {
494 case TOKEN_MACRO_ARGUMENT
:
495 arg
= args
[body
->argnum
].expanded
;
496 count
= &args
[body
->argnum
].n_normal
;
497 if (eof_token(arg
)) {
502 tail
= copy(&added
, arg
, count
);
503 added
->pos
.newline
= pos
->newline
;
504 added
->pos
.whitespace
= pos
->whitespace
;
508 if (state
== Placeholder
)
515 added
= dup_token(body
, base_pos
, pos
);
516 if (added
->ident
->tainted
)
517 added
->pos
.noexpand
= 1;
522 added
= dup_token(body
, base_pos
, pos
);
528 * if we got to doing real concatenation, we already have
529 * added something into the list, so containing_token() is OK.
531 if (state
== Concat
&& merge(containing_token(list
), added
)) {
533 if (tail
!= &added
->next
)
541 *list
= &eof_token_entry
;
545 static int expand(struct token
**list
, struct symbol
*sym
)
548 struct token
*token
= *list
;
549 struct ident
*expanding
= token
->ident
;
551 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
552 struct arg args
[nargs
];
554 if (expanding
->tainted
) {
555 token
->pos
.noexpand
= 1;
560 if (!match_op(scan_next(&token
->next
), '('))
562 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
564 expand_arguments(nargs
, args
);
567 expanding
->tainted
= 1;
570 tail
= substitute(list
, sym
->expansion
, args
);
576 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
579 static char buffer
[256];
583 while (!eof_token(token
) && !match_op(token
, endop
)) {
585 const char *val
= token
->string
->data
;
586 if (token_type(token
) != TOKEN_STRING
)
587 val
= show_token(token
);
589 memcpy(ptr
, val
, len
);
594 if (endop
&& !match_op(token
, endop
))
595 warning(start
->pos
, "expected '>' at end of filename");
599 static int already_tokenized(const char *path
)
602 struct stream
*s
= input_streams
;
604 for (i
= input_stream_nr
; --i
>= 0; s
++) {
605 if (s
->constant
!= CONSTANT_FILE_YES
)
607 if (strcmp(path
, s
->name
))
609 if (!lookup_symbol(s
->protect
, NS_MACRO
))
616 static int try_include(const char *path
, int plen
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
619 static char fullname
[PATH_MAX
];
621 memcpy(fullname
, path
, plen
);
622 if (plen
&& path
[plen
-1] != '/') {
623 fullname
[plen
] = '/';
626 memcpy(fullname
+plen
, filename
, flen
);
627 if (already_tokenized(fullname
))
629 fd
= open(fullname
, O_RDONLY
);
631 char * streamname
= __alloc_bytes(plen
+ flen
);
632 memcpy(streamname
, fullname
, plen
+ flen
);
633 *where
= tokenize(streamname
, fd
, *where
, next_path
);
640 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
644 while ((path
= *pptr
++) != NULL
) {
645 if (!try_include(path
, strlen(path
), filename
, flen
, list
, pptr
))
653 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
655 int flen
= strlen(filename
) + 1;
658 if (filename
[0] == '/') {
659 if (try_include("", 0, filename
, flen
, list
, includepath
))
664 /* Same directory as current stream? */
671 slash
= strrchr(path
, '/');
672 plen
= slash
? slash
- path
: 0;
674 if (try_include(path
, plen
, filename
, flen
, list
, includepath
))
678 /* Check the standard include paths.. */
679 if (do_include_path(path
, list
, token
, filename
, flen
))
682 error_die(token
->pos
, "unable to open '%s'", filename
);
685 static int free_preprocessor_line(struct token
*token
)
688 struct token
*free
= token
;
691 } while (token_type(token
) != TOKEN_EOF
);
695 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
697 const char *filename
;
702 return free_preprocessor_line(token
);
704 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
705 MARK_STREAM_NONCONST(token
->pos
);
709 if (!match_op(next
, '<')) {
710 expand_list(&token
->next
);
713 if (match_op(token
->next
, '<')) {
719 filename
= token_name_sequence(token
, expect
, token
);
720 do_include(!expect
, stream
, list
, token
, filename
, path
);
724 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
726 return handle_include_path(stream
, list
, token
, includepath
);
729 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
731 return handle_include_path(stream
, list
, token
, stream
->next_path
);
734 static int token_different(struct token
*t1
, struct token
*t2
)
738 if (token_type(t1
) != token_type(t2
))
741 switch (token_type(t1
)) {
743 different
= t1
->ident
!= t2
->ident
;
745 case TOKEN_ARG_COUNT
:
748 case TOKEN_GNU_KLUDGE
:
752 different
= strcmp(t1
->number
, t2
->number
);
755 different
= t1
->special
!= t2
->special
;
757 case TOKEN_MACRO_ARGUMENT
:
758 case TOKEN_QUOTED_ARGUMENT
:
759 case TOKEN_STR_ARGUMENT
:
760 different
= t1
->argnum
!= t2
->argnum
;
763 different
= t1
->character
!= t2
->character
;
766 struct string
*s1
, *s2
;
771 if (s1
->length
!= s2
->length
)
773 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
783 static int token_list_different(struct token
*list1
, struct token
*list2
)
788 if (!list1
|| !list2
)
790 if (token_different(list1
, list2
))
797 static inline void set_arg_count(struct token
*token
)
799 token_type(token
) = TOKEN_ARG_COUNT
;
800 token
->count
.normal
= token
->count
.quoted
=
801 token
->count
.str
= token
->count
.vararg
= 0;
804 static struct token
*parse_arguments(struct token
*list
)
806 struct token
*arg
= list
->next
, *next
= list
;
807 struct argcount
*count
= &list
->count
;
811 if (match_op(arg
, ')')) {
813 list
->next
= &eof_token_entry
;
817 while (token_type(arg
) == TOKEN_IDENT
) {
818 if (arg
->ident
== &__VA_ARGS___ident
)
820 if (!++count
->normal
)
824 if (match_op(next
, ',')) {
830 if (match_op(next
, ')')) {
833 arg
->next
->next
= &eof_token_entry
;
837 /* normal cases are finished here */
839 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
840 if (match_op(next
->next
, ')')) {
842 next
->count
.vararg
= 1;
844 arg
->next
->next
= &eof_token_entry
;
852 if (eof_token(next
)) {
860 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
862 token_type(arg
) = TOKEN_IDENT
;
863 arg
->ident
= &__VA_ARGS___ident
;
864 if (!match_op(next
, ')'))
866 if (!++count
->normal
)
869 next
->count
.vararg
= 1;
871 arg
->next
->next
= &eof_token_entry
;
875 if (eof_token(arg
)) {
879 if (match_op(arg
, ','))
886 warning(arg
->pos
, "parameter name missing");
889 warning(arg
->pos
, "\"%s\" may not appear in macro parameter list",
893 warning(arg
->pos
, "missing ')' in macro parameter list");
896 warning(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
899 warning(arg
->pos
, "too many arguments in macro definition");
903 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
905 struct ident
*ident
= token
->ident
;
908 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
911 arglist
= arglist
->next
;
913 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
914 if (arglist
->ident
== ident
) {
915 struct argcount
*count
= &arglist
->next
->count
;
919 token_type(token
) = type
;
921 case TOKEN_MACRO_ARGUMENT
:
924 case TOKEN_QUOTED_ARGUMENT
:
931 return count
->vararg
? 2 : 1;
932 token_type(token
) = TOKEN_ERROR
;
939 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
941 struct token
*token
= expansion
;
943 struct token
*last
= NULL
;
945 if (match_op(token
, SPECIAL_HASHHASH
))
948 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
949 if (match_op(token
, '#')) {
951 struct token
*next
= token
->next
;
952 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
954 next
->pos
.whitespace
= token
->pos
.whitespace
;
957 token
->pos
.noexpand
= 1;
959 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
960 struct token
*next
= token
->next
;
961 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
962 token_type(token
) = TOKEN_CONCAT
;
966 if (arg
== 2 && last
&& match_op(last
, ',')) {
967 token_type(last
) = TOKEN_GNU_KLUDGE
;
970 } else if (match_op(next
, SPECIAL_HASHHASH
))
972 else if (match_op(next
, ','))
974 else if (eof_token(next
))
976 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
977 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
979 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
981 if (token_type(token
) == TOKEN_ERROR
)
985 token
= alloc_token(&expansion
->pos
);
986 token_type(token
) = TOKEN_UNTAINT
;
993 warning(token
->pos
, "'#' is not followed by a macro parameter");
997 warning(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1000 warning(token
->pos
, "too many instances of argument in body");
1004 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int weak
)
1006 struct token
*arglist
, *expansion
;
1007 struct token
*left
= token
->next
;
1011 if (token_type(left
) != TOKEN_IDENT
) {
1012 warning(token
->pos
, "expected identifier to 'define'");
1016 return free_preprocessor_line(token
);
1018 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1019 MARK_STREAM_NONCONST(token
->pos
);
1021 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1025 expansion
= left
->next
;
1026 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
1027 arglist
= expansion
;
1028 expansion
= parse_arguments(expansion
);
1033 expansion
= parse_expansion(expansion
, arglist
, name
);
1037 sym
= lookup_symbol(name
, NS_MACRO
);
1039 if (token_list_different(sym
->expansion
, expansion
) ||
1040 token_list_different(sym
->arglist
, arglist
)) {
1045 warning(left
->pos
, "preprocessor token %.*s redefined",
1046 name
->len
, name
->name
);
1047 info(sym
->pos
, "this was the original definition");
1048 sym
->expansion
= expansion
;
1049 sym
->arglist
= arglist
;
1053 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1054 bind_symbol(sym
, name
, NS_MACRO
);
1057 sym
->expansion
= expansion
;
1058 sym
->arglist
= arglist
;
1063 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1065 return do_handle_define(stream
, line
, token
, 0);
1068 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1070 return do_handle_define(stream
, line
, token
, 1);
1073 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1075 struct token
*left
= token
->next
;
1076 struct symbol
**sym
;
1078 if (token_type(left
) != TOKEN_IDENT
) {
1079 warning(token
->pos
, "expected identifier to 'undef'");
1083 return free_preprocessor_line(token
);
1085 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1086 MARK_STREAM_NONCONST(token
->pos
);
1088 sym
= &left
->ident
->symbols
;
1090 struct symbol
*t
= *sym
;
1091 if (t
->namespace == NS_MACRO
) {
1097 return free_preprocessor_line(token
);
1100 static int preprocessor_if(struct token
*token
, int true)
1102 if (if_nesting
== 0)
1103 unmatched_if
= token
;
1104 if (if_nesting
>= MAX_NEST
)
1105 error_die(token
->pos
, "Maximum preprocessor conditional level exhausted");
1106 elif_ignore
[if_nesting
] = (false_nesting
|| true) ? ELIF_IGNORE
: 0;
1107 if (false_nesting
|| !true) {
1109 return free_preprocessor_line(token
);
1112 return free_preprocessor_line(token
);
1115 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1117 return preprocessor_if(token
, token_defined(token
->next
));
1120 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1122 struct token
*next
= token
->next
;
1123 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1124 if (token_type(next
) == TOKEN_IDENT
&&
1125 (!stream
->protect
|| stream
->protect
== next
->ident
)) {
1126 stream
->constant
= CONSTANT_FILE_IFNDEF
;
1127 stream
->protect
= next
->ident
;
1129 MARK_STREAM_NONCONST(token
->pos
);
1131 return preprocessor_if(token
, !token_defined(next
));
1135 * Expression handling for #if and #elif; it differs from normal expansion
1136 * due to special treatment of "defined".
1138 static int expression_value(struct token
**where
)
1140 struct expression
*expr
;
1142 struct token
**list
= where
, **beginning
= NULL
;
1146 while (!eof_token(p
= scan_next(list
))) {
1149 if (token_type(p
) != TOKEN_IDENT
)
1151 if (p
->ident
== &defined_ident
) {
1156 if (!expand_one_symbol(list
))
1158 if (token_type(p
) != TOKEN_IDENT
)
1160 if (Wundefined_preprocessor
)
1161 warning(p
->pos
, "undefined preprocessor identifier '%s'", show_ident(p
->ident
));
1162 replace_with_integer(p
, 0);
1165 if (match_op(p
, '(')) {
1169 replace_with_defined(p
);
1174 if (token_type(p
) == TOKEN_IDENT
)
1178 replace_with_defined(p
);
1183 if (!match_op(p
, ')'))
1184 warning(p
->pos
, "missing ')' after \"defined\"");
1191 p
= constant_expression(*where
, &expr
);
1193 warning(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1194 value
= get_expression_value(expr
);
1198 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1202 value
= expression_value(&token
->next
);
1204 // This is an approximation. We really only need this if the
1205 // condition does depends on a pre-processor symbol. Note, that
1206 // the important #ifndef case has already changed ->constant.
1207 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1208 MARK_STREAM_NONCONST(token
->pos
);
1210 return preprocessor_if(token
, value
);
1213 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1215 if (stream
->nesting
== if_nesting
)
1216 MARK_STREAM_NONCONST(token
->pos
);
1218 if (stream
->nesting
> if_nesting
)
1219 warning(token
->pos
, "unmatched #elif within stream");
1221 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1222 warning(token
->pos
, "#elif after #else");
1224 if (false_nesting
) {
1225 /* If this whole if-thing is if'ed out, an elif cannot help */
1226 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1228 if (expression_value(&token
->next
)) {
1231 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1237 return free_preprocessor_line(token
);
1240 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1242 if (stream
->nesting
== if_nesting
)
1243 MARK_STREAM_NONCONST(token
->pos
);
1245 if (stream
->nesting
> if_nesting
)
1246 warning(token
->pos
, "unmatched #else within stream");
1248 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1249 warning(token
->pos
, "#else after #else");
1251 elif_ignore
[if_nesting
-1] |= ELIF_SEEN_ELSE
;
1253 if (false_nesting
) {
1254 /* If this whole if-thing is if'ed out, an else cannot help */
1255 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1259 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1264 return free_preprocessor_line(token
);
1267 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1269 if (stream
->constant
== CONSTANT_FILE_IFNDEF
&& stream
->nesting
== if_nesting
)
1270 stream
->constant
= CONSTANT_FILE_MAYBE
;
1272 if (stream
->nesting
> if_nesting
)
1273 warning(token
->pos
, "unmatched #endif in stream");
1278 return free_preprocessor_line(token
);
1281 static const char *show_token_sequence(struct token
*token
)
1283 static char buffer
[1024];
1289 while (!eof_token(token
)) {
1290 const char *val
= show_token(token
);
1291 int len
= strlen(val
);
1293 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1294 warning(token
->pos
, "too long token expansion");
1300 memcpy(ptr
, val
, len
);
1302 token
= token
->next
;
1303 whitespace
= token
->pos
.whitespace
;
1309 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1312 return free_preprocessor_line(token
);
1313 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1314 MARK_STREAM_NONCONST(token
->pos
);
1315 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1316 return free_preprocessor_line(token
);
1319 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1322 return free_preprocessor_line(token
);
1323 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1324 MARK_STREAM_NONCONST(token
->pos
);
1325 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1326 return free_preprocessor_line(token
);
1329 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1334 return free_preprocessor_line(token
);
1337 * Do we have any non-system includes?
1338 * Clear them out if so..
1340 stdinc
= gcc_includepath
- sys_includepath
;
1342 const char **src
= gcc_includepath
;
1343 const char **dst
= sys_includepath
;
1350 gcc_includepath
-= stdinc
;
1352 return free_preprocessor_line(token
);
1355 static void add_path_entry(struct token
*token
, const char *path
)
1360 /* Need one free entry.. */
1361 if (includepath
[INCLUDEPATHS
-2])
1362 error_die(token
->pos
, "too many include path entries");
1365 dst
= sys_includepath
;
1370 * Move them all up starting at "sys_includepath",
1371 * insert the new entry..
1374 const char *tmp
= *dst
;
1383 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1386 token
= token
->next
;
1387 if (eof_token(token
))
1389 if (token_type(token
) != TOKEN_STRING
) {
1390 warning(token
->pos
, "expected path string");
1393 add_path_entry(token
, token
->string
->data
);
1398 * We replace "#pragma xxx" with "__pragma__" in the token
1399 * stream. Just as an example.
1401 * We'll just #define that away for now, but the theory here
1402 * is that we can use this to insert arbitrary token sequences
1403 * to turn the pragma's into internal front-end sequences for
1404 * when we actually start caring about them.
1406 * So eventually this will turn into some kind of extended
1407 * __attribute__() like thing, except called __pragma__(xxx).
1409 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1411 struct token
*next
= *line
;
1413 token
->ident
= &pragma_ident
;
1414 token
->pos
.newline
= 1;
1415 token
->pos
.whitespace
= 1;
1423 * We ignore #line for now.
1425 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1431 void init_preprocessor(void)
1434 int stream
= init_stream("preprocessor", -1, includepath
);
1437 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1439 { "define", handle_define
},
1440 { "weak_define",handle_weak_define
},
1441 { "undef", handle_undef
},
1442 { "ifdef", handle_ifdef
},
1443 { "ifndef", handle_ifndef
},
1444 { "else", handle_else
},
1445 { "endif", handle_endif
},
1446 { "if", handle_if
},
1447 { "elif", handle_elif
},
1448 { "warning", handle_warning
},
1449 { "error", handle_error
},
1450 { "include", handle_include
},
1451 { "include_next",handle_include_next
},
1452 { "pragma", handle_pragma
},
1453 { "line", handle_line
},
1455 // our internal preprocessor tokens
1456 { "nostdinc", handle_nostdinc
},
1457 { "add_include", handle_add_include
},
1460 for (i
= 0; i
< (sizeof (handlers
) / sizeof (handlers
[0])); i
++) {
1462 sym
= create_symbol(stream
, handlers
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1463 sym
->handler
= handlers
[i
].handler
;
1467 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1469 struct token
*token
= start
->next
;
1474 if (token_type(token
) == TOKEN_NUMBER
)
1475 if (handle_line(stream
, line
, start
))
1478 if (token_type(token
) == TOKEN_IDENT
) {
1479 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1480 if (sym
&& sym
->handler(stream
, line
, token
))
1484 warning(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1487 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1489 struct token
*start
= *line
, *next
;
1490 struct token
**tp
= &start
->next
;
1494 if (next
->pos
.newline
)
1499 *tp
= &eof_token_entry
;
1500 handle_preprocessor_line(stream
, line
, start
);
1503 static void do_preprocess(struct token
**list
)
1507 while (!eof_token(next
= scan_next(list
))) {
1508 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1510 if (next
->pos
.newline
&& match_op(next
, '#')) {
1511 if (!next
->pos
.noexpand
) {
1512 preprocessor_line(stream
, list
);
1513 __free_token(next
); /* Free the '#' token */
1518 switch (token_type(next
)) {
1519 case TOKEN_STREAMEND
:
1520 if (stream
->nesting
< if_nesting
+ 1) {
1521 warning(unmatched_if
->pos
, "unterminated preprocessor conditional");
1522 // Pretend to see a series of #endifs
1523 MARK_STREAM_NONCONST(next
->pos
);
1525 handle_endif (stream
, NULL
, NULL
);
1526 } while (stream
->nesting
< if_nesting
+ 1);
1528 if (stream
->constant
== CONSTANT_FILE_MAYBE
&& stream
->protect
) {
1529 stream
->constant
= CONSTANT_FILE_YES
;
1533 case TOKEN_STREAMBEGIN
:
1534 stream
->nesting
= if_nesting
+ 1;
1539 if (false_nesting
) {
1545 if (token_type(next
) != TOKEN_IDENT
||
1546 expand_one_symbol(list
))
1550 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1552 * Any token expansion (even if it ended up being an
1553 * empty expansion) in this stream implies it can't
1556 MARK_STREAM_NONCONST(next
->pos
);
1561 struct token
* preprocess(struct token
*token
)
1564 init_preprocessor();
1565 do_preprocess(&token
);
1567 // Drop all expressions from pre-processing, they're not used any more.
1568 clear_expression_alloc();