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 * Licensed under the Open Software License version 1.1
22 #include "pre-process.h"
28 #include "expression.h"
31 static int false_nesting
= 0;
33 #define INCLUDEPATHS 300
34 const char *includepath
[INCLUDEPATHS
+1] = {
42 static const char **quote_includepath
= includepath
;
43 static const char **angle_includepath
= includepath
+ 1;
44 static const char **sys_includepath
= includepath
+ 1;
46 #define dirty_stream(stream) \
48 if (!stream->dirty) { \
50 if (!stream->ifndef) \
51 stream->protect = NULL; \
55 #define end_group(stream) \
57 if (stream->ifndef == stream->top_if) { \
58 stream->ifndef = NULL; \
60 stream->protect = NULL; \
61 else if (stream->protect) \
66 #define nesting_error(stream) \
69 stream->ifndef = NULL; \
70 stream->protect = NULL; \
73 static struct token
*alloc_token(struct position
*pos
)
75 struct token
*token
= __alloc_token(0);
77 token
->pos
.stream
= pos
->stream
;
78 token
->pos
.line
= pos
->line
;
79 token
->pos
.pos
= pos
->pos
;
80 token
->pos
.whitespace
= 1;
84 static const char *show_token_sequence(struct token
*token
);
86 /* Expand symbol 'sym' at '*list' */
87 static int expand(struct token
**, struct symbol
*);
89 static void replace_with_string(struct token
*token
, const char *str
)
91 int size
= strlen(str
) + 1;
92 struct string
*s
= __alloc_string(size
);
95 memcpy(s
->data
, str
, size
);
96 token_type(token
) = TOKEN_STRING
;
100 static void replace_with_integer(struct token
*token
, unsigned int val
)
102 char *buf
= __alloc_bytes(11);
103 sprintf(buf
, "%u", val
);
104 token_type(token
) = TOKEN_NUMBER
;
108 static struct symbol
*lookup_macro(struct ident
*ident
)
110 struct symbol
*sym
= lookup_symbol(ident
, NS_MACRO
| NS_INVISIBLEMACRO
);
111 if (sym
&& sym
->namespace != NS_MACRO
)
116 static int token_defined(struct token
*token
)
118 if (token_type(token
) == TOKEN_IDENT
) {
119 struct symbol
*sym
= lookup_macro(token
->ident
);
127 sparse_error(token
->pos
, "expected preprocessor identifier");
131 static void replace_with_defined(struct token
*token
)
133 static const char *string
[] = { "0", "1" };
134 int defined
= token_defined(token
);
136 token_type(token
) = TOKEN_NUMBER
;
137 token
->number
= string
[defined
];
140 static int expand_one_symbol(struct token
**list
)
142 struct token
*token
= *list
;
145 if (token
->pos
.noexpand
)
148 sym
= lookup_macro(token
->ident
);
151 return expand(list
, sym
);
153 if (token
->ident
== &__LINE___ident
) {
154 replace_with_integer(token
, token
->pos
.line
);
155 } else if (token
->ident
== &__FILE___ident
) {
156 replace_with_string(token
, stream_name(token
->pos
.stream
));
161 static inline struct token
*scan_next(struct token
**where
)
163 struct token
*token
= *where
;
164 if (token_type(token
) != TOKEN_UNTAINT
)
167 token
->ident
->tainted
= 0;
169 } while (token_type(token
) == TOKEN_UNTAINT
);
174 static void expand_list(struct token
**list
)
177 while (!eof_token(next
= scan_next(list
))) {
178 if (token_type(next
) != TOKEN_IDENT
|| expand_one_symbol(list
))
183 static struct token
*collect_arg(struct token
*prev
, int vararg
, struct position
*pos
)
185 struct token
**p
= &prev
->next
;
189 while (!eof_token(next
= scan_next(p
))) {
190 if (match_op(next
, '(')) {
192 } else if (match_op(next
, ')')) {
195 } else if (match_op(next
, ',') && !nesting
&& !vararg
) {
198 next
->pos
.stream
= pos
->stream
;
199 next
->pos
.line
= pos
->line
;
200 next
->pos
.pos
= pos
->pos
;
203 *p
= &eof_token_entry
;
208 * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
213 struct token
*expanded
;
220 static int collect_arguments(struct token
*start
, struct token
*arglist
, struct arg
*args
, struct token
*what
)
222 int wanted
= arglist
->count
.normal
;
223 struct token
*next
= NULL
;
226 arglist
= arglist
->next
; /* skip counter */
229 next
= collect_arg(start
, 0, &what
->pos
);
232 if (!eof_token(start
->next
) || !match_op(next
, ')')) {
237 for (count
= 0; count
< wanted
; count
++) {
238 struct argcount
*p
= &arglist
->next
->count
;
239 next
= collect_arg(start
, p
->vararg
, &what
->pos
);
240 arglist
= arglist
->next
->next
;
243 args
[count
].arg
= start
->next
;
244 args
[count
].n_normal
= p
->normal
;
245 args
[count
].n_quoted
= p
->quoted
;
246 args
[count
].n_str
= p
->str
;
247 if (match_op(next
, ')')) {
253 if (count
== wanted
&& !match_op(next
, ')'))
255 if (count
== wanted
- 1) {
256 struct argcount
*p
= &arglist
->next
->count
;
259 args
[count
].arg
= NULL
;
260 args
[count
].n_normal
= p
->normal
;
261 args
[count
].n_quoted
= p
->quoted
;
262 args
[count
].n_str
= p
->str
;
264 if (count
< wanted
- 1)
267 what
->next
= next
->next
;
271 sparse_error(what
->pos
, "macro \"%s\" requires %d arguments, but only %d given",
272 show_token(what
), wanted
, count
);
275 while (match_op(next
, ',')) {
276 next
= collect_arg(next
, 0, &what
->pos
);
281 sparse_error(what
->pos
, "macro \"%s\" passed %d arguments, but takes just %d",
282 show_token(what
), count
, wanted
);
285 sparse_error(what
->pos
, "unterminated argument list invoking macro \"%s\"",
288 what
->next
= next
->next
;
292 static struct token
*dup_list(struct token
*list
)
295 struct token
**p
= &res
;
297 while (!eof_token(list
)) {
298 struct token
*newtok
= __alloc_token(0);
307 static struct token
*stringify(struct token
*arg
)
309 const char *s
= show_token_sequence(arg
);
310 int size
= strlen(s
)+1;
311 struct token
*token
= __alloc_token(0);
312 struct string
*string
= __alloc_string(size
);
314 memcpy(string
->data
, s
, size
);
315 string
->length
= size
;
316 token
->pos
= arg
->pos
;
317 token_type(token
) = TOKEN_STRING
;
318 token
->string
= string
;
319 token
->next
= &eof_token_entry
;
323 static void expand_arguments(int count
, struct arg
*args
)
326 for (i
= 0; i
< count
; i
++) {
327 struct token
*arg
= args
[i
].arg
;
329 arg
= &eof_token_entry
;
331 args
[i
].str
= stringify(arg
);
332 if (args
[i
].n_normal
) {
333 if (!args
[i
].n_quoted
) {
334 args
[i
].expanded
= arg
;
336 } else if (eof_token(arg
)) {
337 args
[i
].expanded
= arg
;
339 args
[i
].expanded
= dup_list(arg
);
341 expand_list(&args
[i
].expanded
);
347 * Possibly valid combinations:
348 * - ident + ident -> ident
349 * - ident + number -> ident unless number contains '.', '+' or '-'.
350 * - number + number -> number
351 * - number + ident -> number
352 * - number + '.' -> number
353 * - number + '+' or '-' -> number, if number used to end on [eEpP].
354 * - '.' + number -> number, if number used to start with a digit.
355 * - special + special -> either special or an error.
357 static enum token_type
combine(struct token
*left
, struct token
*right
, char *p
)
360 enum token_type t1
= token_type(left
), t2
= token_type(right
);
362 if (t1
!= TOKEN_IDENT
&& t1
!= TOKEN_NUMBER
&& t1
!= TOKEN_SPECIAL
)
365 if (t2
!= TOKEN_IDENT
&& t2
!= TOKEN_NUMBER
&& t2
!= TOKEN_SPECIAL
)
368 strcpy(p
, show_token(left
));
369 strcat(p
, show_token(right
));
375 if (t1
== TOKEN_IDENT
) {
376 if (t2
== TOKEN_SPECIAL
)
378 if (t2
== TOKEN_NUMBER
&& strpbrk(p
, "+-."))
383 if (t1
== TOKEN_NUMBER
) {
384 if (t2
== TOKEN_SPECIAL
) {
385 switch (right
->special
) {
389 if (strchr("eEpP", p
[len
- 2]))
398 if (p
[0] == '.' && isdigit((unsigned char)p
[1]))
401 return TOKEN_SPECIAL
;
404 static int merge(struct token
*left
, struct token
*right
)
406 static char buffer
[512];
409 switch (combine(left
, right
, buffer
)) {
411 left
->ident
= built_in_ident(buffer
);
412 left
->pos
.noexpand
= 0;
416 char *number
= __alloc_bytes(strlen(buffer
) + 1);
417 memcpy(number
, buffer
, strlen(buffer
) + 1);
418 token_type(left
) = TOKEN_NUMBER
; /* could be . + num */
419 left
->number
= number
;
424 if (buffer
[2] && buffer
[3])
426 for (n
= SPECIAL_BASE
; n
< SPECIAL_ARG_SEPARATOR
; n
++) {
427 if (!memcmp(buffer
, combinations
[n
-SPECIAL_BASE
], 3)) {
435 sparse_error(left
->pos
, "'##' failed: concatenation is not a valid token");
439 static struct token
*dup_token(struct token
*token
, struct position
*streampos
, struct position
*pos
)
441 struct token
*alloc
= alloc_token(streampos
);
442 token_type(alloc
) = token_type(token
);
443 alloc
->pos
.newline
= pos
->newline
;
444 alloc
->pos
.whitespace
= pos
->whitespace
;
445 alloc
->number
= token
->number
;
446 alloc
->pos
.noexpand
= token
->pos
.noexpand
;
450 static struct token
**copy(struct token
**where
, struct token
*list
, int *count
)
452 int need_copy
= --*count
;
453 while (!eof_token(list
)) {
456 token
= dup_token(list
, &list
->pos
, &list
->pos
);
459 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
->tainted
)
460 token
->pos
.noexpand
= 1;
462 where
= &token
->next
;
465 *where
= &eof_token_entry
;
469 static struct token
**substitute(struct token
**list
, struct token
*body
, struct arg
*args
)
471 struct token
*token
= *list
;
472 struct position
*base_pos
= &token
->pos
;
473 struct position
*pos
= base_pos
;
475 enum {Normal
, Placeholder
, Concat
} state
= Normal
;
477 for (; !eof_token(body
); body
= body
->next
, pos
= &body
->pos
) {
478 struct token
*added
, *arg
;
481 switch (token_type(body
)) {
482 case TOKEN_GNU_KLUDGE
:
484 * GNU kludge: if we had <comma>##<vararg>, behaviour
485 * depends on whether we had enough arguments to have
486 * a vararg. If we did, ## is just ignored. Otherwise
487 * both , and ## are ignored. Comma should come from
488 * the body of macro and not be an argument of earlier
491 if (!args
[body
->next
->argnum
].arg
)
493 added
= dup_token(body
, base_pos
, pos
);
494 token_type(added
) = TOKEN_SPECIAL
;
498 case TOKEN_STR_ARGUMENT
:
499 arg
= args
[body
->argnum
].str
;
500 count
= &args
[body
->argnum
].n_str
;
503 case TOKEN_QUOTED_ARGUMENT
:
504 arg
= args
[body
->argnum
].arg
;
505 count
= &args
[body
->argnum
].n_quoted
;
506 if (!arg
|| eof_token(arg
)) {
515 case TOKEN_MACRO_ARGUMENT
:
516 arg
= args
[body
->argnum
].expanded
;
517 count
= &args
[body
->argnum
].n_normal
;
518 if (eof_token(arg
)) {
523 tail
= copy(&added
, arg
, count
);
524 added
->pos
.newline
= pos
->newline
;
525 added
->pos
.whitespace
= pos
->whitespace
;
529 if (state
== Placeholder
)
536 added
= dup_token(body
, base_pos
, pos
);
537 if (added
->ident
->tainted
)
538 added
->pos
.noexpand
= 1;
543 added
= dup_token(body
, base_pos
, pos
);
549 * if we got to doing real concatenation, we already have
550 * added something into the list, so containing_token() is OK.
552 if (state
== Concat
&& merge(containing_token(list
), added
)) {
554 if (tail
!= &added
->next
)
562 *list
= &eof_token_entry
;
566 static int expand(struct token
**list
, struct symbol
*sym
)
569 struct token
*token
= *list
;
570 struct ident
*expanding
= token
->ident
;
572 int nargs
= sym
->arglist
? sym
->arglist
->count
.normal
: 0;
573 struct arg args
[nargs
];
575 if (expanding
->tainted
) {
576 token
->pos
.noexpand
= 1;
581 if (!match_op(scan_next(&token
->next
), '('))
583 if (!collect_arguments(token
->next
, sym
->arglist
, args
, token
))
585 expand_arguments(nargs
, args
);
588 expanding
->tainted
= 1;
591 tail
= substitute(list
, sym
->expansion
, args
);
597 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
600 static char buffer
[256];
604 while (!eof_token(token
) && !match_op(token
, endop
)) {
606 const char *val
= token
->string
->data
;
607 if (token_type(token
) != TOKEN_STRING
)
608 val
= show_token(token
);
610 memcpy(ptr
, val
, len
);
615 if (endop
&& !match_op(token
, endop
))
616 sparse_error(start
->pos
, "expected '>' at end of filename");
620 static int already_tokenized(const char *path
)
623 struct stream
*s
= input_streams
;
625 for (i
= input_stream_nr
; --i
>= 0; s
++) {
626 if (s
->constant
!= CONSTANT_FILE_YES
)
628 if (strcmp(path
, s
->name
))
630 if (s
->protect
&& !lookup_macro(s
->protect
))
637 /* Handle include of header files.
638 * The relevant options are made compatible with gcc. The only options that
639 * are not supported is -withprefix and friends.
641 * Three set of include paths are known:
642 * quote_includepath: Path to search when using #include "file.h"
643 * angle_includepath: Path to search when using #include <file.h>
644 * sys_includepath: Built-in include paths
646 * The above is implmented as one array with pointes
648 * quote_includepath ---> | |
652 * angle_includepath ---> | |
654 * sys_includepath ---> | |
659 * -I dir insert dir just before sys_includepath and move the rest
660 * -I- makes all dirs specified with -I before to quote dirs only and
661 * angle_includepath is set equal to sys_includepath.
662 * -nostdinc removes all sys dirs be storing NULL in entry pointed
663 * to by * sys_includepath. Note this will reset all dirs built-in and added
664 * before -nostdinc by -isystem and -dirafter
665 * -isystem dir adds dir where sys_includepath points adding this dir as
667 * -dirafter dir adds dir to the end of the list
670 static void set_stream_include_path(struct stream
*stream
)
672 const char *path
= stream
->path
;
674 const char *p
= strrchr(stream
->name
, '/');
677 int len
= p
- stream
->name
+ 1;
678 char *m
= malloc(len
+1);
679 /* This includes the final "/" */
680 memcpy(m
, stream
->name
, len
);
686 includepath
[0] = path
;
689 static int try_include(const char *path
, const char *filename
, int flen
, struct token
**where
, const char **next_path
)
692 int plen
= strlen(path
);
693 static char fullname
[PATH_MAX
];
695 memcpy(fullname
, path
, plen
);
696 if (plen
&& path
[plen
-1] != '/') {
697 fullname
[plen
] = '/';
700 memcpy(fullname
+plen
, filename
, flen
);
701 if (already_tokenized(fullname
))
703 fd
= open(fullname
, O_RDONLY
);
705 char * streamname
= __alloc_bytes(plen
+ flen
);
706 memcpy(streamname
, fullname
, plen
+ flen
);
707 *where
= tokenize(streamname
, fd
, *where
, next_path
);
714 static int do_include_path(const char **pptr
, struct token
**list
, struct token
*token
, const char *filename
, int flen
)
718 while ((path
= *pptr
++) != NULL
) {
719 if (!try_include(path
, filename
, flen
, list
, pptr
))
726 static void do_include(int local
, struct stream
*stream
, struct token
**list
, struct token
*token
, const char *filename
, const char **path
)
728 int flen
= strlen(filename
) + 1;
731 if (filename
[0] == '/') {
732 if (try_include("", filename
, flen
, list
, includepath
))
737 /* Dir of inputfile is first dir to search for quoted includes */
738 set_stream_include_path(stream
);
741 /* Do not search quote include if <> is in use */
742 path
= local
? quote_includepath
: angle_includepath
;
744 /* Check the standard include paths.. */
745 if (do_include_path(path
, list
, token
, filename
, flen
))
748 error_die(token
->pos
, "unable to open '%s'", filename
);
751 static int free_preprocessor_line(struct token
*token
)
754 struct token
*free
= token
;
757 } while (token_type(token
) != TOKEN_EOF
);
761 static int handle_include_path(struct stream
*stream
, struct token
**list
, struct token
*token
, const char **path
)
763 const char *filename
;
769 if (!match_op(next
, '<')) {
770 expand_list(&token
->next
);
773 if (match_op(token
->next
, '<')) {
779 filename
= token_name_sequence(token
, expect
, token
);
780 do_include(!expect
, stream
, list
, token
, filename
, path
);
784 static int handle_include(struct stream
*stream
, struct token
**list
, struct token
*token
)
786 return handle_include_path(stream
, list
, token
, NULL
);
789 static int handle_include_next(struct stream
*stream
, struct token
**list
, struct token
*token
)
791 return handle_include_path(stream
, list
, token
, stream
->next_path
);
794 static int token_different(struct token
*t1
, struct token
*t2
)
798 if (token_type(t1
) != token_type(t2
))
801 switch (token_type(t1
)) {
803 different
= t1
->ident
!= t2
->ident
;
805 case TOKEN_ARG_COUNT
:
808 case TOKEN_GNU_KLUDGE
:
812 different
= strcmp(t1
->number
, t2
->number
);
815 different
= t1
->special
!= t2
->special
;
817 case TOKEN_MACRO_ARGUMENT
:
818 case TOKEN_QUOTED_ARGUMENT
:
819 case TOKEN_STR_ARGUMENT
:
820 different
= t1
->argnum
!= t2
->argnum
;
823 different
= t1
->character
!= t2
->character
;
826 struct string
*s1
, *s2
;
831 if (s1
->length
!= s2
->length
)
833 different
= memcmp(s1
->data
, s2
->data
, s1
->length
);
843 static int token_list_different(struct token
*list1
, struct token
*list2
)
848 if (!list1
|| !list2
)
850 if (token_different(list1
, list2
))
857 static inline void set_arg_count(struct token
*token
)
859 token_type(token
) = TOKEN_ARG_COUNT
;
860 token
->count
.normal
= token
->count
.quoted
=
861 token
->count
.str
= token
->count
.vararg
= 0;
864 static struct token
*parse_arguments(struct token
*list
)
866 struct token
*arg
= list
->next
, *next
= list
;
867 struct argcount
*count
= &list
->count
;
871 if (match_op(arg
, ')')) {
873 list
->next
= &eof_token_entry
;
877 while (token_type(arg
) == TOKEN_IDENT
) {
878 if (arg
->ident
== &__VA_ARGS___ident
)
880 if (!++count
->normal
)
884 if (match_op(next
, ',')) {
890 if (match_op(next
, ')')) {
893 arg
->next
->next
= &eof_token_entry
;
897 /* normal cases are finished here */
899 if (match_op(next
, SPECIAL_ELLIPSIS
)) {
900 if (match_op(next
->next
, ')')) {
902 next
->count
.vararg
= 1;
904 arg
->next
->next
= &eof_token_entry
;
912 if (eof_token(next
)) {
920 if (match_op(arg
, SPECIAL_ELLIPSIS
)) {
922 token_type(arg
) = TOKEN_IDENT
;
923 arg
->ident
= &__VA_ARGS___ident
;
924 if (!match_op(next
, ')'))
926 if (!++count
->normal
)
929 next
->count
.vararg
= 1;
931 arg
->next
->next
= &eof_token_entry
;
935 if (eof_token(arg
)) {
939 if (match_op(arg
, ','))
946 sparse_error(arg
->pos
, "parameter name missing");
949 sparse_error(arg
->pos
, "\"%s\" may not appear in macro parameter list",
953 sparse_error(arg
->pos
, "missing ')' in macro parameter list");
956 sparse_error(arg
->pos
, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
959 sparse_error(arg
->pos
, "too many arguments in macro definition");
963 static int try_arg(struct token
*token
, enum token_type type
, struct token
*arglist
)
965 struct ident
*ident
= token
->ident
;
968 if (!arglist
|| token_type(token
) != TOKEN_IDENT
)
971 arglist
= arglist
->next
;
973 for (nr
= 0; !eof_token(arglist
); nr
++, arglist
= arglist
->next
->next
) {
974 if (arglist
->ident
== ident
) {
975 struct argcount
*count
= &arglist
->next
->count
;
979 token_type(token
) = type
;
981 case TOKEN_MACRO_ARGUMENT
:
984 case TOKEN_QUOTED_ARGUMENT
:
991 return count
->vararg
? 2 : 1;
992 token_type(token
) = TOKEN_ERROR
;
999 static struct token
*parse_expansion(struct token
*expansion
, struct token
*arglist
, struct ident
*name
)
1001 struct token
*token
= expansion
;
1003 struct token
*last
= NULL
;
1005 if (match_op(token
, SPECIAL_HASHHASH
))
1008 for (p
= &expansion
; !eof_token(token
); p
= &token
->next
, token
= *p
) {
1009 if (match_op(token
, '#')) {
1011 struct token
*next
= token
->next
;
1012 if (!try_arg(next
, TOKEN_STR_ARGUMENT
, arglist
))
1014 next
->pos
.whitespace
= token
->pos
.whitespace
;
1017 token
->pos
.noexpand
= 1;
1019 } else if (match_op(token
, SPECIAL_HASHHASH
)) {
1020 struct token
*next
= token
->next
;
1021 int arg
= try_arg(next
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1022 token_type(token
) = TOKEN_CONCAT
;
1026 if (arg
== 2 && last
&& match_op(last
, ',')) {
1027 token_type(last
) = TOKEN_GNU_KLUDGE
;
1030 } else if (match_op(next
, SPECIAL_HASHHASH
))
1032 else if (eof_token(next
))
1034 } else if (match_op(token
->next
, SPECIAL_HASHHASH
)) {
1035 try_arg(token
, TOKEN_QUOTED_ARGUMENT
, arglist
);
1037 try_arg(token
, TOKEN_MACRO_ARGUMENT
, arglist
);
1039 if (token_type(token
) == TOKEN_ERROR
)
1043 token
= alloc_token(&expansion
->pos
);
1044 token_type(token
) = TOKEN_UNTAINT
;
1045 token
->ident
= name
;
1051 sparse_error(token
->pos
, "'#' is not followed by a macro parameter");
1055 sparse_error(token
->pos
, "'##' cannot appear at the ends of macro expansion");
1058 sparse_error(token
->pos
, "too many instances of argument in body");
1062 static int do_handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
, int weak
)
1064 struct token
*arglist
, *expansion
;
1065 struct token
*left
= token
->next
;
1069 if (token_type(left
) != TOKEN_IDENT
) {
1070 sparse_error(token
->pos
, "expected identifier to 'define'");
1077 expansion
= left
->next
;
1078 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
1079 arglist
= expansion
;
1080 expansion
= parse_arguments(expansion
);
1085 expansion
= parse_expansion(expansion
, arglist
, name
);
1089 sym
= lookup_macro(name
);
1091 if (token_list_different(sym
->expansion
, expansion
) ||
1092 token_list_different(sym
->arglist
, arglist
)) {
1097 warning(left
->pos
, "preprocessor token %.*s redefined",
1098 name
->len
, name
->name
);
1099 info(sym
->pos
, "this was the original definition");
1101 /* Don't overwrite global defs */
1102 if (sym
->scope
!= file_scope
)
1109 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1110 bind_symbol(sym
, name
, NS_MACRO
);
1113 sym
->expansion
= expansion
;
1114 sym
->arglist
= arglist
;
1116 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1120 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1122 return do_handle_define(stream
, line
, token
, 0);
1125 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1127 return do_handle_define(stream
, line
, token
, 1);
1130 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1132 struct token
*left
= token
->next
;
1133 struct symbol
**sym
;
1135 if (token_type(left
) != TOKEN_IDENT
) {
1136 sparse_error(token
->pos
, "expected identifier to 'undef'");
1140 sym
= &left
->ident
->symbols
;
1142 struct symbol
*t
= *sym
;
1143 if (t
->namespace & (NS_MACRO
| NS_INVISIBLEMACRO
)) {
1144 t
->namespace = NS_INVISIBLEMACRO
;
1152 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1154 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1155 free_preprocessor_line(token
->next
);
1156 token
->next
= stream
->top_if
;
1157 stream
->top_if
= token
;
1158 if (false_nesting
|| true != 1)
1163 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1165 struct token
*next
= token
->next
;
1167 if (token_type(next
) == TOKEN_IDENT
) {
1168 arg
= token_defined(next
);
1170 dirty_stream(stream
);
1172 sparse_error(token
->pos
, "expected preprocessor identifier");
1175 return preprocessor_if(stream
, token
, arg
);
1178 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1180 struct token
*next
= token
->next
;
1182 if (token_type(next
) == TOKEN_IDENT
) {
1183 if (!stream
->dirty
&& !stream
->ifndef
) {
1184 if (!stream
->protect
) {
1185 stream
->ifndef
= token
;
1186 stream
->protect
= next
->ident
;
1187 } else if (stream
->protect
== next
->ident
) {
1188 stream
->ifndef
= token
;
1192 arg
= !token_defined(next
);
1194 dirty_stream(stream
);
1196 sparse_error(token
->pos
, "expected preprocessor identifier");
1200 return preprocessor_if(stream
, token
, arg
);
1204 * Expression handling for #if and #elif; it differs from normal expansion
1205 * due to special treatment of "defined".
1207 static int expression_value(struct token
**where
)
1209 struct expression
*expr
;
1211 struct token
**list
= where
, **beginning
= NULL
;
1215 while (!eof_token(p
= scan_next(list
))) {
1218 if (token_type(p
) != TOKEN_IDENT
)
1220 if (p
->ident
== &defined_ident
) {
1225 if (!expand_one_symbol(list
))
1227 if (token_type(p
) != TOKEN_IDENT
)
1229 token_type(p
) = TOKEN_ZERO_IDENT
;
1232 if (match_op(p
, '(')) {
1236 replace_with_defined(p
);
1241 if (token_type(p
) == TOKEN_IDENT
)
1245 replace_with_defined(p
);
1250 if (!match_op(p
, ')'))
1251 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1258 p
= constant_expression(*where
, &expr
);
1260 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1261 value
= get_expression_value(expr
);
1265 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1269 value
= expression_value(&token
->next
);
1271 dirty_stream(stream
);
1272 return preprocessor_if(stream
, token
, value
);
1275 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1277 struct token
*top_if
= stream
->top_if
;
1281 nesting_error(stream
);
1282 sparse_error(token
->pos
, "unmatched #elif within stream");
1286 if (token_type(top_if
) == TOKEN_ELSE
) {
1287 nesting_error(stream
);
1288 sparse_error(token
->pos
, "#elif after #else");
1294 dirty_stream(stream
);
1295 if (token_type(top_if
) != TOKEN_IF
)
1297 if (false_nesting
) {
1298 if (expression_value(&token
->next
))
1302 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1307 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1309 struct token
*top_if
= stream
->top_if
;
1313 nesting_error(stream
);
1314 sparse_error(token
->pos
, "unmatched #else within stream");
1318 if (token_type(top_if
) == TOKEN_ELSE
) {
1319 nesting_error(stream
);
1320 sparse_error(token
->pos
, "#else after #else");
1322 if (false_nesting
) {
1323 if (token_type(top_if
) == TOKEN_IF
)
1328 token_type(top_if
) = TOKEN_ELSE
;
1332 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1334 struct token
*top_if
= stream
->top_if
;
1337 nesting_error(stream
);
1338 sparse_error(token
->pos
, "unmatched #endif in stream");
1343 stream
->top_if
= top_if
->next
;
1344 __free_token(top_if
);
1348 static const char *show_token_sequence(struct token
*token
)
1350 static char buffer
[1024];
1356 while (!eof_token(token
)) {
1357 const char *val
= show_token(token
);
1358 int len
= strlen(val
);
1360 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1361 sparse_error(token
->pos
, "too long token expansion");
1367 memcpy(ptr
, val
, len
);
1369 token
= token
->next
;
1370 whitespace
= token
->pos
.whitespace
;
1376 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1378 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1382 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1384 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
));
1388 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1391 * Do we have any non-system includes?
1392 * Clear them out if so..
1394 *sys_includepath
= NULL
;
1398 static void add_path_entry(struct token
*token
, const char *path
, const char ***where
, const char **new_path
)
1403 /* Need one free entry.. */
1404 if (includepath
[INCLUDEPATHS
-2])
1405 error_die(token
->pos
, "too many include path entries");
1407 /* check that this is not a duplicate */
1410 if (strcmp(*dst
, path
) == 0)
1419 * Move them all up starting at dst,
1420 * insert the new entry..
1423 const char *tmp
= *dst
;
1432 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1435 token
= token
->next
;
1436 if (eof_token(token
))
1438 if (token_type(token
) != TOKEN_STRING
) {
1439 warning(token
->pos
, "expected path string");
1442 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
+ 1);
1446 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1449 token
= token
->next
;
1450 if (eof_token(token
))
1452 if (token_type(token
) != TOKEN_STRING
) {
1453 sparse_error(token
->pos
, "expected path string");
1456 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
);
1460 /* Add to end on includepath list - no pointer updates */
1461 static void add_dirafter_entry(struct token
*token
, const char *path
)
1463 const char **dst
= includepath
;
1465 /* Need one free entry.. */
1466 if (includepath
[INCLUDEPATHS
-2])
1467 error_die(token
->pos
, "too many include path entries");
1469 /* Add to the end */
1477 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1480 token
= token
->next
;
1481 if (eof_token(token
))
1483 if (token_type(token
) != TOKEN_STRING
) {
1484 sparse_error(token
->pos
, "expected path string");
1487 add_dirafter_entry(token
, token
->string
->data
);
1491 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1496 * Split the include path. Any directories specified with `-I'
1497 * options before `-I-' are searched only for headers requested with
1498 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1499 * If additional directories are specified with `-I' options after
1500 * the `-I-', those directories are searched for all `#include'
1502 * In addition, `-I-' inhibits the use of the directory of the current
1503 * file directory as the first search directory for `#include "FILE"'.
1505 quote_includepath
= includepath
+1;
1506 angle_includepath
= sys_includepath
;
1511 * We replace "#pragma xxx" with "__pragma__" in the token
1512 * stream. Just as an example.
1514 * We'll just #define that away for now, but the theory here
1515 * is that we can use this to insert arbitrary token sequences
1516 * to turn the pragma's into internal front-end sequences for
1517 * when we actually start caring about them.
1519 * So eventually this will turn into some kind of extended
1520 * __attribute__() like thing, except called __pragma__(xxx).
1522 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1524 struct token
*next
= *line
;
1526 token
->ident
= &pragma_ident
;
1527 token
->pos
.newline
= 1;
1528 token
->pos
.whitespace
= 1;
1536 * We ignore #line for now.
1538 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1543 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1545 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1550 static void init_preprocessor(void)
1553 int stream
= init_stream("preprocessor", -1, includepath
);
1556 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1558 { "define", handle_define
},
1559 { "weak_define",handle_weak_define
},
1560 { "undef", handle_undef
},
1561 { "warning", handle_warning
},
1562 { "error", handle_error
},
1563 { "include", handle_include
},
1564 { "include_next",handle_include_next
},
1565 { "pragma", handle_pragma
},
1566 { "line", handle_line
},
1568 // our internal preprocessor tokens
1569 { "nostdinc", handle_nostdinc
},
1570 { "add_include", handle_add_include
},
1571 { "add_isystem", handle_add_isystem
},
1572 { "add_dirafter", handle_add_dirafter
},
1573 { "split_include", handle_split_include
},
1575 { "ifdef", handle_ifdef
},
1576 { "ifndef", handle_ifndef
},
1577 { "else", handle_else
},
1578 { "endif", handle_endif
},
1579 { "if", handle_if
},
1580 { "elif", handle_elif
},
1583 for (i
= 0; i
< (sizeof (normal
) / sizeof (normal
[0])); i
++) {
1585 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1586 sym
->handler
= normal
[i
].handler
;
1589 for (i
= 0; i
< (sizeof (special
) / sizeof (special
[0])); i
++) {
1591 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1592 sym
->handler
= special
[i
].handler
;
1598 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1600 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1601 struct token
*token
= start
->next
;
1604 if (eof_token(token
))
1607 if (token_type(token
) == TOKEN_IDENT
) {
1608 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1610 handler
= sym
->handler
;
1611 is_normal
= sym
->normal
;
1613 handler
= handle_nondirective
;
1615 } else if (token_type(token
) == TOKEN_NUMBER
) {
1616 handler
= handle_line
;
1618 handler
= handle_nondirective
;
1622 dirty_stream(stream
);
1626 if (!handler(stream
, line
, token
)) /* all set */
1630 free_preprocessor_line(token
);
1633 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1635 struct token
*start
= *line
, *next
;
1636 struct token
**tp
= &start
->next
;
1640 if (next
->pos
.newline
)
1645 *tp
= &eof_token_entry
;
1646 handle_preprocessor_line(stream
, line
, start
);
1649 static void do_preprocess(struct token
**list
)
1653 while (!eof_token(next
= scan_next(list
))) {
1654 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1656 if (next
->pos
.newline
&& match_op(next
, '#')) {
1657 if (!next
->pos
.noexpand
) {
1658 preprocessor_line(stream
, list
);
1659 __free_token(next
); /* Free the '#' token */
1664 switch (token_type(next
)) {
1665 case TOKEN_STREAMEND
:
1666 if (stream
->top_if
) {
1667 nesting_error(stream
);
1668 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1669 stream
->top_if
= NULL
;
1673 stream
->constant
= CONSTANT_FILE_YES
;
1676 case TOKEN_STREAMBEGIN
:
1681 dirty_stream(stream
);
1682 if (false_nesting
) {
1688 if (token_type(next
) != TOKEN_IDENT
||
1689 expand_one_symbol(list
))
1695 struct token
* preprocess(struct token
*token
)
1698 init_preprocessor();
1699 do_preprocess(&token
);
1701 // Drop all expressions from pre-processing, they're not used any more.
1702 // This is not true when we have multiple files, though ;/
1703 // clear_expression_alloc();