show_errs.sh: quote some strings properly
[smatch.git] / lib.c
blob4438147da4b210bc6923c5a66f294f81bfcae469
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 void warning(struct position pos, const char * fmt, ...)
131 va_list args;
133 if (!max_warnings) {
134 show_info = 0;
135 return;
138 if (!--max_warnings) {
139 show_info = 0;
140 fmt = "too many warnings";
143 va_start(args, fmt);
144 do_warn("warning: ", pos, fmt, args);
145 va_end(args);
148 static void do_error(struct position pos, const char * fmt, va_list args)
150 static int errors = 0;
151 die_if_error = 1;
152 show_info = 1;
153 /* Shut up warnings after an error */
154 max_warnings = 0;
155 if (errors > 100) {
156 static int once = 0;
157 show_info = 0;
158 if (once)
159 return;
160 fmt = "too many errors";
161 once = 1;
164 do_warn("error: ", pos, fmt, args);
165 errors++;
168 void sparse_error(struct position pos, const char * fmt, ...)
170 va_list args;
171 va_start(args, fmt);
172 do_error(pos, fmt, args);
173 va_end(args);
176 void expression_error(struct expression *expr, const char *fmt, ...)
178 va_list args;
179 va_start(args, fmt);
180 do_error(expr->pos, fmt, args);
181 va_end(args);
182 expr->ctype = &bad_ctype;
185 void error_die(struct position pos, const char * fmt, ...)
187 va_list args;
188 va_start(args, fmt);
189 do_warn("error: ", pos, fmt, args);
190 va_end(args);
191 exit(1);
194 void die(const char *fmt, ...)
196 va_list args;
197 static char buffer[512];
199 va_start(args, fmt);
200 vsnprintf(buffer, sizeof(buffer), fmt, args);
201 va_end(args);
203 fprintf(stderr, "%s\n", buffer);
204 exit(1);
207 static struct token *pre_buffer_begin = NULL;
208 static struct token *pre_buffer_end = NULL;
210 int Waddress_space = 1;
211 int Wbitwise = 0;
212 int Wcast_to_as = 0;
213 int Wcast_truncate = 1;
214 int Wcontext = 1;
215 int Wdecl = 1;
216 int Wdeclarationafterstatement = -1;
217 int Wdefault_bitfield_sign = 0;
218 int Wdesignated_init = 1;
219 int Wdo_while = 0;
220 int Winit_cstring = 0;
221 int Wenum_mismatch = 1;
222 int Wnon_pointer_null = 1;
223 int Wold_initializer = 1;
224 int Wone_bit_signed_bitfield = 1;
225 int Wparen_string = 0;
226 int Wptr_subtraction_blows = 0;
227 int Wreturn_void = 0;
228 int Wshadow = 0;
229 int Wsizeof_bool = 0;
230 int Wtransparent_union = 0;
231 int Wtypesign = 0;
232 int Wundef = 0;
233 int Wuninitialized = 1;
234 int Wvla = 1;
236 int dbg_entry = 0;
237 int dbg_dead = 0;
239 int preprocess_only;
241 static enum { STANDARD_C89,
242 STANDARD_C94,
243 STANDARD_C99,
244 STANDARD_GNU89,
245 STANDARD_GNU99, } standard = STANDARD_GNU89;
247 #ifdef __x86_64__
248 #define ARCH_M64_DEFAULT 1
249 #else
250 #define ARCH_M64_DEFAULT 0
251 #endif
253 int arch_m64 = ARCH_M64_DEFAULT;
254 int arch_msize_long = 0;
256 #define CMDLINE_INCLUDE 20
257 static int cmdline_include_nr = 0;
258 static char *cmdline_include[CMDLINE_INCLUDE];
261 void add_pre_buffer(const char *fmt, ...)
263 va_list args;
264 unsigned int size;
265 struct token *begin, *end;
266 char buffer[4096];
268 va_start(args, fmt);
269 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
270 va_end(args);
271 begin = tokenize_buffer(buffer, size, &end);
272 if (!pre_buffer_begin)
273 pre_buffer_begin = begin;
274 if (pre_buffer_end)
275 pre_buffer_end->next = begin;
276 pre_buffer_end = end;
279 static char **handle_switch_D(char *arg, char **next)
281 const char *name = arg + 1;
282 const char *value = "1";
284 if (!*name || isspace(*name))
285 die("argument to `-D' is missing");
287 for (;;) {
288 char c;
289 c = *++arg;
290 if (!c)
291 break;
292 if (isspace((unsigned char)c) || c == '=') {
293 *arg = '\0';
294 value = arg + 1;
295 break;
298 add_pre_buffer("#define %s %s\n", name, value);
299 return next;
302 static char **handle_switch_E(char *arg, char **next)
304 if (arg[1] == '\0')
305 preprocess_only = 1;
306 return next;
309 static char **handle_switch_I(char *arg, char **next)
311 char *path = arg+1;
313 switch (arg[1]) {
314 case '-':
315 add_pre_buffer("#split_include\n");
316 break;
318 case '\0': /* Plain "-I" */
319 path = *++next;
320 if (!path)
321 die("missing argument for -I option");
322 /* Fall through */
323 default:
324 add_pre_buffer("#add_include \"%s/\"\n", path);
326 return next;
329 static void add_cmdline_include(char *filename)
331 if (cmdline_include_nr >= CMDLINE_INCLUDE)
332 die("too many include files for %s\n", filename);
333 cmdline_include[cmdline_include_nr++] = filename;
336 static char **handle_switch_i(char *arg, char **next)
338 if (*next && !strcmp(arg, "include"))
339 add_cmdline_include(*++next);
340 else if (*next && !strcmp(arg, "imacros"))
341 add_cmdline_include(*++next);
342 else if (*next && !strcmp(arg, "isystem")) {
343 char *path = *++next;
344 if (!path)
345 die("missing argument for -isystem option");
346 add_pre_buffer("#add_isystem \"%s/\"\n", path);
347 } else if (*next && !strcmp(arg, "idirafter")) {
348 char *path = *++next;
349 if (!path)
350 die("missing argument for -idirafter option");
351 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
353 return next;
356 static char **handle_switch_M(char *arg, char **next)
358 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
359 if (!*next)
360 die("missing argument for -%s option", arg);
361 return next + 1;
363 return next;
366 static char **handle_switch_m(char *arg, char **next)
368 if (!strcmp(arg, "m64")) {
369 arch_m64 = 1;
370 } else if (!strcmp(arg, "m32")) {
371 arch_m64 = 0;
372 } else if (!strcmp(arg, "msize-long")) {
373 arch_msize_long = 1;
375 return next;
378 static void handle_arch_m64_finalize(void)
380 if (arch_m64) {
381 bits_in_long = 64;
382 max_int_alignment = 8;
383 bits_in_pointer = 64;
384 pointer_alignment = 8;
385 size_t_ctype = &ulong_ctype;
386 ssize_t_ctype = &long_ctype;
387 #ifdef __x86_64__
388 add_pre_buffer("#weak_define __x86_64__ 1\n");
389 #endif
393 static void handle_arch_msize_long_finalize(void)
395 if (arch_msize_long) {
396 size_t_ctype = &ulong_ctype;
397 ssize_t_ctype = &long_ctype;
401 static void handle_arch_finalize(void)
403 handle_arch_m64_finalize();
404 handle_arch_msize_long_finalize();
408 static char **handle_switch_o(char *arg, char **next)
410 if (!strcmp (arg, "o")) { // "-o foo"
411 if (!*++next)
412 die("argument to '-o' is missing");
414 // else "-ofoo"
416 return next;
419 static const struct warning {
420 const char *name;
421 int *flag;
422 } warnings[] = {
423 { "address-space", &Waddress_space },
424 { "bitwise", &Wbitwise },
425 { "cast-to-as", &Wcast_to_as },
426 { "cast-truncate", &Wcast_truncate },
427 { "context", &Wcontext },
428 { "decl", &Wdecl },
429 { "declaration-after-statement", &Wdeclarationafterstatement },
430 { "default-bitfield-sign", &Wdefault_bitfield_sign },
431 { "designated-init", &Wdesignated_init },
432 { "do-while", &Wdo_while },
433 { "enum-mismatch", &Wenum_mismatch },
434 { "init-cstring", &Winit_cstring },
435 { "non-pointer-null", &Wnon_pointer_null },
436 { "old-initializer", &Wold_initializer },
437 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
438 { "paren-string", &Wparen_string },
439 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
440 { "return-void", &Wreturn_void },
441 { "shadow", &Wshadow },
442 { "sizeof-bool", &Wsizeof_bool },
443 { "transparent-union", &Wtransparent_union },
444 { "typesign", &Wtypesign },
445 { "undef", &Wundef },
446 { "uninitialized", &Wuninitialized },
447 { "vla", &Wvla },
450 enum {
451 WARNING_OFF,
452 WARNING_ON,
453 WARNING_FORCE_OFF
457 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
459 int flag = WARNING_ON;
460 char *p = arg + 1;
461 unsigned i;
463 if (!strcmp(p, "sparse-all")) {
464 for (i = 0; i < n; i++) {
465 if (*warnings[i].flag != WARNING_FORCE_OFF)
466 *warnings[i].flag = WARNING_ON;
470 // Prefixes "no" and "no-" mean to turn warning off.
471 if (p[0] == 'n' && p[1] == 'o') {
472 p += 2;
473 if (p[0] == '-')
474 p++;
475 flag = WARNING_FORCE_OFF;
478 for (i = 0; i < n; i++) {
479 if (!strcmp(p,warnings[i].name)) {
480 *warnings[i].flag = flag;
481 return next;
485 // Unknown.
486 return NULL;
489 static char **handle_switch_W(char *arg, char **next)
491 char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
492 if (ret)
493 return ret;
495 // Unknown.
496 return next;
499 static struct warning debugs[] = {
500 { "entry", &dbg_entry},
501 { "dead", &dbg_dead},
505 static char **handle_switch_v(char *arg, char **next)
507 char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
508 if (ret)
509 return ret;
511 // Unknown.
512 do {
513 verbose++;
514 } while (*++arg == 'v');
515 return next;
519 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
521 unsigned i;
523 for (i = 0; i < n; i++) {
524 if (*warnings[i].flag == WARNING_FORCE_OFF)
525 *warnings[i].flag = WARNING_OFF;
529 static void handle_switch_W_finalize(void)
531 handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
533 /* default Wdeclarationafterstatement based on the C dialect */
534 if (-1 == Wdeclarationafterstatement)
536 switch (standard)
538 case STANDARD_C89:
539 case STANDARD_C94:
540 Wdeclarationafterstatement = 1;
541 break;
543 case STANDARD_C99:
544 case STANDARD_GNU89:
545 case STANDARD_GNU99:
546 Wdeclarationafterstatement = 0;
547 break;
549 default:
550 assert (0);
556 static void handle_switch_v_finalize(void)
558 handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
561 static char **handle_switch_U(char *arg, char **next)
563 const char *name = arg + 1;
564 add_pre_buffer ("#undef %s\n", name);
565 return next;
568 static char **handle_switch_O(char *arg, char **next)
570 int level = 1;
571 if (arg[1] >= '0' && arg[1] <= '9')
572 level = arg[1] - '0';
573 optimize = level;
574 optimize_size = arg[1] == 's';
575 return next;
578 static char **handle_switch_ftabstop(char *arg, char **next)
580 char *end;
581 unsigned long val;
583 if (*arg == '\0')
584 die("error: missing argument to \"-ftabstop=\"");
586 /* we silently ignore silly values */
587 val = strtoul(arg, &end, 10);
588 if (*end == '\0' && 1 <= val && val <= 100)
589 tabstop = val;
591 return next;
594 static char **handle_switch_f(char *arg, char **next)
596 arg++;
598 if (!strncmp(arg, "tabstop=", 8))
599 return handle_switch_ftabstop(arg+8, next);
601 /* handle switches w/ arguments above, boolean and only boolean below */
603 if (!strncmp(arg, "no-", 3)) {
604 arg += 3;
606 /* handle switch here.. */
607 return next;
610 static char **handle_switch_G(char *arg, char **next)
612 if (!strcmp (arg, "G") && *next)
613 return next + 1; // "-G 0"
614 else
615 return next; // "-G0" or (bogus) terminal "-G"
618 static char **handle_switch_a(char *arg, char **next)
620 if (!strcmp (arg, "ansi"))
621 standard = STANDARD_C89;
623 return next;
626 static char **handle_switch_s(char *arg, char **next)
628 if (!strncmp (arg, "std=", 4))
630 arg += 4;
632 if (!strcmp (arg, "c89") ||
633 !strcmp (arg, "iso9899:1990"))
634 standard = STANDARD_C89;
636 else if (!strcmp (arg, "iso9899:199409"))
637 standard = STANDARD_C94;
639 else if (!strcmp (arg, "c99") ||
640 !strcmp (arg, "c9x") ||
641 !strcmp (arg, "iso9899:1999") ||
642 !strcmp (arg, "iso9899:199x"))
643 standard = STANDARD_C99;
645 else if (!strcmp (arg, "gnu89"))
646 standard = STANDARD_GNU89;
648 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
649 standard = STANDARD_GNU99;
651 else
652 die ("Unsupported C dialect");
655 return next;
658 static char **handle_nostdinc(char *arg, char **next)
660 add_pre_buffer("#nostdinc\n");
661 return next;
664 static char **handle_base_dir(char *arg, char **next)
666 gcc_base_dir = *++next;
667 if (!gcc_base_dir)
668 die("missing argument for -gcc-base-dir option");
669 return next;
672 static char **handle_no_lineno(char *arg, char **next)
674 no_lineno = 1;
675 return next;
678 static char **handle_version(char *arg, char **next)
680 printf("%s\n", SPARSE_VERSION);
681 exit(0);
684 struct switches {
685 const char *name;
686 char **(*fn)(char *, char **);
689 static char **handle_long_options(char *arg, char **next)
691 static struct switches cmd[] = {
692 { "version", handle_version },
693 { NULL, NULL }
695 struct switches *s = cmd;
697 while (s->name) {
698 if (!strcmp(s->name, arg))
699 return s->fn(arg, next);
700 s++;
702 return next;
706 static char **handle_switch(char *arg, char **next)
708 static struct switches cmd[] = {
709 { "nostdinc", handle_nostdinc },
710 { "gcc-base-dir", handle_base_dir},
711 { "no-lineno", handle_no_lineno},
712 { NULL, NULL }
714 struct switches *s;
716 switch (*arg) {
717 case 'D': return handle_switch_D(arg, next);
718 case 'E': return handle_switch_E(arg, next);
719 case 'I': return handle_switch_I(arg, next);
720 case 'i': return handle_switch_i(arg, next);
721 case 'M': return handle_switch_M(arg, next);
722 case 'm': return handle_switch_m(arg, next);
723 case 'o': return handle_switch_o(arg, next);
724 case 'U': return handle_switch_U(arg, next);
725 case 'v': return handle_switch_v(arg, next);
726 case 'W': return handle_switch_W(arg, next);
727 case 'O': return handle_switch_O(arg, next);
728 case 'f': return handle_switch_f(arg, next);
729 case 'G': return handle_switch_G(arg, next);
730 case 'a': return handle_switch_a(arg, next);
731 case 's': return handle_switch_s(arg, next);
732 case '-': return handle_long_options(arg + 1, next);
733 default:
734 break;
737 s = cmd;
738 while (s->name) {
739 if (!strcmp(s->name, arg))
740 return s->fn(arg, next);
741 s++;
745 * Ignore unknown command line options:
746 * they're probably gcc switches
748 return next;
751 void declare_builtin_functions(void)
753 /* Gaah. gcc knows tons of builtin <string.h> functions */
754 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
755 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
756 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
757 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
758 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
759 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
760 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
761 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
762 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
763 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
764 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
765 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
766 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
767 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
768 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
770 /* And bitwise operations.. */
771 add_pre_buffer("extern int __builtin_clz(int);\n");
772 add_pre_buffer("extern int __builtin_clzl(long);\n");
773 add_pre_buffer("extern int __builtin_clzll(long long);\n");
774 add_pre_buffer("extern int __builtin_ctz(int);\n");
775 add_pre_buffer("extern int __builtin_ctzl(long);\n");
776 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
777 add_pre_buffer("extern int __builtin_ffs(int);\n");
778 add_pre_buffer("extern int __builtin_ffsl(long);\n");
779 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
780 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
781 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
782 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
784 /* And byte swaps.. */
785 add_pre_buffer("extern unsigned short __builtin_bswap16(unsigned short);\n");
786 add_pre_buffer("extern unsigned int __builtin_bswap32(unsigned int);\n");
787 add_pre_buffer("extern unsigned long long __builtin_bswap64(unsigned long long);\n");
789 /* And atomic memory access functions.. */
790 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
791 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
792 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
793 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
794 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
795 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
796 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
797 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
798 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
799 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
800 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
801 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
802 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
803 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
804 add_pre_buffer("extern void __sync_synchronize();\n");
805 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
806 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
808 /* And some random ones.. */
809 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
810 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
811 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
812 add_pre_buffer("extern void __builtin_trap(void);\n");
813 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
814 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
815 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
816 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
817 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
818 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
819 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
820 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
821 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
822 add_pre_buffer("extern long __builtin_labs(long);\n");
823 add_pre_buffer("extern double __builtin_fabs(double);\n");
824 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
826 /* Add Blackfin-specific stuff */
827 add_pre_buffer(
828 "#ifdef __bfin__\n"
829 "extern void __builtin_bfin_csync(void);\n"
830 "extern void __builtin_bfin_ssync(void);\n"
831 "extern int __builtin_bfin_norm_fr1x32(int);\n"
832 "#endif\n"
835 /* And some floating point stuff.. */
836 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
837 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
838 add_pre_buffer("extern int __builtin_isless(float, float);\n");
839 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
840 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
841 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
843 /* And some __FORTIFY_SOURCE ones.. */
844 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
845 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
846 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
847 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
848 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
849 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
850 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
851 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
852 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
853 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
854 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
855 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
856 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
857 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
858 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
861 void create_builtin_stream(void)
863 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
864 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
865 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
867 /* We add compiler headers path here because we have to parse
868 * the arguments to get it, falling back to default. */
869 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
870 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
872 add_pre_buffer("#define __extension__\n");
873 add_pre_buffer("#define __pragma__\n");
875 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
876 // solaris/sparc that is really "unsigned int" and for linux/x86_64
877 // it is "long unsigned int". In either case we can probably
878 // get away with this. We need the #weak_define as cgcc will define
879 // the right __SIZE_TYPE__.
880 if (size_t_ctype == &ulong_ctype)
881 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
882 else
883 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
884 add_pre_buffer("#weak_define __STDC__ 1\n");
886 switch (standard)
888 case STANDARD_C89:
889 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
890 break;
892 case STANDARD_C94:
893 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
894 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
895 break;
897 case STANDARD_C99:
898 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
899 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
900 break;
902 case STANDARD_GNU89:
903 break;
905 case STANDARD_GNU99:
906 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
907 break;
909 default:
910 assert (0);
913 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
914 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
915 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
916 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
917 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
918 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
919 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
920 add_pre_buffer("#define __builtin_va_end(arg)\n");
921 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
922 add_pre_buffer("#define __builtin_va_arg_pack()\n");
924 /* FIXME! We need to do these as special magic macros at expansion time! */
925 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
927 if (optimize)
928 add_pre_buffer("#define __OPTIMIZE__ 1\n");
929 if (optimize_size)
930 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
932 /* GCC defines these for limits.h */
933 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__) "\n");
934 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__) "\n");
935 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__) "\n");
936 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__) "\n");
937 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__) "\n");
938 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__) "\n");
939 add_pre_buffer("#weak_define __SIZEOF_POINTER__ " STRINGIFY(__SIZEOF_POINTER__) "\n");
940 add_pre_buffer("#weak_define __CHAR_BIT__ " STRINGIFY(__CHAR_BIT__) "\n");
943 static struct symbol_list *sparse_tokenstream(struct token *token)
945 // Preprocess the stream
946 token = preprocess(token);
948 if (preprocess_only) {
949 while (!eof_token(token)) {
950 int prec = 1;
951 struct token *next = token->next;
952 const char *separator = "";
953 if (next->pos.whitespace)
954 separator = " ";
955 if (next->pos.newline) {
956 separator = "\n\t\t\t\t\t";
957 prec = next->pos.pos;
958 if (prec > 4)
959 prec = 4;
961 printf("%s%.*s", show_token(token), prec, separator);
962 token = next;
964 putchar('\n');
966 return NULL;
969 // Parse the resulting C code
970 while (!eof_token(token))
971 token = external_declaration(token, &translation_unit_used_list);
972 return translation_unit_used_list;
975 static struct symbol_list *sparse_file(const char *filename)
977 int fd;
978 struct token *token;
980 if (strcmp (filename, "-") == 0) {
981 fd = 0;
982 } else {
983 fd = open(filename, O_RDONLY);
984 if (fd < 0)
985 die("No such file: %s", filename);
988 // Tokenize the input stream
989 token = tokenize(filename, fd, NULL, includepath);
990 store_all_tokens(token);
991 close(fd);
993 return sparse_tokenstream(token);
997 * This handles the "-include" directive etc: we're in global
998 * scope, and all types/macros etc will affect all the following
999 * files.
1001 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1002 * affect all subsequent files too, i.e. we can have non-local
1003 * behaviour between files!
1005 static struct symbol_list *sparse_initial(void)
1007 int i;
1009 // Prepend any "include" file to the stream.
1010 // We're in global scope, it will affect all files!
1011 for (i = 0; i < cmdline_include_nr; i++)
1012 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]);
1014 return sparse_tokenstream(pre_buffer_begin);
1017 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
1019 char **args;
1020 struct symbol_list *list;
1022 // Initialize symbol stream first, so that we can add defines etc
1023 init_symbols();
1025 args = argv;
1026 for (;;) {
1027 char *arg = *++args;
1028 if (!arg)
1029 break;
1031 if (arg[0] == '-' && arg[1]) {
1032 args = handle_switch(arg+1, args);
1033 continue;
1035 add_ptr_list_notag(filelist, arg);
1037 handle_switch_W_finalize();
1038 handle_switch_v_finalize();
1040 handle_arch_finalize();
1042 list = NULL;
1043 if (!ptr_list_empty(filelist)) {
1044 // Initialize type system
1045 init_ctype();
1047 create_builtin_stream();
1048 add_pre_buffer("#define __CHECKER__ 1\n");
1049 if (!preprocess_only)
1050 declare_builtin_functions();
1052 list = sparse_initial();
1055 * Protect the initial token allocations, since
1056 * they need to survive all the others
1058 protect_token_alloc();
1060 return list;
1063 struct symbol_list * sparse_keep_tokens(char *filename)
1065 struct symbol_list *res;
1067 /* Clear previous symbol list */
1068 translation_unit_used_list = NULL;
1070 new_file_scope();
1071 res = sparse_file(filename);
1073 /* And return it */
1074 return res;
1078 struct symbol_list * __sparse(char *filename)
1080 struct symbol_list *res;
1082 res = sparse_keep_tokens(filename);
1084 /* Drop the tokens for this file after parsing */
1085 clear_token_alloc();
1087 /* And return it */
1088 return res;
1091 struct symbol_list * sparse(char *filename)
1093 struct symbol_list *res = __sparse(filename);
1095 /* Evaluate the complete symbol list */
1096 evaluate_symbol_list(res);
1098 return res;