sparse: Add GCC pre-defined macros for user-space
[smatch.git] / lib.c
blob622b54761b9aa2e303b3f7ffdb69e35dbb6c26db
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 Wdo_while = 0;
201 int Wenum_mismatch = 1;
202 int Wnon_pointer_null = 1;
203 int Wold_initializer = 1;
204 int Wone_bit_signed_bitfield = 1;
205 int Wparen_string = 0;
206 int Wptr_subtraction_blows = 0;
207 int Wreturn_void = 0;
208 int Wshadow = 0;
209 int Wtransparent_union = 0;
210 int Wtypesign = 0;
211 int Wundef = 0;
212 int Wuninitialized = 1;
213 int Wdeclarationafterstatement = -1;
215 int dbg_entry = 0;
216 int dbg_dead = 0;
218 int preprocess_only;
220 static enum { STANDARD_C89,
221 STANDARD_C94,
222 STANDARD_C99,
223 STANDARD_GNU89,
224 STANDARD_GNU99, } standard = STANDARD_GNU89;
226 #define CMDLINE_INCLUDE 20
227 int cmdline_include_nr = 0;
228 struct cmdline_include cmdline_include[CMDLINE_INCLUDE];
231 void add_pre_buffer(const char *fmt, ...)
233 va_list args;
234 unsigned int size;
235 struct token *begin, *end;
236 char buffer[4096];
238 va_start(args, fmt);
239 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
240 va_end(args);
241 begin = tokenize_buffer(buffer, size, &end);
242 if (!pre_buffer_begin)
243 pre_buffer_begin = begin;
244 if (pre_buffer_end)
245 pre_buffer_end->next = begin;
246 pre_buffer_end = end;
249 static char **handle_switch_D(char *arg, char **next)
251 const char *name = arg + 1;
252 const char *value = "1";
254 if (!*name || isspace(*name))
255 die("argument to `-D' is missing");
257 for (;;) {
258 char c;
259 c = *++arg;
260 if (!c)
261 break;
262 if (isspace((unsigned char)c) || c == '=') {
263 *arg = '\0';
264 value = arg + 1;
265 break;
268 add_pre_buffer("#define %s %s\n", name, value);
269 return next;
272 static char **handle_switch_E(char *arg, char **next)
274 if (arg[1] == '\0')
275 preprocess_only = 1;
276 return next;
279 static char **handle_switch_I(char *arg, char **next)
281 char *path = arg+1;
283 switch (arg[1]) {
284 case '-':
285 add_pre_buffer("#split_include\n");
286 break;
288 case '\0': /* Plain "-I" */
289 path = *++next;
290 if (!path)
291 die("missing argument for -I option");
292 /* Fall through */
293 default:
294 add_pre_buffer("#add_include \"%s/\"\n", path);
296 return next;
299 static void add_cmdline_include(char *filename)
301 int fd = open(filename, O_RDONLY);
302 if (fd < 0) {
303 perror(filename);
304 return;
306 if (cmdline_include_nr >= CMDLINE_INCLUDE)
307 die("too many include files for %s\n", filename);
308 cmdline_include[cmdline_include_nr].filename = filename;
309 cmdline_include[cmdline_include_nr].fd = fd;
310 cmdline_include_nr++;
313 static char **handle_switch_i(char *arg, char **next)
315 if (*next && !strcmp(arg, "include"))
316 add_cmdline_include(*++next);
317 else if (*next && !strcmp(arg, "imacros"))
318 add_cmdline_include(*++next);
319 else if (*next && !strcmp(arg, "isystem")) {
320 char *path = *++next;
321 if (!path)
322 die("missing argument for -isystem option");
323 add_pre_buffer("#add_isystem \"%s/\"\n", path);
324 } else if (*next && !strcmp(arg, "idirafter")) {
325 char *path = *++next;
326 if (!path)
327 die("missing argument for -idirafter option");
328 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
330 return next;
333 static char **handle_switch_M(char *arg, char **next)
335 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
336 if (!*next)
337 die("missing argument for -%s option", arg);
338 return next + 1;
340 return next;
343 static char **handle_switch_m(char *arg, char **next)
345 if (!strcmp(arg, "m64")) {
346 bits_in_long = 64;
347 max_int_alignment = 8;
348 bits_in_pointer = 64;
349 pointer_alignment = 8;
350 size_t_ctype = &ulong_ctype;
351 ssize_t_ctype = &long_ctype;
352 } else if (!strcmp(arg, "msize-long")) {
353 size_t_ctype = &ulong_ctype;
354 ssize_t_ctype = &long_ctype;
356 return next;
359 static char **handle_switch_o(char *arg, char **next)
361 if (!strcmp (arg, "o")) { // "-o foo"
362 if (!*++next)
363 die("argument to '-o' is missing");
365 // else "-ofoo"
367 return next;
370 static const struct warning {
371 const char *name;
372 int *flag;
373 } warnings[] = {
374 { "address-space", &Waddress_space },
375 { "bitwise", &Wbitwise },
376 { "cast-to-as", &Wcast_to_as },
377 { "cast-truncate", &Wcast_truncate },
378 { "context", &Wcontext },
379 { "decl", &Wdecl },
380 { "default-bitfield-sign", &Wdefault_bitfield_sign },
381 { "do-while", &Wdo_while },
382 { "enum-mismatch", &Wenum_mismatch },
383 { "non-pointer-null", &Wnon_pointer_null },
384 { "old-initializer", &Wold_initializer },
385 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
386 { "paren-string", &Wparen_string },
387 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
388 { "return-void", &Wreturn_void },
389 { "shadow", &Wshadow },
390 { "transparent-union", &Wtransparent_union },
391 { "typesign", &Wtypesign },
392 { "undef", &Wundef },
393 { "uninitialized", &Wuninitialized },
394 { "declaration-after-statement", &Wdeclarationafterstatement },
397 enum {
398 WARNING_OFF,
399 WARNING_ON,
400 WARNING_FORCE_OFF
404 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
406 int flag = WARNING_ON;
407 char *p = arg + 1;
408 unsigned i;
410 if (!strcmp(p, "all")) {
411 for (i = 0; i < n; i++) {
412 if (*warnings[i].flag != WARNING_FORCE_OFF)
413 *warnings[i].flag = WARNING_ON;
417 // Prefixes "no" and "no-" mean to turn warning off.
418 if (p[0] == 'n' && p[1] == 'o') {
419 p += 2;
420 if (p[0] == '-')
421 p++;
422 flag = WARNING_FORCE_OFF;
425 for (i = 0; i < n; i++) {
426 if (!strcmp(p,warnings[i].name)) {
427 *warnings[i].flag = flag;
428 return next;
432 // Unknown.
433 return NULL;
436 static char **handle_switch_W(char *arg, char **next)
438 char ** ret = handle_onoff_switch(arg, next, warnings, sizeof warnings/sizeof warnings[0]);
439 if (ret)
440 return ret;
442 // Unknown.
443 return next;
446 static struct warning debugs[] = {
447 { "entry", &dbg_entry},
448 { "dead", &dbg_dead},
452 static char **handle_switch_v(char *arg, char **next)
454 char ** ret = handle_onoff_switch(arg, next, debugs, sizeof debugs/sizeof debugs[0]);
455 if (ret)
456 return ret;
458 // Unknown.
459 do {
460 verbose++;
461 } while (*++arg == 'v');
462 return next;
466 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
468 unsigned i;
470 for (i = 0; i < n; i++) {
471 if (*warnings[i].flag == WARNING_FORCE_OFF)
472 *warnings[i].flag = WARNING_OFF;
476 static void handle_switch_W_finalize(void)
478 handle_onoff_switch_finalize(warnings, sizeof(warnings) / sizeof(warnings[0]));
480 /* default Wdeclarationafterstatement based on the C dialect */
481 if (-1 == Wdeclarationafterstatement)
483 switch (standard)
485 case STANDARD_C89:
486 case STANDARD_C94:
487 Wdeclarationafterstatement = 1;
488 break;
490 case STANDARD_C99:
491 case STANDARD_GNU89:
492 case STANDARD_GNU99:
493 Wdeclarationafterstatement = 0;
494 break;
496 default:
497 assert (0);
503 static void handle_switch_v_finalize(void)
505 handle_onoff_switch_finalize(debugs, sizeof(debugs) / sizeof(debugs[0]));
508 static char **handle_switch_U(char *arg, char **next)
510 const char *name = arg + 1;
511 add_pre_buffer ("#undef %s\n", name);
512 return next;
515 static char **handle_switch_O(char *arg, char **next)
517 int level = 1;
518 if (arg[1] >= '0' && arg[1] <= '9')
519 level = arg[1] - '0';
520 optimize = level;
521 optimize_size = arg[1] == 's';
522 return next;
525 static char **handle_switch_ftabstop(char *arg, char **next)
527 char *end;
528 unsigned long val;
530 if (*arg == '\0')
531 die("error: missing argument to \"-ftabstop=\"");
533 /* we silently ignore silly values */
534 val = strtoul(arg, &end, 10);
535 if (*end == '\0' && 1 <= val && val <= 100)
536 tabstop = val;
538 return next;
541 static char **handle_switch_f(char *arg, char **next)
543 int flag = 1;
545 arg++;
547 if (!strncmp(arg, "tabstop=", 8))
548 return handle_switch_ftabstop(arg+8, next);
550 /* handle switches w/ arguments above, boolean and only boolean below */
552 if (!strncmp(arg, "no-", 3)) {
553 flag = 0;
554 arg += 3;
556 /* handle switch here.. */
557 return next;
560 static char **handle_switch_G(char *arg, char **next)
562 if (!strcmp (arg, "G") && *next)
563 return next + 1; // "-G 0"
564 else
565 return next; // "-G0" or (bogus) terminal "-G"
568 static char **handle_switch_a(char *arg, char **next)
570 if (!strcmp (arg, "ansi"))
571 standard = STANDARD_C89;
573 return next;
576 static char **handle_switch_s(char *arg, char **next)
578 if (!strncmp (arg, "std=", 4))
580 arg += 4;
582 if (!strcmp (arg, "c89") ||
583 !strcmp (arg, "iso9899:1990"))
584 standard = STANDARD_C89;
586 else if (!strcmp (arg, "iso9899:199409"))
587 standard = STANDARD_C94;
589 else if (!strcmp (arg, "c99") ||
590 !strcmp (arg, "c9x") ||
591 !strcmp (arg, "iso9899:1999") ||
592 !strcmp (arg, "iso9899:199x"))
593 standard = STANDARD_C99;
595 else if (!strcmp (arg, "gnu89"))
596 standard = STANDARD_GNU89;
598 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
599 standard = STANDARD_GNU99;
601 else
602 die ("Unsupported C dialect");
605 return next;
608 static char **handle_nostdinc(char *arg, char **next)
610 add_pre_buffer("#nostdinc\n");
611 return next;
614 static char **handle_base_dir(char *arg, char **next)
616 gcc_base_dir = *++next;
617 if (!gcc_base_dir)
618 die("missing argument for -gcc-base-dir option");
619 return next;
622 struct switches {
623 const char *name;
624 char **(*fn)(char *, char **);
627 static char **handle_switch(char *arg, char **next)
629 static struct switches cmd[] = {
630 { "nostdinc", handle_nostdinc },
631 { "gcc-base-dir", handle_base_dir},
632 { NULL, NULL }
634 struct switches *s;
636 switch (*arg) {
637 case 'D': return handle_switch_D(arg, next);
638 case 'E': return handle_switch_E(arg, next);
639 case 'I': return handle_switch_I(arg, next);
640 case 'i': return handle_switch_i(arg, next);
641 case 'M': return handle_switch_M(arg, next);
642 case 'm': return handle_switch_m(arg, next);
643 case 'o': return handle_switch_o(arg, next);
644 case 'U': return handle_switch_U(arg, next);
645 case 'v': return handle_switch_v(arg, next);
646 case 'W': return handle_switch_W(arg, next);
647 case 'O': return handle_switch_O(arg, next);
648 case 'f': return handle_switch_f(arg, next);
649 case 'G': return handle_switch_G(arg, next);
650 case 'a': return handle_switch_a(arg, next);
651 case 's': return handle_switch_s(arg, next);
652 default:
653 break;
656 s = cmd;
657 while (s->name) {
658 if (!strcmp(s->name, arg))
659 return s->fn(arg, next);
660 s++;
664 * Ignore unknown command line options:
665 * they're probably gcc switches
667 return next;
670 void declare_builtin_functions(void)
672 /* Gaah. gcc knows tons of builtin <string.h> functions */
673 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
674 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
675 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
676 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
677 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
678 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
679 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
680 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
681 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
682 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
683 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
684 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
685 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
686 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
688 /* And bitwise operations.. */
689 add_pre_buffer("extern int __builtin_clz(int);\n");
690 add_pre_buffer("extern int __builtin_clzl(long);\n");
691 add_pre_buffer("extern int __builtin_clzll(long long);\n");
692 add_pre_buffer("extern int __builtin_ctz(int);\n");
693 add_pre_buffer("extern int __builtin_ctzl(long);\n");
694 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
695 add_pre_buffer("extern int __builtin_ffs(int);\n");
696 add_pre_buffer("extern int __builtin_ffsl(long);\n");
697 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
698 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
699 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
700 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
702 /* And some random ones.. */
703 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
704 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
705 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
706 add_pre_buffer("extern void __builtin_trap(void);\n");
707 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
708 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
709 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
710 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
711 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
712 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
713 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
714 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
715 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
716 add_pre_buffer("extern long __builtin_labs(long);\n");
718 /* And some floating point stuff.. */
719 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
720 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
721 add_pre_buffer("extern int __builtin_isless(float, float);\n");
722 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
723 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
724 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
726 /* And some __FORTIFY_SOURCE ones.. */
727 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
728 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
729 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
730 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
731 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
732 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
733 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
734 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
735 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
736 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
737 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
738 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
739 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
740 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
743 void create_builtin_stream(void)
745 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
746 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
747 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
749 /* We add compiler headers path here because we have to parse
750 * the arguments to get it, falling back to default. */
751 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
752 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
754 add_pre_buffer("#define __extension__\n");
755 add_pre_buffer("#define __pragma__\n");
757 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
758 // solaris/sparc that is really "unsigned int" and for linux/x86_64
759 // it is "long unsigned int". In either case we can probably
760 // get away with this. We need the #weak_define as cgcc will define
761 // the right __SIZE_TYPE__.
762 if (size_t_ctype == &ulong_ctype)
763 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
764 else
765 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
766 add_pre_buffer("#weak_define __STDC__ 1\n");
768 switch (standard)
770 case STANDARD_C89:
771 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
772 break;
774 case STANDARD_C94:
775 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
776 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
777 break;
779 case STANDARD_C99:
780 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
781 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
782 break;
784 case STANDARD_GNU89:
785 break;
787 case STANDARD_GNU99:
788 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
789 break;
791 default:
792 assert (0);
795 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
796 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
797 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
798 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
799 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
800 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
801 add_pre_buffer("#define __builtin_va_end(arg)\n");
803 /* FIXME! We need to do these as special magic macros at expansion time! */
804 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
806 if (optimize)
807 add_pre_buffer("#define __OPTIMIZE__ 1\n");
808 if (optimize_size)
809 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
811 /* GCC defines these for limits.h */
812 add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__) "\n");
813 add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__) "\n");
814 add_pre_buffer("#weak_define __INT_MAX__ " STRINGIFY(__INT_MAX__) "\n");
815 add_pre_buffer("#weak_define __LONG_MAX__ " STRINGIFY(__LONG_MAX__) "\n");
816 add_pre_buffer("#weak_define __LONG_LONG_MAX__ " STRINGIFY(__LONG_LONG_MAX__) "\n");
817 add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__) "\n");
820 static struct symbol_list *sparse_tokenstream(struct token *token)
822 // Preprocess the stream
823 token = preprocess(token);
825 if (preprocess_only) {
826 while (!eof_token(token)) {
827 int prec = 1;
828 struct token *next = token->next;
829 const char *separator = "";
830 if (next->pos.whitespace)
831 separator = " ";
832 if (next->pos.newline) {
833 separator = "\n\t\t\t\t\t";
834 prec = next->pos.pos;
835 if (prec > 4)
836 prec = 4;
838 printf("%s%.*s", show_token(token), prec, separator);
839 token = next;
841 putchar('\n');
843 return NULL;
846 // Parse the resulting C code
847 while (!eof_token(token))
848 token = external_declaration(token, &translation_unit_used_list);
849 return translation_unit_used_list;
852 static struct symbol_list *sparse_file(const char *filename)
854 int fd;
855 struct token *token;
857 if (strcmp (filename, "-") == 0) {
858 fd = 0;
859 } else {
860 fd = open(filename, O_RDONLY);
861 if (fd < 0)
862 die("No such file: %s", filename);
865 // Tokenize the input stream
866 token = tokenize(filename, fd, NULL, includepath);
867 close(fd);
869 return sparse_tokenstream(token);
873 * This handles the "-include" directive etc: we're in global
874 * scope, and all types/macros etc will affect all the following
875 * files.
877 * NOTE NOTE NOTE! "#undef" of anything in this stage will
878 * affect all subsequent files too, i.e. we can have non-local
879 * behaviour between files!
881 static struct symbol_list *sparse_initial(void)
883 struct token *token;
884 int i;
886 // Prepend any "include" file to the stream.
887 // We're in global scope, it will affect all files!
888 token = NULL;
889 for (i = cmdline_include_nr - 1; i >= 0; i--)
890 token = tokenize(cmdline_include[i].filename, cmdline_include[i].fd,
891 token, includepath);
893 // Prepend the initial built-in stream
894 if (token)
895 pre_buffer_end->next = token;
896 return sparse_tokenstream(pre_buffer_begin);
899 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
901 char **args;
902 struct symbol_list *list;
904 // Initialize symbol stream first, so that we can add defines etc
905 init_symbols();
907 args = argv;
908 for (;;) {
909 char *arg = *++args;
910 if (!arg)
911 break;
913 if (arg[0] == '-' && arg[1]) {
914 args = handle_switch(arg+1, args);
915 continue;
917 add_ptr_list_notag(filelist, arg);
919 handle_switch_W_finalize();
920 handle_switch_v_finalize();
922 list = NULL;
923 if (!ptr_list_empty(filelist)) {
924 // Initialize type system
925 init_ctype();
927 create_builtin_stream();
928 add_pre_buffer("#define __CHECKER__ 1\n");
929 if (!preprocess_only)
930 declare_builtin_functions();
932 list = sparse_initial();
935 * Protect the initial token allocations, since
936 * they need to survive all the others
938 protect_token_alloc();
940 return list;
943 struct symbol_list * sparse_keep_tokens(char *filename)
945 struct symbol_list *res;
947 /* Clear previous symbol list */
948 translation_unit_used_list = NULL;
950 new_file_scope();
951 res = sparse_file(filename);
953 /* And return it */
954 return res;
958 struct symbol_list * __sparse(char *filename)
960 struct symbol_list *res;
962 res = sparse_keep_tokens(filename);
964 /* Drop the tokens for this file after parsing */
965 clear_token_alloc();
967 /* And return it */
968 return res;
971 struct symbol_list * sparse(char *filename)
973 struct symbol_list *res = __sparse(filename);
975 /* Evaluate the complete symbol list */
976 evaluate_symbol_list(res);
978 return res;