I have updated the sparse.1 man page including the __bitwise
[smatch.git] / lib.c
blobb4d394474a594765b46c620a21fe3f2aad102741
1 /*
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
8 */
9 #include <ctype.h>
10 #include <fcntl.h>
11 #include <stdarg.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <assert.h>
19 #include <sys/types.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "token.h"
24 #include "parse.h"
25 #include "symbol.h"
26 #include "expression.h"
27 #include "scope.h"
28 #include "linearize.h"
29 #include "target.h"
31 int verbose, optimize, optimize_size, preprocessing;
32 int die_if_error = 0;
34 #ifndef __GNUC__
35 # define __GNUC__ 2
36 # define __GNUC_MINOR__ 95
37 # define __GNUC_PATCHLEVEL__ 0
38 #endif
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))
49 token = token->next;
50 return 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));
62 if (op == ';')
63 return skip_to(token, op);
64 return &bad_token;
66 return token->next;
69 unsigned int hexval(unsigned int c)
71 int retval = 256;
72 switch (c) {
73 case '0'...'9':
74 retval = c - '0';
75 break;
76 case 'a'...'f':
77 retval = c - 'a' + 10;
78 break;
79 case 'A'...'F':
80 retval = c - 'A' + 10;
81 break;
83 return retval;
86 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
88 static char buffer[512];
89 const char *name;
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, ...)
103 va_list args;
105 if (!show_info)
106 return;
107 va_start(args, fmt);
108 do_warn("", pos, fmt, args);
109 va_end(args);
112 void warning(struct position pos, const char * fmt, ...)
114 va_list args;
116 if (!max_warnings) {
117 show_info = 0;
118 return;
121 if (!--max_warnings) {
122 show_info = 0;
123 fmt = "too many warnings";
126 va_start(args, fmt);
127 do_warn("warning: ", pos, fmt, args);
128 va_end(args);
131 static void do_error(struct position pos, const char * fmt, va_list args)
133 static int errors = 0;
134 die_if_error = 1;
135 show_info = 1;
136 /* Shut up warnings after an error */
137 max_warnings = 0;
138 if (errors > 100) {
139 static int once = 0;
140 show_info = 0;
141 if (once)
142 return;
143 fmt = "too many errors";
144 once = 1;
147 do_warn("error: ", pos, fmt, args);
148 errors++;
151 void sparse_error(struct position pos, const char * fmt, ...)
153 va_list args;
154 va_start(args, fmt);
155 do_error(pos, fmt, args);
156 va_end(args);
159 void expression_error(struct expression *expr, const char *fmt, ...)
161 va_list args;
162 va_start(args, fmt);
163 do_error(expr->pos, fmt, args);
164 va_end(args);
165 expr->ctype = &bad_ctype;
168 void error_die(struct position pos, const char * fmt, ...)
170 va_list args;
171 va_start(args, fmt);
172 do_warn("error: ", pos, fmt, args);
173 va_end(args);
174 exit(1);
177 void die(const char *fmt, ...)
179 va_list args;
180 static char buffer[512];
182 va_start(args, fmt);
183 vsnprintf(buffer, sizeof(buffer), fmt, args);
184 va_end(args);
186 fprintf(stderr, "%s\n", buffer);
187 exit(1);
190 static struct token *pre_buffer_begin = NULL;
191 static struct token *pre_buffer_end = NULL;
193 int Waddress_space = 1;
194 int Wbitwise = 0;
195 int Wcast_to_as = 0;
196 int Wcast_truncate = 1;
197 int Wcontext = 1;
198 int Wdecl = 1;
199 int Wdefault_bitfield_sign = 0;
200 int Wdesignated_init = 1;
201 int Wdo_while = 0;
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;
209 int Wshadow = 0;
210 int Wtransparent_union = 0;
211 int Wtypesign = 0;
212 int Wundef = 0;
213 int Wuninitialized = 1;
214 int Wdeclarationafterstatement = -1;
216 int dbg_entry = 0;
217 int dbg_dead = 0;
219 int preprocess_only;
221 static enum { STANDARD_C89,
222 STANDARD_C94,
223 STANDARD_C99,
224 STANDARD_GNU89,
225 STANDARD_GNU99, } standard = STANDARD_GNU89;
227 #ifdef __x86_64__
228 #define ARCH_M64_DEFAULT 1
229 #else
230 #define ARCH_M64_DEFAULT 0
231 #endif
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, ...)
243 va_list args;
244 unsigned int size;
245 struct token *begin, *end;
246 char buffer[4096];
248 va_start(args, fmt);
249 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
250 va_end(args);
251 begin = tokenize_buffer(buffer, size, &end);
252 if (!pre_buffer_begin)
253 pre_buffer_begin = begin;
254 if (pre_buffer_end)
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");
267 for (;;) {
268 char c;
269 c = *++arg;
270 if (!c)
271 break;
272 if (isspace((unsigned char)c) || c == '=') {
273 *arg = '\0';
274 value = arg + 1;
275 break;
278 add_pre_buffer("#define %s %s\n", name, value);
279 return next;
282 static char **handle_switch_E(char *arg, char **next)
284 if (arg[1] == '\0')
285 preprocess_only = 1;
286 return next;
289 static char **handle_switch_I(char *arg, char **next)
291 char *path = arg+1;
293 switch (arg[1]) {
294 case '-':
295 add_pre_buffer("#split_include\n");
296 break;
298 case '\0': /* Plain "-I" */
299 path = *++next;
300 if (!path)
301 die("missing argument for -I option");
302 /* Fall through */
303 default:
304 add_pre_buffer("#add_include \"%s/\"\n", path);
306 return next;
309 static void add_cmdline_include(char *filename)
311 int fd = open(filename, O_RDONLY);
312 if (fd < 0) {
313 perror(filename);
314 return;
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;
331 if (!path)
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;
336 if (!path)
337 die("missing argument for -idirafter option");
338 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
340 return next;
343 static char **handle_switch_M(char *arg, char **next)
345 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
346 if (!*next)
347 die("missing argument for -%s option", arg);
348 return next + 1;
350 return next;
353 static char **handle_switch_m(char *arg, char **next)
355 if (!strcmp(arg, "m64")) {
356 arch_m64 = 1;
357 } else if (!strcmp(arg, "m32")) {
358 arch_m64 = 0;
359 } else if (!strcmp(arg, "msize-long")) {
360 arch_msize_long = 1;
362 return next;
365 static void handle_arch_m64_finalize(void)
367 if (arch_m64) {
368 bits_in_long = 64;
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;
374 #ifdef __x86_64__
375 add_pre_buffer("#weak_define x86_64 1\n");
376 add_pre_buffer("#weak_define __x86_64 1\n");
377 add_pre_buffer("#weak_define __x86_64__ 1\n");
378 #endif
382 static void handle_arch_msize_long_finalize(void)
384 if (arch_msize_long) {
385 size_t_ctype = &ulong_ctype;
386 ssize_t_ctype = &long_ctype;
390 static void handle_arch_finalize(void)
392 handle_arch_m64_finalize();
393 handle_arch_msize_long_finalize();
397 static char **handle_switch_o(char *arg, char **next)
399 if (!strcmp (arg, "o")) { // "-o foo"
400 if (!*++next)
401 die("argument to '-o' is missing");
403 // else "-ofoo"
405 return next;
408 static const struct warning {
409 const char *name;
410 int *flag;
411 } warnings[] = {
412 { "address-space", &Waddress_space },
413 { "bitwise", &Wbitwise },
414 { "cast-to-as", &Wcast_to_as },
415 { "cast-truncate", &Wcast_truncate },
416 { "context", &Wcontext },
417 { "decl", &Wdecl },
418 { "default-bitfield-sign", &Wdefault_bitfield_sign },
419 { "designated-init", &Wdesignated_init },
420 { "do-while", &Wdo_while },
421 { "enum-mismatch", &Wenum_mismatch },
422 { "non-pointer-null", &Wnon_pointer_null },
423 { "old-initializer", &Wold_initializer },
424 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
425 { "paren-string", &Wparen_string },
426 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
427 { "return-void", &Wreturn_void },
428 { "shadow", &Wshadow },
429 { "transparent-union", &Wtransparent_union },
430 { "typesign", &Wtypesign },
431 { "undef", &Wundef },
432 { "uninitialized", &Wuninitialized },
433 { "declaration-after-statement", &Wdeclarationafterstatement },
436 enum {
437 WARNING_OFF,
438 WARNING_ON,
439 WARNING_FORCE_OFF
443 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
445 int flag = WARNING_ON;
446 char *p = arg + 1;
447 unsigned i;
449 if (!strcmp(p, "sparse-all")) {
450 for (i = 0; i < n; i++) {
451 if (*warnings[i].flag != WARNING_FORCE_OFF)
452 *warnings[i].flag = WARNING_ON;
456 // Prefixes "no" and "no-" mean to turn warning off.
457 if (p[0] == 'n' && p[1] == 'o') {
458 p += 2;
459 if (p[0] == '-')
460 p++;
461 flag = WARNING_FORCE_OFF;
464 for (i = 0; i < n; i++) {
465 if (!strcmp(p,warnings[i].name)) {
466 *warnings[i].flag = flag;
467 return next;
471 // Unknown.
472 return NULL;
475 static char **handle_switch_W(char *arg, char **next)
477 char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
478 if (ret)
479 return ret;
481 // Unknown.
482 return next;
485 static struct warning debugs[] = {
486 { "entry", &dbg_entry},
487 { "dead", &dbg_dead},
491 static char **handle_switch_v(char *arg, char **next)
493 char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
494 if (ret)
495 return ret;
497 // Unknown.
498 do {
499 verbose++;
500 } while (*++arg == 'v');
501 return next;
505 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
507 unsigned i;
509 for (i = 0; i < n; i++) {
510 if (*warnings[i].flag == WARNING_FORCE_OFF)
511 *warnings[i].flag = WARNING_OFF;
515 static void handle_switch_W_finalize(void)
517 handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
519 /* default Wdeclarationafterstatement based on the C dialect */
520 if (-1 == Wdeclarationafterstatement)
522 switch (standard)
524 case STANDARD_C89:
525 case STANDARD_C94:
526 Wdeclarationafterstatement = 1;
527 break;
529 case STANDARD_C99:
530 case STANDARD_GNU89:
531 case STANDARD_GNU99:
532 Wdeclarationafterstatement = 0;
533 break;
535 default:
536 assert (0);
542 static void handle_switch_v_finalize(void)
544 handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
547 static char **handle_switch_U(char *arg, char **next)
549 const char *name = arg + 1;
550 add_pre_buffer ("#undef %s\n", name);
551 return next;
554 static char **handle_switch_O(char *arg, char **next)
556 int level = 1;
557 if (arg[1] >= '0' && arg[1] <= '9')
558 level = arg[1] - '0';
559 optimize = level;
560 optimize_size = arg[1] == 's';
561 return next;
564 static char **handle_switch_ftabstop(char *arg, char **next)
566 char *end;
567 unsigned long val;
569 if (*arg == '\0')
570 die("error: missing argument to \"-ftabstop=\"");
572 /* we silently ignore silly values */
573 val = strtoul(arg, &end, 10);
574 if (*end == '\0' && 1 <= val && val <= 100)
575 tabstop = val;
577 return next;
580 static char **handle_switch_f(char *arg, char **next)
582 arg++;
584 if (!strncmp(arg, "tabstop=", 8))
585 return handle_switch_ftabstop(arg+8, next);
587 /* handle switches w/ arguments above, boolean and only boolean below */
589 if (!strncmp(arg, "no-", 3)) {
590 arg += 3;
592 /* handle switch here.. */
593 return next;
596 static char **handle_switch_G(char *arg, char **next)
598 if (!strcmp (arg, "G") && *next)
599 return next + 1; // "-G 0"
600 else
601 return next; // "-G0" or (bogus) terminal "-G"
604 static char **handle_switch_a(char *arg, char **next)
606 if (!strcmp (arg, "ansi"))
607 standard = STANDARD_C89;
609 return next;
612 static char **handle_switch_s(char *arg, char **next)
614 if (!strncmp (arg, "std=", 4))
616 arg += 4;
618 if (!strcmp (arg, "c89") ||
619 !strcmp (arg, "iso9899:1990"))
620 standard = STANDARD_C89;
622 else if (!strcmp (arg, "iso9899:199409"))
623 standard = STANDARD_C94;
625 else if (!strcmp (arg, "c99") ||
626 !strcmp (arg, "c9x") ||
627 !strcmp (arg, "iso9899:1999") ||
628 !strcmp (arg, "iso9899:199x"))
629 standard = STANDARD_C99;
631 else if (!strcmp (arg, "gnu89"))
632 standard = STANDARD_GNU89;
634 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
635 standard = STANDARD_GNU99;
637 else
638 die ("Unsupported C dialect");
641 return next;
644 static char **handle_nostdinc(char *arg, char **next)
646 add_pre_buffer("#nostdinc\n");
647 return next;
650 static char **handle_base_dir(char *arg, char **next)
652 gcc_base_dir = *++next;
653 if (!gcc_base_dir)
654 die("missing argument for -gcc-base-dir option");
655 return next;
658 struct switches {
659 const char *name;
660 char **(*fn)(char *, char **);
663 static char **handle_switch(char *arg, char **next)
665 static struct switches cmd[] = {
666 { "nostdinc", handle_nostdinc },
667 { "gcc-base-dir", handle_base_dir},
668 { NULL, NULL }
670 struct switches *s;
672 switch (*arg) {
673 case 'D': return handle_switch_D(arg, next);
674 case 'E': return handle_switch_E(arg, next);
675 case 'I': return handle_switch_I(arg, next);
676 case 'i': return handle_switch_i(arg, next);
677 case 'M': return handle_switch_M(arg, next);
678 case 'm': return handle_switch_m(arg, next);
679 case 'o': return handle_switch_o(arg, next);
680 case 'U': return handle_switch_U(arg, next);
681 case 'v': return handle_switch_v(arg, next);
682 case 'W': return handle_switch_W(arg, next);
683 case 'O': return handle_switch_O(arg, next);
684 case 'f': return handle_switch_f(arg, next);
685 case 'G': return handle_switch_G(arg, next);
686 case 'a': return handle_switch_a(arg, next);
687 case 's': return handle_switch_s(arg, next);
688 default:
689 break;
692 s = cmd;
693 while (s->name) {
694 if (!strcmp(s->name, arg))
695 return s->fn(arg, next);
696 s++;
700 * Ignore unknown command line options:
701 * they're probably gcc switches
703 return next;
706 void declare_builtin_functions(void)
708 /* Gaah. gcc knows tons of builtin <string.h> functions */
709 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
710 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
711 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
712 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
713 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
714 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
715 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
716 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
717 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
718 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
719 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
720 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
721 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
722 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
723 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
725 /* And bitwise operations.. */
726 add_pre_buffer("extern int __builtin_clz(int);\n");
727 add_pre_buffer("extern int __builtin_clzl(long);\n");
728 add_pre_buffer("extern int __builtin_clzll(long long);\n");
729 add_pre_buffer("extern int __builtin_ctz(int);\n");
730 add_pre_buffer("extern int __builtin_ctzl(long);\n");
731 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
732 add_pre_buffer("extern int __builtin_ffs(int);\n");
733 add_pre_buffer("extern int __builtin_ffsl(long);\n");
734 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
735 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
736 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
737 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
739 /* And some random ones.. */
740 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
741 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
742 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
743 add_pre_buffer("extern void __builtin_trap(void);\n");
744 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
745 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
746 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
747 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
748 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
749 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
750 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
751 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
752 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
753 add_pre_buffer("extern long __builtin_labs(long);\n");
754 add_pre_buffer("extern double __builtin_fabs(double);\n");
755 add_pre_buffer("extern void __sync_synchronize();\n");
756 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
758 /* Add Blackfin-specific stuff */
759 add_pre_buffer(
760 "#ifdef __bfin__\n"
761 "extern void __builtin_bfin_csync(void);\n"
762 "extern void __builtin_bfin_ssync(void);\n"
763 "extern int __builtin_bfin_norm_fr1x32(int);\n"
764 "#endif\n"
767 /* And some floating point stuff.. */
768 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
769 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
770 add_pre_buffer("extern int __builtin_isless(float, float);\n");
771 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
772 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
773 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
775 /* And some __FORTIFY_SOURCE ones.. */
776 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
777 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
778 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
779 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
780 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
781 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
782 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
783 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
784 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
785 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
786 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
787 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
788 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
789 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
790 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
793 void create_builtin_stream(void)
795 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
796 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
797 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
799 /* We add compiler headers path here because we have to parse
800 * the arguments to get it, falling back to default. */
801 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
802 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
804 add_pre_buffer("#define __extension__\n");
805 add_pre_buffer("#define __pragma__\n");
807 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
808 // solaris/sparc that is really "unsigned int" and for linux/x86_64
809 // it is "long unsigned int". In either case we can probably
810 // get away with this. We need the #weak_define as cgcc will define
811 // the right __SIZE_TYPE__.
812 if (size_t_ctype == &ulong_ctype)
813 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
814 else
815 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
816 add_pre_buffer("#weak_define __STDC__ 1\n");
818 switch (standard)
820 case STANDARD_C89:
821 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
822 break;
824 case STANDARD_C94:
825 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
826 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
827 break;
829 case STANDARD_C99:
830 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
831 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
832 break;
834 case STANDARD_GNU89:
835 break;
837 case STANDARD_GNU99:
838 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
839 break;
841 default:
842 assert (0);
845 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
846 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
847 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
848 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
849 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
850 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
851 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
852 add_pre_buffer("#define __builtin_va_end(arg)\n");
853 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
855 /* FIXME! We need to do these as special magic macros at expansion time! */
856 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
858 if (optimize)
859 add_pre_buffer("#define __OPTIMIZE__ 1\n");
860 if (optimize_size)
861 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
863 /* GCC defines these for limits.h */
864 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__) "\n");
865 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__) "\n");
866 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__) "\n");
867 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__) "\n");
868 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__) "\n");
869 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__) "\n");
872 static struct symbol_list *sparse_tokenstream(struct token *token)
874 // Preprocess the stream
875 token = preprocess(token);
877 if (preprocess_only) {
878 while (!eof_token(token)) {
879 int prec = 1;
880 struct token *next = token->next;
881 const char *separator = "";
882 if (next->pos.whitespace)
883 separator = " ";
884 if (next->pos.newline) {
885 separator = "\n\t\t\t\t\t";
886 prec = next->pos.pos;
887 if (prec > 4)
888 prec = 4;
890 printf("%s%.*s", show_token(token), prec, separator);
891 token = next;
893 putchar('\n');
895 return NULL;
898 // Parse the resulting C code
899 while (!eof_token(token))
900 token = external_declaration(token, &translation_unit_used_list);
901 return translation_unit_used_list;
904 static struct symbol_list *sparse_file(const char *filename)
906 int fd;
907 struct token *token;
909 if (strcmp (filename, "-") == 0) {
910 fd = 0;
911 } else {
912 fd = open(filename, O_RDONLY);
913 if (fd < 0)
914 die("No such file: %s", filename);
917 // Tokenize the input stream
918 token = tokenize(filename, fd, NULL, includepath);
919 close(fd);
921 return sparse_tokenstream(token);
925 * This handles the "-include" directive etc: we're in global
926 * scope, and all types/macros etc will affect all the following
927 * files.
929 * NOTE NOTE NOTE! "#undef" of anything in this stage will
930 * affect all subsequent files too, i.e. we can have non-local
931 * behaviour between files!
933 static struct symbol_list *sparse_initial(void)
935 struct token *token;
936 int i;
938 // Prepend any "include" file to the stream.
939 // We're in global scope, it will affect all files!
940 token = NULL;
941 for (i = cmdline_include_nr - 1; i >= 0; i--)
942 token = tokenize(cmdline_include[i].filename, cmdline_include[i].fd,
943 token, includepath);
945 // Prepend the initial built-in stream
946 if (token)
947 pre_buffer_end->next = token;
948 return sparse_tokenstream(pre_buffer_begin);
951 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
953 char **args;
954 struct symbol_list *list;
956 // Initialize symbol stream first, so that we can add defines etc
957 init_symbols();
959 args = argv;
960 for (;;) {
961 char *arg = *++args;
962 if (!arg)
963 break;
965 if (arg[0] == '-' && arg[1]) {
966 args = handle_switch(arg+1, args);
967 continue;
969 add_ptr_list_notag(filelist, arg);
971 handle_switch_W_finalize();
972 handle_switch_v_finalize();
974 handle_arch_finalize();
976 list = NULL;
977 if (!ptr_list_empty(filelist)) {
978 // Initialize type system
979 init_ctype();
981 create_builtin_stream();
982 add_pre_buffer("#define __CHECKER__ 1\n");
983 if (!preprocess_only)
984 declare_builtin_functions();
986 list = sparse_initial();
989 * Protect the initial token allocations, since
990 * they need to survive all the others
992 protect_token_alloc();
994 return list;
997 struct symbol_list * sparse_keep_tokens(char *filename)
999 struct symbol_list *res;
1001 /* Clear previous symbol list */
1002 translation_unit_used_list = NULL;
1004 new_file_scope();
1005 res = sparse_file(filename);
1007 /* And return it */
1008 return res;
1012 struct symbol_list * __sparse(char *filename)
1014 struct symbol_list *res;
1016 res = sparse_keep_tokens(filename);
1018 /* Drop the tokens for this file after parsing */
1019 clear_token_alloc();
1021 /* And return it */
1022 return res;
1025 struct symbol_list * sparse(char *filename)
1027 struct symbol_list *res = __sparse(filename);
1029 /* Evaluate the complete symbol list */
1030 evaluate_symbol_list(res);
1032 return res;