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 #define MAX_NEST (256)
33 static int true_nesting
= 0;
34 static int false_nesting
= 0;
35 static struct token
*unmatched_if
= NULL
;
36 static char elif_ignore
[MAX_NEST
];
37 #define if_nesting (true_nesting + false_nesting)
39 #define INCLUDEPATHS 32
40 const char *includepath
[INCLUDEPATHS
+1] = {
44 const char *sys_includepath
[] = {
50 const char *gcc_includepath
[] = {
57 * This is stupid - the tokenizer already guarantees unique
58 * identifiers, so we should just compare identifier pointers
60 int match_string_ident(struct ident
*ident
, const char *str
)
62 return !str
[ident
->len
] && !memcmp(str
, ident
->name
, ident
->len
);
65 static struct token
*alloc_token(struct position
*pos
)
67 struct token
*token
= __alloc_token(0);
69 token
->pos
.stream
= pos
->stream
;
70 token
->pos
.line
= pos
->line
;
71 token
->pos
.pos
= pos
->pos
;
72 token
->pos
.whitespace
= 1;
76 static const char *show_token_sequence(struct token
*token
);
78 /* Expand symbol 'sym' at '*list' */
79 static struct token
**expand(struct token
**, struct symbol
*);
81 static void replace_with_string(struct token
*token
, const char *str
)
83 int size
= strlen(str
) + 1;
84 struct string
*s
= __alloc_string(size
);
87 memcpy(s
->data
, str
, size
);
88 token_type(token
) = TOKEN_STRING
;
92 static void replace_with_integer(struct token
*token
, unsigned int val
)
94 char *buf
= __alloc_bytes(10);
95 sprintf(buf
, "%d", val
);
96 token_type(token
) = TOKEN_NUMBER
;
100 static void replace_with_defined(struct token
*token
)
102 char *string
[] = { "0", "1" };
104 if (token_type(token
) != TOKEN_IDENT
)
105 warn(token
->pos
, "operator \"defined\" requires an identifier");
106 else if (lookup_symbol(token
->ident
, NS_PREPROCESSOR
))
108 token_type(token
) = TOKEN_NUMBER
;
109 token
->number
= string
[defined
];
112 struct token
**expand_one_symbol(struct token
**list
)
114 struct token
*token
= *list
;
117 if (token
->pos
.noexpand
)
120 sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
122 return expand(list
, sym
);
123 if (token
->ident
== &__LINE___ident
) {
124 replace_with_integer(token
, token
->pos
.line
);
125 } else if (token
->ident
== &__FILE___ident
) {
126 replace_with_string(token
, (input_streams
+ token
->pos
.stream
)->name
);
131 static inline struct token
*scan_next(struct token
**where
)
133 struct token
*token
= *where
;
134 if (token_type(token
) != TOKEN_UNTAINT
)
137 token
->ident
->tainted
= 0;
139 } while (token_type(token
) == TOKEN_UNTAINT
);
144 static struct token
**expand_list(struct token
**list
)
147 while (!eof_token(next
= scan_next(list
))) {
148 if (token_type(next
) == TOKEN_IDENT
)
149 list
= expand_one_symbol(list
);
156 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
158 struct token
**p
= &prev
->next
;
162 while (!eof_token(next
= scan_next(p
))) {
163 if (match_op(next
, '(')) {
165 } else if (match_op(next
, ')')) {
168 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
171 next
->pos
.stream
= pos
->stream
;
172 next
->pos
.line
= pos
->line
;
173 next
->pos
.pos
= pos
->pos
;
176 *p
= &eof_token_entry
;
181 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
186 struct token
*expanded
;
193 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
195 int wanted
= arglist
->count
.normal
;
196 struct token
*next
= NULL
;
199 arglist
= arglist
->next
; /* skip counter */
202 next
= collect_arg(start
, 0, &what
->pos
);
205 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
210 for (count
= 0; count
< wanted
; count
++) {
211 struct argcount
*p
= &arglist
->next
->count
;
212 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
213 arglist
= arglist
->next
->next
;
216 args
[count
].arg
= start
->next
;
217 args
[count
].n_normal
= p
->normal
;
218 args
[count
].n_quoted
= p
->quoted
;
219 args
[count
].n_str
= p
->str
;
220 if (match_op(next
, ')')) {
226 if (count
== wanted
&& !match_op(next
, ')'))
228 if (count
== wanted
- 1) {
229 struct argcount
*p
= &arglist
->next
->count
;
232 args
[count
].arg
= NULL
;
233 args
[count
].n_normal
= p
->normal
;
234 args
[count
].n_quoted
= p
->quoted
;
235 args
[count
].n_str
= p
->str
;
237 if (count
< wanted
- 1)
240 what
->next
= next
->next
;
244 warn(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
245 show_token(what
), wanted
, count
);
248 while (match_op(next
, ',')) {
249 next
= collect_arg(next
, 0, &what
->pos
);
254 warn(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
255 show_token(what
), count
, wanted
);
258 warn(what
->pos
, "unterminated argument list invoking macro \"%s\"",
261 what
->next
= next
->next
;
265 static struct token
*dup_list(struct token
*list
)
268 struct token
**p
= &res
;
270 while (!eof_token(list
)) {
271 struct token
*newtok
= __alloc_token(0);
280 static struct token
*stringify(struct token
*arg
)
282 const char *s
= show_token_sequence(arg
);
283 int size
= strlen(s
)+1;
284 struct token
*token
= __alloc_token(0);
285 struct string
*string
= __alloc_string(size
);
287 memcpy(string
->data
, s
, size
);
288 string
->length
= size
;
289 token
->pos
= arg
->pos
;
290 token_type(token
) = TOKEN_STRING
;
291 token
->string
= string
;
292 token
->next
= &eof_token_entry
;
296 static void expand_arguments(int count
, struct arg
*args
)
299 for (i
= 0; i
< count
; i
++) {
300 struct token
*arg
= args
[i
].arg
;
302 arg
= &eof_token_entry
;
304 args
[i
].str
= stringify(arg
);
305 if (args
[i
].n_normal
) {
306 if (!args
[i
].n_quoted
) {
307 args
[i
].expanded
= arg
;
309 } else if (eof_token(arg
)) {
310 args
[i
].expanded
= arg
;
312 args
[i
].expanded
= dup_list(arg
);
314 expand_list(&args
[i
].expanded
);
320 * Possibly valid combinations:
321 * - ident + ident -> ident
322 * - ident + number -> ident unless number contains '.', '+' or '-'.
323 * - number + number -> number
324 * - number + ident -> number
325 * - number + '.' -> number
326 * - number + '+' or '-' -> number, if number used to end on [eEpP].
327 * - '.' + number -> number, if number used to start with a digit.
328 * - special + special -> either special or an error.
330 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
333 enum token_type t1
= token_type(left
), t2
= token_type(right
);
335 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
338 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
341 strcpy(p
, show_token(left
));
342 strcat(p
, show_token(right
));
348 if (t1
== TOKEN_IDENT
) {
349 if (t2
== TOKEN_SPECIAL
)
351 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
356 if (t1
== TOKEN_NUMBER
) {
357 if (t2
== TOKEN_SPECIAL
) {
358 switch (right
->special
) {
362 if (strchr("eEpP", p
[len
- 2]))
371 if (p
[0] == '.' && isdigit(p
[1]))
374 return TOKEN_SPECIAL
;
377 static int merge(struct token
*left
, struct token
*right
)
379 extern unsigned char combinations
[][3];
380 static char buffer
[512];
383 switch (combine(left
, right
, buffer
)) {
385 left
->ident
= built_in_ident(buffer
);
386 left
->pos
.noexpand
= 0;
390 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
391 left
->number
= __alloc_bytes(strlen(buffer
) + 1);
392 memcpy(left
->number
, buffer
, strlen(buffer
) + 1);
396 if (buffer
[2] && buffer
[3])
398 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
399 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
407 warn(left
->pos
, "'##' failed: concatenation is not a valid token");
411 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
413 struct token
*alloc
= alloc_token(streampos
);
414 token_type(alloc
) = token_type(token
);
415 alloc
->pos
.newline
= pos
->newline
;
416 alloc
->pos
.whitespace
= pos
->whitespace
;
417 alloc
->number
= token
->number
;
418 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
422 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
424 int need_copy
= --*count
;
425 while (!eof_token(list
)) {
428 token
= dup_token(list
, &list
->pos
, &list
->pos
);
431 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
432 token
->pos
.noexpand
= 1;
434 where
= &token
->next
;
437 *where
= &eof_token_entry
;
441 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
443 struct token
*token
= *list
;
444 struct position
*base_pos
= &token
->pos
;
445 struct position
*pos
= base_pos
;
447 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
449 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
450 struct token
*added
, *arg
;
453 switch (token_type(body
)) {
454 case TOKEN_GNU_KLUDGE
:
456 * GNU kludge: if we had <comma>##<vararg>, behaviour
457 * depends on whether we had enough arguments to have
458 * a vararg. If we did, ## is just ignored. Otherwise
459 * both , and ## are ignored. Comma should come from
460 * the body of macro and not be an argument of earlier
463 if (!args
[body
->next
->argnum
].arg
)
465 added
= dup_token(body
, base_pos
, pos
);
466 token_type(added
) = TOKEN_SPECIAL
;
470 case TOKEN_STR_ARGUMENT
:
471 arg
= args
[body
->argnum
].str
;
472 count
= &args
[body
->argnum
].n_str
;
475 case TOKEN_QUOTED_ARGUMENT
:
476 arg
= args
[body
->argnum
].arg
;
477 count
= &args
[body
->argnum
].n_quoted
;
478 if (!arg
|| eof_token(arg
)) {
487 case TOKEN_MACRO_ARGUMENT
:
488 arg
= args
[body
->argnum
].expanded
;
489 count
= &args
[body
->argnum
].n_normal
;
490 if (eof_token(arg
)) {
495 tail
= copy(&added
, arg
, count
);
496 added
->pos
.newline
= pos
->newline
;
497 added
->pos
.whitespace
= pos
->whitespace
;
501 if (state
== Placeholder
)
508 added
= dup_token(body
, base_pos
, pos
);
509 if (added
->ident
->tainted
)
510 added
->pos
.noexpand
= 1;
515 added
= dup_token(body
, base_pos
, pos
);
521 * if we got to doing real concatenation, we already have
522 * added something into the list, so containing_token() is OK.
524 if (state
== Concat
&& merge(containing_token(list
), added
)) {
526 if (tail
!= &added
->next
)
534 *list
= &eof_token_entry
;
538 static struct token
**expand(struct token
**list
, struct symbol
*sym
)
541 struct token
*token
= *list
;
542 struct ident
*expanding
= token
->ident
;
544 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
545 struct arg args
[nargs
];
547 if (expanding
->tainted
) {
548 token
->pos
.noexpand
= 1;
553 if (!match_op(scan_next(&token
->next
), '('))
555 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
557 expand_arguments(nargs
, args
);
560 expanding
->tainted
= 1;
563 tail
= substitute(list
, sym
->expansion
, args
);
569 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
572 static char buffer
[256];
576 while (!eof_token(token
) && !match_op(token
, endop
)) {
578 const char *val
= token
->string
->data
;
579 if (token_type(token
) != TOKEN_STRING
)
580 val
= show_token(token
);
582 memcpy(ptr
, val
, len
);
587 if (endop
&& !match_op(token
, endop
))
588 warn(start
->pos
, "expected '>' at end of filename");
592 static int try_include(const char *path
, int plen
, const char *filename
, int flen
, struct token
**where
)
595 static char fullname
[PATH_MAX
];
597 memcpy(fullname
, path
, plen
);
598 if (plen
&& path
[plen
-1] != '/') {
599 fullname
[plen
] = '/';
602 memcpy(fullname
+plen
, filename
, flen
);
603 fd
= open(fullname
, O_RDONLY
);
605 char * streamname
= __alloc_bytes(plen
+ flen
);
606 memcpy(streamname
, fullname
, plen
+ flen
);
607 *where
= tokenize(streamname
, fd
, *where
);
614 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
618 while ((path
= *pptr
++) != NULL
) {
619 if (!try_include(path
, strlen(path
), filename
, flen
, list
))
627 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
)
629 int flen
= strlen(filename
) + 1;
631 /* Same directory as current stream? */
638 slash
= strrchr(path
, '/');
639 plen
= slash
? slash
- path
: 0;
641 if (try_include(path
, plen
, filename
, flen
, list
))
645 /* Check the standard include paths.. */
646 if (do_include_path(includepath
, list
, token
, filename
, flen
))
648 if (do_include_path(sys_includepath
, list
, token
, filename
, flen
))
650 if (do_include_path(gcc_includepath
, list
, token
, filename
, flen
))
653 error(token
->pos
, "unable to open '%s'", filename
);
656 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
658 const char *filename
;
662 if (stream
->constant
== -1)
663 stream
->constant
= 0;
668 if (!match_op(next
, '<')) {
669 expand_list(&token
->next
);
674 filename
= token_name_sequence(token
, expect
, token
);
675 do_include(!expect
, stream
, list
, token
, filename
);
679 static int token_different(struct token
*t1
, struct token
*t2
)
683 if (token_type(t1
) != token_type(t2
))
686 switch (token_type(t1
)) {
688 different
= t1
->ident
!= t2
->ident
;
690 case TOKEN_ARG_COUNT
:
693 case TOKEN_GNU_KLUDGE
:
697 different
= strcmp(t1
->number
, t2
->number
);
700 different
= t1
->special
!= t2
->special
;
702 case TOKEN_MACRO_ARGUMENT
:
703 case TOKEN_QUOTED_ARGUMENT
:
704 case TOKEN_STR_ARGUMENT
:
705 different
= t1
->argnum
!= t2
->argnum
;
708 different
= t1
->character
!= t2
->character
;
711 struct string
*s1
, *s2
;
716 if (s1
->length
!= s2
->length
)
718 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
728 static int token_list_different(struct token
*list1
, struct token
*list2
)
733 if (!list1
|| !list2
)
735 if (token_different(list1
, list2
))
742 static inline void set_arg_count(struct token
*token
)
744 token_type(token
) = TOKEN_ARG_COUNT
;
745 token
->count
.normal
= token
->count
.quoted
=
746 token
->count
.str
= token
->count
.vararg
= 0;
749 static struct token
*parse_arguments(struct token
*list
)
751 struct token
*arg
= list
->next
, *next
= list
;
752 struct argcount
*count
= &list
->count
;
756 if (match_op(arg
, ')')) {
758 list
->next
= &eof_token_entry
;
762 while (token_type(arg
) == TOKEN_IDENT
) {
763 if (arg
->ident
== &__VA_ARGS___ident
)
765 if (!++count
->normal
)
769 if (match_op(next
, ',')) {
775 if (match_op(next
, ')')) {
778 arg
->next
->next
= &eof_token_entry
;
782 /* normal cases are finished here */
784 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
785 if (match_op(next
->next
, ')')) {
787 next
->count
.vararg
= 1;
789 arg
->next
->next
= &eof_token_entry
;
797 if (eof_token(next
)) {
805 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
807 token_type(arg
) = TOKEN_IDENT
;
808 arg
->ident
= &__VA_ARGS___ident
;
809 if (!match_op(next
, ')'))
811 if (!++count
->normal
)
814 next
->count
.vararg
= 1;
816 arg
->next
->next
= &eof_token_entry
;
820 if (eof_token(arg
)) {
824 if (match_op(arg
, ','))
831 warn(arg
->pos
, "parameter name missing");
834 warn(arg
->pos
, "\"%s\" may not appear in macro parameter list",
838 warn(arg
->pos
, "missing ')' in macro parameter list");
841 warn(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
844 warn(arg
->pos
, "too many arguments in macro definition");
848 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
850 struct ident
*ident
= token
->ident
;
853 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
856 arglist
= arglist
->next
;
858 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
859 if (arglist
->ident
== ident
) {
860 struct argcount
*count
= &arglist
->next
->count
;
864 token_type(token
) = type
;
866 case TOKEN_MACRO_ARGUMENT
:
869 case TOKEN_QUOTED_ARGUMENT
:
876 return count
->vararg
? 2 : 1;
877 token_type(token
) = TOKEN_ERROR
;
884 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
886 struct token
*token
= expansion
;
888 struct token
*last
= NULL
;
890 if (match_op(token
, SPECIAL_HASHHASH
))
893 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
894 if (match_op(token
, '#')) {
896 struct token
*next
= token
->next
;
897 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
899 next
->pos
.whitespace
= token
->pos
.whitespace
;
902 token
->pos
.noexpand
= 1;
904 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
905 struct token
*next
= token
->next
;
906 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
907 token_type(token
) = TOKEN_CONCAT
;
911 if (arg
== 2 && last
&& match_op(last
, ',')) {
912 token_type(last
) = TOKEN_GNU_KLUDGE
;
915 } else if (match_op(next
, SPECIAL_HASHHASH
))
917 else if (match_op(next
, ','))
919 else if (eof_token(next
))
921 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
922 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
924 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
926 if (token_type(token
) == TOKEN_ERROR
)
930 token
= alloc_token(&expansion
->pos
);
931 token_type(token
) = TOKEN_UNTAINT
;
938 warn(token
->pos
, "'#' is not followed by a macro parameter");
942 warn(token
->pos
, "'##' cannot appear at the ends of macro expansion");
945 warn(token
->pos
, "too many instances of argument in body");
949 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
951 struct token
*arglist
, *expansion
;
952 struct token
*left
= token
->next
;
956 if (token_type(left
) != TOKEN_IDENT
) {
957 warn(token
->pos
, "expected identifier to 'define'");
965 expansion
= left
->next
;
966 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
968 expansion
= parse_arguments(expansion
);
973 expansion
= parse_expansion(expansion
, arglist
, name
);
977 sym
= lookup_symbol(name
, NS_PREPROCESSOR
);
979 if (token_list_different(sym
->expansion
, expansion
) ||
980 token_list_different(sym
->arglist
, arglist
)) {
981 warn(left
->pos
, "preprocessor token %.*s redefined",
982 name
->len
, name
->name
);
983 info(sym
->pos
, "this was the original definition");
987 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
988 bind_symbol(sym
, name
, NS_PREPROCESSOR
);
990 sym
->expansion
= expansion
;
991 sym
->arglist
= arglist
;
995 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
997 struct token
*left
= token
->next
;
1000 if (token_type(left
) != TOKEN_IDENT
) {
1001 warn(token
->pos
, "expected identifier to 'undef'");
1006 sym
= &left
->ident
->symbols
;
1008 struct symbol
*t
= *sym
;
1009 if (t
->namespace == NS_PREPROCESSOR
) {
1018 static int preprocessor_if(struct token
*token
, int true)
1020 if (if_nesting
== 0)
1021 unmatched_if
= token
;
1022 if (if_nesting
>= MAX_NEST
)
1023 error(token
->pos
, "Maximum preprocessor conditional level exhausted");
1024 elif_ignore
[if_nesting
] = false_nesting
|| true;
1025 if (false_nesting
|| !true) {
1033 static int token_defined(struct token
*token
)
1035 if (token_type(token
) == TOKEN_IDENT
)
1036 return lookup_symbol(token
->ident
, NS_PREPROCESSOR
) != NULL
;
1038 warn(token
->pos
, "expected identifier for #if[n]def");
1042 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1044 return preprocessor_if(token
, token_defined(token
->next
));
1047 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1049 struct token
*next
= token
->next
;
1050 if (stream
->constant
== -1) {
1051 int newconstant
= 0;
1052 if (token_type(next
) == TOKEN_IDENT
) {
1053 if (!stream
->protect
|| stream
->protect
== next
->ident
) {
1055 stream
->protect
= next
->ident
;
1056 stream
->nesting
= if_nesting
+1;
1059 stream
->constant
= newconstant
;
1061 return preprocessor_if(token
, !token_defined(next
));
1065 * Expression handling for #if and #elif; it differs from normal expansion
1066 * due to special treatment of "defined".
1068 static int expression_value(struct token
**where
)
1070 struct expression
*expr
;
1072 struct token
**list
= where
, **beginning
= NULL
;
1076 while (!eof_token(p
= scan_next(list
))) {
1079 if (token_type(p
) == TOKEN_IDENT
) {
1080 if (p
->ident
!= &defined_ident
) {
1081 list
= expand_one_symbol(list
);
1089 if (match_op(p
, '(')) {
1093 replace_with_defined(p
);
1098 if (token_type(p
) == TOKEN_IDENT
)
1102 replace_with_defined(p
);
1107 if (!match_op(p
, ')'))
1108 warn(p
->pos
, "missing ')' after \"defined\"");
1115 p
= constant_expression(*where
, &expr
);
1117 warn(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1118 value
= get_expression_value(expr
);
1122 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1126 value
= expression_value(&token
->next
);
1127 return preprocessor_if(token
, value
);
1130 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1132 if (stream
->nesting
== if_nesting
)
1133 stream
->constant
= 0;
1134 if (false_nesting
) {
1135 /* If this whole if-thing is if'ed out, an elif cannot help */
1136 if (elif_ignore
[if_nesting
-1])
1138 if (expression_value(&token
->next
)) {
1141 elif_ignore
[if_nesting
-1] = 1;
1150 warn(token
->pos
, "unmatched '#elif'");
1154 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1156 if (stream
->nesting
== if_nesting
)
1157 stream
->constant
= 0;
1158 if (false_nesting
) {
1159 /* If this whole if-thing is if'ed out, an else cannot help */
1160 if (elif_ignore
[if_nesting
-1])
1164 elif_ignore
[if_nesting
-1] = 1;
1172 warn(token
->pos
, "unmatched #else");
1176 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1178 if (stream
->constant
== -2 && stream
->nesting
== if_nesting
)
1179 stream
->constant
= -1;
1181 if (false_nesting
) {
1189 warn(token
->pos
, "unmatched #endif");
1193 static const char *show_token_sequence(struct token
*token
)
1195 static char buffer
[1024];
1201 while (!eof_token(token
)) {
1202 const char *val
= show_token(token
);
1203 int len
= strlen(val
);
1205 if (ptr
+ whitespace
+ len
> buffer
+ sizeof(buffer
)) {
1206 warn(token
->pos
, "too long token expansion");
1212 memcpy(ptr
, val
, len
);
1214 token
= token
->next
;
1215 whitespace
= token
->pos
.whitespace
;
1221 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1225 warn(token
->pos
, "%s", show_token_sequence(token
->next
));
1229 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1233 warn(token
->pos
, "%s", show_token_sequence(token
->next
));
1237 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1241 includepath
[0] = NULL
;
1245 static void add_path_entry(struct token
*token
, const char *path
)
1249 for (i
= 0; i
< INCLUDEPATHS
; i
++) {
1250 if (!includepath
[i
]) {
1251 includepath
[i
] = path
;
1252 includepath
[i
+1] = NULL
;
1256 warn(token
->pos
, "too many include path entries");
1259 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1262 token
= token
->next
;
1263 if (eof_token(token
))
1265 if (token_type(token
) != TOKEN_STRING
) {
1266 warn(token
->pos
, "expected path string");
1269 add_path_entry(token
, token
->string
->data
);
1274 * We replace "#pragma xxx" with "__pragma__" in the token
1275 * stream. Just as an example.
1277 * We'll just #define that away for now, but the theory here
1278 * is that we can use this to insert arbitrary token sequences
1279 * to turn the pragma's into internal front-end sequences for
1280 * when we actually start caring about them.
1282 * So eventually this will turn into some kind of extended
1283 * __attribute__() like thing, except called __pragma__(xxx).
1285 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1287 struct token
*next
= *line
;
1289 token
->ident
= &pragma_ident
;
1290 token
->pos
.newline
= 1;
1291 token
->pos
.whitespace
= 1;
1298 static int handle_preprocessor_command(struct stream
*stream
, struct token
**line
, struct ident
*ident
, struct token
*token
)
1303 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1305 { "define", handle_define
},
1306 { "undef", handle_undef
},
1307 { "ifdef", handle_ifdef
},
1308 { "ifndef", handle_ifndef
},
1309 { "else", handle_else
},
1310 { "endif", handle_endif
},
1311 { "if", handle_if
},
1312 { "elif", handle_elif
},
1313 { "warning", handle_warning
},
1314 { "error", handle_error
},
1315 { "include", handle_include
},
1316 { "pragma", handle_pragma
},
1318 // our internal preprocessor tokens
1319 { "nostdinc", handle_nostdinc
},
1320 { "add_include", handle_add_include
},
1323 for (i
= 0; i
< (sizeof (handlers
) / sizeof (handlers
[0])); i
++) {
1324 if (match_string_ident(ident
, handlers
[i
].name
))
1325 return handlers
[i
].handler(stream
, line
, token
);
1330 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1335 if (token_type(token
) == TOKEN_IDENT
)
1336 if (handle_preprocessor_command(stream
, line
, token
->ident
, token
))
1338 warn(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1341 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1343 struct token
*start
= *line
, *next
;
1344 struct token
**tp
= &start
->next
;
1348 if (next
->pos
.newline
)
1353 *tp
= &eof_token_entry
;
1354 handle_preprocessor_line(stream
, line
, start
->next
);
1357 static void do_preprocess(struct token
**list
)
1361 while (!eof_token(next
= scan_next(list
))) {
1362 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1364 if (next
->pos
.newline
&& match_op(next
, '#')) {
1365 if (!next
->pos
.noexpand
) {
1366 preprocessor_line(stream
, list
);
1371 if (false_nesting
) {
1376 switch (token_type(next
)) {
1377 case TOKEN_STREAMEND
:
1378 if (stream
->constant
== -1 && stream
->protect
) {
1379 stream
->constant
= 1;
1382 case TOKEN_STREAMBEGIN
:
1387 list
= expand_one_symbol(list
);
1393 * Any token expansion (even if it ended up being an
1394 * empty expansion) in this stream implies it can't
1397 stream
->constant
= 0;
1401 struct token
* preprocess(struct token
*token
)
1404 do_preprocess(&token
);
1406 warn(unmatched_if
->pos
, "unmatched preprocessor conditional");
1408 // Drop all expressions from pre-processing, they're not used any more.
1409 clear_expression_alloc();