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.
9 * Licensed under the Open Software License version 1.1
25 #include "expression.h"
27 int preprocessing
= 0;
30 static int true_nesting
= 0;
31 static int false_nesting
= 0;
32 static struct token
*unmatched_if
= NULL
;
33 static int elif_ignore
[MAXNEST
];
34 #define if_nesting (true_nesting + false_nesting)
36 #define INCLUDEPATHS 32
37 const char *includepath
[INCLUDEPATHS
+1] = {
41 const char *sys_includepath
[] = {
47 const char *gcc_includepath
[] = {
48 "/usr/lib/gcc-lib/i386-redhat-linux/3.2.1/include",
49 "/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/include",
55 * This is stupid - the tokenizer already guarantees unique
56 * identifiers, so we should just compare identifier pointers
58 int match_string_ident(struct ident
*ident
, const char *str
)
60 return !str
[ident
->len
] && !memcmp(str
, ident
->name
, ident
->len
);
63 static struct token
*alloc_token(struct position
*pos
)
65 struct token
*token
= __alloc_token(0);
67 token
->pos
.stream
= pos
->stream
;
68 token
->pos
.line
= pos
->line
;
69 token
->pos
.pos
= pos
->pos
;
70 token
->pos
.whitespace
= 1;
74 static const char *show_token_sequence(struct token
*token
);
76 /* Head is one-before-list, and last is one-past-list */
77 static struct token
*for_each_ident(struct token
*parent
, struct token
*head
, struct token
*(*action
)(struct token
*parent
, struct token
*head
, struct token
*))
80 struct token
*next
= head
->next
;
82 /* Did we hit the end of the current expansion? */
86 if (token_type(next
) == TOKEN_IDENT
)
87 next
= action(parent
, head
, next
);
94 static struct token
*is_defined(struct token
*head
, struct token
*token
, struct token
*next
)
96 char *string
[] = { "0", "1" };
97 char *defined
= string
[lookup_symbol(token
->ident
, NS_PREPROCESSOR
) != NULL
];
98 struct token
*newtoken
= alloc_token(&token
->pos
);
100 token_type(newtoken
) = TOKEN_INTEGER
;
101 newtoken
->integer
= defined
;
102 newtoken
->next
= next
;
103 head
->next
= newtoken
;
108 struct token
*defined_one_symbol(struct token
*head
, struct token
*next
)
110 struct token
*token
= next
->next
;
111 struct token
*past
= token
->next
;
113 if (match_op(token
, '(')) {
116 if (!match_op(past
, ')'))
120 if (token_type(token
) == TOKEN_IDENT
)
121 return is_defined(head
, token
, past
);
125 /* Expand symbol 'sym' between 'head->next' and 'head->next->next' */
126 static struct token
*expand(struct token
*, struct token
*, struct symbol
*);
128 static void replace_with_string(struct token
*token
, const char *str
)
130 int size
= strlen(str
) + 1;
131 struct string
*s
= __alloc_string(size
);
134 memcpy(s
->data
, str
, size
);
135 token_type(token
) = TOKEN_STRING
;
139 static void replace_with_integer(struct token
*token
, unsigned int val
)
141 char *buf
= __alloc_bytes(10);
142 sprintf(buf
, "%d", val
);
143 token_type(token
) = TOKEN_INTEGER
;
144 token
->integer
= buf
;
147 struct token
*expand_one_symbol(struct token
*parent
, struct token
*head
, struct token
*token
)
152 /* Avoid recursive expansion */
154 while ((x
= x
->parent
) != NULL
) {
155 if (x
->ident
== parent
->ident
)
159 sym
= lookup_symbol(token
->ident
, NS_PREPROCESSOR
);
161 if (sym
->arglist
&& !match_op(token
->next
, '('))
163 return expand(parent
, head
, sym
);
165 if (!memcmp(token
->ident
->name
, "__LINE__", 9)) {
166 replace_with_integer(token
, token
->pos
.line
);
167 } else if (!memcmp(token
->ident
->name
, "__FILE__", 9)) {
168 replace_with_string(token
, (input_streams
+ token
->pos
.stream
)->name
);
169 } else if (!memcmp(token
->ident
->name
, "defined", 8)) {
170 return defined_one_symbol(head
, token
);
175 static struct token
*expand_list(struct token
*parent
, struct token
*head
)
177 return for_each_ident(parent
, head
, expand_one_symbol
);
180 static struct token
*find_argument_end(struct token
*start
)
184 while (!eof_token(start
)) {
185 struct token
*next
= start
->next
;
186 if (match_op(next
, '('))
188 else if (match_op(next
, ')')) {
190 start
->next
= &eof_token_entry
;
193 } else if (!nesting
&& match_op(next
, ','))
194 next
->special
= SPECIAL_ARG_SEPARATOR
;
200 static struct token
*dup_token(struct token
*token
, struct position
*pos
, int newline
)
202 struct token
*alloc
= alloc_token(pos
);
203 token_type(alloc
) = token_type(token
);
204 alloc
->pos
.line
= pos
->line
;
205 alloc
->pos
.newline
= newline
;
206 alloc
->integer
= token
->integer
;
210 static void insert(struct token
*token
, struct token
*prev
)
212 token
->next
= prev
->next
;
216 static struct token
* replace(struct token
*parent
, struct token
*token
, struct token
*prev
, struct token
*list
)
218 int newline
= token
->pos
.newline
;
220 prev
->next
= token
->next
;
221 while (!eof_token(list
) && !match_op(list
, SPECIAL_ARG_SEPARATOR
)) {
222 struct token
*newtok
= dup_token(list
, &token
->pos
, newline
);
223 newtok
->parent
= parent
;
225 insert(newtok
, prev
);
232 static struct token
*get_argument(int nr
, struct token
*args
)
236 while (!eof_token(args
)) {
237 if (match_op(args
, SPECIAL_ARG_SEPARATOR
))
246 static struct token
*stringify(struct token
*token
, struct token
*arg
)
248 const char *s
= show_token_sequence(arg
);
249 int size
= strlen(s
)+1;
250 struct token
*newtoken
= alloc_token(&token
->pos
);
251 struct string
*string
= __alloc_string(size
);
253 newtoken
->pos
.newline
= token
->pos
.newline
;
254 memcpy(string
->data
, s
, size
);
255 string
->length
= size
;
256 token_type(newtoken
) = TOKEN_STRING
;
257 newtoken
->string
= string
;
258 newtoken
->next
= &eof_token_entry
;
262 static int arg_number(struct token
*arglist
, struct ident
*ident
)
266 while (!eof_token(arglist
)) {
267 if (arglist
->ident
== ident
)
270 arglist
= arglist
->next
;
275 static struct token empty_arg_token
= { .pos
= { .type
= TOKEN_EOF
} };
277 static struct token
*expand_one_arg(struct token
*parent
, struct token
*head
, struct token
*token
,
278 struct token
*arglist
, struct token
*arguments
)
280 int nr
= arg_number(arglist
, token
->ident
);
281 struct token
*orig_head
= head
;
284 struct token
*arg
= get_argument(nr
, arguments
);
285 struct token
*last
= token
->next
;
286 token
->next
= &eof_token_entry
;
289 * Special case for gcc 'x ## arg' semantics: if 'arg' is empty
290 * then the 'x' goes away too.
292 if (match_op(head
, SPECIAL_HASHHASH
) && eof_token(arg
)) {
293 arg
= &empty_arg_token
;
294 empty_arg_token
.next
= &eof_token_entry
;
297 head
= replace(parent
, token
, head
, arg
);
298 if (!match_op(orig_head
, SPECIAL_HASHHASH
) && !match_op(last
, SPECIAL_HASHHASH
) && !match_op(orig_head
, '#'))
299 head
= expand_list(parent
, orig_head
);
306 static void expand_arguments(struct token
*parent
,
307 struct token
*token
, struct token
*head
,
308 struct token
*arguments
, struct token
*arglist
)
311 struct token
*next
= head
->next
;
313 /* Did we hit the end of the current expansion? */
317 if (match_op(next
, '#')) {
318 struct token
*nextnext
= next
->next
;
319 int nr
= arg_number(arglist
, nextnext
->ident
);
320 if (nextnext
!= head
&& nr
>= 0 && token_type(nextnext
) == TOKEN_IDENT
) {
321 struct token
*newtoken
= stringify(nextnext
, get_argument(nr
, arguments
));
322 replace(parent
, nextnext
, head
, newtoken
);
325 warn(next
->pos
, "'#' operation is not followed by argument name");
328 if (token_type(next
) == TOKEN_IDENT
)
329 next
= expand_one_arg(parent
, head
, next
, arglist
, arguments
);
336 * Possibly valid combinations:
337 * - anything + 'empty_arg_token' is empty.
338 * - ident + ident - combine (==ident)
339 * - ident + number - combine (==ident)
340 * - number + number - combine (==number)
341 * - number + ident - combine (==number)
342 * - string + string - leave as is, C will combine them anyway
343 * others cause an error and leave the two tokens as separate tokens.
345 static struct token
*hashhash(struct token
*head
, struct token
*first
, struct token
*second
)
347 static char buffer
[512], *p
;
348 struct token
*newtoken
;
349 static const char *src
;
352 first
->next
= second
;
355 * Special case for gcc 'x ## arg' semantics: if 'arg' is empty
356 * then the 'x' goes away too.
358 * See expand_one_arg.
360 if (token_type(second
) == TOKEN_EOF
) {
361 head
->next
= second
->next
;
366 switch (token_type(first
)) {
368 len
= strlen(first
->integer
);
369 src
= first
->integer
;
372 len
= first
->ident
->len
;
373 src
= first
->ident
->name
;
381 switch (token_type(second
)) {
383 len
= strlen(second
->integer
);
384 src
= second
->integer
;
387 len
= second
->ident
->len
;
388 src
= second
->ident
->name
;
397 newtoken
= alloc_token(&first
->pos
);
398 head
->next
= newtoken
;
399 token_type(newtoken
) = token_type(first
);
400 switch (token_type(newtoken
)) {
402 newtoken
->ident
= built_in_ident(buffer
);
405 newtoken
->integer
= __alloc_bytes(p
- buffer
);
406 memcpy(newtoken
->integer
, buffer
, p
- buffer
);
412 static void retokenize(struct token
*head
)
414 struct token
* next
= head
->next
;
415 struct token
* nextnext
= next
->next
;
416 struct token
* nextnextnext
= nextnext
->next
;
418 if (eof_token(next
) || eof_token(nextnext
))
422 if (eof_token(nextnextnext
))
425 if (match_op(nextnext
, SPECIAL_HASHHASH
)) {
426 struct token
*newtoken
= hashhash(head
, next
, nextnextnext
);
429 nextnext
= nextnextnext
->next
;
430 nextnextnext
= nextnext
->next
;
432 newtoken
->next
= nextnext
;
433 if (!eof_token(nextnext
))
440 nextnext
= nextnext
->next
;
441 nextnextnext
= nextnextnext
->next
;
445 static struct token
*expand(struct token
*parent
, struct token
*head
, struct symbol
*sym
)
447 struct token
*arguments
, *token
, *last
;
454 arguments
= last
->next
;
455 last
= find_argument_end(last
);
457 token
->next
= &eof_token_entry
;
459 /* Replace the token with the token expansion */
460 replace(parent
, token
, head
, sym
->expansion
);
462 /* Then, replace all the arguments with their expansions */
464 expand_arguments(parent
, token
, head
, arguments
, sym
->arglist
);
466 /* Re-tokenize the sequence if any ## token exists.. */
470 while (!eof_token(token
->next
))
476 static const char *token_name_sequence(struct token
*token
, int endop
, struct token
*start
)
479 static char buffer
[256];
483 while (!eof_token(token
) && !match_op(token
, endop
)) {
485 const char *val
= token
->string
->data
;
486 if (token_type(token
) != TOKEN_STRING
)
487 val
= show_token(token
);
489 memcpy(ptr
, val
, len
);
494 if (endop
&& !match_op(token
, endop
))
495 warn(start
->pos
, "expected '>' at end of filename");
499 static int try_include(const char *path
, int plen
, const char *filename
, int flen
, struct token
*head
)
502 static char fullname
[PATH_MAX
];
504 memcpy(fullname
, path
, plen
);
505 if (plen
&& path
[plen
-1] != '/') {
506 fullname
[plen
] = '/';
509 memcpy(fullname
+plen
, filename
, flen
);
510 fd
= open(fullname
, O_RDONLY
);
512 char * streamname
= __alloc_bytes(plen
+ flen
);
513 memcpy(streamname
, fullname
, plen
+ flen
);
514 head
->next
= tokenize(streamname
, fd
, head
->next
);
521 static int do_include_path(const char **pptr
, struct token
*head
, struct token
*token
, const char *filename
, int flen
)
525 while ((path
= *pptr
++) != NULL
) {
526 if (!try_include(path
, strlen(path
), filename
, flen
, head
))
534 static void do_include(struct stream
*stream
, struct token
*head
, struct token
*token
, const char *filename
)
538 int flen
= strlen(filename
) + 1;
540 /* Check the standard include paths.. */
541 if (do_include_path(includepath
, head
, token
, filename
, flen
))
543 if (do_include_path(sys_includepath
, head
, token
, filename
, flen
))
545 if (do_include_path(gcc_includepath
, head
, token
, filename
, flen
))
548 /* Check same directory as current stream.. */
550 slash
= strrchr(path
, '/');
552 if (try_include(path
, slash
-path
, filename
, flen
, head
))
556 error(token
->pos
, "unable to open '%s'", filename
);
559 static int handle_include(struct stream
*stream
, struct token
*head
, struct token
*token
)
561 const char *filename
;
565 if (stream
->constant
== -1)
566 stream
->constant
= 0;
571 if (!match_op(next
, '<')) {
572 expand_list(NULL
, token
);
577 filename
= token_name_sequence(token
, expect
, token
);
578 do_include(stream
, head
, token
, filename
);
582 static int token_list_different(struct token
*list1
, struct token
*list2
)
587 if (!list1
|| !list2
)
589 if (token_type(list1
) != token_type(list2
))
597 static int handle_define(struct stream
*stream
, struct token
*head
, struct token
*token
)
599 struct token
*arglist
, *expansion
;
600 struct token
*left
= token
->next
;
604 if (token_type(left
) != TOKEN_IDENT
) {
605 warn(head
->pos
, "expected identifier to 'define'");
613 expansion
= left
->next
;
614 if (!expansion
->pos
.whitespace
&& match_op(expansion
, '(')) {
616 while (!eof_token(expansion
)) {
617 struct token
*next
= expansion
->next
;
618 if (match_op(next
, ')')) {
619 // Terminate the arglist
620 expansion
->next
= &eof_token_entry
;
621 expansion
= next
->next
;
624 if (match_op(next
, ','))
625 expansion
->next
= next
->next
;
628 arglist
= arglist
->next
;
631 sym
= lookup_symbol(name
, NS_PREPROCESSOR
);
633 if (token_list_different(sym
->expansion
, expansion
) ||
634 token_list_different(sym
->arglist
, arglist
)) {
635 warn(left
->pos
, "preprocessor token redefined");
636 warn(sym
->pos
, "this was the original definition");
640 sym
= alloc_symbol(left
->pos
, SYM_NODE
);
641 bind_symbol(sym
, name
, NS_PREPROCESSOR
);
643 sym
->expansion
= expansion
;
644 sym
->arglist
= arglist
;
648 static int handle_undef(struct stream
*stream
, struct token
*head
, struct token
*token
)
650 struct token
*left
= token
->next
;
653 if (token_type(left
) != TOKEN_IDENT
) {
654 warn(head
->pos
, "expected identifier to 'undef'");
659 sym
= &left
->ident
->symbols
;
661 struct symbol
*t
= *sym
;
662 if (t
->namespace == NS_PREPROCESSOR
) {
671 static int preprocessor_if(struct token
*token
, int true)
674 unmatched_if
= token
;
675 elif_ignore
[if_nesting
] = false_nesting
|| true;
676 if (false_nesting
|| !true) {
684 static int token_defined(struct token
*token
)
686 if (token_type(token
) == TOKEN_IDENT
)
687 return lookup_symbol(token
->ident
, NS_PREPROCESSOR
) != NULL
;
689 warn(token
->pos
, "expected identifier for #if[n]def");
693 static int handle_ifdef(struct stream
*stream
, struct token
*head
, struct token
*token
)
695 return preprocessor_if(token
, token_defined(token
->next
));
698 static int handle_ifndef(struct stream
*stream
, struct token
*head
, struct token
*token
)
700 struct token
*next
= token
->next
;
701 if (stream
->constant
== -1) {
703 if (token_type(next
) == TOKEN_IDENT
) {
704 if (!stream
->protect
|| stream
->protect
== next
->ident
) {
706 stream
->protect
= next
->ident
;
707 stream
->nesting
= if_nesting
+1;
710 stream
->constant
= newconstant
;
712 return preprocessor_if(token
, !token_defined(next
));
715 static int expression_value(struct token
*head
)
717 struct expression
*expr
;
721 expand_list(NULL
, head
);
722 token
= constant_expression(head
->next
, &expr
);
723 if (!eof_token(token
))
724 warn(token
->pos
, "garbage at end: %s", show_token_sequence(token
));
725 value
= get_expression_value(expr
);
729 static int handle_if(struct stream
*stream
, struct token
*head
, struct token
*token
)
733 value
= expression_value(token
);
734 return preprocessor_if(token
, value
);
737 static int handle_elif(struct stream
* stream
, struct token
*head
, struct token
*token
)
739 if (stream
->nesting
== if_nesting
)
740 stream
->constant
= 0;
742 /* If this whole if-thing is if'ed out, an elif cannot help */
743 if (elif_ignore
[if_nesting
-1])
745 if (expression_value(token
)) {
748 elif_ignore
[if_nesting
-1] = 1;
757 warn(token
->pos
, "unmatched '#elif'");
761 static int handle_else(struct stream
*stream
, struct token
*head
, struct token
*token
)
763 if (stream
->nesting
== if_nesting
)
764 stream
->constant
= 0;
766 /* If this whole if-thing is if'ed out, an else cannot help */
767 if (elif_ignore
[if_nesting
-1])
771 elif_ignore
[if_nesting
-1] = 1;
779 warn(token
->pos
, "unmatched #else");
783 static int handle_endif(struct stream
*stream
, struct token
*head
, struct token
*token
)
785 if (stream
->constant
== -2 && stream
->nesting
== if_nesting
)
786 stream
->constant
= -1;
796 warn(token
->pos
, "unmatched #endif");
800 static const char *show_token_sequence(struct token
*token
)
802 static char buffer
[256];
808 while (!eof_token(token
) && !match_op(token
, SPECIAL_ARG_SEPARATOR
)) {
809 const char *val
= show_token(token
);
810 int len
= strlen(val
);
813 memcpy(ptr
, val
, len
);
816 whitespace
= token
->pos
.whitespace
;
822 static int handle_warning(struct stream
*stream
, struct token
*head
, struct token
*token
)
826 warn(token
->pos
, "%s", show_token_sequence(token
->next
));
830 static int handle_error(struct stream
*stream
, struct token
*head
, struct token
*token
)
834 warn(token
->pos
, "%s", show_token_sequence(token
->next
));
838 static int handle_nostdinc(struct stream
*stream
, struct token
*head
, struct token
*token
)
842 includepath
[0] = NULL
;
846 static void add_path_entry(struct token
*token
, const char *path
)
850 for (i
= 0; i
< INCLUDEPATHS
; i
++) {
851 if (!includepath
[i
]) {
852 includepath
[i
] = path
;
853 includepath
[i
+1] = NULL
;
857 warn(token
->pos
, "too many include path entries");
860 static int handle_add_include(struct stream
*stream
, struct token
*head
, struct token
*token
)
864 if (eof_token(token
))
866 if (token_type(token
) != TOKEN_STRING
) {
867 warn(token
->pos
, "expected path string");
870 add_path_entry(token
, token
->string
->data
);
874 static int handle_preprocessor_command(struct stream
*stream
, struct token
*head
, struct ident
*ident
, struct token
*token
)
879 int (*handler
)(struct stream
*, struct token
*, struct token
*);
881 { "define", handle_define
},
882 { "undef", handle_undef
},
883 { "ifdef", handle_ifdef
},
884 { "ifndef", handle_ifndef
},
885 { "else", handle_else
},
886 { "endif", handle_endif
},
888 { "elif", handle_elif
},
889 { "warning", handle_warning
},
890 { "error", handle_error
},
891 { "include", handle_include
},
893 // our internal preprocessor tokens
894 { "nostdinc", handle_nostdinc
},
895 { "add_include", handle_add_include
},
898 for (i
= 0; i
< (sizeof (handlers
) / sizeof (handlers
[0])); i
++) {
899 if (match_string_ident(ident
, handlers
[i
].name
))
900 return handlers
[i
].handler(stream
, head
, token
);
905 static void handle_preprocessor_line(struct stream
*stream
, struct token
* head
, struct token
*token
)
910 if (token_type(token
) == TOKEN_IDENT
)
911 if (handle_preprocessor_command(stream
, head
, token
->ident
, token
))
913 warn(token
->pos
, "unrecognized preprocessor line '%s'", show_token_sequence(token
));
916 static void preprocessor_line(struct stream
*stream
, struct token
* head
)
918 struct token
*start
= head
->next
, *next
;
919 struct token
**tp
= &start
->next
;
923 if (next
->pos
.newline
)
928 *tp
= &eof_token_entry
;
929 handle_preprocessor_line(stream
, head
, start
->next
);
932 static void do_preprocess(struct token
*head
)
935 struct token
*next
= head
->next
;
936 struct stream
*stream
= input_streams
+ next
->pos
.stream
;
938 if (next
->pos
.newline
&& match_op(next
, '#')) {
939 preprocessor_line(stream
, head
);
944 head
->next
= next
->next
;
948 switch (token_type(next
)) {
949 case TOKEN_STREAMEND
:
950 if (stream
->constant
== -1 && stream
->protect
) {
951 stream
->constant
= 1;
954 case TOKEN_STREAMBEGIN
:
955 head
->next
= next
->next
;
959 next
= expand_one_symbol(next
, head
, next
);
963 * Any token expansion (even if it ended up being an
964 * empty expansion) in this stream implies it can't
967 stream
->constant
= 0;
971 } while (!eof_token(head
));
974 struct token
* preprocess(struct token
*token
)
976 struct token header
= { };
980 do_preprocess(&header
);
982 warn(unmatched_if
->pos
, "unmatched preprocessor conditional");
984 // Drop all expressions from pre-processing, they're not used any more.
985 clear_expression_alloc();