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_UNDEF
);
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
);
121 sym
->used_in
= file_scope
;
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
);
150 sym
->used_in
= file_scope
;
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
)
294 struct token
*res
= NULL
;
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 implemented as one array with pointers
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 input file 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 attr
)
1064 struct token
*arglist
, *expansion
;
1065 struct token
*left
= token
->next
;
1070 if (token_type(left
) != TOKEN_IDENT
) {
1071 sparse_error(token
->pos
, "expected identifier to 'define'");
1078 expansion
= left
->next
;
1079 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
1080 arglist
= expansion
;
1081 expansion
= parse_arguments(expansion
);
1086 expansion
= parse_expansion(expansion
, arglist
, name
);
1091 sym
= lookup_symbol(name
, NS_MACRO
| NS_UNDEF
);
1095 if (attr
< sym
->attr
)
1098 clean
= (attr
== sym
->attr
&& sym
->namespace == NS_MACRO
);
1100 if (token_list_different(sym
->expansion
, expansion
) ||
1101 token_list_different(sym
->arglist
, arglist
)) {
1103 if ((clean
&& attr
== SYM_ATTR_NORMAL
)
1104 || sym
->used_in
== file_scope
) {
1105 warning(left
->pos
, "preprocessor token %.*s redefined",
1106 name
->len
, name
->name
);
1107 info(sym
->pos
, "this was the original definition");
1113 if (!sym
|| sym
->scope
!= file_scope
) {
1114 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1115 bind_symbol(sym
, name
, NS_MACRO
);
1120 sym
->expansion
= expansion
;
1121 sym
->arglist
= arglist
;
1122 __free_token(token
); /* Free the "define" token, but not the rest of the line */
1125 sym
->namespace = NS_MACRO
;
1126 sym
->used_in
= NULL
;
1132 static int handle_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1134 return do_handle_define(stream
, line
, token
, SYM_ATTR_NORMAL
);
1137 static int handle_weak_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1139 return do_handle_define(stream
, line
, token
, SYM_ATTR_WEAK
);
1142 static int handle_strong_define(struct stream
*stream
, struct token
**line
, struct token
*token
)
1144 return do_handle_define(stream
, line
, token
, SYM_ATTR_STRONG
);
1147 static int do_handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
, int attr
)
1149 struct token
*left
= token
->next
;
1152 if (token_type(left
) != TOKEN_IDENT
) {
1153 sparse_error(token
->pos
, "expected identifier to 'undef'");
1157 sym
= lookup_symbol(left
->ident
, NS_MACRO
| NS_UNDEF
);
1159 if (attr
< sym
->attr
)
1161 if (attr
== sym
->attr
&& sym
->namespace == NS_UNDEF
)
1163 } else if (attr
<= SYM_ATTR_NORMAL
)
1166 if (!sym
|| sym
->scope
!= file_scope
) {
1167 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
1168 bind_symbol(sym
, left
->ident
, NS_MACRO
);
1171 sym
->namespace = NS_UNDEF
;
1172 sym
->used_in
= NULL
;
1178 static int handle_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1180 return do_handle_undef(stream
, line
, token
, SYM_ATTR_NORMAL
);
1183 static int handle_strong_undef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1185 return do_handle_undef(stream
, line
, token
, SYM_ATTR_STRONG
);
1188 static int preprocessor_if(struct stream
*stream
, struct token
*token
, int true)
1190 token_type(token
) = false_nesting
? TOKEN_SKIP_GROUPS
: TOKEN_IF
;
1191 free_preprocessor_line(token
->next
);
1192 token
->next
= stream
->top_if
;
1193 stream
->top_if
= token
;
1194 if (false_nesting
|| true != 1)
1199 static int handle_ifdef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1201 struct token
*next
= token
->next
;
1203 if (token_type(next
) == TOKEN_IDENT
) {
1204 arg
= token_defined(next
);
1206 dirty_stream(stream
);
1208 sparse_error(token
->pos
, "expected preprocessor identifier");
1211 return preprocessor_if(stream
, token
, arg
);
1214 static int handle_ifndef(struct stream
*stream
, struct token
**line
, struct token
*token
)
1216 struct token
*next
= token
->next
;
1218 if (token_type(next
) == TOKEN_IDENT
) {
1219 if (!stream
->dirty
&& !stream
->ifndef
) {
1220 if (!stream
->protect
) {
1221 stream
->ifndef
= token
;
1222 stream
->protect
= next
->ident
;
1223 } else if (stream
->protect
== next
->ident
) {
1224 stream
->ifndef
= token
;
1228 arg
= !token_defined(next
);
1230 dirty_stream(stream
);
1232 sparse_error(token
->pos
, "expected preprocessor identifier");
1236 return preprocessor_if(stream
, token
, arg
);
1240 * Expression handling for #if and #elif; it differs from normal expansion
1241 * due to special treatment of "defined".
1243 static int expression_value(struct token
**where
)
1245 struct expression
*expr
;
1247 struct token
**list
= where
, **beginning
= NULL
;
1251 while (!eof_token(p
= scan_next(list
))) {
1254 if (token_type(p
) != TOKEN_IDENT
)
1256 if (p
->ident
== &defined_ident
) {
1261 if (!expand_one_symbol(list
))
1263 if (token_type(p
) != TOKEN_IDENT
)
1265 token_type(p
) = TOKEN_ZERO_IDENT
;
1268 if (match_op(p
, '(')) {
1272 replace_with_defined(p
);
1277 if (token_type(p
) == TOKEN_IDENT
)
1281 replace_with_defined(p
);
1286 if (!match_op(p
, ')'))
1287 sparse_error(p
->pos
, "missing ')' after \"defined\"");
1294 p
= constant_expression(*where
, &expr
);
1296 sparse_error(p
->pos
, "garbage at end: %s", show_token_sequence(p
));
1297 value
= get_expression_value(expr
);
1301 static int handle_if(struct stream
*stream
, struct token
**line
, struct token
*token
)
1305 value
= expression_value(&token
->next
);
1307 dirty_stream(stream
);
1308 return preprocessor_if(stream
, token
, value
);
1311 static int handle_elif(struct stream
* stream
, struct token
**line
, struct token
*token
)
1313 struct token
*top_if
= stream
->top_if
;
1317 nesting_error(stream
);
1318 sparse_error(token
->pos
, "unmatched #elif within stream");
1322 if (token_type(top_if
) == TOKEN_ELSE
) {
1323 nesting_error(stream
);
1324 sparse_error(token
->pos
, "#elif after #else");
1330 dirty_stream(stream
);
1331 if (token_type(top_if
) != TOKEN_IF
)
1333 if (false_nesting
) {
1334 if (expression_value(&token
->next
))
1338 token_type(top_if
) = TOKEN_SKIP_GROUPS
;
1343 static int handle_else(struct stream
*stream
, struct token
**line
, struct token
*token
)
1345 struct token
*top_if
= stream
->top_if
;
1349 nesting_error(stream
);
1350 sparse_error(token
->pos
, "unmatched #else within stream");
1354 if (token_type(top_if
) == TOKEN_ELSE
) {
1355 nesting_error(stream
);
1356 sparse_error(token
->pos
, "#else after #else");
1358 if (false_nesting
) {
1359 if (token_type(top_if
) == TOKEN_IF
)
1364 token_type(top_if
) = TOKEN_ELSE
;
1368 static int handle_endif(struct stream
*stream
, struct token
**line
, struct token
*token
)
1370 struct token
*top_if
= stream
->top_if
;
1373 nesting_error(stream
);
1374 sparse_error(token
->pos
, "unmatched #endif in stream");
1379 stream
->top_if
= top_if
->next
;
1380 __free_token(top_if
);
1384 static const char *show_token_sequence(struct token
*token
)
1386 static char buffer
[1024];
1392 while (!eof_token(token
)) {
1393 const char *val
= show_token(token
);
1394 int len
= strlen(val
);
1396 if (ptr
+ whitespace
+ len
>= buffer
+ sizeof(buffer
)) {
1397 sparse_error(token
->pos
, "too long token expansion");
1403 memcpy(ptr
, val
, len
);
1405 token
= token
->next
;
1406 whitespace
= token
->pos
.whitespace
;
1412 static int handle_warning(struct stream
*stream
, struct token
**line
, struct token
*token
)
1414 warning(token
->pos
, "%s", show_token_sequence(token
->next
));
1418 static int handle_error(struct stream
*stream
, struct token
**line
, struct token
*token
)
1420 sparse_error(token
->pos
, "%s", show_token_sequence(token
->next
));
1424 static int handle_nostdinc(struct stream
*stream
, struct token
**line
, struct token
*token
)
1427 * Do we have any non-system includes?
1428 * Clear them out if so..
1430 *sys_includepath
= NULL
;
1434 static void add_path_entry(struct token
*token
, const char *path
, const char ***where
, const char **new_path
)
1439 /* Need one free entry.. */
1440 if (includepath
[INCLUDEPATHS
-2])
1441 error_die(token
->pos
, "too many include path entries");
1443 /* check that this is not a duplicate */
1446 if (strcmp(*dst
, path
) == 0)
1455 * Move them all up starting at dst,
1456 * insert the new entry..
1459 const char *tmp
= *dst
;
1468 static int handle_add_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1471 token
= token
->next
;
1472 if (eof_token(token
))
1474 if (token_type(token
) != TOKEN_STRING
) {
1475 warning(token
->pos
, "expected path string");
1478 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
+ 1);
1482 static int handle_add_isystem(struct stream
*stream
, struct token
**line
, struct token
*token
)
1485 token
= token
->next
;
1486 if (eof_token(token
))
1488 if (token_type(token
) != TOKEN_STRING
) {
1489 sparse_error(token
->pos
, "expected path string");
1492 add_path_entry(token
, token
->string
->data
, &sys_includepath
, sys_includepath
);
1496 /* Add to end on includepath list - no pointer updates */
1497 static void add_dirafter_entry(struct token
*token
, const char *path
)
1499 const char **dst
= includepath
;
1501 /* Need one free entry.. */
1502 if (includepath
[INCLUDEPATHS
-2])
1503 error_die(token
->pos
, "too many include path entries");
1505 /* Add to the end */
1513 static int handle_add_dirafter(struct stream
*stream
, struct token
**line
, struct token
*token
)
1516 token
= token
->next
;
1517 if (eof_token(token
))
1519 if (token_type(token
) != TOKEN_STRING
) {
1520 sparse_error(token
->pos
, "expected path string");
1523 add_dirafter_entry(token
, token
->string
->data
);
1527 static int handle_split_include(struct stream
*stream
, struct token
**line
, struct token
*token
)
1532 * Split the include path. Any directories specified with `-I'
1533 * options before `-I-' are searched only for headers requested with
1534 * `#include "FILE"'; they are not searched for `#include <FILE>'.
1535 * If additional directories are specified with `-I' options after
1536 * the `-I-', those directories are searched for all `#include'
1538 * In addition, `-I-' inhibits the use of the directory of the current
1539 * file directory as the first search directory for `#include "FILE"'.
1541 quote_includepath
= includepath
+1;
1542 angle_includepath
= sys_includepath
;
1547 * We replace "#pragma xxx" with "__pragma__" in the token
1548 * stream. Just as an example.
1550 * We'll just #define that away for now, but the theory here
1551 * is that we can use this to insert arbitrary token sequences
1552 * to turn the pragmas into internal front-end sequences for
1553 * when we actually start caring about them.
1555 * So eventually this will turn into some kind of extended
1556 * __attribute__() like thing, except called __pragma__(xxx).
1558 static int handle_pragma(struct stream
*stream
, struct token
**line
, struct token
*token
)
1560 struct token
*next
= *line
;
1562 token
->ident
= &pragma_ident
;
1563 token
->pos
.newline
= 1;
1564 token
->pos
.whitespace
= 1;
1572 * We ignore #line for now.
1574 static int handle_line(struct stream
*stream
, struct token
**line
, struct token
*token
)
1579 static int handle_nondirective(struct stream
*stream
, struct token
**line
, struct token
*token
)
1581 sparse_error(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
1586 static void init_preprocessor(void)
1589 int stream
= init_stream("preprocessor", -1, includepath
);
1592 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1594 { "define", handle_define
},
1595 { "weak_define", handle_weak_define
},
1596 { "strong_define", handle_strong_define
},
1597 { "undef", handle_undef
},
1598 { "strong_undef", handle_strong_undef
},
1599 { "warning", handle_warning
},
1600 { "error", handle_error
},
1601 { "include", handle_include
},
1602 { "include_next", handle_include_next
},
1603 { "pragma", handle_pragma
},
1604 { "line", handle_line
},
1606 // our internal preprocessor tokens
1607 { "nostdinc", handle_nostdinc
},
1608 { "add_include", handle_add_include
},
1609 { "add_isystem", handle_add_isystem
},
1610 { "add_dirafter", handle_add_dirafter
},
1611 { "split_include", handle_split_include
},
1613 { "ifdef", handle_ifdef
},
1614 { "ifndef", handle_ifndef
},
1615 { "else", handle_else
},
1616 { "endif", handle_endif
},
1617 { "if", handle_if
},
1618 { "elif", handle_elif
},
1621 for (i
= 0; i
< (sizeof (normal
) / sizeof (normal
[0])); i
++) {
1623 sym
= create_symbol(stream
, normal
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1624 sym
->handler
= normal
[i
].handler
;
1627 for (i
= 0; i
< (sizeof (special
) / sizeof (special
[0])); i
++) {
1629 sym
= create_symbol(stream
, special
[i
].name
, SYM_PREPROCESSOR
, NS_PREPROCESSOR
);
1630 sym
->handler
= special
[i
].handler
;
1636 static void handle_preprocessor_line(struct stream
*stream
, struct token
**line
, struct token
*start
)
1638 int (*handler
)(struct stream
*, struct token
**, struct token
*);
1639 struct token
*token
= start
->next
;
1642 if (eof_token(token
))
1645 if (token_type(token
) == TOKEN_IDENT
) {
1646 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
1648 handler
= sym
->handler
;
1649 is_normal
= sym
->normal
;
1651 handler
= handle_nondirective
;
1653 } else if (token_type(token
) == TOKEN_NUMBER
) {
1654 handler
= handle_line
;
1656 handler
= handle_nondirective
;
1660 dirty_stream(stream
);
1664 if (!handler(stream
, line
, token
)) /* all set */
1668 free_preprocessor_line(token
);
1671 static void preprocessor_line(struct stream
*stream
, struct token
**line
)
1673 struct token
*start
= *line
, *next
;
1674 struct token
**tp
= &start
->next
;
1678 if (next
->pos
.newline
)
1683 *tp
= &eof_token_entry
;
1684 handle_preprocessor_line(stream
, line
, start
);
1687 static void do_preprocess(struct token
**list
)
1691 while (!eof_token(next
= scan_next(list
))) {
1692 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
1694 if (next
->pos
.newline
&& match_op(next
, '#')) {
1695 if (!next
->pos
.noexpand
) {
1696 preprocessor_line(stream
, list
);
1697 __free_token(next
); /* Free the '#' token */
1702 switch (token_type(next
)) {
1703 case TOKEN_STREAMEND
:
1704 if (stream
->top_if
) {
1705 nesting_error(stream
);
1706 sparse_error(stream
->top_if
->pos
, "unterminated preprocessor conditional");
1707 stream
->top_if
= NULL
;
1711 stream
->constant
= CONSTANT_FILE_YES
;
1714 case TOKEN_STREAMBEGIN
:
1719 dirty_stream(stream
);
1720 if (false_nesting
) {
1726 if (token_type(next
) != TOKEN_IDENT
||
1727 expand_one_symbol(list
))
1733 struct token
* preprocess(struct token
*token
)
1736 init_preprocessor();
1737 do_preprocess(&token
);
1739 // Drop all expressions from preprocessing, they're not used any more.
1740 // This is not true when we have multiple files, though ;/
1741 // clear_expression_alloc();