add warning option '-Wtautological-compare'
[smatch.git] / lib.c
blob2a1bbac87779cbb8598b3d81f5bb4ef675b6745c
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;
62 static const char *multiarch_dir = MULTIARCH_TRIPLET;
64 struct token *skip_to(struct token *token, int op)
66 while (!match_op(token, op) && !eof_token(token))
67 token = token->next;
68 return token;
71 struct token *expect(struct token *token, int op, const char *where)
73 if (!match_op(token, op)) {
74 static struct token bad_token;
75 if (token != &bad_token) {
76 bad_token.next = token;
77 sparse_error(token->pos, "Expected %s %s", show_special(op), where);
78 sparse_error(token->pos, "got %s", show_token(token));
80 if (op == ';')
81 return skip_to(token, op);
82 return &bad_token;
84 return token->next;
87 unsigned int hexval(unsigned int c)
89 int retval = 256;
90 switch (c) {
91 case '0'...'9':
92 retval = c - '0';
93 break;
94 case 'a'...'f':
95 retval = c - 'a' + 10;
96 break;
97 case 'A'...'F':
98 retval = c - 'A' + 10;
99 break;
101 return retval;
104 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
106 static char buffer[512];
107 const char *name;
109 vsprintf(buffer, fmt, args);
110 name = stream_name(pos.stream);
112 fprintf(stderr, "%s:%d:%d: %s%s\n",
113 name, pos.line, pos.pos, type, buffer);
116 static int max_warnings = 100;
117 static int show_info = 1;
119 void info(struct position pos, const char * fmt, ...)
121 va_list args;
123 if (!show_info)
124 return;
125 va_start(args, fmt);
126 do_warn("", pos, fmt, args);
127 va_end(args);
130 static void do_error(struct position pos, const char * fmt, va_list args)
132 static int errors = 0;
133 die_if_error = 1;
134 show_info = 1;
135 /* Shut up warnings after an error */
136 max_warnings = 0;
137 if (errors > 100) {
138 static int once = 0;
139 show_info = 0;
140 if (once)
141 return;
142 fmt = "too many errors";
143 once = 1;
146 do_warn("error: ", pos, fmt, args);
147 errors++;
150 void warning(struct position pos, const char * fmt, ...)
152 va_list args;
154 if (Wsparse_error) {
155 va_start(args, fmt);
156 do_error(pos, fmt, args);
157 va_end(args);
158 return;
161 if (!max_warnings) {
162 show_info = 0;
163 return;
166 if (!--max_warnings) {
167 show_info = 0;
168 fmt = "too many warnings";
171 va_start(args, fmt);
172 do_warn("warning: ", pos, fmt, args);
173 va_end(args);
176 void sparse_error(struct position pos, const char * fmt, ...)
178 va_list args;
179 va_start(args, fmt);
180 do_error(pos, fmt, args);
181 va_end(args);
184 void expression_error(struct expression *expr, const char *fmt, ...)
186 va_list args;
187 va_start(args, fmt);
188 do_error(expr->pos, fmt, args);
189 va_end(args);
190 expr->ctype = &bad_ctype;
193 void error_die(struct position pos, const char * fmt, ...)
195 va_list args;
196 va_start(args, fmt);
197 do_warn("error: ", pos, fmt, args);
198 va_end(args);
199 exit(1);
202 void die(const char *fmt, ...)
204 va_list args;
205 static char buffer[512];
207 va_start(args, fmt);
208 vsnprintf(buffer, sizeof(buffer), fmt, args);
209 va_end(args);
211 fprintf(stderr, "%s\n", buffer);
212 exit(1);
215 static struct token *pre_buffer_begin = NULL;
216 static struct token *pre_buffer_end = NULL;
218 int Waddress_space = 1;
219 int Wbitwise = 0;
220 int Wcast_to_as = 0;
221 int Wcast_truncate = 1;
222 int Wcontext = 1;
223 int Wdecl = 1;
224 int Wdeclarationafterstatement = -1;
225 int Wdefault_bitfield_sign = 0;
226 int Wdesignated_init = 1;
227 int Wdo_while = 0;
228 int Winit_cstring = 0;
229 int Wenum_mismatch = 1;
230 int Wsparse_error = 0;
231 int Wnon_pointer_null = 1;
232 int Wold_initializer = 1;
233 int Wone_bit_signed_bitfield = 1;
234 int Wparen_string = 0;
235 int Wptr_subtraction_blows = 0;
236 int Wreturn_void = 0;
237 int Wshadow = 0;
238 int Wsizeof_bool = 0;
239 int Wtautological_compare = 0;
240 int Wtransparent_union = 0;
241 int Wtypesign = 0;
242 int Wundef = 0;
243 int Wuninitialized = 1;
244 int Wunknown_attribute = 1;
245 int Wvla = 1;
247 int dbg_entry = 0;
248 int dbg_dead = 0;
250 int preprocess_only;
252 static enum { STANDARD_C89,
253 STANDARD_C94,
254 STANDARD_C99,
255 STANDARD_C11,
256 STANDARD_GNU11,
257 STANDARD_GNU89,
258 STANDARD_GNU99, } standard = STANDARD_GNU89;
260 #ifdef __x86_64__
261 #define ARCH_M64_DEFAULT 1
262 #else
263 #define ARCH_M64_DEFAULT 0
264 #endif
266 int arch_m64 = ARCH_M64_DEFAULT;
267 int arch_msize_long = 0;
269 #define CMDLINE_INCLUDE 20
270 static int cmdline_include_nr = 0;
271 static char *cmdline_include[CMDLINE_INCLUDE];
274 void add_pre_buffer(const char *fmt, ...)
276 va_list args;
277 unsigned int size;
278 struct token *begin, *end;
279 char buffer[4096];
281 va_start(args, fmt);
282 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
283 va_end(args);
284 begin = tokenize_buffer(buffer, size, &end);
285 if (!pre_buffer_begin)
286 pre_buffer_begin = begin;
287 if (pre_buffer_end)
288 pre_buffer_end->next = begin;
289 pre_buffer_end = end;
292 static char **handle_switch_D(char *arg, char **next)
294 const char *name = arg + 1;
295 const char *value = "1";
297 if (!*name || isspace((unsigned char)*name))
298 die("argument to `-D' is missing");
300 for (;;) {
301 char c;
302 c = *++arg;
303 if (!c)
304 break;
305 if (isspace((unsigned char)c) || c == '=') {
306 *arg = '\0';
307 value = arg + 1;
308 break;
311 add_pre_buffer("#define %s %s\n", name, value);
312 return next;
315 static char **handle_switch_E(char *arg, char **next)
317 if (arg[1] == '\0')
318 preprocess_only = 1;
319 return next;
322 static char **handle_switch_I(char *arg, char **next)
324 char *path = arg+1;
326 switch (arg[1]) {
327 case '-':
328 add_pre_buffer("#split_include\n");
329 break;
331 case '\0': /* Plain "-I" */
332 path = *++next;
333 if (!path)
334 die("missing argument for -I option");
335 /* Fall through */
336 default:
337 add_pre_buffer("#add_include \"%s/\"\n", path);
339 return next;
342 static void add_cmdline_include(char *filename)
344 if (cmdline_include_nr >= CMDLINE_INCLUDE)
345 die("too many include files for %s\n", filename);
346 cmdline_include[cmdline_include_nr++] = filename;
349 static char **handle_switch_i(char *arg, char **next)
351 if (*next && !strcmp(arg, "include"))
352 add_cmdline_include(*++next);
353 else if (*next && !strcmp(arg, "imacros"))
354 add_cmdline_include(*++next);
355 else if (*next && !strcmp(arg, "isystem")) {
356 char *path = *++next;
357 if (!path)
358 die("missing argument for -isystem option");
359 add_pre_buffer("#add_isystem \"%s/\"\n", path);
360 } else if (*next && !strcmp(arg, "idirafter")) {
361 char *path = *++next;
362 if (!path)
363 die("missing argument for -idirafter option");
364 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
366 return next;
369 static char **handle_switch_M(char *arg, char **next)
371 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
372 if (!*next)
373 die("missing argument for -%s option", arg);
374 return next + 1;
376 return next;
379 static char **handle_multiarch_dir(char *arg, char **next)
381 multiarch_dir = *++next;
382 if (!multiarch_dir)
383 die("missing argument for -multiarch-dir option");
384 return next;
387 static char **handle_switch_m(char *arg, char **next)
389 if (!strcmp(arg, "m64")) {
390 arch_m64 = 1;
391 } else if (!strcmp(arg, "m32")) {
392 arch_m64 = 0;
393 } else if (!strcmp(arg, "msize-long")) {
394 arch_msize_long = 1;
395 } else if (!strcmp(arg, "multiarch-dir"))
396 return handle_multiarch_dir(arg, next);
397 return next;
400 static void handle_arch_m64_finalize(void)
402 if (arch_m64) {
403 bits_in_long = 64;
404 max_int_alignment = 8;
405 bits_in_pointer = 64;
406 pointer_alignment = 8;
407 size_t_ctype = &ulong_ctype;
408 ssize_t_ctype = &long_ctype;
409 add_pre_buffer("#weak_define __LP64__ 1\n");
410 add_pre_buffer("#weak_define _LP64 1\n");
411 #ifdef __x86_64__
412 add_pre_buffer("#weak_define __x86_64__ 1\n");
413 #endif
417 static void handle_arch_msize_long_finalize(void)
419 if (arch_msize_long) {
420 size_t_ctype = &ulong_ctype;
421 ssize_t_ctype = &long_ctype;
425 static void handle_arch_finalize(void)
427 handle_arch_m64_finalize();
428 handle_arch_msize_long_finalize();
432 static char **handle_switch_o(char *arg, char **next)
434 if (!strcmp (arg, "o")) { // "-o foo"
435 if (!*++next)
436 die("argument to '-o' is missing");
438 // else "-ofoo"
440 return next;
443 static const struct warning {
444 const char *name;
445 int *flag;
446 } warnings[] = {
447 { "address-space", &Waddress_space },
448 { "bitwise", &Wbitwise },
449 { "cast-to-as", &Wcast_to_as },
450 { "cast-truncate", &Wcast_truncate },
451 { "context", &Wcontext },
452 { "decl", &Wdecl },
453 { "declaration-after-statement", &Wdeclarationafterstatement },
454 { "default-bitfield-sign", &Wdefault_bitfield_sign },
455 { "designated-init", &Wdesignated_init },
456 { "do-while", &Wdo_while },
457 { "enum-mismatch", &Wenum_mismatch },
458 { "sparse-error", &Wsparse_error },
459 { "init-cstring", &Winit_cstring },
460 { "non-pointer-null", &Wnon_pointer_null },
461 { "old-initializer", &Wold_initializer },
462 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
463 { "paren-string", &Wparen_string },
464 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
465 { "return-void", &Wreturn_void },
466 { "shadow", &Wshadow },
467 { "sizeof-bool", &Wsizeof_bool },
468 { "tautological-compare", &Wtautological_compare },
469 { "transparent-union", &Wtransparent_union },
470 { "typesign", &Wtypesign },
471 { "undef", &Wundef },
472 { "uninitialized", &Wuninitialized },
473 { "unknown-attribute", &Wunknown_attribute },
474 { "vla", &Wvla },
477 enum {
478 WARNING_OFF,
479 WARNING_ON,
480 WARNING_FORCE_OFF
484 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
486 int flag = WARNING_ON;
487 char *p = arg + 1;
488 unsigned i;
490 if (!strcmp(p, "sparse-all")) {
491 for (i = 0; i < n; i++) {
492 if (*warnings[i].flag != WARNING_FORCE_OFF && warnings[i].flag != &Wsparse_error)
493 *warnings[i].flag = WARNING_ON;
497 // Prefixes "no" and "no-" mean to turn warning off.
498 if (p[0] == 'n' && p[1] == 'o') {
499 p += 2;
500 if (p[0] == '-')
501 p++;
502 flag = WARNING_FORCE_OFF;
505 for (i = 0; i < n; i++) {
506 if (!strcmp(p,warnings[i].name)) {
507 *warnings[i].flag = flag;
508 return next;
512 // Unknown.
513 return NULL;
516 static char **handle_switch_W(char *arg, char **next)
518 char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
519 if (ret)
520 return ret;
522 // Unknown.
523 return next;
526 static struct warning debugs[] = {
527 { "entry", &dbg_entry},
528 { "dead", &dbg_dead},
532 static char **handle_switch_v(char *arg, char **next)
534 char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
535 if (ret)
536 return ret;
538 // Unknown.
539 do {
540 verbose++;
541 } while (*++arg == 'v');
542 return next;
546 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
548 unsigned i;
550 for (i = 0; i < n; i++) {
551 if (*warnings[i].flag == WARNING_FORCE_OFF)
552 *warnings[i].flag = WARNING_OFF;
556 static void handle_switch_W_finalize(void)
558 handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
560 /* default Wdeclarationafterstatement based on the C dialect */
561 if (-1 == Wdeclarationafterstatement)
563 switch (standard)
565 case STANDARD_C89:
566 case STANDARD_C94:
567 Wdeclarationafterstatement = 1;
568 break;
570 case STANDARD_C99:
571 case STANDARD_GNU89:
572 case STANDARD_GNU99:
573 case STANDARD_C11:
574 case STANDARD_GNU11:
575 Wdeclarationafterstatement = 0;
576 break;
578 default:
579 assert (0);
585 static void handle_switch_v_finalize(void)
587 handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
590 static char **handle_switch_U(char *arg, char **next)
592 const char *name = arg + 1;
593 add_pre_buffer ("#undef %s\n", name);
594 return next;
597 static char **handle_switch_O(char *arg, char **next)
599 int level = 1;
600 if (arg[1] >= '0' && arg[1] <= '9')
601 level = arg[1] - '0';
602 optimize = level;
603 optimize_size = arg[1] == 's';
604 return next;
607 static char **handle_switch_ftabstop(char *arg, char **next)
609 char *end;
610 unsigned long val;
612 if (*arg == '\0')
613 die("error: missing argument to \"-ftabstop=\"");
615 /* we silently ignore silly values */
616 val = strtoul(arg, &end, 10);
617 if (*end == '\0' && 1 <= val && val <= 100)
618 tabstop = val;
620 return next;
623 static char **handle_switch_f(char *arg, char **next)
625 arg++;
627 if (!strncmp(arg, "tabstop=", 8))
628 return handle_switch_ftabstop(arg+8, next);
630 /* handle switches w/ arguments above, boolean and only boolean below */
632 if (!strncmp(arg, "no-", 3)) {
633 arg += 3;
635 /* handle switch here.. */
636 return next;
639 static char **handle_switch_G(char *arg, char **next)
641 if (!strcmp (arg, "G") && *next)
642 return next + 1; // "-G 0"
643 else
644 return next; // "-G0" or (bogus) terminal "-G"
647 static char **handle_switch_a(char *arg, char **next)
649 if (!strcmp (arg, "ansi"))
650 standard = STANDARD_C89;
652 return next;
655 static char **handle_switch_s(char *arg, char **next)
657 if (!strncmp (arg, "std=", 4))
659 arg += 4;
661 if (!strcmp (arg, "c89") ||
662 !strcmp (arg, "iso9899:1990"))
663 standard = STANDARD_C89;
665 else if (!strcmp (arg, "iso9899:199409"))
666 standard = STANDARD_C94;
668 else if (!strcmp (arg, "c99") ||
669 !strcmp (arg, "c9x") ||
670 !strcmp (arg, "iso9899:1999") ||
671 !strcmp (arg, "iso9899:199x"))
672 standard = STANDARD_C99;
674 else if (!strcmp (arg, "gnu89"))
675 standard = STANDARD_GNU89;
677 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
678 standard = STANDARD_GNU99;
680 else if (!strcmp(arg, "c11") ||
681 !strcmp(arg, "c1x") ||
682 !strcmp(arg, "iso9899:2011"))
683 standard = STANDARD_C11;
685 else if (!strcmp(arg, "gnu11"))
686 standard = STANDARD_GNU11;
688 else
689 die ("Unsupported C dialect");
692 return next;
695 static char **handle_nostdinc(char *arg, char **next)
697 add_pre_buffer("#nostdinc\n");
698 return next;
701 static char **handle_switch_n(char *arg, char **next)
703 if (!strcmp (arg, "nostdinc"))
704 return handle_nostdinc(arg, next);
706 return next;
709 static char **handle_base_dir(char *arg, char **next)
711 gcc_base_dir = *++next;
712 if (!gcc_base_dir)
713 die("missing argument for -gcc-base-dir option");
714 return next;
717 static char **handle_switch_g(char *arg, char **next)
719 if (!strcmp (arg, "gcc-base-dir"))
720 return handle_base_dir(arg, next);
722 return next;
726 static char **handle_version(char *arg, char **next)
728 printf("%s\n", SPARSE_VERSION);
729 exit(0);
732 static char **handle_param(char *arg, char **next)
734 char *value = NULL;
736 /* For now just skip any '--param=*' or '--param *' */
737 if (*arg == '\0') {
738 value = *++next;
739 } else if (isspace((unsigned char)*arg) || *arg == '=') {
740 value = ++arg;
743 if (!value)
744 die("missing argument for --param option");
746 return next;
749 struct switches {
750 const char *name;
751 char **(*fn)(char *, char **);
752 unsigned int prefix:1;
755 static char **handle_long_options(char *arg, char **next)
757 static struct switches cmd[] = {
758 { "param", handle_param, 1 },
759 { "version", handle_version },
760 { NULL, NULL }
762 struct switches *s = cmd;
764 while (s->name) {
765 int optlen = strlen(s->name);
766 if (!strncmp(s->name, arg, optlen + !s->prefix))
767 return s->fn(arg + optlen, next);
768 s++;
770 return next;
773 static char **handle_switch(char *arg, char **next)
775 switch (*arg) {
776 case 'a': return handle_switch_a(arg, next);
777 case 'D': return handle_switch_D(arg, next);
778 case 'E': return handle_switch_E(arg, next);
779 case 'f': return handle_switch_f(arg, next);
780 case 'g': return handle_switch_g(arg, next);
781 case 'G': return handle_switch_G(arg, next);
782 case 'I': return handle_switch_I(arg, next);
783 case 'i': return handle_switch_i(arg, next);
784 case 'M': return handle_switch_M(arg, next);
785 case 'm': return handle_switch_m(arg, next);
786 case 'n': return handle_switch_n(arg, next);
787 case 'o': return handle_switch_o(arg, next);
788 case 'O': return handle_switch_O(arg, next);
789 case 's': return handle_switch_s(arg, next);
790 case 'U': return handle_switch_U(arg, next);
791 case 'v': return handle_switch_v(arg, next);
792 case 'W': return handle_switch_W(arg, next);
793 case '-': return handle_long_options(arg + 1, next);
794 default:
795 break;
799 * Ignore unknown command line options:
800 * they're probably gcc switches
802 return next;
805 static void predefined_macros(void)
807 unsigned long long val;
809 add_pre_buffer("#define __CHECKER__ 1\n");
811 val = (1ULL << (bits_in_long-1)) - 1;
812 add_pre_buffer("#weak_define __LONG_MAX__ %#llxLL\n", val);
813 add_pre_buffer("#weak_define __SIZEOF_POINTER__ %d\n", bits_in_pointer/8);
816 void declare_builtin_functions(void)
818 /* Gaah. gcc knows tons of builtin <string.h> functions */
819 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
820 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
821 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
822 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
823 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
824 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
825 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
826 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
827 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
828 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
829 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
830 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
831 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
832 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
833 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
835 /* And bitwise operations.. */
836 add_pre_buffer("extern int __builtin_clz(int);\n");
837 add_pre_buffer("extern int __builtin_clzl(long);\n");
838 add_pre_buffer("extern int __builtin_clzll(long long);\n");
839 add_pre_buffer("extern int __builtin_ctz(int);\n");
840 add_pre_buffer("extern int __builtin_ctzl(long);\n");
841 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
842 add_pre_buffer("extern int __builtin_ffs(int);\n");
843 add_pre_buffer("extern int __builtin_ffsl(long);\n");
844 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
845 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
846 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
847 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
849 /* And byte swaps.. */
850 add_pre_buffer("extern unsigned short ____builtin_bswap16(unsigned short);\n");
851 add_pre_buffer("extern unsigned int ____builtin_bswap32(unsigned int);\n");
852 add_pre_buffer("extern unsigned long long ____builtin_bswap64(unsigned long long);\n");
853 add_pre_buffer("#define __sparse_constant_swab16(x) ((unsigned short)("
854 " (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) |"
855 " (((unsigned short)(x) & (unsigned short)0xff00U) >> 8)))\n");
856 add_pre_buffer("#define __sparse_constant_swab32(x) ((unsigned int)("
857 " (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) |"
858 " (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) |"
859 " (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) |"
860 " (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24)))\n");
861 add_pre_buffer("#define __sparse_constant_swab64(x) ((unsigned long long)("
862 " (((unsigned long long)(x) & (unsigned long long)0x00000000000000ffULL) << 56) |"
863 " (((unsigned long long)(x) & (unsigned long long)0x000000000000ff00ULL) << 40) |"
864 " (((unsigned long long)(x) & (unsigned long long)0x0000000000ff0000ULL) << 24) |"
865 " (((unsigned long long)(x) & (unsigned long long)0x00000000ff000000ULL) << 8) |"
866 " (((unsigned long long)(x) & (unsigned long long)0x000000ff00000000ULL) >> 8) |"
867 " (((unsigned long long)(x) & (unsigned long long)0x0000ff0000000000ULL) >> 24) |"
868 " (((unsigned long long)(x) & (unsigned long long)0x00ff000000000000ULL) >> 40) |"
869 " (((unsigned long long)(x) & (unsigned long long)0xff00000000000000ULL) >> 56)))\n");
870 add_pre_buffer("#define __builtin_bswap16(x)"
871 " (__builtin_constant_p((unsigned short)(x)) ?"
872 " __sparse_constant_swab16(x) :"
873 " ____builtin_bswap16(x))\n");
874 add_pre_buffer("#define __builtin_bswap32(x)"
875 " (__builtin_constant_p((unsigned int)(x)) ?"
876 " __sparse_constant_swab32(x) :"
877 " ____builtin_bswap32(x))\n");
878 add_pre_buffer("#define __builtin_bswap64(x)"
879 " (__builtin_constant_p((unsigned long long)(x)) ?"
880 " __sparse_constant_swab64(x) :"
881 " ____builtin_bswap64(x))\n");
883 /* And atomic memory access functions.. */
884 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
885 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
886 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
887 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
888 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
889 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
890 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
891 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
892 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
893 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
894 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
895 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
896 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
897 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
898 add_pre_buffer("extern void __sync_synchronize();\n");
899 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
900 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
902 /* And some random ones.. */
903 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
904 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
905 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
906 add_pre_buffer("extern void __builtin_trap(void);\n");
907 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
908 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
909 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
910 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
911 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
912 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
913 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
914 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
915 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
916 add_pre_buffer("extern long __builtin_labs(long);\n");
917 add_pre_buffer("extern double __builtin_fabs(double);\n");
918 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
920 /* Add Blackfin-specific stuff */
921 add_pre_buffer(
922 "#ifdef __bfin__\n"
923 "extern void __builtin_bfin_csync(void);\n"
924 "extern void __builtin_bfin_ssync(void);\n"
925 "extern int __builtin_bfin_norm_fr1x32(int);\n"
926 "#endif\n"
929 /* And some floating point stuff.. */
930 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
931 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
932 add_pre_buffer("extern int __builtin_isless(float, float);\n");
933 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
934 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
935 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
937 /* And some INFINITY / NAN stuff.. */
938 add_pre_buffer("extern double __builtin_huge_val(void);\n");
939 add_pre_buffer("extern float __builtin_huge_valf(void);\n");
940 add_pre_buffer("extern long double __builtin_huge_vall(void);\n");
941 add_pre_buffer("extern double __builtin_inf(void);\n");
942 add_pre_buffer("extern float __builtin_inff(void);\n");
943 add_pre_buffer("extern long double __builtin_infl(void);\n");
944 add_pre_buffer("extern double __builtin_nan(const char *);\n");
945 add_pre_buffer("extern float __builtin_nanf(const char *);\n");
946 add_pre_buffer("extern long double __builtin_nanl(const char *);\n");
948 /* And some __FORTIFY_SOURCE ones.. */
949 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(const void *, int);\n");
950 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
951 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
952 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
953 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
954 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
955 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
956 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
957 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
958 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
959 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
960 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
961 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
962 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
963 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
966 void create_builtin_stream(void)
968 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
969 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
970 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
972 /* add the multiarch include directories, if any */
973 if (multiarch_dir && *multiarch_dir) {
974 add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir);
975 add_pre_buffer("#add_system \"/usr/local/include/%s\"\n", multiarch_dir);
978 /* We add compiler headers path here because we have to parse
979 * the arguments to get it, falling back to default. */
980 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
981 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
983 add_pre_buffer("#define __extension__\n");
984 add_pre_buffer("#define __pragma__\n");
986 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
987 // solaris/sparc that is really "unsigned int" and for linux/x86_64
988 // it is "long unsigned int". In either case we can probably
989 // get away with this. We need the #weak_define as cgcc will define
990 // the right __SIZE_TYPE__.
991 if (size_t_ctype == &ulong_ctype)
992 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
993 else
994 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
995 add_pre_buffer("#weak_define __STDC__ 1\n");
997 switch (standard)
999 case STANDARD_C89:
1000 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1001 break;
1003 case STANDARD_C94:
1004 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
1005 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1006 break;
1008 case STANDARD_C99:
1009 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1010 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1011 break;
1013 case STANDARD_GNU89:
1014 break;
1016 case STANDARD_GNU99:
1017 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1018 break;
1020 case STANDARD_C11:
1021 add_pre_buffer("#weak_define __STRICT_ANSI__ 1\n");
1022 case STANDARD_GNU11:
1023 add_pre_buffer("#weak_define __STDC_NO_ATOMICS__ 1\n");
1024 add_pre_buffer("#weak_define __STDC_NO_COMPLEX__ 1\n");
1025 add_pre_buffer("#weak_define __STDC_NO_THREADS__ 1\n");
1026 add_pre_buffer("#weak_define __STDC_VERSION__ 201112L\n");
1027 break;
1029 default:
1030 assert (0);
1033 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1034 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1035 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
1036 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
1037 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
1038 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
1039 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1040 add_pre_buffer("#define __builtin_ms_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1041 add_pre_buffer("#define __builtin_va_end(arg)\n");
1042 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
1043 add_pre_buffer("#define __builtin_va_arg_pack()\n");
1045 /* FIXME! We need to do these as special magic macros at expansion time! */
1046 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
1048 if (optimize)
1049 add_pre_buffer("#define __OPTIMIZE__ 1\n");
1050 if (optimize_size)
1051 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
1053 /* GCC defines these for limits.h */
1054 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__) "\n");
1055 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__) "\n");
1056 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__) "\n");
1057 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__) "\n");
1058 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__) "\n");
1059 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__) "\n");
1060 add_pre_buffer("#weak_define __SIZEOF_POINTER__ " STRINGIFY(__SIZEOF_POINTER__) "\n");
1061 add_pre_buffer("#weak_define __CHAR_BIT__ " STRINGIFY(__CHAR_BIT__) "\n");
1064 static struct symbol_list *sparse_tokenstream(struct token *token)
1066 // Preprocess the stream
1067 token = preprocess(token);
1069 if (preprocess_only) {
1070 while (!eof_token(token)) {
1071 int prec = 1;
1072 struct token *next = token->next;
1073 const char *separator = "";
1074 if (next->pos.whitespace)
1075 separator = " ";
1076 if (next->pos.newline) {
1077 separator = "\n\t\t\t\t\t";
1078 prec = next->pos.pos;
1079 if (prec > 4)
1080 prec = 4;
1082 printf("%s%.*s", show_token(token), prec, separator);
1083 token = next;
1085 putchar('\n');
1087 return NULL;
1090 // Parse the resulting C code
1091 while (!eof_token(token))
1092 token = external_declaration(token, &translation_unit_used_list);
1093 return translation_unit_used_list;
1096 static struct symbol_list *sparse_file(const char *filename)
1098 int fd;
1099 struct token *token;
1101 if (strcmp (filename, "-") == 0) {
1102 fd = 0;
1103 } else {
1104 fd = open(filename, O_RDONLY);
1105 if (fd < 0)
1106 die("No such file: %s", filename);
1109 // Tokenize the input stream
1110 token = tokenize(filename, fd, NULL, includepath);
1111 close(fd);
1113 return sparse_tokenstream(token);
1117 * This handles the "-include" directive etc: we're in global
1118 * scope, and all types/macros etc will affect all the following
1119 * files.
1121 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1122 * affect all subsequent files too, i.e. we can have non-local
1123 * behaviour between files!
1125 static struct symbol_list *sparse_initial(void)
1127 int i;
1129 // Prepend any "include" file to the stream.
1130 // We're in global scope, it will affect all files!
1131 for (i = 0; i < cmdline_include_nr; i++)
1132 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]);
1134 return sparse_tokenstream(pre_buffer_begin);
1137 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
1139 char **args;
1140 struct symbol_list *list;
1142 // Initialize symbol stream first, so that we can add defines etc
1143 init_symbols();
1145 args = argv;
1146 for (;;) {
1147 char *arg = *++args;
1148 if (!arg)
1149 break;
1151 if (arg[0] == '-' && arg[1]) {
1152 args = handle_switch(arg+1, args);
1153 continue;
1155 add_ptr_list_notag(filelist, arg);
1157 handle_switch_W_finalize();
1158 handle_switch_v_finalize();
1160 handle_arch_finalize();
1162 list = NULL;
1163 if (!ptr_list_empty(filelist)) {
1164 // Initialize type system
1165 init_ctype();
1167 create_builtin_stream();
1168 predefined_macros();
1169 if (!preprocess_only)
1170 declare_builtin_functions();
1172 list = sparse_initial();
1175 * Protect the initial token allocations, since
1176 * they need to survive all the others
1178 protect_token_alloc();
1180 return list;
1183 struct symbol_list * sparse_keep_tokens(char *filename)
1185 struct symbol_list *res;
1187 /* Clear previous symbol list */
1188 translation_unit_used_list = NULL;
1190 new_file_scope();
1191 res = sparse_file(filename);
1193 /* And return it */
1194 return res;
1198 struct symbol_list * __sparse(char *filename)
1200 struct symbol_list *res;
1202 res = sparse_keep_tokens(filename);
1204 /* Drop the tokens for this file after parsing */
1205 clear_token_alloc();
1207 /* And return it */
1208 return res;
1211 struct symbol_list * sparse(char *filename)
1213 struct symbol_list *res = __sparse(filename);
1215 /* Evaluate the complete symbol list */
1216 evaluate_symbol_list(res);
1218 return res;