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 static 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 struct token
*pre_buffer_begin
= NULL
;
189 static struct token
*pre_buffer_end
= NULL
;
191 int Waddress_space
= 1;
194 int Wcast_truncate
= 1;
197 int Wdefault_bitfield_sign
= 0;
199 int Wenum_mismatch
= 1;
200 int Wnon_pointer_null
= 1;
201 int Wold_initializer
= 1;
202 int Wone_bit_signed_bitfield
= 1;
203 int Wparen_string
= 0;
204 int Wptr_subtraction_blows
= 0;
205 int Wreturn_void
= 0;
207 int Wtransparent_union
= 1;
210 int Wuninitialized
= 1;
211 int Wdeclarationafterstatement
= -1;
218 static enum { STANDARD_C89
,
222 STANDARD_GNU99
, } standard
= STANDARD_GNU89
;
224 #define CMDLINE_INCLUDE 20
225 int cmdline_include_nr
= 0;
226 struct cmdline_include cmdline_include
[CMDLINE_INCLUDE
];
229 void add_pre_buffer(const char *fmt
, ...)
233 struct token
*begin
, *end
;
237 size
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
239 begin
= tokenize_buffer(buffer
, size
, &end
);
240 if (!pre_buffer_begin
)
241 pre_buffer_begin
= begin
;
243 pre_buffer_end
->next
= begin
;
244 pre_buffer_end
= end
;
247 static char **handle_switch_D(char *arg
, char **next
)
249 const char *name
= arg
+ 1;
250 const char *value
= "1";
252 if (!*name
|| isspace(*name
))
253 die("argument to `-D' is missing");
260 if (isspace((unsigned char)c
) || c
== '=') {
266 add_pre_buffer("#define %s %s\n", name
, value
);
270 static char **handle_switch_E(char *arg
, char **next
)
277 static char **handle_switch_I(char *arg
, char **next
)
283 add_pre_buffer("#split_include\n");
286 case '\0': /* Plain "-I" */
289 die("missing argument for -I option");
292 add_pre_buffer("#add_include \"%s/\"\n", path
);
297 static void add_cmdline_include(char *filename
)
299 int fd
= open(filename
, O_RDONLY
);
304 if (cmdline_include_nr
>= CMDLINE_INCLUDE
)
305 die("too many include files for %s\n", filename
);
306 cmdline_include
[cmdline_include_nr
].filename
= filename
;
307 cmdline_include
[cmdline_include_nr
].fd
= fd
;
308 cmdline_include_nr
++;
311 static char **handle_switch_i(char *arg
, char **next
)
313 if (*next
&& !strcmp(arg
, "include"))
314 add_cmdline_include(*++next
);
315 else if (*next
&& !strcmp(arg
, "imacros"))
316 add_cmdline_include(*++next
);
317 else if (*next
&& !strcmp(arg
, "isystem")) {
318 char *path
= *++next
;
320 die("missing argument for -isystem option");
321 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
326 static char **handle_switch_M(char *arg
, char **next
)
328 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
330 die("missing argument for -%s option", arg
);
336 static char **handle_switch_m(char *arg
, char **next
)
338 if (!strcmp(arg
, "m64")) {
340 max_int_alignment
= 8;
341 bits_in_pointer
= 64;
342 pointer_alignment
= 8;
343 size_t_ctype
= &ulong_ctype
;
344 ssize_t_ctype
= &long_ctype
;
345 } else if (!strcmp(arg
, "msize-long")) {
346 size_t_ctype
= &ulong_ctype
;
347 ssize_t_ctype
= &long_ctype
;
352 static char **handle_switch_o(char *arg
, char **next
)
354 if (!strcmp (arg
, "o")) { // "-o foo"
356 die("argument to '-o' is missing");
363 static const struct warning
{
367 { "address-space", &Waddress_space
},
368 { "bitwise", &Wbitwise
},
369 { "cast-to-as", &Wcast_to_as
},
370 { "cast-truncate", &Wcast_truncate
},
371 { "context", &Wcontext
},
373 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
374 { "do-while", &Wdo_while
},
375 { "enum-mismatch", &Wenum_mismatch
},
376 { "non-pointer-null", &Wnon_pointer_null
},
377 { "old-initializer", &Wold_initializer
},
378 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield
},
379 { "paren-string", &Wparen_string
},
380 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
381 { "return-void", &Wreturn_void
},
382 { "shadow", &Wshadow
},
383 { "transparent-union", &Wtransparent_union
},
384 { "typesign", &Wtypesign
},
385 { "undef", &Wundef
},
386 { "uninitialized", &Wuninitialized
},
387 { "declaration-after-statement", &Wdeclarationafterstatement
},
397 static char **handle_onoff_switch(char *arg
, char **next
, const struct warning warnings
[], int n
)
399 int flag
= WARNING_ON
;
403 if (!strcmp(p
, "all")) {
404 for (i
= 0; i
< n
; i
++) {
405 if (*warnings
[i
].flag
!= WARNING_FORCE_OFF
)
406 *warnings
[i
].flag
= WARNING_ON
;
410 // Prefixes "no" and "no-" mean to turn warning off.
411 if (p
[0] == 'n' && p
[1] == 'o') {
415 flag
= WARNING_FORCE_OFF
;
418 for (i
= 0; i
< n
; i
++) {
419 if (!strcmp(p
,warnings
[i
].name
)) {
420 *warnings
[i
].flag
= flag
;
429 static char **handle_switch_W(char *arg
, char **next
)
431 char ** ret
= handle_onoff_switch(arg
, next
, warnings
, sizeof warnings
/sizeof warnings
[0]);
439 static struct warning debugs
[] = {
440 { "entry", &dbg_entry
},
441 { "dead", &dbg_dead
},
445 static char **handle_switch_v(char *arg
, char **next
)
447 char ** ret
= handle_onoff_switch(arg
, next
, debugs
, sizeof debugs
/sizeof debugs
[0]);
454 } while (*++arg
== 'v');
459 static void handle_onoff_switch_finalize(const struct warning warnings
[], int n
)
463 for (i
= 0; i
< n
; i
++) {
464 if (*warnings
[i
].flag
== WARNING_FORCE_OFF
)
465 *warnings
[i
].flag
= WARNING_OFF
;
469 static void handle_switch_W_finalize(void)
471 handle_onoff_switch_finalize(warnings
, sizeof(warnings
) / sizeof(warnings
[0]));
473 /* default Wdeclarationafterstatement based on the C dialect */
474 if (-1 == Wdeclarationafterstatement
)
480 Wdeclarationafterstatement
= 1;
486 Wdeclarationafterstatement
= 0;
496 static void handle_switch_v_finalize(void)
498 handle_onoff_switch_finalize(debugs
, sizeof(debugs
) / sizeof(debugs
[0]));
501 static char **handle_switch_U(char *arg
, char **next
)
503 const char *name
= arg
+ 1;
504 add_pre_buffer ("#undef %s\n", name
);
508 static char **handle_switch_O(char *arg
, char **next
)
511 if (arg
[1] >= '0' && arg
[1] <= '9')
512 level
= arg
[1] - '0';
514 optimize_size
= arg
[1] == 's';
518 static char **handle_switch_f(char *arg
, char **next
)
523 if (!strncmp(arg
, "no-", 3)) {
527 /* handle switch here.. */
531 static char **handle_switch_G(char *arg
, char **next
)
533 if (!strcmp (arg
, "G") && *next
)
534 return next
+ 1; // "-G 0"
536 return next
; // "-G0" or (bogus) terminal "-G"
539 static char **handle_switch_a(char *arg
, char **next
)
541 if (!strcmp (arg
, "ansi"))
542 standard
= STANDARD_C89
;
547 static char **handle_switch_s(char *arg
, char **next
)
549 if (!strncmp (arg
, "std=", 4))
553 if (!strcmp (arg
, "c89") ||
554 !strcmp (arg
, "iso9899:1990"))
555 standard
= STANDARD_C89
;
557 else if (!strcmp (arg
, "iso9899:199409"))
558 standard
= STANDARD_C94
;
560 else if (!strcmp (arg
, "c99") ||
561 !strcmp (arg
, "c9x") ||
562 !strcmp (arg
, "iso9899:1999") ||
563 !strcmp (arg
, "iso9899:199x"))
564 standard
= STANDARD_C99
;
566 else if (!strcmp (arg
, "gnu89"))
567 standard
= STANDARD_GNU89
;
569 else if (!strcmp (arg
, "gnu99") || !strcmp (arg
, "gnu9x"))
570 standard
= STANDARD_GNU99
;
573 die ("Unsupported C dialect");
579 static char **handle_nostdinc(char *arg
, char **next
)
581 add_pre_buffer("#nostdinc\n");
585 static char **handle_dirafter(char *arg
, char **next
)
587 char *path
= *++next
;
589 die("missing argument for -dirafter option");
590 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
596 char **(*fn
)(char *, char **);
599 static char **handle_switch(char *arg
, char **next
)
601 static struct switches cmd
[] = {
602 { "nostdinc", handle_nostdinc
},
603 { "dirafter", handle_dirafter
},
609 case 'D': return handle_switch_D(arg
, next
);
610 case 'E': return handle_switch_E(arg
, next
);
611 case 'I': return handle_switch_I(arg
, next
);
612 case 'i': return handle_switch_i(arg
, next
);
613 case 'M': return handle_switch_M(arg
, next
);
614 case 'm': return handle_switch_m(arg
, next
);
615 case 'o': return handle_switch_o(arg
, next
);
616 case 'U': return handle_switch_U(arg
, next
);
617 case 'v': return handle_switch_v(arg
, next
);
618 case 'W': return handle_switch_W(arg
, next
);
619 case 'O': return handle_switch_O(arg
, next
);
620 case 'f': return handle_switch_f(arg
, next
);
621 case 'G': return handle_switch_G(arg
, next
);
622 case 'a': return handle_switch_a(arg
, next
);
623 case 's': return handle_switch_s(arg
, next
);
630 if (!strcmp(s
->name
, arg
))
631 return s
->fn(arg
, next
);
636 * Ignore unknown command line options:
637 * they're probably gcc switches
642 void declare_builtin_functions(void)
644 /* Gaah. gcc knows tons of builtin <string.h> functions */
645 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
646 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
647 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
648 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
649 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
650 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
651 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
652 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
653 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
654 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
655 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
656 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
657 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
658 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
660 /* And some random ones.. */
661 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
662 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
663 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
664 add_pre_buffer("extern void __builtin_trap(void);\n");
665 add_pre_buffer("extern int __builtin_ffs(int);\n");
666 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
667 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
668 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
669 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
670 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
671 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
672 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
673 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
674 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
675 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
676 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
677 add_pre_buffer("extern long __builtin_labs(long);\n");
679 /* And some __FORTIFY_SOURCE ones.. */
680 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
681 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
682 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
683 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
684 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
685 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
686 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
687 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
688 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
689 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
690 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
691 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
692 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
693 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
696 void create_builtin_stream(void)
698 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major
);
699 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor
);
700 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
701 add_pre_buffer("#define __extension__\n");
702 add_pre_buffer("#define __pragma__\n");
704 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
705 // solaris/sparc that is really "unsigned int" and for linux/x86_64
706 // it is "long unsigned int". In either case we can probably
707 // get away with this. We need the #weak_define as cgcc will define
708 // the right __SIZE_TYPE__.
709 if (size_t_ctype
== &ulong_ctype
)
710 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
712 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
713 add_pre_buffer("#weak_define __STDC__ 1\n");
718 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
722 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
723 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
727 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
728 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
735 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
742 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
743 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
744 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
745 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
746 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
747 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
748 add_pre_buffer("#define __builtin_va_end(arg)\n");
750 /* FIXME! We need to do these as special magic macros at expansion time! */
751 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
754 add_pre_buffer("#define __OPTIMIZE__ 1\n");
756 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
759 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
761 // Preprocess the stream
762 token
= preprocess(token
);
764 if (preprocess_only
) {
765 while (!eof_token(token
)) {
767 struct token
*next
= token
->next
;
768 const char *separator
= "";
769 if (next
->pos
.whitespace
)
771 if (next
->pos
.newline
) {
772 separator
= "\n\t\t\t\t\t";
773 prec
= next
->pos
.pos
;
777 printf("%s%.*s", show_token(token
), prec
, separator
);
785 // Parse the resulting C code
786 while (!eof_token(token
))
787 token
= external_declaration(token
, &translation_unit_used_list
);
788 return translation_unit_used_list
;
791 static struct symbol_list
*sparse_file(const char *filename
)
796 if (strcmp (filename
, "-") == 0) {
799 fd
= open(filename
, O_RDONLY
);
801 die("No such file: %s", filename
);
804 // Tokenize the input stream
805 token
= tokenize(filename
, fd
, NULL
, includepath
);
808 return sparse_tokenstream(token
);
812 * This handles the "-include" directive etc: we're in global
813 * scope, and all types/macros etc will affect all the following
816 * NOTE NOTE NOTE! "#undef" of anything in this stage will
817 * affect all subsequent files too, i.e. we can have non-local
818 * behaviour between files!
820 static struct symbol_list
*sparse_initial(void)
825 // Prepend any "include" file to the stream.
826 // We're in global scope, it will affect all files!
828 for (i
= cmdline_include_nr
- 1; i
>= 0; i
--)
829 token
= tokenize(cmdline_include
[i
].filename
, cmdline_include
[i
].fd
,
832 // Prepend the initial built-in stream
834 pre_buffer_end
->next
= token
;
835 return sparse_tokenstream(pre_buffer_begin
);
838 struct symbol_list
*sparse_initialize(int argc
, char **argv
, struct string_list
**filelist
)
841 struct symbol_list
*list
;
843 // Initialize symbol stream first, so that we can add defines etc
852 if (arg
[0] == '-' && arg
[1]) {
853 args
= handle_switch(arg
+1, args
);
856 add_ptr_list_notag(filelist
, arg
);
858 handle_switch_W_finalize();
859 handle_switch_v_finalize();
862 if (!ptr_list_empty(filelist
)) {
863 // Initialize type system
866 create_builtin_stream();
867 add_pre_buffer("#define __CHECKER__ 1\n");
868 if (!preprocess_only
)
869 declare_builtin_functions();
871 list
= sparse_initial();
874 * Protect the initial token allocations, since
875 * they need to survive all the others
877 protect_token_alloc();
882 struct symbol_list
* sparse_keep_tokens(char *filename
)
884 struct symbol_list
*res
;
886 /* Clear previous symbol list */
887 translation_unit_used_list
= NULL
;
890 res
= sparse_file(filename
);
897 struct symbol_list
* __sparse(char *filename
)
899 struct symbol_list
*res
;
901 res
= sparse_keep_tokens(filename
);
903 /* Drop the tokens for this file after parsing */
910 struct symbol_list
* sparse(char *filename
)
912 struct symbol_list
*res
= __sparse(filename
);
914 /* Evaluate the complete symbol list */
915 evaluate_symbol_list(res
);