wine: add more functions that don't return
[smatch.git] / lib.c
blob0abcc9a69054542a94d76df2fa7bd00c07de404d
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 unsigned int pre_buffer_size;
189 static char pre_buffer[8192];
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;
234 va_start(args, fmt);
235 size = pre_buffer_size;
236 size += vsnprintf(pre_buffer + size,
237 sizeof(pre_buffer) - size,
238 fmt, args);
239 pre_buffer_size = size;
240 va_end(args);
243 static char **handle_switch_D(char *arg, char **next)
245 const char *name = arg + 1;
246 const char *value = "1";
247 for (;;) {
248 char c;
249 c = *++arg;
250 if (!c)
251 break;
252 if (isspace((unsigned char)c) || c == '=') {
253 *arg = '\0';
254 value = arg + 1;
255 break;
258 add_pre_buffer("#define %s %s\n", name, value);
259 return next;
262 static char **handle_switch_E(char *arg, char **next)
264 if (arg[1] == '\0')
265 preprocess_only = 1;
266 return next;
269 static char **handle_switch_I(char *arg, char **next)
271 char *path = arg+1;
273 switch (arg[1]) {
274 case '-':
275 add_pre_buffer("#split_include\n");
276 break;
278 case '\0': /* Plain "-I" */
279 path = *++next;
280 if (!path)
281 die("missing argument for -I option");
282 /* Fall through */
283 default:
284 add_pre_buffer("#add_include \"%s/\"\n", path);
286 return next;
289 static void add_cmdline_include(char *filename)
291 int fd = open(filename, O_RDONLY);
292 if (fd < 0) {
293 perror(filename);
294 return;
296 if (cmdline_include_nr >= CMDLINE_INCLUDE)
297 die("too many include files for %s\n", filename);
298 cmdline_include[cmdline_include_nr].filename = filename;
299 cmdline_include[cmdline_include_nr].fd = fd;
300 cmdline_include_nr++;
303 static char **handle_switch_i(char *arg, char **next)
305 if (*next && !strcmp(arg, "include"))
306 add_cmdline_include(*++next);
307 else if (*next && !strcmp(arg, "imacros"))
308 add_cmdline_include(*++next);
309 else if (*next && !strcmp(arg, "isystem")) {
310 char *path = *++next;
311 if (!path)
312 die("missing argument for -isystem option");
313 add_pre_buffer("#add_isystem \"%s/\"\n", path);
315 return next;
318 static char **handle_switch_M(char *arg, char **next)
320 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
321 if (!*next)
322 die("missing argument for -%s option", arg);
323 return next + 1;
325 return next;
328 static char **handle_switch_m(char *arg, char **next)
330 if (!strcmp(arg, "m64")) {
331 bits_in_long = 64;
332 max_int_alignment = 8;
333 bits_in_pointer = 64;
334 pointer_alignment = 8;
335 size_t_ctype = &ulong_ctype;
336 ssize_t_ctype = &long_ctype;
337 } else if (!strcmp(arg, "msize-long")) {
338 size_t_ctype = &ulong_ctype;
339 ssize_t_ctype = &long_ctype;
341 return next;
344 static char **handle_switch_o(char *arg, char **next)
346 if (!strcmp (arg, "o") && *next)
347 return next + 1; // "-o foo"
348 else
349 return next; // "-ofoo" or (bogus) terminal "-o"
352 static const struct warning {
353 const char *name;
354 int *flag;
355 } warnings[] = {
356 { "address-space", &Waddress_space },
357 { "bitwise", &Wbitwise },
358 { "cast-to-as", &Wcast_to_as },
359 { "cast-truncate", &Wcast_truncate },
360 { "context", &Wcontext },
361 { "decl", &Wdecl },
362 { "default-bitfield-sign", &Wdefault_bitfield_sign },
363 { "do-while", &Wdo_while },
364 { "enum-mismatch", &Wenum_mismatch },
365 { "non-pointer-null", &Wnon_pointer_null },
366 { "old-initializer", &Wold_initializer },
367 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
368 { "paren-string", &Wparen_string },
369 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
370 { "return-void", &Wreturn_void },
371 { "shadow", &Wshadow },
372 { "transparent-union", &Wtransparent_union },
373 { "typesign", &Wtypesign },
374 { "undef", &Wundef },
375 { "uninitialized", &Wuninitialized },
376 { "declaration-after-statement", &Wdeclarationafterstatement },
379 enum {
380 WARNING_OFF,
381 WARNING_ON,
382 WARNING_FORCE_OFF
386 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
388 int flag = WARNING_ON;
389 char *p = arg + 1;
390 unsigned i;
392 if (!strcmp(p, "all")) {
393 for (i = 0; i < n; i++) {
394 if (*warnings[i].flag != WARNING_FORCE_OFF)
395 *warnings[i].flag = WARNING_ON;
399 // Prefixes "no" and "no-" mean to turn warning off.
400 if (p[0] == 'n' && p[1] == 'o') {
401 p += 2;
402 if (p[0] == '-')
403 p++;
404 flag = WARNING_FORCE_OFF;
407 for (i = 0; i < n; i++) {
408 if (!strcmp(p,warnings[i].name)) {
409 *warnings[i].flag = flag;
410 return next;
414 // Unknown.
415 return NULL;
418 static char **handle_switch_W(char *arg, char **next)
420 char ** ret = handle_onoff_switch(arg, next, warnings, sizeof warnings/sizeof warnings[0]);
421 if (ret)
422 return ret;
424 // Unknown.
425 return next;
428 static struct warning debugs[] = {
429 { "entry", &dbg_entry},
430 { "dead", &dbg_dead},
434 static char **handle_switch_v(char *arg, char **next)
436 char ** ret = handle_onoff_switch(arg, next, debugs, sizeof debugs/sizeof debugs[0]);
437 if (ret)
438 return ret;
440 // Unknown.
441 do {
442 verbose++;
443 } while (*++arg == 'v');
444 return next;
448 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
450 unsigned i;
452 for (i = 0; i < n; i++) {
453 if (*warnings[i].flag == WARNING_FORCE_OFF)
454 *warnings[i].flag = WARNING_OFF;
458 static void handle_switch_W_finalize(void)
460 handle_onoff_switch_finalize(warnings, sizeof(warnings) / sizeof(warnings[0]));
462 /* default Wdeclarationafterstatement based on the C dialect */
463 if (-1 == Wdeclarationafterstatement)
465 switch (standard)
467 case STANDARD_C89:
468 case STANDARD_C94:
469 Wdeclarationafterstatement = 1;
470 break;
472 case STANDARD_C99:
473 case STANDARD_GNU89:
474 case STANDARD_GNU99:
475 Wdeclarationafterstatement = 0;
476 break;
478 default:
479 assert (0);
485 static void handle_switch_v_finalize(void)
487 handle_onoff_switch_finalize(debugs, sizeof(debugs) / sizeof(debugs[0]));
490 static char **handle_switch_U(char *arg, char **next)
492 const char *name = arg + 1;
493 add_pre_buffer ("#undef %s\n", name);
494 return next;
497 static char **handle_switch_O(char *arg, char **next)
499 int level = 1;
500 if (arg[1] >= '0' && arg[1] <= '9')
501 level = arg[1] - '0';
502 optimize = level;
503 optimize_size = arg[1] == 's';
504 return next;
507 static char **handle_switch_f(char *arg, char **next)
509 int flag = 1;
511 arg++;
512 if (!strncmp(arg, "no-", 3)) {
513 flag = 0;
514 arg += 3;
516 /* handle switch here.. */
517 return next;
520 static char **handle_switch_G(char *arg, char **next)
522 if (!strcmp (arg, "G") && *next)
523 return next + 1; // "-G 0"
524 else
525 return next; // "-G0" or (bogus) terminal "-G"
528 static char **handle_switch_a(char *arg, char **next)
530 if (!strcmp (arg, "ansi"))
531 standard = STANDARD_C89;
533 return next;
536 static char **handle_switch_s(char *arg, char **next)
538 if (!strncmp (arg, "std=", 4))
540 arg += 4;
542 if (!strcmp (arg, "c89") ||
543 !strcmp (arg, "iso9899:1990"))
544 standard = STANDARD_C89;
546 else if (!strcmp (arg, "iso9899:199409"))
547 standard = STANDARD_C94;
549 else if (!strcmp (arg, "c99") ||
550 !strcmp (arg, "c9x") ||
551 !strcmp (arg, "iso9899:1999") ||
552 !strcmp (arg, "iso9899:199x"))
553 standard = STANDARD_C99;
555 else if (!strcmp (arg, "gnu89"))
556 standard = STANDARD_GNU89;
558 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
559 standard = STANDARD_GNU99;
561 else
562 die ("Unsupported C dialect");
565 return next;
568 static char **handle_nostdinc(char *arg, char **next)
570 add_pre_buffer("#nostdinc\n");
571 return next;
574 static char **handle_dirafter(char *arg, char **next)
576 char *path = *++next;
577 if (!path)
578 die("missing argument for -dirafter option");
579 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
580 return next;
583 struct switches {
584 const char *name;
585 char **(*fn)(char *, char **);
588 char **handle_switch(char *arg, char **next)
590 static struct switches cmd[] = {
591 { "nostdinc", handle_nostdinc },
592 { "dirafter", handle_dirafter },
593 { NULL, NULL }
595 struct switches *s;
597 switch (*arg) {
598 case 'D': return handle_switch_D(arg, next);
599 case 'E': return handle_switch_E(arg, next);
600 case 'I': return handle_switch_I(arg, next);
601 case 'i': return handle_switch_i(arg, next);
602 case 'M': return handle_switch_M(arg, next);
603 case 'm': return handle_switch_m(arg, next);
604 case 'o': return handle_switch_o(arg, next);
605 case 'U': return handle_switch_U(arg, next);
606 case 'v': return handle_switch_v(arg, next);
607 case 'W': return handle_switch_W(arg, next);
608 case 'O': return handle_switch_O(arg, next);
609 case 'f': return handle_switch_f(arg, next);
610 case 'G': return handle_switch_G(arg, next);
611 case 'a': return handle_switch_a(arg, next);
612 case 's': return handle_switch_s(arg, next);
613 default:
614 break;
617 s = cmd;
618 while (s->name) {
619 if (!strcmp(s->name, arg))
620 return s->fn(arg, next);
621 s++;
625 * Ignore unknown command line options:
626 * they're probably gcc switches
628 return next;
631 void declare_builtin_functions(void)
633 /* Gaah. gcc knows tons of builtin <string.h> functions */
634 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
635 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
636 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
637 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
638 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
639 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
640 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
641 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
642 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
643 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
644 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
645 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
646 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
647 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
649 /* And some random ones.. */
650 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
651 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
652 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
653 add_pre_buffer("extern void __builtin_trap(void);\n");
654 add_pre_buffer("extern int __builtin_ffs(int);\n");
655 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
656 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
657 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
658 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
659 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
660 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
661 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
662 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
663 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
664 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
665 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
666 add_pre_buffer("extern long __builtin_labs(long);\n");
668 /* And some __FORTIFY_SOURCE ones.. */
669 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(void *, int);\n");
670 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
671 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
672 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
673 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
674 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
675 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
676 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
677 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
678 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
679 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
680 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
681 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
682 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
685 void create_builtin_stream(void)
687 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
688 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
689 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
690 add_pre_buffer("#define __extension__\n");
691 add_pre_buffer("#define __pragma__\n");
693 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
694 // solaris/sparc that is really "unsigned int" and for linux/x86_64
695 // it is "long unsigned int". In either case we can probably
696 // get away with this. We need the #weak_define as cgcc will define
697 // the right __SIZE_TYPE__.
698 if (size_t_ctype == &ulong_ctype)
699 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
700 else
701 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
702 add_pre_buffer("#weak_define __STDC__ 1\n");
704 switch (standard)
706 case STANDARD_C89:
707 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
708 break;
710 case STANDARD_C94:
711 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
712 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
713 break;
715 case STANDARD_C99:
716 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
717 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
718 break;
720 case STANDARD_GNU89:
721 break;
723 case STANDARD_GNU99:
724 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
725 break;
727 default:
728 assert (0);
731 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
732 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
733 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
734 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
735 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
736 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
737 add_pre_buffer("#define __builtin_va_end(arg)\n");
739 /* FIXME! We need to do these as special magic macros at expansion time! */
740 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
742 if (optimize)
743 add_pre_buffer("#define __OPTIMIZE__ 1\n");
744 if (optimize_size)
745 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
748 static struct symbol_list *sparse_tokenstream(struct token *token)
750 // Preprocess the stream
751 token = preprocess(token);
753 if (preprocess_only) {
754 while (!eof_token(token)) {
755 int prec = 1;
756 struct token *next = token->next;
757 const char *separator = "";
758 if (next->pos.whitespace)
759 separator = " ";
760 if (next->pos.newline) {
761 separator = "\n\t\t\t\t\t";
762 prec = next->pos.pos;
763 if (prec > 4)
764 prec = 4;
766 printf("%s%.*s", show_token(token), prec, separator);
767 token = next;
769 putchar('\n');
771 return NULL;
774 // Parse the resulting C code
775 while (!eof_token(token))
776 token = external_declaration(token, &translation_unit_used_list);
777 return translation_unit_used_list;
780 static struct symbol_list *sparse_file(const char *filename)
782 int fd;
783 struct token *token;
785 if (strcmp (filename, "-") == 0) {
786 fd = 0;
787 } else {
788 fd = open(filename, O_RDONLY);
789 if (fd < 0)
790 die("No such file: %s", filename);
793 // Tokenize the input stream
794 token = tokenize(filename, fd, NULL, includepath);
795 close(fd);
797 return sparse_tokenstream(token);
801 * This handles the "-include" directive etc: we're in global
802 * scope, and all types/macros etc will affect all the following
803 * files.
805 * NOTE NOTE NOTE! "#undef" of anything in this stage will
806 * affect all subsequent files too, i.e. we can have non-local
807 * behaviour between files!
809 static struct symbol_list *sparse_initial(void)
811 struct token *token;
812 int i;
814 // Prepend any "include" file to the stream.
815 // We're in global scope, it will affect all files!
816 token = NULL;
817 for (i = cmdline_include_nr - 1; i >= 0; i--)
818 token = tokenize(cmdline_include[i].filename, cmdline_include[i].fd,
819 token, includepath);
821 // Prepend the initial built-in stream
822 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
823 return sparse_tokenstream(token);
826 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
828 char **args;
829 struct symbol_list *list;
831 // Initialize symbol stream first, so that we can add defines etc
832 init_symbols();
834 args = argv;
835 for (;;) {
836 char *arg = *++args;
837 if (!arg)
838 break;
840 if (arg[0] == '-' && arg[1]) {
841 args = handle_switch(arg+1, args);
842 continue;
844 add_ptr_list_notag(filelist, arg);
846 handle_switch_W_finalize();
847 handle_switch_v_finalize();
849 list = NULL;
850 if (!ptr_list_empty(filelist)) {
851 // Initialize type system
852 init_ctype();
854 create_builtin_stream();
855 add_pre_buffer("#define __CHECKER__ 1\n");
856 if (!preprocess_only)
857 declare_builtin_functions();
859 list = sparse_initial();
862 * Protect the initial token allocations, since
863 * they need to survive all the others
865 protect_token_alloc();
867 return list;
870 struct symbol_list * sparse_keep_tokens(char *filename)
872 struct symbol_list *res;
874 /* Clear previous symbol list */
875 translation_unit_used_list = NULL;
877 new_file_scope();
878 res = sparse_file(filename);
880 /* And return it */
881 return res;
885 struct symbol_list * __sparse(char *filename)
887 struct symbol_list *res;
889 res = sparse_keep_tokens(filename);
891 /* Drop the tokens for this file after parsing */
892 clear_token_alloc();
894 /* And return it */
895 return res;
898 struct symbol_list * sparse(char *filename)
900 struct symbol_list *res = __sparse(filename);
902 /* Evaluate the complete symbol list */
903 evaluate_symbol_list(res);
905 return res;