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;
97 static int show_info
= 1;
99 void info(struct position pos
, const char * fmt
, ...)
106 do_warn("", pos
, fmt
, args
);
110 void warning(struct position pos
, const char * fmt
, ...)
119 if (!--max_warnings
) {
121 fmt
= "too many warnings";
125 do_warn("warning: ", pos
, fmt
, args
);
129 void do_error(struct position pos
, const char * fmt
, va_list args
)
131 static int errors
= 0;
134 /* Shut up warnings after an error */
141 fmt
= "too many errors";
145 do_warn("error: ", pos
, fmt
, args
);
149 void sparse_error(struct position pos
, const char * fmt
, ...)
153 do_error(pos
, fmt
, args
);
157 void expression_error(struct expression
*expr
, const char *fmt
, ...)
161 do_error(expr
->pos
, fmt
, args
);
163 expr
->ctype
= &bad_ctype
;
166 void error_die(struct position pos
, const char * fmt
, ...)
170 do_warn("error: ", pos
, fmt
, args
);
175 void die(const char *fmt
, ...)
178 static char buffer
[512];
181 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
184 fprintf(stderr
, "%s\n", buffer
);
188 static unsigned int pre_buffer_size
;
189 static char pre_buffer
[8192];
191 int Wdefault_bitfield_sign
= 0;
192 int Wone_bit_signed_bitfield
= 1;
193 int Wcast_truncate
= 1;
197 int Wundefined_preprocessor
= 0;
198 int Wptr_subtraction_blows
= 0;
199 int Wcast_to_address_space
= 0;
201 int Wtransparent_union
= 1;
203 int Waddress_space
= 1;
204 int Wenum_mismatch
= 1;
206 int Wuninitialized
= 1;
213 #define CMDLINE_INCLUDE 20
214 int cmdline_include_nr
= 0;
215 struct cmdline_include cmdline_include
[CMDLINE_INCLUDE
];
218 void add_pre_buffer(const char *fmt
, ...)
224 size
= pre_buffer_size
;
225 size
+= vsnprintf(pre_buffer
+ size
,
226 sizeof(pre_buffer
) - size
,
228 pre_buffer_size
= size
;
232 static char **handle_switch_D(char *arg
, char **next
)
234 const char *name
= arg
+ 1;
235 const char *value
= "1";
241 if (isspace((unsigned char)c
) || c
== '=') {
247 add_pre_buffer("#define %s %s\n", name
, value
);
251 static char **handle_switch_E(char *arg
, char **next
)
257 static char **handle_switch_I(char *arg
, char **next
)
263 add_pre_buffer("#split_include\n");
266 case '\0': /* Plain "-I" */
269 die("missing argument for -I option");
272 add_pre_buffer("#add_include \"%s/\"\n", path
);
277 static void add_cmdline_include(char *filename
)
279 int fd
= open(filename
, O_RDONLY
);
284 if (cmdline_include_nr
>= CMDLINE_INCLUDE
)
285 die("too many include files for %s\n", filename
);
286 cmdline_include
[cmdline_include_nr
].filename
= filename
;
287 cmdline_include
[cmdline_include_nr
].fd
= fd
;
288 cmdline_include_nr
++;
291 static char **handle_switch_i(char *arg
, char **next
)
293 if (*next
&& !strcmp(arg
, "include"))
294 add_cmdline_include(*++next
);
295 else if (*next
&& !strcmp(arg
, "imacros"))
296 add_cmdline_include(*++next
);
297 else if (*next
&& !strcmp(arg
, "isystem")) {
298 char *path
= *++next
;
300 die("missing argument for -isystem option");
301 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
306 static char **handle_switch_M(char *arg
, char **next
)
308 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
310 die("missing argument for -%s option", arg
);
316 static char **handle_switch_m(char *arg
, char **next
)
318 if (!strcmp(arg
, "m64")) {
320 max_int_alignment
= 8;
321 bits_in_pointer
= 64;
322 pointer_alignment
= 8;
327 static char **handle_switch_o(char *arg
, char **next
)
329 if (!strcmp (arg
, "o") && *next
)
330 return next
+ 1; // "-o foo"
332 return next
; // "-ofoo" or (bogus) terminal "-o"
335 static const struct warning
{
339 { "cast-to-as", &Wcast_to_address_space
},
341 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield
},
342 { "cast-truncate", &Wcast_truncate
},
343 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
344 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
345 { "undef", &Wundefined_preprocessor
},
346 { "bitwise", &Wbitwise
},
347 { "typesign", &Wtypesign
},
348 { "context", &Wcontext
},
349 { "transparent-union", &Wtransparent_union
},
350 { "shadow", &Wshadow
},
351 { "address-space", &Waddress_space
},
352 { "enum-mismatch", &Wenum_mismatch
},
353 { "do-while", &Wdo_while
},
354 { "uninitialized", &Wuninitialized
},
364 static char **handle_onoff_switch(char *arg
, char **next
, const struct warning warnings
[], int n
)
366 int flag
= WARNING_ON
;
370 if (!strcmp(p
, "all")) {
371 for (i
= 0; i
< n
; i
++) {
372 if (*warnings
[i
].flag
!= WARNING_FORCE_OFF
)
373 *warnings
[i
].flag
= WARNING_ON
;
377 // Prefixes "no" and "no-" mean to turn warning off.
378 if (p
[0] == 'n' && p
[1] == 'o') {
382 flag
= WARNING_FORCE_OFF
;
385 for (i
= 0; i
< n
; i
++) {
386 if (!strcmp(p
,warnings
[i
].name
)) {
387 *warnings
[i
].flag
= flag
;
396 static char **handle_switch_W(char *arg
, char **next
)
398 char ** ret
= handle_onoff_switch(arg
, next
, warnings
, sizeof warnings
/sizeof warnings
[0]);
406 static struct warning debugs
[] = {
407 { "entry", &dbg_entry
},
408 { "dead", &dbg_dead
},
412 static char **handle_switch_v(char *arg
, char **next
)
414 char ** ret
= handle_onoff_switch(arg
, next
, debugs
, sizeof debugs
/sizeof debugs
[0]);
421 } while (*++arg
== 'v');
426 static void handle_onoff_switch_finalize(const struct warning warnings
[], int n
)
430 for (i
= 0; i
< n
; i
++) {
431 if (*warnings
[i
].flag
== WARNING_FORCE_OFF
)
432 *warnings
[i
].flag
= WARNING_OFF
;
436 static void handle_switch_W_finalize(void)
438 handle_onoff_switch_finalize(warnings
, sizeof(warnings
) / sizeof(warnings
[0]));
441 static void handle_switch_v_finalize(void)
443 handle_onoff_switch_finalize(debugs
, sizeof(debugs
) / sizeof(debugs
[0]));
446 static char **handle_switch_U(char *arg
, char **next
)
448 const char *name
= arg
+ 1;
449 add_pre_buffer ("#undef %s\n", name
);
453 static char **handle_switch_O(char *arg
, char **next
)
456 if (arg
[1] >= '0' && arg
[1] <= '9')
457 level
= arg
[1] - '0';
459 optimize_size
= arg
[1] == 's';
463 static char **handle_switch_f(char *arg
, char **next
)
468 if (!strncmp(arg
, "no-", 3)) {
472 /* handle switch here.. */
476 static char **handle_switch_G(char *arg
, char **next
)
478 if (!strcmp (arg
, "G") && *next
)
479 return next
+ 1; // "-G 0"
481 return next
; // "-G0" or (bogus) terminal "-G"
484 static char **handle_nostdinc(char *arg
, char **next
)
486 add_pre_buffer("#nostdinc\n");
490 static char **handle_dirafter(char *arg
, char **next
)
492 char *path
= *++next
;
494 die("missing argument for -dirafter option");
495 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
501 char **(*fn
)(char *, char **);
504 char **handle_switch(char *arg
, char **next
)
506 static struct switches cmd
[] = {
507 { "nostdinc", handle_nostdinc
},
508 { "dirafter", handle_dirafter
},
514 case 'D': return handle_switch_D(arg
, next
);
515 case 'E': return handle_switch_E(arg
, next
);
516 case 'I': return handle_switch_I(arg
, next
);
517 case 'i': return handle_switch_i(arg
, next
);
518 case 'M': return handle_switch_M(arg
, next
);
519 case 'm': return handle_switch_m(arg
, next
);
520 case 'o': return handle_switch_o(arg
, next
);
521 case 'U': return handle_switch_U(arg
, next
);
522 case 'v': return handle_switch_v(arg
, next
);
523 case 'W': return handle_switch_W(arg
, next
);
524 case 'O': return handle_switch_O(arg
, next
);
525 case 'f': return handle_switch_f(arg
, next
);
526 case 'G': return handle_switch_G(arg
, next
);
533 if (!strcmp(s
->name
, arg
))
534 return s
->fn(arg
, next
);
539 * Ignore unknown command line options:
540 * they're probably gcc switches
545 void declare_builtin_functions(void)
547 /* Gaah. gcc knows tons of builtin <string.h> functions */
548 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
549 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
550 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
551 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
552 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
553 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
554 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
555 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
556 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
557 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
559 /* And some random ones.. */
560 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
561 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
562 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
563 add_pre_buffer("extern void __builtin_trap(void);\n");
564 add_pre_buffer("extern int __builtin_ffs(int);\n");
565 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
566 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
567 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
568 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
569 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
570 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
571 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
572 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
573 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
574 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
575 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
578 void create_builtin_stream(void)
580 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major
);
581 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor
);
582 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
583 add_pre_buffer("#define __extension__\n");
584 add_pre_buffer("#define __pragma__\n");
586 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
587 // solaris/sparc that is really "unsigned int" and for linux/x86_64
588 // it is "long unsigned int". In either case we can probably
589 // get away with this. We need the #weak_define as cgcc will define
590 // the right __SIZE_TYPE__.
591 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
592 add_pre_buffer("#weak_define __STDC__ 1\n");
594 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
595 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
596 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
597 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
598 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
599 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
600 add_pre_buffer("#define __builtin_va_end(arg)\n");
601 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
603 /* FIXME! We need to do these as special magic macros at expansion time! */
604 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
605 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
606 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
609 add_pre_buffer("#define __OPTIMIZE__ 1\n");
611 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
614 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
616 // Preprocess the stream
617 token
= preprocess(token
);
619 if (preprocess_only
) {
620 while (!eof_token(token
)) {
622 struct token
*next
= token
->next
;
623 const char *separator
= "";
624 if (next
->pos
.whitespace
)
626 if (next
->pos
.newline
) {
627 separator
= "\n\t\t\t\t\t";
628 prec
= next
->pos
.pos
;
632 printf("%s%.*s", show_token(token
), prec
, separator
);
640 // Parse the resulting C code
641 while (!eof_token(token
))
642 token
= external_declaration(token
, &translation_unit_used_list
);
643 return translation_unit_used_list
;
646 static struct symbol_list
*sparse_file(const char *filename
)
651 if (strcmp (filename
, "-") == 0) {
654 fd
= open(filename
, O_RDONLY
);
656 die("No such file: %s", filename
);
659 // Tokenize the input stream
660 token
= tokenize(filename
, fd
, NULL
, includepath
);
663 return sparse_tokenstream(token
);
667 * This handles the "-include" directive etc: we're in global
668 * scope, and all types/macros etc will affect all the following
671 * NOTE NOTE NOTE! "#undef" of anything in this stage will
672 * affect all subsequent files too, i.e. we can have non-local
673 * behaviour between files!
675 static struct symbol_list
*sparse_initial(void)
680 // Prepend any "include" file to the stream.
681 // We're in global scope, it will affect all files!
683 for (i
= cmdline_include_nr
- 1; i
>= 0; i
--)
684 token
= tokenize(cmdline_include
[i
].filename
, cmdline_include
[i
].fd
,
687 // Prepend the initial built-in stream
688 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
689 return sparse_tokenstream(token
);
692 struct symbol_list
*sparse_initialize(int argc
, char **argv
, struct string_list
**filelist
)
695 struct symbol_list
*list
;
697 // Initialize symbol stream first, so that we can add defines etc
706 if (arg
[0] == '-' && arg
[1]) {
707 args
= handle_switch(arg
+1, args
);
710 add_ptr_list_notag(filelist
, arg
);
712 handle_switch_W_finalize();
713 handle_switch_v_finalize();
716 if (!ptr_list_empty(filelist
)) {
717 // Initialize type system
720 create_builtin_stream();
721 add_pre_buffer("#define __CHECKER__ 1\n");
722 if (!preprocess_only
)
723 declare_builtin_functions();
725 list
= sparse_initial();
728 * Protect the initial token allocations, since
729 * they need to survive all the others
731 protect_token_alloc();
736 struct symbol_list
* __sparse(char *filename
)
738 struct symbol_list
*res
;
740 /* Clear previous symbol list */
741 translation_unit_used_list
= NULL
;
744 res
= sparse_file(filename
);
746 /* Drop the tokens for this file after parsing */
753 struct symbol_list
* sparse(char *filename
)
755 struct symbol_list
*res
= __sparse(filename
);
757 /* Evaluate the complete symbol list */
758 evaluate_symbol_list(res
);