2 * 'sparse' library helper routines.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
19 #include <sys/types.h>
26 #include "expression.h"
28 #include "linearize.h"
31 int verbose
, optimize
, optimize_size
, preprocessing
;
36 # define __GNUC_MINOR__ 95
37 # define __GNUC_PATCHLEVEL__ 0
40 int gcc_major
= __GNUC__
;
41 int gcc_minor
= __GNUC_MINOR__
;
42 int gcc_patchlevel
= __GNUC_PATCHLEVEL__
;
44 static const char *gcc_base_dir
= GCC_BASE
;
46 struct token
*skip_to(struct token
*token
, int op
)
48 while (!match_op(token
, op
) && !eof_token(token
))
53 struct token
*expect(struct token
*token
, int op
, const char *where
)
55 if (!match_op(token
, op
)) {
56 static struct token bad_token
;
57 if (token
!= &bad_token
) {
58 bad_token
.next
= token
;
59 sparse_error(token
->pos
, "Expected %s %s", show_special(op
), where
);
60 sparse_error(token
->pos
, "got %s", show_token(token
));
63 return skip_to(token
, op
);
69 unsigned int hexval(unsigned int c
)
77 retval
= c
- 'a' + 10;
80 retval
= c
- 'A' + 10;
86 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
88 static char buffer
[512];
91 vsprintf(buffer
, fmt
, args
);
92 name
= stream_name(pos
.stream
);
94 fprintf(stderr
, "%s:%d:%d: %s%s\n",
95 name
, pos
.line
, pos
.pos
, type
, buffer
);
98 static int max_warnings
= 100;
99 static int show_info
= 1;
101 void info(struct position pos
, const char * fmt
, ...)
108 do_warn("", pos
, fmt
, args
);
112 void warning(struct position pos
, const char * fmt
, ...)
121 if (!--max_warnings
) {
123 fmt
= "too many warnings";
127 do_warn("warning: ", pos
, fmt
, args
);
131 static void do_error(struct position pos
, const char * fmt
, va_list args
)
133 static int errors
= 0;
136 /* Shut up warnings after an error */
143 fmt
= "too many errors";
147 do_warn("error: ", pos
, fmt
, args
);
151 void sparse_error(struct position pos
, const char * fmt
, ...)
155 do_error(pos
, fmt
, args
);
159 void expression_error(struct expression
*expr
, const char *fmt
, ...)
163 do_error(expr
->pos
, fmt
, args
);
165 expr
->ctype
= &bad_ctype
;
168 void error_die(struct position pos
, const char * fmt
, ...)
172 do_warn("error: ", pos
, fmt
, args
);
177 void die(const char *fmt
, ...)
180 static char buffer
[512];
183 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
186 fprintf(stderr
, "%s\n", buffer
);
190 static struct token
*pre_buffer_begin
= NULL
;
191 static struct token
*pre_buffer_end
= NULL
;
193 int Waddress_space
= 1;
196 int Wcast_truncate
= 1;
199 int Wdefault_bitfield_sign
= 0;
200 int Wdesignated_init
= 1;
202 int Wenum_mismatch
= 1;
203 int Wnon_pointer_null
= 1;
204 int Wold_initializer
= 1;
205 int Wone_bit_signed_bitfield
= 1;
206 int Wparen_string
= 0;
207 int Wptr_subtraction_blows
= 0;
208 int Wreturn_void
= 0;
210 int Wtransparent_union
= 0;
213 int Wuninitialized
= 1;
214 int Wdeclarationafterstatement
= -1;
221 static enum { STANDARD_C89
,
225 STANDARD_GNU99
, } standard
= STANDARD_GNU89
;
228 #define ARCH_M64_DEFAULT 1
230 #define ARCH_M64_DEFAULT 0
233 int arch_m64
= ARCH_M64_DEFAULT
;
234 int arch_msize_long
= 0;
236 #define CMDLINE_INCLUDE 20
237 int cmdline_include_nr
= 0;
238 struct cmdline_include cmdline_include
[CMDLINE_INCLUDE
];
241 void add_pre_buffer(const char *fmt
, ...)
245 struct token
*begin
, *end
;
249 size
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
251 begin
= tokenize_buffer(buffer
, size
, &end
);
252 if (!pre_buffer_begin
)
253 pre_buffer_begin
= begin
;
255 pre_buffer_end
->next
= begin
;
256 pre_buffer_end
= end
;
259 static char **handle_switch_D(char *arg
, char **next
)
261 const char *name
= arg
+ 1;
262 const char *value
= "1";
264 if (!*name
|| isspace(*name
))
265 die("argument to `-D' is missing");
272 if (isspace((unsigned char)c
) || c
== '=') {
278 add_pre_buffer("#define %s %s\n", name
, value
);
282 static char **handle_switch_E(char *arg
, char **next
)
289 static char **handle_switch_I(char *arg
, char **next
)
295 add_pre_buffer("#split_include\n");
298 case '\0': /* Plain "-I" */
301 die("missing argument for -I option");
304 add_pre_buffer("#add_include \"%s/\"\n", path
);
309 static void add_cmdline_include(char *filename
)
311 int fd
= open(filename
, O_RDONLY
);
316 if (cmdline_include_nr
>= CMDLINE_INCLUDE
)
317 die("too many include files for %s\n", filename
);
318 cmdline_include
[cmdline_include_nr
].filename
= filename
;
319 cmdline_include
[cmdline_include_nr
].fd
= fd
;
320 cmdline_include_nr
++;
323 static char **handle_switch_i(char *arg
, char **next
)
325 if (*next
&& !strcmp(arg
, "include"))
326 add_cmdline_include(*++next
);
327 else if (*next
&& !strcmp(arg
, "imacros"))
328 add_cmdline_include(*++next
);
329 else if (*next
&& !strcmp(arg
, "isystem")) {
330 char *path
= *++next
;
332 die("missing argument for -isystem option");
333 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
334 } else if (*next
&& !strcmp(arg
, "idirafter")) {
335 char *path
= *++next
;
337 die("missing argument for -idirafter option");
338 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
343 static char **handle_switch_M(char *arg
, char **next
)
345 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
347 die("missing argument for -%s option", arg
);
353 static char **handle_switch_m(char *arg
, char **next
)
355 if (!strcmp(arg
, "m64")) {
357 } else if (!strcmp(arg
, "m32")) {
359 } else if (!strcmp(arg
, "msize-long")) {
365 static void handle_arch_m64_finalize(void)
369 max_int_alignment
= 8;
370 bits_in_pointer
= 64;
371 pointer_alignment
= 8;
372 size_t_ctype
= &ulong_ctype
;
373 ssize_t_ctype
= &long_ctype
;
375 add_pre_buffer("#weak_define __x86_64__ 1\n");
380 static void handle_arch_msize_long_finalize(void)
382 if (arch_msize_long
) {
383 size_t_ctype
= &ulong_ctype
;
384 ssize_t_ctype
= &long_ctype
;
388 static void handle_arch_finalize(void)
390 handle_arch_m64_finalize();
391 handle_arch_msize_long_finalize();
395 static char **handle_switch_o(char *arg
, char **next
)
397 if (!strcmp (arg
, "o")) { // "-o foo"
399 die("argument to '-o' is missing");
406 static const struct warning
{
410 { "address-space", &Waddress_space
},
411 { "bitwise", &Wbitwise
},
412 { "cast-to-as", &Wcast_to_as
},
413 { "cast-truncate", &Wcast_truncate
},
414 { "context", &Wcontext
},
416 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
417 { "designated-init", &Wdesignated_init
},
418 { "do-while", &Wdo_while
},
419 { "enum-mismatch", &Wenum_mismatch
},
420 { "non-pointer-null", &Wnon_pointer_null
},
421 { "old-initializer", &Wold_initializer
},
422 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield
},
423 { "paren-string", &Wparen_string
},
424 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
425 { "return-void", &Wreturn_void
},
426 { "shadow", &Wshadow
},
427 { "transparent-union", &Wtransparent_union
},
428 { "typesign", &Wtypesign
},
429 { "undef", &Wundef
},
430 { "uninitialized", &Wuninitialized
},
431 { "declaration-after-statement", &Wdeclarationafterstatement
},
441 static char **handle_onoff_switch(char *arg
, char **next
, const struct warning warnings
[], int n
)
443 int flag
= WARNING_ON
;
447 if (!strcmp(p
, "sparse-all")) {
448 for (i
= 0; i
< n
; i
++) {
449 if (*warnings
[i
].flag
!= WARNING_FORCE_OFF
)
450 *warnings
[i
].flag
= WARNING_ON
;
454 // Prefixes "no" and "no-" mean to turn warning off.
455 if (p
[0] == 'n' && p
[1] == 'o') {
459 flag
= WARNING_FORCE_OFF
;
462 for (i
= 0; i
< n
; i
++) {
463 if (!strcmp(p
,warnings
[i
].name
)) {
464 *warnings
[i
].flag
= flag
;
473 static char **handle_switch_W(char *arg
, char **next
)
475 char ** ret
= handle_onoff_switch(arg
, next
, warnings
, ARRAY_SIZE(warnings
));
483 static struct warning debugs
[] = {
484 { "entry", &dbg_entry
},
485 { "dead", &dbg_dead
},
489 static char **handle_switch_v(char *arg
, char **next
)
491 char ** ret
= handle_onoff_switch(arg
, next
, debugs
, ARRAY_SIZE(debugs
));
498 } while (*++arg
== 'v');
503 static void handle_onoff_switch_finalize(const struct warning warnings
[], int n
)
507 for (i
= 0; i
< n
; i
++) {
508 if (*warnings
[i
].flag
== WARNING_FORCE_OFF
)
509 *warnings
[i
].flag
= WARNING_OFF
;
513 static void handle_switch_W_finalize(void)
515 handle_onoff_switch_finalize(warnings
, ARRAY_SIZE(warnings
));
517 /* default Wdeclarationafterstatement based on the C dialect */
518 if (-1 == Wdeclarationafterstatement
)
524 Wdeclarationafterstatement
= 1;
530 Wdeclarationafterstatement
= 0;
540 static void handle_switch_v_finalize(void)
542 handle_onoff_switch_finalize(debugs
, ARRAY_SIZE(debugs
));
545 static char **handle_switch_U(char *arg
, char **next
)
547 const char *name
= arg
+ 1;
548 add_pre_buffer ("#undef %s\n", name
);
552 static char **handle_switch_O(char *arg
, char **next
)
555 if (arg
[1] >= '0' && arg
[1] <= '9')
556 level
= arg
[1] - '0';
558 optimize_size
= arg
[1] == 's';
562 static char **handle_switch_ftabstop(char *arg
, char **next
)
568 die("error: missing argument to \"-ftabstop=\"");
570 /* we silently ignore silly values */
571 val
= strtoul(arg
, &end
, 10);
572 if (*end
== '\0' && 1 <= val
&& val
<= 100)
578 static char **handle_switch_f(char *arg
, char **next
)
582 if (!strncmp(arg
, "tabstop=", 8))
583 return handle_switch_ftabstop(arg
+8, next
);
585 /* handle switches w/ arguments above, boolean and only boolean below */
587 if (!strncmp(arg
, "no-", 3)) {
590 /* handle switch here.. */
594 static char **handle_switch_G(char *arg
, char **next
)
596 if (!strcmp (arg
, "G") && *next
)
597 return next
+ 1; // "-G 0"
599 return next
; // "-G0" or (bogus) terminal "-G"
602 static char **handle_switch_a(char *arg
, char **next
)
604 if (!strcmp (arg
, "ansi"))
605 standard
= STANDARD_C89
;
610 static char **handle_switch_s(char *arg
, char **next
)
612 if (!strncmp (arg
, "std=", 4))
616 if (!strcmp (arg
, "c89") ||
617 !strcmp (arg
, "iso9899:1990"))
618 standard
= STANDARD_C89
;
620 else if (!strcmp (arg
, "iso9899:199409"))
621 standard
= STANDARD_C94
;
623 else if (!strcmp (arg
, "c99") ||
624 !strcmp (arg
, "c9x") ||
625 !strcmp (arg
, "iso9899:1999") ||
626 !strcmp (arg
, "iso9899:199x"))
627 standard
= STANDARD_C99
;
629 else if (!strcmp (arg
, "gnu89"))
630 standard
= STANDARD_GNU89
;
632 else if (!strcmp (arg
, "gnu99") || !strcmp (arg
, "gnu9x"))
633 standard
= STANDARD_GNU99
;
636 die ("Unsupported C dialect");
642 static char **handle_nostdinc(char *arg
, char **next
)
644 add_pre_buffer("#nostdinc\n");
648 static char **handle_base_dir(char *arg
, char **next
)
650 gcc_base_dir
= *++next
;
652 die("missing argument for -gcc-base-dir option");
656 static char **handle_no_lineno(char *arg
, char **next
)
664 char **(*fn
)(char *, char **);
667 static char **handle_switch(char *arg
, char **next
)
669 static struct switches cmd
[] = {
670 { "nostdinc", handle_nostdinc
},
671 { "gcc-base-dir", handle_base_dir
},
672 { "no-lineno", handle_no_lineno
},
678 case 'D': return handle_switch_D(arg
, next
);
679 case 'E': return handle_switch_E(arg
, next
);
680 case 'I': return handle_switch_I(arg
, next
);
681 case 'i': return handle_switch_i(arg
, next
);
682 case 'M': return handle_switch_M(arg
, next
);
683 case 'm': return handle_switch_m(arg
, next
);
684 case 'o': return handle_switch_o(arg
, next
);
685 case 'U': return handle_switch_U(arg
, next
);
686 case 'v': return handle_switch_v(arg
, next
);
687 case 'W': return handle_switch_W(arg
, next
);
688 case 'O': return handle_switch_O(arg
, next
);
689 case 'f': return handle_switch_f(arg
, next
);
690 case 'G': return handle_switch_G(arg
, next
);
691 case 'a': return handle_switch_a(arg
, next
);
692 case 's': return handle_switch_s(arg
, next
);
699 if (!strcmp(s
->name
, arg
))
700 return s
->fn(arg
, next
);
705 * Ignore unknown command line options:
706 * they're probably gcc switches
711 void declare_builtin_functions(void)
713 /* Gaah. gcc knows tons of builtin <string.h> functions */
714 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
715 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
716 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
717 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
718 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
719 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
720 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
721 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
722 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
723 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
724 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
725 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
726 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
727 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
728 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
730 /* And bitwise operations.. */
731 add_pre_buffer("extern int __builtin_clz(int);\n");
732 add_pre_buffer("extern int __builtin_clzl(long);\n");
733 add_pre_buffer("extern int __builtin_clzll(long long);\n");
734 add_pre_buffer("extern int __builtin_ctz(int);\n");
735 add_pre_buffer("extern int __builtin_ctzl(long);\n");
736 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
737 add_pre_buffer("extern int __builtin_ffs(int);\n");
738 add_pre_buffer("extern int __builtin_ffsl(long);\n");
739 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
740 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
741 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
742 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
744 /* And some random ones.. */
745 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
746 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
747 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
748 add_pre_buffer("extern void __builtin_trap(void);\n");
749 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
750 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
751 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
752 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
753 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
754 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
755 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
756 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
757 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
758 add_pre_buffer("extern long __builtin_labs(long);\n");
759 add_pre_buffer("extern double __builtin_fabs(double);\n");
760 add_pre_buffer("extern void __sync_synchronize();\n");
761 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
763 /* Add Blackfin-specific stuff */
766 "extern void __builtin_bfin_csync(void);\n"
767 "extern void __builtin_bfin_ssync(void);\n"
768 "extern int __builtin_bfin_norm_fr1x32(int);\n"
772 /* And some floating point stuff.. */
773 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
774 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
775 add_pre_buffer("extern int __builtin_isless(float, float);\n");
776 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
777 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
778 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
780 /* And some __FORTIFY_SOURCE ones.. */
781 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
782 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
783 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
784 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
785 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
786 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
787 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
788 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
789 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
790 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
791 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
792 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
793 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
794 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
795 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
798 void create_builtin_stream(void)
800 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major
);
801 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor
);
802 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel
);
804 /* We add compiler headers path here because we have to parse
805 * the arguments to get it, falling back to default. */
806 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir
);
807 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir
);
809 add_pre_buffer("#define __extension__\n");
810 add_pre_buffer("#define __pragma__\n");
812 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
813 // solaris/sparc that is really "unsigned int" and for linux/x86_64
814 // it is "long unsigned int". In either case we can probably
815 // get away with this. We need the #weak_define as cgcc will define
816 // the right __SIZE_TYPE__.
817 if (size_t_ctype
== &ulong_ctype
)
818 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
820 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
821 add_pre_buffer("#weak_define __STDC__ 1\n");
826 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
830 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
831 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
835 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
836 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
843 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
850 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
851 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
852 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
853 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
854 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
855 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
856 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
857 add_pre_buffer("#define __builtin_va_end(arg)\n");
858 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
860 /* FIXME! We need to do these as special magic macros at expansion time! */
861 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
864 add_pre_buffer("#define __OPTIMIZE__ 1\n");
866 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
868 /* GCC defines these for limits.h */
869 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__
) "\n");
870 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__
) "\n");
871 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__
) "\n");
872 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__
) "\n");
873 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__
) "\n");
874 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__
) "\n");
877 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
879 // Preprocess the stream
880 token
= preprocess(token
);
882 if (preprocess_only
) {
883 while (!eof_token(token
)) {
885 struct token
*next
= token
->next
;
886 const char *separator
= "";
887 if (next
->pos
.whitespace
)
889 if (next
->pos
.newline
) {
890 separator
= "\n\t\t\t\t\t";
891 prec
= next
->pos
.pos
;
895 printf("%s%.*s", show_token(token
), prec
, separator
);
903 // Parse the resulting C code
904 while (!eof_token(token
))
905 token
= external_declaration(token
, &translation_unit_used_list
);
906 return translation_unit_used_list
;
909 static struct symbol_list
*sparse_file(const char *filename
)
914 if (strcmp (filename
, "-") == 0) {
917 fd
= open(filename
, O_RDONLY
);
919 die("No such file: %s", filename
);
922 // Tokenize the input stream
923 token
= tokenize(filename
, fd
, NULL
, includepath
);
924 store_all_tokens(token
);
927 return sparse_tokenstream(token
);
931 * This handles the "-include" directive etc: we're in global
932 * scope, and all types/macros etc will affect all the following
935 * NOTE NOTE NOTE! "#undef" of anything in this stage will
936 * affect all subsequent files too, i.e. we can have non-local
937 * behaviour between files!
939 static struct symbol_list
*sparse_initial(void)
944 // Prepend any "include" file to the stream.
945 // We're in global scope, it will affect all files!
947 for (i
= cmdline_include_nr
- 1; i
>= 0; i
--)
948 token
= tokenize(cmdline_include
[i
].filename
, cmdline_include
[i
].fd
,
951 // Prepend the initial built-in stream
953 pre_buffer_end
->next
= token
;
954 return sparse_tokenstream(pre_buffer_begin
);
957 struct symbol_list
*sparse_initialize(int argc
, char **argv
, struct string_list
**filelist
)
960 struct symbol_list
*list
;
962 // Initialize symbol stream first, so that we can add defines etc
971 if (arg
[0] == '-' && arg
[1]) {
972 args
= handle_switch(arg
+1, args
);
975 add_ptr_list_notag(filelist
, arg
);
977 handle_switch_W_finalize();
978 handle_switch_v_finalize();
980 handle_arch_finalize();
983 if (!ptr_list_empty(filelist
)) {
984 // Initialize type system
987 create_builtin_stream();
988 add_pre_buffer("#define __CHECKER__ 1\n");
989 if (!preprocess_only
)
990 declare_builtin_functions();
992 list
= sparse_initial();
995 * Protect the initial token allocations, since
996 * they need to survive all the others
998 protect_token_alloc();
1003 struct symbol_list
* sparse_keep_tokens(char *filename
)
1005 struct symbol_list
*res
;
1007 /* Clear previous symbol list */
1008 translation_unit_used_list
= NULL
;
1011 res
= sparse_file(filename
);
1018 struct symbol_list
* __sparse(char *filename
)
1020 struct symbol_list
*res
;
1022 res
= sparse_keep_tokens(filename
);
1024 /* Drop the tokens for this file after parsing */
1025 clear_token_alloc();
1031 struct symbol_list
* sparse(char *filename
)
1033 struct symbol_list
*res
= __sparse(filename
);
1035 /* Evaluate the complete symbol list */
1036 evaluate_symbol_list(res
);