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"
32 int verbose
, optimize
, optimize_size
, preprocessing
;
37 # define __GNUC_MINOR__ 95
38 # define __GNUC_PATCHLEVEL__ 0
41 int gcc_major
= __GNUC__
;
42 int gcc_minor
= __GNUC_MINOR__
;
43 int gcc_patchlevel
= __GNUC_PATCHLEVEL__
;
45 static const char *gcc_base_dir
= GCC_BASE
;
47 struct token
*skip_to(struct token
*token
, int op
)
49 while (!match_op(token
, op
) && !eof_token(token
))
54 struct token
*expect(struct token
*token
, int op
, const char *where
)
56 if (!match_op(token
, op
)) {
57 static struct token bad_token
;
58 if (token
!= &bad_token
) {
59 bad_token
.next
= token
;
60 sparse_error(token
->pos
, "Expected %s %s", show_special(op
), where
);
61 sparse_error(token
->pos
, "got %s", show_token(token
));
64 return skip_to(token
, op
);
70 unsigned int hexval(unsigned int c
)
78 retval
= c
- 'a' + 10;
81 retval
= c
- 'A' + 10;
87 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
89 static char buffer
[512];
92 vsprintf(buffer
, fmt
, args
);
93 name
= stream_name(pos
.stream
);
95 fprintf(stderr
, "%s:%d:%d: %s%s\n",
96 name
, pos
.line
, pos
.pos
, type
, buffer
);
99 static int max_warnings
= 100;
100 static int show_info
= 1;
102 void info(struct position pos
, const char * fmt
, ...)
109 do_warn("", pos
, fmt
, args
);
113 void warning(struct position pos
, const char * fmt
, ...)
122 if (!--max_warnings
) {
124 fmt
= "too many warnings";
128 do_warn("warning: ", pos
, fmt
, args
);
132 static void do_error(struct position pos
, const char * fmt
, va_list args
)
134 static int errors
= 0;
137 /* Shut up warnings after an error */
144 fmt
= "too many errors";
148 do_warn("error: ", pos
, fmt
, args
);
152 void sparse_error(struct position pos
, const char * fmt
, ...)
156 do_error(pos
, fmt
, args
);
160 void expression_error(struct expression
*expr
, const char *fmt
, ...)
164 do_error(expr
->pos
, fmt
, args
);
166 expr
->ctype
= &bad_ctype
;
169 void error_die(struct position pos
, const char * fmt
, ...)
173 do_warn("error: ", pos
, fmt
, args
);
178 void die(const char *fmt
, ...)
181 static char buffer
[512];
184 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
187 fprintf(stderr
, "%s\n", buffer
);
191 static struct token
*pre_buffer_begin
= NULL
;
192 static struct token
*pre_buffer_end
= NULL
;
194 int Waddress_space
= 1;
197 int Wcast_truncate
= 1;
200 int Wdeclarationafterstatement
= -1;
201 int Wdefault_bitfield_sign
= 0;
202 int Wdesignated_init
= 1;
204 int Winit_cstring
= 0;
205 int Wenum_mismatch
= 1;
206 int Wnon_pointer_null
= 1;
207 int Wold_initializer
= 1;
208 int Wone_bit_signed_bitfield
= 1;
209 int Wparen_string
= 0;
210 int Wptr_subtraction_blows
= 0;
211 int Wreturn_void
= 0;
213 int Wtransparent_union
= 0;
216 int Wuninitialized
= 1;
224 static enum { STANDARD_C89
,
228 STANDARD_GNU99
, } standard
= STANDARD_GNU89
;
231 #define ARCH_M64_DEFAULT 1
233 #define ARCH_M64_DEFAULT 0
236 int arch_m64
= ARCH_M64_DEFAULT
;
237 int arch_msize_long
= 0;
239 #define CMDLINE_INCLUDE 20
240 static int cmdline_include_nr
= 0;
241 static char *cmdline_include
[CMDLINE_INCLUDE
];
244 void add_pre_buffer(const char *fmt
, ...)
248 struct token
*begin
, *end
;
252 size
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
254 begin
= tokenize_buffer(buffer
, size
, &end
);
255 if (!pre_buffer_begin
)
256 pre_buffer_begin
= begin
;
258 pre_buffer_end
->next
= begin
;
259 pre_buffer_end
= end
;
262 static char **handle_switch_D(char *arg
, char **next
)
264 const char *name
= arg
+ 1;
265 const char *value
= "1";
267 if (!*name
|| isspace(*name
))
268 die("argument to `-D' is missing");
275 if (isspace((unsigned char)c
) || c
== '=') {
281 add_pre_buffer("#define %s %s\n", name
, value
);
285 static char **handle_switch_E(char *arg
, char **next
)
292 static char **handle_switch_I(char *arg
, char **next
)
298 add_pre_buffer("#split_include\n");
301 case '\0': /* Plain "-I" */
304 die("missing argument for -I option");
307 add_pre_buffer("#add_include \"%s/\"\n", path
);
312 static void add_cmdline_include(char *filename
)
314 if (cmdline_include_nr
>= CMDLINE_INCLUDE
)
315 die("too many include files for %s\n", filename
);
316 cmdline_include
[cmdline_include_nr
++] = filename
;
319 static char **handle_switch_i(char *arg
, char **next
)
321 if (*next
&& !strcmp(arg
, "include"))
322 add_cmdline_include(*++next
);
323 else if (*next
&& !strcmp(arg
, "imacros"))
324 add_cmdline_include(*++next
);
325 else if (*next
&& !strcmp(arg
, "isystem")) {
326 char *path
= *++next
;
328 die("missing argument for -isystem option");
329 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
330 } else if (*next
&& !strcmp(arg
, "idirafter")) {
331 char *path
= *++next
;
333 die("missing argument for -idirafter option");
334 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
339 static char **handle_switch_M(char *arg
, char **next
)
341 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
343 die("missing argument for -%s option", arg
);
349 static char **handle_switch_m(char *arg
, char **next
)
351 if (!strcmp(arg
, "m64")) {
353 } else if (!strcmp(arg
, "m32")) {
355 } else if (!strcmp(arg
, "msize-long")) {
361 static void handle_arch_m64_finalize(void)
365 max_int_alignment
= 8;
366 bits_in_pointer
= 64;
367 pointer_alignment
= 8;
368 size_t_ctype
= &ulong_ctype
;
369 ssize_t_ctype
= &long_ctype
;
371 add_pre_buffer("#weak_define __x86_64__ 1\n");
376 static void handle_arch_msize_long_finalize(void)
378 if (arch_msize_long
) {
379 size_t_ctype
= &ulong_ctype
;
380 ssize_t_ctype
= &long_ctype
;
384 static void handle_arch_finalize(void)
386 handle_arch_m64_finalize();
387 handle_arch_msize_long_finalize();
391 static char **handle_switch_o(char *arg
, char **next
)
393 if (!strcmp (arg
, "o")) { // "-o foo"
395 die("argument to '-o' is missing");
402 static const struct warning
{
406 { "address-space", &Waddress_space
},
407 { "bitwise", &Wbitwise
},
408 { "cast-to-as", &Wcast_to_as
},
409 { "cast-truncate", &Wcast_truncate
},
410 { "context", &Wcontext
},
412 { "declaration-after-statement", &Wdeclarationafterstatement
},
413 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
414 { "designated-init", &Wdesignated_init
},
415 { "do-while", &Wdo_while
},
416 { "enum-mismatch", &Wenum_mismatch
},
417 { "init-cstring", &Winit_cstring
},
418 { "non-pointer-null", &Wnon_pointer_null
},
419 { "old-initializer", &Wold_initializer
},
420 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield
},
421 { "paren-string", &Wparen_string
},
422 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
423 { "return-void", &Wreturn_void
},
424 { "shadow", &Wshadow
},
425 { "transparent-union", &Wtransparent_union
},
426 { "typesign", &Wtypesign
},
427 { "undef", &Wundef
},
428 { "uninitialized", &Wuninitialized
},
439 static char **handle_onoff_switch(char *arg
, char **next
, const struct warning warnings
[], int n
)
441 int flag
= WARNING_ON
;
445 if (!strcmp(p
, "sparse-all")) {
446 for (i
= 0; i
< n
; i
++) {
447 if (*warnings
[i
].flag
!= WARNING_FORCE_OFF
)
448 *warnings
[i
].flag
= WARNING_ON
;
452 // Prefixes "no" and "no-" mean to turn warning off.
453 if (p
[0] == 'n' && p
[1] == 'o') {
457 flag
= WARNING_FORCE_OFF
;
460 for (i
= 0; i
< n
; i
++) {
461 if (!strcmp(p
,warnings
[i
].name
)) {
462 *warnings
[i
].flag
= flag
;
471 static char **handle_switch_W(char *arg
, char **next
)
473 char ** ret
= handle_onoff_switch(arg
, next
, warnings
, ARRAY_SIZE(warnings
));
481 static struct warning debugs
[] = {
482 { "entry", &dbg_entry
},
483 { "dead", &dbg_dead
},
487 static char **handle_switch_v(char *arg
, char **next
)
489 char ** ret
= handle_onoff_switch(arg
, next
, debugs
, ARRAY_SIZE(debugs
));
496 } while (*++arg
== 'v');
501 static void handle_onoff_switch_finalize(const struct warning warnings
[], int n
)
505 for (i
= 0; i
< n
; i
++) {
506 if (*warnings
[i
].flag
== WARNING_FORCE_OFF
)
507 *warnings
[i
].flag
= WARNING_OFF
;
511 static void handle_switch_W_finalize(void)
513 handle_onoff_switch_finalize(warnings
, ARRAY_SIZE(warnings
));
515 /* default Wdeclarationafterstatement based on the C dialect */
516 if (-1 == Wdeclarationafterstatement
)
522 Wdeclarationafterstatement
= 1;
528 Wdeclarationafterstatement
= 0;
538 static void handle_switch_v_finalize(void)
540 handle_onoff_switch_finalize(debugs
, ARRAY_SIZE(debugs
));
543 static char **handle_switch_U(char *arg
, char **next
)
545 const char *name
= arg
+ 1;
546 add_pre_buffer ("#undef %s\n", name
);
550 static char **handle_switch_O(char *arg
, char **next
)
553 if (arg
[1] >= '0' && arg
[1] <= '9')
554 level
= arg
[1] - '0';
556 optimize_size
= arg
[1] == 's';
560 static char **handle_switch_ftabstop(char *arg
, char **next
)
566 die("error: missing argument to \"-ftabstop=\"");
568 /* we silently ignore silly values */
569 val
= strtoul(arg
, &end
, 10);
570 if (*end
== '\0' && 1 <= val
&& val
<= 100)
576 static char **handle_switch_f(char *arg
, char **next
)
580 if (!strncmp(arg
, "tabstop=", 8))
581 return handle_switch_ftabstop(arg
+8, next
);
583 /* handle switches w/ arguments above, boolean and only boolean below */
585 if (!strncmp(arg
, "no-", 3)) {
588 /* handle switch here.. */
592 static char **handle_switch_G(char *arg
, char **next
)
594 if (!strcmp (arg
, "G") && *next
)
595 return next
+ 1; // "-G 0"
597 return next
; // "-G0" or (bogus) terminal "-G"
600 static char **handle_switch_a(char *arg
, char **next
)
602 if (!strcmp (arg
, "ansi"))
603 standard
= STANDARD_C89
;
608 static char **handle_switch_s(char *arg
, char **next
)
610 if (!strncmp (arg
, "std=", 4))
614 if (!strcmp (arg
, "c89") ||
615 !strcmp (arg
, "iso9899:1990"))
616 standard
= STANDARD_C89
;
618 else if (!strcmp (arg
, "iso9899:199409"))
619 standard
= STANDARD_C94
;
621 else if (!strcmp (arg
, "c99") ||
622 !strcmp (arg
, "c9x") ||
623 !strcmp (arg
, "iso9899:1999") ||
624 !strcmp (arg
, "iso9899:199x"))
625 standard
= STANDARD_C99
;
627 else if (!strcmp (arg
, "gnu89"))
628 standard
= STANDARD_GNU89
;
630 else if (!strcmp (arg
, "gnu99") || !strcmp (arg
, "gnu9x"))
631 standard
= STANDARD_GNU99
;
634 die ("Unsupported C dialect");
640 static char **handle_nostdinc(char *arg
, char **next
)
642 add_pre_buffer("#nostdinc\n");
646 static char **handle_base_dir(char *arg
, char **next
)
648 gcc_base_dir
= *++next
;
650 die("missing argument for -gcc-base-dir option");
654 static char **handle_version(char *arg
, char **next
)
656 printf("%s\n", SPARSE_VERSION
);
662 char **(*fn
)(char *, char **);
665 static char **handle_long_options(char *arg
, char **next
)
667 static struct switches cmd
[] = {
668 { "version", handle_version
},
671 struct switches
*s
= cmd
;
674 if (!strcmp(s
->name
, arg
))
675 return s
->fn(arg
, next
);
682 static char **handle_switch(char *arg
, char **next
)
684 static struct switches cmd
[] = {
685 { "nostdinc", handle_nostdinc
},
686 { "gcc-base-dir", handle_base_dir
},
692 case 'D': return handle_switch_D(arg
, next
);
693 case 'E': return handle_switch_E(arg
, next
);
694 case 'I': return handle_switch_I(arg
, next
);
695 case 'i': return handle_switch_i(arg
, next
);
696 case 'M': return handle_switch_M(arg
, next
);
697 case 'm': return handle_switch_m(arg
, next
);
698 case 'o': return handle_switch_o(arg
, next
);
699 case 'U': return handle_switch_U(arg
, next
);
700 case 'v': return handle_switch_v(arg
, next
);
701 case 'W': return handle_switch_W(arg
, next
);
702 case 'O': return handle_switch_O(arg
, next
);
703 case 'f': return handle_switch_f(arg
, next
);
704 case 'G': return handle_switch_G(arg
, next
);
705 case 'a': return handle_switch_a(arg
, next
);
706 case 's': return handle_switch_s(arg
, next
);
707 case '-': return handle_long_options(arg
+ 1, next
);
714 if (!strcmp(s
->name
, arg
))
715 return s
->fn(arg
, next
);
720 * Ignore unknown command line options:
721 * they're probably gcc switches
726 void declare_builtin_functions(void)
728 /* Gaah. gcc knows tons of builtin <string.h> functions */
729 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
730 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
731 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
732 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
733 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
734 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
735 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
736 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
737 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
738 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
739 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
740 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
741 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
742 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
743 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
745 /* And bitwise operations.. */
746 add_pre_buffer("extern int __builtin_clz(int);\n");
747 add_pre_buffer("extern int __builtin_clzl(long);\n");
748 add_pre_buffer("extern int __builtin_clzll(long long);\n");
749 add_pre_buffer("extern int __builtin_ctz(int);\n");
750 add_pre_buffer("extern int __builtin_ctzl(long);\n");
751 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
752 add_pre_buffer("extern int __builtin_ffs(int);\n");
753 add_pre_buffer("extern int __builtin_ffsl(long);\n");
754 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
755 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
756 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
757 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
759 /* And byte swaps.. */
760 add_pre_buffer("extern unsigned short __builtin_bswap16(unsigned short);\n");
761 add_pre_buffer("extern unsigned int __builtin_bswap32(unsigned int);\n");
762 add_pre_buffer("extern unsigned long long __builtin_bswap64(unsigned long long);\n");
764 /* And some random ones.. */
765 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
766 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
767 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
768 add_pre_buffer("extern void __builtin_trap(void);\n");
769 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
770 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
771 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
772 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
773 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
774 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
775 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
776 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
777 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
778 add_pre_buffer("extern long __builtin_labs(long);\n");
779 add_pre_buffer("extern double __builtin_fabs(double);\n");
780 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
781 add_pre_buffer("extern void __sync_synchronize();\n");
782 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
784 /* Add Blackfin-specific stuff */
787 "extern void __builtin_bfin_csync(void);\n"
788 "extern void __builtin_bfin_ssync(void);\n"
789 "extern int __builtin_bfin_norm_fr1x32(int);\n"
793 /* And some floating point stuff.. */
794 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
795 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
796 add_pre_buffer("extern int __builtin_isless(float, float);\n");
797 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
798 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
799 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
801 /* And some __FORTIFY_SOURCE ones.. */
802 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
803 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
804 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
805 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
806 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
807 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
808 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
809 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
810 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
811 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
812 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
813 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
814 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
815 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
816 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
819 void create_builtin_stream(void)
821 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major
);
822 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor
);
823 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
825 /* We add compiler headers path here because we have to parse
826 * the arguments to get it, falling back to default. */
827 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir
);
828 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir
);
830 add_pre_buffer("#define __extension__\n");
831 add_pre_buffer("#define __pragma__\n");
833 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
834 // solaris/sparc that is really "unsigned int" and for linux/x86_64
835 // it is "long unsigned int". In either case we can probably
836 // get away with this. We need the #weak_define as cgcc will define
837 // the right __SIZE_TYPE__.
838 if (size_t_ctype
== &ulong_ctype
)
839 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
841 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
842 add_pre_buffer("#weak_define __STDC__ 1\n");
847 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
851 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
852 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
856 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
857 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
864 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
871 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
872 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
873 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
874 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
875 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
876 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
877 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
878 add_pre_buffer("#define __builtin_va_end(arg)\n");
879 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
880 add_pre_buffer("#define __builtin_va_arg_pack()\n");
882 /* FIXME! We need to do these as special magic macros at expansion time! */
883 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
886 add_pre_buffer("#define __OPTIMIZE__ 1\n");
888 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
890 /* GCC defines these for limits.h */
891 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__
) "\n");
892 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__
) "\n");
893 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__
) "\n");
894 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__
) "\n");
895 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__
) "\n");
896 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__
) "\n");
897 add_pre_buffer("#weak_define __SIZEOF_POINTER__ " STRINGIFY(__SIZEOF_POINTER__
) "\n");
900 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
902 // Preprocess the stream
903 token
= preprocess(token
);
905 if (preprocess_only
) {
906 while (!eof_token(token
)) {
908 struct token
*next
= token
->next
;
909 const char *separator
= "";
910 if (next
->pos
.whitespace
)
912 if (next
->pos
.newline
) {
913 separator
= "\n\t\t\t\t\t";
914 prec
= next
->pos
.pos
;
918 printf("%s%.*s", show_token(token
), prec
, separator
);
926 // Parse the resulting C code
927 while (!eof_token(token
))
928 token
= external_declaration(token
, &translation_unit_used_list
);
929 return translation_unit_used_list
;
932 static struct symbol_list
*sparse_file(const char *filename
)
937 if (strcmp (filename
, "-") == 0) {
940 fd
= open(filename
, O_RDONLY
);
942 die("No such file: %s", filename
);
945 // Tokenize the input stream
946 token
= tokenize(filename
, fd
, NULL
, includepath
);
949 return sparse_tokenstream(token
);
953 * This handles the "-include" directive etc: we're in global
954 * scope, and all types/macros etc will affect all the following
957 * NOTE NOTE NOTE! "#undef" of anything in this stage will
958 * affect all subsequent files too, i.e. we can have non-local
959 * behaviour between files!
961 static struct symbol_list
*sparse_initial(void)
965 // Prepend any "include" file to the stream.
966 // We're in global scope, it will affect all files!
967 for (i
= 0; i
< cmdline_include_nr
; i
++)
968 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include
[i
]);
970 return sparse_tokenstream(pre_buffer_begin
);
973 struct symbol_list
*sparse_initialize(int argc
, char **argv
, struct string_list
**filelist
)
976 struct symbol_list
*list
;
978 // Initialize symbol stream first, so that we can add defines etc
987 if (arg
[0] == '-' && arg
[1]) {
988 args
= handle_switch(arg
+1, args
);
991 add_ptr_list_notag(filelist
, arg
);
993 handle_switch_W_finalize();
994 handle_switch_v_finalize();
996 handle_arch_finalize();
999 if (!ptr_list_empty(filelist
)) {
1000 // Initialize type system
1003 create_builtin_stream();
1004 add_pre_buffer("#define __CHECKER__ 1\n");
1005 if (!preprocess_only
)
1006 declare_builtin_functions();
1008 list
= sparse_initial();
1011 * Protect the initial token allocations, since
1012 * they need to survive all the others
1014 protect_token_alloc();
1019 struct symbol_list
* sparse_keep_tokens(char *filename
)
1021 struct symbol_list
*res
;
1023 /* Clear previous symbol list */
1024 translation_unit_used_list
= NULL
;
1027 res
= sparse_file(filename
);
1034 struct symbol_list
* __sparse(char *filename
)
1036 struct symbol_list
*res
;
1038 res
= sparse_keep_tokens(filename
);
1040 /* Drop the tokens for this file after parsing */
1041 clear_token_alloc();
1047 struct symbol_list
* sparse(char *filename
)
1049 struct symbol_list
*res
= __sparse(filename
);
1051 /* Evaluate the complete symbol list */
1052 evaluate_symbol_list(res
);