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;
174 int Wundefined_preprocessor
= 0;
175 int Wptr_subtraction_blows
= 0;
176 int Wcast_to_address_space
= 0;
177 int Wtransparent_union
= 1;
183 void add_pre_buffer(const char *fmt
, ...)
189 size
= pre_buffer_size
;
190 size
+= vsnprintf(pre_buffer
+ size
,
191 sizeof(pre_buffer
) - size
,
193 pre_buffer_size
= size
;
197 static char **handle_switch_D(char *arg
, char **next
)
199 const char *name
= arg
+ 1;
200 const char *value
= "1";
206 if (isspace((unsigned char)c
) || c
== '=') {
212 add_pre_buffer("#define %s %s\n", name
, value
);
216 static char **handle_switch_E(char *arg
, char **next
)
222 static char **handle_switch_v(char *arg
, char **next
)
226 } while (*++arg
== 'v');
230 static char **handle_switch_I(char *arg
, char **next
)
236 add_pre_buffer("#split_include\n");
239 case '\0': /* Plain "-I" */
242 die("missing argument for -I option");
245 add_pre_buffer("#add_include \"%s/\"\n", path
);
250 static char **handle_switch_i(char *arg
, char **next
)
252 if (*next
&& !strcmp(arg
, "include")) {
253 char *name
= *++next
;
254 int fd
= open(name
, O_RDONLY
);
261 if (*next
&& !strcmp(arg
, "imacros")) {
262 char *name
= *++next
;
263 int fd
= open(name
, O_RDONLY
);
270 else if (*next
&& !strcmp(arg
, "isystem")) {
271 char *path
= *++next
;
273 die("missing argument for -isystem option");
274 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
279 static char **handle_switch_M(char *arg
, char **next
)
281 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
283 die("missing argument for -%s option", arg
);
289 static char **handle_switch_m(char *arg
, char **next
)
291 if (!strcmp(arg
, "m64")) {
293 max_int_alignment
= 8;
294 bits_in_pointer
= 64;
295 pointer_alignment
= 8;
300 static char **handle_switch_o(char *arg
, char **next
)
302 if (!strcmp (arg
, "o") && *next
)
303 return next
+ 1; // "-o foo"
305 return next
; // "-ofoo" or (bogus) terminal "-o"
308 static const struct warning
{
312 { "cast-to-as", &Wcast_to_address_space
},
313 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
314 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
315 { "undef", &Wundefined_preprocessor
},
316 { "bitwise", &Wbitwise
},
317 { "typesign", &Wtypesign
},
318 { "context", &Wcontext
},
319 { "transparent-union", &Wtransparent_union
},
323 static char **handle_switch_W(char *arg
, char **next
)
329 // Prefixes "no" and "no-" mean to turn warning off.
330 if (p
[0] == 'n' && p
[1] == 'o') {
337 for (i
= 0; i
< sizeof(warnings
) / sizeof(warnings
[0]); i
++) {
338 if (!strcmp(p
,warnings
[i
].name
)) {
339 *warnings
[i
].flag
= !no
;
348 static char **handle_switch_U(char *arg
, char **next
)
350 const char *name
= arg
+ 1;
351 add_pre_buffer ("#undef %s\n", name
);
355 static char **handle_switch_O(char *arg
, char **next
)
358 if (arg
[1] >= '0' && arg
[1] <= '9')
359 level
= arg
[1] - '0';
361 optimize_size
= arg
[1] == 's';
365 static char **handle_switch_f(char *arg
, char **next
)
370 if (!strncmp(arg
, "no-", 3)) {
374 /* handle switch here.. */
378 static char **handle_switch_G(char *arg
, char **next
)
380 if (!strcmp (arg
, "G") && *next
)
381 return next
+ 1; // "-G 0"
383 return next
; // "-G0" or (bogus) terminal "-G"
386 static char **handle_nostdinc(char *arg
, char **next
)
388 add_pre_buffer("#nostdinc\n");
392 static char **handle_dirafter(char *arg
, char **next
)
394 char *path
= *++next
;
396 die("missing argument for -dirafter option");
397 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
403 char **(*fn
)(char *, char**);
406 char **handle_switch(char *arg
, char **next
)
408 static struct switches cmd
[] = {
409 { "nostdinc", handle_nostdinc
},
410 { "dirafter", handle_dirafter
},
416 case 'D': return handle_switch_D(arg
, next
);
417 case 'E': return handle_switch_E(arg
, next
);
418 case 'I': return handle_switch_I(arg
, next
);
419 case 'i': return handle_switch_i(arg
, next
);
420 case 'M': return handle_switch_M(arg
, next
);
421 case 'm': return handle_switch_m(arg
, next
);
422 case 'o': return handle_switch_o(arg
, next
);
423 case 'U': return handle_switch_U(arg
, next
);
424 case 'v': return handle_switch_v(arg
, next
);
425 case 'W': return handle_switch_W(arg
, next
);
426 case 'O': return handle_switch_O(arg
, next
);
427 case 'f': return handle_switch_f(arg
, next
);
428 case 'G': return handle_switch_G(arg
, next
);
435 if (!strcmp(s
->name
, arg
))
436 return s
->fn(arg
, next
);
441 * Ignore unknown command line options:
442 * they're probably gcc switches
447 void declare_builtin_functions(void)
449 /* Gaah. gcc knows tons of builtin <string.h> functions */
450 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
451 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
452 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
453 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
454 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
455 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
456 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
457 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
459 /* And some random ones.. */
460 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
461 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
462 add_pre_buffer("extern void __builtin_trap(void);\n");
463 add_pre_buffer("extern int __builtin_ffs(int);\n");
464 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
467 void create_builtin_stream(void)
469 add_pre_buffer("#define __GNUC__ %d\n", gcc_major
);
470 add_pre_buffer("#define __GNUC_MINOR__ %d\n", gcc_minor
);
471 add_pre_buffer("#define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
472 add_pre_buffer("#define __extension__\n");
473 add_pre_buffer("#define __pragma__\n");
475 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
476 // solaris/sparc that is really "unsigned int" and for linux/x86_64
477 // it is "long unsigned int". In either case we can probably
478 // get away with this. We need the #ifndef as cgcc will define
479 // the right __SIZE_TYPE__.
480 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
481 add_pre_buffer("#weak_define __STDC__ 1\n");
483 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
484 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
485 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
486 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
487 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
488 add_pre_buffer("#define __builtin_va_end(arg)\n");
489 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
491 /* FIXME! We need to do these as special magic macros at expansion time! */
492 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
493 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
494 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
497 add_pre_buffer("#define __OPTIMIZE__ 1\n");
499 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
502 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
504 // Pre-process the stream
505 token
= preprocess(token
);
507 if (preprocess_only
) {
508 while (!eof_token(token
)) {
510 struct token
*next
= token
->next
;
511 const char *separator
= "";
512 if (next
->pos
.whitespace
)
514 if (next
->pos
.newline
) {
515 separator
= "\n\t\t\t\t\t";
516 prec
= next
->pos
.pos
;
520 printf("%s%.*s", show_token(token
), prec
, separator
);
528 // Parse the resulting C code
529 while (!eof_token(token
))
530 token
= external_declaration(token
, &translation_unit_used_list
);
531 return translation_unit_used_list
;
534 static struct symbol_list
*sparse_file(const char *filename
)
539 if (strcmp (filename
, "-") == 0) {
542 fd
= open(filename
, O_RDONLY
);
544 die("No such file: %s", filename
);
547 // Tokenize the input stream
548 token
= tokenize(filename
, fd
, NULL
, includepath
);
551 return sparse_tokenstream(token
);
555 * This handles the "-include" directive etc: we're in global
556 * scope, and all types/macros etc will affect all the following
559 * NOTE NOTE NOTE! "#undef" of anything in this stage will
560 * affect all subsequent files too, ie we can have non-local
561 * behaviour between files!
563 static struct symbol_list
*sparse_initial(void)
567 // Prepend any "include" file to the stream.
568 // We're in global scope, it will affect all files!
571 token
= tokenize(include
, include_fd
, NULL
, includepath
);
573 // Prepend the initial built-in stream
574 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
575 return sparse_tokenstream(token
);
578 struct symbol_list
*sparse_initialize(int argc
, char **argv
)
582 struct symbol_list
*list
;
584 // Initialize symbol stream first, so that we can add defines etc
593 if (arg
[0] == '-' && arg
[1]) {
594 args
= handle_switch(arg
+1, args
);
599 * Hacky hacky hacky: we re-use the argument space
600 * to save the filenames.
608 // Initialize type system
611 create_builtin_stream();
612 add_pre_buffer("#define __CHECKER__ 1\n");
613 if (!preprocess_only
)
614 declare_builtin_functions();
616 list
= sparse_initial();
619 * Protect the initial token allocations, since
620 * they need to survive all the others
622 protect_token_alloc();
627 struct symbol_list
* sparse(char **argv
)
629 struct symbol_list
*res
;
630 char *filename
, *next
;
632 /* Clear previous symbol list */
633 translation_unit_used_list
= NULL
;
644 res
= sparse_file(filename
);
647 /* Drop the tokens for this file after parsing */
650 /* Evaluate the complete symbol list */
651 evaluate_symbol_list(res
);