db: export get_static_filter()
[smatch.git] / lib.c
blob0f2f9d7d1745d45657ce7dec9d5c3b016bf359b9
1 /*
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
23 * THE SOFTWARE.
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <stdarg.h>
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <assert.h>
35 #include <sys/types.h>
37 #include "lib.h"
38 #include "allocate.h"
39 #include "token.h"
40 #include "parse.h"
41 #include "symbol.h"
42 #include "expression.h"
43 #include "scope.h"
44 #include "linearize.h"
45 #include "target.h"
46 #include "version.h"
48 int verbose, optimize, optimize_size, preprocessing;
49 int die_if_error = 0;
51 #ifndef __GNUC__
52 # define __GNUC__ 2
53 # define __GNUC_MINOR__ 95
54 # define __GNUC_PATCHLEVEL__ 0
55 #endif
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;
63 struct token *skip_to(struct token *token, int op)
65 while (!match_op(token, op) && !eof_token(token))
66 token = token->next;
67 return token;
70 struct token *expect(struct token *token, int op, const char *where)
72 if (!match_op(token, op)) {
73 static struct token bad_token;
74 if (token != &bad_token) {
75 bad_token.next = token;
76 sparse_error(token->pos, "Expected %s %s", show_special(op), where);
77 sparse_error(token->pos, "got %s", show_token(token));
79 if (op == ';')
80 return skip_to(token, op);
81 return &bad_token;
83 return token->next;
86 unsigned int hexval(unsigned int c)
88 int retval = 256;
89 switch (c) {
90 case '0'...'9':
91 retval = c - '0';
92 break;
93 case 'a'...'f':
94 retval = c - 'a' + 10;
95 break;
96 case 'A'...'F':
97 retval = c - 'A' + 10;
98 break;
100 return retval;
103 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
105 static char buffer[512];
106 const char *name;
108 vsprintf(buffer, fmt, args);
109 name = stream_name(pos.stream);
111 fprintf(stderr, "%s:%d:%d: %s%s\n",
112 name, pos.line, pos.pos, type, buffer);
115 static int max_warnings = 100;
116 static int show_info = 1;
118 void info(struct position pos, const char * fmt, ...)
120 va_list args;
122 if (!show_info)
123 return;
124 va_start(args, fmt);
125 do_warn("", pos, fmt, args);
126 va_end(args);
129 static void do_error(struct position pos, const char * fmt, va_list args)
131 static int errors = 0;
132 die_if_error = 1;
133 show_info = 1;
134 /* Shut up warnings after an error */
135 max_warnings = 0;
136 if (errors > 100) {
137 static int once = 0;
138 show_info = 0;
139 if (once)
140 return;
141 fmt = "too many errors";
142 once = 1;
145 do_warn("error: ", pos, fmt, args);
146 errors++;
149 void warning(struct position pos, const char * fmt, ...)
151 va_list args;
153 if (Werror) {
154 va_start(args, fmt);
155 do_error(pos, fmt, args);
156 va_end(args);
157 return;
160 if (!max_warnings) {
161 show_info = 0;
162 return;
165 if (!--max_warnings) {
166 show_info = 0;
167 fmt = "too many warnings";
170 va_start(args, fmt);
171 do_warn("warning: ", pos, fmt, args);
172 va_end(args);
175 void sparse_error(struct position pos, const char * fmt, ...)
177 va_list args;
178 va_start(args, fmt);
179 do_error(pos, fmt, args);
180 va_end(args);
183 void expression_error(struct expression *expr, const char *fmt, ...)
185 va_list args;
186 va_start(args, fmt);
187 do_error(expr->pos, fmt, args);
188 va_end(args);
189 expr->ctype = &bad_ctype;
192 void error_die(struct position pos, const char * fmt, ...)
194 va_list args;
195 va_start(args, fmt);
196 do_warn("error: ", pos, fmt, args);
197 va_end(args);
198 exit(1);
201 void die(const char *fmt, ...)
203 va_list args;
204 static char buffer[512];
206 va_start(args, fmt);
207 vsnprintf(buffer, sizeof(buffer), fmt, args);
208 va_end(args);
210 fprintf(stderr, "%s\n", buffer);
211 exit(1);
214 static struct token *pre_buffer_begin = NULL;
215 static struct token *pre_buffer_end = NULL;
217 int Waddress_space = 1;
218 int Wbitwise = 0;
219 int Wcast_to_as = 0;
220 int Wcast_truncate = 1;
221 int Wcontext = 1;
222 int Wdecl = 1;
223 int Wdeclarationafterstatement = -1;
224 int Wdefault_bitfield_sign = 0;
225 int Wdesignated_init = 1;
226 int Wdo_while = 0;
227 int Winit_cstring = 0;
228 int Wenum_mismatch = 1;
229 int Werror = 0;
230 int Wnon_pointer_null = 1;
231 int Wold_initializer = 1;
232 int Wone_bit_signed_bitfield = 1;
233 int Wparen_string = 0;
234 int Wptr_subtraction_blows = 0;
235 int Wreturn_void = 0;
236 int Wshadow = 0;
237 int Wsizeof_bool = 0;
238 int Wtransparent_union = 0;
239 int Wtypesign = 0;
240 int Wundef = 0;
241 int Wuninitialized = 1;
242 int Wvla = 1;
244 int dbg_entry = 0;
245 int dbg_dead = 0;
247 int preprocess_only;
249 static enum { STANDARD_C89,
250 STANDARD_C94,
251 STANDARD_C99,
252 STANDARD_GNU89,
253 STANDARD_GNU99, } standard = STANDARD_GNU89;
255 #ifdef __x86_64__
256 #define ARCH_M64_DEFAULT 1
257 #else
258 #define ARCH_M64_DEFAULT 0
259 #endif
261 int arch_m64 = ARCH_M64_DEFAULT;
262 int arch_msize_long = 0;
264 #define CMDLINE_INCLUDE 20
265 static int cmdline_include_nr = 0;
266 static char *cmdline_include[CMDLINE_INCLUDE];
269 void add_pre_buffer(const char *fmt, ...)
271 va_list args;
272 unsigned int size;
273 struct token *begin, *end;
274 char buffer[4096];
276 va_start(args, fmt);
277 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
278 va_end(args);
279 begin = tokenize_buffer(buffer, size, &end);
280 if (!pre_buffer_begin)
281 pre_buffer_begin = begin;
282 if (pre_buffer_end)
283 pre_buffer_end->next = begin;
284 pre_buffer_end = end;
287 static char **handle_switch_D(char *arg, char **next)
289 const char *name = arg + 1;
290 const char *value = "1";
292 if (!*name || isspace(*name))
293 die("argument to `-D' is missing");
295 for (;;) {
296 char c;
297 c = *++arg;
298 if (!c)
299 break;
300 if (isspace((unsigned char)c) || c == '=') {
301 *arg = '\0';
302 value = arg + 1;
303 break;
306 add_pre_buffer("#define %s %s\n", name, value);
307 return next;
310 static char **handle_switch_E(char *arg, char **next)
312 if (arg[1] == '\0')
313 preprocess_only = 1;
314 return next;
317 static char **handle_switch_I(char *arg, char **next)
319 char *path = arg+1;
321 switch (arg[1]) {
322 case '-':
323 add_pre_buffer("#split_include\n");
324 break;
326 case '\0': /* Plain "-I" */
327 path = *++next;
328 if (!path)
329 die("missing argument for -I option");
330 /* Fall through */
331 default:
332 add_pre_buffer("#add_include \"%s/\"\n", path);
334 return next;
337 static void add_cmdline_include(char *filename)
339 if (cmdline_include_nr >= CMDLINE_INCLUDE)
340 die("too many include files for %s\n", filename);
341 cmdline_include[cmdline_include_nr++] = filename;
344 static char **handle_switch_i(char *arg, char **next)
346 if (*next && !strcmp(arg, "include"))
347 add_cmdline_include(*++next);
348 else if (*next && !strcmp(arg, "imacros"))
349 add_cmdline_include(*++next);
350 else if (*next && !strcmp(arg, "isystem")) {
351 char *path = *++next;
352 if (!path)
353 die("missing argument for -isystem option");
354 add_pre_buffer("#add_isystem \"%s/\"\n", path);
355 } else if (*next && !strcmp(arg, "idirafter")) {
356 char *path = *++next;
357 if (!path)
358 die("missing argument for -idirafter option");
359 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
361 return next;
364 static char **handle_switch_M(char *arg, char **next)
366 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
367 if (!*next)
368 die("missing argument for -%s option", arg);
369 return next + 1;
371 return next;
374 static char **handle_switch_m(char *arg, char **next)
376 if (!strcmp(arg, "m64")) {
377 arch_m64 = 1;
378 } else if (!strcmp(arg, "m32")) {
379 arch_m64 = 0;
380 } else if (!strcmp(arg, "msize-long")) {
381 arch_msize_long = 1;
383 return next;
386 static void handle_arch_m64_finalize(void)
388 if (arch_m64) {
389 bits_in_long = 64;
390 max_int_alignment = 8;
391 bits_in_pointer = 64;
392 pointer_alignment = 8;
393 size_t_ctype = &ulong_ctype;
394 ssize_t_ctype = &long_ctype;
395 #ifdef __x86_64__
396 add_pre_buffer("#weak_define __x86_64__ 1\n");
397 #endif
401 static void handle_arch_msize_long_finalize(void)
403 if (arch_msize_long) {
404 size_t_ctype = &ulong_ctype;
405 ssize_t_ctype = &long_ctype;
409 static void handle_arch_finalize(void)
411 handle_arch_m64_finalize();
412 handle_arch_msize_long_finalize();
416 static char **handle_switch_o(char *arg, char **next)
418 if (!strcmp (arg, "o")) { // "-o foo"
419 if (!*++next)
420 die("argument to '-o' is missing");
422 // else "-ofoo"
424 return next;
427 static const struct warning {
428 const char *name;
429 int *flag;
430 } warnings[] = {
431 { "address-space", &Waddress_space },
432 { "bitwise", &Wbitwise },
433 { "cast-to-as", &Wcast_to_as },
434 { "cast-truncate", &Wcast_truncate },
435 { "context", &Wcontext },
436 { "decl", &Wdecl },
437 { "declaration-after-statement", &Wdeclarationafterstatement },
438 { "default-bitfield-sign", &Wdefault_bitfield_sign },
439 { "designated-init", &Wdesignated_init },
440 { "do-while", &Wdo_while },
441 { "enum-mismatch", &Wenum_mismatch },
442 { "error", &Werror },
443 { "init-cstring", &Winit_cstring },
444 { "non-pointer-null", &Wnon_pointer_null },
445 { "old-initializer", &Wold_initializer },
446 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
447 { "paren-string", &Wparen_string },
448 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
449 { "return-void", &Wreturn_void },
450 { "shadow", &Wshadow },
451 { "sizeof-bool", &Wsizeof_bool },
452 { "transparent-union", &Wtransparent_union },
453 { "typesign", &Wtypesign },
454 { "undef", &Wundef },
455 { "uninitialized", &Wuninitialized },
456 { "vla", &Wvla },
459 enum {
460 WARNING_OFF,
461 WARNING_ON,
462 WARNING_FORCE_OFF
466 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
468 int flag = WARNING_ON;
469 char *p = arg + 1;
470 unsigned i;
472 if (!strcmp(p, "sparse-all")) {
473 for (i = 0; i < n; i++) {
474 if (*warnings[i].flag != WARNING_FORCE_OFF && warnings[i].flag != &Werror)
475 *warnings[i].flag = WARNING_ON;
479 // Prefixes "no" and "no-" mean to turn warning off.
480 if (p[0] == 'n' && p[1] == 'o') {
481 p += 2;
482 if (p[0] == '-')
483 p++;
484 flag = WARNING_FORCE_OFF;
487 for (i = 0; i < n; i++) {
488 if (!strcmp(p,warnings[i].name)) {
489 *warnings[i].flag = flag;
490 return next;
494 // Unknown.
495 return NULL;
498 static char **handle_switch_W(char *arg, char **next)
500 char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
501 if (ret)
502 return ret;
504 // Unknown.
505 return next;
508 static struct warning debugs[] = {
509 { "entry", &dbg_entry},
510 { "dead", &dbg_dead},
514 static char **handle_switch_v(char *arg, char **next)
516 char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
517 if (ret)
518 return ret;
520 // Unknown.
521 do {
522 verbose++;
523 } while (*++arg == 'v');
524 return next;
528 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
530 unsigned i;
532 for (i = 0; i < n; i++) {
533 if (*warnings[i].flag == WARNING_FORCE_OFF)
534 *warnings[i].flag = WARNING_OFF;
538 static void handle_switch_W_finalize(void)
540 handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
542 /* default Wdeclarationafterstatement based on the C dialect */
543 if (-1 == Wdeclarationafterstatement)
545 switch (standard)
547 case STANDARD_C89:
548 case STANDARD_C94:
549 Wdeclarationafterstatement = 1;
550 break;
552 case STANDARD_C99:
553 case STANDARD_GNU89:
554 case STANDARD_GNU99:
555 Wdeclarationafterstatement = 0;
556 break;
558 default:
559 assert (0);
565 static void handle_switch_v_finalize(void)
567 handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
570 static char **handle_switch_U(char *arg, char **next)
572 const char *name = arg + 1;
573 add_pre_buffer ("#undef %s\n", name);
574 return next;
577 static char **handle_switch_O(char *arg, char **next)
579 int level = 1;
580 if (arg[1] >= '0' && arg[1] <= '9')
581 level = arg[1] - '0';
582 optimize = level;
583 optimize_size = arg[1] == 's';
584 return next;
587 static char **handle_switch_ftabstop(char *arg, char **next)
589 char *end;
590 unsigned long val;
592 if (*arg == '\0')
593 die("error: missing argument to \"-ftabstop=\"");
595 /* we silently ignore silly values */
596 val = strtoul(arg, &end, 10);
597 if (*end == '\0' && 1 <= val && val <= 100)
598 tabstop = val;
600 return next;
603 static char **handle_switch_f(char *arg, char **next)
605 arg++;
607 if (!strncmp(arg, "tabstop=", 8))
608 return handle_switch_ftabstop(arg+8, next);
610 /* handle switches w/ arguments above, boolean and only boolean below */
612 if (!strncmp(arg, "no-", 3)) {
613 arg += 3;
615 /* handle switch here.. */
616 return next;
619 static char **handle_switch_G(char *arg, char **next)
621 if (!strcmp (arg, "G") && *next)
622 return next + 1; // "-G 0"
623 else
624 return next; // "-G0" or (bogus) terminal "-G"
627 static char **handle_switch_a(char *arg, char **next)
629 if (!strcmp (arg, "ansi"))
630 standard = STANDARD_C89;
632 return next;
635 static char **handle_switch_s(char *arg, char **next)
637 if (!strncmp (arg, "std=", 4))
639 arg += 4;
641 if (!strcmp (arg, "c89") ||
642 !strcmp (arg, "iso9899:1990"))
643 standard = STANDARD_C89;
645 else if (!strcmp (arg, "iso9899:199409"))
646 standard = STANDARD_C94;
648 else if (!strcmp (arg, "c99") ||
649 !strcmp (arg, "c9x") ||
650 !strcmp (arg, "iso9899:1999") ||
651 !strcmp (arg, "iso9899:199x"))
652 standard = STANDARD_C99;
654 else if (!strcmp (arg, "gnu89"))
655 standard = STANDARD_GNU89;
657 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
658 standard = STANDARD_GNU99;
660 else
661 die ("Unsupported C dialect");
664 return next;
667 static char **handle_nostdinc(char *arg, char **next)
669 add_pre_buffer("#nostdinc\n");
670 return next;
673 static char **handle_switch_n(char *arg, char **next)
675 if (!strcmp (arg, "nostdinc"))
676 return handle_nostdinc(arg, next);
678 return next;
681 static char **handle_base_dir(char *arg, char **next)
683 gcc_base_dir = *++next;
684 if (!gcc_base_dir)
685 die("missing argument for -gcc-base-dir option");
686 return next;
689 static char **handle_no_lineno(char *arg, char **next)
691 no_lineno = 1;
692 return next;
695 static char **handle_switch_g(char *arg, char **next)
697 if (!strcmp (arg, "gcc-base-dir"))
698 return handle_base_dir(arg, next);
700 return next;
703 static char **handle_version(char *arg, char **next)
705 printf("%s\n", SPARSE_VERSION);
706 exit(0);
709 static char **handle_param(char *arg, char **next)
711 char *value = NULL;
713 /* For now just skip any '--param=*' or '--param *' */
714 if (*arg == '\0') {
715 value = *++next;
716 } else if (isspace(*arg) || *arg == '=') {
717 value = ++arg;
720 if (!value)
721 die("missing argument for --param option");
723 return next;
726 struct switches {
727 const char *name;
728 char **(*fn)(char *, char **);
729 unsigned int prefix:1;
732 static char **handle_long_options(char *arg, char **next)
734 static struct switches cmd[] = {
735 { "param", handle_param, 1 },
736 { "version", handle_version },
737 { "nostdinc", handle_nostdinc },
738 { "gcc-base-dir", handle_base_dir},
739 { "no-lineno", handle_no_lineno},
740 { NULL, NULL }
742 struct switches *s = cmd;
744 while (s->name) {
745 int optlen = strlen(s->name);
746 if (!strncmp(s->name, arg, optlen + !s->prefix))
747 return s->fn(arg + optlen, next);
748 s++;
750 return next;
753 static char **handle_switch(char *arg, char **next)
755 switch (*arg) {
756 case 'a': return handle_switch_a(arg, next);
757 case 'D': return handle_switch_D(arg, next);
758 case 'E': return handle_switch_E(arg, next);
759 case 'f': return handle_switch_f(arg, next);
760 case 'g': return handle_switch_g(arg, next);
761 case 'G': return handle_switch_G(arg, next);
762 case 'I': return handle_switch_I(arg, next);
763 case 'i': return handle_switch_i(arg, next);
764 case 'M': return handle_switch_M(arg, next);
765 case 'm': return handle_switch_m(arg, next);
766 case 'n': return handle_switch_n(arg, next);
767 case 'o': return handle_switch_o(arg, next);
768 case 'O': return handle_switch_O(arg, next);
769 case 's': return handle_switch_s(arg, next);
770 case 'U': return handle_switch_U(arg, next);
771 case 'v': return handle_switch_v(arg, next);
772 case 'W': return handle_switch_W(arg, next);
773 case '-': return handle_long_options(arg + 1, next);
774 default:
775 break;
779 * Ignore unknown command line options:
780 * they're probably gcc switches
782 return next;
785 void declare_builtin_functions(void)
787 /* Gaah. gcc knows tons of builtin <string.h> functions */
788 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
789 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
790 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
791 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
792 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
793 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
794 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
795 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
796 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
797 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
798 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
799 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
800 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
801 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
802 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
804 /* And bitwise operations.. */
805 add_pre_buffer("extern int __builtin_clz(int);\n");
806 add_pre_buffer("extern int __builtin_clzl(long);\n");
807 add_pre_buffer("extern int __builtin_clzll(long long);\n");
808 add_pre_buffer("extern int __builtin_ctz(int);\n");
809 add_pre_buffer("extern int __builtin_ctzl(long);\n");
810 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
811 add_pre_buffer("extern int __builtin_ffs(int);\n");
812 add_pre_buffer("extern int __builtin_ffsl(long);\n");
813 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
814 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
815 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
816 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
818 /* And byte swaps.. */
819 add_pre_buffer("extern unsigned short __builtin_bswap16(unsigned short);\n");
820 add_pre_buffer("extern unsigned int __builtin_bswap32(unsigned int);\n");
821 add_pre_buffer("extern unsigned long long __builtin_bswap64(unsigned long long);\n");
823 /* And atomic memory access functions.. */
824 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
825 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
826 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
827 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
828 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
829 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
830 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
831 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
832 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
833 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
834 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
835 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
836 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
837 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
838 add_pre_buffer("extern void __sync_synchronize();\n");
839 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
840 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
842 /* And some random ones.. */
843 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
844 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
845 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
846 add_pre_buffer("extern void __builtin_trap(void);\n");
847 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
848 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
849 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
850 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
851 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
852 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
853 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
854 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
855 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
856 add_pre_buffer("extern long __builtin_labs(long);\n");
857 add_pre_buffer("extern double __builtin_fabs(double);\n");
858 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
860 /* Add Blackfin-specific stuff */
861 add_pre_buffer(
862 "#ifdef __bfin__\n"
863 "extern void __builtin_bfin_csync(void);\n"
864 "extern void __builtin_bfin_ssync(void);\n"
865 "extern int __builtin_bfin_norm_fr1x32(int);\n"
866 "#endif\n"
869 /* And some floating point stuff.. */
870 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
871 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
872 add_pre_buffer("extern int __builtin_isless(float, float);\n");
873 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
874 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
875 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
877 /* And some __FORTIFY_SOURCE ones.. */
878 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
879 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
880 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
881 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
882 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
883 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
884 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
885 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
886 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
887 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
888 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
889 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
890 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
891 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
892 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
895 void create_builtin_stream(void)
897 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
898 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
899 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
901 /* We add compiler headers path here because we have to parse
902 * the arguments to get it, falling back to default. */
903 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
904 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
906 add_pre_buffer("#define __extension__\n");
907 add_pre_buffer("#define __pragma__\n");
909 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
910 // solaris/sparc that is really "unsigned int" and for linux/x86_64
911 // it is "long unsigned int". In either case we can probably
912 // get away with this. We need the #weak_define as cgcc will define
913 // the right __SIZE_TYPE__.
914 if (size_t_ctype == &ulong_ctype)
915 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
916 else
917 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
918 add_pre_buffer("#weak_define __STDC__ 1\n");
920 switch (standard)
922 case STANDARD_C89:
923 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
924 break;
926 case STANDARD_C94:
927 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
928 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
929 break;
931 case STANDARD_C99:
932 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
933 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
934 break;
936 case STANDARD_GNU89:
937 break;
939 case STANDARD_GNU99:
940 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
941 break;
943 default:
944 assert (0);
947 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
948 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
949 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
950 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
951 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
952 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
953 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
954 add_pre_buffer("#define __builtin_va_end(arg)\n");
955 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
956 add_pre_buffer("#define __builtin_va_arg_pack()\n");
958 /* FIXME! We need to do these as special magic macros at expansion time! */
959 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
961 if (optimize)
962 add_pre_buffer("#define __OPTIMIZE__ 1\n");
963 if (optimize_size)
964 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
966 /* GCC defines these for limits.h */
967 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__) "\n");
968 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__) "\n");
969 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__) "\n");
970 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__) "\n");
971 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__) "\n");
972 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__) "\n");
973 add_pre_buffer("#weak_define __SIZEOF_POINTER__ " STRINGIFY(__SIZEOF_POINTER__) "\n");
974 add_pre_buffer("#weak_define __CHAR_BIT__ " STRINGIFY(__CHAR_BIT__) "\n");
977 static struct symbol_list *sparse_tokenstream(struct token *token)
979 // Preprocess the stream
980 token = preprocess(token);
982 if (preprocess_only) {
983 while (!eof_token(token)) {
984 int prec = 1;
985 struct token *next = token->next;
986 const char *separator = "";
987 if (next->pos.whitespace)
988 separator = " ";
989 if (next->pos.newline) {
990 separator = "\n\t\t\t\t\t";
991 prec = next->pos.pos;
992 if (prec > 4)
993 prec = 4;
995 printf("%s%.*s", show_token(token), prec, separator);
996 token = next;
998 putchar('\n');
1000 return NULL;
1003 // Parse the resulting C code
1004 while (!eof_token(token))
1005 token = external_declaration(token, &translation_unit_used_list);
1006 return translation_unit_used_list;
1009 static struct symbol_list *sparse_file(const char *filename)
1011 int fd;
1012 struct token *token;
1014 if (strcmp (filename, "-") == 0) {
1015 fd = 0;
1016 } else {
1017 fd = open(filename, O_RDONLY);
1018 if (fd < 0)
1019 die("No such file: %s", filename);
1022 // Tokenize the input stream
1023 token = tokenize(filename, fd, NULL, includepath);
1024 store_all_tokens(token);
1025 close(fd);
1027 return sparse_tokenstream(token);
1031 * This handles the "-include" directive etc: we're in global
1032 * scope, and all types/macros etc will affect all the following
1033 * files.
1035 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1036 * affect all subsequent files too, i.e. we can have non-local
1037 * behaviour between files!
1039 static struct symbol_list *sparse_initial(void)
1041 int i;
1043 // Prepend any "include" file to the stream.
1044 // We're in global scope, it will affect all files!
1045 for (i = 0; i < cmdline_include_nr; i++)
1046 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]);
1048 return sparse_tokenstream(pre_buffer_begin);
1051 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
1053 char **args;
1054 struct symbol_list *list;
1056 // Initialize symbol stream first, so that we can add defines etc
1057 init_symbols();
1059 args = argv;
1060 for (;;) {
1061 char *arg = *++args;
1062 if (!arg)
1063 break;
1065 if (arg[0] == '-' && arg[1]) {
1066 args = handle_switch(arg+1, args);
1067 continue;
1069 add_ptr_list_notag(filelist, arg);
1071 handle_switch_W_finalize();
1072 handle_switch_v_finalize();
1074 handle_arch_finalize();
1076 list = NULL;
1077 if (!ptr_list_empty(filelist)) {
1078 // Initialize type system
1079 init_ctype();
1081 create_builtin_stream();
1082 add_pre_buffer("#define __CHECKER__ 1\n");
1083 if (!preprocess_only)
1084 declare_builtin_functions();
1086 list = sparse_initial();
1089 * Protect the initial token allocations, since
1090 * they need to survive all the others
1092 protect_token_alloc();
1094 return list;
1097 struct symbol_list * sparse_keep_tokens(char *filename)
1099 struct symbol_list *res;
1101 /* Clear previous symbol list */
1102 translation_unit_used_list = NULL;
1104 new_file_scope();
1105 res = sparse_file(filename);
1107 /* And return it */
1108 return res;
1112 struct symbol_list * __sparse(char *filename)
1114 struct symbol_list *res;
1116 res = sparse_keep_tokens(filename);
1118 /* Drop the tokens for this file after parsing */
1119 clear_token_alloc();
1121 /* And return it */
1122 return res;
1125 struct symbol_list * sparse(char *filename)
1127 struct symbol_list *res = __sparse(filename);
1129 /* Evaluate the complete symbol list */
1130 evaluate_symbol_list(res);
1132 return res;