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
;
62 static const char *multiarch_dir
= MULTIARCH_TRIPLET
;
64 struct token
*skip_to(struct token
*token
, int op
)
66 while (!match_op(token
, op
) && !eof_token(token
))
71 struct token
*expect(struct token
*token
, int op
, const char *where
)
73 if (!match_op(token
, op
)) {
74 static struct token bad_token
;
75 if (token
!= &bad_token
) {
76 bad_token
.next
= token
;
77 sparse_error(token
->pos
, "Expected %s %s", show_special(op
), where
);
78 sparse_error(token
->pos
, "got %s", show_token(token
));
81 return skip_to(token
, op
);
87 unsigned int hexval(unsigned int c
)
95 retval
= c
- 'a' + 10;
98 retval
= c
- 'A' + 10;
104 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
106 static char buffer
[512];
109 vsprintf(buffer
, fmt
, args
);
110 name
= stream_name(pos
.stream
);
112 fprintf(stderr
, "%s:%d:%d: %s%s\n",
113 name
, pos
.line
, pos
.pos
, type
, buffer
);
116 static int max_warnings
= 100;
117 static int show_info
= 1;
119 void info(struct position pos
, const char * fmt
, ...)
126 do_warn("", pos
, fmt
, args
);
130 static void do_error(struct position pos
, const char * fmt
, va_list args
)
132 static int errors
= 0;
135 /* Shut up warnings after an error */
142 fmt
= "too many errors";
146 do_warn("error: ", pos
, fmt
, args
);
150 void warning(struct position pos
, const char * fmt
, ...)
156 do_error(pos
, fmt
, args
);
166 if (!--max_warnings
) {
168 fmt
= "too many warnings";
172 do_warn("warning: ", pos
, fmt
, args
);
176 void sparse_error(struct position pos
, const char * fmt
, ...)
180 do_error(pos
, fmt
, args
);
184 void expression_error(struct expression
*expr
, const char *fmt
, ...)
188 do_error(expr
->pos
, fmt
, args
);
190 expr
->ctype
= &bad_ctype
;
193 void error_die(struct position pos
, const char * fmt
, ...)
197 do_warn("error: ", pos
, fmt
, args
);
202 void die(const char *fmt
, ...)
205 static char buffer
[512];
208 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
211 fprintf(stderr
, "%s\n", buffer
);
215 static struct token
*pre_buffer_begin
= NULL
;
216 static struct token
*pre_buffer_end
= NULL
;
218 int Waddress_space
= 1;
221 int Wcast_truncate
= 1;
224 int Wdeclarationafterstatement
= -1;
225 int Wdefault_bitfield_sign
= 0;
226 int Wdesignated_init
= 1;
228 int Winit_cstring
= 0;
229 int Wenum_mismatch
= 1;
230 int Wsparse_error
= 0;
231 int Wnon_pointer_null
= 1;
232 int Wold_initializer
= 1;
233 int Wone_bit_signed_bitfield
= 1;
234 int Woverride_init
= 1;
235 int Woverride_init_all
= 0;
236 int Woverride_init_whole_range
= 0;
237 int Wparen_string
= 0;
238 int Wptr_subtraction_blows
= 0;
239 int Wreturn_void
= 0;
241 int Wsizeof_bool
= 0;
242 int Wtautological_compare
= 0;
243 int Wtransparent_union
= 0;
246 int Wuninitialized
= 1;
247 int Wunknown_attribute
= 1;
255 static enum { STANDARD_C89
,
261 STANDARD_GNU99
, } standard
= STANDARD_GNU89
;
268 #define ARCH_M64_DEFAULT ARCH_LP64
270 #define ARCH_M64_DEFAULT ARCH_LP32
273 int arch_m64
= ARCH_M64_DEFAULT
;
274 int arch_msize_long
= 0;
276 #define CMDLINE_INCLUDE 20
277 static int cmdline_include_nr
= 0;
278 static char *cmdline_include
[CMDLINE_INCLUDE
];
281 void add_pre_buffer(const char *fmt
, ...)
285 struct token
*begin
, *end
;
289 size
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
291 begin
= tokenize_buffer(buffer
, size
, &end
);
292 if (!pre_buffer_begin
)
293 pre_buffer_begin
= begin
;
295 pre_buffer_end
->next
= begin
;
296 pre_buffer_end
= end
;
299 static char **handle_switch_D(char *arg
, char **next
)
301 const char *name
= arg
+ 1;
302 const char *value
= "1";
304 if (!*name
|| isspace((unsigned char)*name
))
305 die("argument to `-D' is missing");
312 if (isspace((unsigned char)c
) || c
== '=') {
318 add_pre_buffer("#define %s %s\n", name
, value
);
322 static char **handle_switch_E(char *arg
, char **next
)
329 static char **handle_switch_I(char *arg
, char **next
)
335 add_pre_buffer("#split_include\n");
338 case '\0': /* Plain "-I" */
341 die("missing argument for -I option");
344 add_pre_buffer("#add_include \"%s/\"\n", path
);
349 static void add_cmdline_include(char *filename
)
351 if (cmdline_include_nr
>= CMDLINE_INCLUDE
)
352 die("too many include files for %s\n", filename
);
353 cmdline_include
[cmdline_include_nr
++] = filename
;
356 static char **handle_switch_i(char *arg
, char **next
)
358 if (*next
&& !strcmp(arg
, "include"))
359 add_cmdline_include(*++next
);
360 else if (*next
&& !strcmp(arg
, "imacros"))
361 add_cmdline_include(*++next
);
362 else if (*next
&& !strcmp(arg
, "isystem")) {
363 char *path
= *++next
;
365 die("missing argument for -isystem option");
366 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
367 } else if (*next
&& !strcmp(arg
, "idirafter")) {
368 char *path
= *++next
;
370 die("missing argument for -idirafter option");
371 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
376 static char **handle_switch_M(char *arg
, char **next
)
378 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
380 die("missing argument for -%s option", arg
);
386 static char **handle_multiarch_dir(char *arg
, char **next
)
388 multiarch_dir
= *++next
;
390 die("missing argument for -multiarch-dir option");
394 static char **handle_switch_m(char *arg
, char **next
)
396 if (!strcmp(arg
, "m64")) {
397 arch_m64
= ARCH_LP64
;
398 } else if (!strcmp(arg
, "m32")) {
399 arch_m64
= ARCH_LP32
;
400 } else if (!strcmp(arg
, "msize-llp64")) {
401 arch_m64
= ARCH_LLP64
;
402 } else if (!strcmp(arg
, "msize-long")) {
404 } else if (!strcmp(arg
, "multiarch-dir"))
405 return handle_multiarch_dir(arg
, next
);
409 static void handle_arch_m64_finalize(void)
417 max_int_alignment
= 8;
418 size_t_ctype
= &ulong_ctype
;
419 ssize_t_ctype
= &long_ctype
;
420 add_pre_buffer("#weak_define __LP64__ 1\n");
421 add_pre_buffer("#weak_define _LP64 1\n");
422 goto case_64bit_common
;
425 max_int_alignment
= 4;
426 size_t_ctype
= &ullong_ctype
;
427 ssize_t_ctype
= &llong_ctype
;
428 add_pre_buffer("#weak_define __LLP64__ 1\n");
429 goto case_64bit_common
;
431 bits_in_pointer
= 64;
432 pointer_alignment
= 8;
434 add_pre_buffer("#weak_define __x86_64__ 1\n");
440 static void handle_arch_msize_long_finalize(void)
442 if (arch_msize_long
) {
443 size_t_ctype
= &ulong_ctype
;
444 ssize_t_ctype
= &long_ctype
;
448 static void handle_arch_finalize(void)
450 handle_arch_m64_finalize();
451 handle_arch_msize_long_finalize();
455 static char **handle_switch_o(char *arg
, char **next
)
457 if (!strcmp (arg
, "o")) { // "-o foo"
459 die("argument to '-o' is missing");
466 static const struct warning
{
470 { "address-space", &Waddress_space
},
471 { "bitwise", &Wbitwise
},
472 { "cast-to-as", &Wcast_to_as
},
473 { "cast-truncate", &Wcast_truncate
},
474 { "context", &Wcontext
},
476 { "declaration-after-statement", &Wdeclarationafterstatement
},
477 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
478 { "designated-init", &Wdesignated_init
},
479 { "do-while", &Wdo_while
},
480 { "enum-mismatch", &Wenum_mismatch
},
481 { "sparse-error", &Wsparse_error
},
482 { "init-cstring", &Winit_cstring
},
483 { "non-pointer-null", &Wnon_pointer_null
},
484 { "old-initializer", &Wold_initializer
},
485 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield
},
486 { "override-init", &Woverride_init
},
487 { "override-init-all", &Woverride_init_all
},
488 { "paren-string", &Wparen_string
},
489 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
490 { "return-void", &Wreturn_void
},
491 { "shadow", &Wshadow
},
492 { "sizeof-bool", &Wsizeof_bool
},
493 { "tautological-compare", &Wtautological_compare
},
494 { "transparent-union", &Wtransparent_union
},
495 { "typesign", &Wtypesign
},
496 { "undef", &Wundef
},
497 { "uninitialized", &Wuninitialized
},
498 { "unknown-attribute", &Wunknown_attribute
},
509 static char **handle_onoff_switch(char *arg
, char **next
, const struct warning warnings
[], int n
)
511 int flag
= WARNING_ON
;
515 if (!strcmp(p
, "sparse-all")) {
516 for (i
= 0; i
< n
; i
++) {
517 if (*warnings
[i
].flag
!= WARNING_FORCE_OFF
&& warnings
[i
].flag
!= &Wsparse_error
)
518 *warnings
[i
].flag
= WARNING_ON
;
522 // Prefixes "no" and "no-" mean to turn warning off.
523 if (p
[0] == 'n' && p
[1] == 'o') {
527 flag
= WARNING_FORCE_OFF
;
530 for (i
= 0; i
< n
; i
++) {
531 if (!strcmp(p
,warnings
[i
].name
)) {
532 *warnings
[i
].flag
= flag
;
541 static char **handle_switch_W(char *arg
, char **next
)
543 char ** ret
= handle_onoff_switch(arg
, next
, warnings
, ARRAY_SIZE(warnings
));
551 static struct warning debugs
[] = {
552 { "entry", &dbg_entry
},
553 { "dead", &dbg_dead
},
557 static char **handle_switch_v(char *arg
, char **next
)
559 char ** ret
= handle_onoff_switch(arg
, next
, debugs
, ARRAY_SIZE(debugs
));
566 } while (*++arg
== 'v');
571 static void handle_onoff_switch_finalize(const struct warning warnings
[], int n
)
575 for (i
= 0; i
< n
; i
++) {
576 if (*warnings
[i
].flag
== WARNING_FORCE_OFF
)
577 *warnings
[i
].flag
= WARNING_OFF
;
581 static void handle_switch_W_finalize(void)
583 handle_onoff_switch_finalize(warnings
, ARRAY_SIZE(warnings
));
585 /* default Wdeclarationafterstatement based on the C dialect */
586 if (-1 == Wdeclarationafterstatement
)
592 Wdeclarationafterstatement
= 1;
600 Wdeclarationafterstatement
= 0;
610 static void handle_switch_v_finalize(void)
612 handle_onoff_switch_finalize(debugs
, ARRAY_SIZE(debugs
));
615 static char **handle_switch_U(char *arg
, char **next
)
617 const char *name
= arg
+ 1;
618 add_pre_buffer ("#undef %s\n", name
);
622 static char **handle_switch_O(char *arg
, char **next
)
625 if (arg
[1] >= '0' && arg
[1] <= '9')
626 level
= arg
[1] - '0';
628 optimize_size
= arg
[1] == 's';
632 static char **handle_switch_ftabstop(char *arg
, char **next
)
638 die("error: missing argument to \"-ftabstop=\"");
640 /* we silently ignore silly values */
641 val
= strtoul(arg
, &end
, 10);
642 if (*end
== '\0' && 1 <= val
&& val
<= 100)
648 static char **handle_switch_f(char *arg
, char **next
)
652 if (!strncmp(arg
, "tabstop=", 8))
653 return handle_switch_ftabstop(arg
+8, next
);
655 /* handle switches w/ arguments above, boolean and only boolean below */
657 if (!strncmp(arg
, "no-", 3)) {
660 /* handle switch here.. */
664 static char **handle_switch_G(char *arg
, char **next
)
666 if (!strcmp (arg
, "G") && *next
)
667 return next
+ 1; // "-G 0"
669 return next
; // "-G0" or (bogus) terminal "-G"
672 static char **handle_switch_a(char *arg
, char **next
)
674 if (!strcmp (arg
, "ansi"))
675 standard
= STANDARD_C89
;
680 static char **handle_switch_s(char *arg
, char **next
)
682 if (!strncmp (arg
, "std=", 4))
686 if (!strcmp (arg
, "c89") ||
687 !strcmp (arg
, "iso9899:1990"))
688 standard
= STANDARD_C89
;
690 else if (!strcmp (arg
, "iso9899:199409"))
691 standard
= STANDARD_C94
;
693 else if (!strcmp (arg
, "c99") ||
694 !strcmp (arg
, "c9x") ||
695 !strcmp (arg
, "iso9899:1999") ||
696 !strcmp (arg
, "iso9899:199x"))
697 standard
= STANDARD_C99
;
699 else if (!strcmp (arg
, "gnu89"))
700 standard
= STANDARD_GNU89
;
702 else if (!strcmp (arg
, "gnu99") || !strcmp (arg
, "gnu9x"))
703 standard
= STANDARD_GNU99
;
705 else if (!strcmp(arg
, "c11") ||
706 !strcmp(arg
, "c1x") ||
707 !strcmp(arg
, "iso9899:2011"))
708 standard
= STANDARD_C11
;
710 else if (!strcmp(arg
, "gnu11"))
711 standard
= STANDARD_GNU11
;
714 die ("Unsupported C dialect");
720 static char **handle_nostdinc(char *arg
, char **next
)
722 add_pre_buffer("#nostdinc\n");
726 static char **handle_switch_n(char *arg
, char **next
)
728 if (!strcmp (arg
, "nostdinc"))
729 return handle_nostdinc(arg
, next
);
734 static char **handle_base_dir(char *arg
, char **next
)
736 gcc_base_dir
= *++next
;
738 die("missing argument for -gcc-base-dir option");
742 static char **handle_switch_g(char *arg
, char **next
)
744 if (!strcmp (arg
, "gcc-base-dir"))
745 return handle_base_dir(arg
, next
);
751 static char **handle_version(char *arg
, char **next
)
753 printf("%s\n", SPARSE_VERSION
);
757 static char **handle_param(char *arg
, char **next
)
761 /* For now just skip any '--param=*' or '--param *' */
764 } else if (isspace((unsigned char)*arg
) || *arg
== '=') {
769 die("missing argument for --param option");
776 char **(*fn
)(char *, char **);
777 unsigned int prefix
:1;
780 static char **handle_long_options(char *arg
, char **next
)
782 static struct switches cmd
[] = {
783 { "param", handle_param
, 1 },
784 { "version", handle_version
},
787 struct switches
*s
= cmd
;
790 int optlen
= strlen(s
->name
);
791 if (!strncmp(s
->name
, arg
, optlen
+ !s
->prefix
))
792 return s
->fn(arg
+ optlen
, next
);
798 static char **handle_switch(char *arg
, char **next
)
801 case 'a': return handle_switch_a(arg
, next
);
802 case 'D': return handle_switch_D(arg
, next
);
803 case 'E': return handle_switch_E(arg
, next
);
804 case 'f': return handle_switch_f(arg
, next
);
805 case 'g': return handle_switch_g(arg
, next
);
806 case 'G': return handle_switch_G(arg
, next
);
807 case 'I': return handle_switch_I(arg
, next
);
808 case 'i': return handle_switch_i(arg
, next
);
809 case 'M': return handle_switch_M(arg
, next
);
810 case 'm': return handle_switch_m(arg
, next
);
811 case 'n': return handle_switch_n(arg
, next
);
812 case 'o': return handle_switch_o(arg
, next
);
813 case 'O': return handle_switch_O(arg
, next
);
814 case 's': return handle_switch_s(arg
, next
);
815 case 'U': return handle_switch_U(arg
, next
);
816 case 'v': return handle_switch_v(arg
, next
);
817 case 'W': return handle_switch_W(arg
, next
);
818 case '-': return handle_long_options(arg
+ 1, next
);
824 * Ignore unknown command line options:
825 * they're probably gcc switches
830 static void predefined_macros(void)
832 unsigned long long val
;
834 add_pre_buffer("#define __CHECKER__ 1\n");
836 val
= (1ULL << (bits_in_long
-1)) - 1;
837 add_pre_buffer("#weak_define __LONG_MAX__ %#llxLL\n", val
);
838 add_pre_buffer("#weak_define __SIZEOF_POINTER__ %d\n", bits_in_pointer
/8);
841 void declare_builtin_functions(void)
843 /* Gaah. gcc knows tons of builtin <string.h> functions */
844 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
845 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
846 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
847 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
848 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
849 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
850 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
851 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
852 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
853 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
854 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
855 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
856 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
857 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
858 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
860 /* And bitwise operations.. */
861 add_pre_buffer("extern int __builtin_clz(int);\n");
862 add_pre_buffer("extern int __builtin_clzl(long);\n");
863 add_pre_buffer("extern int __builtin_clzll(long long);\n");
864 add_pre_buffer("extern int __builtin_ctz(int);\n");
865 add_pre_buffer("extern int __builtin_ctzl(long);\n");
866 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
867 add_pre_buffer("extern int __builtin_ffs(int);\n");
868 add_pre_buffer("extern int __builtin_ffsl(long);\n");
869 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
870 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
871 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
872 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
874 /* And byte swaps.. */
875 add_pre_buffer("extern unsigned short __builtin_bswap16(unsigned short);\n");
876 add_pre_buffer("extern unsigned int __builtin_bswap32(unsigned int);\n");
877 add_pre_buffer("extern unsigned long long __builtin_bswap64(unsigned long long);\n");
879 /* And atomic memory access functions.. */
880 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
881 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
882 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
883 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
884 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
885 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
886 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
887 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
888 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
889 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
890 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
891 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
892 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
893 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
894 add_pre_buffer("extern void __sync_synchronize();\n");
895 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
896 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
898 /* And some random ones.. */
899 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
900 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
901 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
902 add_pre_buffer("extern void __builtin_trap(void);\n");
903 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
904 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
905 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
906 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
907 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
908 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
909 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
910 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
911 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
912 add_pre_buffer("extern long __builtin_labs(long);\n");
913 add_pre_buffer("extern double __builtin_fabs(double);\n");
914 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
916 /* Add Blackfin-specific stuff */
919 "extern void __builtin_bfin_csync(void);\n"
920 "extern void __builtin_bfin_ssync(void);\n"
921 "extern int __builtin_bfin_norm_fr1x32(int);\n"
925 /* And some floating point stuff.. */
926 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
927 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
928 add_pre_buffer("extern int __builtin_isless(float, float);\n");
929 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
930 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
931 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
933 /* And some INFINITY / NAN stuff.. */
934 add_pre_buffer("extern double __builtin_huge_val(void);\n");
935 add_pre_buffer("extern float __builtin_huge_valf(void);\n");
936 add_pre_buffer("extern long double __builtin_huge_vall(void);\n");
937 add_pre_buffer("extern double __builtin_inf(void);\n");
938 add_pre_buffer("extern float __builtin_inff(void);\n");
939 add_pre_buffer("extern long double __builtin_infl(void);\n");
940 add_pre_buffer("extern double __builtin_nan(const char *);\n");
941 add_pre_buffer("extern float __builtin_nanf(const char *);\n");
942 add_pre_buffer("extern long double __builtin_nanl(const char *);\n");
944 /* And some __FORTIFY_SOURCE ones.. */
945 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(const void *, int);\n");
946 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
947 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
948 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
949 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
950 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
951 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
952 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
953 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
954 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
955 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
956 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
957 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
958 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
959 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
962 void create_builtin_stream(void)
964 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major
);
965 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor
);
966 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
968 /* add the multiarch include directories, if any */
969 if (multiarch_dir
&& *multiarch_dir
) {
970 add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir
);
971 add_pre_buffer("#add_system \"/usr/local/include/%s\"\n", multiarch_dir
);
974 /* We add compiler headers path here because we have to parse
975 * the arguments to get it, falling back to default. */
976 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir
);
977 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir
);
979 add_pre_buffer("#define __extension__\n");
980 add_pre_buffer("#define __pragma__\n");
982 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
983 // solaris/sparc that is really "unsigned int" and for linux/x86_64
984 // it is "long unsigned int". In either case we can probably
985 // get away with this. We need the #weak_define as cgcc will define
986 // the right __SIZE_TYPE__.
987 if (size_t_ctype
== &ulong_ctype
)
988 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
990 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
991 add_pre_buffer("#weak_define __STDC__ 1\n");
996 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1000 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
1001 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1005 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1006 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1009 case STANDARD_GNU89
:
1012 case STANDARD_GNU99
:
1013 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1017 add_pre_buffer("#weak_define __STRICT_ANSI__ 1\n");
1018 case STANDARD_GNU11
:
1019 add_pre_buffer("#weak_define __STDC_NO_ATOMICS__ 1\n");
1020 add_pre_buffer("#weak_define __STDC_NO_COMPLEX__ 1\n");
1021 add_pre_buffer("#weak_define __STDC_NO_THREADS__ 1\n");
1022 add_pre_buffer("#weak_define __STDC_VERSION__ 201112L\n");
1029 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1030 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1031 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
1032 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
1033 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
1034 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
1035 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1036 add_pre_buffer("#define __builtin_ms_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1037 add_pre_buffer("#define __builtin_va_end(arg)\n");
1038 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
1039 add_pre_buffer("#define __builtin_va_arg_pack()\n");
1041 /* FIXME! We need to do these as special magic macros at expansion time! */
1042 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
1045 add_pre_buffer("#define __OPTIMIZE__ 1\n");
1047 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
1049 /* GCC defines these for limits.h */
1050 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__
) "\n");
1051 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__
) "\n");
1052 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__
) "\n");
1053 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__
) "\n");
1054 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__
) "\n");
1055 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__
) "\n");
1056 add_pre_buffer("#weak_define __SIZEOF_POINTER__ " STRINGIFY(__SIZEOF_POINTER__
) "\n");
1057 add_pre_buffer("#weak_define __CHAR_BIT__ " STRINGIFY(__CHAR_BIT__
) "\n");
1060 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
1062 // Preprocess the stream
1063 token
= preprocess(token
);
1065 if (preprocess_only
) {
1066 while (!eof_token(token
)) {
1068 struct token
*next
= token
->next
;
1069 const char *separator
= "";
1070 if (next
->pos
.whitespace
)
1072 if (next
->pos
.newline
) {
1073 separator
= "\n\t\t\t\t\t";
1074 prec
= next
->pos
.pos
;
1078 printf("%s%.*s", show_token(token
), prec
, separator
);
1086 // Parse the resulting C code
1087 while (!eof_token(token
))
1088 token
= external_declaration(token
, &translation_unit_used_list
, NULL
);
1089 return translation_unit_used_list
;
1092 static struct symbol_list
*sparse_file(const char *filename
)
1095 struct token
*token
;
1097 if (strcmp (filename
, "-") == 0) {
1100 fd
= open(filename
, O_RDONLY
);
1102 die("No such file: %s", filename
);
1105 // Tokenize the input stream
1106 token
= tokenize(filename
, fd
, NULL
, includepath
);
1109 return sparse_tokenstream(token
);
1113 * This handles the "-include" directive etc: we're in global
1114 * scope, and all types/macros etc will affect all the following
1117 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1118 * affect all subsequent files too, i.e. we can have non-local
1119 * behaviour between files!
1121 static struct symbol_list
*sparse_initial(void)
1125 // Prepend any "include" file to the stream.
1126 // We're in global scope, it will affect all files!
1127 for (i
= 0; i
< cmdline_include_nr
; i
++)
1128 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include
[i
]);
1130 return sparse_tokenstream(pre_buffer_begin
);
1133 struct symbol_list
*sparse_initialize(int argc
, char **argv
, struct string_list
**filelist
)
1136 struct symbol_list
*list
;
1138 // Initialize symbol stream first, so that we can add defines etc
1143 char *arg
= *++args
;
1147 if (arg
[0] == '-' && arg
[1]) {
1148 args
= handle_switch(arg
+1, args
);
1151 add_ptr_list_notag(filelist
, arg
);
1153 handle_switch_W_finalize();
1154 handle_switch_v_finalize();
1156 handle_arch_finalize();
1159 if (!ptr_list_empty(filelist
)) {
1160 // Initialize type system
1163 create_builtin_stream();
1164 predefined_macros();
1165 if (!preprocess_only
)
1166 declare_builtin_functions();
1168 list
= sparse_initial();
1171 * Protect the initial token allocations, since
1172 * they need to survive all the others
1174 protect_token_alloc();
1179 struct symbol_list
* sparse_keep_tokens(char *filename
)
1181 struct symbol_list
*res
;
1183 /* Clear previous symbol list */
1184 translation_unit_used_list
= NULL
;
1187 res
= sparse_file(filename
);
1194 struct symbol_list
* __sparse(char *filename
)
1196 struct symbol_list
*res
;
1198 res
= sparse_keep_tokens(filename
);
1200 /* Drop the tokens for this file after parsing */
1201 clear_token_alloc();
1207 struct symbol_list
* sparse(char *filename
)
1209 struct symbol_list
*res
= __sparse(filename
);
1211 /* Evaluate the complete symbol list */
1212 evaluate_symbol_list(res
);