2 * Do C preprocessing, based on a token list gathered by
5 * This may not be the smartest preprocessor on the planet.
7 * Copyright (C) 2003 Transmeta Corp.
8 * 2003-2004 Linus Torvalds
10 * Licensed under the Open Software License version 1.1
28 #include "expression.h"
31 static int false_nesting
= 0;
33 #define INCLUDEPATHS 300
34 const char *includepath
[INCLUDEPATHS
+1] = {
41 static const char **quote_includepath
= includepath
;
42 static const char **angle_includepath
= includepath
+ 1;
43 static const char **isys_includepath
= includepath
+ 1;
44 static const char **sys_includepath
= includepath
+ 1;
45 static const char **dirafter_includepath
= includepath
+ 3;
47 #define dirty_stream(stream) \
49 if (!stream->dirty) { \
51 if (!stream->ifndef) \
52 stream->protect = NULL; \
56 #define end_group(stream) \
58 if (stream->ifndef == stream->top_if) { \
59 stream->ifndef = NULL; \
61 stream->protect = NULL; \
62 else if (stream->protect) \
67 #define nesting_error(stream) \
70 stream->ifndef = NULL; \
71 stream->protect = NULL; \
74 static struct token
*alloc_token(struct position
*pos
)
76 struct token
*token
= __alloc_token(0);
78 token
->pos
.stream
= pos
->stream
;
79 token
->pos
.line
= pos
->line
;
80 token
->pos
.pos
= pos
->pos
;
81 token
->pos
.whitespace
= 1;
85 static const char *show_token_sequence(struct token
*token
);
87 /* Expand symbol 'sym' at '*list' */
88 static int expand(struct token
**, struct symbol
*);
90 static void replace_with_string(struct token
*token
, const char *str
)
92 int size
= strlen(str
) + 1;
93 struct string
*s
= __alloc_string(size
);
96 memcpy(s
->data
, str
, size
);
97 token_type(token
) = TOKEN_STRING
;
101 static void replace_with_integer(struct token
*token
, unsigned int val
)
103 char *buf
= __alloc_bytes(11);
104 sprintf(buf
, "%u", val
);
105 token_type(token
) = TOKEN_NUMBER
;
109 static struct symbol
*lookup_macro(struct ident
*ident
)
111 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_UNDEF
);
112 if (sym
&& sym
->namespace != NS_MACRO
)
117 static int token_defined(struct token
*token
)
119 if (token_type(token
) == TOKEN_IDENT
) {
120 struct symbol
*sym
= lookup_macro(token
->ident
);
122 sym
->used_in
= file_scope
;
128 sparse_error(token
->pos
, "expected preprocessor identifier");
132 static void replace_with_defined(struct token
*token
)
134 static const char *string
[] = { "0", "1" };
135 int defined
= token_defined(token
);
137 token_type(token
) = TOKEN_NUMBER
;
138 token
->number
= string
[defined
];
141 static int expand_one_symbol(struct token
**list
)
143 struct token
*token
= *list
;
145 static char buffer
[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
148 if (token
->pos
.noexpand
)
151 sym
= lookup_macro(token
->ident
);
153 sym
->used_in
= file_scope
;
154 return expand(list
, sym
);
156 if (token
->ident
== &__LINE___ident
) {
157 replace_with_integer(token
, token
->pos
.line
);
158 } else if (token
->ident
== &__FILE___ident
) {
159 replace_with_string(token
, stream_name(token
->pos
.stream
));
160 } else if (token
->ident
== &__DATE___ident
) {
163 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
164 replace_with_string(token
, buffer
);
165 } else if (token
->ident
== &__TIME___ident
) {
168 strftime(buffer
, 9, "%T", localtime(&t
));
169 replace_with_string(token
, buffer
);
174 static inline struct token
*scan_next(struct token
**where
)
176 struct token
*token
= *where
;
177 if (token_type(token
) != TOKEN_UNTAINT
)
180 token
->ident
->tainted
= 0;
182 } while (token_type(token
) == TOKEN_UNTAINT
);
187 static void expand_list(struct token
**list
)
190 while (!eof_token(next
= scan_next(list
))) {
191 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
196 static void preprocessor_line(struct stream
*stream
, struct token
**line
);
198 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
200 struct stream
*stream
= input_streams
+ prev
->pos
.stream
;
201 struct token
**p
= &prev
->next
;
205 while (!eof_token(next
= scan_next(p
))) {
206 if (next
->pos
.newline
&& match_op(next
, '#')) {
207 if (!next
->pos
.noexpand
) {
208 sparse_error(next
->pos
,
209 "directive in argument list");
210 preprocessor_line(stream
, p
);
211 __free_token(next
); /* Free the '#' token */
215 switch (token_type(next
)) {
216 case TOKEN_STREAMEND
:
217 case TOKEN_STREAMBEGIN
:
218 *p
= &eof_token_entry
;
226 if (match_op(next
, '(')) {
228 } else if (match_op(next
, ')')) {
231 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
234 next
->pos
.stream
= pos
->stream
;
235 next
->pos
.line
= pos
->line
;
236 next
->pos
.pos
= pos
->pos
;
239 *p
= &eof_token_entry
;
244 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
249 struct token
*expanded
;
256 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
258 int wanted
= arglist
->count
.normal
;
259 struct token
*next
= NULL
;
262 arglist
= arglist
->next
; /* skip counter */
265 next
= collect_arg(start
, 0, &what
->pos
);
268 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
273 for (count
= 0; count
< wanted
; count
++) {
274 struct argcount
*p
= &arglist
->next
->count
;
275 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
276 arglist
= arglist
->next
->next
;
279 args
[count
].arg
= start
->next
;
280 args
[count
].n_normal
= p
->normal
;
281 args
[count
].n_quoted
= p
->quoted
;
282 args
[count
].n_str
= p
->str
;
283 if (match_op(next
, ')')) {
289 if (count
== wanted
&& !match_op(next
, ')'))
291 if (count
== wanted
- 1) {
292 struct argcount
*p
= &arglist
->next
->count
;
295 args
[count
].arg
= NULL
;
296 args
[count
].n_normal
= p
->normal
;
297 args
[count
].n_quoted
= p
->quoted
;
298 args
[count
].n_str
= p
->str
;
300 if (count
< wanted
- 1)
303 what
->next
= next
->next
;
307 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
308 show_token(what
), wanted
, count
);
311 while (match_op(next
, ',')) {
312 next
= collect_arg(next
, 0, &what
->pos
);
317 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
318 show_token(what
), count
, wanted
);
321 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
324 what
->next
= next
->next
;
328 static struct token
*dup_list(struct token
*list
)
330 struct token
*res
= NULL
;
331 struct token
**p
= &res
;
333 while (!eof_token(list
)) {
334 struct token
*newtok
= __alloc_token(0);
343 static struct token
*stringify(struct token
*arg
)
345 const char *s
= show_token_sequence(arg
);
346 int size
= strlen(s
)+1;
347 struct token
*token
= __alloc_token(0);
348 struct string
*string
= __alloc_string(size
);
350 memcpy(string
->data
, s
, size
);
351 string
->length
= size
;
352 token
->pos
= arg
->pos
;
353 token_type(token
) = TOKEN_STRING
;
354 token
->string
= string
;
355 token
->next
= &eof_token_entry
;
359 static void expand_arguments(int count
, struct arg
*args
)
362 for (i
= 0; i
< count
; i
++) {
363 struct token
*arg
= args
[i
].arg
;
365 arg
= &eof_token_entry
;
367 args
[i
].str
= stringify(arg
);
368 if (args
[i
].n_normal
) {
369 if (!args
[i
].n_quoted
) {
370 args
[i
].expanded
= arg
;
372 } else if (eof_token(arg
)) {
373 args
[i
].expanded
= arg
;
375 args
[i
].expanded
= dup_list(arg
);
377 expand_list(&args
[i
].expanded
);
383 * Possibly valid combinations:
384 * - ident + ident -> ident
385 * - ident + number -> ident unless number contains '.', '+' or '-'.
386 * - number + number -> number
387 * - number + ident -> number
388 * - number + '.' -> number
389 * - number + '+' or '-' -> number, if number used to end on [eEpP].
390 * - '.' + number -> number, if number used to start with a digit.
391 * - special + special -> either special or an error.
393 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
396 enum token_type t1
= token_type(left
), t2
= token_type(right
);
398 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
401 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
404 strcpy(p
, show_token(left
));
405 strcat(p
, show_token(right
));
411 if (t1
== TOKEN_IDENT
) {
412 if (t2
== TOKEN_SPECIAL
)
414 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
419 if (t1
== TOKEN_NUMBER
) {
420 if (t2
== TOKEN_SPECIAL
) {
421 switch (right
->special
) {
425 if (strchr("eEpP", p
[len
- 2]))
434 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
437 return TOKEN_SPECIAL
;
440 static int merge(struct token
*left
, struct token
*right
)
442 static char buffer
[512];
445 switch (combine(left
, right
, buffer
)) {
447 left
->ident
= built_in_ident(buffer
);
448 left
->pos
.noexpand
= 0;
452 char *number
= __alloc_bytes(strlen(buffer
) + 1);
453 memcpy(number
, buffer
, strlen(buffer
) + 1);
454 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
455 left
->number
= number
;
460 if (buffer
[2] && buffer
[3])
462 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
463 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
471 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
475 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
477 struct token
*alloc
= alloc_token(streampos
);
478 token_type(alloc
) = token_type(token
);
479 alloc
->pos
.newline
= pos
->newline
;
480 alloc
->pos
.whitespace
= pos
->whitespace
;
481 alloc
->number
= token
->number
;
482 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
486 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
488 int need_copy
= --*count
;
489 while (!eof_token(list
)) {
492 token
= dup_token(list
, &list
->pos
, &list
->pos
);
495 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
496 token
->pos
.noexpand
= 1;
498 where
= &token
->next
;
501 *where
= &eof_token_entry
;
505 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
507 struct token
*token
= *list
;
508 struct position
*base_pos
= &token
->pos
;
509 struct position
*pos
= base_pos
;
511 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
513 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
514 struct token
*added
, *arg
;
517 switch (token_type(body
)) {
518 case TOKEN_GNU_KLUDGE
:
520 * GNU kludge: if we had <comma>##<vararg>, behaviour
521 * depends on whether we had enough arguments to have
522 * a vararg. If we did, ## is just ignored. Otherwise
523 * both , and ## are ignored. Comma should come from
524 * the body of macro and not be an argument of earlier
527 if (!args
[body
->next
->argnum
].arg
)
529 added
= dup_token(body
, base_pos
, pos
);
530 token_type(added
) = TOKEN_SPECIAL
;
534 case TOKEN_STR_ARGUMENT
:
535 arg
= args
[body
->argnum
].str
;
536 count
= &args
[body
->argnum
].n_str
;
539 case TOKEN_QUOTED_ARGUMENT
:
540 arg
= args
[body
->argnum
].arg
;
541 count
= &args
[body
->argnum
].n_quoted
;
542 if (!arg
|| eof_token(arg
)) {
551 case TOKEN_MACRO_ARGUMENT
:
552 arg
= args
[body
->argnum
].expanded
;
553 count
= &args
[body
->argnum
].n_normal
;
554 if (eof_token(arg
)) {
559 tail
= copy(&added
, arg
, count
);
560 added
->pos
.newline
= pos
->newline
;
561 added
->pos
.whitespace
= pos
->whitespace
;
565 if (state
== Placeholder
)
572 added
= dup_token(body
, base_pos
, pos
);
573 if (added
->ident
->tainted
)
574 added
->pos
.noexpand
= 1;
579 added
= dup_token(body
, base_pos
, pos
);
585 * if we got to doing real concatenation, we already have
586 * added something into the list, so containing_token() is OK.
588 if (state
== Concat
&& merge(containing_token(list
), added
)) {
590 if (tail
!= &added
->next
)
598 *list
= &eof_token_entry
;
602 static int expand(struct token
**list
, struct symbol
*sym
)
605 struct token
*token
= *list
;
606 struct ident
*expanding
= token
->ident
;
608 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
609 struct arg args
[nargs
];
611 if (expanding
->tainted
) {
612 token
->pos
.noexpand
= 1;
617 if (!match_op(scan_next(&token
->next
), '('))
619 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
621 expand_arguments(nargs
, args
);
624 expanding
->tainted
= 1;
627 tail
= substitute(list
, sym
->expansion
, args
);
633 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
636 static char buffer
[256];
640 while (!eof_token(token
) && !match_op(token
, endop
)) {
642 const char *val
= token
->string
->data
;
643 if (token_type(token
) != TOKEN_STRING
)
644 val
= show_token(token
);
646 memcpy(ptr
, val
, len
);
651 if (endop
&& !match_op(token
, endop
))
652 sparse_error(start
->pos
, "expected '>' at end of filename");
656 static int already_tokenized(const char *path
)
659 struct stream
*s
= input_streams
;
661 for (i
= input_stream_nr
; --i
>= 0; s
++) {
662 if (s
->constant
!= CONSTANT_FILE_YES
)
664 if (strcmp(path
, s
->name
))
666 if (s
->protect
&& !lookup_macro(s
->protect
))
673 /* Handle include of header files.
674 * The relevant options are made compatible with gcc. The only options that
675 * are not supported is -withprefix and friends.
677 * Three set of include paths are known:
678 * quote_includepath: Path to search when using #include "file.h"
679 * angle_includepath: Paths to search when using #include <file.h>
680 * isys_includepath: Paths specified with -isystem, come before the
681 * built-in system include paths. Gcc would suppress
682 * warnings from system headers. Here we separate
683 * them from the angle_ ones to keep search ordering.
685 * sys_includepath: Built-in include paths.
686 * dirafter_includepath Paths added with -dirafter.
688 * The above is implemented as one array with pointers
690 * quote_includepath ---> | |
694 * angle_includepath ---> | |
696 * isys_includepath ---> | |
698 * sys_includepath ---> | |
700 * dirafter_includepath -> | |
703 * -I dir insert dir just before isys_includepath and move the rest
704 * -I- makes all dirs specified with -I before to quote dirs only and
705 * angle_includepath is set equal to isys_includepath.
706 * -nostdinc removes all sys dirs by storing NULL in entry pointed
707 * to by * sys_includepath. Note that this will reset all dirs built-in
708 * and added before -nostdinc by -isystem and -idirafter.
709 * -isystem dir adds dir where isys_includepath points adding this dir as
711 * -idirafter dir adds dir to the end of the list
714 static void set_stream_include_path(struct stream
*stream
)
716 const char *path
= stream
->path
;
718 const char *p
= strrchr(stream
->name
, '/');
721 int len
= p
- stream
->name
+ 1;
722 char *m
= malloc(len
+1);
723 /* This includes the final "/" */
724 memcpy(m
, stream
->name
, len
);
730 includepath
[0] = path
;
733 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
736 int plen
= strlen(path
);
737 static char fullname
[PATH_MAX
];
739 memcpy(fullname
, path
, plen
);
740 if (plen
&& path
[plen
-1] != '/') {
741 fullname
[plen
] = '/';
744 memcpy(fullname
+plen
, filename
, flen
);
745 if (already_tokenized(fullname
))
747 fd
= open(fullname
, O_RDONLY
);
749 char * streamname
= __alloc_bytes(plen
+ flen
);
750 memcpy(streamname
, fullname
, plen
+ flen
);
751 *where
= tokenize(streamname
, fd
, *where
, next_path
);
758 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
762 while ((path
= *pptr
++) != NULL
) {
763 if (!try_include(path
, filename
, flen
, list
, pptr
))
770 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
772 int flen
= strlen(filename
) + 1;
775 if (filename
[0] == '/') {
776 if (try_include("", filename
, flen
, list
, includepath
))
781 /* Dir of input file is first dir to search for quoted includes */
782 set_stream_include_path(stream
);
785 /* Do not search quote include if <> is in use */
786 path
= local
? quote_includepath
: angle_includepath
;
788 /* Check the standard include paths.. */
789 if (do_include_path(path
, list
, token
, filename
, flen
))
792 error_die(token
->pos
, "unable to open '%s'", filename
);
795 static int free_preprocessor_line(struct token
*token
)
797 while (token_type(token
) != TOKEN_EOF
) {
798 struct token
*free
= token
;
805 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
807 const char *filename
;
813 if (!match_op(next
, '<')) {
814 expand_list(&token
->next
);
817 if (match_op(token
->next
, '<')) {
823 filename
= token_name_sequence(token
, expect
, token
);
824 do_include(!expect
, stream
, list
, token
, filename
, path
);
828 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
830 return handle_include_path(stream
, list
, token
, NULL
);
833 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
835 return handle_include_path(stream
, list
, token
, stream
->next_path
);
838 static int token_different(struct token
*t1
, struct token
*t2
)
842 if (token_type(t1
) != token_type(t2
))
845 switch (token_type(t1
)) {
847 different
= t1
->ident
!= t2
->ident
;
849 case TOKEN_ARG_COUNT
:
852 case TOKEN_GNU_KLUDGE
:
856 different
= strcmp(t1
->number
, t2
->number
);
859 different
= t1
->special
!= t2
->special
;
861 case TOKEN_MACRO_ARGUMENT
:
862 case TOKEN_QUOTED_ARGUMENT
:
863 case TOKEN_STR_ARGUMENT
:
864 different
= t1
->argnum
!= t2
->argnum
;
867 different
= t1
->character
!= t2
->character
;
870 struct string
*s1
, *s2
;
875 if (s1
->length
!= s2
->length
)
877 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
887 static int token_list_different(struct token
*list1
, struct token
*list2
)
892 if (!list1
|| !list2
)
894 if (token_different(list1
, list2
))
901 static inline void set_arg_count(struct token
*token
)
903 token_type(token
) = TOKEN_ARG_COUNT
;
904 token
->count
.normal
= token
->count
.quoted
=
905 token
->count
.str
= token
->count
.vararg
= 0;
908 static struct token
*parse_arguments(struct token
*list
)
910 struct token
*arg
= list
->next
, *next
= list
;
911 struct argcount
*count
= &list
->count
;
915 if (match_op(arg
, ')')) {
917 list
->next
= &eof_token_entry
;
921 while (token_type(arg
) == TOKEN_IDENT
) {
922 if (arg
->ident
== &__VA_ARGS___ident
)
924 if (!++count
->normal
)
928 if (match_op(next
, ',')) {
934 if (match_op(next
, ')')) {
937 arg
->next
->next
= &eof_token_entry
;
941 /* normal cases are finished here */
943 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
944 if (match_op(next
->next
, ')')) {
946 next
->count
.vararg
= 1;
948 arg
->next
->next
= &eof_token_entry
;
956 if (eof_token(next
)) {
964 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
966 token_type(arg
) = TOKEN_IDENT
;
967 arg
->ident
= &__VA_ARGS___ident
;
968 if (!match_op(next
, ')'))
970 if (!++count
->normal
)
973 next
->count
.vararg
= 1;
975 arg
->next
->next
= &eof_token_entry
;
979 if (eof_token(arg
)) {
983 if (match_op(arg
, ','))
990 sparse_error(arg
->pos
, "parameter name missing");
993 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
997 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1000 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1003 sparse_error(arg
->pos
, "too many arguments in macro definition");
1007 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1009 struct ident
*ident
= token
->ident
;
1012 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1015 arglist
= arglist
->next
;
1017 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1018 if (arglist
->ident
== ident
) {
1019 struct argcount
*count
= &arglist
->next
->count
;
1023 token_type(token
) = type
;
1025 case TOKEN_MACRO_ARGUMENT
:
1026 n
= ++count
->normal
;
1028 case TOKEN_QUOTED_ARGUMENT
:
1029 n
= ++count
->quoted
;
1035 return count
->vararg
? 2 : 1;
1036 token_type(token
) = TOKEN_ERROR
;
1043 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1045 struct token
*token
= expansion
;
1047 struct token
*last
= NULL
;
1049 if (match_op(token
, SPECIAL_HASHHASH
))
1052 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1053 if (match_op(token
, '#')) {
1055 struct token
*next
= token
->next
;
1056 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1058 next
->pos
.whitespace
= token
->pos
.whitespace
;
1061 token
->pos
.noexpand
= 1;
1063 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
1064 struct token
*next
= token
->next
;
1065 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1066 token_type(token
) = TOKEN_CONCAT
;
1070 if (arg
== 2 && last
&& match_op(last
, ',')) {
1071 token_type(last
) = TOKEN_GNU_KLUDGE
;
1074 } else if (match_op(next
, SPECIAL_HASHHASH
))
1076 else if (eof_token(next
))
1078 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1079 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1081 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1083 if (token_type(token
) == TOKEN_ERROR
)
1087 token
= alloc_token(&expansion
->pos
);
1088 token_type(token
) = TOKEN_UNTAINT
;
1089 token
->ident
= name
;
1095 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1099 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1102 sparse_error(token
->pos
, "too many instances of argument in body");
1106 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1108 struct token
*arglist
, *expansion
;
1109 struct token
*left
= token
->next
;
1114 if (token_type(left
) != TOKEN_IDENT
) {
1115 sparse_error(token
->pos
, "expected identifier to 'define'");
1122 expansion
= left
->next
;
1123 if (!expansion
->pos
.whitespace
) {
1124 if (match_op(expansion
, '(')) {
1125 arglist
= expansion
;
1126 expansion
= parse_arguments(expansion
);
1129 } else if (!eof_token(expansion
)) {
1130 warning(expansion
->pos
,
1131 "no whitespace before object-like macro body");
1135 expansion
= parse_expansion(expansion
, arglist
, name
);
1140 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1144 if (attr
< sym
->attr
)
1147 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1149 if (token_list_different(sym
->expansion
, expansion
) ||
1150 token_list_different(sym
->arglist
, arglist
)) {
1152 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1153 || sym
->used_in
== file_scope
) {
1154 warning(left
->pos
, "preprocessor token %.*s redefined",
1155 name
->len
, name
->name
);
1156 info(sym
->pos
, "this was the original definition");
1162 if (!sym
|| sym
->scope
!= file_scope
) {
1163 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1164 bind_symbol(sym
, name
, NS_MACRO
);
1169 sym
->expansion
= expansion
;
1170 sym
->arglist
= arglist
;
1171 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1174 sym
->namespace = NS_MACRO
;
1175 sym
->used_in
= NULL
;
1181 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1183 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1186 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1188 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1191 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1193 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1196 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1198 struct token
*left
= token
->next
;
1201 if (token_type(left
) != TOKEN_IDENT
) {
1202 sparse_error(token
->pos
, "expected identifier to 'undef'");
1206 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1208 if (attr
< sym
->attr
)
1210 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1212 } else if (attr
<= SYM_ATTR_NORMAL
)
1215 if (!sym
|| sym
->scope
!= file_scope
) {
1216 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1217 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1220 sym
->namespace = NS_UNDEF
;
1221 sym
->used_in
= NULL
;
1227 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1229 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1232 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1234 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1237 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1239 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1240 free_preprocessor_line(token
->next
);
1241 token
->next
= stream
->top_if
;
1242 stream
->top_if
= token
;
1243 if (false_nesting
|| true != 1)
1248 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1250 struct token
*next
= token
->next
;
1252 if (token_type(next
) == TOKEN_IDENT
) {
1253 arg
= token_defined(next
);
1255 dirty_stream(stream
);
1257 sparse_error(token
->pos
, "expected preprocessor identifier");
1260 return preprocessor_if(stream
, token
, arg
);
1263 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1265 struct token
*next
= token
->next
;
1267 if (token_type(next
) == TOKEN_IDENT
) {
1268 if (!stream
->dirty
&& !stream
->ifndef
) {
1269 if (!stream
->protect
) {
1270 stream
->ifndef
= token
;
1271 stream
->protect
= next
->ident
;
1272 } else if (stream
->protect
== next
->ident
) {
1273 stream
->ifndef
= token
;
1277 arg
= !token_defined(next
);
1279 dirty_stream(stream
);
1281 sparse_error(token
->pos
, "expected preprocessor identifier");
1285 return preprocessor_if(stream
, token
, arg
);
1289 * Expression handling for #if and #elif; it differs from normal expansion
1290 * due to special treatment of "defined".
1292 static int expression_value(struct token
**where
)
1294 struct expression
*expr
;
1296 struct token
**list
= where
, **beginning
= NULL
;
1300 while (!eof_token(p
= scan_next(list
))) {
1303 if (token_type(p
) != TOKEN_IDENT
)
1305 if (p
->ident
== &defined_ident
) {
1310 if (!expand_one_symbol(list
))
1312 if (token_type(p
) != TOKEN_IDENT
)
1314 token_type(p
) = TOKEN_ZERO_IDENT
;
1317 if (match_op(p
, '(')) {
1321 replace_with_defined(p
);
1326 if (token_type(p
) == TOKEN_IDENT
)
1330 replace_with_defined(p
);
1335 if (!match_op(p
, ')'))
1336 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1343 p
= constant_expression(*where
, &expr
);
1345 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1346 value
= get_expression_value(expr
);
1350 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1354 value
= expression_value(&token
->next
);
1356 dirty_stream(stream
);
1357 return preprocessor_if(stream
, token
, value
);
1360 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1362 struct token
*top_if
= stream
->top_if
;
1366 nesting_error(stream
);
1367 sparse_error(token
->pos
, "unmatched #elif within stream");
1371 if (token_type(top_if
) == TOKEN_ELSE
) {
1372 nesting_error(stream
);
1373 sparse_error(token
->pos
, "#elif after #else");
1379 dirty_stream(stream
);
1380 if (token_type(top_if
) != TOKEN_IF
)
1382 if (false_nesting
) {
1384 if (!expression_value(&token
->next
))
1388 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1393 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1395 struct token
*top_if
= stream
->top_if
;
1399 nesting_error(stream
);
1400 sparse_error(token
->pos
, "unmatched #else within stream");
1404 if (token_type(top_if
) == TOKEN_ELSE
) {
1405 nesting_error(stream
);
1406 sparse_error(token
->pos
, "#else after #else");
1408 if (false_nesting
) {
1409 if (token_type(top_if
) == TOKEN_IF
)
1414 token_type(top_if
) = TOKEN_ELSE
;
1418 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1420 struct token
*top_if
= stream
->top_if
;
1423 nesting_error(stream
);
1424 sparse_error(token
->pos
, "unmatched #endif in stream");
1429 stream
->top_if
= top_if
->next
;
1430 __free_token(top_if
);
1434 static const char *show_token_sequence(struct token
*token
)
1436 static char buffer
[1024];
1442 while (!eof_token(token
)) {
1443 const char *val
= show_token(token
);
1444 int len
= strlen(val
);
1446 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1447 sparse_error(token
->pos
, "too long token expansion");
1453 memcpy(ptr
, val
, len
);
1455 token
= token
->next
;
1456 whitespace
= token
->pos
.whitespace
;
1462 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1464 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1468 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1470 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
));
1474 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1477 * Do we have any non-system includes?
1478 * Clear them out if so..
1480 *sys_includepath
= NULL
;
1484 static inline void update_inc_ptrs(const char ***where
)
1487 if (*where
<= dirafter_includepath
) {
1488 dirafter_includepath
++;
1489 /* If this was the entry that we prepend, don't
1490 * rise the lower entries, even if they are at
1491 * the same level. */
1492 if (where
== &dirafter_includepath
)
1495 if (*where
<= sys_includepath
) {
1497 if (where
== &sys_includepath
)
1500 if (*where
<= isys_includepath
) {
1502 if (where
== &isys_includepath
)
1506 /* angle_includepath is actually never updated, since we
1507 * don't suppport -iquote rught now. May change some day. */
1508 if (*where
<= angle_includepath
) {
1509 angle_includepath
++;
1510 if (where
== &angle_includepath
)
1515 /* Add a path before 'where' and update the pointers associated with the
1516 * includepath array */
1517 static void add_path_entry(struct token
*token
, const char *path
,
1518 const char ***where
)
1523 /* Need one free entry.. */
1524 if (includepath
[INCLUDEPATHS
-2])
1525 error_die(token
->pos
, "too many include path entries");
1527 /* check that this is not a duplicate */
1530 if (strcmp(*dst
, path
) == 0)
1537 update_inc_ptrs(where
);
1540 * Move them all up starting at dst,
1541 * insert the new entry..
1544 const char *tmp
= *dst
;
1551 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1554 token
= token
->next
;
1555 if (eof_token(token
))
1557 if (token_type(token
) != TOKEN_STRING
) {
1558 warning(token
->pos
, "expected path string");
1561 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1565 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1568 token
= token
->next
;
1569 if (eof_token(token
))
1571 if (token_type(token
) != TOKEN_STRING
) {
1572 sparse_error(token
->pos
, "expected path string");
1575 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1579 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
1582 token
= token
->next
;
1583 if (eof_token(token
))
1585 if (token_type(token
) != TOKEN_STRING
) {
1586 sparse_error(token
->pos
, "expected path string");
1589 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1593 /* Add to end on includepath list - no pointer updates */
1594 static void add_dirafter_entry(struct token
*token
, const char *path
)
1596 const char **dst
= includepath
;
1598 /* Need one free entry.. */
1599 if (includepath
[INCLUDEPATHS
-2])
1600 error_die(token
->pos
, "too many include path entries");
1602 /* Add to the end */
1610 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1613 token
= token
->next
;
1614 if (eof_token(token
))
1616 if (token_type(token
) != TOKEN_STRING
) {
1617 sparse_error(token
->pos
, "expected path string");
1620 add_dirafter_entry(token
, token
->string
->data
);
1624 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1629 * Split the include path. Any directories specified with `-I'
1630 * options before `-I-' are searched only for headers requested with
1631 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1632 * If additional directories are specified with `-I' options after
1633 * the `-I-', those directories are searched for all `#include'
1635 * In addition, `-I-' inhibits the use of the directory of the current
1636 * file directory as the first search directory for `#include "FILE"'.
1638 quote_includepath
= includepath
+1;
1639 angle_includepath
= sys_includepath
;
1644 * We replace "#pragma xxx" with "__pragma__" in the token
1645 * stream. Just as an example.
1647 * We'll just #define that away for now, but the theory here
1648 * is that we can use this to insert arbitrary token sequences
1649 * to turn the pragmas into internal front-end sequences for
1650 * when we actually start caring about them.
1652 * So eventually this will turn into some kind of extended
1653 * __attribute__() like thing, except called __pragma__(xxx).
1655 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1657 struct token
*next
= *line
;
1659 token
->ident
= &pragma_ident
;
1660 token
->pos
.newline
= 1;
1661 token
->pos
.whitespace
= 1;
1669 * We ignore #line for now.
1671 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1676 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1678 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1683 static void init_preprocessor(void)
1686 int stream
= init_stream("preprocessor", -1, includepath
);
1689 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1691 { "define", handle_define
},
1692 { "weak_define", handle_weak_define
},
1693 { "strong_define", handle_strong_define
},
1694 { "undef", handle_undef
},
1695 { "strong_undef", handle_strong_undef
},
1696 { "warning", handle_warning
},
1697 { "error", handle_error
},
1698 { "include", handle_include
},
1699 { "include_next", handle_include_next
},
1700 { "pragma", handle_pragma
},
1701 { "line", handle_line
},
1703 // our internal preprocessor tokens
1704 { "nostdinc", handle_nostdinc
},
1705 { "add_include", handle_add_include
},
1706 { "add_isystem", handle_add_isystem
},
1707 { "add_system", handle_add_system
},
1708 { "add_dirafter", handle_add_dirafter
},
1709 { "split_include", handle_split_include
},
1711 { "ifdef", handle_ifdef
},
1712 { "ifndef", handle_ifndef
},
1713 { "else", handle_else
},
1714 { "endif", handle_endif
},
1715 { "if", handle_if
},
1716 { "elif", handle_elif
},
1719 for (i
= 0; i
< (sizeof (normal
) / sizeof (normal
[0])); i
++) {
1721 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1722 sym
->handler
= normal
[i
].handler
;
1725 for (i
= 0; i
< (sizeof (special
) / sizeof (special
[0])); i
++) {
1727 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1728 sym
->handler
= special
[i
].handler
;
1734 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1736 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1737 struct token
*token
= start
->next
;
1740 if (eof_token(token
))
1743 if (token_type(token
) == TOKEN_IDENT
) {
1744 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1746 handler
= sym
->handler
;
1747 is_normal
= sym
->normal
;
1749 handler
= handle_nondirective
;
1751 } else if (token_type(token
) == TOKEN_NUMBER
) {
1752 handler
= handle_line
;
1754 handler
= handle_nondirective
;
1758 dirty_stream(stream
);
1762 if (!handler(stream
, line
, token
)) /* all set */
1766 free_preprocessor_line(token
);
1769 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1771 struct token
*start
= *line
, *next
;
1772 struct token
**tp
= &start
->next
;
1776 if (next
->pos
.newline
)
1781 *tp
= &eof_token_entry
;
1782 handle_preprocessor_line(stream
, line
, start
);
1785 static void do_preprocess(struct token
**list
)
1789 while (!eof_token(next
= scan_next(list
))) {
1790 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1792 if (next
->pos
.newline
&& match_op(next
, '#')) {
1793 if (!next
->pos
.noexpand
) {
1794 preprocessor_line(stream
, list
);
1795 __free_token(next
); /* Free the '#' token */
1800 switch (token_type(next
)) {
1801 case TOKEN_STREAMEND
:
1802 if (stream
->top_if
) {
1803 nesting_error(stream
);
1804 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1805 stream
->top_if
= NULL
;
1809 stream
->constant
= CONSTANT_FILE_YES
;
1812 case TOKEN_STREAMBEGIN
:
1817 dirty_stream(stream
);
1818 if (false_nesting
) {
1824 if (token_type(next
) != TOKEN_IDENT
||
1825 expand_one_symbol(list
))
1831 struct token
* preprocess(struct token
*token
)
1834 init_preprocessor();
1835 do_preprocess(&token
);
1837 // Drop all expressions from preprocessing, they're not used any more.
1838 // This is not true when we have multiple files, though ;/
1839 // clear_expression_alloc();