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 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
44 #include "expression.h"
47 static int false_nesting
= 0;
49 #define INCLUDEPATHS 300
50 const char *includepath
[INCLUDEPATHS
+1] = {
57 static const char **quote_includepath
= includepath
;
58 static const char **angle_includepath
= includepath
+ 1;
59 static const char **isys_includepath
= includepath
+ 1;
60 static const char **sys_includepath
= includepath
+ 1;
61 static const char **dirafter_includepath
= includepath
+ 3;
63 #define dirty_stream(stream) \
65 if (!stream->dirty) { \
67 if (!stream->ifndef) \
68 stream->protect = NULL; \
72 #define end_group(stream) \
74 if (stream->ifndef == stream->top_if) { \
75 stream->ifndef = NULL; \
77 stream->protect = NULL; \
78 else if (stream->protect) \
83 #define nesting_error(stream) \
86 stream->ifndef = NULL; \
87 stream->protect = NULL; \
90 static struct token
*alloc_token(struct position
*pos
)
92 struct token
*token
= __alloc_token(0);
94 token
->pos
.stream
= pos
->stream
;
95 token
->pos
.line
= pos
->line
;
96 token
->pos
.pos
= pos
->pos
;
97 token
->pos
.whitespace
= 1;
101 /* Expand symbol 'sym' at '*list' */
102 static int expand(struct token
**, struct symbol
*);
104 static void replace_with_string(struct token
*token
, const char *str
)
106 int size
= strlen(str
) + 1;
107 struct string
*s
= __alloc_string(size
);
110 memcpy(s
->data
, str
, size
);
111 token_type(token
) = TOKEN_STRING
;
115 static void replace_with_integer(struct token
*token
, unsigned int val
)
117 char *buf
= __alloc_bytes(11);
118 sprintf(buf
, "%u", val
);
119 token_type(token
) = TOKEN_NUMBER
;
123 static struct symbol
*lookup_macro(struct ident
*ident
)
125 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_UNDEF
);
126 if (sym
&& sym
->namespace != NS_MACRO
)
131 static int token_defined(struct token
*token
)
133 if (token_type(token
) == TOKEN_IDENT
) {
134 struct symbol
*sym
= lookup_macro(token
->ident
);
136 sym
->used_in
= file_scope
;
142 sparse_error(token
->pos
, "expected preprocessor identifier");
146 static void replace_with_defined(struct token
*token
)
148 static const char *string
[] = { "0", "1" };
149 int defined
= token_defined(token
);
151 token_type(token
) = TOKEN_NUMBER
;
152 token
->number
= string
[defined
];
155 static int expand_one_symbol(struct token
**list
)
157 struct token
*token
= *list
;
159 static char buffer
[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
162 if (token
->pos
.noexpand
)
165 sym
= lookup_macro(token
->ident
);
167 store_macro_pos(token
);
168 sym
->used_in
= file_scope
;
169 return expand(list
, sym
);
171 if (token
->ident
== &__LINE___ident
) {
172 replace_with_integer(token
, token
->pos
.line
);
173 } else if (token
->ident
== &__FILE___ident
) {
174 replace_with_string(token
, stream_name(token
->pos
.stream
));
175 } else if (token
->ident
== &__DATE___ident
) {
178 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
179 replace_with_string(token
, buffer
);
180 } else if (token
->ident
== &__TIME___ident
) {
183 strftime(buffer
, 9, "%T", localtime(&t
));
184 replace_with_string(token
, buffer
);
189 static inline struct token
*scan_next(struct token
**where
)
191 struct token
*token
= *where
;
192 if (token_type(token
) != TOKEN_UNTAINT
)
195 token
->ident
->tainted
= 0;
197 } while (token_type(token
) == TOKEN_UNTAINT
);
202 static void expand_list(struct token
**list
)
205 while (!eof_token(next
= scan_next(list
))) {
206 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
211 static void preprocessor_line(struct stream
*stream
, struct token
**line
);
213 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
215 struct stream
*stream
= input_streams
+ prev
->pos
.stream
;
216 struct token
**p
= &prev
->next
;
220 while (!eof_token(next
= scan_next(p
))) {
221 if (next
->pos
.newline
&& match_op(next
, '#')) {
222 if (!next
->pos
.noexpand
) {
223 sparse_error(next
->pos
,
224 "directive in argument list");
225 preprocessor_line(stream
, p
);
226 __free_token(next
); /* Free the '#' token */
230 switch (token_type(next
)) {
231 case TOKEN_STREAMEND
:
232 case TOKEN_STREAMBEGIN
:
233 *p
= &eof_token_entry
;
241 if (match_op(next
, '(')) {
243 } else if (match_op(next
, ')')) {
246 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
249 next
->pos
.stream
= pos
->stream
;
250 next
->pos
.line
= pos
->line
;
251 next
->pos
.pos
= pos
->pos
;
254 *p
= &eof_token_entry
;
259 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
264 struct token
*expanded
;
271 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
273 int wanted
= arglist
->count
.normal
;
274 struct token
*next
= NULL
;
277 arglist
= arglist
->next
; /* skip counter */
280 next
= collect_arg(start
, 0, &what
->pos
);
283 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
288 for (count
= 0; count
< wanted
; count
++) {
289 struct argcount
*p
= &arglist
->next
->count
;
290 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
291 arglist
= arglist
->next
->next
;
294 args
[count
].arg
= start
->next
;
295 args
[count
].n_normal
= p
->normal
;
296 args
[count
].n_quoted
= p
->quoted
;
297 args
[count
].n_str
= p
->str
;
298 if (match_op(next
, ')')) {
304 if (count
== wanted
&& !match_op(next
, ')'))
306 if (count
== wanted
- 1) {
307 struct argcount
*p
= &arglist
->next
->count
;
310 args
[count
].arg
= NULL
;
311 args
[count
].n_normal
= p
->normal
;
312 args
[count
].n_quoted
= p
->quoted
;
313 args
[count
].n_str
= p
->str
;
315 if (count
< wanted
- 1)
318 what
->next
= next
->next
;
322 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
323 show_token(what
), wanted
, count
);
326 while (match_op(next
, ',')) {
327 next
= collect_arg(next
, 0, &what
->pos
);
332 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
333 show_token(what
), count
, wanted
);
336 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
339 what
->next
= next
->next
;
343 static struct token
*dup_list(struct token
*list
)
345 struct token
*res
= NULL
;
346 struct token
**p
= &res
;
348 while (!eof_token(list
)) {
349 struct token
*newtok
= __alloc_token(0);
358 static const char *show_token_sequence(struct token
*token
, int quote
)
360 static char buffer
[MAX_STRING
];
364 if (!token
&& !quote
)
366 while (!eof_token(token
)) {
367 const char *val
= quote
? quote_token(token
) : show_token(token
);
368 int len
= strlen(val
);
370 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
371 sparse_error(token
->pos
, "too long token expansion");
377 memcpy(ptr
, val
, len
);
380 whitespace
= token
->pos
.whitespace
;
386 static struct token
*stringify(struct token
*arg
)
388 const char *s
= show_token_sequence(arg
, 1);
389 int size
= strlen(s
)+1;
390 struct token
*token
= __alloc_token(0);
391 struct string
*string
= __alloc_string(size
);
393 memcpy(string
->data
, s
, size
);
394 string
->length
= size
;
395 token
->pos
= arg
->pos
;
396 token_type(token
) = TOKEN_STRING
;
397 token
->string
= string
;
398 token
->next
= &eof_token_entry
;
402 static void expand_arguments(int count
, struct arg
*args
)
405 for (i
= 0; i
< count
; i
++) {
406 struct token
*arg
= args
[i
].arg
;
408 arg
= &eof_token_entry
;
410 args
[i
].str
= stringify(arg
);
411 if (args
[i
].n_normal
) {
412 if (!args
[i
].n_quoted
) {
413 args
[i
].expanded
= arg
;
415 } else if (eof_token(arg
)) {
416 args
[i
].expanded
= arg
;
418 args
[i
].expanded
= dup_list(arg
);
420 expand_list(&args
[i
].expanded
);
426 * Possibly valid combinations:
427 * - ident + ident -> ident
428 * - ident + number -> ident unless number contains '.', '+' or '-'.
429 * - 'L' + char constant -> wide char constant
430 * - 'L' + string literal -> wide string literal
431 * - number + number -> number
432 * - number + ident -> number
433 * - number + '.' -> number
434 * - number + '+' or '-' -> number, if number used to end on [eEpP].
435 * - '.' + number -> number, if number used to start with a digit.
436 * - special + special -> either special or an error.
438 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
441 enum token_type t1
= token_type(left
), t2
= token_type(right
);
443 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
446 if (t1
== TOKEN_IDENT
&& left
->ident
== &L_ident
) {
447 if (t2
>= TOKEN_CHAR
&& t2
< TOKEN_WIDE_CHAR
)
448 return t2
+ TOKEN_WIDE_CHAR
- TOKEN_CHAR
;
449 if (t2
== TOKEN_STRING
)
450 return TOKEN_WIDE_STRING
;
453 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
456 strcpy(p
, show_token(left
));
457 strcat(p
, show_token(right
));
463 if (t1
== TOKEN_IDENT
) {
464 if (t2
== TOKEN_SPECIAL
)
466 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
471 if (t1
== TOKEN_NUMBER
) {
472 if (t2
== TOKEN_SPECIAL
) {
473 switch (right
->special
) {
477 if (strchr("eEpP", p
[len
- 2]))
486 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
489 return TOKEN_SPECIAL
;
492 static int merge(struct token
*left
, struct token
*right
)
494 static char buffer
[512];
495 enum token_type res
= combine(left
, right
, buffer
);
500 left
->ident
= built_in_ident(buffer
);
501 left
->pos
.noexpand
= 0;
505 char *number
= __alloc_bytes(strlen(buffer
) + 1);
506 memcpy(number
, buffer
, strlen(buffer
) + 1);
507 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
508 left
->number
= number
;
513 if (buffer
[2] && buffer
[3])
515 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
516 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
523 case TOKEN_WIDE_CHAR
:
524 case TOKEN_WIDE_STRING
:
525 token_type(left
) = res
;
526 left
->pos
.noexpand
= 0;
527 left
->string
= right
->string
;
530 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
531 token_type(left
) = res
;
532 left
->pos
.noexpand
= 0;
533 memcpy(left
->embedded
, right
->embedded
, 4);
539 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
543 static struct token
*dup_token(struct token
*token
, struct position
*streampos
)
545 struct token
*alloc
= alloc_token(streampos
);
546 token_type(alloc
) = token_type(token
);
547 alloc
->pos
.newline
= token
->pos
.newline
;
548 alloc
->pos
.whitespace
= token
->pos
.whitespace
;
549 alloc
->number
= token
->number
;
550 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
554 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
556 int need_copy
= --*count
;
557 while (!eof_token(list
)) {
560 token
= dup_token(list
, &list
->pos
);
563 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
564 token
->pos
.noexpand
= 1;
566 where
= &token
->next
;
569 *where
= &eof_token_entry
;
573 static int handle_kludge(struct token
**p
, struct arg
*args
)
575 struct token
*t
= (*p
)->next
->next
;
577 struct arg
*v
= &args
[t
->argnum
];
578 if (token_type(t
->next
) != TOKEN_CONCAT
) {
580 /* ignore the first ## */
584 /* skip the entire thing */
588 if (v
->arg
&& !eof_token(v
->arg
))
589 return 0; /* no magic */
594 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
596 struct position
*base_pos
= &(*list
)->pos
;
598 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
600 for (; !eof_token(body
); body
= body
->next
) {
601 struct token
*added
, *arg
;
605 switch (token_type(body
)) {
606 case TOKEN_GNU_KLUDGE
:
608 * GNU kludge: if we had <comma>##<vararg>, behaviour
609 * depends on whether we had enough arguments to have
610 * a vararg. If we did, ## is just ignored. Otherwise
611 * both , and ## are ignored. Worse, there can be
612 * an arbitrary number of ##<arg> in between; if all of
613 * those are empty, we act as if they hadn't been there,
614 * otherwise we act as if the kludge didn't exist.
617 if (handle_kludge(&body
, args
)) {
624 added
= dup_token(t
, base_pos
);
625 token_type(added
) = TOKEN_SPECIAL
;
629 case TOKEN_STR_ARGUMENT
:
630 arg
= args
[body
->argnum
].str
;
631 count
= &args
[body
->argnum
].n_str
;
634 case TOKEN_QUOTED_ARGUMENT
:
635 arg
= args
[body
->argnum
].arg
;
636 count
= &args
[body
->argnum
].n_quoted
;
637 if (!arg
|| eof_token(arg
)) {
646 case TOKEN_MACRO_ARGUMENT
:
647 arg
= args
[body
->argnum
].expanded
;
648 count
= &args
[body
->argnum
].n_normal
;
649 if (eof_token(arg
)) {
654 tail
= copy(&added
, arg
, count
);
655 added
->pos
.newline
= body
->pos
.newline
;
656 added
->pos
.whitespace
= body
->pos
.whitespace
;
660 if (state
== Placeholder
)
667 added
= dup_token(body
, base_pos
);
668 if (added
->ident
->tainted
)
669 added
->pos
.noexpand
= 1;
674 added
= dup_token(body
, base_pos
);
680 * if we got to doing real concatenation, we already have
681 * added something into the list, so containing_token() is OK.
683 if (state
== Concat
&& merge(containing_token(list
), added
)) {
685 if (tail
!= &added
->next
)
693 *list
= &eof_token_entry
;
697 static int expand(struct token
**list
, struct symbol
*sym
)
700 struct token
*token
= *list
;
701 struct ident
*expanding
= token
->ident
;
703 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
704 struct arg args
[nargs
];
706 if (expanding
->tainted
) {
707 token
->pos
.noexpand
= 1;
712 if (!match_op(scan_next(&token
->next
), '('))
714 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
716 expand_arguments(nargs
, args
);
719 expanding
->tainted
= 1;
722 tail
= substitute(list
, sym
->expansion
, args
);
724 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
725 * We still can lose the newline flag if the sucker expands to nothing,
726 * but the price of dealing with that is probably too high (we'd need
727 * to collect the flags during scan_next())
729 (*list
)->pos
.newline
= token
->pos
.newline
;
730 (*list
)->pos
.whitespace
= token
->pos
.whitespace
;
736 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
738 static char buffer
[256];
741 while (!eof_token(token
) && !match_op(token
, endop
)) {
743 const char *val
= token
->string
->data
;
744 if (token_type(token
) != TOKEN_STRING
)
745 val
= show_token(token
);
747 memcpy(ptr
, val
, len
);
752 if (endop
&& !match_op(token
, endop
))
753 sparse_error(start
->pos
, "expected '>' at end of filename");
757 static int already_tokenized(const char *path
)
761 for (stream
= *hash_stream(path
); stream
>= 0 ; stream
= next
) {
762 struct stream
*s
= input_streams
+ stream
;
764 next
= s
->next_stream
;
766 if (strcmp(path
, s
->name
))
770 if (s
->constant
!= CONSTANT_FILE_YES
)
772 if (strcmp(path
, s
->name
))
774 if (s
->protect
&& !lookup_macro(s
->protect
))
781 /* Handle include of header files.
782 * The relevant options are made compatible with gcc. The only options that
783 * are not supported is -withprefix and friends.
785 * Three set of include paths are known:
786 * quote_includepath: Path to search when using #include "file.h"
787 * angle_includepath: Paths to search when using #include <file.h>
788 * isys_includepath: Paths specified with -isystem, come before the
789 * built-in system include paths. Gcc would suppress
790 * warnings from system headers. Here we separate
791 * them from the angle_ ones to keep search ordering.
793 * sys_includepath: Built-in include paths.
794 * dirafter_includepath Paths added with -dirafter.
796 * The above is implemented as one array with pointers
798 * quote_includepath ---> | |
802 * angle_includepath ---> | |
804 * isys_includepath ---> | |
806 * sys_includepath ---> | |
808 * dirafter_includepath -> | |
811 * -I dir insert dir just before isys_includepath and move the rest
812 * -I- makes all dirs specified with -I before to quote dirs only and
813 * angle_includepath is set equal to isys_includepath.
814 * -nostdinc removes all sys dirs by storing NULL in entry pointed
815 * to by * sys_includepath. Note that this will reset all dirs built-in
816 * and added before -nostdinc by -isystem and -idirafter.
817 * -isystem dir adds dir where isys_includepath points adding this dir as
819 * -idirafter dir adds dir to the end of the list
822 static void set_stream_include_path(struct stream
*stream
)
824 const char *path
= stream
->path
;
826 const char *p
= strrchr(stream
->name
, '/');
829 int len
= p
- stream
->name
+ 1;
830 char *m
= malloc(len
+1);
831 /* This includes the final "/" */
832 memcpy(m
, stream
->name
, len
);
838 includepath
[0] = path
;
841 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
844 int plen
= strlen(path
);
845 static char fullname
[PATH_MAX
];
847 memcpy(fullname
, path
, plen
);
848 if (plen
&& path
[plen
-1] != '/') {
849 fullname
[plen
] = '/';
852 memcpy(fullname
+plen
, filename
, flen
);
853 if (already_tokenized(fullname
))
855 fd
= open(fullname
, O_RDONLY
);
857 char * streamname
= __alloc_bytes(plen
+ flen
);
858 memcpy(streamname
, fullname
, plen
+ flen
);
859 *where
= tokenize(streamname
, fd
, *where
, next_path
);
866 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
870 while ((path
= *pptr
++) != NULL
) {
871 if (!try_include(path
, filename
, flen
, list
, pptr
))
878 static int free_preprocessor_line(struct token
*token
)
880 while (token_type(token
) != TOKEN_EOF
) {
881 struct token
*free
= token
;
888 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, int how
)
890 const char *filename
;
898 if (!match_op(next
, '<')) {
899 expand_list(&token
->next
);
902 if (match_op(token
->next
, '<')) {
909 filename
= token_name_sequence(token
, expect
, token
);
910 flen
= strlen(filename
) + 1;
913 if (filename
[0] == '/') {
914 if (try_include("", filename
, flen
, list
, includepath
))
921 path
= stream
->next_path
;
928 /* Dir of input file is first dir to search for quoted includes */
929 set_stream_include_path(stream
);
930 path
= expect
? angle_includepath
: quote_includepath
;
933 /* Check the standard include paths.. */
934 if (do_include_path(path
, list
, token
, filename
, flen
))
937 error_die(token
->pos
, "unable to open '%s'", filename
);
940 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
942 return handle_include_path(stream
, list
, token
, 0);
945 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
947 return handle_include_path(stream
, list
, token
, 1);
950 static int handle_argv_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
952 return handle_include_path(stream
, list
, token
, 2);
955 static int token_different(struct token
*t1
, struct token
*t2
)
959 if (token_type(t1
) != token_type(t2
))
962 switch (token_type(t1
)) {
964 different
= t1
->ident
!= t2
->ident
;
966 case TOKEN_ARG_COUNT
:
969 case TOKEN_GNU_KLUDGE
:
973 different
= strcmp(t1
->number
, t2
->number
);
976 different
= t1
->special
!= t2
->special
;
978 case TOKEN_MACRO_ARGUMENT
:
979 case TOKEN_QUOTED_ARGUMENT
:
980 case TOKEN_STR_ARGUMENT
:
981 different
= t1
->argnum
!= t2
->argnum
;
983 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
984 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
985 different
= memcmp(t1
->embedded
, t2
->embedded
, 4);
988 case TOKEN_WIDE_CHAR
:
990 case TOKEN_WIDE_STRING
: {
991 struct string
*s1
, *s2
;
996 if (s1
->length
!= s2
->length
)
998 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
1008 static int token_list_different(struct token
*list1
, struct token
*list2
)
1013 if (!list1
|| !list2
)
1015 if (token_different(list1
, list2
))
1017 list1
= list1
->next
;
1018 list2
= list2
->next
;
1022 static inline void set_arg_count(struct token
*token
)
1024 token_type(token
) = TOKEN_ARG_COUNT
;
1025 token
->count
.normal
= token
->count
.quoted
=
1026 token
->count
.str
= token
->count
.vararg
= 0;
1029 static struct token
*parse_arguments(struct token
*list
)
1031 struct token
*arg
= list
->next
, *next
= list
;
1032 struct argcount
*count
= &list
->count
;
1034 set_arg_count(list
);
1036 if (match_op(arg
, ')')) {
1038 list
->next
= &eof_token_entry
;
1042 while (token_type(arg
) == TOKEN_IDENT
) {
1043 if (arg
->ident
== &__VA_ARGS___ident
)
1045 if (!++count
->normal
)
1049 if (match_op(next
, ',')) {
1050 set_arg_count(next
);
1055 if (match_op(next
, ')')) {
1056 set_arg_count(next
);
1058 arg
->next
->next
= &eof_token_entry
;
1062 /* normal cases are finished here */
1064 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
1065 if (match_op(next
->next
, ')')) {
1066 set_arg_count(next
);
1067 next
->count
.vararg
= 1;
1069 arg
->next
->next
= &eof_token_entry
;
1077 if (eof_token(next
)) {
1085 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
1087 token_type(arg
) = TOKEN_IDENT
;
1088 arg
->ident
= &__VA_ARGS___ident
;
1089 if (!match_op(next
, ')'))
1091 if (!++count
->normal
)
1093 set_arg_count(next
);
1094 next
->count
.vararg
= 1;
1096 arg
->next
->next
= &eof_token_entry
;
1100 if (eof_token(arg
)) {
1104 if (match_op(arg
, ','))
1111 sparse_error(arg
->pos
, "parameter name missing");
1114 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
1118 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1121 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1124 sparse_error(arg
->pos
, "too many arguments in macro definition");
1128 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1130 struct ident
*ident
= token
->ident
;
1133 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1136 arglist
= arglist
->next
;
1138 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1139 if (arglist
->ident
== ident
) {
1140 struct argcount
*count
= &arglist
->next
->count
;
1144 token_type(token
) = type
;
1146 case TOKEN_MACRO_ARGUMENT
:
1147 n
= ++count
->normal
;
1149 case TOKEN_QUOTED_ARGUMENT
:
1150 n
= ++count
->quoted
;
1156 return count
->vararg
? 2 : 1;
1158 * XXX - need saner handling of that
1159 * (>= 1024 instances of argument)
1161 token_type(token
) = TOKEN_ERROR
;
1168 static struct token
*handle_hash(struct token
**p
, struct token
*arglist
)
1170 struct token
*token
= *p
;
1172 struct token
*next
= token
->next
;
1173 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1175 next
->pos
.whitespace
= token
->pos
.whitespace
;
1176 __free_token(token
);
1179 token
->pos
.noexpand
= 1;
1184 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1188 /* token->next is ## */
1189 static struct token
*handle_hashhash(struct token
*token
, struct token
*arglist
)
1191 struct token
*last
= token
;
1192 struct token
*concat
;
1193 int state
= match_op(token
, ',');
1195 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1201 /* eat duplicate ## */
1202 concat
= token
->next
;
1203 while (match_op(t
= concat
->next
, SPECIAL_HASHHASH
)) {
1205 __free_token(concat
);
1208 token_type(concat
) = TOKEN_CONCAT
;
1213 if (match_op(t
, '#')) {
1214 t
= handle_hash(&concat
->next
, arglist
);
1219 is_arg
= try_arg(t
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1221 if (state
== 1 && is_arg
) {
1225 state
= match_op(t
, ',');
1229 if (!match_op(token
->next
, SPECIAL_HASHHASH
))
1232 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1234 token_type(last
) = TOKEN_GNU_KLUDGE
;
1238 sparse_error(concat
->pos
, "'##' cannot appear at the ends of macro expansion");
1242 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1244 struct token
*token
= expansion
;
1247 if (match_op(token
, SPECIAL_HASHHASH
))
1250 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1251 if (match_op(token
, '#')) {
1252 token
= handle_hash(p
, arglist
);
1256 if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1257 token
= handle_hashhash(token
, arglist
);
1261 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1263 if (token_type(token
) == TOKEN_ERROR
)
1266 token
= alloc_token(&expansion
->pos
);
1267 token_type(token
) = TOKEN_UNTAINT
;
1268 token
->ident
= name
;
1274 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1277 sparse_error(token
->pos
, "too many instances of argument in body");
1281 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1283 struct token
*arglist
, *expansion
;
1284 struct token
*left
= token
->next
;
1289 if (token_type(left
) != TOKEN_IDENT
) {
1290 sparse_error(token
->pos
, "expected identifier to 'define'");
1297 expansion
= left
->next
;
1298 if (!expansion
->pos
.whitespace
) {
1299 if (match_op(expansion
, '(')) {
1300 arglist
= expansion
;
1301 expansion
= parse_arguments(expansion
);
1304 } else if (!eof_token(expansion
)) {
1305 warning(expansion
->pos
,
1306 "no whitespace before object-like macro body");
1310 expansion
= parse_expansion(expansion
, arglist
, name
);
1315 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1319 if (attr
< sym
->attr
)
1322 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1324 if (token_list_different(sym
->expansion
, expansion
) ||
1325 token_list_different(sym
->arglist
, arglist
)) {
1327 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1328 || sym
->used_in
== file_scope
) {
1329 warning(left
->pos
, "preprocessor token %.*s redefined",
1330 name
->len
, name
->name
);
1331 info(sym
->pos
, "this was the original definition");
1337 if (!sym
|| sym
->scope
!= file_scope
) {
1338 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1339 bind_symbol(sym
, name
, NS_MACRO
);
1344 sym
->expansion
= expansion
;
1345 sym
->arglist
= arglist
;
1346 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1349 sym
->namespace = NS_MACRO
;
1350 sym
->used_in
= NULL
;
1356 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1358 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1361 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1363 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1366 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1368 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1371 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1373 struct token
*left
= token
->next
;
1376 if (token_type(left
) != TOKEN_IDENT
) {
1377 sparse_error(token
->pos
, "expected identifier to 'undef'");
1381 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1383 if (attr
< sym
->attr
)
1385 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1387 } else if (attr
<= SYM_ATTR_NORMAL
)
1390 if (!sym
|| sym
->scope
!= file_scope
) {
1391 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1392 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1395 sym
->namespace = NS_UNDEF
;
1396 sym
->used_in
= NULL
;
1402 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1404 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1407 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1409 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1412 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1414 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1415 free_preprocessor_line(token
->next
);
1416 token
->next
= stream
->top_if
;
1417 stream
->top_if
= token
;
1418 if (false_nesting
|| true != 1)
1423 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1425 struct token
*next
= token
->next
;
1427 if (token_type(next
) == TOKEN_IDENT
) {
1428 arg
= token_defined(next
);
1430 dirty_stream(stream
);
1432 sparse_error(token
->pos
, "expected preprocessor identifier");
1435 return preprocessor_if(stream
, token
, arg
);
1438 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1440 struct token
*next
= token
->next
;
1442 if (token_type(next
) == TOKEN_IDENT
) {
1443 if (!stream
->dirty
&& !stream
->ifndef
) {
1444 if (!stream
->protect
) {
1445 stream
->ifndef
= token
;
1446 stream
->protect
= next
->ident
;
1447 } else if (stream
->protect
== next
->ident
) {
1448 stream
->ifndef
= token
;
1452 arg
= !token_defined(next
);
1454 dirty_stream(stream
);
1456 sparse_error(token
->pos
, "expected preprocessor identifier");
1460 return preprocessor_if(stream
, token
, arg
);
1463 static const char *show_token_sequence(struct token
*token
, int quote
);
1466 * Expression handling for #if and #elif; it differs from normal expansion
1467 * due to special treatment of "defined".
1469 static int expression_value(struct token
**where
)
1471 struct expression
*expr
;
1473 struct token
**list
= where
, **beginning
= NULL
;
1477 while (!eof_token(p
= scan_next(list
))) {
1480 if (token_type(p
) != TOKEN_IDENT
)
1482 if (p
->ident
== &defined_ident
) {
1487 if (!expand_one_symbol(list
))
1489 if (token_type(p
) != TOKEN_IDENT
)
1491 token_type(p
) = TOKEN_ZERO_IDENT
;
1494 if (match_op(p
, '(')) {
1498 replace_with_defined(p
);
1503 if (token_type(p
) == TOKEN_IDENT
)
1507 replace_with_defined(p
);
1512 if (!match_op(p
, ')'))
1513 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1520 p
= constant_expression(*where
, &expr
);
1522 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
, 0));
1523 value
= get_expression_value(expr
);
1527 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1531 value
= expression_value(&token
->next
);
1533 dirty_stream(stream
);
1534 return preprocessor_if(stream
, token
, value
);
1537 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1539 struct token
*top_if
= stream
->top_if
;
1543 nesting_error(stream
);
1544 sparse_error(token
->pos
, "unmatched #elif within stream");
1548 if (token_type(top_if
) == TOKEN_ELSE
) {
1549 nesting_error(stream
);
1550 sparse_error(token
->pos
, "#elif after #else");
1556 dirty_stream(stream
);
1557 if (token_type(top_if
) != TOKEN_IF
)
1559 if (false_nesting
) {
1561 if (!expression_value(&token
->next
))
1565 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1570 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1572 struct token
*top_if
= stream
->top_if
;
1576 nesting_error(stream
);
1577 sparse_error(token
->pos
, "unmatched #else within stream");
1581 if (token_type(top_if
) == TOKEN_ELSE
) {
1582 nesting_error(stream
);
1583 sparse_error(token
->pos
, "#else after #else");
1585 if (false_nesting
) {
1586 if (token_type(top_if
) == TOKEN_IF
)
1591 token_type(top_if
) = TOKEN_ELSE
;
1595 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1597 struct token
*top_if
= stream
->top_if
;
1600 nesting_error(stream
);
1601 sparse_error(token
->pos
, "unmatched #endif in stream");
1606 stream
->top_if
= top_if
->next
;
1607 __free_token(top_if
);
1611 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1613 warning(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1617 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1619 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1623 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1626 * Do we have any non-system includes?
1627 * Clear them out if so..
1629 *sys_includepath
= NULL
;
1633 static inline void update_inc_ptrs(const char ***where
)
1636 if (*where
<= dirafter_includepath
) {
1637 dirafter_includepath
++;
1638 /* If this was the entry that we prepend, don't
1639 * rise the lower entries, even if they are at
1640 * the same level. */
1641 if (where
== &dirafter_includepath
)
1644 if (*where
<= sys_includepath
) {
1646 if (where
== &sys_includepath
)
1649 if (*where
<= isys_includepath
) {
1651 if (where
== &isys_includepath
)
1655 /* angle_includepath is actually never updated, since we
1656 * don't suppport -iquote rught now. May change some day. */
1657 if (*where
<= angle_includepath
) {
1658 angle_includepath
++;
1659 if (where
== &angle_includepath
)
1664 /* Add a path before 'where' and update the pointers associated with the
1665 * includepath array */
1666 static void add_path_entry(struct token
*token
, const char *path
,
1667 const char ***where
)
1672 /* Need one free entry.. */
1673 if (includepath
[INCLUDEPATHS
-2])
1674 error_die(token
->pos
, "too many include path entries");
1676 /* check that this is not a duplicate */
1679 if (strcmp(*dst
, path
) == 0)
1686 update_inc_ptrs(where
);
1689 * Move them all up starting at dst,
1690 * insert the new entry..
1693 const char *tmp
= *dst
;
1700 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1703 token
= token
->next
;
1704 if (eof_token(token
))
1706 if (token_type(token
) != TOKEN_STRING
) {
1707 warning(token
->pos
, "expected path string");
1710 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1714 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1717 token
= token
->next
;
1718 if (eof_token(token
))
1720 if (token_type(token
) != TOKEN_STRING
) {
1721 sparse_error(token
->pos
, "expected path string");
1724 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1728 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
1731 token
= token
->next
;
1732 if (eof_token(token
))
1734 if (token_type(token
) != TOKEN_STRING
) {
1735 sparse_error(token
->pos
, "expected path string");
1738 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1742 /* Add to end on includepath list - no pointer updates */
1743 static void add_dirafter_entry(struct token
*token
, const char *path
)
1745 const char **dst
= includepath
;
1747 /* Need one free entry.. */
1748 if (includepath
[INCLUDEPATHS
-2])
1749 error_die(token
->pos
, "too many include path entries");
1751 /* Add to the end */
1759 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1762 token
= token
->next
;
1763 if (eof_token(token
))
1765 if (token_type(token
) != TOKEN_STRING
) {
1766 sparse_error(token
->pos
, "expected path string");
1769 add_dirafter_entry(token
, token
->string
->data
);
1773 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1778 * Split the include path. Any directories specified with `-I'
1779 * options before `-I-' are searched only for headers requested with
1780 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1781 * If additional directories are specified with `-I' options after
1782 * the `-I-', those directories are searched for all `#include'
1784 * In addition, `-I-' inhibits the use of the directory of the current
1785 * file directory as the first search directory for `#include "FILE"'.
1787 quote_includepath
= includepath
+1;
1788 angle_includepath
= sys_includepath
;
1793 * We replace "#pragma xxx" with "__pragma__" in the token
1794 * stream. Just as an example.
1796 * We'll just #define that away for now, but the theory here
1797 * is that we can use this to insert arbitrary token sequences
1798 * to turn the pragmas into internal front-end sequences for
1799 * when we actually start caring about them.
1801 * So eventually this will turn into some kind of extended
1802 * __attribute__() like thing, except called __pragma__(xxx).
1804 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1806 struct token
*next
= *line
;
1808 if (match_ident(token
->next
, &once_ident
) && eof_token(token
->next
->next
)) {
1812 token
->ident
= &pragma_ident
;
1813 token
->pos
.newline
= 1;
1814 token
->pos
.whitespace
= 1;
1822 * We ignore #line for now.
1824 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1829 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1831 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
, 0));
1836 static void init_preprocessor(void)
1839 int stream
= init_stream("preprocessor", -1, includepath
);
1842 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1844 { "define", handle_define
},
1845 { "weak_define", handle_weak_define
},
1846 { "strong_define", handle_strong_define
},
1847 { "undef", handle_undef
},
1848 { "strong_undef", handle_strong_undef
},
1849 { "warning", handle_warning
},
1850 { "error", handle_error
},
1851 { "include", handle_include
},
1852 { "include_next", handle_include_next
},
1853 { "pragma", handle_pragma
},
1854 { "line", handle_line
},
1856 // our internal preprocessor tokens
1857 { "nostdinc", handle_nostdinc
},
1858 { "add_include", handle_add_include
},
1859 { "add_isystem", handle_add_isystem
},
1860 { "add_system", handle_add_system
},
1861 { "add_dirafter", handle_add_dirafter
},
1862 { "split_include", handle_split_include
},
1863 { "argv_include", handle_argv_include
},
1865 { "ifdef", handle_ifdef
},
1866 { "ifndef", handle_ifndef
},
1867 { "else", handle_else
},
1868 { "endif", handle_endif
},
1869 { "if", handle_if
},
1870 { "elif", handle_elif
},
1873 for (i
= 0; i
< ARRAY_SIZE(normal
); i
++) {
1875 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1876 sym
->handler
= normal
[i
].handler
;
1879 for (i
= 0; i
< ARRAY_SIZE(special
); i
++) {
1881 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1882 sym
->handler
= special
[i
].handler
;
1888 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1890 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1891 struct token
*token
= start
->next
;
1894 if (eof_token(token
))
1897 if (token_type(token
) == TOKEN_IDENT
) {
1898 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1900 handler
= sym
->handler
;
1901 is_normal
= sym
->normal
;
1903 handler
= handle_nondirective
;
1905 } else if (token_type(token
) == TOKEN_NUMBER
) {
1906 handler
= handle_line
;
1908 handler
= handle_nondirective
;
1912 dirty_stream(stream
);
1916 if (!handler(stream
, line
, token
)) /* all set */
1920 free_preprocessor_line(token
);
1923 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1925 struct token
*start
= *line
, *next
;
1926 struct token
**tp
= &start
->next
;
1930 if (next
->pos
.newline
)
1935 *tp
= &eof_token_entry
;
1936 handle_preprocessor_line(stream
, line
, start
);
1939 static void do_preprocess(struct token
**list
)
1943 while (!eof_token(next
= scan_next(list
))) {
1944 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1946 if (next
->pos
.newline
&& match_op(next
, '#')) {
1947 if (!next
->pos
.noexpand
) {
1948 preprocessor_line(stream
, list
);
1949 __free_token(next
); /* Free the '#' token */
1954 switch (token_type(next
)) {
1955 case TOKEN_STREAMEND
:
1956 if (stream
->top_if
) {
1957 nesting_error(stream
);
1958 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1959 stream
->top_if
= NULL
;
1963 stream
->constant
= CONSTANT_FILE_YES
;
1966 case TOKEN_STREAMBEGIN
:
1971 dirty_stream(stream
);
1972 if (false_nesting
) {
1978 if (token_type(next
) != TOKEN_IDENT
||
1979 expand_one_symbol(list
))
1985 struct token
* preprocess(struct token
*token
)
1988 init_preprocessor();
1989 do_preprocess(&token
);
1991 // Drop all expressions from preprocessing, they're not used any more.
1992 // This is not true when we have multiple files, though ;/
1993 // clear_expression_alloc();