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
23 #include "pre-process.h"
29 #include "expression.h"
32 static int false_nesting
= 0;
34 #define INCLUDEPATHS 300
35 const char *includepath
[INCLUDEPATHS
+1] = {
43 static const char **quote_includepath
= includepath
;
44 static const char **angle_includepath
= includepath
+ 1;
45 static const char **sys_includepath
= includepath
+ 1;
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 struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
198 struct token
**p
= &prev
->next
;
202 while (!eof_token(next
= scan_next(p
))) {
203 if (match_op(next
, '(')) {
205 } else if (match_op(next
, ')')) {
208 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
211 next
->pos
.stream
= pos
->stream
;
212 next
->pos
.line
= pos
->line
;
213 next
->pos
.pos
= pos
->pos
;
216 *p
= &eof_token_entry
;
221 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
226 struct token
*expanded
;
233 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
235 int wanted
= arglist
->count
.normal
;
236 struct token
*next
= NULL
;
239 arglist
= arglist
->next
; /* skip counter */
242 next
= collect_arg(start
, 0, &what
->pos
);
245 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
250 for (count
= 0; count
< wanted
; count
++) {
251 struct argcount
*p
= &arglist
->next
->count
;
252 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
253 arglist
= arglist
->next
->next
;
256 args
[count
].arg
= start
->next
;
257 args
[count
].n_normal
= p
->normal
;
258 args
[count
].n_quoted
= p
->quoted
;
259 args
[count
].n_str
= p
->str
;
260 if (match_op(next
, ')')) {
266 if (count
== wanted
&& !match_op(next
, ')'))
268 if (count
== wanted
- 1) {
269 struct argcount
*p
= &arglist
->next
->count
;
272 args
[count
].arg
= NULL
;
273 args
[count
].n_normal
= p
->normal
;
274 args
[count
].n_quoted
= p
->quoted
;
275 args
[count
].n_str
= p
->str
;
277 if (count
< wanted
- 1)
280 what
->next
= next
->next
;
284 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
285 show_token(what
), wanted
, count
);
288 while (match_op(next
, ',')) {
289 next
= collect_arg(next
, 0, &what
->pos
);
294 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
295 show_token(what
), count
, wanted
);
298 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
301 what
->next
= next
->next
;
305 static struct token
*dup_list(struct token
*list
)
307 struct token
*res
= NULL
;
308 struct token
**p
= &res
;
310 while (!eof_token(list
)) {
311 struct token
*newtok
= __alloc_token(0);
320 static struct token
*stringify(struct token
*arg
)
322 const char *s
= show_token_sequence(arg
);
323 int size
= strlen(s
)+1;
324 struct token
*token
= __alloc_token(0);
325 struct string
*string
= __alloc_string(size
);
327 memcpy(string
->data
, s
, size
);
328 string
->length
= size
;
329 token
->pos
= arg
->pos
;
330 token_type(token
) = TOKEN_STRING
;
331 token
->string
= string
;
332 token
->next
= &eof_token_entry
;
336 static void expand_arguments(int count
, struct arg
*args
)
339 for (i
= 0; i
< count
; i
++) {
340 struct token
*arg
= args
[i
].arg
;
342 arg
= &eof_token_entry
;
344 args
[i
].str
= stringify(arg
);
345 if (args
[i
].n_normal
) {
346 if (!args
[i
].n_quoted
) {
347 args
[i
].expanded
= arg
;
349 } else if (eof_token(arg
)) {
350 args
[i
].expanded
= arg
;
352 args
[i
].expanded
= dup_list(arg
);
354 expand_list(&args
[i
].expanded
);
360 * Possibly valid combinations:
361 * - ident + ident -> ident
362 * - ident + number -> ident unless number contains '.', '+' or '-'.
363 * - number + number -> number
364 * - number + ident -> number
365 * - number + '.' -> number
366 * - number + '+' or '-' -> number, if number used to end on [eEpP].
367 * - '.' + number -> number, if number used to start with a digit.
368 * - special + special -> either special or an error.
370 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
373 enum token_type t1
= token_type(left
), t2
= token_type(right
);
375 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
378 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
381 strcpy(p
, show_token(left
));
382 strcat(p
, show_token(right
));
388 if (t1
== TOKEN_IDENT
) {
389 if (t2
== TOKEN_SPECIAL
)
391 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
396 if (t1
== TOKEN_NUMBER
) {
397 if (t2
== TOKEN_SPECIAL
) {
398 switch (right
->special
) {
402 if (strchr("eEpP", p
[len
- 2]))
411 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
414 return TOKEN_SPECIAL
;
417 static int merge(struct token
*left
, struct token
*right
)
419 static char buffer
[512];
422 switch (combine(left
, right
, buffer
)) {
424 left
->ident
= built_in_ident(buffer
);
425 left
->pos
.noexpand
= 0;
429 char *number
= __alloc_bytes(strlen(buffer
) + 1);
430 memcpy(number
, buffer
, strlen(buffer
) + 1);
431 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
432 left
->number
= number
;
437 if (buffer
[2] && buffer
[3])
439 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
440 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
448 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
452 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
454 struct token
*alloc
= alloc_token(streampos
);
455 token_type(alloc
) = token_type(token
);
456 alloc
->pos
.newline
= pos
->newline
;
457 alloc
->pos
.whitespace
= pos
->whitespace
;
458 alloc
->number
= token
->number
;
459 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
463 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
465 int need_copy
= --*count
;
466 while (!eof_token(list
)) {
469 token
= dup_token(list
, &list
->pos
, &list
->pos
);
472 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
473 token
->pos
.noexpand
= 1;
475 where
= &token
->next
;
478 *where
= &eof_token_entry
;
482 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
484 struct token
*token
= *list
;
485 struct position
*base_pos
= &token
->pos
;
486 struct position
*pos
= base_pos
;
488 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
490 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
491 struct token
*added
, *arg
;
494 switch (token_type(body
)) {
495 case TOKEN_GNU_KLUDGE
:
497 * GNU kludge: if we had <comma>##<vararg>, behaviour
498 * depends on whether we had enough arguments to have
499 * a vararg. If we did, ## is just ignored. Otherwise
500 * both , and ## are ignored. Comma should come from
501 * the body of macro and not be an argument of earlier
504 if (!args
[body
->next
->argnum
].arg
)
506 added
= dup_token(body
, base_pos
, pos
);
507 token_type(added
) = TOKEN_SPECIAL
;
511 case TOKEN_STR_ARGUMENT
:
512 arg
= args
[body
->argnum
].str
;
513 count
= &args
[body
->argnum
].n_str
;
516 case TOKEN_QUOTED_ARGUMENT
:
517 arg
= args
[body
->argnum
].arg
;
518 count
= &args
[body
->argnum
].n_quoted
;
519 if (!arg
|| eof_token(arg
)) {
528 case TOKEN_MACRO_ARGUMENT
:
529 arg
= args
[body
->argnum
].expanded
;
530 count
= &args
[body
->argnum
].n_normal
;
531 if (eof_token(arg
)) {
536 tail
= copy(&added
, arg
, count
);
537 added
->pos
.newline
= pos
->newline
;
538 added
->pos
.whitespace
= pos
->whitespace
;
542 if (state
== Placeholder
)
549 added
= dup_token(body
, base_pos
, pos
);
550 if (added
->ident
->tainted
)
551 added
->pos
.noexpand
= 1;
556 added
= dup_token(body
, base_pos
, pos
);
562 * if we got to doing real concatenation, we already have
563 * added something into the list, so containing_token() is OK.
565 if (state
== Concat
&& merge(containing_token(list
), added
)) {
567 if (tail
!= &added
->next
)
575 *list
= &eof_token_entry
;
579 static int expand(struct token
**list
, struct symbol
*sym
)
582 struct token
*token
= *list
;
583 struct ident
*expanding
= token
->ident
;
585 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
586 struct arg args
[nargs
];
588 if (expanding
->tainted
) {
589 token
->pos
.noexpand
= 1;
594 if (!match_op(scan_next(&token
->next
), '('))
596 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
598 expand_arguments(nargs
, args
);
601 expanding
->tainted
= 1;
604 tail
= substitute(list
, sym
->expansion
, args
);
610 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
613 static char buffer
[256];
617 while (!eof_token(token
) && !match_op(token
, endop
)) {
619 const char *val
= token
->string
->data
;
620 if (token_type(token
) != TOKEN_STRING
)
621 val
= show_token(token
);
623 memcpy(ptr
, val
, len
);
628 if (endop
&& !match_op(token
, endop
))
629 sparse_error(start
->pos
, "expected '>' at end of filename");
633 static int already_tokenized(const char *path
)
636 struct stream
*s
= input_streams
;
638 for (i
= input_stream_nr
; --i
>= 0; s
++) {
639 if (s
->constant
!= CONSTANT_FILE_YES
)
641 if (strcmp(path
, s
->name
))
643 if (s
->protect
&& !lookup_macro(s
->protect
))
650 /* Handle include of header files.
651 * The relevant options are made compatible with gcc. The only options that
652 * are not supported is -withprefix and friends.
654 * Three set of include paths are known:
655 * quote_includepath: Path to search when using #include "file.h"
656 * angle_includepath: Path to search when using #include <file.h>
657 * sys_includepath: Built-in include paths
659 * The above is implemented as one array with pointers
661 * quote_includepath ---> | |
665 * angle_includepath ---> | |
667 * sys_includepath ---> | |
672 * -I dir insert dir just before sys_includepath and move the rest
673 * -I- makes all dirs specified with -I before to quote dirs only and
674 * angle_includepath is set equal to sys_includepath.
675 * -nostdinc removes all sys dirs be storing NULL in entry pointed
676 * to by * sys_includepath. Note this will reset all dirs built-in and added
677 * before -nostdinc by -isystem and -dirafter
678 * -isystem dir adds dir where sys_includepath points adding this dir as
680 * -dirafter dir adds dir to the end of the list
683 static void set_stream_include_path(struct stream
*stream
)
685 const char *path
= stream
->path
;
687 const char *p
= strrchr(stream
->name
, '/');
690 int len
= p
- stream
->name
+ 1;
691 char *m
= malloc(len
+1);
692 /* This includes the final "/" */
693 memcpy(m
, stream
->name
, len
);
699 includepath
[0] = path
;
702 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
705 int plen
= strlen(path
);
706 static char fullname
[PATH_MAX
];
708 memcpy(fullname
, path
, plen
);
709 if (plen
&& path
[plen
-1] != '/') {
710 fullname
[plen
] = '/';
713 memcpy(fullname
+plen
, filename
, flen
);
714 if (already_tokenized(fullname
))
716 fd
= open(fullname
, O_RDONLY
);
718 char * streamname
= __alloc_bytes(plen
+ flen
);
719 memcpy(streamname
, fullname
, plen
+ flen
);
720 *where
= tokenize(streamname
, fd
, *where
, next_path
);
727 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
731 while ((path
= *pptr
++) != NULL
) {
732 if (!try_include(path
, filename
, flen
, list
, pptr
))
739 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
741 int flen
= strlen(filename
) + 1;
744 if (filename
[0] == '/') {
745 if (try_include("", filename
, flen
, list
, includepath
))
750 /* Dir of input file is first dir to search for quoted includes */
751 set_stream_include_path(stream
);
754 /* Do not search quote include if <> is in use */
755 path
= local
? quote_includepath
: angle_includepath
;
757 /* Check the standard include paths.. */
758 if (do_include_path(path
, list
, token
, filename
, flen
))
761 error_die(token
->pos
, "unable to open '%s'", filename
);
764 static int free_preprocessor_line(struct token
*token
)
766 while (token_type(token
) != TOKEN_EOF
) {
767 struct token
*free
= token
;
774 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
776 const char *filename
;
782 if (!match_op(next
, '<')) {
783 expand_list(&token
->next
);
786 if (match_op(token
->next
, '<')) {
792 filename
= token_name_sequence(token
, expect
, token
);
793 do_include(!expect
, stream
, list
, token
, filename
, path
);
797 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
799 return handle_include_path(stream
, list
, token
, NULL
);
802 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
804 return handle_include_path(stream
, list
, token
, stream
->next_path
);
807 static int token_different(struct token
*t1
, struct token
*t2
)
811 if (token_type(t1
) != token_type(t2
))
814 switch (token_type(t1
)) {
816 different
= t1
->ident
!= t2
->ident
;
818 case TOKEN_ARG_COUNT
:
821 case TOKEN_GNU_KLUDGE
:
825 different
= strcmp(t1
->number
, t2
->number
);
828 different
= t1
->special
!= t2
->special
;
830 case TOKEN_MACRO_ARGUMENT
:
831 case TOKEN_QUOTED_ARGUMENT
:
832 case TOKEN_STR_ARGUMENT
:
833 different
= t1
->argnum
!= t2
->argnum
;
836 different
= t1
->character
!= t2
->character
;
839 struct string
*s1
, *s2
;
844 if (s1
->length
!= s2
->length
)
846 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
856 static int token_list_different(struct token
*list1
, struct token
*list2
)
861 if (!list1
|| !list2
)
863 if (token_different(list1
, list2
))
870 static inline void set_arg_count(struct token
*token
)
872 token_type(token
) = TOKEN_ARG_COUNT
;
873 token
->count
.normal
= token
->count
.quoted
=
874 token
->count
.str
= token
->count
.vararg
= 0;
877 static struct token
*parse_arguments(struct token
*list
)
879 struct token
*arg
= list
->next
, *next
= list
;
880 struct argcount
*count
= &list
->count
;
884 if (match_op(arg
, ')')) {
886 list
->next
= &eof_token_entry
;
890 while (token_type(arg
) == TOKEN_IDENT
) {
891 if (arg
->ident
== &__VA_ARGS___ident
)
893 if (!++count
->normal
)
897 if (match_op(next
, ',')) {
903 if (match_op(next
, ')')) {
906 arg
->next
->next
= &eof_token_entry
;
910 /* normal cases are finished here */
912 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
913 if (match_op(next
->next
, ')')) {
915 next
->count
.vararg
= 1;
917 arg
->next
->next
= &eof_token_entry
;
925 if (eof_token(next
)) {
933 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
935 token_type(arg
) = TOKEN_IDENT
;
936 arg
->ident
= &__VA_ARGS___ident
;
937 if (!match_op(next
, ')'))
939 if (!++count
->normal
)
942 next
->count
.vararg
= 1;
944 arg
->next
->next
= &eof_token_entry
;
948 if (eof_token(arg
)) {
952 if (match_op(arg
, ','))
959 sparse_error(arg
->pos
, "parameter name missing");
962 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
966 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
969 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
972 sparse_error(arg
->pos
, "too many arguments in macro definition");
976 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
978 struct ident
*ident
= token
->ident
;
981 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
984 arglist
= arglist
->next
;
986 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
987 if (arglist
->ident
== ident
) {
988 struct argcount
*count
= &arglist
->next
->count
;
992 token_type(token
) = type
;
994 case TOKEN_MACRO_ARGUMENT
:
997 case TOKEN_QUOTED_ARGUMENT
:
1004 return count
->vararg
? 2 : 1;
1005 token_type(token
) = TOKEN_ERROR
;
1012 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1014 struct token
*token
= expansion
;
1016 struct token
*last
= NULL
;
1018 if (match_op(token
, SPECIAL_HASHHASH
))
1021 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1022 if (match_op(token
, '#')) {
1024 struct token
*next
= token
->next
;
1025 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1027 next
->pos
.whitespace
= token
->pos
.whitespace
;
1030 token
->pos
.noexpand
= 1;
1032 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
1033 struct token
*next
= token
->next
;
1034 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1035 token_type(token
) = TOKEN_CONCAT
;
1039 if (arg
== 2 && last
&& match_op(last
, ',')) {
1040 token_type(last
) = TOKEN_GNU_KLUDGE
;
1043 } else if (match_op(next
, SPECIAL_HASHHASH
))
1045 else if (eof_token(next
))
1047 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1048 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1050 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1052 if (token_type(token
) == TOKEN_ERROR
)
1056 token
= alloc_token(&expansion
->pos
);
1057 token_type(token
) = TOKEN_UNTAINT
;
1058 token
->ident
= name
;
1064 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1068 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1071 sparse_error(token
->pos
, "too many instances of argument in body");
1075 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1077 struct token
*arglist
, *expansion
;
1078 struct token
*left
= token
->next
;
1083 if (token_type(left
) != TOKEN_IDENT
) {
1084 sparse_error(token
->pos
, "expected identifier to 'define'");
1091 expansion
= left
->next
;
1092 if (!expansion
->pos
.whitespace
) {
1093 if (match_op(expansion
, '(')) {
1094 arglist
= expansion
;
1095 expansion
= parse_arguments(expansion
);
1098 } else if (!eof_token(expansion
)) {
1099 warning(expansion
->pos
,
1100 "no whitespace before object-like macro body");
1104 expansion
= parse_expansion(expansion
, arglist
, name
);
1109 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1113 if (attr
< sym
->attr
)
1116 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1118 if (token_list_different(sym
->expansion
, expansion
) ||
1119 token_list_different(sym
->arglist
, arglist
)) {
1121 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1122 || sym
->used_in
== file_scope
) {
1123 warning(left
->pos
, "preprocessor token %.*s redefined",
1124 name
->len
, name
->name
);
1125 info(sym
->pos
, "this was the original definition");
1131 if (!sym
|| sym
->scope
!= file_scope
) {
1132 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1133 bind_symbol(sym
, name
, NS_MACRO
);
1138 sym
->expansion
= expansion
;
1139 sym
->arglist
= arglist
;
1140 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1143 sym
->namespace = NS_MACRO
;
1144 sym
->used_in
= NULL
;
1150 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1152 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1155 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1157 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1160 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1162 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1165 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1167 struct token
*left
= token
->next
;
1170 if (token_type(left
) != TOKEN_IDENT
) {
1171 sparse_error(token
->pos
, "expected identifier to 'undef'");
1175 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1177 if (attr
< sym
->attr
)
1179 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1181 } else if (attr
<= SYM_ATTR_NORMAL
)
1184 if (!sym
|| sym
->scope
!= file_scope
) {
1185 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1186 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1189 sym
->namespace = NS_UNDEF
;
1190 sym
->used_in
= NULL
;
1196 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1198 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1201 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1203 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1206 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1208 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1209 free_preprocessor_line(token
->next
);
1210 token
->next
= stream
->top_if
;
1211 stream
->top_if
= token
;
1212 if (false_nesting
|| true != 1)
1217 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1219 struct token
*next
= token
->next
;
1221 if (token_type(next
) == TOKEN_IDENT
) {
1222 arg
= token_defined(next
);
1224 dirty_stream(stream
);
1226 sparse_error(token
->pos
, "expected preprocessor identifier");
1229 return preprocessor_if(stream
, token
, arg
);
1232 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1234 struct token
*next
= token
->next
;
1236 if (token_type(next
) == TOKEN_IDENT
) {
1237 if (!stream
->dirty
&& !stream
->ifndef
) {
1238 if (!stream
->protect
) {
1239 stream
->ifndef
= token
;
1240 stream
->protect
= next
->ident
;
1241 } else if (stream
->protect
== next
->ident
) {
1242 stream
->ifndef
= token
;
1246 arg
= !token_defined(next
);
1248 dirty_stream(stream
);
1250 sparse_error(token
->pos
, "expected preprocessor identifier");
1254 return preprocessor_if(stream
, token
, arg
);
1258 * Expression handling for #if and #elif; it differs from normal expansion
1259 * due to special treatment of "defined".
1261 static int expression_value(struct token
**where
)
1263 struct expression
*expr
;
1265 struct token
**list
= where
, **beginning
= NULL
;
1269 while (!eof_token(p
= scan_next(list
))) {
1272 if (token_type(p
) != TOKEN_IDENT
)
1274 if (p
->ident
== &defined_ident
) {
1279 if (!expand_one_symbol(list
))
1281 if (token_type(p
) != TOKEN_IDENT
)
1283 token_type(p
) = TOKEN_ZERO_IDENT
;
1286 if (match_op(p
, '(')) {
1290 replace_with_defined(p
);
1295 if (token_type(p
) == TOKEN_IDENT
)
1299 replace_with_defined(p
);
1304 if (!match_op(p
, ')'))
1305 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1312 p
= constant_expression(*where
, &expr
);
1314 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1315 value
= get_expression_value(expr
);
1319 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1323 value
= expression_value(&token
->next
);
1325 dirty_stream(stream
);
1326 return preprocessor_if(stream
, token
, value
);
1329 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1331 struct token
*top_if
= stream
->top_if
;
1335 nesting_error(stream
);
1336 sparse_error(token
->pos
, "unmatched #elif within stream");
1340 if (token_type(top_if
) == TOKEN_ELSE
) {
1341 nesting_error(stream
);
1342 sparse_error(token
->pos
, "#elif after #else");
1348 dirty_stream(stream
);
1349 if (token_type(top_if
) != TOKEN_IF
)
1351 if (false_nesting
) {
1352 if (expression_value(&token
->next
))
1356 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1361 static int handle_else(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 #else within stream");
1372 if (token_type(top_if
) == TOKEN_ELSE
) {
1373 nesting_error(stream
);
1374 sparse_error(token
->pos
, "#else after #else");
1376 if (false_nesting
) {
1377 if (token_type(top_if
) == TOKEN_IF
)
1382 token_type(top_if
) = TOKEN_ELSE
;
1386 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1388 struct token
*top_if
= stream
->top_if
;
1391 nesting_error(stream
);
1392 sparse_error(token
->pos
, "unmatched #endif in stream");
1397 stream
->top_if
= top_if
->next
;
1398 __free_token(top_if
);
1402 static const char *show_token_sequence(struct token
*token
)
1404 static char buffer
[1024];
1410 while (!eof_token(token
)) {
1411 const char *val
= show_token(token
);
1412 int len
= strlen(val
);
1414 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1415 sparse_error(token
->pos
, "too long token expansion");
1421 memcpy(ptr
, val
, len
);
1423 token
= token
->next
;
1424 whitespace
= token
->pos
.whitespace
;
1430 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1432 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1436 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1438 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
));
1442 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1445 * Do we have any non-system includes?
1446 * Clear them out if so..
1448 *sys_includepath
= NULL
;
1452 static void add_path_entry(struct token
*token
, const char *path
, const char ***where
, const char **new_path
)
1457 /* Need one free entry.. */
1458 if (includepath
[INCLUDEPATHS
-2])
1459 error_die(token
->pos
, "too many include path entries");
1461 /* check that this is not a duplicate */
1464 if (strcmp(*dst
, path
) == 0)
1473 * Move them all up starting at dst,
1474 * insert the new entry..
1477 const char *tmp
= *dst
;
1486 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1489 token
= token
->next
;
1490 if (eof_token(token
))
1492 if (token_type(token
) != TOKEN_STRING
) {
1493 warning(token
->pos
, "expected path string");
1496 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
+ 1);
1500 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1503 token
= token
->next
;
1504 if (eof_token(token
))
1506 if (token_type(token
) != TOKEN_STRING
) {
1507 sparse_error(token
->pos
, "expected path string");
1510 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
);
1514 /* Add to end on includepath list - no pointer updates */
1515 static void add_dirafter_entry(struct token
*token
, const char *path
)
1517 const char **dst
= includepath
;
1519 /* Need one free entry.. */
1520 if (includepath
[INCLUDEPATHS
-2])
1521 error_die(token
->pos
, "too many include path entries");
1523 /* Add to the end */
1531 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1534 token
= token
->next
;
1535 if (eof_token(token
))
1537 if (token_type(token
) != TOKEN_STRING
) {
1538 sparse_error(token
->pos
, "expected path string");
1541 add_dirafter_entry(token
, token
->string
->data
);
1545 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1550 * Split the include path. Any directories specified with `-I'
1551 * options before `-I-' are searched only for headers requested with
1552 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1553 * If additional directories are specified with `-I' options after
1554 * the `-I-', those directories are searched for all `#include'
1556 * In addition, `-I-' inhibits the use of the directory of the current
1557 * file directory as the first search directory for `#include "FILE"'.
1559 quote_includepath
= includepath
+1;
1560 angle_includepath
= sys_includepath
;
1565 * We replace "#pragma xxx" with "__pragma__" in the token
1566 * stream. Just as an example.
1568 * We'll just #define that away for now, but the theory here
1569 * is that we can use this to insert arbitrary token sequences
1570 * to turn the pragmas into internal front-end sequences for
1571 * when we actually start caring about them.
1573 * So eventually this will turn into some kind of extended
1574 * __attribute__() like thing, except called __pragma__(xxx).
1576 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1578 struct token
*next
= *line
;
1580 token
->ident
= &pragma_ident
;
1581 token
->pos
.newline
= 1;
1582 token
->pos
.whitespace
= 1;
1590 * We ignore #line for now.
1592 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1597 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1599 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1604 static void init_preprocessor(void)
1607 int stream
= init_stream("preprocessor", -1, includepath
);
1610 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1612 { "define", handle_define
},
1613 { "weak_define", handle_weak_define
},
1614 { "strong_define", handle_strong_define
},
1615 { "undef", handle_undef
},
1616 { "strong_undef", handle_strong_undef
},
1617 { "warning", handle_warning
},
1618 { "error", handle_error
},
1619 { "include", handle_include
},
1620 { "include_next", handle_include_next
},
1621 { "pragma", handle_pragma
},
1622 { "line", handle_line
},
1624 // our internal preprocessor tokens
1625 { "nostdinc", handle_nostdinc
},
1626 { "add_include", handle_add_include
},
1627 { "add_isystem", handle_add_isystem
},
1628 { "add_dirafter", handle_add_dirafter
},
1629 { "split_include", handle_split_include
},
1631 { "ifdef", handle_ifdef
},
1632 { "ifndef", handle_ifndef
},
1633 { "else", handle_else
},
1634 { "endif", handle_endif
},
1635 { "if", handle_if
},
1636 { "elif", handle_elif
},
1639 for (i
= 0; i
< (sizeof (normal
) / sizeof (normal
[0])); i
++) {
1641 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1642 sym
->handler
= normal
[i
].handler
;
1645 for (i
= 0; i
< (sizeof (special
) / sizeof (special
[0])); i
++) {
1647 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1648 sym
->handler
= special
[i
].handler
;
1654 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1656 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1657 struct token
*token
= start
->next
;
1660 if (eof_token(token
))
1663 if (token_type(token
) == TOKEN_IDENT
) {
1664 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1666 handler
= sym
->handler
;
1667 is_normal
= sym
->normal
;
1669 handler
= handle_nondirective
;
1671 } else if (token_type(token
) == TOKEN_NUMBER
) {
1672 handler
= handle_line
;
1674 handler
= handle_nondirective
;
1678 dirty_stream(stream
);
1682 if (!handler(stream
, line
, token
)) /* all set */
1686 free_preprocessor_line(token
);
1689 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1691 struct token
*start
= *line
, *next
;
1692 struct token
**tp
= &start
->next
;
1696 if (next
->pos
.newline
)
1701 *tp
= &eof_token_entry
;
1702 handle_preprocessor_line(stream
, line
, start
);
1705 static void do_preprocess(struct token
**list
)
1709 while (!eof_token(next
= scan_next(list
))) {
1710 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1712 if (next
->pos
.newline
&& match_op(next
, '#')) {
1713 if (!next
->pos
.noexpand
) {
1714 preprocessor_line(stream
, list
);
1715 __free_token(next
); /* Free the '#' token */
1720 switch (token_type(next
)) {
1721 case TOKEN_STREAMEND
:
1722 if (stream
->top_if
) {
1723 nesting_error(stream
);
1724 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1725 stream
->top_if
= NULL
;
1729 stream
->constant
= CONSTANT_FILE_YES
;
1732 case TOKEN_STREAMBEGIN
:
1737 dirty_stream(stream
);
1738 if (false_nesting
) {
1744 if (token_type(next
) != TOKEN_IDENT
||
1745 expand_one_symbol(list
))
1751 struct token
* preprocess(struct token
*token
)
1754 init_preprocessor();
1755 do_preprocess(&token
);
1757 // Drop all expressions from preprocessing, they're not used any more.
1758 // This is not true when we have multiple files, though ;/
1759 // clear_expression_alloc();