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
28 #include "expression.h"
31 static int false_nesting
= 0;
33 #define INCLUDEPATHS 300
34 const char *includepath
[INCLUDEPATHS
+1] = {
41 static const char **quote_includepath
= includepath
;
42 static const char **angle_includepath
= includepath
+ 1;
43 static const char **isys_includepath
= includepath
+ 1;
44 static const char **sys_includepath
= includepath
+ 1;
45 static const char **dirafter_includepath
= includepath
+ 3;
47 #define dirty_stream(stream) \
49 if (!stream->dirty) { \
51 if (!stream->ifndef) \
52 stream->protect = NULL; \
56 #define end_group(stream) \
58 if (stream->ifndef == stream->top_if) { \
59 stream->ifndef = NULL; \
61 stream->protect = NULL; \
62 else if (stream->protect) \
67 #define nesting_error(stream) \
70 stream->ifndef = NULL; \
71 stream->protect = NULL; \
74 static struct token
*alloc_token(struct position
*pos
)
76 struct token
*token
= __alloc_token(0);
78 token
->pos
.stream
= pos
->stream
;
79 token
->pos
.line
= pos
->line
;
80 token
->pos
.pos
= pos
->pos
;
81 token
->pos
.whitespace
= 1;
85 static const char *show_token_sequence(struct token
*token
);
87 /* Expand symbol 'sym' at '*list' */
88 static int expand(struct token
**, struct symbol
*);
90 static void replace_with_string(struct token
*token
, const char *str
)
92 int size
= strlen(str
) + 1;
93 struct string
*s
= __alloc_string(size
);
96 memcpy(s
->data
, str
, size
);
97 token_type(token
) = TOKEN_STRING
;
101 static void replace_with_integer(struct token
*token
, unsigned int val
)
103 char *buf
= __alloc_bytes(11);
104 sprintf(buf
, "%u", val
);
105 token_type(token
) = TOKEN_NUMBER
;
109 static struct symbol
*lookup_macro(struct ident
*ident
)
111 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_UNDEF
);
112 if (sym
&& sym
->namespace != NS_MACRO
)
117 static int token_defined(struct token
*token
)
119 if (token_type(token
) == TOKEN_IDENT
) {
120 struct symbol
*sym
= lookup_macro(token
->ident
);
122 sym
->used_in
= file_scope
;
128 sparse_error(token
->pos
, "expected preprocessor identifier");
132 static void replace_with_defined(struct token
*token
)
134 static const char *string
[] = { "0", "1" };
135 int defined
= token_defined(token
);
137 token_type(token
) = TOKEN_NUMBER
;
138 token
->number
= string
[defined
];
141 static int expand_one_symbol(struct token
**list
)
143 struct token
*token
= *list
;
145 static char buffer
[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
148 if (token
->pos
.noexpand
)
151 sym
= lookup_macro(token
->ident
);
153 sym
->used_in
= file_scope
;
154 return expand(list
, sym
);
156 if (token
->ident
== &__LINE___ident
) {
157 replace_with_integer(token
, token
->pos
.line
);
158 } else if (token
->ident
== &__FILE___ident
) {
159 replace_with_string(token
, stream_name(token
->pos
.stream
));
160 } else if (token
->ident
== &__DATE___ident
) {
163 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
164 replace_with_string(token
, buffer
);
165 } else if (token
->ident
== &__TIME___ident
) {
168 strftime(buffer
, 9, "%T", localtime(&t
));
169 replace_with_string(token
, buffer
);
174 static inline struct token
*scan_next(struct token
**where
)
176 struct token
*token
= *where
;
177 if (token_type(token
) != TOKEN_UNTAINT
)
180 token
->ident
->tainted
= 0;
182 } while (token_type(token
) == TOKEN_UNTAINT
);
187 static void expand_list(struct token
**list
)
190 while (!eof_token(next
= scan_next(list
))) {
191 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
196 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
198 struct token
**p
= &prev
->next
;
202 while (!eof_token(next
= scan_next(p
))) {
203 if (match_op(next
, '(')) {
205 } else if (match_op(next
, ')')) {
208 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
211 next
->pos
.stream
= pos
->stream
;
212 next
->pos
.line
= pos
->line
;
213 next
->pos
.pos
= pos
->pos
;
216 *p
= &eof_token_entry
;
221 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
226 struct token
*expanded
;
233 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
235 int wanted
= arglist
->count
.normal
;
236 struct token
*next
= NULL
;
239 arglist
= arglist
->next
; /* skip counter */
242 next
= collect_arg(start
, 0, &what
->pos
);
245 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
250 for (count
= 0; count
< wanted
; count
++) {
251 struct argcount
*p
= &arglist
->next
->count
;
252 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
253 arglist
= arglist
->next
->next
;
256 args
[count
].arg
= start
->next
;
257 args
[count
].n_normal
= p
->normal
;
258 args
[count
].n_quoted
= p
->quoted
;
259 args
[count
].n_str
= p
->str
;
260 if (match_op(next
, ')')) {
266 if (count
== wanted
&& !match_op(next
, ')'))
268 if (count
== wanted
- 1) {
269 struct argcount
*p
= &arglist
->next
->count
;
272 args
[count
].arg
= NULL
;
273 args
[count
].n_normal
= p
->normal
;
274 args
[count
].n_quoted
= p
->quoted
;
275 args
[count
].n_str
= p
->str
;
277 if (count
< wanted
- 1)
280 what
->next
= next
->next
;
284 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
285 show_token(what
), wanted
, count
);
288 while (match_op(next
, ',')) {
289 next
= collect_arg(next
, 0, &what
->pos
);
294 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
295 show_token(what
), count
, wanted
);
298 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
301 what
->next
= next
->next
;
305 static struct token
*dup_list(struct token
*list
)
307 struct token
*res
= NULL
;
308 struct token
**p
= &res
;
310 while (!eof_token(list
)) {
311 struct token
*newtok
= __alloc_token(0);
320 static struct token
*stringify(struct token
*arg
)
322 const char *s
= show_token_sequence(arg
);
323 int size
= strlen(s
)+1;
324 struct token
*token
= __alloc_token(0);
325 struct string
*string
= __alloc_string(size
);
327 memcpy(string
->data
, s
, size
);
328 string
->length
= size
;
329 token
->pos
= arg
->pos
;
330 token_type(token
) = TOKEN_STRING
;
331 token
->string
= string
;
332 token
->next
= &eof_token_entry
;
336 static void expand_arguments(int count
, struct arg
*args
)
339 for (i
= 0; i
< count
; i
++) {
340 struct token
*arg
= args
[i
].arg
;
342 arg
= &eof_token_entry
;
344 args
[i
].str
= stringify(arg
);
345 if (args
[i
].n_normal
) {
346 if (!args
[i
].n_quoted
) {
347 args
[i
].expanded
= arg
;
349 } else if (eof_token(arg
)) {
350 args
[i
].expanded
= arg
;
352 args
[i
].expanded
= dup_list(arg
);
354 expand_list(&args
[i
].expanded
);
360 * Possibly valid combinations:
361 * - ident + ident -> ident
362 * - ident + number -> ident unless number contains '.', '+' or '-'.
363 * - number + number -> number
364 * - number + ident -> number
365 * - number + '.' -> number
366 * - number + '+' or '-' -> number, if number used to end on [eEpP].
367 * - '.' + number -> number, if number used to start with a digit.
368 * - special + special -> either special or an error.
370 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
373 enum token_type t1
= token_type(left
), t2
= token_type(right
);
375 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
378 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
381 strcpy(p
, show_token(left
));
382 strcat(p
, show_token(right
));
388 if (t1
== TOKEN_IDENT
) {
389 if (t2
== TOKEN_SPECIAL
)
391 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
396 if (t1
== TOKEN_NUMBER
) {
397 if (t2
== TOKEN_SPECIAL
) {
398 switch (right
->special
) {
402 if (strchr("eEpP", p
[len
- 2]))
411 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
414 return TOKEN_SPECIAL
;
417 static int merge(struct token
*left
, struct token
*right
)
419 static char buffer
[512];
422 switch (combine(left
, right
, buffer
)) {
424 left
->ident
= built_in_ident(buffer
);
425 left
->pos
.noexpand
= 0;
429 char *number
= __alloc_bytes(strlen(buffer
) + 1);
430 memcpy(number
, buffer
, strlen(buffer
) + 1);
431 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
432 left
->number
= number
;
437 if (buffer
[2] && buffer
[3])
439 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
440 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
448 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
452 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
454 struct token
*alloc
= alloc_token(streampos
);
455 token_type(alloc
) = token_type(token
);
456 alloc
->pos
.newline
= pos
->newline
;
457 alloc
->pos
.whitespace
= pos
->whitespace
;
458 alloc
->number
= token
->number
;
459 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
463 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
465 int need_copy
= --*count
;
466 while (!eof_token(list
)) {
469 token
= dup_token(list
, &list
->pos
, &list
->pos
);
472 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
473 token
->pos
.noexpand
= 1;
475 where
= &token
->next
;
478 *where
= &eof_token_entry
;
482 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
484 struct token
*token
= *list
;
485 struct position
*base_pos
= &token
->pos
;
486 struct position
*pos
= base_pos
;
488 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
490 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
491 struct token
*added
, *arg
;
494 switch (token_type(body
)) {
495 case TOKEN_GNU_KLUDGE
:
497 * GNU kludge: if we had <comma>##<vararg>, behaviour
498 * depends on whether we had enough arguments to have
499 * a vararg. If we did, ## is just ignored. Otherwise
500 * both , and ## are ignored. Comma should come from
501 * the body of macro and not be an argument of earlier
504 if (!args
[body
->next
->argnum
].arg
)
506 added
= dup_token(body
, base_pos
, pos
);
507 token_type(added
) = TOKEN_SPECIAL
;
511 case TOKEN_STR_ARGUMENT
:
512 arg
= args
[body
->argnum
].str
;
513 count
= &args
[body
->argnum
].n_str
;
516 case TOKEN_QUOTED_ARGUMENT
:
517 arg
= args
[body
->argnum
].arg
;
518 count
= &args
[body
->argnum
].n_quoted
;
519 if (!arg
|| eof_token(arg
)) {
528 case TOKEN_MACRO_ARGUMENT
:
529 arg
= args
[body
->argnum
].expanded
;
530 count
= &args
[body
->argnum
].n_normal
;
531 if (eof_token(arg
)) {
536 tail
= copy(&added
, arg
, count
);
537 added
->pos
.newline
= pos
->newline
;
538 added
->pos
.whitespace
= pos
->whitespace
;
542 if (state
== Placeholder
)
549 added
= dup_token(body
, base_pos
, pos
);
550 if (added
->ident
->tainted
)
551 added
->pos
.noexpand
= 1;
556 added
= dup_token(body
, base_pos
, pos
);
562 * if we got to doing real concatenation, we already have
563 * added something into the list, so containing_token() is OK.
565 if (state
== Concat
&& merge(containing_token(list
), added
)) {
567 if (tail
!= &added
->next
)
575 *list
= &eof_token_entry
;
579 static int expand(struct token
**list
, struct symbol
*sym
)
582 struct token
*token
= *list
;
583 struct ident
*expanding
= token
->ident
;
585 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
586 struct arg args
[nargs
];
588 if (expanding
->tainted
) {
589 token
->pos
.noexpand
= 1;
594 if (!match_op(scan_next(&token
->next
), '('))
596 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
598 expand_arguments(nargs
, args
);
601 expanding
->tainted
= 1;
604 tail
= substitute(list
, sym
->expansion
, args
);
610 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
613 static char buffer
[256];
617 while (!eof_token(token
) && !match_op(token
, endop
)) {
619 const char *val
= token
->string
->data
;
620 if (token_type(token
) != TOKEN_STRING
)
621 val
= show_token(token
);
623 memcpy(ptr
, val
, len
);
628 if (endop
&& !match_op(token
, endop
))
629 sparse_error(start
->pos
, "expected '>' at end of filename");
633 static int already_tokenized(const char *path
)
636 struct stream
*s
= input_streams
;
638 for (i
= input_stream_nr
; --i
>= 0; s
++) {
639 if (s
->constant
!= CONSTANT_FILE_YES
)
641 if (strcmp(path
, s
->name
))
643 if (s
->protect
&& !lookup_macro(s
->protect
))
650 /* Handle include of header files.
651 * The relevant options are made compatible with gcc. The only options that
652 * are not supported is -withprefix and friends.
654 * Three set of include paths are known:
655 * quote_includepath: Path to search when using #include "file.h"
656 * angle_includepath: Paths to search when using #include <file.h>
657 * isys_includepath: Paths specified with -isystem, come before the
658 * built-in system include paths. Gcc would suppress
659 * warnings from system headers. Here we separate
660 * them from the angle_ ones to keep search ordering.
662 * sys_includepath: Built-in include paths.
663 * dirafter_includepath Paths added with -dirafter.
665 * The above is implemented as one array with pointers
667 * quote_includepath ---> | |
671 * angle_includepath ---> | |
673 * isys_includepath ---> | |
675 * sys_includepath ---> | |
677 * dirafter_includepath -> | |
680 * -I dir insert dir just before isys_includepath and move the rest
681 * -I- makes all dirs specified with -I before to quote dirs only and
682 * angle_includepath is set equal to isys_includepath.
683 * -nostdinc removes all sys dirs by storing NULL in entry pointed
684 * to by * sys_includepath. Note that this will reset all dirs built-in
685 * and added before -nostdinc by -isystem and -idirafter.
686 * -isystem dir adds dir where isys_includepath points adding this dir as
688 * -idirafter dir adds dir to the end of the list
691 static void set_stream_include_path(struct stream
*stream
)
693 const char *path
= stream
->path
;
695 const char *p
= strrchr(stream
->name
, '/');
698 int len
= p
- stream
->name
+ 1;
699 char *m
= malloc(len
+1);
700 /* This includes the final "/" */
701 memcpy(m
, stream
->name
, len
);
707 includepath
[0] = path
;
710 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
713 int plen
= strlen(path
);
714 static char fullname
[PATH_MAX
];
716 memcpy(fullname
, path
, plen
);
717 if (plen
&& path
[plen
-1] != '/') {
718 fullname
[plen
] = '/';
721 memcpy(fullname
+plen
, filename
, flen
);
722 if (already_tokenized(fullname
))
724 fd
= open(fullname
, O_RDONLY
);
726 char * streamname
= __alloc_bytes(plen
+ flen
);
727 memcpy(streamname
, fullname
, plen
+ flen
);
728 *where
= tokenize(streamname
, fd
, *where
, next_path
);
735 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
739 while ((path
= *pptr
++) != NULL
) {
740 if (!try_include(path
, filename
, flen
, list
, pptr
))
747 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
749 int flen
= strlen(filename
) + 1;
752 if (filename
[0] == '/') {
753 if (try_include("", filename
, flen
, list
, includepath
))
758 /* Dir of input file is first dir to search for quoted includes */
759 set_stream_include_path(stream
);
762 /* Do not search quote include if <> is in use */
763 path
= local
? quote_includepath
: angle_includepath
;
765 /* Check the standard include paths.. */
766 if (do_include_path(path
, list
, token
, filename
, flen
))
769 error_die(token
->pos
, "unable to open '%s'", filename
);
772 static int free_preprocessor_line(struct token
*token
)
774 while (token_type(token
) != TOKEN_EOF
) {
775 struct token
*free
= token
;
782 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
784 const char *filename
;
790 if (!match_op(next
, '<')) {
791 expand_list(&token
->next
);
794 if (match_op(token
->next
, '<')) {
800 filename
= token_name_sequence(token
, expect
, token
);
801 do_include(!expect
, stream
, list
, token
, filename
, path
);
805 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
807 return handle_include_path(stream
, list
, token
, NULL
);
810 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
812 return handle_include_path(stream
, list
, token
, stream
->next_path
);
815 static int token_different(struct token
*t1
, struct token
*t2
)
819 if (token_type(t1
) != token_type(t2
))
822 switch (token_type(t1
)) {
824 different
= t1
->ident
!= t2
->ident
;
826 case TOKEN_ARG_COUNT
:
829 case TOKEN_GNU_KLUDGE
:
833 different
= strcmp(t1
->number
, t2
->number
);
836 different
= t1
->special
!= t2
->special
;
838 case TOKEN_MACRO_ARGUMENT
:
839 case TOKEN_QUOTED_ARGUMENT
:
840 case TOKEN_STR_ARGUMENT
:
841 different
= t1
->argnum
!= t2
->argnum
;
844 different
= t1
->character
!= t2
->character
;
847 struct string
*s1
, *s2
;
852 if (s1
->length
!= s2
->length
)
854 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
864 static int token_list_different(struct token
*list1
, struct token
*list2
)
869 if (!list1
|| !list2
)
871 if (token_different(list1
, list2
))
878 static inline void set_arg_count(struct token
*token
)
880 token_type(token
) = TOKEN_ARG_COUNT
;
881 token
->count
.normal
= token
->count
.quoted
=
882 token
->count
.str
= token
->count
.vararg
= 0;
885 static struct token
*parse_arguments(struct token
*list
)
887 struct token
*arg
= list
->next
, *next
= list
;
888 struct argcount
*count
= &list
->count
;
892 if (match_op(arg
, ')')) {
894 list
->next
= &eof_token_entry
;
898 while (token_type(arg
) == TOKEN_IDENT
) {
899 if (arg
->ident
== &__VA_ARGS___ident
)
901 if (!++count
->normal
)
905 if (match_op(next
, ',')) {
911 if (match_op(next
, ')')) {
914 arg
->next
->next
= &eof_token_entry
;
918 /* normal cases are finished here */
920 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
921 if (match_op(next
->next
, ')')) {
923 next
->count
.vararg
= 1;
925 arg
->next
->next
= &eof_token_entry
;
933 if (eof_token(next
)) {
941 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
943 token_type(arg
) = TOKEN_IDENT
;
944 arg
->ident
= &__VA_ARGS___ident
;
945 if (!match_op(next
, ')'))
947 if (!++count
->normal
)
950 next
->count
.vararg
= 1;
952 arg
->next
->next
= &eof_token_entry
;
956 if (eof_token(arg
)) {
960 if (match_op(arg
, ','))
967 sparse_error(arg
->pos
, "parameter name missing");
970 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
974 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
977 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
980 sparse_error(arg
->pos
, "too many arguments in macro definition");
984 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
986 struct ident
*ident
= token
->ident
;
989 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
992 arglist
= arglist
->next
;
994 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
995 if (arglist
->ident
== ident
) {
996 struct argcount
*count
= &arglist
->next
->count
;
1000 token_type(token
) = type
;
1002 case TOKEN_MACRO_ARGUMENT
:
1003 n
= ++count
->normal
;
1005 case TOKEN_QUOTED_ARGUMENT
:
1006 n
= ++count
->quoted
;
1012 return count
->vararg
? 2 : 1;
1013 token_type(token
) = TOKEN_ERROR
;
1020 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1022 struct token
*token
= expansion
;
1024 struct token
*last
= NULL
;
1026 if (match_op(token
, SPECIAL_HASHHASH
))
1029 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1030 if (match_op(token
, '#')) {
1032 struct token
*next
= token
->next
;
1033 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1035 next
->pos
.whitespace
= token
->pos
.whitespace
;
1038 token
->pos
.noexpand
= 1;
1040 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
1041 struct token
*next
= token
->next
;
1042 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1043 token_type(token
) = TOKEN_CONCAT
;
1047 if (arg
== 2 && last
&& match_op(last
, ',')) {
1048 token_type(last
) = TOKEN_GNU_KLUDGE
;
1051 } else if (match_op(next
, SPECIAL_HASHHASH
))
1053 else if (eof_token(next
))
1055 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1056 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1058 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1060 if (token_type(token
) == TOKEN_ERROR
)
1064 token
= alloc_token(&expansion
->pos
);
1065 token_type(token
) = TOKEN_UNTAINT
;
1066 token
->ident
= name
;
1072 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1076 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1079 sparse_error(token
->pos
, "too many instances of argument in body");
1083 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1085 struct token
*arglist
, *expansion
;
1086 struct token
*left
= token
->next
;
1091 if (token_type(left
) != TOKEN_IDENT
) {
1092 sparse_error(token
->pos
, "expected identifier to 'define'");
1099 expansion
= left
->next
;
1100 if (!expansion
->pos
.whitespace
) {
1101 if (match_op(expansion
, '(')) {
1102 arglist
= expansion
;
1103 expansion
= parse_arguments(expansion
);
1106 } else if (!eof_token(expansion
)) {
1107 warning(expansion
->pos
,
1108 "no whitespace before object-like macro body");
1112 expansion
= parse_expansion(expansion
, arglist
, name
);
1117 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1121 if (attr
< sym
->attr
)
1124 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1126 if (token_list_different(sym
->expansion
, expansion
) ||
1127 token_list_different(sym
->arglist
, arglist
)) {
1129 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1130 || sym
->used_in
== file_scope
) {
1131 warning(left
->pos
, "preprocessor token %.*s redefined",
1132 name
->len
, name
->name
);
1133 info(sym
->pos
, "this was the original definition");
1139 if (!sym
|| sym
->scope
!= file_scope
) {
1140 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1141 bind_symbol(sym
, name
, NS_MACRO
);
1146 sym
->expansion
= expansion
;
1147 sym
->arglist
= arglist
;
1148 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1151 sym
->namespace = NS_MACRO
;
1152 sym
->used_in
= NULL
;
1158 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1160 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1163 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1165 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1168 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1170 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1173 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1175 struct token
*left
= token
->next
;
1178 if (token_type(left
) != TOKEN_IDENT
) {
1179 sparse_error(token
->pos
, "expected identifier to 'undef'");
1183 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1185 if (attr
< sym
->attr
)
1187 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1189 } else if (attr
<= SYM_ATTR_NORMAL
)
1192 if (!sym
|| sym
->scope
!= file_scope
) {
1193 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1194 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1197 sym
->namespace = NS_UNDEF
;
1198 sym
->used_in
= NULL
;
1204 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1206 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1209 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1211 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1214 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1216 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1217 free_preprocessor_line(token
->next
);
1218 token
->next
= stream
->top_if
;
1219 stream
->top_if
= token
;
1220 if (false_nesting
|| true != 1)
1225 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1227 struct token
*next
= token
->next
;
1229 if (token_type(next
) == TOKEN_IDENT
) {
1230 arg
= token_defined(next
);
1232 dirty_stream(stream
);
1234 sparse_error(token
->pos
, "expected preprocessor identifier");
1237 return preprocessor_if(stream
, token
, arg
);
1240 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1242 struct token
*next
= token
->next
;
1244 if (token_type(next
) == TOKEN_IDENT
) {
1245 if (!stream
->dirty
&& !stream
->ifndef
) {
1246 if (!stream
->protect
) {
1247 stream
->ifndef
= token
;
1248 stream
->protect
= next
->ident
;
1249 } else if (stream
->protect
== next
->ident
) {
1250 stream
->ifndef
= token
;
1254 arg
= !token_defined(next
);
1256 dirty_stream(stream
);
1258 sparse_error(token
->pos
, "expected preprocessor identifier");
1262 return preprocessor_if(stream
, token
, arg
);
1266 * Expression handling for #if and #elif; it differs from normal expansion
1267 * due to special treatment of "defined".
1269 static int expression_value(struct token
**where
)
1271 struct expression
*expr
;
1273 struct token
**list
= where
, **beginning
= NULL
;
1277 while (!eof_token(p
= scan_next(list
))) {
1280 if (token_type(p
) != TOKEN_IDENT
)
1282 if (p
->ident
== &defined_ident
) {
1287 if (!expand_one_symbol(list
))
1289 if (token_type(p
) != TOKEN_IDENT
)
1291 token_type(p
) = TOKEN_ZERO_IDENT
;
1294 if (match_op(p
, '(')) {
1298 replace_with_defined(p
);
1303 if (token_type(p
) == TOKEN_IDENT
)
1307 replace_with_defined(p
);
1312 if (!match_op(p
, ')'))
1313 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1320 p
= constant_expression(*where
, &expr
);
1322 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1323 value
= get_expression_value(expr
);
1327 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1331 value
= expression_value(&token
->next
);
1333 dirty_stream(stream
);
1334 return preprocessor_if(stream
, token
, value
);
1337 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1339 struct token
*top_if
= stream
->top_if
;
1343 nesting_error(stream
);
1344 sparse_error(token
->pos
, "unmatched #elif within stream");
1348 if (token_type(top_if
) == TOKEN_ELSE
) {
1349 nesting_error(stream
);
1350 sparse_error(token
->pos
, "#elif after #else");
1356 dirty_stream(stream
);
1357 if (token_type(top_if
) != TOKEN_IF
)
1359 if (false_nesting
) {
1360 if (expression_value(&token
->next
))
1364 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1369 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1371 struct token
*top_if
= stream
->top_if
;
1375 nesting_error(stream
);
1376 sparse_error(token
->pos
, "unmatched #else within stream");
1380 if (token_type(top_if
) == TOKEN_ELSE
) {
1381 nesting_error(stream
);
1382 sparse_error(token
->pos
, "#else after #else");
1384 if (false_nesting
) {
1385 if (token_type(top_if
) == TOKEN_IF
)
1390 token_type(top_if
) = TOKEN_ELSE
;
1394 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1396 struct token
*top_if
= stream
->top_if
;
1399 nesting_error(stream
);
1400 sparse_error(token
->pos
, "unmatched #endif in stream");
1405 stream
->top_if
= top_if
->next
;
1406 __free_token(top_if
);
1410 static const char *show_token_sequence(struct token
*token
)
1412 static char buffer
[1024];
1418 while (!eof_token(token
)) {
1419 const char *val
= show_token(token
);
1420 int len
= strlen(val
);
1422 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1423 sparse_error(token
->pos
, "too long token expansion");
1429 memcpy(ptr
, val
, len
);
1431 token
= token
->next
;
1432 whitespace
= token
->pos
.whitespace
;
1438 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1440 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1444 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1446 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
));
1450 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1453 * Do we have any non-system includes?
1454 * Clear them out if so..
1456 *sys_includepath
= NULL
;
1460 static inline void update_inc_ptrs(const char ***where
)
1463 if (*where
<= dirafter_includepath
) {
1464 dirafter_includepath
++;
1465 /* If this was the entry that we prepend, don't
1466 * rise the lower entries, even if they are at
1467 * the same level. */
1468 if (where
== &dirafter_includepath
)
1471 if (*where
<= sys_includepath
) {
1473 if (where
== &sys_includepath
)
1476 if (*where
<= isys_includepath
) {
1478 if (where
== &isys_includepath
)
1482 /* angle_includepath is actually never updated, since we
1483 * don't suppport -iquote rught now. May change some day. */
1484 if (*where
<= angle_includepath
) {
1485 angle_includepath
++;
1486 if (where
== &angle_includepath
)
1491 /* Add a path before 'where' and update the pointers associated with the
1492 * includepath array */
1493 static void add_path_entry(struct token
*token
, const char *path
,
1494 const char ***where
)
1499 /* Need one free entry.. */
1500 if (includepath
[INCLUDEPATHS
-2])
1501 error_die(token
->pos
, "too many include path entries");
1503 /* check that this is not a duplicate */
1506 if (strcmp(*dst
, path
) == 0)
1513 update_inc_ptrs(where
);
1516 * Move them all up starting at dst,
1517 * insert the new entry..
1520 const char *tmp
= *dst
;
1527 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1530 token
= token
->next
;
1531 if (eof_token(token
))
1533 if (token_type(token
) != TOKEN_STRING
) {
1534 warning(token
->pos
, "expected path string");
1537 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1541 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1544 token
= token
->next
;
1545 if (eof_token(token
))
1547 if (token_type(token
) != TOKEN_STRING
) {
1548 sparse_error(token
->pos
, "expected path string");
1551 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1555 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
1558 token
= token
->next
;
1559 if (eof_token(token
))
1561 if (token_type(token
) != TOKEN_STRING
) {
1562 sparse_error(token
->pos
, "expected path string");
1565 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1569 /* Add to end on includepath list - no pointer updates */
1570 static void add_dirafter_entry(struct token
*token
, const char *path
)
1572 const char **dst
= includepath
;
1574 /* Need one free entry.. */
1575 if (includepath
[INCLUDEPATHS
-2])
1576 error_die(token
->pos
, "too many include path entries");
1578 /* Add to the end */
1586 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1589 token
= token
->next
;
1590 if (eof_token(token
))
1592 if (token_type(token
) != TOKEN_STRING
) {
1593 sparse_error(token
->pos
, "expected path string");
1596 add_dirafter_entry(token
, token
->string
->data
);
1600 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1605 * Split the include path. Any directories specified with `-I'
1606 * options before `-I-' are searched only for headers requested with
1607 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1608 * If additional directories are specified with `-I' options after
1609 * the `-I-', those directories are searched for all `#include'
1611 * In addition, `-I-' inhibits the use of the directory of the current
1612 * file directory as the first search directory for `#include "FILE"'.
1614 quote_includepath
= includepath
+1;
1615 angle_includepath
= sys_includepath
;
1620 * We replace "#pragma xxx" with "__pragma__" in the token
1621 * stream. Just as an example.
1623 * We'll just #define that away for now, but the theory here
1624 * is that we can use this to insert arbitrary token sequences
1625 * to turn the pragmas into internal front-end sequences for
1626 * when we actually start caring about them.
1628 * So eventually this will turn into some kind of extended
1629 * __attribute__() like thing, except called __pragma__(xxx).
1631 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1633 struct token
*next
= *line
;
1635 token
->ident
= &pragma_ident
;
1636 token
->pos
.newline
= 1;
1637 token
->pos
.whitespace
= 1;
1645 * We ignore #line for now.
1647 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1652 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1654 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1659 static void init_preprocessor(void)
1662 int stream
= init_stream("preprocessor", -1, includepath
);
1665 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1667 { "define", handle_define
},
1668 { "weak_define", handle_weak_define
},
1669 { "strong_define", handle_strong_define
},
1670 { "undef", handle_undef
},
1671 { "strong_undef", handle_strong_undef
},
1672 { "warning", handle_warning
},
1673 { "error", handle_error
},
1674 { "include", handle_include
},
1675 { "include_next", handle_include_next
},
1676 { "pragma", handle_pragma
},
1677 { "line", handle_line
},
1679 // our internal preprocessor tokens
1680 { "nostdinc", handle_nostdinc
},
1681 { "add_include", handle_add_include
},
1682 { "add_isystem", handle_add_isystem
},
1683 { "add_system", handle_add_system
},
1684 { "add_dirafter", handle_add_dirafter
},
1685 { "split_include", handle_split_include
},
1687 { "ifdef", handle_ifdef
},
1688 { "ifndef", handle_ifndef
},
1689 { "else", handle_else
},
1690 { "endif", handle_endif
},
1691 { "if", handle_if
},
1692 { "elif", handle_elif
},
1695 for (i
= 0; i
< (sizeof (normal
) / sizeof (normal
[0])); i
++) {
1697 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1698 sym
->handler
= normal
[i
].handler
;
1701 for (i
= 0; i
< (sizeof (special
) / sizeof (special
[0])); i
++) {
1703 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1704 sym
->handler
= special
[i
].handler
;
1710 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1712 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1713 struct token
*token
= start
->next
;
1716 if (eof_token(token
))
1719 if (token_type(token
) == TOKEN_IDENT
) {
1720 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1722 handler
= sym
->handler
;
1723 is_normal
= sym
->normal
;
1725 handler
= handle_nondirective
;
1727 } else if (token_type(token
) == TOKEN_NUMBER
) {
1728 handler
= handle_line
;
1730 handler
= handle_nondirective
;
1734 dirty_stream(stream
);
1738 if (!handler(stream
, line
, token
)) /* all set */
1742 free_preprocessor_line(token
);
1745 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1747 struct token
*start
= *line
, *next
;
1748 struct token
**tp
= &start
->next
;
1752 if (next
->pos
.newline
)
1757 *tp
= &eof_token_entry
;
1758 handle_preprocessor_line(stream
, line
, start
);
1761 static void do_preprocess(struct token
**list
)
1765 while (!eof_token(next
= scan_next(list
))) {
1766 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1768 if (next
->pos
.newline
&& match_op(next
, '#')) {
1769 if (!next
->pos
.noexpand
) {
1770 preprocessor_line(stream
, list
);
1771 __free_token(next
); /* Free the '#' token */
1776 switch (token_type(next
)) {
1777 case TOKEN_STREAMEND
:
1778 if (stream
->top_if
) {
1779 nesting_error(stream
);
1780 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1781 stream
->top_if
= NULL
;
1785 stream
->constant
= CONSTANT_FILE_YES
;
1788 case TOKEN_STREAMBEGIN
:
1793 dirty_stream(stream
);
1794 if (false_nesting
) {
1800 if (token_type(next
) != TOKEN_IDENT
||
1801 expand_one_symbol(list
))
1807 struct token
* preprocess(struct token
*token
)
1810 init_preprocessor();
1811 do_preprocess(&token
);
1813 // Drop all expressions from preprocessing, they're not used any more.
1814 // This is not true when we have multiple files, though ;/
1815 // clear_expression_alloc();