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
22 #include "pre-process.h"
28 #include "expression.h"
31 static int false_nesting
= 0;
32 static int if_nesting
= 0;
33 static struct position unmatched_if_pos
;
35 #define MAX_NEST (256)
36 static unsigned char elif_ignore
[MAX_NEST
];
37 enum { ELIF_IGNORE
= 1, ELIF_SEEN_ELSE
= 2 };
39 #define INCLUDEPATHS 300
40 const char *includepath
[INCLUDEPATHS
+1] = {
48 static const char **quote_includepath
= includepath
;
49 static const char **angle_includepath
= includepath
+ 1;
50 static const char **sys_includepath
= includepath
+ 1;
52 #define dirty_stream(stream) \
53 do if (!stream->dirty) { \
55 if (!stream->ifndef) \
56 stream->protect = NULL; \
59 #define end_group(stream) \
60 do if (stream->ifndef == if_nesting) { \
63 stream->protect = NULL; \
64 else if (stream->protect) \
68 #define nesting_error(stream) \
72 stream->protect = NULL; \
75 static struct token
*alloc_token(struct position
*pos
)
77 struct token
*token
= __alloc_token(0);
79 token
->pos
.stream
= pos
->stream
;
80 token
->pos
.line
= pos
->line
;
81 token
->pos
.pos
= pos
->pos
;
82 token
->pos
.whitespace
= 1;
86 static const char *show_token_sequence(struct token
*token
);
88 /* Expand symbol 'sym' at '*list' */
89 static int expand(struct token
**, struct symbol
*);
91 static void replace_with_string(struct token
*token
, const char *str
)
93 int size
= strlen(str
) + 1;
94 struct string
*s
= __alloc_string(size
);
97 memcpy(s
->data
, str
, size
);
98 token_type(token
) = TOKEN_STRING
;
102 static void replace_with_integer(struct token
*token
, unsigned int val
)
104 char *buf
= __alloc_bytes(11);
105 sprintf(buf
, "%u", val
);
106 token_type(token
) = TOKEN_NUMBER
;
110 struct symbol
*lookup_macro(struct ident
*ident
)
112 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_INVISIBLEMACRO
);
113 if (sym
&& sym
->namespace != NS_MACRO
)
118 static int token_defined(struct token
*token
)
120 if (token_type(token
) == TOKEN_IDENT
) {
121 struct symbol
*sym
= lookup_macro(token
->ident
);
129 sparse_error(token
->pos
, "expected preprocessor identifier");
133 static void replace_with_defined(struct token
*token
)
135 static const char *string
[] = { "0", "1" };
136 int defined
= token_defined(token
);
138 token_type(token
) = TOKEN_NUMBER
;
139 token
->number
= string
[defined
];
142 static int expand_one_symbol(struct token
**list
)
144 struct token
*token
= *list
;
147 if (token
->pos
.noexpand
)
150 sym
= lookup_macro(token
->ident
);
153 return expand(list
, sym
);
155 if (token
->ident
== &__LINE___ident
) {
156 replace_with_integer(token
, token
->pos
.line
);
157 } else if (token
->ident
== &__FILE___ident
) {
158 replace_with_string(token
, stream_name(token
->pos
.stream
));
163 static inline struct token
*scan_next(struct token
**where
)
165 struct token
*token
= *where
;
166 if (token_type(token
) != TOKEN_UNTAINT
)
169 token
->ident
->tainted
= 0;
171 } while (token_type(token
) == TOKEN_UNTAINT
);
176 static void expand_list(struct token
**list
)
179 while (!eof_token(next
= scan_next(list
))) {
180 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
185 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
187 struct token
**p
= &prev
->next
;
191 while (!eof_token(next
= scan_next(p
))) {
192 if (match_op(next
, '(')) {
194 } else if (match_op(next
, ')')) {
197 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
200 next
->pos
.stream
= pos
->stream
;
201 next
->pos
.line
= pos
->line
;
202 next
->pos
.pos
= pos
->pos
;
205 *p
= &eof_token_entry
;
210 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
215 struct token
*expanded
;
222 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
224 int wanted
= arglist
->count
.normal
;
225 struct token
*next
= NULL
;
228 arglist
= arglist
->next
; /* skip counter */
231 next
= collect_arg(start
, 0, &what
->pos
);
234 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
239 for (count
= 0; count
< wanted
; count
++) {
240 struct argcount
*p
= &arglist
->next
->count
;
241 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
242 arglist
= arglist
->next
->next
;
245 args
[count
].arg
= start
->next
;
246 args
[count
].n_normal
= p
->normal
;
247 args
[count
].n_quoted
= p
->quoted
;
248 args
[count
].n_str
= p
->str
;
249 if (match_op(next
, ')')) {
255 if (count
== wanted
&& !match_op(next
, ')'))
257 if (count
== wanted
- 1) {
258 struct argcount
*p
= &arglist
->next
->count
;
261 args
[count
].arg
= NULL
;
262 args
[count
].n_normal
= p
->normal
;
263 args
[count
].n_quoted
= p
->quoted
;
264 args
[count
].n_str
= p
->str
;
266 if (count
< wanted
- 1)
269 what
->next
= next
->next
;
273 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
274 show_token(what
), wanted
, count
);
277 while (match_op(next
, ',')) {
278 next
= collect_arg(next
, 0, &what
->pos
);
283 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
284 show_token(what
), count
, wanted
);
287 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
290 what
->next
= next
->next
;
294 static struct token
*dup_list(struct token
*list
)
297 struct token
**p
= &res
;
299 while (!eof_token(list
)) {
300 struct token
*newtok
= __alloc_token(0);
309 static struct token
*stringify(struct token
*arg
)
311 const char *s
= show_token_sequence(arg
);
312 int size
= strlen(s
)+1;
313 struct token
*token
= __alloc_token(0);
314 struct string
*string
= __alloc_string(size
);
316 memcpy(string
->data
, s
, size
);
317 string
->length
= size
;
318 token
->pos
= arg
->pos
;
319 token_type(token
) = TOKEN_STRING
;
320 token
->string
= string
;
321 token
->next
= &eof_token_entry
;
325 static void expand_arguments(int count
, struct arg
*args
)
328 for (i
= 0; i
< count
; i
++) {
329 struct token
*arg
= args
[i
].arg
;
331 arg
= &eof_token_entry
;
333 args
[i
].str
= stringify(arg
);
334 if (args
[i
].n_normal
) {
335 if (!args
[i
].n_quoted
) {
336 args
[i
].expanded
= arg
;
338 } else if (eof_token(arg
)) {
339 args
[i
].expanded
= arg
;
341 args
[i
].expanded
= dup_list(arg
);
343 expand_list(&args
[i
].expanded
);
349 * Possibly valid combinations:
350 * - ident + ident -> ident
351 * - ident + number -> ident unless number contains '.', '+' or '-'.
352 * - number + number -> number
353 * - number + ident -> number
354 * - number + '.' -> number
355 * - number + '+' or '-' -> number, if number used to end on [eEpP].
356 * - '.' + number -> number, if number used to start with a digit.
357 * - special + special -> either special or an error.
359 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
362 enum token_type t1
= token_type(left
), t2
= token_type(right
);
364 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
367 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
370 strcpy(p
, show_token(left
));
371 strcat(p
, show_token(right
));
377 if (t1
== TOKEN_IDENT
) {
378 if (t2
== TOKEN_SPECIAL
)
380 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
385 if (t1
== TOKEN_NUMBER
) {
386 if (t2
== TOKEN_SPECIAL
) {
387 switch (right
->special
) {
391 if (strchr("eEpP", p
[len
- 2]))
400 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
403 return TOKEN_SPECIAL
;
406 static int merge(struct token
*left
, struct token
*right
)
408 static char buffer
[512];
411 switch (combine(left
, right
, buffer
)) {
413 left
->ident
= built_in_ident(buffer
);
414 left
->pos
.noexpand
= 0;
418 char *number
= __alloc_bytes(strlen(buffer
) + 1);
419 memcpy(number
, buffer
, strlen(buffer
) + 1);
420 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
421 left
->number
= number
;
426 if (buffer
[2] && buffer
[3])
428 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
429 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
437 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
441 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
443 struct token
*alloc
= alloc_token(streampos
);
444 token_type(alloc
) = token_type(token
);
445 alloc
->pos
.newline
= pos
->newline
;
446 alloc
->pos
.whitespace
= pos
->whitespace
;
447 alloc
->number
= token
->number
;
448 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
452 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
454 int need_copy
= --*count
;
455 while (!eof_token(list
)) {
458 token
= dup_token(list
, &list
->pos
, &list
->pos
);
461 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
462 token
->pos
.noexpand
= 1;
464 where
= &token
->next
;
467 *where
= &eof_token_entry
;
471 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
473 struct token
*token
= *list
;
474 struct position
*base_pos
= &token
->pos
;
475 struct position
*pos
= base_pos
;
477 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
479 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
480 struct token
*added
, *arg
;
483 switch (token_type(body
)) {
484 case TOKEN_GNU_KLUDGE
:
486 * GNU kludge: if we had <comma>##<vararg>, behaviour
487 * depends on whether we had enough arguments to have
488 * a vararg. If we did, ## is just ignored. Otherwise
489 * both , and ## are ignored. Comma should come from
490 * the body of macro and not be an argument of earlier
493 if (!args
[body
->next
->argnum
].arg
)
495 added
= dup_token(body
, base_pos
, pos
);
496 token_type(added
) = TOKEN_SPECIAL
;
500 case TOKEN_STR_ARGUMENT
:
501 arg
= args
[body
->argnum
].str
;
502 count
= &args
[body
->argnum
].n_str
;
505 case TOKEN_QUOTED_ARGUMENT
:
506 arg
= args
[body
->argnum
].arg
;
507 count
= &args
[body
->argnum
].n_quoted
;
508 if (!arg
|| eof_token(arg
)) {
517 case TOKEN_MACRO_ARGUMENT
:
518 arg
= args
[body
->argnum
].expanded
;
519 count
= &args
[body
->argnum
].n_normal
;
520 if (eof_token(arg
)) {
525 tail
= copy(&added
, arg
, count
);
526 added
->pos
.newline
= pos
->newline
;
527 added
->pos
.whitespace
= pos
->whitespace
;
531 if (state
== Placeholder
)
538 added
= dup_token(body
, base_pos
, pos
);
539 if (added
->ident
->tainted
)
540 added
->pos
.noexpand
= 1;
545 added
= dup_token(body
, base_pos
, pos
);
551 * if we got to doing real concatenation, we already have
552 * added something into the list, so containing_token() is OK.
554 if (state
== Concat
&& merge(containing_token(list
), added
)) {
556 if (tail
!= &added
->next
)
564 *list
= &eof_token_entry
;
568 static int expand(struct token
**list
, struct symbol
*sym
)
571 struct token
*token
= *list
;
572 struct ident
*expanding
= token
->ident
;
574 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
575 struct arg args
[nargs
];
577 if (expanding
->tainted
) {
578 token
->pos
.noexpand
= 1;
583 if (!match_op(scan_next(&token
->next
), '('))
585 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
587 expand_arguments(nargs
, args
);
590 expanding
->tainted
= 1;
593 tail
= substitute(list
, sym
->expansion
, args
);
599 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
602 static char buffer
[256];
606 while (!eof_token(token
) && !match_op(token
, endop
)) {
608 const char *val
= token
->string
->data
;
609 if (token_type(token
) != TOKEN_STRING
)
610 val
= show_token(token
);
612 memcpy(ptr
, val
, len
);
617 if (endop
&& !match_op(token
, endop
))
618 sparse_error(start
->pos
, "expected '>' at end of filename");
622 static int already_tokenized(const char *path
)
625 struct stream
*s
= input_streams
;
627 for (i
= input_stream_nr
; --i
>= 0; s
++) {
628 if (s
->constant
!= CONSTANT_FILE_YES
)
630 if (strcmp(path
, s
->name
))
632 if (s
->protect
&& !lookup_macro(s
->protect
))
639 /* Hande include of header files.
640 * The relevant options are made compatible with gcc. The only options that
641 * are not supported is -withprefix and friends.
643 * Three set of include paths are known:
644 * quote_includepath: Path to search when using #include "file.h"
645 * angle_includepath: Path to search when using #include <file.h>
646 * sys_includepath: Built-in include paths
648 * The above is implmented as one array with pointes
650 * quote_includepath ---> | |
654 * angle_includepath ---> | |
656 * sys_includepath ---> | |
661 * -I dir insert dir just before sys_includepath and move the rest
662 * -I- makes all dirs specified with -I before to quote dirs only and
663 * angle_includepath is set equal to sys_includepath.
664 * -nostdinc removes all sys dirs be storing NULL in entry pointed
665 * to by * sys_includepath. Note this will reset all dirs built-in and added
666 * before -nostdinc by -isystem and -dirafter
667 * -isystem dir adds dir where sys_includepath points adding this dir as
669 * -dirafter dir adds dir to the end of the list
672 static void set_stream_include_path(struct stream
*stream
)
674 const char *path
= stream
->path
;
676 const char *p
= strrchr(stream
->name
, '/');
679 int len
= p
- stream
->name
+ 1;
680 char *m
= malloc(len
+1);
681 /* This includes the final "/" */
682 memcpy(m
, stream
->name
, len
);
688 includepath
[0] = path
;
691 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
694 int plen
= strlen(path
);
695 static char fullname
[PATH_MAX
];
697 memcpy(fullname
, path
, plen
);
698 if (plen
&& path
[plen
-1] != '/') {
699 fullname
[plen
] = '/';
702 memcpy(fullname
+plen
, filename
, flen
);
703 if (already_tokenized(fullname
))
705 fd
= open(fullname
, O_RDONLY
);
707 char * streamname
= __alloc_bytes(plen
+ flen
);
708 memcpy(streamname
, fullname
, plen
+ flen
);
709 *where
= tokenize(streamname
, fd
, *where
, next_path
);
716 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
720 while ((path
= *pptr
++) != NULL
) {
721 if (!try_include(path
, filename
, flen
, list
, pptr
))
728 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
730 int flen
= strlen(filename
) + 1;
733 if (filename
[0] == '/') {
734 if (try_include("", filename
, flen
, list
, includepath
))
739 /* Dir of inputfile is first dir to search for quoted includes */
740 set_stream_include_path(stream
);
743 /* Do not search quote include if <> is in use */
744 path
= local
? quote_includepath
: angle_includepath
;
746 /* Check the standard include paths.. */
747 if (do_include_path(path
, list
, token
, filename
, flen
))
750 error_die(token
->pos
, "unable to open '%s'", filename
);
753 static int free_preprocessor_line(struct token
*token
)
756 struct token
*free
= token
;
759 } while (token_type(token
) != TOKEN_EOF
);
763 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
765 const char *filename
;
771 if (!match_op(next
, '<')) {
772 expand_list(&token
->next
);
775 if (match_op(token
->next
, '<')) {
781 filename
= token_name_sequence(token
, expect
, token
);
782 do_include(!expect
, stream
, list
, token
, filename
, path
);
786 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
788 return handle_include_path(stream
, list
, token
, NULL
);
791 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
793 return handle_include_path(stream
, list
, token
, stream
->next_path
);
796 static int token_different(struct token
*t1
, struct token
*t2
)
800 if (token_type(t1
) != token_type(t2
))
803 switch (token_type(t1
)) {
805 different
= t1
->ident
!= t2
->ident
;
807 case TOKEN_ARG_COUNT
:
810 case TOKEN_GNU_KLUDGE
:
814 different
= strcmp(t1
->number
, t2
->number
);
817 different
= t1
->special
!= t2
->special
;
819 case TOKEN_MACRO_ARGUMENT
:
820 case TOKEN_QUOTED_ARGUMENT
:
821 case TOKEN_STR_ARGUMENT
:
822 different
= t1
->argnum
!= t2
->argnum
;
825 different
= t1
->character
!= t2
->character
;
828 struct string
*s1
, *s2
;
833 if (s1
->length
!= s2
->length
)
835 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
845 static int token_list_different(struct token
*list1
, struct token
*list2
)
850 if (!list1
|| !list2
)
852 if (token_different(list1
, list2
))
859 static inline void set_arg_count(struct token
*token
)
861 token_type(token
) = TOKEN_ARG_COUNT
;
862 token
->count
.normal
= token
->count
.quoted
=
863 token
->count
.str
= token
->count
.vararg
= 0;
866 static struct token
*parse_arguments(struct token
*list
)
868 struct token
*arg
= list
->next
, *next
= list
;
869 struct argcount
*count
= &list
->count
;
873 if (match_op(arg
, ')')) {
875 list
->next
= &eof_token_entry
;
879 while (token_type(arg
) == TOKEN_IDENT
) {
880 if (arg
->ident
== &__VA_ARGS___ident
)
882 if (!++count
->normal
)
886 if (match_op(next
, ',')) {
892 if (match_op(next
, ')')) {
895 arg
->next
->next
= &eof_token_entry
;
899 /* normal cases are finished here */
901 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
902 if (match_op(next
->next
, ')')) {
904 next
->count
.vararg
= 1;
906 arg
->next
->next
= &eof_token_entry
;
914 if (eof_token(next
)) {
922 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
924 token_type(arg
) = TOKEN_IDENT
;
925 arg
->ident
= &__VA_ARGS___ident
;
926 if (!match_op(next
, ')'))
928 if (!++count
->normal
)
931 next
->count
.vararg
= 1;
933 arg
->next
->next
= &eof_token_entry
;
937 if (eof_token(arg
)) {
941 if (match_op(arg
, ','))
948 sparse_error(arg
->pos
, "parameter name missing");
951 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
955 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
958 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
961 sparse_error(arg
->pos
, "too many arguments in macro definition");
965 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
967 struct ident
*ident
= token
->ident
;
970 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
973 arglist
= arglist
->next
;
975 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
976 if (arglist
->ident
== ident
) {
977 struct argcount
*count
= &arglist
->next
->count
;
981 token_type(token
) = type
;
983 case TOKEN_MACRO_ARGUMENT
:
986 case TOKEN_QUOTED_ARGUMENT
:
993 return count
->vararg
? 2 : 1;
994 token_type(token
) = TOKEN_ERROR
;
1001 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1003 struct token
*token
= expansion
;
1005 struct token
*last
= NULL
;
1007 if (match_op(token
, SPECIAL_HASHHASH
))
1010 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1011 if (match_op(token
, '#')) {
1013 struct token
*next
= token
->next
;
1014 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1016 next
->pos
.whitespace
= token
->pos
.whitespace
;
1019 token
->pos
.noexpand
= 1;
1021 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
1022 struct token
*next
= token
->next
;
1023 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1024 token_type(token
) = TOKEN_CONCAT
;
1028 if (arg
== 2 && last
&& match_op(last
, ',')) {
1029 token_type(last
) = TOKEN_GNU_KLUDGE
;
1032 } else if (match_op(next
, SPECIAL_HASHHASH
))
1034 else if (eof_token(next
))
1036 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1037 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1039 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1041 if (token_type(token
) == TOKEN_ERROR
)
1045 token
= alloc_token(&expansion
->pos
);
1046 token_type(token
) = TOKEN_UNTAINT
;
1047 token
->ident
= name
;
1053 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1057 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1060 sparse_error(token
->pos
, "too many instances of argument in body");
1064 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int weak
)
1066 struct token
*arglist
, *expansion
;
1067 struct token
*left
= token
->next
;
1071 if (token_type(left
) != TOKEN_IDENT
) {
1072 sparse_error(token
->pos
, "expected identifier to 'define'");
1079 expansion
= left
->next
;
1080 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
1081 arglist
= expansion
;
1082 expansion
= parse_arguments(expansion
);
1087 expansion
= parse_expansion(expansion
, arglist
, name
);
1091 sym
= lookup_macro(name
);
1093 if (token_list_different(sym
->expansion
, expansion
) ||
1094 token_list_different(sym
->arglist
, arglist
)) {
1099 warning(left
->pos
, "preprocessor token %.*s redefined",
1100 name
->len
, name
->name
);
1101 info(sym
->pos
, "this was the original definition");
1103 /* Don't overwrite global defs */
1104 if (sym
->scope
!= file_scope
)
1111 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1112 bind_symbol(sym
, name
, NS_MACRO
);
1115 sym
->expansion
= expansion
;
1116 sym
->arglist
= arglist
;
1118 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1122 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1124 return do_handle_define(stream
, line
, token
, 0);
1127 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1129 return do_handle_define(stream
, line
, token
, 1);
1132 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1134 struct token
*left
= token
->next
;
1135 struct symbol
**sym
;
1137 if (token_type(left
) != TOKEN_IDENT
) {
1138 sparse_error(token
->pos
, "expected identifier to 'undef'");
1142 sym
= &left
->ident
->symbols
;
1144 struct symbol
*t
= *sym
;
1145 if (t
->namespace & (NS_MACRO
| NS_INVISIBLEMACRO
)) {
1146 t
->namespace = NS_INVISIBLEMACRO
;
1154 static int preprocessor_if(struct token
*token
, int true)
1156 if (if_nesting
== 0)
1157 unmatched_if_pos
= token
->pos
;
1158 if (if_nesting
>= MAX_NEST
)
1159 error_die(token
->pos
, "Maximum preprocessor conditional level exhausted");
1160 elif_ignore
[if_nesting
++] = (false_nesting
|| true) ? ELIF_IGNORE
: 0;
1161 if (false_nesting
|| true != 1)
1166 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1168 struct token
*next
= token
->next
;
1170 if (token_type(next
) == TOKEN_IDENT
) {
1171 arg
= token_defined(next
);
1173 dirty_stream(stream
);
1175 sparse_error(token
->pos
, "expected preprocessor identifier");
1178 return preprocessor_if(token
, arg
);
1181 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1183 struct token
*next
= token
->next
;
1185 if (token_type(next
) == TOKEN_IDENT
) {
1186 if (!stream
->dirty
&& !stream
->ifndef
) {
1187 if (!stream
->protect
) {
1188 stream
->ifndef
= if_nesting
+ 1;
1189 stream
->protect
= next
->ident
;
1190 } else if (stream
->protect
== next
->ident
) {
1191 stream
->ifndef
= if_nesting
+ 1;
1195 arg
= !token_defined(next
);
1197 dirty_stream(stream
);
1199 sparse_error(token
->pos
, "expected preprocessor identifier");
1203 return preprocessor_if(token
, arg
);
1207 * Expression handling for #if and #elif; it differs from normal expansion
1208 * due to special treatment of "defined".
1210 static int expression_value(struct token
**where
)
1212 struct expression
*expr
;
1214 struct token
**list
= where
, **beginning
= NULL
;
1218 while (!eof_token(p
= scan_next(list
))) {
1221 if (token_type(p
) != TOKEN_IDENT
)
1223 if (p
->ident
== &defined_ident
) {
1228 if (!expand_one_symbol(list
))
1230 if (token_type(p
) != TOKEN_IDENT
)
1232 token_type(p
) = TOKEN_ZERO_IDENT
;
1235 if (match_op(p
, '(')) {
1239 replace_with_defined(p
);
1244 if (token_type(p
) == TOKEN_IDENT
)
1248 replace_with_defined(p
);
1253 if (!match_op(p
, ')'))
1254 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1261 p
= constant_expression(*where
, &expr
);
1263 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1264 value
= get_expression_value(expr
);
1268 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1272 value
= expression_value(&token
->next
);
1274 dirty_stream(stream
);
1275 return preprocessor_if(token
, value
);
1278 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1282 if (stream
->nesting
> if_nesting
) {
1283 nesting_error(stream
);
1284 sparse_error(token
->pos
, "unmatched #elif within stream");
1288 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
) {
1289 nesting_error(stream
);
1290 sparse_error(token
->pos
, "#elif after #else");
1294 dirty_stream(stream
);
1295 if (false_nesting
) {
1296 /* If this whole if-thing is if'ed out, an elif cannot help */
1297 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1299 if (expression_value(&token
->next
)) {
1301 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1309 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1313 if (stream
->nesting
> if_nesting
) {
1314 nesting_error(stream
);
1315 sparse_error(token
->pos
, "unmatched #else within stream");
1319 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
) {
1320 nesting_error(stream
);
1321 sparse_error(token
->pos
, "#else after #else");
1325 elif_ignore
[if_nesting
-1] |= ELIF_SEEN_ELSE
;
1327 if (false_nesting
) {
1328 /* If this whole if-thing is if'ed out, an else cannot help */
1329 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1332 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1339 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1342 if (stream
->nesting
> if_nesting
) {
1343 nesting_error(stream
);
1344 sparse_error(token
->pos
, "unmatched #endif in stream");
1353 static const char *show_token_sequence(struct token
*token
)
1355 static char buffer
[1024];
1361 while (!eof_token(token
)) {
1362 const char *val
= show_token(token
);
1363 int len
= strlen(val
);
1365 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1366 sparse_error(token
->pos
, "too long token expansion");
1372 memcpy(ptr
, val
, len
);
1374 token
= token
->next
;
1375 whitespace
= token
->pos
.whitespace
;
1381 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1383 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1387 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1389 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
));
1393 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1396 * Do we have any non-system includes?
1397 * Clear them out if so..
1399 *sys_includepath
= NULL
;
1403 static void add_path_entry(struct token
*token
, const char *path
, const char ***where
, const char **new_path
)
1408 /* Need one free entry.. */
1409 if (includepath
[INCLUDEPATHS
-2])
1410 error_die(token
->pos
, "too many include path entries");
1412 /* check that this is not a duplicate */
1415 if (strcmp(*dst
, path
) == 0)
1424 * Move them all up starting at dst,
1425 * insert the new entry..
1428 const char *tmp
= *dst
;
1437 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1440 token
= token
->next
;
1441 if (eof_token(token
))
1443 if (token_type(token
) != TOKEN_STRING
) {
1444 warning(token
->pos
, "expected path string");
1447 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
+ 1);
1451 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1454 token
= token
->next
;
1455 if (eof_token(token
))
1457 if (token_type(token
) != TOKEN_STRING
) {
1458 sparse_error(token
->pos
, "expected path string");
1461 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
);
1465 /* Add to end on includepath list - no pointer updates */
1466 static void add_dirafter_entry(struct token
*token
, const char *path
)
1468 const char **dst
= includepath
;
1470 /* Need one free entry.. */
1471 if (includepath
[INCLUDEPATHS
-2])
1472 error_die(token
->pos
, "too many include path entries");
1474 /* Add to the end */
1482 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1485 token
= token
->next
;
1486 if (eof_token(token
))
1488 if (token_type(token
) != TOKEN_STRING
) {
1489 sparse_error(token
->pos
, "expected path string");
1492 add_dirafter_entry(token
, token
->string
->data
);
1496 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1501 * Split the include path. Any directories specified with `-I'
1502 * options before `-I-' are searched only for headers requested with
1503 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1504 * If additional directories are specified with `-I' options after
1505 * the `-I-', those directories are searched for all `#include'
1507 * In addition, `-I-' inhibits the use of the directory of the current
1508 * file directory as the first search directory for `#include "FILE"'.
1510 quote_includepath
= includepath
+1;
1511 angle_includepath
= sys_includepath
;
1516 * We replace "#pragma xxx" with "__pragma__" in the token
1517 * stream. Just as an example.
1519 * We'll just #define that away for now, but the theory here
1520 * is that we can use this to insert arbitrary token sequences
1521 * to turn the pragma's into internal front-end sequences for
1522 * when we actually start caring about them.
1524 * So eventually this will turn into some kind of extended
1525 * __attribute__() like thing, except called __pragma__(xxx).
1527 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1529 struct token
*next
= *line
;
1531 token
->ident
= &pragma_ident
;
1532 token
->pos
.newline
= 1;
1533 token
->pos
.whitespace
= 1;
1541 * We ignore #line for now.
1543 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1548 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1550 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1555 static void init_preprocessor(void)
1558 int stream
= init_stream("preprocessor", -1, includepath
);
1561 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1563 { "define", handle_define
},
1564 { "weak_define",handle_weak_define
},
1565 { "undef", handle_undef
},
1566 { "warning", handle_warning
},
1567 { "error", handle_error
},
1568 { "include", handle_include
},
1569 { "include_next",handle_include_next
},
1570 { "pragma", handle_pragma
},
1571 { "line", handle_line
},
1573 // our internal preprocessor tokens
1574 { "nostdinc", handle_nostdinc
},
1575 { "add_include", handle_add_include
},
1576 { "add_isystem", handle_add_isystem
},
1577 { "add_dirafter", handle_add_dirafter
},
1578 { "split_include", handle_split_include
},
1580 { "ifdef", handle_ifdef
},
1581 { "ifndef", handle_ifndef
},
1582 { "else", handle_else
},
1583 { "endif", handle_endif
},
1584 { "if", handle_if
},
1585 { "elif", handle_elif
},
1588 for (i
= 0; i
< (sizeof (normal
) / sizeof (normal
[0])); i
++) {
1590 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1591 sym
->handler
= normal
[i
].handler
;
1594 for (i
= 0; i
< (sizeof (special
) / sizeof (special
[0])); i
++) {
1596 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1597 sym
->handler
= special
[i
].handler
;
1603 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1605 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1606 struct token
*token
= start
->next
;
1609 if (eof_token(token
))
1612 if (token_type(token
) == TOKEN_IDENT
) {
1613 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1615 handler
= sym
->handler
;
1616 is_normal
= sym
->normal
;
1618 handler
= handle_nondirective
;
1620 } else if (token_type(token
) == TOKEN_NUMBER
) {
1621 handler
= handle_line
;
1623 handler
= handle_nondirective
;
1627 dirty_stream(stream
);
1631 if (!handler(stream
, line
, token
)) /* all set */
1635 free_preprocessor_line(token
);
1638 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1640 struct token
*start
= *line
, *next
;
1641 struct token
**tp
= &start
->next
;
1645 if (next
->pos
.newline
)
1650 *tp
= &eof_token_entry
;
1651 handle_preprocessor_line(stream
, line
, start
);
1654 static void do_preprocess(struct token
**list
)
1658 while (!eof_token(next
= scan_next(list
))) {
1659 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1661 if (next
->pos
.newline
&& match_op(next
, '#')) {
1662 if (!next
->pos
.noexpand
) {
1663 preprocessor_line(stream
, list
);
1664 __free_token(next
); /* Free the '#' token */
1669 switch (token_type(next
)) {
1670 case TOKEN_STREAMEND
:
1671 if (stream
->nesting
< if_nesting
+ 1) {
1672 nesting_error(stream
);
1673 sparse_error(unmatched_if_pos
, "unterminated preprocessor conditional");
1674 if_nesting
= stream
->nesting
- 1;
1678 stream
->constant
= CONSTANT_FILE_YES
;
1681 case TOKEN_STREAMBEGIN
:
1682 stream
->nesting
= if_nesting
+ 1;
1687 dirty_stream(stream
);
1688 if (false_nesting
) {
1694 if (token_type(next
) != TOKEN_IDENT
||
1695 expand_one_symbol(list
))
1701 struct token
* preprocess(struct token
*token
)
1704 init_preprocessor();
1705 do_preprocess(&token
);
1707 // Drop all expressions from pre-processing, they're not used any more.
1708 // This is not true when we have multiple files, though ;/
1709 // clear_expression_alloc();