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
46 #include "expression.h"
49 static struct ident_list
*macros
; // only needed for -dD
50 static int false_nesting
= 0;
51 static int counter_macro
= 0; // __COUNTER__ expansion
52 static int include_level
= 0;
53 static int expanding
= 0;
55 #define INCLUDEPATHS 300
56 const char *includepath
[INCLUDEPATHS
+1] = {
63 static const char **quote_includepath
= includepath
;
64 static const char **angle_includepath
= includepath
+ 1;
65 static const char **isys_includepath
= includepath
+ 1;
66 static const char **sys_includepath
= includepath
+ 1;
67 static const char **dirafter_includepath
= includepath
+ 3;
69 #define dirty_stream(stream) \
71 if (!stream->dirty) { \
73 if (!stream->ifndef) \
74 stream->protect = NULL; \
78 #define end_group(stream) \
80 if (stream->ifndef == stream->top_if) { \
81 stream->ifndef = NULL; \
83 stream->protect = NULL; \
84 else if (stream->protect) \
89 #define nesting_error(stream) \
92 stream->ifndef = NULL; \
93 stream->protect = NULL; \
96 static struct token
*alloc_token(struct position
*pos
)
98 struct token
*token
= __alloc_token(0);
100 token
->pos
.stream
= pos
->stream
;
101 token
->pos
.line
= pos
->line
;
102 token
->pos
.pos
= pos
->pos
;
103 token
->pos
.whitespace
= 1;
107 /* Expand symbol 'sym' at '*list' */
108 static int expand(struct token
**, struct symbol
*);
110 static void replace_with_string(struct token
*token
, const char *str
)
112 int size
= strlen(str
) + 1;
113 struct string
*s
= __alloc_string(size
);
116 memcpy(s
->data
, str
, size
);
117 token_type(token
) = TOKEN_STRING
;
121 static void replace_with_integer(struct token
*token
, unsigned int val
)
123 char *buf
= __alloc_bytes(11);
124 sprintf(buf
, "%u", val
);
125 token_type(token
) = TOKEN_NUMBER
;
129 static struct symbol
*lookup_macro(struct ident
*ident
)
131 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_UNDEF
);
132 if (sym
&& sym
->namespace != NS_MACRO
)
137 static int token_defined(struct token
*token
)
139 if (token_type(token
) == TOKEN_IDENT
) {
140 struct symbol
*sym
= lookup_macro(token
->ident
);
142 sym
->used_in
= file_scope
;
148 sparse_error(token
->pos
, "expected preprocessor identifier");
152 static void replace_with_bool(struct token
*token
, bool val
)
154 static const char *string
[] = { "0", "1" };
156 token_type(token
) = TOKEN_NUMBER
;
157 token
->number
= string
[val
];
160 static void replace_with_defined(struct token
*token
)
162 replace_with_bool(token
, token_defined(token
));
165 static void expand_line(struct token
*token
)
167 replace_with_integer(token
, token
->pos
.line
);
170 static void expand_file(struct token
*token
)
172 replace_with_string(token
, stream_name(token
->pos
.stream
));
175 static void expand_basefile(struct token
*token
)
177 replace_with_string(token
, base_filename
);
181 static void expand_date(struct token
*token
)
183 static char buffer
[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
187 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
188 replace_with_string(token
, buffer
);
191 static void expand_time(struct token
*token
)
193 static char buffer
[9]; /* __TIME__: 2 + ':' + 2 + ':' + 2 + '\0' */
197 strftime(buffer
, 9, "%T", localtime(&t
));
198 replace_with_string(token
, buffer
);
201 static void expand_counter(struct token
*token
)
203 replace_with_integer(token
, counter_macro
++);
206 static void expand_include_level(struct token
*token
)
208 replace_with_integer(token
, include_level
- 1);
211 static int expand_one_symbol(struct token
**list
)
213 struct token
*token
= *list
;
216 if (token
->pos
.noexpand
)
219 sym
= lookup_macro(token
->ident
);
222 store_macro_pos(token
);
223 if (sym
->expand_simple
) {
224 sym
->expand_simple(token
);
229 sym
->used_in
= file_scope
;
231 rc
= expand(list
, sym
);
237 static inline struct token
*scan_next(struct token
**where
)
239 struct token
*token
= *where
;
240 if (token_type(token
) != TOKEN_UNTAINT
)
243 token
->ident
->tainted
= 0;
245 } while (token_type(token
) == TOKEN_UNTAINT
);
250 static void expand_list(struct token
**list
)
253 while (!eof_token(next
= scan_next(list
))) {
254 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
259 static void preprocessor_line(struct stream
*stream
, struct token
**line
);
261 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
, int count
)
263 struct stream
*stream
= input_streams
+ prev
->pos
.stream
;
264 struct token
**p
= &prev
->next
;
268 while (!eof_token(next
= scan_next(p
))) {
269 if (next
->pos
.newline
&& match_op(next
, '#')) {
270 if (!next
->pos
.noexpand
) {
271 preprocessor_line(stream
, p
);
272 __free_token(next
); /* Free the '#' token */
276 switch (token_type(next
)) {
277 case TOKEN_STREAMEND
:
278 case TOKEN_STREAMBEGIN
:
279 *p
= &eof_token_entry
;
282 case TOKEN_WIDE_STRING
:
284 next
->string
->immutable
= 1;
292 if (match_op(next
, '(')) {
294 } else if (match_op(next
, ')')) {
297 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
300 next
->pos
.stream
= pos
->stream
;
301 next
->pos
.line
= pos
->line
;
302 next
->pos
.pos
= pos
->pos
;
303 next
->pos
.newline
= 0;
306 *p
= &eof_token_entry
;
311 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
316 struct token
*expanded
;
323 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
325 int wanted
= arglist
->count
.normal
;
326 struct token
*next
= NULL
;
329 arglist
= arglist
->next
; /* skip counter */
332 next
= collect_arg(start
, 0, &what
->pos
, 0);
335 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
340 for (count
= 0; count
< wanted
; count
++) {
341 struct argcount
*p
= &arglist
->next
->count
;
342 next
= collect_arg(start
, p
->vararg
, &what
->pos
, p
->normal
);
345 if (p
->vararg
&& wanted
== 1 && eof_token(start
->next
))
347 arglist
= arglist
->next
->next
;
348 args
[count
].arg
= start
->next
;
349 args
[count
].n_normal
= p
->normal
;
350 args
[count
].n_quoted
= p
->quoted
;
351 args
[count
].n_str
= p
->str
;
352 if (match_op(next
, ')')) {
358 if (count
== wanted
&& !match_op(next
, ')'))
360 if (count
== wanted
- 1) {
361 struct argcount
*p
= &arglist
->next
->count
;
364 args
[count
].arg
= NULL
;
365 args
[count
].n_normal
= p
->normal
;
366 args
[count
].n_quoted
= p
->quoted
;
367 args
[count
].n_str
= p
->str
;
369 if (count
< wanted
- 1)
372 what
->next
= next
->next
;
376 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
377 show_token(what
), wanted
, count
);
380 while (match_op(next
, ',')) {
381 next
= collect_arg(next
, 0, &what
->pos
, 0);
386 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
387 show_token(what
), count
, wanted
);
390 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
393 what
->next
= next
->next
;
397 static struct token
*dup_list(struct token
*list
)
399 struct token
*res
= NULL
;
400 struct token
**p
= &res
;
402 while (!eof_token(list
)) {
403 struct token
*newtok
= __alloc_token(0);
412 static const char *show_token_sequence(struct token
*token
, int quote
)
414 static char buffer
[MAX_STRING
];
418 if (!token
&& !quote
)
420 while (!eof_token(token
)) {
421 const char *val
= quote
? quote_token(token
) : show_token(token
);
422 int len
= strlen(val
);
424 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
425 sparse_error(token
->pos
, "too long token expansion");
431 memcpy(ptr
, val
, len
);
434 whitespace
= token
->pos
.whitespace
;
440 static struct token
*stringify(struct token
*arg
)
442 const char *s
= show_token_sequence(arg
, 1);
443 int size
= strlen(s
)+1;
444 struct token
*token
= __alloc_token(0);
445 struct string
*string
= __alloc_string(size
);
447 memcpy(string
->data
, s
, size
);
448 string
->length
= size
;
449 token
->pos
= arg
->pos
;
450 token_type(token
) = TOKEN_STRING
;
451 token
->string
= string
;
452 token
->next
= &eof_token_entry
;
456 static void expand_arguments(int count
, struct arg
*args
)
459 for (i
= 0; i
< count
; i
++) {
460 struct token
*arg
= args
[i
].arg
;
462 arg
= &eof_token_entry
;
464 args
[i
].str
= stringify(arg
);
465 if (args
[i
].n_normal
) {
466 if (!args
[i
].n_quoted
) {
467 args
[i
].expanded
= arg
;
469 } else if (eof_token(arg
)) {
470 args
[i
].expanded
= arg
;
472 args
[i
].expanded
= dup_list(arg
);
474 expand_list(&args
[i
].expanded
);
480 * Possibly valid combinations:
481 * - ident + ident -> ident
482 * - ident + number -> ident unless number contains '.', '+' or '-'.
483 * - 'L' + char constant -> wide char constant
484 * - 'L' + string literal -> wide string literal
485 * - number + number -> number
486 * - number + ident -> number
487 * - number + '.' -> number
488 * - number + '+' or '-' -> number, if number used to end on [eEpP].
489 * - '.' + number -> number, if number used to start with a digit.
490 * - special + special -> either special or an error.
492 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
495 enum token_type t1
= token_type(left
), t2
= token_type(right
);
497 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
500 if (t1
== TOKEN_IDENT
&& left
->ident
== &L_ident
) {
501 if (t2
>= TOKEN_CHAR
&& t2
< TOKEN_WIDE_CHAR
)
502 return t2
+ TOKEN_WIDE_CHAR
- TOKEN_CHAR
;
503 if (t2
== TOKEN_STRING
)
504 return TOKEN_WIDE_STRING
;
507 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
510 strcpy(p
, show_token(left
));
511 strcat(p
, show_token(right
));
517 if (t1
== TOKEN_IDENT
) {
518 if (t2
== TOKEN_SPECIAL
)
520 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
525 if (t1
== TOKEN_NUMBER
) {
526 if (t2
== TOKEN_SPECIAL
) {
527 switch (right
->special
) {
531 if (strchr("eEpP", p
[len
- 2]))
540 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
543 return TOKEN_SPECIAL
;
546 static int merge(struct token
*left
, struct token
*right
)
548 static char buffer
[512];
549 enum token_type res
= combine(left
, right
, buffer
);
554 left
->ident
= built_in_ident(buffer
);
555 left
->pos
.noexpand
= 0;
559 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
560 left
->number
= xstrdup(buffer
);
564 if (buffer
[2] && buffer
[3])
566 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
567 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
574 case TOKEN_WIDE_CHAR
:
575 case TOKEN_WIDE_STRING
:
576 token_type(left
) = res
;
577 left
->pos
.noexpand
= 0;
578 left
->string
= right
->string
;
581 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
582 token_type(left
) = res
;
583 left
->pos
.noexpand
= 0;
584 memcpy(left
->embedded
, right
->embedded
, 4);
590 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
594 static struct token
*dup_token(struct token
*token
, struct position
*streampos
)
596 struct token
*alloc
= alloc_token(streampos
);
597 token_type(alloc
) = token_type(token
);
598 alloc
->pos
.newline
= token
->pos
.newline
;
599 alloc
->pos
.whitespace
= token
->pos
.whitespace
;
600 alloc
->number
= token
->number
;
601 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
605 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
607 int need_copy
= --*count
;
608 while (!eof_token(list
)) {
611 token
= dup_token(list
, &list
->pos
);
614 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
615 token
->pos
.noexpand
= 1;
617 where
= &token
->next
;
620 *where
= &eof_token_entry
;
624 static int handle_kludge(struct token
**p
, struct arg
*args
)
626 struct token
*t
= (*p
)->next
->next
;
628 struct arg
*v
= &args
[t
->argnum
];
629 if (token_type(t
->next
) != TOKEN_CONCAT
) {
631 /* ignore the first ## */
635 /* skip the entire thing */
639 if (v
->arg
&& !eof_token(v
->arg
))
640 return 0; /* no magic */
645 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
647 struct position
*base_pos
= &(*list
)->pos
;
649 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
651 for (; !eof_token(body
); body
= body
->next
) {
652 struct token
*added
, *arg
;
656 switch (token_type(body
)) {
657 case TOKEN_GNU_KLUDGE
:
659 * GNU kludge: if we had <comma>##<vararg>, behaviour
660 * depends on whether we had enough arguments to have
661 * a vararg. If we did, ## is just ignored. Otherwise
662 * both , and ## are ignored. Worse, there can be
663 * an arbitrary number of ##<arg> in between; if all of
664 * those are empty, we act as if they hadn't been there,
665 * otherwise we act as if the kludge didn't exist.
668 if (handle_kludge(&body
, args
)) {
675 added
= dup_token(t
, base_pos
);
676 token_type(added
) = TOKEN_SPECIAL
;
680 case TOKEN_STR_ARGUMENT
:
681 arg
= args
[body
->argnum
].str
;
682 count
= &args
[body
->argnum
].n_str
;
685 case TOKEN_QUOTED_ARGUMENT
:
686 arg
= args
[body
->argnum
].arg
;
687 count
= &args
[body
->argnum
].n_quoted
;
688 if (!arg
|| eof_token(arg
)) {
697 case TOKEN_MACRO_ARGUMENT
:
698 arg
= args
[body
->argnum
].expanded
;
699 count
= &args
[body
->argnum
].n_normal
;
700 if (eof_token(arg
)) {
705 tail
= copy(&added
, arg
, count
);
706 added
->pos
.newline
= body
->pos
.newline
;
707 added
->pos
.whitespace
= body
->pos
.whitespace
;
711 if (state
== Placeholder
)
718 added
= dup_token(body
, base_pos
);
719 if (added
->ident
->tainted
)
720 added
->pos
.noexpand
= 1;
725 added
= dup_token(body
, base_pos
);
731 * if we got to doing real concatenation, we already have
732 * added something into the list, so containing_token() is OK.
734 if (state
== Concat
&& merge(containing_token(list
), added
)) {
736 if (tail
!= &added
->next
)
744 *list
= &eof_token_entry
;
748 static int expand(struct token
**list
, struct symbol
*sym
)
751 struct token
*token
= *list
;
752 struct ident
*expanding
= token
->ident
;
754 struct token
*expansion
= sym
->expansion
;
755 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
756 struct arg args
[nargs
];
758 if (expanding
->tainted
) {
759 token
->pos
.noexpand
= 1;
764 if (!match_op(scan_next(&token
->next
), '('))
766 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
768 expand_arguments(nargs
, args
);
772 return sym
->expand(token
, args
) ? 0 : 1;
774 expanding
->tainted
= 1;
777 tail
= substitute(list
, expansion
, args
);
779 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
780 * We still can lose the newline flag if the sucker expands to nothing,
781 * but the price of dealing with that is probably too high (we'd need
782 * to collect the flags during scan_next())
784 (*list
)->pos
.newline
= token
->pos
.newline
;
785 (*list
)->pos
.whitespace
= token
->pos
.whitespace
;
791 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
793 static char buffer
[256];
796 while (!eof_token(token
) && !match_op(token
, endop
)) {
798 const char *val
= token
->string
->data
;
799 if (token_type(token
) != TOKEN_STRING
)
800 val
= show_token(token
);
802 memcpy(ptr
, val
, len
);
807 if (endop
&& !match_op(token
, endop
))
808 sparse_error(start
->pos
, "expected '>' at end of filename");
812 static int already_tokenized(const char *path
)
816 for (stream
= *hash_stream(path
); stream
>= 0 ; stream
= next
) {
817 struct stream
*s
= input_streams
+ stream
;
819 next
= s
->next_stream
;
821 if (strcmp(path
, s
->name
))
825 if (s
->constant
!= CONSTANT_FILE_YES
)
827 if (strcmp(path
, s
->name
))
829 if (s
->protect
&& !lookup_macro(s
->protect
))
836 /* Handle include of header files.
837 * The relevant options are made compatible with gcc. The only options that
838 * are not supported is -withprefix and friends.
840 * Three set of include paths are known:
841 * quote_includepath: Path to search when using #include "file.h"
842 * angle_includepath: Paths to search when using #include <file.h>
843 * isys_includepath: Paths specified with -isystem, come before the
844 * built-in system include paths. Gcc would suppress
845 * warnings from system headers. Here we separate
846 * them from the angle_ ones to keep search ordering.
848 * sys_includepath: Built-in include paths.
849 * dirafter_includepath Paths added with -dirafter.
851 * The above is implemented as one array with pointers
853 * quote_includepath ---> | |
857 * angle_includepath ---> | |
859 * isys_includepath ---> | |
861 * sys_includepath ---> | |
863 * dirafter_includepath -> | |
866 * -I dir insert dir just before isys_includepath and move the rest
867 * -I- makes all dirs specified with -I before to quote dirs only and
868 * angle_includepath is set equal to isys_includepath.
869 * -nostdinc removes all sys dirs by storing NULL in entry pointed
870 * to by * sys_includepath. Note that this will reset all dirs built-in
871 * and added before -nostdinc by -isystem and -idirafter.
872 * -isystem dir adds dir where isys_includepath points adding this dir as
874 * -idirafter dir adds dir to the end of the list
877 static void set_stream_include_path(struct stream
*stream
)
879 const char *path
= stream
->path
;
881 const char *p
= strrchr(stream
->name
, '/');
884 int len
= p
- stream
->name
+ 1;
885 char *m
= malloc(len
+1);
886 /* This includes the final "/" */
887 memcpy(m
, stream
->name
, len
);
893 includepath
[0] = path
;
896 static int try_include(struct position pos
, const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
899 int plen
= strlen(path
);
900 static char fullname
[PATH_MAX
];
902 memcpy(fullname
, path
, plen
);
903 if (plen
&& path
[plen
-1] != '/') {
904 fullname
[plen
] = '/';
907 memcpy(fullname
+plen
, filename
, flen
);
908 if (already_tokenized(fullname
))
910 fd
= open(fullname
, O_RDONLY
);
912 char *streamname
= xmemdup(fullname
, plen
+ flen
);
913 *where
= tokenize(&pos
, streamname
, fd
, *where
, next_path
);
920 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
924 while ((path
= *pptr
++) != NULL
) {
925 if (!try_include(token
->pos
, path
, filename
, flen
, list
, pptr
))
932 static int free_preprocessor_line(struct token
*token
)
934 while (token_type(token
) != TOKEN_EOF
) {
935 struct token
*free
= token
;
942 const char *find_include(const char *skip
, const char *look_for
)
945 struct dirent
*entry
;
949 static char buf
[PATH_MAX
+ 1];
955 if (!getcwd(cwd
, sizeof(cwd
)))
958 while ((entry
= readdir(dp
))) {
959 lstat(entry
->d_name
, &statbuf
);
961 if (strcmp(entry
->d_name
, look_for
) == 0) {
962 snprintf(buf
, sizeof(buf
), "%s/%s", cwd
, entry
->d_name
);
967 if (S_ISDIR(statbuf
.st_mode
)) {
968 /* Found a directory, but ignore . and .. */
969 if (strcmp(".", entry
->d_name
) == 0 ||
970 strcmp("..", entry
->d_name
) == 0 ||
971 strcmp(skip
, entry
->d_name
) == 0)
974 chdir(entry
->d_name
);
975 ret
= find_include("", look_for
);
989 const char *search_dir(const char *stop
, const char *look_for
)
996 if (!getcwd(cwd
, sizeof(cwd
)))
1001 ret
= find_include(cnt
++ ? cwd
+ len
+ 1 : "", look_for
);
1005 if (strcmp(cwd
, stop
) == 0 ||
1006 strcmp(cwd
, "/usr/include") == 0 ||
1007 strcmp(cwd
, "/usr/local/include") == 0 ||
1008 strlen(cwd
) <= 10 || /* heck... don't search /usr/lib/ */
1009 strcmp(cwd
, "/") == 0)
1012 while (--len
>= 0) {
1013 if (cwd
[len
] == '/') {
1024 static void use_best_guess_header_file(struct token
*token
, const char *filename
, struct token
**list
)
1027 char dir_part
[PATH_MAX
];
1028 const char *file_part
;
1029 const char *include_name
;
1033 /* Avoid guessing includes recursively. */
1037 if (!filename
|| filename
[0] == '\0')
1040 file_part
= filename
;
1041 while ((filename
= strchr(filename
, '/'))) {
1044 file_part
= filename
;
1047 snprintf(dir_part
, sizeof(dir_part
), "%s", stream_name(token
->pos
.stream
));
1048 len
= strlen(dir_part
);
1049 while (--len
>= 0) {
1050 if (dir_part
[len
] == '/') {
1051 dir_part
[len
] = '\0';
1056 sprintf(dir_part
, ".");
1058 if (!getcwd(cwd
, sizeof(cwd
)))
1062 include_name
= search_dir(cwd
, file_part
);
1066 sparse_error(token
->pos
, "using '%s'", include_name
);
1068 try_include(token
->pos
, "", include_name
, strlen(include_name
), list
, includepath
);
1071 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, int how
)
1073 const char *filename
;
1081 if (!match_op(next
, '<')) {
1082 expand_list(&token
->next
);
1085 if (match_op(token
->next
, '<')) {
1092 filename
= token_name_sequence(token
, expect
, token
);
1093 flen
= strlen(filename
) + 1;
1095 /* Absolute path? */
1096 if (filename
[0] == '/') {
1097 if (try_include(token
->pos
, "", filename
, flen
, list
, includepath
))
1104 path
= stream
->next_path
;
1107 includepath
[0] = "";
1111 /* Dir of input file is first dir to search for quoted includes */
1112 set_stream_include_path(stream
);
1113 path
= expect
? angle_includepath
: quote_includepath
;
1116 /* Check the standard include paths.. */
1117 if (do_include_path(path
, list
, token
, filename
, flen
))
1120 sparse_error(token
->pos
, "unable to open '%s'", filename
);
1121 use_best_guess_header_file(token
, filename
, list
);
1125 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
1127 return handle_include_path(stream
, list
, token
, 0);
1130 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
1132 return handle_include_path(stream
, list
, token
, 1);
1135 static int handle_argv_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
1137 return handle_include_path(stream
, list
, token
, 2);
1140 static int token_different(struct token
*t1
, struct token
*t2
)
1144 if (token_type(t1
) != token_type(t2
))
1147 switch (token_type(t1
)) {
1149 different
= t1
->ident
!= t2
->ident
;
1151 case TOKEN_ARG_COUNT
:
1154 case TOKEN_GNU_KLUDGE
:
1158 different
= strcmp(t1
->number
, t2
->number
);
1161 different
= t1
->special
!= t2
->special
;
1163 case TOKEN_MACRO_ARGUMENT
:
1164 case TOKEN_QUOTED_ARGUMENT
:
1165 case TOKEN_STR_ARGUMENT
:
1166 different
= t1
->argnum
!= t2
->argnum
;
1168 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
1169 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
1170 different
= memcmp(t1
->embedded
, t2
->embedded
, 4);
1173 case TOKEN_WIDE_CHAR
:
1175 case TOKEN_WIDE_STRING
: {
1176 struct string
*s1
, *s2
;
1181 if (s1
->length
!= s2
->length
)
1183 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
1193 static int token_list_different(struct token
*list1
, struct token
*list2
)
1198 if (!list1
|| !list2
)
1200 if (token_different(list1
, list2
))
1202 list1
= list1
->next
;
1203 list2
= list2
->next
;
1207 static inline void set_arg_count(struct token
*token
)
1209 token_type(token
) = TOKEN_ARG_COUNT
;
1210 token
->count
.normal
= token
->count
.quoted
=
1211 token
->count
.str
= token
->count
.vararg
= 0;
1214 static struct token
*parse_arguments(struct token
*list
)
1216 struct token
*arg
= list
->next
, *next
= list
;
1217 struct argcount
*count
= &list
->count
;
1219 set_arg_count(list
);
1221 if (match_op(arg
, ')')) {
1223 list
->next
= &eof_token_entry
;
1227 while (token_type(arg
) == TOKEN_IDENT
) {
1228 if (arg
->ident
== &__VA_ARGS___ident
)
1230 if (!++count
->normal
)
1234 if (match_op(next
, ',')) {
1235 set_arg_count(next
);
1240 if (match_op(next
, ')')) {
1241 set_arg_count(next
);
1243 arg
->next
->next
= &eof_token_entry
;
1247 /* normal cases are finished here */
1249 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
1250 if (match_op(next
->next
, ')')) {
1251 set_arg_count(next
);
1252 next
->count
.vararg
= 1;
1254 arg
->next
->next
= &eof_token_entry
;
1262 if (eof_token(next
)) {
1270 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
1272 token_type(arg
) = TOKEN_IDENT
;
1273 arg
->ident
= &__VA_ARGS___ident
;
1274 if (!match_op(next
, ')'))
1276 if (!++count
->normal
)
1278 set_arg_count(next
);
1279 next
->count
.vararg
= 1;
1281 arg
->next
->next
= &eof_token_entry
;
1285 if (eof_token(arg
)) {
1289 if (match_op(arg
, ','))
1296 sparse_error(arg
->pos
, "parameter name missing");
1299 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
1303 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1306 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1309 sparse_error(arg
->pos
, "too many arguments in macro definition");
1313 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1315 struct ident
*ident
= token
->ident
;
1318 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1321 arglist
= arglist
->next
;
1323 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1324 if (arglist
->ident
== ident
) {
1325 struct argcount
*count
= &arglist
->next
->count
;
1329 token_type(token
) = type
;
1331 case TOKEN_MACRO_ARGUMENT
:
1332 n
= ++count
->normal
;
1334 case TOKEN_QUOTED_ARGUMENT
:
1335 n
= ++count
->quoted
;
1341 return count
->vararg
? 2 : 1;
1343 * XXX - need saner handling of that
1344 * (>= 1024 instances of argument)
1346 token_type(token
) = TOKEN_ERROR
;
1353 static struct token
*handle_hash(struct token
**p
, struct token
*arglist
)
1355 struct token
*token
= *p
;
1357 struct token
*next
= token
->next
;
1358 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1360 next
->pos
.whitespace
= token
->pos
.whitespace
;
1361 __free_token(token
);
1364 token
->pos
.noexpand
= 1;
1369 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1373 /* token->next is ## */
1374 static struct token
*handle_hashhash(struct token
*token
, struct token
*arglist
)
1376 struct token
*last
= token
;
1377 struct token
*concat
;
1378 int state
= match_op(token
, ',');
1380 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1386 /* eat duplicate ## */
1387 concat
= token
->next
;
1388 while (match_op(t
= concat
->next
, SPECIAL_HASHHASH
)) {
1390 __free_token(concat
);
1393 token_type(concat
) = TOKEN_CONCAT
;
1398 if (match_op(t
, '#')) {
1399 t
= handle_hash(&concat
->next
, arglist
);
1404 is_arg
= try_arg(t
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1406 if (state
== 1 && is_arg
) {
1410 state
= match_op(t
, ',');
1414 if (!match_op(token
->next
, SPECIAL_HASHHASH
))
1417 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1419 token_type(last
) = TOKEN_GNU_KLUDGE
;
1423 sparse_error(concat
->pos
, "'##' cannot appear at the ends of macro expansion");
1427 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1429 struct token
*token
= expansion
;
1432 if (match_op(token
, SPECIAL_HASHHASH
))
1435 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1436 if (match_op(token
, '#')) {
1437 token
= handle_hash(p
, arglist
);
1441 if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1442 token
= handle_hashhash(token
, arglist
);
1446 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1448 switch (token_type(token
)) {
1453 case TOKEN_WIDE_STRING
:
1454 token
->string
->immutable
= 1;
1458 token
= alloc_token(&expansion
->pos
);
1459 token_type(token
) = TOKEN_UNTAINT
;
1460 token
->ident
= name
;
1466 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1469 sparse_error(token
->pos
, "too many instances of argument in body");
1473 static int do_define(struct position pos
, struct token
*token
, struct ident
*name
,
1474 struct token
*arglist
, struct token
*expansion
, int attr
)
1479 expansion
= parse_expansion(expansion
, arglist
, name
);
1483 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1487 if (attr
< sym
->attr
)
1490 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1492 if (token_list_different(sym
->expansion
, expansion
) ||
1493 token_list_different(sym
->arglist
, arglist
)) {
1495 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1496 || sym
->used_in
== file_scope
) {
1497 warning(pos
, "preprocessor token %.*s redefined",
1498 name
->len
, name
->name
);
1499 info(sym
->pos
, "this was the original definition");
1505 if (!sym
|| sym
->scope
!= file_scope
) {
1506 sym
= alloc_symbol(pos
, SYM_NODE
);
1507 bind_symbol(sym
, name
, NS_MACRO
);
1508 add_ident(¯os
, name
);
1513 sym
->expansion
= expansion
;
1514 sym
->arglist
= arglist
;
1515 if (token
) /* Free the "define" token, but not the rest of the line */
1516 __free_token(token
);
1519 sym
->namespace = NS_MACRO
;
1520 sym
->used_in
= NULL
;
1527 // predefine a macro with a printf-formatted value
1528 // @name: the name of the macro
1529 // @weak: 0/1 for a normal or a weak define
1530 // @fmt: the printf format followed by it's arguments.
1532 // The type of the value is automatically infered:
1533 // TOKEN_NUMBER if it starts by a digit, TOKEN_IDENT otherwise.
1534 // If @fmt is null or empty, the macro is defined with an empty definition.
1535 void predefine(const char *name
, int weak
, const char *fmt
, ...)
1537 struct ident
*ident
= built_in_ident(name
);
1538 struct token
*value
= &eof_token_entry
;
1539 int attr
= weak
? SYM_ATTR_WEAK
: SYM_ATTR_NORMAL
;
1541 if (fmt
&& fmt
[0]) {
1542 static char buf
[256];
1546 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1549 value
= __alloc_token(0);
1550 if (isdigit((unsigned char)buf
[0])) {
1551 token_type(value
) = TOKEN_NUMBER
;
1552 value
->number
= xstrdup(buf
);
1554 token_type(value
) = TOKEN_IDENT
;
1555 value
->ident
= built_in_ident(buf
);
1557 value
->pos
.whitespace
= 1;
1558 value
->next
= &eof_token_entry
;
1561 do_define(value
->pos
, NULL
, ident
, NULL
, value
, attr
);
1565 // like predefine() but only if one of the non-standard dialect is chosen
1566 void predefine_nostd(const char *name
)
1568 if ((standard
& STANDARD_GNU
) || (standard
== STANDARD_NONE
))
1569 predefine(name
, 1, "1");
1572 static void predefine_fmt(const char *fmt
, int weak
, va_list ap
)
1574 static char buf
[256];
1576 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1577 predefine(buf
, weak
, "1");
1580 void predefine_strong(const char *fmt
, ...)
1585 predefine_fmt(fmt
, 0, ap
);
1589 void predefine_weak(const char *fmt
, ...)
1594 predefine_fmt(fmt
, 1, ap
);
1598 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1600 struct token
*arglist
, *expansion
;
1601 struct token
*left
= token
->next
;
1604 if (token_type(left
) != TOKEN_IDENT
) {
1605 sparse_error(token
->pos
, "expected identifier to 'define'");
1612 expansion
= left
->next
;
1613 if (!expansion
->pos
.whitespace
) {
1614 if (match_op(expansion
, '(')) {
1615 arglist
= expansion
;
1616 expansion
= parse_arguments(expansion
);
1619 } else if (!eof_token(expansion
)) {
1620 warning(expansion
->pos
,
1621 "no whitespace before object-like macro body");
1625 return do_define(left
->pos
, token
, name
, arglist
, expansion
, attr
);
1628 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1630 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1633 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1635 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1638 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1640 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1643 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1645 struct token
*left
= token
->next
;
1648 if (token_type(left
) != TOKEN_IDENT
) {
1649 sparse_error(token
->pos
, "expected identifier to 'undef'");
1653 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1655 if (attr
< sym
->attr
)
1657 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1659 } else if (attr
<= SYM_ATTR_NORMAL
)
1662 if (!sym
|| sym
->scope
!= file_scope
) {
1663 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1664 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1667 sym
->namespace = NS_UNDEF
;
1668 sym
->used_in
= NULL
;
1674 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1676 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1679 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1681 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1684 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int cond
)
1686 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1687 free_preprocessor_line(token
->next
);
1688 token
->next
= stream
->top_if
;
1689 stream
->top_if
= token
;
1690 if (false_nesting
|| cond
!= 1)
1695 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1697 struct token
*next
= token
->next
;
1699 if (token_type(next
) == TOKEN_IDENT
) {
1700 arg
= token_defined(next
);
1702 dirty_stream(stream
);
1704 sparse_error(token
->pos
, "expected preprocessor identifier");
1707 return preprocessor_if(stream
, token
, arg
);
1710 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1712 struct token
*next
= token
->next
;
1714 if (token_type(next
) == TOKEN_IDENT
) {
1715 if (!stream
->dirty
&& !stream
->ifndef
) {
1716 if (!stream
->protect
) {
1717 stream
->ifndef
= token
;
1718 stream
->protect
= next
->ident
;
1719 } else if (stream
->protect
== next
->ident
) {
1720 stream
->ifndef
= token
;
1724 arg
= !token_defined(next
);
1726 dirty_stream(stream
);
1728 sparse_error(token
->pos
, "expected preprocessor identifier");
1732 return preprocessor_if(stream
, token
, arg
);
1736 * Expression handling for #if and #elif; it differs from normal expansion
1737 * due to special treatment of "defined".
1739 static int expression_value(struct token
**where
)
1741 struct expression
*expr
;
1743 struct token
**list
= where
, **beginning
= NULL
;
1747 while (!eof_token(p
= scan_next(list
))) {
1750 if (token_type(p
) != TOKEN_IDENT
)
1752 if (p
->ident
== &defined_ident
) {
1757 if (!expand_one_symbol(list
))
1759 if (token_type(p
) != TOKEN_IDENT
)
1761 token_type(p
) = TOKEN_ZERO_IDENT
;
1764 if (match_op(p
, '(')) {
1768 replace_with_defined(p
);
1773 if (token_type(p
) == TOKEN_IDENT
)
1777 replace_with_defined(p
);
1782 if (!match_op(p
, ')'))
1783 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1790 p
= constant_expression(*where
, &expr
);
1792 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
, 0));
1793 value
= get_expression_value(expr
);
1797 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1801 value
= expression_value(&token
->next
);
1803 dirty_stream(stream
);
1804 return preprocessor_if(stream
, token
, value
);
1807 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1809 struct token
*top_if
= stream
->top_if
;
1813 nesting_error(stream
);
1814 sparse_error(token
->pos
, "unmatched #elif within stream");
1818 if (token_type(top_if
) == TOKEN_ELSE
) {
1819 nesting_error(stream
);
1820 sparse_error(token
->pos
, "#elif after #else");
1826 dirty_stream(stream
);
1827 if (token_type(top_if
) != TOKEN_IF
)
1829 if (false_nesting
) {
1831 if (!expression_value(&token
->next
))
1835 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1840 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1842 struct token
*top_if
= stream
->top_if
;
1846 nesting_error(stream
);
1847 sparse_error(token
->pos
, "unmatched #else within stream");
1851 if (token_type(top_if
) == TOKEN_ELSE
) {
1852 nesting_error(stream
);
1853 sparse_error(token
->pos
, "#else after #else");
1855 if (false_nesting
) {
1856 if (token_type(top_if
) == TOKEN_IF
)
1861 token_type(top_if
) = TOKEN_ELSE
;
1865 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1867 struct token
*top_if
= stream
->top_if
;
1870 nesting_error(stream
);
1871 sparse_error(token
->pos
, "unmatched #endif in stream");
1876 stream
->top_if
= top_if
->next
;
1877 __free_token(top_if
);
1881 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1883 warning(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1887 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1889 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1893 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1896 * Do we have any non-system includes?
1897 * Clear them out if so..
1899 *sys_includepath
= NULL
;
1903 static inline void update_inc_ptrs(const char ***where
)
1906 if (*where
<= dirafter_includepath
) {
1907 dirafter_includepath
++;
1908 /* If this was the entry that we prepend, don't
1909 * rise the lower entries, even if they are at
1910 * the same level. */
1911 if (where
== &dirafter_includepath
)
1914 if (*where
<= sys_includepath
) {
1916 if (where
== &sys_includepath
)
1919 if (*where
<= isys_includepath
) {
1921 if (where
== &isys_includepath
)
1925 /* angle_includepath is actually never updated, since we
1926 * don't suppport -iquote rught now. May change some day. */
1927 if (*where
<= angle_includepath
) {
1928 angle_includepath
++;
1929 if (where
== &angle_includepath
)
1934 /* Add a path before 'where' and update the pointers associated with the
1935 * includepath array */
1936 static void add_path_entry(struct token
*token
, const char *path
,
1937 const char ***where
)
1942 /* Need one free entry.. */
1943 if (includepath
[INCLUDEPATHS
-2])
1944 error_die(token
->pos
, "too many include path entries");
1946 /* check that this is not a duplicate */
1949 if (strcmp(*dst
, path
) == 0)
1956 update_inc_ptrs(where
);
1959 * Move them all up starting at dst,
1960 * insert the new entry..
1963 const char *tmp
= *dst
;
1970 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1973 token
= token
->next
;
1974 if (eof_token(token
))
1976 if (token_type(token
) != TOKEN_STRING
) {
1977 warning(token
->pos
, "expected path string");
1980 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1984 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1987 token
= token
->next
;
1988 if (eof_token(token
))
1990 if (token_type(token
) != TOKEN_STRING
) {
1991 sparse_error(token
->pos
, "expected path string");
1994 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1998 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
2001 token
= token
->next
;
2002 if (eof_token(token
))
2004 if (token_type(token
) != TOKEN_STRING
) {
2005 sparse_error(token
->pos
, "expected path string");
2008 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
2012 /* Add to end on includepath list - no pointer updates */
2013 static void add_dirafter_entry(struct token
*token
, const char *path
)
2015 const char **dst
= includepath
;
2017 /* Need one free entry.. */
2018 if (includepath
[INCLUDEPATHS
-2])
2019 error_die(token
->pos
, "too many include path entries");
2021 /* Add to the end */
2029 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
2032 token
= token
->next
;
2033 if (eof_token(token
))
2035 if (token_type(token
) != TOKEN_STRING
) {
2036 sparse_error(token
->pos
, "expected path string");
2039 add_dirafter_entry(token
, token
->string
->data
);
2043 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
2048 * Split the include path. Any directories specified with `-I'
2049 * options before `-I-' are searched only for headers requested with
2050 * `#include "FILE"'; they are not searched for `#include <FILE>'.
2051 * If additional directories are specified with `-I' options after
2052 * the `-I-', those directories are searched for all `#include'
2054 * In addition, `-I-' inhibits the use of the directory of the current
2055 * file directory as the first search directory for `#include "FILE"'.
2057 quote_includepath
= includepath
+1;
2058 angle_includepath
= sys_includepath
;
2063 * We replace "#pragma xxx" with "__pragma__" in the token
2064 * stream. Just as an example.
2066 * We'll just #define that away for now, but the theory here
2067 * is that we can use this to insert arbitrary token sequences
2068 * to turn the pragmas into internal front-end sequences for
2069 * when we actually start caring about them.
2071 * So eventually this will turn into some kind of extended
2072 * __attribute__() like thing, except called __pragma__(xxx).
2074 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
2076 struct token
*next
= *line
;
2078 if (match_ident(token
->next
, &once_ident
) && eof_token(token
->next
->next
)) {
2082 token
->ident
= &pragma_ident
;
2083 token
->pos
.newline
= 1;
2084 token
->pos
.whitespace
= 1;
2092 * We ignore #line for now.
2094 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
2099 static int handle_ident(struct stream
*stream
, struct token
**line
, struct token
*token
)
2104 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
2106 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
, 0));
2110 static bool expand_has_attribute(struct token
*token
, struct arg
*args
)
2112 struct token
*arg
= args
[0].expanded
;
2115 if (token_type(arg
) != TOKEN_IDENT
) {
2116 sparse_error(arg
->pos
, "identifier expected");
2120 sym
= lookup_symbol(arg
->ident
, NS_KEYWORD
);
2121 replace_with_bool(token
, sym
&& sym
->op
&& sym
->op
->attribute
);
2125 static bool expand_has_builtin(struct token
*token
, struct arg
*args
)
2127 struct token
*arg
= args
[0].expanded
;
2130 if (token_type(arg
) != TOKEN_IDENT
) {
2131 sparse_error(arg
->pos
, "identifier expected");
2135 sym
= lookup_symbol(arg
->ident
, NS_SYMBOL
);
2136 replace_with_bool(token
, sym
&& sym
->builtin
);
2140 static bool expand_has_extension(struct token
*token
, struct arg
*args
)
2142 struct token
*arg
= args
[0].expanded
;
2143 struct ident
*ident
;
2146 if (token_type(arg
) != TOKEN_IDENT
) {
2147 sparse_error(arg
->pos
, "identifier expected");
2152 if (ident
== &c_alignas_ident
)
2154 else if (ident
== &c_alignof_ident
)
2156 else if (ident
== &c_generic_selections_ident
)
2158 else if (ident
== &c_static_assert_ident
)
2161 replace_with_bool(token
, val
);
2165 static bool expand_has_feature(struct token
*token
, struct arg
*args
)
2167 struct token
*arg
= args
[0].expanded
;
2168 struct ident
*ident
;
2171 if (token_type(arg
) != TOKEN_IDENT
) {
2172 sparse_error(arg
->pos
, "identifier expected");
2177 if (standard
>= STANDARD_C11
) {
2178 if (ident
== &c_alignas_ident
)
2180 else if (ident
== &c_alignof_ident
)
2182 else if (ident
== &c_generic_selections_ident
)
2184 else if (ident
== &c_static_assert_ident
)
2188 replace_with_bool(token
, val
);
2192 static void create_arglist(struct symbol
*sym
, int count
)
2194 struct token
*token
;
2195 struct token
**next
;
2200 token
= __alloc_token(0);
2201 token_type(token
) = TOKEN_ARG_COUNT
;
2202 token
->count
.normal
= count
;
2203 sym
->arglist
= token
;
2204 next
= &token
->next
;
2207 struct token
*id
, *uses
;
2208 id
= __alloc_token(0);
2209 token_type(id
) = TOKEN_IDENT
;
2210 uses
= __alloc_token(0);
2211 token_type(uses
) = TOKEN_ARG_COUNT
;
2212 uses
->count
.normal
= 1;
2218 *next
= &eof_token_entry
;
2221 static void init_preprocessor(void)
2224 int stream
= init_stream(NULL
, "preprocessor", -1, includepath
);
2227 int (*handler
)(struct stream
*, struct token
**, struct token
*);
2229 { "define", handle_define
},
2230 { "weak_define", handle_weak_define
},
2231 { "strong_define", handle_strong_define
},
2232 { "undef", handle_undef
},
2233 { "strong_undef", handle_strong_undef
},
2234 { "warning", handle_warning
},
2235 { "error", handle_error
},
2236 { "include", handle_include
},
2237 { "include_next", handle_include_next
},
2238 { "pragma", handle_pragma
},
2239 { "line", handle_line
},
2240 { "ident", handle_ident
},
2242 // our internal preprocessor tokens
2243 { "nostdinc", handle_nostdinc
},
2244 { "add_include", handle_add_include
},
2245 { "add_isystem", handle_add_isystem
},
2246 { "add_system", handle_add_system
},
2247 { "add_dirafter", handle_add_dirafter
},
2248 { "split_include", handle_split_include
},
2249 { "argv_include", handle_argv_include
},
2251 { "ifdef", handle_ifdef
},
2252 { "ifndef", handle_ifndef
},
2253 { "else", handle_else
},
2254 { "endif", handle_endif
},
2255 { "if", handle_if
},
2256 { "elif", handle_elif
},
2260 void (*expand_simple
)(struct token
*);
2261 bool (*expand
)(struct token
*, struct arg
*args
);
2263 { "__LINE__", expand_line
},
2264 { "__FILE__", expand_file
},
2265 { "__BASE_FILE__", expand_basefile
},
2266 { "__DATE__", expand_date
},
2267 { "__TIME__", expand_time
},
2268 { "__COUNTER__", expand_counter
},
2269 { "__INCLUDE_LEVEL__", expand_include_level
},
2270 { "__has_attribute", NULL
, expand_has_attribute
},
2271 { "__has_builtin", NULL
, expand_has_builtin
},
2272 { "__has_extension", NULL
, expand_has_extension
},
2273 { "__has_feature", NULL
, expand_has_feature
},
2276 for (i
= 0; i
< ARRAY_SIZE(normal
); i
++) {
2278 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
2279 sym
->handler
= normal
[i
].handler
;
2282 for (i
= 0; i
< ARRAY_SIZE(special
); i
++) {
2284 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
2285 sym
->handler
= special
[i
].handler
;
2288 for (i
= 0; i
< ARRAY_SIZE(dynamic
); i
++) {
2290 sym
= create_symbol(stream
, dynamic
[i
].name
, SYM_NODE
, NS_MACRO
);
2291 sym
->expand_simple
= dynamic
[i
].expand_simple
;
2292 if ((sym
->expand
= dynamic
[i
].expand
) != NULL
)
2293 create_arglist(sym
, 1);
2299 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
2301 int (*handler
)(struct stream
*, struct token
**, struct token
*);
2302 struct token
*token
= start
->next
;
2304 int is_cond
= 0; // is one of {is,ifdef,ifndef,elif,else,endif}
2306 if (eof_token(token
))
2309 if (token_type(token
) == TOKEN_IDENT
) {
2310 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
2312 handler
= sym
->handler
;
2313 is_normal
= sym
->normal
;
2314 is_cond
= !sym
->normal
;
2316 handler
= handle_nondirective
;
2318 } else if (token_type(token
) == TOKEN_NUMBER
) {
2319 handler
= handle_line
;
2321 handler
= handle_nondirective
;
2325 dirty_stream(stream
);
2331 if (!is_cond
|| Wpedantic
)
2332 warning(start
->pos
, "directive in macro's argument list");
2334 if (!handler(stream
, line
, token
)) /* all set */
2338 free_preprocessor_line(token
);
2341 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
2343 struct token
*start
= *line
, *next
;
2344 struct token
**tp
= &start
->next
;
2348 if (next
->pos
.newline
)
2353 *tp
= &eof_token_entry
;
2354 handle_preprocessor_line(stream
, line
, start
);
2357 static void do_preprocess(struct token
**list
)
2361 while (!eof_token(next
= scan_next(list
))) {
2362 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
2364 if (next
->pos
.newline
&& match_op(next
, '#')) {
2365 if (!next
->pos
.noexpand
) {
2366 preprocessor_line(stream
, list
);
2367 __free_token(next
); /* Free the '#' token */
2372 switch (token_type(next
)) {
2373 case TOKEN_STREAMEND
:
2374 if (stream
->top_if
) {
2375 nesting_error(stream
);
2376 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
2377 stream
->top_if
= NULL
;
2381 stream
->constant
= CONSTANT_FILE_YES
;
2385 case TOKEN_STREAMBEGIN
:
2391 dirty_stream(stream
);
2392 if (false_nesting
) {
2398 if (token_type(next
) != TOKEN_IDENT
||
2399 expand_one_symbol(list
))
2405 void init_include_path(void)
2412 fp
= popen("/bin/uname -m", "r");
2415 if (!fgets(arch
, sizeof(arch
) - 1, fp
))
2418 if (arch
[strlen(arch
) - 1] == '\n')
2419 arch
[strlen(arch
) - 1] = '\0';
2421 fp
= popen("/bin/uname -o", "r");
2424 fgets(os
, sizeof(os
) - 1, fp
);
2427 if (strcmp(os
, "GNU/Linux\n") != 0)
2429 strcpy(os
, "linux-gnu");
2431 snprintf(path
, sizeof(path
), "/usr/include/%s-%s/", arch
, os
);
2432 add_pre_buffer("#add_system \"%s/\"\n", path
);
2435 struct token
* preprocess(struct token
*token
)
2438 init_preprocessor();
2439 do_preprocess(&token
);
2441 // Drop all expressions from preprocessing, they're not used any more.
2442 // This is not true when we have multiple files, though ;/
2443 // clear_expression_alloc();
2449 static int is_VA_ARGS_token(struct token
*token
)
2451 return (token_type(token
) == TOKEN_IDENT
) &&
2452 (token
->ident
== &__VA_ARGS___ident
);
2455 static void dump_macro(struct symbol
*sym
)
2457 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
2458 struct token
*args
[nargs
];
2459 struct token
*token
;
2461 printf("#define %s", show_ident(sym
->ident
));
2462 token
= sym
->arglist
;
2464 const char *sep
= "";
2467 for (; !eof_token(token
); token
= token
->next
) {
2468 if (token_type(token
) == TOKEN_ARG_COUNT
)
2470 if (is_VA_ARGS_token(token
))
2471 printf("%s...", sep
);
2473 printf("%s%s", sep
, show_token(token
));
2474 args
[narg
++] = token
;
2480 token
= sym
->expansion
;
2481 while (token_type(token
) != TOKEN_UNTAINT
) {
2482 struct token
*next
= token
->next
;
2483 if (token
->pos
.whitespace
)
2485 switch (token_type(token
)) {
2489 case TOKEN_STR_ARGUMENT
:
2492 case TOKEN_QUOTED_ARGUMENT
:
2493 case TOKEN_MACRO_ARGUMENT
:
2494 token
= args
[token
->argnum
];
2497 printf("%s", show_token(token
));
2504 void dump_macro_definitions(void)
2508 FOR_EACH_PTR(macros
, name
) {
2509 struct symbol
*sym
= lookup_macro(name
);
2512 } END_FOR_EACH_PTR(name
);