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
, (input_streams
+ token
->pos
.stream
)->name
);
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 try_include(const char *path
, int plen
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
602 static char fullname
[PATH_MAX
];
604 memcpy(fullname
, path
, plen
);
605 if (plen
&& path
[plen
-1] != '/') {
606 fullname
[plen
] = '/';
609 memcpy(fullname
+plen
, filename
, flen
);
610 fd
= open(fullname
, O_RDONLY
);
612 char * streamname
= __alloc_bytes(plen
+ flen
);
613 memcpy(streamname
, fullname
, plen
+ flen
);
614 *where
= tokenize(streamname
, fd
, *where
, next_path
);
621 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
625 while ((path
= *pptr
++) != NULL
) {
626 if (!try_include(path
, strlen(path
), filename
, flen
, list
, pptr
))
634 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
636 int flen
= strlen(filename
) + 1;
639 if (filename
[0] == '/') {
640 if (try_include("", 0, filename
, flen
, list
, includepath
))
645 /* Same directory as current stream? */
652 slash
= strrchr(path
, '/');
653 plen
= slash
? slash
- path
: 0;
655 if (try_include(path
, plen
, filename
, flen
, list
, includepath
))
659 /* Check the standard include paths.. */
660 if (do_include_path(path
, list
, token
, filename
, flen
))
663 error_die(token
->pos
, "unable to open '%s'", filename
);
666 static int free_preprocessor_line(struct token
*token
)
669 struct token
*free
= token
;
672 } while (token_type(token
) != TOKEN_EOF
);
676 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
678 const char *filename
;
683 return free_preprocessor_line(token
);
685 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
686 MARK_STREAM_NONCONST(token
->pos
);
690 if (!match_op(next
, '<')) {
691 expand_list(&token
->next
);
694 if (match_op(token
->next
, '<')) {
700 filename
= token_name_sequence(token
, expect
, token
);
701 do_include(!expect
, stream
, list
, token
, filename
, path
);
705 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
707 return handle_include_path(stream
, list
, token
, includepath
);
710 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
712 return handle_include_path(stream
, list
, token
, stream
->next_path
);
715 static int token_different(struct token
*t1
, struct token
*t2
)
719 if (token_type(t1
) != token_type(t2
))
722 switch (token_type(t1
)) {
724 different
= t1
->ident
!= t2
->ident
;
726 case TOKEN_ARG_COUNT
:
729 case TOKEN_GNU_KLUDGE
:
733 different
= strcmp(t1
->number
, t2
->number
);
736 different
= t1
->special
!= t2
->special
;
738 case TOKEN_MACRO_ARGUMENT
:
739 case TOKEN_QUOTED_ARGUMENT
:
740 case TOKEN_STR_ARGUMENT
:
741 different
= t1
->argnum
!= t2
->argnum
;
744 different
= t1
->character
!= t2
->character
;
747 struct string
*s1
, *s2
;
752 if (s1
->length
!= s2
->length
)
754 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
764 static int token_list_different(struct token
*list1
, struct token
*list2
)
769 if (!list1
|| !list2
)
771 if (token_different(list1
, list2
))
778 static inline void set_arg_count(struct token
*token
)
780 token_type(token
) = TOKEN_ARG_COUNT
;
781 token
->count
.normal
= token
->count
.quoted
=
782 token
->count
.str
= token
->count
.vararg
= 0;
785 static struct token
*parse_arguments(struct token
*list
)
787 struct token
*arg
= list
->next
, *next
= list
;
788 struct argcount
*count
= &list
->count
;
792 if (match_op(arg
, ')')) {
794 list
->next
= &eof_token_entry
;
798 while (token_type(arg
) == TOKEN_IDENT
) {
799 if (arg
->ident
== &__VA_ARGS___ident
)
801 if (!++count
->normal
)
805 if (match_op(next
, ',')) {
811 if (match_op(next
, ')')) {
814 arg
->next
->next
= &eof_token_entry
;
818 /* normal cases are finished here */
820 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
821 if (match_op(next
->next
, ')')) {
823 next
->count
.vararg
= 1;
825 arg
->next
->next
= &eof_token_entry
;
833 if (eof_token(next
)) {
841 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
843 token_type(arg
) = TOKEN_IDENT
;
844 arg
->ident
= &__VA_ARGS___ident
;
845 if (!match_op(next
, ')'))
847 if (!++count
->normal
)
850 next
->count
.vararg
= 1;
852 arg
->next
->next
= &eof_token_entry
;
856 if (eof_token(arg
)) {
860 if (match_op(arg
, ','))
867 warning(arg
->pos
, "parameter name missing");
870 warning(arg
->pos
, "\"%s\" may not appear in macro parameter list",
874 warning(arg
->pos
, "missing ')' in macro parameter list");
877 warning(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
880 warning(arg
->pos
, "too many arguments in macro definition");
884 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
886 struct ident
*ident
= token
->ident
;
889 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
892 arglist
= arglist
->next
;
894 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
895 if (arglist
->ident
== ident
) {
896 struct argcount
*count
= &arglist
->next
->count
;
900 token_type(token
) = type
;
902 case TOKEN_MACRO_ARGUMENT
:
905 case TOKEN_QUOTED_ARGUMENT
:
912 return count
->vararg
? 2 : 1;
913 token_type(token
) = TOKEN_ERROR
;
920 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
922 struct token
*token
= expansion
;
924 struct token
*last
= NULL
;
926 if (match_op(token
, SPECIAL_HASHHASH
))
929 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
930 if (match_op(token
, '#')) {
932 struct token
*next
= token
->next
;
933 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
935 next
->pos
.whitespace
= token
->pos
.whitespace
;
938 token
->pos
.noexpand
= 1;
940 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
941 struct token
*next
= token
->next
;
942 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
943 token_type(token
) = TOKEN_CONCAT
;
947 if (arg
== 2 && last
&& match_op(last
, ',')) {
948 token_type(last
) = TOKEN_GNU_KLUDGE
;
951 } else if (match_op(next
, SPECIAL_HASHHASH
))
953 else if (match_op(next
, ','))
955 else if (eof_token(next
))
957 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
958 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
960 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
962 if (token_type(token
) == TOKEN_ERROR
)
966 token
= alloc_token(&expansion
->pos
);
967 token_type(token
) = TOKEN_UNTAINT
;
974 warning(token
->pos
, "'#' is not followed by a macro parameter");
978 warning(token
->pos
, "'##' cannot appear at the ends of macro expansion");
981 warning(token
->pos
, "too many instances of argument in body");
985 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int weak
)
987 struct token
*arglist
, *expansion
;
988 struct token
*left
= token
->next
;
992 if (token_type(left
) != TOKEN_IDENT
) {
993 warning(token
->pos
, "expected identifier to 'define'");
997 return free_preprocessor_line(token
);
999 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1000 MARK_STREAM_NONCONST(token
->pos
);
1002 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1006 expansion
= left
->next
;
1007 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
1008 arglist
= expansion
;
1009 expansion
= parse_arguments(expansion
);
1014 expansion
= parse_expansion(expansion
, arglist
, name
);
1018 sym
= lookup_symbol(name
, NS_MACRO
);
1020 if (token_list_different(sym
->expansion
, expansion
) ||
1021 token_list_different(sym
->arglist
, arglist
)) {
1026 warning(left
->pos
, "preprocessor token %.*s redefined",
1027 name
->len
, name
->name
);
1028 info(sym
->pos
, "this was the original definition");
1029 sym
->expansion
= expansion
;
1030 sym
->arglist
= arglist
;
1034 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1035 bind_symbol(sym
, name
, NS_MACRO
);
1038 sym
->expansion
= expansion
;
1039 sym
->arglist
= arglist
;
1044 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1046 return do_handle_define(stream
, line
, token
, 0);
1049 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1051 return do_handle_define(stream
, line
, token
, 1);
1054 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1056 struct token
*left
= token
->next
;
1057 struct symbol
**sym
;
1059 if (token_type(left
) != TOKEN_IDENT
) {
1060 warning(token
->pos
, "expected identifier to 'undef'");
1064 return free_preprocessor_line(token
);
1066 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1067 MARK_STREAM_NONCONST(token
->pos
);
1069 sym
= &left
->ident
->symbols
;
1071 struct symbol
*t
= *sym
;
1072 if (t
->namespace == NS_MACRO
) {
1078 return free_preprocessor_line(token
);
1081 static int preprocessor_if(struct token
*token
, int true)
1083 if (if_nesting
== 0)
1084 unmatched_if
= token
;
1085 if (if_nesting
>= MAX_NEST
)
1086 error_die(token
->pos
, "Maximum preprocessor conditional level exhausted");
1087 elif_ignore
[if_nesting
] = (false_nesting
|| true) ? ELIF_IGNORE
: 0;
1088 if (false_nesting
|| !true) {
1090 return free_preprocessor_line(token
);
1093 return free_preprocessor_line(token
);
1096 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1098 return preprocessor_if(token
, token_defined(token
->next
));
1101 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1103 struct token
*next
= token
->next
;
1104 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1105 if (token_type(next
) == TOKEN_IDENT
&&
1106 (!stream
->protect
|| stream
->protect
== next
->ident
)) {
1107 stream
->constant
= CONSTANT_FILE_IFNDEF
;
1108 stream
->protect
= next
->ident
;
1110 MARK_STREAM_NONCONST(token
->pos
);
1112 return preprocessor_if(token
, !token_defined(next
));
1116 * Expression handling for #if and #elif; it differs from normal expansion
1117 * due to special treatment of "defined".
1119 static int expression_value(struct token
**where
)
1121 struct expression
*expr
;
1123 struct token
**list
= where
, **beginning
= NULL
;
1127 while (!eof_token(p
= scan_next(list
))) {
1130 if (token_type(p
) != TOKEN_IDENT
)
1132 if (p
->ident
== &defined_ident
) {
1137 if (!expand_one_symbol(list
))
1139 if (token_type(p
) != TOKEN_IDENT
)
1141 if (Wundefined_preprocessor
)
1142 warning(p
->pos
, "undefined preprocessor identifier '%s'", show_ident(p
->ident
));
1143 replace_with_integer(p
, 0);
1146 if (match_op(p
, '(')) {
1150 replace_with_defined(p
);
1155 if (token_type(p
) == TOKEN_IDENT
)
1159 replace_with_defined(p
);
1164 if (!match_op(p
, ')'))
1165 warning(p
->pos
, "missing ')' after \"defined\"");
1172 p
= constant_expression(*where
, &expr
);
1174 warning(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1175 value
= get_expression_value(expr
);
1179 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1183 value
= expression_value(&token
->next
);
1185 // This is an approximation. We really only need this if the
1186 // condition does depends on a pre-processor symbol. Note, that
1187 // the important #ifndef case has already changed ->constant.
1188 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1189 MARK_STREAM_NONCONST(token
->pos
);
1191 return preprocessor_if(token
, value
);
1194 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1196 if (stream
->nesting
== if_nesting
)
1197 MARK_STREAM_NONCONST(token
->pos
);
1199 if (stream
->nesting
> if_nesting
)
1200 warning(token
->pos
, "unmatched #elif within stream");
1202 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1203 warning(token
->pos
, "#elif after #else");
1205 if (false_nesting
) {
1206 /* If this whole if-thing is if'ed out, an elif cannot help */
1207 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1209 if (expression_value(&token
->next
)) {
1212 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1218 return free_preprocessor_line(token
);
1221 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1223 if (stream
->nesting
== if_nesting
)
1224 MARK_STREAM_NONCONST(token
->pos
);
1226 if (stream
->nesting
> if_nesting
)
1227 warning(token
->pos
, "unmatched #else within stream");
1229 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1230 warning(token
->pos
, "#else after #else");
1232 elif_ignore
[if_nesting
-1] |= ELIF_SEEN_ELSE
;
1234 if (false_nesting
) {
1235 /* If this whole if-thing is if'ed out, an else cannot help */
1236 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1240 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1245 return free_preprocessor_line(token
);
1248 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1250 if (stream
->constant
== CONSTANT_FILE_IFNDEF
&& stream
->nesting
== if_nesting
)
1251 stream
->constant
= CONSTANT_FILE_MAYBE
;
1253 if (stream
->nesting
> if_nesting
)
1254 warning(token
->pos
, "unmatched #endif in stream");
1259 return free_preprocessor_line(token
);
1262 static const char *show_token_sequence(struct token
*token
)
1264 static char buffer
[1024];
1270 while (!eof_token(token
)) {
1271 const char *val
= show_token(token
);
1272 int len
= strlen(val
);
1274 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1275 warning(token
->pos
, "too long token expansion");
1281 memcpy(ptr
, val
, len
);
1283 token
= token
->next
;
1284 whitespace
= token
->pos
.whitespace
;
1290 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1293 return free_preprocessor_line(token
);
1294 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1295 MARK_STREAM_NONCONST(token
->pos
);
1296 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1297 return free_preprocessor_line(token
);
1300 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1303 return free_preprocessor_line(token
);
1304 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1305 MARK_STREAM_NONCONST(token
->pos
);
1306 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1307 return free_preprocessor_line(token
);
1310 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1315 return free_preprocessor_line(token
);
1318 * Do we have any non-system includes?
1319 * Clear them out if so..
1321 stdinc
= gcc_includepath
- sys_includepath
;
1323 const char **src
= gcc_includepath
;
1324 const char **dst
= sys_includepath
;
1331 gcc_includepath
-= stdinc
;
1333 return free_preprocessor_line(token
);
1336 static void add_path_entry(struct token
*token
, const char *path
)
1341 /* Need one free entry.. */
1342 if (includepath
[INCLUDEPATHS
-2])
1343 error_die(token
->pos
, "too many include path entries");
1346 dst
= sys_includepath
;
1351 * Move them all up starting at "sys_includepath",
1352 * insert the new entry..
1355 const char *tmp
= *dst
;
1364 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1367 token
= token
->next
;
1368 if (eof_token(token
))
1370 if (token_type(token
) != TOKEN_STRING
) {
1371 warning(token
->pos
, "expected path string");
1374 add_path_entry(token
, token
->string
->data
);
1379 * We replace "#pragma xxx" with "__pragma__" in the token
1380 * stream. Just as an example.
1382 * We'll just #define that away for now, but the theory here
1383 * is that we can use this to insert arbitrary token sequences
1384 * to turn the pragma's into internal front-end sequences for
1385 * when we actually start caring about them.
1387 * So eventually this will turn into some kind of extended
1388 * __attribute__() like thing, except called __pragma__(xxx).
1390 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1392 struct token
*next
= *line
;
1394 token
->ident
= &pragma_ident
;
1395 token
->pos
.newline
= 1;
1396 token
->pos
.whitespace
= 1;
1404 * We ignore #line for now.
1406 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1412 void init_preprocessor(void)
1415 int stream
= init_stream("preprocessor", -1, includepath
);
1418 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1420 { "define", handle_define
},
1421 { "weak_define",handle_weak_define
},
1422 { "undef", handle_undef
},
1423 { "ifdef", handle_ifdef
},
1424 { "ifndef", handle_ifndef
},
1425 { "else", handle_else
},
1426 { "endif", handle_endif
},
1427 { "if", handle_if
},
1428 { "elif", handle_elif
},
1429 { "warning", handle_warning
},
1430 { "error", handle_error
},
1431 { "include", handle_include
},
1432 { "include_next",handle_include_next
},
1433 { "pragma", handle_pragma
},
1434 { "line", handle_line
},
1436 // our internal preprocessor tokens
1437 { "nostdinc", handle_nostdinc
},
1438 { "add_include", handle_add_include
},
1441 for (i
= 0; i
< (sizeof (handlers
) / sizeof (handlers
[0])); i
++) {
1443 sym
= create_symbol(stream
, handlers
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1444 sym
->handler
= handlers
[i
].handler
;
1448 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1450 struct token
*token
= start
->next
;
1455 if (token_type(token
) == TOKEN_NUMBER
)
1456 if (handle_line(stream
, line
, start
))
1459 if (token_type(token
) == TOKEN_IDENT
) {
1460 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1461 if (sym
&& sym
->handler(stream
, line
, token
))
1465 warning(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1468 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1470 struct token
*start
= *line
, *next
;
1471 struct token
**tp
= &start
->next
;
1475 if (next
->pos
.newline
)
1480 *tp
= &eof_token_entry
;
1481 handle_preprocessor_line(stream
, line
, start
);
1484 static void do_preprocess(struct token
**list
)
1488 while (!eof_token(next
= scan_next(list
))) {
1489 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1491 if (next
->pos
.newline
&& match_op(next
, '#')) {
1492 if (!next
->pos
.noexpand
) {
1493 preprocessor_line(stream
, list
);
1494 __free_token(next
); /* Free the '#' token */
1499 switch (token_type(next
)) {
1500 case TOKEN_STREAMEND
:
1501 if (stream
->nesting
< if_nesting
+ 1) {
1502 warning(unmatched_if
->pos
, "unterminated preprocessor conditional");
1503 // Pretend to see a series of #endifs
1504 MARK_STREAM_NONCONST(next
->pos
);
1506 handle_endif (stream
, NULL
, NULL
);
1507 } while (stream
->nesting
< if_nesting
+ 1);
1509 if (stream
->constant
== CONSTANT_FILE_MAYBE
&& stream
->protect
) {
1510 stream
->constant
= CONSTANT_FILE_YES
;
1514 case TOKEN_STREAMBEGIN
:
1515 stream
->nesting
= if_nesting
+ 1;
1520 if (false_nesting
) {
1526 if (token_type(next
) != TOKEN_IDENT
||
1527 expand_one_symbol(list
))
1531 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1533 * Any token expansion (even if it ended up being an
1534 * empty expansion) in this stream implies it can't
1537 MARK_STREAM_NONCONST(next
->pos
);
1542 struct token
* preprocess(struct token
*token
)
1545 init_preprocessor();
1546 do_preprocess(&token
);
1548 // Drop all expressions from pre-processing, they're not used any more.
1549 clear_expression_alloc();