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 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
44 #include "expression.h"
47 static int false_nesting
= 0;
48 static int counter_macro
= 0; // __COUNTER__ expansion
50 #define INCLUDEPATHS 300
51 const char *includepath
[INCLUDEPATHS
+1] = {
58 static const char **quote_includepath
= includepath
;
59 static const char **angle_includepath
= includepath
+ 1;
60 static const char **isys_includepath
= includepath
+ 1;
61 static const char **sys_includepath
= includepath
+ 1;
62 static const char **dirafter_includepath
= includepath
+ 3;
64 #define dirty_stream(stream) \
66 if (!stream->dirty) { \
68 if (!stream->ifndef) \
69 stream->protect = NULL; \
73 #define end_group(stream) \
75 if (stream->ifndef == stream->top_if) { \
76 stream->ifndef = NULL; \
78 stream->protect = NULL; \
79 else if (stream->protect) \
84 #define nesting_error(stream) \
87 stream->ifndef = NULL; \
88 stream->protect = NULL; \
91 static struct token
*alloc_token(struct position
*pos
)
93 struct token
*token
= __alloc_token(0);
95 token
->pos
.stream
= pos
->stream
;
96 token
->pos
.line
= pos
->line
;
97 token
->pos
.pos
= pos
->pos
;
98 token
->pos
.whitespace
= 1;
102 /* Expand symbol 'sym' at '*list' */
103 static int expand(struct token
**, struct symbol
*);
105 static void replace_with_string(struct token
*token
, const char *str
)
107 int size
= strlen(str
) + 1;
108 struct string
*s
= __alloc_string(size
);
111 memcpy(s
->data
, str
, size
);
112 token_type(token
) = TOKEN_STRING
;
116 static void replace_with_integer(struct token
*token
, unsigned int val
)
118 char *buf
= __alloc_bytes(11);
119 sprintf(buf
, "%u", val
);
120 token_type(token
) = TOKEN_NUMBER
;
124 static struct symbol
*lookup_macro(struct ident
*ident
)
126 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_UNDEF
);
127 if (sym
&& sym
->namespace != NS_MACRO
)
132 static int token_defined(struct token
*token
)
134 if (token_type(token
) == TOKEN_IDENT
) {
135 struct symbol
*sym
= lookup_macro(token
->ident
);
137 sym
->used_in
= file_scope
;
143 sparse_error(token
->pos
, "expected preprocessor identifier");
147 static void replace_with_defined(struct token
*token
)
149 static const char *string
[] = { "0", "1" };
150 int defined
= token_defined(token
);
152 token_type(token
) = TOKEN_NUMBER
;
153 token
->number
= string
[defined
];
156 static int expand_one_symbol(struct token
**list
)
158 struct token
*token
= *list
;
160 static char buffer
[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
163 if (token
->pos
.noexpand
)
166 sym
= lookup_macro(token
->ident
);
168 sym
->used_in
= file_scope
;
169 return expand(list
, sym
);
171 if (token
->ident
== &__LINE___ident
) {
172 replace_with_integer(token
, token
->pos
.line
);
173 } else if (token
->ident
== &__FILE___ident
) {
174 replace_with_string(token
, stream_name(token
->pos
.stream
));
175 } else if (token
->ident
== &__DATE___ident
) {
178 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
179 replace_with_string(token
, buffer
);
180 } else if (token
->ident
== &__TIME___ident
) {
183 strftime(buffer
, 9, "%T", localtime(&t
));
184 replace_with_string(token
, buffer
);
185 } else if (token
->ident
== &__COUNTER___ident
) {
186 replace_with_integer(token
, counter_macro
++);
191 static inline struct token
*scan_next(struct token
**where
)
193 struct token
*token
= *where
;
194 if (token_type(token
) != TOKEN_UNTAINT
)
197 token
->ident
->tainted
= 0;
199 } while (token_type(token
) == TOKEN_UNTAINT
);
204 static void expand_list(struct token
**list
)
207 while (!eof_token(next
= scan_next(list
))) {
208 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
213 static void preprocessor_line(struct stream
*stream
, struct token
**line
);
215 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
, int count
)
217 struct stream
*stream
= input_streams
+ prev
->pos
.stream
;
218 struct token
**p
= &prev
->next
;
222 while (!eof_token(next
= scan_next(p
))) {
223 if (next
->pos
.newline
&& match_op(next
, '#')) {
224 if (!next
->pos
.noexpand
) {
225 sparse_error(next
->pos
,
226 "directive in argument list");
227 preprocessor_line(stream
, p
);
228 __free_token(next
); /* Free the '#' token */
232 switch (token_type(next
)) {
233 case TOKEN_STREAMEND
:
234 case TOKEN_STREAMBEGIN
:
235 *p
= &eof_token_entry
;
238 case TOKEN_WIDE_STRING
:
240 next
->string
->immutable
= 1;
248 if (match_op(next
, '(')) {
250 } else if (match_op(next
, ')')) {
253 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
256 next
->pos
.stream
= pos
->stream
;
257 next
->pos
.line
= pos
->line
;
258 next
->pos
.pos
= pos
->pos
;
261 *p
= &eof_token_entry
;
266 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
271 struct token
*expanded
;
278 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
280 int wanted
= arglist
->count
.normal
;
281 struct token
*next
= NULL
;
284 arglist
= arglist
->next
; /* skip counter */
287 next
= collect_arg(start
, 0, &what
->pos
, 0);
290 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
295 for (count
= 0; count
< wanted
; count
++) {
296 struct argcount
*p
= &arglist
->next
->count
;
297 next
= collect_arg(start
, p
->vararg
, &what
->pos
, p
->normal
);
298 arglist
= arglist
->next
->next
;
301 args
[count
].arg
= start
->next
;
302 args
[count
].n_normal
= p
->normal
;
303 args
[count
].n_quoted
= p
->quoted
;
304 args
[count
].n_str
= p
->str
;
305 if (match_op(next
, ')')) {
311 if (count
== wanted
&& !match_op(next
, ')'))
313 if (count
== wanted
- 1) {
314 struct argcount
*p
= &arglist
->next
->count
;
317 args
[count
].arg
= NULL
;
318 args
[count
].n_normal
= p
->normal
;
319 args
[count
].n_quoted
= p
->quoted
;
320 args
[count
].n_str
= p
->str
;
322 if (count
< wanted
- 1)
325 what
->next
= next
->next
;
329 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
330 show_token(what
), wanted
, count
);
333 while (match_op(next
, ',')) {
334 next
= collect_arg(next
, 0, &what
->pos
, 0);
339 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
340 show_token(what
), count
, wanted
);
343 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
346 what
->next
= next
->next
;
350 static struct token
*dup_list(struct token
*list
)
352 struct token
*res
= NULL
;
353 struct token
**p
= &res
;
355 while (!eof_token(list
)) {
356 struct token
*newtok
= __alloc_token(0);
365 static const char *show_token_sequence(struct token
*token
, int quote
)
367 static char buffer
[MAX_STRING
];
371 if (!token
&& !quote
)
373 while (!eof_token(token
)) {
374 const char *val
= quote
? quote_token(token
) : show_token(token
);
375 int len
= strlen(val
);
377 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
378 sparse_error(token
->pos
, "too long token expansion");
384 memcpy(ptr
, val
, len
);
387 whitespace
= token
->pos
.whitespace
;
393 static struct token
*stringify(struct token
*arg
)
395 const char *s
= show_token_sequence(arg
, 1);
396 int size
= strlen(s
)+1;
397 struct token
*token
= __alloc_token(0);
398 struct string
*string
= __alloc_string(size
);
400 memcpy(string
->data
, s
, size
);
401 string
->length
= size
;
402 token
->pos
= arg
->pos
;
403 token_type(token
) = TOKEN_STRING
;
404 token
->string
= string
;
405 token
->next
= &eof_token_entry
;
409 static void expand_arguments(int count
, struct arg
*args
)
412 for (i
= 0; i
< count
; i
++) {
413 struct token
*arg
= args
[i
].arg
;
415 arg
= &eof_token_entry
;
417 args
[i
].str
= stringify(arg
);
418 if (args
[i
].n_normal
) {
419 if (!args
[i
].n_quoted
) {
420 args
[i
].expanded
= arg
;
422 } else if (eof_token(arg
)) {
423 args
[i
].expanded
= arg
;
425 args
[i
].expanded
= dup_list(arg
);
427 expand_list(&args
[i
].expanded
);
433 * Possibly valid combinations:
434 * - ident + ident -> ident
435 * - ident + number -> ident unless number contains '.', '+' or '-'.
436 * - 'L' + char constant -> wide char constant
437 * - 'L' + string literal -> wide string literal
438 * - number + number -> number
439 * - number + ident -> number
440 * - number + '.' -> number
441 * - number + '+' or '-' -> number, if number used to end on [eEpP].
442 * - '.' + number -> number, if number used to start with a digit.
443 * - special + special -> either special or an error.
445 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
448 enum token_type t1
= token_type(left
), t2
= token_type(right
);
450 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
453 if (t1
== TOKEN_IDENT
&& left
->ident
== &L_ident
) {
454 if (t2
>= TOKEN_CHAR
&& t2
< TOKEN_WIDE_CHAR
)
455 return t2
+ TOKEN_WIDE_CHAR
- TOKEN_CHAR
;
456 if (t2
== TOKEN_STRING
)
457 return TOKEN_WIDE_STRING
;
460 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
463 strcpy(p
, show_token(left
));
464 strcat(p
, show_token(right
));
470 if (t1
== TOKEN_IDENT
) {
471 if (t2
== TOKEN_SPECIAL
)
473 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
478 if (t1
== TOKEN_NUMBER
) {
479 if (t2
== TOKEN_SPECIAL
) {
480 switch (right
->special
) {
484 if (strchr("eEpP", p
[len
- 2]))
493 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
496 return TOKEN_SPECIAL
;
499 static int merge(struct token
*left
, struct token
*right
)
501 static char buffer
[512];
502 enum token_type res
= combine(left
, right
, buffer
);
507 left
->ident
= built_in_ident(buffer
);
508 left
->pos
.noexpand
= 0;
512 char *number
= __alloc_bytes(strlen(buffer
) + 1);
513 memcpy(number
, buffer
, strlen(buffer
) + 1);
514 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
515 left
->number
= number
;
520 if (buffer
[2] && buffer
[3])
522 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
523 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
530 case TOKEN_WIDE_CHAR
:
531 case TOKEN_WIDE_STRING
:
532 token_type(left
) = res
;
533 left
->pos
.noexpand
= 0;
534 left
->string
= right
->string
;
537 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
538 token_type(left
) = res
;
539 left
->pos
.noexpand
= 0;
540 memcpy(left
->embedded
, right
->embedded
, 4);
546 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
550 static struct token
*dup_token(struct token
*token
, struct position
*streampos
)
552 struct token
*alloc
= alloc_token(streampos
);
553 token_type(alloc
) = token_type(token
);
554 alloc
->pos
.newline
= token
->pos
.newline
;
555 alloc
->pos
.whitespace
= token
->pos
.whitespace
;
556 alloc
->number
= token
->number
;
557 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
561 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
563 int need_copy
= --*count
;
564 while (!eof_token(list
)) {
567 token
= dup_token(list
, &list
->pos
);
570 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
571 token
->pos
.noexpand
= 1;
573 where
= &token
->next
;
576 *where
= &eof_token_entry
;
580 static int handle_kludge(struct token
**p
, struct arg
*args
)
582 struct token
*t
= (*p
)->next
->next
;
584 struct arg
*v
= &args
[t
->argnum
];
585 if (token_type(t
->next
) != TOKEN_CONCAT
) {
587 /* ignore the first ## */
591 /* skip the entire thing */
595 if (v
->arg
&& !eof_token(v
->arg
))
596 return 0; /* no magic */
601 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
603 struct position
*base_pos
= &(*list
)->pos
;
605 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
607 for (; !eof_token(body
); body
= body
->next
) {
608 struct token
*added
, *arg
;
612 switch (token_type(body
)) {
613 case TOKEN_GNU_KLUDGE
:
615 * GNU kludge: if we had <comma>##<vararg>, behaviour
616 * depends on whether we had enough arguments to have
617 * a vararg. If we did, ## is just ignored. Otherwise
618 * both , and ## are ignored. Worse, there can be
619 * an arbitrary number of ##<arg> in between; if all of
620 * those are empty, we act as if they hadn't been there,
621 * otherwise we act as if the kludge didn't exist.
624 if (handle_kludge(&body
, args
)) {
631 added
= dup_token(t
, base_pos
);
632 token_type(added
) = TOKEN_SPECIAL
;
636 case TOKEN_STR_ARGUMENT
:
637 arg
= args
[body
->argnum
].str
;
638 count
= &args
[body
->argnum
].n_str
;
641 case TOKEN_QUOTED_ARGUMENT
:
642 arg
= args
[body
->argnum
].arg
;
643 count
= &args
[body
->argnum
].n_quoted
;
644 if (!arg
|| eof_token(arg
)) {
653 case TOKEN_MACRO_ARGUMENT
:
654 arg
= args
[body
->argnum
].expanded
;
655 count
= &args
[body
->argnum
].n_normal
;
656 if (eof_token(arg
)) {
661 tail
= copy(&added
, arg
, count
);
662 added
->pos
.newline
= body
->pos
.newline
;
663 added
->pos
.whitespace
= body
->pos
.whitespace
;
667 if (state
== Placeholder
)
674 added
= dup_token(body
, base_pos
);
675 if (added
->ident
->tainted
)
676 added
->pos
.noexpand
= 1;
681 added
= dup_token(body
, base_pos
);
687 * if we got to doing real concatenation, we already have
688 * added something into the list, so containing_token() is OK.
690 if (state
== Concat
&& merge(containing_token(list
), added
)) {
692 if (tail
!= &added
->next
)
700 *list
= &eof_token_entry
;
704 static int expand(struct token
**list
, struct symbol
*sym
)
707 struct token
*token
= *list
;
708 struct ident
*expanding
= token
->ident
;
710 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
711 struct arg args
[nargs
];
713 if (expanding
->tainted
) {
714 token
->pos
.noexpand
= 1;
719 if (!match_op(scan_next(&token
->next
), '('))
721 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
723 expand_arguments(nargs
, args
);
726 expanding
->tainted
= 1;
729 tail
= substitute(list
, sym
->expansion
, args
);
731 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
732 * We still can lose the newline flag if the sucker expands to nothing,
733 * but the price of dealing with that is probably too high (we'd need
734 * to collect the flags during scan_next())
736 (*list
)->pos
.newline
= token
->pos
.newline
;
737 (*list
)->pos
.whitespace
= token
->pos
.whitespace
;
743 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
745 static char buffer
[256];
748 while (!eof_token(token
) && !match_op(token
, endop
)) {
750 const char *val
= token
->string
->data
;
751 if (token_type(token
) != TOKEN_STRING
)
752 val
= show_token(token
);
754 memcpy(ptr
, val
, len
);
759 if (endop
&& !match_op(token
, endop
))
760 sparse_error(start
->pos
, "expected '>' at end of filename");
764 static int already_tokenized(const char *path
)
768 for (stream
= *hash_stream(path
); stream
>= 0 ; stream
= next
) {
769 struct stream
*s
= input_streams
+ stream
;
771 next
= s
->next_stream
;
773 if (strcmp(path
, s
->name
))
777 if (s
->constant
!= CONSTANT_FILE_YES
)
779 if (strcmp(path
, s
->name
))
781 if (s
->protect
&& !lookup_macro(s
->protect
))
788 /* Handle include of header files.
789 * The relevant options are made compatible with gcc. The only options that
790 * are not supported is -withprefix and friends.
792 * Three set of include paths are known:
793 * quote_includepath: Path to search when using #include "file.h"
794 * angle_includepath: Paths to search when using #include <file.h>
795 * isys_includepath: Paths specified with -isystem, come before the
796 * built-in system include paths. Gcc would suppress
797 * warnings from system headers. Here we separate
798 * them from the angle_ ones to keep search ordering.
800 * sys_includepath: Built-in include paths.
801 * dirafter_includepath Paths added with -dirafter.
803 * The above is implemented as one array with pointers
805 * quote_includepath ---> | |
809 * angle_includepath ---> | |
811 * isys_includepath ---> | |
813 * sys_includepath ---> | |
815 * dirafter_includepath -> | |
818 * -I dir insert dir just before isys_includepath and move the rest
819 * -I- makes all dirs specified with -I before to quote dirs only and
820 * angle_includepath is set equal to isys_includepath.
821 * -nostdinc removes all sys dirs by storing NULL in entry pointed
822 * to by * sys_includepath. Note that this will reset all dirs built-in
823 * and added before -nostdinc by -isystem and -idirafter.
824 * -isystem dir adds dir where isys_includepath points adding this dir as
826 * -idirafter dir adds dir to the end of the list
829 static void set_stream_include_path(struct stream
*stream
)
831 const char *path
= stream
->path
;
833 const char *p
= strrchr(stream
->name
, '/');
836 int len
= p
- stream
->name
+ 1;
837 char *m
= malloc(len
+1);
838 /* This includes the final "/" */
839 memcpy(m
, stream
->name
, len
);
845 includepath
[0] = path
;
848 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
851 int plen
= strlen(path
);
852 static char fullname
[PATH_MAX
];
854 memcpy(fullname
, path
, plen
);
855 if (plen
&& path
[plen
-1] != '/') {
856 fullname
[plen
] = '/';
859 memcpy(fullname
+plen
, filename
, flen
);
860 if (already_tokenized(fullname
))
862 fd
= open(fullname
, O_RDONLY
);
864 char * streamname
= __alloc_bytes(plen
+ flen
);
865 memcpy(streamname
, fullname
, plen
+ flen
);
866 *where
= tokenize(streamname
, fd
, *where
, next_path
);
873 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
877 while ((path
= *pptr
++) != NULL
) {
878 if (!try_include(path
, filename
, flen
, list
, pptr
))
885 static int free_preprocessor_line(struct token
*token
)
887 while (token_type(token
) != TOKEN_EOF
) {
888 struct token
*free
= token
;
895 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, int how
)
897 const char *filename
;
905 if (!match_op(next
, '<')) {
906 expand_list(&token
->next
);
909 if (match_op(token
->next
, '<')) {
916 filename
= token_name_sequence(token
, expect
, token
);
917 flen
= strlen(filename
) + 1;
920 if (filename
[0] == '/') {
921 if (try_include("", filename
, flen
, list
, includepath
))
928 path
= stream
->next_path
;
935 /* Dir of input file is first dir to search for quoted includes */
936 set_stream_include_path(stream
);
937 path
= expect
? angle_includepath
: quote_includepath
;
940 /* Check the standard include paths.. */
941 if (do_include_path(path
, list
, token
, filename
, flen
))
944 error_die(token
->pos
, "unable to open '%s'", filename
);
947 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
949 return handle_include_path(stream
, list
, token
, 0);
952 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
954 return handle_include_path(stream
, list
, token
, 1);
957 static int handle_argv_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
959 return handle_include_path(stream
, list
, token
, 2);
962 static int token_different(struct token
*t1
, struct token
*t2
)
966 if (token_type(t1
) != token_type(t2
))
969 switch (token_type(t1
)) {
971 different
= t1
->ident
!= t2
->ident
;
973 case TOKEN_ARG_COUNT
:
976 case TOKEN_GNU_KLUDGE
:
980 different
= strcmp(t1
->number
, t2
->number
);
983 different
= t1
->special
!= t2
->special
;
985 case TOKEN_MACRO_ARGUMENT
:
986 case TOKEN_QUOTED_ARGUMENT
:
987 case TOKEN_STR_ARGUMENT
:
988 different
= t1
->argnum
!= t2
->argnum
;
990 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
991 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
992 different
= memcmp(t1
->embedded
, t2
->embedded
, 4);
995 case TOKEN_WIDE_CHAR
:
997 case TOKEN_WIDE_STRING
: {
998 struct string
*s1
, *s2
;
1003 if (s1
->length
!= s2
->length
)
1005 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
1015 static int token_list_different(struct token
*list1
, struct token
*list2
)
1020 if (!list1
|| !list2
)
1022 if (token_different(list1
, list2
))
1024 list1
= list1
->next
;
1025 list2
= list2
->next
;
1029 static inline void set_arg_count(struct token
*token
)
1031 token_type(token
) = TOKEN_ARG_COUNT
;
1032 token
->count
.normal
= token
->count
.quoted
=
1033 token
->count
.str
= token
->count
.vararg
= 0;
1036 static struct token
*parse_arguments(struct token
*list
)
1038 struct token
*arg
= list
->next
, *next
= list
;
1039 struct argcount
*count
= &list
->count
;
1041 set_arg_count(list
);
1043 if (match_op(arg
, ')')) {
1045 list
->next
= &eof_token_entry
;
1049 while (token_type(arg
) == TOKEN_IDENT
) {
1050 if (arg
->ident
== &__VA_ARGS___ident
)
1052 if (!++count
->normal
)
1056 if (match_op(next
, ',')) {
1057 set_arg_count(next
);
1062 if (match_op(next
, ')')) {
1063 set_arg_count(next
);
1065 arg
->next
->next
= &eof_token_entry
;
1069 /* normal cases are finished here */
1071 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
1072 if (match_op(next
->next
, ')')) {
1073 set_arg_count(next
);
1074 next
->count
.vararg
= 1;
1076 arg
->next
->next
= &eof_token_entry
;
1084 if (eof_token(next
)) {
1092 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
1094 token_type(arg
) = TOKEN_IDENT
;
1095 arg
->ident
= &__VA_ARGS___ident
;
1096 if (!match_op(next
, ')'))
1098 if (!++count
->normal
)
1100 set_arg_count(next
);
1101 next
->count
.vararg
= 1;
1103 arg
->next
->next
= &eof_token_entry
;
1107 if (eof_token(arg
)) {
1111 if (match_op(arg
, ','))
1118 sparse_error(arg
->pos
, "parameter name missing");
1121 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
1125 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1128 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1131 sparse_error(arg
->pos
, "too many arguments in macro definition");
1135 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1137 struct ident
*ident
= token
->ident
;
1140 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1143 arglist
= arglist
->next
;
1145 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1146 if (arglist
->ident
== ident
) {
1147 struct argcount
*count
= &arglist
->next
->count
;
1151 token_type(token
) = type
;
1153 case TOKEN_MACRO_ARGUMENT
:
1154 n
= ++count
->normal
;
1156 case TOKEN_QUOTED_ARGUMENT
:
1157 n
= ++count
->quoted
;
1163 return count
->vararg
? 2 : 1;
1165 * XXX - need saner handling of that
1166 * (>= 1024 instances of argument)
1168 token_type(token
) = TOKEN_ERROR
;
1175 static struct token
*handle_hash(struct token
**p
, struct token
*arglist
)
1177 struct token
*token
= *p
;
1179 struct token
*next
= token
->next
;
1180 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1182 next
->pos
.whitespace
= token
->pos
.whitespace
;
1183 __free_token(token
);
1186 token
->pos
.noexpand
= 1;
1191 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1195 /* token->next is ## */
1196 static struct token
*handle_hashhash(struct token
*token
, struct token
*arglist
)
1198 struct token
*last
= token
;
1199 struct token
*concat
;
1200 int state
= match_op(token
, ',');
1202 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1208 /* eat duplicate ## */
1209 concat
= token
->next
;
1210 while (match_op(t
= concat
->next
, SPECIAL_HASHHASH
)) {
1212 __free_token(concat
);
1215 token_type(concat
) = TOKEN_CONCAT
;
1220 if (match_op(t
, '#')) {
1221 t
= handle_hash(&concat
->next
, arglist
);
1226 is_arg
= try_arg(t
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1228 if (state
== 1 && is_arg
) {
1232 state
= match_op(t
, ',');
1236 if (!match_op(token
->next
, SPECIAL_HASHHASH
))
1239 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1241 token_type(last
) = TOKEN_GNU_KLUDGE
;
1245 sparse_error(concat
->pos
, "'##' cannot appear at the ends of macro expansion");
1249 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1251 struct token
*token
= expansion
;
1254 if (match_op(token
, SPECIAL_HASHHASH
))
1257 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1258 if (match_op(token
, '#')) {
1259 token
= handle_hash(p
, arglist
);
1263 if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1264 token
= handle_hashhash(token
, arglist
);
1268 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1270 switch (token_type(token
)) {
1275 case TOKEN_WIDE_STRING
:
1276 token
->string
->immutable
= 1;
1280 token
= alloc_token(&expansion
->pos
);
1281 token_type(token
) = TOKEN_UNTAINT
;
1282 token
->ident
= name
;
1288 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1291 sparse_error(token
->pos
, "too many instances of argument in body");
1295 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1297 struct token
*arglist
, *expansion
;
1298 struct token
*left
= token
->next
;
1303 if (token_type(left
) != TOKEN_IDENT
) {
1304 sparse_error(token
->pos
, "expected identifier to 'define'");
1311 expansion
= left
->next
;
1312 if (!expansion
->pos
.whitespace
) {
1313 if (match_op(expansion
, '(')) {
1314 arglist
= expansion
;
1315 expansion
= parse_arguments(expansion
);
1318 } else if (!eof_token(expansion
)) {
1319 warning(expansion
->pos
,
1320 "no whitespace before object-like macro body");
1324 expansion
= parse_expansion(expansion
, arglist
, name
);
1329 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1333 if (attr
< sym
->attr
)
1336 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1338 if (token_list_different(sym
->expansion
, expansion
) ||
1339 token_list_different(sym
->arglist
, arglist
)) {
1341 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1342 || sym
->used_in
== file_scope
) {
1343 warning(left
->pos
, "preprocessor token %.*s redefined",
1344 name
->len
, name
->name
);
1345 info(sym
->pos
, "this was the original definition");
1351 if (!sym
|| sym
->scope
!= file_scope
) {
1352 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1353 bind_symbol(sym
, name
, NS_MACRO
);
1358 sym
->expansion
= expansion
;
1359 sym
->arglist
= arglist
;
1360 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1363 sym
->namespace = NS_MACRO
;
1364 sym
->used_in
= NULL
;
1370 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1372 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1375 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1377 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1380 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1382 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1385 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1387 struct token
*left
= token
->next
;
1390 if (token_type(left
) != TOKEN_IDENT
) {
1391 sparse_error(token
->pos
, "expected identifier to 'undef'");
1395 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1397 if (attr
< sym
->attr
)
1399 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1401 } else if (attr
<= SYM_ATTR_NORMAL
)
1404 if (!sym
|| sym
->scope
!= file_scope
) {
1405 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1406 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1409 sym
->namespace = NS_UNDEF
;
1410 sym
->used_in
= NULL
;
1416 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1418 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1421 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1423 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1426 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1428 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1429 free_preprocessor_line(token
->next
);
1430 token
->next
= stream
->top_if
;
1431 stream
->top_if
= token
;
1432 if (false_nesting
|| true != 1)
1437 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1439 struct token
*next
= token
->next
;
1441 if (token_type(next
) == TOKEN_IDENT
) {
1442 arg
= token_defined(next
);
1444 dirty_stream(stream
);
1446 sparse_error(token
->pos
, "expected preprocessor identifier");
1449 return preprocessor_if(stream
, token
, arg
);
1452 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1454 struct token
*next
= token
->next
;
1456 if (token_type(next
) == TOKEN_IDENT
) {
1457 if (!stream
->dirty
&& !stream
->ifndef
) {
1458 if (!stream
->protect
) {
1459 stream
->ifndef
= token
;
1460 stream
->protect
= next
->ident
;
1461 } else if (stream
->protect
== next
->ident
) {
1462 stream
->ifndef
= token
;
1466 arg
= !token_defined(next
);
1468 dirty_stream(stream
);
1470 sparse_error(token
->pos
, "expected preprocessor identifier");
1474 return preprocessor_if(stream
, token
, arg
);
1477 static const char *show_token_sequence(struct token
*token
, int quote
);
1480 * Expression handling for #if and #elif; it differs from normal expansion
1481 * due to special treatment of "defined".
1483 static int expression_value(struct token
**where
)
1485 struct expression
*expr
;
1487 struct token
**list
= where
, **beginning
= NULL
;
1491 while (!eof_token(p
= scan_next(list
))) {
1494 if (token_type(p
) != TOKEN_IDENT
)
1496 if (p
->ident
== &defined_ident
) {
1501 if (!expand_one_symbol(list
))
1503 if (token_type(p
) != TOKEN_IDENT
)
1505 token_type(p
) = TOKEN_ZERO_IDENT
;
1508 if (match_op(p
, '(')) {
1512 replace_with_defined(p
);
1517 if (token_type(p
) == TOKEN_IDENT
)
1521 replace_with_defined(p
);
1526 if (!match_op(p
, ')'))
1527 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1534 p
= constant_expression(*where
, &expr
);
1536 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
, 0));
1537 value
= get_expression_value(expr
);
1541 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1545 value
= expression_value(&token
->next
);
1547 dirty_stream(stream
);
1548 return preprocessor_if(stream
, token
, value
);
1551 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1553 struct token
*top_if
= stream
->top_if
;
1557 nesting_error(stream
);
1558 sparse_error(token
->pos
, "unmatched #elif within stream");
1562 if (token_type(top_if
) == TOKEN_ELSE
) {
1563 nesting_error(stream
);
1564 sparse_error(token
->pos
, "#elif after #else");
1570 dirty_stream(stream
);
1571 if (token_type(top_if
) != TOKEN_IF
)
1573 if (false_nesting
) {
1575 if (!expression_value(&token
->next
))
1579 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1584 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1586 struct token
*top_if
= stream
->top_if
;
1590 nesting_error(stream
);
1591 sparse_error(token
->pos
, "unmatched #else within stream");
1595 if (token_type(top_if
) == TOKEN_ELSE
) {
1596 nesting_error(stream
);
1597 sparse_error(token
->pos
, "#else after #else");
1599 if (false_nesting
) {
1600 if (token_type(top_if
) == TOKEN_IF
)
1605 token_type(top_if
) = TOKEN_ELSE
;
1609 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1611 struct token
*top_if
= stream
->top_if
;
1614 nesting_error(stream
);
1615 sparse_error(token
->pos
, "unmatched #endif in stream");
1620 stream
->top_if
= top_if
->next
;
1621 __free_token(top_if
);
1625 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1627 warning(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1631 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1633 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1637 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1640 * Do we have any non-system includes?
1641 * Clear them out if so..
1643 *sys_includepath
= NULL
;
1647 static inline void update_inc_ptrs(const char ***where
)
1650 if (*where
<= dirafter_includepath
) {
1651 dirafter_includepath
++;
1652 /* If this was the entry that we prepend, don't
1653 * rise the lower entries, even if they are at
1654 * the same level. */
1655 if (where
== &dirafter_includepath
)
1658 if (*where
<= sys_includepath
) {
1660 if (where
== &sys_includepath
)
1663 if (*where
<= isys_includepath
) {
1665 if (where
== &isys_includepath
)
1669 /* angle_includepath is actually never updated, since we
1670 * don't suppport -iquote rught now. May change some day. */
1671 if (*where
<= angle_includepath
) {
1672 angle_includepath
++;
1673 if (where
== &angle_includepath
)
1678 /* Add a path before 'where' and update the pointers associated with the
1679 * includepath array */
1680 static void add_path_entry(struct token
*token
, const char *path
,
1681 const char ***where
)
1686 /* Need one free entry.. */
1687 if (includepath
[INCLUDEPATHS
-2])
1688 error_die(token
->pos
, "too many include path entries");
1690 /* check that this is not a duplicate */
1693 if (strcmp(*dst
, path
) == 0)
1700 update_inc_ptrs(where
);
1703 * Move them all up starting at dst,
1704 * insert the new entry..
1707 const char *tmp
= *dst
;
1714 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1717 token
= token
->next
;
1718 if (eof_token(token
))
1720 if (token_type(token
) != TOKEN_STRING
) {
1721 warning(token
->pos
, "expected path string");
1724 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1728 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1731 token
= token
->next
;
1732 if (eof_token(token
))
1734 if (token_type(token
) != TOKEN_STRING
) {
1735 sparse_error(token
->pos
, "expected path string");
1738 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1742 static int handle_add_system(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_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1756 /* Add to end on includepath list - no pointer updates */
1757 static void add_dirafter_entry(struct token
*token
, const char *path
)
1759 const char **dst
= includepath
;
1761 /* Need one free entry.. */
1762 if (includepath
[INCLUDEPATHS
-2])
1763 error_die(token
->pos
, "too many include path entries");
1765 /* Add to the end */
1773 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1776 token
= token
->next
;
1777 if (eof_token(token
))
1779 if (token_type(token
) != TOKEN_STRING
) {
1780 sparse_error(token
->pos
, "expected path string");
1783 add_dirafter_entry(token
, token
->string
->data
);
1787 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1792 * Split the include path. Any directories specified with `-I'
1793 * options before `-I-' are searched only for headers requested with
1794 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1795 * If additional directories are specified with `-I' options after
1796 * the `-I-', those directories are searched for all `#include'
1798 * In addition, `-I-' inhibits the use of the directory of the current
1799 * file directory as the first search directory for `#include "FILE"'.
1801 quote_includepath
= includepath
+1;
1802 angle_includepath
= sys_includepath
;
1807 * We replace "#pragma xxx" with "__pragma__" in the token
1808 * stream. Just as an example.
1810 * We'll just #define that away for now, but the theory here
1811 * is that we can use this to insert arbitrary token sequences
1812 * to turn the pragmas into internal front-end sequences for
1813 * when we actually start caring about them.
1815 * So eventually this will turn into some kind of extended
1816 * __attribute__() like thing, except called __pragma__(xxx).
1818 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1820 struct token
*next
= *line
;
1822 if (match_ident(token
->next
, &once_ident
) && eof_token(token
->next
->next
)) {
1826 token
->ident
= &pragma_ident
;
1827 token
->pos
.newline
= 1;
1828 token
->pos
.whitespace
= 1;
1836 * We ignore #line for now.
1838 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1843 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1845 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
, 0));
1850 static void init_preprocessor(void)
1853 int stream
= init_stream("preprocessor", -1, includepath
);
1856 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1858 { "define", handle_define
},
1859 { "weak_define", handle_weak_define
},
1860 { "strong_define", handle_strong_define
},
1861 { "undef", handle_undef
},
1862 { "strong_undef", handle_strong_undef
},
1863 { "warning", handle_warning
},
1864 { "error", handle_error
},
1865 { "include", handle_include
},
1866 { "include_next", handle_include_next
},
1867 { "pragma", handle_pragma
},
1868 { "line", handle_line
},
1870 // our internal preprocessor tokens
1871 { "nostdinc", handle_nostdinc
},
1872 { "add_include", handle_add_include
},
1873 { "add_isystem", handle_add_isystem
},
1874 { "add_system", handle_add_system
},
1875 { "add_dirafter", handle_add_dirafter
},
1876 { "split_include", handle_split_include
},
1877 { "argv_include", handle_argv_include
},
1879 { "ifdef", handle_ifdef
},
1880 { "ifndef", handle_ifndef
},
1881 { "else", handle_else
},
1882 { "endif", handle_endif
},
1883 { "if", handle_if
},
1884 { "elif", handle_elif
},
1887 for (i
= 0; i
< ARRAY_SIZE(normal
); i
++) {
1889 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1890 sym
->handler
= normal
[i
].handler
;
1893 for (i
= 0; i
< ARRAY_SIZE(special
); i
++) {
1895 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1896 sym
->handler
= special
[i
].handler
;
1903 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1905 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1906 struct token
*token
= start
->next
;
1909 if (eof_token(token
))
1912 if (token_type(token
) == TOKEN_IDENT
) {
1913 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1915 handler
= sym
->handler
;
1916 is_normal
= sym
->normal
;
1918 handler
= handle_nondirective
;
1920 } else if (token_type(token
) == TOKEN_NUMBER
) {
1921 handler
= handle_line
;
1923 handler
= handle_nondirective
;
1927 dirty_stream(stream
);
1931 if (!handler(stream
, line
, token
)) /* all set */
1935 free_preprocessor_line(token
);
1938 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1940 struct token
*start
= *line
, *next
;
1941 struct token
**tp
= &start
->next
;
1945 if (next
->pos
.newline
)
1950 *tp
= &eof_token_entry
;
1951 handle_preprocessor_line(stream
, line
, start
);
1954 static void do_preprocess(struct token
**list
)
1958 while (!eof_token(next
= scan_next(list
))) {
1959 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1961 if (next
->pos
.newline
&& match_op(next
, '#')) {
1962 if (!next
->pos
.noexpand
) {
1963 preprocessor_line(stream
, list
);
1964 __free_token(next
); /* Free the '#' token */
1969 switch (token_type(next
)) {
1970 case TOKEN_STREAMEND
:
1971 if (stream
->top_if
) {
1972 nesting_error(stream
);
1973 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1974 stream
->top_if
= NULL
;
1978 stream
->constant
= CONSTANT_FILE_YES
;
1981 case TOKEN_STREAMBEGIN
:
1986 dirty_stream(stream
);
1987 if (false_nesting
) {
1993 if (token_type(next
) != TOKEN_IDENT
||
1994 expand_one_symbol(list
))
2000 struct token
* preprocess(struct token
*token
)
2003 init_preprocessor();
2004 do_preprocess(&token
);
2006 // Drop all expressions from preprocessing, they're not used any more.
2007 // This is not true when we have multiple files, though ;/
2008 // clear_expression_alloc();