comparison: improve "foo = min(...);" assignment handling
[smatch.git] / lib.c
blobb03229c01c40b34fdecdc01cef2fb35f3f714cc6
1 /*
2 * 'sparse' library helper routines.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <stdarg.h>
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <assert.h>
35 #include <sys/types.h>
37 #include "lib.h"
38 #include "allocate.h"
39 #include "token.h"
40 #include "parse.h"
41 #include "symbol.h"
42 #include "expression.h"
43 #include "scope.h"
44 #include "linearize.h"
45 #include "target.h"
46 #include "version.h"
48 int verbose, optimize, optimize_size, preprocessing;
49 int die_if_error = 0;
50 int parse_error;
51 int has_error = 0;
53 #ifndef __GNUC__
54 # define __GNUC__ 2
55 # define __GNUC_MINOR__ 95
56 # define __GNUC_PATCHLEVEL__ 0
57 #endif
59 int gcc_major = __GNUC__;
60 int gcc_minor = __GNUC_MINOR__;
61 int gcc_patchlevel = __GNUC_PATCHLEVEL__;
63 static const char *gcc_base_dir = GCC_BASE;
64 static const char *multiarch_dir = MULTIARCH_TRIPLET;
66 struct token *skip_to(struct token *token, int op)
68 while (!match_op(token, op) && !eof_token(token))
69 token = token->next;
70 return token;
73 struct token *expect(struct token *token, int op, const char *where)
75 if (!match_op(token, op)) {
76 static struct token bad_token;
77 if (token != &bad_token) {
78 bad_token.next = token;
79 sparse_error(token->pos, "Expected %s %s", show_special(op), where);
80 sparse_error(token->pos, "got %s", show_token(token));
82 if (op == ';')
83 return skip_to(token, op);
84 return &bad_token;
86 return token->next;
89 unsigned int hexval(unsigned int c)
91 int retval = 256;
92 switch (c) {
93 case '0'...'9':
94 retval = c - '0';
95 break;
96 case 'a'...'f':
97 retval = c - 'a' + 10;
98 break;
99 case 'A'...'F':
100 retval = c - 'A' + 10;
101 break;
103 return retval;
106 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
108 static char buffer[512];
109 const char *name;
111 vsprintf(buffer, fmt, args);
112 name = stream_name(pos.stream);
114 fprintf(stderr, "%s:%d:%d: %s%s\n",
115 name, pos.line, pos.pos, type, buffer);
118 static int max_warnings = 100;
119 static int show_info = 1;
121 void info(struct position pos, const char * fmt, ...)
123 va_list args;
125 if (!show_info)
126 return;
127 va_start(args, fmt);
128 do_warn("", pos, fmt, args);
129 va_end(args);
132 static void do_error(struct position pos, const char * fmt, va_list args)
134 static int errors = 0;
136 parse_error = 1;
137 die_if_error = 1;
138 show_info = 1;
139 /* Shut up warnings after an error */
140 has_error |= ERROR_CURR_PHASE;
141 if (errors > 100) {
142 static int once = 0;
143 show_info = 0;
144 if (once)
145 return;
146 fmt = "too many errors";
147 once = 1;
150 do_warn("error: ", pos, fmt, args);
151 errors++;
154 void warning(struct position pos, const char * fmt, ...)
156 va_list args;
158 if (Wsparse_error) {
159 va_start(args, fmt);
160 do_error(pos, fmt, args);
161 va_end(args);
162 return;
165 if (!max_warnings || has_error) {
166 show_info = 0;
167 return;
170 if (!--max_warnings) {
171 show_info = 0;
172 fmt = "too many warnings";
175 va_start(args, fmt);
176 do_warn("warning: ", pos, fmt, args);
177 va_end(args);
180 void sparse_error(struct position pos, const char * fmt, ...)
182 va_list args;
183 va_start(args, fmt);
184 do_error(pos, fmt, args);
185 va_end(args);
188 void expression_error(struct expression *expr, const char *fmt, ...)
190 va_list args;
191 va_start(args, fmt);
192 do_error(expr->pos, fmt, args);
193 va_end(args);
194 expr->ctype = &bad_ctype;
197 NORETURN_ATTR
198 void error_die(struct position pos, const char * fmt, ...)
200 va_list args;
201 va_start(args, fmt);
202 do_warn("error: ", pos, fmt, args);
203 va_end(args);
204 exit(1);
207 NORETURN_ATTR
208 void die(const char *fmt, ...)
210 va_list args;
211 static char buffer[512];
213 va_start(args, fmt);
214 vsnprintf(buffer, sizeof(buffer), fmt, args);
215 va_end(args);
217 fprintf(stderr, "%s\n", buffer);
218 exit(1);
221 static struct token *pre_buffer_begin = NULL;
222 static struct token *pre_buffer_end = NULL;
224 int Waddress = 0;
225 int Waddress_space = 1;
226 int Wbitwise = 1;
227 int Wcast_to_as = 0;
228 int Wcast_truncate = 1;
229 int Wconstexpr_not_const = 0;
230 int Wcontext = 1;
231 int Wdecl = 1;
232 int Wdeclarationafterstatement = -1;
233 int Wdefault_bitfield_sign = 0;
234 int Wdesignated_init = 1;
235 int Wdo_while = 0;
236 int Winit_cstring = 0;
237 int Wenum_mismatch = 1;
238 int Wsparse_error = 0;
239 int Wmemcpy_max_count = 1;
240 int Wnon_pointer_null = 1;
241 int Wold_initializer = 1;
242 int Wone_bit_signed_bitfield = 1;
243 int Woverride_init = 1;
244 int Woverride_init_all = 0;
245 int Woverride_init_whole_range = 0;
246 int Wparen_string = 0;
247 int Wpointer_arith = 0;
248 int Wptr_subtraction_blows = 0;
249 int Wreturn_void = 0;
250 int Wshadow = 0;
251 int Wsizeof_bool = 0;
252 int Wtautological_compare = 0;
253 int Wtransparent_union = 0;
254 int Wtypesign = 0;
255 int Wundef = 0;
256 int Wuninitialized = 1;
257 int Wunknown_attribute = 0;
258 int Wvla = 1;
260 int dump_macro_defs = 0;
262 int dbg_entry = 0;
263 int dbg_dead = 0;
265 int fmem_report = 0;
266 int fdump_linearize;
267 unsigned long long fmemcpy_max_count = 100000;
269 int preprocess_only;
271 static enum { STANDARD_C89,
272 STANDARD_C94,
273 STANDARD_C99,
274 STANDARD_C11,
275 STANDARD_GNU11,
276 STANDARD_GNU89,
277 STANDARD_GNU99, } standard = STANDARD_GNU89;
279 #define ARCH_LP32 0
280 #define ARCH_LP64 1
281 #define ARCH_LLP64 2
283 #ifdef __x86_64__
284 #define ARCH_M64_DEFAULT ARCH_LP64
285 #else
286 #define ARCH_M64_DEFAULT ARCH_LP32
287 #endif
289 int arch_m64 = ARCH_M64_DEFAULT;
290 int arch_msize_long = 0;
292 #ifdef __BIG_ENDIAN__
293 #define ARCH_BIG_ENDIAN 1
294 #else
295 #define ARCH_BIG_ENDIAN 0
296 #endif
297 int arch_big_endian = ARCH_BIG_ENDIAN;
300 #define CMDLINE_INCLUDE 20
301 static int cmdline_include_nr = 0;
302 static char *cmdline_include[CMDLINE_INCLUDE];
305 void add_pre_buffer(const char *fmt, ...)
307 va_list args;
308 unsigned int size;
309 struct token *begin, *end;
310 char buffer[4096];
312 va_start(args, fmt);
313 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
314 va_end(args);
315 begin = tokenize_buffer(buffer, size, &end);
316 if (!pre_buffer_begin)
317 pre_buffer_begin = begin;
318 if (pre_buffer_end)
319 pre_buffer_end->next = begin;
320 pre_buffer_end = end;
323 static char **handle_switch_D(char *arg, char **next)
325 const char *name = arg + 1;
326 const char *value = "1";
328 if (!*name || isspace((unsigned char)*name))
329 die("argument to `-D' is missing");
331 for (;;) {
332 char c;
333 c = *++arg;
334 if (!c)
335 break;
336 if (isspace((unsigned char)c) || c == '=') {
337 *arg = '\0';
338 value = arg + 1;
339 break;
342 add_pre_buffer("#define %s %s\n", name, value);
343 return next;
346 static char **handle_switch_E(char *arg, char **next)
348 if (arg[1] == '\0')
349 preprocess_only = 1;
350 return next;
353 static char **handle_switch_I(char *arg, char **next)
355 char *path = arg+1;
357 switch (arg[1]) {
358 case '-':
359 add_pre_buffer("#split_include\n");
360 break;
362 case '\0': /* Plain "-I" */
363 path = *++next;
364 if (!path)
365 die("missing argument for -I option");
366 /* Fall through */
367 default:
368 add_pre_buffer("#add_include \"%s/\"\n", path);
370 return next;
373 static void add_cmdline_include(char *filename)
375 if (cmdline_include_nr >= CMDLINE_INCLUDE)
376 die("too many include files for %s\n", filename);
377 cmdline_include[cmdline_include_nr++] = filename;
380 static char **handle_switch_i(char *arg, char **next)
382 if (*next && !strcmp(arg, "include"))
383 add_cmdline_include(*++next);
384 else if (*next && !strcmp(arg, "imacros"))
385 add_cmdline_include(*++next);
386 else if (*next && !strcmp(arg, "isystem")) {
387 char *path = *++next;
388 if (!path)
389 die("missing argument for -isystem option");
390 add_pre_buffer("#add_isystem \"%s/\"\n", path);
391 } else if (*next && !strcmp(arg, "idirafter")) {
392 char *path = *++next;
393 if (!path)
394 die("missing argument for -idirafter option");
395 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
397 return next;
400 static char **handle_switch_M(char *arg, char **next)
402 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
403 if (!*next)
404 die("missing argument for -%s option", arg);
405 return next + 1;
407 return next;
410 static char **handle_multiarch_dir(char *arg, char **next)
412 multiarch_dir = *++next;
413 if (!multiarch_dir)
414 die("missing argument for -multiarch-dir option");
415 return next;
418 static char **handle_switch_m(char *arg, char **next)
420 if (!strcmp(arg, "m64")) {
421 arch_m64 = ARCH_LP64;
422 } else if (!strcmp(arg, "m32")) {
423 arch_m64 = ARCH_LP32;
424 } else if (!strcmp(arg, "msize-llp64")) {
425 arch_m64 = ARCH_LLP64;
426 } else if (!strcmp(arg, "msize-long")) {
427 arch_msize_long = 1;
428 } else if (!strcmp(arg, "multiarch-dir")) {
429 return handle_multiarch_dir(arg, next);
430 } else if (!strcmp(arg, "mbig-endian")) {
431 arch_big_endian = 1;
432 } else if (!strcmp(arg, "mlittle-endian")) {
433 arch_big_endian = 0;
435 return next;
438 static void handle_arch_m64_finalize(void)
440 switch (arch_m64) {
441 case ARCH_LP32:
442 /* default values */
443 return;
444 case ARCH_LP64:
445 bits_in_long = 64;
446 max_int_alignment = 8;
447 size_t_ctype = &ulong_ctype;
448 ssize_t_ctype = &long_ctype;
449 add_pre_buffer("#weak_define __LP64__ 1\n");
450 add_pre_buffer("#weak_define _LP64 1\n");
451 goto case_64bit_common;
452 case ARCH_LLP64:
453 bits_in_long = 32;
454 max_int_alignment = 4;
455 size_t_ctype = &ullong_ctype;
456 ssize_t_ctype = &llong_ctype;
457 add_pre_buffer("#weak_define __LLP64__ 1\n");
458 goto case_64bit_common;
459 case_64bit_common:
460 bits_in_pointer = 64;
461 pointer_alignment = 8;
462 #ifdef __x86_64__
463 add_pre_buffer("#weak_define __x86_64__ 1\n");
464 #endif
465 break;
469 static void handle_arch_msize_long_finalize(void)
471 if (arch_msize_long) {
472 size_t_ctype = &ulong_ctype;
473 ssize_t_ctype = &long_ctype;
477 static void handle_arch_finalize(void)
479 handle_arch_m64_finalize();
480 handle_arch_msize_long_finalize();
484 static int handle_simple_switch(const char *arg, const char *name, int *flag)
486 int val = 1;
488 // Prefixe "no-" mean to turn flag off.
489 if (strncmp(arg, "no-", 3) == 0) {
490 arg += 3;
491 val = 0;
494 if (strcmp(arg, name) == 0) {
495 *flag = val;
496 return 1;
499 // not handled
500 return 0;
503 static char **handle_switch_o(char *arg, char **next)
505 if (!strcmp (arg, "o")) { // "-o foo"
506 if (!*++next)
507 die("argument to '-o' is missing");
509 // else "-ofoo"
511 return next;
514 static const struct warning {
515 const char *name;
516 int *flag;
517 } warnings[] = {
518 { "address", &Waddress },
519 { "address-space", &Waddress_space },
520 { "bitwise", &Wbitwise },
521 { "cast-to-as", &Wcast_to_as },
522 { "cast-truncate", &Wcast_truncate },
523 { "constexpr-not-const", &Wconstexpr_not_const},
524 { "context", &Wcontext },
525 { "decl", &Wdecl },
526 { "declaration-after-statement", &Wdeclarationafterstatement },
527 { "default-bitfield-sign", &Wdefault_bitfield_sign },
528 { "designated-init", &Wdesignated_init },
529 { "do-while", &Wdo_while },
530 { "enum-mismatch", &Wenum_mismatch },
531 { "init-cstring", &Winit_cstring },
532 { "memcpy-max-count", &Wmemcpy_max_count },
533 { "non-pointer-null", &Wnon_pointer_null },
534 { "old-initializer", &Wold_initializer },
535 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
536 { "override-init", &Woverride_init },
537 { "override-init-all", &Woverride_init_all },
538 { "paren-string", &Wparen_string },
539 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
540 { "return-void", &Wreturn_void },
541 { "shadow", &Wshadow },
542 { "sizeof-bool", &Wsizeof_bool },
543 { "pointer-arith", &Wpointer_arith },
544 { "sparse-error", &Wsparse_error },
545 { "tautological-compare", &Wtautological_compare },
546 { "transparent-union", &Wtransparent_union },
547 { "typesign", &Wtypesign },
548 { "undef", &Wundef },
549 { "uninitialized", &Wuninitialized },
550 { "unknown-attribute", &Wunknown_attribute },
551 { "vla", &Wvla },
554 enum {
555 WARNING_OFF,
556 WARNING_ON,
557 WARNING_FORCE_OFF
561 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
563 int flag = WARNING_ON;
564 char *p = arg + 1;
565 unsigned i;
567 if (!strcmp(p, "sparse-all")) {
568 for (i = 0; i < n; i++) {
569 if (*warnings[i].flag != WARNING_FORCE_OFF && warnings[i].flag != &Wsparse_error)
570 *warnings[i].flag = WARNING_ON;
574 // Prefixes "no" and "no-" mean to turn warning off.
575 if (p[0] == 'n' && p[1] == 'o') {
576 p += 2;
577 if (p[0] == '-')
578 p++;
579 flag = WARNING_FORCE_OFF;
582 for (i = 0; i < n; i++) {
583 if (!strcmp(p,warnings[i].name)) {
584 *warnings[i].flag = flag;
585 return next;
589 // Unknown.
590 return NULL;
593 static char **handle_switch_W(char *arg, char **next)
595 char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
596 if (ret)
597 return ret;
599 // Unknown.
600 return next;
603 static struct warning debugs[] = {
604 { "entry", &dbg_entry},
605 { "dead", &dbg_dead},
609 static char **handle_switch_v(char *arg, char **next)
611 char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
612 if (ret)
613 return ret;
615 // Unknown.
616 do {
617 verbose++;
618 } while (*++arg == 'v');
619 return next;
622 static struct warning dumps[] = {
623 { "D", &dump_macro_defs},
626 static char **handle_switch_d(char *arg, char **next)
628 char ** ret = handle_onoff_switch(arg, next, dumps, ARRAY_SIZE(dumps));
629 if (ret)
630 return ret;
632 return next;
636 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
638 unsigned i;
640 for (i = 0; i < n; i++) {
641 if (*warnings[i].flag == WARNING_FORCE_OFF)
642 *warnings[i].flag = WARNING_OFF;
646 static void handle_switch_W_finalize(void)
648 handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
650 /* default Wdeclarationafterstatement based on the C dialect */
651 if (-1 == Wdeclarationafterstatement)
653 switch (standard)
655 case STANDARD_C89:
656 case STANDARD_C94:
657 Wdeclarationafterstatement = 1;
658 break;
660 case STANDARD_C99:
661 case STANDARD_GNU89:
662 case STANDARD_GNU99:
663 case STANDARD_C11:
664 case STANDARD_GNU11:
665 Wdeclarationafterstatement = 0;
666 break;
668 default:
669 assert (0);
675 static void handle_switch_v_finalize(void)
677 handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
680 static char **handle_switch_U(char *arg, char **next)
682 const char *name = arg + 1;
683 add_pre_buffer ("#undef %s\n", name);
684 return next;
687 static char **handle_switch_O(char *arg, char **next)
689 int level = 1;
690 if (arg[1] >= '0' && arg[1] <= '9')
691 level = arg[1] - '0';
692 optimize = level;
693 optimize_size = arg[1] == 's';
694 return next;
697 static char **handle_switch_fmemcpy_max_count(char *arg, char **next)
699 unsigned long long val;
700 char *end;
702 val = strtoull(arg, &end, 0);
703 if (*end != '\0' || end == arg)
704 die("error: missing argument to \"-fmemcpy-max-count=\"");
706 if (val == 0)
707 val = ~0ULL;
708 fmemcpy_max_count = val;
709 return next;
712 static char **handle_switch_ftabstop(char *arg, char **next)
714 char *end;
715 unsigned long val;
717 if (*arg == '\0')
718 die("error: missing argument to \"-ftabstop=\"");
720 /* we silently ignore silly values */
721 val = strtoul(arg, &end, 10);
722 if (*end == '\0' && 1 <= val && val <= 100)
723 tabstop = val;
725 return next;
728 static int funsigned_char;
729 static void handle_funsigned_char(void)
731 if (funsigned_char) {
732 char_ctype.ctype.modifiers &= ~MOD_SIGNED;
733 char_ctype.ctype.modifiers |= MOD_UNSIGNED;
737 static char **handle_switch_fdump(char *arg, char **next)
739 if (!strncmp(arg, "linearize", 9)) {
740 arg += 9;
741 if (*arg == '\0')
742 fdump_linearize = 1;
743 else if (!strcmp(arg, "=only"))
744 fdump_linearize = 2;
745 else
746 goto err;
749 /* ignore others flags */
750 return next;
752 err:
753 die("error: unknown flag \"-fdump-%s\"", arg);
756 static char **handle_switch_f(char *arg, char **next)
758 arg++;
760 if (!strncmp(arg, "tabstop=", 8))
761 return handle_switch_ftabstop(arg+8, next);
762 if (!strncmp(arg, "dump-", 5))
763 return handle_switch_fdump(arg+5, next);
764 if (!strncmp(arg, "memcpy-max-count=", 17))
765 return handle_switch_fmemcpy_max_count(arg+17, next);
767 if (!strcmp(arg, "unsigned-char")) {
768 funsigned_char = 1;
769 return next;
772 /* handle switches w/ arguments above, boolean and only boolean below */
773 if (handle_simple_switch(arg, "mem-report", &fmem_report))
774 return next;
776 return next;
779 static char **handle_switch_G(char *arg, char **next)
781 if (!strcmp (arg, "G") && *next)
782 return next + 1; // "-G 0"
783 else
784 return next; // "-G0" or (bogus) terminal "-G"
787 static char **handle_switch_a(char *arg, char **next)
789 if (!strcmp (arg, "ansi"))
790 standard = STANDARD_C89;
792 return next;
795 static char **handle_switch_s(char *arg, char **next)
797 if (!strncmp (arg, "std=", 4))
799 arg += 4;
801 if (!strcmp (arg, "c89") ||
802 !strcmp (arg, "iso9899:1990"))
803 standard = STANDARD_C89;
805 else if (!strcmp (arg, "iso9899:199409"))
806 standard = STANDARD_C94;
808 else if (!strcmp (arg, "c99") ||
809 !strcmp (arg, "c9x") ||
810 !strcmp (arg, "iso9899:1999") ||
811 !strcmp (arg, "iso9899:199x"))
812 standard = STANDARD_C99;
814 else if (!strcmp (arg, "gnu89"))
815 standard = STANDARD_GNU89;
817 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
818 standard = STANDARD_GNU99;
820 else if (!strcmp(arg, "c11") ||
821 !strcmp(arg, "c1x") ||
822 !strcmp(arg, "iso9899:2011"))
823 standard = STANDARD_C11;
825 else if (!strcmp(arg, "gnu11"))
826 standard = STANDARD_GNU11;
828 else
829 die ("Unsupported C dialect");
832 return next;
835 static char **handle_nostdinc(char *arg, char **next)
837 add_pre_buffer("#nostdinc\n");
838 return next;
841 static char **handle_switch_n(char *arg, char **next)
843 if (!strcmp (arg, "nostdinc"))
844 return handle_nostdinc(arg, next);
846 return next;
849 static char **handle_base_dir(char *arg, char **next)
851 gcc_base_dir = *++next;
852 if (!gcc_base_dir)
853 die("missing argument for -gcc-base-dir option");
854 return next;
857 static char **handle_no_lineno(char *arg, char **next)
859 no_lineno = 1;
860 return next;
863 static char **handle_switch_g(char *arg, char **next)
865 if (!strcmp (arg, "gcc-base-dir"))
866 return handle_base_dir(arg, next);
868 return next;
871 static char **handle_version(char *arg, char **next)
873 printf("%s\n", SPARSE_VERSION);
874 exit(0);
877 static char **handle_param(char *arg, char **next)
879 char *value = NULL;
881 /* For now just skip any '--param=*' or '--param *' */
882 if (*arg == '\0') {
883 value = *++next;
884 } else if (isspace((unsigned char)*arg) || *arg == '=') {
885 value = ++arg;
888 if (!value)
889 die("missing argument for --param option");
891 return next;
894 struct switches {
895 const char *name;
896 char **(*fn)(char *, char **);
897 unsigned int prefix:1;
900 static char **handle_long_options(char *arg, char **next)
902 static struct switches cmd[] = {
903 { "param", handle_param, 1 },
904 { "version", handle_version },
905 { "nostdinc", handle_nostdinc },
906 { "gcc-base-dir", handle_base_dir},
907 { "no-lineno", handle_no_lineno},
908 { NULL, NULL }
910 struct switches *s = cmd;
912 while (s->name) {
913 int optlen = strlen(s->name);
914 if (!strncmp(s->name, arg, optlen + !s->prefix))
915 return s->fn(arg + optlen, next);
916 s++;
918 return next;
921 static char **handle_switch(char *arg, char **next)
923 switch (*arg) {
924 case 'a': return handle_switch_a(arg, next);
925 case 'D': return handle_switch_D(arg, next);
926 case 'd': return handle_switch_d(arg, next);
927 case 'E': return handle_switch_E(arg, next);
928 case 'f': return handle_switch_f(arg, next);
929 case 'g': return handle_switch_g(arg, next);
930 case 'G': return handle_switch_G(arg, next);
931 case 'I': return handle_switch_I(arg, next);
932 case 'i': return handle_switch_i(arg, next);
933 case 'M': return handle_switch_M(arg, next);
934 case 'm': return handle_switch_m(arg, next);
935 case 'n': return handle_switch_n(arg, next);
936 case 'o': return handle_switch_o(arg, next);
937 case 'O': return handle_switch_O(arg, next);
938 case 's': return handle_switch_s(arg, next);
939 case 'U': return handle_switch_U(arg, next);
940 case 'v': return handle_switch_v(arg, next);
941 case 'W': return handle_switch_W(arg, next);
942 case '-': return handle_long_options(arg + 1, next);
943 default:
944 break;
948 * Ignore unknown command line options:
949 * they're probably gcc switches
951 return next;
954 static void predefined_sizeof(const char *name, unsigned bits)
956 add_pre_buffer("#weak_define __SIZEOF_%s__ %d\n", name, bits/8);
959 static void predefined_max(const char *name, const char *suffix, unsigned bits)
961 unsigned long long max = (1ULL << (bits - 1 )) - 1;
963 add_pre_buffer("#weak_define __%s_MAX__ %#llx%s\n", name, max, suffix);
966 static void predefined_type_size(const char *name, const char *suffix, unsigned bits)
968 predefined_max(name, suffix, bits);
969 predefined_sizeof(name, bits);
972 static void predefined_macros(void)
974 add_pre_buffer("#define __CHECKER__ 1\n");
976 predefined_sizeof("SHORT", bits_in_short);
977 predefined_max("SHRT", "", bits_in_short);
978 predefined_max("SCHAR", "", bits_in_char);
979 predefined_max("WCHAR", "", bits_in_wchar);
980 add_pre_buffer("#weak_define __CHAR_BIT__ %d\n", bits_in_char);
982 predefined_type_size("INT", "", bits_in_int);
983 predefined_type_size("LONG", "L", bits_in_long);
984 predefined_type_size("LONG_LONG", "LL", bits_in_longlong);
986 predefined_sizeof("INT128", 128);
988 predefined_sizeof("SIZE_T", bits_in_pointer);
989 predefined_sizeof("PTRDIFF_T", bits_in_pointer);
990 predefined_sizeof("POINTER", bits_in_pointer);
992 predefined_sizeof("FLOAT", bits_in_float);
993 predefined_sizeof("DOUBLE", bits_in_double);
994 predefined_sizeof("LONG_DOUBLE", bits_in_longdouble);
996 add_pre_buffer("#weak_define __%s_ENDIAN__ 1\n",
997 arch_big_endian ? "BIG" : "LITTLE");
999 add_pre_buffer("#weak_define __ORDER_LITTLE_ENDIAN__ 1234\n");
1000 add_pre_buffer("#weak_define __ORDER_BIG_ENDIAN__ 4321\n");
1001 add_pre_buffer("#weak_define __ORDER_PDP_ENDIAN__ 3412\n");
1002 add_pre_buffer("#weak_define __BYTE_ORDER__ __ORDER_%s_ENDIAN__\n",
1003 arch_big_endian ? "BIG" : "LITTLE");
1006 void declare_builtin_functions(void)
1008 /* Gaah. gcc knows tons of builtin <string.h> functions */
1009 add_pre_buffer("extern void *__builtin_memchr(const void *, int, __SIZE_TYPE__);\n");
1010 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
1011 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
1012 add_pre_buffer("extern void *__builtin_memmove(void *, const void *, __SIZE_TYPE__);\n");
1013 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
1014 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
1015 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
1016 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
1017 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
1018 add_pre_buffer("extern int __builtin_strncmp(const char *, const char *, __SIZE_TYPE__);\n");
1019 add_pre_buffer("extern int __builtin_strcasecmp(const char *, const char *);\n");
1020 add_pre_buffer("extern int __builtin_strncasecmp(const char *, const char *, __SIZE_TYPE__);\n");
1021 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
1022 add_pre_buffer("extern char *__builtin_strrchr(const char *, int);\n");
1023 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
1024 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
1025 add_pre_buffer("extern char *__builtin_strdup(const char *);\n");
1026 add_pre_buffer("extern char *__builtin_strndup(const char *, __SIZE_TYPE__);\n");
1027 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
1028 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
1029 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
1030 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
1031 add_pre_buffer("extern char* __builtin_stpncpy(const char *, const char*, __SIZE_TYPE__);\n");
1032 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
1033 add_pre_buffer("extern char *__builtin_strstr(const char *, const char *);\n");
1034 add_pre_buffer("extern char *__builtin_strcasestr(const char *, const char *);\n");
1035 add_pre_buffer("extern char *__builtin_strnstr(const char *, const char *, __SIZE_TYPE__);\n");
1037 /* And even some from <strings.h> */
1038 add_pre_buffer("extern int __builtin_bcmp(const void *, const void *, __SIZE_TYPE__);\n");
1039 add_pre_buffer("extern void __builtin_bcopy(const void *, void *, __SIZE_TYPE__);\n");
1040 add_pre_buffer("extern void __builtin_bzero(void *, __SIZE_TYPE__);\n");
1041 add_pre_buffer("extern char*__builtin_index(const char *, int);\n");
1042 add_pre_buffer("extern char*__builtin_rindex(const char *, int);\n");
1044 /* And bitwise operations.. */
1045 add_pre_buffer("extern int __builtin_clrsb(int);\n");
1046 add_pre_buffer("extern int __builtin_clrsbl(long);\n");
1047 add_pre_buffer("extern int __builtin_clrsbll(long long);\n");
1048 add_pre_buffer("extern int __builtin_clz(int);\n");
1049 add_pre_buffer("extern int __builtin_clzl(long);\n");
1050 add_pre_buffer("extern int __builtin_clzll(long long);\n");
1051 add_pre_buffer("extern int __builtin_ctz(int);\n");
1052 add_pre_buffer("extern int __builtin_ctzl(long);\n");
1053 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
1054 add_pre_buffer("extern int __builtin_ffs(int);\n");
1055 add_pre_buffer("extern int __builtin_ffsl(long);\n");
1056 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
1057 add_pre_buffer("extern int __builtin_parity(unsigned int);\n");
1058 add_pre_buffer("extern int __builtin_parityl(unsigned long);\n");
1059 add_pre_buffer("extern int __builtin_parityll(unsigned long long);\n");
1060 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
1061 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
1062 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
1064 /* And byte swaps.. */
1065 add_pre_buffer("extern unsigned short __builtin_bswap16(unsigned short);\n");
1066 add_pre_buffer("extern unsigned int __builtin_bswap32(unsigned int);\n");
1067 add_pre_buffer("extern unsigned long long __builtin_bswap64(unsigned long long);\n");
1069 /* And atomic memory access functions.. */
1070 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
1071 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
1072 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
1073 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
1074 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
1075 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
1076 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
1077 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
1078 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
1079 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
1080 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
1081 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
1082 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
1083 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
1084 add_pre_buffer("extern void __sync_synchronize();\n");
1085 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
1086 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
1088 /* And some random ones.. */
1089 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
1090 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
1091 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
1092 add_pre_buffer("extern void __builtin_trap(void);\n");
1093 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
1094 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
1095 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
1096 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
1097 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
1098 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
1099 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
1100 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
1101 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
1102 add_pre_buffer("extern int __builtin_abs(int);\n");
1103 add_pre_buffer("extern long __builtin_labs(long);\n");
1104 add_pre_buffer("extern long long __builtin_llabs(long long);\n");
1105 add_pre_buffer("extern double __builtin_fabs(double);\n");
1106 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
1108 /* Add Blackfin-specific stuff */
1109 add_pre_buffer(
1110 "#ifdef __bfin__\n"
1111 "extern void __builtin_bfin_csync(void);\n"
1112 "extern void __builtin_bfin_ssync(void);\n"
1113 "extern int __builtin_bfin_norm_fr1x32(int);\n"
1114 "#endif\n"
1117 /* And some floating point stuff.. */
1118 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
1119 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
1120 add_pre_buffer("extern int __builtin_isless(float, float);\n");
1121 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
1122 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
1123 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
1125 /* And some INFINITY / NAN stuff.. */
1126 add_pre_buffer("extern double __builtin_huge_val(void);\n");
1127 add_pre_buffer("extern float __builtin_huge_valf(void);\n");
1128 add_pre_buffer("extern long double __builtin_huge_vall(void);\n");
1129 add_pre_buffer("extern double __builtin_inf(void);\n");
1130 add_pre_buffer("extern float __builtin_inff(void);\n");
1131 add_pre_buffer("extern long double __builtin_infl(void);\n");
1132 add_pre_buffer("extern double __builtin_nan(const char *);\n");
1133 add_pre_buffer("extern float __builtin_nanf(const char *);\n");
1134 add_pre_buffer("extern long double __builtin_nanl(const char *);\n");
1135 add_pre_buffer("extern int __builtin_isinf_sign(float);\n");
1136 add_pre_buffer("extern int __builtin_isfinite(float);\n");
1137 add_pre_buffer("extern int __builtin_isnan(float);\n");
1139 /* And some __FORTIFY_SOURCE ones.. */
1140 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(const void *, int);\n");
1141 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1142 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1143 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1144 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1145 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
1146 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
1147 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
1148 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
1149 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
1150 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1151 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1152 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
1153 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
1154 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
1156 /* And some from <stdlib.h> */
1157 add_pre_buffer("extern void __builtin_abort(void);\n");
1158 add_pre_buffer("extern void *__builtin_calloc(__SIZE_TYPE__, __SIZE_TYPE__);\n");
1159 add_pre_buffer("extern void __builtin_exit(int);\n");
1160 add_pre_buffer("extern void *__builtin_malloc(__SIZE_TYPE__);\n");
1161 add_pre_buffer("extern void *__builtin_realloc(void *, __SIZE_TYPE__);\n");
1162 add_pre_buffer("extern void __builtin_free(void *);\n");
1164 /* And some from <stdio.h> */
1165 add_pre_buffer("extern int __builtin_printf(const char *, ...);\n");
1166 add_pre_buffer("extern int __builtin_sprintf(char *, const char *, ...);\n");
1167 add_pre_buffer("extern int __builtin_snprintf(char *, __SIZE_TYPE__, const char *, ...);\n");
1168 add_pre_buffer("extern int __builtin_puts(const char *);\n");
1169 add_pre_buffer("extern int __builtin_vprintf(const char *, __builtin_va_list);\n");
1170 add_pre_buffer("extern int __builtin_vsprintf(char *, const char *, __builtin_va_list);\n");
1171 add_pre_buffer("extern int __builtin_vsnprintf(char *, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
1174 void create_builtin_stream(void)
1176 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
1177 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
1178 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
1180 /* add the multiarch include directories, if any */
1181 if (multiarch_dir && *multiarch_dir) {
1182 add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir);
1183 add_pre_buffer("#add_system \"/usr/local/include/%s\"\n", multiarch_dir);
1186 /* We add compiler headers path here because we have to parse
1187 * the arguments to get it, falling back to default. */
1188 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
1189 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
1191 add_pre_buffer("#define __extension__\n");
1192 add_pre_buffer("#define __pragma__\n");
1193 add_pre_buffer("#define _Pragma(x)\n");
1195 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
1196 // solaris/sparc that is really "unsigned int" and for linux/x86_64
1197 // it is "long unsigned int". In either case we can probably
1198 // get away with this. We need the #weak_define as cgcc will define
1199 // the right __SIZE_TYPE__.
1200 if (size_t_ctype == &ulong_ctype)
1201 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
1202 else
1203 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
1204 add_pre_buffer("#weak_define __STDC__ 1\n");
1206 switch (standard)
1208 case STANDARD_C89:
1209 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1210 break;
1212 case STANDARD_C94:
1213 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
1214 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1215 break;
1217 case STANDARD_C99:
1218 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1219 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1220 break;
1222 case STANDARD_GNU89:
1223 break;
1225 case STANDARD_GNU99:
1226 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1227 break;
1229 case STANDARD_C11:
1230 add_pre_buffer("#weak_define __STRICT_ANSI__ 1\n");
1231 case STANDARD_GNU11:
1232 add_pre_buffer("#weak_define __STDC_NO_ATOMICS__ 1\n");
1233 add_pre_buffer("#weak_define __STDC_NO_COMPLEX__ 1\n");
1234 add_pre_buffer("#weak_define __STDC_NO_THREADS__ 1\n");
1235 add_pre_buffer("#weak_define __STDC_VERSION__ 201112L\n");
1236 break;
1238 default:
1239 assert (0);
1242 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1243 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1244 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
1245 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
1246 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
1247 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
1248 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1249 add_pre_buffer("#define __builtin_ms_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1250 add_pre_buffer("#define __builtin_va_end(arg)\n");
1251 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
1252 add_pre_buffer("#define __builtin_va_arg_pack()\n");
1254 /* FIXME! We need to do these as special magic macros at expansion time! */
1255 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
1257 if (optimize)
1258 add_pre_buffer("#define __OPTIMIZE__ 1\n");
1259 if (optimize_size)
1260 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
1263 static struct symbol_list *sparse_tokenstream(struct token *token)
1265 int builtin = token && !token->pos.stream;
1267 // Preprocess the stream
1268 token = preprocess(token);
1270 if (dump_macro_defs && !builtin)
1271 dump_macro_definitions();
1273 if (preprocess_only) {
1274 while (!eof_token(token)) {
1275 int prec = 1;
1276 struct token *next = token->next;
1277 const char *separator = "";
1278 if (next->pos.whitespace)
1279 separator = " ";
1280 if (next->pos.newline) {
1281 separator = "\n\t\t\t\t\t";
1282 prec = next->pos.pos;
1283 if (prec > 4)
1284 prec = 4;
1286 printf("%s%.*s", show_token(token), prec, separator);
1287 token = next;
1289 putchar('\n');
1291 return NULL;
1294 // Parse the resulting C code
1295 while (!eof_token(token))
1296 token = external_declaration(token, &translation_unit_used_list, NULL);
1297 return translation_unit_used_list;
1300 static struct symbol_list *sparse_file(const char *filename)
1302 int fd;
1303 struct token *token;
1305 if (strcmp (filename, "-") == 0) {
1306 fd = 0;
1307 } else {
1308 fd = open(filename, O_RDONLY);
1309 if (fd < 0)
1310 die("No such file: %s", filename);
1313 // Tokenize the input stream
1314 token = tokenize(filename, fd, NULL, includepath);
1315 store_all_tokens(token);
1316 close(fd);
1318 return sparse_tokenstream(token);
1322 * This handles the "-include" directive etc: we're in global
1323 * scope, and all types/macros etc will affect all the following
1324 * files.
1326 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1327 * affect all subsequent files too, i.e. we can have non-local
1328 * behaviour between files!
1330 static struct symbol_list *sparse_initial(void)
1332 int i;
1334 // Prepend any "include" file to the stream.
1335 // We're in global scope, it will affect all files!
1336 for (i = 0; i < cmdline_include_nr; i++)
1337 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]);
1339 return sparse_tokenstream(pre_buffer_begin);
1342 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
1344 char **args;
1345 struct symbol_list *list;
1347 // Initialize symbol stream first, so that we can add defines etc
1348 init_symbols();
1349 init_include_path();
1351 args = argv;
1352 for (;;) {
1353 char *arg = *++args;
1354 if (!arg)
1355 break;
1357 if (arg[0] == '-' && arg[1]) {
1358 args = handle_switch(arg+1, args);
1359 continue;
1361 add_ptr_list_notag(filelist, arg);
1363 handle_switch_W_finalize();
1364 handle_switch_v_finalize();
1366 handle_arch_finalize();
1368 list = NULL;
1369 if (!ptr_list_empty(filelist)) {
1370 // Initialize type system
1371 init_ctype();
1372 handle_funsigned_char();
1374 create_builtin_stream();
1375 predefined_macros();
1376 if (!preprocess_only)
1377 declare_builtin_functions();
1379 list = sparse_initial();
1382 * Protect the initial token allocations, since
1383 * they need to survive all the others
1385 protect_token_alloc();
1388 * Evaluate the complete symbol list
1389 * Note: This is not needed for normal cases.
1390 * These symbols should only be predefined defines and
1391 * declaratons which will be evaluated later, when needed.
1392 * This is also the case when a file is directly included via
1393 * '-include <file>' on the command line *AND* the file only
1394 * contains defines, declarations and inline definitions.
1395 * However, in the rare cases where the given file should
1396 * contain some definitions, these will never be evaluated
1397 * and thus won't be able to be linearized correctly.
1398 * Hence the evaluate_symbol_list() here under.
1400 evaluate_symbol_list(list);
1401 return list;
1404 struct symbol_list * sparse_keep_tokens(char *filename)
1406 struct symbol_list *res;
1408 /* Clear previous symbol list */
1409 translation_unit_used_list = NULL;
1411 new_file_scope();
1412 res = sparse_file(filename);
1414 /* And return it */
1415 return res;
1419 struct symbol_list * __sparse(char *filename)
1421 struct symbol_list *res;
1423 res = sparse_keep_tokens(filename);
1425 /* Drop the tokens for this file after parsing */
1426 clear_token_alloc();
1428 /* And return it */
1429 return res;
1432 struct symbol_list * sparse(char *filename)
1434 struct symbol_list *res = __sparse(filename);
1436 if (has_error & ERROR_CURR_PHASE)
1437 has_error = ERROR_PREV_PHASE;
1438 /* Evaluate the complete symbol list */
1439 evaluate_symbol_list(res);
1441 return res;