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 /* Expand symbol 'sym' at '*list' */
86 static int expand(struct token
**, struct symbol
*);
88 static void replace_with_string(struct token
*token
, const char *str
)
90 int size
= strlen(str
) + 1;
91 struct string
*s
= __alloc_string(size
);
94 memcpy(s
->data
, str
, size
);
95 token_type(token
) = TOKEN_STRING
;
99 static void replace_with_integer(struct token
*token
, unsigned int val
)
101 char *buf
= __alloc_bytes(11);
102 sprintf(buf
, "%u", val
);
103 token_type(token
) = TOKEN_NUMBER
;
107 static struct symbol
*lookup_macro(struct ident
*ident
)
109 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_UNDEF
);
110 if (sym
&& sym
->namespace != NS_MACRO
)
115 static int token_defined(struct token
*token
)
117 if (token_type(token
) == TOKEN_IDENT
) {
118 struct symbol
*sym
= lookup_macro(token
->ident
);
120 sym
->used_in
= file_scope
;
126 sparse_error(token
->pos
, "expected preprocessor identifier");
130 static void replace_with_defined(struct token
*token
)
132 static const char *string
[] = { "0", "1" };
133 int defined
= token_defined(token
);
135 token_type(token
) = TOKEN_NUMBER
;
136 token
->number
= string
[defined
];
139 static int expand_one_symbol(struct token
**list
)
141 struct token
*token
= *list
;
143 static char buffer
[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
146 if (token
->pos
.noexpand
)
149 sym
= lookup_macro(token
->ident
);
151 sym
->used_in
= file_scope
;
152 return expand(list
, sym
);
154 if (token
->ident
== &__LINE___ident
) {
155 replace_with_integer(token
, token
->pos
.line
);
156 } else if (token
->ident
== &__FILE___ident
) {
157 replace_with_string(token
, stream_name(token
->pos
.stream
));
158 } else if (token
->ident
== &__DATE___ident
) {
161 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
162 replace_with_string(token
, buffer
);
163 } else if (token
->ident
== &__TIME___ident
) {
166 strftime(buffer
, 9, "%T", localtime(&t
));
167 replace_with_string(token
, buffer
);
172 static inline struct token
*scan_next(struct token
**where
)
174 struct token
*token
= *where
;
175 if (token_type(token
) != TOKEN_UNTAINT
)
178 token
->ident
->tainted
= 0;
180 } while (token_type(token
) == TOKEN_UNTAINT
);
185 static void expand_list(struct token
**list
)
188 while (!eof_token(next
= scan_next(list
))) {
189 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
194 static void preprocessor_line(struct stream
*stream
, struct token
**line
);
196 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
198 struct stream
*stream
= input_streams
+ prev
->pos
.stream
;
199 struct token
**p
= &prev
->next
;
203 while (!eof_token(next
= scan_next(p
))) {
204 if (next
->pos
.newline
&& match_op(next
, '#')) {
205 if (!next
->pos
.noexpand
) {
206 sparse_error(next
->pos
,
207 "directive in argument list");
208 preprocessor_line(stream
, p
);
209 __free_token(next
); /* Free the '#' token */
213 switch (token_type(next
)) {
214 case TOKEN_STREAMEND
:
215 case TOKEN_STREAMBEGIN
:
216 *p
= &eof_token_entry
;
224 if (match_op(next
, '(')) {
226 } else if (match_op(next
, ')')) {
229 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
232 next
->pos
.stream
= pos
->stream
;
233 next
->pos
.line
= pos
->line
;
234 next
->pos
.pos
= pos
->pos
;
237 *p
= &eof_token_entry
;
242 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
247 struct token
*expanded
;
254 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
256 int wanted
= arglist
->count
.normal
;
257 struct token
*next
= NULL
;
260 arglist
= arglist
->next
; /* skip counter */
263 next
= collect_arg(start
, 0, &what
->pos
);
266 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
271 for (count
= 0; count
< wanted
; count
++) {
272 struct argcount
*p
= &arglist
->next
->count
;
273 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
274 arglist
= arglist
->next
->next
;
277 args
[count
].arg
= start
->next
;
278 args
[count
].n_normal
= p
->normal
;
279 args
[count
].n_quoted
= p
->quoted
;
280 args
[count
].n_str
= p
->str
;
281 if (match_op(next
, ')')) {
287 if (count
== wanted
&& !match_op(next
, ')'))
289 if (count
== wanted
- 1) {
290 struct argcount
*p
= &arglist
->next
->count
;
293 args
[count
].arg
= NULL
;
294 args
[count
].n_normal
= p
->normal
;
295 args
[count
].n_quoted
= p
->quoted
;
296 args
[count
].n_str
= p
->str
;
298 if (count
< wanted
- 1)
301 what
->next
= next
->next
;
305 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
306 show_token(what
), wanted
, count
);
309 while (match_op(next
, ',')) {
310 next
= collect_arg(next
, 0, &what
->pos
);
315 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
316 show_token(what
), count
, wanted
);
319 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
322 what
->next
= next
->next
;
326 static struct token
*dup_list(struct token
*list
)
328 struct token
*res
= NULL
;
329 struct token
**p
= &res
;
331 while (!eof_token(list
)) {
332 struct token
*newtok
= __alloc_token(0);
341 static const char *show_token_sequence(struct token
*token
, int quote
)
343 static char buffer
[MAX_STRING
];
347 if (!token
&& !quote
)
349 while (!eof_token(token
)) {
350 const char *val
= quote
? quote_token(token
) : show_token(token
);
351 int len
= strlen(val
);
353 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
354 sparse_error(token
->pos
, "too long token expansion");
360 memcpy(ptr
, val
, len
);
363 whitespace
= token
->pos
.whitespace
;
369 static struct token
*stringify(struct token
*arg
)
371 const char *s
= show_token_sequence(arg
, 1);
372 int size
= strlen(s
)+1;
373 struct token
*token
= __alloc_token(0);
374 struct string
*string
= __alloc_string(size
);
376 memcpy(string
->data
, s
, size
);
377 string
->length
= size
;
378 token
->pos
= arg
->pos
;
379 token_type(token
) = TOKEN_STRING
;
380 token
->string
= string
;
381 token
->next
= &eof_token_entry
;
385 static void expand_arguments(int count
, struct arg
*args
)
388 for (i
= 0; i
< count
; i
++) {
389 struct token
*arg
= args
[i
].arg
;
391 arg
= &eof_token_entry
;
393 args
[i
].str
= stringify(arg
);
394 if (args
[i
].n_normal
) {
395 if (!args
[i
].n_quoted
) {
396 args
[i
].expanded
= arg
;
398 } else if (eof_token(arg
)) {
399 args
[i
].expanded
= arg
;
401 args
[i
].expanded
= dup_list(arg
);
403 expand_list(&args
[i
].expanded
);
409 * Possibly valid combinations:
410 * - ident + ident -> ident
411 * - ident + number -> ident unless number contains '.', '+' or '-'.
412 * - 'L' + char constant -> wide char constant
413 * - 'L' + string literal -> wide string literal
414 * - number + number -> number
415 * - number + ident -> number
416 * - number + '.' -> number
417 * - number + '+' or '-' -> number, if number used to end on [eEpP].
418 * - '.' + number -> number, if number used to start with a digit.
419 * - special + special -> either special or an error.
421 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
424 enum token_type t1
= token_type(left
), t2
= token_type(right
);
426 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
429 if (t1
== TOKEN_IDENT
&& left
->ident
== &L_ident
) {
430 if (t2
>= TOKEN_CHAR
&& t2
< TOKEN_WIDE_CHAR
)
431 return t2
+ TOKEN_WIDE_CHAR
- TOKEN_CHAR
;
432 if (t2
== TOKEN_STRING
)
433 return TOKEN_WIDE_STRING
;
436 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
439 strcpy(p
, show_token(left
));
440 strcat(p
, show_token(right
));
446 if (t1
== TOKEN_IDENT
) {
447 if (t2
== TOKEN_SPECIAL
)
449 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
454 if (t1
== TOKEN_NUMBER
) {
455 if (t2
== TOKEN_SPECIAL
) {
456 switch (right
->special
) {
460 if (strchr("eEpP", p
[len
- 2]))
469 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
472 return TOKEN_SPECIAL
;
475 static int merge(struct token
*left
, struct token
*right
)
477 static char buffer
[512];
478 enum token_type res
= combine(left
, right
, buffer
);
483 left
->ident
= built_in_ident(buffer
);
484 left
->pos
.noexpand
= 0;
488 char *number
= __alloc_bytes(strlen(buffer
) + 1);
489 memcpy(number
, buffer
, strlen(buffer
) + 1);
490 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
491 left
->number
= number
;
496 if (buffer
[2] && buffer
[3])
498 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
499 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
506 case TOKEN_WIDE_CHAR
:
507 case TOKEN_WIDE_STRING
:
508 token_type(left
) = res
;
509 left
->pos
.noexpand
= 0;
510 left
->string
= right
->string
;
513 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
514 token_type(left
) = res
;
515 left
->pos
.noexpand
= 0;
516 memcpy(left
->embedded
, right
->embedded
, 4);
522 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
526 static struct token
*dup_token(struct token
*token
, struct position
*streampos
)
528 struct token
*alloc
= alloc_token(streampos
);
529 token_type(alloc
) = token_type(token
);
530 alloc
->pos
.newline
= token
->pos
.newline
;
531 alloc
->pos
.whitespace
= token
->pos
.whitespace
;
532 alloc
->number
= token
->number
;
533 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
537 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
539 int need_copy
= --*count
;
540 while (!eof_token(list
)) {
543 token
= dup_token(list
, &list
->pos
);
546 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
547 token
->pos
.noexpand
= 1;
549 where
= &token
->next
;
552 *where
= &eof_token_entry
;
556 static int handle_kludge(struct token
**p
, struct arg
*args
)
558 struct token
*t
= (*p
)->next
->next
;
560 struct arg
*v
= &args
[t
->argnum
];
561 if (token_type(t
->next
) != TOKEN_CONCAT
) {
563 /* ignore the first ## */
567 /* skip the entire thing */
571 if (v
->arg
&& !eof_token(v
->arg
))
572 return 0; /* no magic */
577 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
579 struct position
*base_pos
= &(*list
)->pos
;
581 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
583 for (; !eof_token(body
); body
= body
->next
) {
584 struct token
*added
, *arg
;
588 switch (token_type(body
)) {
589 case TOKEN_GNU_KLUDGE
:
591 * GNU kludge: if we had <comma>##<vararg>, behaviour
592 * depends on whether we had enough arguments to have
593 * a vararg. If we did, ## is just ignored. Otherwise
594 * both , and ## are ignored. Worse, there can be
595 * an arbitrary number of ##<arg> in between; if all of
596 * those are empty, we act as if they hadn't been there,
597 * otherwise we act as if the kludge didn't exist.
600 if (handle_kludge(&body
, args
)) {
607 added
= dup_token(t
, base_pos
);
608 token_type(added
) = TOKEN_SPECIAL
;
612 case TOKEN_STR_ARGUMENT
:
613 arg
= args
[body
->argnum
].str
;
614 count
= &args
[body
->argnum
].n_str
;
617 case TOKEN_QUOTED_ARGUMENT
:
618 arg
= args
[body
->argnum
].arg
;
619 count
= &args
[body
->argnum
].n_quoted
;
620 if (!arg
|| eof_token(arg
)) {
629 case TOKEN_MACRO_ARGUMENT
:
630 arg
= args
[body
->argnum
].expanded
;
631 count
= &args
[body
->argnum
].n_normal
;
632 if (eof_token(arg
)) {
637 tail
= copy(&added
, arg
, count
);
638 added
->pos
.newline
= body
->pos
.newline
;
639 added
->pos
.whitespace
= body
->pos
.whitespace
;
643 if (state
== Placeholder
)
650 added
= dup_token(body
, base_pos
);
651 if (added
->ident
->tainted
)
652 added
->pos
.noexpand
= 1;
657 added
= dup_token(body
, base_pos
);
663 * if we got to doing real concatenation, we already have
664 * added something into the list, so containing_token() is OK.
666 if (state
== Concat
&& merge(containing_token(list
), added
)) {
668 if (tail
!= &added
->next
)
676 *list
= &eof_token_entry
;
680 static int expand(struct token
**list
, struct symbol
*sym
)
683 struct token
*token
= *list
;
684 struct ident
*expanding
= token
->ident
;
686 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
687 struct arg args
[nargs
];
689 if (expanding
->tainted
) {
690 token
->pos
.noexpand
= 1;
695 if (!match_op(scan_next(&token
->next
), '('))
697 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
699 expand_arguments(nargs
, args
);
702 expanding
->tainted
= 1;
705 tail
= substitute(list
, sym
->expansion
, args
);
707 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
708 * We still can lose the newline flag if the sucker expands to nothing,
709 * but the price of dealing with that is probably too high (we'd need
710 * to collect the flags during scan_next())
712 (*list
)->pos
.newline
= token
->pos
.newline
;
713 (*list
)->pos
.whitespace
= token
->pos
.whitespace
;
719 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
721 static char buffer
[256];
724 while (!eof_token(token
) && !match_op(token
, endop
)) {
726 const char *val
= token
->string
->data
;
727 if (token_type(token
) != TOKEN_STRING
)
728 val
= show_token(token
);
730 memcpy(ptr
, val
, len
);
735 if (endop
&& !match_op(token
, endop
))
736 sparse_error(start
->pos
, "expected '>' at end of filename");
740 static int already_tokenized(const char *path
)
744 for (stream
= *hash_stream(path
); stream
>= 0 ; stream
= next
) {
745 struct stream
*s
= input_streams
+ stream
;
747 next
= s
->next_stream
;
748 if (s
->constant
!= CONSTANT_FILE_YES
)
750 if (strcmp(path
, s
->name
))
752 if (s
->protect
&& !lookup_macro(s
->protect
))
759 /* Handle include of header files.
760 * The relevant options are made compatible with gcc. The only options that
761 * are not supported is -withprefix and friends.
763 * Three set of include paths are known:
764 * quote_includepath: Path to search when using #include "file.h"
765 * angle_includepath: Paths to search when using #include <file.h>
766 * isys_includepath: Paths specified with -isystem, come before the
767 * built-in system include paths. Gcc would suppress
768 * warnings from system headers. Here we separate
769 * them from the angle_ ones to keep search ordering.
771 * sys_includepath: Built-in include paths.
772 * dirafter_includepath Paths added with -dirafter.
774 * The above is implemented as one array with pointers
776 * quote_includepath ---> | |
780 * angle_includepath ---> | |
782 * isys_includepath ---> | |
784 * sys_includepath ---> | |
786 * dirafter_includepath -> | |
789 * -I dir insert dir just before isys_includepath and move the rest
790 * -I- makes all dirs specified with -I before to quote dirs only and
791 * angle_includepath is set equal to isys_includepath.
792 * -nostdinc removes all sys dirs by storing NULL in entry pointed
793 * to by * sys_includepath. Note that this will reset all dirs built-in
794 * and added before -nostdinc by -isystem and -idirafter.
795 * -isystem dir adds dir where isys_includepath points adding this dir as
797 * -idirafter dir adds dir to the end of the list
800 static void set_stream_include_path(struct stream
*stream
)
802 const char *path
= stream
->path
;
804 const char *p
= strrchr(stream
->name
, '/');
807 int len
= p
- stream
->name
+ 1;
808 char *m
= malloc(len
+1);
809 /* This includes the final "/" */
810 memcpy(m
, stream
->name
, len
);
816 includepath
[0] = path
;
819 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
822 int plen
= strlen(path
);
823 static char fullname
[PATH_MAX
];
825 memcpy(fullname
, path
, plen
);
826 if (plen
&& path
[plen
-1] != '/') {
827 fullname
[plen
] = '/';
830 memcpy(fullname
+plen
, filename
, flen
);
831 if (already_tokenized(fullname
))
833 fd
= open(fullname
, O_RDONLY
);
835 char * streamname
= __alloc_bytes(plen
+ flen
);
836 memcpy(streamname
, fullname
, plen
+ flen
);
837 *where
= tokenize(streamname
, fd
, *where
, next_path
);
844 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
848 while ((path
= *pptr
++) != NULL
) {
849 if (!try_include(path
, filename
, flen
, list
, pptr
))
856 static int free_preprocessor_line(struct token
*token
)
858 while (token_type(token
) != TOKEN_EOF
) {
859 struct token
*free
= token
;
866 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, int how
)
868 const char *filename
;
876 if (!match_op(next
, '<')) {
877 expand_list(&token
->next
);
880 if (match_op(token
->next
, '<')) {
887 filename
= token_name_sequence(token
, expect
, token
);
888 flen
= strlen(filename
) + 1;
891 if (filename
[0] == '/') {
892 if (try_include("", filename
, flen
, list
, includepath
))
899 path
= stream
->next_path
;
906 /* Dir of input file is first dir to search for quoted includes */
907 set_stream_include_path(stream
);
908 path
= expect
? angle_includepath
: quote_includepath
;
911 /* Check the standard include paths.. */
912 if (do_include_path(path
, list
, token
, filename
, flen
))
915 error_die(token
->pos
, "unable to open '%s'", filename
);
918 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
920 return handle_include_path(stream
, list
, token
, 0);
923 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
925 return handle_include_path(stream
, list
, token
, 1);
928 static int handle_argv_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
930 return handle_include_path(stream
, list
, token
, 2);
933 static int token_different(struct token
*t1
, struct token
*t2
)
937 if (token_type(t1
) != token_type(t2
))
940 switch (token_type(t1
)) {
942 different
= t1
->ident
!= t2
->ident
;
944 case TOKEN_ARG_COUNT
:
947 case TOKEN_GNU_KLUDGE
:
951 different
= strcmp(t1
->number
, t2
->number
);
954 different
= t1
->special
!= t2
->special
;
956 case TOKEN_MACRO_ARGUMENT
:
957 case TOKEN_QUOTED_ARGUMENT
:
958 case TOKEN_STR_ARGUMENT
:
959 different
= t1
->argnum
!= t2
->argnum
;
961 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
962 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
963 different
= memcmp(t1
->embedded
, t2
->embedded
, 4);
966 case TOKEN_WIDE_CHAR
:
968 case TOKEN_WIDE_STRING
: {
969 struct string
*s1
, *s2
;
974 if (s1
->length
!= s2
->length
)
976 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
986 static int token_list_different(struct token
*list1
, struct token
*list2
)
991 if (!list1
|| !list2
)
993 if (token_different(list1
, list2
))
1000 static inline void set_arg_count(struct token
*token
)
1002 token_type(token
) = TOKEN_ARG_COUNT
;
1003 token
->count
.normal
= token
->count
.quoted
=
1004 token
->count
.str
= token
->count
.vararg
= 0;
1007 static struct token
*parse_arguments(struct token
*list
)
1009 struct token
*arg
= list
->next
, *next
= list
;
1010 struct argcount
*count
= &list
->count
;
1012 set_arg_count(list
);
1014 if (match_op(arg
, ')')) {
1016 list
->next
= &eof_token_entry
;
1020 while (token_type(arg
) == TOKEN_IDENT
) {
1021 if (arg
->ident
== &__VA_ARGS___ident
)
1023 if (!++count
->normal
)
1027 if (match_op(next
, ',')) {
1028 set_arg_count(next
);
1033 if (match_op(next
, ')')) {
1034 set_arg_count(next
);
1036 arg
->next
->next
= &eof_token_entry
;
1040 /* normal cases are finished here */
1042 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
1043 if (match_op(next
->next
, ')')) {
1044 set_arg_count(next
);
1045 next
->count
.vararg
= 1;
1047 arg
->next
->next
= &eof_token_entry
;
1055 if (eof_token(next
)) {
1063 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
1065 token_type(arg
) = TOKEN_IDENT
;
1066 arg
->ident
= &__VA_ARGS___ident
;
1067 if (!match_op(next
, ')'))
1069 if (!++count
->normal
)
1071 set_arg_count(next
);
1072 next
->count
.vararg
= 1;
1074 arg
->next
->next
= &eof_token_entry
;
1078 if (eof_token(arg
)) {
1082 if (match_op(arg
, ','))
1089 sparse_error(arg
->pos
, "parameter name missing");
1092 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
1096 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1099 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1102 sparse_error(arg
->pos
, "too many arguments in macro definition");
1106 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1108 struct ident
*ident
= token
->ident
;
1111 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1114 arglist
= arglist
->next
;
1116 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1117 if (arglist
->ident
== ident
) {
1118 struct argcount
*count
= &arglist
->next
->count
;
1122 token_type(token
) = type
;
1124 case TOKEN_MACRO_ARGUMENT
:
1125 n
= ++count
->normal
;
1127 case TOKEN_QUOTED_ARGUMENT
:
1128 n
= ++count
->quoted
;
1134 return count
->vararg
? 2 : 1;
1136 * XXX - need saner handling of that
1137 * (>= 1024 instances of argument)
1139 token_type(token
) = TOKEN_ERROR
;
1146 static struct token
*handle_hash(struct token
**p
, struct token
*arglist
)
1148 struct token
*token
= *p
;
1150 struct token
*next
= token
->next
;
1151 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1153 next
->pos
.whitespace
= token
->pos
.whitespace
;
1154 __free_token(token
);
1157 token
->pos
.noexpand
= 1;
1162 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1166 /* token->next is ## */
1167 static struct token
*handle_hashhash(struct token
*token
, struct token
*arglist
)
1169 struct token
*last
= token
;
1170 struct token
*concat
;
1171 int state
= match_op(token
, ',');
1173 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1179 /* eat duplicate ## */
1180 concat
= token
->next
;
1181 while (match_op(t
= concat
->next
, SPECIAL_HASHHASH
)) {
1183 __free_token(concat
);
1186 token_type(concat
) = TOKEN_CONCAT
;
1191 if (match_op(t
, '#')) {
1192 t
= handle_hash(&concat
->next
, arglist
);
1197 is_arg
= try_arg(t
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1199 if (state
== 1 && is_arg
) {
1203 state
= match_op(t
, ',');
1207 if (!match_op(token
->next
, SPECIAL_HASHHASH
))
1210 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1212 token_type(last
) = TOKEN_GNU_KLUDGE
;
1216 sparse_error(concat
->pos
, "'##' cannot appear at the ends of macro expansion");
1220 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1222 struct token
*token
= expansion
;
1225 if (match_op(token
, SPECIAL_HASHHASH
))
1228 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1229 if (match_op(token
, '#')) {
1230 token
= handle_hash(p
, arglist
);
1234 if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1235 token
= handle_hashhash(token
, arglist
);
1239 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1241 if (token_type(token
) == TOKEN_ERROR
)
1244 token
= alloc_token(&expansion
->pos
);
1245 token_type(token
) = TOKEN_UNTAINT
;
1246 token
->ident
= name
;
1252 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1255 sparse_error(token
->pos
, "too many instances of argument in body");
1259 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1261 struct token
*arglist
, *expansion
;
1262 struct token
*left
= token
->next
;
1267 if (token_type(left
) != TOKEN_IDENT
) {
1268 sparse_error(token
->pos
, "expected identifier to 'define'");
1275 expansion
= left
->next
;
1276 if (!expansion
->pos
.whitespace
) {
1277 if (match_op(expansion
, '(')) {
1278 arglist
= expansion
;
1279 expansion
= parse_arguments(expansion
);
1282 } else if (!eof_token(expansion
)) {
1283 warning(expansion
->pos
,
1284 "no whitespace before object-like macro body");
1288 expansion
= parse_expansion(expansion
, arglist
, name
);
1293 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1297 if (attr
< sym
->attr
)
1300 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1302 if (token_list_different(sym
->expansion
, expansion
) ||
1303 token_list_different(sym
->arglist
, arglist
)) {
1305 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1306 || sym
->used_in
== file_scope
) {
1307 warning(left
->pos
, "preprocessor token %.*s redefined",
1308 name
->len
, name
->name
);
1309 info(sym
->pos
, "this was the original definition");
1315 if (!sym
|| sym
->scope
!= file_scope
) {
1316 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1317 bind_symbol(sym
, name
, NS_MACRO
);
1322 sym
->expansion
= expansion
;
1323 sym
->arglist
= arglist
;
1324 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1327 sym
->namespace = NS_MACRO
;
1328 sym
->used_in
= NULL
;
1334 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1336 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1339 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1341 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1344 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1346 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1349 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1351 struct token
*left
= token
->next
;
1354 if (token_type(left
) != TOKEN_IDENT
) {
1355 sparse_error(token
->pos
, "expected identifier to 'undef'");
1359 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1361 if (attr
< sym
->attr
)
1363 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1365 } else if (attr
<= SYM_ATTR_NORMAL
)
1368 if (!sym
|| sym
->scope
!= file_scope
) {
1369 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1370 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1373 sym
->namespace = NS_UNDEF
;
1374 sym
->used_in
= NULL
;
1380 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1382 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1385 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1387 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1390 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1392 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1393 free_preprocessor_line(token
->next
);
1394 token
->next
= stream
->top_if
;
1395 stream
->top_if
= token
;
1396 if (false_nesting
|| true != 1)
1401 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1403 struct token
*next
= token
->next
;
1405 if (token_type(next
) == TOKEN_IDENT
) {
1406 arg
= token_defined(next
);
1408 dirty_stream(stream
);
1410 sparse_error(token
->pos
, "expected preprocessor identifier");
1413 return preprocessor_if(stream
, token
, arg
);
1416 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1418 struct token
*next
= token
->next
;
1420 if (token_type(next
) == TOKEN_IDENT
) {
1421 if (!stream
->dirty
&& !stream
->ifndef
) {
1422 if (!stream
->protect
) {
1423 stream
->ifndef
= token
;
1424 stream
->protect
= next
->ident
;
1425 } else if (stream
->protect
== next
->ident
) {
1426 stream
->ifndef
= token
;
1430 arg
= !token_defined(next
);
1432 dirty_stream(stream
);
1434 sparse_error(token
->pos
, "expected preprocessor identifier");
1438 return preprocessor_if(stream
, token
, arg
);
1441 static const char *show_token_sequence(struct token
*token
, int quote
);
1444 * Expression handling for #if and #elif; it differs from normal expansion
1445 * due to special treatment of "defined".
1447 static int expression_value(struct token
**where
)
1449 struct expression
*expr
;
1451 struct token
**list
= where
, **beginning
= NULL
;
1455 while (!eof_token(p
= scan_next(list
))) {
1458 if (token_type(p
) != TOKEN_IDENT
)
1460 if (p
->ident
== &defined_ident
) {
1465 if (!expand_one_symbol(list
))
1467 if (token_type(p
) != TOKEN_IDENT
)
1469 token_type(p
) = TOKEN_ZERO_IDENT
;
1472 if (match_op(p
, '(')) {
1476 replace_with_defined(p
);
1481 if (token_type(p
) == TOKEN_IDENT
)
1485 replace_with_defined(p
);
1490 if (!match_op(p
, ')'))
1491 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1498 p
= constant_expression(*where
, &expr
);
1500 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
, 0));
1501 value
= get_expression_value(expr
);
1505 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1509 value
= expression_value(&token
->next
);
1511 dirty_stream(stream
);
1512 return preprocessor_if(stream
, token
, value
);
1515 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1517 struct token
*top_if
= stream
->top_if
;
1521 nesting_error(stream
);
1522 sparse_error(token
->pos
, "unmatched #elif within stream");
1526 if (token_type(top_if
) == TOKEN_ELSE
) {
1527 nesting_error(stream
);
1528 sparse_error(token
->pos
, "#elif after #else");
1534 dirty_stream(stream
);
1535 if (token_type(top_if
) != TOKEN_IF
)
1537 if (false_nesting
) {
1539 if (!expression_value(&token
->next
))
1543 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1548 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1550 struct token
*top_if
= stream
->top_if
;
1554 nesting_error(stream
);
1555 sparse_error(token
->pos
, "unmatched #else within stream");
1559 if (token_type(top_if
) == TOKEN_ELSE
) {
1560 nesting_error(stream
);
1561 sparse_error(token
->pos
, "#else after #else");
1563 if (false_nesting
) {
1564 if (token_type(top_if
) == TOKEN_IF
)
1569 token_type(top_if
) = TOKEN_ELSE
;
1573 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1575 struct token
*top_if
= stream
->top_if
;
1578 nesting_error(stream
);
1579 sparse_error(token
->pos
, "unmatched #endif in stream");
1584 stream
->top_if
= top_if
->next
;
1585 __free_token(top_if
);
1589 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1591 warning(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1595 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1597 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1601 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1604 * Do we have any non-system includes?
1605 * Clear them out if so..
1607 *sys_includepath
= NULL
;
1611 static inline void update_inc_ptrs(const char ***where
)
1614 if (*where
<= dirafter_includepath
) {
1615 dirafter_includepath
++;
1616 /* If this was the entry that we prepend, don't
1617 * rise the lower entries, even if they are at
1618 * the same level. */
1619 if (where
== &dirafter_includepath
)
1622 if (*where
<= sys_includepath
) {
1624 if (where
== &sys_includepath
)
1627 if (*where
<= isys_includepath
) {
1629 if (where
== &isys_includepath
)
1633 /* angle_includepath is actually never updated, since we
1634 * don't suppport -iquote rught now. May change some day. */
1635 if (*where
<= angle_includepath
) {
1636 angle_includepath
++;
1637 if (where
== &angle_includepath
)
1642 /* Add a path before 'where' and update the pointers associated with the
1643 * includepath array */
1644 static void add_path_entry(struct token
*token
, const char *path
,
1645 const char ***where
)
1650 /* Need one free entry.. */
1651 if (includepath
[INCLUDEPATHS
-2])
1652 error_die(token
->pos
, "too many include path entries");
1654 /* check that this is not a duplicate */
1657 if (strcmp(*dst
, path
) == 0)
1664 update_inc_ptrs(where
);
1667 * Move them all up starting at dst,
1668 * insert the new entry..
1671 const char *tmp
= *dst
;
1678 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1681 token
= token
->next
;
1682 if (eof_token(token
))
1684 if (token_type(token
) != TOKEN_STRING
) {
1685 warning(token
->pos
, "expected path string");
1688 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1692 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1695 token
= token
->next
;
1696 if (eof_token(token
))
1698 if (token_type(token
) != TOKEN_STRING
) {
1699 sparse_error(token
->pos
, "expected path string");
1702 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1706 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
1709 token
= token
->next
;
1710 if (eof_token(token
))
1712 if (token_type(token
) != TOKEN_STRING
) {
1713 sparse_error(token
->pos
, "expected path string");
1716 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1720 /* Add to end on includepath list - no pointer updates */
1721 static void add_dirafter_entry(struct token
*token
, const char *path
)
1723 const char **dst
= includepath
;
1725 /* Need one free entry.. */
1726 if (includepath
[INCLUDEPATHS
-2])
1727 error_die(token
->pos
, "too many include path entries");
1729 /* Add to the end */
1737 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1740 token
= token
->next
;
1741 if (eof_token(token
))
1743 if (token_type(token
) != TOKEN_STRING
) {
1744 sparse_error(token
->pos
, "expected path string");
1747 add_dirafter_entry(token
, token
->string
->data
);
1751 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1756 * Split the include path. Any directories specified with `-I'
1757 * options before `-I-' are searched only for headers requested with
1758 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1759 * If additional directories are specified with `-I' options after
1760 * the `-I-', those directories are searched for all `#include'
1762 * In addition, `-I-' inhibits the use of the directory of the current
1763 * file directory as the first search directory for `#include "FILE"'.
1765 quote_includepath
= includepath
+1;
1766 angle_includepath
= sys_includepath
;
1771 * We replace "#pragma xxx" with "__pragma__" in the token
1772 * stream. Just as an example.
1774 * We'll just #define that away for now, but the theory here
1775 * is that we can use this to insert arbitrary token sequences
1776 * to turn the pragmas into internal front-end sequences for
1777 * when we actually start caring about them.
1779 * So eventually this will turn into some kind of extended
1780 * __attribute__() like thing, except called __pragma__(xxx).
1782 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1784 struct token
*next
= *line
;
1786 token
->ident
= &pragma_ident
;
1787 token
->pos
.newline
= 1;
1788 token
->pos
.whitespace
= 1;
1796 * We ignore #line for now.
1798 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1803 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1805 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
, 0));
1810 static void init_preprocessor(void)
1813 int stream
= init_stream("preprocessor", -1, includepath
);
1816 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1818 { "define", handle_define
},
1819 { "weak_define", handle_weak_define
},
1820 { "strong_define", handle_strong_define
},
1821 { "undef", handle_undef
},
1822 { "strong_undef", handle_strong_undef
},
1823 { "warning", handle_warning
},
1824 { "error", handle_error
},
1825 { "include", handle_include
},
1826 { "include_next", handle_include_next
},
1827 { "pragma", handle_pragma
},
1828 { "line", handle_line
},
1830 // our internal preprocessor tokens
1831 { "nostdinc", handle_nostdinc
},
1832 { "add_include", handle_add_include
},
1833 { "add_isystem", handle_add_isystem
},
1834 { "add_system", handle_add_system
},
1835 { "add_dirafter", handle_add_dirafter
},
1836 { "split_include", handle_split_include
},
1837 { "argv_include", handle_argv_include
},
1839 { "ifdef", handle_ifdef
},
1840 { "ifndef", handle_ifndef
},
1841 { "else", handle_else
},
1842 { "endif", handle_endif
},
1843 { "if", handle_if
},
1844 { "elif", handle_elif
},
1847 for (i
= 0; i
< ARRAY_SIZE(normal
); i
++) {
1849 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1850 sym
->handler
= normal
[i
].handler
;
1853 for (i
= 0; i
< ARRAY_SIZE(special
); i
++) {
1855 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1856 sym
->handler
= special
[i
].handler
;
1862 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1864 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1865 struct token
*token
= start
->next
;
1868 if (eof_token(token
))
1871 if (token_type(token
) == TOKEN_IDENT
) {
1872 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1874 handler
= sym
->handler
;
1875 is_normal
= sym
->normal
;
1877 handler
= handle_nondirective
;
1879 } else if (token_type(token
) == TOKEN_NUMBER
) {
1880 handler
= handle_line
;
1882 handler
= handle_nondirective
;
1886 dirty_stream(stream
);
1890 if (!handler(stream
, line
, token
)) /* all set */
1894 free_preprocessor_line(token
);
1897 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1899 struct token
*start
= *line
, *next
;
1900 struct token
**tp
= &start
->next
;
1904 if (next
->pos
.newline
)
1909 *tp
= &eof_token_entry
;
1910 handle_preprocessor_line(stream
, line
, start
);
1913 static void do_preprocess(struct token
**list
)
1917 while (!eof_token(next
= scan_next(list
))) {
1918 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1920 if (next
->pos
.newline
&& match_op(next
, '#')) {
1921 if (!next
->pos
.noexpand
) {
1922 preprocessor_line(stream
, list
);
1923 __free_token(next
); /* Free the '#' token */
1928 switch (token_type(next
)) {
1929 case TOKEN_STREAMEND
:
1930 if (stream
->top_if
) {
1931 nesting_error(stream
);
1932 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1933 stream
->top_if
= NULL
;
1937 stream
->constant
= CONSTANT_FILE_YES
;
1940 case TOKEN_STREAMBEGIN
:
1945 dirty_stream(stream
);
1946 if (false_nesting
) {
1952 if (token_type(next
) != TOKEN_IDENT
||
1953 expand_one_symbol(list
))
1959 struct token
* preprocess(struct token
*token
)
1962 init_preprocessor();
1963 do_preprocess(&token
);
1965 // Drop all expressions from preprocessing, they're not used any more.
1966 // This is not true when we have multiple files, though ;/
1967 // clear_expression_alloc();