db: caller info needs to record the -1 parameters
[smatch.git] / lib.c
blobef7653126625c44b7c00e74a45a6167ad8c8f423
1 /*
2 * 'sparse' library helper routines.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
8 */
9 #include <ctype.h>
10 #include <fcntl.h>
11 #include <stdarg.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <assert.h>
19 #include <sys/types.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "token.h"
24 #include "parse.h"
25 #include "symbol.h"
26 #include "expression.h"
27 #include "scope.h"
28 #include "linearize.h"
29 #include "target.h"
31 int verbose, optimize, optimize_size, preprocessing;
32 int die_if_error = 0;
34 #ifndef __GNUC__
35 # define __GNUC__ 2
36 # define __GNUC_MINOR__ 95
37 # define __GNUC_PATCHLEVEL__ 0
38 #endif
40 int gcc_major = __GNUC__;
41 int gcc_minor = __GNUC_MINOR__;
42 int gcc_patchlevel = __GNUC_PATCHLEVEL__;
44 static const char *gcc_base_dir = GCC_BASE;
46 struct token *skip_to(struct token *token, int op)
48 while (!match_op(token, op) && !eof_token(token))
49 token = token->next;
50 return token;
53 struct token *expect(struct token *token, int op, const char *where)
55 if (!match_op(token, op)) {
56 static struct token bad_token;
57 if (token != &bad_token) {
58 bad_token.next = token;
59 sparse_error(token->pos, "Expected %s %s", show_special(op), where);
60 sparse_error(token->pos, "got %s", show_token(token));
62 if (op == ';')
63 return skip_to(token, op);
64 return &bad_token;
66 return token->next;
69 unsigned int hexval(unsigned int c)
71 int retval = 256;
72 switch (c) {
73 case '0'...'9':
74 retval = c - '0';
75 break;
76 case 'a'...'f':
77 retval = c - 'a' + 10;
78 break;
79 case 'A'...'F':
80 retval = c - 'A' + 10;
81 break;
83 return retval;
86 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
88 static char buffer[512];
89 const char *name;
91 vsprintf(buffer, fmt, args);
92 name = stream_name(pos.stream);
94 fprintf(stderr, "%s:%d:%d: %s%s\n",
95 name, pos.line, pos.pos, type, buffer);
98 static int max_warnings = 100;
99 static int show_info = 1;
101 void info(struct position pos, const char * fmt, ...)
103 va_list args;
105 if (!show_info)
106 return;
107 va_start(args, fmt);
108 do_warn("", pos, fmt, args);
109 va_end(args);
112 void warning(struct position pos, const char * fmt, ...)
114 va_list args;
116 if (!max_warnings) {
117 show_info = 0;
118 return;
121 if (!--max_warnings) {
122 show_info = 0;
123 fmt = "too many warnings";
126 va_start(args, fmt);
127 do_warn("warning: ", pos, fmt, args);
128 va_end(args);
131 static void do_error(struct position pos, const char * fmt, va_list args)
133 static int errors = 0;
134 die_if_error = 1;
135 show_info = 1;
136 /* Shut up warnings after an error */
137 max_warnings = 0;
138 if (errors > 100) {
139 static int once = 0;
140 show_info = 0;
141 if (once)
142 return;
143 fmt = "too many errors";
144 once = 1;
147 do_warn("error: ", pos, fmt, args);
148 errors++;
151 void sparse_error(struct position pos, const char * fmt, ...)
153 va_list args;
154 va_start(args, fmt);
155 do_error(pos, fmt, args);
156 va_end(args);
159 void expression_error(struct expression *expr, const char *fmt, ...)
161 va_list args;
162 va_start(args, fmt);
163 do_error(expr->pos, fmt, args);
164 va_end(args);
165 expr->ctype = &bad_ctype;
168 void error_die(struct position pos, const char * fmt, ...)
170 va_list args;
171 va_start(args, fmt);
172 do_warn("error: ", pos, fmt, args);
173 va_end(args);
174 exit(1);
177 void die(const char *fmt, ...)
179 va_list args;
180 static char buffer[512];
182 va_start(args, fmt);
183 vsnprintf(buffer, sizeof(buffer), fmt, args);
184 va_end(args);
186 fprintf(stderr, "%s\n", buffer);
187 exit(1);
190 static struct token *pre_buffer_begin = NULL;
191 static struct token *pre_buffer_end = NULL;
193 int Waddress_space = 1;
194 int Wbitwise = 0;
195 int Wcast_to_as = 0;
196 int Wcast_truncate = 1;
197 int Wcontext = 1;
198 int Wdecl = 1;
199 int Wdefault_bitfield_sign = 0;
200 int Wdesignated_init = 1;
201 int Wdo_while = 0;
202 int Wenum_mismatch = 1;
203 int Wnon_pointer_null = 1;
204 int Wold_initializer = 1;
205 int Wone_bit_signed_bitfield = 1;
206 int Wparen_string = 0;
207 int Wptr_subtraction_blows = 0;
208 int Wreturn_void = 0;
209 int Wshadow = 0;
210 int Wtransparent_union = 0;
211 int Wtypesign = 0;
212 int Wundef = 0;
213 int Wuninitialized = 1;
214 int Wdeclarationafterstatement = -1;
216 int dbg_entry = 0;
217 int dbg_dead = 0;
219 int preprocess_only;
221 static enum { STANDARD_C89,
222 STANDARD_C94,
223 STANDARD_C99,
224 STANDARD_GNU89,
225 STANDARD_GNU99, } standard = STANDARD_GNU89;
227 #define CMDLINE_INCLUDE 20
228 int cmdline_include_nr = 0;
229 struct cmdline_include cmdline_include[CMDLINE_INCLUDE];
232 void add_pre_buffer(const char *fmt, ...)
234 va_list args;
235 unsigned int size;
236 struct token *begin, *end;
237 char buffer[4096];
239 va_start(args, fmt);
240 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
241 va_end(args);
242 begin = tokenize_buffer(buffer, size, &end);
243 if (!pre_buffer_begin)
244 pre_buffer_begin = begin;
245 if (pre_buffer_end)
246 pre_buffer_end->next = begin;
247 pre_buffer_end = end;
250 static char **handle_switch_D(char *arg, char **next)
252 const char *name = arg + 1;
253 const char *value = "1";
255 if (!*name || isspace(*name))
256 die("argument to `-D' is missing");
258 for (;;) {
259 char c;
260 c = *++arg;
261 if (!c)
262 break;
263 if (isspace((unsigned char)c) || c == '=') {
264 *arg = '\0';
265 value = arg + 1;
266 break;
269 add_pre_buffer("#define %s %s\n", name, value);
270 return next;
273 static char **handle_switch_E(char *arg, char **next)
275 if (arg[1] == '\0')
276 preprocess_only = 1;
277 return next;
280 static char **handle_switch_I(char *arg, char **next)
282 char *path = arg+1;
284 switch (arg[1]) {
285 case '-':
286 add_pre_buffer("#split_include\n");
287 break;
289 case '\0': /* Plain "-I" */
290 path = *++next;
291 if (!path)
292 die("missing argument for -I option");
293 /* Fall through */
294 default:
295 add_pre_buffer("#add_include \"%s/\"\n", path);
297 return next;
300 static void add_cmdline_include(char *filename)
302 int fd = open(filename, O_RDONLY);
303 if (fd < 0) {
304 perror(filename);
305 return;
307 if (cmdline_include_nr >= CMDLINE_INCLUDE)
308 die("too many include files for %s\n", filename);
309 cmdline_include[cmdline_include_nr].filename = filename;
310 cmdline_include[cmdline_include_nr].fd = fd;
311 cmdline_include_nr++;
314 static char **handle_switch_i(char *arg, char **next)
316 if (*next && !strcmp(arg, "include"))
317 add_cmdline_include(*++next);
318 else if (*next && !strcmp(arg, "imacros"))
319 add_cmdline_include(*++next);
320 else if (*next && !strcmp(arg, "isystem")) {
321 char *path = *++next;
322 if (!path)
323 die("missing argument for -isystem option");
324 add_pre_buffer("#add_isystem \"%s/\"\n", path);
325 } else if (*next && !strcmp(arg, "idirafter")) {
326 char *path = *++next;
327 if (!path)
328 die("missing argument for -idirafter option");
329 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
331 return next;
334 static char **handle_switch_M(char *arg, char **next)
336 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
337 if (!*next)
338 die("missing argument for -%s option", arg);
339 return next + 1;
341 return next;
344 static char **handle_switch_m(char *arg, char **next)
346 if (!strcmp(arg, "m64")) {
347 bits_in_long = 64;
348 max_int_alignment = 8;
349 bits_in_pointer = 64;
350 pointer_alignment = 8;
351 size_t_ctype = &ulong_ctype;
352 ssize_t_ctype = &long_ctype;
353 } else if (!strcmp(arg, "msize-long")) {
354 size_t_ctype = &ulong_ctype;
355 ssize_t_ctype = &long_ctype;
357 return next;
360 static char **handle_switch_o(char *arg, char **next)
362 if (!strcmp (arg, "o")) { // "-o foo"
363 if (!*++next)
364 die("argument to '-o' is missing");
366 // else "-ofoo"
368 return next;
371 static const struct warning {
372 const char *name;
373 int *flag;
374 } warnings[] = {
375 { "address-space", &Waddress_space },
376 { "bitwise", &Wbitwise },
377 { "cast-to-as", &Wcast_to_as },
378 { "cast-truncate", &Wcast_truncate },
379 { "context", &Wcontext },
380 { "decl", &Wdecl },
381 { "default-bitfield-sign", &Wdefault_bitfield_sign },
382 { "designated-init", &Wdesignated_init },
383 { "do-while", &Wdo_while },
384 { "enum-mismatch", &Wenum_mismatch },
385 { "non-pointer-null", &Wnon_pointer_null },
386 { "old-initializer", &Wold_initializer },
387 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
388 { "paren-string", &Wparen_string },
389 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
390 { "return-void", &Wreturn_void },
391 { "shadow", &Wshadow },
392 { "transparent-union", &Wtransparent_union },
393 { "typesign", &Wtypesign },
394 { "undef", &Wundef },
395 { "uninitialized", &Wuninitialized },
396 { "declaration-after-statement", &Wdeclarationafterstatement },
399 enum {
400 WARNING_OFF,
401 WARNING_ON,
402 WARNING_FORCE_OFF
406 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
408 int flag = WARNING_ON;
409 char *p = arg + 1;
410 unsigned i;
412 if (!strcmp(p, "sparse-all")) {
413 for (i = 0; i < n; i++) {
414 if (*warnings[i].flag != WARNING_FORCE_OFF)
415 *warnings[i].flag = WARNING_ON;
419 // Prefixes "no" and "no-" mean to turn warning off.
420 if (p[0] == 'n' && p[1] == 'o') {
421 p += 2;
422 if (p[0] == '-')
423 p++;
424 flag = WARNING_FORCE_OFF;
427 for (i = 0; i < n; i++) {
428 if (!strcmp(p,warnings[i].name)) {
429 *warnings[i].flag = flag;
430 return next;
434 // Unknown.
435 return NULL;
438 static char **handle_switch_W(char *arg, char **next)
440 char ** ret = handle_onoff_switch(arg, next, warnings, sizeof warnings/sizeof warnings[0]);
441 if (ret)
442 return ret;
444 // Unknown.
445 return next;
448 static struct warning debugs[] = {
449 { "entry", &dbg_entry},
450 { "dead", &dbg_dead},
454 static char **handle_switch_v(char *arg, char **next)
456 char ** ret = handle_onoff_switch(arg, next, debugs, sizeof debugs/sizeof debugs[0]);
457 if (ret)
458 return ret;
460 // Unknown.
461 do {
462 verbose++;
463 } while (*++arg == 'v');
464 return next;
468 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
470 unsigned i;
472 for (i = 0; i < n; i++) {
473 if (*warnings[i].flag == WARNING_FORCE_OFF)
474 *warnings[i].flag = WARNING_OFF;
478 static void handle_switch_W_finalize(void)
480 handle_onoff_switch_finalize(warnings, sizeof(warnings) / sizeof(warnings[0]));
482 /* default Wdeclarationafterstatement based on the C dialect */
483 if (-1 == Wdeclarationafterstatement)
485 switch (standard)
487 case STANDARD_C89:
488 case STANDARD_C94:
489 Wdeclarationafterstatement = 1;
490 break;
492 case STANDARD_C99:
493 case STANDARD_GNU89:
494 case STANDARD_GNU99:
495 Wdeclarationafterstatement = 0;
496 break;
498 default:
499 assert (0);
505 static void handle_switch_v_finalize(void)
507 handle_onoff_switch_finalize(debugs, sizeof(debugs) / sizeof(debugs[0]));
510 static char **handle_switch_U(char *arg, char **next)
512 const char *name = arg + 1;
513 add_pre_buffer ("#undef %s\n", name);
514 return next;
517 static char **handle_switch_O(char *arg, char **next)
519 int level = 1;
520 if (arg[1] >= '0' && arg[1] <= '9')
521 level = arg[1] - '0';
522 optimize = level;
523 optimize_size = arg[1] == 's';
524 return next;
527 static char **handle_switch_ftabstop(char *arg, char **next)
529 char *end;
530 unsigned long val;
532 if (*arg == '\0')
533 die("error: missing argument to \"-ftabstop=\"");
535 /* we silently ignore silly values */
536 val = strtoul(arg, &end, 10);
537 if (*end == '\0' && 1 <= val && val <= 100)
538 tabstop = val;
540 return next;
543 static char **handle_switch_f(char *arg, char **next)
545 int flag = 1;
547 arg++;
549 if (!strncmp(arg, "tabstop=", 8))
550 return handle_switch_ftabstop(arg+8, next);
552 /* handle switches w/ arguments above, boolean and only boolean below */
554 if (!strncmp(arg, "no-", 3)) {
555 flag = 0;
556 arg += 3;
558 /* handle switch here.. */
559 return next;
562 static char **handle_switch_G(char *arg, char **next)
564 if (!strcmp (arg, "G") && *next)
565 return next + 1; // "-G 0"
566 else
567 return next; // "-G0" or (bogus) terminal "-G"
570 static char **handle_switch_a(char *arg, char **next)
572 if (!strcmp (arg, "ansi"))
573 standard = STANDARD_C89;
575 return next;
578 static char **handle_switch_s(char *arg, char **next)
580 if (!strncmp (arg, "std=", 4))
582 arg += 4;
584 if (!strcmp (arg, "c89") ||
585 !strcmp (arg, "iso9899:1990"))
586 standard = STANDARD_C89;
588 else if (!strcmp (arg, "iso9899:199409"))
589 standard = STANDARD_C94;
591 else if (!strcmp (arg, "c99") ||
592 !strcmp (arg, "c9x") ||
593 !strcmp (arg, "iso9899:1999") ||
594 !strcmp (arg, "iso9899:199x"))
595 standard = STANDARD_C99;
597 else if (!strcmp (arg, "gnu89"))
598 standard = STANDARD_GNU89;
600 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
601 standard = STANDARD_GNU99;
603 else
604 die ("Unsupported C dialect");
607 return next;
610 static char **handle_nostdinc(char *arg, char **next)
612 add_pre_buffer("#nostdinc\n");
613 return next;
616 static char **handle_base_dir(char *arg, char **next)
618 gcc_base_dir = *++next;
619 if (!gcc_base_dir)
620 die("missing argument for -gcc-base-dir option");
621 return next;
624 static char **handle_no_lineno(char *arg, char **next)
626 no_lineno = 1;
627 return next;
630 struct switches {
631 const char *name;
632 char **(*fn)(char *, char **);
635 static char **handle_switch(char *arg, char **next)
637 static struct switches cmd[] = {
638 { "nostdinc", handle_nostdinc },
639 { "gcc-base-dir", handle_base_dir},
640 { "no-lineno", handle_no_lineno},
641 { NULL, NULL }
643 struct switches *s;
645 switch (*arg) {
646 case 'D': return handle_switch_D(arg, next);
647 case 'E': return handle_switch_E(arg, next);
648 case 'I': return handle_switch_I(arg, next);
649 case 'i': return handle_switch_i(arg, next);
650 case 'M': return handle_switch_M(arg, next);
651 case 'm': return handle_switch_m(arg, next);
652 case 'o': return handle_switch_o(arg, next);
653 case 'U': return handle_switch_U(arg, next);
654 case 'v': return handle_switch_v(arg, next);
655 case 'W': return handle_switch_W(arg, next);
656 case 'O': return handle_switch_O(arg, next);
657 case 'f': return handle_switch_f(arg, next);
658 case 'G': return handle_switch_G(arg, next);
659 case 'a': return handle_switch_a(arg, next);
660 case 's': return handle_switch_s(arg, next);
661 default:
662 break;
665 s = cmd;
666 while (s->name) {
667 if (!strcmp(s->name, arg))
668 return s->fn(arg, next);
669 s++;
673 * Ignore unknown command line options:
674 * they're probably gcc switches
676 return next;
679 void declare_builtin_functions(void)
681 /* Gaah. gcc knows tons of builtin <string.h> functions */
682 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
683 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
684 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
685 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
686 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
687 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
688 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
689 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
690 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
691 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
692 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
693 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
694 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
695 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
697 /* And bitwise operations.. */
698 add_pre_buffer("extern int __builtin_clz(int);\n");
699 add_pre_buffer("extern int __builtin_clzl(long);\n");
700 add_pre_buffer("extern int __builtin_clzll(long long);\n");
701 add_pre_buffer("extern int __builtin_ctz(int);\n");
702 add_pre_buffer("extern int __builtin_ctzl(long);\n");
703 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
704 add_pre_buffer("extern int __builtin_ffs(int);\n");
705 add_pre_buffer("extern int __builtin_ffsl(long);\n");
706 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
707 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
708 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
709 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
711 /* And some random ones.. */
712 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
713 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
714 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
715 add_pre_buffer("extern void __builtin_trap(void);\n");
716 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
717 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
718 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
719 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
720 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
721 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
722 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
723 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
724 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
725 add_pre_buffer("extern long __builtin_labs(long);\n");
726 add_pre_buffer("extern double __builtin_fabs(double);\n");
728 /* Add Blackfin-specific stuff */
729 add_pre_buffer(
730 "#ifdef __bfin__\n"
731 "extern void __builtin_bfin_csync(void);\n"
732 "extern void __builtin_bfin_ssync(void);\n"
733 "extern int __builtin_bfin_norm_fr1x32(int);\n"
734 "#endif\n"
737 /* And some floating point stuff.. */
738 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
739 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
740 add_pre_buffer("extern int __builtin_isless(float, float);\n");
741 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
742 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
743 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
745 /* And some __FORTIFY_SOURCE ones.. */
746 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
747 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
748 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
749 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
750 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
751 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
752 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
753 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
754 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
755 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
756 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
757 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
758 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
759 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
760 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
763 void create_builtin_stream(void)
765 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
766 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
767 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
769 /* We add compiler headers path here because we have to parse
770 * the arguments to get it, falling back to default. */
771 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
772 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
774 add_pre_buffer("#define __extension__\n");
775 add_pre_buffer("#define __pragma__\n");
777 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
778 // solaris/sparc that is really "unsigned int" and for linux/x86_64
779 // it is "long unsigned int". In either case we can probably
780 // get away with this. We need the #weak_define as cgcc will define
781 // the right __SIZE_TYPE__.
782 if (size_t_ctype == &ulong_ctype)
783 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
784 else
785 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
786 add_pre_buffer("#weak_define __STDC__ 1\n");
788 switch (standard)
790 case STANDARD_C89:
791 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
792 break;
794 case STANDARD_C94:
795 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
796 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
797 break;
799 case STANDARD_C99:
800 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
801 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
802 break;
804 case STANDARD_GNU89:
805 break;
807 case STANDARD_GNU99:
808 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
809 break;
811 default:
812 assert (0);
815 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
816 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
817 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
818 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
819 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
820 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
821 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
822 add_pre_buffer("#define __builtin_va_end(arg)\n");
823 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
825 /* FIXME! We need to do these as special magic macros at expansion time! */
826 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
828 if (optimize)
829 add_pre_buffer("#define __OPTIMIZE__ 1\n");
830 if (optimize_size)
831 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
833 /* GCC defines these for limits.h */
834 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__) "\n");
835 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__) "\n");
836 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__) "\n");
837 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__) "\n");
838 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__) "\n");
839 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__) "\n");
842 static struct symbol_list *sparse_tokenstream(struct token *token)
844 // Preprocess the stream
845 token = preprocess(token);
847 if (preprocess_only) {
848 while (!eof_token(token)) {
849 int prec = 1;
850 struct token *next = token->next;
851 const char *separator = "";
852 if (next->pos.whitespace)
853 separator = " ";
854 if (next->pos.newline) {
855 separator = "\n\t\t\t\t\t";
856 prec = next->pos.pos;
857 if (prec > 4)
858 prec = 4;
860 printf("%s%.*s", show_token(token), prec, separator);
861 token = next;
863 putchar('\n');
865 return NULL;
868 // Parse the resulting C code
869 while (!eof_token(token))
870 token = external_declaration(token, &translation_unit_used_list);
871 return translation_unit_used_list;
874 static struct symbol_list *sparse_file(const char *filename)
876 int fd;
877 struct token *token;
879 if (strcmp (filename, "-") == 0) {
880 fd = 0;
881 } else {
882 fd = open(filename, O_RDONLY);
883 if (fd < 0)
884 die("No such file: %s", filename);
887 // Tokenize the input stream
888 token = tokenize(filename, fd, NULL, includepath);
889 close(fd);
891 return sparse_tokenstream(token);
895 * This handles the "-include" directive etc: we're in global
896 * scope, and all types/macros etc will affect all the following
897 * files.
899 * NOTE NOTE NOTE! "#undef" of anything in this stage will
900 * affect all subsequent files too, i.e. we can have non-local
901 * behaviour between files!
903 static struct symbol_list *sparse_initial(void)
905 struct token *token;
906 int i;
908 // Prepend any "include" file to the stream.
909 // We're in global scope, it will affect all files!
910 token = NULL;
911 for (i = cmdline_include_nr - 1; i >= 0; i--)
912 token = tokenize(cmdline_include[i].filename, cmdline_include[i].fd,
913 token, includepath);
915 // Prepend the initial built-in stream
916 if (token)
917 pre_buffer_end->next = token;
918 return sparse_tokenstream(pre_buffer_begin);
921 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
923 char **args;
924 struct symbol_list *list;
926 // Initialize symbol stream first, so that we can add defines etc
927 init_symbols();
929 args = argv;
930 for (;;) {
931 char *arg = *++args;
932 if (!arg)
933 break;
935 if (arg[0] == '-' && arg[1]) {
936 args = handle_switch(arg+1, args);
937 continue;
939 add_ptr_list_notag(filelist, arg);
941 handle_switch_W_finalize();
942 handle_switch_v_finalize();
944 list = NULL;
945 if (!ptr_list_empty(filelist)) {
946 // Initialize type system
947 init_ctype();
949 create_builtin_stream();
950 add_pre_buffer("#define __CHECKER__ 1\n");
951 if (!preprocess_only)
952 declare_builtin_functions();
954 list = sparse_initial();
957 * Protect the initial token allocations, since
958 * they need to survive all the others
960 protect_token_alloc();
962 return list;
965 struct symbol_list * sparse_keep_tokens(char *filename)
967 struct symbol_list *res;
969 /* Clear previous symbol list */
970 translation_unit_used_list = NULL;
972 new_file_scope();
973 res = sparse_file(filename);
975 /* And return it */
976 return res;
980 struct symbol_list * __sparse(char *filename)
982 struct symbol_list *res;
984 res = sparse_keep_tokens(filename);
986 /* Drop the tokens for this file after parsing */
987 clear_token_alloc();
989 /* And return it */
990 return res;
993 struct symbol_list * sparse(char *filename)
995 struct symbol_list *res = __sparse(filename);
997 /* Evaluate the complete symbol list */
998 evaluate_symbol_list(res);
1000 return res;