A slightly edited irc discussion with Josh Triplett.
[smatch.git] / lib.c
blob8dadfa9de43e4cab969379957cb049fb12ca5c26
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 struct token *skip_to(struct token *token, int op)
46 while (!match_op(token, op) && !eof_token(token))
47 token = token->next;
48 return token;
51 struct token *expect(struct token *token, int op, const char *where)
53 if (!match_op(token, op)) {
54 static struct token bad_token;
55 if (token != &bad_token) {
56 bad_token.next = token;
57 sparse_error(token->pos, "Expected %s %s", show_special(op), where);
58 sparse_error(token->pos, "got %s", show_token(token));
60 if (op == ';')
61 return skip_to(token, op);
62 return &bad_token;
64 return token->next;
67 unsigned int hexval(unsigned int c)
69 int retval = 256;
70 switch (c) {
71 case '0'...'9':
72 retval = c - '0';
73 break;
74 case 'a'...'f':
75 retval = c - 'a' + 10;
76 break;
77 case 'A'...'F':
78 retval = c - 'A' + 10;
79 break;
81 return retval;
84 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
86 static char buffer[512];
87 const char *name;
89 vsprintf(buffer, fmt, args);
90 name = stream_name(pos.stream);
92 fprintf(stderr, "%s:%d:%d: %s%s\n",
93 name, pos.line, pos.pos, type, buffer);
96 static int max_warnings = 100;
97 static int show_info = 1;
99 void info(struct position pos, const char * fmt, ...)
101 va_list args;
103 if (!show_info)
104 return;
105 va_start(args, fmt);
106 do_warn("", pos, fmt, args);
107 va_end(args);
110 void warning(struct position pos, const char * fmt, ...)
112 va_list args;
114 if (!max_warnings) {
115 show_info = 0;
116 return;
119 if (!--max_warnings) {
120 show_info = 0;
121 fmt = "too many warnings";
124 va_start(args, fmt);
125 do_warn("warning: ", pos, fmt, args);
126 va_end(args);
129 static void do_error(struct position pos, const char * fmt, va_list args)
131 static int errors = 0;
132 die_if_error = 1;
133 show_info = 1;
134 /* Shut up warnings after an error */
135 max_warnings = 0;
136 if (errors > 100) {
137 static int once = 0;
138 show_info = 0;
139 if (once)
140 return;
141 fmt = "too many errors";
142 once = 1;
145 do_warn("error: ", pos, fmt, args);
146 errors++;
149 void sparse_error(struct position pos, const char * fmt, ...)
151 va_list args;
152 va_start(args, fmt);
153 do_error(pos, fmt, args);
154 va_end(args);
157 void expression_error(struct expression *expr, const char *fmt, ...)
159 va_list args;
160 va_start(args, fmt);
161 do_error(expr->pos, fmt, args);
162 va_end(args);
163 expr->ctype = &bad_ctype;
166 void error_die(struct position pos, const char * fmt, ...)
168 va_list args;
169 va_start(args, fmt);
170 do_warn("error: ", pos, fmt, args);
171 va_end(args);
172 exit(1);
175 void die(const char *fmt, ...)
177 va_list args;
178 static char buffer[512];
180 va_start(args, fmt);
181 vsnprintf(buffer, sizeof(buffer), fmt, args);
182 va_end(args);
184 fprintf(stderr, "%s\n", buffer);
185 exit(1);
188 static struct token *pre_buffer_begin = NULL;
189 static struct token *pre_buffer_end = NULL;
191 int Waddress_space = 1;
192 int Wbitwise = 0;
193 int Wcast_to_as = 0;
194 int Wcast_truncate = 1;
195 int Wcontext = 1;
196 int Wdecl = 1;
197 int Wdefault_bitfield_sign = 0;
198 int Wdo_while = 0;
199 int Wenum_mismatch = 1;
200 int Wnon_pointer_null = 1;
201 int Wold_initializer = 1;
202 int Wone_bit_signed_bitfield = 1;
203 int Wparen_string = 0;
204 int Wptr_subtraction_blows = 0;
205 int Wreturn_void = 0;
206 int Wshadow = 0;
207 int Wtransparent_union = 1;
208 int Wtypesign = 0;
209 int Wundef = 0;
210 int Wuninitialized = 1;
211 int Wdeclarationafterstatement = -1;
213 int dbg_entry = 0;
214 int dbg_dead = 0;
216 int preprocess_only;
218 static enum { STANDARD_C89,
219 STANDARD_C94,
220 STANDARD_C99,
221 STANDARD_GNU89,
222 STANDARD_GNU99, } standard = STANDARD_GNU89;
224 #define CMDLINE_INCLUDE 20
225 int cmdline_include_nr = 0;
226 struct cmdline_include cmdline_include[CMDLINE_INCLUDE];
229 void add_pre_buffer(const char *fmt, ...)
231 va_list args;
232 unsigned int size;
233 struct token *begin, *end;
234 char buffer[4096];
236 va_start(args, fmt);
237 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
238 va_end(args);
239 begin = tokenize_buffer(buffer, size, &end);
240 if (!pre_buffer_begin)
241 pre_buffer_begin = begin;
242 if (pre_buffer_end)
243 pre_buffer_end->next = begin;
244 pre_buffer_end = end;
247 static char **handle_switch_D(char *arg, char **next)
249 const char *name = arg + 1;
250 const char *value = "1";
252 if (!*name || isspace(*name))
253 die("argument to `-D' is missing");
255 for (;;) {
256 char c;
257 c = *++arg;
258 if (!c)
259 break;
260 if (isspace((unsigned char)c) || c == '=') {
261 *arg = '\0';
262 value = arg + 1;
263 break;
266 add_pre_buffer("#define %s %s\n", name, value);
267 return next;
270 static char **handle_switch_E(char *arg, char **next)
272 if (arg[1] == '\0')
273 preprocess_only = 1;
274 return next;
277 static char **handle_switch_I(char *arg, char **next)
279 char *path = arg+1;
281 switch (arg[1]) {
282 case '-':
283 add_pre_buffer("#split_include\n");
284 break;
286 case '\0': /* Plain "-I" */
287 path = *++next;
288 if (!path)
289 die("missing argument for -I option");
290 /* Fall through */
291 default:
292 add_pre_buffer("#add_include \"%s/\"\n", path);
294 return next;
297 static void add_cmdline_include(char *filename)
299 int fd = open(filename, O_RDONLY);
300 if (fd < 0) {
301 perror(filename);
302 return;
304 if (cmdline_include_nr >= CMDLINE_INCLUDE)
305 die("too many include files for %s\n", filename);
306 cmdline_include[cmdline_include_nr].filename = filename;
307 cmdline_include[cmdline_include_nr].fd = fd;
308 cmdline_include_nr++;
311 static char **handle_switch_i(char *arg, char **next)
313 if (*next && !strcmp(arg, "include"))
314 add_cmdline_include(*++next);
315 else if (*next && !strcmp(arg, "imacros"))
316 add_cmdline_include(*++next);
317 else if (*next && !strcmp(arg, "isystem")) {
318 char *path = *++next;
319 if (!path)
320 die("missing argument for -isystem option");
321 add_pre_buffer("#add_isystem \"%s/\"\n", path);
323 return next;
326 static char **handle_switch_M(char *arg, char **next)
328 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
329 if (!*next)
330 die("missing argument for -%s option", arg);
331 return next + 1;
333 return next;
336 static char **handle_switch_m(char *arg, char **next)
338 if (!strcmp(arg, "m64")) {
339 bits_in_long = 64;
340 max_int_alignment = 8;
341 bits_in_pointer = 64;
342 pointer_alignment = 8;
343 size_t_ctype = &ulong_ctype;
344 ssize_t_ctype = &long_ctype;
345 } else if (!strcmp(arg, "msize-long")) {
346 size_t_ctype = &ulong_ctype;
347 ssize_t_ctype = &long_ctype;
349 return next;
352 static char **handle_switch_o(char *arg, char **next)
354 if (!strcmp (arg, "o")) { // "-o foo"
355 if (!*++next)
356 die("argument to '-o' is missing");
358 // else "-ofoo"
360 return next;
363 static const struct warning {
364 const char *name;
365 int *flag;
366 } warnings[] = {
367 { "address-space", &Waddress_space },
368 { "bitwise", &Wbitwise },
369 { "cast-to-as", &Wcast_to_as },
370 { "cast-truncate", &Wcast_truncate },
371 { "context", &Wcontext },
372 { "decl", &Wdecl },
373 { "default-bitfield-sign", &Wdefault_bitfield_sign },
374 { "do-while", &Wdo_while },
375 { "enum-mismatch", &Wenum_mismatch },
376 { "non-pointer-null", &Wnon_pointer_null },
377 { "old-initializer", &Wold_initializer },
378 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
379 { "paren-string", &Wparen_string },
380 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
381 { "return-void", &Wreturn_void },
382 { "shadow", &Wshadow },
383 { "transparent-union", &Wtransparent_union },
384 { "typesign", &Wtypesign },
385 { "undef", &Wundef },
386 { "uninitialized", &Wuninitialized },
387 { "declaration-after-statement", &Wdeclarationafterstatement },
390 enum {
391 WARNING_OFF,
392 WARNING_ON,
393 WARNING_FORCE_OFF
397 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
399 int flag = WARNING_ON;
400 char *p = arg + 1;
401 unsigned i;
403 if (!strcmp(p, "all")) {
404 for (i = 0; i < n; i++) {
405 if (*warnings[i].flag != WARNING_FORCE_OFF)
406 *warnings[i].flag = WARNING_ON;
410 // Prefixes "no" and "no-" mean to turn warning off.
411 if (p[0] == 'n' && p[1] == 'o') {
412 p += 2;
413 if (p[0] == '-')
414 p++;
415 flag = WARNING_FORCE_OFF;
418 for (i = 0; i < n; i++) {
419 if (!strcmp(p,warnings[i].name)) {
420 *warnings[i].flag = flag;
421 return next;
425 // Unknown.
426 return NULL;
429 static char **handle_switch_W(char *arg, char **next)
431 char ** ret = handle_onoff_switch(arg, next, warnings, sizeof warnings/sizeof warnings[0]);
432 if (ret)
433 return ret;
435 // Unknown.
436 return next;
439 static struct warning debugs[] = {
440 { "entry", &dbg_entry},
441 { "dead", &dbg_dead},
445 static char **handle_switch_v(char *arg, char **next)
447 char ** ret = handle_onoff_switch(arg, next, debugs, sizeof debugs/sizeof debugs[0]);
448 if (ret)
449 return ret;
451 // Unknown.
452 do {
453 verbose++;
454 } while (*++arg == 'v');
455 return next;
459 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
461 unsigned i;
463 for (i = 0; i < n; i++) {
464 if (*warnings[i].flag == WARNING_FORCE_OFF)
465 *warnings[i].flag = WARNING_OFF;
469 static void handle_switch_W_finalize(void)
471 handle_onoff_switch_finalize(warnings, sizeof(warnings) / sizeof(warnings[0]));
473 /* default Wdeclarationafterstatement based on the C dialect */
474 if (-1 == Wdeclarationafterstatement)
476 switch (standard)
478 case STANDARD_C89:
479 case STANDARD_C94:
480 Wdeclarationafterstatement = 1;
481 break;
483 case STANDARD_C99:
484 case STANDARD_GNU89:
485 case STANDARD_GNU99:
486 Wdeclarationafterstatement = 0;
487 break;
489 default:
490 assert (0);
496 static void handle_switch_v_finalize(void)
498 handle_onoff_switch_finalize(debugs, sizeof(debugs) / sizeof(debugs[0]));
501 static char **handle_switch_U(char *arg, char **next)
503 const char *name = arg + 1;
504 add_pre_buffer ("#undef %s\n", name);
505 return next;
508 static char **handle_switch_O(char *arg, char **next)
510 int level = 1;
511 if (arg[1] >= '0' && arg[1] <= '9')
512 level = arg[1] - '0';
513 optimize = level;
514 optimize_size = arg[1] == 's';
515 return next;
518 static char **handle_switch_f(char *arg, char **next)
520 int flag = 1;
522 arg++;
523 if (!strncmp(arg, "no-", 3)) {
524 flag = 0;
525 arg += 3;
527 /* handle switch here.. */
528 return next;
531 static char **handle_switch_G(char *arg, char **next)
533 if (!strcmp (arg, "G") && *next)
534 return next + 1; // "-G 0"
535 else
536 return next; // "-G0" or (bogus) terminal "-G"
539 static char **handle_switch_a(char *arg, char **next)
541 if (!strcmp (arg, "ansi"))
542 standard = STANDARD_C89;
544 return next;
547 static char **handle_switch_s(char *arg, char **next)
549 if (!strncmp (arg, "std=", 4))
551 arg += 4;
553 if (!strcmp (arg, "c89") ||
554 !strcmp (arg, "iso9899:1990"))
555 standard = STANDARD_C89;
557 else if (!strcmp (arg, "iso9899:199409"))
558 standard = STANDARD_C94;
560 else if (!strcmp (arg, "c99") ||
561 !strcmp (arg, "c9x") ||
562 !strcmp (arg, "iso9899:1999") ||
563 !strcmp (arg, "iso9899:199x"))
564 standard = STANDARD_C99;
566 else if (!strcmp (arg, "gnu89"))
567 standard = STANDARD_GNU89;
569 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
570 standard = STANDARD_GNU99;
572 else
573 die ("Unsupported C dialect");
576 return next;
579 static char **handle_nostdinc(char *arg, char **next)
581 add_pre_buffer("#nostdinc\n");
582 return next;
585 static char **handle_dirafter(char *arg, char **next)
587 char *path = *++next;
588 if (!path)
589 die("missing argument for -dirafter option");
590 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
591 return next;
594 struct switches {
595 const char *name;
596 char **(*fn)(char *, char **);
599 static char **handle_switch(char *arg, char **next)
601 static struct switches cmd[] = {
602 { "nostdinc", handle_nostdinc },
603 { "dirafter", handle_dirafter },
604 { NULL, NULL }
606 struct switches *s;
608 switch (*arg) {
609 case 'D': return handle_switch_D(arg, next);
610 case 'E': return handle_switch_E(arg, next);
611 case 'I': return handle_switch_I(arg, next);
612 case 'i': return handle_switch_i(arg, next);
613 case 'M': return handle_switch_M(arg, next);
614 case 'm': return handle_switch_m(arg, next);
615 case 'o': return handle_switch_o(arg, next);
616 case 'U': return handle_switch_U(arg, next);
617 case 'v': return handle_switch_v(arg, next);
618 case 'W': return handle_switch_W(arg, next);
619 case 'O': return handle_switch_O(arg, next);
620 case 'f': return handle_switch_f(arg, next);
621 case 'G': return handle_switch_G(arg, next);
622 case 'a': return handle_switch_a(arg, next);
623 case 's': return handle_switch_s(arg, next);
624 default:
625 break;
628 s = cmd;
629 while (s->name) {
630 if (!strcmp(s->name, arg))
631 return s->fn(arg, next);
632 s++;
636 * Ignore unknown command line options:
637 * they're probably gcc switches
639 return next;
642 void declare_builtin_functions(void)
644 /* Gaah. gcc knows tons of builtin <string.h> functions */
645 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
646 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
647 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
648 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
649 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
650 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
651 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
652 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
653 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
654 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
655 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
656 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
657 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
658 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
660 /* And some random ones.. */
661 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
662 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
663 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
664 add_pre_buffer("extern void __builtin_trap(void);\n");
665 add_pre_buffer("extern int __builtin_ffs(int);\n");
666 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
667 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
668 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
669 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
670 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
671 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
672 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
673 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
674 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
675 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
676 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
677 add_pre_buffer("extern long __builtin_labs(long);\n");
679 /* And some __FORTIFY_SOURCE ones.. */
680 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
681 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
682 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
683 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
684 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
685 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
686 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
687 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
688 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
689 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
690 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
691 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
692 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
693 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
696 void create_builtin_stream(void)
698 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
699 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
700 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
701 add_pre_buffer("#define __extension__\n");
702 add_pre_buffer("#define __pragma__\n");
704 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
705 // solaris/sparc that is really "unsigned int" and for linux/x86_64
706 // it is "long unsigned int". In either case we can probably
707 // get away with this. We need the #weak_define as cgcc will define
708 // the right __SIZE_TYPE__.
709 if (size_t_ctype == &ulong_ctype)
710 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
711 else
712 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
713 add_pre_buffer("#weak_define __STDC__ 1\n");
715 switch (standard)
717 case STANDARD_C89:
718 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
719 break;
721 case STANDARD_C94:
722 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
723 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
724 break;
726 case STANDARD_C99:
727 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
728 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
729 break;
731 case STANDARD_GNU89:
732 break;
734 case STANDARD_GNU99:
735 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
736 break;
738 default:
739 assert (0);
742 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
743 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
744 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
745 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
746 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
747 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
748 add_pre_buffer("#define __builtin_va_end(arg)\n");
750 /* FIXME! We need to do these as special magic macros at expansion time! */
751 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
753 if (optimize)
754 add_pre_buffer("#define __OPTIMIZE__ 1\n");
755 if (optimize_size)
756 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
759 static struct symbol_list *sparse_tokenstream(struct token *token)
761 // Preprocess the stream
762 token = preprocess(token);
764 if (preprocess_only) {
765 while (!eof_token(token)) {
766 int prec = 1;
767 struct token *next = token->next;
768 const char *separator = "";
769 if (next->pos.whitespace)
770 separator = " ";
771 if (next->pos.newline) {
772 separator = "\n\t\t\t\t\t";
773 prec = next->pos.pos;
774 if (prec > 4)
775 prec = 4;
777 printf("%s%.*s", show_token(token), prec, separator);
778 token = next;
780 putchar('\n');
782 return NULL;
785 // Parse the resulting C code
786 while (!eof_token(token))
787 token = external_declaration(token, &translation_unit_used_list);
788 return translation_unit_used_list;
791 static struct symbol_list *sparse_file(const char *filename)
793 int fd;
794 struct token *token;
796 if (strcmp (filename, "-") == 0) {
797 fd = 0;
798 } else {
799 fd = open(filename, O_RDONLY);
800 if (fd < 0)
801 die("No such file: %s", filename);
804 // Tokenize the input stream
805 token = tokenize(filename, fd, NULL, includepath);
806 close(fd);
808 return sparse_tokenstream(token);
812 * This handles the "-include" directive etc: we're in global
813 * scope, and all types/macros etc will affect all the following
814 * files.
816 * NOTE NOTE NOTE! "#undef" of anything in this stage will
817 * affect all subsequent files too, i.e. we can have non-local
818 * behaviour between files!
820 static struct symbol_list *sparse_initial(void)
822 struct token *token;
823 int i;
825 // Prepend any "include" file to the stream.
826 // We're in global scope, it will affect all files!
827 token = NULL;
828 for (i = cmdline_include_nr - 1; i >= 0; i--)
829 token = tokenize(cmdline_include[i].filename, cmdline_include[i].fd,
830 token, includepath);
832 // Prepend the initial built-in stream
833 if (token)
834 pre_buffer_end->next = token;
835 return sparse_tokenstream(pre_buffer_begin);
838 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
840 char **args;
841 struct symbol_list *list;
843 // Initialize symbol stream first, so that we can add defines etc
844 init_symbols();
846 args = argv;
847 for (;;) {
848 char *arg = *++args;
849 if (!arg)
850 break;
852 if (arg[0] == '-' && arg[1]) {
853 args = handle_switch(arg+1, args);
854 continue;
856 add_ptr_list_notag(filelist, arg);
858 handle_switch_W_finalize();
859 handle_switch_v_finalize();
861 list = NULL;
862 if (!ptr_list_empty(filelist)) {
863 // Initialize type system
864 init_ctype();
866 create_builtin_stream();
867 add_pre_buffer("#define __CHECKER__ 1\n");
868 if (!preprocess_only)
869 declare_builtin_functions();
871 list = sparse_initial();
874 * Protect the initial token allocations, since
875 * they need to survive all the others
877 protect_token_alloc();
879 return list;
882 struct symbol_list * sparse_keep_tokens(char *filename)
884 struct symbol_list *res;
886 /* Clear previous symbol list */
887 translation_unit_used_list = NULL;
889 new_file_scope();
890 res = sparse_file(filename);
892 /* And return it */
893 return res;
897 struct symbol_list * __sparse(char *filename)
899 struct symbol_list *res;
901 res = sparse_keep_tokens(filename);
903 /* Drop the tokens for this file after parsing */
904 clear_token_alloc();
906 /* And return it */
907 return res;
910 struct symbol_list * sparse(char *filename)
912 struct symbol_list *res = __sparse(filename);
914 /* Evaluate the complete symbol list */
915 evaluate_symbol_list(res);
917 return res;