2 * Do C preprocessing, based on a token list gathered by
5 * This may not be the smartest preprocessor on the planet.
7 * Copyright (C) 2003 Transmeta Corp.
10 * Licensed under the Open Software License version 1.1
22 #include "pre-process.h"
27 #include "expression.h"
30 int preprocessing
= 0;
32 static int true_nesting
= 0;
33 static int false_nesting
= 0;
34 static struct token
*unmatched_if
= NULL
;
35 #define if_nesting (true_nesting + false_nesting)
37 #define MAX_NEST (256)
38 static unsigned char elif_ignore
[MAX_NEST
];
39 enum { ELIF_IGNORE
= 1, ELIF_SEEN_ELSE
= 2 };
41 #define INCLUDEPATHS 300
42 const char *includepath
[INCLUDEPATHS
+1] = {
49 static const char **sys_includepath
= includepath
+ 0;
50 static const char **gcc_includepath
= includepath
+ 2;
52 #define MARK_STREAM_NONCONST(pos) do { \
53 if (stream->constant != CONSTANT_FILE_NOPE) { \
55 info(pos, "%s triggers non-const", __func__); \
56 stream->constant = CONSTANT_FILE_NOPE; \
61 static struct token
*alloc_token(struct position
*pos
)
63 struct token
*token
= __alloc_token(0);
65 token
->pos
.stream
= pos
->stream
;
66 token
->pos
.line
= pos
->line
;
67 token
->pos
.pos
= pos
->pos
;
68 token
->pos
.whitespace
= 1;
72 static const char *show_token_sequence(struct token
*token
);
74 /* Expand symbol 'sym' at '*list' */
75 static int expand(struct token
**, struct symbol
*);
77 static void replace_with_string(struct token
*token
, const char *str
)
79 int size
= strlen(str
) + 1;
80 struct string
*s
= __alloc_string(size
);
83 memcpy(s
->data
, str
, size
);
84 token_type(token
) = TOKEN_STRING
;
88 static void replace_with_integer(struct token
*token
, unsigned int val
)
90 char *buf
= __alloc_bytes(11);
91 sprintf(buf
, "%u", val
);
92 token_type(token
) = TOKEN_NUMBER
;
96 static void replace_with_defined(struct token
*token
)
98 static const char *string
[] = { "0", "1" };
100 if (token_type(token
) != TOKEN_IDENT
)
101 warning(token
->pos
, "operator \"defined\" requires an identifier");
102 else if (lookup_symbol(token
->ident
, NS_MACRO
))
104 token_type(token
) = TOKEN_NUMBER
;
105 token
->number
= string
[defined
];
108 static int expand_one_symbol(struct token
**list
)
110 struct token
*token
= *list
;
113 if (token
->pos
.noexpand
)
116 sym
= lookup_symbol(token
->ident
, NS_MACRO
);
118 return expand(list
, sym
);
119 if (token
->ident
== &__LINE___ident
) {
120 replace_with_integer(token
, token
->pos
.line
);
121 } else if (token
->ident
== &__FILE___ident
) {
122 replace_with_string(token
, (input_streams
+ token
->pos
.stream
)->name
);
127 static inline struct token
*scan_next(struct token
**where
)
129 struct token
*token
= *where
;
130 if (token_type(token
) != TOKEN_UNTAINT
)
133 token
->ident
->tainted
= 0;
135 } while (token_type(token
) == TOKEN_UNTAINT
);
140 static void expand_list(struct token
**list
)
143 while (!eof_token(next
= scan_next(list
))) {
144 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
149 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
151 struct token
**p
= &prev
->next
;
155 while (!eof_token(next
= scan_next(p
))) {
156 if (match_op(next
, '(')) {
158 } else if (match_op(next
, ')')) {
161 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
164 next
->pos
.stream
= pos
->stream
;
165 next
->pos
.line
= pos
->line
;
166 next
->pos
.pos
= pos
->pos
;
169 *p
= &eof_token_entry
;
174 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
179 struct token
*expanded
;
186 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
188 int wanted
= arglist
->count
.normal
;
189 struct token
*next
= NULL
;
192 arglist
= arglist
->next
; /* skip counter */
195 next
= collect_arg(start
, 0, &what
->pos
);
198 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
203 for (count
= 0; count
< wanted
; count
++) {
204 struct argcount
*p
= &arglist
->next
->count
;
205 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
206 arglist
= arglist
->next
->next
;
209 args
[count
].arg
= start
->next
;
210 args
[count
].n_normal
= p
->normal
;
211 args
[count
].n_quoted
= p
->quoted
;
212 args
[count
].n_str
= p
->str
;
213 if (match_op(next
, ')')) {
219 if (count
== wanted
&& !match_op(next
, ')'))
221 if (count
== wanted
- 1) {
222 struct argcount
*p
= &arglist
->next
->count
;
225 args
[count
].arg
= NULL
;
226 args
[count
].n_normal
= p
->normal
;
227 args
[count
].n_quoted
= p
->quoted
;
228 args
[count
].n_str
= p
->str
;
230 if (count
< wanted
- 1)
233 what
->next
= next
->next
;
237 warning(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
238 show_token(what
), wanted
, count
);
241 while (match_op(next
, ',')) {
242 next
= collect_arg(next
, 0, &what
->pos
);
247 warning(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
248 show_token(what
), count
, wanted
);
251 warning(what
->pos
, "unterminated argument list invoking macro \"%s\"",
254 what
->next
= next
->next
;
258 static struct token
*dup_list(struct token
*list
)
261 struct token
**p
= &res
;
263 while (!eof_token(list
)) {
264 struct token
*newtok
= __alloc_token(0);
273 static struct token
*stringify(struct token
*arg
)
275 const char *s
= show_token_sequence(arg
);
276 int size
= strlen(s
)+1;
277 struct token
*token
= __alloc_token(0);
278 struct string
*string
= __alloc_string(size
);
280 memcpy(string
->data
, s
, size
);
281 string
->length
= size
;
282 token
->pos
= arg
->pos
;
283 token_type(token
) = TOKEN_STRING
;
284 token
->string
= string
;
285 token
->next
= &eof_token_entry
;
289 static void expand_arguments(int count
, struct arg
*args
)
292 for (i
= 0; i
< count
; i
++) {
293 struct token
*arg
= args
[i
].arg
;
295 arg
= &eof_token_entry
;
297 args
[i
].str
= stringify(arg
);
298 if (args
[i
].n_normal
) {
299 if (!args
[i
].n_quoted
) {
300 args
[i
].expanded
= arg
;
302 } else if (eof_token(arg
)) {
303 args
[i
].expanded
= arg
;
305 args
[i
].expanded
= dup_list(arg
);
307 expand_list(&args
[i
].expanded
);
313 * Possibly valid combinations:
314 * - ident + ident -> ident
315 * - ident + number -> ident unless number contains '.', '+' or '-'.
316 * - number + number -> number
317 * - number + ident -> number
318 * - number + '.' -> number
319 * - number + '+' or '-' -> number, if number used to end on [eEpP].
320 * - '.' + number -> number, if number used to start with a digit.
321 * - special + special -> either special or an error.
323 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
326 enum token_type t1
= token_type(left
), t2
= token_type(right
);
328 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
331 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
334 strcpy(p
, show_token(left
));
335 strcat(p
, show_token(right
));
341 if (t1
== TOKEN_IDENT
) {
342 if (t2
== TOKEN_SPECIAL
)
344 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
349 if (t1
== TOKEN_NUMBER
) {
350 if (t2
== TOKEN_SPECIAL
) {
351 switch (right
->special
) {
355 if (strchr("eEpP", p
[len
- 2]))
364 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
367 return TOKEN_SPECIAL
;
370 static int merge(struct token
*left
, struct token
*right
)
372 extern unsigned char combinations
[][3];
373 static char buffer
[512];
376 switch (combine(left
, right
, buffer
)) {
378 left
->ident
= built_in_ident(buffer
);
379 left
->pos
.noexpand
= 0;
383 char *number
= __alloc_bytes(strlen(buffer
) + 1);
384 memcpy(number
, buffer
, strlen(buffer
) + 1);
385 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
386 left
->number
= number
;
391 if (buffer
[2] && buffer
[3])
393 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
394 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
402 warning(left
->pos
, "'##' failed: concatenation is not a valid token");
406 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
408 struct token
*alloc
= alloc_token(streampos
);
409 token_type(alloc
) = token_type(token
);
410 alloc
->pos
.newline
= pos
->newline
;
411 alloc
->pos
.whitespace
= pos
->whitespace
;
412 alloc
->number
= token
->number
;
413 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
417 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
419 int need_copy
= --*count
;
420 while (!eof_token(list
)) {
423 token
= dup_token(list
, &list
->pos
, &list
->pos
);
426 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
427 token
->pos
.noexpand
= 1;
429 where
= &token
->next
;
432 *where
= &eof_token_entry
;
436 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
438 struct token
*token
= *list
;
439 struct position
*base_pos
= &token
->pos
;
440 struct position
*pos
= base_pos
;
442 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
444 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
445 struct token
*added
, *arg
;
448 switch (token_type(body
)) {
449 case TOKEN_GNU_KLUDGE
:
451 * GNU kludge: if we had <comma>##<vararg>, behaviour
452 * depends on whether we had enough arguments to have
453 * a vararg. If we did, ## is just ignored. Otherwise
454 * both , and ## are ignored. Comma should come from
455 * the body of macro and not be an argument of earlier
458 if (!args
[body
->next
->argnum
].arg
)
460 added
= dup_token(body
, base_pos
, pos
);
461 token_type(added
) = TOKEN_SPECIAL
;
465 case TOKEN_STR_ARGUMENT
:
466 arg
= args
[body
->argnum
].str
;
467 count
= &args
[body
->argnum
].n_str
;
470 case TOKEN_QUOTED_ARGUMENT
:
471 arg
= args
[body
->argnum
].arg
;
472 count
= &args
[body
->argnum
].n_quoted
;
473 if (!arg
|| eof_token(arg
)) {
482 case TOKEN_MACRO_ARGUMENT
:
483 arg
= args
[body
->argnum
].expanded
;
484 count
= &args
[body
->argnum
].n_normal
;
485 if (eof_token(arg
)) {
490 tail
= copy(&added
, arg
, count
);
491 added
->pos
.newline
= pos
->newline
;
492 added
->pos
.whitespace
= pos
->whitespace
;
496 if (state
== Placeholder
)
503 added
= dup_token(body
, base_pos
, pos
);
504 if (added
->ident
->tainted
)
505 added
->pos
.noexpand
= 1;
510 added
= dup_token(body
, base_pos
, pos
);
516 * if we got to doing real concatenation, we already have
517 * added something into the list, so containing_token() is OK.
519 if (state
== Concat
&& merge(containing_token(list
), added
)) {
521 if (tail
!= &added
->next
)
529 *list
= &eof_token_entry
;
533 static int expand(struct token
**list
, struct symbol
*sym
)
536 struct token
*token
= *list
;
537 struct ident
*expanding
= token
->ident
;
539 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
540 struct arg args
[nargs
];
542 if (expanding
->tainted
) {
543 token
->pos
.noexpand
= 1;
548 if (!match_op(scan_next(&token
->next
), '('))
550 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
552 expand_arguments(nargs
, args
);
555 expanding
->tainted
= 1;
558 tail
= substitute(list
, sym
->expansion
, args
);
564 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
567 static char buffer
[256];
571 while (!eof_token(token
) && !match_op(token
, endop
)) {
573 const char *val
= token
->string
->data
;
574 if (token_type(token
) != TOKEN_STRING
)
575 val
= show_token(token
);
577 memcpy(ptr
, val
, len
);
582 if (endop
&& !match_op(token
, endop
))
583 warning(start
->pos
, "expected '>' at end of filename");
587 static int try_include(const char *path
, int plen
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
590 static char fullname
[PATH_MAX
];
592 memcpy(fullname
, path
, plen
);
593 if (plen
&& path
[plen
-1] != '/') {
594 fullname
[plen
] = '/';
597 memcpy(fullname
+plen
, filename
, flen
);
598 fd
= open(fullname
, O_RDONLY
);
600 char * streamname
= __alloc_bytes(plen
+ flen
);
601 memcpy(streamname
, fullname
, plen
+ flen
);
602 *where
= tokenize(streamname
, fd
, *where
, next_path
);
609 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
613 while ((path
= *pptr
++) != NULL
) {
614 if (!try_include(path
, strlen(path
), filename
, flen
, list
, pptr
))
622 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
624 int flen
= strlen(filename
) + 1;
627 if (filename
[0] == '/') {
628 if (try_include("", 0, filename
, flen
, list
, includepath
))
633 /* Same directory as current stream? */
640 slash
= strrchr(path
, '/');
641 plen
= slash
? slash
- path
: 0;
643 if (try_include(path
, plen
, filename
, flen
, list
, includepath
))
647 /* Check the standard include paths.. */
648 if (do_include_path(path
, list
, token
, filename
, flen
))
651 error_die(token
->pos
, "unable to open '%s'", filename
);
654 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
656 const char *filename
;
663 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
664 MARK_STREAM_NONCONST(token
->pos
);
668 if (!match_op(next
, '<')) {
669 expand_list(&token
->next
);
672 if (match_op(token
->next
, '<')) {
678 filename
= token_name_sequence(token
, expect
, token
);
679 do_include(!expect
, stream
, list
, token
, filename
, path
);
683 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
685 return handle_include_path(stream
, list
, token
, includepath
);
688 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
690 return handle_include_path(stream
, list
, token
, stream
->next_path
);
693 static int token_different(struct token
*t1
, struct token
*t2
)
697 if (token_type(t1
) != token_type(t2
))
700 switch (token_type(t1
)) {
702 different
= t1
->ident
!= t2
->ident
;
704 case TOKEN_ARG_COUNT
:
707 case TOKEN_GNU_KLUDGE
:
711 different
= strcmp(t1
->number
, t2
->number
);
714 different
= t1
->special
!= t2
->special
;
716 case TOKEN_MACRO_ARGUMENT
:
717 case TOKEN_QUOTED_ARGUMENT
:
718 case TOKEN_STR_ARGUMENT
:
719 different
= t1
->argnum
!= t2
->argnum
;
722 different
= t1
->character
!= t2
->character
;
725 struct string
*s1
, *s2
;
730 if (s1
->length
!= s2
->length
)
732 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
742 static int token_list_different(struct token
*list1
, struct token
*list2
)
747 if (!list1
|| !list2
)
749 if (token_different(list1
, list2
))
756 static inline void set_arg_count(struct token
*token
)
758 token_type(token
) = TOKEN_ARG_COUNT
;
759 token
->count
.normal
= token
->count
.quoted
=
760 token
->count
.str
= token
->count
.vararg
= 0;
763 static struct token
*parse_arguments(struct token
*list
)
765 struct token
*arg
= list
->next
, *next
= list
;
766 struct argcount
*count
= &list
->count
;
770 if (match_op(arg
, ')')) {
772 list
->next
= &eof_token_entry
;
776 while (token_type(arg
) == TOKEN_IDENT
) {
777 if (arg
->ident
== &__VA_ARGS___ident
)
779 if (!++count
->normal
)
783 if (match_op(next
, ',')) {
789 if (match_op(next
, ')')) {
792 arg
->next
->next
= &eof_token_entry
;
796 /* normal cases are finished here */
798 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
799 if (match_op(next
->next
, ')')) {
801 next
->count
.vararg
= 1;
803 arg
->next
->next
= &eof_token_entry
;
811 if (eof_token(next
)) {
819 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
821 token_type(arg
) = TOKEN_IDENT
;
822 arg
->ident
= &__VA_ARGS___ident
;
823 if (!match_op(next
, ')'))
825 if (!++count
->normal
)
828 next
->count
.vararg
= 1;
830 arg
->next
->next
= &eof_token_entry
;
834 if (eof_token(arg
)) {
838 if (match_op(arg
, ','))
845 warning(arg
->pos
, "parameter name missing");
848 warning(arg
->pos
, "\"%s\" may not appear in macro parameter list",
852 warning(arg
->pos
, "missing ')' in macro parameter list");
855 warning(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
858 warning(arg
->pos
, "too many arguments in macro definition");
862 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
864 struct ident
*ident
= token
->ident
;
867 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
870 arglist
= arglist
->next
;
872 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
873 if (arglist
->ident
== ident
) {
874 struct argcount
*count
= &arglist
->next
->count
;
878 token_type(token
) = type
;
880 case TOKEN_MACRO_ARGUMENT
:
883 case TOKEN_QUOTED_ARGUMENT
:
890 return count
->vararg
? 2 : 1;
891 token_type(token
) = TOKEN_ERROR
;
898 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
900 struct token
*token
= expansion
;
902 struct token
*last
= NULL
;
904 if (match_op(token
, SPECIAL_HASHHASH
))
907 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
908 if (match_op(token
, '#')) {
910 struct token
*next
= token
->next
;
911 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
913 next
->pos
.whitespace
= token
->pos
.whitespace
;
916 token
->pos
.noexpand
= 1;
918 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
919 struct token
*next
= token
->next
;
920 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
921 token_type(token
) = TOKEN_CONCAT
;
925 if (arg
== 2 && last
&& match_op(last
, ',')) {
926 token_type(last
) = TOKEN_GNU_KLUDGE
;
929 } else if (match_op(next
, SPECIAL_HASHHASH
))
931 else if (match_op(next
, ','))
933 else if (eof_token(next
))
935 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
936 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
938 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
940 if (token_type(token
) == TOKEN_ERROR
)
944 token
= alloc_token(&expansion
->pos
);
945 token_type(token
) = TOKEN_UNTAINT
;
952 warning(token
->pos
, "'#' is not followed by a macro parameter");
956 warning(token
->pos
, "'##' cannot appear at the ends of macro expansion");
959 warning(token
->pos
, "too many instances of argument in body");
963 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
965 struct token
*arglist
, *expansion
;
966 struct token
*left
= token
->next
;
970 if (token_type(left
) != TOKEN_IDENT
) {
971 warning(token
->pos
, "expected identifier to 'define'");
977 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
978 MARK_STREAM_NONCONST(token
->pos
);
983 expansion
= left
->next
;
984 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
986 expansion
= parse_arguments(expansion
);
991 expansion
= parse_expansion(expansion
, arglist
, name
);
995 sym
= lookup_symbol(name
, NS_MACRO
);
997 if (token_list_different(sym
->expansion
, expansion
) ||
998 token_list_different(sym
->arglist
, arglist
)) {
999 warning(left
->pos
, "preprocessor token %.*s redefined",
1000 name
->len
, name
->name
);
1001 info(sym
->pos
, "this was the original definition");
1005 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1006 bind_symbol(sym
, name
, NS_MACRO
);
1008 sym
->expansion
= expansion
;
1009 sym
->arglist
= arglist
;
1013 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1015 struct token
*left
= token
->next
;
1016 struct symbol
**sym
;
1018 if (token_type(left
) != TOKEN_IDENT
) {
1019 warning(token
->pos
, "expected identifier to 'undef'");
1025 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1026 MARK_STREAM_NONCONST(token
->pos
);
1028 sym
= &left
->ident
->symbols
;
1030 struct symbol
*t
= *sym
;
1031 if (t
->namespace == NS_MACRO
) {
1040 static int preprocessor_if(struct token
*token
, int true)
1042 if (if_nesting
== 0)
1043 unmatched_if
= token
;
1044 if (if_nesting
>= MAX_NEST
)
1045 error_die(token
->pos
, "Maximum preprocessor conditional level exhausted");
1046 elif_ignore
[if_nesting
] = (false_nesting
|| true) ? ELIF_IGNORE
: 0;
1047 if (false_nesting
|| !true) {
1055 static int token_defined(struct token
*token
)
1057 if (token_type(token
) == TOKEN_IDENT
)
1058 return lookup_symbol(token
->ident
, NS_MACRO
) != NULL
;
1060 warning(token
->pos
, "expected identifier for #if[n]def");
1064 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1066 return preprocessor_if(token
, token_defined(token
->next
));
1069 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1071 struct token
*next
= token
->next
;
1072 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1073 if (token_type(next
) == TOKEN_IDENT
&&
1074 (!stream
->protect
|| stream
->protect
== next
->ident
)) {
1075 stream
->constant
= CONSTANT_FILE_IFNDEF
;
1076 stream
->protect
= next
->ident
;
1078 MARK_STREAM_NONCONST(token
->pos
);
1080 return preprocessor_if(token
, !token_defined(next
));
1084 * Expression handling for #if and #elif; it differs from normal expansion
1085 * due to special treatment of "defined".
1087 static int expression_value(struct token
**where
)
1089 struct expression
*expr
;
1091 struct token
**list
= where
, **beginning
= NULL
;
1095 while (!eof_token(p
= scan_next(list
))) {
1098 if (token_type(p
) != TOKEN_IDENT
)
1100 if (p
->ident
== &defined_ident
) {
1105 if (!expand_one_symbol(list
))
1107 if (token_type(p
) != TOKEN_IDENT
)
1109 if (Wundefined_preprocessor
)
1110 warning(p
->pos
, "undefined preprocessor identifier '%s'", show_ident(p
->ident
));
1111 replace_with_integer(p
, 0);
1114 if (match_op(p
, '(')) {
1118 replace_with_defined(p
);
1123 if (token_type(p
) == TOKEN_IDENT
)
1127 replace_with_defined(p
);
1132 if (!match_op(p
, ')'))
1133 warning(p
->pos
, "missing ')' after \"defined\"");
1140 p
= constant_expression(*where
, &expr
);
1142 warning(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1143 value
= get_expression_value(expr
);
1147 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1151 value
= expression_value(&token
->next
);
1153 // This is an approximation. We really only need this if the
1154 // condition does depends on a pre-processor symbol. Note, that
1155 // the important #ifndef case has already changed ->constant.
1156 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1157 MARK_STREAM_NONCONST(token
->pos
);
1159 return preprocessor_if(token
, value
);
1162 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1164 if (stream
->nesting
== if_nesting
)
1165 MARK_STREAM_NONCONST(token
->pos
);
1167 if (stream
->nesting
> if_nesting
) {
1168 warning(token
->pos
, "unmatched #elif");
1172 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1173 warning(token
->pos
, "#elif after #else");
1175 if (false_nesting
) {
1176 /* If this whole if-thing is if'ed out, an elif cannot help */
1177 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1179 if (expression_value(&token
->next
)) {
1182 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1191 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1193 if (stream
->nesting
== if_nesting
)
1194 MARK_STREAM_NONCONST(token
->pos
);
1196 if (stream
->nesting
> if_nesting
) {
1197 warning(token
->pos
, "unmatched #else");
1201 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1202 warning(token
->pos
, "#else after #else");
1204 elif_ignore
[if_nesting
-1] |= ELIF_SEEN_ELSE
;
1206 if (false_nesting
) {
1207 /* If this whole if-thing is if'ed out, an else cannot help */
1208 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1212 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1220 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1222 if (stream
->constant
== CONSTANT_FILE_IFNDEF
&& stream
->nesting
== if_nesting
)
1223 stream
->constant
= CONSTANT_FILE_MAYBE
;
1225 if (stream
->nesting
> if_nesting
)
1226 warning(token
->pos
, "unmatched #endif");
1227 else if (false_nesting
)
1234 static const char *show_token_sequence(struct token
*token
)
1236 static char buffer
[1024];
1242 while (!eof_token(token
)) {
1243 const char *val
= show_token(token
);
1244 int len
= strlen(val
);
1246 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1247 warning(token
->pos
, "too long token expansion");
1253 memcpy(ptr
, val
, len
);
1255 token
= token
->next
;
1256 whitespace
= token
->pos
.whitespace
;
1262 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1266 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1267 MARK_STREAM_NONCONST(token
->pos
);
1268 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1272 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1276 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1277 MARK_STREAM_NONCONST(token
->pos
);
1278 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1282 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1290 * Do we have any non-system includes?
1291 * Clear them out if so..
1293 stdinc
= gcc_includepath
- sys_includepath
;
1295 const char **src
= gcc_includepath
;
1296 const char **dst
= sys_includepath
;
1303 gcc_includepath
-= stdinc
;
1308 static void add_path_entry(struct token
*token
, const char *path
)
1313 /* Need one free entry.. */
1314 if (includepath
[INCLUDEPATHS
-2])
1315 error_die(token
->pos
, "too many include path entries");
1318 dst
= sys_includepath
;
1323 * Move them all up starting at "sys_includepath",
1324 * insert the new entry..
1327 const char *tmp
= *dst
;
1336 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1339 token
= token
->next
;
1340 if (eof_token(token
))
1342 if (token_type(token
) != TOKEN_STRING
) {
1343 warning(token
->pos
, "expected path string");
1346 add_path_entry(token
, token
->string
->data
);
1351 * We replace "#pragma xxx" with "__pragma__" in the token
1352 * stream. Just as an example.
1354 * We'll just #define that away for now, but the theory here
1355 * is that we can use this to insert arbitrary token sequences
1356 * to turn the pragma's into internal front-end sequences for
1357 * when we actually start caring about them.
1359 * So eventually this will turn into some kind of extended
1360 * __attribute__() like thing, except called __pragma__(xxx).
1362 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1364 struct token
*next
= *line
;
1366 token
->ident
= &pragma_ident
;
1367 token
->pos
.newline
= 1;
1368 token
->pos
.whitespace
= 1;
1376 * We ignore #line for now.
1378 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1384 void init_preprocessor(void)
1387 int stream
= init_stream("preprocessor", -1, includepath
);
1390 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1392 { "define", handle_define
},
1393 { "undef", handle_undef
},
1394 { "ifdef", handle_ifdef
},
1395 { "ifndef", handle_ifndef
},
1396 { "else", handle_else
},
1397 { "endif", handle_endif
},
1398 { "if", handle_if
},
1399 { "elif", handle_elif
},
1400 { "warning", handle_warning
},
1401 { "error", handle_error
},
1402 { "include", handle_include
},
1403 { "include_next",handle_include_next
},
1404 { "pragma", handle_pragma
},
1405 { "line", handle_line
},
1407 // our internal preprocessor tokens
1408 { "nostdinc", handle_nostdinc
},
1409 { "add_include", handle_add_include
},
1412 for (i
= 0; i
< (sizeof (handlers
) / sizeof (handlers
[0])); i
++) {
1414 sym
= create_symbol(stream
, handlers
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1415 sym
->handler
= handlers
[i
].handler
;
1419 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1421 struct token
*token
= start
->next
;
1426 if (token_type(token
) == TOKEN_NUMBER
)
1427 if (handle_line(stream
, line
, start
))
1430 if (token_type(token
) == TOKEN_IDENT
) {
1431 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1432 if (sym
&& sym
->handler(stream
, line
, token
))
1436 warning(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1439 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1441 struct token
*start
= *line
, *next
;
1442 struct token
**tp
= &start
->next
;
1446 if (next
->pos
.newline
)
1451 *tp
= &eof_token_entry
;
1452 handle_preprocessor_line(stream
, line
, start
);
1455 static void do_preprocess(struct token
**list
)
1459 while (!eof_token(next
= scan_next(list
))) {
1460 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1462 if (next
->pos
.newline
&& match_op(next
, '#')) {
1463 if (!next
->pos
.noexpand
) {
1464 preprocessor_line(stream
, list
);
1469 switch (token_type(next
)) {
1470 case TOKEN_STREAMEND
:
1471 if (stream
->nesting
< if_nesting
+ 1) {
1472 warning(unmatched_if
->pos
, "unterminated preprocessor conditional");
1473 // Pretend to see a series of #endifs
1474 MARK_STREAM_NONCONST(next
->pos
);
1476 handle_endif (stream
, NULL
, NULL
);
1477 } while (stream
->nesting
< if_nesting
+ 1);
1479 if (stream
->constant
== CONSTANT_FILE_MAYBE
&& stream
->protect
) {
1480 stream
->constant
= CONSTANT_FILE_YES
;
1484 case TOKEN_STREAMBEGIN
:
1485 stream
->nesting
= if_nesting
+ 1;
1490 if (false_nesting
) {
1495 if (token_type(next
) != TOKEN_IDENT
||
1496 expand_one_symbol(list
))
1500 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1502 * Any token expansion (even if it ended up being an
1503 * empty expansion) in this stream implies it can't
1506 MARK_STREAM_NONCONST(next
->pos
);
1511 struct token
* preprocess(struct token
*token
)
1514 init_preprocessor();
1515 do_preprocess(&token
);
1517 // Drop all expressions from pre-processing, they're not used any more.
1518 clear_expression_alloc();