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;
33 #define INCLUDEPATHS 300
34 const char *includepath
[INCLUDEPATHS
+1] = {
42 static const char **quote_includepath
= includepath
;
43 static const char **angle_includepath
= includepath
+ 1;
44 static const char **sys_includepath
= includepath
+ 1;
46 #define dirty_stream(stream) \
47 do if (!stream->dirty) { \
49 if (!stream->ifndef) \
50 stream->protect = NULL; \
53 #define end_group(stream) \
54 do if (stream->ifndef == stream->top_if) { \
55 stream->ifndef = NULL; \
57 stream->protect = NULL; \
58 else if (stream->protect) \
62 #define nesting_error(stream) \
65 stream->ifndef = NULL; \
66 stream->protect = NULL; \
69 static struct token
*alloc_token(struct position
*pos
)
71 struct token
*token
= __alloc_token(0);
73 token
->pos
.stream
= pos
->stream
;
74 token
->pos
.line
= pos
->line
;
75 token
->pos
.pos
= pos
->pos
;
76 token
->pos
.whitespace
= 1;
80 static const char *show_token_sequence(struct token
*token
);
82 /* Expand symbol 'sym' at '*list' */
83 static int expand(struct token
**, struct symbol
*);
85 static void replace_with_string(struct token
*token
, const char *str
)
87 int size
= strlen(str
) + 1;
88 struct string
*s
= __alloc_string(size
);
91 memcpy(s
->data
, str
, size
);
92 token_type(token
) = TOKEN_STRING
;
96 static void replace_with_integer(struct token
*token
, unsigned int val
)
98 char *buf
= __alloc_bytes(11);
99 sprintf(buf
, "%u", val
);
100 token_type(token
) = TOKEN_NUMBER
;
104 static struct symbol
*lookup_macro(struct ident
*ident
)
106 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_INVISIBLEMACRO
);
107 if (sym
&& sym
->namespace != NS_MACRO
)
112 static int token_defined(struct token
*token
)
114 if (token_type(token
) == TOKEN_IDENT
) {
115 struct symbol
*sym
= lookup_macro(token
->ident
);
123 sparse_error(token
->pos
, "expected preprocessor identifier");
127 static void replace_with_defined(struct token
*token
)
129 static const char *string
[] = { "0", "1" };
130 int defined
= token_defined(token
);
132 token_type(token
) = TOKEN_NUMBER
;
133 token
->number
= string
[defined
];
136 static int expand_one_symbol(struct token
**list
)
138 struct token
*token
= *list
;
141 if (token
->pos
.noexpand
)
144 sym
= lookup_macro(token
->ident
);
147 return expand(list
, sym
);
149 if (token
->ident
== &__LINE___ident
) {
150 replace_with_integer(token
, token
->pos
.line
);
151 } else if (token
->ident
== &__FILE___ident
) {
152 replace_with_string(token
, stream_name(token
->pos
.stream
));
157 static inline struct token
*scan_next(struct token
**where
)
159 struct token
*token
= *where
;
160 if (token_type(token
) != TOKEN_UNTAINT
)
163 token
->ident
->tainted
= 0;
165 } while (token_type(token
) == TOKEN_UNTAINT
);
170 static void expand_list(struct token
**list
)
173 while (!eof_token(next
= scan_next(list
))) {
174 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
179 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
181 struct token
**p
= &prev
->next
;
185 while (!eof_token(next
= scan_next(p
))) {
186 if (match_op(next
, '(')) {
188 } else if (match_op(next
, ')')) {
191 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
194 next
->pos
.stream
= pos
->stream
;
195 next
->pos
.line
= pos
->line
;
196 next
->pos
.pos
= pos
->pos
;
199 *p
= &eof_token_entry
;
204 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
209 struct token
*expanded
;
216 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
218 int wanted
= arglist
->count
.normal
;
219 struct token
*next
= NULL
;
222 arglist
= arglist
->next
; /* skip counter */
225 next
= collect_arg(start
, 0, &what
->pos
);
228 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
233 for (count
= 0; count
< wanted
; count
++) {
234 struct argcount
*p
= &arglist
->next
->count
;
235 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
236 arglist
= arglist
->next
->next
;
239 args
[count
].arg
= start
->next
;
240 args
[count
].n_normal
= p
->normal
;
241 args
[count
].n_quoted
= p
->quoted
;
242 args
[count
].n_str
= p
->str
;
243 if (match_op(next
, ')')) {
249 if (count
== wanted
&& !match_op(next
, ')'))
251 if (count
== wanted
- 1) {
252 struct argcount
*p
= &arglist
->next
->count
;
255 args
[count
].arg
= NULL
;
256 args
[count
].n_normal
= p
->normal
;
257 args
[count
].n_quoted
= p
->quoted
;
258 args
[count
].n_str
= p
->str
;
260 if (count
< wanted
- 1)
263 what
->next
= next
->next
;
267 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
268 show_token(what
), wanted
, count
);
271 while (match_op(next
, ',')) {
272 next
= collect_arg(next
, 0, &what
->pos
);
277 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
278 show_token(what
), count
, wanted
);
281 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
284 what
->next
= next
->next
;
288 static struct token
*dup_list(struct token
*list
)
291 struct token
**p
= &res
;
293 while (!eof_token(list
)) {
294 struct token
*newtok
= __alloc_token(0);
303 static struct token
*stringify(struct token
*arg
)
305 const char *s
= show_token_sequence(arg
);
306 int size
= strlen(s
)+1;
307 struct token
*token
= __alloc_token(0);
308 struct string
*string
= __alloc_string(size
);
310 memcpy(string
->data
, s
, size
);
311 string
->length
= size
;
312 token
->pos
= arg
->pos
;
313 token_type(token
) = TOKEN_STRING
;
314 token
->string
= string
;
315 token
->next
= &eof_token_entry
;
319 static void expand_arguments(int count
, struct arg
*args
)
322 for (i
= 0; i
< count
; i
++) {
323 struct token
*arg
= args
[i
].arg
;
325 arg
= &eof_token_entry
;
327 args
[i
].str
= stringify(arg
);
328 if (args
[i
].n_normal
) {
329 if (!args
[i
].n_quoted
) {
330 args
[i
].expanded
= arg
;
332 } else if (eof_token(arg
)) {
333 args
[i
].expanded
= arg
;
335 args
[i
].expanded
= dup_list(arg
);
337 expand_list(&args
[i
].expanded
);
343 * Possibly valid combinations:
344 * - ident + ident -> ident
345 * - ident + number -> ident unless number contains '.', '+' or '-'.
346 * - number + number -> number
347 * - number + ident -> number
348 * - number + '.' -> number
349 * - number + '+' or '-' -> number, if number used to end on [eEpP].
350 * - '.' + number -> number, if number used to start with a digit.
351 * - special + special -> either special or an error.
353 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
356 enum token_type t1
= token_type(left
), t2
= token_type(right
);
358 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
361 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
364 strcpy(p
, show_token(left
));
365 strcat(p
, show_token(right
));
371 if (t1
== TOKEN_IDENT
) {
372 if (t2
== TOKEN_SPECIAL
)
374 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
379 if (t1
== TOKEN_NUMBER
) {
380 if (t2
== TOKEN_SPECIAL
) {
381 switch (right
->special
) {
385 if (strchr("eEpP", p
[len
- 2]))
394 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
397 return TOKEN_SPECIAL
;
400 static int merge(struct token
*left
, struct token
*right
)
402 static char buffer
[512];
405 switch (combine(left
, right
, buffer
)) {
407 left
->ident
= built_in_ident(buffer
);
408 left
->pos
.noexpand
= 0;
412 char *number
= __alloc_bytes(strlen(buffer
) + 1);
413 memcpy(number
, buffer
, strlen(buffer
) + 1);
414 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
415 left
->number
= number
;
420 if (buffer
[2] && buffer
[3])
422 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
423 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
431 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
435 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
437 struct token
*alloc
= alloc_token(streampos
);
438 token_type(alloc
) = token_type(token
);
439 alloc
->pos
.newline
= pos
->newline
;
440 alloc
->pos
.whitespace
= pos
->whitespace
;
441 alloc
->number
= token
->number
;
442 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
446 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
448 int need_copy
= --*count
;
449 while (!eof_token(list
)) {
452 token
= dup_token(list
, &list
->pos
, &list
->pos
);
455 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
456 token
->pos
.noexpand
= 1;
458 where
= &token
->next
;
461 *where
= &eof_token_entry
;
465 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
467 struct token
*token
= *list
;
468 struct position
*base_pos
= &token
->pos
;
469 struct position
*pos
= base_pos
;
471 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
473 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
474 struct token
*added
, *arg
;
477 switch (token_type(body
)) {
478 case TOKEN_GNU_KLUDGE
:
480 * GNU kludge: if we had <comma>##<vararg>, behaviour
481 * depends on whether we had enough arguments to have
482 * a vararg. If we did, ## is just ignored. Otherwise
483 * both , and ## are ignored. Comma should come from
484 * the body of macro and not be an argument of earlier
487 if (!args
[body
->next
->argnum
].arg
)
489 added
= dup_token(body
, base_pos
, pos
);
490 token_type(added
) = TOKEN_SPECIAL
;
494 case TOKEN_STR_ARGUMENT
:
495 arg
= args
[body
->argnum
].str
;
496 count
= &args
[body
->argnum
].n_str
;
499 case TOKEN_QUOTED_ARGUMENT
:
500 arg
= args
[body
->argnum
].arg
;
501 count
= &args
[body
->argnum
].n_quoted
;
502 if (!arg
|| eof_token(arg
)) {
511 case TOKEN_MACRO_ARGUMENT
:
512 arg
= args
[body
->argnum
].expanded
;
513 count
= &args
[body
->argnum
].n_normal
;
514 if (eof_token(arg
)) {
519 tail
= copy(&added
, arg
, count
);
520 added
->pos
.newline
= pos
->newline
;
521 added
->pos
.whitespace
= pos
->whitespace
;
525 if (state
== Placeholder
)
532 added
= dup_token(body
, base_pos
, pos
);
533 if (added
->ident
->tainted
)
534 added
->pos
.noexpand
= 1;
539 added
= dup_token(body
, base_pos
, pos
);
545 * if we got to doing real concatenation, we already have
546 * added something into the list, so containing_token() is OK.
548 if (state
== Concat
&& merge(containing_token(list
), added
)) {
550 if (tail
!= &added
->next
)
558 *list
= &eof_token_entry
;
562 static int expand(struct token
**list
, struct symbol
*sym
)
565 struct token
*token
= *list
;
566 struct ident
*expanding
= token
->ident
;
568 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
569 struct arg args
[nargs
];
571 if (expanding
->tainted
) {
572 token
->pos
.noexpand
= 1;
577 if (!match_op(scan_next(&token
->next
), '('))
579 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
581 expand_arguments(nargs
, args
);
584 expanding
->tainted
= 1;
587 tail
= substitute(list
, sym
->expansion
, args
);
593 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
596 static char buffer
[256];
600 while (!eof_token(token
) && !match_op(token
, endop
)) {
602 const char *val
= token
->string
->data
;
603 if (token_type(token
) != TOKEN_STRING
)
604 val
= show_token(token
);
606 memcpy(ptr
, val
, len
);
611 if (endop
&& !match_op(token
, endop
))
612 sparse_error(start
->pos
, "expected '>' at end of filename");
616 static int already_tokenized(const char *path
)
619 struct stream
*s
= input_streams
;
621 for (i
= input_stream_nr
; --i
>= 0; s
++) {
622 if (s
->constant
!= CONSTANT_FILE_YES
)
624 if (strcmp(path
, s
->name
))
626 if (s
->protect
&& !lookup_macro(s
->protect
))
633 /* Handle include of header files.
634 * The relevant options are made compatible with gcc. The only options that
635 * are not supported is -withprefix and friends.
637 * Three set of include paths are known:
638 * quote_includepath: Path to search when using #include "file.h"
639 * angle_includepath: Path to search when using #include <file.h>
640 * sys_includepath: Built-in include paths
642 * The above is implmented as one array with pointes
644 * quote_includepath ---> | |
648 * angle_includepath ---> | |
650 * sys_includepath ---> | |
655 * -I dir insert dir just before sys_includepath and move the rest
656 * -I- makes all dirs specified with -I before to quote dirs only and
657 * angle_includepath is set equal to sys_includepath.
658 * -nostdinc removes all sys dirs be storing NULL in entry pointed
659 * to by * sys_includepath. Note this will reset all dirs built-in and added
660 * before -nostdinc by -isystem and -dirafter
661 * -isystem dir adds dir where sys_includepath points adding this dir as
663 * -dirafter dir adds dir to the end of the list
666 static void set_stream_include_path(struct stream
*stream
)
668 const char *path
= stream
->path
;
670 const char *p
= strrchr(stream
->name
, '/');
673 int len
= p
- stream
->name
+ 1;
674 char *m
= malloc(len
+1);
675 /* This includes the final "/" */
676 memcpy(m
, stream
->name
, len
);
682 includepath
[0] = path
;
685 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
688 int plen
= strlen(path
);
689 static char fullname
[PATH_MAX
];
691 memcpy(fullname
, path
, plen
);
692 if (plen
&& path
[plen
-1] != '/') {
693 fullname
[plen
] = '/';
696 memcpy(fullname
+plen
, filename
, flen
);
697 if (already_tokenized(fullname
))
699 fd
= open(fullname
, O_RDONLY
);
701 char * streamname
= __alloc_bytes(plen
+ flen
);
702 memcpy(streamname
, fullname
, plen
+ flen
);
703 *where
= tokenize(streamname
, fd
, *where
, next_path
);
710 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
714 while ((path
= *pptr
++) != NULL
) {
715 if (!try_include(path
, filename
, flen
, list
, pptr
))
722 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
724 int flen
= strlen(filename
) + 1;
727 if (filename
[0] == '/') {
728 if (try_include("", filename
, flen
, list
, includepath
))
733 /* Dir of inputfile is first dir to search for quoted includes */
734 set_stream_include_path(stream
);
737 /* Do not search quote include if <> is in use */
738 path
= local
? quote_includepath
: angle_includepath
;
740 /* Check the standard include paths.. */
741 if (do_include_path(path
, list
, token
, filename
, flen
))
744 error_die(token
->pos
, "unable to open '%s'", filename
);
747 static int free_preprocessor_line(struct token
*token
)
750 struct token
*free
= token
;
753 } while (token_type(token
) != TOKEN_EOF
);
757 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
759 const char *filename
;
765 if (!match_op(next
, '<')) {
766 expand_list(&token
->next
);
769 if (match_op(token
->next
, '<')) {
775 filename
= token_name_sequence(token
, expect
, token
);
776 do_include(!expect
, stream
, list
, token
, filename
, path
);
780 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
782 return handle_include_path(stream
, list
, token
, NULL
);
785 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
787 return handle_include_path(stream
, list
, token
, stream
->next_path
);
790 static int token_different(struct token
*t1
, struct token
*t2
)
794 if (token_type(t1
) != token_type(t2
))
797 switch (token_type(t1
)) {
799 different
= t1
->ident
!= t2
->ident
;
801 case TOKEN_ARG_COUNT
:
804 case TOKEN_GNU_KLUDGE
:
808 different
= strcmp(t1
->number
, t2
->number
);
811 different
= t1
->special
!= t2
->special
;
813 case TOKEN_MACRO_ARGUMENT
:
814 case TOKEN_QUOTED_ARGUMENT
:
815 case TOKEN_STR_ARGUMENT
:
816 different
= t1
->argnum
!= t2
->argnum
;
819 different
= t1
->character
!= t2
->character
;
822 struct string
*s1
, *s2
;
827 if (s1
->length
!= s2
->length
)
829 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
839 static int token_list_different(struct token
*list1
, struct token
*list2
)
844 if (!list1
|| !list2
)
846 if (token_different(list1
, list2
))
853 static inline void set_arg_count(struct token
*token
)
855 token_type(token
) = TOKEN_ARG_COUNT
;
856 token
->count
.normal
= token
->count
.quoted
=
857 token
->count
.str
= token
->count
.vararg
= 0;
860 static struct token
*parse_arguments(struct token
*list
)
862 struct token
*arg
= list
->next
, *next
= list
;
863 struct argcount
*count
= &list
->count
;
867 if (match_op(arg
, ')')) {
869 list
->next
= &eof_token_entry
;
873 while (token_type(arg
) == TOKEN_IDENT
) {
874 if (arg
->ident
== &__VA_ARGS___ident
)
876 if (!++count
->normal
)
880 if (match_op(next
, ',')) {
886 if (match_op(next
, ')')) {
889 arg
->next
->next
= &eof_token_entry
;
893 /* normal cases are finished here */
895 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
896 if (match_op(next
->next
, ')')) {
898 next
->count
.vararg
= 1;
900 arg
->next
->next
= &eof_token_entry
;
908 if (eof_token(next
)) {
916 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
918 token_type(arg
) = TOKEN_IDENT
;
919 arg
->ident
= &__VA_ARGS___ident
;
920 if (!match_op(next
, ')'))
922 if (!++count
->normal
)
925 next
->count
.vararg
= 1;
927 arg
->next
->next
= &eof_token_entry
;
931 if (eof_token(arg
)) {
935 if (match_op(arg
, ','))
942 sparse_error(arg
->pos
, "parameter name missing");
945 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
949 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
952 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
955 sparse_error(arg
->pos
, "too many arguments in macro definition");
959 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
961 struct ident
*ident
= token
->ident
;
964 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
967 arglist
= arglist
->next
;
969 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
970 if (arglist
->ident
== ident
) {
971 struct argcount
*count
= &arglist
->next
->count
;
975 token_type(token
) = type
;
977 case TOKEN_MACRO_ARGUMENT
:
980 case TOKEN_QUOTED_ARGUMENT
:
987 return count
->vararg
? 2 : 1;
988 token_type(token
) = TOKEN_ERROR
;
995 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
997 struct token
*token
= expansion
;
999 struct token
*last
= NULL
;
1001 if (match_op(token
, SPECIAL_HASHHASH
))
1004 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1005 if (match_op(token
, '#')) {
1007 struct token
*next
= token
->next
;
1008 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1010 next
->pos
.whitespace
= token
->pos
.whitespace
;
1013 token
->pos
.noexpand
= 1;
1015 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
1016 struct token
*next
= token
->next
;
1017 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1018 token_type(token
) = TOKEN_CONCAT
;
1022 if (arg
== 2 && last
&& match_op(last
, ',')) {
1023 token_type(last
) = TOKEN_GNU_KLUDGE
;
1026 } else if (match_op(next
, SPECIAL_HASHHASH
))
1028 else if (eof_token(next
))
1030 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1031 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1033 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1035 if (token_type(token
) == TOKEN_ERROR
)
1039 token
= alloc_token(&expansion
->pos
);
1040 token_type(token
) = TOKEN_UNTAINT
;
1041 token
->ident
= name
;
1047 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1051 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1054 sparse_error(token
->pos
, "too many instances of argument in body");
1058 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int weak
)
1060 struct token
*arglist
, *expansion
;
1061 struct token
*left
= token
->next
;
1065 if (token_type(left
) != TOKEN_IDENT
) {
1066 sparse_error(token
->pos
, "expected identifier to 'define'");
1073 expansion
= left
->next
;
1074 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
1075 arglist
= expansion
;
1076 expansion
= parse_arguments(expansion
);
1081 expansion
= parse_expansion(expansion
, arglist
, name
);
1085 sym
= lookup_macro(name
);
1087 if (token_list_different(sym
->expansion
, expansion
) ||
1088 token_list_different(sym
->arglist
, arglist
)) {
1093 warning(left
->pos
, "preprocessor token %.*s redefined",
1094 name
->len
, name
->name
);
1095 info(sym
->pos
, "this was the original definition");
1097 /* Don't overwrite global defs */
1098 if (sym
->scope
!= file_scope
)
1105 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1106 bind_symbol(sym
, name
, NS_MACRO
);
1109 sym
->expansion
= expansion
;
1110 sym
->arglist
= arglist
;
1112 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1116 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1118 return do_handle_define(stream
, line
, token
, 0);
1121 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1123 return do_handle_define(stream
, line
, token
, 1);
1126 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1128 struct token
*left
= token
->next
;
1129 struct symbol
**sym
;
1131 if (token_type(left
) != TOKEN_IDENT
) {
1132 sparse_error(token
->pos
, "expected identifier to 'undef'");
1136 sym
= &left
->ident
->symbols
;
1138 struct symbol
*t
= *sym
;
1139 if (t
->namespace & (NS_MACRO
| NS_INVISIBLEMACRO
)) {
1140 t
->namespace = NS_INVISIBLEMACRO
;
1148 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1150 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1151 free_preprocessor_line(token
->next
);
1152 token
->next
= stream
->top_if
;
1153 stream
->top_if
= token
;
1154 if (false_nesting
|| true != 1)
1159 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1161 struct token
*next
= token
->next
;
1163 if (token_type(next
) == TOKEN_IDENT
) {
1164 arg
= token_defined(next
);
1166 dirty_stream(stream
);
1168 sparse_error(token
->pos
, "expected preprocessor identifier");
1171 return preprocessor_if(stream
, token
, arg
);
1174 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1176 struct token
*next
= token
->next
;
1178 if (token_type(next
) == TOKEN_IDENT
) {
1179 if (!stream
->dirty
&& !stream
->ifndef
) {
1180 if (!stream
->protect
) {
1181 stream
->ifndef
= token
;
1182 stream
->protect
= next
->ident
;
1183 } else if (stream
->protect
== next
->ident
) {
1184 stream
->ifndef
= token
;
1188 arg
= !token_defined(next
);
1190 dirty_stream(stream
);
1192 sparse_error(token
->pos
, "expected preprocessor identifier");
1196 return preprocessor_if(stream
, token
, arg
);
1200 * Expression handling for #if and #elif; it differs from normal expansion
1201 * due to special treatment of "defined".
1203 static int expression_value(struct token
**where
)
1205 struct expression
*expr
;
1207 struct token
**list
= where
, **beginning
= NULL
;
1211 while (!eof_token(p
= scan_next(list
))) {
1214 if (token_type(p
) != TOKEN_IDENT
)
1216 if (p
->ident
== &defined_ident
) {
1221 if (!expand_one_symbol(list
))
1223 if (token_type(p
) != TOKEN_IDENT
)
1225 token_type(p
) = TOKEN_ZERO_IDENT
;
1228 if (match_op(p
, '(')) {
1232 replace_with_defined(p
);
1237 if (token_type(p
) == TOKEN_IDENT
)
1241 replace_with_defined(p
);
1246 if (!match_op(p
, ')'))
1247 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1254 p
= constant_expression(*where
, &expr
);
1256 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1257 value
= get_expression_value(expr
);
1261 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1265 value
= expression_value(&token
->next
);
1267 dirty_stream(stream
);
1268 return preprocessor_if(stream
, token
, value
);
1271 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1273 struct token
*top_if
= stream
->top_if
;
1277 nesting_error(stream
);
1278 sparse_error(token
->pos
, "unmatched #elif within stream");
1282 if (token_type(top_if
) == TOKEN_ELSE
) {
1283 nesting_error(stream
);
1284 sparse_error(token
->pos
, "#elif after #else");
1290 dirty_stream(stream
);
1291 if (token_type(top_if
) != TOKEN_IF
)
1293 if (false_nesting
) {
1294 if (expression_value(&token
->next
))
1298 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1303 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1305 struct token
*top_if
= stream
->top_if
;
1309 nesting_error(stream
);
1310 sparse_error(token
->pos
, "unmatched #else within stream");
1314 if (token_type(top_if
) == TOKEN_ELSE
) {
1315 nesting_error(stream
);
1316 sparse_error(token
->pos
, "#else after #else");
1318 if (false_nesting
) {
1319 if (token_type(top_if
) == TOKEN_IF
)
1324 token_type(top_if
) = TOKEN_ELSE
;
1328 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1330 struct token
*top_if
= stream
->top_if
;
1333 nesting_error(stream
);
1334 sparse_error(token
->pos
, "unmatched #endif in stream");
1339 stream
->top_if
= top_if
->next
;
1340 __free_token(top_if
);
1344 static const char *show_token_sequence(struct token
*token
)
1346 static char buffer
[1024];
1352 while (!eof_token(token
)) {
1353 const char *val
= show_token(token
);
1354 int len
= strlen(val
);
1356 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1357 sparse_error(token
->pos
, "too long token expansion");
1363 memcpy(ptr
, val
, len
);
1365 token
= token
->next
;
1366 whitespace
= token
->pos
.whitespace
;
1372 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1374 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1378 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1380 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
));
1384 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1387 * Do we have any non-system includes?
1388 * Clear them out if so..
1390 *sys_includepath
= NULL
;
1394 static void add_path_entry(struct token
*token
, const char *path
, const char ***where
, const char **new_path
)
1399 /* Need one free entry.. */
1400 if (includepath
[INCLUDEPATHS
-2])
1401 error_die(token
->pos
, "too many include path entries");
1403 /* check that this is not a duplicate */
1406 if (strcmp(*dst
, path
) == 0)
1415 * Move them all up starting at dst,
1416 * insert the new entry..
1419 const char *tmp
= *dst
;
1428 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1431 token
= token
->next
;
1432 if (eof_token(token
))
1434 if (token_type(token
) != TOKEN_STRING
) {
1435 warning(token
->pos
, "expected path string");
1438 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
+ 1);
1442 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1445 token
= token
->next
;
1446 if (eof_token(token
))
1448 if (token_type(token
) != TOKEN_STRING
) {
1449 sparse_error(token
->pos
, "expected path string");
1452 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
);
1456 /* Add to end on includepath list - no pointer updates */
1457 static void add_dirafter_entry(struct token
*token
, const char *path
)
1459 const char **dst
= includepath
;
1461 /* Need one free entry.. */
1462 if (includepath
[INCLUDEPATHS
-2])
1463 error_die(token
->pos
, "too many include path entries");
1465 /* Add to the end */
1473 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1476 token
= token
->next
;
1477 if (eof_token(token
))
1479 if (token_type(token
) != TOKEN_STRING
) {
1480 sparse_error(token
->pos
, "expected path string");
1483 add_dirafter_entry(token
, token
->string
->data
);
1487 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1492 * Split the include path. Any directories specified with `-I'
1493 * options before `-I-' are searched only for headers requested with
1494 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1495 * If additional directories are specified with `-I' options after
1496 * the `-I-', those directories are searched for all `#include'
1498 * In addition, `-I-' inhibits the use of the directory of the current
1499 * file directory as the first search directory for `#include "FILE"'.
1501 quote_includepath
= includepath
+1;
1502 angle_includepath
= sys_includepath
;
1507 * We replace "#pragma xxx" with "__pragma__" in the token
1508 * stream. Just as an example.
1510 * We'll just #define that away for now, but the theory here
1511 * is that we can use this to insert arbitrary token sequences
1512 * to turn the pragma's into internal front-end sequences for
1513 * when we actually start caring about them.
1515 * So eventually this will turn into some kind of extended
1516 * __attribute__() like thing, except called __pragma__(xxx).
1518 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1520 struct token
*next
= *line
;
1522 token
->ident
= &pragma_ident
;
1523 token
->pos
.newline
= 1;
1524 token
->pos
.whitespace
= 1;
1532 * We ignore #line for now.
1534 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1539 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1541 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1546 static void init_preprocessor(void)
1549 int stream
= init_stream("preprocessor", -1, includepath
);
1552 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1554 { "define", handle_define
},
1555 { "weak_define",handle_weak_define
},
1556 { "undef", handle_undef
},
1557 { "warning", handle_warning
},
1558 { "error", handle_error
},
1559 { "include", handle_include
},
1560 { "include_next",handle_include_next
},
1561 { "pragma", handle_pragma
},
1562 { "line", handle_line
},
1564 // our internal preprocessor tokens
1565 { "nostdinc", handle_nostdinc
},
1566 { "add_include", handle_add_include
},
1567 { "add_isystem", handle_add_isystem
},
1568 { "add_dirafter", handle_add_dirafter
},
1569 { "split_include", handle_split_include
},
1571 { "ifdef", handle_ifdef
},
1572 { "ifndef", handle_ifndef
},
1573 { "else", handle_else
},
1574 { "endif", handle_endif
},
1575 { "if", handle_if
},
1576 { "elif", handle_elif
},
1579 for (i
= 0; i
< (sizeof (normal
) / sizeof (normal
[0])); i
++) {
1581 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1582 sym
->handler
= normal
[i
].handler
;
1585 for (i
= 0; i
< (sizeof (special
) / sizeof (special
[0])); i
++) {
1587 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1588 sym
->handler
= special
[i
].handler
;
1594 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1596 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1597 struct token
*token
= start
->next
;
1600 if (eof_token(token
))
1603 if (token_type(token
) == TOKEN_IDENT
) {
1604 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1606 handler
= sym
->handler
;
1607 is_normal
= sym
->normal
;
1609 handler
= handle_nondirective
;
1611 } else if (token_type(token
) == TOKEN_NUMBER
) {
1612 handler
= handle_line
;
1614 handler
= handle_nondirective
;
1618 dirty_stream(stream
);
1622 if (!handler(stream
, line
, token
)) /* all set */
1626 free_preprocessor_line(token
);
1629 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1631 struct token
*start
= *line
, *next
;
1632 struct token
**tp
= &start
->next
;
1636 if (next
->pos
.newline
)
1641 *tp
= &eof_token_entry
;
1642 handle_preprocessor_line(stream
, line
, start
);
1645 static void do_preprocess(struct token
**list
)
1649 while (!eof_token(next
= scan_next(list
))) {
1650 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1652 if (next
->pos
.newline
&& match_op(next
, '#')) {
1653 if (!next
->pos
.noexpand
) {
1654 preprocessor_line(stream
, list
);
1655 __free_token(next
); /* Free the '#' token */
1660 switch (token_type(next
)) {
1661 case TOKEN_STREAMEND
:
1662 if (stream
->top_if
) {
1663 nesting_error(stream
);
1664 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1665 stream
->top_if
= NULL
;
1669 stream
->constant
= CONSTANT_FILE_YES
;
1672 case TOKEN_STREAMBEGIN
:
1677 dirty_stream(stream
);
1678 if (false_nesting
) {
1684 if (token_type(next
) != TOKEN_IDENT
||
1685 expand_one_symbol(list
))
1691 struct token
* preprocess(struct token
*token
)
1694 init_preprocessor();
1695 do_preprocess(&token
);
1697 // Drop all expressions from pre-processing, they're not used any more.
1698 // This is not true when we have multiple files, though ;/
1699 // clear_expression_alloc();