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 true_nesting
= 0;
32 static int false_nesting
= 0;
33 static struct position unmatched_if_pos
;
34 #define if_nesting (true_nesting + false_nesting)
36 #define MAX_NEST (256)
37 static unsigned char elif_ignore
[MAX_NEST
];
38 enum { ELIF_IGNORE
= 1, ELIF_SEEN_ELSE
= 2 };
40 #define INCLUDEPATHS 300
41 const char *includepath
[INCLUDEPATHS
+1] = {
49 static const char **quote_includepath
= includepath
;
50 static const char **angle_includepath
= includepath
+ 1;
51 static const char **sys_includepath
= includepath
+ 1;
53 #define MARK_STREAM_NONCONST(pos) do { \
54 if (stream->constant != CONSTANT_FILE_NOPE) { \
56 info(pos, "%s triggers non-const", __func__); \
57 stream->constant = CONSTANT_FILE_NOPE; \
62 static struct token
*alloc_token(struct position
*pos
)
64 struct token
*token
= __alloc_token(0);
66 token
->pos
.stream
= pos
->stream
;
67 token
->pos
.line
= pos
->line
;
68 token
->pos
.pos
= pos
->pos
;
69 token
->pos
.whitespace
= 1;
73 static const char *show_token_sequence(struct token
*token
);
75 /* Expand symbol 'sym' at '*list' */
76 static int expand(struct token
**, struct symbol
*);
78 static void replace_with_string(struct token
*token
, const char *str
)
80 int size
= strlen(str
) + 1;
81 struct string
*s
= __alloc_string(size
);
84 memcpy(s
->data
, str
, size
);
85 token_type(token
) = TOKEN_STRING
;
89 static void replace_with_integer(struct token
*token
, unsigned int val
)
91 char *buf
= __alloc_bytes(11);
92 sprintf(buf
, "%u", val
);
93 token_type(token
) = TOKEN_NUMBER
;
97 struct symbol
*lookup_macro(struct ident
*ident
)
99 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_INVISIBLEMACRO
);
100 if (sym
&& sym
->namespace != NS_MACRO
)
105 static int token_defined(struct token
*token
)
107 if (token_type(token
) == TOKEN_IDENT
) {
108 struct symbol
*sym
= lookup_macro(token
->ident
);
116 warning(token
->pos
, "expected preprocessor identifier");
120 static void replace_with_defined(struct token
*token
)
122 static const char *string
[] = { "0", "1" };
123 int defined
= token_defined(token
);
125 token_type(token
) = TOKEN_NUMBER
;
126 token
->number
= string
[defined
];
129 static int expand_one_symbol(struct token
**list
)
131 struct token
*token
= *list
;
134 if (token
->pos
.noexpand
)
137 sym
= lookup_macro(token
->ident
);
140 return expand(list
, sym
);
142 if (token
->ident
== &__LINE___ident
) {
143 replace_with_integer(token
, token
->pos
.line
);
144 } else if (token
->ident
== &__FILE___ident
) {
145 replace_with_string(token
, stream_name(token
->pos
.stream
));
150 static inline struct token
*scan_next(struct token
**where
)
152 struct token
*token
= *where
;
153 if (token_type(token
) != TOKEN_UNTAINT
)
156 token
->ident
->tainted
= 0;
158 } while (token_type(token
) == TOKEN_UNTAINT
);
163 static void expand_list(struct token
**list
)
166 while (!eof_token(next
= scan_next(list
))) {
167 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
172 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
174 struct token
**p
= &prev
->next
;
178 while (!eof_token(next
= scan_next(p
))) {
179 if (match_op(next
, '(')) {
181 } else if (match_op(next
, ')')) {
184 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
187 next
->pos
.stream
= pos
->stream
;
188 next
->pos
.line
= pos
->line
;
189 next
->pos
.pos
= pos
->pos
;
192 *p
= &eof_token_entry
;
197 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
202 struct token
*expanded
;
209 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
211 int wanted
= arglist
->count
.normal
;
212 struct token
*next
= NULL
;
215 arglist
= arglist
->next
; /* skip counter */
218 next
= collect_arg(start
, 0, &what
->pos
);
221 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
226 for (count
= 0; count
< wanted
; count
++) {
227 struct argcount
*p
= &arglist
->next
->count
;
228 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
229 arglist
= arglist
->next
->next
;
232 args
[count
].arg
= start
->next
;
233 args
[count
].n_normal
= p
->normal
;
234 args
[count
].n_quoted
= p
->quoted
;
235 args
[count
].n_str
= p
->str
;
236 if (match_op(next
, ')')) {
242 if (count
== wanted
&& !match_op(next
, ')'))
244 if (count
== wanted
- 1) {
245 struct argcount
*p
= &arglist
->next
->count
;
248 args
[count
].arg
= NULL
;
249 args
[count
].n_normal
= p
->normal
;
250 args
[count
].n_quoted
= p
->quoted
;
251 args
[count
].n_str
= p
->str
;
253 if (count
< wanted
- 1)
256 what
->next
= next
->next
;
260 warning(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
261 show_token(what
), wanted
, count
);
264 while (match_op(next
, ',')) {
265 next
= collect_arg(next
, 0, &what
->pos
);
270 warning(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
271 show_token(what
), count
, wanted
);
274 warning(what
->pos
, "unterminated argument list invoking macro \"%s\"",
277 what
->next
= next
->next
;
281 static struct token
*dup_list(struct token
*list
)
284 struct token
**p
= &res
;
286 while (!eof_token(list
)) {
287 struct token
*newtok
= __alloc_token(0);
296 static struct token
*stringify(struct token
*arg
)
298 const char *s
= show_token_sequence(arg
);
299 int size
= strlen(s
)+1;
300 struct token
*token
= __alloc_token(0);
301 struct string
*string
= __alloc_string(size
);
303 memcpy(string
->data
, s
, size
);
304 string
->length
= size
;
305 token
->pos
= arg
->pos
;
306 token_type(token
) = TOKEN_STRING
;
307 token
->string
= string
;
308 token
->next
= &eof_token_entry
;
312 static void expand_arguments(int count
, struct arg
*args
)
315 for (i
= 0; i
< count
; i
++) {
316 struct token
*arg
= args
[i
].arg
;
318 arg
= &eof_token_entry
;
320 args
[i
].str
= stringify(arg
);
321 if (args
[i
].n_normal
) {
322 if (!args
[i
].n_quoted
) {
323 args
[i
].expanded
= arg
;
325 } else if (eof_token(arg
)) {
326 args
[i
].expanded
= arg
;
328 args
[i
].expanded
= dup_list(arg
);
330 expand_list(&args
[i
].expanded
);
336 * Possibly valid combinations:
337 * - ident + ident -> ident
338 * - ident + number -> ident unless number contains '.', '+' or '-'.
339 * - number + number -> number
340 * - number + ident -> number
341 * - number + '.' -> number
342 * - number + '+' or '-' -> number, if number used to end on [eEpP].
343 * - '.' + number -> number, if number used to start with a digit.
344 * - special + special -> either special or an error.
346 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
349 enum token_type t1
= token_type(left
), t2
= token_type(right
);
351 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
354 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
357 strcpy(p
, show_token(left
));
358 strcat(p
, show_token(right
));
364 if (t1
== TOKEN_IDENT
) {
365 if (t2
== TOKEN_SPECIAL
)
367 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
372 if (t1
== TOKEN_NUMBER
) {
373 if (t2
== TOKEN_SPECIAL
) {
374 switch (right
->special
) {
378 if (strchr("eEpP", p
[len
- 2]))
387 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
390 return TOKEN_SPECIAL
;
393 static int merge(struct token
*left
, struct token
*right
)
395 static char buffer
[512];
398 switch (combine(left
, right
, buffer
)) {
400 left
->ident
= built_in_ident(buffer
);
401 left
->pos
.noexpand
= 0;
405 char *number
= __alloc_bytes(strlen(buffer
) + 1);
406 memcpy(number
, buffer
, strlen(buffer
) + 1);
407 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
408 left
->number
= number
;
413 if (buffer
[2] && buffer
[3])
415 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
416 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
424 warning(left
->pos
, "'##' failed: concatenation is not a valid token");
428 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
430 struct token
*alloc
= alloc_token(streampos
);
431 token_type(alloc
) = token_type(token
);
432 alloc
->pos
.newline
= pos
->newline
;
433 alloc
->pos
.whitespace
= pos
->whitespace
;
434 alloc
->number
= token
->number
;
435 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
439 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
441 int need_copy
= --*count
;
442 while (!eof_token(list
)) {
445 token
= dup_token(list
, &list
->pos
, &list
->pos
);
448 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
449 token
->pos
.noexpand
= 1;
451 where
= &token
->next
;
454 *where
= &eof_token_entry
;
458 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
460 struct token
*token
= *list
;
461 struct position
*base_pos
= &token
->pos
;
462 struct position
*pos
= base_pos
;
464 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
466 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
467 struct token
*added
, *arg
;
470 switch (token_type(body
)) {
471 case TOKEN_GNU_KLUDGE
:
473 * GNU kludge: if we had <comma>##<vararg>, behaviour
474 * depends on whether we had enough arguments to have
475 * a vararg. If we did, ## is just ignored. Otherwise
476 * both , and ## are ignored. Comma should come from
477 * the body of macro and not be an argument of earlier
480 if (!args
[body
->next
->argnum
].arg
)
482 added
= dup_token(body
, base_pos
, pos
);
483 token_type(added
) = TOKEN_SPECIAL
;
487 case TOKEN_STR_ARGUMENT
:
488 arg
= args
[body
->argnum
].str
;
489 count
= &args
[body
->argnum
].n_str
;
492 case TOKEN_QUOTED_ARGUMENT
:
493 arg
= args
[body
->argnum
].arg
;
494 count
= &args
[body
->argnum
].n_quoted
;
495 if (!arg
|| eof_token(arg
)) {
504 case TOKEN_MACRO_ARGUMENT
:
505 arg
= args
[body
->argnum
].expanded
;
506 count
= &args
[body
->argnum
].n_normal
;
507 if (eof_token(arg
)) {
512 tail
= copy(&added
, arg
, count
);
513 added
->pos
.newline
= pos
->newline
;
514 added
->pos
.whitespace
= pos
->whitespace
;
518 if (state
== Placeholder
)
525 added
= dup_token(body
, base_pos
, pos
);
526 if (added
->ident
->tainted
)
527 added
->pos
.noexpand
= 1;
532 added
= dup_token(body
, base_pos
, pos
);
538 * if we got to doing real concatenation, we already have
539 * added something into the list, so containing_token() is OK.
541 if (state
== Concat
&& merge(containing_token(list
), added
)) {
543 if (tail
!= &added
->next
)
551 *list
= &eof_token_entry
;
555 static int expand(struct token
**list
, struct symbol
*sym
)
558 struct token
*token
= *list
;
559 struct ident
*expanding
= token
->ident
;
561 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
562 struct arg args
[nargs
];
564 if (expanding
->tainted
) {
565 token
->pos
.noexpand
= 1;
570 if (!match_op(scan_next(&token
->next
), '('))
572 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
574 expand_arguments(nargs
, args
);
577 expanding
->tainted
= 1;
580 tail
= substitute(list
, sym
->expansion
, args
);
586 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
589 static char buffer
[256];
593 while (!eof_token(token
) && !match_op(token
, endop
)) {
595 const char *val
= token
->string
->data
;
596 if (token_type(token
) != TOKEN_STRING
)
597 val
= show_token(token
);
599 memcpy(ptr
, val
, len
);
604 if (endop
&& !match_op(token
, endop
))
605 warning(start
->pos
, "expected '>' at end of filename");
609 static int already_tokenized(const char *path
)
612 struct stream
*s
= input_streams
;
614 for (i
= input_stream_nr
; --i
>= 0; s
++) {
615 if (s
->constant
!= CONSTANT_FILE_YES
)
617 if (strcmp(path
, s
->name
))
619 if (!lookup_macro(s
->protect
))
626 /* Hande include of header files.
627 * The relevant options are made compatible with gcc. The only options that
628 * are not supported is -withprefix and friends.
630 * Three set of include paths are known:
631 * quote_includepath: Path to search when using #include "file.h"
632 * angle_includepath: Path to search when using #include <file.h>
633 * sys_includepath: Built-in include paths
635 * The above is implmented as one array with pointes
637 * quote_includepath ---> | |
641 * angle_includepath ---> | |
643 * sys_includepath ---> | |
648 * -I dir insert dir just before sys_includepath and move the rest
649 * -I- makes all dirs specified with -I before to quote dirs only and
650 * angle_includepath is set equal to sys_includepath.
651 * -nostdinc removes all sys dirs be storing NULL in entry pointed
652 * to by * sys_includepath. Note this will reset all dirs built-in and added
653 * before -nostdinc by -isystem and -dirafter
654 * -isystem dir adds dir where sys_includepath points adding this dir as
656 * -dirafter dir adds dir to the end of the list
659 static void set_stream_include_path(struct stream
*stream
)
661 const char *path
= stream
->path
;
663 const char *p
= strrchr(stream
->name
, '/');
666 int len
= p
- stream
->name
+ 1;
667 char *m
= malloc(len
+1);
668 /* This includes the final "/" */
669 memcpy(m
, stream
->name
, len
);
675 includepath
[0] = path
;
678 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
681 int plen
= strlen(path
);
682 static char fullname
[PATH_MAX
];
684 memcpy(fullname
, path
, plen
);
685 if (plen
&& path
[plen
-1] != '/') {
686 fullname
[plen
] = '/';
689 memcpy(fullname
+plen
, filename
, flen
);
690 if (already_tokenized(fullname
))
692 fd
= open(fullname
, O_RDONLY
);
694 char * streamname
= __alloc_bytes(plen
+ flen
);
695 memcpy(streamname
, fullname
, plen
+ flen
);
696 *where
= tokenize(streamname
, fd
, *where
, next_path
);
703 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
707 while ((path
= *pptr
++) != NULL
) {
708 if (!try_include(path
, filename
, flen
, list
, pptr
))
715 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
717 int flen
= strlen(filename
) + 1;
720 if (filename
[0] == '/') {
721 if (try_include("", filename
, flen
, list
, includepath
))
726 /* Dir of inputfile is first dir to search for quoted includes */
727 set_stream_include_path(stream
);
730 /* Do not search quote include if <> is in use */
731 path
= local
? quote_includepath
: angle_includepath
;
733 /* Check the standard include paths.. */
734 if (do_include_path(path
, list
, token
, filename
, flen
))
737 error_die(token
->pos
, "unable to open '%s'", filename
);
740 static int free_preprocessor_line(struct token
*token
)
743 struct token
*free
= token
;
746 } while (token_type(token
) != TOKEN_EOF
);
750 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
752 const char *filename
;
757 return free_preprocessor_line(token
);
759 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
760 MARK_STREAM_NONCONST(token
->pos
);
764 if (!match_op(next
, '<')) {
765 expand_list(&token
->next
);
768 if (match_op(token
->next
, '<')) {
774 filename
= token_name_sequence(token
, expect
, token
);
775 do_include(!expect
, stream
, list
, token
, filename
, path
);
779 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
781 return handle_include_path(stream
, list
, token
, NULL
);
784 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
786 return handle_include_path(stream
, list
, token
, stream
->next_path
);
789 static int token_different(struct token
*t1
, struct token
*t2
)
793 if (token_type(t1
) != token_type(t2
))
796 switch (token_type(t1
)) {
798 different
= t1
->ident
!= t2
->ident
;
800 case TOKEN_ARG_COUNT
:
803 case TOKEN_GNU_KLUDGE
:
807 different
= strcmp(t1
->number
, t2
->number
);
810 different
= t1
->special
!= t2
->special
;
812 case TOKEN_MACRO_ARGUMENT
:
813 case TOKEN_QUOTED_ARGUMENT
:
814 case TOKEN_STR_ARGUMENT
:
815 different
= t1
->argnum
!= t2
->argnum
;
818 different
= t1
->character
!= t2
->character
;
821 struct string
*s1
, *s2
;
826 if (s1
->length
!= s2
->length
)
828 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
838 static int token_list_different(struct token
*list1
, struct token
*list2
)
843 if (!list1
|| !list2
)
845 if (token_different(list1
, list2
))
852 static inline void set_arg_count(struct token
*token
)
854 token_type(token
) = TOKEN_ARG_COUNT
;
855 token
->count
.normal
= token
->count
.quoted
=
856 token
->count
.str
= token
->count
.vararg
= 0;
859 static struct token
*parse_arguments(struct token
*list
)
861 struct token
*arg
= list
->next
, *next
= list
;
862 struct argcount
*count
= &list
->count
;
866 if (match_op(arg
, ')')) {
868 list
->next
= &eof_token_entry
;
872 while (token_type(arg
) == TOKEN_IDENT
) {
873 if (arg
->ident
== &__VA_ARGS___ident
)
875 if (!++count
->normal
)
879 if (match_op(next
, ',')) {
885 if (match_op(next
, ')')) {
888 arg
->next
->next
= &eof_token_entry
;
892 /* normal cases are finished here */
894 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
895 if (match_op(next
->next
, ')')) {
897 next
->count
.vararg
= 1;
899 arg
->next
->next
= &eof_token_entry
;
907 if (eof_token(next
)) {
915 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
917 token_type(arg
) = TOKEN_IDENT
;
918 arg
->ident
= &__VA_ARGS___ident
;
919 if (!match_op(next
, ')'))
921 if (!++count
->normal
)
924 next
->count
.vararg
= 1;
926 arg
->next
->next
= &eof_token_entry
;
930 if (eof_token(arg
)) {
934 if (match_op(arg
, ','))
941 warning(arg
->pos
, "parameter name missing");
944 warning(arg
->pos
, "\"%s\" may not appear in macro parameter list",
948 warning(arg
->pos
, "missing ')' in macro parameter list");
951 warning(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
954 warning(arg
->pos
, "too many arguments in macro definition");
958 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
960 struct ident
*ident
= token
->ident
;
963 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
966 arglist
= arglist
->next
;
968 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
969 if (arglist
->ident
== ident
) {
970 struct argcount
*count
= &arglist
->next
->count
;
974 token_type(token
) = type
;
976 case TOKEN_MACRO_ARGUMENT
:
979 case TOKEN_QUOTED_ARGUMENT
:
986 return count
->vararg
? 2 : 1;
987 token_type(token
) = TOKEN_ERROR
;
994 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
996 struct token
*token
= expansion
;
998 struct token
*last
= NULL
;
1000 if (match_op(token
, SPECIAL_HASHHASH
))
1003 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1004 if (match_op(token
, '#')) {
1006 struct token
*next
= token
->next
;
1007 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1009 next
->pos
.whitespace
= token
->pos
.whitespace
;
1012 token
->pos
.noexpand
= 1;
1014 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
1015 struct token
*next
= token
->next
;
1016 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1017 token_type(token
) = TOKEN_CONCAT
;
1021 if (arg
== 2 && last
&& match_op(last
, ',')) {
1022 token_type(last
) = TOKEN_GNU_KLUDGE
;
1025 } else if (match_op(next
, SPECIAL_HASHHASH
))
1027 else if (match_op(next
, ','))
1029 else if (eof_token(next
))
1031 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1032 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1034 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1036 if (token_type(token
) == TOKEN_ERROR
)
1040 token
= alloc_token(&expansion
->pos
);
1041 token_type(token
) = TOKEN_UNTAINT
;
1042 token
->ident
= name
;
1048 warning(token
->pos
, "'#' is not followed by a macro parameter");
1052 warning(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1055 warning(token
->pos
, "too many instances of argument in body");
1059 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int weak
)
1061 struct token
*arglist
, *expansion
;
1062 struct token
*left
= token
->next
;
1066 if (token_type(left
) != TOKEN_IDENT
) {
1067 warning(token
->pos
, "expected identifier to 'define'");
1071 return free_preprocessor_line(token
);
1073 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1074 MARK_STREAM_NONCONST(token
->pos
);
1076 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1080 expansion
= left
->next
;
1081 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
1082 arglist
= expansion
;
1083 expansion
= parse_arguments(expansion
);
1088 expansion
= parse_expansion(expansion
, arglist
, name
);
1092 sym
= lookup_macro(name
);
1094 if (token_list_different(sym
->expansion
, expansion
) ||
1095 token_list_different(sym
->arglist
, arglist
)) {
1100 warning(left
->pos
, "preprocessor token %.*s redefined",
1101 name
->len
, name
->name
);
1102 info(sym
->pos
, "this was the original definition");
1104 /* Don't overwrite global defs */
1105 if (sym
->scope
!= file_scope
)
1107 sym
->expansion
= expansion
;
1108 sym
->arglist
= arglist
;
1113 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1114 bind_symbol(sym
, name
, NS_MACRO
);
1117 sym
->expansion
= expansion
;
1118 sym
->arglist
= arglist
;
1123 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1125 return do_handle_define(stream
, line
, token
, 0);
1128 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1130 return do_handle_define(stream
, line
, token
, 1);
1133 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1135 struct token
*left
= token
->next
;
1136 struct symbol
**sym
;
1138 if (token_type(left
) != TOKEN_IDENT
) {
1139 warning(token
->pos
, "expected identifier to 'undef'");
1143 return free_preprocessor_line(token
);
1145 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1146 MARK_STREAM_NONCONST(token
->pos
);
1148 sym
= &left
->ident
->symbols
;
1150 struct symbol
*t
= *sym
;
1151 if (t
->namespace & (NS_MACRO
| NS_INVISIBLEMACRO
)) {
1152 t
->namespace = NS_INVISIBLEMACRO
;
1157 return free_preprocessor_line(token
);
1160 static int preprocessor_if(struct token
*token
, int true)
1162 if (if_nesting
== 0)
1163 unmatched_if_pos
= token
->pos
;
1164 if (if_nesting
>= MAX_NEST
)
1165 error_die(token
->pos
, "Maximum preprocessor conditional level exhausted");
1166 elif_ignore
[if_nesting
] = (false_nesting
|| true) ? ELIF_IGNORE
: 0;
1167 if (false_nesting
|| !true) {
1169 return free_preprocessor_line(token
);
1172 return free_preprocessor_line(token
);
1175 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1177 return preprocessor_if(token
, token_defined(token
->next
));
1180 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1182 struct token
*next
= token
->next
;
1183 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1184 if (token_type(next
) == TOKEN_IDENT
&&
1185 (!stream
->protect
|| stream
->protect
== next
->ident
)) {
1186 stream
->constant
= CONSTANT_FILE_IFNDEF
;
1187 stream
->protect
= next
->ident
;
1189 MARK_STREAM_NONCONST(token
->pos
);
1191 return preprocessor_if(token
, !token_defined(next
));
1195 * Expression handling for #if and #elif; it differs from normal expansion
1196 * due to special treatment of "defined".
1198 static int expression_value(struct token
**where
)
1200 struct expression
*expr
;
1202 struct token
**list
= where
, **beginning
= NULL
;
1206 while (!eof_token(p
= scan_next(list
))) {
1209 if (token_type(p
) != TOKEN_IDENT
)
1211 if (p
->ident
== &defined_ident
) {
1216 if (!expand_one_symbol(list
))
1218 if (token_type(p
) != TOKEN_IDENT
)
1220 if (Wundefined_preprocessor
)
1221 warning(p
->pos
, "undefined preprocessor identifier '%s'", show_ident(p
->ident
));
1222 replace_with_integer(p
, 0);
1225 if (match_op(p
, '(')) {
1229 replace_with_defined(p
);
1234 if (token_type(p
) == TOKEN_IDENT
)
1238 replace_with_defined(p
);
1243 if (!match_op(p
, ')'))
1244 warning(p
->pos
, "missing ')' after \"defined\"");
1251 p
= constant_expression(*where
, &expr
);
1253 warning(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1254 value
= get_expression_value(expr
);
1258 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1262 value
= expression_value(&token
->next
);
1264 // This is an approximation. We really only need this if the
1265 // condition does depends on a pre-processor symbol. Note, that
1266 // the important #ifndef case has already changed ->constant.
1267 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1268 MARK_STREAM_NONCONST(token
->pos
);
1270 return preprocessor_if(token
, value
);
1273 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1275 if (stream
->nesting
== if_nesting
)
1276 MARK_STREAM_NONCONST(token
->pos
);
1278 if (stream
->nesting
> if_nesting
)
1279 warning(token
->pos
, "unmatched #elif within stream");
1281 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1282 warning(token
->pos
, "#elif after #else");
1284 if (false_nesting
) {
1285 /* If this whole if-thing is if'ed out, an elif cannot help */
1286 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1288 if (expression_value(&token
->next
)) {
1291 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1297 return free_preprocessor_line(token
);
1300 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1302 if (stream
->nesting
== if_nesting
)
1303 MARK_STREAM_NONCONST(token
->pos
);
1305 if (stream
->nesting
> if_nesting
)
1306 warning(token
->pos
, "unmatched #else within stream");
1308 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1309 warning(token
->pos
, "#else after #else");
1311 elif_ignore
[if_nesting
-1] |= ELIF_SEEN_ELSE
;
1313 if (false_nesting
) {
1314 /* If this whole if-thing is if'ed out, an else cannot help */
1315 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1319 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1324 return free_preprocessor_line(token
);
1327 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1329 if (stream
->constant
== CONSTANT_FILE_IFNDEF
&& stream
->nesting
== if_nesting
)
1330 stream
->constant
= CONSTANT_FILE_MAYBE
;
1332 if (stream
->nesting
> if_nesting
)
1333 warning(token
->pos
, "unmatched #endif in stream");
1340 return free_preprocessor_line(token
);
1343 static const char *show_token_sequence(struct token
*token
)
1345 static char buffer
[1024];
1351 while (!eof_token(token
)) {
1352 const char *val
= show_token(token
);
1353 int len
= strlen(val
);
1355 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1356 warning(token
->pos
, "too long token expansion");
1362 memcpy(ptr
, val
, len
);
1364 token
= token
->next
;
1365 whitespace
= token
->pos
.whitespace
;
1371 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1374 return free_preprocessor_line(token
);
1375 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1376 MARK_STREAM_NONCONST(token
->pos
);
1377 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1378 return free_preprocessor_line(token
);
1381 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1384 return free_preprocessor_line(token
);
1385 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1386 MARK_STREAM_NONCONST(token
->pos
);
1387 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1388 return free_preprocessor_line(token
);
1391 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1394 return free_preprocessor_line(token
);
1397 * Do we have any non-system includes?
1398 * Clear them out if so..
1400 *sys_includepath
= NULL
;
1401 return free_preprocessor_line(token
);
1404 static void add_path_entry(struct token
*token
, const char *path
, const char ***where
, const char **new_path
)
1409 /* Need one free entry.. */
1410 if (includepath
[INCLUDEPATHS
-2])
1411 error_die(token
->pos
, "too many include path entries");
1413 /* check that this is not a duplicate */
1416 if (strcmp(*dst
, path
) == 0)
1425 * Move them all up starting at dst,
1426 * insert the new entry..
1429 const char *tmp
= *dst
;
1438 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1441 token
= token
->next
;
1442 if (eof_token(token
))
1444 if (token_type(token
) != TOKEN_STRING
) {
1445 warning(token
->pos
, "expected path string");
1448 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
+ 1);
1452 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1455 token
= token
->next
;
1456 if (eof_token(token
))
1458 if (token_type(token
) != TOKEN_STRING
) {
1459 warning(token
->pos
, "expected path string");
1462 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
);
1466 /* Add to end on includepath list - no pointer updates */
1467 static void add_dirafter_entry(struct token
*token
, const char *path
)
1469 const char **dst
= includepath
;
1471 /* Need one free entry.. */
1472 if (includepath
[INCLUDEPATHS
-2])
1473 error_die(token
->pos
, "too many include path entries");
1475 /* Add to the end */
1483 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1486 token
= token
->next
;
1487 if (eof_token(token
))
1489 if (token_type(token
) != TOKEN_STRING
) {
1490 warning(token
->pos
, "expected path string");
1493 add_dirafter_entry(token
, token
->string
->data
);
1497 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1500 return free_preprocessor_line(token
);
1505 * Split the include path. Any directories specified with `-I'
1506 * options before `-I-' are searched only for headers requested with
1507 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1508 * If additional directories are specified with `-I' options after
1509 * the `-I-', those directories are searched for all `#include'
1511 * In addition, `-I-' inhibits the use of the directory of the current
1512 * file directory as the first search directory for `#include "FILE"'.
1514 quote_includepath
= includepath
+1;
1515 angle_includepath
= sys_includepath
;
1516 return free_preprocessor_line(token
);
1520 * We replace "#pragma xxx" with "__pragma__" in the token
1521 * stream. Just as an example.
1523 * We'll just #define that away for now, but the theory here
1524 * is that we can use this to insert arbitrary token sequences
1525 * to turn the pragma's into internal front-end sequences for
1526 * when we actually start caring about them.
1528 * So eventually this will turn into some kind of extended
1529 * __attribute__() like thing, except called __pragma__(xxx).
1531 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1533 struct token
*next
= *line
;
1535 token
->ident
= &pragma_ident
;
1536 token
->pos
.newline
= 1;
1537 token
->pos
.whitespace
= 1;
1545 * We ignore #line for now.
1547 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1553 static void init_preprocessor(void)
1556 int stream
= init_stream("preprocessor", -1, includepath
);
1559 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1561 { "define", handle_define
},
1562 { "weak_define",handle_weak_define
},
1563 { "undef", handle_undef
},
1564 { "ifdef", handle_ifdef
},
1565 { "ifndef", handle_ifndef
},
1566 { "else", handle_else
},
1567 { "endif", handle_endif
},
1568 { "if", handle_if
},
1569 { "elif", handle_elif
},
1570 { "warning", handle_warning
},
1571 { "error", handle_error
},
1572 { "include", handle_include
},
1573 { "include_next",handle_include_next
},
1574 { "pragma", handle_pragma
},
1575 { "line", handle_line
},
1577 // our internal preprocessor tokens
1578 { "nostdinc", handle_nostdinc
},
1579 { "add_include", handle_add_include
},
1580 { "add_isystem", handle_add_isystem
},
1581 { "add_dirafter", handle_add_dirafter
},
1582 { "split_include", handle_split_include
},
1585 for (i
= 0; i
< (sizeof (handlers
) / sizeof (handlers
[0])); i
++) {
1587 sym
= create_symbol(stream
, handlers
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1588 sym
->handler
= handlers
[i
].handler
;
1592 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1594 struct token
*token
= start
->next
;
1599 if (token_type(token
) == TOKEN_NUMBER
)
1600 if (handle_line(stream
, line
, start
))
1603 if (token_type(token
) == TOKEN_IDENT
) {
1604 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1605 if (sym
&& sym
->handler(stream
, line
, token
))
1609 warning(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1612 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1614 struct token
*start
= *line
, *next
;
1615 struct token
**tp
= &start
->next
;
1619 if (next
->pos
.newline
)
1624 *tp
= &eof_token_entry
;
1625 handle_preprocessor_line(stream
, line
, start
);
1628 static void do_preprocess(struct token
**list
)
1632 while (!eof_token(next
= scan_next(list
))) {
1633 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1635 if (next
->pos
.newline
&& match_op(next
, '#')) {
1636 if (!next
->pos
.noexpand
) {
1637 preprocessor_line(stream
, list
);
1638 __free_token(next
); /* Free the '#' token */
1643 switch (token_type(next
)) {
1644 case TOKEN_STREAMEND
:
1645 if (stream
->nesting
< if_nesting
+ 1) {
1646 warning(unmatched_if_pos
, "unterminated preprocessor conditional");
1647 // Pretend to see a series of #endifs
1648 MARK_STREAM_NONCONST(next
->pos
);
1650 handle_endif (stream
, NULL
, NULL
);
1651 } while (stream
->nesting
< if_nesting
+ 1);
1653 if (stream
->constant
== CONSTANT_FILE_MAYBE
&& stream
->protect
) {
1654 stream
->constant
= CONSTANT_FILE_YES
;
1658 case TOKEN_STREAMBEGIN
:
1659 stream
->nesting
= if_nesting
+ 1;
1664 if (false_nesting
) {
1670 if (token_type(next
) != TOKEN_IDENT
||
1671 expand_one_symbol(list
))
1675 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1677 * Any token expansion (even if it ended up being an
1678 * empty expansion) in this stream implies it can't
1681 MARK_STREAM_NONCONST(next
->pos
);
1686 struct token
* preprocess(struct token
*token
)
1689 init_preprocessor();
1690 do_preprocess(&token
);
1692 // Drop all expressions from pre-processing, they're not used any more.
1693 // This is not true when we have multiple files, though ;/
1694 // clear_expression_alloc();