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 MARK_STREAM_NONCONST(pos) do { \
53 if (stream->constant != CONSTANT_FILE_NOPE) { \
55 info(pos, "%s triggers non-const", __func__); \
56 stream->constant = CONSTANT_FILE_NOPE; \
61 static struct token
*alloc_token(struct position
*pos
)
63 struct token
*token
= __alloc_token(0);
65 token
->pos
.stream
= pos
->stream
;
66 token
->pos
.line
= pos
->line
;
67 token
->pos
.pos
= pos
->pos
;
68 token
->pos
.whitespace
= 1;
72 static const char *show_token_sequence(struct token
*token
);
74 /* Expand symbol 'sym' at '*list' */
75 static int expand(struct token
**, struct symbol
*);
77 static void replace_with_string(struct token
*token
, const char *str
)
79 int size
= strlen(str
) + 1;
80 struct string
*s
= __alloc_string(size
);
83 memcpy(s
->data
, str
, size
);
84 token_type(token
) = TOKEN_STRING
;
88 static void replace_with_integer(struct token
*token
, unsigned int val
)
90 char *buf
= __alloc_bytes(11);
91 sprintf(buf
, "%u", val
);
92 token_type(token
) = TOKEN_NUMBER
;
96 struct symbol
*lookup_macro(struct ident
*ident
)
98 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_INVISIBLEMACRO
);
99 if (sym
&& sym
->namespace != NS_MACRO
)
104 static int token_defined(struct token
*token
)
106 if (token_type(token
) == TOKEN_IDENT
) {
107 struct symbol
*sym
= lookup_macro(token
->ident
);
115 sparse_error(token
->pos
, "expected preprocessor identifier");
119 static void replace_with_defined(struct token
*token
)
121 static const char *string
[] = { "0", "1" };
122 int defined
= token_defined(token
);
124 token_type(token
) = TOKEN_NUMBER
;
125 token
->number
= string
[defined
];
128 static int expand_one_symbol(struct token
**list
)
130 struct token
*token
= *list
;
133 if (token
->pos
.noexpand
)
136 sym
= lookup_macro(token
->ident
);
139 return expand(list
, sym
);
141 if (token
->ident
== &__LINE___ident
) {
142 replace_with_integer(token
, token
->pos
.line
);
143 } else if (token
->ident
== &__FILE___ident
) {
144 replace_with_string(token
, stream_name(token
->pos
.stream
));
149 static inline struct token
*scan_next(struct token
**where
)
151 struct token
*token
= *where
;
152 if (token_type(token
) != TOKEN_UNTAINT
)
155 token
->ident
->tainted
= 0;
157 } while (token_type(token
) == TOKEN_UNTAINT
);
162 static void expand_list(struct token
**list
)
165 while (!eof_token(next
= scan_next(list
))) {
166 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
171 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
173 struct token
**p
= &prev
->next
;
177 while (!eof_token(next
= scan_next(p
))) {
178 if (match_op(next
, '(')) {
180 } else if (match_op(next
, ')')) {
183 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
186 next
->pos
.stream
= pos
->stream
;
187 next
->pos
.line
= pos
->line
;
188 next
->pos
.pos
= pos
->pos
;
191 *p
= &eof_token_entry
;
196 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
201 struct token
*expanded
;
208 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
210 int wanted
= arglist
->count
.normal
;
211 struct token
*next
= NULL
;
214 arglist
= arglist
->next
; /* skip counter */
217 next
= collect_arg(start
, 0, &what
->pos
);
220 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
225 for (count
= 0; count
< wanted
; count
++) {
226 struct argcount
*p
= &arglist
->next
->count
;
227 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
228 arglist
= arglist
->next
->next
;
231 args
[count
].arg
= start
->next
;
232 args
[count
].n_normal
= p
->normal
;
233 args
[count
].n_quoted
= p
->quoted
;
234 args
[count
].n_str
= p
->str
;
235 if (match_op(next
, ')')) {
241 if (count
== wanted
&& !match_op(next
, ')'))
243 if (count
== wanted
- 1) {
244 struct argcount
*p
= &arglist
->next
->count
;
247 args
[count
].arg
= NULL
;
248 args
[count
].n_normal
= p
->normal
;
249 args
[count
].n_quoted
= p
->quoted
;
250 args
[count
].n_str
= p
->str
;
252 if (count
< wanted
- 1)
255 what
->next
= next
->next
;
259 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
260 show_token(what
), wanted
, count
);
263 while (match_op(next
, ',')) {
264 next
= collect_arg(next
, 0, &what
->pos
);
269 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
270 show_token(what
), count
, wanted
);
273 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
276 what
->next
= next
->next
;
280 static struct token
*dup_list(struct token
*list
)
283 struct token
**p
= &res
;
285 while (!eof_token(list
)) {
286 struct token
*newtok
= __alloc_token(0);
295 static struct token
*stringify(struct token
*arg
)
297 const char *s
= show_token_sequence(arg
);
298 int size
= strlen(s
)+1;
299 struct token
*token
= __alloc_token(0);
300 struct string
*string
= __alloc_string(size
);
302 memcpy(string
->data
, s
, size
);
303 string
->length
= size
;
304 token
->pos
= arg
->pos
;
305 token_type(token
) = TOKEN_STRING
;
306 token
->string
= string
;
307 token
->next
= &eof_token_entry
;
311 static void expand_arguments(int count
, struct arg
*args
)
314 for (i
= 0; i
< count
; i
++) {
315 struct token
*arg
= args
[i
].arg
;
317 arg
= &eof_token_entry
;
319 args
[i
].str
= stringify(arg
);
320 if (args
[i
].n_normal
) {
321 if (!args
[i
].n_quoted
) {
322 args
[i
].expanded
= arg
;
324 } else if (eof_token(arg
)) {
325 args
[i
].expanded
= arg
;
327 args
[i
].expanded
= dup_list(arg
);
329 expand_list(&args
[i
].expanded
);
335 * Possibly valid combinations:
336 * - ident + ident -> ident
337 * - ident + number -> ident unless number contains '.', '+' or '-'.
338 * - number + number -> number
339 * - number + ident -> number
340 * - number + '.' -> number
341 * - number + '+' or '-' -> number, if number used to end on [eEpP].
342 * - '.' + number -> number, if number used to start with a digit.
343 * - special + special -> either special or an error.
345 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
348 enum token_type t1
= token_type(left
), t2
= token_type(right
);
350 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
353 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
356 strcpy(p
, show_token(left
));
357 strcat(p
, show_token(right
));
363 if (t1
== TOKEN_IDENT
) {
364 if (t2
== TOKEN_SPECIAL
)
366 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
371 if (t1
== TOKEN_NUMBER
) {
372 if (t2
== TOKEN_SPECIAL
) {
373 switch (right
->special
) {
377 if (strchr("eEpP", p
[len
- 2]))
386 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
389 return TOKEN_SPECIAL
;
392 static int merge(struct token
*left
, struct token
*right
)
394 static char buffer
[512];
397 switch (combine(left
, right
, buffer
)) {
399 left
->ident
= built_in_ident(buffer
);
400 left
->pos
.noexpand
= 0;
404 char *number
= __alloc_bytes(strlen(buffer
) + 1);
405 memcpy(number
, buffer
, strlen(buffer
) + 1);
406 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
407 left
->number
= number
;
412 if (buffer
[2] && buffer
[3])
414 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
415 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
423 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
427 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
429 struct token
*alloc
= alloc_token(streampos
);
430 token_type(alloc
) = token_type(token
);
431 alloc
->pos
.newline
= pos
->newline
;
432 alloc
->pos
.whitespace
= pos
->whitespace
;
433 alloc
->number
= token
->number
;
434 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
438 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
440 int need_copy
= --*count
;
441 while (!eof_token(list
)) {
444 token
= dup_token(list
, &list
->pos
, &list
->pos
);
447 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
448 token
->pos
.noexpand
= 1;
450 where
= &token
->next
;
453 *where
= &eof_token_entry
;
457 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
459 struct token
*token
= *list
;
460 struct position
*base_pos
= &token
->pos
;
461 struct position
*pos
= base_pos
;
463 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
465 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
466 struct token
*added
, *arg
;
469 switch (token_type(body
)) {
470 case TOKEN_GNU_KLUDGE
:
472 * GNU kludge: if we had <comma>##<vararg>, behaviour
473 * depends on whether we had enough arguments to have
474 * a vararg. If we did, ## is just ignored. Otherwise
475 * both , and ## are ignored. Comma should come from
476 * the body of macro and not be an argument of earlier
479 if (!args
[body
->next
->argnum
].arg
)
481 added
= dup_token(body
, base_pos
, pos
);
482 token_type(added
) = TOKEN_SPECIAL
;
486 case TOKEN_STR_ARGUMENT
:
487 arg
= args
[body
->argnum
].str
;
488 count
= &args
[body
->argnum
].n_str
;
491 case TOKEN_QUOTED_ARGUMENT
:
492 arg
= args
[body
->argnum
].arg
;
493 count
= &args
[body
->argnum
].n_quoted
;
494 if (!arg
|| eof_token(arg
)) {
503 case TOKEN_MACRO_ARGUMENT
:
504 arg
= args
[body
->argnum
].expanded
;
505 count
= &args
[body
->argnum
].n_normal
;
506 if (eof_token(arg
)) {
511 tail
= copy(&added
, arg
, count
);
512 added
->pos
.newline
= pos
->newline
;
513 added
->pos
.whitespace
= pos
->whitespace
;
517 if (state
== Placeholder
)
524 added
= dup_token(body
, base_pos
, pos
);
525 if (added
->ident
->tainted
)
526 added
->pos
.noexpand
= 1;
531 added
= dup_token(body
, base_pos
, pos
);
537 * if we got to doing real concatenation, we already have
538 * added something into the list, so containing_token() is OK.
540 if (state
== Concat
&& merge(containing_token(list
), added
)) {
542 if (tail
!= &added
->next
)
550 *list
= &eof_token_entry
;
554 static int expand(struct token
**list
, struct symbol
*sym
)
557 struct token
*token
= *list
;
558 struct ident
*expanding
= token
->ident
;
560 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
561 struct arg args
[nargs
];
563 if (expanding
->tainted
) {
564 token
->pos
.noexpand
= 1;
569 if (!match_op(scan_next(&token
->next
), '('))
571 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
573 expand_arguments(nargs
, args
);
576 expanding
->tainted
= 1;
579 tail
= substitute(list
, sym
->expansion
, args
);
585 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
588 static char buffer
[256];
592 while (!eof_token(token
) && !match_op(token
, endop
)) {
594 const char *val
= token
->string
->data
;
595 if (token_type(token
) != TOKEN_STRING
)
596 val
= show_token(token
);
598 memcpy(ptr
, val
, len
);
603 if (endop
&& !match_op(token
, endop
))
604 sparse_error(start
->pos
, "expected '>' at end of filename");
608 static int already_tokenized(const char *path
)
611 struct stream
*s
= input_streams
;
613 for (i
= input_stream_nr
; --i
>= 0; s
++) {
614 if (s
->constant
!= CONSTANT_FILE_YES
)
616 if (strcmp(path
, s
->name
))
618 if (!lookup_macro(s
->protect
))
625 /* Hande include of header files.
626 * The relevant options are made compatible with gcc. The only options that
627 * are not supported is -withprefix and friends.
629 * Three set of include paths are known:
630 * quote_includepath: Path to search when using #include "file.h"
631 * angle_includepath: Path to search when using #include <file.h>
632 * sys_includepath: Built-in include paths
634 * The above is implmented as one array with pointes
636 * quote_includepath ---> | |
640 * angle_includepath ---> | |
642 * sys_includepath ---> | |
647 * -I dir insert dir just before sys_includepath and move the rest
648 * -I- makes all dirs specified with -I before to quote dirs only and
649 * angle_includepath is set equal to sys_includepath.
650 * -nostdinc removes all sys dirs be storing NULL in entry pointed
651 * to by * sys_includepath. Note this will reset all dirs built-in and added
652 * before -nostdinc by -isystem and -dirafter
653 * -isystem dir adds dir where sys_includepath points adding this dir as
655 * -dirafter dir adds dir to the end of the list
658 static void set_stream_include_path(struct stream
*stream
)
660 const char *path
= stream
->path
;
662 const char *p
= strrchr(stream
->name
, '/');
665 int len
= p
- stream
->name
+ 1;
666 char *m
= malloc(len
+1);
667 /* This includes the final "/" */
668 memcpy(m
, stream
->name
, len
);
674 includepath
[0] = path
;
677 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
680 int plen
= strlen(path
);
681 static char fullname
[PATH_MAX
];
683 memcpy(fullname
, path
, plen
);
684 if (plen
&& path
[plen
-1] != '/') {
685 fullname
[plen
] = '/';
688 memcpy(fullname
+plen
, filename
, flen
);
689 if (already_tokenized(fullname
))
691 fd
= open(fullname
, O_RDONLY
);
693 char * streamname
= __alloc_bytes(plen
+ flen
);
694 memcpy(streamname
, fullname
, plen
+ flen
);
695 *where
= tokenize(streamname
, fd
, *where
, next_path
);
702 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
706 while ((path
= *pptr
++) != NULL
) {
707 if (!try_include(path
, filename
, flen
, list
, pptr
))
714 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
716 int flen
= strlen(filename
) + 1;
719 if (filename
[0] == '/') {
720 if (try_include("", filename
, flen
, list
, includepath
))
725 /* Dir of inputfile is first dir to search for quoted includes */
726 set_stream_include_path(stream
);
729 /* Do not search quote include if <> is in use */
730 path
= local
? quote_includepath
: angle_includepath
;
732 /* Check the standard include paths.. */
733 if (do_include_path(path
, list
, token
, filename
, flen
))
736 error_die(token
->pos
, "unable to open '%s'", filename
);
739 static int free_preprocessor_line(struct token
*token
)
742 struct token
*free
= token
;
745 } while (token_type(token
) != TOKEN_EOF
);
749 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
751 const char *filename
;
756 return free_preprocessor_line(token
);
758 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
759 MARK_STREAM_NONCONST(token
->pos
);
763 if (!match_op(next
, '<')) {
764 expand_list(&token
->next
);
767 if (match_op(token
->next
, '<')) {
773 filename
= token_name_sequence(token
, expect
, token
);
774 do_include(!expect
, stream
, list
, token
, filename
, path
);
778 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
780 return handle_include_path(stream
, list
, token
, NULL
);
783 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
785 return handle_include_path(stream
, list
, token
, stream
->next_path
);
788 static int token_different(struct token
*t1
, struct token
*t2
)
792 if (token_type(t1
) != token_type(t2
))
795 switch (token_type(t1
)) {
797 different
= t1
->ident
!= t2
->ident
;
799 case TOKEN_ARG_COUNT
:
802 case TOKEN_GNU_KLUDGE
:
806 different
= strcmp(t1
->number
, t2
->number
);
809 different
= t1
->special
!= t2
->special
;
811 case TOKEN_MACRO_ARGUMENT
:
812 case TOKEN_QUOTED_ARGUMENT
:
813 case TOKEN_STR_ARGUMENT
:
814 different
= t1
->argnum
!= t2
->argnum
;
817 different
= t1
->character
!= t2
->character
;
820 struct string
*s1
, *s2
;
825 if (s1
->length
!= s2
->length
)
827 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
837 static int token_list_different(struct token
*list1
, struct token
*list2
)
842 if (!list1
|| !list2
)
844 if (token_different(list1
, list2
))
851 static inline void set_arg_count(struct token
*token
)
853 token_type(token
) = TOKEN_ARG_COUNT
;
854 token
->count
.normal
= token
->count
.quoted
=
855 token
->count
.str
= token
->count
.vararg
= 0;
858 static struct token
*parse_arguments(struct token
*list
)
860 struct token
*arg
= list
->next
, *next
= list
;
861 struct argcount
*count
= &list
->count
;
865 if (match_op(arg
, ')')) {
867 list
->next
= &eof_token_entry
;
871 while (token_type(arg
) == TOKEN_IDENT
) {
872 if (arg
->ident
== &__VA_ARGS___ident
)
874 if (!++count
->normal
)
878 if (match_op(next
, ',')) {
884 if (match_op(next
, ')')) {
887 arg
->next
->next
= &eof_token_entry
;
891 /* normal cases are finished here */
893 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
894 if (match_op(next
->next
, ')')) {
896 next
->count
.vararg
= 1;
898 arg
->next
->next
= &eof_token_entry
;
906 if (eof_token(next
)) {
914 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
916 token_type(arg
) = TOKEN_IDENT
;
917 arg
->ident
= &__VA_ARGS___ident
;
918 if (!match_op(next
, ')'))
920 if (!++count
->normal
)
923 next
->count
.vararg
= 1;
925 arg
->next
->next
= &eof_token_entry
;
929 if (eof_token(arg
)) {
933 if (match_op(arg
, ','))
940 sparse_error(arg
->pos
, "parameter name missing");
943 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
947 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
950 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
953 sparse_error(arg
->pos
, "too many arguments in macro definition");
957 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
959 struct ident
*ident
= token
->ident
;
962 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
965 arglist
= arglist
->next
;
967 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
968 if (arglist
->ident
== ident
) {
969 struct argcount
*count
= &arglist
->next
->count
;
973 token_type(token
) = type
;
975 case TOKEN_MACRO_ARGUMENT
:
978 case TOKEN_QUOTED_ARGUMENT
:
985 return count
->vararg
? 2 : 1;
986 token_type(token
) = TOKEN_ERROR
;
993 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
995 struct token
*token
= expansion
;
997 struct token
*last
= NULL
;
999 if (match_op(token
, SPECIAL_HASHHASH
))
1002 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1003 if (match_op(token
, '#')) {
1005 struct token
*next
= token
->next
;
1006 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1008 next
->pos
.whitespace
= token
->pos
.whitespace
;
1011 token
->pos
.noexpand
= 1;
1013 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
1014 struct token
*next
= token
->next
;
1015 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1016 token_type(token
) = TOKEN_CONCAT
;
1020 if (arg
== 2 && last
&& match_op(last
, ',')) {
1021 token_type(last
) = TOKEN_GNU_KLUDGE
;
1024 } else if (match_op(next
, SPECIAL_HASHHASH
))
1026 else if (eof_token(next
))
1028 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1029 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1031 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1033 if (token_type(token
) == TOKEN_ERROR
)
1037 token
= alloc_token(&expansion
->pos
);
1038 token_type(token
) = TOKEN_UNTAINT
;
1039 token
->ident
= name
;
1045 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1049 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1052 sparse_error(token
->pos
, "too many instances of argument in body");
1056 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int weak
)
1058 struct token
*arglist
, *expansion
;
1059 struct token
*left
= token
->next
;
1064 return free_preprocessor_line(token
);
1066 if (token_type(left
) != TOKEN_IDENT
) {
1067 sparse_error(token
->pos
, "expected identifier to 'define'");
1071 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1072 MARK_STREAM_NONCONST(token
->pos
);
1074 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1078 expansion
= left
->next
;
1079 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
1080 arglist
= expansion
;
1081 expansion
= parse_arguments(expansion
);
1086 expansion
= parse_expansion(expansion
, arglist
, name
);
1090 sym
= lookup_macro(name
);
1092 if (token_list_different(sym
->expansion
, expansion
) ||
1093 token_list_different(sym
->arglist
, arglist
)) {
1098 warning(left
->pos
, "preprocessor token %.*s redefined",
1099 name
->len
, name
->name
);
1100 info(sym
->pos
, "this was the original definition");
1102 /* Don't overwrite global defs */
1103 if (sym
->scope
!= file_scope
)
1105 sym
->expansion
= expansion
;
1106 sym
->arglist
= arglist
;
1111 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1112 bind_symbol(sym
, name
, NS_MACRO
);
1115 sym
->expansion
= expansion
;
1116 sym
->arglist
= arglist
;
1121 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1123 return do_handle_define(stream
, line
, token
, 0);
1126 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1128 return do_handle_define(stream
, line
, token
, 1);
1131 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1133 struct token
*left
= token
->next
;
1134 struct symbol
**sym
;
1137 return free_preprocessor_line(token
);
1139 if (token_type(left
) != TOKEN_IDENT
) {
1140 sparse_error(token
->pos
, "expected identifier to 'undef'");
1144 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1145 MARK_STREAM_NONCONST(token
->pos
);
1147 sym
= &left
->ident
->symbols
;
1149 struct symbol
*t
= *sym
;
1150 if (t
->namespace & (NS_MACRO
| NS_INVISIBLEMACRO
)) {
1151 t
->namespace = NS_INVISIBLEMACRO
;
1156 return free_preprocessor_line(token
);
1159 static int preprocessor_if(struct token
*token
, int true)
1161 if (if_nesting
== 0)
1162 unmatched_if_pos
= token
->pos
;
1163 if (if_nesting
>= MAX_NEST
)
1164 error_die(token
->pos
, "Maximum preprocessor conditional level exhausted");
1165 elif_ignore
[if_nesting
++] = (false_nesting
|| true) ? ELIF_IGNORE
: 0;
1166 if (false_nesting
|| true != 1)
1168 return free_preprocessor_line(token
);
1171 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1173 struct token
*next
= token
->next
;
1175 if (token_type(next
) == TOKEN_IDENT
) {
1176 arg
= token_defined(next
);
1179 sparse_error(token
->pos
, "expected preprocessor identifier");
1182 return preprocessor_if(token
, arg
);
1185 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1187 struct token
*next
= token
->next
;
1189 if (token_type(next
) == TOKEN_IDENT
) {
1190 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1191 if (!stream
->protect
|| stream
->protect
== next
->ident
) {
1192 stream
->constant
= CONSTANT_FILE_IFNDEF
;
1193 stream
->protect
= next
->ident
;
1195 MARK_STREAM_NONCONST(token
->pos
);
1197 arg
= !token_defined(next
);
1199 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1200 MARK_STREAM_NONCONST(token
->pos
);
1202 sparse_error(token
->pos
, "expected preprocessor identifier");
1206 return preprocessor_if(token
, arg
);
1210 * Expression handling for #if and #elif; it differs from normal expansion
1211 * due to special treatment of "defined".
1213 static int expression_value(struct token
**where
)
1215 struct expression
*expr
;
1217 struct token
**list
= where
, **beginning
= NULL
;
1221 while (!eof_token(p
= scan_next(list
))) {
1224 if (token_type(p
) != TOKEN_IDENT
)
1226 if (p
->ident
== &defined_ident
) {
1231 if (!expand_one_symbol(list
))
1233 if (token_type(p
) != TOKEN_IDENT
)
1235 token_type(p
) = TOKEN_ZERO_IDENT
;
1238 if (match_op(p
, '(')) {
1242 replace_with_defined(p
);
1247 if (token_type(p
) == TOKEN_IDENT
)
1251 replace_with_defined(p
);
1256 if (!match_op(p
, ')'))
1257 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1264 p
= constant_expression(*where
, &expr
);
1266 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1267 value
= get_expression_value(expr
);
1271 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1275 value
= expression_value(&token
->next
);
1277 // This is an approximation. We really only need this if the
1278 // condition does depends on a pre-processor symbol. Note, that
1279 // the important #ifndef case has already changed ->constant.
1280 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1281 MARK_STREAM_NONCONST(token
->pos
);
1283 return preprocessor_if(token
, value
);
1286 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1288 if (stream
->nesting
== if_nesting
)
1289 MARK_STREAM_NONCONST(token
->pos
);
1291 if (stream
->nesting
> if_nesting
)
1292 sparse_error(token
->pos
, "unmatched #elif within stream");
1294 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1295 sparse_error(token
->pos
, "#elif after #else");
1297 if (false_nesting
) {
1298 /* If this whole if-thing is if'ed out, an elif cannot help */
1299 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1301 if (expression_value(&token
->next
)) {
1303 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1308 return free_preprocessor_line(token
);
1311 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1313 if (stream
->nesting
== if_nesting
)
1314 MARK_STREAM_NONCONST(token
->pos
);
1316 if (stream
->nesting
> if_nesting
)
1317 sparse_error(token
->pos
, "unmatched #else within stream");
1319 if (elif_ignore
[if_nesting
-1] & ELIF_SEEN_ELSE
)
1320 sparse_error(token
->pos
, "#else after #else");
1322 elif_ignore
[if_nesting
-1] |= ELIF_SEEN_ELSE
;
1324 if (false_nesting
) {
1325 /* If this whole if-thing is if'ed out, an else cannot help */
1326 if (elif_ignore
[if_nesting
-1] & ELIF_IGNORE
)
1329 elif_ignore
[if_nesting
-1] |= ELIF_IGNORE
;
1333 return free_preprocessor_line(token
);
1336 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1338 if (stream
->constant
== CONSTANT_FILE_IFNDEF
&& stream
->nesting
== if_nesting
)
1339 stream
->constant
= CONSTANT_FILE_MAYBE
;
1341 if (stream
->nesting
> if_nesting
)
1342 sparse_error(token
->pos
, "unmatched #endif in stream");
1348 return free_preprocessor_line(token
);
1351 static const char *show_token_sequence(struct token
*token
)
1353 static char buffer
[1024];
1359 while (!eof_token(token
)) {
1360 const char *val
= show_token(token
);
1361 int len
= strlen(val
);
1363 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1364 sparse_error(token
->pos
, "too long token expansion");
1370 memcpy(ptr
, val
, len
);
1372 token
= token
->next
;
1373 whitespace
= token
->pos
.whitespace
;
1379 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1382 return free_preprocessor_line(token
);
1383 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1384 MARK_STREAM_NONCONST(token
->pos
);
1385 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1386 return free_preprocessor_line(token
);
1389 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1392 return free_preprocessor_line(token
);
1393 if (stream
->constant
== CONSTANT_FILE_MAYBE
)
1394 MARK_STREAM_NONCONST(token
->pos
);
1395 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
));
1396 return free_preprocessor_line(token
);
1399 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1402 return free_preprocessor_line(token
);
1405 * Do we have any non-system includes?
1406 * Clear them out if so..
1408 *sys_includepath
= NULL
;
1409 return free_preprocessor_line(token
);
1412 static void add_path_entry(struct token
*token
, const char *path
, const char ***where
, const char **new_path
)
1417 /* Need one free entry.. */
1418 if (includepath
[INCLUDEPATHS
-2])
1419 error_die(token
->pos
, "too many include path entries");
1421 /* check that this is not a duplicate */
1424 if (strcmp(*dst
, path
) == 0)
1433 * Move them all up starting at dst,
1434 * insert the new entry..
1437 const char *tmp
= *dst
;
1446 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1449 token
= token
->next
;
1450 if (eof_token(token
))
1452 if (token_type(token
) != TOKEN_STRING
) {
1453 warning(token
->pos
, "expected path string");
1456 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
+ 1);
1460 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1463 token
= token
->next
;
1464 if (eof_token(token
))
1466 if (token_type(token
) != TOKEN_STRING
) {
1467 sparse_error(token
->pos
, "expected path string");
1470 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
);
1474 /* Add to end on includepath list - no pointer updates */
1475 static void add_dirafter_entry(struct token
*token
, const char *path
)
1477 const char **dst
= includepath
;
1479 /* Need one free entry.. */
1480 if (includepath
[INCLUDEPATHS
-2])
1481 error_die(token
->pos
, "too many include path entries");
1483 /* Add to the end */
1491 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1494 token
= token
->next
;
1495 if (eof_token(token
))
1497 if (token_type(token
) != TOKEN_STRING
) {
1498 sparse_error(token
->pos
, "expected path string");
1501 add_dirafter_entry(token
, token
->string
->data
);
1505 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1508 return free_preprocessor_line(token
);
1513 * Split the include path. Any directories specified with `-I'
1514 * options before `-I-' are searched only for headers requested with
1515 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1516 * If additional directories are specified with `-I' options after
1517 * the `-I-', those directories are searched for all `#include'
1519 * In addition, `-I-' inhibits the use of the directory of the current
1520 * file directory as the first search directory for `#include "FILE"'.
1522 quote_includepath
= includepath
+1;
1523 angle_includepath
= sys_includepath
;
1524 return free_preprocessor_line(token
);
1528 * We replace "#pragma xxx" with "__pragma__" in the token
1529 * stream. Just as an example.
1531 * We'll just #define that away for now, but the theory here
1532 * is that we can use this to insert arbitrary token sequences
1533 * to turn the pragma's into internal front-end sequences for
1534 * when we actually start caring about them.
1536 * So eventually this will turn into some kind of extended
1537 * __attribute__() like thing, except called __pragma__(xxx).
1539 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1541 struct token
*next
= *line
;
1543 token
->ident
= &pragma_ident
;
1544 token
->pos
.newline
= 1;
1545 token
->pos
.whitespace
= 1;
1553 * We ignore #line for now.
1555 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1561 static void init_preprocessor(void)
1564 int stream
= init_stream("preprocessor", -1, includepath
);
1567 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1569 { "define", handle_define
},
1570 { "weak_define",handle_weak_define
},
1571 { "undef", handle_undef
},
1572 { "ifdef", handle_ifdef
},
1573 { "ifndef", handle_ifndef
},
1574 { "else", handle_else
},
1575 { "endif", handle_endif
},
1576 { "if", handle_if
},
1577 { "elif", handle_elif
},
1578 { "warning", handle_warning
},
1579 { "error", handle_error
},
1580 { "include", handle_include
},
1581 { "include_next",handle_include_next
},
1582 { "pragma", handle_pragma
},
1583 { "line", handle_line
},
1585 // our internal preprocessor tokens
1586 { "nostdinc", handle_nostdinc
},
1587 { "add_include", handle_add_include
},
1588 { "add_isystem", handle_add_isystem
},
1589 { "add_dirafter", handle_add_dirafter
},
1590 { "split_include", handle_split_include
},
1593 for (i
= 0; i
< (sizeof (handlers
) / sizeof (handlers
[0])); i
++) {
1595 sym
= create_symbol(stream
, handlers
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1596 sym
->handler
= handlers
[i
].handler
;
1600 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1602 struct token
*token
= start
->next
;
1607 if (token_type(token
) == TOKEN_NUMBER
)
1608 if (handle_line(stream
, line
, start
))
1611 if (token_type(token
) == TOKEN_IDENT
) {
1612 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1613 if (sym
&& sym
->handler(stream
, line
, token
))
1620 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1623 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1625 struct token
*start
= *line
, *next
;
1626 struct token
**tp
= &start
->next
;
1630 if (next
->pos
.newline
)
1635 *tp
= &eof_token_entry
;
1636 handle_preprocessor_line(stream
, line
, start
);
1639 static void do_preprocess(struct token
**list
)
1643 while (!eof_token(next
= scan_next(list
))) {
1644 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1646 if (next
->pos
.newline
&& match_op(next
, '#')) {
1647 if (!next
->pos
.noexpand
) {
1648 preprocessor_line(stream
, list
);
1649 __free_token(next
); /* Free the '#' token */
1654 switch (token_type(next
)) {
1655 case TOKEN_STREAMEND
:
1656 if (stream
->nesting
< if_nesting
+ 1) {
1657 sparse_error(unmatched_if_pos
, "unterminated preprocessor conditional");
1658 // Pretend to see a series of #endifs
1659 MARK_STREAM_NONCONST(next
->pos
);
1661 handle_endif (stream
, NULL
, NULL
);
1662 } while (stream
->nesting
< if_nesting
+ 1);
1664 if (stream
->constant
== CONSTANT_FILE_MAYBE
&& stream
->protect
) {
1665 stream
->constant
= CONSTANT_FILE_YES
;
1669 case TOKEN_STREAMBEGIN
:
1670 stream
->nesting
= if_nesting
+ 1;
1675 if (false_nesting
) {
1681 if (token_type(next
) != TOKEN_IDENT
||
1682 expand_one_symbol(list
))
1686 if (stream
->constant
== CONSTANT_FILE_MAYBE
) {
1688 * Any token expansion (even if it ended up being an
1689 * empty expansion) in this stream implies it can't
1692 MARK_STREAM_NONCONST(next
->pos
);
1697 struct token
* preprocess(struct token
*token
)
1700 init_preprocessor();
1701 do_preprocess(&token
);
1703 // Drop all expressions from pre-processing, they're not used any more.
1704 // This is not true when we have multiple files, though ;/
1705 // clear_expression_alloc();