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
28 #include "expression.h"
31 static int false_nesting
= 0;
33 #define INCLUDEPATHS 300
34 const char *includepath
[INCLUDEPATHS
+1] = {
41 static const char **quote_includepath
= includepath
;
42 static const char **angle_includepath
= includepath
+ 1;
43 static const char **isys_includepath
= includepath
+ 1;
44 static const char **sys_includepath
= includepath
+ 1;
45 static const char **dirafter_includepath
= includepath
+ 3;
47 #define dirty_stream(stream) \
49 if (!stream->dirty) { \
51 if (!stream->ifndef) \
52 stream->protect = NULL; \
56 #define end_group(stream) \
58 if (stream->ifndef == stream->top_if) { \
59 stream->ifndef = NULL; \
61 stream->protect = NULL; \
62 else if (stream->protect) \
67 #define nesting_error(stream) \
70 stream->ifndef = NULL; \
71 stream->protect = NULL; \
74 static struct token
*alloc_token(struct position
*pos
)
76 struct token
*token
= __alloc_token(0);
78 token
->pos
.stream
= pos
->stream
;
79 token
->pos
.line
= pos
->line
;
80 token
->pos
.pos
= pos
->pos
;
81 token
->pos
.whitespace
= 1;
85 static const char *show_token_sequence(struct token
*token
);
87 /* Expand symbol 'sym' at '*list' */
88 static int expand(struct token
**, struct symbol
*);
90 static void replace_with_string(struct token
*token
, const char *str
)
92 int size
= strlen(str
) + 1;
93 struct string
*s
= __alloc_string(size
);
96 memcpy(s
->data
, str
, size
);
97 token_type(token
) = TOKEN_STRING
;
101 static void replace_with_integer(struct token
*token
, unsigned int val
)
103 char *buf
= __alloc_bytes(11);
104 sprintf(buf
, "%u", val
);
105 token_type(token
) = TOKEN_NUMBER
;
109 static struct symbol
*lookup_macro(struct ident
*ident
)
111 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_UNDEF
);
112 if (sym
&& sym
->namespace != NS_MACRO
)
117 static int token_defined(struct token
*token
)
119 if (token_type(token
) == TOKEN_IDENT
) {
120 struct symbol
*sym
= lookup_macro(token
->ident
);
122 sym
->used_in
= file_scope
;
128 sparse_error(token
->pos
, "expected preprocessor identifier");
132 static void replace_with_defined(struct token
*token
)
134 static const char *string
[] = { "0", "1" };
135 int defined
= token_defined(token
);
137 token_type(token
) = TOKEN_NUMBER
;
138 token
->number
= string
[defined
];
141 static int expand_one_symbol(struct token
**list
)
143 struct token
*token
= *list
;
145 static char buffer
[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
148 if (token
->pos
.noexpand
)
151 sym
= lookup_macro(token
->ident
);
153 store_macro_pos(token
);
154 sym
->used_in
= file_scope
;
155 return expand(list
, sym
);
157 if (token
->ident
== &__LINE___ident
) {
158 replace_with_integer(token
, token
->pos
.line
);
159 } else if (token
->ident
== &__FILE___ident
) {
160 replace_with_string(token
, stream_name(token
->pos
.stream
));
161 } else if (token
->ident
== &__DATE___ident
) {
164 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
165 replace_with_string(token
, buffer
);
166 } else if (token
->ident
== &__TIME___ident
) {
169 strftime(buffer
, 9, "%T", localtime(&t
));
170 replace_with_string(token
, buffer
);
175 static inline struct token
*scan_next(struct token
**where
)
177 struct token
*token
= *where
;
178 if (token_type(token
) != TOKEN_UNTAINT
)
181 token
->ident
->tainted
= 0;
183 } while (token_type(token
) == TOKEN_UNTAINT
);
188 static void expand_list(struct token
**list
)
191 while (!eof_token(next
= scan_next(list
))) {
192 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
197 static void preprocessor_line(struct stream
*stream
, struct token
**line
);
199 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
201 struct stream
*stream
= input_streams
+ prev
->pos
.stream
;
202 struct token
**p
= &prev
->next
;
206 while (!eof_token(next
= scan_next(p
))) {
207 if (next
->pos
.newline
&& match_op(next
, '#')) {
208 if (!next
->pos
.noexpand
) {
209 sparse_error(next
->pos
,
210 "directive in argument list");
211 preprocessor_line(stream
, p
);
212 __free_token(next
); /* Free the '#' token */
216 switch (token_type(next
)) {
217 case TOKEN_STREAMEND
:
218 case TOKEN_STREAMBEGIN
:
219 *p
= &eof_token_entry
;
227 if (match_op(next
, '(')) {
229 } else if (match_op(next
, ')')) {
232 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
235 next
->pos
.stream
= pos
->stream
;
236 next
->pos
.line
= pos
->line
;
237 next
->pos
.pos
= pos
->pos
;
240 *p
= &eof_token_entry
;
245 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
250 struct token
*expanded
;
257 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
259 int wanted
= arglist
->count
.normal
;
260 struct token
*next
= NULL
;
263 arglist
= arglist
->next
; /* skip counter */
266 next
= collect_arg(start
, 0, &what
->pos
);
269 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
274 for (count
= 0; count
< wanted
; count
++) {
275 struct argcount
*p
= &arglist
->next
->count
;
276 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
277 arglist
= arglist
->next
->next
;
280 args
[count
].arg
= start
->next
;
281 args
[count
].n_normal
= p
->normal
;
282 args
[count
].n_quoted
= p
->quoted
;
283 args
[count
].n_str
= p
->str
;
284 if (match_op(next
, ')')) {
290 if (count
== wanted
&& !match_op(next
, ')'))
292 if (count
== wanted
- 1) {
293 struct argcount
*p
= &arglist
->next
->count
;
296 args
[count
].arg
= NULL
;
297 args
[count
].n_normal
= p
->normal
;
298 args
[count
].n_quoted
= p
->quoted
;
299 args
[count
].n_str
= p
->str
;
301 if (count
< wanted
- 1)
304 what
->next
= next
->next
;
308 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
309 show_token(what
), wanted
, count
);
312 while (match_op(next
, ',')) {
313 next
= collect_arg(next
, 0, &what
->pos
);
318 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
319 show_token(what
), count
, wanted
);
322 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
325 what
->next
= next
->next
;
329 static struct token
*dup_list(struct token
*list
)
331 struct token
*res
= NULL
;
332 struct token
**p
= &res
;
334 while (!eof_token(list
)) {
335 struct token
*newtok
= __alloc_token(0);
344 static struct token
*stringify(struct token
*arg
)
346 const char *s
= show_token_sequence(arg
);
347 int size
= strlen(s
)+1;
348 struct token
*token
= __alloc_token(0);
349 struct string
*string
= __alloc_string(size
);
351 memcpy(string
->data
, s
, size
);
352 string
->length
= size
;
353 token
->pos
= arg
->pos
;
354 token_type(token
) = TOKEN_STRING
;
355 token
->string
= string
;
356 token
->next
= &eof_token_entry
;
360 static void expand_arguments(int count
, struct arg
*args
)
363 for (i
= 0; i
< count
; i
++) {
364 struct token
*arg
= args
[i
].arg
;
366 arg
= &eof_token_entry
;
368 args
[i
].str
= stringify(arg
);
369 if (args
[i
].n_normal
) {
370 if (!args
[i
].n_quoted
) {
371 args
[i
].expanded
= arg
;
373 } else if (eof_token(arg
)) {
374 args
[i
].expanded
= arg
;
376 args
[i
].expanded
= dup_list(arg
);
378 expand_list(&args
[i
].expanded
);
384 * Possibly valid combinations:
385 * - ident + ident -> ident
386 * - ident + number -> ident unless number contains '.', '+' or '-'.
387 * - number + number -> number
388 * - number + ident -> number
389 * - number + '.' -> number
390 * - number + '+' or '-' -> number, if number used to end on [eEpP].
391 * - '.' + number -> number, if number used to start with a digit.
392 * - special + special -> either special or an error.
394 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
397 enum token_type t1
= token_type(left
), t2
= token_type(right
);
399 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
402 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
405 strcpy(p
, show_token(left
));
406 strcat(p
, show_token(right
));
412 if (t1
== TOKEN_IDENT
) {
413 if (t2
== TOKEN_SPECIAL
)
415 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
420 if (t1
== TOKEN_NUMBER
) {
421 if (t2
== TOKEN_SPECIAL
) {
422 switch (right
->special
) {
426 if (strchr("eEpP", p
[len
- 2]))
435 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
438 return TOKEN_SPECIAL
;
441 static int merge(struct token
*left
, struct token
*right
)
443 static char buffer
[512];
446 switch (combine(left
, right
, buffer
)) {
448 left
->ident
= built_in_ident(buffer
);
449 left
->pos
.noexpand
= 0;
453 char *number
= __alloc_bytes(strlen(buffer
) + 1);
454 memcpy(number
, buffer
, strlen(buffer
) + 1);
455 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
456 left
->number
= number
;
461 if (buffer
[2] && buffer
[3])
463 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
464 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
472 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
476 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
478 struct token
*alloc
= alloc_token(streampos
);
479 token_type(alloc
) = token_type(token
);
480 alloc
->pos
.newline
= pos
->newline
;
481 alloc
->pos
.whitespace
= pos
->whitespace
;
482 alloc
->number
= token
->number
;
483 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
487 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
489 int need_copy
= --*count
;
490 while (!eof_token(list
)) {
493 token
= dup_token(list
, &list
->pos
, &list
->pos
);
496 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
497 token
->pos
.noexpand
= 1;
499 where
= &token
->next
;
502 *where
= &eof_token_entry
;
506 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
508 struct token
*token
= *list
;
509 struct position
*base_pos
= &token
->pos
;
510 struct position
*pos
= base_pos
;
512 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
514 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
515 struct token
*added
, *arg
;
518 switch (token_type(body
)) {
519 case TOKEN_GNU_KLUDGE
:
521 * GNU kludge: if we had <comma>##<vararg>, behaviour
522 * depends on whether we had enough arguments to have
523 * a vararg. If we did, ## is just ignored. Otherwise
524 * both , and ## are ignored. Comma should come from
525 * the body of macro and not be an argument of earlier
528 if (!args
[body
->next
->argnum
].arg
)
530 added
= dup_token(body
, base_pos
, pos
);
531 token_type(added
) = TOKEN_SPECIAL
;
535 case TOKEN_STR_ARGUMENT
:
536 arg
= args
[body
->argnum
].str
;
537 count
= &args
[body
->argnum
].n_str
;
540 case TOKEN_QUOTED_ARGUMENT
:
541 arg
= args
[body
->argnum
].arg
;
542 count
= &args
[body
->argnum
].n_quoted
;
543 if (!arg
|| eof_token(arg
)) {
552 case TOKEN_MACRO_ARGUMENT
:
553 arg
= args
[body
->argnum
].expanded
;
554 count
= &args
[body
->argnum
].n_normal
;
555 if (eof_token(arg
)) {
560 tail
= copy(&added
, arg
, count
);
561 added
->pos
.newline
= pos
->newline
;
562 added
->pos
.whitespace
= pos
->whitespace
;
566 if (state
== Placeholder
)
573 added
= dup_token(body
, base_pos
, pos
);
574 if (added
->ident
->tainted
)
575 added
->pos
.noexpand
= 1;
580 added
= dup_token(body
, base_pos
, pos
);
586 * if we got to doing real concatenation, we already have
587 * added something into the list, so containing_token() is OK.
589 if (state
== Concat
&& merge(containing_token(list
), added
)) {
591 if (tail
!= &added
->next
)
599 *list
= &eof_token_entry
;
603 static int expand(struct token
**list
, struct symbol
*sym
)
606 struct token
*token
= *list
;
607 struct ident
*expanding
= token
->ident
;
609 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
610 struct arg args
[nargs
];
612 if (expanding
->tainted
) {
613 token
->pos
.noexpand
= 1;
618 if (!match_op(scan_next(&token
->next
), '('))
620 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
622 expand_arguments(nargs
, args
);
625 expanding
->tainted
= 1;
628 tail
= substitute(list
, sym
->expansion
, args
);
634 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
636 static char buffer
[256];
639 while (!eof_token(token
) && !match_op(token
, endop
)) {
641 const char *val
= token
->string
->data
;
642 if (token_type(token
) != TOKEN_STRING
)
643 val
= show_token(token
);
645 memcpy(ptr
, val
, len
);
650 if (endop
&& !match_op(token
, endop
))
651 sparse_error(start
->pos
, "expected '>' at end of filename");
655 static int already_tokenized(const char *path
)
659 for (stream
= *hash_stream(path
); stream
>= 0 ; stream
= next
) {
660 struct stream
*s
= input_streams
+ stream
;
662 next
= s
->next_stream
;
663 if (s
->constant
!= CONSTANT_FILE_YES
)
665 if (strcmp(path
, s
->name
))
667 if (s
->protect
&& !lookup_macro(s
->protect
))
674 /* Handle include of header files.
675 * The relevant options are made compatible with gcc. The only options that
676 * are not supported is -withprefix and friends.
678 * Three set of include paths are known:
679 * quote_includepath: Path to search when using #include "file.h"
680 * angle_includepath: Paths to search when using #include <file.h>
681 * isys_includepath: Paths specified with -isystem, come before the
682 * built-in system include paths. Gcc would suppress
683 * warnings from system headers. Here we separate
684 * them from the angle_ ones to keep search ordering.
686 * sys_includepath: Built-in include paths.
687 * dirafter_includepath Paths added with -dirafter.
689 * The above is implemented as one array with pointers
691 * quote_includepath ---> | |
695 * angle_includepath ---> | |
697 * isys_includepath ---> | |
699 * sys_includepath ---> | |
701 * dirafter_includepath -> | |
704 * -I dir insert dir just before isys_includepath and move the rest
705 * -I- makes all dirs specified with -I before to quote dirs only and
706 * angle_includepath is set equal to isys_includepath.
707 * -nostdinc removes all sys dirs by storing NULL in entry pointed
708 * to by * sys_includepath. Note that this will reset all dirs built-in
709 * and added before -nostdinc by -isystem and -idirafter.
710 * -isystem dir adds dir where isys_includepath points adding this dir as
712 * -idirafter dir adds dir to the end of the list
715 static void set_stream_include_path(struct stream
*stream
)
717 const char *path
= stream
->path
;
719 const char *p
= strrchr(stream
->name
, '/');
722 int len
= p
- stream
->name
+ 1;
723 char *m
= malloc(len
+1);
724 /* This includes the final "/" */
725 memcpy(m
, stream
->name
, len
);
731 includepath
[0] = path
;
734 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
737 int plen
= strlen(path
);
738 static char fullname
[PATH_MAX
];
740 memcpy(fullname
, path
, plen
);
741 if (plen
&& path
[plen
-1] != '/') {
742 fullname
[plen
] = '/';
745 memcpy(fullname
+plen
, filename
, flen
);
746 if (already_tokenized(fullname
))
748 fd
= open(fullname
, O_RDONLY
);
750 char * streamname
= __alloc_bytes(plen
+ flen
);
751 memcpy(streamname
, fullname
, plen
+ flen
);
752 *where
= tokenize(streamname
, fd
, *where
, next_path
);
759 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
763 while ((path
= *pptr
++) != NULL
) {
764 if (!try_include(path
, filename
, flen
, list
, pptr
))
771 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
773 int flen
= strlen(filename
) + 1;
776 if (filename
[0] == '/') {
777 if (try_include("", filename
, flen
, list
, includepath
))
782 /* Dir of input file is first dir to search for quoted includes */
783 set_stream_include_path(stream
);
786 /* Do not search quote include if <> is in use */
787 path
= local
? quote_includepath
: angle_includepath
;
789 /* Check the standard include paths.. */
790 if (do_include_path(path
, list
, token
, filename
, flen
))
793 error_die(token
->pos
, "unable to open '%s'", filename
);
796 static int free_preprocessor_line(struct token
*token
)
798 while (token_type(token
) != TOKEN_EOF
) {
799 struct token
*free
= token
;
806 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
808 const char *filename
;
814 if (!match_op(next
, '<')) {
815 expand_list(&token
->next
);
818 if (match_op(token
->next
, '<')) {
824 filename
= token_name_sequence(token
, expect
, token
);
825 do_include(!expect
, stream
, list
, token
, filename
, path
);
829 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
831 return handle_include_path(stream
, list
, token
, NULL
);
834 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
836 return handle_include_path(stream
, list
, token
, stream
->next_path
);
839 static int token_different(struct token
*t1
, struct token
*t2
)
843 if (token_type(t1
) != token_type(t2
))
846 switch (token_type(t1
)) {
848 different
= t1
->ident
!= t2
->ident
;
850 case TOKEN_ARG_COUNT
:
853 case TOKEN_GNU_KLUDGE
:
857 different
= strcmp(t1
->number
, t2
->number
);
860 different
= t1
->special
!= t2
->special
;
862 case TOKEN_MACRO_ARGUMENT
:
863 case TOKEN_QUOTED_ARGUMENT
:
864 case TOKEN_STR_ARGUMENT
:
865 different
= t1
->argnum
!= t2
->argnum
;
868 case TOKEN_WIDE_CHAR
:
869 different
= t1
->character
!= t2
->character
;
872 case TOKEN_WIDE_STRING
: {
873 struct string
*s1
, *s2
;
878 if (s1
->length
!= s2
->length
)
880 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
890 static int token_list_different(struct token
*list1
, struct token
*list2
)
895 if (!list1
|| !list2
)
897 if (token_different(list1
, list2
))
904 static inline void set_arg_count(struct token
*token
)
906 token_type(token
) = TOKEN_ARG_COUNT
;
907 token
->count
.normal
= token
->count
.quoted
=
908 token
->count
.str
= token
->count
.vararg
= 0;
911 static struct token
*parse_arguments(struct token
*list
)
913 struct token
*arg
= list
->next
, *next
= list
;
914 struct argcount
*count
= &list
->count
;
918 if (match_op(arg
, ')')) {
920 list
->next
= &eof_token_entry
;
924 while (token_type(arg
) == TOKEN_IDENT
) {
925 if (arg
->ident
== &__VA_ARGS___ident
)
927 if (!++count
->normal
)
931 if (match_op(next
, ',')) {
937 if (match_op(next
, ')')) {
940 arg
->next
->next
= &eof_token_entry
;
944 /* normal cases are finished here */
946 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
947 if (match_op(next
->next
, ')')) {
949 next
->count
.vararg
= 1;
951 arg
->next
->next
= &eof_token_entry
;
959 if (eof_token(next
)) {
967 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
969 token_type(arg
) = TOKEN_IDENT
;
970 arg
->ident
= &__VA_ARGS___ident
;
971 if (!match_op(next
, ')'))
973 if (!++count
->normal
)
976 next
->count
.vararg
= 1;
978 arg
->next
->next
= &eof_token_entry
;
982 if (eof_token(arg
)) {
986 if (match_op(arg
, ','))
993 sparse_error(arg
->pos
, "parameter name missing");
996 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
1000 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1003 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1006 sparse_error(arg
->pos
, "too many arguments in macro definition");
1010 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1012 struct ident
*ident
= token
->ident
;
1015 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1018 arglist
= arglist
->next
;
1020 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1021 if (arglist
->ident
== ident
) {
1022 struct argcount
*count
= &arglist
->next
->count
;
1026 token_type(token
) = type
;
1028 case TOKEN_MACRO_ARGUMENT
:
1029 n
= ++count
->normal
;
1031 case TOKEN_QUOTED_ARGUMENT
:
1032 n
= ++count
->quoted
;
1038 return count
->vararg
? 2 : 1;
1039 token_type(token
) = TOKEN_ERROR
;
1046 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1048 struct token
*token
= expansion
;
1050 struct token
*last
= NULL
;
1052 if (match_op(token
, SPECIAL_HASHHASH
))
1055 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1056 if (match_op(token
, '#')) {
1058 struct token
*next
= token
->next
;
1059 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1061 next
->pos
.whitespace
= token
->pos
.whitespace
;
1064 token
->pos
.noexpand
= 1;
1066 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
1067 struct token
*next
= token
->next
;
1068 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1069 token_type(token
) = TOKEN_CONCAT
;
1073 if (arg
== 2 && last
&& match_op(last
, ',')) {
1074 token_type(last
) = TOKEN_GNU_KLUDGE
;
1077 } else if (match_op(next
, SPECIAL_HASHHASH
))
1079 else if (eof_token(next
))
1081 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1082 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1084 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1086 if (token_type(token
) == TOKEN_ERROR
)
1090 token
= alloc_token(&expansion
->pos
);
1091 token_type(token
) = TOKEN_UNTAINT
;
1092 token
->ident
= name
;
1098 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1102 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1105 sparse_error(token
->pos
, "too many instances of argument in body");
1109 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1111 struct token
*arglist
, *expansion
;
1112 struct token
*left
= token
->next
;
1117 if (token_type(left
) != TOKEN_IDENT
) {
1118 sparse_error(token
->pos
, "expected identifier to 'define'");
1125 expansion
= left
->next
;
1126 if (!expansion
->pos
.whitespace
) {
1127 if (match_op(expansion
, '(')) {
1128 arglist
= expansion
;
1129 expansion
= parse_arguments(expansion
);
1132 } else if (!eof_token(expansion
)) {
1133 warning(expansion
->pos
,
1134 "no whitespace before object-like macro body");
1138 expansion
= parse_expansion(expansion
, arglist
, name
);
1143 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1147 if (attr
< sym
->attr
)
1150 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1152 if (token_list_different(sym
->expansion
, expansion
) ||
1153 token_list_different(sym
->arglist
, arglist
)) {
1155 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1156 || sym
->used_in
== file_scope
) {
1157 warning(left
->pos
, "preprocessor token %.*s redefined",
1158 name
->len
, name
->name
);
1159 info(sym
->pos
, "this was the original definition");
1165 if (!sym
|| sym
->scope
!= file_scope
) {
1166 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1167 bind_symbol(sym
, name
, NS_MACRO
);
1172 sym
->expansion
= expansion
;
1173 sym
->arglist
= arglist
;
1174 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1177 sym
->namespace = NS_MACRO
;
1178 sym
->used_in
= NULL
;
1184 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1186 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1189 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1191 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1194 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1196 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1199 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1201 struct token
*left
= token
->next
;
1204 if (token_type(left
) != TOKEN_IDENT
) {
1205 sparse_error(token
->pos
, "expected identifier to 'undef'");
1209 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1211 if (attr
< sym
->attr
)
1213 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1215 } else if (attr
<= SYM_ATTR_NORMAL
)
1218 if (!sym
|| sym
->scope
!= file_scope
) {
1219 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1220 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1223 sym
->namespace = NS_UNDEF
;
1224 sym
->used_in
= NULL
;
1230 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1232 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1235 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1237 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1240 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1242 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1243 free_preprocessor_line(token
->next
);
1244 token
->next
= stream
->top_if
;
1245 stream
->top_if
= token
;
1246 if (false_nesting
|| true != 1)
1251 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1253 struct token
*next
= token
->next
;
1255 if (token_type(next
) == TOKEN_IDENT
) {
1256 arg
= token_defined(next
);
1258 dirty_stream(stream
);
1260 sparse_error(token
->pos
, "expected preprocessor identifier");
1263 return preprocessor_if(stream
, token
, arg
);
1266 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1268 struct token
*next
= token
->next
;
1270 if (token_type(next
) == TOKEN_IDENT
) {
1271 if (!stream
->dirty
&& !stream
->ifndef
) {
1272 if (!stream
->protect
) {
1273 stream
->ifndef
= token
;
1274 stream
->protect
= next
->ident
;
1275 } else if (stream
->protect
== next
->ident
) {
1276 stream
->ifndef
= token
;
1280 arg
= !token_defined(next
);
1282 dirty_stream(stream
);
1284 sparse_error(token
->pos
, "expected preprocessor identifier");
1288 return preprocessor_if(stream
, token
, arg
);
1292 * Expression handling for #if and #elif; it differs from normal expansion
1293 * due to special treatment of "defined".
1295 static int expression_value(struct token
**where
)
1297 struct expression
*expr
;
1299 struct token
**list
= where
, **beginning
= NULL
;
1303 while (!eof_token(p
= scan_next(list
))) {
1306 if (token_type(p
) != TOKEN_IDENT
)
1308 if (p
->ident
== &defined_ident
) {
1313 if (!expand_one_symbol(list
))
1315 if (token_type(p
) != TOKEN_IDENT
)
1317 token_type(p
) = TOKEN_ZERO_IDENT
;
1320 if (match_op(p
, '(')) {
1324 replace_with_defined(p
);
1329 if (token_type(p
) == TOKEN_IDENT
)
1333 replace_with_defined(p
);
1338 if (!match_op(p
, ')'))
1339 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1346 p
= constant_expression(*where
, &expr
);
1348 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1349 value
= get_expression_value(expr
);
1353 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1357 value
= expression_value(&token
->next
);
1359 dirty_stream(stream
);
1360 return preprocessor_if(stream
, token
, value
);
1363 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1365 struct token
*top_if
= stream
->top_if
;
1369 nesting_error(stream
);
1370 sparse_error(token
->pos
, "unmatched #elif within stream");
1374 if (token_type(top_if
) == TOKEN_ELSE
) {
1375 nesting_error(stream
);
1376 sparse_error(token
->pos
, "#elif after #else");
1382 dirty_stream(stream
);
1383 if (token_type(top_if
) != TOKEN_IF
)
1385 if (false_nesting
) {
1387 if (!expression_value(&token
->next
))
1391 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1396 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1398 struct token
*top_if
= stream
->top_if
;
1402 nesting_error(stream
);
1403 sparse_error(token
->pos
, "unmatched #else within stream");
1407 if (token_type(top_if
) == TOKEN_ELSE
) {
1408 nesting_error(stream
);
1409 sparse_error(token
->pos
, "#else after #else");
1411 if (false_nesting
) {
1412 if (token_type(top_if
) == TOKEN_IF
)
1417 token_type(top_if
) = TOKEN_ELSE
;
1421 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1423 struct token
*top_if
= stream
->top_if
;
1426 nesting_error(stream
);
1427 sparse_error(token
->pos
, "unmatched #endif in stream");
1432 stream
->top_if
= top_if
->next
;
1433 __free_token(top_if
);
1437 static const char *show_token_sequence(struct token
*token
)
1439 static char buffer
[1024];
1445 while (!eof_token(token
)) {
1446 const char *val
= show_token(token
);
1447 int len
= strlen(val
);
1449 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1450 sparse_error(token
->pos
, "too long token expansion");
1456 memcpy(ptr
, val
, len
);
1458 token
= token
->next
;
1459 whitespace
= token
->pos
.whitespace
;
1465 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1467 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1471 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1473 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
));
1477 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1480 * Do we have any non-system includes?
1481 * Clear them out if so..
1483 *sys_includepath
= NULL
;
1487 static inline void update_inc_ptrs(const char ***where
)
1490 if (*where
<= dirafter_includepath
) {
1491 dirafter_includepath
++;
1492 /* If this was the entry that we prepend, don't
1493 * rise the lower entries, even if they are at
1494 * the same level. */
1495 if (where
== &dirafter_includepath
)
1498 if (*where
<= sys_includepath
) {
1500 if (where
== &sys_includepath
)
1503 if (*where
<= isys_includepath
) {
1505 if (where
== &isys_includepath
)
1509 /* angle_includepath is actually never updated, since we
1510 * don't suppport -iquote rught now. May change some day. */
1511 if (*where
<= angle_includepath
) {
1512 angle_includepath
++;
1513 if (where
== &angle_includepath
)
1518 /* Add a path before 'where' and update the pointers associated with the
1519 * includepath array */
1520 static void add_path_entry(struct token
*token
, const char *path
,
1521 const char ***where
)
1526 /* Need one free entry.. */
1527 if (includepath
[INCLUDEPATHS
-2])
1528 error_die(token
->pos
, "too many include path entries");
1530 /* check that this is not a duplicate */
1533 if (strcmp(*dst
, path
) == 0)
1540 update_inc_ptrs(where
);
1543 * Move them all up starting at dst,
1544 * insert the new entry..
1547 const char *tmp
= *dst
;
1554 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1557 token
= token
->next
;
1558 if (eof_token(token
))
1560 if (token_type(token
) != TOKEN_STRING
) {
1561 warning(token
->pos
, "expected path string");
1564 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1568 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1571 token
= token
->next
;
1572 if (eof_token(token
))
1574 if (token_type(token
) != TOKEN_STRING
) {
1575 sparse_error(token
->pos
, "expected path string");
1578 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1582 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
1585 token
= token
->next
;
1586 if (eof_token(token
))
1588 if (token_type(token
) != TOKEN_STRING
) {
1589 sparse_error(token
->pos
, "expected path string");
1592 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1596 /* Add to end on includepath list - no pointer updates */
1597 static void add_dirafter_entry(struct token
*token
, const char *path
)
1599 const char **dst
= includepath
;
1601 /* Need one free entry.. */
1602 if (includepath
[INCLUDEPATHS
-2])
1603 error_die(token
->pos
, "too many include path entries");
1605 /* Add to the end */
1613 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1616 token
= token
->next
;
1617 if (eof_token(token
))
1619 if (token_type(token
) != TOKEN_STRING
) {
1620 sparse_error(token
->pos
, "expected path string");
1623 add_dirafter_entry(token
, token
->string
->data
);
1627 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1632 * Split the include path. Any directories specified with `-I'
1633 * options before `-I-' are searched only for headers requested with
1634 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1635 * If additional directories are specified with `-I' options after
1636 * the `-I-', those directories are searched for all `#include'
1638 * In addition, `-I-' inhibits the use of the directory of the current
1639 * file directory as the first search directory for `#include "FILE"'.
1641 quote_includepath
= includepath
+1;
1642 angle_includepath
= sys_includepath
;
1647 * We replace "#pragma xxx" with "__pragma__" in the token
1648 * stream. Just as an example.
1650 * We'll just #define that away for now, but the theory here
1651 * is that we can use this to insert arbitrary token sequences
1652 * to turn the pragmas into internal front-end sequences for
1653 * when we actually start caring about them.
1655 * So eventually this will turn into some kind of extended
1656 * __attribute__() like thing, except called __pragma__(xxx).
1658 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1660 struct token
*next
= *line
;
1662 token
->ident
= &pragma_ident
;
1663 token
->pos
.newline
= 1;
1664 token
->pos
.whitespace
= 1;
1672 * We ignore #line for now.
1674 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1679 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1681 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1686 static void init_preprocessor(void)
1689 int stream
= init_stream("preprocessor", -1, includepath
);
1692 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1694 { "define", handle_define
},
1695 { "weak_define", handle_weak_define
},
1696 { "strong_define", handle_strong_define
},
1697 { "undef", handle_undef
},
1698 { "strong_undef", handle_strong_undef
},
1699 { "warning", handle_warning
},
1700 { "error", handle_error
},
1701 { "include", handle_include
},
1702 { "include_next", handle_include_next
},
1703 { "pragma", handle_pragma
},
1704 { "line", handle_line
},
1706 // our internal preprocessor tokens
1707 { "nostdinc", handle_nostdinc
},
1708 { "add_include", handle_add_include
},
1709 { "add_isystem", handle_add_isystem
},
1710 { "add_system", handle_add_system
},
1711 { "add_dirafter", handle_add_dirafter
},
1712 { "split_include", handle_split_include
},
1714 { "ifdef", handle_ifdef
},
1715 { "ifndef", handle_ifndef
},
1716 { "else", handle_else
},
1717 { "endif", handle_endif
},
1718 { "if", handle_if
},
1719 { "elif", handle_elif
},
1722 for (i
= 0; i
< ARRAY_SIZE(normal
); i
++) {
1724 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1725 sym
->handler
= normal
[i
].handler
;
1728 for (i
= 0; i
< ARRAY_SIZE(special
); i
++) {
1730 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1731 sym
->handler
= special
[i
].handler
;
1737 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1739 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1740 struct token
*token
= start
->next
;
1743 if (eof_token(token
))
1746 if (token_type(token
) == TOKEN_IDENT
) {
1747 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1749 handler
= sym
->handler
;
1750 is_normal
= sym
->normal
;
1752 handler
= handle_nondirective
;
1754 } else if (token_type(token
) == TOKEN_NUMBER
) {
1755 handler
= handle_line
;
1757 handler
= handle_nondirective
;
1761 dirty_stream(stream
);
1765 if (!handler(stream
, line
, token
)) /* all set */
1769 free_preprocessor_line(token
);
1772 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1774 struct token
*start
= *line
, *next
;
1775 struct token
**tp
= &start
->next
;
1779 if (next
->pos
.newline
)
1784 *tp
= &eof_token_entry
;
1785 handle_preprocessor_line(stream
, line
, start
);
1788 static void do_preprocess(struct token
**list
)
1792 while (!eof_token(next
= scan_next(list
))) {
1793 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1795 if (next
->pos
.newline
&& match_op(next
, '#')) {
1796 if (!next
->pos
.noexpand
) {
1797 preprocessor_line(stream
, list
);
1798 __free_token(next
); /* Free the '#' token */
1803 switch (token_type(next
)) {
1804 case TOKEN_STREAMEND
:
1805 if (stream
->top_if
) {
1806 nesting_error(stream
);
1807 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1808 stream
->top_if
= NULL
;
1812 stream
->constant
= CONSTANT_FILE_YES
;
1815 case TOKEN_STREAMBEGIN
:
1820 dirty_stream(stream
);
1821 if (false_nesting
) {
1827 if (token_type(next
) != TOKEN_IDENT
||
1828 expand_one_symbol(list
))
1834 struct token
* preprocess(struct token
*token
)
1837 init_preprocessor();
1838 do_preprocess(&token
);
1840 // Drop all expressions from preprocessing, they're not used any more.
1841 // This is not true when we have multiple files, though ;/
1842 // clear_expression_alloc();