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 unsigned int pre_buffer_size
;
189 static char pre_buffer
[8192];
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
, ...)
235 size
= pre_buffer_size
;
236 size
+= vsnprintf(pre_buffer
+ size
,
237 sizeof(pre_buffer
) - size
,
239 pre_buffer_size
= size
;
243 static char **handle_switch_D(char *arg
, char **next
)
245 const char *name
= arg
+ 1;
246 const char *value
= "1";
252 if (isspace((unsigned char)c
) || c
== '=') {
258 add_pre_buffer("#define %s %s\n", name
, value
);
262 static char **handle_switch_E(char *arg
, char **next
)
269 static char **handle_switch_I(char *arg
, char **next
)
275 add_pre_buffer("#split_include\n");
278 case '\0': /* Plain "-I" */
281 die("missing argument for -I option");
284 add_pre_buffer("#add_include \"%s/\"\n", path
);
289 static void add_cmdline_include(char *filename
)
291 int fd
= open(filename
, O_RDONLY
);
296 if (cmdline_include_nr
>= CMDLINE_INCLUDE
)
297 die("too many include files for %s\n", filename
);
298 cmdline_include
[cmdline_include_nr
].filename
= filename
;
299 cmdline_include
[cmdline_include_nr
].fd
= fd
;
300 cmdline_include_nr
++;
303 static char **handle_switch_i(char *arg
, char **next
)
305 if (*next
&& !strcmp(arg
, "include"))
306 add_cmdline_include(*++next
);
307 else if (*next
&& !strcmp(arg
, "imacros"))
308 add_cmdline_include(*++next
);
309 else if (*next
&& !strcmp(arg
, "isystem")) {
310 char *path
= *++next
;
312 die("missing argument for -isystem option");
313 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
318 static char **handle_switch_M(char *arg
, char **next
)
320 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
322 die("missing argument for -%s option", arg
);
328 static char **handle_switch_m(char *arg
, char **next
)
330 if (!strcmp(arg
, "m64")) {
332 max_int_alignment
= 8;
333 bits_in_pointer
= 64;
334 pointer_alignment
= 8;
335 size_t_ctype
= &ulong_ctype
;
336 ssize_t_ctype
= &long_ctype
;
337 } else if (!strcmp(arg
, "msize-long")) {
338 size_t_ctype
= &ulong_ctype
;
339 ssize_t_ctype
= &long_ctype
;
344 static char **handle_switch_o(char *arg
, char **next
)
346 if (!strcmp (arg
, "o") && *next
)
347 return next
+ 1; // "-o foo"
349 return next
; // "-ofoo" or (bogus) terminal "-o"
352 static const struct warning
{
356 { "address-space", &Waddress_space
},
357 { "bitwise", &Wbitwise
},
358 { "cast-to-as", &Wcast_to_as
},
359 { "cast-truncate", &Wcast_truncate
},
360 { "context", &Wcontext
},
362 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
363 { "do-while", &Wdo_while
},
364 { "enum-mismatch", &Wenum_mismatch
},
365 { "non-pointer-null", &Wnon_pointer_null
},
366 { "old-initializer", &Wold_initializer
},
367 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield
},
368 { "paren-string", &Wparen_string
},
369 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
370 { "return-void", &Wreturn_void
},
371 { "shadow", &Wshadow
},
372 { "transparent-union", &Wtransparent_union
},
373 { "typesign", &Wtypesign
},
374 { "undef", &Wundef
},
375 { "uninitialized", &Wuninitialized
},
376 { "declaration-after-statement", &Wdeclarationafterstatement
},
386 static char **handle_onoff_switch(char *arg
, char **next
, const struct warning warnings
[], int n
)
388 int flag
= WARNING_ON
;
392 if (!strcmp(p
, "all")) {
393 for (i
= 0; i
< n
; i
++) {
394 if (*warnings
[i
].flag
!= WARNING_FORCE_OFF
)
395 *warnings
[i
].flag
= WARNING_ON
;
399 // Prefixes "no" and "no-" mean to turn warning off.
400 if (p
[0] == 'n' && p
[1] == 'o') {
404 flag
= WARNING_FORCE_OFF
;
407 for (i
= 0; i
< n
; i
++) {
408 if (!strcmp(p
,warnings
[i
].name
)) {
409 *warnings
[i
].flag
= flag
;
418 static char **handle_switch_W(char *arg
, char **next
)
420 char ** ret
= handle_onoff_switch(arg
, next
, warnings
, sizeof warnings
/sizeof warnings
[0]);
428 static struct warning debugs
[] = {
429 { "entry", &dbg_entry
},
430 { "dead", &dbg_dead
},
434 static char **handle_switch_v(char *arg
, char **next
)
436 char ** ret
= handle_onoff_switch(arg
, next
, debugs
, sizeof debugs
/sizeof debugs
[0]);
443 } while (*++arg
== 'v');
448 static void handle_onoff_switch_finalize(const struct warning warnings
[], int n
)
452 for (i
= 0; i
< n
; i
++) {
453 if (*warnings
[i
].flag
== WARNING_FORCE_OFF
)
454 *warnings
[i
].flag
= WARNING_OFF
;
458 static void handle_switch_W_finalize(void)
460 handle_onoff_switch_finalize(warnings
, sizeof(warnings
) / sizeof(warnings
[0]));
462 /* default Wdeclarationafterstatement based on the C dialect */
463 if (-1 == Wdeclarationafterstatement
)
469 Wdeclarationafterstatement
= 1;
475 Wdeclarationafterstatement
= 0;
485 static void handle_switch_v_finalize(void)
487 handle_onoff_switch_finalize(debugs
, sizeof(debugs
) / sizeof(debugs
[0]));
490 static char **handle_switch_U(char *arg
, char **next
)
492 const char *name
= arg
+ 1;
493 add_pre_buffer ("#undef %s\n", name
);
497 static char **handle_switch_O(char *arg
, char **next
)
500 if (arg
[1] >= '0' && arg
[1] <= '9')
501 level
= arg
[1] - '0';
503 optimize_size
= arg
[1] == 's';
507 static char **handle_switch_f(char *arg
, char **next
)
512 if (!strncmp(arg
, "no-", 3)) {
516 /* handle switch here.. */
520 static char **handle_switch_G(char *arg
, char **next
)
522 if (!strcmp (arg
, "G") && *next
)
523 return next
+ 1; // "-G 0"
525 return next
; // "-G0" or (bogus) terminal "-G"
528 static char **handle_switch_a(char *arg
, char **next
)
530 if (!strcmp (arg
, "ansi"))
531 standard
= STANDARD_C89
;
536 static char **handle_switch_s(char *arg
, char **next
)
538 if (!strncmp (arg
, "std=", 4))
542 if (!strcmp (arg
, "c89") ||
543 !strcmp (arg
, "iso9899:1990"))
544 standard
= STANDARD_C89
;
546 else if (!strcmp (arg
, "iso9899:199409"))
547 standard
= STANDARD_C94
;
549 else if (!strcmp (arg
, "c99") ||
550 !strcmp (arg
, "c9x") ||
551 !strcmp (arg
, "iso9899:1999") ||
552 !strcmp (arg
, "iso9899:199x"))
553 standard
= STANDARD_C99
;
555 else if (!strcmp (arg
, "gnu89"))
556 standard
= STANDARD_GNU89
;
558 else if (!strcmp (arg
, "gnu99") || !strcmp (arg
, "gnu9x"))
559 standard
= STANDARD_GNU99
;
562 die ("Unsupported C dialect");
568 static char **handle_nostdinc(char *arg
, char **next
)
570 add_pre_buffer("#nostdinc\n");
574 static char **handle_dirafter(char *arg
, char **next
)
576 char *path
= *++next
;
578 die("missing argument for -dirafter option");
579 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
585 char **(*fn
)(char *, char **);
588 char **handle_switch(char *arg
, char **next
)
590 static struct switches cmd
[] = {
591 { "nostdinc", handle_nostdinc
},
592 { "dirafter", handle_dirafter
},
598 case 'D': return handle_switch_D(arg
, next
);
599 case 'E': return handle_switch_E(arg
, next
);
600 case 'I': return handle_switch_I(arg
, next
);
601 case 'i': return handle_switch_i(arg
, next
);
602 case 'M': return handle_switch_M(arg
, next
);
603 case 'm': return handle_switch_m(arg
, next
);
604 case 'o': return handle_switch_o(arg
, next
);
605 case 'U': return handle_switch_U(arg
, next
);
606 case 'v': return handle_switch_v(arg
, next
);
607 case 'W': return handle_switch_W(arg
, next
);
608 case 'O': return handle_switch_O(arg
, next
);
609 case 'f': return handle_switch_f(arg
, next
);
610 case 'G': return handle_switch_G(arg
, next
);
611 case 'a': return handle_switch_a(arg
, next
);
612 case 's': return handle_switch_s(arg
, next
);
619 if (!strcmp(s
->name
, arg
))
620 return s
->fn(arg
, next
);
625 * Ignore unknown command line options:
626 * they're probably gcc switches
631 void declare_builtin_functions(void)
633 /* Gaah. gcc knows tons of builtin <string.h> functions */
634 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
635 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
636 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
637 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
638 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
639 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
640 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
641 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
642 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
643 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
644 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
645 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
646 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
647 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
649 /* And some random ones.. */
650 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
651 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
652 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
653 add_pre_buffer("extern void __builtin_trap(void);\n");
654 add_pre_buffer("extern int __builtin_ffs(int);\n");
655 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
656 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
657 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
658 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
659 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
660 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
661 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
662 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
663 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
664 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
665 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
666 add_pre_buffer("extern long __builtin_labs(long);\n");
668 /* And some __FORTIFY_SOURCE ones.. */
669 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
670 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
671 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
672 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
673 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
674 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
675 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
676 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
677 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
678 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
679 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
680 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
681 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
682 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
685 void create_builtin_stream(void)
687 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major
);
688 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor
);
689 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
690 add_pre_buffer("#define __extension__\n");
691 add_pre_buffer("#define __pragma__\n");
693 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
694 // solaris/sparc that is really "unsigned int" and for linux/x86_64
695 // it is "long unsigned int". In either case we can probably
696 // get away with this. We need the #weak_define as cgcc will define
697 // the right __SIZE_TYPE__.
698 if (size_t_ctype
== &ulong_ctype
)
699 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
701 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
702 add_pre_buffer("#weak_define __STDC__ 1\n");
707 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
711 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
712 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
716 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
717 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
724 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
731 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
732 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
733 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
734 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
735 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
736 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
737 add_pre_buffer("#define __builtin_va_end(arg)\n");
739 /* FIXME! We need to do these as special magic macros at expansion time! */
740 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
743 add_pre_buffer("#define __OPTIMIZE__ 1\n");
745 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
748 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
750 // Preprocess the stream
751 token
= preprocess(token
);
753 if (preprocess_only
) {
754 while (!eof_token(token
)) {
756 struct token
*next
= token
->next
;
757 const char *separator
= "";
758 if (next
->pos
.whitespace
)
760 if (next
->pos
.newline
) {
761 separator
= "\n\t\t\t\t\t";
762 prec
= next
->pos
.pos
;
766 printf("%s%.*s", show_token(token
), prec
, separator
);
774 // Parse the resulting C code
775 while (!eof_token(token
))
776 token
= external_declaration(token
, &translation_unit_used_list
);
777 return translation_unit_used_list
;
780 static struct symbol_list
*sparse_file(const char *filename
)
785 if (strcmp (filename
, "-") == 0) {
788 fd
= open(filename
, O_RDONLY
);
790 die("No such file: %s", filename
);
793 // Tokenize the input stream
794 token
= tokenize(filename
, fd
, NULL
, includepath
);
797 return sparse_tokenstream(token
);
801 * This handles the "-include" directive etc: we're in global
802 * scope, and all types/macros etc will affect all the following
805 * NOTE NOTE NOTE! "#undef" of anything in this stage will
806 * affect all subsequent files too, i.e. we can have non-local
807 * behaviour between files!
809 static struct symbol_list
*sparse_initial(void)
814 // Prepend any "include" file to the stream.
815 // We're in global scope, it will affect all files!
817 for (i
= cmdline_include_nr
- 1; i
>= 0; i
--)
818 token
= tokenize(cmdline_include
[i
].filename
, cmdline_include
[i
].fd
,
821 // Prepend the initial built-in stream
822 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
823 return sparse_tokenstream(token
);
826 struct symbol_list
*sparse_initialize(int argc
, char **argv
, struct string_list
**filelist
)
829 struct symbol_list
*list
;
831 // Initialize symbol stream first, so that we can add defines etc
840 if (arg
[0] == '-' && arg
[1]) {
841 args
= handle_switch(arg
+1, args
);
844 add_ptr_list_notag(filelist
, arg
);
846 handle_switch_W_finalize();
847 handle_switch_v_finalize();
850 if (!ptr_list_empty(filelist
)) {
851 // Initialize type system
854 create_builtin_stream();
855 add_pre_buffer("#define __CHECKER__ 1\n");
856 if (!preprocess_only
)
857 declare_builtin_functions();
859 list
= sparse_initial();
862 * Protect the initial token allocations, since
863 * they need to survive all the others
865 protect_token_alloc();
870 struct symbol_list
* sparse_keep_tokens(char *filename
)
872 struct symbol_list
*res
;
874 /* Clear previous symbol list */
875 translation_unit_used_list
= NULL
;
878 res
= sparse_file(filename
);
885 struct symbol_list
* __sparse(char *filename
)
887 struct symbol_list
*res
;
889 res
= sparse_keep_tokens(filename
);
891 /* Drop the tokens for this file after parsing */
898 struct symbol_list
* sparse(char *filename
)
900 struct symbol_list
*res
= __sparse(filename
);
902 /* Evaluate the complete symbol list */
903 evaluate_symbol_list(res
);