C11: teach sparse about '--std={c11,gnu11}'
[smatch.git] / lib.c
bloba2820237183bf1eb05ae6649ea1b8ee2be0ed24e
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 Wtransparent_union = 0;
240 int Wtypesign = 0;
241 int Wundef = 0;
242 int Wuninitialized = 1;
243 int Wunknown_attribute = 1;
244 int Wvla = 1;
246 int dbg_entry = 0;
247 int dbg_dead = 0;
249 int preprocess_only;
251 static enum { STANDARD_C89,
252 STANDARD_C94,
253 STANDARD_C99,
254 STANDARD_C11,
255 STANDARD_GNU11,
256 STANDARD_GNU89,
257 STANDARD_GNU99, } standard = STANDARD_GNU89;
259 #ifdef __x86_64__
260 #define ARCH_M64_DEFAULT 1
261 #else
262 #define ARCH_M64_DEFAULT 0
263 #endif
265 int arch_m64 = ARCH_M64_DEFAULT;
266 int arch_msize_long = 0;
268 #define CMDLINE_INCLUDE 20
269 static int cmdline_include_nr = 0;
270 static char *cmdline_include[CMDLINE_INCLUDE];
273 void add_pre_buffer(const char *fmt, ...)
275 va_list args;
276 unsigned int size;
277 struct token *begin, *end;
278 char buffer[4096];
280 va_start(args, fmt);
281 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
282 va_end(args);
283 begin = tokenize_buffer(buffer, size, &end);
284 if (!pre_buffer_begin)
285 pre_buffer_begin = begin;
286 if (pre_buffer_end)
287 pre_buffer_end->next = begin;
288 pre_buffer_end = end;
291 static char **handle_switch_D(char *arg, char **next)
293 const char *name = arg + 1;
294 const char *value = "1";
296 if (!*name || isspace((unsigned char)*name))
297 die("argument to `-D' is missing");
299 for (;;) {
300 char c;
301 c = *++arg;
302 if (!c)
303 break;
304 if (isspace((unsigned char)c) || c == '=') {
305 *arg = '\0';
306 value = arg + 1;
307 break;
310 add_pre_buffer("#define %s %s\n", name, value);
311 return next;
314 static char **handle_switch_E(char *arg, char **next)
316 if (arg[1] == '\0')
317 preprocess_only = 1;
318 return next;
321 static char **handle_switch_I(char *arg, char **next)
323 char *path = arg+1;
325 switch (arg[1]) {
326 case '-':
327 add_pre_buffer("#split_include\n");
328 break;
330 case '\0': /* Plain "-I" */
331 path = *++next;
332 if (!path)
333 die("missing argument for -I option");
334 /* Fall through */
335 default:
336 add_pre_buffer("#add_include \"%s/\"\n", path);
338 return next;
341 static void add_cmdline_include(char *filename)
343 if (cmdline_include_nr >= CMDLINE_INCLUDE)
344 die("too many include files for %s\n", filename);
345 cmdline_include[cmdline_include_nr++] = filename;
348 static char **handle_switch_i(char *arg, char **next)
350 if (*next && !strcmp(arg, "include"))
351 add_cmdline_include(*++next);
352 else if (*next && !strcmp(arg, "imacros"))
353 add_cmdline_include(*++next);
354 else if (*next && !strcmp(arg, "isystem")) {
355 char *path = *++next;
356 if (!path)
357 die("missing argument for -isystem option");
358 add_pre_buffer("#add_isystem \"%s/\"\n", path);
359 } else if (*next && !strcmp(arg, "idirafter")) {
360 char *path = *++next;
361 if (!path)
362 die("missing argument for -idirafter option");
363 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
365 return next;
368 static char **handle_switch_M(char *arg, char **next)
370 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
371 if (!*next)
372 die("missing argument for -%s option", arg);
373 return next + 1;
375 return next;
378 static char **handle_multiarch_dir(char *arg, char **next)
380 multiarch_dir = *++next;
381 if (!multiarch_dir)
382 die("missing argument for -multiarch-dir option");
383 return next;
386 static char **handle_switch_m(char *arg, char **next)
388 if (!strcmp(arg, "m64")) {
389 arch_m64 = 1;
390 } else if (!strcmp(arg, "m32")) {
391 arch_m64 = 0;
392 } else if (!strcmp(arg, "msize-long")) {
393 arch_msize_long = 1;
394 } else if (!strcmp(arg, "multiarch-dir"))
395 return handle_multiarch_dir(arg, next);
396 return next;
399 static void handle_arch_m64_finalize(void)
401 if (arch_m64) {
402 bits_in_long = 64;
403 max_int_alignment = 8;
404 bits_in_pointer = 64;
405 pointer_alignment = 8;
406 size_t_ctype = &ulong_ctype;
407 ssize_t_ctype = &long_ctype;
408 #ifdef __x86_64__
409 add_pre_buffer("#weak_define __x86_64__ 1\n");
410 #endif
414 static void handle_arch_msize_long_finalize(void)
416 if (arch_msize_long) {
417 size_t_ctype = &ulong_ctype;
418 ssize_t_ctype = &long_ctype;
422 static void handle_arch_finalize(void)
424 handle_arch_m64_finalize();
425 handle_arch_msize_long_finalize();
429 static char **handle_switch_o(char *arg, char **next)
431 if (!strcmp (arg, "o")) { // "-o foo"
432 if (!*++next)
433 die("argument to '-o' is missing");
435 // else "-ofoo"
437 return next;
440 static const struct warning {
441 const char *name;
442 int *flag;
443 } warnings[] = {
444 { "address-space", &Waddress_space },
445 { "bitwise", &Wbitwise },
446 { "cast-to-as", &Wcast_to_as },
447 { "cast-truncate", &Wcast_truncate },
448 { "context", &Wcontext },
449 { "decl", &Wdecl },
450 { "declaration-after-statement", &Wdeclarationafterstatement },
451 { "default-bitfield-sign", &Wdefault_bitfield_sign },
452 { "designated-init", &Wdesignated_init },
453 { "do-while", &Wdo_while },
454 { "enum-mismatch", &Wenum_mismatch },
455 { "sparse-error", &Wsparse_error },
456 { "init-cstring", &Winit_cstring },
457 { "non-pointer-null", &Wnon_pointer_null },
458 { "old-initializer", &Wold_initializer },
459 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
460 { "paren-string", &Wparen_string },
461 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
462 { "return-void", &Wreturn_void },
463 { "shadow", &Wshadow },
464 { "sizeof-bool", &Wsizeof_bool },
465 { "transparent-union", &Wtransparent_union },
466 { "typesign", &Wtypesign },
467 { "undef", &Wundef },
468 { "uninitialized", &Wuninitialized },
469 { "unknown-attribute", &Wunknown_attribute },
470 { "vla", &Wvla },
473 enum {
474 WARNING_OFF,
475 WARNING_ON,
476 WARNING_FORCE_OFF
480 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
482 int flag = WARNING_ON;
483 char *p = arg + 1;
484 unsigned i;
486 if (!strcmp(p, "sparse-all")) {
487 for (i = 0; i < n; i++) {
488 if (*warnings[i].flag != WARNING_FORCE_OFF && warnings[i].flag != &Wsparse_error)
489 *warnings[i].flag = WARNING_ON;
493 // Prefixes "no" and "no-" mean to turn warning off.
494 if (p[0] == 'n' && p[1] == 'o') {
495 p += 2;
496 if (p[0] == '-')
497 p++;
498 flag = WARNING_FORCE_OFF;
501 for (i = 0; i < n; i++) {
502 if (!strcmp(p,warnings[i].name)) {
503 *warnings[i].flag = flag;
504 return next;
508 // Unknown.
509 return NULL;
512 static char **handle_switch_W(char *arg, char **next)
514 char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
515 if (ret)
516 return ret;
518 // Unknown.
519 return next;
522 static struct warning debugs[] = {
523 { "entry", &dbg_entry},
524 { "dead", &dbg_dead},
528 static char **handle_switch_v(char *arg, char **next)
530 char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
531 if (ret)
532 return ret;
534 // Unknown.
535 do {
536 verbose++;
537 } while (*++arg == 'v');
538 return next;
542 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
544 unsigned i;
546 for (i = 0; i < n; i++) {
547 if (*warnings[i].flag == WARNING_FORCE_OFF)
548 *warnings[i].flag = WARNING_OFF;
552 static void handle_switch_W_finalize(void)
554 handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
556 /* default Wdeclarationafterstatement based on the C dialect */
557 if (-1 == Wdeclarationafterstatement)
559 switch (standard)
561 case STANDARD_C89:
562 case STANDARD_C94:
563 Wdeclarationafterstatement = 1;
564 break;
566 case STANDARD_C99:
567 case STANDARD_GNU89:
568 case STANDARD_GNU99:
569 case STANDARD_C11:
570 case STANDARD_GNU11:
571 Wdeclarationafterstatement = 0;
572 break;
574 default:
575 assert (0);
581 static void handle_switch_v_finalize(void)
583 handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
586 static char **handle_switch_U(char *arg, char **next)
588 const char *name = arg + 1;
589 add_pre_buffer ("#undef %s\n", name);
590 return next;
593 static char **handle_switch_O(char *arg, char **next)
595 int level = 1;
596 if (arg[1] >= '0' && arg[1] <= '9')
597 level = arg[1] - '0';
598 optimize = level;
599 optimize_size = arg[1] == 's';
600 return next;
603 static char **handle_switch_ftabstop(char *arg, char **next)
605 char *end;
606 unsigned long val;
608 if (*arg == '\0')
609 die("error: missing argument to \"-ftabstop=\"");
611 /* we silently ignore silly values */
612 val = strtoul(arg, &end, 10);
613 if (*end == '\0' && 1 <= val && val <= 100)
614 tabstop = val;
616 return next;
619 static char **handle_switch_f(char *arg, char **next)
621 arg++;
623 if (!strncmp(arg, "tabstop=", 8))
624 return handle_switch_ftabstop(arg+8, next);
626 /* handle switches w/ arguments above, boolean and only boolean below */
628 if (!strncmp(arg, "no-", 3)) {
629 arg += 3;
631 /* handle switch here.. */
632 return next;
635 static char **handle_switch_G(char *arg, char **next)
637 if (!strcmp (arg, "G") && *next)
638 return next + 1; // "-G 0"
639 else
640 return next; // "-G0" or (bogus) terminal "-G"
643 static char **handle_switch_a(char *arg, char **next)
645 if (!strcmp (arg, "ansi"))
646 standard = STANDARD_C89;
648 return next;
651 static char **handle_switch_s(char *arg, char **next)
653 if (!strncmp (arg, "std=", 4))
655 arg += 4;
657 if (!strcmp (arg, "c89") ||
658 !strcmp (arg, "iso9899:1990"))
659 standard = STANDARD_C89;
661 else if (!strcmp (arg, "iso9899:199409"))
662 standard = STANDARD_C94;
664 else if (!strcmp (arg, "c99") ||
665 !strcmp (arg, "c9x") ||
666 !strcmp (arg, "iso9899:1999") ||
667 !strcmp (arg, "iso9899:199x"))
668 standard = STANDARD_C99;
670 else if (!strcmp (arg, "gnu89"))
671 standard = STANDARD_GNU89;
673 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
674 standard = STANDARD_GNU99;
676 else if (!strcmp(arg, "c11") ||
677 !strcmp(arg, "c1x") ||
678 !strcmp(arg, "iso9899:2011"))
679 standard = STANDARD_C11;
681 else if (!strcmp(arg, "gnu11"))
682 standard = STANDARD_GNU11;
684 else
685 die ("Unsupported C dialect");
688 return next;
691 static char **handle_nostdinc(char *arg, char **next)
693 add_pre_buffer("#nostdinc\n");
694 return next;
697 static char **handle_switch_n(char *arg, char **next)
699 if (!strcmp (arg, "nostdinc"))
700 return handle_nostdinc(arg, next);
702 return next;
705 static char **handle_base_dir(char *arg, char **next)
707 gcc_base_dir = *++next;
708 if (!gcc_base_dir)
709 die("missing argument for -gcc-base-dir option");
710 return next;
713 static char **handle_switch_g(char *arg, char **next)
715 if (!strcmp (arg, "gcc-base-dir"))
716 return handle_base_dir(arg, next);
718 return next;
722 static char **handle_version(char *arg, char **next)
724 printf("%s\n", SPARSE_VERSION);
725 exit(0);
728 static char **handle_param(char *arg, char **next)
730 char *value = NULL;
732 /* For now just skip any '--param=*' or '--param *' */
733 if (*arg == '\0') {
734 value = *++next;
735 } else if (isspace((unsigned char)*arg) || *arg == '=') {
736 value = ++arg;
739 if (!value)
740 die("missing argument for --param option");
742 return next;
745 struct switches {
746 const char *name;
747 char **(*fn)(char *, char **);
748 unsigned int prefix:1;
751 static char **handle_long_options(char *arg, char **next)
753 static struct switches cmd[] = {
754 { "param", handle_param, 1 },
755 { "version", handle_version },
756 { NULL, NULL }
758 struct switches *s = cmd;
760 while (s->name) {
761 int optlen = strlen(s->name);
762 if (!strncmp(s->name, arg, optlen + !s->prefix))
763 return s->fn(arg + optlen, next);
764 s++;
766 return next;
769 static char **handle_switch(char *arg, char **next)
771 switch (*arg) {
772 case 'a': return handle_switch_a(arg, next);
773 case 'D': return handle_switch_D(arg, next);
774 case 'E': return handle_switch_E(arg, next);
775 case 'f': return handle_switch_f(arg, next);
776 case 'g': return handle_switch_g(arg, next);
777 case 'G': return handle_switch_G(arg, next);
778 case 'I': return handle_switch_I(arg, next);
779 case 'i': return handle_switch_i(arg, next);
780 case 'M': return handle_switch_M(arg, next);
781 case 'm': return handle_switch_m(arg, next);
782 case 'n': return handle_switch_n(arg, next);
783 case 'o': return handle_switch_o(arg, next);
784 case 'O': return handle_switch_O(arg, next);
785 case 's': return handle_switch_s(arg, next);
786 case 'U': return handle_switch_U(arg, next);
787 case 'v': return handle_switch_v(arg, next);
788 case 'W': return handle_switch_W(arg, next);
789 case '-': return handle_long_options(arg + 1, next);
790 default:
791 break;
795 * Ignore unknown command line options:
796 * they're probably gcc switches
798 return next;
801 void declare_builtin_functions(void)
803 /* Gaah. gcc knows tons of builtin <string.h> functions */
804 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
805 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
806 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
807 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
808 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
809 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
810 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
811 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
812 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
813 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
814 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
815 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
816 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
817 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
818 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
820 /* And bitwise operations.. */
821 add_pre_buffer("extern int __builtin_clz(int);\n");
822 add_pre_buffer("extern int __builtin_clzl(long);\n");
823 add_pre_buffer("extern int __builtin_clzll(long long);\n");
824 add_pre_buffer("extern int __builtin_ctz(int);\n");
825 add_pre_buffer("extern int __builtin_ctzl(long);\n");
826 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
827 add_pre_buffer("extern int __builtin_ffs(int);\n");
828 add_pre_buffer("extern int __builtin_ffsl(long);\n");
829 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
830 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
831 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
832 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
834 /* And byte swaps.. */
835 add_pre_buffer("extern unsigned short ____builtin_bswap16(unsigned short);\n");
836 add_pre_buffer("extern unsigned int ____builtin_bswap32(unsigned int);\n");
837 add_pre_buffer("extern unsigned long long ____builtin_bswap64(unsigned long long);\n");
838 add_pre_buffer("#define __sparse_constant_swab16(x) ((unsigned short)("
839 " (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) |"
840 " (((unsigned short)(x) & (unsigned short)0xff00U) >> 8)))\n");
841 add_pre_buffer("#define __sparse_constant_swab32(x) ((unsigned int)("
842 " (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) |"
843 " (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) |"
844 " (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) |"
845 " (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24)))\n");
846 add_pre_buffer("#define __sparse_constant_swab64(x) ((unsigned long long)("
847 " (((unsigned long long)(x) & (unsigned long long)0x00000000000000ffULL) << 56) |"
848 " (((unsigned long long)(x) & (unsigned long long)0x000000000000ff00ULL) << 40) |"
849 " (((unsigned long long)(x) & (unsigned long long)0x0000000000ff0000ULL) << 24) |"
850 " (((unsigned long long)(x) & (unsigned long long)0x00000000ff000000ULL) << 8) |"
851 " (((unsigned long long)(x) & (unsigned long long)0x000000ff00000000ULL) >> 8) |"
852 " (((unsigned long long)(x) & (unsigned long long)0x0000ff0000000000ULL) >> 24) |"
853 " (((unsigned long long)(x) & (unsigned long long)0x00ff000000000000ULL) >> 40) |"
854 " (((unsigned long long)(x) & (unsigned long long)0xff00000000000000ULL) >> 56)))\n");
855 add_pre_buffer("#define __builtin_bswap16(x)"
856 " (__builtin_constant_p((unsigned short)(x)) ?"
857 " __sparse_constant_swab16(x) :"
858 " ____builtin_bswap16(x))\n");
859 add_pre_buffer("#define __builtin_bswap32(x)"
860 " (__builtin_constant_p((unsigned int)(x)) ?"
861 " __sparse_constant_swab32(x) :"
862 " ____builtin_bswap32(x))\n");
863 add_pre_buffer("#define __builtin_bswap64(x)"
864 " (__builtin_constant_p((unsigned long long)(x)) ?"
865 " __sparse_constant_swab64(x) :"
866 " ____builtin_bswap64(x))\n");
868 /* And atomic memory access functions.. */
869 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
870 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
871 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
872 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
873 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
874 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
875 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
876 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
877 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
878 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
879 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
880 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
881 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
882 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
883 add_pre_buffer("extern void __sync_synchronize();\n");
884 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
885 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
887 /* And some random ones.. */
888 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
889 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
890 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
891 add_pre_buffer("extern void __builtin_trap(void);\n");
892 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
893 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
894 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
895 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
896 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
897 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
898 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
899 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
900 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
901 add_pre_buffer("extern long __builtin_labs(long);\n");
902 add_pre_buffer("extern double __builtin_fabs(double);\n");
903 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
905 /* Add Blackfin-specific stuff */
906 add_pre_buffer(
907 "#ifdef __bfin__\n"
908 "extern void __builtin_bfin_csync(void);\n"
909 "extern void __builtin_bfin_ssync(void);\n"
910 "extern int __builtin_bfin_norm_fr1x32(int);\n"
911 "#endif\n"
914 /* And some floating point stuff.. */
915 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
916 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
917 add_pre_buffer("extern int __builtin_isless(float, float);\n");
918 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
919 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
920 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
922 /* And some INFINITY / NAN stuff.. */
923 add_pre_buffer("extern double __builtin_huge_val(void);\n");
924 add_pre_buffer("extern float __builtin_huge_valf(void);\n");
925 add_pre_buffer("extern long double __builtin_huge_vall(void);\n");
926 add_pre_buffer("extern double __builtin_inf(void);\n");
927 add_pre_buffer("extern float __builtin_inff(void);\n");
928 add_pre_buffer("extern long double __builtin_infl(void);\n");
929 add_pre_buffer("extern double __builtin_nan(const char *);\n");
930 add_pre_buffer("extern float __builtin_nanf(const char *);\n");
931 add_pre_buffer("extern long double __builtin_nanl(const char *);\n");
933 /* And some __FORTIFY_SOURCE ones.. */
934 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(const void *, int);\n");
935 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
936 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
937 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
938 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
939 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
940 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
941 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
942 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
943 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
944 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
945 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
946 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
947 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
948 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
951 void create_builtin_stream(void)
953 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
954 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
955 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
957 /* add the multiarch include directories, if any */
958 if (multiarch_dir && *multiarch_dir) {
959 add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir);
960 add_pre_buffer("#add_system \"/usr/local/include/%s\"\n", multiarch_dir);
963 /* We add compiler headers path here because we have to parse
964 * the arguments to get it, falling back to default. */
965 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
966 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
968 add_pre_buffer("#define __extension__\n");
969 add_pre_buffer("#define __pragma__\n");
971 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
972 // solaris/sparc that is really "unsigned int" and for linux/x86_64
973 // it is "long unsigned int". In either case we can probably
974 // get away with this. We need the #weak_define as cgcc will define
975 // the right __SIZE_TYPE__.
976 if (size_t_ctype == &ulong_ctype)
977 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
978 else
979 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
980 add_pre_buffer("#weak_define __STDC__ 1\n");
982 switch (standard)
984 case STANDARD_C89:
985 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
986 break;
988 case STANDARD_C94:
989 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
990 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
991 break;
993 case STANDARD_C99:
994 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
995 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
996 break;
998 case STANDARD_GNU89:
999 break;
1001 case STANDARD_GNU99:
1002 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1003 break;
1005 case STANDARD_C11:
1006 add_pre_buffer("#weak_define __STRICT_ANSI__ 1\n");
1007 case STANDARD_GNU11:
1008 add_pre_buffer("#weak_define __STDC_NO_ATOMICS__ 1\n");
1009 add_pre_buffer("#weak_define __STDC_NO_COMPLEX__ 1\n");
1010 add_pre_buffer("#weak_define __STDC_NO_THREADS__ 1\n");
1011 add_pre_buffer("#weak_define __STDC_VERSION__ 201112L\n");
1012 break;
1014 default:
1015 assert (0);
1018 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1019 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1020 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
1021 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
1022 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
1023 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
1024 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1025 add_pre_buffer("#define __builtin_ms_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1026 add_pre_buffer("#define __builtin_va_end(arg)\n");
1027 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
1028 add_pre_buffer("#define __builtin_va_arg_pack()\n");
1030 /* FIXME! We need to do these as special magic macros at expansion time! */
1031 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
1033 if (optimize)
1034 add_pre_buffer("#define __OPTIMIZE__ 1\n");
1035 if (optimize_size)
1036 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
1038 /* GCC defines these for limits.h */
1039 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__) "\n");
1040 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__) "\n");
1041 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__) "\n");
1042 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__) "\n");
1043 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__) "\n");
1044 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__) "\n");
1045 add_pre_buffer("#weak_define __SIZEOF_POINTER__ " STRINGIFY(__SIZEOF_POINTER__) "\n");
1046 add_pre_buffer("#weak_define __CHAR_BIT__ " STRINGIFY(__CHAR_BIT__) "\n");
1049 static struct symbol_list *sparse_tokenstream(struct token *token)
1051 // Preprocess the stream
1052 token = preprocess(token);
1054 if (preprocess_only) {
1055 while (!eof_token(token)) {
1056 int prec = 1;
1057 struct token *next = token->next;
1058 const char *separator = "";
1059 if (next->pos.whitespace)
1060 separator = " ";
1061 if (next->pos.newline) {
1062 separator = "\n\t\t\t\t\t";
1063 prec = next->pos.pos;
1064 if (prec > 4)
1065 prec = 4;
1067 printf("%s%.*s", show_token(token), prec, separator);
1068 token = next;
1070 putchar('\n');
1072 return NULL;
1075 // Parse the resulting C code
1076 while (!eof_token(token))
1077 token = external_declaration(token, &translation_unit_used_list);
1078 return translation_unit_used_list;
1081 static struct symbol_list *sparse_file(const char *filename)
1083 int fd;
1084 struct token *token;
1086 if (strcmp (filename, "-") == 0) {
1087 fd = 0;
1088 } else {
1089 fd = open(filename, O_RDONLY);
1090 if (fd < 0)
1091 die("No such file: %s", filename);
1094 // Tokenize the input stream
1095 token = tokenize(filename, fd, NULL, includepath);
1096 close(fd);
1098 return sparse_tokenstream(token);
1102 * This handles the "-include" directive etc: we're in global
1103 * scope, and all types/macros etc will affect all the following
1104 * files.
1106 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1107 * affect all subsequent files too, i.e. we can have non-local
1108 * behaviour between files!
1110 static struct symbol_list *sparse_initial(void)
1112 int i;
1114 // Prepend any "include" file to the stream.
1115 // We're in global scope, it will affect all files!
1116 for (i = 0; i < cmdline_include_nr; i++)
1117 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]);
1119 return sparse_tokenstream(pre_buffer_begin);
1122 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
1124 char **args;
1125 struct symbol_list *list;
1127 // Initialize symbol stream first, so that we can add defines etc
1128 init_symbols();
1130 args = argv;
1131 for (;;) {
1132 char *arg = *++args;
1133 if (!arg)
1134 break;
1136 if (arg[0] == '-' && arg[1]) {
1137 args = handle_switch(arg+1, args);
1138 continue;
1140 add_ptr_list_notag(filelist, arg);
1142 handle_switch_W_finalize();
1143 handle_switch_v_finalize();
1145 handle_arch_finalize();
1147 list = NULL;
1148 if (!ptr_list_empty(filelist)) {
1149 // Initialize type system
1150 init_ctype();
1152 create_builtin_stream();
1153 add_pre_buffer("#define __CHECKER__ 1\n");
1154 if (!preprocess_only)
1155 declare_builtin_functions();
1157 list = sparse_initial();
1160 * Protect the initial token allocations, since
1161 * they need to survive all the others
1163 protect_token_alloc();
1165 return list;
1168 struct symbol_list * sparse_keep_tokens(char *filename)
1170 struct symbol_list *res;
1172 /* Clear previous symbol list */
1173 translation_unit_used_list = NULL;
1175 new_file_scope();
1176 res = sparse_file(filename);
1178 /* And return it */
1179 return res;
1183 struct symbol_list * __sparse(char *filename)
1185 struct symbol_list *res;
1187 res = sparse_keep_tokens(filename);
1189 /* Drop the tokens for this file after parsing */
1190 clear_token_alloc();
1192 /* And return it */
1193 return res;
1196 struct symbol_list * sparse(char *filename)
1198 struct symbol_list *res = __sparse(filename);
1200 /* Evaluate the complete symbol list */
1201 evaluate_symbol_list(res);
1203 return res;