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
;
749 if (strcmp(path
, s
->name
))
753 if (s
->constant
!= CONSTANT_FILE_YES
)
755 if (strcmp(path
, s
->name
))
757 if (s
->protect
&& !lookup_macro(s
->protect
))
764 /* Handle include of header files.
765 * The relevant options are made compatible with gcc. The only options that
766 * are not supported is -withprefix and friends.
768 * Three set of include paths are known:
769 * quote_includepath: Path to search when using #include "file.h"
770 * angle_includepath: Paths to search when using #include <file.h>
771 * isys_includepath: Paths specified with -isystem, come before the
772 * built-in system include paths. Gcc would suppress
773 * warnings from system headers. Here we separate
774 * them from the angle_ ones to keep search ordering.
776 * sys_includepath: Built-in include paths.
777 * dirafter_includepath Paths added with -dirafter.
779 * The above is implemented as one array with pointers
781 * quote_includepath ---> | |
785 * angle_includepath ---> | |
787 * isys_includepath ---> | |
789 * sys_includepath ---> | |
791 * dirafter_includepath -> | |
794 * -I dir insert dir just before isys_includepath and move the rest
795 * -I- makes all dirs specified with -I before to quote dirs only and
796 * angle_includepath is set equal to isys_includepath.
797 * -nostdinc removes all sys dirs by storing NULL in entry pointed
798 * to by * sys_includepath. Note that this will reset all dirs built-in
799 * and added before -nostdinc by -isystem and -idirafter.
800 * -isystem dir adds dir where isys_includepath points adding this dir as
802 * -idirafter dir adds dir to the end of the list
805 static void set_stream_include_path(struct stream
*stream
)
807 const char *path
= stream
->path
;
809 const char *p
= strrchr(stream
->name
, '/');
812 int len
= p
- stream
->name
+ 1;
813 char *m
= malloc(len
+1);
814 /* This includes the final "/" */
815 memcpy(m
, stream
->name
, len
);
821 includepath
[0] = path
;
824 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
827 int plen
= strlen(path
);
828 static char fullname
[PATH_MAX
];
830 memcpy(fullname
, path
, plen
);
831 if (plen
&& path
[plen
-1] != '/') {
832 fullname
[plen
] = '/';
835 memcpy(fullname
+plen
, filename
, flen
);
836 if (already_tokenized(fullname
))
838 fd
= open(fullname
, O_RDONLY
);
840 char * streamname
= __alloc_bytes(plen
+ flen
);
841 memcpy(streamname
, fullname
, plen
+ flen
);
842 *where
= tokenize(streamname
, fd
, *where
, next_path
);
849 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
853 while ((path
= *pptr
++) != NULL
) {
854 if (!try_include(path
, filename
, flen
, list
, pptr
))
861 static int free_preprocessor_line(struct token
*token
)
863 while (token_type(token
) != TOKEN_EOF
) {
864 struct token
*free
= token
;
871 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, int how
)
873 const char *filename
;
881 if (!match_op(next
, '<')) {
882 expand_list(&token
->next
);
885 if (match_op(token
->next
, '<')) {
892 filename
= token_name_sequence(token
, expect
, token
);
893 flen
= strlen(filename
) + 1;
896 if (filename
[0] == '/') {
897 if (try_include("", filename
, flen
, list
, includepath
))
904 path
= stream
->next_path
;
911 /* Dir of input file is first dir to search for quoted includes */
912 set_stream_include_path(stream
);
913 path
= expect
? angle_includepath
: quote_includepath
;
916 /* Check the standard include paths.. */
917 if (do_include_path(path
, list
, token
, filename
, flen
))
920 error_die(token
->pos
, "unable to open '%s'", filename
);
923 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
925 return handle_include_path(stream
, list
, token
, 0);
928 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
930 return handle_include_path(stream
, list
, token
, 1);
933 static int handle_argv_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
935 return handle_include_path(stream
, list
, token
, 2);
938 static int token_different(struct token
*t1
, struct token
*t2
)
942 if (token_type(t1
) != token_type(t2
))
945 switch (token_type(t1
)) {
947 different
= t1
->ident
!= t2
->ident
;
949 case TOKEN_ARG_COUNT
:
952 case TOKEN_GNU_KLUDGE
:
956 different
= strcmp(t1
->number
, t2
->number
);
959 different
= t1
->special
!= t2
->special
;
961 case TOKEN_MACRO_ARGUMENT
:
962 case TOKEN_QUOTED_ARGUMENT
:
963 case TOKEN_STR_ARGUMENT
:
964 different
= t1
->argnum
!= t2
->argnum
;
966 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
967 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
968 different
= memcmp(t1
->embedded
, t2
->embedded
, 4);
971 case TOKEN_WIDE_CHAR
:
973 case TOKEN_WIDE_STRING
: {
974 struct string
*s1
, *s2
;
979 if (s1
->length
!= s2
->length
)
981 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
991 static int token_list_different(struct token
*list1
, struct token
*list2
)
996 if (!list1
|| !list2
)
998 if (token_different(list1
, list2
))
1000 list1
= list1
->next
;
1001 list2
= list2
->next
;
1005 static inline void set_arg_count(struct token
*token
)
1007 token_type(token
) = TOKEN_ARG_COUNT
;
1008 token
->count
.normal
= token
->count
.quoted
=
1009 token
->count
.str
= token
->count
.vararg
= 0;
1012 static struct token
*parse_arguments(struct token
*list
)
1014 struct token
*arg
= list
->next
, *next
= list
;
1015 struct argcount
*count
= &list
->count
;
1017 set_arg_count(list
);
1019 if (match_op(arg
, ')')) {
1021 list
->next
= &eof_token_entry
;
1025 while (token_type(arg
) == TOKEN_IDENT
) {
1026 if (arg
->ident
== &__VA_ARGS___ident
)
1028 if (!++count
->normal
)
1032 if (match_op(next
, ',')) {
1033 set_arg_count(next
);
1038 if (match_op(next
, ')')) {
1039 set_arg_count(next
);
1041 arg
->next
->next
= &eof_token_entry
;
1045 /* normal cases are finished here */
1047 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
1048 if (match_op(next
->next
, ')')) {
1049 set_arg_count(next
);
1050 next
->count
.vararg
= 1;
1052 arg
->next
->next
= &eof_token_entry
;
1060 if (eof_token(next
)) {
1068 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
1070 token_type(arg
) = TOKEN_IDENT
;
1071 arg
->ident
= &__VA_ARGS___ident
;
1072 if (!match_op(next
, ')'))
1074 if (!++count
->normal
)
1076 set_arg_count(next
);
1077 next
->count
.vararg
= 1;
1079 arg
->next
->next
= &eof_token_entry
;
1083 if (eof_token(arg
)) {
1087 if (match_op(arg
, ','))
1094 sparse_error(arg
->pos
, "parameter name missing");
1097 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
1101 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1104 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1107 sparse_error(arg
->pos
, "too many arguments in macro definition");
1111 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1113 struct ident
*ident
= token
->ident
;
1116 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1119 arglist
= arglist
->next
;
1121 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1122 if (arglist
->ident
== ident
) {
1123 struct argcount
*count
= &arglist
->next
->count
;
1127 token_type(token
) = type
;
1129 case TOKEN_MACRO_ARGUMENT
:
1130 n
= ++count
->normal
;
1132 case TOKEN_QUOTED_ARGUMENT
:
1133 n
= ++count
->quoted
;
1139 return count
->vararg
? 2 : 1;
1141 * XXX - need saner handling of that
1142 * (>= 1024 instances of argument)
1144 token_type(token
) = TOKEN_ERROR
;
1151 static struct token
*handle_hash(struct token
**p
, struct token
*arglist
)
1153 struct token
*token
= *p
;
1155 struct token
*next
= token
->next
;
1156 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1158 next
->pos
.whitespace
= token
->pos
.whitespace
;
1159 __free_token(token
);
1162 token
->pos
.noexpand
= 1;
1167 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1171 /* token->next is ## */
1172 static struct token
*handle_hashhash(struct token
*token
, struct token
*arglist
)
1174 struct token
*last
= token
;
1175 struct token
*concat
;
1176 int state
= match_op(token
, ',');
1178 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1184 /* eat duplicate ## */
1185 concat
= token
->next
;
1186 while (match_op(t
= concat
->next
, SPECIAL_HASHHASH
)) {
1188 __free_token(concat
);
1191 token_type(concat
) = TOKEN_CONCAT
;
1196 if (match_op(t
, '#')) {
1197 t
= handle_hash(&concat
->next
, arglist
);
1202 is_arg
= try_arg(t
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1204 if (state
== 1 && is_arg
) {
1208 state
= match_op(t
, ',');
1212 if (!match_op(token
->next
, SPECIAL_HASHHASH
))
1215 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1217 token_type(last
) = TOKEN_GNU_KLUDGE
;
1221 sparse_error(concat
->pos
, "'##' cannot appear at the ends of macro expansion");
1225 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1227 struct token
*token
= expansion
;
1230 if (match_op(token
, SPECIAL_HASHHASH
))
1233 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1234 if (match_op(token
, '#')) {
1235 token
= handle_hash(p
, arglist
);
1239 if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1240 token
= handle_hashhash(token
, arglist
);
1244 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1246 if (token_type(token
) == TOKEN_ERROR
)
1249 token
= alloc_token(&expansion
->pos
);
1250 token_type(token
) = TOKEN_UNTAINT
;
1251 token
->ident
= name
;
1257 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1260 sparse_error(token
->pos
, "too many instances of argument in body");
1264 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1266 struct token
*arglist
, *expansion
;
1267 struct token
*left
= token
->next
;
1272 if (token_type(left
) != TOKEN_IDENT
) {
1273 sparse_error(token
->pos
, "expected identifier to 'define'");
1280 expansion
= left
->next
;
1281 if (!expansion
->pos
.whitespace
) {
1282 if (match_op(expansion
, '(')) {
1283 arglist
= expansion
;
1284 expansion
= parse_arguments(expansion
);
1287 } else if (!eof_token(expansion
)) {
1288 warning(expansion
->pos
,
1289 "no whitespace before object-like macro body");
1293 expansion
= parse_expansion(expansion
, arglist
, name
);
1298 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1302 if (attr
< sym
->attr
)
1305 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1307 if (token_list_different(sym
->expansion
, expansion
) ||
1308 token_list_different(sym
->arglist
, arglist
)) {
1310 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1311 || sym
->used_in
== file_scope
) {
1312 warning(left
->pos
, "preprocessor token %.*s redefined",
1313 name
->len
, name
->name
);
1314 info(sym
->pos
, "this was the original definition");
1320 if (!sym
|| sym
->scope
!= file_scope
) {
1321 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1322 bind_symbol(sym
, name
, NS_MACRO
);
1327 sym
->expansion
= expansion
;
1328 sym
->arglist
= arglist
;
1329 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1332 sym
->namespace = NS_MACRO
;
1333 sym
->used_in
= NULL
;
1339 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1341 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1344 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1346 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1349 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1351 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1354 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1356 struct token
*left
= token
->next
;
1359 if (token_type(left
) != TOKEN_IDENT
) {
1360 sparse_error(token
->pos
, "expected identifier to 'undef'");
1364 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1366 if (attr
< sym
->attr
)
1368 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1370 } else if (attr
<= SYM_ATTR_NORMAL
)
1373 if (!sym
|| sym
->scope
!= file_scope
) {
1374 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1375 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1378 sym
->namespace = NS_UNDEF
;
1379 sym
->used_in
= NULL
;
1385 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1387 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1390 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1392 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1395 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1397 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1398 free_preprocessor_line(token
->next
);
1399 token
->next
= stream
->top_if
;
1400 stream
->top_if
= token
;
1401 if (false_nesting
|| true != 1)
1406 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1408 struct token
*next
= token
->next
;
1410 if (token_type(next
) == TOKEN_IDENT
) {
1411 arg
= token_defined(next
);
1413 dirty_stream(stream
);
1415 sparse_error(token
->pos
, "expected preprocessor identifier");
1418 return preprocessor_if(stream
, token
, arg
);
1421 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1423 struct token
*next
= token
->next
;
1425 if (token_type(next
) == TOKEN_IDENT
) {
1426 if (!stream
->dirty
&& !stream
->ifndef
) {
1427 if (!stream
->protect
) {
1428 stream
->ifndef
= token
;
1429 stream
->protect
= next
->ident
;
1430 } else if (stream
->protect
== next
->ident
) {
1431 stream
->ifndef
= token
;
1435 arg
= !token_defined(next
);
1437 dirty_stream(stream
);
1439 sparse_error(token
->pos
, "expected preprocessor identifier");
1443 return preprocessor_if(stream
, token
, arg
);
1446 static const char *show_token_sequence(struct token
*token
, int quote
);
1449 * Expression handling for #if and #elif; it differs from normal expansion
1450 * due to special treatment of "defined".
1452 static int expression_value(struct token
**where
)
1454 struct expression
*expr
;
1456 struct token
**list
= where
, **beginning
= NULL
;
1460 while (!eof_token(p
= scan_next(list
))) {
1463 if (token_type(p
) != TOKEN_IDENT
)
1465 if (p
->ident
== &defined_ident
) {
1470 if (!expand_one_symbol(list
))
1472 if (token_type(p
) != TOKEN_IDENT
)
1474 token_type(p
) = TOKEN_ZERO_IDENT
;
1477 if (match_op(p
, '(')) {
1481 replace_with_defined(p
);
1486 if (token_type(p
) == TOKEN_IDENT
)
1490 replace_with_defined(p
);
1495 if (!match_op(p
, ')'))
1496 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1503 p
= constant_expression(*where
, &expr
);
1505 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
, 0));
1506 value
= get_expression_value(expr
);
1510 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1514 value
= expression_value(&token
->next
);
1516 dirty_stream(stream
);
1517 return preprocessor_if(stream
, token
, value
);
1520 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1522 struct token
*top_if
= stream
->top_if
;
1526 nesting_error(stream
);
1527 sparse_error(token
->pos
, "unmatched #elif within stream");
1531 if (token_type(top_if
) == TOKEN_ELSE
) {
1532 nesting_error(stream
);
1533 sparse_error(token
->pos
, "#elif after #else");
1539 dirty_stream(stream
);
1540 if (token_type(top_if
) != TOKEN_IF
)
1542 if (false_nesting
) {
1544 if (!expression_value(&token
->next
))
1548 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1553 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1555 struct token
*top_if
= stream
->top_if
;
1559 nesting_error(stream
);
1560 sparse_error(token
->pos
, "unmatched #else within stream");
1564 if (token_type(top_if
) == TOKEN_ELSE
) {
1565 nesting_error(stream
);
1566 sparse_error(token
->pos
, "#else after #else");
1568 if (false_nesting
) {
1569 if (token_type(top_if
) == TOKEN_IF
)
1574 token_type(top_if
) = TOKEN_ELSE
;
1578 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1580 struct token
*top_if
= stream
->top_if
;
1583 nesting_error(stream
);
1584 sparse_error(token
->pos
, "unmatched #endif in stream");
1589 stream
->top_if
= top_if
->next
;
1590 __free_token(top_if
);
1594 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1596 warning(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1600 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1602 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1606 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1609 * Do we have any non-system includes?
1610 * Clear them out if so..
1612 *sys_includepath
= NULL
;
1616 static inline void update_inc_ptrs(const char ***where
)
1619 if (*where
<= dirafter_includepath
) {
1620 dirafter_includepath
++;
1621 /* If this was the entry that we prepend, don't
1622 * rise the lower entries, even if they are at
1623 * the same level. */
1624 if (where
== &dirafter_includepath
)
1627 if (*where
<= sys_includepath
) {
1629 if (where
== &sys_includepath
)
1632 if (*where
<= isys_includepath
) {
1634 if (where
== &isys_includepath
)
1638 /* angle_includepath is actually never updated, since we
1639 * don't suppport -iquote rught now. May change some day. */
1640 if (*where
<= angle_includepath
) {
1641 angle_includepath
++;
1642 if (where
== &angle_includepath
)
1647 /* Add a path before 'where' and update the pointers associated with the
1648 * includepath array */
1649 static void add_path_entry(struct token
*token
, const char *path
,
1650 const char ***where
)
1655 /* Need one free entry.. */
1656 if (includepath
[INCLUDEPATHS
-2])
1657 error_die(token
->pos
, "too many include path entries");
1659 /* check that this is not a duplicate */
1662 if (strcmp(*dst
, path
) == 0)
1669 update_inc_ptrs(where
);
1672 * Move them all up starting at dst,
1673 * insert the new entry..
1676 const char *tmp
= *dst
;
1683 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1686 token
= token
->next
;
1687 if (eof_token(token
))
1689 if (token_type(token
) != TOKEN_STRING
) {
1690 warning(token
->pos
, "expected path string");
1693 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1697 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1700 token
= token
->next
;
1701 if (eof_token(token
))
1703 if (token_type(token
) != TOKEN_STRING
) {
1704 sparse_error(token
->pos
, "expected path string");
1707 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1711 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
1714 token
= token
->next
;
1715 if (eof_token(token
))
1717 if (token_type(token
) != TOKEN_STRING
) {
1718 sparse_error(token
->pos
, "expected path string");
1721 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1725 /* Add to end on includepath list - no pointer updates */
1726 static void add_dirafter_entry(struct token
*token
, const char *path
)
1728 const char **dst
= includepath
;
1730 /* Need one free entry.. */
1731 if (includepath
[INCLUDEPATHS
-2])
1732 error_die(token
->pos
, "too many include path entries");
1734 /* Add to the end */
1742 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1745 token
= token
->next
;
1746 if (eof_token(token
))
1748 if (token_type(token
) != TOKEN_STRING
) {
1749 sparse_error(token
->pos
, "expected path string");
1752 add_dirafter_entry(token
, token
->string
->data
);
1756 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1761 * Split the include path. Any directories specified with `-I'
1762 * options before `-I-' are searched only for headers requested with
1763 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1764 * If additional directories are specified with `-I' options after
1765 * the `-I-', those directories are searched for all `#include'
1767 * In addition, `-I-' inhibits the use of the directory of the current
1768 * file directory as the first search directory for `#include "FILE"'.
1770 quote_includepath
= includepath
+1;
1771 angle_includepath
= sys_includepath
;
1776 * We replace "#pragma xxx" with "__pragma__" in the token
1777 * stream. Just as an example.
1779 * We'll just #define that away for now, but the theory here
1780 * is that we can use this to insert arbitrary token sequences
1781 * to turn the pragmas into internal front-end sequences for
1782 * when we actually start caring about them.
1784 * So eventually this will turn into some kind of extended
1785 * __attribute__() like thing, except called __pragma__(xxx).
1787 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1789 struct token
*next
= *line
;
1791 if (match_ident(token
->next
, &once_ident
) && eof_token(token
->next
->next
)) {
1795 token
->ident
= &pragma_ident
;
1796 token
->pos
.newline
= 1;
1797 token
->pos
.whitespace
= 1;
1805 * We ignore #line for now.
1807 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1812 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1814 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
, 0));
1819 static void init_preprocessor(void)
1822 int stream
= init_stream("preprocessor", -1, includepath
);
1825 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1827 { "define", handle_define
},
1828 { "weak_define", handle_weak_define
},
1829 { "strong_define", handle_strong_define
},
1830 { "undef", handle_undef
},
1831 { "strong_undef", handle_strong_undef
},
1832 { "warning", handle_warning
},
1833 { "error", handle_error
},
1834 { "include", handle_include
},
1835 { "include_next", handle_include_next
},
1836 { "pragma", handle_pragma
},
1837 { "line", handle_line
},
1839 // our internal preprocessor tokens
1840 { "nostdinc", handle_nostdinc
},
1841 { "add_include", handle_add_include
},
1842 { "add_isystem", handle_add_isystem
},
1843 { "add_system", handle_add_system
},
1844 { "add_dirafter", handle_add_dirafter
},
1845 { "split_include", handle_split_include
},
1846 { "argv_include", handle_argv_include
},
1848 { "ifdef", handle_ifdef
},
1849 { "ifndef", handle_ifndef
},
1850 { "else", handle_else
},
1851 { "endif", handle_endif
},
1852 { "if", handle_if
},
1853 { "elif", handle_elif
},
1856 for (i
= 0; i
< ARRAY_SIZE(normal
); i
++) {
1858 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1859 sym
->handler
= normal
[i
].handler
;
1862 for (i
= 0; i
< ARRAY_SIZE(special
); i
++) {
1864 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1865 sym
->handler
= special
[i
].handler
;
1871 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1873 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1874 struct token
*token
= start
->next
;
1877 if (eof_token(token
))
1880 if (token_type(token
) == TOKEN_IDENT
) {
1881 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1883 handler
= sym
->handler
;
1884 is_normal
= sym
->normal
;
1886 handler
= handle_nondirective
;
1888 } else if (token_type(token
) == TOKEN_NUMBER
) {
1889 handler
= handle_line
;
1891 handler
= handle_nondirective
;
1895 dirty_stream(stream
);
1899 if (!handler(stream
, line
, token
)) /* all set */
1903 free_preprocessor_line(token
);
1906 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1908 struct token
*start
= *line
, *next
;
1909 struct token
**tp
= &start
->next
;
1913 if (next
->pos
.newline
)
1918 *tp
= &eof_token_entry
;
1919 handle_preprocessor_line(stream
, line
, start
);
1922 static void do_preprocess(struct token
**list
)
1926 while (!eof_token(next
= scan_next(list
))) {
1927 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1929 if (next
->pos
.newline
&& match_op(next
, '#')) {
1930 if (!next
->pos
.noexpand
) {
1931 preprocessor_line(stream
, list
);
1932 __free_token(next
); /* Free the '#' token */
1937 switch (token_type(next
)) {
1938 case TOKEN_STREAMEND
:
1939 if (stream
->top_if
) {
1940 nesting_error(stream
);
1941 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1942 stream
->top_if
= NULL
;
1946 stream
->constant
= CONSTANT_FILE_YES
;
1949 case TOKEN_STREAMBEGIN
:
1954 dirty_stream(stream
);
1955 if (false_nesting
) {
1961 if (token_type(next
) != TOKEN_IDENT
||
1962 expand_one_symbol(list
))
1968 struct token
* preprocess(struct token
*token
)
1971 init_preprocessor();
1972 do_preprocess(&token
);
1974 // Drop all expressions from preprocessing, they're not used any more.
1975 // This is not true when we have multiple files, though ;/
1976 // clear_expression_alloc();