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;
54 #define INCLUDEPATHS 300
55 const char *includepath
[INCLUDEPATHS
+1] = {
62 static const char **quote_includepath
= includepath
;
63 static const char **angle_includepath
= includepath
+ 1;
64 static const char **isys_includepath
= includepath
+ 1;
65 static const char **sys_includepath
= includepath
+ 1;
66 static const char **dirafter_includepath
= includepath
+ 3;
68 #define dirty_stream(stream) \
70 if (!stream->dirty) { \
72 if (!stream->ifndef) \
73 stream->protect = NULL; \
77 #define end_group(stream) \
79 if (stream->ifndef == stream->top_if) { \
80 stream->ifndef = NULL; \
82 stream->protect = NULL; \
83 else if (stream->protect) \
88 #define nesting_error(stream) \
91 stream->ifndef = NULL; \
92 stream->protect = NULL; \
95 static struct token
*alloc_token(struct position
*pos
)
97 struct token
*token
= __alloc_token(0);
99 token
->pos
.stream
= pos
->stream
;
100 token
->pos
.line
= pos
->line
;
101 token
->pos
.pos
= pos
->pos
;
102 token
->pos
.whitespace
= 1;
106 /* Expand symbol 'sym' at '*list' */
107 static int expand(struct token
**, struct symbol
*);
109 static void replace_with_string(struct token
*token
, const char *str
)
111 int size
= strlen(str
) + 1;
112 struct string
*s
= __alloc_string(size
);
115 memcpy(s
->data
, str
, size
);
116 token_type(token
) = TOKEN_STRING
;
120 static void replace_with_integer(struct token
*token
, unsigned int val
)
122 char *buf
= __alloc_bytes(11);
123 sprintf(buf
, "%u", val
);
124 token_type(token
) = TOKEN_NUMBER
;
128 static struct symbol
*lookup_macro(struct ident
*ident
)
130 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_UNDEF
);
131 if (sym
&& sym
->namespace != NS_MACRO
)
136 static int token_defined(struct token
*token
)
138 if (token_type(token
) == TOKEN_IDENT
) {
139 struct symbol
*sym
= lookup_macro(token
->ident
);
141 sym
->used_in
= file_scope
;
147 sparse_error(token
->pos
, "expected preprocessor identifier");
151 static void replace_with_bool(struct token
*token
, bool val
)
153 static const char *string
[] = { "0", "1" };
155 token_type(token
) = TOKEN_NUMBER
;
156 token
->number
= string
[val
];
159 static void replace_with_defined(struct token
*token
)
161 replace_with_bool(token
, token_defined(token
));
164 static void replace_with_has_builtin(struct token
*token
)
166 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
);
167 replace_with_bool(token
, sym
&& sym
->builtin
);
170 static void replace_with_has_attribute(struct token
*token
)
172 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_KEYWORD
);
173 replace_with_bool(token
, sym
&& sym
->op
&& sym
->op
->attribute
);
176 static void expand_line(struct token
*token
)
178 replace_with_integer(token
, token
->pos
.line
);
181 static void expand_file(struct token
*token
)
183 replace_with_string(token
, stream_name(token
->pos
.stream
));
186 static void expand_basefile(struct token
*token
)
188 replace_with_string(token
, base_filename
);
192 static void expand_date(struct token
*token
)
194 static char buffer
[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
198 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
199 replace_with_string(token
, buffer
);
202 static void expand_time(struct token
*token
)
204 static char buffer
[9]; /* __TIME__: 2 + ':' + 2 + ':' + 2 + '\0' */
208 strftime(buffer
, 9, "%T", localtime(&t
));
209 replace_with_string(token
, buffer
);
212 static void expand_counter(struct token
*token
)
214 replace_with_integer(token
, counter_macro
++);
217 static void expand_include_level(struct token
*token
)
219 replace_with_integer(token
, include_level
- 1);
222 static int expand_one_symbol(struct token
**list
)
224 struct token
*token
= *list
;
227 if (token
->pos
.noexpand
)
230 sym
= lookup_macro(token
->ident
);
233 store_macro_pos(token
);
235 sym
->expander(token
);
238 sym
->used_in
= file_scope
;
239 return expand(list
, sym
);
243 static inline struct token
*scan_next(struct token
**where
)
245 struct token
*token
= *where
;
246 if (token_type(token
) != TOKEN_UNTAINT
)
249 token
->ident
->tainted
= 0;
251 } while (token_type(token
) == TOKEN_UNTAINT
);
256 static void expand_list(struct token
**list
)
259 while (!eof_token(next
= scan_next(list
))) {
260 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
265 static void preprocessor_line(struct stream
*stream
, struct token
**line
);
267 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
, int count
)
269 struct stream
*stream
= input_streams
+ prev
->pos
.stream
;
270 struct token
**p
= &prev
->next
;
274 while (!eof_token(next
= scan_next(p
))) {
275 if (next
->pos
.newline
&& match_op(next
, '#')) {
276 if (!next
->pos
.noexpand
) {
277 sparse_error(next
->pos
,
278 "directive in argument list");
279 preprocessor_line(stream
, p
);
280 __free_token(next
); /* Free the '#' token */
284 switch (token_type(next
)) {
285 case TOKEN_STREAMEND
:
286 case TOKEN_STREAMBEGIN
:
287 *p
= &eof_token_entry
;
290 case TOKEN_WIDE_STRING
:
292 next
->string
->immutable
= 1;
300 if (match_op(next
, '(')) {
302 } else if (match_op(next
, ')')) {
305 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
308 next
->pos
.stream
= pos
->stream
;
309 next
->pos
.line
= pos
->line
;
310 next
->pos
.pos
= pos
->pos
;
313 *p
= &eof_token_entry
;
318 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
323 struct token
*expanded
;
330 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
332 int wanted
= arglist
->count
.normal
;
333 struct token
*next
= NULL
;
336 arglist
= arglist
->next
; /* skip counter */
339 next
= collect_arg(start
, 0, &what
->pos
, 0);
342 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
347 for (count
= 0; count
< wanted
; count
++) {
348 struct argcount
*p
= &arglist
->next
->count
;
349 next
= collect_arg(start
, p
->vararg
, &what
->pos
, p
->normal
);
352 if (p
->vararg
&& wanted
== 1 && eof_token(start
->next
))
354 arglist
= arglist
->next
->next
;
355 args
[count
].arg
= start
->next
;
356 args
[count
].n_normal
= p
->normal
;
357 args
[count
].n_quoted
= p
->quoted
;
358 args
[count
].n_str
= p
->str
;
359 if (match_op(next
, ')')) {
365 if (count
== wanted
&& !match_op(next
, ')'))
367 if (count
== wanted
- 1) {
368 struct argcount
*p
= &arglist
->next
->count
;
371 args
[count
].arg
= NULL
;
372 args
[count
].n_normal
= p
->normal
;
373 args
[count
].n_quoted
= p
->quoted
;
374 args
[count
].n_str
= p
->str
;
376 if (count
< wanted
- 1)
379 what
->next
= next
->next
;
383 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
384 show_token(what
), wanted
, count
);
387 while (match_op(next
, ',')) {
388 next
= collect_arg(next
, 0, &what
->pos
, 0);
393 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
394 show_token(what
), count
, wanted
);
397 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
400 what
->next
= next
->next
;
404 static struct token
*dup_list(struct token
*list
)
406 struct token
*res
= NULL
;
407 struct token
**p
= &res
;
409 while (!eof_token(list
)) {
410 struct token
*newtok
= __alloc_token(0);
419 static const char *show_token_sequence(struct token
*token
, int quote
)
421 static char buffer
[MAX_STRING
];
425 if (!token
&& !quote
)
427 while (!eof_token(token
)) {
428 const char *val
= quote
? quote_token(token
) : show_token(token
);
429 int len
= strlen(val
);
431 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
432 sparse_error(token
->pos
, "too long token expansion");
438 memcpy(ptr
, val
, len
);
441 whitespace
= token
->pos
.whitespace
;
447 static struct token
*stringify(struct token
*arg
)
449 const char *s
= show_token_sequence(arg
, 1);
450 int size
= strlen(s
)+1;
451 struct token
*token
= __alloc_token(0);
452 struct string
*string
= __alloc_string(size
);
454 memcpy(string
->data
, s
, size
);
455 string
->length
= size
;
456 token
->pos
= arg
->pos
;
457 token_type(token
) = TOKEN_STRING
;
458 token
->string
= string
;
459 token
->next
= &eof_token_entry
;
463 static void expand_arguments(int count
, struct arg
*args
)
466 for (i
= 0; i
< count
; i
++) {
467 struct token
*arg
= args
[i
].arg
;
469 arg
= &eof_token_entry
;
471 args
[i
].str
= stringify(arg
);
472 if (args
[i
].n_normal
) {
473 if (!args
[i
].n_quoted
) {
474 args
[i
].expanded
= arg
;
476 } else if (eof_token(arg
)) {
477 args
[i
].expanded
= arg
;
479 args
[i
].expanded
= dup_list(arg
);
481 expand_list(&args
[i
].expanded
);
487 * Possibly valid combinations:
488 * - ident + ident -> ident
489 * - ident + number -> ident unless number contains '.', '+' or '-'.
490 * - 'L' + char constant -> wide char constant
491 * - 'L' + string literal -> wide string literal
492 * - number + number -> number
493 * - number + ident -> number
494 * - number + '.' -> number
495 * - number + '+' or '-' -> number, if number used to end on [eEpP].
496 * - '.' + number -> number, if number used to start with a digit.
497 * - special + special -> either special or an error.
499 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
502 enum token_type t1
= token_type(left
), t2
= token_type(right
);
504 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
507 if (t1
== TOKEN_IDENT
&& left
->ident
== &L_ident
) {
508 if (t2
>= TOKEN_CHAR
&& t2
< TOKEN_WIDE_CHAR
)
509 return t2
+ TOKEN_WIDE_CHAR
- TOKEN_CHAR
;
510 if (t2
== TOKEN_STRING
)
511 return TOKEN_WIDE_STRING
;
514 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
517 strcpy(p
, show_token(left
));
518 strcat(p
, show_token(right
));
524 if (t1
== TOKEN_IDENT
) {
525 if (t2
== TOKEN_SPECIAL
)
527 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
532 if (t1
== TOKEN_NUMBER
) {
533 if (t2
== TOKEN_SPECIAL
) {
534 switch (right
->special
) {
538 if (strchr("eEpP", p
[len
- 2]))
547 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
550 return TOKEN_SPECIAL
;
553 static int merge(struct token
*left
, struct token
*right
)
555 static char buffer
[512];
556 enum token_type res
= combine(left
, right
, buffer
);
561 left
->ident
= built_in_ident(buffer
);
562 left
->pos
.noexpand
= 0;
566 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
567 left
->number
= xstrdup(buffer
);
571 if (buffer
[2] && buffer
[3])
573 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
574 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
581 case TOKEN_WIDE_CHAR
:
582 case TOKEN_WIDE_STRING
:
583 token_type(left
) = res
;
584 left
->pos
.noexpand
= 0;
585 left
->string
= right
->string
;
588 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
589 token_type(left
) = res
;
590 left
->pos
.noexpand
= 0;
591 memcpy(left
->embedded
, right
->embedded
, 4);
597 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
601 static struct token
*dup_token(struct token
*token
, struct position
*streampos
)
603 struct token
*alloc
= alloc_token(streampos
);
604 token_type(alloc
) = token_type(token
);
605 alloc
->pos
.newline
= token
->pos
.newline
;
606 alloc
->pos
.whitespace
= token
->pos
.whitespace
;
607 alloc
->number
= token
->number
;
608 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
612 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
614 int need_copy
= --*count
;
615 while (!eof_token(list
)) {
618 token
= dup_token(list
, &list
->pos
);
621 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
622 token
->pos
.noexpand
= 1;
624 where
= &token
->next
;
627 *where
= &eof_token_entry
;
631 static int handle_kludge(struct token
**p
, struct arg
*args
)
633 struct token
*t
= (*p
)->next
->next
;
635 struct arg
*v
= &args
[t
->argnum
];
636 if (token_type(t
->next
) != TOKEN_CONCAT
) {
638 /* ignore the first ## */
642 /* skip the entire thing */
646 if (v
->arg
&& !eof_token(v
->arg
))
647 return 0; /* no magic */
652 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
654 struct position
*base_pos
= &(*list
)->pos
;
656 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
658 for (; !eof_token(body
); body
= body
->next
) {
659 struct token
*added
, *arg
;
663 switch (token_type(body
)) {
664 case TOKEN_GNU_KLUDGE
:
666 * GNU kludge: if we had <comma>##<vararg>, behaviour
667 * depends on whether we had enough arguments to have
668 * a vararg. If we did, ## is just ignored. Otherwise
669 * both , and ## are ignored. Worse, there can be
670 * an arbitrary number of ##<arg> in between; if all of
671 * those are empty, we act as if they hadn't been there,
672 * otherwise we act as if the kludge didn't exist.
675 if (handle_kludge(&body
, args
)) {
682 added
= dup_token(t
, base_pos
);
683 token_type(added
) = TOKEN_SPECIAL
;
687 case TOKEN_STR_ARGUMENT
:
688 arg
= args
[body
->argnum
].str
;
689 count
= &args
[body
->argnum
].n_str
;
692 case TOKEN_QUOTED_ARGUMENT
:
693 arg
= args
[body
->argnum
].arg
;
694 count
= &args
[body
->argnum
].n_quoted
;
695 if (!arg
|| eof_token(arg
)) {
704 case TOKEN_MACRO_ARGUMENT
:
705 arg
= args
[body
->argnum
].expanded
;
706 count
= &args
[body
->argnum
].n_normal
;
707 if (eof_token(arg
)) {
712 tail
= copy(&added
, arg
, count
);
713 added
->pos
.newline
= body
->pos
.newline
;
714 added
->pos
.whitespace
= body
->pos
.whitespace
;
718 if (state
== Placeholder
)
725 added
= dup_token(body
, base_pos
);
726 if (added
->ident
->tainted
)
727 added
->pos
.noexpand
= 1;
732 added
= dup_token(body
, base_pos
);
738 * if we got to doing real concatenation, we already have
739 * added something into the list, so containing_token() is OK.
741 if (state
== Concat
&& merge(containing_token(list
), added
)) {
743 if (tail
!= &added
->next
)
751 *list
= &eof_token_entry
;
755 static int expand(struct token
**list
, struct symbol
*sym
)
758 struct token
*token
= *list
;
759 struct ident
*expanding
= token
->ident
;
761 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
762 struct arg args
[nargs
];
764 if (expanding
->tainted
) {
765 token
->pos
.noexpand
= 1;
770 if (!match_op(scan_next(&token
->next
), '('))
772 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
774 expand_arguments(nargs
, args
);
777 expanding
->tainted
= 1;
780 tail
= substitute(list
, sym
->expansion
, args
);
782 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
783 * We still can lose the newline flag if the sucker expands to nothing,
784 * but the price of dealing with that is probably too high (we'd need
785 * to collect the flags during scan_next())
787 (*list
)->pos
.newline
= token
->pos
.newline
;
788 (*list
)->pos
.whitespace
= token
->pos
.whitespace
;
794 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
796 static char buffer
[256];
799 while (!eof_token(token
) && !match_op(token
, endop
)) {
801 const char *val
= token
->string
->data
;
802 if (token_type(token
) != TOKEN_STRING
)
803 val
= show_token(token
);
805 memcpy(ptr
, val
, len
);
810 if (endop
&& !match_op(token
, endop
))
811 sparse_error(start
->pos
, "expected '>' at end of filename");
815 static int already_tokenized(const char *path
)
819 for (stream
= *hash_stream(path
); stream
>= 0 ; stream
= next
) {
820 struct stream
*s
= input_streams
+ stream
;
822 next
= s
->next_stream
;
824 if (strcmp(path
, s
->name
))
828 if (s
->constant
!= CONSTANT_FILE_YES
)
830 if (strcmp(path
, s
->name
))
832 if (s
->protect
&& !lookup_macro(s
->protect
))
839 /* Handle include of header files.
840 * The relevant options are made compatible with gcc. The only options that
841 * are not supported is -withprefix and friends.
843 * Three set of include paths are known:
844 * quote_includepath: Path to search when using #include "file.h"
845 * angle_includepath: Paths to search when using #include <file.h>
846 * isys_includepath: Paths specified with -isystem, come before the
847 * built-in system include paths. Gcc would suppress
848 * warnings from system headers. Here we separate
849 * them from the angle_ ones to keep search ordering.
851 * sys_includepath: Built-in include paths.
852 * dirafter_includepath Paths added with -dirafter.
854 * The above is implemented as one array with pointers
856 * quote_includepath ---> | |
860 * angle_includepath ---> | |
862 * isys_includepath ---> | |
864 * sys_includepath ---> | |
866 * dirafter_includepath -> | |
869 * -I dir insert dir just before isys_includepath and move the rest
870 * -I- makes all dirs specified with -I before to quote dirs only and
871 * angle_includepath is set equal to isys_includepath.
872 * -nostdinc removes all sys dirs by storing NULL in entry pointed
873 * to by * sys_includepath. Note that this will reset all dirs built-in
874 * and added before -nostdinc by -isystem and -idirafter.
875 * -isystem dir adds dir where isys_includepath points adding this dir as
877 * -idirafter dir adds dir to the end of the list
880 static void set_stream_include_path(struct stream
*stream
)
882 const char *path
= stream
->path
;
884 const char *p
= strrchr(stream
->name
, '/');
887 int len
= p
- stream
->name
+ 1;
888 char *m
= malloc(len
+1);
889 /* This includes the final "/" */
890 memcpy(m
, stream
->name
, len
);
896 includepath
[0] = path
;
900 #define PATH_MAX 4096 // for Hurd where it's not defined
903 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
906 int plen
= strlen(path
);
907 static char fullname
[PATH_MAX
];
909 memcpy(fullname
, path
, plen
);
910 if (plen
&& path
[plen
-1] != '/') {
911 fullname
[plen
] = '/';
914 memcpy(fullname
+plen
, filename
, flen
);
915 if (already_tokenized(fullname
))
917 fd
= open(fullname
, O_RDONLY
);
919 char *streamname
= xmemdup(fullname
, plen
+ flen
);
920 *where
= tokenize(streamname
, fd
, *where
, next_path
);
927 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
931 while ((path
= *pptr
++) != NULL
) {
932 if (!try_include(path
, filename
, flen
, list
, pptr
))
939 static int free_preprocessor_line(struct token
*token
)
941 while (token_type(token
) != TOKEN_EOF
) {
942 struct token
*free
= token
;
949 const char *find_include(const char *skip
, const char *look_for
)
952 struct dirent
*entry
;
956 static char buf
[PATH_MAX
+ 1];
962 if (!getcwd(cwd
, sizeof(cwd
)))
965 while ((entry
= readdir(dp
))) {
966 lstat(entry
->d_name
, &statbuf
);
968 if (strcmp(entry
->d_name
, look_for
) == 0) {
969 snprintf(buf
, sizeof(buf
), "%s/%s", cwd
, entry
->d_name
);
974 if (S_ISDIR(statbuf
.st_mode
)) {
975 /* Found a directory, but ignore . and .. */
976 if (strcmp(".", entry
->d_name
) == 0 ||
977 strcmp("..", entry
->d_name
) == 0 ||
978 strcmp(skip
, entry
->d_name
) == 0)
981 chdir(entry
->d_name
);
982 ret
= find_include("", look_for
);
996 const char *search_dir(const char *stop
, const char *look_for
)
1003 if (!getcwd(cwd
, sizeof(cwd
)))
1008 ret
= find_include(cnt
++ ? cwd
+ len
+ 1 : "", look_for
);
1012 if (strcmp(cwd
, stop
) == 0 ||
1013 strcmp(cwd
, "/usr/include") == 0 ||
1014 strcmp(cwd
, "/usr/local/include") == 0 ||
1015 strlen(cwd
) <= 10 || /* heck... don't search /usr/lib/ */
1016 strcmp(cwd
, "/") == 0)
1019 while (--len
>= 0) {
1020 if (cwd
[len
] == '/') {
1031 static void use_best_guess_header_file(struct token
*token
, const char *filename
, struct token
**list
)
1034 char dir_part
[PATH_MAX
];
1035 const char *file_part
;
1036 const char *include_name
;
1040 /* Avoid guessing includes recursively. */
1044 if (!filename
|| filename
[0] == '\0')
1047 file_part
= filename
;
1048 while ((filename
= strchr(filename
, '/'))) {
1051 file_part
= filename
;
1054 snprintf(dir_part
, sizeof(dir_part
), "%s", stream_name(token
->pos
.stream
));
1055 len
= strlen(dir_part
);
1056 while (--len
>= 0) {
1057 if (dir_part
[len
] == '/') {
1058 dir_part
[len
] = '\0';
1063 sprintf(dir_part
, ".");
1065 if (!getcwd(cwd
, sizeof(cwd
)))
1069 include_name
= search_dir(cwd
, file_part
);
1073 sparse_error(token
->pos
, "using '%s'", include_name
);
1075 try_include("", include_name
, strlen(include_name
), list
, includepath
);
1078 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, int how
)
1080 const char *filename
;
1088 if (!match_op(next
, '<')) {
1089 expand_list(&token
->next
);
1092 if (match_op(token
->next
, '<')) {
1099 filename
= token_name_sequence(token
, expect
, token
);
1100 flen
= strlen(filename
) + 1;
1102 /* Absolute path? */
1103 if (filename
[0] == '/') {
1104 if (try_include("", filename
, flen
, list
, includepath
))
1111 path
= stream
->next_path
;
1114 includepath
[0] = "";
1118 /* Dir of input file is first dir to search for quoted includes */
1119 set_stream_include_path(stream
);
1120 path
= expect
? angle_includepath
: quote_includepath
;
1123 /* Check the standard include paths.. */
1124 if (do_include_path(path
, list
, token
, filename
, flen
))
1127 sparse_error(token
->pos
, "unable to open '%s'", filename
);
1128 use_best_guess_header_file(token
, filename
, list
);
1132 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
1134 return handle_include_path(stream
, list
, token
, 0);
1137 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
1139 return handle_include_path(stream
, list
, token
, 1);
1142 static int handle_argv_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
1144 return handle_include_path(stream
, list
, token
, 2);
1147 static int token_different(struct token
*t1
, struct token
*t2
)
1151 if (token_type(t1
) != token_type(t2
))
1154 switch (token_type(t1
)) {
1156 different
= t1
->ident
!= t2
->ident
;
1158 case TOKEN_ARG_COUNT
:
1161 case TOKEN_GNU_KLUDGE
:
1165 different
= strcmp(t1
->number
, t2
->number
);
1168 different
= t1
->special
!= t2
->special
;
1170 case TOKEN_MACRO_ARGUMENT
:
1171 case TOKEN_QUOTED_ARGUMENT
:
1172 case TOKEN_STR_ARGUMENT
:
1173 different
= t1
->argnum
!= t2
->argnum
;
1175 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
1176 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
1177 different
= memcmp(t1
->embedded
, t2
->embedded
, 4);
1180 case TOKEN_WIDE_CHAR
:
1182 case TOKEN_WIDE_STRING
: {
1183 struct string
*s1
, *s2
;
1188 if (s1
->length
!= s2
->length
)
1190 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
1200 static int token_list_different(struct token
*list1
, struct token
*list2
)
1205 if (!list1
|| !list2
)
1207 if (token_different(list1
, list2
))
1209 list1
= list1
->next
;
1210 list2
= list2
->next
;
1214 static inline void set_arg_count(struct token
*token
)
1216 token_type(token
) = TOKEN_ARG_COUNT
;
1217 token
->count
.normal
= token
->count
.quoted
=
1218 token
->count
.str
= token
->count
.vararg
= 0;
1221 static struct token
*parse_arguments(struct token
*list
)
1223 struct token
*arg
= list
->next
, *next
= list
;
1224 struct argcount
*count
= &list
->count
;
1226 set_arg_count(list
);
1228 if (match_op(arg
, ')')) {
1230 list
->next
= &eof_token_entry
;
1234 while (token_type(arg
) == TOKEN_IDENT
) {
1235 if (arg
->ident
== &__VA_ARGS___ident
)
1237 if (!++count
->normal
)
1241 if (match_op(next
, ',')) {
1242 set_arg_count(next
);
1247 if (match_op(next
, ')')) {
1248 set_arg_count(next
);
1250 arg
->next
->next
= &eof_token_entry
;
1254 /* normal cases are finished here */
1256 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
1257 if (match_op(next
->next
, ')')) {
1258 set_arg_count(next
);
1259 next
->count
.vararg
= 1;
1261 arg
->next
->next
= &eof_token_entry
;
1269 if (eof_token(next
)) {
1277 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
1279 token_type(arg
) = TOKEN_IDENT
;
1280 arg
->ident
= &__VA_ARGS___ident
;
1281 if (!match_op(next
, ')'))
1283 if (!++count
->normal
)
1285 set_arg_count(next
);
1286 next
->count
.vararg
= 1;
1288 arg
->next
->next
= &eof_token_entry
;
1292 if (eof_token(arg
)) {
1296 if (match_op(arg
, ','))
1303 sparse_error(arg
->pos
, "parameter name missing");
1306 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
1310 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1313 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1316 sparse_error(arg
->pos
, "too many arguments in macro definition");
1320 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1322 struct ident
*ident
= token
->ident
;
1325 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1328 arglist
= arglist
->next
;
1330 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1331 if (arglist
->ident
== ident
) {
1332 struct argcount
*count
= &arglist
->next
->count
;
1336 token_type(token
) = type
;
1338 case TOKEN_MACRO_ARGUMENT
:
1339 n
= ++count
->normal
;
1341 case TOKEN_QUOTED_ARGUMENT
:
1342 n
= ++count
->quoted
;
1348 return count
->vararg
? 2 : 1;
1350 * XXX - need saner handling of that
1351 * (>= 1024 instances of argument)
1353 token_type(token
) = TOKEN_ERROR
;
1360 static struct token
*handle_hash(struct token
**p
, struct token
*arglist
)
1362 struct token
*token
= *p
;
1364 struct token
*next
= token
->next
;
1365 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1367 next
->pos
.whitespace
= token
->pos
.whitespace
;
1368 __free_token(token
);
1371 token
->pos
.noexpand
= 1;
1376 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1380 /* token->next is ## */
1381 static struct token
*handle_hashhash(struct token
*token
, struct token
*arglist
)
1383 struct token
*last
= token
;
1384 struct token
*concat
;
1385 int state
= match_op(token
, ',');
1387 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1393 /* eat duplicate ## */
1394 concat
= token
->next
;
1395 while (match_op(t
= concat
->next
, SPECIAL_HASHHASH
)) {
1397 __free_token(concat
);
1400 token_type(concat
) = TOKEN_CONCAT
;
1405 if (match_op(t
, '#')) {
1406 t
= handle_hash(&concat
->next
, arglist
);
1411 is_arg
= try_arg(t
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1413 if (state
== 1 && is_arg
) {
1417 state
= match_op(t
, ',');
1421 if (!match_op(token
->next
, SPECIAL_HASHHASH
))
1424 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1426 token_type(last
) = TOKEN_GNU_KLUDGE
;
1430 sparse_error(concat
->pos
, "'##' cannot appear at the ends of macro expansion");
1434 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1436 struct token
*token
= expansion
;
1439 if (match_op(token
, SPECIAL_HASHHASH
))
1442 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1443 if (match_op(token
, '#')) {
1444 token
= handle_hash(p
, arglist
);
1448 if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1449 token
= handle_hashhash(token
, arglist
);
1453 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1455 switch (token_type(token
)) {
1460 case TOKEN_WIDE_STRING
:
1461 token
->string
->immutable
= 1;
1465 token
= alloc_token(&expansion
->pos
);
1466 token_type(token
) = TOKEN_UNTAINT
;
1467 token
->ident
= name
;
1473 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1476 sparse_error(token
->pos
, "too many instances of argument in body");
1480 static int do_define(struct position pos
, struct token
*token
, struct ident
*name
,
1481 struct token
*arglist
, struct token
*expansion
, int attr
)
1486 expansion
= parse_expansion(expansion
, arglist
, name
);
1490 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1494 if (attr
< sym
->attr
)
1497 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1499 if (token_list_different(sym
->expansion
, expansion
) ||
1500 token_list_different(sym
->arglist
, arglist
)) {
1502 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1503 || sym
->used_in
== file_scope
) {
1504 warning(pos
, "preprocessor token %.*s redefined",
1505 name
->len
, name
->name
);
1506 info(sym
->pos
, "this was the original definition");
1512 if (!sym
|| sym
->scope
!= file_scope
) {
1513 sym
= alloc_symbol(pos
, SYM_NODE
);
1514 bind_symbol(sym
, name
, NS_MACRO
);
1515 add_ident(¯os
, name
);
1520 sym
->expansion
= expansion
;
1521 sym
->arglist
= arglist
;
1522 if (token
) /* Free the "define" token, but not the rest of the line */
1523 __free_token(token
);
1526 sym
->namespace = NS_MACRO
;
1527 sym
->used_in
= NULL
;
1534 // predefine a macro with a printf-formatted value
1535 // @name: the name of the macro
1536 // @weak: 0/1 for a normal or a weak define
1537 // @fmt: the printf format followed by it's arguments.
1539 // The type of the value is automatically infered:
1540 // TOKEN_NUMBER if it starts by a digit, TOKEN_IDENT otherwise.
1541 // If @fmt is null or empty, the macro is defined with an empty definition.
1542 void predefine(const char *name
, int weak
, const char *fmt
, ...)
1544 struct ident
*ident
= built_in_ident(name
);
1545 struct token
*value
= &eof_token_entry
;
1546 int attr
= weak
? SYM_ATTR_WEAK
: SYM_ATTR_NORMAL
;
1548 if (fmt
&& fmt
[0]) {
1549 static char buf
[256];
1553 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1556 value
= __alloc_token(0);
1557 if (isdigit(buf
[0])) {
1558 token_type(value
) = TOKEN_NUMBER
;
1559 value
->number
= xstrdup(buf
);
1561 token_type(value
) = TOKEN_IDENT
;
1562 value
->ident
= built_in_ident(buf
);
1564 value
->pos
.whitespace
= 1;
1565 value
->next
= &eof_token_entry
;
1568 do_define(value
->pos
, NULL
, ident
, NULL
, value
, attr
);
1572 // like predefine() but only if one of the non-standard dialect is chosen
1573 void predefine_nostd(const char *name
)
1575 if ((standard
& STANDARD_GNU
) || (standard
== STANDARD_NONE
))
1576 predefine(name
, 1, "1");
1579 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1581 struct token
*arglist
, *expansion
;
1582 struct token
*left
= token
->next
;
1585 if (token_type(left
) != TOKEN_IDENT
) {
1586 sparse_error(token
->pos
, "expected identifier to 'define'");
1593 expansion
= left
->next
;
1594 if (!expansion
->pos
.whitespace
) {
1595 if (match_op(expansion
, '(')) {
1596 arglist
= expansion
;
1597 expansion
= parse_arguments(expansion
);
1600 } else if (!eof_token(expansion
)) {
1601 warning(expansion
->pos
,
1602 "no whitespace before object-like macro body");
1606 return do_define(left
->pos
, token
, name
, arglist
, expansion
, attr
);
1609 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1611 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1614 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1616 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1619 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1621 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1624 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1626 struct token
*left
= token
->next
;
1629 if (token_type(left
) != TOKEN_IDENT
) {
1630 sparse_error(token
->pos
, "expected identifier to 'undef'");
1634 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1636 if (attr
< sym
->attr
)
1638 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1640 } else if (attr
<= SYM_ATTR_NORMAL
)
1643 if (!sym
|| sym
->scope
!= file_scope
) {
1644 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1645 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1648 sym
->namespace = NS_UNDEF
;
1649 sym
->used_in
= NULL
;
1655 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1657 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1660 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1662 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1665 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int cond
)
1667 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1668 free_preprocessor_line(token
->next
);
1669 token
->next
= stream
->top_if
;
1670 stream
->top_if
= token
;
1671 if (false_nesting
|| cond
!= 1)
1676 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1678 struct token
*next
= token
->next
;
1680 if (token_type(next
) == TOKEN_IDENT
) {
1681 arg
= token_defined(next
);
1683 dirty_stream(stream
);
1685 sparse_error(token
->pos
, "expected preprocessor identifier");
1688 return preprocessor_if(stream
, token
, arg
);
1691 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1693 struct token
*next
= token
->next
;
1695 if (token_type(next
) == TOKEN_IDENT
) {
1696 if (!stream
->dirty
&& !stream
->ifndef
) {
1697 if (!stream
->protect
) {
1698 stream
->ifndef
= token
;
1699 stream
->protect
= next
->ident
;
1700 } else if (stream
->protect
== next
->ident
) {
1701 stream
->ifndef
= token
;
1705 arg
= !token_defined(next
);
1707 dirty_stream(stream
);
1709 sparse_error(token
->pos
, "expected preprocessor identifier");
1713 return preprocessor_if(stream
, token
, arg
);
1716 static const char *show_token_sequence(struct token
*token
, int quote
);
1719 * Expression handling for #if and #elif; it differs from normal expansion
1720 * due to special treatment of "defined".
1722 static int expression_value(struct token
**where
)
1724 struct expression
*expr
;
1726 struct token
**list
= where
, **beginning
= NULL
;
1730 while (!eof_token(p
= scan_next(list
))) {
1733 if (token_type(p
) != TOKEN_IDENT
)
1735 if (p
->ident
== &defined_ident
) {
1739 } else if (p
->ident
== &__has_builtin_ident
) {
1743 } else if (p
->ident
== &__has_attribute_ident
) {
1748 if (!expand_one_symbol(list
))
1750 if (token_type(p
) != TOKEN_IDENT
)
1752 token_type(p
) = TOKEN_ZERO_IDENT
;
1755 if (match_op(p
, '(')) {
1759 replace_with_defined(p
);
1764 if (token_type(p
) == TOKEN_IDENT
)
1768 replace_with_defined(p
);
1773 if (!match_op(p
, ')'))
1774 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1778 // __has_builtin(x) or __has_attribute(x)
1780 if (match_op(p
, '(')) {
1783 sparse_error(p
->pos
, "missing '(' after \"__has_%s\"",
1784 state
== 4 ? "builtin" : "attribute");
1790 if (token_type(p
) != TOKEN_IDENT
) {
1791 sparse_error(p
->pos
, "identifier expected");
1795 if (!match_op(p
->next
, ')'))
1796 sparse_error(p
->pos
, "missing ')' after \"__has_%s\"",
1797 state
== 5 ? "builtin" : "attribute");
1799 replace_with_has_builtin(p
);
1801 replace_with_has_attribute(p
);
1813 p
= constant_expression(*where
, &expr
);
1815 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
, 0));
1816 value
= get_expression_value(expr
);
1820 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1824 value
= expression_value(&token
->next
);
1826 dirty_stream(stream
);
1827 return preprocessor_if(stream
, token
, value
);
1830 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1832 struct token
*top_if
= stream
->top_if
;
1836 nesting_error(stream
);
1837 sparse_error(token
->pos
, "unmatched #elif within stream");
1841 if (token_type(top_if
) == TOKEN_ELSE
) {
1842 nesting_error(stream
);
1843 sparse_error(token
->pos
, "#elif after #else");
1849 dirty_stream(stream
);
1850 if (token_type(top_if
) != TOKEN_IF
)
1852 if (false_nesting
) {
1854 if (!expression_value(&token
->next
))
1858 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1863 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1865 struct token
*top_if
= stream
->top_if
;
1869 nesting_error(stream
);
1870 sparse_error(token
->pos
, "unmatched #else within stream");
1874 if (token_type(top_if
) == TOKEN_ELSE
) {
1875 nesting_error(stream
);
1876 sparse_error(token
->pos
, "#else after #else");
1878 if (false_nesting
) {
1879 if (token_type(top_if
) == TOKEN_IF
)
1884 token_type(top_if
) = TOKEN_ELSE
;
1888 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1890 struct token
*top_if
= stream
->top_if
;
1893 nesting_error(stream
);
1894 sparse_error(token
->pos
, "unmatched #endif in stream");
1899 stream
->top_if
= top_if
->next
;
1900 __free_token(top_if
);
1904 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1906 warning(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1910 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1912 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1916 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1919 * Do we have any non-system includes?
1920 * Clear them out if so..
1922 *sys_includepath
= NULL
;
1926 static inline void update_inc_ptrs(const char ***where
)
1929 if (*where
<= dirafter_includepath
) {
1930 dirafter_includepath
++;
1931 /* If this was the entry that we prepend, don't
1932 * rise the lower entries, even if they are at
1933 * the same level. */
1934 if (where
== &dirafter_includepath
)
1937 if (*where
<= sys_includepath
) {
1939 if (where
== &sys_includepath
)
1942 if (*where
<= isys_includepath
) {
1944 if (where
== &isys_includepath
)
1948 /* angle_includepath is actually never updated, since we
1949 * don't suppport -iquote rught now. May change some day. */
1950 if (*where
<= angle_includepath
) {
1951 angle_includepath
++;
1952 if (where
== &angle_includepath
)
1957 /* Add a path before 'where' and update the pointers associated with the
1958 * includepath array */
1959 static void add_path_entry(struct token
*token
, const char *path
,
1960 const char ***where
)
1965 /* Need one free entry.. */
1966 if (includepath
[INCLUDEPATHS
-2])
1967 error_die(token
->pos
, "too many include path entries");
1969 /* check that this is not a duplicate */
1972 if (strcmp(*dst
, path
) == 0)
1979 update_inc_ptrs(where
);
1982 * Move them all up starting at dst,
1983 * insert the new entry..
1986 const char *tmp
= *dst
;
1993 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1996 token
= token
->next
;
1997 if (eof_token(token
))
1999 if (token_type(token
) != TOKEN_STRING
) {
2000 warning(token
->pos
, "expected path string");
2003 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
2007 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
2010 token
= token
->next
;
2011 if (eof_token(token
))
2013 if (token_type(token
) != TOKEN_STRING
) {
2014 sparse_error(token
->pos
, "expected path string");
2017 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
2021 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
2024 token
= token
->next
;
2025 if (eof_token(token
))
2027 if (token_type(token
) != TOKEN_STRING
) {
2028 sparse_error(token
->pos
, "expected path string");
2031 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
2035 /* Add to end on includepath list - no pointer updates */
2036 static void add_dirafter_entry(struct token
*token
, const char *path
)
2038 const char **dst
= includepath
;
2040 /* Need one free entry.. */
2041 if (includepath
[INCLUDEPATHS
-2])
2042 error_die(token
->pos
, "too many include path entries");
2044 /* Add to the end */
2052 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
2055 token
= token
->next
;
2056 if (eof_token(token
))
2058 if (token_type(token
) != TOKEN_STRING
) {
2059 sparse_error(token
->pos
, "expected path string");
2062 add_dirafter_entry(token
, token
->string
->data
);
2066 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
2071 * Split the include path. Any directories specified with `-I'
2072 * options before `-I-' are searched only for headers requested with
2073 * `#include "FILE"'; they are not searched for `#include <FILE>'.
2074 * If additional directories are specified with `-I' options after
2075 * the `-I-', those directories are searched for all `#include'
2077 * In addition, `-I-' inhibits the use of the directory of the current
2078 * file directory as the first search directory for `#include "FILE"'.
2080 quote_includepath
= includepath
+1;
2081 angle_includepath
= sys_includepath
;
2086 * We replace "#pragma xxx" with "__pragma__" in the token
2087 * stream. Just as an example.
2089 * We'll just #define that away for now, but the theory here
2090 * is that we can use this to insert arbitrary token sequences
2091 * to turn the pragmas into internal front-end sequences for
2092 * when we actually start caring about them.
2094 * So eventually this will turn into some kind of extended
2095 * __attribute__() like thing, except called __pragma__(xxx).
2097 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
2099 struct token
*next
= *line
;
2101 if (match_ident(token
->next
, &once_ident
) && eof_token(token
->next
->next
)) {
2105 token
->ident
= &pragma_ident
;
2106 token
->pos
.newline
= 1;
2107 token
->pos
.whitespace
= 1;
2115 * We ignore #line for now.
2117 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
2122 static int handle_ident(struct stream
*stream
, struct token
**line
, struct token
*token
)
2127 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
2129 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
, 0));
2134 static void init_preprocessor(void)
2137 int stream
= init_stream("preprocessor", -1, includepath
);
2140 int (*handler
)(struct stream
*, struct token
**, struct token
*);
2142 { "define", handle_define
},
2143 { "weak_define", handle_weak_define
},
2144 { "strong_define", handle_strong_define
},
2145 { "undef", handle_undef
},
2146 { "strong_undef", handle_strong_undef
},
2147 { "warning", handle_warning
},
2148 { "error", handle_error
},
2149 { "include", handle_include
},
2150 { "include_next", handle_include_next
},
2151 { "pragma", handle_pragma
},
2152 { "line", handle_line
},
2153 { "ident", handle_ident
},
2155 // our internal preprocessor tokens
2156 { "nostdinc", handle_nostdinc
},
2157 { "add_include", handle_add_include
},
2158 { "add_isystem", handle_add_isystem
},
2159 { "add_system", handle_add_system
},
2160 { "add_dirafter", handle_add_dirafter
},
2161 { "split_include", handle_split_include
},
2162 { "argv_include", handle_argv_include
},
2164 { "ifdef", handle_ifdef
},
2165 { "ifndef", handle_ifndef
},
2166 { "else", handle_else
},
2167 { "endif", handle_endif
},
2168 { "if", handle_if
},
2169 { "elif", handle_elif
},
2173 void (*expander
)(struct token
*);
2175 { "__LINE__", expand_line
},
2176 { "__FILE__", expand_file
},
2177 { "__BASE_FILE__", expand_basefile
},
2178 { "__DATE__", expand_date
},
2179 { "__TIME__", expand_time
},
2180 { "__COUNTER__", expand_counter
},
2181 { "__INCLUDE_LEVEL__", expand_include_level
},
2184 for (i
= 0; i
< ARRAY_SIZE(normal
); i
++) {
2186 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
2187 sym
->handler
= normal
[i
].handler
;
2190 for (i
= 0; i
< ARRAY_SIZE(special
); i
++) {
2192 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
2193 sym
->handler
= special
[i
].handler
;
2196 for (i
= 0; i
< ARRAY_SIZE(dynamic
); i
++) {
2198 sym
= create_symbol(stream
, dynamic
[i
].name
, SYM_NODE
, NS_MACRO
);
2199 sym
->expander
= dynamic
[i
].expander
;
2205 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
2207 int (*handler
)(struct stream
*, struct token
**, struct token
*);
2208 struct token
*token
= start
->next
;
2211 if (eof_token(token
))
2214 if (token_type(token
) == TOKEN_IDENT
) {
2215 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
2217 handler
= sym
->handler
;
2218 is_normal
= sym
->normal
;
2220 handler
= handle_nondirective
;
2222 } else if (token_type(token
) == TOKEN_NUMBER
) {
2223 handler
= handle_line
;
2225 handler
= handle_nondirective
;
2229 dirty_stream(stream
);
2233 if (!handler(stream
, line
, token
)) /* all set */
2237 free_preprocessor_line(token
);
2240 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
2242 struct token
*start
= *line
, *next
;
2243 struct token
**tp
= &start
->next
;
2247 if (next
->pos
.newline
)
2252 *tp
= &eof_token_entry
;
2253 handle_preprocessor_line(stream
, line
, start
);
2256 static void do_preprocess(struct token
**list
)
2260 while (!eof_token(next
= scan_next(list
))) {
2261 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
2263 if (next
->pos
.newline
&& match_op(next
, '#')) {
2264 if (!next
->pos
.noexpand
) {
2265 preprocessor_line(stream
, list
);
2266 __free_token(next
); /* Free the '#' token */
2271 switch (token_type(next
)) {
2272 case TOKEN_STREAMEND
:
2273 if (stream
->top_if
) {
2274 nesting_error(stream
);
2275 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
2276 stream
->top_if
= NULL
;
2280 stream
->constant
= CONSTANT_FILE_YES
;
2284 case TOKEN_STREAMBEGIN
:
2290 dirty_stream(stream
);
2291 if (false_nesting
) {
2297 if (token_type(next
) != TOKEN_IDENT
||
2298 expand_one_symbol(list
))
2304 void init_include_path(void)
2311 fp
= popen("/bin/uname -m", "r");
2314 if (!fgets(arch
, sizeof(arch
) - 1, fp
))
2317 if (arch
[strlen(arch
) - 1] == '\n')
2318 arch
[strlen(arch
) - 1] = '\0';
2320 fp
= popen("/bin/uname -o", "r");
2323 fgets(os
, sizeof(os
) - 1, fp
);
2326 if (strcmp(os
, "GNU/Linux\n") != 0)
2328 strcpy(os
, "linux-gnu");
2330 snprintf(path
, sizeof(path
), "/usr/include/%s-%s/", arch
, os
);
2331 add_pre_buffer("#add_system \"%s/\"\n", path
);
2334 struct token
* preprocess(struct token
*token
)
2337 init_preprocessor();
2338 do_preprocess(&token
);
2340 // Drop all expressions from preprocessing, they're not used any more.
2341 // This is not true when we have multiple files, though ;/
2342 // clear_expression_alloc();
2348 static int is_VA_ARGS_token(struct token
*token
)
2350 return (token_type(token
) == TOKEN_IDENT
) &&
2351 (token
->ident
== &__VA_ARGS___ident
);
2354 static void dump_macro(struct symbol
*sym
)
2356 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
2357 struct token
*args
[nargs
];
2358 struct token
*token
;
2360 printf("#define %s", show_ident(sym
->ident
));
2361 token
= sym
->arglist
;
2363 const char *sep
= "";
2366 for (; !eof_token(token
); token
= token
->next
) {
2367 if (token_type(token
) == TOKEN_ARG_COUNT
)
2369 if (is_VA_ARGS_token(token
))
2370 printf("%s...", sep
);
2372 printf("%s%s", sep
, show_token(token
));
2373 args
[narg
++] = token
;
2379 token
= sym
->expansion
;
2380 while (token_type(token
) != TOKEN_UNTAINT
) {
2381 struct token
*next
= token
->next
;
2382 if (token
->pos
.whitespace
)
2384 switch (token_type(token
)) {
2388 case TOKEN_STR_ARGUMENT
:
2391 case TOKEN_QUOTED_ARGUMENT
:
2392 case TOKEN_MACRO_ARGUMENT
:
2393 token
= args
[token
->argnum
];
2396 printf("%s", show_token(token
));
2403 void dump_macro_definitions(void)
2407 FOR_EACH_PTR(macros
, name
) {
2408 struct symbol
*sym
= lookup_macro(name
);
2411 } END_FOR_EACH_PTR(name
);