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
;
219 int Waddress_space
= 1;
222 int Wcast_truncate
= 1;
225 int Wdeclarationafterstatement
= -1;
226 int Wdefault_bitfield_sign
= 0;
227 int Wdesignated_init
= 1;
229 int Winit_cstring
= 0;
230 int Wenum_mismatch
= 1;
231 int Wsparse_error
= 0;
232 int Wnon_pointer_null
= 1;
233 int Wold_initializer
= 1;
234 int Wone_bit_signed_bitfield
= 1;
235 int Woverride_init
= 1;
236 int Woverride_init_all
= 0;
237 int Woverride_init_whole_range
= 0;
238 int Wparen_string
= 0;
239 int Wptr_subtraction_blows
= 0;
240 int Wreturn_void
= 0;
242 int Wsizeof_bool
= 0;
243 int Wtautological_compare
= 0;
244 int Wtransparent_union
= 0;
247 int Wuninitialized
= 1;
248 int Wunknown_attribute
= 1;
251 int dump_macro_defs
= 0;
261 static enum { STANDARD_C89
,
267 STANDARD_GNU99
, } standard
= STANDARD_GNU89
;
274 #define ARCH_M64_DEFAULT ARCH_LP64
276 #define ARCH_M64_DEFAULT ARCH_LP32
279 int arch_m64
= ARCH_M64_DEFAULT
;
280 int arch_msize_long
= 0;
282 #define CMDLINE_INCLUDE 20
283 static int cmdline_include_nr
= 0;
284 static char *cmdline_include
[CMDLINE_INCLUDE
];
287 void add_pre_buffer(const char *fmt
, ...)
291 struct token
*begin
, *end
;
295 size
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
297 begin
= tokenize_buffer(buffer
, size
, &end
);
298 if (!pre_buffer_begin
)
299 pre_buffer_begin
= begin
;
301 pre_buffer_end
->next
= begin
;
302 pre_buffer_end
= end
;
305 static char **handle_switch_D(char *arg
, char **next
)
307 const char *name
= arg
+ 1;
308 const char *value
= "1";
310 if (!*name
|| isspace((unsigned char)*name
))
311 die("argument to `-D' is missing");
318 if (isspace((unsigned char)c
) || c
== '=') {
324 add_pre_buffer("#define %s %s\n", name
, value
);
328 static char **handle_switch_E(char *arg
, char **next
)
335 static char **handle_switch_I(char *arg
, char **next
)
341 add_pre_buffer("#split_include\n");
344 case '\0': /* Plain "-I" */
347 die("missing argument for -I option");
350 add_pre_buffer("#add_include \"%s/\"\n", path
);
355 static void add_cmdline_include(char *filename
)
357 if (cmdline_include_nr
>= CMDLINE_INCLUDE
)
358 die("too many include files for %s\n", filename
);
359 cmdline_include
[cmdline_include_nr
++] = filename
;
362 static char **handle_switch_i(char *arg
, char **next
)
364 if (*next
&& !strcmp(arg
, "include"))
365 add_cmdline_include(*++next
);
366 else if (*next
&& !strcmp(arg
, "imacros"))
367 add_cmdline_include(*++next
);
368 else if (*next
&& !strcmp(arg
, "isystem")) {
369 char *path
= *++next
;
371 die("missing argument for -isystem option");
372 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
373 } else if (*next
&& !strcmp(arg
, "idirafter")) {
374 char *path
= *++next
;
376 die("missing argument for -idirafter option");
377 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
382 static char **handle_switch_M(char *arg
, char **next
)
384 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
386 die("missing argument for -%s option", arg
);
392 static char **handle_multiarch_dir(char *arg
, char **next
)
394 multiarch_dir
= *++next
;
396 die("missing argument for -multiarch-dir option");
400 static char **handle_switch_m(char *arg
, char **next
)
402 if (!strcmp(arg
, "m64")) {
403 arch_m64
= ARCH_LP64
;
404 } else if (!strcmp(arg
, "m32")) {
405 arch_m64
= ARCH_LP32
;
406 } else if (!strcmp(arg
, "msize-llp64")) {
407 arch_m64
= ARCH_LLP64
;
408 } else if (!strcmp(arg
, "msize-long")) {
410 } else if (!strcmp(arg
, "multiarch-dir"))
411 return handle_multiarch_dir(arg
, next
);
415 static void handle_arch_m64_finalize(void)
423 max_int_alignment
= 8;
424 size_t_ctype
= &ulong_ctype
;
425 ssize_t_ctype
= &long_ctype
;
426 add_pre_buffer("#weak_define __LP64__ 1\n");
427 add_pre_buffer("#weak_define _LP64 1\n");
428 goto case_64bit_common
;
431 max_int_alignment
= 4;
432 size_t_ctype
= &ullong_ctype
;
433 ssize_t_ctype
= &llong_ctype
;
434 add_pre_buffer("#weak_define __LLP64__ 1\n");
435 goto case_64bit_common
;
437 bits_in_pointer
= 64;
438 pointer_alignment
= 8;
440 add_pre_buffer("#weak_define __x86_64__ 1\n");
446 static void handle_arch_msize_long_finalize(void)
448 if (arch_msize_long
) {
449 size_t_ctype
= &ulong_ctype
;
450 ssize_t_ctype
= &long_ctype
;
454 static void handle_arch_finalize(void)
456 handle_arch_m64_finalize();
457 handle_arch_msize_long_finalize();
461 static int handle_simple_switch(const char *arg
, const char *name
, int *flag
)
465 // Prefixe "no-" mean to turn flag off.
466 if (strncmp(arg
, "no-", 3) == 0) {
471 if (strcmp(arg
, name
) == 0) {
480 static char **handle_switch_o(char *arg
, char **next
)
482 if (!strcmp (arg
, "o")) { // "-o foo"
484 die("argument to '-o' is missing");
491 static const struct warning
{
495 { "address", &Waddress
},
496 { "address-space", &Waddress_space
},
497 { "bitwise", &Wbitwise
},
498 { "cast-to-as", &Wcast_to_as
},
499 { "cast-truncate", &Wcast_truncate
},
500 { "context", &Wcontext
},
502 { "declaration-after-statement", &Wdeclarationafterstatement
},
503 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
504 { "designated-init", &Wdesignated_init
},
505 { "do-while", &Wdo_while
},
506 { "enum-mismatch", &Wenum_mismatch
},
507 { "sparse-error", &Wsparse_error
},
508 { "init-cstring", &Winit_cstring
},
509 { "non-pointer-null", &Wnon_pointer_null
},
510 { "old-initializer", &Wold_initializer
},
511 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield
},
512 { "override-init", &Woverride_init
},
513 { "override-init-all", &Woverride_init_all
},
514 { "paren-string", &Wparen_string
},
515 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
516 { "return-void", &Wreturn_void
},
517 { "shadow", &Wshadow
},
518 { "sizeof-bool", &Wsizeof_bool
},
519 { "tautological-compare", &Wtautological_compare
},
520 { "transparent-union", &Wtransparent_union
},
521 { "typesign", &Wtypesign
},
522 { "undef", &Wundef
},
523 { "uninitialized", &Wuninitialized
},
524 { "unknown-attribute", &Wunknown_attribute
},
535 static char **handle_onoff_switch(char *arg
, char **next
, const struct warning warnings
[], int n
)
537 int flag
= WARNING_ON
;
541 if (!strcmp(p
, "sparse-all")) {
542 for (i
= 0; i
< n
; i
++) {
543 if (*warnings
[i
].flag
!= WARNING_FORCE_OFF
&& warnings
[i
].flag
!= &Wsparse_error
)
544 *warnings
[i
].flag
= WARNING_ON
;
548 // Prefixes "no" and "no-" mean to turn warning off.
549 if (p
[0] == 'n' && p
[1] == 'o') {
553 flag
= WARNING_FORCE_OFF
;
556 for (i
= 0; i
< n
; i
++) {
557 if (!strcmp(p
,warnings
[i
].name
)) {
558 *warnings
[i
].flag
= flag
;
567 static char **handle_switch_W(char *arg
, char **next
)
569 char ** ret
= handle_onoff_switch(arg
, next
, warnings
, ARRAY_SIZE(warnings
));
577 static struct warning debugs
[] = {
578 { "entry", &dbg_entry
},
579 { "dead", &dbg_dead
},
583 static char **handle_switch_v(char *arg
, char **next
)
585 char ** ret
= handle_onoff_switch(arg
, next
, debugs
, ARRAY_SIZE(debugs
));
592 } while (*++arg
== 'v');
596 static struct warning dumps
[] = {
597 { "D", &dump_macro_defs
},
600 static char **handle_switch_d(char *arg
, char **next
)
602 char ** ret
= handle_onoff_switch(arg
, next
, dumps
, ARRAY_SIZE(dumps
));
610 static void handle_onoff_switch_finalize(const struct warning warnings
[], int n
)
614 for (i
= 0; i
< n
; i
++) {
615 if (*warnings
[i
].flag
== WARNING_FORCE_OFF
)
616 *warnings
[i
].flag
= WARNING_OFF
;
620 static void handle_switch_W_finalize(void)
622 handle_onoff_switch_finalize(warnings
, ARRAY_SIZE(warnings
));
624 /* default Wdeclarationafterstatement based on the C dialect */
625 if (-1 == Wdeclarationafterstatement
)
631 Wdeclarationafterstatement
= 1;
639 Wdeclarationafterstatement
= 0;
649 static void handle_switch_v_finalize(void)
651 handle_onoff_switch_finalize(debugs
, ARRAY_SIZE(debugs
));
654 static char **handle_switch_U(char *arg
, char **next
)
656 const char *name
= arg
+ 1;
657 add_pre_buffer ("#undef %s\n", name
);
661 static char **handle_switch_O(char *arg
, char **next
)
664 if (arg
[1] >= '0' && arg
[1] <= '9')
665 level
= arg
[1] - '0';
667 optimize_size
= arg
[1] == 's';
671 static char **handle_switch_ftabstop(char *arg
, char **next
)
677 die("error: missing argument to \"-ftabstop=\"");
679 /* we silently ignore silly values */
680 val
= strtoul(arg
, &end
, 10);
681 if (*end
== '\0' && 1 <= val
&& val
<= 100)
687 static char **handle_switch_fdump(char *arg
, char **next
)
689 if (!strncmp(arg
, "linearize", 9)) {
693 else if (!strcmp(arg
, "=only"))
699 /* ignore others flags */
703 die("error: unknown flag \"-fdump-%s\"", arg
);
706 static char **handle_switch_f(char *arg
, char **next
)
710 if (!strncmp(arg
, "tabstop=", 8))
711 return handle_switch_ftabstop(arg
+8, next
);
712 if (!strncmp(arg
, "dump-", 5))
713 return handle_switch_fdump(arg
+5, next
);
715 /* handle switches w/ arguments above, boolean and only boolean below */
716 if (handle_simple_switch(arg
, "mem-report", &fmem_report
))
722 static char **handle_switch_G(char *arg
, char **next
)
724 if (!strcmp (arg
, "G") && *next
)
725 return next
+ 1; // "-G 0"
727 return next
; // "-G0" or (bogus) terminal "-G"
730 static char **handle_switch_a(char *arg
, char **next
)
732 if (!strcmp (arg
, "ansi"))
733 standard
= STANDARD_C89
;
738 static char **handle_switch_s(char *arg
, char **next
)
740 if (!strncmp (arg
, "std=", 4))
744 if (!strcmp (arg
, "c89") ||
745 !strcmp (arg
, "iso9899:1990"))
746 standard
= STANDARD_C89
;
748 else if (!strcmp (arg
, "iso9899:199409"))
749 standard
= STANDARD_C94
;
751 else if (!strcmp (arg
, "c99") ||
752 !strcmp (arg
, "c9x") ||
753 !strcmp (arg
, "iso9899:1999") ||
754 !strcmp (arg
, "iso9899:199x"))
755 standard
= STANDARD_C99
;
757 else if (!strcmp (arg
, "gnu89"))
758 standard
= STANDARD_GNU89
;
760 else if (!strcmp (arg
, "gnu99") || !strcmp (arg
, "gnu9x"))
761 standard
= STANDARD_GNU99
;
763 else if (!strcmp(arg
, "c11") ||
764 !strcmp(arg
, "c1x") ||
765 !strcmp(arg
, "iso9899:2011"))
766 standard
= STANDARD_C11
;
768 else if (!strcmp(arg
, "gnu11"))
769 standard
= STANDARD_GNU11
;
772 die ("Unsupported C dialect");
778 static char **handle_nostdinc(char *arg
, char **next
)
780 add_pre_buffer("#nostdinc\n");
784 static char **handle_switch_n(char *arg
, char **next
)
786 if (!strcmp (arg
, "nostdinc"))
787 return handle_nostdinc(arg
, next
);
792 static char **handle_base_dir(char *arg
, char **next
)
794 gcc_base_dir
= *++next
;
796 die("missing argument for -gcc-base-dir option");
800 static char **handle_switch_g(char *arg
, char **next
)
802 if (!strcmp (arg
, "gcc-base-dir"))
803 return handle_base_dir(arg
, next
);
809 static char **handle_version(char *arg
, char **next
)
811 printf("%s\n", SPARSE_VERSION
);
815 static char **handle_param(char *arg
, char **next
)
819 /* For now just skip any '--param=*' or '--param *' */
822 } else if (isspace((unsigned char)*arg
) || *arg
== '=') {
827 die("missing argument for --param option");
834 char **(*fn
)(char *, char **);
835 unsigned int prefix
:1;
838 static char **handle_long_options(char *arg
, char **next
)
840 static struct switches cmd
[] = {
841 { "param", handle_param
, 1 },
842 { "version", handle_version
},
845 struct switches
*s
= cmd
;
848 int optlen
= strlen(s
->name
);
849 if (!strncmp(s
->name
, arg
, optlen
+ !s
->prefix
))
850 return s
->fn(arg
+ optlen
, next
);
856 static char **handle_switch(char *arg
, char **next
)
859 case 'a': return handle_switch_a(arg
, next
);
860 case 'D': return handle_switch_D(arg
, next
);
861 case 'd': return handle_switch_d(arg
, next
);
862 case 'E': return handle_switch_E(arg
, next
);
863 case 'f': return handle_switch_f(arg
, next
);
864 case 'g': return handle_switch_g(arg
, next
);
865 case 'G': return handle_switch_G(arg
, next
);
866 case 'I': return handle_switch_I(arg
, next
);
867 case 'i': return handle_switch_i(arg
, next
);
868 case 'M': return handle_switch_M(arg
, next
);
869 case 'm': return handle_switch_m(arg
, next
);
870 case 'n': return handle_switch_n(arg
, next
);
871 case 'o': return handle_switch_o(arg
, next
);
872 case 'O': return handle_switch_O(arg
, next
);
873 case 's': return handle_switch_s(arg
, next
);
874 case 'U': return handle_switch_U(arg
, next
);
875 case 'v': return handle_switch_v(arg
, next
);
876 case 'W': return handle_switch_W(arg
, next
);
877 case '-': return handle_long_options(arg
+ 1, next
);
883 * Ignore unknown command line options:
884 * they're probably gcc switches
889 static void predefined_sizeof(const char *name
, unsigned bits
)
891 add_pre_buffer("#weak_define __SIZEOF_%s__ %d\n", name
, bits
/8);
894 static void predefined_max(const char *name
, const char *suffix
, unsigned bits
)
896 unsigned long long max
= (1ULL << (bits
- 1 )) - 1;
898 add_pre_buffer("#weak_define __%s_MAX__ %#llx%s\n", name
, max
, suffix
);
901 static void predefined_type_size(const char *name
, const char *suffix
, unsigned bits
)
903 predefined_max(name
, suffix
, bits
);
904 predefined_sizeof(name
, bits
);
907 static void predefined_macros(void)
909 add_pre_buffer("#define __CHECKER__ 1\n");
911 predefined_sizeof("SHORT", bits_in_short
);
912 predefined_max("SHRT", "", bits_in_short
);
913 predefined_max("SCHAR", "", bits_in_char
);
914 predefined_max("WCHAR", "", bits_in_wchar
);
915 add_pre_buffer("#weak_define __CHAR_BIT__ %d\n", bits_in_char
);
917 predefined_type_size("INT", "", bits_in_int
);
918 predefined_type_size("LONG", "L", bits_in_long
);
919 predefined_type_size("LONG_LONG", "LL", bits_in_longlong
);
921 predefined_sizeof("INT128", 128);
923 predefined_sizeof("SIZE_T", bits_in_pointer
);
924 predefined_sizeof("PTRDIFF_T", bits_in_pointer
);
925 predefined_sizeof("POINTER", bits_in_pointer
);
927 predefined_sizeof("FLOAT", bits_in_float
);
928 predefined_sizeof("DOUBLE", bits_in_double
);
929 predefined_sizeof("LONG_DOUBLE", bits_in_longdouble
);
932 void declare_builtin_functions(void)
934 /* Gaah. gcc knows tons of builtin <string.h> functions */
935 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
936 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
937 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
938 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
939 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
940 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
941 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
942 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
943 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
944 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
945 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
946 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
947 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
948 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
949 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
951 /* And bitwise operations.. */
952 add_pre_buffer("extern int __builtin_clz(int);\n");
953 add_pre_buffer("extern int __builtin_clzl(long);\n");
954 add_pre_buffer("extern int __builtin_clzll(long long);\n");
955 add_pre_buffer("extern int __builtin_ctz(int);\n");
956 add_pre_buffer("extern int __builtin_ctzl(long);\n");
957 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
958 add_pre_buffer("extern int __builtin_ffs(int);\n");
959 add_pre_buffer("extern int __builtin_ffsl(long);\n");
960 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
961 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
962 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
963 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
965 /* And byte swaps.. */
966 add_pre_buffer("extern unsigned short __builtin_bswap16(unsigned short);\n");
967 add_pre_buffer("extern unsigned int __builtin_bswap32(unsigned int);\n");
968 add_pre_buffer("extern unsigned long long __builtin_bswap64(unsigned long long);\n");
970 /* And atomic memory access functions.. */
971 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
972 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
973 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
974 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
975 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
976 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
977 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
978 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
979 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
980 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
981 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
982 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
983 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
984 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
985 add_pre_buffer("extern void __sync_synchronize();\n");
986 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
987 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
989 /* And some random ones.. */
990 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
991 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
992 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
993 add_pre_buffer("extern void __builtin_trap(void);\n");
994 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
995 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
996 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
997 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
998 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
999 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
1000 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
1001 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
1002 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
1003 add_pre_buffer("extern long __builtin_labs(long);\n");
1004 add_pre_buffer("extern double __builtin_fabs(double);\n");
1005 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
1007 /* Add Blackfin-specific stuff */
1010 "extern void __builtin_bfin_csync(void);\n"
1011 "extern void __builtin_bfin_ssync(void);\n"
1012 "extern int __builtin_bfin_norm_fr1x32(int);\n"
1016 /* And some floating point stuff.. */
1017 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
1018 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
1019 add_pre_buffer("extern int __builtin_isless(float, float);\n");
1020 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
1021 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
1022 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
1024 /* And some INFINITY / NAN stuff.. */
1025 add_pre_buffer("extern double __builtin_huge_val(void);\n");
1026 add_pre_buffer("extern float __builtin_huge_valf(void);\n");
1027 add_pre_buffer("extern long double __builtin_huge_vall(void);\n");
1028 add_pre_buffer("extern double __builtin_inf(void);\n");
1029 add_pre_buffer("extern float __builtin_inff(void);\n");
1030 add_pre_buffer("extern long double __builtin_infl(void);\n");
1031 add_pre_buffer("extern double __builtin_nan(const char *);\n");
1032 add_pre_buffer("extern float __builtin_nanf(const char *);\n");
1033 add_pre_buffer("extern long double __builtin_nanl(const char *);\n");
1035 /* And some __FORTIFY_SOURCE ones.. */
1036 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(const void *, int);\n");
1037 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1038 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1039 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1040 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1041 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
1042 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
1043 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
1044 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
1045 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
1046 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1047 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1048 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
1049 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
1050 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
1053 void create_builtin_stream(void)
1055 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major
);
1056 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor
);
1057 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
1059 /* add the multiarch include directories, if any */
1060 if (multiarch_dir
&& *multiarch_dir
) {
1061 add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir
);
1062 add_pre_buffer("#add_system \"/usr/local/include/%s\"\n", multiarch_dir
);
1065 /* We add compiler headers path here because we have to parse
1066 * the arguments to get it, falling back to default. */
1067 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir
);
1068 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir
);
1070 add_pre_buffer("#define __extension__\n");
1071 add_pre_buffer("#define __pragma__\n");
1073 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
1074 // solaris/sparc that is really "unsigned int" and for linux/x86_64
1075 // it is "long unsigned int". In either case we can probably
1076 // get away with this. We need the #weak_define as cgcc will define
1077 // the right __SIZE_TYPE__.
1078 if (size_t_ctype
== &ulong_ctype
)
1079 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
1081 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
1082 add_pre_buffer("#weak_define __STDC__ 1\n");
1087 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1091 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
1092 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1096 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1097 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1100 case STANDARD_GNU89
:
1103 case STANDARD_GNU99
:
1104 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1108 add_pre_buffer("#weak_define __STRICT_ANSI__ 1\n");
1109 case STANDARD_GNU11
:
1110 add_pre_buffer("#weak_define __STDC_NO_ATOMICS__ 1\n");
1111 add_pre_buffer("#weak_define __STDC_NO_COMPLEX__ 1\n");
1112 add_pre_buffer("#weak_define __STDC_NO_THREADS__ 1\n");
1113 add_pre_buffer("#weak_define __STDC_VERSION__ 201112L\n");
1120 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1121 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1122 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
1123 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
1124 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
1125 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
1126 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1127 add_pre_buffer("#define __builtin_ms_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1128 add_pre_buffer("#define __builtin_va_end(arg)\n");
1129 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
1130 add_pre_buffer("#define __builtin_va_arg_pack()\n");
1132 /* FIXME! We need to do these as special magic macros at expansion time! */
1133 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
1136 add_pre_buffer("#define __OPTIMIZE__ 1\n");
1138 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
1141 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
1143 int builtin
= token
&& !token
->pos
.stream
;
1145 // Preprocess the stream
1146 token
= preprocess(token
);
1148 if (dump_macro_defs
&& !builtin
)
1149 dump_macro_definitions();
1151 if (preprocess_only
) {
1152 while (!eof_token(token
)) {
1154 struct token
*next
= token
->next
;
1155 const char *separator
= "";
1156 if (next
->pos
.whitespace
)
1158 if (next
->pos
.newline
) {
1159 separator
= "\n\t\t\t\t\t";
1160 prec
= next
->pos
.pos
;
1164 printf("%s%.*s", show_token(token
), prec
, separator
);
1172 // Parse the resulting C code
1173 while (!eof_token(token
))
1174 token
= external_declaration(token
, &translation_unit_used_list
, NULL
);
1175 return translation_unit_used_list
;
1178 static struct symbol_list
*sparse_file(const char *filename
)
1181 struct token
*token
;
1183 if (strcmp (filename
, "-") == 0) {
1186 fd
= open(filename
, O_RDONLY
);
1188 die("No such file: %s", filename
);
1191 // Tokenize the input stream
1192 token
= tokenize(filename
, fd
, NULL
, includepath
);
1195 return sparse_tokenstream(token
);
1199 * This handles the "-include" directive etc: we're in global
1200 * scope, and all types/macros etc will affect all the following
1203 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1204 * affect all subsequent files too, i.e. we can have non-local
1205 * behaviour between files!
1207 static struct symbol_list
*sparse_initial(void)
1211 // Prepend any "include" file to the stream.
1212 // We're in global scope, it will affect all files!
1213 for (i
= 0; i
< cmdline_include_nr
; i
++)
1214 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include
[i
]);
1216 return sparse_tokenstream(pre_buffer_begin
);
1219 struct symbol_list
*sparse_initialize(int argc
, char **argv
, struct string_list
**filelist
)
1222 struct symbol_list
*list
;
1224 // Initialize symbol stream first, so that we can add defines etc
1229 char *arg
= *++args
;
1233 if (arg
[0] == '-' && arg
[1]) {
1234 args
= handle_switch(arg
+1, args
);
1237 add_ptr_list_notag(filelist
, arg
);
1239 handle_switch_W_finalize();
1240 handle_switch_v_finalize();
1242 handle_arch_finalize();
1245 if (!ptr_list_empty(filelist
)) {
1246 // Initialize type system
1249 create_builtin_stream();
1250 predefined_macros();
1251 if (!preprocess_only
)
1252 declare_builtin_functions();
1254 list
= sparse_initial();
1257 * Protect the initial token allocations, since
1258 * they need to survive all the others
1260 protect_token_alloc();
1265 struct symbol_list
* sparse_keep_tokens(char *filename
)
1267 struct symbol_list
*res
;
1269 /* Clear previous symbol list */
1270 translation_unit_used_list
= NULL
;
1273 res
= sparse_file(filename
);
1280 struct symbol_list
* __sparse(char *filename
)
1282 struct symbol_list
*res
;
1284 res
= sparse_keep_tokens(filename
);
1286 /* Drop the tokens for this file after parsing */
1287 clear_token_alloc();
1293 struct symbol_list
* sparse(char *filename
)
1295 struct symbol_list
*res
= __sparse(filename
);
1297 /* Evaluate the complete symbol list */
1298 evaluate_symbol_list(res
);