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 /* Expand symbol 'sym' at '*list' */
86 static int expand(struct token
**, struct symbol
*);
88 static void replace_with_string(struct token
*token
, const char *str
)
90 int size
= strlen(str
) + 1;
91 struct string
*s
= __alloc_string(size
);
94 memcpy(s
->data
, str
, size
);
95 token_type(token
) = TOKEN_STRING
;
99 static void replace_with_integer(struct token
*token
, unsigned int val
)
101 char *buf
= __alloc_bytes(11);
102 sprintf(buf
, "%u", val
);
103 token_type(token
) = TOKEN_NUMBER
;
107 static struct symbol
*lookup_macro(struct ident
*ident
)
109 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_UNDEF
);
110 if (sym
&& sym
->namespace != NS_MACRO
)
115 static int token_defined(struct token
*token
)
117 if (token_type(token
) == TOKEN_IDENT
) {
118 struct symbol
*sym
= lookup_macro(token
->ident
);
120 sym
->used_in
= file_scope
;
126 sparse_error(token
->pos
, "expected preprocessor identifier");
130 static void replace_with_defined(struct token
*token
)
132 static const char *string
[] = { "0", "1" };
133 int defined
= token_defined(token
);
135 token_type(token
) = TOKEN_NUMBER
;
136 token
->number
= string
[defined
];
139 static int expand_one_symbol(struct token
**list
)
141 struct token
*token
= *list
;
143 static char buffer
[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
146 if (token
->pos
.noexpand
)
149 sym
= lookup_macro(token
->ident
);
151 store_macro_pos(token
);
152 sym
->used_in
= file_scope
;
153 return expand(list
, sym
);
155 if (token
->ident
== &__LINE___ident
) {
156 replace_with_integer(token
, token
->pos
.line
);
157 } else if (token
->ident
== &__FILE___ident
) {
158 replace_with_string(token
, stream_name(token
->pos
.stream
));
159 } else if (token
->ident
== &__DATE___ident
) {
162 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
163 replace_with_string(token
, buffer
);
164 } else if (token
->ident
== &__TIME___ident
) {
167 strftime(buffer
, 9, "%T", localtime(&t
));
168 replace_with_string(token
, buffer
);
173 static inline struct token
*scan_next(struct token
**where
)
175 struct token
*token
= *where
;
176 if (token_type(token
) != TOKEN_UNTAINT
)
179 token
->ident
->tainted
= 0;
181 } while (token_type(token
) == TOKEN_UNTAINT
);
186 static void expand_list(struct token
**list
)
189 while (!eof_token(next
= scan_next(list
))) {
190 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
195 static void preprocessor_line(struct stream
*stream
, struct token
**line
);
197 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
199 struct stream
*stream
= input_streams
+ prev
->pos
.stream
;
200 struct token
**p
= &prev
->next
;
204 while (!eof_token(next
= scan_next(p
))) {
205 if (next
->pos
.newline
&& match_op(next
, '#')) {
206 if (!next
->pos
.noexpand
) {
207 sparse_error(next
->pos
,
208 "directive in argument list");
209 preprocessor_line(stream
, p
);
210 __free_token(next
); /* Free the '#' token */
214 switch (token_type(next
)) {
215 case TOKEN_STREAMEND
:
216 case TOKEN_STREAMBEGIN
:
217 *p
= &eof_token_entry
;
225 if (match_op(next
, '(')) {
227 } else if (match_op(next
, ')')) {
230 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
233 next
->pos
.stream
= pos
->stream
;
234 next
->pos
.line
= pos
->line
;
235 next
->pos
.pos
= pos
->pos
;
238 *p
= &eof_token_entry
;
243 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
248 struct token
*expanded
;
255 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
257 int wanted
= arglist
->count
.normal
;
258 struct token
*next
= NULL
;
261 arglist
= arglist
->next
; /* skip counter */
264 next
= collect_arg(start
, 0, &what
->pos
);
267 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
272 for (count
= 0; count
< wanted
; count
++) {
273 struct argcount
*p
= &arglist
->next
->count
;
274 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
275 arglist
= arglist
->next
->next
;
278 args
[count
].arg
= start
->next
;
279 args
[count
].n_normal
= p
->normal
;
280 args
[count
].n_quoted
= p
->quoted
;
281 args
[count
].n_str
= p
->str
;
282 if (match_op(next
, ')')) {
288 if (count
== wanted
&& !match_op(next
, ')'))
290 if (count
== wanted
- 1) {
291 struct argcount
*p
= &arglist
->next
->count
;
294 args
[count
].arg
= NULL
;
295 args
[count
].n_normal
= p
->normal
;
296 args
[count
].n_quoted
= p
->quoted
;
297 args
[count
].n_str
= p
->str
;
299 if (count
< wanted
- 1)
302 what
->next
= next
->next
;
306 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
307 show_token(what
), wanted
, count
);
310 while (match_op(next
, ',')) {
311 next
= collect_arg(next
, 0, &what
->pos
);
316 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
317 show_token(what
), count
, wanted
);
320 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
323 what
->next
= next
->next
;
327 static struct token
*dup_list(struct token
*list
)
329 struct token
*res
= NULL
;
330 struct token
**p
= &res
;
332 while (!eof_token(list
)) {
333 struct token
*newtok
= __alloc_token(0);
342 static const char *quote_token_sequence(struct token
*token
)
344 static char buffer
[1024];
348 while (!eof_token(token
)) {
349 const char *val
= quote_token(token
);
350 int len
= strlen(val
);
352 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
353 sparse_error(token
->pos
, "too long token expansion");
359 memcpy(ptr
, val
, len
);
362 whitespace
= token
->pos
.whitespace
;
368 static struct token
*stringify(struct token
*arg
)
370 const char *s
= quote_token_sequence(arg
);
371 int size
= strlen(s
)+1;
372 struct token
*token
= __alloc_token(0);
373 struct string
*string
= __alloc_string(size
);
375 memcpy(string
->data
, s
, size
);
376 string
->length
= size
;
377 token
->pos
= arg
->pos
;
378 token_type(token
) = TOKEN_STRING
;
379 token
->string
= string
;
380 token
->next
= &eof_token_entry
;
384 static void expand_arguments(int count
, struct arg
*args
)
387 for (i
= 0; i
< count
; i
++) {
388 struct token
*arg
= args
[i
].arg
;
390 arg
= &eof_token_entry
;
392 args
[i
].str
= stringify(arg
);
393 if (args
[i
].n_normal
) {
394 if (!args
[i
].n_quoted
) {
395 args
[i
].expanded
= arg
;
397 } else if (eof_token(arg
)) {
398 args
[i
].expanded
= arg
;
400 args
[i
].expanded
= dup_list(arg
);
402 expand_list(&args
[i
].expanded
);
408 * Possibly valid combinations:
409 * - ident + ident -> ident
410 * - ident + number -> ident unless number contains '.', '+' or '-'.
411 * - 'L' + char constant -> wide char constant
412 * - 'L' + string literal -> wide string literal
413 * - number + number -> number
414 * - number + ident -> number
415 * - number + '.' -> number
416 * - number + '+' or '-' -> number, if number used to end on [eEpP].
417 * - '.' + number -> number, if number used to start with a digit.
418 * - special + special -> either special or an error.
420 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
423 enum token_type t1
= token_type(left
), t2
= token_type(right
);
425 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
428 if (t1
== TOKEN_IDENT
&& left
->ident
== &L_ident
) {
429 if (t2
>= TOKEN_CHAR
&& t2
< TOKEN_WIDE_CHAR
)
430 return t2
+ TOKEN_WIDE_CHAR
- TOKEN_CHAR
;
431 if (t2
== TOKEN_STRING
)
432 return TOKEN_WIDE_STRING
;
435 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
438 strcpy(p
, show_token(left
));
439 strcat(p
, show_token(right
));
445 if (t1
== TOKEN_IDENT
) {
446 if (t2
== TOKEN_SPECIAL
)
448 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
453 if (t1
== TOKEN_NUMBER
) {
454 if (t2
== TOKEN_SPECIAL
) {
455 switch (right
->special
) {
459 if (strchr("eEpP", p
[len
- 2]))
468 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
471 return TOKEN_SPECIAL
;
474 static int merge(struct token
*left
, struct token
*right
)
476 static char buffer
[512];
477 enum token_type res
= combine(left
, right
, buffer
);
482 left
->ident
= built_in_ident(buffer
);
483 left
->pos
.noexpand
= 0;
487 char *number
= __alloc_bytes(strlen(buffer
) + 1);
488 memcpy(number
, buffer
, strlen(buffer
) + 1);
489 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
490 left
->number
= number
;
495 if (buffer
[2] && buffer
[3])
497 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
498 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
505 case TOKEN_WIDE_CHAR
:
506 case TOKEN_WIDE_STRING
:
507 token_type(left
) = res
;
508 left
->pos
.noexpand
= 0;
509 left
->string
= right
->string
;
512 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
513 token_type(left
) = res
;
514 left
->pos
.noexpand
= 0;
515 memcpy(left
->embedded
, right
->embedded
, 4);
521 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
525 static struct token
*dup_token(struct token
*token
, struct position
*streampos
)
527 struct token
*alloc
= alloc_token(streampos
);
528 token_type(alloc
) = token_type(token
);
529 alloc
->pos
.newline
= token
->pos
.newline
;
530 alloc
->pos
.whitespace
= token
->pos
.whitespace
;
531 alloc
->number
= token
->number
;
532 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
536 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
538 int need_copy
= --*count
;
539 while (!eof_token(list
)) {
542 token
= dup_token(list
, &list
->pos
);
545 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
546 token
->pos
.noexpand
= 1;
548 where
= &token
->next
;
551 *where
= &eof_token_entry
;
555 static int handle_kludge(struct token
**p
, struct arg
*args
)
557 struct token
*t
= (*p
)->next
->next
;
559 struct arg
*v
= &args
[t
->argnum
];
560 if (token_type(t
->next
) != TOKEN_CONCAT
) {
562 /* ignore the first ## */
566 /* skip the entire thing */
570 if (v
->arg
&& !eof_token(v
->arg
))
571 return 0; /* no magic */
576 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
578 struct position
*base_pos
= &(*list
)->pos
;
580 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
582 for (; !eof_token(body
); body
= body
->next
) {
583 struct token
*added
, *arg
;
587 switch (token_type(body
)) {
588 case TOKEN_GNU_KLUDGE
:
590 * GNU kludge: if we had <comma>##<vararg>, behaviour
591 * depends on whether we had enough arguments to have
592 * a vararg. If we did, ## is just ignored. Otherwise
593 * both , and ## are ignored. Worse, there can be
594 * an arbitrary number of ##<arg> in between; if all of
595 * those are empty, we act as if they hadn't been there,
596 * otherwise we act as if the kludge didn't exist.
599 if (handle_kludge(&body
, args
)) {
606 added
= dup_token(t
, base_pos
);
607 token_type(added
) = TOKEN_SPECIAL
;
611 case TOKEN_STR_ARGUMENT
:
612 arg
= args
[body
->argnum
].str
;
613 count
= &args
[body
->argnum
].n_str
;
616 case TOKEN_QUOTED_ARGUMENT
:
617 arg
= args
[body
->argnum
].arg
;
618 count
= &args
[body
->argnum
].n_quoted
;
619 if (!arg
|| eof_token(arg
)) {
628 case TOKEN_MACRO_ARGUMENT
:
629 arg
= args
[body
->argnum
].expanded
;
630 count
= &args
[body
->argnum
].n_normal
;
631 if (eof_token(arg
)) {
636 tail
= copy(&added
, arg
, count
);
637 added
->pos
.newline
= body
->pos
.newline
;
638 added
->pos
.whitespace
= body
->pos
.whitespace
;
642 if (state
== Placeholder
)
649 added
= dup_token(body
, base_pos
);
650 if (added
->ident
->tainted
)
651 added
->pos
.noexpand
= 1;
656 added
= dup_token(body
, base_pos
);
662 * if we got to doing real concatenation, we already have
663 * added something into the list, so containing_token() is OK.
665 if (state
== Concat
&& merge(containing_token(list
), added
)) {
667 if (tail
!= &added
->next
)
675 *list
= &eof_token_entry
;
679 static int expand(struct token
**list
, struct symbol
*sym
)
682 struct token
*token
= *list
;
683 struct ident
*expanding
= token
->ident
;
685 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
686 struct arg args
[nargs
];
688 if (expanding
->tainted
) {
689 token
->pos
.noexpand
= 1;
694 if (!match_op(scan_next(&token
->next
), '('))
696 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
698 expand_arguments(nargs
, args
);
701 expanding
->tainted
= 1;
704 tail
= substitute(list
, sym
->expansion
, args
);
706 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
707 * We still can lose the newline flag if the sucker expands to nothing,
708 * but the price of dealing with that is probably too high (we'd need
709 * to collect the flags during scan_next())
711 (*list
)->pos
.newline
= token
->pos
.newline
;
712 (*list
)->pos
.whitespace
= token
->pos
.whitespace
;
718 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
720 static char buffer
[256];
723 while (!eof_token(token
) && !match_op(token
, endop
)) {
725 const char *val
= token
->string
->data
;
726 if (token_type(token
) != TOKEN_STRING
)
727 val
= show_token(token
);
729 memcpy(ptr
, val
, len
);
734 if (endop
&& !match_op(token
, endop
))
735 sparse_error(start
->pos
, "expected '>' at end of filename");
739 static int already_tokenized(const char *path
)
743 for (stream
= *hash_stream(path
); stream
>= 0 ; stream
= next
) {
744 struct stream
*s
= input_streams
+ stream
;
746 next
= s
->next_stream
;
747 if (s
->constant
!= CONSTANT_FILE_YES
)
749 if (strcmp(path
, s
->name
))
751 if (s
->protect
&& !lookup_macro(s
->protect
))
758 /* Handle include of header files.
759 * The relevant options are made compatible with gcc. The only options that
760 * are not supported is -withprefix and friends.
762 * Three set of include paths are known:
763 * quote_includepath: Path to search when using #include "file.h"
764 * angle_includepath: Paths to search when using #include <file.h>
765 * isys_includepath: Paths specified with -isystem, come before the
766 * built-in system include paths. Gcc would suppress
767 * warnings from system headers. Here we separate
768 * them from the angle_ ones to keep search ordering.
770 * sys_includepath: Built-in include paths.
771 * dirafter_includepath Paths added with -dirafter.
773 * The above is implemented as one array with pointers
775 * quote_includepath ---> | |
779 * angle_includepath ---> | |
781 * isys_includepath ---> | |
783 * sys_includepath ---> | |
785 * dirafter_includepath -> | |
788 * -I dir insert dir just before isys_includepath and move the rest
789 * -I- makes all dirs specified with -I before to quote dirs only and
790 * angle_includepath is set equal to isys_includepath.
791 * -nostdinc removes all sys dirs by storing NULL in entry pointed
792 * to by * sys_includepath. Note that this will reset all dirs built-in
793 * and added before -nostdinc by -isystem and -idirafter.
794 * -isystem dir adds dir where isys_includepath points adding this dir as
796 * -idirafter dir adds dir to the end of the list
799 static void set_stream_include_path(struct stream
*stream
)
801 const char *path
= stream
->path
;
803 const char *p
= strrchr(stream
->name
, '/');
806 int len
= p
- stream
->name
+ 1;
807 char *m
= malloc(len
+1);
808 /* This includes the final "/" */
809 memcpy(m
, stream
->name
, len
);
815 includepath
[0] = path
;
818 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
821 int plen
= strlen(path
);
822 static char fullname
[PATH_MAX
];
824 memcpy(fullname
, path
, plen
);
825 if (plen
&& path
[plen
-1] != '/') {
826 fullname
[plen
] = '/';
829 memcpy(fullname
+plen
, filename
, flen
);
830 if (already_tokenized(fullname
))
832 fd
= open(fullname
, O_RDONLY
);
834 char * streamname
= __alloc_bytes(plen
+ flen
);
835 memcpy(streamname
, fullname
, plen
+ flen
);
836 *where
= tokenize(streamname
, fd
, *where
, next_path
);
843 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
847 while ((path
= *pptr
++) != NULL
) {
848 if (!try_include(path
, filename
, flen
, list
, pptr
))
855 static int free_preprocessor_line(struct token
*token
)
857 while (token_type(token
) != TOKEN_EOF
) {
858 struct token
*free
= token
;
865 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, int how
)
867 const char *filename
;
875 if (!match_op(next
, '<')) {
876 expand_list(&token
->next
);
879 if (match_op(token
->next
, '<')) {
886 filename
= token_name_sequence(token
, expect
, token
);
887 flen
= strlen(filename
) + 1;
890 if (filename
[0] == '/') {
891 if (try_include("", filename
, flen
, list
, includepath
))
898 path
= stream
->next_path
;
905 /* Dir of input file is first dir to search for quoted includes */
906 set_stream_include_path(stream
);
907 path
= expect
? angle_includepath
: quote_includepath
;
910 /* Check the standard include paths.. */
911 if (do_include_path(path
, list
, token
, filename
, flen
))
914 error_die(token
->pos
, "unable to open '%s'", filename
);
917 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
919 return handle_include_path(stream
, list
, token
, 0);
922 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
924 return handle_include_path(stream
, list
, token
, 1);
927 static int handle_argv_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
929 return handle_include_path(stream
, list
, token
, 2);
932 static int token_different(struct token
*t1
, struct token
*t2
)
936 if (token_type(t1
) != token_type(t2
))
939 switch (token_type(t1
)) {
941 different
= t1
->ident
!= t2
->ident
;
943 case TOKEN_ARG_COUNT
:
946 case TOKEN_GNU_KLUDGE
:
950 different
= strcmp(t1
->number
, t2
->number
);
953 different
= t1
->special
!= t2
->special
;
955 case TOKEN_MACRO_ARGUMENT
:
956 case TOKEN_QUOTED_ARGUMENT
:
957 case TOKEN_STR_ARGUMENT
:
958 different
= t1
->argnum
!= t2
->argnum
;
960 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
961 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
962 different
= memcmp(t1
->embedded
, t2
->embedded
, 4);
965 case TOKEN_WIDE_CHAR
:
967 case TOKEN_WIDE_STRING
: {
968 struct string
*s1
, *s2
;
973 if (s1
->length
!= s2
->length
)
975 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
985 static int token_list_different(struct token
*list1
, struct token
*list2
)
990 if (!list1
|| !list2
)
992 if (token_different(list1
, list2
))
999 static inline void set_arg_count(struct token
*token
)
1001 token_type(token
) = TOKEN_ARG_COUNT
;
1002 token
->count
.normal
= token
->count
.quoted
=
1003 token
->count
.str
= token
->count
.vararg
= 0;
1006 static struct token
*parse_arguments(struct token
*list
)
1008 struct token
*arg
= list
->next
, *next
= list
;
1009 struct argcount
*count
= &list
->count
;
1011 set_arg_count(list
);
1013 if (match_op(arg
, ')')) {
1015 list
->next
= &eof_token_entry
;
1019 while (token_type(arg
) == TOKEN_IDENT
) {
1020 if (arg
->ident
== &__VA_ARGS___ident
)
1022 if (!++count
->normal
)
1026 if (match_op(next
, ',')) {
1027 set_arg_count(next
);
1032 if (match_op(next
, ')')) {
1033 set_arg_count(next
);
1035 arg
->next
->next
= &eof_token_entry
;
1039 /* normal cases are finished here */
1041 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
1042 if (match_op(next
->next
, ')')) {
1043 set_arg_count(next
);
1044 next
->count
.vararg
= 1;
1046 arg
->next
->next
= &eof_token_entry
;
1054 if (eof_token(next
)) {
1062 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
1064 token_type(arg
) = TOKEN_IDENT
;
1065 arg
->ident
= &__VA_ARGS___ident
;
1066 if (!match_op(next
, ')'))
1068 if (!++count
->normal
)
1070 set_arg_count(next
);
1071 next
->count
.vararg
= 1;
1073 arg
->next
->next
= &eof_token_entry
;
1077 if (eof_token(arg
)) {
1081 if (match_op(arg
, ','))
1088 sparse_error(arg
->pos
, "parameter name missing");
1091 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
1095 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1098 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1101 sparse_error(arg
->pos
, "too many arguments in macro definition");
1105 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1107 struct ident
*ident
= token
->ident
;
1110 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1113 arglist
= arglist
->next
;
1115 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1116 if (arglist
->ident
== ident
) {
1117 struct argcount
*count
= &arglist
->next
->count
;
1121 token_type(token
) = type
;
1123 case TOKEN_MACRO_ARGUMENT
:
1124 n
= ++count
->normal
;
1126 case TOKEN_QUOTED_ARGUMENT
:
1127 n
= ++count
->quoted
;
1133 return count
->vararg
? 2 : 1;
1135 * XXX - need saner handling of that
1136 * (>= 1024 instances of argument)
1138 token_type(token
) = TOKEN_ERROR
;
1145 static struct token
*handle_hash(struct token
**p
, struct token
*arglist
)
1147 struct token
*token
= *p
;
1149 struct token
*next
= token
->next
;
1150 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1152 next
->pos
.whitespace
= token
->pos
.whitespace
;
1153 __free_token(token
);
1156 token
->pos
.noexpand
= 1;
1161 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1165 /* token->next is ## */
1166 static struct token
*handle_hashhash(struct token
*token
, struct token
*arglist
)
1168 struct token
*last
= token
;
1169 struct token
*concat
;
1170 int state
= match_op(token
, ',');
1172 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1178 /* eat duplicate ## */
1179 concat
= token
->next
;
1180 while (match_op(t
= concat
->next
, SPECIAL_HASHHASH
)) {
1182 __free_token(concat
);
1185 token_type(concat
) = TOKEN_CONCAT
;
1190 if (match_op(t
, '#')) {
1191 t
= handle_hash(&concat
->next
, arglist
);
1196 is_arg
= try_arg(t
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1198 if (state
== 1 && is_arg
) {
1202 state
= match_op(t
, ',');
1206 if (!match_op(token
->next
, SPECIAL_HASHHASH
))
1209 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1211 token_type(last
) = TOKEN_GNU_KLUDGE
;
1215 sparse_error(concat
->pos
, "'##' cannot appear at the ends of macro expansion");
1219 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1221 struct token
*token
= expansion
;
1224 if (match_op(token
, SPECIAL_HASHHASH
))
1227 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1228 if (match_op(token
, '#')) {
1229 token
= handle_hash(p
, arglist
);
1233 if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1234 token
= handle_hashhash(token
, arglist
);
1238 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1240 if (token_type(token
) == TOKEN_ERROR
)
1243 token
= alloc_token(&expansion
->pos
);
1244 token_type(token
) = TOKEN_UNTAINT
;
1245 token
->ident
= name
;
1251 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1254 sparse_error(token
->pos
, "too many instances of argument in body");
1258 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1260 struct token
*arglist
, *expansion
;
1261 struct token
*left
= token
->next
;
1266 if (token_type(left
) != TOKEN_IDENT
) {
1267 sparse_error(token
->pos
, "expected identifier to 'define'");
1274 expansion
= left
->next
;
1275 if (!expansion
->pos
.whitespace
) {
1276 if (match_op(expansion
, '(')) {
1277 arglist
= expansion
;
1278 expansion
= parse_arguments(expansion
);
1281 } else if (!eof_token(expansion
)) {
1282 warning(expansion
->pos
,
1283 "no whitespace before object-like macro body");
1287 expansion
= parse_expansion(expansion
, arglist
, name
);
1292 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1296 if (attr
< sym
->attr
)
1299 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1301 if (token_list_different(sym
->expansion
, expansion
) ||
1302 token_list_different(sym
->arglist
, arglist
)) {
1304 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1305 || sym
->used_in
== file_scope
) {
1306 warning(left
->pos
, "preprocessor token %.*s redefined",
1307 name
->len
, name
->name
);
1308 info(sym
->pos
, "this was the original definition");
1314 if (!sym
|| sym
->scope
!= file_scope
) {
1315 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1316 bind_symbol(sym
, name
, NS_MACRO
);
1321 sym
->expansion
= expansion
;
1322 sym
->arglist
= arglist
;
1323 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1326 sym
->namespace = NS_MACRO
;
1327 sym
->used_in
= NULL
;
1333 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1335 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1338 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1340 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1343 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1345 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1348 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1350 struct token
*left
= token
->next
;
1353 if (token_type(left
) != TOKEN_IDENT
) {
1354 sparse_error(token
->pos
, "expected identifier to 'undef'");
1358 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1360 if (attr
< sym
->attr
)
1362 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1364 } else if (attr
<= SYM_ATTR_NORMAL
)
1367 if (!sym
|| sym
->scope
!= file_scope
) {
1368 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1369 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1372 sym
->namespace = NS_UNDEF
;
1373 sym
->used_in
= NULL
;
1379 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1381 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1384 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1386 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1389 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1391 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1392 free_preprocessor_line(token
->next
);
1393 token
->next
= stream
->top_if
;
1394 stream
->top_if
= token
;
1395 if (false_nesting
|| true != 1)
1400 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1402 struct token
*next
= token
->next
;
1404 if (token_type(next
) == TOKEN_IDENT
) {
1405 arg
= token_defined(next
);
1407 dirty_stream(stream
);
1409 sparse_error(token
->pos
, "expected preprocessor identifier");
1412 return preprocessor_if(stream
, token
, arg
);
1415 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1417 struct token
*next
= token
->next
;
1419 if (token_type(next
) == TOKEN_IDENT
) {
1420 if (!stream
->dirty
&& !stream
->ifndef
) {
1421 if (!stream
->protect
) {
1422 stream
->ifndef
= token
;
1423 stream
->protect
= next
->ident
;
1424 } else if (stream
->protect
== next
->ident
) {
1425 stream
->ifndef
= token
;
1429 arg
= !token_defined(next
);
1431 dirty_stream(stream
);
1433 sparse_error(token
->pos
, "expected preprocessor identifier");
1437 return preprocessor_if(stream
, token
, arg
);
1440 static const char *show_token_sequence(struct token
*token
);
1443 * Expression handling for #if and #elif; it differs from normal expansion
1444 * due to special treatment of "defined".
1446 static int expression_value(struct token
**where
)
1448 struct expression
*expr
;
1450 struct token
**list
= where
, **beginning
= NULL
;
1454 while (!eof_token(p
= scan_next(list
))) {
1457 if (token_type(p
) != TOKEN_IDENT
)
1459 if (p
->ident
== &defined_ident
) {
1464 if (!expand_one_symbol(list
))
1466 if (token_type(p
) != TOKEN_IDENT
)
1468 token_type(p
) = TOKEN_ZERO_IDENT
;
1471 if (match_op(p
, '(')) {
1475 replace_with_defined(p
);
1480 if (token_type(p
) == TOKEN_IDENT
)
1484 replace_with_defined(p
);
1489 if (!match_op(p
, ')'))
1490 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1497 p
= constant_expression(*where
, &expr
);
1499 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1500 value
= get_expression_value(expr
);
1504 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1508 value
= expression_value(&token
->next
);
1510 dirty_stream(stream
);
1511 return preprocessor_if(stream
, token
, value
);
1514 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1516 struct token
*top_if
= stream
->top_if
;
1520 nesting_error(stream
);
1521 sparse_error(token
->pos
, "unmatched #elif within stream");
1525 if (token_type(top_if
) == TOKEN_ELSE
) {
1526 nesting_error(stream
);
1527 sparse_error(token
->pos
, "#elif after #else");
1533 dirty_stream(stream
);
1534 if (token_type(top_if
) != TOKEN_IF
)
1536 if (false_nesting
) {
1538 if (!expression_value(&token
->next
))
1542 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1547 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1549 struct token
*top_if
= stream
->top_if
;
1553 nesting_error(stream
);
1554 sparse_error(token
->pos
, "unmatched #else within stream");
1558 if (token_type(top_if
) == TOKEN_ELSE
) {
1559 nesting_error(stream
);
1560 sparse_error(token
->pos
, "#else after #else");
1562 if (false_nesting
) {
1563 if (token_type(top_if
) == TOKEN_IF
)
1568 token_type(top_if
) = TOKEN_ELSE
;
1572 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1574 struct token
*top_if
= stream
->top_if
;
1577 nesting_error(stream
);
1578 sparse_error(token
->pos
, "unmatched #endif in stream");
1583 stream
->top_if
= top_if
->next
;
1584 __free_token(top_if
);
1588 static const char *show_token_sequence(struct token
*token
)
1590 static char buffer
[1024];
1596 while (!eof_token(token
)) {
1597 const char *val
= show_token(token
);
1598 int len
= strlen(val
);
1600 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1601 sparse_error(token
->pos
, "too long token expansion");
1607 memcpy(ptr
, val
, len
);
1609 token
= token
->next
;
1610 whitespace
= token
->pos
.whitespace
;
1616 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1618 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1622 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1624 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
));
1628 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1631 * Do we have any non-system includes?
1632 * Clear them out if so..
1634 *sys_includepath
= NULL
;
1638 static inline void update_inc_ptrs(const char ***where
)
1641 if (*where
<= dirafter_includepath
) {
1642 dirafter_includepath
++;
1643 /* If this was the entry that we prepend, don't
1644 * rise the lower entries, even if they are at
1645 * the same level. */
1646 if (where
== &dirafter_includepath
)
1649 if (*where
<= sys_includepath
) {
1651 if (where
== &sys_includepath
)
1654 if (*where
<= isys_includepath
) {
1656 if (where
== &isys_includepath
)
1660 /* angle_includepath is actually never updated, since we
1661 * don't suppport -iquote rught now. May change some day. */
1662 if (*where
<= angle_includepath
) {
1663 angle_includepath
++;
1664 if (where
== &angle_includepath
)
1669 /* Add a path before 'where' and update the pointers associated with the
1670 * includepath array */
1671 static void add_path_entry(struct token
*token
, const char *path
,
1672 const char ***where
)
1677 /* Need one free entry.. */
1678 if (includepath
[INCLUDEPATHS
-2])
1679 error_die(token
->pos
, "too many include path entries");
1681 /* check that this is not a duplicate */
1684 if (strcmp(*dst
, path
) == 0)
1691 update_inc_ptrs(where
);
1694 * Move them all up starting at dst,
1695 * insert the new entry..
1698 const char *tmp
= *dst
;
1705 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1708 token
= token
->next
;
1709 if (eof_token(token
))
1711 if (token_type(token
) != TOKEN_STRING
) {
1712 warning(token
->pos
, "expected path string");
1715 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1719 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1722 token
= token
->next
;
1723 if (eof_token(token
))
1725 if (token_type(token
) != TOKEN_STRING
) {
1726 sparse_error(token
->pos
, "expected path string");
1729 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1733 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
1736 token
= token
->next
;
1737 if (eof_token(token
))
1739 if (token_type(token
) != TOKEN_STRING
) {
1740 sparse_error(token
->pos
, "expected path string");
1743 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1747 /* Add to end on includepath list - no pointer updates */
1748 static void add_dirafter_entry(struct token
*token
, const char *path
)
1750 const char **dst
= includepath
;
1752 /* Need one free entry.. */
1753 if (includepath
[INCLUDEPATHS
-2])
1754 error_die(token
->pos
, "too many include path entries");
1756 /* Add to the end */
1764 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1767 token
= token
->next
;
1768 if (eof_token(token
))
1770 if (token_type(token
) != TOKEN_STRING
) {
1771 sparse_error(token
->pos
, "expected path string");
1774 add_dirafter_entry(token
, token
->string
->data
);
1778 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1783 * Split the include path. Any directories specified with `-I'
1784 * options before `-I-' are searched only for headers requested with
1785 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1786 * If additional directories are specified with `-I' options after
1787 * the `-I-', those directories are searched for all `#include'
1789 * In addition, `-I-' inhibits the use of the directory of the current
1790 * file directory as the first search directory for `#include "FILE"'.
1792 quote_includepath
= includepath
+1;
1793 angle_includepath
= sys_includepath
;
1798 * We replace "#pragma xxx" with "__pragma__" in the token
1799 * stream. Just as an example.
1801 * We'll just #define that away for now, but the theory here
1802 * is that we can use this to insert arbitrary token sequences
1803 * to turn the pragmas into internal front-end sequences for
1804 * when we actually start caring about them.
1806 * So eventually this will turn into some kind of extended
1807 * __attribute__() like thing, except called __pragma__(xxx).
1809 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1811 struct token
*next
= *line
;
1813 token
->ident
= &pragma_ident
;
1814 token
->pos
.newline
= 1;
1815 token
->pos
.whitespace
= 1;
1823 * We ignore #line for now.
1825 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1830 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1832 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1837 static void init_preprocessor(void)
1840 int stream
= init_stream("preprocessor", -1, includepath
);
1843 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1845 { "define", handle_define
},
1846 { "weak_define", handle_weak_define
},
1847 { "strong_define", handle_strong_define
},
1848 { "undef", handle_undef
},
1849 { "strong_undef", handle_strong_undef
},
1850 { "warning", handle_warning
},
1851 { "error", handle_error
},
1852 { "include", handle_include
},
1853 { "include_next", handle_include_next
},
1854 { "pragma", handle_pragma
},
1855 { "line", handle_line
},
1857 // our internal preprocessor tokens
1858 { "nostdinc", handle_nostdinc
},
1859 { "add_include", handle_add_include
},
1860 { "add_isystem", handle_add_isystem
},
1861 { "add_system", handle_add_system
},
1862 { "add_dirafter", handle_add_dirafter
},
1863 { "split_include", handle_split_include
},
1864 { "argv_include", handle_argv_include
},
1866 { "ifdef", handle_ifdef
},
1867 { "ifndef", handle_ifndef
},
1868 { "else", handle_else
},
1869 { "endif", handle_endif
},
1870 { "if", handle_if
},
1871 { "elif", handle_elif
},
1874 for (i
= 0; i
< ARRAY_SIZE(normal
); i
++) {
1876 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1877 sym
->handler
= normal
[i
].handler
;
1880 for (i
= 0; i
< ARRAY_SIZE(special
); i
++) {
1882 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1883 sym
->handler
= special
[i
].handler
;
1889 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1891 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1892 struct token
*token
= start
->next
;
1895 if (eof_token(token
))
1898 if (token_type(token
) == TOKEN_IDENT
) {
1899 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1901 handler
= sym
->handler
;
1902 is_normal
= sym
->normal
;
1904 handler
= handle_nondirective
;
1906 } else if (token_type(token
) == TOKEN_NUMBER
) {
1907 handler
= handle_line
;
1909 handler
= handle_nondirective
;
1913 dirty_stream(stream
);
1917 if (!handler(stream
, line
, token
)) /* all set */
1921 free_preprocessor_line(token
);
1924 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1926 struct token
*start
= *line
, *next
;
1927 struct token
**tp
= &start
->next
;
1931 if (next
->pos
.newline
)
1936 *tp
= &eof_token_entry
;
1937 handle_preprocessor_line(stream
, line
, start
);
1940 static void do_preprocess(struct token
**list
)
1944 while (!eof_token(next
= scan_next(list
))) {
1945 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1947 if (next
->pos
.newline
&& match_op(next
, '#')) {
1948 if (!next
->pos
.noexpand
) {
1949 preprocessor_line(stream
, list
);
1950 __free_token(next
); /* Free the '#' token */
1955 switch (token_type(next
)) {
1956 case TOKEN_STREAMEND
:
1957 if (stream
->top_if
) {
1958 nesting_error(stream
);
1959 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1960 stream
->top_if
= NULL
;
1964 stream
->constant
= CONSTANT_FILE_YES
;
1967 case TOKEN_STREAMBEGIN
:
1972 dirty_stream(stream
);
1973 if (false_nesting
) {
1979 if (token_type(next
) != TOKEN_IDENT
||
1980 expand_one_symbol(list
))
1986 struct token
* preprocess(struct token
*token
)
1989 init_preprocessor();
1990 do_preprocess(&token
);
1992 // Drop all expressions from preprocessing, they're not used any more.
1993 // This is not true when we have multiple files, though ;/
1994 // clear_expression_alloc();