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 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;
207 int Wold_initializer
= 1;
208 int Wnon_pointer_null
= 1;
209 int Wparen_string
= 0;
210 int Wreturn_void
= 0;
217 #define CMDLINE_INCLUDE 20
218 int cmdline_include_nr
= 0;
219 struct cmdline_include cmdline_include
[CMDLINE_INCLUDE
];
222 void add_pre_buffer(const char *fmt
, ...)
228 size
= pre_buffer_size
;
229 size
+= vsnprintf(pre_buffer
+ size
,
230 sizeof(pre_buffer
) - size
,
232 pre_buffer_size
= size
;
236 static char **handle_switch_D(char *arg
, char **next
)
238 const char *name
= arg
+ 1;
239 const char *value
= "1";
245 if (isspace((unsigned char)c
) || c
== '=') {
251 add_pre_buffer("#define %s %s\n", name
, value
);
255 static char **handle_switch_E(char *arg
, char **next
)
262 static char **handle_switch_I(char *arg
, char **next
)
268 add_pre_buffer("#split_include\n");
271 case '\0': /* Plain "-I" */
274 die("missing argument for -I option");
277 add_pre_buffer("#add_include \"%s/\"\n", path
);
282 static void add_cmdline_include(char *filename
)
284 int fd
= open(filename
, O_RDONLY
);
289 if (cmdline_include_nr
>= CMDLINE_INCLUDE
)
290 die("too many include files for %s\n", filename
);
291 cmdline_include
[cmdline_include_nr
].filename
= filename
;
292 cmdline_include
[cmdline_include_nr
].fd
= fd
;
293 cmdline_include_nr
++;
296 static char **handle_switch_i(char *arg
, char **next
)
298 if (*next
&& !strcmp(arg
, "include"))
299 add_cmdline_include(*++next
);
300 else if (*next
&& !strcmp(arg
, "imacros"))
301 add_cmdline_include(*++next
);
302 else if (*next
&& !strcmp(arg
, "isystem")) {
303 char *path
= *++next
;
305 die("missing argument for -isystem option");
306 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
311 static char **handle_switch_M(char *arg
, char **next
)
313 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
315 die("missing argument for -%s option", arg
);
321 static char **handle_switch_m(char *arg
, char **next
)
323 if (!strcmp(arg
, "m64")) {
325 max_int_alignment
= 8;
326 bits_in_pointer
= 64;
327 pointer_alignment
= 8;
328 size_t_ctype
= &ulong_ctype
;
329 ssize_t_ctype
= &long_ctype
;
330 } else if (!strcmp(arg
, "msize-long")) {
331 size_t_ctype
= &ulong_ctype
;
332 ssize_t_ctype
= &long_ctype
;
337 static char **handle_switch_o(char *arg
, char **next
)
339 if (!strcmp (arg
, "o") && *next
)
340 return next
+ 1; // "-o foo"
342 return next
; // "-ofoo" or (bogus) terminal "-o"
345 static const struct warning
{
349 { "cast-to-as", &Wcast_to_address_space
},
351 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield
},
352 { "cast-truncate", &Wcast_truncate
},
353 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
354 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
355 { "undef", &Wundefined_preprocessor
},
356 { "bitwise", &Wbitwise
},
357 { "typesign", &Wtypesign
},
358 { "context", &Wcontext
},
359 { "transparent-union", &Wtransparent_union
},
360 { "shadow", &Wshadow
},
361 { "address-space", &Waddress_space
},
362 { "enum-mismatch", &Wenum_mismatch
},
363 { "do-while", &Wdo_while
},
364 { "uninitialized", &Wuninitialized
},
365 { "old-initializer", &Wold_initializer
},
366 { "non-pointer-null", &Wnon_pointer_null
},
367 { "paren-string", &Wparen_string
},
368 { "return-void", &Wreturn_void
},
378 static char **handle_onoff_switch(char *arg
, char **next
, const struct warning warnings
[], int n
)
380 int flag
= WARNING_ON
;
384 if (!strcmp(p
, "all")) {
385 for (i
= 0; i
< n
; i
++) {
386 if (*warnings
[i
].flag
!= WARNING_FORCE_OFF
)
387 *warnings
[i
].flag
= WARNING_ON
;
391 // Prefixes "no" and "no-" mean to turn warning off.
392 if (p
[0] == 'n' && p
[1] == 'o') {
396 flag
= WARNING_FORCE_OFF
;
399 for (i
= 0; i
< n
; i
++) {
400 if (!strcmp(p
,warnings
[i
].name
)) {
401 *warnings
[i
].flag
= flag
;
410 static char **handle_switch_W(char *arg
, char **next
)
412 char ** ret
= handle_onoff_switch(arg
, next
, warnings
, sizeof warnings
/sizeof warnings
[0]);
420 static struct warning debugs
[] = {
421 { "entry", &dbg_entry
},
422 { "dead", &dbg_dead
},
426 static char **handle_switch_v(char *arg
, char **next
)
428 char ** ret
= handle_onoff_switch(arg
, next
, debugs
, sizeof debugs
/sizeof debugs
[0]);
435 } while (*++arg
== 'v');
440 static void handle_onoff_switch_finalize(const struct warning warnings
[], int n
)
444 for (i
= 0; i
< n
; i
++) {
445 if (*warnings
[i
].flag
== WARNING_FORCE_OFF
)
446 *warnings
[i
].flag
= WARNING_OFF
;
450 static void handle_switch_W_finalize(void)
452 handle_onoff_switch_finalize(warnings
, sizeof(warnings
) / sizeof(warnings
[0]));
455 static void handle_switch_v_finalize(void)
457 handle_onoff_switch_finalize(debugs
, sizeof(debugs
) / sizeof(debugs
[0]));
460 static char **handle_switch_U(char *arg
, char **next
)
462 const char *name
= arg
+ 1;
463 add_pre_buffer ("#undef %s\n", name
);
467 static char **handle_switch_O(char *arg
, char **next
)
470 if (arg
[1] >= '0' && arg
[1] <= '9')
471 level
= arg
[1] - '0';
473 optimize_size
= arg
[1] == 's';
477 static char **handle_switch_f(char *arg
, char **next
)
482 if (!strncmp(arg
, "no-", 3)) {
486 /* handle switch here.. */
490 static char **handle_switch_G(char *arg
, char **next
)
492 if (!strcmp (arg
, "G") && *next
)
493 return next
+ 1; // "-G 0"
495 return next
; // "-G0" or (bogus) terminal "-G"
498 static char **handle_nostdinc(char *arg
, char **next
)
500 add_pre_buffer("#nostdinc\n");
504 static char **handle_dirafter(char *arg
, char **next
)
506 char *path
= *++next
;
508 die("missing argument for -dirafter option");
509 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
515 char **(*fn
)(char *, char **);
518 char **handle_switch(char *arg
, char **next
)
520 static struct switches cmd
[] = {
521 { "nostdinc", handle_nostdinc
},
522 { "dirafter", handle_dirafter
},
528 case 'D': return handle_switch_D(arg
, next
);
529 case 'E': return handle_switch_E(arg
, next
);
530 case 'I': return handle_switch_I(arg
, next
);
531 case 'i': return handle_switch_i(arg
, next
);
532 case 'M': return handle_switch_M(arg
, next
);
533 case 'm': return handle_switch_m(arg
, next
);
534 case 'o': return handle_switch_o(arg
, next
);
535 case 'U': return handle_switch_U(arg
, next
);
536 case 'v': return handle_switch_v(arg
, next
);
537 case 'W': return handle_switch_W(arg
, next
);
538 case 'O': return handle_switch_O(arg
, next
);
539 case 'f': return handle_switch_f(arg
, next
);
540 case 'G': return handle_switch_G(arg
, next
);
547 if (!strcmp(s
->name
, arg
))
548 return s
->fn(arg
, next
);
553 * Ignore unknown command line options:
554 * they're probably gcc switches
559 void declare_builtin_functions(void)
561 /* Gaah. gcc knows tons of builtin <string.h> functions */
562 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
563 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
564 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
565 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
566 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
567 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
568 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
569 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
570 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
571 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
572 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
573 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
575 /* And some random ones.. */
576 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
577 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
578 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
579 add_pre_buffer("extern void __builtin_trap(void);\n");
580 add_pre_buffer("extern int __builtin_ffs(int);\n");
581 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
582 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
583 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
584 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
585 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
586 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
587 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
588 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
589 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
590 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
591 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
594 void create_builtin_stream(void)
596 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major
);
597 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor
);
598 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
599 add_pre_buffer("#define __extension__\n");
600 add_pre_buffer("#define __pragma__\n");
602 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
603 // solaris/sparc that is really "unsigned int" and for linux/x86_64
604 // it is "long unsigned int". In either case we can probably
605 // get away with this. We need the #weak_define as cgcc will define
606 // the right __SIZE_TYPE__.
607 if (size_t_ctype
== &ulong_ctype
)
608 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
610 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
611 add_pre_buffer("#weak_define __STDC__ 1\n");
613 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
614 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
615 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
616 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
617 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
618 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
619 add_pre_buffer("#define __builtin_va_end(arg)\n");
621 /* FIXME! We need to do these as special magic macros at expansion time! */
622 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
625 add_pre_buffer("#define __OPTIMIZE__ 1\n");
627 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
630 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
632 // Preprocess the stream
633 token
= preprocess(token
);
635 if (preprocess_only
) {
636 while (!eof_token(token
)) {
638 struct token
*next
= token
->next
;
639 const char *separator
= "";
640 if (next
->pos
.whitespace
)
642 if (next
->pos
.newline
) {
643 separator
= "\n\t\t\t\t\t";
644 prec
= next
->pos
.pos
;
648 printf("%s%.*s", show_token(token
), prec
, separator
);
656 // Parse the resulting C code
657 while (!eof_token(token
))
658 token
= external_declaration(token
, &translation_unit_used_list
);
659 return translation_unit_used_list
;
662 static struct symbol_list
*sparse_file(const char *filename
)
667 if (strcmp (filename
, "-") == 0) {
670 fd
= open(filename
, O_RDONLY
);
672 die("No such file: %s", filename
);
675 // Tokenize the input stream
676 token
= tokenize(filename
, fd
, NULL
, includepath
);
679 return sparse_tokenstream(token
);
683 * This handles the "-include" directive etc: we're in global
684 * scope, and all types/macros etc will affect all the following
687 * NOTE NOTE NOTE! "#undef" of anything in this stage will
688 * affect all subsequent files too, i.e. we can have non-local
689 * behaviour between files!
691 static struct symbol_list
*sparse_initial(void)
696 // Prepend any "include" file to the stream.
697 // We're in global scope, it will affect all files!
699 for (i
= cmdline_include_nr
- 1; i
>= 0; i
--)
700 token
= tokenize(cmdline_include
[i
].filename
, cmdline_include
[i
].fd
,
703 // Prepend the initial built-in stream
704 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
705 return sparse_tokenstream(token
);
708 struct symbol_list
*sparse_initialize(int argc
, char **argv
, struct string_list
**filelist
)
711 struct symbol_list
*list
;
713 // Initialize symbol stream first, so that we can add defines etc
722 if (arg
[0] == '-' && arg
[1]) {
723 args
= handle_switch(arg
+1, args
);
726 add_ptr_list_notag(filelist
, arg
);
728 handle_switch_W_finalize();
729 handle_switch_v_finalize();
732 if (!ptr_list_empty(filelist
)) {
733 // Initialize type system
736 create_builtin_stream();
737 add_pre_buffer("#define __CHECKER__ 1\n");
738 if (!preprocess_only
)
739 declare_builtin_functions();
741 list
= sparse_initial();
744 * Protect the initial token allocations, since
745 * they need to survive all the others
747 protect_token_alloc();
752 struct symbol_list
* sparse_keep_tokens(char *filename
)
754 struct symbol_list
*res
;
756 /* Clear previous symbol list */
757 translation_unit_used_list
= NULL
;
760 res
= sparse_file(filename
);
767 struct symbol_list
* __sparse(char *filename
)
769 struct symbol_list
*res
;
771 res
= sparse_keep_tokens(filename
);
773 /* Drop the tokens for this file after parsing */
780 struct symbol_list
* sparse(char *filename
)
782 struct symbol_list
*res
= __sparse(filename
);
784 /* Evaluate the complete symbol list */
785 evaluate_symbol_list(res
);