user_data2: remove some debug printfs
[smatch.git] / lib.c
blob545c3e41174b39b6cca0d93f5bcddbb04c57d66a
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 Wunknown_attribute = 0;
243 int Wvla = 1;
245 int dbg_entry = 0;
246 int dbg_dead = 0;
248 int preprocess_only;
250 static enum { STANDARD_C89,
251 STANDARD_C94,
252 STANDARD_C99,
253 STANDARD_GNU89,
254 STANDARD_GNU99, } standard = STANDARD_GNU89;
256 #ifdef __x86_64__
257 #define ARCH_M64_DEFAULT 1
258 #else
259 #define ARCH_M64_DEFAULT 0
260 #endif
262 int arch_m64 = ARCH_M64_DEFAULT;
263 int arch_msize_long = 0;
265 #define CMDLINE_INCLUDE 20
266 static int cmdline_include_nr = 0;
267 static char *cmdline_include[CMDLINE_INCLUDE];
270 void add_pre_buffer(const char *fmt, ...)
272 va_list args;
273 unsigned int size;
274 struct token *begin, *end;
275 char buffer[4096];
277 va_start(args, fmt);
278 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
279 va_end(args);
280 begin = tokenize_buffer(buffer, size, &end);
281 if (!pre_buffer_begin)
282 pre_buffer_begin = begin;
283 if (pre_buffer_end)
284 pre_buffer_end->next = begin;
285 pre_buffer_end = end;
288 static char **handle_switch_D(char *arg, char **next)
290 const char *name = arg + 1;
291 const char *value = "1";
293 if (!*name || isspace(*name))
294 die("argument to `-D' is missing");
296 for (;;) {
297 char c;
298 c = *++arg;
299 if (!c)
300 break;
301 if (isspace((unsigned char)c) || c == '=') {
302 *arg = '\0';
303 value = arg + 1;
304 break;
307 add_pre_buffer("#define %s %s\n", name, value);
308 return next;
311 static char **handle_switch_E(char *arg, char **next)
313 if (arg[1] == '\0')
314 preprocess_only = 1;
315 return next;
318 static char **handle_switch_I(char *arg, char **next)
320 char *path = arg+1;
322 switch (arg[1]) {
323 case '-':
324 add_pre_buffer("#split_include\n");
325 break;
327 case '\0': /* Plain "-I" */
328 path = *++next;
329 if (!path)
330 die("missing argument for -I option");
331 /* Fall through */
332 default:
333 add_pre_buffer("#add_include \"%s/\"\n", path);
335 return next;
338 static void add_cmdline_include(char *filename)
340 if (cmdline_include_nr >= CMDLINE_INCLUDE)
341 die("too many include files for %s\n", filename);
342 cmdline_include[cmdline_include_nr++] = filename;
345 static char **handle_switch_i(char *arg, char **next)
347 if (*next && !strcmp(arg, "include"))
348 add_cmdline_include(*++next);
349 else if (*next && !strcmp(arg, "imacros"))
350 add_cmdline_include(*++next);
351 else if (*next && !strcmp(arg, "isystem")) {
352 char *path = *++next;
353 if (!path)
354 die("missing argument for -isystem option");
355 add_pre_buffer("#add_isystem \"%s/\"\n", path);
356 } else if (*next && !strcmp(arg, "idirafter")) {
357 char *path = *++next;
358 if (!path)
359 die("missing argument for -idirafter option");
360 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
362 return next;
365 static char **handle_switch_M(char *arg, char **next)
367 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
368 if (!*next)
369 die("missing argument for -%s option", arg);
370 return next + 1;
372 return next;
375 static char **handle_switch_m(char *arg, char **next)
377 if (!strcmp(arg, "m64")) {
378 arch_m64 = 1;
379 } else if (!strcmp(arg, "m32")) {
380 arch_m64 = 0;
381 } else if (!strcmp(arg, "msize-long")) {
382 arch_msize_long = 1;
384 return next;
387 static void handle_arch_m64_finalize(void)
389 if (arch_m64) {
390 bits_in_long = 64;
391 max_int_alignment = 8;
392 bits_in_pointer = 64;
393 pointer_alignment = 8;
394 size_t_ctype = &ulong_ctype;
395 ssize_t_ctype = &long_ctype;
396 #ifdef __x86_64__
397 add_pre_buffer("#weak_define __x86_64__ 1\n");
398 #endif
402 static void handle_arch_msize_long_finalize(void)
404 if (arch_msize_long) {
405 size_t_ctype = &ulong_ctype;
406 ssize_t_ctype = &long_ctype;
410 static void handle_arch_finalize(void)
412 handle_arch_m64_finalize();
413 handle_arch_msize_long_finalize();
417 static char **handle_switch_o(char *arg, char **next)
419 if (!strcmp (arg, "o")) { // "-o foo"
420 if (!*++next)
421 die("argument to '-o' is missing");
423 // else "-ofoo"
425 return next;
428 static const struct warning {
429 const char *name;
430 int *flag;
431 } warnings[] = {
432 { "address-space", &Waddress_space },
433 { "bitwise", &Wbitwise },
434 { "cast-to-as", &Wcast_to_as },
435 { "cast-truncate", &Wcast_truncate },
436 { "context", &Wcontext },
437 { "decl", &Wdecl },
438 { "declaration-after-statement", &Wdeclarationafterstatement },
439 { "default-bitfield-sign", &Wdefault_bitfield_sign },
440 { "designated-init", &Wdesignated_init },
441 { "do-while", &Wdo_while },
442 { "enum-mismatch", &Wenum_mismatch },
443 { "error", &Werror },
444 { "init-cstring", &Winit_cstring },
445 { "non-pointer-null", &Wnon_pointer_null },
446 { "old-initializer", &Wold_initializer },
447 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
448 { "paren-string", &Wparen_string },
449 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
450 { "return-void", &Wreturn_void },
451 { "shadow", &Wshadow },
452 { "sizeof-bool", &Wsizeof_bool },
453 { "transparent-union", &Wtransparent_union },
454 { "typesign", &Wtypesign },
455 { "undef", &Wundef },
456 { "uninitialized", &Wuninitialized },
457 { "unknown-attribute", &Wunknown_attribute },
458 { "vla", &Wvla },
461 enum {
462 WARNING_OFF,
463 WARNING_ON,
464 WARNING_FORCE_OFF
468 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
470 int flag = WARNING_ON;
471 char *p = arg + 1;
472 unsigned i;
474 if (!strcmp(p, "sparse-all")) {
475 for (i = 0; i < n; i++) {
476 if (*warnings[i].flag != WARNING_FORCE_OFF && warnings[i].flag != &Werror)
477 *warnings[i].flag = WARNING_ON;
481 // Prefixes "no" and "no-" mean to turn warning off.
482 if (p[0] == 'n' && p[1] == 'o') {
483 p += 2;
484 if (p[0] == '-')
485 p++;
486 flag = WARNING_FORCE_OFF;
489 for (i = 0; i < n; i++) {
490 if (!strcmp(p,warnings[i].name)) {
491 *warnings[i].flag = flag;
492 return next;
496 // Unknown.
497 return NULL;
500 static char **handle_switch_W(char *arg, char **next)
502 char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
503 if (ret)
504 return ret;
506 // Unknown.
507 return next;
510 static struct warning debugs[] = {
511 { "entry", &dbg_entry},
512 { "dead", &dbg_dead},
516 static char **handle_switch_v(char *arg, char **next)
518 char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
519 if (ret)
520 return ret;
522 // Unknown.
523 do {
524 verbose++;
525 } while (*++arg == 'v');
526 return next;
530 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
532 unsigned i;
534 for (i = 0; i < n; i++) {
535 if (*warnings[i].flag == WARNING_FORCE_OFF)
536 *warnings[i].flag = WARNING_OFF;
540 static void handle_switch_W_finalize(void)
542 handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
544 /* default Wdeclarationafterstatement based on the C dialect */
545 if (-1 == Wdeclarationafterstatement)
547 switch (standard)
549 case STANDARD_C89:
550 case STANDARD_C94:
551 Wdeclarationafterstatement = 1;
552 break;
554 case STANDARD_C99:
555 case STANDARD_GNU89:
556 case STANDARD_GNU99:
557 Wdeclarationafterstatement = 0;
558 break;
560 default:
561 assert (0);
567 static void handle_switch_v_finalize(void)
569 handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
572 static char **handle_switch_U(char *arg, char **next)
574 const char *name = arg + 1;
575 add_pre_buffer ("#undef %s\n", name);
576 return next;
579 static char **handle_switch_O(char *arg, char **next)
581 int level = 1;
582 if (arg[1] >= '0' && arg[1] <= '9')
583 level = arg[1] - '0';
584 optimize = level;
585 optimize_size = arg[1] == 's';
586 return next;
589 static char **handle_switch_ftabstop(char *arg, char **next)
591 char *end;
592 unsigned long val;
594 if (*arg == '\0')
595 die("error: missing argument to \"-ftabstop=\"");
597 /* we silently ignore silly values */
598 val = strtoul(arg, &end, 10);
599 if (*end == '\0' && 1 <= val && val <= 100)
600 tabstop = val;
602 return next;
605 static int funsigned_char;
606 static void handle_funsigned_char(void)
608 if (funsigned_char) {
609 char_ctype.ctype.modifiers &= ~MOD_SIGNED;
610 char_ctype.ctype.modifiers |= MOD_UNSIGNED;
614 static char **handle_switch_f(char *arg, char **next)
616 arg++;
618 if (!strncmp(arg, "tabstop=", 8))
619 return handle_switch_ftabstop(arg+8, next);
621 if (!strcmp(arg, "unsigned-char")) {
622 funsigned_char = 1;
623 return next;
626 /* handle switches w/ arguments above, boolean and only boolean below */
628 if (!strncmp(arg, "no-", 3)) {
629 arg += 3;
631 /* handle switch here.. */
632 return next;
635 static char **handle_switch_G(char *arg, char **next)
637 if (!strcmp (arg, "G") && *next)
638 return next + 1; // "-G 0"
639 else
640 return next; // "-G0" or (bogus) terminal "-G"
643 static char **handle_switch_a(char *arg, char **next)
645 if (!strcmp (arg, "ansi"))
646 standard = STANDARD_C89;
648 return next;
651 static char **handle_switch_s(char *arg, char **next)
653 if (!strncmp (arg, "std=", 4))
655 arg += 4;
657 if (!strcmp (arg, "c89") ||
658 !strcmp (arg, "iso9899:1990"))
659 standard = STANDARD_C89;
661 else if (!strcmp (arg, "iso9899:199409"))
662 standard = STANDARD_C94;
664 else if (!strcmp (arg, "c99") ||
665 !strcmp (arg, "c9x") ||
666 !strcmp (arg, "iso9899:1999") ||
667 !strcmp (arg, "iso9899:199x"))
668 standard = STANDARD_C99;
670 else if (!strcmp (arg, "gnu89"))
671 standard = STANDARD_GNU89;
673 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
674 standard = STANDARD_GNU99;
676 else
677 die ("Unsupported C dialect");
680 return next;
683 static char **handle_nostdinc(char *arg, char **next)
685 add_pre_buffer("#nostdinc\n");
686 return next;
689 static char **handle_switch_n(char *arg, char **next)
691 if (!strcmp (arg, "nostdinc"))
692 return handle_nostdinc(arg, next);
694 return next;
697 static char **handle_base_dir(char *arg, char **next)
699 gcc_base_dir = *++next;
700 if (!gcc_base_dir)
701 die("missing argument for -gcc-base-dir option");
702 return next;
705 static char **handle_no_lineno(char *arg, char **next)
707 no_lineno = 1;
708 return next;
711 static char **handle_switch_g(char *arg, char **next)
713 if (!strcmp (arg, "gcc-base-dir"))
714 return handle_base_dir(arg, next);
716 return next;
719 static char **handle_version(char *arg, char **next)
721 printf("%s\n", SPARSE_VERSION);
722 exit(0);
725 static char **handle_param(char *arg, char **next)
727 char *value = NULL;
729 /* For now just skip any '--param=*' or '--param *' */
730 if (*arg == '\0') {
731 value = *++next;
732 } else if (isspace(*arg) || *arg == '=') {
733 value = ++arg;
736 if (!value)
737 die("missing argument for --param option");
739 return next;
742 struct switches {
743 const char *name;
744 char **(*fn)(char *, char **);
745 unsigned int prefix:1;
748 static char **handle_long_options(char *arg, char **next)
750 static struct switches cmd[] = {
751 { "param", handle_param, 1 },
752 { "version", handle_version },
753 { "nostdinc", handle_nostdinc },
754 { "gcc-base-dir", handle_base_dir},
755 { "no-lineno", handle_no_lineno},
756 { NULL, NULL }
758 struct switches *s = cmd;
760 while (s->name) {
761 int optlen = strlen(s->name);
762 if (!strncmp(s->name, arg, optlen + !s->prefix))
763 return s->fn(arg + optlen, next);
764 s++;
766 return next;
769 static char **handle_switch(char *arg, char **next)
771 switch (*arg) {
772 case 'a': return handle_switch_a(arg, next);
773 case 'D': return handle_switch_D(arg, next);
774 case 'E': return handle_switch_E(arg, next);
775 case 'f': return handle_switch_f(arg, next);
776 case 'g': return handle_switch_g(arg, next);
777 case 'G': return handle_switch_G(arg, next);
778 case 'I': return handle_switch_I(arg, next);
779 case 'i': return handle_switch_i(arg, next);
780 case 'M': return handle_switch_M(arg, next);
781 case 'm': return handle_switch_m(arg, next);
782 case 'n': return handle_switch_n(arg, next);
783 case 'o': return handle_switch_o(arg, next);
784 case 'O': return handle_switch_O(arg, next);
785 case 's': return handle_switch_s(arg, next);
786 case 'U': return handle_switch_U(arg, next);
787 case 'v': return handle_switch_v(arg, next);
788 case 'W': return handle_switch_W(arg, next);
789 case '-': return handle_long_options(arg + 1, next);
790 default:
791 break;
795 * Ignore unknown command line options:
796 * they're probably gcc switches
798 return next;
801 void declare_builtin_functions(void)
803 /* Gaah. gcc knows tons of builtin <string.h> functions */
804 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
805 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
806 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
807 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
808 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
809 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
810 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
811 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
812 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
813 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
814 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
815 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
816 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
817 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
818 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
820 /* And bitwise operations.. */
821 add_pre_buffer("extern int __builtin_clz(int);\n");
822 add_pre_buffer("extern int __builtin_clzl(long);\n");
823 add_pre_buffer("extern int __builtin_clzll(long long);\n");
824 add_pre_buffer("extern int __builtin_ctz(int);\n");
825 add_pre_buffer("extern int __builtin_ctzl(long);\n");
826 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
827 add_pre_buffer("extern int __builtin_ffs(int);\n");
828 add_pre_buffer("extern int __builtin_ffsl(long);\n");
829 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
830 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
831 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
832 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
834 /* And byte swaps.. */
835 add_pre_buffer("extern unsigned short __builtin_bswap16(unsigned short);\n");
836 add_pre_buffer("extern unsigned int __builtin_bswap32(unsigned int);\n");
837 add_pre_buffer("extern unsigned long long __builtin_bswap64(unsigned long long);\n");
839 /* And atomic memory access functions.. */
840 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
841 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
842 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
843 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
844 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
845 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
846 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
847 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
848 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
849 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
850 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
851 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
852 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
853 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
854 add_pre_buffer("extern void __sync_synchronize();\n");
855 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
856 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
858 /* And some random ones.. */
859 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
860 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
861 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
862 add_pre_buffer("extern void __builtin_trap(void);\n");
863 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
864 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
865 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
866 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
867 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
868 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
869 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
870 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
871 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
872 add_pre_buffer("extern long __builtin_labs(long);\n");
873 add_pre_buffer("extern double __builtin_fabs(double);\n");
874 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
876 /* Add Blackfin-specific stuff */
877 add_pre_buffer(
878 "#ifdef __bfin__\n"
879 "extern void __builtin_bfin_csync(void);\n"
880 "extern void __builtin_bfin_ssync(void);\n"
881 "extern int __builtin_bfin_norm_fr1x32(int);\n"
882 "#endif\n"
885 /* And some floating point stuff.. */
886 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
887 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
888 add_pre_buffer("extern int __builtin_isless(float, float);\n");
889 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
890 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
891 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
893 /* And some __FORTIFY_SOURCE ones.. */
894 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
895 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
896 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
897 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
898 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
899 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
900 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
901 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
902 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
903 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
904 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
905 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
906 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
907 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
908 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
911 void create_builtin_stream(void)
913 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
914 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
915 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
917 /* We add compiler headers path here because we have to parse
918 * the arguments to get it, falling back to default. */
919 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
920 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
922 add_pre_buffer("#define __extension__\n");
923 add_pre_buffer("#define __pragma__\n");
925 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
926 // solaris/sparc that is really "unsigned int" and for linux/x86_64
927 // it is "long unsigned int". In either case we can probably
928 // get away with this. We need the #weak_define as cgcc will define
929 // the right __SIZE_TYPE__.
930 if (size_t_ctype == &ulong_ctype)
931 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
932 else
933 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
934 add_pre_buffer("#weak_define __STDC__ 1\n");
936 switch (standard)
938 case STANDARD_C89:
939 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
940 break;
942 case STANDARD_C94:
943 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
944 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
945 break;
947 case STANDARD_C99:
948 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
949 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
950 break;
952 case STANDARD_GNU89:
953 break;
955 case STANDARD_GNU99:
956 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
957 break;
959 default:
960 assert (0);
963 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
964 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
965 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
966 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
967 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
968 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
969 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
970 add_pre_buffer("#define __builtin_va_end(arg)\n");
971 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
972 add_pre_buffer("#define __builtin_va_arg_pack()\n");
974 /* FIXME! We need to do these as special magic macros at expansion time! */
975 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
977 if (optimize)
978 add_pre_buffer("#define __OPTIMIZE__ 1\n");
979 if (optimize_size)
980 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
982 /* GCC defines these for limits.h */
983 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__) "\n");
984 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__) "\n");
985 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__) "\n");
986 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__) "\n");
987 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__) "\n");
988 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__) "\n");
989 add_pre_buffer("#weak_define __SIZEOF_POINTER__ " STRINGIFY(__SIZEOF_POINTER__) "\n");
990 add_pre_buffer("#weak_define __CHAR_BIT__ " STRINGIFY(__CHAR_BIT__) "\n");
993 static struct symbol_list *sparse_tokenstream(struct token *token)
995 // Preprocess the stream
996 token = preprocess(token);
998 if (preprocess_only) {
999 while (!eof_token(token)) {
1000 int prec = 1;
1001 struct token *next = token->next;
1002 const char *separator = "";
1003 if (next->pos.whitespace)
1004 separator = " ";
1005 if (next->pos.newline) {
1006 separator = "\n\t\t\t\t\t";
1007 prec = next->pos.pos;
1008 if (prec > 4)
1009 prec = 4;
1011 printf("%s%.*s", show_token(token), prec, separator);
1012 token = next;
1014 putchar('\n');
1016 return NULL;
1019 // Parse the resulting C code
1020 while (!eof_token(token))
1021 token = external_declaration(token, &translation_unit_used_list);
1022 return translation_unit_used_list;
1025 static struct symbol_list *sparse_file(const char *filename)
1027 int fd;
1028 struct token *token;
1030 if (strcmp (filename, "-") == 0) {
1031 fd = 0;
1032 } else {
1033 fd = open(filename, O_RDONLY);
1034 if (fd < 0)
1035 die("No such file: %s", filename);
1038 // Tokenize the input stream
1039 token = tokenize(filename, fd, NULL, includepath);
1040 store_all_tokens(token);
1041 close(fd);
1043 return sparse_tokenstream(token);
1047 * This handles the "-include" directive etc: we're in global
1048 * scope, and all types/macros etc will affect all the following
1049 * files.
1051 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1052 * affect all subsequent files too, i.e. we can have non-local
1053 * behaviour between files!
1055 static struct symbol_list *sparse_initial(void)
1057 int i;
1059 // Prepend any "include" file to the stream.
1060 // We're in global scope, it will affect all files!
1061 for (i = 0; i < cmdline_include_nr; i++)
1062 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]);
1064 return sparse_tokenstream(pre_buffer_begin);
1067 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
1069 char **args;
1070 struct symbol_list *list;
1072 // Initialize symbol stream first, so that we can add defines etc
1073 init_symbols();
1074 init_include_path();
1076 args = argv;
1077 for (;;) {
1078 char *arg = *++args;
1079 if (!arg)
1080 break;
1082 if (arg[0] == '-' && arg[1]) {
1083 args = handle_switch(arg+1, args);
1084 continue;
1086 add_ptr_list_notag(filelist, arg);
1088 handle_switch_W_finalize();
1089 handle_switch_v_finalize();
1091 handle_arch_finalize();
1093 list = NULL;
1094 if (!ptr_list_empty(filelist)) {
1095 // Initialize type system
1096 init_ctype();
1097 handle_funsigned_char();
1099 create_builtin_stream();
1100 add_pre_buffer("#define __CHECKER__ 1\n");
1101 if (!preprocess_only)
1102 declare_builtin_functions();
1104 list = sparse_initial();
1107 * Protect the initial token allocations, since
1108 * they need to survive all the others
1110 protect_token_alloc();
1112 return list;
1115 struct symbol_list * sparse_keep_tokens(char *filename)
1117 struct symbol_list *res;
1119 /* Clear previous symbol list */
1120 translation_unit_used_list = NULL;
1122 new_file_scope();
1123 res = sparse_file(filename);
1125 /* And return it */
1126 return res;
1130 struct symbol_list * __sparse(char *filename)
1132 struct symbol_list *res;
1134 res = sparse_keep_tokens(filename);
1136 /* Drop the tokens for this file after parsing */
1137 clear_token_alloc();
1139 /* And return it */
1140 return res;
1143 struct symbol_list * sparse(char *filename)
1145 struct symbol_list *res = __sparse(filename);
1147 /* Evaluate the complete symbol list */
1148 evaluate_symbol_list(res);
1150 return res;