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
;
35 # define __GNUC_MINOR__ 95
36 # define __GNUC_PATCHLEVEL__ 0
39 int gcc_major
= __GNUC__
;
40 int gcc_minor
= __GNUC_MINOR__
;
41 int gcc_patchlevel
= __GNUC_PATCHLEVEL__
;
43 struct token
*skip_to(struct token
*token
, int op
)
45 while (!match_op(token
, op
) && !eof_token(token
))
50 struct token
*expect(struct token
*token
, int op
, const char *where
)
52 if (!match_op(token
, op
)) {
53 static struct token bad_token
;
54 if (token
!= &bad_token
) {
55 bad_token
.next
= token
;
56 warning(token
->pos
, "Expected %s %s", show_special(op
), where
);
57 warning(token
->pos
, "got %s", show_token(token
));
60 return skip_to(token
, op
);
66 unsigned int hexval(unsigned int c
)
74 retval
= c
- 'a' + 10;
77 retval
= c
- 'A' + 10;
83 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
85 static char buffer
[512];
88 vsprintf(buffer
, fmt
, args
);
89 name
= stream_name(pos
.stream
);
91 fprintf(stderr
, "%s:%d:%d: %s%s\n",
92 name
, pos
.line
, pos
.pos
, type
, buffer
);
95 static int max_warnings
= 100;
97 void info(struct position pos
, const char * fmt
, ...)
104 do_warn("", pos
, fmt
, args
);
108 void warning(struct position pos
, const char * fmt
, ...)
116 fmt
= "too many warnings";
119 do_warn("warning: ", pos
, fmt
, args
);
123 void error(struct position pos
, const char * fmt
, ...)
125 static int errors
= 0;
128 /* Shut up warnings after an error */
134 fmt
= "too many errors";
139 do_warn("error: ", pos
, fmt
, args
);
144 void error_die(struct position pos
, const char * fmt
, ...)
148 do_warn("error: ", pos
, fmt
, args
);
153 void die(const char *fmt
, ...)
156 static char buffer
[512];
159 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
162 fprintf(stderr
, "%s\n", buffer
);
166 static unsigned int pre_buffer_size
;
167 static char pre_buffer
[8192];
169 int Wdefault_bitfield_sign
= 0;
173 int Wundefined_preprocessor
= 0;
174 int Wptr_subtraction_blows
= 0;
175 int Wcast_to_address_space
= 0;
176 int Wtransparent_union
= 1;
181 void add_pre_buffer(const char *fmt
, ...)
187 size
= pre_buffer_size
;
188 size
+= vsnprintf(pre_buffer
+ size
,
189 sizeof(pre_buffer
) - size
,
191 pre_buffer_size
= size
;
195 static char **handle_switch_D(char *arg
, char **next
)
197 const char *name
= arg
+ 1;
198 const char *value
= "1";
204 if (isspace((unsigned char)c
) || c
== '=') {
210 add_pre_buffer("#define %s %s\n", name
, value
);
214 static char **handle_switch_E(char *arg
, char **next
)
220 static char **handle_switch_v(char *arg
, char **next
)
224 } while (*++arg
== 'v');
228 static char **handle_switch_I(char *arg
, char **next
)
234 add_pre_buffer("#split_include\n");
237 case '\0': /* Plain "-I" */
240 die("missing argument for -I option");
243 add_pre_buffer("#add_include \"%s/\"\n", path
);
248 static char **handle_switch_i(char *arg
, char **next
)
250 if (*next
&& !strcmp(arg
, "include")) {
251 char *name
= *++next
;
252 int fd
= open(name
, O_RDONLY
);
259 if (*next
&& !strcmp(arg
, "imacros")) {
260 char *name
= *++next
;
261 int fd
= open(name
, O_RDONLY
);
268 else if (*next
&& !strcmp(arg
, "isystem")) {
269 char *path
= *++next
;
271 die("missing argument for -isystem option");
272 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
277 static char **handle_switch_M(char *arg
, char **next
)
279 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
281 die("missing argument for -%s option", arg
);
287 static char **handle_switch_m(char *arg
, char **next
)
289 if (!strcmp(arg
, "m64")) {
291 max_int_alignment
= 8;
292 bits_in_pointer
= 64;
293 pointer_alignment
= 8;
298 static char **handle_switch_o(char *arg
, char **next
)
300 if (!strcmp (arg
, "o") && *next
)
301 return next
+ 1; // "-o foo"
303 return next
; // "-ofoo" or (bogus) terminal "-o"
306 static const struct warning
{
310 { "cast-to-as", &Wcast_to_address_space
},
311 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
312 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
313 { "undef", &Wundefined_preprocessor
},
314 { "bitwise", &Wbitwise
},
315 { "typesign", &Wtypesign
},
316 { "context", &Wcontext
},
317 { "transparent-union", &Wtransparent_union
},
321 static char **handle_switch_W(char *arg
, char **next
)
327 // Prefixes "no" and "no-" mean to turn warning off.
328 if (p
[0] == 'n' && p
[1] == 'o') {
335 for (i
= 0; i
< sizeof(warnings
) / sizeof(warnings
[0]); i
++) {
336 if (!strcmp(p
,warnings
[i
].name
)) {
337 *warnings
[i
].flag
= !no
;
346 static char **handle_switch_U(char *arg
, char **next
)
348 const char *name
= arg
+ 1;
349 add_pre_buffer ("#undef %s\n", name
);
353 static char **handle_switch_O(char *arg
, char **next
)
356 if (arg
[1] >= '0' && arg
[1] <= '9')
357 level
= arg
[1] - '0';
359 optimize_size
= arg
[1] == 's';
363 static char **handle_switch_f(char *arg
, char **next
)
368 if (!strncmp(arg
, "no-", 3)) {
372 /* handle switch here.. */
376 static char **handle_nostdinc(char *arg
, char **next
)
378 add_pre_buffer("#nostdinc\n");
382 static char **handle_dirafter(char *arg
, char **next
)
384 char *path
= *++next
;
386 die("missing argument for -dirafter option");
387 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
393 char **(*fn
)(char *, char**);
396 char **handle_switch(char *arg
, char **next
)
398 static struct switches cmd
[] = {
399 { "nostdinc", handle_nostdinc
},
400 { "dirafter", handle_dirafter
},
406 case 'D': return handle_switch_D(arg
, next
);
407 case 'E': return handle_switch_E(arg
, next
);
408 case 'I': return handle_switch_I(arg
, next
);
409 case 'i': return handle_switch_i(arg
, next
);
410 case 'M': return handle_switch_M(arg
, next
);
411 case 'm': return handle_switch_m(arg
, next
);
412 case 'o': return handle_switch_o(arg
, next
);
413 case 'U': return handle_switch_U(arg
, next
);
414 case 'v': return handle_switch_v(arg
, next
);
415 case 'W': return handle_switch_W(arg
, next
);
416 case 'O': return handle_switch_O(arg
, next
);
417 case 'f': return handle_switch_f(arg
, next
);
424 if (!strcmp(s
->name
, arg
))
425 return s
->fn(arg
, next
);
430 * Ignore unknown command line options:
431 * they're probably gcc switches
436 void declare_builtin_functions(void)
438 /* Gaah. gcc knows tons of builtin <string.h> functions */
439 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
440 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
441 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
442 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
443 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
444 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
445 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
446 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
448 /* And some random ones.. */
449 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
450 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
451 add_pre_buffer("extern void __builtin_trap(void);\n");
452 add_pre_buffer("extern int __builtin_ffs(int);\n");
453 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
456 void create_builtin_stream(void)
458 add_pre_buffer("#define __GNUC__ %d\n", gcc_major
);
459 add_pre_buffer("#define __GNUC_MINOR__ %d\n", gcc_minor
);
460 add_pre_buffer("#define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
461 add_pre_buffer("#define __extension__\n");
462 add_pre_buffer("#define __pragma__\n");
464 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
465 // solaris/sparc that is really "unsigned int" and for linux/x86_64
466 // it is "long unsigned int". In either case we can probably
467 // get away with this. We need the #ifndef as cgcc will define
468 // the right __SIZE_TYPE__.
469 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
470 add_pre_buffer("#weak_define __STDC__ 1\n");
472 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
473 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
474 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
475 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
476 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
477 add_pre_buffer("#define __builtin_va_end(arg)\n");
478 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
480 /* FIXME! We need to do these as special magic macros at expansion time! */
481 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
482 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
483 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
486 add_pre_buffer("#define __OPTIMIZE__ 1\n");
488 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
491 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
493 // Pre-process the stream
494 token
= preprocess(token
);
496 if (preprocess_only
) {
497 while (!eof_token(token
)) {
499 struct token
*next
= token
->next
;
500 const char *separator
= "";
501 if (next
->pos
.whitespace
)
503 if (next
->pos
.newline
) {
504 separator
= "\n\t\t\t\t\t";
505 prec
= next
->pos
.pos
;
509 printf("%s%.*s", show_token(token
), prec
, separator
);
517 // Parse the resulting C code
518 while (!eof_token(token
))
519 token
= external_declaration(token
, &translation_unit_used_list
);
520 return translation_unit_used_list
;
523 static struct symbol_list
*sparse_file(const char *filename
)
528 if (strcmp (filename
, "-") == 0) {
531 fd
= open(filename
, O_RDONLY
);
533 die("No such file: %s", filename
);
536 // Tokenize the input stream
537 token
= tokenize(filename
, fd
, NULL
, includepath
);
540 return sparse_tokenstream(token
);
544 * This handles the "-include" directive etc: we're in global
545 * scope, and all types/macros etc will affect all the following
548 * NOTE NOTE NOTE! "#undef" of anything in this stage will
549 * affect all subsequent files too, ie we can have non-local
550 * behaviour between files!
552 static void sparse_initial(void)
556 // Prepend any "include" file to the stream.
557 // We're in global scope, it will affect all files!
560 token
= tokenize(include
, include_fd
, NULL
, includepath
);
562 // Prepend the initial built-in stream
563 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
564 sparse_tokenstream(token
);
567 int sparse_initialize(int argc
, char **argv
)
572 // Initialize symbol stream first, so that we can add defines etc
581 if (arg
[0] == '-' && arg
[1]) {
582 args
= handle_switch(arg
+1, args
);
587 * Hacky hacky hacky: we re-use the argument space
588 * to save the filenames.
595 // Initialize type system
598 create_builtin_stream();
599 add_pre_buffer("#define __CHECKER__ 1\n");
600 if (!preprocess_only
)
601 declare_builtin_functions();
606 * Protect the initial token allocations, since
607 * they need to survive all the others
609 protect_token_alloc();
614 struct symbol_list
* sparse(char **argv
)
616 struct symbol_list
*res
;
617 char *filename
, *next
;
619 /* Clear previous symbol list */
620 translation_unit_used_list
= NULL
;
631 res
= sparse_file(filename
);
634 /* Drop the tokens for this file after parsing */
637 /* Evaluate the complete symbol list */
638 evaluate_symbol_list(res
);