Merge branch 'llvmcore'
[smatch.git] / lib.c
blob3f687ae9caa9f072c1449287e4c257c6e3b2fe69
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"
30 #include "version.h"
32 int verbose, optimize, optimize_size, preprocessing;
33 int die_if_error = 0;
35 #ifndef __GNUC__
36 # define __GNUC__ 2
37 # define __GNUC_MINOR__ 95
38 # define __GNUC_PATCHLEVEL__ 0
39 #endif
41 int gcc_major = __GNUC__;
42 int gcc_minor = __GNUC_MINOR__;
43 int gcc_patchlevel = __GNUC_PATCHLEVEL__;
45 static const char *gcc_base_dir = GCC_BASE;
47 struct token *skip_to(struct token *token, int op)
49 while (!match_op(token, op) && !eof_token(token))
50 token = token->next;
51 return token;
54 struct token *expect(struct token *token, int op, const char *where)
56 if (!match_op(token, op)) {
57 static struct token bad_token;
58 if (token != &bad_token) {
59 bad_token.next = token;
60 sparse_error(token->pos, "Expected %s %s", show_special(op), where);
61 sparse_error(token->pos, "got %s", show_token(token));
63 if (op == ';')
64 return skip_to(token, op);
65 return &bad_token;
67 return token->next;
70 unsigned int hexval(unsigned int c)
72 int retval = 256;
73 switch (c) {
74 case '0'...'9':
75 retval = c - '0';
76 break;
77 case 'a'...'f':
78 retval = c - 'a' + 10;
79 break;
80 case 'A'...'F':
81 retval = c - 'A' + 10;
82 break;
84 return retval;
87 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
89 static char buffer[512];
90 const char *name;
92 vsprintf(buffer, fmt, args);
93 name = stream_name(pos.stream);
95 fprintf(stderr, "%s:%d:%d: %s%s\n",
96 name, pos.line, pos.pos, type, buffer);
99 static int max_warnings = 100;
100 static int show_info = 1;
102 void info(struct position pos, const char * fmt, ...)
104 va_list args;
106 if (!show_info)
107 return;
108 va_start(args, fmt);
109 do_warn("", pos, fmt, args);
110 va_end(args);
113 void warning(struct position pos, const char * fmt, ...)
115 va_list args;
117 if (!max_warnings) {
118 show_info = 0;
119 return;
122 if (!--max_warnings) {
123 show_info = 0;
124 fmt = "too many warnings";
127 va_start(args, fmt);
128 do_warn("warning: ", pos, fmt, args);
129 va_end(args);
132 static void do_error(struct position pos, const char * fmt, va_list args)
134 static int errors = 0;
135 die_if_error = 1;
136 show_info = 1;
137 /* Shut up warnings after an error */
138 max_warnings = 0;
139 if (errors > 100) {
140 static int once = 0;
141 show_info = 0;
142 if (once)
143 return;
144 fmt = "too many errors";
145 once = 1;
148 do_warn("error: ", pos, fmt, args);
149 errors++;
152 void sparse_error(struct position pos, const char * fmt, ...)
154 va_list args;
155 va_start(args, fmt);
156 do_error(pos, fmt, args);
157 va_end(args);
160 void expression_error(struct expression *expr, const char *fmt, ...)
162 va_list args;
163 va_start(args, fmt);
164 do_error(expr->pos, fmt, args);
165 va_end(args);
166 expr->ctype = &bad_ctype;
169 void error_die(struct position pos, const char * fmt, ...)
171 va_list args;
172 va_start(args, fmt);
173 do_warn("error: ", pos, fmt, args);
174 va_end(args);
175 exit(1);
178 void die(const char *fmt, ...)
180 va_list args;
181 static char buffer[512];
183 va_start(args, fmt);
184 vsnprintf(buffer, sizeof(buffer), fmt, args);
185 va_end(args);
187 fprintf(stderr, "%s\n", buffer);
188 exit(1);
191 static struct token *pre_buffer_begin = NULL;
192 static struct token *pre_buffer_end = NULL;
194 int Waddress_space = 1;
195 int Wbitwise = 0;
196 int Wcast_to_as = 0;
197 int Wcast_truncate = 1;
198 int Wcontext = 1;
199 int Wdecl = 1;
200 int Wdeclarationafterstatement = -1;
201 int Wdefault_bitfield_sign = 0;
202 int Wdesignated_init = 1;
203 int Wdo_while = 0;
204 int Winit_cstring = 0;
205 int Wenum_mismatch = 1;
206 int Wnon_pointer_null = 1;
207 int Wold_initializer = 1;
208 int Wone_bit_signed_bitfield = 1;
209 int Wparen_string = 0;
210 int Wptr_subtraction_blows = 0;
211 int Wreturn_void = 0;
212 int Wshadow = 0;
213 int Wtransparent_union = 0;
214 int Wtypesign = 0;
215 int Wundef = 0;
216 int Wuninitialized = 1;
217 int Wvla = 1;
219 int dbg_entry = 0;
220 int dbg_dead = 0;
222 int preprocess_only;
224 static enum { STANDARD_C89,
225 STANDARD_C94,
226 STANDARD_C99,
227 STANDARD_GNU89,
228 STANDARD_GNU99, } standard = STANDARD_GNU89;
230 #ifdef __x86_64__
231 #define ARCH_M64_DEFAULT 1
232 #else
233 #define ARCH_M64_DEFAULT 0
234 #endif
236 int arch_m64 = ARCH_M64_DEFAULT;
237 int arch_msize_long = 0;
239 #define CMDLINE_INCLUDE 20
240 static int cmdline_include_nr = 0;
241 static char *cmdline_include[CMDLINE_INCLUDE];
244 void add_pre_buffer(const char *fmt, ...)
246 va_list args;
247 unsigned int size;
248 struct token *begin, *end;
249 char buffer[4096];
251 va_start(args, fmt);
252 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
253 va_end(args);
254 begin = tokenize_buffer(buffer, size, &end);
255 if (!pre_buffer_begin)
256 pre_buffer_begin = begin;
257 if (pre_buffer_end)
258 pre_buffer_end->next = begin;
259 pre_buffer_end = end;
262 static char **handle_switch_D(char *arg, char **next)
264 const char *name = arg + 1;
265 const char *value = "1";
267 if (!*name || isspace(*name))
268 die("argument to `-D' is missing");
270 for (;;) {
271 char c;
272 c = *++arg;
273 if (!c)
274 break;
275 if (isspace((unsigned char)c) || c == '=') {
276 *arg = '\0';
277 value = arg + 1;
278 break;
281 add_pre_buffer("#define %s %s\n", name, value);
282 return next;
285 static char **handle_switch_E(char *arg, char **next)
287 if (arg[1] == '\0')
288 preprocess_only = 1;
289 return next;
292 static char **handle_switch_I(char *arg, char **next)
294 char *path = arg+1;
296 switch (arg[1]) {
297 case '-':
298 add_pre_buffer("#split_include\n");
299 break;
301 case '\0': /* Plain "-I" */
302 path = *++next;
303 if (!path)
304 die("missing argument for -I option");
305 /* Fall through */
306 default:
307 add_pre_buffer("#add_include \"%s/\"\n", path);
309 return next;
312 static void add_cmdline_include(char *filename)
314 if (cmdline_include_nr >= CMDLINE_INCLUDE)
315 die("too many include files for %s\n", filename);
316 cmdline_include[cmdline_include_nr++] = filename;
319 static char **handle_switch_i(char *arg, char **next)
321 if (*next && !strcmp(arg, "include"))
322 add_cmdline_include(*++next);
323 else if (*next && !strcmp(arg, "imacros"))
324 add_cmdline_include(*++next);
325 else if (*next && !strcmp(arg, "isystem")) {
326 char *path = *++next;
327 if (!path)
328 die("missing argument for -isystem option");
329 add_pre_buffer("#add_isystem \"%s/\"\n", path);
330 } else if (*next && !strcmp(arg, "idirafter")) {
331 char *path = *++next;
332 if (!path)
333 die("missing argument for -idirafter option");
334 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
336 return next;
339 static char **handle_switch_M(char *arg, char **next)
341 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
342 if (!*next)
343 die("missing argument for -%s option", arg);
344 return next + 1;
346 return next;
349 static char **handle_switch_m(char *arg, char **next)
351 if (!strcmp(arg, "m64")) {
352 arch_m64 = 1;
353 } else if (!strcmp(arg, "m32")) {
354 arch_m64 = 0;
355 } else if (!strcmp(arg, "msize-long")) {
356 arch_msize_long = 1;
358 return next;
361 static void handle_arch_m64_finalize(void)
363 if (arch_m64) {
364 bits_in_long = 64;
365 max_int_alignment = 8;
366 bits_in_pointer = 64;
367 pointer_alignment = 8;
368 size_t_ctype = &ulong_ctype;
369 ssize_t_ctype = &long_ctype;
370 #ifdef __x86_64__
371 add_pre_buffer("#weak_define __x86_64__ 1\n");
372 #endif
376 static void handle_arch_msize_long_finalize(void)
378 if (arch_msize_long) {
379 size_t_ctype = &ulong_ctype;
380 ssize_t_ctype = &long_ctype;
384 static void handle_arch_finalize(void)
386 handle_arch_m64_finalize();
387 handle_arch_msize_long_finalize();
391 static char **handle_switch_o(char *arg, char **next)
393 if (!strcmp (arg, "o")) { // "-o foo"
394 if (!*++next)
395 die("argument to '-o' is missing");
397 // else "-ofoo"
399 return next;
402 static const struct warning {
403 const char *name;
404 int *flag;
405 } warnings[] = {
406 { "address-space", &Waddress_space },
407 { "bitwise", &Wbitwise },
408 { "cast-to-as", &Wcast_to_as },
409 { "cast-truncate", &Wcast_truncate },
410 { "context", &Wcontext },
411 { "decl", &Wdecl },
412 { "declaration-after-statement", &Wdeclarationafterstatement },
413 { "default-bitfield-sign", &Wdefault_bitfield_sign },
414 { "designated-init", &Wdesignated_init },
415 { "do-while", &Wdo_while },
416 { "enum-mismatch", &Wenum_mismatch },
417 { "init-cstring", &Winit_cstring },
418 { "non-pointer-null", &Wnon_pointer_null },
419 { "old-initializer", &Wold_initializer },
420 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
421 { "paren-string", &Wparen_string },
422 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
423 { "return-void", &Wreturn_void },
424 { "shadow", &Wshadow },
425 { "transparent-union", &Wtransparent_union },
426 { "typesign", &Wtypesign },
427 { "undef", &Wundef },
428 { "uninitialized", &Wuninitialized },
429 { "vla", &Wvla },
432 enum {
433 WARNING_OFF,
434 WARNING_ON,
435 WARNING_FORCE_OFF
439 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
441 int flag = WARNING_ON;
442 char *p = arg + 1;
443 unsigned i;
445 if (!strcmp(p, "sparse-all")) {
446 for (i = 0; i < n; i++) {
447 if (*warnings[i].flag != WARNING_FORCE_OFF)
448 *warnings[i].flag = WARNING_ON;
452 // Prefixes "no" and "no-" mean to turn warning off.
453 if (p[0] == 'n' && p[1] == 'o') {
454 p += 2;
455 if (p[0] == '-')
456 p++;
457 flag = WARNING_FORCE_OFF;
460 for (i = 0; i < n; i++) {
461 if (!strcmp(p,warnings[i].name)) {
462 *warnings[i].flag = flag;
463 return next;
467 // Unknown.
468 return NULL;
471 static char **handle_switch_W(char *arg, char **next)
473 char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
474 if (ret)
475 return ret;
477 // Unknown.
478 return next;
481 static struct warning debugs[] = {
482 { "entry", &dbg_entry},
483 { "dead", &dbg_dead},
487 static char **handle_switch_v(char *arg, char **next)
489 char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
490 if (ret)
491 return ret;
493 // Unknown.
494 do {
495 verbose++;
496 } while (*++arg == 'v');
497 return next;
501 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
503 unsigned i;
505 for (i = 0; i < n; i++) {
506 if (*warnings[i].flag == WARNING_FORCE_OFF)
507 *warnings[i].flag = WARNING_OFF;
511 static void handle_switch_W_finalize(void)
513 handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
515 /* default Wdeclarationafterstatement based on the C dialect */
516 if (-1 == Wdeclarationafterstatement)
518 switch (standard)
520 case STANDARD_C89:
521 case STANDARD_C94:
522 Wdeclarationafterstatement = 1;
523 break;
525 case STANDARD_C99:
526 case STANDARD_GNU89:
527 case STANDARD_GNU99:
528 Wdeclarationafterstatement = 0;
529 break;
531 default:
532 assert (0);
538 static void handle_switch_v_finalize(void)
540 handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
543 static char **handle_switch_U(char *arg, char **next)
545 const char *name = arg + 1;
546 add_pre_buffer ("#undef %s\n", name);
547 return next;
550 static char **handle_switch_O(char *arg, char **next)
552 int level = 1;
553 if (arg[1] >= '0' && arg[1] <= '9')
554 level = arg[1] - '0';
555 optimize = level;
556 optimize_size = arg[1] == 's';
557 return next;
560 static char **handle_switch_ftabstop(char *arg, char **next)
562 char *end;
563 unsigned long val;
565 if (*arg == '\0')
566 die("error: missing argument to \"-ftabstop=\"");
568 /* we silently ignore silly values */
569 val = strtoul(arg, &end, 10);
570 if (*end == '\0' && 1 <= val && val <= 100)
571 tabstop = val;
573 return next;
576 static char **handle_switch_f(char *arg, char **next)
578 arg++;
580 if (!strncmp(arg, "tabstop=", 8))
581 return handle_switch_ftabstop(arg+8, next);
583 /* handle switches w/ arguments above, boolean and only boolean below */
585 if (!strncmp(arg, "no-", 3)) {
586 arg += 3;
588 /* handle switch here.. */
589 return next;
592 static char **handle_switch_G(char *arg, char **next)
594 if (!strcmp (arg, "G") && *next)
595 return next + 1; // "-G 0"
596 else
597 return next; // "-G0" or (bogus) terminal "-G"
600 static char **handle_switch_a(char *arg, char **next)
602 if (!strcmp (arg, "ansi"))
603 standard = STANDARD_C89;
605 return next;
608 static char **handle_switch_s(char *arg, char **next)
610 if (!strncmp (arg, "std=", 4))
612 arg += 4;
614 if (!strcmp (arg, "c89") ||
615 !strcmp (arg, "iso9899:1990"))
616 standard = STANDARD_C89;
618 else if (!strcmp (arg, "iso9899:199409"))
619 standard = STANDARD_C94;
621 else if (!strcmp (arg, "c99") ||
622 !strcmp (arg, "c9x") ||
623 !strcmp (arg, "iso9899:1999") ||
624 !strcmp (arg, "iso9899:199x"))
625 standard = STANDARD_C99;
627 else if (!strcmp (arg, "gnu89"))
628 standard = STANDARD_GNU89;
630 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
631 standard = STANDARD_GNU99;
633 else
634 die ("Unsupported C dialect");
637 return next;
640 static char **handle_nostdinc(char *arg, char **next)
642 add_pre_buffer("#nostdinc\n");
643 return next;
646 static char **handle_base_dir(char *arg, char **next)
648 gcc_base_dir = *++next;
649 if (!gcc_base_dir)
650 die("missing argument for -gcc-base-dir option");
651 return next;
654 static char **handle_version(char *arg, char **next)
656 printf("%s\n", SPARSE_VERSION);
657 exit(0);
660 struct switches {
661 const char *name;
662 char **(*fn)(char *, char **);
665 static char **handle_long_options(char *arg, char **next)
667 static struct switches cmd[] = {
668 { "version", handle_version },
669 { NULL, NULL }
671 struct switches *s = cmd;
673 while (s->name) {
674 if (!strcmp(s->name, arg))
675 return s->fn(arg, next);
676 s++;
678 return next;
682 static char **handle_switch(char *arg, char **next)
684 static struct switches cmd[] = {
685 { "nostdinc", handle_nostdinc },
686 { "gcc-base-dir", handle_base_dir},
687 { NULL, NULL }
689 struct switches *s;
691 switch (*arg) {
692 case 'D': return handle_switch_D(arg, next);
693 case 'E': return handle_switch_E(arg, next);
694 case 'I': return handle_switch_I(arg, next);
695 case 'i': return handle_switch_i(arg, next);
696 case 'M': return handle_switch_M(arg, next);
697 case 'm': return handle_switch_m(arg, next);
698 case 'o': return handle_switch_o(arg, next);
699 case 'U': return handle_switch_U(arg, next);
700 case 'v': return handle_switch_v(arg, next);
701 case 'W': return handle_switch_W(arg, next);
702 case 'O': return handle_switch_O(arg, next);
703 case 'f': return handle_switch_f(arg, next);
704 case 'G': return handle_switch_G(arg, next);
705 case 'a': return handle_switch_a(arg, next);
706 case 's': return handle_switch_s(arg, next);
707 case '-': return handle_long_options(arg + 1, next);
708 default:
709 break;
712 s = cmd;
713 while (s->name) {
714 if (!strcmp(s->name, arg))
715 return s->fn(arg, next);
716 s++;
720 * Ignore unknown command line options:
721 * they're probably gcc switches
723 return next;
726 void declare_builtin_functions(void)
728 /* Gaah. gcc knows tons of builtin <string.h> functions */
729 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
730 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
731 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
732 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
733 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
734 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
735 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
736 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
737 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
738 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
739 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
740 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
741 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
742 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
743 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
745 /* And bitwise operations.. */
746 add_pre_buffer("extern int __builtin_clz(int);\n");
747 add_pre_buffer("extern int __builtin_clzl(long);\n");
748 add_pre_buffer("extern int __builtin_clzll(long long);\n");
749 add_pre_buffer("extern int __builtin_ctz(int);\n");
750 add_pre_buffer("extern int __builtin_ctzl(long);\n");
751 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
752 add_pre_buffer("extern int __builtin_ffs(int);\n");
753 add_pre_buffer("extern int __builtin_ffsl(long);\n");
754 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
755 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
756 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
757 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
759 /* And byte swaps.. */
760 add_pre_buffer("extern unsigned short __builtin_bswap16(unsigned short);\n");
761 add_pre_buffer("extern unsigned int __builtin_bswap32(unsigned int);\n");
762 add_pre_buffer("extern unsigned long long __builtin_bswap64(unsigned long long);\n");
764 /* And some random ones.. */
765 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
766 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
767 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
768 add_pre_buffer("extern void __builtin_trap(void);\n");
769 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
770 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
771 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
772 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
773 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
774 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
775 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
776 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
777 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
778 add_pre_buffer("extern long __builtin_labs(long);\n");
779 add_pre_buffer("extern double __builtin_fabs(double);\n");
780 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
781 add_pre_buffer("extern void __sync_synchronize();\n");
782 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
784 /* Add Blackfin-specific stuff */
785 add_pre_buffer(
786 "#ifdef __bfin__\n"
787 "extern void __builtin_bfin_csync(void);\n"
788 "extern void __builtin_bfin_ssync(void);\n"
789 "extern int __builtin_bfin_norm_fr1x32(int);\n"
790 "#endif\n"
793 /* And some floating point stuff.. */
794 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
795 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
796 add_pre_buffer("extern int __builtin_isless(float, float);\n");
797 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
798 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
799 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
801 /* And some __FORTIFY_SOURCE ones.. */
802 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
803 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
804 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
805 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
806 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
807 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
808 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
809 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
810 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
811 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
812 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
813 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
814 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
815 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
816 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
819 void create_builtin_stream(void)
821 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
822 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
823 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
825 /* We add compiler headers path here because we have to parse
826 * the arguments to get it, falling back to default. */
827 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
828 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
830 add_pre_buffer("#define __extension__\n");
831 add_pre_buffer("#define __pragma__\n");
833 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
834 // solaris/sparc that is really "unsigned int" and for linux/x86_64
835 // it is "long unsigned int". In either case we can probably
836 // get away with this. We need the #weak_define as cgcc will define
837 // the right __SIZE_TYPE__.
838 if (size_t_ctype == &ulong_ctype)
839 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
840 else
841 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
842 add_pre_buffer("#weak_define __STDC__ 1\n");
844 switch (standard)
846 case STANDARD_C89:
847 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
848 break;
850 case STANDARD_C94:
851 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
852 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
853 break;
855 case STANDARD_C99:
856 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
857 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
858 break;
860 case STANDARD_GNU89:
861 break;
863 case STANDARD_GNU99:
864 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
865 break;
867 default:
868 assert (0);
871 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
872 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
873 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
874 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
875 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
876 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
877 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
878 add_pre_buffer("#define __builtin_va_end(arg)\n");
879 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
880 add_pre_buffer("#define __builtin_va_arg_pack()\n");
882 /* FIXME! We need to do these as special magic macros at expansion time! */
883 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
885 if (optimize)
886 add_pre_buffer("#define __OPTIMIZE__ 1\n");
887 if (optimize_size)
888 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
890 /* GCC defines these for limits.h */
891 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__) "\n");
892 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__) "\n");
893 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__) "\n");
894 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__) "\n");
895 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__) "\n");
896 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__) "\n");
897 add_pre_buffer("#weak_define __SIZEOF_POINTER__ " STRINGIFY(__SIZEOF_POINTER__) "\n");
900 static struct symbol_list *sparse_tokenstream(struct token *token)
902 // Preprocess the stream
903 token = preprocess(token);
905 if (preprocess_only) {
906 while (!eof_token(token)) {
907 int prec = 1;
908 struct token *next = token->next;
909 const char *separator = "";
910 if (next->pos.whitespace)
911 separator = " ";
912 if (next->pos.newline) {
913 separator = "\n\t\t\t\t\t";
914 prec = next->pos.pos;
915 if (prec > 4)
916 prec = 4;
918 printf("%s%.*s", show_token(token), prec, separator);
919 token = next;
921 putchar('\n');
923 return NULL;
926 // Parse the resulting C code
927 while (!eof_token(token))
928 token = external_declaration(token, &translation_unit_used_list);
929 return translation_unit_used_list;
932 static struct symbol_list *sparse_file(const char *filename)
934 int fd;
935 struct token *token;
937 if (strcmp (filename, "-") == 0) {
938 fd = 0;
939 } else {
940 fd = open(filename, O_RDONLY);
941 if (fd < 0)
942 die("No such file: %s", filename);
945 // Tokenize the input stream
946 token = tokenize(filename, fd, NULL, includepath);
947 close(fd);
949 return sparse_tokenstream(token);
953 * This handles the "-include" directive etc: we're in global
954 * scope, and all types/macros etc will affect all the following
955 * files.
957 * NOTE NOTE NOTE! "#undef" of anything in this stage will
958 * affect all subsequent files too, i.e. we can have non-local
959 * behaviour between files!
961 static struct symbol_list *sparse_initial(void)
963 int i;
965 // Prepend any "include" file to the stream.
966 // We're in global scope, it will affect all files!
967 for (i = 0; i < cmdline_include_nr; i++)
968 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]);
970 return sparse_tokenstream(pre_buffer_begin);
973 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
975 char **args;
976 struct symbol_list *list;
978 // Initialize symbol stream first, so that we can add defines etc
979 init_symbols();
981 args = argv;
982 for (;;) {
983 char *arg = *++args;
984 if (!arg)
985 break;
987 if (arg[0] == '-' && arg[1]) {
988 args = handle_switch(arg+1, args);
989 continue;
991 add_ptr_list_notag(filelist, arg);
993 handle_switch_W_finalize();
994 handle_switch_v_finalize();
996 handle_arch_finalize();
998 list = NULL;
999 if (!ptr_list_empty(filelist)) {
1000 // Initialize type system
1001 init_ctype();
1003 create_builtin_stream();
1004 add_pre_buffer("#define __CHECKER__ 1\n");
1005 if (!preprocess_only)
1006 declare_builtin_functions();
1008 list = sparse_initial();
1011 * Protect the initial token allocations, since
1012 * they need to survive all the others
1014 protect_token_alloc();
1016 return list;
1019 struct symbol_list * sparse_keep_tokens(char *filename)
1021 struct symbol_list *res;
1023 /* Clear previous symbol list */
1024 translation_unit_used_list = NULL;
1026 new_file_scope();
1027 res = sparse_file(filename);
1029 /* And return it */
1030 return res;
1034 struct symbol_list * __sparse(char *filename)
1036 struct symbol_list *res;
1038 res = sparse_keep_tokens(filename);
1040 /* Drop the tokens for this file after parsing */
1041 clear_token_alloc();
1043 /* And return it */
1044 return res;
1047 struct symbol_list * sparse(char *filename)
1049 struct symbol_list *res = __sparse(filename);
1051 /* Evaluate the complete symbol list */
1052 evaluate_symbol_list(res);
1054 return res;