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 store_macro_pos(token
);
154 sym
->used_in
= file_scope
;
155 return expand(list
, sym
);
157 if (token
->ident
== &__LINE___ident
) {
158 replace_with_integer(token
, token
->pos
.line
);
159 } else if (token
->ident
== &__FILE___ident
) {
160 replace_with_string(token
, stream_name(token
->pos
.stream
));
161 } else if (token
->ident
== &__DATE___ident
) {
164 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
165 replace_with_string(token
, buffer
);
166 } else if (token
->ident
== &__TIME___ident
) {
169 strftime(buffer
, 9, "%T", localtime(&t
));
170 replace_with_string(token
, buffer
);
175 static inline struct token
*scan_next(struct token
**where
)
177 struct token
*token
= *where
;
178 if (token_type(token
) != TOKEN_UNTAINT
)
181 token
->ident
->tainted
= 0;
183 } while (token_type(token
) == TOKEN_UNTAINT
);
188 static void expand_list(struct token
**list
)
191 while (!eof_token(next
= scan_next(list
))) {
192 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
197 static void preprocessor_line(struct stream
*stream
, struct token
**line
);
199 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
201 struct stream
*stream
= input_streams
+ prev
->pos
.stream
;
202 struct token
**p
= &prev
->next
;
206 while (!eof_token(next
= scan_next(p
))) {
207 if (next
->pos
.newline
&& match_op(next
, '#')) {
208 if (!next
->pos
.noexpand
) {
209 sparse_error(next
->pos
,
210 "directive in argument list");
211 preprocessor_line(stream
, p
);
212 __free_token(next
); /* Free the '#' token */
216 switch (token_type(next
)) {
217 case TOKEN_STREAMEND
:
218 case TOKEN_STREAMBEGIN
:
219 *p
= &eof_token_entry
;
227 if (match_op(next
, '(')) {
229 } else if (match_op(next
, ')')) {
232 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
235 next
->pos
.stream
= pos
->stream
;
236 next
->pos
.line
= pos
->line
;
237 next
->pos
.pos
= pos
->pos
;
240 *p
= &eof_token_entry
;
245 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
250 struct token
*expanded
;
257 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
259 int wanted
= arglist
->count
.normal
;
260 struct token
*next
= NULL
;
263 arglist
= arglist
->next
; /* skip counter */
266 next
= collect_arg(start
, 0, &what
->pos
);
269 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
274 for (count
= 0; count
< wanted
; count
++) {
275 struct argcount
*p
= &arglist
->next
->count
;
276 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
277 arglist
= arglist
->next
->next
;
280 args
[count
].arg
= start
->next
;
281 args
[count
].n_normal
= p
->normal
;
282 args
[count
].n_quoted
= p
->quoted
;
283 args
[count
].n_str
= p
->str
;
284 if (match_op(next
, ')')) {
290 if (count
== wanted
&& !match_op(next
, ')'))
292 if (count
== wanted
- 1) {
293 struct argcount
*p
= &arglist
->next
->count
;
296 args
[count
].arg
= NULL
;
297 args
[count
].n_normal
= p
->normal
;
298 args
[count
].n_quoted
= p
->quoted
;
299 args
[count
].n_str
= p
->str
;
301 if (count
< wanted
- 1)
304 what
->next
= next
->next
;
308 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
309 show_token(what
), wanted
, count
);
312 while (match_op(next
, ',')) {
313 next
= collect_arg(next
, 0, &what
->pos
);
318 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
319 show_token(what
), count
, wanted
);
322 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
325 what
->next
= next
->next
;
329 static struct token
*dup_list(struct token
*list
)
331 struct token
*res
= NULL
;
332 struct token
**p
= &res
;
334 while (!eof_token(list
)) {
335 struct token
*newtok
= __alloc_token(0);
344 static struct token
*stringify(struct token
*arg
)
346 const char *s
= show_token_sequence(arg
);
347 int size
= strlen(s
)+1;
348 struct token
*token
= __alloc_token(0);
349 struct string
*string
= __alloc_string(size
);
351 memcpy(string
->data
, s
, size
);
352 string
->length
= size
;
353 token
->pos
= arg
->pos
;
354 token_type(token
) = TOKEN_STRING
;
355 token
->string
= string
;
356 token
->next
= &eof_token_entry
;
360 static void expand_arguments(int count
, struct arg
*args
)
363 for (i
= 0; i
< count
; i
++) {
364 struct token
*arg
= args
[i
].arg
;
366 arg
= &eof_token_entry
;
368 args
[i
].str
= stringify(arg
);
369 if (args
[i
].n_normal
) {
370 if (!args
[i
].n_quoted
) {
371 args
[i
].expanded
= arg
;
373 } else if (eof_token(arg
)) {
374 args
[i
].expanded
= arg
;
376 args
[i
].expanded
= dup_list(arg
);
378 expand_list(&args
[i
].expanded
);
384 * Possibly valid combinations:
385 * - ident + ident -> ident
386 * - ident + number -> ident unless number contains '.', '+' or '-'.
387 * - number + number -> number
388 * - number + ident -> number
389 * - number + '.' -> number
390 * - number + '+' or '-' -> number, if number used to end on [eEpP].
391 * - '.' + number -> number, if number used to start with a digit.
392 * - special + special -> either special or an error.
394 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
397 enum token_type t1
= token_type(left
), t2
= token_type(right
);
399 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
402 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
405 strcpy(p
, show_token(left
));
406 strcat(p
, show_token(right
));
412 if (t1
== TOKEN_IDENT
) {
413 if (t2
== TOKEN_SPECIAL
)
415 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
420 if (t1
== TOKEN_NUMBER
) {
421 if (t2
== TOKEN_SPECIAL
) {
422 switch (right
->special
) {
426 if (strchr("eEpP", p
[len
- 2]))
435 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
438 return TOKEN_SPECIAL
;
441 static int merge(struct token
*left
, struct token
*right
)
443 static char buffer
[512];
446 switch (combine(left
, right
, buffer
)) {
448 left
->ident
= built_in_ident(buffer
);
449 left
->pos
.noexpand
= 0;
453 char *number
= __alloc_bytes(strlen(buffer
) + 1);
454 memcpy(number
, buffer
, strlen(buffer
) + 1);
455 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
456 left
->number
= number
;
461 if (buffer
[2] && buffer
[3])
463 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
464 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
472 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
476 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
478 struct token
*alloc
= alloc_token(streampos
);
479 token_type(alloc
) = token_type(token
);
480 alloc
->pos
.newline
= pos
->newline
;
481 alloc
->pos
.whitespace
= pos
->whitespace
;
482 alloc
->number
= token
->number
;
483 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
487 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
489 int need_copy
= --*count
;
490 while (!eof_token(list
)) {
493 token
= dup_token(list
, &list
->pos
, &list
->pos
);
496 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
497 token
->pos
.noexpand
= 1;
499 where
= &token
->next
;
502 *where
= &eof_token_entry
;
506 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
508 struct token
*token
= *list
;
509 struct position
*base_pos
= &token
->pos
;
510 struct position
*pos
= base_pos
;
512 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
514 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
515 struct token
*added
, *arg
;
518 switch (token_type(body
)) {
519 case TOKEN_GNU_KLUDGE
:
521 * GNU kludge: if we had <comma>##<vararg>, behaviour
522 * depends on whether we had enough arguments to have
523 * a vararg. If we did, ## is just ignored. Otherwise
524 * both , and ## are ignored. Comma should come from
525 * the body of macro and not be an argument of earlier
528 if (!args
[body
->next
->argnum
].arg
)
530 added
= dup_token(body
, base_pos
, pos
);
531 token_type(added
) = TOKEN_SPECIAL
;
535 case TOKEN_STR_ARGUMENT
:
536 arg
= args
[body
->argnum
].str
;
537 count
= &args
[body
->argnum
].n_str
;
540 case TOKEN_QUOTED_ARGUMENT
:
541 arg
= args
[body
->argnum
].arg
;
542 count
= &args
[body
->argnum
].n_quoted
;
543 if (!arg
|| eof_token(arg
)) {
552 case TOKEN_MACRO_ARGUMENT
:
553 arg
= args
[body
->argnum
].expanded
;
554 count
= &args
[body
->argnum
].n_normal
;
555 if (eof_token(arg
)) {
560 tail
= copy(&added
, arg
, count
);
561 added
->pos
.newline
= pos
->newline
;
562 added
->pos
.whitespace
= pos
->whitespace
;
566 if (state
== Placeholder
)
573 added
= dup_token(body
, base_pos
, pos
);
574 if (added
->ident
->tainted
)
575 added
->pos
.noexpand
= 1;
580 added
= dup_token(body
, base_pos
, pos
);
586 * if we got to doing real concatenation, we already have
587 * added something into the list, so containing_token() is OK.
589 if (state
== Concat
&& merge(containing_token(list
), added
)) {
591 if (tail
!= &added
->next
)
599 *list
= &eof_token_entry
;
603 static int expand(struct token
**list
, struct symbol
*sym
)
606 struct token
*token
= *list
;
607 struct ident
*expanding
= token
->ident
;
609 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
610 struct arg args
[nargs
];
612 if (expanding
->tainted
) {
613 token
->pos
.noexpand
= 1;
618 if (!match_op(scan_next(&token
->next
), '('))
620 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
622 expand_arguments(nargs
, args
);
625 expanding
->tainted
= 1;
628 tail
= substitute(list
, sym
->expansion
, args
);
634 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
637 static char buffer
[256];
641 while (!eof_token(token
) && !match_op(token
, endop
)) {
643 const char *val
= token
->string
->data
;
644 if (token_type(token
) != TOKEN_STRING
)
645 val
= show_token(token
);
647 memcpy(ptr
, val
, len
);
652 if (endop
&& !match_op(token
, endop
))
653 sparse_error(start
->pos
, "expected '>' at end of filename");
657 static int already_tokenized(const char *path
)
660 struct stream
*s
= input_streams
;
662 for (i
= input_stream_nr
; --i
>= 0; s
++) {
663 if (s
->constant
!= CONSTANT_FILE_YES
)
665 if (strcmp(path
, s
->name
))
667 if (s
->protect
&& !lookup_macro(s
->protect
))
674 /* Handle include of header files.
675 * The relevant options are made compatible with gcc. The only options that
676 * are not supported is -withprefix and friends.
678 * Three set of include paths are known:
679 * quote_includepath: Path to search when using #include "file.h"
680 * angle_includepath: Paths to search when using #include <file.h>
681 * isys_includepath: Paths specified with -isystem, come before the
682 * built-in system include paths. Gcc would suppress
683 * warnings from system headers. Here we separate
684 * them from the angle_ ones to keep search ordering.
686 * sys_includepath: Built-in include paths.
687 * dirafter_includepath Paths added with -dirafter.
689 * The above is implemented as one array with pointers
691 * quote_includepath ---> | |
695 * angle_includepath ---> | |
697 * isys_includepath ---> | |
699 * sys_includepath ---> | |
701 * dirafter_includepath -> | |
704 * -I dir insert dir just before isys_includepath and move the rest
705 * -I- makes all dirs specified with -I before to quote dirs only and
706 * angle_includepath is set equal to isys_includepath.
707 * -nostdinc removes all sys dirs by storing NULL in entry pointed
708 * to by * sys_includepath. Note that this will reset all dirs built-in
709 * and added before -nostdinc by -isystem and -idirafter.
710 * -isystem dir adds dir where isys_includepath points adding this dir as
712 * -idirafter dir adds dir to the end of the list
715 static void set_stream_include_path(struct stream
*stream
)
717 const char *path
= stream
->path
;
719 const char *p
= strrchr(stream
->name
, '/');
722 int len
= p
- stream
->name
+ 1;
723 char *m
= malloc(len
+1);
724 /* This includes the final "/" */
725 memcpy(m
, stream
->name
, len
);
731 includepath
[0] = path
;
734 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
737 int plen
= strlen(path
);
738 static char fullname
[PATH_MAX
];
740 memcpy(fullname
, path
, plen
);
741 if (plen
&& path
[plen
-1] != '/') {
742 fullname
[plen
] = '/';
745 memcpy(fullname
+plen
, filename
, flen
);
746 if (already_tokenized(fullname
))
748 fd
= open(fullname
, O_RDONLY
);
750 char * streamname
= __alloc_bytes(plen
+ flen
);
751 memcpy(streamname
, fullname
, plen
+ flen
);
752 *where
= tokenize(streamname
, fd
, *where
, next_path
);
759 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
763 while ((path
= *pptr
++) != NULL
) {
764 if (!try_include(path
, filename
, flen
, list
, pptr
))
771 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
773 int flen
= strlen(filename
) + 1;
776 if (filename
[0] == '/') {
777 if (try_include("", filename
, flen
, list
, includepath
))
782 /* Dir of input file is first dir to search for quoted includes */
783 set_stream_include_path(stream
);
786 /* Do not search quote include if <> is in use */
787 path
= local
? quote_includepath
: angle_includepath
;
789 /* Check the standard include paths.. */
790 if (do_include_path(path
, list
, token
, filename
, flen
))
793 error_die(token
->pos
, "unable to open '%s'", filename
);
796 static int free_preprocessor_line(struct token
*token
)
798 while (token_type(token
) != TOKEN_EOF
) {
799 struct token
*free
= token
;
806 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
808 const char *filename
;
814 if (!match_op(next
, '<')) {
815 expand_list(&token
->next
);
818 if (match_op(token
->next
, '<')) {
824 filename
= token_name_sequence(token
, expect
, token
);
825 do_include(!expect
, stream
, list
, token
, filename
, path
);
829 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
831 return handle_include_path(stream
, list
, token
, NULL
);
834 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
836 return handle_include_path(stream
, list
, token
, stream
->next_path
);
839 static int token_different(struct token
*t1
, struct token
*t2
)
843 if (token_type(t1
) != token_type(t2
))
846 switch (token_type(t1
)) {
848 different
= t1
->ident
!= t2
->ident
;
850 case TOKEN_ARG_COUNT
:
853 case TOKEN_GNU_KLUDGE
:
857 different
= strcmp(t1
->number
, t2
->number
);
860 different
= t1
->special
!= t2
->special
;
862 case TOKEN_MACRO_ARGUMENT
:
863 case TOKEN_QUOTED_ARGUMENT
:
864 case TOKEN_STR_ARGUMENT
:
865 different
= t1
->argnum
!= t2
->argnum
;
868 different
= t1
->character
!= t2
->character
;
871 struct string
*s1
, *s2
;
876 if (s1
->length
!= s2
->length
)
878 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
888 static int token_list_different(struct token
*list1
, struct token
*list2
)
893 if (!list1
|| !list2
)
895 if (token_different(list1
, list2
))
902 static inline void set_arg_count(struct token
*token
)
904 token_type(token
) = TOKEN_ARG_COUNT
;
905 token
->count
.normal
= token
->count
.quoted
=
906 token
->count
.str
= token
->count
.vararg
= 0;
909 static struct token
*parse_arguments(struct token
*list
)
911 struct token
*arg
= list
->next
, *next
= list
;
912 struct argcount
*count
= &list
->count
;
916 if (match_op(arg
, ')')) {
918 list
->next
= &eof_token_entry
;
922 while (token_type(arg
) == TOKEN_IDENT
) {
923 if (arg
->ident
== &__VA_ARGS___ident
)
925 if (!++count
->normal
)
929 if (match_op(next
, ',')) {
935 if (match_op(next
, ')')) {
938 arg
->next
->next
= &eof_token_entry
;
942 /* normal cases are finished here */
944 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
945 if (match_op(next
->next
, ')')) {
947 next
->count
.vararg
= 1;
949 arg
->next
->next
= &eof_token_entry
;
957 if (eof_token(next
)) {
965 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
967 token_type(arg
) = TOKEN_IDENT
;
968 arg
->ident
= &__VA_ARGS___ident
;
969 if (!match_op(next
, ')'))
971 if (!++count
->normal
)
974 next
->count
.vararg
= 1;
976 arg
->next
->next
= &eof_token_entry
;
980 if (eof_token(arg
)) {
984 if (match_op(arg
, ','))
991 sparse_error(arg
->pos
, "parameter name missing");
994 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
998 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1001 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1004 sparse_error(arg
->pos
, "too many arguments in macro definition");
1008 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1010 struct ident
*ident
= token
->ident
;
1013 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1016 arglist
= arglist
->next
;
1018 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1019 if (arglist
->ident
== ident
) {
1020 struct argcount
*count
= &arglist
->next
->count
;
1024 token_type(token
) = type
;
1026 case TOKEN_MACRO_ARGUMENT
:
1027 n
= ++count
->normal
;
1029 case TOKEN_QUOTED_ARGUMENT
:
1030 n
= ++count
->quoted
;
1036 return count
->vararg
? 2 : 1;
1037 token_type(token
) = TOKEN_ERROR
;
1044 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1046 struct token
*token
= expansion
;
1048 struct token
*last
= NULL
;
1050 if (match_op(token
, SPECIAL_HASHHASH
))
1053 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1054 if (match_op(token
, '#')) {
1056 struct token
*next
= token
->next
;
1057 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1059 next
->pos
.whitespace
= token
->pos
.whitespace
;
1062 token
->pos
.noexpand
= 1;
1064 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
1065 struct token
*next
= token
->next
;
1066 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1067 token_type(token
) = TOKEN_CONCAT
;
1071 if (arg
== 2 && last
&& match_op(last
, ',')) {
1072 token_type(last
) = TOKEN_GNU_KLUDGE
;
1075 } else if (match_op(next
, SPECIAL_HASHHASH
))
1077 else if (eof_token(next
))
1079 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1080 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1082 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1084 if (token_type(token
) == TOKEN_ERROR
)
1088 token
= alloc_token(&expansion
->pos
);
1089 token_type(token
) = TOKEN_UNTAINT
;
1090 token
->ident
= name
;
1096 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1100 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1103 sparse_error(token
->pos
, "too many instances of argument in body");
1107 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1109 struct token
*arglist
, *expansion
;
1110 struct token
*left
= token
->next
;
1115 if (token_type(left
) != TOKEN_IDENT
) {
1116 sparse_error(token
->pos
, "expected identifier to 'define'");
1123 expansion
= left
->next
;
1124 if (!expansion
->pos
.whitespace
) {
1125 if (match_op(expansion
, '(')) {
1126 arglist
= expansion
;
1127 expansion
= parse_arguments(expansion
);
1130 } else if (!eof_token(expansion
)) {
1131 warning(expansion
->pos
,
1132 "no whitespace before object-like macro body");
1136 expansion
= parse_expansion(expansion
, arglist
, name
);
1141 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1145 if (attr
< sym
->attr
)
1148 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1150 if (token_list_different(sym
->expansion
, expansion
) ||
1151 token_list_different(sym
->arglist
, arglist
)) {
1153 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1154 || sym
->used_in
== file_scope
) {
1155 warning(left
->pos
, "preprocessor token %.*s redefined",
1156 name
->len
, name
->name
);
1157 info(sym
->pos
, "this was the original definition");
1163 if (!sym
|| sym
->scope
!= file_scope
) {
1164 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1165 bind_symbol(sym
, name
, NS_MACRO
);
1170 sym
->expansion
= expansion
;
1171 sym
->arglist
= arglist
;
1172 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1175 sym
->namespace = NS_MACRO
;
1176 sym
->used_in
= NULL
;
1182 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1184 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1187 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1189 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1192 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1194 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1197 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1199 struct token
*left
= token
->next
;
1202 if (token_type(left
) != TOKEN_IDENT
) {
1203 sparse_error(token
->pos
, "expected identifier to 'undef'");
1207 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1209 if (attr
< sym
->attr
)
1211 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1213 } else if (attr
<= SYM_ATTR_NORMAL
)
1216 if (!sym
|| sym
->scope
!= file_scope
) {
1217 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1218 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1221 sym
->namespace = NS_UNDEF
;
1222 sym
->used_in
= NULL
;
1228 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1230 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1233 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1235 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1238 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1240 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1241 free_preprocessor_line(token
->next
);
1242 token
->next
= stream
->top_if
;
1243 stream
->top_if
= token
;
1244 if (false_nesting
|| true != 1)
1249 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1251 struct token
*next
= token
->next
;
1253 if (token_type(next
) == TOKEN_IDENT
) {
1254 arg
= token_defined(next
);
1256 dirty_stream(stream
);
1258 sparse_error(token
->pos
, "expected preprocessor identifier");
1261 return preprocessor_if(stream
, token
, arg
);
1264 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1266 struct token
*next
= token
->next
;
1268 if (token_type(next
) == TOKEN_IDENT
) {
1269 if (!stream
->dirty
&& !stream
->ifndef
) {
1270 if (!stream
->protect
) {
1271 stream
->ifndef
= token
;
1272 stream
->protect
= next
->ident
;
1273 } else if (stream
->protect
== next
->ident
) {
1274 stream
->ifndef
= token
;
1278 arg
= !token_defined(next
);
1280 dirty_stream(stream
);
1282 sparse_error(token
->pos
, "expected preprocessor identifier");
1286 return preprocessor_if(stream
, token
, arg
);
1290 * Expression handling for #if and #elif; it differs from normal expansion
1291 * due to special treatment of "defined".
1293 static int expression_value(struct token
**where
)
1295 struct expression
*expr
;
1297 struct token
**list
= where
, **beginning
= NULL
;
1301 while (!eof_token(p
= scan_next(list
))) {
1304 if (token_type(p
) != TOKEN_IDENT
)
1306 if (p
->ident
== &defined_ident
) {
1311 if (!expand_one_symbol(list
))
1313 if (token_type(p
) != TOKEN_IDENT
)
1315 token_type(p
) = TOKEN_ZERO_IDENT
;
1318 if (match_op(p
, '(')) {
1322 replace_with_defined(p
);
1327 if (token_type(p
) == TOKEN_IDENT
)
1331 replace_with_defined(p
);
1336 if (!match_op(p
, ')'))
1337 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1344 p
= constant_expression(*where
, &expr
);
1346 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1347 value
= get_expression_value(expr
);
1351 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1355 value
= expression_value(&token
->next
);
1357 dirty_stream(stream
);
1358 return preprocessor_if(stream
, token
, value
);
1361 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1363 struct token
*top_if
= stream
->top_if
;
1367 nesting_error(stream
);
1368 sparse_error(token
->pos
, "unmatched #elif within stream");
1372 if (token_type(top_if
) == TOKEN_ELSE
) {
1373 nesting_error(stream
);
1374 sparse_error(token
->pos
, "#elif after #else");
1380 dirty_stream(stream
);
1381 if (token_type(top_if
) != TOKEN_IF
)
1383 if (false_nesting
) {
1385 if (!expression_value(&token
->next
))
1389 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1394 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1396 struct token
*top_if
= stream
->top_if
;
1400 nesting_error(stream
);
1401 sparse_error(token
->pos
, "unmatched #else within stream");
1405 if (token_type(top_if
) == TOKEN_ELSE
) {
1406 nesting_error(stream
);
1407 sparse_error(token
->pos
, "#else after #else");
1409 if (false_nesting
) {
1410 if (token_type(top_if
) == TOKEN_IF
)
1415 token_type(top_if
) = TOKEN_ELSE
;
1419 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1421 struct token
*top_if
= stream
->top_if
;
1424 nesting_error(stream
);
1425 sparse_error(token
->pos
, "unmatched #endif in stream");
1430 stream
->top_if
= top_if
->next
;
1431 __free_token(top_if
);
1435 static const char *show_token_sequence(struct token
*token
)
1437 static char buffer
[1024];
1443 while (!eof_token(token
)) {
1444 const char *val
= show_token(token
);
1445 int len
= strlen(val
);
1447 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1448 sparse_error(token
->pos
, "too long token expansion");
1454 memcpy(ptr
, val
, len
);
1456 token
= token
->next
;
1457 whitespace
= token
->pos
.whitespace
;
1463 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1465 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1469 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1471 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
));
1475 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1478 * Do we have any non-system includes?
1479 * Clear them out if so..
1481 *sys_includepath
= NULL
;
1485 static inline void update_inc_ptrs(const char ***where
)
1488 if (*where
<= dirafter_includepath
) {
1489 dirafter_includepath
++;
1490 /* If this was the entry that we prepend, don't
1491 * rise the lower entries, even if they are at
1492 * the same level. */
1493 if (where
== &dirafter_includepath
)
1496 if (*where
<= sys_includepath
) {
1498 if (where
== &sys_includepath
)
1501 if (*where
<= isys_includepath
) {
1503 if (where
== &isys_includepath
)
1507 /* angle_includepath is actually never updated, since we
1508 * don't suppport -iquote rught now. May change some day. */
1509 if (*where
<= angle_includepath
) {
1510 angle_includepath
++;
1511 if (where
== &angle_includepath
)
1516 /* Add a path before 'where' and update the pointers associated with the
1517 * includepath array */
1518 static void add_path_entry(struct token
*token
, const char *path
,
1519 const char ***where
)
1524 /* Need one free entry.. */
1525 if (includepath
[INCLUDEPATHS
-2])
1526 error_die(token
->pos
, "too many include path entries");
1528 /* check that this is not a duplicate */
1531 if (strcmp(*dst
, path
) == 0)
1538 update_inc_ptrs(where
);
1541 * Move them all up starting at dst,
1542 * insert the new entry..
1545 const char *tmp
= *dst
;
1552 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1555 token
= token
->next
;
1556 if (eof_token(token
))
1558 if (token_type(token
) != TOKEN_STRING
) {
1559 warning(token
->pos
, "expected path string");
1562 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1566 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1569 token
= token
->next
;
1570 if (eof_token(token
))
1572 if (token_type(token
) != TOKEN_STRING
) {
1573 sparse_error(token
->pos
, "expected path string");
1576 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1580 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
1583 token
= token
->next
;
1584 if (eof_token(token
))
1586 if (token_type(token
) != TOKEN_STRING
) {
1587 sparse_error(token
->pos
, "expected path string");
1590 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1594 /* Add to end on includepath list - no pointer updates */
1595 static void add_dirafter_entry(struct token
*token
, const char *path
)
1597 const char **dst
= includepath
;
1599 /* Need one free entry.. */
1600 if (includepath
[INCLUDEPATHS
-2])
1601 error_die(token
->pos
, "too many include path entries");
1603 /* Add to the end */
1611 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1614 token
= token
->next
;
1615 if (eof_token(token
))
1617 if (token_type(token
) != TOKEN_STRING
) {
1618 sparse_error(token
->pos
, "expected path string");
1621 add_dirafter_entry(token
, token
->string
->data
);
1625 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1630 * Split the include path. Any directories specified with `-I'
1631 * options before `-I-' are searched only for headers requested with
1632 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1633 * If additional directories are specified with `-I' options after
1634 * the `-I-', those directories are searched for all `#include'
1636 * In addition, `-I-' inhibits the use of the directory of the current
1637 * file directory as the first search directory for `#include "FILE"'.
1639 quote_includepath
= includepath
+1;
1640 angle_includepath
= sys_includepath
;
1645 * We replace "#pragma xxx" with "__pragma__" in the token
1646 * stream. Just as an example.
1648 * We'll just #define that away for now, but the theory here
1649 * is that we can use this to insert arbitrary token sequences
1650 * to turn the pragmas into internal front-end sequences for
1651 * when we actually start caring about them.
1653 * So eventually this will turn into some kind of extended
1654 * __attribute__() like thing, except called __pragma__(xxx).
1656 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1658 struct token
*next
= *line
;
1660 token
->ident
= &pragma_ident
;
1661 token
->pos
.newline
= 1;
1662 token
->pos
.whitespace
= 1;
1670 * We ignore #line for now.
1672 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1677 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1679 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1684 static void init_preprocessor(void)
1687 int stream
= init_stream("preprocessor", -1, includepath
);
1690 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1692 { "define", handle_define
},
1693 { "weak_define", handle_weak_define
},
1694 { "strong_define", handle_strong_define
},
1695 { "undef", handle_undef
},
1696 { "strong_undef", handle_strong_undef
},
1697 { "warning", handle_warning
},
1698 { "error", handle_error
},
1699 { "include", handle_include
},
1700 { "include_next", handle_include_next
},
1701 { "pragma", handle_pragma
},
1702 { "line", handle_line
},
1704 // our internal preprocessor tokens
1705 { "nostdinc", handle_nostdinc
},
1706 { "add_include", handle_add_include
},
1707 { "add_isystem", handle_add_isystem
},
1708 { "add_system", handle_add_system
},
1709 { "add_dirafter", handle_add_dirafter
},
1710 { "split_include", handle_split_include
},
1712 { "ifdef", handle_ifdef
},
1713 { "ifndef", handle_ifndef
},
1714 { "else", handle_else
},
1715 { "endif", handle_endif
},
1716 { "if", handle_if
},
1717 { "elif", handle_elif
},
1720 for (i
= 0; i
< (sizeof (normal
) / sizeof (normal
[0])); i
++) {
1722 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1723 sym
->handler
= normal
[i
].handler
;
1726 for (i
= 0; i
< (sizeof (special
) / sizeof (special
[0])); i
++) {
1728 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1729 sym
->handler
= special
[i
].handler
;
1735 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1737 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1738 struct token
*token
= start
->next
;
1741 if (eof_token(token
))
1744 if (token_type(token
) == TOKEN_IDENT
) {
1745 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1747 handler
= sym
->handler
;
1748 is_normal
= sym
->normal
;
1750 handler
= handle_nondirective
;
1752 } else if (token_type(token
) == TOKEN_NUMBER
) {
1753 handler
= handle_line
;
1755 handler
= handle_nondirective
;
1759 dirty_stream(stream
);
1763 if (!handler(stream
, line
, token
)) /* all set */
1767 free_preprocessor_line(token
);
1770 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1772 struct token
*start
= *line
, *next
;
1773 struct token
**tp
= &start
->next
;
1777 if (next
->pos
.newline
)
1782 *tp
= &eof_token_entry
;
1783 handle_preprocessor_line(stream
, line
, start
);
1786 static void do_preprocess(struct token
**list
)
1790 while (!eof_token(next
= scan_next(list
))) {
1791 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1793 if (next
->pos
.newline
&& match_op(next
, '#')) {
1794 if (!next
->pos
.noexpand
) {
1795 preprocessor_line(stream
, list
);
1796 __free_token(next
); /* Free the '#' token */
1801 switch (token_type(next
)) {
1802 case TOKEN_STREAMEND
:
1803 if (stream
->top_if
) {
1804 nesting_error(stream
);
1805 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1806 stream
->top_if
= NULL
;
1810 stream
->constant
= CONSTANT_FILE_YES
;
1813 case TOKEN_STREAMBEGIN
:
1818 dirty_stream(stream
);
1819 if (false_nesting
) {
1825 if (token_type(next
) != TOKEN_IDENT
||
1826 expand_one_symbol(list
))
1832 struct token
* preprocess(struct token
*token
)
1835 init_preprocessor();
1836 do_preprocess(&token
);
1838 // Drop all expressions from preprocessing, they're not used any more.
1839 // This is not true when we have multiple files, though ;/
1840 // clear_expression_alloc();