2 * 'sparse' library helper routines.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35 #include <sys/types.h>
42 #include "expression.h"
44 #include "linearize.h"
48 int verbose
, optimize
, optimize_size
, preprocessing
;
53 # define __GNUC_MINOR__ 95
54 # define __GNUC_PATCHLEVEL__ 0
57 int gcc_major
= __GNUC__
;
58 int gcc_minor
= __GNUC_MINOR__
;
59 int gcc_patchlevel
= __GNUC_PATCHLEVEL__
;
61 static const char *gcc_base_dir
= GCC_BASE
;
63 struct token
*skip_to(struct token
*token
, int op
)
65 while (!match_op(token
, op
) && !eof_token(token
))
70 struct token
*expect(struct token
*token
, int op
, const char *where
)
72 if (!match_op(token
, op
)) {
73 static struct token bad_token
;
74 if (token
!= &bad_token
) {
75 bad_token
.next
= token
;
76 sparse_error(token
->pos
, "Expected %s %s", show_special(op
), where
);
77 sparse_error(token
->pos
, "got %s", show_token(token
));
80 return skip_to(token
, op
);
86 unsigned int hexval(unsigned int c
)
94 retval
= c
- 'a' + 10;
97 retval
= c
- 'A' + 10;
103 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
105 static char buffer
[512];
108 vsprintf(buffer
, fmt
, args
);
109 name
= stream_name(pos
.stream
);
111 fprintf(stderr
, "%s:%d:%d: %s%s\n",
112 name
, pos
.line
, pos
.pos
, type
, buffer
);
115 static int max_warnings
= 100;
116 static int show_info
= 1;
118 void info(struct position pos
, const char * fmt
, ...)
125 do_warn("", pos
, fmt
, args
);
129 void warning(struct position pos
, const char * fmt
, ...)
138 if (!--max_warnings
) {
140 fmt
= "too many warnings";
144 do_warn("warning: ", pos
, fmt
, args
);
148 static void do_error(struct position pos
, const char * fmt
, va_list args
)
150 static int errors
= 0;
153 /* Shut up warnings after an error */
160 fmt
= "too many errors";
164 do_warn("error: ", pos
, fmt
, args
);
168 void sparse_error(struct position pos
, const char * fmt
, ...)
172 do_error(pos
, fmt
, args
);
176 void expression_error(struct expression
*expr
, const char *fmt
, ...)
180 do_error(expr
->pos
, fmt
, args
);
182 expr
->ctype
= &bad_ctype
;
185 void error_die(struct position pos
, const char * fmt
, ...)
189 do_warn("error: ", pos
, fmt
, args
);
194 void die(const char *fmt
, ...)
197 static char buffer
[512];
200 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
203 fprintf(stderr
, "%s\n", buffer
);
207 static struct token
*pre_buffer_begin
= NULL
;
208 static struct token
*pre_buffer_end
= NULL
;
210 int Waddress_space
= 1;
213 int Wcast_truncate
= 1;
216 int Wdeclarationafterstatement
= -1;
217 int Wdefault_bitfield_sign
= 0;
218 int Wdesignated_init
= 1;
220 int Winit_cstring
= 0;
221 int Wenum_mismatch
= 1;
222 int Wnon_pointer_null
= 1;
223 int Wold_initializer
= 1;
224 int Wone_bit_signed_bitfield
= 1;
225 int Wparen_string
= 0;
226 int Wptr_subtraction_blows
= 0;
227 int Wreturn_void
= 0;
229 int Wtransparent_union
= 0;
232 int Wuninitialized
= 1;
240 static enum { STANDARD_C89
,
244 STANDARD_GNU99
, } standard
= STANDARD_GNU89
;
247 #define ARCH_M64_DEFAULT 1
249 #define ARCH_M64_DEFAULT 0
252 int arch_m64
= ARCH_M64_DEFAULT
;
253 int arch_msize_long
= 0;
255 #define CMDLINE_INCLUDE 20
256 static int cmdline_include_nr
= 0;
257 static char *cmdline_include
[CMDLINE_INCLUDE
];
260 void add_pre_buffer(const char *fmt
, ...)
264 struct token
*begin
, *end
;
268 size
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
270 begin
= tokenize_buffer(buffer
, size
, &end
);
271 if (!pre_buffer_begin
)
272 pre_buffer_begin
= begin
;
274 pre_buffer_end
->next
= begin
;
275 pre_buffer_end
= end
;
278 static char **handle_switch_D(char *arg
, char **next
)
280 const char *name
= arg
+ 1;
281 const char *value
= "1";
283 if (!*name
|| isspace(*name
))
284 die("argument to `-D' is missing");
291 if (isspace((unsigned char)c
) || c
== '=') {
297 add_pre_buffer("#define %s %s\n", name
, value
);
301 static char **handle_switch_E(char *arg
, char **next
)
308 static char **handle_switch_I(char *arg
, char **next
)
314 add_pre_buffer("#split_include\n");
317 case '\0': /* Plain "-I" */
320 die("missing argument for -I option");
323 add_pre_buffer("#add_include \"%s/\"\n", path
);
328 static void add_cmdline_include(char *filename
)
330 if (cmdline_include_nr
>= CMDLINE_INCLUDE
)
331 die("too many include files for %s\n", filename
);
332 cmdline_include
[cmdline_include_nr
++] = filename
;
335 static char **handle_switch_i(char *arg
, char **next
)
337 if (*next
&& !strcmp(arg
, "include"))
338 add_cmdline_include(*++next
);
339 else if (*next
&& !strcmp(arg
, "imacros"))
340 add_cmdline_include(*++next
);
341 else if (*next
&& !strcmp(arg
, "isystem")) {
342 char *path
= *++next
;
344 die("missing argument for -isystem option");
345 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
346 } else if (*next
&& !strcmp(arg
, "idirafter")) {
347 char *path
= *++next
;
349 die("missing argument for -idirafter option");
350 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
355 static char **handle_switch_M(char *arg
, char **next
)
357 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
359 die("missing argument for -%s option", arg
);
365 static char **handle_switch_m(char *arg
, char **next
)
367 if (!strcmp(arg
, "m64")) {
369 } else if (!strcmp(arg
, "m32")) {
371 } else if (!strcmp(arg
, "msize-long")) {
377 static void handle_arch_m64_finalize(void)
381 max_int_alignment
= 8;
382 bits_in_pointer
= 64;
383 pointer_alignment
= 8;
384 size_t_ctype
= &ulong_ctype
;
385 ssize_t_ctype
= &long_ctype
;
387 add_pre_buffer("#weak_define __x86_64__ 1\n");
392 static void handle_arch_msize_long_finalize(void)
394 if (arch_msize_long
) {
395 size_t_ctype
= &ulong_ctype
;
396 ssize_t_ctype
= &long_ctype
;
400 static void handle_arch_finalize(void)
402 handle_arch_m64_finalize();
403 handle_arch_msize_long_finalize();
407 static char **handle_switch_o(char *arg
, char **next
)
409 if (!strcmp (arg
, "o")) { // "-o foo"
411 die("argument to '-o' is missing");
418 static const struct warning
{
422 { "address-space", &Waddress_space
},
423 { "bitwise", &Wbitwise
},
424 { "cast-to-as", &Wcast_to_as
},
425 { "cast-truncate", &Wcast_truncate
},
426 { "context", &Wcontext
},
428 { "declaration-after-statement", &Wdeclarationafterstatement
},
429 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
430 { "designated-init", &Wdesignated_init
},
431 { "do-while", &Wdo_while
},
432 { "enum-mismatch", &Wenum_mismatch
},
433 { "init-cstring", &Winit_cstring
},
434 { "non-pointer-null", &Wnon_pointer_null
},
435 { "old-initializer", &Wold_initializer
},
436 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield
},
437 { "paren-string", &Wparen_string
},
438 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
439 { "return-void", &Wreturn_void
},
440 { "shadow", &Wshadow
},
441 { "transparent-union", &Wtransparent_union
},
442 { "typesign", &Wtypesign
},
443 { "undef", &Wundef
},
444 { "uninitialized", &Wuninitialized
},
455 static char **handle_onoff_switch(char *arg
, char **next
, const struct warning warnings
[], int n
)
457 int flag
= WARNING_ON
;
461 if (!strcmp(p
, "sparse-all")) {
462 for (i
= 0; i
< n
; i
++) {
463 if (*warnings
[i
].flag
!= WARNING_FORCE_OFF
)
464 *warnings
[i
].flag
= WARNING_ON
;
468 // Prefixes "no" and "no-" mean to turn warning off.
469 if (p
[0] == 'n' && p
[1] == 'o') {
473 flag
= WARNING_FORCE_OFF
;
476 for (i
= 0; i
< n
; i
++) {
477 if (!strcmp(p
,warnings
[i
].name
)) {
478 *warnings
[i
].flag
= flag
;
487 static char **handle_switch_W(char *arg
, char **next
)
489 char ** ret
= handle_onoff_switch(arg
, next
, warnings
, ARRAY_SIZE(warnings
));
497 static struct warning debugs
[] = {
498 { "entry", &dbg_entry
},
499 { "dead", &dbg_dead
},
503 static char **handle_switch_v(char *arg
, char **next
)
505 char ** ret
= handle_onoff_switch(arg
, next
, debugs
, ARRAY_SIZE(debugs
));
512 } while (*++arg
== 'v');
517 static void handle_onoff_switch_finalize(const struct warning warnings
[], int n
)
521 for (i
= 0; i
< n
; i
++) {
522 if (*warnings
[i
].flag
== WARNING_FORCE_OFF
)
523 *warnings
[i
].flag
= WARNING_OFF
;
527 static void handle_switch_W_finalize(void)
529 handle_onoff_switch_finalize(warnings
, ARRAY_SIZE(warnings
));
531 /* default Wdeclarationafterstatement based on the C dialect */
532 if (-1 == Wdeclarationafterstatement
)
538 Wdeclarationafterstatement
= 1;
544 Wdeclarationafterstatement
= 0;
554 static void handle_switch_v_finalize(void)
556 handle_onoff_switch_finalize(debugs
, ARRAY_SIZE(debugs
));
559 static char **handle_switch_U(char *arg
, char **next
)
561 const char *name
= arg
+ 1;
562 add_pre_buffer ("#undef %s\n", name
);
566 static char **handle_switch_O(char *arg
, char **next
)
569 if (arg
[1] >= '0' && arg
[1] <= '9')
570 level
= arg
[1] - '0';
572 optimize_size
= arg
[1] == 's';
576 static char **handle_switch_ftabstop(char *arg
, char **next
)
582 die("error: missing argument to \"-ftabstop=\"");
584 /* we silently ignore silly values */
585 val
= strtoul(arg
, &end
, 10);
586 if (*end
== '\0' && 1 <= val
&& val
<= 100)
592 static char **handle_switch_f(char *arg
, char **next
)
596 if (!strncmp(arg
, "tabstop=", 8))
597 return handle_switch_ftabstop(arg
+8, next
);
599 /* handle switches w/ arguments above, boolean and only boolean below */
601 if (!strncmp(arg
, "no-", 3)) {
604 /* handle switch here.. */
608 static char **handle_switch_G(char *arg
, char **next
)
610 if (!strcmp (arg
, "G") && *next
)
611 return next
+ 1; // "-G 0"
613 return next
; // "-G0" or (bogus) terminal "-G"
616 static char **handle_switch_a(char *arg
, char **next
)
618 if (!strcmp (arg
, "ansi"))
619 standard
= STANDARD_C89
;
624 static char **handle_switch_s(char *arg
, char **next
)
626 if (!strncmp (arg
, "std=", 4))
630 if (!strcmp (arg
, "c89") ||
631 !strcmp (arg
, "iso9899:1990"))
632 standard
= STANDARD_C89
;
634 else if (!strcmp (arg
, "iso9899:199409"))
635 standard
= STANDARD_C94
;
637 else if (!strcmp (arg
, "c99") ||
638 !strcmp (arg
, "c9x") ||
639 !strcmp (arg
, "iso9899:1999") ||
640 !strcmp (arg
, "iso9899:199x"))
641 standard
= STANDARD_C99
;
643 else if (!strcmp (arg
, "gnu89"))
644 standard
= STANDARD_GNU89
;
646 else if (!strcmp (arg
, "gnu99") || !strcmp (arg
, "gnu9x"))
647 standard
= STANDARD_GNU99
;
650 die ("Unsupported C dialect");
656 static char **handle_nostdinc(char *arg
, char **next
)
658 add_pre_buffer("#nostdinc\n");
662 static char **handle_base_dir(char *arg
, char **next
)
664 gcc_base_dir
= *++next
;
666 die("missing argument for -gcc-base-dir option");
670 static char **handle_no_lineno(char *arg
, char **next
)
676 static char **handle_version(char *arg
, char **next
)
678 printf("%s\n", SPARSE_VERSION
);
684 char **(*fn
)(char *, char **);
687 static char **handle_long_options(char *arg
, char **next
)
689 static struct switches cmd
[] = {
690 { "version", handle_version
},
693 struct switches
*s
= cmd
;
696 if (!strcmp(s
->name
, arg
))
697 return s
->fn(arg
, next
);
704 static char **handle_switch(char *arg
, char **next
)
706 static struct switches cmd
[] = {
707 { "nostdinc", handle_nostdinc
},
708 { "gcc-base-dir", handle_base_dir
},
709 { "no-lineno", handle_no_lineno
},
715 case 'D': return handle_switch_D(arg
, next
);
716 case 'E': return handle_switch_E(arg
, next
);
717 case 'I': return handle_switch_I(arg
, next
);
718 case 'i': return handle_switch_i(arg
, next
);
719 case 'M': return handle_switch_M(arg
, next
);
720 case 'm': return handle_switch_m(arg
, next
);
721 case 'o': return handle_switch_o(arg
, next
);
722 case 'U': return handle_switch_U(arg
, next
);
723 case 'v': return handle_switch_v(arg
, next
);
724 case 'W': return handle_switch_W(arg
, next
);
725 case 'O': return handle_switch_O(arg
, next
);
726 case 'f': return handle_switch_f(arg
, next
);
727 case 'G': return handle_switch_G(arg
, next
);
728 case 'a': return handle_switch_a(arg
, next
);
729 case 's': return handle_switch_s(arg
, next
);
730 case '-': return handle_long_options(arg
+ 1, next
);
737 if (!strcmp(s
->name
, arg
))
738 return s
->fn(arg
, next
);
743 * Ignore unknown command line options:
744 * they're probably gcc switches
749 void declare_builtin_functions(void)
751 /* Gaah. gcc knows tons of builtin <string.h> functions */
752 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
753 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
754 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
755 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
756 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
757 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
758 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
759 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
760 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
761 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
762 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
763 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
764 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
765 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
766 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
768 /* And bitwise operations.. */
769 add_pre_buffer("extern int __builtin_clz(int);\n");
770 add_pre_buffer("extern int __builtin_clzl(long);\n");
771 add_pre_buffer("extern int __builtin_clzll(long long);\n");
772 add_pre_buffer("extern int __builtin_ctz(int);\n");
773 add_pre_buffer("extern int __builtin_ctzl(long);\n");
774 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
775 add_pre_buffer("extern int __builtin_ffs(int);\n");
776 add_pre_buffer("extern int __builtin_ffsl(long);\n");
777 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
778 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
779 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
780 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
782 /* And byte swaps.. */
783 add_pre_buffer("extern unsigned short __builtin_bswap16(unsigned short);\n");
784 add_pre_buffer("extern unsigned int __builtin_bswap32(unsigned int);\n");
785 add_pre_buffer("extern unsigned long long __builtin_bswap64(unsigned long long);\n");
787 /* And atomic memory access functions.. */
788 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
789 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
790 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
791 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
792 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
793 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
794 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
795 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
796 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
797 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
798 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
799 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
800 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
801 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
802 add_pre_buffer("extern void __sync_synchronize();\n");
803 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
804 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
806 /* And some random ones.. */
807 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
808 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
809 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
810 add_pre_buffer("extern void __builtin_trap(void);\n");
811 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
812 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
813 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
814 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
815 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
816 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
817 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
818 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
819 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
820 add_pre_buffer("extern long __builtin_labs(long);\n");
821 add_pre_buffer("extern double __builtin_fabs(double);\n");
822 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
824 /* Add Blackfin-specific stuff */
827 "extern void __builtin_bfin_csync(void);\n"
828 "extern void __builtin_bfin_ssync(void);\n"
829 "extern int __builtin_bfin_norm_fr1x32(int);\n"
833 /* And some floating point stuff.. */
834 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
835 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
836 add_pre_buffer("extern int __builtin_isless(float, float);\n");
837 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
838 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
839 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
841 /* And some __FORTIFY_SOURCE ones.. */
842 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
843 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
844 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
845 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
846 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
847 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
848 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
849 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
850 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
851 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
852 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
853 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
854 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
855 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
856 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
859 void create_builtin_stream(void)
861 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major
);
862 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor
);
863 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
865 /* We add compiler headers path here because we have to parse
866 * the arguments to get it, falling back to default. */
867 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir
);
868 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir
);
870 add_pre_buffer("#define __extension__\n");
871 add_pre_buffer("#define __pragma__\n");
873 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
874 // solaris/sparc that is really "unsigned int" and for linux/x86_64
875 // it is "long unsigned int". In either case we can probably
876 // get away with this. We need the #weak_define as cgcc will define
877 // the right __SIZE_TYPE__.
878 if (size_t_ctype
== &ulong_ctype
)
879 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
881 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
882 add_pre_buffer("#weak_define __STDC__ 1\n");
887 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
891 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
892 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
896 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
897 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
904 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
911 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
912 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
913 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
914 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
915 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
916 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
917 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
918 add_pre_buffer("#define __builtin_va_end(arg)\n");
919 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
920 add_pre_buffer("#define __builtin_va_arg_pack()\n");
922 /* FIXME! We need to do these as special magic macros at expansion time! */
923 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
926 add_pre_buffer("#define __OPTIMIZE__ 1\n");
928 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
930 /* GCC defines these for limits.h */
931 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__
) "\n");
932 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__
) "\n");
933 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__
) "\n");
934 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__
) "\n");
935 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__
) "\n");
936 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__
) "\n");
937 add_pre_buffer("#weak_define __SIZEOF_POINTER__ " STRINGIFY(__SIZEOF_POINTER__
) "\n");
938 add_pre_buffer("#weak_define __CHAR_BIT__ " STRINGIFY(__CHAR_BIT__
) "\n");
941 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
943 // Preprocess the stream
944 token
= preprocess(token
);
946 if (preprocess_only
) {
947 while (!eof_token(token
)) {
949 struct token
*next
= token
->next
;
950 const char *separator
= "";
951 if (next
->pos
.whitespace
)
953 if (next
->pos
.newline
) {
954 separator
= "\n\t\t\t\t\t";
955 prec
= next
->pos
.pos
;
959 printf("%s%.*s", show_token(token
), prec
, separator
);
967 // Parse the resulting C code
968 while (!eof_token(token
))
969 token
= external_declaration(token
, &translation_unit_used_list
);
970 return translation_unit_used_list
;
973 static struct symbol_list
*sparse_file(const char *filename
)
978 if (strcmp (filename
, "-") == 0) {
981 fd
= open(filename
, O_RDONLY
);
983 die("No such file: %s", filename
);
986 // Tokenize the input stream
987 token
= tokenize(filename
, fd
, NULL
, includepath
);
988 store_all_tokens(token
);
991 return sparse_tokenstream(token
);
995 * This handles the "-include" directive etc: we're in global
996 * scope, and all types/macros etc will affect all the following
999 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1000 * affect all subsequent files too, i.e. we can have non-local
1001 * behaviour between files!
1003 static struct symbol_list
*sparse_initial(void)
1007 // Prepend any "include" file to the stream.
1008 // We're in global scope, it will affect all files!
1009 for (i
= 0; i
< cmdline_include_nr
; i
++)
1010 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include
[i
]);
1012 return sparse_tokenstream(pre_buffer_begin
);
1015 struct symbol_list
*sparse_initialize(int argc
, char **argv
, struct string_list
**filelist
)
1018 struct symbol_list
*list
;
1020 // Initialize symbol stream first, so that we can add defines etc
1025 char *arg
= *++args
;
1029 if (arg
[0] == '-' && arg
[1]) {
1030 args
= handle_switch(arg
+1, args
);
1033 add_ptr_list_notag(filelist
, arg
);
1035 handle_switch_W_finalize();
1036 handle_switch_v_finalize();
1038 handle_arch_finalize();
1041 if (!ptr_list_empty(filelist
)) {
1042 // Initialize type system
1045 create_builtin_stream();
1046 add_pre_buffer("#define __CHECKER__ 1\n");
1047 if (!preprocess_only
)
1048 declare_builtin_functions();
1050 list
= sparse_initial();
1053 * Protect the initial token allocations, since
1054 * they need to survive all the others
1056 protect_token_alloc();
1061 struct symbol_list
* sparse_keep_tokens(char *filename
)
1063 struct symbol_list
*res
;
1065 /* Clear previous symbol list */
1066 translation_unit_used_list
= NULL
;
1069 res
= sparse_file(filename
);
1076 struct symbol_list
* __sparse(char *filename
)
1078 struct symbol_list
*res
;
1080 res
= sparse_keep_tokens(filename
);
1082 /* Drop the tokens for this file after parsing */
1083 clear_token_alloc();
1089 struct symbol_list
* sparse(char *filename
)
1091 struct symbol_list
*res
= __sparse(filename
);
1093 /* Evaluate the complete symbol list */
1094 evaluate_symbol_list(res
);