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 struct ident_list
*macros
; // only needed for -dD
48 static int false_nesting
= 0;
49 static int counter_macro
= 0; // __COUNTER__ expansion
50 static int include_level
= 0;
51 static int expanding
= 0;
53 #define INCLUDEPATHS 300
54 const char *includepath
[INCLUDEPATHS
+1] = {
61 static const char **quote_includepath
= includepath
;
62 static const char **angle_includepath
= includepath
+ 1;
63 static const char **isys_includepath
= includepath
+ 1;
64 static const char **sys_includepath
= includepath
+ 1;
65 static const char **dirafter_includepath
= includepath
+ 3;
67 #define dirty_stream(stream) \
69 if (!stream->dirty) { \
71 if (!stream->ifndef) \
72 stream->protect = NULL; \
76 #define end_group(stream) \
78 if (stream->ifndef == stream->top_if) { \
79 stream->ifndef = NULL; \
81 stream->protect = NULL; \
82 else if (stream->protect) \
87 #define nesting_error(stream) \
90 stream->ifndef = NULL; \
91 stream->protect = NULL; \
94 static struct token
*alloc_token(struct position
*pos
)
96 struct token
*token
= __alloc_token(0);
98 token
->pos
.stream
= pos
->stream
;
99 token
->pos
.line
= pos
->line
;
100 token
->pos
.pos
= pos
->pos
;
101 token
->pos
.whitespace
= 1;
105 /* Expand symbol 'sym' at '*list' */
106 static int expand(struct token
**, struct symbol
*);
108 static void replace_with_string(struct token
*token
, const char *str
)
110 int size
= strlen(str
) + 1;
111 struct string
*s
= __alloc_string(size
);
114 memcpy(s
->data
, str
, size
);
115 token_type(token
) = TOKEN_STRING
;
119 static void replace_with_integer(struct token
*token
, unsigned int val
)
121 char *buf
= __alloc_bytes(11);
122 sprintf(buf
, "%u", val
);
123 token_type(token
) = TOKEN_NUMBER
;
127 static struct symbol
*lookup_macro(struct ident
*ident
)
129 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_UNDEF
);
130 if (sym
&& sym
->namespace != NS_MACRO
)
135 static int token_defined(struct token
*token
)
137 if (token_type(token
) == TOKEN_IDENT
) {
138 struct symbol
*sym
= lookup_macro(token
->ident
);
140 sym
->used_in
= file_scope
;
146 sparse_error(token
->pos
, "expected preprocessor identifier");
150 static void replace_with_bool(struct token
*token
, bool val
)
152 static const char *string
[] = { "0", "1" };
154 token_type(token
) = TOKEN_NUMBER
;
155 token
->number
= string
[val
];
158 static void replace_with_defined(struct token
*token
)
160 replace_with_bool(token
, token_defined(token
));
163 static void expand_line(struct token
*token
)
165 replace_with_integer(token
, token
->pos
.line
);
168 static void expand_file(struct token
*token
)
170 replace_with_string(token
, stream_name(token
->pos
.stream
));
173 static void expand_basefile(struct token
*token
)
175 replace_with_string(token
, base_filename
);
179 static void expand_date(struct token
*token
)
181 static char buffer
[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
185 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
186 replace_with_string(token
, buffer
);
189 static void expand_time(struct token
*token
)
191 static char buffer
[9]; /* __TIME__: 2 + ':' + 2 + ':' + 2 + '\0' */
195 strftime(buffer
, 9, "%T", localtime(&t
));
196 replace_with_string(token
, buffer
);
199 static void expand_counter(struct token
*token
)
201 replace_with_integer(token
, counter_macro
++);
204 static void expand_include_level(struct token
*token
)
206 replace_with_integer(token
, include_level
- 1);
209 static int expand_one_symbol(struct token
**list
)
211 struct token
*token
= *list
;
214 if (token
->pos
.noexpand
)
217 sym
= lookup_macro(token
->ident
);
220 if (sym
->expand_simple
) {
221 sym
->expand_simple(token
);
226 sym
->used_in
= file_scope
;
228 rc
= expand(list
, sym
);
234 static inline struct token
*scan_next(struct token
**where
)
236 struct token
*token
= *where
;
237 if (token_type(token
) != TOKEN_UNTAINT
)
240 token
->ident
->tainted
= 0;
242 } while (token_type(token
) == TOKEN_UNTAINT
);
247 static void expand_list(struct token
**list
)
250 while (!eof_token(next
= scan_next(list
))) {
251 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
256 static void preprocessor_line(struct stream
*stream
, struct token
**line
);
258 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
, int count
)
260 struct stream
*stream
= input_streams
+ prev
->pos
.stream
;
261 struct token
**p
= &prev
->next
;
265 while (!eof_token(next
= scan_next(p
))) {
266 if (next
->pos
.newline
&& match_op(next
, '#')) {
267 if (!next
->pos
.noexpand
) {
268 preprocessor_line(stream
, p
);
269 __free_token(next
); /* Free the '#' token */
273 switch (token_type(next
)) {
274 case TOKEN_STREAMEND
:
275 case TOKEN_STREAMBEGIN
:
276 *p
= &eof_token_entry
;
279 case TOKEN_WIDE_STRING
:
281 next
->string
->immutable
= 1;
289 if (match_op(next
, '(')) {
291 } else if (match_op(next
, ')')) {
294 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
297 next
->pos
.stream
= pos
->stream
;
298 next
->pos
.line
= pos
->line
;
299 next
->pos
.pos
= pos
->pos
;
300 next
->pos
.newline
= 0;
303 *p
= &eof_token_entry
;
308 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
313 struct token
*expanded
;
320 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
322 int wanted
= arglist
->count
.normal
;
323 struct token
*next
= NULL
;
326 arglist
= arglist
->next
; /* skip counter */
329 next
= collect_arg(start
, 0, &what
->pos
, 0);
332 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
337 for (count
= 0; count
< wanted
; count
++) {
338 struct argcount
*p
= &arglist
->next
->count
;
339 next
= collect_arg(start
, p
->vararg
, &what
->pos
, p
->normal
);
342 if (p
->vararg
&& wanted
== 1 && eof_token(start
->next
))
344 arglist
= arglist
->next
->next
;
345 args
[count
].arg
= start
->next
;
346 args
[count
].n_normal
= p
->normal
;
347 args
[count
].n_quoted
= p
->quoted
;
348 args
[count
].n_str
= p
->str
;
349 if (match_op(next
, ')')) {
355 if (count
== wanted
&& !match_op(next
, ')'))
357 if (count
== wanted
- 1) {
358 struct argcount
*p
= &arglist
->next
->count
;
361 args
[count
].arg
= NULL
;
362 args
[count
].n_normal
= p
->normal
;
363 args
[count
].n_quoted
= p
->quoted
;
364 args
[count
].n_str
= p
->str
;
366 if (count
< wanted
- 1)
369 what
->next
= next
->next
;
373 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
374 show_token(what
), wanted
, count
);
377 while (match_op(next
, ',')) {
378 next
= collect_arg(next
, 0, &what
->pos
, 0);
383 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
384 show_token(what
), count
, wanted
);
387 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
390 what
->next
= next
->next
;
394 static struct token
*dup_list(struct token
*list
)
396 struct token
*res
= NULL
;
397 struct token
**p
= &res
;
399 while (!eof_token(list
)) {
400 struct token
*newtok
= __alloc_token(0);
409 static const char *show_token_sequence(struct token
*token
, int quote
)
411 static char buffer
[MAX_STRING
];
415 if (!token
&& !quote
)
417 while (!eof_token(token
)) {
418 const char *val
= quote
? quote_token(token
) : show_token(token
);
419 int len
= strlen(val
);
421 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
422 sparse_error(token
->pos
, "too long token expansion");
428 memcpy(ptr
, val
, len
);
431 whitespace
= token
->pos
.whitespace
;
437 static struct token
*stringify(struct token
*arg
)
439 const char *s
= show_token_sequence(arg
, 1);
440 int size
= strlen(s
)+1;
441 struct token
*token
= __alloc_token(0);
442 struct string
*string
= __alloc_string(size
);
444 memcpy(string
->data
, s
, size
);
445 string
->length
= size
;
446 token
->pos
= arg
->pos
;
447 token_type(token
) = TOKEN_STRING
;
448 token
->string
= string
;
449 token
->next
= &eof_token_entry
;
453 static void expand_arguments(int count
, struct arg
*args
)
456 for (i
= 0; i
< count
; i
++) {
457 struct token
*arg
= args
[i
].arg
;
459 arg
= &eof_token_entry
;
461 args
[i
].str
= stringify(arg
);
462 if (args
[i
].n_normal
) {
463 if (!args
[i
].n_quoted
) {
464 args
[i
].expanded
= arg
;
466 } else if (eof_token(arg
)) {
467 args
[i
].expanded
= arg
;
469 args
[i
].expanded
= dup_list(arg
);
471 expand_list(&args
[i
].expanded
);
477 * Possibly valid combinations:
478 * - ident + ident -> ident
479 * - ident + number -> ident unless number contains '.', '+' or '-'.
480 * - 'L' + char constant -> wide char constant
481 * - 'L' + string literal -> wide string literal
482 * - number + number -> number
483 * - number + ident -> number
484 * - number + '.' -> number
485 * - number + '+' or '-' -> number, if number used to end on [eEpP].
486 * - '.' + number -> number, if number used to start with a digit.
487 * - special + special -> either special or an error.
489 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
492 enum token_type t1
= token_type(left
), t2
= token_type(right
);
494 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
497 if (t1
== TOKEN_IDENT
&& left
->ident
== &L_ident
) {
498 if (t2
>= TOKEN_CHAR
&& t2
< TOKEN_WIDE_CHAR
)
499 return t2
+ TOKEN_WIDE_CHAR
- TOKEN_CHAR
;
500 if (t2
== TOKEN_STRING
)
501 return TOKEN_WIDE_STRING
;
504 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
507 strcpy(p
, show_token(left
));
508 strcat(p
, show_token(right
));
514 if (t1
== TOKEN_IDENT
) {
515 if (t2
== TOKEN_SPECIAL
)
517 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
522 if (t1
== TOKEN_NUMBER
) {
523 if (t2
== TOKEN_SPECIAL
) {
524 switch (right
->special
) {
528 if (strchr("eEpP", p
[len
- 2]))
537 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
540 return TOKEN_SPECIAL
;
543 static int merge(struct token
*left
, struct token
*right
)
545 static char buffer
[512];
546 enum token_type res
= combine(left
, right
, buffer
);
551 left
->ident
= built_in_ident(buffer
);
552 left
->pos
.noexpand
= 0;
556 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
557 left
->number
= xstrdup(buffer
);
561 if (buffer
[2] && buffer
[3])
563 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
564 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
571 case TOKEN_WIDE_CHAR
:
572 case TOKEN_WIDE_STRING
:
573 token_type(left
) = res
;
574 left
->pos
.noexpand
= 0;
575 left
->string
= right
->string
;
578 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
579 token_type(left
) = res
;
580 left
->pos
.noexpand
= 0;
581 memcpy(left
->embedded
, right
->embedded
, 4);
587 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
591 static struct token
*dup_token(struct token
*token
, struct position
*streampos
)
593 struct token
*alloc
= alloc_token(streampos
);
594 token_type(alloc
) = token_type(token
);
595 alloc
->pos
.newline
= token
->pos
.newline
;
596 alloc
->pos
.whitespace
= token
->pos
.whitespace
;
597 alloc
->number
= token
->number
;
598 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
602 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
604 int need_copy
= --*count
;
605 while (!eof_token(list
)) {
608 token
= dup_token(list
, &list
->pos
);
611 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
612 token
->pos
.noexpand
= 1;
614 where
= &token
->next
;
617 *where
= &eof_token_entry
;
621 static int handle_kludge(struct token
**p
, struct arg
*args
)
623 struct token
*t
= (*p
)->next
->next
;
625 struct arg
*v
= &args
[t
->argnum
];
626 if (token_type(t
->next
) != TOKEN_CONCAT
) {
628 /* ignore the first ## */
632 /* skip the entire thing */
636 if (v
->arg
&& !eof_token(v
->arg
))
637 return 0; /* no magic */
642 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
644 struct position
*base_pos
= &(*list
)->pos
;
646 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
648 for (; !eof_token(body
); body
= body
->next
) {
649 struct token
*added
, *arg
;
653 switch (token_type(body
)) {
654 case TOKEN_GNU_KLUDGE
:
656 * GNU kludge: if we had <comma>##<vararg>, behaviour
657 * depends on whether we had enough arguments to have
658 * a vararg. If we did, ## is just ignored. Otherwise
659 * both , and ## are ignored. Worse, there can be
660 * an arbitrary number of ##<arg> in between; if all of
661 * those are empty, we act as if they hadn't been there,
662 * otherwise we act as if the kludge didn't exist.
665 if (handle_kludge(&body
, args
)) {
672 added
= dup_token(t
, base_pos
);
673 token_type(added
) = TOKEN_SPECIAL
;
677 case TOKEN_STR_ARGUMENT
:
678 arg
= args
[body
->argnum
].str
;
679 count
= &args
[body
->argnum
].n_str
;
682 case TOKEN_QUOTED_ARGUMENT
:
683 arg
= args
[body
->argnum
].arg
;
684 count
= &args
[body
->argnum
].n_quoted
;
685 if (!arg
|| eof_token(arg
)) {
694 case TOKEN_MACRO_ARGUMENT
:
695 arg
= args
[body
->argnum
].expanded
;
696 count
= &args
[body
->argnum
].n_normal
;
697 if (eof_token(arg
)) {
702 tail
= copy(&added
, arg
, count
);
703 added
->pos
.newline
= body
->pos
.newline
;
704 added
->pos
.whitespace
= body
->pos
.whitespace
;
708 if (state
== Placeholder
)
715 added
= dup_token(body
, base_pos
);
716 if (added
->ident
->tainted
)
717 added
->pos
.noexpand
= 1;
722 added
= dup_token(body
, base_pos
);
728 * if we got to doing real concatenation, we already have
729 * added something into the list, so containing_token() is OK.
731 if (state
== Concat
&& merge(containing_token(list
), added
)) {
733 if (tail
!= &added
->next
)
741 *list
= &eof_token_entry
;
745 static int expand(struct token
**list
, struct symbol
*sym
)
748 struct token
*token
= *list
;
749 struct ident
*expanding
= token
->ident
;
751 struct token
*expansion
= sym
->expansion
;
752 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
753 struct arg args
[nargs
];
755 if (expanding
->tainted
) {
756 token
->pos
.noexpand
= 1;
761 if (!match_op(scan_next(&token
->next
), '('))
763 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
765 expand_arguments(nargs
, args
);
769 return sym
->expand(token
, args
) ? 0 : 1;
771 expanding
->tainted
= 1;
774 tail
= substitute(list
, expansion
, args
);
776 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
777 * We still can lose the newline flag if the sucker expands to nothing,
778 * but the price of dealing with that is probably too high (we'd need
779 * to collect the flags during scan_next())
781 (*list
)->pos
.newline
= token
->pos
.newline
;
782 (*list
)->pos
.whitespace
= token
->pos
.whitespace
;
788 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
790 static char buffer
[256];
793 while (!eof_token(token
) && !match_op(token
, endop
)) {
795 const char *val
= token
->string
->data
;
796 if (token_type(token
) != TOKEN_STRING
)
797 val
= show_token(token
);
799 memcpy(ptr
, val
, len
);
804 if (endop
&& !match_op(token
, endop
))
805 sparse_error(start
->pos
, "expected '>' at end of filename");
809 static int already_tokenized(const char *path
)
813 for (stream
= *hash_stream(path
); stream
>= 0 ; stream
= next
) {
814 struct stream
*s
= input_streams
+ stream
;
816 next
= s
->next_stream
;
818 if (strcmp(path
, s
->name
))
822 if (s
->constant
!= CONSTANT_FILE_YES
)
824 if (strcmp(path
, s
->name
))
826 if (s
->protect
&& !lookup_macro(s
->protect
))
833 /* Handle include of header files.
834 * The relevant options are made compatible with gcc. The only options that
835 * are not supported is -withprefix and friends.
837 * Three set of include paths are known:
838 * quote_includepath: Path to search when using #include "file.h"
839 * angle_includepath: Paths to search when using #include <file.h>
840 * isys_includepath: Paths specified with -isystem, come before the
841 * built-in system include paths. Gcc would suppress
842 * warnings from system headers. Here we separate
843 * them from the angle_ ones to keep search ordering.
845 * sys_includepath: Built-in include paths.
846 * dirafter_includepath Paths added with -dirafter.
848 * The above is implemented as one array with pointers
850 * quote_includepath ---> | |
854 * angle_includepath ---> | |
856 * isys_includepath ---> | |
858 * sys_includepath ---> | |
860 * dirafter_includepath -> | |
863 * -I dir insert dir just before isys_includepath and move the rest
864 * -I- makes all dirs specified with -I before to quote dirs only and
865 * angle_includepath is set equal to isys_includepath.
866 * -nostdinc removes all sys dirs by storing NULL in entry pointed
867 * to by * sys_includepath. Note that this will reset all dirs built-in
868 * and added before -nostdinc by -isystem and -idirafter.
869 * -isystem dir adds dir where isys_includepath points adding this dir as
871 * -idirafter dir adds dir to the end of the list
874 static void set_stream_include_path(struct stream
*stream
)
876 const char *path
= stream
->path
;
878 const char *p
= strrchr(stream
->name
, '/');
881 int len
= p
- stream
->name
+ 1;
882 char *m
= malloc(len
+1);
883 /* This includes the final "/" */
884 memcpy(m
, stream
->name
, len
);
887 /* normalize this path */
888 while (path
[0] == '.' && path
[1] == '/') {
890 while (path
[0] == '/')
896 includepath
[0] = path
;
899 static int try_include(struct position pos
, const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
902 int plen
= strlen(path
);
903 static char fullname
[PATH_MAX
];
905 memcpy(fullname
, path
, plen
);
906 if (plen
&& path
[plen
-1] != '/') {
907 fullname
[plen
] = '/';
910 memcpy(fullname
+plen
, filename
, flen
);
911 if (already_tokenized(fullname
))
913 fd
= open(fullname
, O_RDONLY
);
915 char *streamname
= xmemdup(fullname
, plen
+ flen
);
916 *where
= tokenize(&pos
, streamname
, fd
, *where
, next_path
);
923 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
927 while ((path
= *pptr
++) != NULL
) {
928 if (!try_include(token
->pos
, path
, filename
, flen
, list
, pptr
))
935 static int free_preprocessor_line(struct token
*token
)
937 while (token_type(token
) != TOKEN_EOF
) {
938 struct token
*free
= token
;
945 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, int how
)
947 const char *filename
;
955 if (!match_op(next
, '<')) {
956 expand_list(&token
->next
);
959 if (match_op(token
->next
, '<')) {
966 filename
= token_name_sequence(token
, expect
, token
);
967 flen
= strlen(filename
) + 1;
970 if (filename
[0] == '/') {
971 if (try_include(token
->pos
, "", filename
, flen
, list
, includepath
))
978 path
= stream
->next_path
;
985 /* Dir of input file is first dir to search for quoted includes */
986 set_stream_include_path(stream
);
987 path
= expect
? angle_includepath
: quote_includepath
;
990 /* Check the standard include paths.. */
991 if (do_include_path(path
, list
, token
, filename
, flen
))
994 error_die(token
->pos
, "unable to open '%s'", filename
);
997 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
999 return handle_include_path(stream
, list
, token
, 0);
1002 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
1004 return handle_include_path(stream
, list
, token
, 1);
1007 static int handle_argv_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
1009 return handle_include_path(stream
, list
, token
, 2);
1012 static int token_different(struct token
*t1
, struct token
*t2
)
1016 if (token_type(t1
) != token_type(t2
))
1019 switch (token_type(t1
)) {
1021 different
= t1
->ident
!= t2
->ident
;
1023 case TOKEN_ARG_COUNT
:
1026 case TOKEN_GNU_KLUDGE
:
1030 different
= strcmp(t1
->number
, t2
->number
);
1033 different
= t1
->special
!= t2
->special
;
1035 case TOKEN_MACRO_ARGUMENT
:
1036 case TOKEN_QUOTED_ARGUMENT
:
1037 case TOKEN_STR_ARGUMENT
:
1038 different
= t1
->argnum
!= t2
->argnum
;
1040 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
1041 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
1042 different
= memcmp(t1
->embedded
, t2
->embedded
, 4);
1045 case TOKEN_WIDE_CHAR
:
1047 case TOKEN_WIDE_STRING
: {
1048 struct string
*s1
, *s2
;
1053 if (s1
->length
!= s2
->length
)
1055 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
1065 static int token_list_different(struct token
*list1
, struct token
*list2
)
1070 if (!list1
|| !list2
)
1072 if (token_different(list1
, list2
))
1074 list1
= list1
->next
;
1075 list2
= list2
->next
;
1079 static inline void set_arg_count(struct token
*token
)
1081 token_type(token
) = TOKEN_ARG_COUNT
;
1082 token
->count
.normal
= token
->count
.quoted
=
1083 token
->count
.str
= token
->count
.vararg
= 0;
1086 static struct token
*parse_arguments(struct token
*list
)
1088 struct token
*arg
= list
->next
, *next
= list
;
1089 struct argcount
*count
= &list
->count
;
1091 set_arg_count(list
);
1093 if (match_op(arg
, ')')) {
1095 list
->next
= &eof_token_entry
;
1099 while (token_type(arg
) == TOKEN_IDENT
) {
1100 if (arg
->ident
== &__VA_ARGS___ident
)
1102 if (!++count
->normal
)
1106 if (match_op(next
, ',')) {
1107 set_arg_count(next
);
1112 if (match_op(next
, ')')) {
1113 set_arg_count(next
);
1115 arg
->next
->next
= &eof_token_entry
;
1119 /* normal cases are finished here */
1121 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
1122 if (match_op(next
->next
, ')')) {
1123 set_arg_count(next
);
1124 next
->count
.vararg
= 1;
1126 arg
->next
->next
= &eof_token_entry
;
1134 if (eof_token(next
)) {
1142 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
1144 token_type(arg
) = TOKEN_IDENT
;
1145 arg
->ident
= &__VA_ARGS___ident
;
1146 if (!match_op(next
, ')'))
1148 if (!++count
->normal
)
1150 set_arg_count(next
);
1151 next
->count
.vararg
= 1;
1153 arg
->next
->next
= &eof_token_entry
;
1157 if (eof_token(arg
)) {
1161 if (match_op(arg
, ','))
1168 sparse_error(arg
->pos
, "parameter name missing");
1171 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
1175 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1178 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1181 sparse_error(arg
->pos
, "too many arguments in macro definition");
1185 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1187 struct ident
*ident
= token
->ident
;
1190 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1193 arglist
= arglist
->next
;
1195 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1196 if (arglist
->ident
== ident
) {
1197 struct argcount
*count
= &arglist
->next
->count
;
1201 token_type(token
) = type
;
1203 case TOKEN_MACRO_ARGUMENT
:
1204 n
= ++count
->normal
;
1206 case TOKEN_QUOTED_ARGUMENT
:
1207 n
= ++count
->quoted
;
1213 return count
->vararg
? 2 : 1;
1215 * XXX - need saner handling of that
1216 * (>= 1024 instances of argument)
1218 token_type(token
) = TOKEN_ERROR
;
1225 static struct token
*handle_hash(struct token
**p
, struct token
*arglist
)
1227 struct token
*token
= *p
;
1229 struct token
*next
= token
->next
;
1230 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1232 next
->pos
.whitespace
= token
->pos
.whitespace
;
1233 __free_token(token
);
1236 token
->pos
.noexpand
= 1;
1241 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1245 /* token->next is ## */
1246 static struct token
*handle_hashhash(struct token
*token
, struct token
*arglist
)
1248 struct token
*last
= token
;
1249 struct token
*concat
;
1250 int state
= match_op(token
, ',');
1252 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1258 /* eat duplicate ## */
1259 concat
= token
->next
;
1260 while (match_op(t
= concat
->next
, SPECIAL_HASHHASH
)) {
1262 __free_token(concat
);
1265 token_type(concat
) = TOKEN_CONCAT
;
1270 if (match_op(t
, '#')) {
1271 t
= handle_hash(&concat
->next
, arglist
);
1276 is_arg
= try_arg(t
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1278 if (state
== 1 && is_arg
) {
1282 state
= match_op(t
, ',');
1286 if (!match_op(token
->next
, SPECIAL_HASHHASH
))
1289 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1291 token_type(last
) = TOKEN_GNU_KLUDGE
;
1295 sparse_error(concat
->pos
, "'##' cannot appear at the ends of macro expansion");
1299 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1301 struct token
*token
= expansion
;
1304 if (match_op(token
, SPECIAL_HASHHASH
))
1307 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1308 if (match_op(token
, '#')) {
1309 token
= handle_hash(p
, arglist
);
1313 if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1314 token
= handle_hashhash(token
, arglist
);
1318 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1320 switch (token_type(token
)) {
1325 case TOKEN_WIDE_STRING
:
1326 token
->string
->immutable
= 1;
1330 token
= alloc_token(&expansion
->pos
);
1331 token_type(token
) = TOKEN_UNTAINT
;
1332 token
->ident
= name
;
1338 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1341 sparse_error(token
->pos
, "too many instances of argument in body");
1345 static int do_define(struct position pos
, struct token
*token
, struct ident
*name
,
1346 struct token
*arglist
, struct token
*expansion
, int attr
)
1351 expansion
= parse_expansion(expansion
, arglist
, name
);
1355 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1359 if (attr
< sym
->attr
)
1362 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1364 if (token_list_different(sym
->expansion
, expansion
) ||
1365 token_list_different(sym
->arglist
, arglist
)) {
1367 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1368 || sym
->used_in
== file_scope
) {
1369 warning(pos
, "preprocessor token %.*s redefined",
1370 name
->len
, name
->name
);
1371 info(sym
->pos
, "this was the original definition");
1377 if (!sym
|| sym
->scope
!= file_scope
) {
1378 sym
= alloc_symbol(pos
, SYM_NODE
);
1379 bind_symbol(sym
, name
, NS_MACRO
);
1380 add_ident(¯os
, name
);
1385 sym
->expansion
= expansion
;
1386 sym
->arglist
= arglist
;
1387 if (token
) /* Free the "define" token, but not the rest of the line */
1388 __free_token(token
);
1391 sym
->namespace = NS_MACRO
;
1392 sym
->used_in
= NULL
;
1399 // predefine a macro with a printf-formatted value
1400 // @name: the name of the macro
1401 // @weak: 0/1 for a normal or a weak define
1402 // @fmt: the printf format followed by it's arguments.
1404 // The type of the value is automatically infered:
1405 // TOKEN_NUMBER if it starts by a digit, TOKEN_IDENT otherwise.
1406 // If @fmt is null or empty, the macro is defined with an empty definition.
1407 void predefine(const char *name
, int weak
, const char *fmt
, ...)
1409 struct ident
*ident
= built_in_ident(name
);
1410 struct token
*value
= &eof_token_entry
;
1411 int attr
= weak
? SYM_ATTR_WEAK
: SYM_ATTR_NORMAL
;
1413 if (fmt
&& fmt
[0]) {
1414 static char buf
[256];
1418 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1421 value
= __alloc_token(0);
1422 if (isdigit((unsigned char)buf
[0])) {
1423 token_type(value
) = TOKEN_NUMBER
;
1424 value
->number
= xstrdup(buf
);
1426 token_type(value
) = TOKEN_IDENT
;
1427 value
->ident
= built_in_ident(buf
);
1429 value
->pos
.whitespace
= 1;
1430 value
->next
= &eof_token_entry
;
1433 do_define(value
->pos
, NULL
, ident
, NULL
, value
, attr
);
1437 // like predefine() but only if one of the non-standard dialect is chosen
1438 void predefine_nostd(const char *name
)
1440 if ((standard
& STANDARD_GNU
) || (standard
== STANDARD_NONE
))
1441 predefine(name
, 1, "1");
1444 static void predefine_fmt(const char *fmt
, int weak
, va_list ap
)
1446 static char buf
[256];
1448 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1449 predefine(buf
, weak
, "1");
1452 void predefine_strong(const char *fmt
, ...)
1457 predefine_fmt(fmt
, 0, ap
);
1461 void predefine_weak(const char *fmt
, ...)
1466 predefine_fmt(fmt
, 1, ap
);
1470 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1472 struct token
*arglist
, *expansion
;
1473 struct token
*left
= token
->next
;
1476 if (token_type(left
) != TOKEN_IDENT
) {
1477 sparse_error(token
->pos
, "expected identifier to 'define'");
1484 expansion
= left
->next
;
1485 if (!expansion
->pos
.whitespace
) {
1486 if (match_op(expansion
, '(')) {
1487 arglist
= expansion
;
1488 expansion
= parse_arguments(expansion
);
1491 } else if (!eof_token(expansion
)) {
1492 warning(expansion
->pos
,
1493 "no whitespace before object-like macro body");
1497 return do_define(left
->pos
, token
, name
, arglist
, expansion
, attr
);
1500 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1502 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1505 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1507 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1510 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1512 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1515 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1517 struct token
*left
= token
->next
;
1520 if (token_type(left
) != TOKEN_IDENT
) {
1521 sparse_error(token
->pos
, "expected identifier to 'undef'");
1525 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1527 if (attr
< sym
->attr
)
1529 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1531 } else if (attr
<= SYM_ATTR_NORMAL
)
1534 if (!sym
|| sym
->scope
!= file_scope
) {
1535 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1536 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1539 sym
->namespace = NS_UNDEF
;
1540 sym
->used_in
= NULL
;
1546 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1548 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1551 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1553 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1556 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int cond
)
1558 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1559 free_preprocessor_line(token
->next
);
1560 token
->next
= stream
->top_if
;
1561 stream
->top_if
= token
;
1562 if (false_nesting
|| cond
!= 1)
1567 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1569 struct token
*next
= token
->next
;
1571 if (token_type(next
) == TOKEN_IDENT
) {
1572 arg
= token_defined(next
);
1574 dirty_stream(stream
);
1576 sparse_error(token
->pos
, "expected preprocessor identifier");
1579 return preprocessor_if(stream
, token
, arg
);
1582 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1584 struct token
*next
= token
->next
;
1586 if (token_type(next
) == TOKEN_IDENT
) {
1587 if (!stream
->dirty
&& !stream
->ifndef
) {
1588 if (!stream
->protect
) {
1589 stream
->ifndef
= token
;
1590 stream
->protect
= next
->ident
;
1591 } else if (stream
->protect
== next
->ident
) {
1592 stream
->ifndef
= token
;
1596 arg
= !token_defined(next
);
1598 dirty_stream(stream
);
1600 sparse_error(token
->pos
, "expected preprocessor identifier");
1604 return preprocessor_if(stream
, token
, arg
);
1608 * Expression handling for #if and #elif; it differs from normal expansion
1609 * due to special treatment of "defined".
1611 static int expression_value(struct token
**where
)
1613 struct expression
*expr
;
1615 struct token
**list
= where
, **beginning
= NULL
;
1619 while (!eof_token(p
= scan_next(list
))) {
1622 if (token_type(p
) != TOKEN_IDENT
)
1624 if (p
->ident
== &defined_ident
) {
1629 if (!expand_one_symbol(list
))
1631 if (token_type(p
) != TOKEN_IDENT
)
1633 token_type(p
) = TOKEN_ZERO_IDENT
;
1636 if (match_op(p
, '(')) {
1640 replace_with_defined(p
);
1645 if (token_type(p
) == TOKEN_IDENT
)
1649 replace_with_defined(p
);
1654 if (!match_op(p
, ')'))
1655 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1662 p
= constant_expression(*where
, &expr
);
1664 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
, 0));
1665 value
= get_expression_value(expr
);
1669 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1673 value
= expression_value(&token
->next
);
1675 dirty_stream(stream
);
1676 return preprocessor_if(stream
, token
, value
);
1679 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1681 struct token
*top_if
= stream
->top_if
;
1685 nesting_error(stream
);
1686 sparse_error(token
->pos
, "unmatched #elif within stream");
1690 if (token_type(top_if
) == TOKEN_ELSE
) {
1691 nesting_error(stream
);
1692 sparse_error(token
->pos
, "#elif after #else");
1698 dirty_stream(stream
);
1699 if (token_type(top_if
) != TOKEN_IF
)
1701 if (false_nesting
) {
1703 if (!expression_value(&token
->next
))
1707 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1712 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1714 struct token
*top_if
= stream
->top_if
;
1718 nesting_error(stream
);
1719 sparse_error(token
->pos
, "unmatched #else within stream");
1723 if (token_type(top_if
) == TOKEN_ELSE
) {
1724 nesting_error(stream
);
1725 sparse_error(token
->pos
, "#else after #else");
1727 if (false_nesting
) {
1728 if (token_type(top_if
) == TOKEN_IF
)
1733 token_type(top_if
) = TOKEN_ELSE
;
1737 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1739 struct token
*top_if
= stream
->top_if
;
1742 nesting_error(stream
);
1743 sparse_error(token
->pos
, "unmatched #endif in stream");
1748 stream
->top_if
= top_if
->next
;
1749 __free_token(top_if
);
1753 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1755 warning(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1759 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1761 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1765 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1768 * Do we have any non-system includes?
1769 * Clear them out if so..
1771 *sys_includepath
= NULL
;
1775 static inline void update_inc_ptrs(const char ***where
)
1778 if (*where
<= dirafter_includepath
) {
1779 dirafter_includepath
++;
1780 /* If this was the entry that we prepend, don't
1781 * rise the lower entries, even if they are at
1782 * the same level. */
1783 if (where
== &dirafter_includepath
)
1786 if (*where
<= sys_includepath
) {
1788 if (where
== &sys_includepath
)
1791 if (*where
<= isys_includepath
) {
1793 if (where
== &isys_includepath
)
1797 /* angle_includepath is actually never updated, since we
1798 * don't suppport -iquote rught now. May change some day. */
1799 if (*where
<= angle_includepath
) {
1800 angle_includepath
++;
1801 if (where
== &angle_includepath
)
1806 /* Add a path before 'where' and update the pointers associated with the
1807 * includepath array */
1808 static void add_path_entry(struct token
*token
, const char *path
,
1809 const char ***where
)
1814 /* Need one free entry.. */
1815 if (includepath
[INCLUDEPATHS
-2])
1816 error_die(token
->pos
, "too many include path entries");
1818 /* check that this is not a duplicate */
1821 if (strcmp(*dst
, path
) == 0)
1828 update_inc_ptrs(where
);
1831 * Move them all up starting at dst,
1832 * insert the new entry..
1835 const char *tmp
= *dst
;
1842 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1845 token
= token
->next
;
1846 if (eof_token(token
))
1848 if (token_type(token
) != TOKEN_STRING
) {
1849 warning(token
->pos
, "expected path string");
1852 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1856 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1859 token
= token
->next
;
1860 if (eof_token(token
))
1862 if (token_type(token
) != TOKEN_STRING
) {
1863 sparse_error(token
->pos
, "expected path string");
1866 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1870 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
1873 token
= token
->next
;
1874 if (eof_token(token
))
1876 if (token_type(token
) != TOKEN_STRING
) {
1877 sparse_error(token
->pos
, "expected path string");
1880 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1884 /* Add to end on includepath list - no pointer updates */
1885 static void add_dirafter_entry(struct token
*token
, const char *path
)
1887 const char **dst
= includepath
;
1889 /* Need one free entry.. */
1890 if (includepath
[INCLUDEPATHS
-2])
1891 error_die(token
->pos
, "too many include path entries");
1893 /* Add to the end */
1901 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1904 token
= token
->next
;
1905 if (eof_token(token
))
1907 if (token_type(token
) != TOKEN_STRING
) {
1908 sparse_error(token
->pos
, "expected path string");
1911 add_dirafter_entry(token
, token
->string
->data
);
1915 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1920 * Split the include path. Any directories specified with `-I'
1921 * options before `-I-' are searched only for headers requested with
1922 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1923 * If additional directories are specified with `-I' options after
1924 * the `-I-', those directories are searched for all `#include'
1926 * In addition, `-I-' inhibits the use of the directory of the current
1927 * file directory as the first search directory for `#include "FILE"'.
1929 quote_includepath
= includepath
+1;
1930 angle_includepath
= sys_includepath
;
1935 * We replace "#pragma xxx" with "__pragma__" in the token
1936 * stream. Just as an example.
1938 * We'll just #define that away for now, but the theory here
1939 * is that we can use this to insert arbitrary token sequences
1940 * to turn the pragmas into internal front-end sequences for
1941 * when we actually start caring about them.
1943 * So eventually this will turn into some kind of extended
1944 * __attribute__() like thing, except called __pragma__(xxx).
1946 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1948 struct token
*next
= *line
;
1950 if (match_ident(token
->next
, &once_ident
) && eof_token(token
->next
->next
)) {
1954 token
->ident
= &pragma_ident
;
1955 token
->pos
.newline
= 1;
1956 token
->pos
.whitespace
= 1;
1964 * We ignore #line for now.
1966 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1971 static int handle_ident(struct stream
*stream
, struct token
**line
, struct token
*token
)
1976 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1978 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
, 0));
1982 static bool expand_has_attribute(struct token
*token
, struct arg
*args
)
1984 struct token
*arg
= args
[0].expanded
;
1987 if (token_type(arg
) != TOKEN_IDENT
) {
1988 sparse_error(arg
->pos
, "identifier expected");
1992 sym
= lookup_symbol(arg
->ident
, NS_KEYWORD
);
1993 replace_with_bool(token
, sym
&& sym
->op
&& sym
->op
->attribute
);
1997 static bool expand_has_builtin(struct token
*token
, struct arg
*args
)
1999 struct token
*arg
= args
[0].expanded
;
2002 if (token_type(arg
) != TOKEN_IDENT
) {
2003 sparse_error(arg
->pos
, "identifier expected");
2007 sym
= lookup_symbol(arg
->ident
, NS_SYMBOL
);
2008 replace_with_bool(token
, sym
&& sym
->builtin
);
2012 static bool expand_has_extension(struct token
*token
, struct arg
*args
)
2014 struct token
*arg
= args
[0].expanded
;
2015 struct ident
*ident
;
2018 if (token_type(arg
) != TOKEN_IDENT
) {
2019 sparse_error(arg
->pos
, "identifier expected");
2024 if (ident
== &c_alignas_ident
)
2026 else if (ident
== &c_alignof_ident
)
2028 else if (ident
== &c_generic_selections_ident
)
2030 else if (ident
== &c_static_assert_ident
)
2033 replace_with_bool(token
, val
);
2037 static bool expand_has_feature(struct token
*token
, struct arg
*args
)
2039 struct token
*arg
= args
[0].expanded
;
2040 struct ident
*ident
;
2043 if (token_type(arg
) != TOKEN_IDENT
) {
2044 sparse_error(arg
->pos
, "identifier expected");
2049 if (standard
>= STANDARD_C11
) {
2050 if (ident
== &c_alignas_ident
)
2052 else if (ident
== &c_alignof_ident
)
2054 else if (ident
== &c_generic_selections_ident
)
2056 else if (ident
== &c_static_assert_ident
)
2060 replace_with_bool(token
, val
);
2064 static void create_arglist(struct symbol
*sym
, int count
)
2066 struct token
*token
;
2067 struct token
**next
;
2072 token
= __alloc_token(0);
2073 token_type(token
) = TOKEN_ARG_COUNT
;
2074 token
->count
.normal
= count
;
2075 sym
->arglist
= token
;
2076 next
= &token
->next
;
2079 struct token
*id
, *uses
;
2080 id
= __alloc_token(0);
2081 token_type(id
) = TOKEN_IDENT
;
2082 uses
= __alloc_token(0);
2083 token_type(uses
) = TOKEN_ARG_COUNT
;
2084 uses
->count
.normal
= 1;
2090 *next
= &eof_token_entry
;
2093 static void init_preprocessor(void)
2096 int stream
= init_stream(NULL
, "preprocessor", -1, includepath
);
2099 int (*handler
)(struct stream
*, struct token
**, struct token
*);
2101 { "define", handle_define
},
2102 { "weak_define", handle_weak_define
},
2103 { "strong_define", handle_strong_define
},
2104 { "undef", handle_undef
},
2105 { "strong_undef", handle_strong_undef
},
2106 { "warning", handle_warning
},
2107 { "error", handle_error
},
2108 { "include", handle_include
},
2109 { "include_next", handle_include_next
},
2110 { "pragma", handle_pragma
},
2111 { "line", handle_line
},
2112 { "ident", handle_ident
},
2114 // our internal preprocessor tokens
2115 { "nostdinc", handle_nostdinc
},
2116 { "add_include", handle_add_include
},
2117 { "add_isystem", handle_add_isystem
},
2118 { "add_system", handle_add_system
},
2119 { "add_dirafter", handle_add_dirafter
},
2120 { "split_include", handle_split_include
},
2121 { "argv_include", handle_argv_include
},
2123 { "ifdef", handle_ifdef
},
2124 { "ifndef", handle_ifndef
},
2125 { "else", handle_else
},
2126 { "endif", handle_endif
},
2127 { "if", handle_if
},
2128 { "elif", handle_elif
},
2132 void (*expand_simple
)(struct token
*);
2133 bool (*expand
)(struct token
*, struct arg
*args
);
2135 { "__LINE__", expand_line
},
2136 { "__FILE__", expand_file
},
2137 { "__BASE_FILE__", expand_basefile
},
2138 { "__DATE__", expand_date
},
2139 { "__TIME__", expand_time
},
2140 { "__COUNTER__", expand_counter
},
2141 { "__INCLUDE_LEVEL__", expand_include_level
},
2142 { "__has_attribute", NULL
, expand_has_attribute
},
2143 { "__has_builtin", NULL
, expand_has_builtin
},
2144 { "__has_extension", NULL
, expand_has_extension
},
2145 { "__has_feature", NULL
, expand_has_feature
},
2148 for (i
= 0; i
< ARRAY_SIZE(normal
); i
++) {
2150 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
2151 sym
->handler
= normal
[i
].handler
;
2154 for (i
= 0; i
< ARRAY_SIZE(special
); i
++) {
2156 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
2157 sym
->handler
= special
[i
].handler
;
2160 for (i
= 0; i
< ARRAY_SIZE(dynamic
); i
++) {
2162 sym
= create_symbol(stream
, dynamic
[i
].name
, SYM_NODE
, NS_MACRO
);
2163 sym
->expand_simple
= dynamic
[i
].expand_simple
;
2164 if ((sym
->expand
= dynamic
[i
].expand
) != NULL
)
2165 create_arglist(sym
, 1);
2171 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
2173 int (*handler
)(struct stream
*, struct token
**, struct token
*);
2174 struct token
*token
= start
->next
;
2176 int is_cond
= 0; // is one of {is,ifdef,ifndef,elif,else,endif}
2178 if (eof_token(token
))
2181 if (token_type(token
) == TOKEN_IDENT
) {
2182 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
2184 handler
= sym
->handler
;
2185 is_normal
= sym
->normal
;
2186 is_cond
= !sym
->normal
;
2188 handler
= handle_nondirective
;
2190 } else if (token_type(token
) == TOKEN_NUMBER
) {
2191 handler
= handle_line
;
2193 handler
= handle_nondirective
;
2197 dirty_stream(stream
);
2203 if (!is_cond
|| Wpedantic
)
2204 warning(start
->pos
, "directive in macro's argument list");
2206 if (!handler(stream
, line
, token
)) /* all set */
2210 free_preprocessor_line(token
);
2213 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
2215 struct token
*start
= *line
, *next
;
2216 struct token
**tp
= &start
->next
;
2220 if (next
->pos
.newline
)
2225 *tp
= &eof_token_entry
;
2226 handle_preprocessor_line(stream
, line
, start
);
2229 static void do_preprocess(struct token
**list
)
2233 while (!eof_token(next
= scan_next(list
))) {
2234 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
2236 if (next
->pos
.newline
&& match_op(next
, '#')) {
2237 if (!next
->pos
.noexpand
) {
2238 preprocessor_line(stream
, list
);
2239 __free_token(next
); /* Free the '#' token */
2244 switch (token_type(next
)) {
2245 case TOKEN_STREAMEND
:
2246 if (stream
->top_if
) {
2247 nesting_error(stream
);
2248 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
2249 stream
->top_if
= NULL
;
2253 stream
->constant
= CONSTANT_FILE_YES
;
2257 case TOKEN_STREAMBEGIN
:
2263 dirty_stream(stream
);
2264 if (false_nesting
) {
2270 if (token_type(next
) != TOKEN_IDENT
||
2271 expand_one_symbol(list
))
2277 struct token
* preprocess(struct token
*token
)
2280 init_preprocessor();
2281 do_preprocess(&token
);
2283 // Drop all expressions from preprocessing, they're not used any more.
2284 // This is not true when we have multiple files, though ;/
2285 // clear_expression_alloc();
2291 static int is_VA_ARGS_token(struct token
*token
)
2293 return (token_type(token
) == TOKEN_IDENT
) &&
2294 (token
->ident
== &__VA_ARGS___ident
);
2297 static void dump_macro(struct symbol
*sym
)
2299 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
2300 struct token
*args
[nargs
];
2301 struct token
*token
;
2303 printf("#define %s", show_ident(sym
->ident
));
2304 token
= sym
->arglist
;
2306 const char *sep
= "";
2309 for (; !eof_token(token
); token
= token
->next
) {
2310 if (token_type(token
) == TOKEN_ARG_COUNT
)
2312 if (is_VA_ARGS_token(token
))
2313 printf("%s...", sep
);
2315 printf("%s%s", sep
, show_token(token
));
2316 args
[narg
++] = token
;
2322 token
= sym
->expansion
;
2323 while (token_type(token
) != TOKEN_UNTAINT
) {
2324 struct token
*next
= token
->next
;
2325 if (token
->pos
.whitespace
)
2327 switch (token_type(token
)) {
2331 case TOKEN_STR_ARGUMENT
:
2334 case TOKEN_QUOTED_ARGUMENT
:
2335 case TOKEN_MACRO_ARGUMENT
:
2336 token
= args
[token
->argnum
];
2339 printf("%s", show_token(token
));
2346 void dump_macro_definitions(void)
2350 FOR_EACH_PTR(macros
, name
) {
2351 struct symbol
*sym
= lookup_macro(name
);
2354 } END_FOR_EACH_PTR(name
);