cgcc: filter-out '-fdump-linearize[=...]'
[smatch.git] / lib.c
blobe3901ac99f72a0300d1e5b62f1c6a02e4328ae7c
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;
50 int has_error = 0;
52 #ifndef __GNUC__
53 # define __GNUC__ 2
54 # define __GNUC_MINOR__ 95
55 # define __GNUC_PATCHLEVEL__ 0
56 #endif
58 int gcc_major = __GNUC__;
59 int gcc_minor = __GNUC_MINOR__;
60 int gcc_patchlevel = __GNUC_PATCHLEVEL__;
62 static const char *gcc_base_dir = GCC_BASE;
63 static const char *multiarch_dir = MULTIARCH_TRIPLET;
65 struct token *skip_to(struct token *token, int op)
67 while (!match_op(token, op) && !eof_token(token))
68 token = token->next;
69 return token;
72 struct token *expect(struct token *token, int op, const char *where)
74 if (!match_op(token, op)) {
75 static struct token bad_token;
76 if (token != &bad_token) {
77 bad_token.next = token;
78 sparse_error(token->pos, "Expected %s %s", show_special(op), where);
79 sparse_error(token->pos, "got %s", show_token(token));
81 if (op == ';')
82 return skip_to(token, op);
83 return &bad_token;
85 return token->next;
88 unsigned int hexval(unsigned int c)
90 int retval = 256;
91 switch (c) {
92 case '0'...'9':
93 retval = c - '0';
94 break;
95 case 'a'...'f':
96 retval = c - 'a' + 10;
97 break;
98 case 'A'...'F':
99 retval = c - 'A' + 10;
100 break;
102 return retval;
105 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
107 static char buffer[512];
108 const char *name;
110 vsprintf(buffer, fmt, args);
111 name = stream_name(pos.stream);
113 fprintf(stderr, "%s:%d:%d: %s%s\n",
114 name, pos.line, pos.pos, type, buffer);
117 static int max_warnings = 100;
118 static int show_info = 1;
120 void info(struct position pos, const char * fmt, ...)
122 va_list args;
124 if (!show_info)
125 return;
126 va_start(args, fmt);
127 do_warn("", pos, fmt, args);
128 va_end(args);
131 static void do_error(struct position pos, const char * fmt, va_list args)
133 static int errors = 0;
134 die_if_error = 1;
135 show_info = 1;
136 /* Shut up warnings after an error */
137 has_error |= ERROR_CURR_PHASE;
138 if (errors > 100) {
139 static int once = 0;
140 show_info = 0;
141 if (once)
142 return;
143 fmt = "too many errors";
144 once = 1;
147 do_warn("error: ", pos, fmt, args);
148 errors++;
151 void warning(struct position pos, const char * fmt, ...)
153 va_list args;
155 if (Wsparse_error) {
156 va_start(args, fmt);
157 do_error(pos, fmt, args);
158 va_end(args);
159 return;
162 if (!max_warnings || has_error) {
163 show_info = 0;
164 return;
167 if (!--max_warnings) {
168 show_info = 0;
169 fmt = "too many warnings";
172 va_start(args, fmt);
173 do_warn("warning: ", pos, fmt, args);
174 va_end(args);
177 void sparse_error(struct position pos, const char * fmt, ...)
179 va_list args;
180 va_start(args, fmt);
181 do_error(pos, fmt, args);
182 va_end(args);
185 void expression_error(struct expression *expr, const char *fmt, ...)
187 va_list args;
188 va_start(args, fmt);
189 do_error(expr->pos, fmt, args);
190 va_end(args);
191 expr->ctype = &bad_ctype;
194 void error_die(struct position pos, const char * fmt, ...)
196 va_list args;
197 va_start(args, fmt);
198 do_warn("error: ", pos, fmt, args);
199 va_end(args);
200 exit(1);
203 void die(const char *fmt, ...)
205 va_list args;
206 static char buffer[512];
208 va_start(args, fmt);
209 vsnprintf(buffer, sizeof(buffer), fmt, args);
210 va_end(args);
212 fprintf(stderr, "%s\n", buffer);
213 exit(1);
216 static struct token *pre_buffer_begin = NULL;
217 static struct token *pre_buffer_end = NULL;
219 int Waddress = 0;
220 int Waddress_space = 1;
221 int Wbitwise = 1;
222 int Wcast_to_as = 0;
223 int Wcast_truncate = 1;
224 int Wcontext = 1;
225 int Wdecl = 1;
226 int Wdeclarationafterstatement = -1;
227 int Wdefault_bitfield_sign = 0;
228 int Wdesignated_init = 1;
229 int Wdo_while = 0;
230 int Winit_cstring = 0;
231 int Wenum_mismatch = 1;
232 int Wsparse_error = 0;
233 int Wnon_pointer_null = 1;
234 int Wold_initializer = 1;
235 int Wone_bit_signed_bitfield = 1;
236 int Woverride_init = 1;
237 int Woverride_init_all = 0;
238 int Woverride_init_whole_range = 0;
239 int Wparen_string = 0;
240 int Wptr_subtraction_blows = 0;
241 int Wreturn_void = 0;
242 int Wshadow = 0;
243 int Wsizeof_bool = 0;
244 int Wtautological_compare = 0;
245 int Wtransparent_union = 0;
246 int Wtypesign = 0;
247 int Wundef = 0;
248 int Wuninitialized = 1;
249 int Wunknown_attribute = 1;
250 int Wvla = 1;
252 int dump_macro_defs = 0;
254 int dbg_entry = 0;
255 int dbg_dead = 0;
257 int fmem_report = 0;
258 int fdump_linearize;
260 int preprocess_only;
262 static enum { STANDARD_C89,
263 STANDARD_C94,
264 STANDARD_C99,
265 STANDARD_C11,
266 STANDARD_GNU11,
267 STANDARD_GNU89,
268 STANDARD_GNU99, } standard = STANDARD_GNU89;
270 #define ARCH_LP32 0
271 #define ARCH_LP64 1
272 #define ARCH_LLP64 2
274 #ifdef __x86_64__
275 #define ARCH_M64_DEFAULT ARCH_LP64
276 #else
277 #define ARCH_M64_DEFAULT ARCH_LP32
278 #endif
280 int arch_m64 = ARCH_M64_DEFAULT;
281 int arch_msize_long = 0;
283 #define CMDLINE_INCLUDE 20
284 static int cmdline_include_nr = 0;
285 static char *cmdline_include[CMDLINE_INCLUDE];
288 void add_pre_buffer(const char *fmt, ...)
290 va_list args;
291 unsigned int size;
292 struct token *begin, *end;
293 char buffer[4096];
295 va_start(args, fmt);
296 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
297 va_end(args);
298 begin = tokenize_buffer(buffer, size, &end);
299 if (!pre_buffer_begin)
300 pre_buffer_begin = begin;
301 if (pre_buffer_end)
302 pre_buffer_end->next = begin;
303 pre_buffer_end = end;
306 static char **handle_switch_D(char *arg, char **next)
308 const char *name = arg + 1;
309 const char *value = "1";
311 if (!*name || isspace((unsigned char)*name))
312 die("argument to `-D' is missing");
314 for (;;) {
315 char c;
316 c = *++arg;
317 if (!c)
318 break;
319 if (isspace((unsigned char)c) || c == '=') {
320 *arg = '\0';
321 value = arg + 1;
322 break;
325 add_pre_buffer("#define %s %s\n", name, value);
326 return next;
329 static char **handle_switch_E(char *arg, char **next)
331 if (arg[1] == '\0')
332 preprocess_only = 1;
333 return next;
336 static char **handle_switch_I(char *arg, char **next)
338 char *path = arg+1;
340 switch (arg[1]) {
341 case '-':
342 add_pre_buffer("#split_include\n");
343 break;
345 case '\0': /* Plain "-I" */
346 path = *++next;
347 if (!path)
348 die("missing argument for -I option");
349 /* Fall through */
350 default:
351 add_pre_buffer("#add_include \"%s/\"\n", path);
353 return next;
356 static void add_cmdline_include(char *filename)
358 if (cmdline_include_nr >= CMDLINE_INCLUDE)
359 die("too many include files for %s\n", filename);
360 cmdline_include[cmdline_include_nr++] = filename;
363 static char **handle_switch_i(char *arg, char **next)
365 if (*next && !strcmp(arg, "include"))
366 add_cmdline_include(*++next);
367 else if (*next && !strcmp(arg, "imacros"))
368 add_cmdline_include(*++next);
369 else if (*next && !strcmp(arg, "isystem")) {
370 char *path = *++next;
371 if (!path)
372 die("missing argument for -isystem option");
373 add_pre_buffer("#add_isystem \"%s/\"\n", path);
374 } else if (*next && !strcmp(arg, "idirafter")) {
375 char *path = *++next;
376 if (!path)
377 die("missing argument for -idirafter option");
378 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
380 return next;
383 static char **handle_switch_M(char *arg, char **next)
385 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
386 if (!*next)
387 die("missing argument for -%s option", arg);
388 return next + 1;
390 return next;
393 static char **handle_multiarch_dir(char *arg, char **next)
395 multiarch_dir = *++next;
396 if (!multiarch_dir)
397 die("missing argument for -multiarch-dir option");
398 return next;
401 static char **handle_switch_m(char *arg, char **next)
403 if (!strcmp(arg, "m64")) {
404 arch_m64 = ARCH_LP64;
405 } else if (!strcmp(arg, "m32")) {
406 arch_m64 = ARCH_LP32;
407 } else if (!strcmp(arg, "msize-llp64")) {
408 arch_m64 = ARCH_LLP64;
409 } else if (!strcmp(arg, "msize-long")) {
410 arch_msize_long = 1;
411 } else if (!strcmp(arg, "multiarch-dir"))
412 return handle_multiarch_dir(arg, next);
413 return next;
416 static void handle_arch_m64_finalize(void)
418 switch (arch_m64) {
419 case ARCH_LP32:
420 /* default values */
421 return;
422 case ARCH_LP64:
423 bits_in_long = 64;
424 max_int_alignment = 8;
425 size_t_ctype = &ulong_ctype;
426 ssize_t_ctype = &long_ctype;
427 add_pre_buffer("#weak_define __LP64__ 1\n");
428 add_pre_buffer("#weak_define _LP64 1\n");
429 goto case_64bit_common;
430 case ARCH_LLP64:
431 bits_in_long = 32;
432 max_int_alignment = 4;
433 size_t_ctype = &ullong_ctype;
434 ssize_t_ctype = &llong_ctype;
435 add_pre_buffer("#weak_define __LLP64__ 1\n");
436 goto case_64bit_common;
437 case_64bit_common:
438 bits_in_pointer = 64;
439 pointer_alignment = 8;
440 #ifdef __x86_64__
441 add_pre_buffer("#weak_define __x86_64__ 1\n");
442 #endif
443 break;
447 static void handle_arch_msize_long_finalize(void)
449 if (arch_msize_long) {
450 size_t_ctype = &ulong_ctype;
451 ssize_t_ctype = &long_ctype;
455 static void handle_arch_finalize(void)
457 handle_arch_m64_finalize();
458 handle_arch_msize_long_finalize();
462 static int handle_simple_switch(const char *arg, const char *name, int *flag)
464 int val = 1;
466 // Prefixe "no-" mean to turn flag off.
467 if (strncmp(arg, "no-", 3) == 0) {
468 arg += 3;
469 val = 0;
472 if (strcmp(arg, name) == 0) {
473 *flag = val;
474 return 1;
477 // not handled
478 return 0;
481 static char **handle_switch_o(char *arg, char **next)
483 if (!strcmp (arg, "o")) { // "-o foo"
484 if (!*++next)
485 die("argument to '-o' is missing");
487 // else "-ofoo"
489 return next;
492 static const struct warning {
493 const char *name;
494 int *flag;
495 } warnings[] = {
496 { "address", &Waddress },
497 { "address-space", &Waddress_space },
498 { "bitwise", &Wbitwise },
499 { "cast-to-as", &Wcast_to_as },
500 { "cast-truncate", &Wcast_truncate },
501 { "context", &Wcontext },
502 { "decl", &Wdecl },
503 { "declaration-after-statement", &Wdeclarationafterstatement },
504 { "default-bitfield-sign", &Wdefault_bitfield_sign },
505 { "designated-init", &Wdesignated_init },
506 { "do-while", &Wdo_while },
507 { "enum-mismatch", &Wenum_mismatch },
508 { "init-cstring", &Winit_cstring },
509 { "non-pointer-null", &Wnon_pointer_null },
510 { "old-initializer", &Wold_initializer },
511 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
512 { "override-init", &Woverride_init },
513 { "override-init-all", &Woverride_init_all },
514 { "paren-string", &Wparen_string },
515 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
516 { "return-void", &Wreturn_void },
517 { "shadow", &Wshadow },
518 { "sizeof-bool", &Wsizeof_bool },
519 { "sparse-error", &Wsparse_error },
520 { "tautological-compare", &Wtautological_compare },
521 { "transparent-union", &Wtransparent_union },
522 { "typesign", &Wtypesign },
523 { "undef", &Wundef },
524 { "uninitialized", &Wuninitialized },
525 { "unknown-attribute", &Wunknown_attribute },
526 { "vla", &Wvla },
529 enum {
530 WARNING_OFF,
531 WARNING_ON,
532 WARNING_FORCE_OFF
536 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
538 int flag = WARNING_ON;
539 char *p = arg + 1;
540 unsigned i;
542 if (!strcmp(p, "sparse-all")) {
543 for (i = 0; i < n; i++) {
544 if (*warnings[i].flag != WARNING_FORCE_OFF && warnings[i].flag != &Wsparse_error)
545 *warnings[i].flag = WARNING_ON;
549 // Prefixes "no" and "no-" mean to turn warning off.
550 if (p[0] == 'n' && p[1] == 'o') {
551 p += 2;
552 if (p[0] == '-')
553 p++;
554 flag = WARNING_FORCE_OFF;
557 for (i = 0; i < n; i++) {
558 if (!strcmp(p,warnings[i].name)) {
559 *warnings[i].flag = flag;
560 return next;
564 // Unknown.
565 return NULL;
568 static char **handle_switch_W(char *arg, char **next)
570 char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
571 if (ret)
572 return ret;
574 // Unknown.
575 return next;
578 static struct warning debugs[] = {
579 { "entry", &dbg_entry},
580 { "dead", &dbg_dead},
584 static char **handle_switch_v(char *arg, char **next)
586 char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
587 if (ret)
588 return ret;
590 // Unknown.
591 do {
592 verbose++;
593 } while (*++arg == 'v');
594 return next;
597 static struct warning dumps[] = {
598 { "D", &dump_macro_defs},
601 static char **handle_switch_d(char *arg, char **next)
603 char ** ret = handle_onoff_switch(arg, next, dumps, ARRAY_SIZE(dumps));
604 if (ret)
605 return ret;
607 return next;
611 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
613 unsigned i;
615 for (i = 0; i < n; i++) {
616 if (*warnings[i].flag == WARNING_FORCE_OFF)
617 *warnings[i].flag = WARNING_OFF;
621 static void handle_switch_W_finalize(void)
623 handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
625 /* default Wdeclarationafterstatement based on the C dialect */
626 if (-1 == Wdeclarationafterstatement)
628 switch (standard)
630 case STANDARD_C89:
631 case STANDARD_C94:
632 Wdeclarationafterstatement = 1;
633 break;
635 case STANDARD_C99:
636 case STANDARD_GNU89:
637 case STANDARD_GNU99:
638 case STANDARD_C11:
639 case STANDARD_GNU11:
640 Wdeclarationafterstatement = 0;
641 break;
643 default:
644 assert (0);
650 static void handle_switch_v_finalize(void)
652 handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
655 static char **handle_switch_U(char *arg, char **next)
657 const char *name = arg + 1;
658 add_pre_buffer ("#undef %s\n", name);
659 return next;
662 static char **handle_switch_O(char *arg, char **next)
664 int level = 1;
665 if (arg[1] >= '0' && arg[1] <= '9')
666 level = arg[1] - '0';
667 optimize = level;
668 optimize_size = arg[1] == 's';
669 return next;
672 static char **handle_switch_ftabstop(char *arg, char **next)
674 char *end;
675 unsigned long val;
677 if (*arg == '\0')
678 die("error: missing argument to \"-ftabstop=\"");
680 /* we silently ignore silly values */
681 val = strtoul(arg, &end, 10);
682 if (*end == '\0' && 1 <= val && val <= 100)
683 tabstop = val;
685 return next;
688 static char **handle_switch_fdump(char *arg, char **next)
690 if (!strncmp(arg, "linearize", 9)) {
691 arg += 9;
692 if (*arg == '\0')
693 fdump_linearize = 1;
694 else if (!strcmp(arg, "=only"))
695 fdump_linearize = 2;
696 else
697 goto err;
700 /* ignore others flags */
701 return next;
703 err:
704 die("error: unknown flag \"-fdump-%s\"", arg);
707 static char **handle_switch_f(char *arg, char **next)
709 arg++;
711 if (!strncmp(arg, "tabstop=", 8))
712 return handle_switch_ftabstop(arg+8, next);
713 if (!strncmp(arg, "dump-", 5))
714 return handle_switch_fdump(arg+5, next);
716 /* handle switches w/ arguments above, boolean and only boolean below */
717 if (handle_simple_switch(arg, "mem-report", &fmem_report))
718 return next;
720 return next;
723 static char **handle_switch_G(char *arg, char **next)
725 if (!strcmp (arg, "G") && *next)
726 return next + 1; // "-G 0"
727 else
728 return next; // "-G0" or (bogus) terminal "-G"
731 static char **handle_switch_a(char *arg, char **next)
733 if (!strcmp (arg, "ansi"))
734 standard = STANDARD_C89;
736 return next;
739 static char **handle_switch_s(char *arg, char **next)
741 if (!strncmp (arg, "std=", 4))
743 arg += 4;
745 if (!strcmp (arg, "c89") ||
746 !strcmp (arg, "iso9899:1990"))
747 standard = STANDARD_C89;
749 else if (!strcmp (arg, "iso9899:199409"))
750 standard = STANDARD_C94;
752 else if (!strcmp (arg, "c99") ||
753 !strcmp (arg, "c9x") ||
754 !strcmp (arg, "iso9899:1999") ||
755 !strcmp (arg, "iso9899:199x"))
756 standard = STANDARD_C99;
758 else if (!strcmp (arg, "gnu89"))
759 standard = STANDARD_GNU89;
761 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
762 standard = STANDARD_GNU99;
764 else if (!strcmp(arg, "c11") ||
765 !strcmp(arg, "c1x") ||
766 !strcmp(arg, "iso9899:2011"))
767 standard = STANDARD_C11;
769 else if (!strcmp(arg, "gnu11"))
770 standard = STANDARD_GNU11;
772 else
773 die ("Unsupported C dialect");
776 return next;
779 static char **handle_nostdinc(char *arg, char **next)
781 add_pre_buffer("#nostdinc\n");
782 return next;
785 static char **handle_switch_n(char *arg, char **next)
787 if (!strcmp (arg, "nostdinc"))
788 return handle_nostdinc(arg, next);
790 return next;
793 static char **handle_base_dir(char *arg, char **next)
795 gcc_base_dir = *++next;
796 if (!gcc_base_dir)
797 die("missing argument for -gcc-base-dir option");
798 return next;
801 static char **handle_switch_g(char *arg, char **next)
803 if (!strcmp (arg, "gcc-base-dir"))
804 return handle_base_dir(arg, next);
806 return next;
810 static char **handle_version(char *arg, char **next)
812 printf("%s\n", SPARSE_VERSION);
813 exit(0);
816 static char **handle_param(char *arg, char **next)
818 char *value = NULL;
820 /* For now just skip any '--param=*' or '--param *' */
821 if (*arg == '\0') {
822 value = *++next;
823 } else if (isspace((unsigned char)*arg) || *arg == '=') {
824 value = ++arg;
827 if (!value)
828 die("missing argument for --param option");
830 return next;
833 struct switches {
834 const char *name;
835 char **(*fn)(char *, char **);
836 unsigned int prefix:1;
839 static char **handle_long_options(char *arg, char **next)
841 static struct switches cmd[] = {
842 { "param", handle_param, 1 },
843 { "version", handle_version },
844 { NULL, NULL }
846 struct switches *s = cmd;
848 while (s->name) {
849 int optlen = strlen(s->name);
850 if (!strncmp(s->name, arg, optlen + !s->prefix))
851 return s->fn(arg + optlen, next);
852 s++;
854 return next;
857 static char **handle_switch(char *arg, char **next)
859 switch (*arg) {
860 case 'a': return handle_switch_a(arg, next);
861 case 'D': return handle_switch_D(arg, next);
862 case 'd': return handle_switch_d(arg, next);
863 case 'E': return handle_switch_E(arg, next);
864 case 'f': return handle_switch_f(arg, next);
865 case 'g': return handle_switch_g(arg, next);
866 case 'G': return handle_switch_G(arg, next);
867 case 'I': return handle_switch_I(arg, next);
868 case 'i': return handle_switch_i(arg, next);
869 case 'M': return handle_switch_M(arg, next);
870 case 'm': return handle_switch_m(arg, next);
871 case 'n': return handle_switch_n(arg, next);
872 case 'o': return handle_switch_o(arg, next);
873 case 'O': return handle_switch_O(arg, next);
874 case 's': return handle_switch_s(arg, next);
875 case 'U': return handle_switch_U(arg, next);
876 case 'v': return handle_switch_v(arg, next);
877 case 'W': return handle_switch_W(arg, next);
878 case '-': return handle_long_options(arg + 1, next);
879 default:
880 break;
884 * Ignore unknown command line options:
885 * they're probably gcc switches
887 return next;
890 static void predefined_sizeof(const char *name, unsigned bits)
892 add_pre_buffer("#weak_define __SIZEOF_%s__ %d\n", name, bits/8);
895 static void predefined_max(const char *name, const char *suffix, unsigned bits)
897 unsigned long long max = (1ULL << (bits - 1 )) - 1;
899 add_pre_buffer("#weak_define __%s_MAX__ %#llx%s\n", name, max, suffix);
902 static void predefined_type_size(const char *name, const char *suffix, unsigned bits)
904 predefined_max(name, suffix, bits);
905 predefined_sizeof(name, bits);
908 static void predefined_macros(void)
910 add_pre_buffer("#define __CHECKER__ 1\n");
912 predefined_sizeof("SHORT", bits_in_short);
913 predefined_max("SHRT", "", bits_in_short);
914 predefined_max("SCHAR", "", bits_in_char);
915 predefined_max("WCHAR", "", bits_in_wchar);
916 add_pre_buffer("#weak_define __CHAR_BIT__ %d\n", bits_in_char);
918 predefined_type_size("INT", "", bits_in_int);
919 predefined_type_size("LONG", "L", bits_in_long);
920 predefined_type_size("LONG_LONG", "LL", bits_in_longlong);
922 predefined_sizeof("INT128", 128);
924 predefined_sizeof("SIZE_T", bits_in_pointer);
925 predefined_sizeof("PTRDIFF_T", bits_in_pointer);
926 predefined_sizeof("POINTER", bits_in_pointer);
928 predefined_sizeof("FLOAT", bits_in_float);
929 predefined_sizeof("DOUBLE", bits_in_double);
930 predefined_sizeof("LONG_DOUBLE", bits_in_longdouble);
933 void declare_builtin_functions(void)
935 /* Gaah. gcc knows tons of builtin <string.h> functions */
936 add_pre_buffer("extern void *__builtin_memchr(const void *, int, __SIZE_TYPE__);\n");
937 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
938 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
939 add_pre_buffer("extern void *__builtin_memmove(void *, const void *, __SIZE_TYPE__);\n");
940 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
941 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
942 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
943 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
944 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
945 add_pre_buffer("extern int __builtin_strncmp(const char *, const char *, __SIZE_TYPE__);\n");
946 add_pre_buffer("extern int __builtin_strcasecmp(const char *, const char *);\n");
947 add_pre_buffer("extern int __builtin_strncasecmp(const char *, const char *, __SIZE_TYPE__);\n");
948 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
949 add_pre_buffer("extern char *__builtin_strrchr(const char *, int);\n");
950 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
951 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
952 add_pre_buffer("extern char *__builtin_strdup(const char *);\n");
953 add_pre_buffer("extern char *__builtin_strndup(const char *, __SIZE_TYPE__);\n");
954 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
955 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
956 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
957 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
958 add_pre_buffer("extern char* __builtin_stpncpy(const char *, const char*, __SIZE_TYPE__);\n");
959 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
960 add_pre_buffer("extern char *__builtin_strstr(const char *, const char *);\n");
961 add_pre_buffer("extern char *__builtin_strcasestr(const char *, const char *);\n");
962 add_pre_buffer("extern char *__builtin_strnstr(const char *, const char *, __SIZE_TYPE__);\n");
964 /* And even some from <strings.h> */
965 add_pre_buffer("extern int __builtin_bcmp(const void *, const void *, __SIZE_TYPE__);\n");
966 add_pre_buffer("extern void __builtin_bcopy(const void *, void *, __SIZE_TYPE__);\n");
967 add_pre_buffer("extern void __builtin_bzero(void *, __SIZE_TYPE__);\n");
968 add_pre_buffer("extern char*__builtin_index(const char *, int);\n");
969 add_pre_buffer("extern char*__builtin_rindex(const char *, int);\n");
971 /* And bitwise operations.. */
972 add_pre_buffer("extern int __builtin_clrsb(int);\n");
973 add_pre_buffer("extern int __builtin_clrsbl(long);\n");
974 add_pre_buffer("extern int __builtin_clrsbll(long long);\n");
975 add_pre_buffer("extern int __builtin_clz(int);\n");
976 add_pre_buffer("extern int __builtin_clzl(long);\n");
977 add_pre_buffer("extern int __builtin_clzll(long long);\n");
978 add_pre_buffer("extern int __builtin_ctz(int);\n");
979 add_pre_buffer("extern int __builtin_ctzl(long);\n");
980 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
981 add_pre_buffer("extern int __builtin_ffs(int);\n");
982 add_pre_buffer("extern int __builtin_ffsl(long);\n");
983 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
984 add_pre_buffer("extern int __builtin_parity(unsigned int);\n");
985 add_pre_buffer("extern int __builtin_parityl(unsigned long);\n");
986 add_pre_buffer("extern int __builtin_parityll(unsigned long long);\n");
987 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
988 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
989 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
991 /* And byte swaps.. */
992 add_pre_buffer("extern unsigned short __builtin_bswap16(unsigned short);\n");
993 add_pre_buffer("extern unsigned int __builtin_bswap32(unsigned int);\n");
994 add_pre_buffer("extern unsigned long long __builtin_bswap64(unsigned long long);\n");
996 /* And atomic memory access functions.. */
997 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
998 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
999 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
1000 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
1001 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
1002 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
1003 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
1004 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
1005 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
1006 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
1007 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
1008 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
1009 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
1010 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
1011 add_pre_buffer("extern void __sync_synchronize();\n");
1012 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
1013 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
1015 /* And some random ones.. */
1016 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
1017 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
1018 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
1019 add_pre_buffer("extern void __builtin_trap(void);\n");
1020 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
1021 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
1022 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
1023 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
1024 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
1025 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
1026 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
1027 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
1028 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
1029 add_pre_buffer("extern int __builtin_abs(int);\n");
1030 add_pre_buffer("extern long __builtin_labs(long);\n");
1031 add_pre_buffer("extern long long __builtin_llabs(long long);\n");
1032 add_pre_buffer("extern double __builtin_fabs(double);\n");
1033 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
1035 /* Add Blackfin-specific stuff */
1036 add_pre_buffer(
1037 "#ifdef __bfin__\n"
1038 "extern void __builtin_bfin_csync(void);\n"
1039 "extern void __builtin_bfin_ssync(void);\n"
1040 "extern int __builtin_bfin_norm_fr1x32(int);\n"
1041 "#endif\n"
1044 /* And some floating point stuff.. */
1045 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
1046 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
1047 add_pre_buffer("extern int __builtin_isless(float, float);\n");
1048 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
1049 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
1050 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
1052 /* And some INFINITY / NAN stuff.. */
1053 add_pre_buffer("extern double __builtin_huge_val(void);\n");
1054 add_pre_buffer("extern float __builtin_huge_valf(void);\n");
1055 add_pre_buffer("extern long double __builtin_huge_vall(void);\n");
1056 add_pre_buffer("extern double __builtin_inf(void);\n");
1057 add_pre_buffer("extern float __builtin_inff(void);\n");
1058 add_pre_buffer("extern long double __builtin_infl(void);\n");
1059 add_pre_buffer("extern double __builtin_nan(const char *);\n");
1060 add_pre_buffer("extern float __builtin_nanf(const char *);\n");
1061 add_pre_buffer("extern long double __builtin_nanl(const char *);\n");
1063 /* And some __FORTIFY_SOURCE ones.. */
1064 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(const void *, int);\n");
1065 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1066 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1067 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1068 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1069 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
1070 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
1071 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
1072 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
1073 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
1074 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1075 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1076 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
1077 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
1078 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
1080 /* And some from <stdlib.h> */
1081 add_pre_buffer("extern void __builtin_abort(void);\n");
1082 add_pre_buffer("extern void *__builtin_calloc(__SIZE_TYPE__, __SIZE_TYPE__);\n");
1083 add_pre_buffer("extern void __builtin_exit(int);\n");
1084 add_pre_buffer("extern void *__builtin_malloc(__SIZE_TYPE__);\n");
1085 add_pre_buffer("extern void *__builtin_realloc(void *, __SIZE_TYPE__);\n");
1086 add_pre_buffer("extern void __builtin_free(void *);\n");
1088 /* And some from <stdio.h> */
1089 add_pre_buffer("extern int __builtin_printf(const char *, ...);\n");
1090 add_pre_buffer("extern int __builtin_sprintf(char *, const char *, ...);\n");
1091 add_pre_buffer("extern int __builtin_snprintf(char *, __SIZE_TYPE__, const char *, ...);\n");
1092 add_pre_buffer("extern int __builtin_puts(const char *);\n");
1093 add_pre_buffer("extern int __builtin_vprintf(const char *, __builtin_va_list);\n");
1094 add_pre_buffer("extern int __builtin_vsprintf(char *, const char *, __builtin_va_list);\n");
1095 add_pre_buffer("extern int __builtin_vsnprintf(char *, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
1098 void create_builtin_stream(void)
1100 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
1101 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
1102 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
1104 /* add the multiarch include directories, if any */
1105 if (multiarch_dir && *multiarch_dir) {
1106 add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir);
1107 add_pre_buffer("#add_system \"/usr/local/include/%s\"\n", multiarch_dir);
1110 /* We add compiler headers path here because we have to parse
1111 * the arguments to get it, falling back to default. */
1112 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
1113 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
1115 add_pre_buffer("#define __extension__\n");
1116 add_pre_buffer("#define __pragma__\n");
1118 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
1119 // solaris/sparc that is really "unsigned int" and for linux/x86_64
1120 // it is "long unsigned int". In either case we can probably
1121 // get away with this. We need the #weak_define as cgcc will define
1122 // the right __SIZE_TYPE__.
1123 if (size_t_ctype == &ulong_ctype)
1124 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
1125 else
1126 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
1127 add_pre_buffer("#weak_define __STDC__ 1\n");
1129 switch (standard)
1131 case STANDARD_C89:
1132 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1133 break;
1135 case STANDARD_C94:
1136 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
1137 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1138 break;
1140 case STANDARD_C99:
1141 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1142 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1143 break;
1145 case STANDARD_GNU89:
1146 break;
1148 case STANDARD_GNU99:
1149 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1150 break;
1152 case STANDARD_C11:
1153 add_pre_buffer("#weak_define __STRICT_ANSI__ 1\n");
1154 case STANDARD_GNU11:
1155 add_pre_buffer("#weak_define __STDC_NO_ATOMICS__ 1\n");
1156 add_pre_buffer("#weak_define __STDC_NO_COMPLEX__ 1\n");
1157 add_pre_buffer("#weak_define __STDC_NO_THREADS__ 1\n");
1158 add_pre_buffer("#weak_define __STDC_VERSION__ 201112L\n");
1159 break;
1161 default:
1162 assert (0);
1165 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1166 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1167 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
1168 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
1169 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
1170 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
1171 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1172 add_pre_buffer("#define __builtin_ms_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1173 add_pre_buffer("#define __builtin_va_end(arg)\n");
1174 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
1175 add_pre_buffer("#define __builtin_va_arg_pack()\n");
1177 /* FIXME! We need to do these as special magic macros at expansion time! */
1178 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
1180 if (optimize)
1181 add_pre_buffer("#define __OPTIMIZE__ 1\n");
1182 if (optimize_size)
1183 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
1186 static struct symbol_list *sparse_tokenstream(struct token *token)
1188 int builtin = token && !token->pos.stream;
1190 // Preprocess the stream
1191 token = preprocess(token);
1193 if (dump_macro_defs && !builtin)
1194 dump_macro_definitions();
1196 if (preprocess_only) {
1197 while (!eof_token(token)) {
1198 int prec = 1;
1199 struct token *next = token->next;
1200 const char *separator = "";
1201 if (next->pos.whitespace)
1202 separator = " ";
1203 if (next->pos.newline) {
1204 separator = "\n\t\t\t\t\t";
1205 prec = next->pos.pos;
1206 if (prec > 4)
1207 prec = 4;
1209 printf("%s%.*s", show_token(token), prec, separator);
1210 token = next;
1212 putchar('\n');
1214 return NULL;
1217 // Parse the resulting C code
1218 while (!eof_token(token))
1219 token = external_declaration(token, &translation_unit_used_list, NULL);
1220 return translation_unit_used_list;
1223 static struct symbol_list *sparse_file(const char *filename)
1225 int fd;
1226 struct token *token;
1228 if (strcmp (filename, "-") == 0) {
1229 fd = 0;
1230 } else {
1231 fd = open(filename, O_RDONLY);
1232 if (fd < 0)
1233 die("No such file: %s", filename);
1236 // Tokenize the input stream
1237 token = tokenize(filename, fd, NULL, includepath);
1238 close(fd);
1240 return sparse_tokenstream(token);
1244 * This handles the "-include" directive etc: we're in global
1245 * scope, and all types/macros etc will affect all the following
1246 * files.
1248 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1249 * affect all subsequent files too, i.e. we can have non-local
1250 * behaviour between files!
1252 static struct symbol_list *sparse_initial(void)
1254 int i;
1256 // Prepend any "include" file to the stream.
1257 // We're in global scope, it will affect all files!
1258 for (i = 0; i < cmdline_include_nr; i++)
1259 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]);
1261 return sparse_tokenstream(pre_buffer_begin);
1264 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
1266 char **args;
1267 struct symbol_list *list;
1269 // Initialize symbol stream first, so that we can add defines etc
1270 init_symbols();
1272 args = argv;
1273 for (;;) {
1274 char *arg = *++args;
1275 if (!arg)
1276 break;
1278 if (arg[0] == '-' && arg[1]) {
1279 args = handle_switch(arg+1, args);
1280 continue;
1282 add_ptr_list_notag(filelist, arg);
1284 handle_switch_W_finalize();
1285 handle_switch_v_finalize();
1287 handle_arch_finalize();
1289 list = NULL;
1290 if (!ptr_list_empty(filelist)) {
1291 // Initialize type system
1292 init_ctype();
1294 create_builtin_stream();
1295 predefined_macros();
1296 if (!preprocess_only)
1297 declare_builtin_functions();
1299 list = sparse_initial();
1302 * Protect the initial token allocations, since
1303 * they need to survive all the others
1305 protect_token_alloc();
1307 return list;
1310 struct symbol_list * sparse_keep_tokens(char *filename)
1312 struct symbol_list *res;
1314 /* Clear previous symbol list */
1315 translation_unit_used_list = NULL;
1317 new_file_scope();
1318 res = sparse_file(filename);
1320 /* And return it */
1321 return res;
1325 struct symbol_list * __sparse(char *filename)
1327 struct symbol_list *res;
1329 res = sparse_keep_tokens(filename);
1331 /* Drop the tokens for this file after parsing */
1332 clear_token_alloc();
1334 /* And return it */
1335 return res;
1338 struct symbol_list * sparse(char *filename)
1340 struct symbol_list *res = __sparse(filename);
1342 if (has_error & ERROR_CURR_PHASE)
1343 has_error = ERROR_PREV_PHASE;
1344 /* Evaluate the complete symbol list */
1345 evaluate_symbol_list(res);
1347 return res;