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 sym
->used_in
= file_scope
;
168 return expand(list
, sym
);
170 if (token
->ident
== &__LINE___ident
) {
171 replace_with_integer(token
, token
->pos
.line
);
172 } else if (token
->ident
== &__FILE___ident
) {
173 replace_with_string(token
, stream_name(token
->pos
.stream
));
174 } else if (token
->ident
== &__DATE___ident
) {
177 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
178 replace_with_string(token
, buffer
);
179 } else if (token
->ident
== &__TIME___ident
) {
182 strftime(buffer
, 9, "%T", localtime(&t
));
183 replace_with_string(token
, buffer
);
188 static inline struct token
*scan_next(struct token
**where
)
190 struct token
*token
= *where
;
191 if (token_type(token
) != TOKEN_UNTAINT
)
194 token
->ident
->tainted
= 0;
196 } while (token_type(token
) == TOKEN_UNTAINT
);
201 static void expand_list(struct token
**list
)
204 while (!eof_token(next
= scan_next(list
))) {
205 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
210 static void preprocessor_line(struct stream
*stream
, struct token
**line
);
212 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
214 struct stream
*stream
= input_streams
+ prev
->pos
.stream
;
215 struct token
**p
= &prev
->next
;
219 while (!eof_token(next
= scan_next(p
))) {
220 if (next
->pos
.newline
&& match_op(next
, '#')) {
221 if (!next
->pos
.noexpand
) {
222 sparse_error(next
->pos
,
223 "directive in argument list");
224 preprocessor_line(stream
, p
);
225 __free_token(next
); /* Free the '#' token */
229 switch (token_type(next
)) {
230 case TOKEN_STREAMEND
:
231 case TOKEN_STREAMBEGIN
:
232 *p
= &eof_token_entry
;
240 if (match_op(next
, '(')) {
242 } else if (match_op(next
, ')')) {
245 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
248 next
->pos
.stream
= pos
->stream
;
249 next
->pos
.line
= pos
->line
;
250 next
->pos
.pos
= pos
->pos
;
253 *p
= &eof_token_entry
;
258 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
263 struct token
*expanded
;
270 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
272 int wanted
= arglist
->count
.normal
;
273 struct token
*next
= NULL
;
276 arglist
= arglist
->next
; /* skip counter */
279 next
= collect_arg(start
, 0, &what
->pos
);
282 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
287 for (count
= 0; count
< wanted
; count
++) {
288 struct argcount
*p
= &arglist
->next
->count
;
289 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
290 arglist
= arglist
->next
->next
;
293 args
[count
].arg
= start
->next
;
294 args
[count
].n_normal
= p
->normal
;
295 args
[count
].n_quoted
= p
->quoted
;
296 args
[count
].n_str
= p
->str
;
297 if (match_op(next
, ')')) {
303 if (count
== wanted
&& !match_op(next
, ')'))
305 if (count
== wanted
- 1) {
306 struct argcount
*p
= &arglist
->next
->count
;
309 args
[count
].arg
= NULL
;
310 args
[count
].n_normal
= p
->normal
;
311 args
[count
].n_quoted
= p
->quoted
;
312 args
[count
].n_str
= p
->str
;
314 if (count
< wanted
- 1)
317 what
->next
= next
->next
;
321 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
322 show_token(what
), wanted
, count
);
325 while (match_op(next
, ',')) {
326 next
= collect_arg(next
, 0, &what
->pos
);
331 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
332 show_token(what
), count
, wanted
);
335 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
338 what
->next
= next
->next
;
342 static struct token
*dup_list(struct token
*list
)
344 struct token
*res
= NULL
;
345 struct token
**p
= &res
;
347 while (!eof_token(list
)) {
348 struct token
*newtok
= __alloc_token(0);
357 static const char *show_token_sequence(struct token
*token
, int quote
)
359 static char buffer
[MAX_STRING
];
363 if (!token
&& !quote
)
365 while (!eof_token(token
)) {
366 const char *val
= quote
? quote_token(token
) : show_token(token
);
367 int len
= strlen(val
);
369 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
370 sparse_error(token
->pos
, "too long token expansion");
376 memcpy(ptr
, val
, len
);
379 whitespace
= token
->pos
.whitespace
;
385 static struct token
*stringify(struct token
*arg
)
387 const char *s
= show_token_sequence(arg
, 1);
388 int size
= strlen(s
)+1;
389 struct token
*token
= __alloc_token(0);
390 struct string
*string
= __alloc_string(size
);
392 memcpy(string
->data
, s
, size
);
393 string
->length
= size
;
394 token
->pos
= arg
->pos
;
395 token_type(token
) = TOKEN_STRING
;
396 token
->string
= string
;
397 token
->next
= &eof_token_entry
;
401 static void expand_arguments(int count
, struct arg
*args
)
404 for (i
= 0; i
< count
; i
++) {
405 struct token
*arg
= args
[i
].arg
;
407 arg
= &eof_token_entry
;
409 args
[i
].str
= stringify(arg
);
410 if (args
[i
].n_normal
) {
411 if (!args
[i
].n_quoted
) {
412 args
[i
].expanded
= arg
;
414 } else if (eof_token(arg
)) {
415 args
[i
].expanded
= arg
;
417 args
[i
].expanded
= dup_list(arg
);
419 expand_list(&args
[i
].expanded
);
425 * Possibly valid combinations:
426 * - ident + ident -> ident
427 * - ident + number -> ident unless number contains '.', '+' or '-'.
428 * - 'L' + char constant -> wide char constant
429 * - 'L' + string literal -> wide string literal
430 * - number + number -> number
431 * - number + ident -> number
432 * - number + '.' -> number
433 * - number + '+' or '-' -> number, if number used to end on [eEpP].
434 * - '.' + number -> number, if number used to start with a digit.
435 * - special + special -> either special or an error.
437 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
440 enum token_type t1
= token_type(left
), t2
= token_type(right
);
442 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
445 if (t1
== TOKEN_IDENT
&& left
->ident
== &L_ident
) {
446 if (t2
>= TOKEN_CHAR
&& t2
< TOKEN_WIDE_CHAR
)
447 return t2
+ TOKEN_WIDE_CHAR
- TOKEN_CHAR
;
448 if (t2
== TOKEN_STRING
)
449 return TOKEN_WIDE_STRING
;
452 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
455 strcpy(p
, show_token(left
));
456 strcat(p
, show_token(right
));
462 if (t1
== TOKEN_IDENT
) {
463 if (t2
== TOKEN_SPECIAL
)
465 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
470 if (t1
== TOKEN_NUMBER
) {
471 if (t2
== TOKEN_SPECIAL
) {
472 switch (right
->special
) {
476 if (strchr("eEpP", p
[len
- 2]))
485 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
488 return TOKEN_SPECIAL
;
491 static int merge(struct token
*left
, struct token
*right
)
493 static char buffer
[512];
494 enum token_type res
= combine(left
, right
, buffer
);
499 left
->ident
= built_in_ident(buffer
);
500 left
->pos
.noexpand
= 0;
504 char *number
= __alloc_bytes(strlen(buffer
) + 1);
505 memcpy(number
, buffer
, strlen(buffer
) + 1);
506 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
507 left
->number
= number
;
512 if (buffer
[2] && buffer
[3])
514 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
515 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
522 case TOKEN_WIDE_CHAR
:
523 case TOKEN_WIDE_STRING
:
524 token_type(left
) = res
;
525 left
->pos
.noexpand
= 0;
526 left
->string
= right
->string
;
529 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
530 token_type(left
) = res
;
531 left
->pos
.noexpand
= 0;
532 memcpy(left
->embedded
, right
->embedded
, 4);
538 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
542 static struct token
*dup_token(struct token
*token
, struct position
*streampos
)
544 struct token
*alloc
= alloc_token(streampos
);
545 token_type(alloc
) = token_type(token
);
546 alloc
->pos
.newline
= token
->pos
.newline
;
547 alloc
->pos
.whitespace
= token
->pos
.whitespace
;
548 alloc
->number
= token
->number
;
549 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
553 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
555 int need_copy
= --*count
;
556 while (!eof_token(list
)) {
559 token
= dup_token(list
, &list
->pos
);
562 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
563 token
->pos
.noexpand
= 1;
565 where
= &token
->next
;
568 *where
= &eof_token_entry
;
572 static int handle_kludge(struct token
**p
, struct arg
*args
)
574 struct token
*t
= (*p
)->next
->next
;
576 struct arg
*v
= &args
[t
->argnum
];
577 if (token_type(t
->next
) != TOKEN_CONCAT
) {
579 /* ignore the first ## */
583 /* skip the entire thing */
587 if (v
->arg
&& !eof_token(v
->arg
))
588 return 0; /* no magic */
593 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
595 struct position
*base_pos
= &(*list
)->pos
;
597 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
599 for (; !eof_token(body
); body
= body
->next
) {
600 struct token
*added
, *arg
;
604 switch (token_type(body
)) {
605 case TOKEN_GNU_KLUDGE
:
607 * GNU kludge: if we had <comma>##<vararg>, behaviour
608 * depends on whether we had enough arguments to have
609 * a vararg. If we did, ## is just ignored. Otherwise
610 * both , and ## are ignored. Worse, there can be
611 * an arbitrary number of ##<arg> in between; if all of
612 * those are empty, we act as if they hadn't been there,
613 * otherwise we act as if the kludge didn't exist.
616 if (handle_kludge(&body
, args
)) {
623 added
= dup_token(t
, base_pos
);
624 token_type(added
) = TOKEN_SPECIAL
;
628 case TOKEN_STR_ARGUMENT
:
629 arg
= args
[body
->argnum
].str
;
630 count
= &args
[body
->argnum
].n_str
;
633 case TOKEN_QUOTED_ARGUMENT
:
634 arg
= args
[body
->argnum
].arg
;
635 count
= &args
[body
->argnum
].n_quoted
;
636 if (!arg
|| eof_token(arg
)) {
645 case TOKEN_MACRO_ARGUMENT
:
646 arg
= args
[body
->argnum
].expanded
;
647 count
= &args
[body
->argnum
].n_normal
;
648 if (eof_token(arg
)) {
653 tail
= copy(&added
, arg
, count
);
654 added
->pos
.newline
= body
->pos
.newline
;
655 added
->pos
.whitespace
= body
->pos
.whitespace
;
659 if (state
== Placeholder
)
666 added
= dup_token(body
, base_pos
);
667 if (added
->ident
->tainted
)
668 added
->pos
.noexpand
= 1;
673 added
= dup_token(body
, base_pos
);
679 * if we got to doing real concatenation, we already have
680 * added something into the list, so containing_token() is OK.
682 if (state
== Concat
&& merge(containing_token(list
), added
)) {
684 if (tail
!= &added
->next
)
692 *list
= &eof_token_entry
;
696 static int expand(struct token
**list
, struct symbol
*sym
)
699 struct token
*token
= *list
;
700 struct ident
*expanding
= token
->ident
;
702 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
703 struct arg args
[nargs
];
705 if (expanding
->tainted
) {
706 token
->pos
.noexpand
= 1;
711 if (!match_op(scan_next(&token
->next
), '('))
713 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
715 expand_arguments(nargs
, args
);
718 expanding
->tainted
= 1;
721 tail
= substitute(list
, sym
->expansion
, args
);
723 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
724 * We still can lose the newline flag if the sucker expands to nothing,
725 * but the price of dealing with that is probably too high (we'd need
726 * to collect the flags during scan_next())
728 (*list
)->pos
.newline
= token
->pos
.newline
;
729 (*list
)->pos
.whitespace
= token
->pos
.whitespace
;
735 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
737 static char buffer
[256];
740 while (!eof_token(token
) && !match_op(token
, endop
)) {
742 const char *val
= token
->string
->data
;
743 if (token_type(token
) != TOKEN_STRING
)
744 val
= show_token(token
);
746 memcpy(ptr
, val
, len
);
751 if (endop
&& !match_op(token
, endop
))
752 sparse_error(start
->pos
, "expected '>' at end of filename");
756 static int already_tokenized(const char *path
)
760 for (stream
= *hash_stream(path
); stream
>= 0 ; stream
= next
) {
761 struct stream
*s
= input_streams
+ stream
;
763 next
= s
->next_stream
;
765 if (strcmp(path
, s
->name
))
769 if (s
->constant
!= CONSTANT_FILE_YES
)
771 if (strcmp(path
, s
->name
))
773 if (s
->protect
&& !lookup_macro(s
->protect
))
780 /* Handle include of header files.
781 * The relevant options are made compatible with gcc. The only options that
782 * are not supported is -withprefix and friends.
784 * Three set of include paths are known:
785 * quote_includepath: Path to search when using #include "file.h"
786 * angle_includepath: Paths to search when using #include <file.h>
787 * isys_includepath: Paths specified with -isystem, come before the
788 * built-in system include paths. Gcc would suppress
789 * warnings from system headers. Here we separate
790 * them from the angle_ ones to keep search ordering.
792 * sys_includepath: Built-in include paths.
793 * dirafter_includepath Paths added with -dirafter.
795 * The above is implemented as one array with pointers
797 * quote_includepath ---> | |
801 * angle_includepath ---> | |
803 * isys_includepath ---> | |
805 * sys_includepath ---> | |
807 * dirafter_includepath -> | |
810 * -I dir insert dir just before isys_includepath and move the rest
811 * -I- makes all dirs specified with -I before to quote dirs only and
812 * angle_includepath is set equal to isys_includepath.
813 * -nostdinc removes all sys dirs by storing NULL in entry pointed
814 * to by * sys_includepath. Note that this will reset all dirs built-in
815 * and added before -nostdinc by -isystem and -idirafter.
816 * -isystem dir adds dir where isys_includepath points adding this dir as
818 * -idirafter dir adds dir to the end of the list
821 static void set_stream_include_path(struct stream
*stream
)
823 const char *path
= stream
->path
;
825 const char *p
= strrchr(stream
->name
, '/');
828 int len
= p
- stream
->name
+ 1;
829 char *m
= malloc(len
+1);
830 /* This includes the final "/" */
831 memcpy(m
, stream
->name
, len
);
837 includepath
[0] = path
;
840 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
843 int plen
= strlen(path
);
844 static char fullname
[PATH_MAX
];
846 memcpy(fullname
, path
, plen
);
847 if (plen
&& path
[plen
-1] != '/') {
848 fullname
[plen
] = '/';
851 memcpy(fullname
+plen
, filename
, flen
);
852 if (already_tokenized(fullname
))
854 fd
= open(fullname
, O_RDONLY
);
856 char * streamname
= __alloc_bytes(plen
+ flen
);
857 memcpy(streamname
, fullname
, plen
+ flen
);
858 *where
= tokenize(streamname
, fd
, *where
, next_path
);
865 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
869 while ((path
= *pptr
++) != NULL
) {
870 if (!try_include(path
, filename
, flen
, list
, pptr
))
877 static int free_preprocessor_line(struct token
*token
)
879 while (token_type(token
) != TOKEN_EOF
) {
880 struct token
*free
= token
;
887 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, int how
)
889 const char *filename
;
897 if (!match_op(next
, '<')) {
898 expand_list(&token
->next
);
901 if (match_op(token
->next
, '<')) {
908 filename
= token_name_sequence(token
, expect
, token
);
909 flen
= strlen(filename
) + 1;
912 if (filename
[0] == '/') {
913 if (try_include("", filename
, flen
, list
, includepath
))
920 path
= stream
->next_path
;
927 /* Dir of input file is first dir to search for quoted includes */
928 set_stream_include_path(stream
);
929 path
= expect
? angle_includepath
: quote_includepath
;
932 /* Check the standard include paths.. */
933 if (do_include_path(path
, list
, token
, filename
, flen
))
936 error_die(token
->pos
, "unable to open '%s'", filename
);
939 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
941 return handle_include_path(stream
, list
, token
, 0);
944 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
946 return handle_include_path(stream
, list
, token
, 1);
949 static int handle_argv_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
951 return handle_include_path(stream
, list
, token
, 2);
954 static int token_different(struct token
*t1
, struct token
*t2
)
958 if (token_type(t1
) != token_type(t2
))
961 switch (token_type(t1
)) {
963 different
= t1
->ident
!= t2
->ident
;
965 case TOKEN_ARG_COUNT
:
968 case TOKEN_GNU_KLUDGE
:
972 different
= strcmp(t1
->number
, t2
->number
);
975 different
= t1
->special
!= t2
->special
;
977 case TOKEN_MACRO_ARGUMENT
:
978 case TOKEN_QUOTED_ARGUMENT
:
979 case TOKEN_STR_ARGUMENT
:
980 different
= t1
->argnum
!= t2
->argnum
;
982 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
983 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
984 different
= memcmp(t1
->embedded
, t2
->embedded
, 4);
987 case TOKEN_WIDE_CHAR
:
989 case TOKEN_WIDE_STRING
: {
990 struct string
*s1
, *s2
;
995 if (s1
->length
!= s2
->length
)
997 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
1007 static int token_list_different(struct token
*list1
, struct token
*list2
)
1012 if (!list1
|| !list2
)
1014 if (token_different(list1
, list2
))
1016 list1
= list1
->next
;
1017 list2
= list2
->next
;
1021 static inline void set_arg_count(struct token
*token
)
1023 token_type(token
) = TOKEN_ARG_COUNT
;
1024 token
->count
.normal
= token
->count
.quoted
=
1025 token
->count
.str
= token
->count
.vararg
= 0;
1028 static struct token
*parse_arguments(struct token
*list
)
1030 struct token
*arg
= list
->next
, *next
= list
;
1031 struct argcount
*count
= &list
->count
;
1033 set_arg_count(list
);
1035 if (match_op(arg
, ')')) {
1037 list
->next
= &eof_token_entry
;
1041 while (token_type(arg
) == TOKEN_IDENT
) {
1042 if (arg
->ident
== &__VA_ARGS___ident
)
1044 if (!++count
->normal
)
1048 if (match_op(next
, ',')) {
1049 set_arg_count(next
);
1054 if (match_op(next
, ')')) {
1055 set_arg_count(next
);
1057 arg
->next
->next
= &eof_token_entry
;
1061 /* normal cases are finished here */
1063 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
1064 if (match_op(next
->next
, ')')) {
1065 set_arg_count(next
);
1066 next
->count
.vararg
= 1;
1068 arg
->next
->next
= &eof_token_entry
;
1076 if (eof_token(next
)) {
1084 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
1086 token_type(arg
) = TOKEN_IDENT
;
1087 arg
->ident
= &__VA_ARGS___ident
;
1088 if (!match_op(next
, ')'))
1090 if (!++count
->normal
)
1092 set_arg_count(next
);
1093 next
->count
.vararg
= 1;
1095 arg
->next
->next
= &eof_token_entry
;
1099 if (eof_token(arg
)) {
1103 if (match_op(arg
, ','))
1110 sparse_error(arg
->pos
, "parameter name missing");
1113 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
1117 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1120 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1123 sparse_error(arg
->pos
, "too many arguments in macro definition");
1127 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1129 struct ident
*ident
= token
->ident
;
1132 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1135 arglist
= arglist
->next
;
1137 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1138 if (arglist
->ident
== ident
) {
1139 struct argcount
*count
= &arglist
->next
->count
;
1143 token_type(token
) = type
;
1145 case TOKEN_MACRO_ARGUMENT
:
1146 n
= ++count
->normal
;
1148 case TOKEN_QUOTED_ARGUMENT
:
1149 n
= ++count
->quoted
;
1155 return count
->vararg
? 2 : 1;
1157 * XXX - need saner handling of that
1158 * (>= 1024 instances of argument)
1160 token_type(token
) = TOKEN_ERROR
;
1167 static struct token
*handle_hash(struct token
**p
, struct token
*arglist
)
1169 struct token
*token
= *p
;
1171 struct token
*next
= token
->next
;
1172 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1174 next
->pos
.whitespace
= token
->pos
.whitespace
;
1175 __free_token(token
);
1178 token
->pos
.noexpand
= 1;
1183 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1187 /* token->next is ## */
1188 static struct token
*handle_hashhash(struct token
*token
, struct token
*arglist
)
1190 struct token
*last
= token
;
1191 struct token
*concat
;
1192 int state
= match_op(token
, ',');
1194 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1200 /* eat duplicate ## */
1201 concat
= token
->next
;
1202 while (match_op(t
= concat
->next
, SPECIAL_HASHHASH
)) {
1204 __free_token(concat
);
1207 token_type(concat
) = TOKEN_CONCAT
;
1212 if (match_op(t
, '#')) {
1213 t
= handle_hash(&concat
->next
, arglist
);
1218 is_arg
= try_arg(t
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1220 if (state
== 1 && is_arg
) {
1224 state
= match_op(t
, ',');
1228 if (!match_op(token
->next
, SPECIAL_HASHHASH
))
1231 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1233 token_type(last
) = TOKEN_GNU_KLUDGE
;
1237 sparse_error(concat
->pos
, "'##' cannot appear at the ends of macro expansion");
1241 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1243 struct token
*token
= expansion
;
1246 if (match_op(token
, SPECIAL_HASHHASH
))
1249 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1250 if (match_op(token
, '#')) {
1251 token
= handle_hash(p
, arglist
);
1255 if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1256 token
= handle_hashhash(token
, arglist
);
1260 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1262 if (token_type(token
) == TOKEN_ERROR
)
1265 token
= alloc_token(&expansion
->pos
);
1266 token_type(token
) = TOKEN_UNTAINT
;
1267 token
->ident
= name
;
1273 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1276 sparse_error(token
->pos
, "too many instances of argument in body");
1280 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1282 struct token
*arglist
, *expansion
;
1283 struct token
*left
= token
->next
;
1288 if (token_type(left
) != TOKEN_IDENT
) {
1289 sparse_error(token
->pos
, "expected identifier to 'define'");
1296 expansion
= left
->next
;
1297 if (!expansion
->pos
.whitespace
) {
1298 if (match_op(expansion
, '(')) {
1299 arglist
= expansion
;
1300 expansion
= parse_arguments(expansion
);
1303 } else if (!eof_token(expansion
)) {
1304 warning(expansion
->pos
,
1305 "no whitespace before object-like macro body");
1309 expansion
= parse_expansion(expansion
, arglist
, name
);
1314 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1318 if (attr
< sym
->attr
)
1321 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1323 if (token_list_different(sym
->expansion
, expansion
) ||
1324 token_list_different(sym
->arglist
, arglist
)) {
1326 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1327 || sym
->used_in
== file_scope
) {
1328 warning(left
->pos
, "preprocessor token %.*s redefined",
1329 name
->len
, name
->name
);
1330 info(sym
->pos
, "this was the original definition");
1336 if (!sym
|| sym
->scope
!= file_scope
) {
1337 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1338 bind_symbol(sym
, name
, NS_MACRO
);
1343 sym
->expansion
= expansion
;
1344 sym
->arglist
= arglist
;
1345 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1348 sym
->namespace = NS_MACRO
;
1349 sym
->used_in
= NULL
;
1355 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1357 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1360 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1362 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1365 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1367 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1370 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1372 struct token
*left
= token
->next
;
1375 if (token_type(left
) != TOKEN_IDENT
) {
1376 sparse_error(token
->pos
, "expected identifier to 'undef'");
1380 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1382 if (attr
< sym
->attr
)
1384 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1386 } else if (attr
<= SYM_ATTR_NORMAL
)
1389 if (!sym
|| sym
->scope
!= file_scope
) {
1390 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1391 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1394 sym
->namespace = NS_UNDEF
;
1395 sym
->used_in
= NULL
;
1401 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1403 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1406 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1408 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1411 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1413 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1414 free_preprocessor_line(token
->next
);
1415 token
->next
= stream
->top_if
;
1416 stream
->top_if
= token
;
1417 if (false_nesting
|| true != 1)
1422 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1424 struct token
*next
= token
->next
;
1426 if (token_type(next
) == TOKEN_IDENT
) {
1427 arg
= token_defined(next
);
1429 dirty_stream(stream
);
1431 sparse_error(token
->pos
, "expected preprocessor identifier");
1434 return preprocessor_if(stream
, token
, arg
);
1437 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1439 struct token
*next
= token
->next
;
1441 if (token_type(next
) == TOKEN_IDENT
) {
1442 if (!stream
->dirty
&& !stream
->ifndef
) {
1443 if (!stream
->protect
) {
1444 stream
->ifndef
= token
;
1445 stream
->protect
= next
->ident
;
1446 } else if (stream
->protect
== next
->ident
) {
1447 stream
->ifndef
= token
;
1451 arg
= !token_defined(next
);
1453 dirty_stream(stream
);
1455 sparse_error(token
->pos
, "expected preprocessor identifier");
1459 return preprocessor_if(stream
, token
, arg
);
1462 static const char *show_token_sequence(struct token
*token
, int quote
);
1465 * Expression handling for #if and #elif; it differs from normal expansion
1466 * due to special treatment of "defined".
1468 static int expression_value(struct token
**where
)
1470 struct expression
*expr
;
1472 struct token
**list
= where
, **beginning
= NULL
;
1476 while (!eof_token(p
= scan_next(list
))) {
1479 if (token_type(p
) != TOKEN_IDENT
)
1481 if (p
->ident
== &defined_ident
) {
1486 if (!expand_one_symbol(list
))
1488 if (token_type(p
) != TOKEN_IDENT
)
1490 token_type(p
) = TOKEN_ZERO_IDENT
;
1493 if (match_op(p
, '(')) {
1497 replace_with_defined(p
);
1502 if (token_type(p
) == TOKEN_IDENT
)
1506 replace_with_defined(p
);
1511 if (!match_op(p
, ')'))
1512 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1519 p
= constant_expression(*where
, &expr
);
1521 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
, 0));
1522 value
= get_expression_value(expr
);
1526 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1530 value
= expression_value(&token
->next
);
1532 dirty_stream(stream
);
1533 return preprocessor_if(stream
, token
, value
);
1536 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1538 struct token
*top_if
= stream
->top_if
;
1542 nesting_error(stream
);
1543 sparse_error(token
->pos
, "unmatched #elif within stream");
1547 if (token_type(top_if
) == TOKEN_ELSE
) {
1548 nesting_error(stream
);
1549 sparse_error(token
->pos
, "#elif after #else");
1555 dirty_stream(stream
);
1556 if (token_type(top_if
) != TOKEN_IF
)
1558 if (false_nesting
) {
1560 if (!expression_value(&token
->next
))
1564 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1569 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1571 struct token
*top_if
= stream
->top_if
;
1575 nesting_error(stream
);
1576 sparse_error(token
->pos
, "unmatched #else within stream");
1580 if (token_type(top_if
) == TOKEN_ELSE
) {
1581 nesting_error(stream
);
1582 sparse_error(token
->pos
, "#else after #else");
1584 if (false_nesting
) {
1585 if (token_type(top_if
) == TOKEN_IF
)
1590 token_type(top_if
) = TOKEN_ELSE
;
1594 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1596 struct token
*top_if
= stream
->top_if
;
1599 nesting_error(stream
);
1600 sparse_error(token
->pos
, "unmatched #endif in stream");
1605 stream
->top_if
= top_if
->next
;
1606 __free_token(top_if
);
1610 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1612 warning(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1616 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1618 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1622 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1625 * Do we have any non-system includes?
1626 * Clear them out if so..
1628 *sys_includepath
= NULL
;
1632 static inline void update_inc_ptrs(const char ***where
)
1635 if (*where
<= dirafter_includepath
) {
1636 dirafter_includepath
++;
1637 /* If this was the entry that we prepend, don't
1638 * rise the lower entries, even if they are at
1639 * the same level. */
1640 if (where
== &dirafter_includepath
)
1643 if (*where
<= sys_includepath
) {
1645 if (where
== &sys_includepath
)
1648 if (*where
<= isys_includepath
) {
1650 if (where
== &isys_includepath
)
1654 /* angle_includepath is actually never updated, since we
1655 * don't suppport -iquote rught now. May change some day. */
1656 if (*where
<= angle_includepath
) {
1657 angle_includepath
++;
1658 if (where
== &angle_includepath
)
1663 /* Add a path before 'where' and update the pointers associated with the
1664 * includepath array */
1665 static void add_path_entry(struct token
*token
, const char *path
,
1666 const char ***where
)
1671 /* Need one free entry.. */
1672 if (includepath
[INCLUDEPATHS
-2])
1673 error_die(token
->pos
, "too many include path entries");
1675 /* check that this is not a duplicate */
1678 if (strcmp(*dst
, path
) == 0)
1685 update_inc_ptrs(where
);
1688 * Move them all up starting at dst,
1689 * insert the new entry..
1692 const char *tmp
= *dst
;
1699 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1702 token
= token
->next
;
1703 if (eof_token(token
))
1705 if (token_type(token
) != TOKEN_STRING
) {
1706 warning(token
->pos
, "expected path string");
1709 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1713 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1716 token
= token
->next
;
1717 if (eof_token(token
))
1719 if (token_type(token
) != TOKEN_STRING
) {
1720 sparse_error(token
->pos
, "expected path string");
1723 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1727 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
1730 token
= token
->next
;
1731 if (eof_token(token
))
1733 if (token_type(token
) != TOKEN_STRING
) {
1734 sparse_error(token
->pos
, "expected path string");
1737 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1741 /* Add to end on includepath list - no pointer updates */
1742 static void add_dirafter_entry(struct token
*token
, const char *path
)
1744 const char **dst
= includepath
;
1746 /* Need one free entry.. */
1747 if (includepath
[INCLUDEPATHS
-2])
1748 error_die(token
->pos
, "too many include path entries");
1750 /* Add to the end */
1758 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1761 token
= token
->next
;
1762 if (eof_token(token
))
1764 if (token_type(token
) != TOKEN_STRING
) {
1765 sparse_error(token
->pos
, "expected path string");
1768 add_dirafter_entry(token
, token
->string
->data
);
1772 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1777 * Split the include path. Any directories specified with `-I'
1778 * options before `-I-' are searched only for headers requested with
1779 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1780 * If additional directories are specified with `-I' options after
1781 * the `-I-', those directories are searched for all `#include'
1783 * In addition, `-I-' inhibits the use of the directory of the current
1784 * file directory as the first search directory for `#include "FILE"'.
1786 quote_includepath
= includepath
+1;
1787 angle_includepath
= sys_includepath
;
1792 * We replace "#pragma xxx" with "__pragma__" in the token
1793 * stream. Just as an example.
1795 * We'll just #define that away for now, but the theory here
1796 * is that we can use this to insert arbitrary token sequences
1797 * to turn the pragmas into internal front-end sequences for
1798 * when we actually start caring about them.
1800 * So eventually this will turn into some kind of extended
1801 * __attribute__() like thing, except called __pragma__(xxx).
1803 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1805 struct token
*next
= *line
;
1807 if (match_ident(token
->next
, &once_ident
) && eof_token(token
->next
->next
)) {
1811 token
->ident
= &pragma_ident
;
1812 token
->pos
.newline
= 1;
1813 token
->pos
.whitespace
= 1;
1821 * We ignore #line for now.
1823 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1828 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1830 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
, 0));
1835 static void init_preprocessor(void)
1838 int stream
= init_stream("preprocessor", -1, includepath
);
1841 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1843 { "define", handle_define
},
1844 { "weak_define", handle_weak_define
},
1845 { "strong_define", handle_strong_define
},
1846 { "undef", handle_undef
},
1847 { "strong_undef", handle_strong_undef
},
1848 { "warning", handle_warning
},
1849 { "error", handle_error
},
1850 { "include", handle_include
},
1851 { "include_next", handle_include_next
},
1852 { "pragma", handle_pragma
},
1853 { "line", handle_line
},
1855 // our internal preprocessor tokens
1856 { "nostdinc", handle_nostdinc
},
1857 { "add_include", handle_add_include
},
1858 { "add_isystem", handle_add_isystem
},
1859 { "add_system", handle_add_system
},
1860 { "add_dirafter", handle_add_dirafter
},
1861 { "split_include", handle_split_include
},
1862 { "argv_include", handle_argv_include
},
1864 { "ifdef", handle_ifdef
},
1865 { "ifndef", handle_ifndef
},
1866 { "else", handle_else
},
1867 { "endif", handle_endif
},
1868 { "if", handle_if
},
1869 { "elif", handle_elif
},
1872 for (i
= 0; i
< ARRAY_SIZE(normal
); i
++) {
1874 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1875 sym
->handler
= normal
[i
].handler
;
1878 for (i
= 0; i
< ARRAY_SIZE(special
); i
++) {
1880 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1881 sym
->handler
= special
[i
].handler
;
1887 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1889 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1890 struct token
*token
= start
->next
;
1893 if (eof_token(token
))
1896 if (token_type(token
) == TOKEN_IDENT
) {
1897 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1899 handler
= sym
->handler
;
1900 is_normal
= sym
->normal
;
1902 handler
= handle_nondirective
;
1904 } else if (token_type(token
) == TOKEN_NUMBER
) {
1905 handler
= handle_line
;
1907 handler
= handle_nondirective
;
1911 dirty_stream(stream
);
1915 if (!handler(stream
, line
, token
)) /* all set */
1919 free_preprocessor_line(token
);
1922 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1924 struct token
*start
= *line
, *next
;
1925 struct token
**tp
= &start
->next
;
1929 if (next
->pos
.newline
)
1934 *tp
= &eof_token_entry
;
1935 handle_preprocessor_line(stream
, line
, start
);
1938 static void do_preprocess(struct token
**list
)
1942 while (!eof_token(next
= scan_next(list
))) {
1943 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1945 if (next
->pos
.newline
&& match_op(next
, '#')) {
1946 if (!next
->pos
.noexpand
) {
1947 preprocessor_line(stream
, list
);
1948 __free_token(next
); /* Free the '#' token */
1953 switch (token_type(next
)) {
1954 case TOKEN_STREAMEND
:
1955 if (stream
->top_if
) {
1956 nesting_error(stream
);
1957 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1958 stream
->top_if
= NULL
;
1962 stream
->constant
= CONSTANT_FILE_YES
;
1965 case TOKEN_STREAMBEGIN
:
1970 dirty_stream(stream
);
1971 if (false_nesting
) {
1977 if (token_type(next
) != TOKEN_IDENT
||
1978 expand_one_symbol(list
))
1984 struct token
* preprocess(struct token
*token
)
1987 init_preprocessor();
1988 do_preprocess(&token
);
1990 // Drop all expressions from preprocessing, they're not used any more.
1991 // This is not true when we have multiple files, though ;/
1992 // clear_expression_alloc();