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
53 #define INCLUDEPATHS 300
54 const char *includepath
[INCLUDEPATHS
+1] = {
61 static const char **quote_includepath
= includepath
;
62 static const char **angle_includepath
= includepath
+ 1;
63 static const char **isys_includepath
= includepath
+ 1;
64 static const char **sys_includepath
= includepath
+ 1;
65 static const char **dirafter_includepath
= includepath
+ 3;
67 #define dirty_stream(stream) \
69 if (!stream->dirty) { \
71 if (!stream->ifndef) \
72 stream->protect = NULL; \
76 #define end_group(stream) \
78 if (stream->ifndef == stream->top_if) { \
79 stream->ifndef = NULL; \
81 stream->protect = NULL; \
82 else if (stream->protect) \
87 #define nesting_error(stream) \
90 stream->ifndef = NULL; \
91 stream->protect = NULL; \
94 static struct token
*alloc_token(struct position
*pos
)
96 struct token
*token
= __alloc_token(0);
98 token
->pos
.stream
= pos
->stream
;
99 token
->pos
.line
= pos
->line
;
100 token
->pos
.pos
= pos
->pos
;
101 token
->pos
.whitespace
= 1;
105 /* Expand symbol 'sym' at '*list' */
106 static int expand(struct token
**, struct symbol
*);
108 static void replace_with_string(struct token
*token
, const char *str
)
110 int size
= strlen(str
) + 1;
111 struct string
*s
= __alloc_string(size
);
114 memcpy(s
->data
, str
, size
);
115 token_type(token
) = TOKEN_STRING
;
119 static void replace_with_integer(struct token
*token
, unsigned int val
)
121 char *buf
= __alloc_bytes(11);
122 sprintf(buf
, "%u", val
);
123 token_type(token
) = TOKEN_NUMBER
;
127 static struct symbol
*lookup_macro(struct ident
*ident
)
129 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_UNDEF
);
130 if (sym
&& sym
->namespace != NS_MACRO
)
135 static int token_defined(struct token
*token
)
137 if (token_type(token
) == TOKEN_IDENT
) {
138 struct symbol
*sym
= lookup_macro(token
->ident
);
140 sym
->used_in
= file_scope
;
146 sparse_error(token
->pos
, "expected preprocessor identifier");
150 static void replace_with_defined(struct token
*token
)
152 static const char *string
[] = { "0", "1" };
153 int defined
= token_defined(token
);
155 token_type(token
) = TOKEN_NUMBER
;
156 token
->number
= string
[defined
];
159 static int expand_one_symbol(struct token
**list
)
161 struct token
*token
= *list
;
163 static char buffer
[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
166 if (token
->pos
.noexpand
)
169 sym
= lookup_macro(token
->ident
);
171 store_macro_pos(token
);
172 sym
->used_in
= file_scope
;
173 return expand(list
, sym
);
175 if (token
->ident
== &__LINE___ident
) {
176 replace_with_integer(token
, token
->pos
.line
);
177 } else if (token
->ident
== &__FILE___ident
) {
178 replace_with_string(token
, stream_name(token
->pos
.stream
));
179 } else if (token
->ident
== &__DATE___ident
) {
182 strftime(buffer
, 12, "%b %e %Y", localtime(&t
));
183 replace_with_string(token
, buffer
);
184 } else if (token
->ident
== &__TIME___ident
) {
187 strftime(buffer
, 9, "%T", localtime(&t
));
188 replace_with_string(token
, buffer
);
189 } else if (token
->ident
== &__COUNTER___ident
) {
190 replace_with_integer(token
, counter_macro
++);
195 static inline struct token
*scan_next(struct token
**where
)
197 struct token
*token
= *where
;
198 if (token_type(token
) != TOKEN_UNTAINT
)
201 token
->ident
->tainted
= 0;
203 } while (token_type(token
) == TOKEN_UNTAINT
);
208 static void expand_list(struct token
**list
)
211 while (!eof_token(next
= scan_next(list
))) {
212 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
217 static void preprocessor_line(struct stream
*stream
, struct token
**line
);
219 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
, int count
)
221 struct stream
*stream
= input_streams
+ prev
->pos
.stream
;
222 struct token
**p
= &prev
->next
;
226 while (!eof_token(next
= scan_next(p
))) {
227 if (next
->pos
.newline
&& match_op(next
, '#')) {
228 if (!next
->pos
.noexpand
) {
229 sparse_error(next
->pos
,
230 "directive in argument list");
231 preprocessor_line(stream
, p
);
232 __free_token(next
); /* Free the '#' token */
236 switch (token_type(next
)) {
237 case TOKEN_STREAMEND
:
238 case TOKEN_STREAMBEGIN
:
239 *p
= &eof_token_entry
;
242 case TOKEN_WIDE_STRING
:
244 next
->string
->immutable
= 1;
252 if (match_op(next
, '(')) {
254 } else if (match_op(next
, ')')) {
257 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
260 next
->pos
.stream
= pos
->stream
;
261 next
->pos
.line
= pos
->line
;
262 next
->pos
.pos
= pos
->pos
;
265 *p
= &eof_token_entry
;
270 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
275 struct token
*expanded
;
282 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
284 int wanted
= arglist
->count
.normal
;
285 struct token
*next
= NULL
;
288 arglist
= arglist
->next
; /* skip counter */
291 next
= collect_arg(start
, 0, &what
->pos
, 0);
294 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
299 for (count
= 0; count
< wanted
; count
++) {
300 struct argcount
*p
= &arglist
->next
->count
;
301 next
= collect_arg(start
, p
->vararg
, &what
->pos
, p
->normal
);
304 if (p
->vararg
&& wanted
== 1 && eof_token(start
->next
))
306 arglist
= arglist
->next
->next
;
307 args
[count
].arg
= start
->next
;
308 args
[count
].n_normal
= p
->normal
;
309 args
[count
].n_quoted
= p
->quoted
;
310 args
[count
].n_str
= p
->str
;
311 if (match_op(next
, ')')) {
317 if (count
== wanted
&& !match_op(next
, ')'))
319 if (count
== wanted
- 1) {
320 struct argcount
*p
= &arglist
->next
->count
;
323 args
[count
].arg
= NULL
;
324 args
[count
].n_normal
= p
->normal
;
325 args
[count
].n_quoted
= p
->quoted
;
326 args
[count
].n_str
= p
->str
;
328 if (count
< wanted
- 1)
331 what
->next
= next
->next
;
335 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
336 show_token(what
), wanted
, count
);
339 while (match_op(next
, ',')) {
340 next
= collect_arg(next
, 0, &what
->pos
, 0);
345 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
346 show_token(what
), count
, wanted
);
349 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
352 what
->next
= next
->next
;
356 static struct token
*dup_list(struct token
*list
)
358 struct token
*res
= NULL
;
359 struct token
**p
= &res
;
361 while (!eof_token(list
)) {
362 struct token
*newtok
= __alloc_token(0);
371 static const char *show_token_sequence(struct token
*token
, int quote
)
373 static char buffer
[MAX_STRING
];
377 if (!token
&& !quote
)
379 while (!eof_token(token
)) {
380 const char *val
= quote
? quote_token(token
) : show_token(token
);
381 int len
= strlen(val
);
383 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
384 sparse_error(token
->pos
, "too long token expansion");
390 memcpy(ptr
, val
, len
);
393 whitespace
= token
->pos
.whitespace
;
399 static struct token
*stringify(struct token
*arg
)
401 const char *s
= show_token_sequence(arg
, 1);
402 int size
= strlen(s
)+1;
403 struct token
*token
= __alloc_token(0);
404 struct string
*string
= __alloc_string(size
);
406 memcpy(string
->data
, s
, size
);
407 string
->length
= size
;
408 token
->pos
= arg
->pos
;
409 token_type(token
) = TOKEN_STRING
;
410 token
->string
= string
;
411 token
->next
= &eof_token_entry
;
415 static void expand_arguments(int count
, struct arg
*args
)
418 for (i
= 0; i
< count
; i
++) {
419 struct token
*arg
= args
[i
].arg
;
421 arg
= &eof_token_entry
;
423 args
[i
].str
= stringify(arg
);
424 if (args
[i
].n_normal
) {
425 if (!args
[i
].n_quoted
) {
426 args
[i
].expanded
= arg
;
428 } else if (eof_token(arg
)) {
429 args
[i
].expanded
= arg
;
431 args
[i
].expanded
= dup_list(arg
);
433 expand_list(&args
[i
].expanded
);
439 * Possibly valid combinations:
440 * - ident + ident -> ident
441 * - ident + number -> ident unless number contains '.', '+' or '-'.
442 * - 'L' + char constant -> wide char constant
443 * - 'L' + string literal -> wide string literal
444 * - number + number -> number
445 * - number + ident -> number
446 * - number + '.' -> number
447 * - number + '+' or '-' -> number, if number used to end on [eEpP].
448 * - '.' + number -> number, if number used to start with a digit.
449 * - special + special -> either special or an error.
451 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
454 enum token_type t1
= token_type(left
), t2
= token_type(right
);
456 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
459 if (t1
== TOKEN_IDENT
&& left
->ident
== &L_ident
) {
460 if (t2
>= TOKEN_CHAR
&& t2
< TOKEN_WIDE_CHAR
)
461 return t2
+ TOKEN_WIDE_CHAR
- TOKEN_CHAR
;
462 if (t2
== TOKEN_STRING
)
463 return TOKEN_WIDE_STRING
;
466 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
469 strcpy(p
, show_token(left
));
470 strcat(p
, show_token(right
));
476 if (t1
== TOKEN_IDENT
) {
477 if (t2
== TOKEN_SPECIAL
)
479 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
484 if (t1
== TOKEN_NUMBER
) {
485 if (t2
== TOKEN_SPECIAL
) {
486 switch (right
->special
) {
490 if (strchr("eEpP", p
[len
- 2]))
499 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
502 return TOKEN_SPECIAL
;
505 static int merge(struct token
*left
, struct token
*right
)
507 static char buffer
[512];
508 enum token_type res
= combine(left
, right
, buffer
);
513 left
->ident
= built_in_ident(buffer
);
514 left
->pos
.noexpand
= 0;
518 char *number
= __alloc_bytes(strlen(buffer
) + 1);
519 memcpy(number
, buffer
, strlen(buffer
) + 1);
520 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
521 left
->number
= number
;
526 if (buffer
[2] && buffer
[3])
528 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
529 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
536 case TOKEN_WIDE_CHAR
:
537 case TOKEN_WIDE_STRING
:
538 token_type(left
) = res
;
539 left
->pos
.noexpand
= 0;
540 left
->string
= right
->string
;
543 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
544 token_type(left
) = res
;
545 left
->pos
.noexpand
= 0;
546 memcpy(left
->embedded
, right
->embedded
, 4);
552 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
556 static struct token
*dup_token(struct token
*token
, struct position
*streampos
)
558 struct token
*alloc
= alloc_token(streampos
);
559 token_type(alloc
) = token_type(token
);
560 alloc
->pos
.newline
= token
->pos
.newline
;
561 alloc
->pos
.whitespace
= token
->pos
.whitespace
;
562 alloc
->number
= token
->number
;
563 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
567 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
569 int need_copy
= --*count
;
570 while (!eof_token(list
)) {
573 token
= dup_token(list
, &list
->pos
);
576 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
577 token
->pos
.noexpand
= 1;
579 where
= &token
->next
;
582 *where
= &eof_token_entry
;
586 static int handle_kludge(struct token
**p
, struct arg
*args
)
588 struct token
*t
= (*p
)->next
->next
;
590 struct arg
*v
= &args
[t
->argnum
];
591 if (token_type(t
->next
) != TOKEN_CONCAT
) {
593 /* ignore the first ## */
597 /* skip the entire thing */
601 if (v
->arg
&& !eof_token(v
->arg
))
602 return 0; /* no magic */
607 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
609 struct position
*base_pos
= &(*list
)->pos
;
611 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
613 for (; !eof_token(body
); body
= body
->next
) {
614 struct token
*added
, *arg
;
618 switch (token_type(body
)) {
619 case TOKEN_GNU_KLUDGE
:
621 * GNU kludge: if we had <comma>##<vararg>, behaviour
622 * depends on whether we had enough arguments to have
623 * a vararg. If we did, ## is just ignored. Otherwise
624 * both , and ## are ignored. Worse, there can be
625 * an arbitrary number of ##<arg> in between; if all of
626 * those are empty, we act as if they hadn't been there,
627 * otherwise we act as if the kludge didn't exist.
630 if (handle_kludge(&body
, args
)) {
637 added
= dup_token(t
, base_pos
);
638 token_type(added
) = TOKEN_SPECIAL
;
642 case TOKEN_STR_ARGUMENT
:
643 arg
= args
[body
->argnum
].str
;
644 count
= &args
[body
->argnum
].n_str
;
647 case TOKEN_QUOTED_ARGUMENT
:
648 arg
= args
[body
->argnum
].arg
;
649 count
= &args
[body
->argnum
].n_quoted
;
650 if (!arg
|| eof_token(arg
)) {
659 case TOKEN_MACRO_ARGUMENT
:
660 arg
= args
[body
->argnum
].expanded
;
661 count
= &args
[body
->argnum
].n_normal
;
662 if (eof_token(arg
)) {
667 tail
= copy(&added
, arg
, count
);
668 added
->pos
.newline
= body
->pos
.newline
;
669 added
->pos
.whitespace
= body
->pos
.whitespace
;
673 if (state
== Placeholder
)
680 added
= dup_token(body
, base_pos
);
681 if (added
->ident
->tainted
)
682 added
->pos
.noexpand
= 1;
687 added
= dup_token(body
, base_pos
);
693 * if we got to doing real concatenation, we already have
694 * added something into the list, so containing_token() is OK.
696 if (state
== Concat
&& merge(containing_token(list
), added
)) {
698 if (tail
!= &added
->next
)
706 *list
= &eof_token_entry
;
710 static int expand(struct token
**list
, struct symbol
*sym
)
713 struct token
*token
= *list
;
714 struct ident
*expanding
= token
->ident
;
716 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
717 struct arg args
[nargs
];
719 if (expanding
->tainted
) {
720 token
->pos
.noexpand
= 1;
725 if (!match_op(scan_next(&token
->next
), '('))
727 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
729 expand_arguments(nargs
, args
);
732 expanding
->tainted
= 1;
735 tail
= substitute(list
, sym
->expansion
, args
);
737 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
738 * We still can lose the newline flag if the sucker expands to nothing,
739 * but the price of dealing with that is probably too high (we'd need
740 * to collect the flags during scan_next())
742 (*list
)->pos
.newline
= token
->pos
.newline
;
743 (*list
)->pos
.whitespace
= token
->pos
.whitespace
;
749 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
751 static char buffer
[256];
754 while (!eof_token(token
) && !match_op(token
, endop
)) {
756 const char *val
= token
->string
->data
;
757 if (token_type(token
) != TOKEN_STRING
)
758 val
= show_token(token
);
760 memcpy(ptr
, val
, len
);
765 if (endop
&& !match_op(token
, endop
))
766 sparse_error(start
->pos
, "expected '>' at end of filename");
770 static int already_tokenized(const char *path
)
774 for (stream
= *hash_stream(path
); stream
>= 0 ; stream
= next
) {
775 struct stream
*s
= input_streams
+ stream
;
777 next
= s
->next_stream
;
779 if (strcmp(path
, s
->name
))
783 if (s
->constant
!= CONSTANT_FILE_YES
)
785 if (strcmp(path
, s
->name
))
787 if (s
->protect
&& !lookup_macro(s
->protect
))
794 /* Handle include of header files.
795 * The relevant options are made compatible with gcc. The only options that
796 * are not supported is -withprefix and friends.
798 * Three set of include paths are known:
799 * quote_includepath: Path to search when using #include "file.h"
800 * angle_includepath: Paths to search when using #include <file.h>
801 * isys_includepath: Paths specified with -isystem, come before the
802 * built-in system include paths. Gcc would suppress
803 * warnings from system headers. Here we separate
804 * them from the angle_ ones to keep search ordering.
806 * sys_includepath: Built-in include paths.
807 * dirafter_includepath Paths added with -dirafter.
809 * The above is implemented as one array with pointers
811 * quote_includepath ---> | |
815 * angle_includepath ---> | |
817 * isys_includepath ---> | |
819 * sys_includepath ---> | |
821 * dirafter_includepath -> | |
824 * -I dir insert dir just before isys_includepath and move the rest
825 * -I- makes all dirs specified with -I before to quote dirs only and
826 * angle_includepath is set equal to isys_includepath.
827 * -nostdinc removes all sys dirs by storing NULL in entry pointed
828 * to by * sys_includepath. Note that this will reset all dirs built-in
829 * and added before -nostdinc by -isystem and -idirafter.
830 * -isystem dir adds dir where isys_includepath points adding this dir as
832 * -idirafter dir adds dir to the end of the list
835 static void set_stream_include_path(struct stream
*stream
)
837 const char *path
= stream
->path
;
839 const char *p
= strrchr(stream
->name
, '/');
842 int len
= p
- stream
->name
+ 1;
843 char *m
= malloc(len
+1);
844 /* This includes the final "/" */
845 memcpy(m
, stream
->name
, len
);
851 includepath
[0] = path
;
854 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
857 int plen
= strlen(path
);
858 static char fullname
[PATH_MAX
];
860 memcpy(fullname
, path
, plen
);
861 if (plen
&& path
[plen
-1] != '/') {
862 fullname
[plen
] = '/';
865 memcpy(fullname
+plen
, filename
, flen
);
866 if (already_tokenized(fullname
))
868 fd
= open(fullname
, O_RDONLY
);
870 char * streamname
= __alloc_bytes(plen
+ flen
);
871 memcpy(streamname
, fullname
, plen
+ flen
);
872 *where
= tokenize(streamname
, fd
, *where
, next_path
);
879 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
883 while ((path
= *pptr
++) != NULL
) {
884 if (!try_include(path
, filename
, flen
, list
, pptr
))
891 static int free_preprocessor_line(struct token
*token
)
893 while (token_type(token
) != TOKEN_EOF
) {
894 struct token
*free
= token
;
901 const char *find_include(const char *skip
, const char *look_for
)
904 struct dirent
*entry
;
908 static char buf
[PATH_MAX
+ 1];
914 if (!getcwd(cwd
, sizeof(cwd
)))
917 while ((entry
= readdir(dp
))) {
918 lstat(entry
->d_name
, &statbuf
);
920 if (strcmp(entry
->d_name
, look_for
) == 0) {
921 snprintf(buf
, sizeof(buf
), "%s/%s", cwd
, entry
->d_name
);
925 if (S_ISDIR(statbuf
.st_mode
)) {
926 /* Found a directory, but ignore . and .. */
927 if (strcmp(".", entry
->d_name
) == 0 ||
928 strcmp("..", entry
->d_name
) == 0 ||
929 strcmp(skip
, entry
->d_name
) == 0)
932 chdir(entry
->d_name
);
933 ret
= find_include("", look_for
);
944 const char *search_dir(const char *stop
, const char *look_for
)
951 if (!getcwd(cwd
, sizeof(cwd
)))
956 ret
= find_include(cnt
++ ? cwd
+ len
+ 1 : "", look_for
);
960 if (strcmp(cwd
, stop
) == 0 ||
961 strcmp(cwd
, "/usr/include") == 0 ||
962 strcmp(cwd
, "/usr/local/include") == 0 ||
963 strlen(cwd
) <= 10 || /* heck... don't search /usr/lib/ */
964 strcmp(cwd
, "/") == 0)
968 if (cwd
[len
] == '/') {
979 static void use_best_guess_header_file(struct token
*token
, const char *filename
, struct token
**list
)
982 char dir_part
[PATH_MAX
];
983 const char *file_part
;
984 const char *include_name
;
987 if (!filename
|| filename
[0] == '\0')
990 file_part
= filename
;
991 while ((filename
= strchr(filename
, '/'))) {
994 file_part
= filename
;
997 snprintf(dir_part
, sizeof(dir_part
), "%s", stream_name(token
->pos
.stream
));
998 len
= strlen(dir_part
);
1000 if (dir_part
[len
] == '/') {
1001 dir_part
[len
] = '\0';
1006 sprintf(dir_part
, ".");
1008 if (!getcwd(cwd
, sizeof(cwd
)))
1012 include_name
= search_dir(cwd
, file_part
);
1016 sparse_error(token
->pos
, "using '%s'", include_name
);
1018 try_include("", include_name
, strlen(include_name
), list
, includepath
);
1021 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, int how
)
1023 const char *filename
;
1031 if (!match_op(next
, '<')) {
1032 expand_list(&token
->next
);
1035 if (match_op(token
->next
, '<')) {
1042 filename
= token_name_sequence(token
, expect
, token
);
1043 flen
= strlen(filename
) + 1;
1045 /* Absolute path? */
1046 if (filename
[0] == '/') {
1047 if (try_include("", filename
, flen
, list
, includepath
))
1054 path
= stream
->next_path
;
1057 includepath
[0] = "";
1061 /* Dir of input file is first dir to search for quoted includes */
1062 set_stream_include_path(stream
);
1063 path
= expect
? angle_includepath
: quote_includepath
;
1066 /* Check the standard include paths.. */
1067 if (do_include_path(path
, list
, token
, filename
, flen
))
1070 sparse_error(token
->pos
, "unable to open '%s'", filename
);
1071 use_best_guess_header_file(token
, filename
, list
);
1075 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
1077 return handle_include_path(stream
, list
, token
, 0);
1080 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
1082 return handle_include_path(stream
, list
, token
, 1);
1085 static int handle_argv_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
1087 return handle_include_path(stream
, list
, token
, 2);
1090 static int token_different(struct token
*t1
, struct token
*t2
)
1094 if (token_type(t1
) != token_type(t2
))
1097 switch (token_type(t1
)) {
1099 different
= t1
->ident
!= t2
->ident
;
1101 case TOKEN_ARG_COUNT
:
1104 case TOKEN_GNU_KLUDGE
:
1108 different
= strcmp(t1
->number
, t2
->number
);
1111 different
= t1
->special
!= t2
->special
;
1113 case TOKEN_MACRO_ARGUMENT
:
1114 case TOKEN_QUOTED_ARGUMENT
:
1115 case TOKEN_STR_ARGUMENT
:
1116 different
= t1
->argnum
!= t2
->argnum
;
1118 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
1119 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
1120 different
= memcmp(t1
->embedded
, t2
->embedded
, 4);
1123 case TOKEN_WIDE_CHAR
:
1125 case TOKEN_WIDE_STRING
: {
1126 struct string
*s1
, *s2
;
1131 if (s1
->length
!= s2
->length
)
1133 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
1143 static int token_list_different(struct token
*list1
, struct token
*list2
)
1148 if (!list1
|| !list2
)
1150 if (token_different(list1
, list2
))
1152 list1
= list1
->next
;
1153 list2
= list2
->next
;
1157 static inline void set_arg_count(struct token
*token
)
1159 token_type(token
) = TOKEN_ARG_COUNT
;
1160 token
->count
.normal
= token
->count
.quoted
=
1161 token
->count
.str
= token
->count
.vararg
= 0;
1164 static struct token
*parse_arguments(struct token
*list
)
1166 struct token
*arg
= list
->next
, *next
= list
;
1167 struct argcount
*count
= &list
->count
;
1169 set_arg_count(list
);
1171 if (match_op(arg
, ')')) {
1173 list
->next
= &eof_token_entry
;
1177 while (token_type(arg
) == TOKEN_IDENT
) {
1178 if (arg
->ident
== &__VA_ARGS___ident
)
1180 if (!++count
->normal
)
1184 if (match_op(next
, ',')) {
1185 set_arg_count(next
);
1190 if (match_op(next
, ')')) {
1191 set_arg_count(next
);
1193 arg
->next
->next
= &eof_token_entry
;
1197 /* normal cases are finished here */
1199 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
1200 if (match_op(next
->next
, ')')) {
1201 set_arg_count(next
);
1202 next
->count
.vararg
= 1;
1204 arg
->next
->next
= &eof_token_entry
;
1212 if (eof_token(next
)) {
1220 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
1222 token_type(arg
) = TOKEN_IDENT
;
1223 arg
->ident
= &__VA_ARGS___ident
;
1224 if (!match_op(next
, ')'))
1226 if (!++count
->normal
)
1228 set_arg_count(next
);
1229 next
->count
.vararg
= 1;
1231 arg
->next
->next
= &eof_token_entry
;
1235 if (eof_token(arg
)) {
1239 if (match_op(arg
, ','))
1246 sparse_error(arg
->pos
, "parameter name missing");
1249 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
1253 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
1256 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
1259 sparse_error(arg
->pos
, "too many arguments in macro definition");
1263 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
1265 struct ident
*ident
= token
->ident
;
1268 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
1271 arglist
= arglist
->next
;
1273 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
1274 if (arglist
->ident
== ident
) {
1275 struct argcount
*count
= &arglist
->next
->count
;
1279 token_type(token
) = type
;
1281 case TOKEN_MACRO_ARGUMENT
:
1282 n
= ++count
->normal
;
1284 case TOKEN_QUOTED_ARGUMENT
:
1285 n
= ++count
->quoted
;
1291 return count
->vararg
? 2 : 1;
1293 * XXX - need saner handling of that
1294 * (>= 1024 instances of argument)
1296 token_type(token
) = TOKEN_ERROR
;
1303 static struct token
*handle_hash(struct token
**p
, struct token
*arglist
)
1305 struct token
*token
= *p
;
1307 struct token
*next
= token
->next
;
1308 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1310 next
->pos
.whitespace
= token
->pos
.whitespace
;
1311 __free_token(token
);
1314 token
->pos
.noexpand
= 1;
1319 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1323 /* token->next is ## */
1324 static struct token
*handle_hashhash(struct token
*token
, struct token
*arglist
)
1326 struct token
*last
= token
;
1327 struct token
*concat
;
1328 int state
= match_op(token
, ',');
1330 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1336 /* eat duplicate ## */
1337 concat
= token
->next
;
1338 while (match_op(t
= concat
->next
, SPECIAL_HASHHASH
)) {
1340 __free_token(concat
);
1343 token_type(concat
) = TOKEN_CONCAT
;
1348 if (match_op(t
, '#')) {
1349 t
= handle_hash(&concat
->next
, arglist
);
1354 is_arg
= try_arg(t
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1356 if (state
== 1 && is_arg
) {
1360 state
= match_op(t
, ',');
1364 if (!match_op(token
->next
, SPECIAL_HASHHASH
))
1367 /* handle GNU ,##__VA_ARGS__ kludge, in all its weirdness */
1369 token_type(last
) = TOKEN_GNU_KLUDGE
;
1373 sparse_error(concat
->pos
, "'##' cannot appear at the ends of macro expansion");
1377 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1379 struct token
*token
= expansion
;
1382 if (match_op(token
, SPECIAL_HASHHASH
))
1385 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1386 if (match_op(token
, '#')) {
1387 token
= handle_hash(p
, arglist
);
1391 if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1392 token
= handle_hashhash(token
, arglist
);
1396 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1398 switch (token_type(token
)) {
1403 case TOKEN_WIDE_STRING
:
1404 token
->string
->immutable
= 1;
1408 token
= alloc_token(&expansion
->pos
);
1409 token_type(token
) = TOKEN_UNTAINT
;
1410 token
->ident
= name
;
1416 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1419 sparse_error(token
->pos
, "too many instances of argument in body");
1423 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1425 struct token
*arglist
, *expansion
;
1426 struct token
*left
= token
->next
;
1431 if (token_type(left
) != TOKEN_IDENT
) {
1432 sparse_error(token
->pos
, "expected identifier to 'define'");
1439 expansion
= left
->next
;
1440 if (!expansion
->pos
.whitespace
) {
1441 if (match_op(expansion
, '(')) {
1442 arglist
= expansion
;
1443 expansion
= parse_arguments(expansion
);
1446 } else if (!eof_token(expansion
)) {
1447 warning(expansion
->pos
,
1448 "no whitespace before object-like macro body");
1452 expansion
= parse_expansion(expansion
, arglist
, name
);
1457 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1461 if (attr
< sym
->attr
)
1464 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1466 if (token_list_different(sym
->expansion
, expansion
) ||
1467 token_list_different(sym
->arglist
, arglist
)) {
1469 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1470 || sym
->used_in
== file_scope
) {
1471 warning(left
->pos
, "preprocessor token %.*s redefined",
1472 name
->len
, name
->name
);
1473 info(sym
->pos
, "this was the original definition");
1479 if (!sym
|| sym
->scope
!= file_scope
) {
1480 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1481 bind_symbol(sym
, name
, NS_MACRO
);
1482 add_ident(¯os
, name
);
1487 sym
->expansion
= expansion
;
1488 sym
->arglist
= arglist
;
1489 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1492 sym
->namespace = NS_MACRO
;
1493 sym
->used_in
= NULL
;
1499 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1501 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1504 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1506 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1509 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1511 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1514 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1516 struct token
*left
= token
->next
;
1519 if (token_type(left
) != TOKEN_IDENT
) {
1520 sparse_error(token
->pos
, "expected identifier to 'undef'");
1524 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1526 if (attr
< sym
->attr
)
1528 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1530 } else if (attr
<= SYM_ATTR_NORMAL
)
1533 if (!sym
|| sym
->scope
!= file_scope
) {
1534 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1535 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1538 sym
->namespace = NS_UNDEF
;
1539 sym
->used_in
= NULL
;
1545 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1547 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1550 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1552 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1555 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1557 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1558 free_preprocessor_line(token
->next
);
1559 token
->next
= stream
->top_if
;
1560 stream
->top_if
= token
;
1561 if (false_nesting
|| true != 1)
1566 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1568 struct token
*next
= token
->next
;
1570 if (token_type(next
) == TOKEN_IDENT
) {
1571 arg
= token_defined(next
);
1573 dirty_stream(stream
);
1575 sparse_error(token
->pos
, "expected preprocessor identifier");
1578 return preprocessor_if(stream
, token
, arg
);
1581 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1583 struct token
*next
= token
->next
;
1585 if (token_type(next
) == TOKEN_IDENT
) {
1586 if (!stream
->dirty
&& !stream
->ifndef
) {
1587 if (!stream
->protect
) {
1588 stream
->ifndef
= token
;
1589 stream
->protect
= next
->ident
;
1590 } else if (stream
->protect
== next
->ident
) {
1591 stream
->ifndef
= token
;
1595 arg
= !token_defined(next
);
1597 dirty_stream(stream
);
1599 sparse_error(token
->pos
, "expected preprocessor identifier");
1603 return preprocessor_if(stream
, token
, arg
);
1606 static const char *show_token_sequence(struct token
*token
, int quote
);
1609 * Expression handling for #if and #elif; it differs from normal expansion
1610 * due to special treatment of "defined".
1612 static int expression_value(struct token
**where
)
1614 struct expression
*expr
;
1616 struct token
**list
= where
, **beginning
= NULL
;
1620 while (!eof_token(p
= scan_next(list
))) {
1623 if (token_type(p
) != TOKEN_IDENT
)
1625 if (p
->ident
== &defined_ident
) {
1630 if (!expand_one_symbol(list
))
1632 if (token_type(p
) != TOKEN_IDENT
)
1634 token_type(p
) = TOKEN_ZERO_IDENT
;
1637 if (match_op(p
, '(')) {
1641 replace_with_defined(p
);
1646 if (token_type(p
) == TOKEN_IDENT
)
1650 replace_with_defined(p
);
1655 if (!match_op(p
, ')'))
1656 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1663 p
= constant_expression(*where
, &expr
);
1665 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
, 0));
1666 value
= get_expression_value(expr
);
1670 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1674 value
= expression_value(&token
->next
);
1676 dirty_stream(stream
);
1677 return preprocessor_if(stream
, token
, value
);
1680 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1682 struct token
*top_if
= stream
->top_if
;
1686 nesting_error(stream
);
1687 sparse_error(token
->pos
, "unmatched #elif within stream");
1691 if (token_type(top_if
) == TOKEN_ELSE
) {
1692 nesting_error(stream
);
1693 sparse_error(token
->pos
, "#elif after #else");
1699 dirty_stream(stream
);
1700 if (token_type(top_if
) != TOKEN_IF
)
1702 if (false_nesting
) {
1704 if (!expression_value(&token
->next
))
1708 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1713 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1715 struct token
*top_if
= stream
->top_if
;
1719 nesting_error(stream
);
1720 sparse_error(token
->pos
, "unmatched #else within stream");
1724 if (token_type(top_if
) == TOKEN_ELSE
) {
1725 nesting_error(stream
);
1726 sparse_error(token
->pos
, "#else after #else");
1728 if (false_nesting
) {
1729 if (token_type(top_if
) == TOKEN_IF
)
1734 token_type(top_if
) = TOKEN_ELSE
;
1738 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1740 struct token
*top_if
= stream
->top_if
;
1743 nesting_error(stream
);
1744 sparse_error(token
->pos
, "unmatched #endif in stream");
1749 stream
->top_if
= top_if
->next
;
1750 __free_token(top_if
);
1754 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1756 warning(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1760 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1762 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
, 0));
1766 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1769 * Do we have any non-system includes?
1770 * Clear them out if so..
1772 *sys_includepath
= NULL
;
1776 static inline void update_inc_ptrs(const char ***where
)
1779 if (*where
<= dirafter_includepath
) {
1780 dirafter_includepath
++;
1781 /* If this was the entry that we prepend, don't
1782 * rise the lower entries, even if they are at
1783 * the same level. */
1784 if (where
== &dirafter_includepath
)
1787 if (*where
<= sys_includepath
) {
1789 if (where
== &sys_includepath
)
1792 if (*where
<= isys_includepath
) {
1794 if (where
== &isys_includepath
)
1798 /* angle_includepath is actually never updated, since we
1799 * don't suppport -iquote rught now. May change some day. */
1800 if (*where
<= angle_includepath
) {
1801 angle_includepath
++;
1802 if (where
== &angle_includepath
)
1807 /* Add a path before 'where' and update the pointers associated with the
1808 * includepath array */
1809 static void add_path_entry(struct token
*token
, const char *path
,
1810 const char ***where
)
1815 /* Need one free entry.. */
1816 if (includepath
[INCLUDEPATHS
-2])
1817 error_die(token
->pos
, "too many include path entries");
1819 /* check that this is not a duplicate */
1822 if (strcmp(*dst
, path
) == 0)
1829 update_inc_ptrs(where
);
1832 * Move them all up starting at dst,
1833 * insert the new entry..
1836 const char *tmp
= *dst
;
1843 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1846 token
= token
->next
;
1847 if (eof_token(token
))
1849 if (token_type(token
) != TOKEN_STRING
) {
1850 warning(token
->pos
, "expected path string");
1853 add_path_entry(token
, token
->string
->data
, &isys_includepath
);
1857 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1860 token
= token
->next
;
1861 if (eof_token(token
))
1863 if (token_type(token
) != TOKEN_STRING
) {
1864 sparse_error(token
->pos
, "expected path string");
1867 add_path_entry(token
, token
->string
->data
, &sys_includepath
);
1871 static int handle_add_system(struct stream
*stream
, struct token
**line
, struct token
*token
)
1874 token
= token
->next
;
1875 if (eof_token(token
))
1877 if (token_type(token
) != TOKEN_STRING
) {
1878 sparse_error(token
->pos
, "expected path string");
1881 add_path_entry(token
, token
->string
->data
, &dirafter_includepath
);
1885 /* Add to end on includepath list - no pointer updates */
1886 static void add_dirafter_entry(struct token
*token
, const char *path
)
1888 const char **dst
= includepath
;
1890 /* Need one free entry.. */
1891 if (includepath
[INCLUDEPATHS
-2])
1892 error_die(token
->pos
, "too many include path entries");
1894 /* Add to the end */
1902 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1905 token
= token
->next
;
1906 if (eof_token(token
))
1908 if (token_type(token
) != TOKEN_STRING
) {
1909 sparse_error(token
->pos
, "expected path string");
1912 add_dirafter_entry(token
, token
->string
->data
);
1916 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1921 * Split the include path. Any directories specified with `-I'
1922 * options before `-I-' are searched only for headers requested with
1923 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1924 * If additional directories are specified with `-I' options after
1925 * the `-I-', those directories are searched for all `#include'
1927 * In addition, `-I-' inhibits the use of the directory of the current
1928 * file directory as the first search directory for `#include "FILE"'.
1930 quote_includepath
= includepath
+1;
1931 angle_includepath
= sys_includepath
;
1936 * We replace "#pragma xxx" with "__pragma__" in the token
1937 * stream. Just as an example.
1939 * We'll just #define that away for now, but the theory here
1940 * is that we can use this to insert arbitrary token sequences
1941 * to turn the pragmas into internal front-end sequences for
1942 * when we actually start caring about them.
1944 * So eventually this will turn into some kind of extended
1945 * __attribute__() like thing, except called __pragma__(xxx).
1947 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1949 struct token
*next
= *line
;
1951 if (match_ident(token
->next
, &once_ident
) && eof_token(token
->next
->next
)) {
1955 token
->ident
= &pragma_ident
;
1956 token
->pos
.newline
= 1;
1957 token
->pos
.whitespace
= 1;
1965 * We ignore #line for now.
1967 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1975 static int handle_ident(struct stream
*stream
, struct token
**line
, struct token
*token
)
1980 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1982 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
, 0));
1987 static void init_preprocessor(void)
1990 int stream
= init_stream("preprocessor", -1, includepath
);
1993 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1995 { "define", handle_define
},
1996 { "weak_define", handle_weak_define
},
1997 { "strong_define", handle_strong_define
},
1998 { "undef", handle_undef
},
1999 { "strong_undef", handle_strong_undef
},
2000 { "warning", handle_warning
},
2001 { "error", handle_error
},
2002 { "include", handle_include
},
2003 { "include_next", handle_include_next
},
2004 { "pragma", handle_pragma
},
2005 { "line", handle_line
},
2006 { "ident", handle_ident
},
2008 // our internal preprocessor tokens
2009 { "nostdinc", handle_nostdinc
},
2010 { "add_include", handle_add_include
},
2011 { "add_isystem", handle_add_isystem
},
2012 { "add_system", handle_add_system
},
2013 { "add_dirafter", handle_add_dirafter
},
2014 { "split_include", handle_split_include
},
2015 { "argv_include", handle_argv_include
},
2017 { "ifdef", handle_ifdef
},
2018 { "ifndef", handle_ifndef
},
2019 { "else", handle_else
},
2020 { "endif", handle_endif
},
2021 { "if", handle_if
},
2022 { "elif", handle_elif
},
2025 for (i
= 0; i
< ARRAY_SIZE(normal
); i
++) {
2027 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
2028 sym
->handler
= normal
[i
].handler
;
2031 for (i
= 0; i
< ARRAY_SIZE(special
); i
++) {
2033 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
2034 sym
->handler
= special
[i
].handler
;
2041 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
2043 int (*handler
)(struct stream
*, struct token
**, struct token
*);
2044 struct token
*token
= start
->next
;
2047 if (eof_token(token
))
2050 if (token_type(token
) == TOKEN_IDENT
) {
2051 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
2053 handler
= sym
->handler
;
2054 is_normal
= sym
->normal
;
2056 handler
= handle_nondirective
;
2058 } else if (token_type(token
) == TOKEN_NUMBER
) {
2059 handler
= handle_line
;
2061 handler
= handle_nondirective
;
2065 dirty_stream(stream
);
2069 if (!handler(stream
, line
, token
)) /* all set */
2073 free_preprocessor_line(token
);
2076 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
2078 struct token
*start
= *line
, *next
;
2079 struct token
**tp
= &start
->next
;
2083 if (next
->pos
.newline
)
2088 *tp
= &eof_token_entry
;
2089 handle_preprocessor_line(stream
, line
, start
);
2092 static void do_preprocess(struct token
**list
)
2096 while (!eof_token(next
= scan_next(list
))) {
2097 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
2099 if (next
->pos
.newline
&& match_op(next
, '#')) {
2100 if (!next
->pos
.noexpand
) {
2101 preprocessor_line(stream
, list
);
2102 __free_token(next
); /* Free the '#' token */
2107 switch (token_type(next
)) {
2108 case TOKEN_STREAMEND
:
2109 if (stream
->top_if
) {
2110 nesting_error(stream
);
2111 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
2112 stream
->top_if
= NULL
;
2116 stream
->constant
= CONSTANT_FILE_YES
;
2119 case TOKEN_STREAMBEGIN
:
2124 dirty_stream(stream
);
2125 if (false_nesting
) {
2131 if (token_type(next
) != TOKEN_IDENT
||
2132 expand_one_symbol(list
))
2138 void init_include_path(void)
2145 fp
= popen("/bin/uname -m", "r");
2148 if (!fgets(arch
, sizeof(arch
) - 1, fp
))
2151 if (arch
[strlen(arch
) - 1] == '\n')
2152 arch
[strlen(arch
) - 1] = '\0';
2154 fp
= popen("/bin/uname -o", "r");
2157 fgets(os
, sizeof(os
) - 1, fp
);
2160 if (strcmp(os
, "GNU/Linux\n") != 0)
2162 strcpy(os
, "linux-gnu");
2164 snprintf(path
, sizeof(path
), "/usr/include/%s-%s/", arch
, os
);
2165 add_pre_buffer("#add_system \"%s/\"\n", path
);
2168 struct token
* preprocess(struct token
*token
)
2171 init_preprocessor();
2172 do_preprocess(&token
);
2174 // Drop all expressions from preprocessing, they're not used any more.
2175 // This is not true when we have multiple files, though ;/
2176 // clear_expression_alloc();
2182 static void dump_macro(struct symbol
*sym
)
2184 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
2185 struct token
*args
[nargs
];
2186 struct token
*token
;
2188 printf("#define %s", show_ident(sym
->ident
));
2189 token
= sym
->arglist
;
2191 const char *sep
= "";
2194 for (; !eof_token(token
); token
= token
->next
) {
2195 if (token_type(token
) == TOKEN_ARG_COUNT
)
2197 printf("%s%s", sep
, show_token(token
));
2198 args
[narg
++] = token
;
2205 token
= sym
->expansion
;
2206 while (!eof_token(token
)) {
2207 struct token
*next
= token
->next
;
2208 switch (token_type(token
)) {
2211 case TOKEN_MACRO_ARGUMENT
:
2212 token
= args
[token
->argnum
];
2215 printf("%s", show_token(token
));
2216 if (next
->pos
.whitespace
)
2224 void dump_macro_definitions(void)
2228 FOR_EACH_PTR(macros
, name
) {
2229 struct symbol
*sym
= lookup_macro(name
);
2232 } END_FOR_EACH_PTR(name
);