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
)
635 static char buffer
[256];
638 while (!eof_token(token
) && !match_op(token
, endop
)) {
640 const char *val
= token
->string
->data
;
641 if (token_type(token
) != TOKEN_STRING
)
642 val
= show_token(token
);
644 memcpy(ptr
, val
, len
);
649 if (endop
&& !match_op(token
, endop
))
650 sparse_error(start
->pos
, "expected '>' at end of filename");
654 static int already_tokenized(const char *path
)
658 for (stream
= *hash_stream(path
); stream
>= 0 ; stream
= next
) {
659 struct stream
*s
= input_streams
+ stream
;
661 next
= s
->next_stream
;
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 case TOKEN_WIDE_CHAR
:
868 different
= t1
->character
!= t2
->character
;
871 case TOKEN_WIDE_STRING
: {
872 struct string
*s1
, *s2
;
877 if (s1
->length
!= s2
->length
)
879 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
889 static int token_list_different(struct token
*list1
, struct token
*list2
)
894 if (!list1
|| !list2
)
896 if (token_different(list1
, list2
))
903 static inline void set_arg_count(struct token
*token
)
905 token_type(token
) = TOKEN_ARG_COUNT
;
906 token
->count
.normal
= token
->count
.quoted
=
907 token
->count
.str
= token
->count
.vararg
= 0;
910 static struct token
*parse_arguments(struct token
*list
)
912 struct token
*arg
= list
->next
, *next
= list
;
913 struct argcount
*count
= &list
->count
;
917 if (match_op(arg
, ')')) {
919 list
->next
= &eof_token_entry
;
923 while (token_type(arg
) == TOKEN_IDENT
) {
924 if (arg
->ident
== &__VA_ARGS___ident
)
926 if (!++count
->normal
)
930 if (match_op(next
, ',')) {
936 if (match_op(next
, ')')) {
939 arg
->next
->next
= &eof_token_entry
;
943 /* normal cases are finished here */
945 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
946 if (match_op(next
->next
, ')')) {
948 next
->count
.vararg
= 1;
950 arg
->next
->next
= &eof_token_entry
;
958 if (eof_token(next
)) {
966 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
968 token_type(arg
) = TOKEN_IDENT
;
969 arg
->ident
= &__VA_ARGS___ident
;
970 if (!match_op(next
, ')'))
972 if (!++count
->normal
)
975 next
->count
.vararg
= 1;
977 arg
->next
->next
= &eof_token_entry
;
981 if (eof_token(arg
)) {
985 if (match_op(arg
, ','))
992 sparse_error(arg
->pos
, "parameter name missing");
995 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
999 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1002 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1005 sparse_error(arg
->pos
, "too many arguments in macro definition");
1009 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1011 struct ident
*ident
= token
->ident
;
1014 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1017 arglist
= arglist
->next
;
1019 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1020 if (arglist
->ident
== ident
) {
1021 struct argcount
*count
= &arglist
->next
->count
;
1025 token_type(token
) = type
;
1027 case TOKEN_MACRO_ARGUMENT
:
1028 n
= ++count
->normal
;
1030 case TOKEN_QUOTED_ARGUMENT
:
1031 n
= ++count
->quoted
;
1037 return count
->vararg
? 2 : 1;
1038 token_type(token
) = TOKEN_ERROR
;
1045 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1047 struct token
*token
= expansion
;
1049 struct token
*last
= NULL
;
1051 if (match_op(token
, SPECIAL_HASHHASH
))
1054 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1055 if (match_op(token
, '#')) {
1057 struct token
*next
= token
->next
;
1058 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1060 next
->pos
.whitespace
= token
->pos
.whitespace
;
1063 token
->pos
.noexpand
= 1;
1065 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
1066 struct token
*next
= token
->next
;
1067 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1068 token_type(token
) = TOKEN_CONCAT
;
1072 if (arg
== 2 && last
&& match_op(last
, ',')) {
1073 token_type(last
) = TOKEN_GNU_KLUDGE
;
1076 } else if (match_op(next
, SPECIAL_HASHHASH
))
1078 else if (eof_token(next
))
1080 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1081 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1083 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1085 if (token_type(token
) == TOKEN_ERROR
)
1089 token
= alloc_token(&expansion
->pos
);
1090 token_type(token
) = TOKEN_UNTAINT
;
1091 token
->ident
= name
;
1097 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1101 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1104 sparse_error(token
->pos
, "too many instances of argument in body");
1108 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1110 struct token
*arglist
, *expansion
;
1111 struct token
*left
= token
->next
;
1116 if (token_type(left
) != TOKEN_IDENT
) {
1117 sparse_error(token
->pos
, "expected identifier to 'define'");
1124 expansion
= left
->next
;
1125 if (!expansion
->pos
.whitespace
) {
1126 if (match_op(expansion
, '(')) {
1127 arglist
= expansion
;
1128 expansion
= parse_arguments(expansion
);
1131 } else if (!eof_token(expansion
)) {
1132 warning(expansion
->pos
,
1133 "no whitespace before object-like macro body");
1137 expansion
= parse_expansion(expansion
, arglist
, name
);
1142 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1146 if (attr
< sym
->attr
)
1149 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1151 if (token_list_different(sym
->expansion
, expansion
) ||
1152 token_list_different(sym
->arglist
, arglist
)) {
1154 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1155 || sym
->used_in
== file_scope
) {
1156 warning(left
->pos
, "preprocessor token %.*s redefined",
1157 name
->len
, name
->name
);
1158 info(sym
->pos
, "this was the original definition");
1164 if (!sym
|| sym
->scope
!= file_scope
) {
1165 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1166 bind_symbol(sym
, name
, NS_MACRO
);
1171 sym
->expansion
= expansion
;
1172 sym
->arglist
= arglist
;
1173 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1176 sym
->namespace = NS_MACRO
;
1177 sym
->used_in
= NULL
;
1183 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1185 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1188 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1190 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1193 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1195 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1198 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1200 struct token
*left
= token
->next
;
1203 if (token_type(left
) != TOKEN_IDENT
) {
1204 sparse_error(token
->pos
, "expected identifier to 'undef'");
1208 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1210 if (attr
< sym
->attr
)
1212 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1214 } else if (attr
<= SYM_ATTR_NORMAL
)
1217 if (!sym
|| sym
->scope
!= file_scope
) {
1218 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1219 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1222 sym
->namespace = NS_UNDEF
;
1223 sym
->used_in
= NULL
;
1229 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1231 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1234 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1236 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1239 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1241 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1242 free_preprocessor_line(token
->next
);
1243 token
->next
= stream
->top_if
;
1244 stream
->top_if
= token
;
1245 if (false_nesting
|| true != 1)
1250 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1252 struct token
*next
= token
->next
;
1254 if (token_type(next
) == TOKEN_IDENT
) {
1255 arg
= token_defined(next
);
1257 dirty_stream(stream
);
1259 sparse_error(token
->pos
, "expected preprocessor identifier");
1262 return preprocessor_if(stream
, token
, arg
);
1265 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1267 struct token
*next
= token
->next
;
1269 if (token_type(next
) == TOKEN_IDENT
) {
1270 if (!stream
->dirty
&& !stream
->ifndef
) {
1271 if (!stream
->protect
) {
1272 stream
->ifndef
= token
;
1273 stream
->protect
= next
->ident
;
1274 } else if (stream
->protect
== next
->ident
) {
1275 stream
->ifndef
= token
;
1279 arg
= !token_defined(next
);
1281 dirty_stream(stream
);
1283 sparse_error(token
->pos
, "expected preprocessor identifier");
1287 return preprocessor_if(stream
, token
, arg
);
1291 * Expression handling for #if and #elif; it differs from normal expansion
1292 * due to special treatment of "defined".
1294 static int expression_value(struct token
**where
)
1296 struct expression
*expr
;
1298 struct token
**list
= where
, **beginning
= NULL
;
1302 while (!eof_token(p
= scan_next(list
))) {
1305 if (token_type(p
) != TOKEN_IDENT
)
1307 if (p
->ident
== &defined_ident
) {
1312 if (!expand_one_symbol(list
))
1314 if (token_type(p
) != TOKEN_IDENT
)
1316 token_type(p
) = TOKEN_ZERO_IDENT
;
1319 if (match_op(p
, '(')) {
1323 replace_with_defined(p
);
1328 if (token_type(p
) == TOKEN_IDENT
)
1332 replace_with_defined(p
);
1337 if (!match_op(p
, ')'))
1338 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1345 p
= constant_expression(*where
, &expr
);
1347 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1348 value
= get_expression_value(expr
);
1352 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1356 value
= expression_value(&token
->next
);
1358 dirty_stream(stream
);
1359 return preprocessor_if(stream
, token
, value
);
1362 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1364 struct token
*top_if
= stream
->top_if
;
1368 nesting_error(stream
);
1369 sparse_error(token
->pos
, "unmatched #elif within stream");
1373 if (token_type(top_if
) == TOKEN_ELSE
) {
1374 nesting_error(stream
);
1375 sparse_error(token
->pos
, "#elif after #else");
1381 dirty_stream(stream
);
1382 if (token_type(top_if
) != TOKEN_IF
)
1384 if (false_nesting
) {
1386 if (!expression_value(&token
->next
))
1390 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1395 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1397 struct token
*top_if
= stream
->top_if
;
1401 nesting_error(stream
);
1402 sparse_error(token
->pos
, "unmatched #else within stream");
1406 if (token_type(top_if
) == TOKEN_ELSE
) {
1407 nesting_error(stream
);
1408 sparse_error(token
->pos
, "#else after #else");
1410 if (false_nesting
) {
1411 if (token_type(top_if
) == TOKEN_IF
)
1416 token_type(top_if
) = TOKEN_ELSE
;
1420 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1422 struct token
*top_if
= stream
->top_if
;
1425 nesting_error(stream
);
1426 sparse_error(token
->pos
, "unmatched #endif in stream");
1431 stream
->top_if
= top_if
->next
;
1432 __free_token(top_if
);
1436 static const char *show_token_sequence(struct token
*token
)
1438 static char buffer
[1024];
1444 while (!eof_token(token
)) {
1445 const char *val
= show_token(token
);
1446 int len
= strlen(val
);
1448 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1449 sparse_error(token
->pos
, "too long token expansion");
1455 memcpy(ptr
, val
, len
);
1457 token
= token
->next
;
1458 whitespace
= token
->pos
.whitespace
;
1464 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1466 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1470 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1472 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
));
1476 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1479 * Do we have any non-system includes?
1480 * Clear them out if so..
1482 *sys_includepath
= NULL
;
1486 static inline void update_inc_ptrs(const char ***where
)
1489 if (*where
<= dirafter_includepath
) {
1490 dirafter_includepath
++;
1491 /* If this was the entry that we prepend, don't
1492 * rise the lower entries, even if they are at
1493 * the same level. */
1494 if (where
== &dirafter_includepath
)
1497 if (*where
<= sys_includepath
) {
1499 if (where
== &sys_includepath
)
1502 if (*where
<= isys_includepath
) {
1504 if (where
== &isys_includepath
)
1508 /* angle_includepath is actually never updated, since we
1509 * don't suppport -iquote rught now. May change some day. */
1510 if (*where
<= angle_includepath
) {
1511 angle_includepath
++;
1512 if (where
== &angle_includepath
)
1517 /* Add a path before 'where' and update the pointers associated with the
1518 * includepath array */
1519 static void add_path_entry(struct token
*token
, const char *path
,
1520 const char ***where
)
1525 /* Need one free entry.. */
1526 if (includepath
[INCLUDEPATHS
-2])
1527 error_die(token
->pos
, "too many include path entries");
1529 /* check that this is not a duplicate */
1532 if (strcmp(*dst
, path
) == 0)
1539 update_inc_ptrs(where
);
1542 * Move them all up starting at dst,
1543 * insert the new entry..
1546 const char *tmp
= *dst
;
1553 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1556 token
= token
->next
;
1557 if (eof_token(token
))
1559 if (token_type(token
) != TOKEN_STRING
) {
1560 warning(token
->pos
, "expected path string");
1563 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1567 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1570 token
= token
->next
;
1571 if (eof_token(token
))
1573 if (token_type(token
) != TOKEN_STRING
) {
1574 sparse_error(token
->pos
, "expected path string");
1577 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1581 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
1584 token
= token
->next
;
1585 if (eof_token(token
))
1587 if (token_type(token
) != TOKEN_STRING
) {
1588 sparse_error(token
->pos
, "expected path string");
1591 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1595 /* Add to end on includepath list - no pointer updates */
1596 static void add_dirafter_entry(struct token
*token
, const char *path
)
1598 const char **dst
= includepath
;
1600 /* Need one free entry.. */
1601 if (includepath
[INCLUDEPATHS
-2])
1602 error_die(token
->pos
, "too many include path entries");
1604 /* Add to the end */
1612 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1615 token
= token
->next
;
1616 if (eof_token(token
))
1618 if (token_type(token
) != TOKEN_STRING
) {
1619 sparse_error(token
->pos
, "expected path string");
1622 add_dirafter_entry(token
, token
->string
->data
);
1626 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1631 * Split the include path. Any directories specified with `-I'
1632 * options before `-I-' are searched only for headers requested with
1633 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1634 * If additional directories are specified with `-I' options after
1635 * the `-I-', those directories are searched for all `#include'
1637 * In addition, `-I-' inhibits the use of the directory of the current
1638 * file directory as the first search directory for `#include "FILE"'.
1640 quote_includepath
= includepath
+1;
1641 angle_includepath
= sys_includepath
;
1646 * We replace "#pragma xxx" with "__pragma__" in the token
1647 * stream. Just as an example.
1649 * We'll just #define that away for now, but the theory here
1650 * is that we can use this to insert arbitrary token sequences
1651 * to turn the pragmas into internal front-end sequences for
1652 * when we actually start caring about them.
1654 * So eventually this will turn into some kind of extended
1655 * __attribute__() like thing, except called __pragma__(xxx).
1657 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1659 struct token
*next
= *line
;
1661 token
->ident
= &pragma_ident
;
1662 token
->pos
.newline
= 1;
1663 token
->pos
.whitespace
= 1;
1671 * We ignore #line for now.
1673 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1678 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1680 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1685 static void init_preprocessor(void)
1688 int stream
= init_stream("preprocessor", -1, includepath
);
1691 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1693 { "define", handle_define
},
1694 { "weak_define", handle_weak_define
},
1695 { "strong_define", handle_strong_define
},
1696 { "undef", handle_undef
},
1697 { "strong_undef", handle_strong_undef
},
1698 { "warning", handle_warning
},
1699 { "error", handle_error
},
1700 { "include", handle_include
},
1701 { "include_next", handle_include_next
},
1702 { "pragma", handle_pragma
},
1703 { "line", handle_line
},
1705 // our internal preprocessor tokens
1706 { "nostdinc", handle_nostdinc
},
1707 { "add_include", handle_add_include
},
1708 { "add_isystem", handle_add_isystem
},
1709 { "add_system", handle_add_system
},
1710 { "add_dirafter", handle_add_dirafter
},
1711 { "split_include", handle_split_include
},
1713 { "ifdef", handle_ifdef
},
1714 { "ifndef", handle_ifndef
},
1715 { "else", handle_else
},
1716 { "endif", handle_endif
},
1717 { "if", handle_if
},
1718 { "elif", handle_elif
},
1721 for (i
= 0; i
< ARRAY_SIZE(normal
); i
++) {
1723 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1724 sym
->handler
= normal
[i
].handler
;
1727 for (i
= 0; i
< ARRAY_SIZE(special
); i
++) {
1729 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1730 sym
->handler
= special
[i
].handler
;
1736 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1738 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1739 struct token
*token
= start
->next
;
1742 if (eof_token(token
))
1745 if (token_type(token
) == TOKEN_IDENT
) {
1746 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1748 handler
= sym
->handler
;
1749 is_normal
= sym
->normal
;
1751 handler
= handle_nondirective
;
1753 } else if (token_type(token
) == TOKEN_NUMBER
) {
1754 handler
= handle_line
;
1756 handler
= handle_nondirective
;
1760 dirty_stream(stream
);
1764 if (!handler(stream
, line
, token
)) /* all set */
1768 free_preprocessor_line(token
);
1771 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1773 struct token
*start
= *line
, *next
;
1774 struct token
**tp
= &start
->next
;
1778 if (next
->pos
.newline
)
1783 *tp
= &eof_token_entry
;
1784 handle_preprocessor_line(stream
, line
, start
);
1787 static void do_preprocess(struct token
**list
)
1791 while (!eof_token(next
= scan_next(list
))) {
1792 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1794 if (next
->pos
.newline
&& match_op(next
, '#')) {
1795 if (!next
->pos
.noexpand
) {
1796 preprocessor_line(stream
, list
);
1797 __free_token(next
); /* Free the '#' token */
1802 switch (token_type(next
)) {
1803 case TOKEN_STREAMEND
:
1804 if (stream
->top_if
) {
1805 nesting_error(stream
);
1806 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1807 stream
->top_if
= NULL
;
1811 stream
->constant
= CONSTANT_FILE_YES
;
1814 case TOKEN_STREAMBEGIN
:
1819 dirty_stream(stream
);
1820 if (false_nesting
) {
1826 if (token_type(next
) != TOKEN_IDENT
||
1827 expand_one_symbol(list
))
1833 struct token
* preprocess(struct token
*token
)
1836 init_preprocessor();
1837 do_preprocess(&token
);
1839 // Drop all expressions from preprocessing, they're not used any more.
1840 // This is not true when we have multiple files, though ;/
1841 // clear_expression_alloc();