2 * 'sparse' library helper routines.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
19 #include <sys/types.h>
26 #include "expression.h"
28 #include "linearize.h"
31 int verbose
, optimize
, optimize_size
, preprocessing
;
36 # define __GNUC_MINOR__ 95
37 # define __GNUC_PATCHLEVEL__ 0
40 int gcc_major
= __GNUC__
;
41 int gcc_minor
= __GNUC_MINOR__
;
42 int gcc_patchlevel
= __GNUC_PATCHLEVEL__
;
44 struct token
*skip_to(struct token
*token
, int op
)
46 while (!match_op(token
, op
) && !eof_token(token
))
51 struct token
*expect(struct token
*token
, int op
, const char *where
)
53 if (!match_op(token
, op
)) {
54 static struct token bad_token
;
55 if (token
!= &bad_token
) {
56 bad_token
.next
= token
;
57 sparse_error(token
->pos
, "Expected %s %s", show_special(op
), where
);
58 sparse_error(token
->pos
, "got %s", show_token(token
));
61 return skip_to(token
, op
);
67 unsigned int hexval(unsigned int c
)
75 retval
= c
- 'a' + 10;
78 retval
= c
- 'A' + 10;
84 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
86 static char buffer
[512];
89 vsprintf(buffer
, fmt
, args
);
90 name
= stream_name(pos
.stream
);
92 fprintf(stderr
, "%s:%d:%d: %s%s\n",
93 name
, pos
.line
, pos
.pos
, type
, buffer
);
96 static int max_warnings
= 100;
98 void info(struct position pos
, const char * fmt
, ...)
105 do_warn("", pos
, fmt
, args
);
109 void warning(struct position pos
, const char * fmt
, ...)
117 fmt
= "too many warnings";
120 do_warn("warning: ", pos
, fmt
, args
);
124 void sparse_error(struct position pos
, const char * fmt
, ...)
126 static int errors
= 0;
129 /* Shut up warnings after an error */
135 fmt
= "too many errors";
140 do_warn("error: ", pos
, fmt
, args
);
145 void error_die(struct position pos
, const char * fmt
, ...)
149 do_warn("error: ", pos
, fmt
, args
);
154 void die(const char *fmt
, ...)
157 static char buffer
[512];
160 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
163 fprintf(stderr
, "%s\n", buffer
);
167 static unsigned int pre_buffer_size
;
168 static char pre_buffer
[8192];
170 int Wdefault_bitfield_sign
= 0;
171 int Wone_bit_signed_bitfield
= 1;
175 int Wundefined_preprocessor
= 0;
176 int Wptr_subtraction_blows
= 0;
177 int Wcast_to_address_space
= 0;
179 int Wtransparent_union
= 1;
185 void add_pre_buffer(const char *fmt
, ...)
191 size
= pre_buffer_size
;
192 size
+= vsnprintf(pre_buffer
+ size
,
193 sizeof(pre_buffer
) - size
,
195 pre_buffer_size
= size
;
199 static char **handle_switch_D(char *arg
, char **next
)
201 const char *name
= arg
+ 1;
202 const char *value
= "1";
208 if (isspace((unsigned char)c
) || c
== '=') {
214 add_pre_buffer("#define %s %s\n", name
, value
);
218 static char **handle_switch_E(char *arg
, char **next
)
224 static char **handle_switch_v(char *arg
, char **next
)
228 } while (*++arg
== 'v');
232 static char **handle_switch_I(char *arg
, char **next
)
238 add_pre_buffer("#split_include\n");
241 case '\0': /* Plain "-I" */
244 die("missing argument for -I option");
247 add_pre_buffer("#add_include \"%s/\"\n", path
);
252 static char **handle_switch_i(char *arg
, char **next
)
254 if (*next
&& !strcmp(arg
, "include")) {
255 char *name
= *++next
;
256 int fd
= open(name
, O_RDONLY
);
263 if (*next
&& !strcmp(arg
, "imacros")) {
264 char *name
= *++next
;
265 int fd
= open(name
, O_RDONLY
);
272 else if (*next
&& !strcmp(arg
, "isystem")) {
273 char *path
= *++next
;
275 die("missing argument for -isystem option");
276 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
281 static char **handle_switch_M(char *arg
, char **next
)
283 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
285 die("missing argument for -%s option", arg
);
291 static char **handle_switch_m(char *arg
, char **next
)
293 if (!strcmp(arg
, "m64")) {
295 max_int_alignment
= 8;
296 bits_in_pointer
= 64;
297 pointer_alignment
= 8;
302 static char **handle_switch_o(char *arg
, char **next
)
304 if (!strcmp (arg
, "o") && *next
)
305 return next
+ 1; // "-o foo"
307 return next
; // "-ofoo" or (bogus) terminal "-o"
310 static const struct warning
{
314 { "cast-to-as", &Wcast_to_address_space
},
316 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield
},
317 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
318 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
319 { "undef", &Wundefined_preprocessor
},
320 { "bitwise", &Wbitwise
},
321 { "typesign", &Wtypesign
},
322 { "context", &Wcontext
},
323 { "transparent-union", &Wtransparent_union
},
327 static char **handle_switch_W(char *arg
, char **next
)
333 // Prefixes "no" and "no-" mean to turn warning off.
334 if (p
[0] == 'n' && p
[1] == 'o') {
341 for (i
= 0; i
< sizeof(warnings
) / sizeof(warnings
[0]); i
++) {
342 if (!strcmp(p
,warnings
[i
].name
)) {
343 *warnings
[i
].flag
= !no
;
352 static char **handle_switch_U(char *arg
, char **next
)
354 const char *name
= arg
+ 1;
355 add_pre_buffer ("#undef %s\n", name
);
359 static char **handle_switch_O(char *arg
, char **next
)
362 if (arg
[1] >= '0' && arg
[1] <= '9')
363 level
= arg
[1] - '0';
365 optimize_size
= arg
[1] == 's';
369 static char **handle_switch_f(char *arg
, char **next
)
374 if (!strncmp(arg
, "no-", 3)) {
378 /* handle switch here.. */
382 static char **handle_switch_G(char *arg
, char **next
)
384 if (!strcmp (arg
, "G") && *next
)
385 return next
+ 1; // "-G 0"
387 return next
; // "-G0" or (bogus) terminal "-G"
390 static char **handle_nostdinc(char *arg
, char **next
)
392 add_pre_buffer("#nostdinc\n");
396 static char **handle_dirafter(char *arg
, char **next
)
398 char *path
= *++next
;
400 die("missing argument for -dirafter option");
401 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
407 char **(*fn
)(char *, char**);
410 char **handle_switch(char *arg
, char **next
)
412 static struct switches cmd
[] = {
413 { "nostdinc", handle_nostdinc
},
414 { "dirafter", handle_dirafter
},
420 case 'D': return handle_switch_D(arg
, next
);
421 case 'E': return handle_switch_E(arg
, next
);
422 case 'I': return handle_switch_I(arg
, next
);
423 case 'i': return handle_switch_i(arg
, next
);
424 case 'M': return handle_switch_M(arg
, next
);
425 case 'm': return handle_switch_m(arg
, next
);
426 case 'o': return handle_switch_o(arg
, next
);
427 case 'U': return handle_switch_U(arg
, next
);
428 case 'v': return handle_switch_v(arg
, next
);
429 case 'W': return handle_switch_W(arg
, next
);
430 case 'O': return handle_switch_O(arg
, next
);
431 case 'f': return handle_switch_f(arg
, next
);
432 case 'G': return handle_switch_G(arg
, next
);
439 if (!strcmp(s
->name
, arg
))
440 return s
->fn(arg
, next
);
445 * Ignore unknown command line options:
446 * they're probably gcc switches
451 void declare_builtin_functions(void)
453 /* Gaah. gcc knows tons of builtin <string.h> functions */
454 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
455 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
456 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
457 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
458 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
459 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
460 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
461 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
462 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
464 /* And some random ones.. */
465 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
466 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
467 add_pre_buffer("extern void __builtin_trap(void);\n");
468 add_pre_buffer("extern int __builtin_ffs(int);\n");
469 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
472 void create_builtin_stream(void)
474 add_pre_buffer("#define __GNUC__ %d\n", gcc_major
);
475 add_pre_buffer("#define __GNUC_MINOR__ %d\n", gcc_minor
);
476 add_pre_buffer("#define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
477 add_pre_buffer("#define __extension__\n");
478 add_pre_buffer("#define __pragma__\n");
480 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
481 // solaris/sparc that is really "unsigned int" and for linux/x86_64
482 // it is "long unsigned int". In either case we can probably
483 // get away with this. We need the #ifndef as cgcc will define
484 // the right __SIZE_TYPE__.
485 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
486 add_pre_buffer("#weak_define __STDC__ 1\n");
488 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
489 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
490 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
491 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
492 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
493 add_pre_buffer("#define __builtin_va_end(arg)\n");
494 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
496 /* FIXME! We need to do these as special magic macros at expansion time! */
497 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
498 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
499 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
502 add_pre_buffer("#define __OPTIMIZE__ 1\n");
504 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
507 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
509 // Pre-process the stream
510 token
= preprocess(token
);
512 if (preprocess_only
) {
513 while (!eof_token(token
)) {
515 struct token
*next
= token
->next
;
516 const char *separator
= "";
517 if (next
->pos
.whitespace
)
519 if (next
->pos
.newline
) {
520 separator
= "\n\t\t\t\t\t";
521 prec
= next
->pos
.pos
;
525 printf("%s%.*s", show_token(token
), prec
, separator
);
533 // Parse the resulting C code
534 while (!eof_token(token
))
535 token
= external_declaration(token
, &translation_unit_used_list
);
536 return translation_unit_used_list
;
539 static struct symbol_list
*sparse_file(const char *filename
)
544 if (strcmp (filename
, "-") == 0) {
547 fd
= open(filename
, O_RDONLY
);
549 die("No such file: %s", filename
);
552 // Tokenize the input stream
553 token
= tokenize(filename
, fd
, NULL
, includepath
);
556 return sparse_tokenstream(token
);
560 * This handles the "-include" directive etc: we're in global
561 * scope, and all types/macros etc will affect all the following
564 * NOTE NOTE NOTE! "#undef" of anything in this stage will
565 * affect all subsequent files too, ie we can have non-local
566 * behaviour between files!
568 static struct symbol_list
*sparse_initial(void)
572 // Prepend any "include" file to the stream.
573 // We're in global scope, it will affect all files!
576 token
= tokenize(include
, include_fd
, NULL
, includepath
);
578 // Prepend the initial built-in stream
579 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
580 return sparse_tokenstream(token
);
583 struct symbol_list
*sparse_initialize(int argc
, char **argv
)
587 struct symbol_list
*list
;
589 // Initialize symbol stream first, so that we can add defines etc
598 if (arg
[0] == '-' && arg
[1]) {
599 args
= handle_switch(arg
+1, args
);
604 * Hacky hacky hacky: we re-use the argument space
605 * to save the filenames.
613 // Initialize type system
616 create_builtin_stream();
617 add_pre_buffer("#define __CHECKER__ 1\n");
618 if (!preprocess_only
)
619 declare_builtin_functions();
621 list
= sparse_initial();
624 * Protect the initial token allocations, since
625 * they need to survive all the others
627 protect_token_alloc();
632 struct symbol_list
* sparse(char **argv
)
634 struct symbol_list
*res
;
635 char *filename
, *next
;
637 /* Clear previous symbol list */
638 translation_unit_used_list
= NULL
;
649 res
= sparse_file(filename
);
652 /* Drop the tokens for this file after parsing */
655 /* Evaluate the complete symbol list */
656 evaluate_symbol_list(res
);