data_source: rename a variable "tmp" => "arg"
[smatch.git] / lib.c
blob69b5ab8c5f9cbf1c5320a334ff7f12d2dc8761ad
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 parse_error;
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;
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;
134 parse_error = 1;
135 die_if_error = 1;
136 show_info = 1;
137 /* Shut up warnings after an error */
138 max_warnings = 0;
139 if (errors > 100) {
140 static int once = 0;
141 show_info = 0;
142 if (once)
143 return;
144 fmt = "too many errors";
145 once = 1;
148 do_warn("error: ", pos, fmt, args);
149 errors++;
152 void warning(struct position pos, const char * fmt, ...)
154 va_list args;
156 if (Werror) {
157 va_start(args, fmt);
158 do_error(pos, fmt, args);
159 va_end(args);
160 return;
163 if (!max_warnings) {
164 show_info = 0;
165 return;
168 if (!--max_warnings) {
169 show_info = 0;
170 fmt = "too many warnings";
173 va_start(args, fmt);
174 do_warn("warning: ", pos, fmt, args);
175 va_end(args);
178 void sparse_error(struct position pos, const char * fmt, ...)
180 va_list args;
181 va_start(args, fmt);
182 do_error(pos, fmt, args);
183 va_end(args);
186 void expression_error(struct expression *expr, const char *fmt, ...)
188 va_list args;
189 va_start(args, fmt);
190 do_error(expr->pos, fmt, args);
191 va_end(args);
192 expr->ctype = &bad_ctype;
195 void error_die(struct position pos, const char * fmt, ...)
197 va_list args;
198 va_start(args, fmt);
199 do_warn("error: ", pos, fmt, args);
200 va_end(args);
201 exit(1);
204 void die(const char *fmt, ...)
206 va_list args;
207 static char buffer[512];
209 va_start(args, fmt);
210 vsnprintf(buffer, sizeof(buffer), fmt, args);
211 va_end(args);
213 fprintf(stderr, "%s\n", buffer);
214 exit(1);
217 static struct token *pre_buffer_begin = NULL;
218 static struct token *pre_buffer_end = NULL;
220 int Waddress_space = 1;
221 int Wbitwise = 0;
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 Werror = 0;
233 int Wnon_pointer_null = 1;
234 int Wold_initializer = 1;
235 int Wone_bit_signed_bitfield = 1;
236 int Wparen_string = 0;
237 int Wptr_subtraction_blows = 0;
238 int Wreturn_void = 0;
239 int Wshadow = 0;
240 int Wsizeof_bool = 0;
241 int Wtransparent_union = 0;
242 int Wtypesign = 0;
243 int Wundef = 0;
244 int Wuninitialized = 1;
245 int Wunknown_attribute = 0;
246 int Wvla = 1;
248 int dbg_entry = 0;
249 int dbg_dead = 0;
251 int preprocess_only;
253 static enum { STANDARD_C89,
254 STANDARD_C94,
255 STANDARD_C99,
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(*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_switch_m(char *arg, char **next)
380 if (!strcmp(arg, "m64")) {
381 arch_m64 = 1;
382 } else if (!strcmp(arg, "m32")) {
383 arch_m64 = 0;
384 } else if (!strcmp(arg, "msize-long")) {
385 arch_msize_long = 1;
387 return next;
390 static void handle_arch_m64_finalize(void)
392 if (arch_m64) {
393 bits_in_long = 64;
394 max_int_alignment = 8;
395 bits_in_pointer = 64;
396 pointer_alignment = 8;
397 size_t_ctype = &ulong_ctype;
398 ssize_t_ctype = &long_ctype;
399 #ifdef __x86_64__
400 add_pre_buffer("#weak_define __x86_64__ 1\n");
401 #endif
405 static void handle_arch_msize_long_finalize(void)
407 if (arch_msize_long) {
408 size_t_ctype = &ulong_ctype;
409 ssize_t_ctype = &long_ctype;
413 static void handle_arch_finalize(void)
415 handle_arch_m64_finalize();
416 handle_arch_msize_long_finalize();
420 static char **handle_switch_o(char *arg, char **next)
422 if (!strcmp (arg, "o")) { // "-o foo"
423 if (!*++next)
424 die("argument to '-o' is missing");
426 // else "-ofoo"
428 return next;
431 static const struct warning {
432 const char *name;
433 int *flag;
434 } warnings[] = {
435 { "address-space", &Waddress_space },
436 { "bitwise", &Wbitwise },
437 { "cast-to-as", &Wcast_to_as },
438 { "cast-truncate", &Wcast_truncate },
439 { "context", &Wcontext },
440 { "decl", &Wdecl },
441 { "declaration-after-statement", &Wdeclarationafterstatement },
442 { "default-bitfield-sign", &Wdefault_bitfield_sign },
443 { "designated-init", &Wdesignated_init },
444 { "do-while", &Wdo_while },
445 { "enum-mismatch", &Wenum_mismatch },
446 { "error", &Werror },
447 { "init-cstring", &Winit_cstring },
448 { "non-pointer-null", &Wnon_pointer_null },
449 { "old-initializer", &Wold_initializer },
450 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
451 { "paren-string", &Wparen_string },
452 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
453 { "return-void", &Wreturn_void },
454 { "shadow", &Wshadow },
455 { "sizeof-bool", &Wsizeof_bool },
456 { "transparent-union", &Wtransparent_union },
457 { "typesign", &Wtypesign },
458 { "undef", &Wundef },
459 { "uninitialized", &Wuninitialized },
460 { "unknown-attribute", &Wunknown_attribute },
461 { "vla", &Wvla },
464 enum {
465 WARNING_OFF,
466 WARNING_ON,
467 WARNING_FORCE_OFF
471 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
473 int flag = WARNING_ON;
474 char *p = arg + 1;
475 unsigned i;
477 if (!strcmp(p, "sparse-all")) {
478 for (i = 0; i < n; i++) {
479 if (*warnings[i].flag != WARNING_FORCE_OFF && warnings[i].flag != &Werror)
480 *warnings[i].flag = WARNING_ON;
484 // Prefixes "no" and "no-" mean to turn warning off.
485 if (p[0] == 'n' && p[1] == 'o') {
486 p += 2;
487 if (p[0] == '-')
488 p++;
489 flag = WARNING_FORCE_OFF;
492 for (i = 0; i < n; i++) {
493 if (!strcmp(p,warnings[i].name)) {
494 *warnings[i].flag = flag;
495 return next;
499 // Unknown.
500 return NULL;
503 static char **handle_switch_W(char *arg, char **next)
505 char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
506 if (ret)
507 return ret;
509 // Unknown.
510 return next;
513 static struct warning debugs[] = {
514 { "entry", &dbg_entry},
515 { "dead", &dbg_dead},
519 static char **handle_switch_v(char *arg, char **next)
521 char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
522 if (ret)
523 return ret;
525 // Unknown.
526 do {
527 verbose++;
528 } while (*++arg == 'v');
529 return next;
533 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
535 unsigned i;
537 for (i = 0; i < n; i++) {
538 if (*warnings[i].flag == WARNING_FORCE_OFF)
539 *warnings[i].flag = WARNING_OFF;
543 static void handle_switch_W_finalize(void)
545 handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
547 /* default Wdeclarationafterstatement based on the C dialect */
548 if (-1 == Wdeclarationafterstatement)
550 switch (standard)
552 case STANDARD_C89:
553 case STANDARD_C94:
554 Wdeclarationafterstatement = 1;
555 break;
557 case STANDARD_C99:
558 case STANDARD_GNU89:
559 case STANDARD_GNU99:
560 Wdeclarationafterstatement = 0;
561 break;
563 default:
564 assert (0);
570 static void handle_switch_v_finalize(void)
572 handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
575 static char **handle_switch_U(char *arg, char **next)
577 const char *name = arg + 1;
578 add_pre_buffer ("#undef %s\n", name);
579 return next;
582 static char **handle_switch_O(char *arg, char **next)
584 int level = 1;
585 if (arg[1] >= '0' && arg[1] <= '9')
586 level = arg[1] - '0';
587 optimize = level;
588 optimize_size = arg[1] == 's';
589 return next;
592 static char **handle_switch_ftabstop(char *arg, char **next)
594 char *end;
595 unsigned long val;
597 if (*arg == '\0')
598 die("error: missing argument to \"-ftabstop=\"");
600 /* we silently ignore silly values */
601 val = strtoul(arg, &end, 10);
602 if (*end == '\0' && 1 <= val && val <= 100)
603 tabstop = val;
605 return next;
608 static int funsigned_char;
609 static void handle_funsigned_char(void)
611 if (funsigned_char) {
612 char_ctype.ctype.modifiers &= ~MOD_SIGNED;
613 char_ctype.ctype.modifiers |= MOD_UNSIGNED;
617 static char **handle_switch_f(char *arg, char **next)
619 arg++;
621 if (!strncmp(arg, "tabstop=", 8))
622 return handle_switch_ftabstop(arg+8, next);
624 if (!strcmp(arg, "unsigned-char")) {
625 funsigned_char = 1;
626 return next;
629 /* handle switches w/ arguments above, boolean and only boolean below */
631 if (!strncmp(arg, "no-", 3)) {
632 arg += 3;
634 /* handle switch here.. */
635 return next;
638 static char **handle_switch_G(char *arg, char **next)
640 if (!strcmp (arg, "G") && *next)
641 return next + 1; // "-G 0"
642 else
643 return next; // "-G0" or (bogus) terminal "-G"
646 static char **handle_switch_a(char *arg, char **next)
648 if (!strcmp (arg, "ansi"))
649 standard = STANDARD_C89;
651 return next;
654 static char **handle_switch_s(char *arg, char **next)
656 if (!strncmp (arg, "std=", 4))
658 arg += 4;
660 if (!strcmp (arg, "c89") ||
661 !strcmp (arg, "iso9899:1990"))
662 standard = STANDARD_C89;
664 else if (!strcmp (arg, "iso9899:199409"))
665 standard = STANDARD_C94;
667 else if (!strcmp (arg, "c99") ||
668 !strcmp (arg, "c9x") ||
669 !strcmp (arg, "iso9899:1999") ||
670 !strcmp (arg, "iso9899:199x"))
671 standard = STANDARD_C99;
673 else if (!strcmp (arg, "gnu89"))
674 standard = STANDARD_GNU89;
676 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
677 standard = STANDARD_GNU99;
679 else
680 die ("Unsupported C dialect");
683 return next;
686 static char **handle_nostdinc(char *arg, char **next)
688 add_pre_buffer("#nostdinc\n");
689 return next;
692 static char **handle_switch_n(char *arg, char **next)
694 if (!strcmp (arg, "nostdinc"))
695 return handle_nostdinc(arg, next);
697 return next;
700 static char **handle_base_dir(char *arg, char **next)
702 gcc_base_dir = *++next;
703 if (!gcc_base_dir)
704 die("missing argument for -gcc-base-dir option");
705 return next;
708 static char **handle_no_lineno(char *arg, char **next)
710 no_lineno = 1;
711 return next;
714 static char **handle_switch_g(char *arg, char **next)
716 if (!strcmp (arg, "gcc-base-dir"))
717 return handle_base_dir(arg, next);
719 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(*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 { "nostdinc", handle_nostdinc },
757 { "gcc-base-dir", handle_base_dir},
758 { "no-lineno", handle_no_lineno},
759 { NULL, NULL }
761 struct switches *s = cmd;
763 while (s->name) {
764 int optlen = strlen(s->name);
765 if (!strncmp(s->name, arg, optlen + !s->prefix))
766 return s->fn(arg + optlen, next);
767 s++;
769 return next;
772 static char **handle_switch(char *arg, char **next)
774 switch (*arg) {
775 case 'a': return handle_switch_a(arg, next);
776 case 'D': return handle_switch_D(arg, next);
777 case 'E': return handle_switch_E(arg, next);
778 case 'f': return handle_switch_f(arg, next);
779 case 'g': return handle_switch_g(arg, next);
780 case 'G': return handle_switch_G(arg, next);
781 case 'I': return handle_switch_I(arg, next);
782 case 'i': return handle_switch_i(arg, next);
783 case 'M': return handle_switch_M(arg, next);
784 case 'm': return handle_switch_m(arg, next);
785 case 'n': return handle_switch_n(arg, next);
786 case 'o': return handle_switch_o(arg, next);
787 case 'O': return handle_switch_O(arg, next);
788 case 's': return handle_switch_s(arg, next);
789 case 'U': return handle_switch_U(arg, next);
790 case 'v': return handle_switch_v(arg, next);
791 case 'W': return handle_switch_W(arg, next);
792 case '-': return handle_long_options(arg + 1, next);
793 default:
794 break;
798 * Ignore unknown command line options:
799 * they're probably gcc switches
801 return next;
804 void declare_builtin_functions(void)
806 /* Gaah. gcc knows tons of builtin <string.h> functions */
807 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
808 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
809 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
810 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
811 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
812 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
813 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
814 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
815 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
816 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
817 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
818 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
819 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
820 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
821 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
823 /* And bitwise operations.. */
824 add_pre_buffer("extern int __builtin_clz(int);\n");
825 add_pre_buffer("extern int __builtin_clzl(long);\n");
826 add_pre_buffer("extern int __builtin_clzll(long long);\n");
827 add_pre_buffer("extern int __builtin_ctz(int);\n");
828 add_pre_buffer("extern int __builtin_ctzl(long);\n");
829 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
830 add_pre_buffer("extern int __builtin_ffs(int);\n");
831 add_pre_buffer("extern int __builtin_ffsl(long);\n");
832 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
833 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
834 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
835 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
837 /* And byte swaps.. */
838 add_pre_buffer("extern unsigned short __builtin_bswap16(unsigned short);\n");
839 add_pre_buffer("extern unsigned int __builtin_bswap32(unsigned int);\n");
840 add_pre_buffer("extern unsigned long long __builtin_bswap64(unsigned long long);\n");
842 /* And atomic memory access functions.. */
843 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
844 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
845 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
846 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
847 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
848 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
849 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
850 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
851 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
852 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
853 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
854 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
855 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
856 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
857 add_pre_buffer("extern void __sync_synchronize();\n");
858 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
859 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
861 /* And some random ones.. */
862 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
863 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
864 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
865 add_pre_buffer("extern void __builtin_trap(void);\n");
866 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
867 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
868 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
869 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
870 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
871 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
872 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
873 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
874 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
875 add_pre_buffer("extern long __builtin_labs(long);\n");
876 add_pre_buffer("extern double __builtin_fabs(double);\n");
877 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
879 /* Add Blackfin-specific stuff */
880 add_pre_buffer(
881 "#ifdef __bfin__\n"
882 "extern void __builtin_bfin_csync(void);\n"
883 "extern void __builtin_bfin_ssync(void);\n"
884 "extern int __builtin_bfin_norm_fr1x32(int);\n"
885 "#endif\n"
888 /* And some floating point stuff.. */
889 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
890 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
891 add_pre_buffer("extern int __builtin_isless(float, float);\n");
892 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
893 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
894 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
896 /* And some __FORTIFY_SOURCE ones.. */
897 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
898 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
899 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
900 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
901 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
902 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
903 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
904 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
905 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
906 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
907 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
908 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
909 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
910 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
911 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
914 void create_builtin_stream(void)
916 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
917 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
918 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
920 /* We add compiler headers path here because we have to parse
921 * the arguments to get it, falling back to default. */
922 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
923 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
925 add_pre_buffer("#define __extension__\n");
926 add_pre_buffer("#define __pragma__\n");
928 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
929 // solaris/sparc that is really "unsigned int" and for linux/x86_64
930 // it is "long unsigned int". In either case we can probably
931 // get away with this. We need the #weak_define as cgcc will define
932 // the right __SIZE_TYPE__.
933 if (size_t_ctype == &ulong_ctype)
934 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
935 else
936 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
937 add_pre_buffer("#weak_define __STDC__ 1\n");
939 switch (standard)
941 case STANDARD_C89:
942 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
943 break;
945 case STANDARD_C94:
946 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
947 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
948 break;
950 case STANDARD_C99:
951 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
952 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
953 break;
955 case STANDARD_GNU89:
956 break;
958 case STANDARD_GNU99:
959 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
960 break;
962 default:
963 assert (0);
966 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
967 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
968 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
969 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
970 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
971 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
972 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
973 add_pre_buffer("#define __builtin_va_end(arg)\n");
974 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
975 add_pre_buffer("#define __builtin_va_arg_pack()\n");
977 /* FIXME! We need to do these as special magic macros at expansion time! */
978 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
980 if (optimize)
981 add_pre_buffer("#define __OPTIMIZE__ 1\n");
982 if (optimize_size)
983 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
985 /* GCC defines these for limits.h */
986 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__) "\n");
987 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__) "\n");
988 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__) "\n");
989 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__) "\n");
990 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__) "\n");
991 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__) "\n");
992 add_pre_buffer("#weak_define __SIZEOF_POINTER__ " STRINGIFY(__SIZEOF_POINTER__) "\n");
993 add_pre_buffer("#weak_define __CHAR_BIT__ " STRINGIFY(__CHAR_BIT__) "\n");
996 static struct symbol_list *sparse_tokenstream(struct token *token)
998 // Preprocess the stream
999 token = preprocess(token);
1001 if (preprocess_only) {
1002 while (!eof_token(token)) {
1003 int prec = 1;
1004 struct token *next = token->next;
1005 const char *separator = "";
1006 if (next->pos.whitespace)
1007 separator = " ";
1008 if (next->pos.newline) {
1009 separator = "\n\t\t\t\t\t";
1010 prec = next->pos.pos;
1011 if (prec > 4)
1012 prec = 4;
1014 printf("%s%.*s", show_token(token), prec, separator);
1015 token = next;
1017 putchar('\n');
1019 return NULL;
1022 // Parse the resulting C code
1023 while (!eof_token(token))
1024 token = external_declaration(token, &translation_unit_used_list);
1025 return translation_unit_used_list;
1028 static struct symbol_list *sparse_file(const char *filename)
1030 int fd;
1031 struct token *token;
1033 if (strcmp (filename, "-") == 0) {
1034 fd = 0;
1035 } else {
1036 fd = open(filename, O_RDONLY);
1037 if (fd < 0)
1038 die("No such file: %s", filename);
1041 // Tokenize the input stream
1042 token = tokenize(filename, fd, NULL, includepath);
1043 store_all_tokens(token);
1044 close(fd);
1046 return sparse_tokenstream(token);
1050 * This handles the "-include" directive etc: we're in global
1051 * scope, and all types/macros etc will affect all the following
1052 * files.
1054 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1055 * affect all subsequent files too, i.e. we can have non-local
1056 * behaviour between files!
1058 static struct symbol_list *sparse_initial(void)
1060 int i;
1062 // Prepend any "include" file to the stream.
1063 // We're in global scope, it will affect all files!
1064 for (i = 0; i < cmdline_include_nr; i++)
1065 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]);
1067 return sparse_tokenstream(pre_buffer_begin);
1070 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
1072 char **args;
1073 struct symbol_list *list;
1075 // Initialize symbol stream first, so that we can add defines etc
1076 init_symbols();
1077 init_include_path();
1079 args = argv;
1080 for (;;) {
1081 char *arg = *++args;
1082 if (!arg)
1083 break;
1085 if (arg[0] == '-' && arg[1]) {
1086 args = handle_switch(arg+1, args);
1087 continue;
1089 add_ptr_list_notag(filelist, arg);
1091 handle_switch_W_finalize();
1092 handle_switch_v_finalize();
1094 handle_arch_finalize();
1096 list = NULL;
1097 if (!ptr_list_empty(filelist)) {
1098 // Initialize type system
1099 init_ctype();
1100 handle_funsigned_char();
1102 create_builtin_stream();
1103 add_pre_buffer("#define __CHECKER__ 1\n");
1104 if (!preprocess_only)
1105 declare_builtin_functions();
1107 list = sparse_initial();
1110 * Protect the initial token allocations, since
1111 * they need to survive all the others
1113 protect_token_alloc();
1115 return list;
1118 struct symbol_list * sparse_keep_tokens(char *filename)
1120 struct symbol_list *res;
1122 /* Clear previous symbol list */
1123 translation_unit_used_list = NULL;
1125 new_file_scope();
1126 res = sparse_file(filename);
1128 /* And return it */
1129 return res;
1133 struct symbol_list * __sparse(char *filename)
1135 struct symbol_list *res;
1137 res = sparse_keep_tokens(filename);
1139 /* Drop the tokens for this file after parsing */
1140 clear_token_alloc();
1142 /* And return it */
1143 return res;
1146 struct symbol_list * sparse(char *filename)
1148 struct symbol_list *res = __sparse(filename);
1150 /* Evaluate the complete symbol list */
1151 evaluate_symbol_list(res);
1153 return res;