extra: pass hard max across function boundaries
[smatch.git] / lib.c
blob8924c2ebd2cfcb7f4a47ed3197b0a134fab77f7c
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 Wbig_constants = 1;
227 int Wbitwise = 1;
228 int Wcast_to_as = 0;
229 int Wcast_truncate = 1;
230 int Wconstexpr_not_const = 0;
231 int Wcontext = 1;
232 int Wdecl = 1;
233 int Wdeclarationafterstatement = -1;
234 int Wdefault_bitfield_sign = 0;
235 int Wdesignated_init = 1;
236 int Wdo_while = 0;
237 int Winit_cstring = 0;
238 int Wenum_mismatch = 1;
239 int Wsparse_error = 0;
240 int Wmemcpy_max_count = 1;
241 int Wnon_ansi_function_declaration = 1;
242 int Wnon_pointer_null = 1;
243 int Wold_initializer = 1;
244 int Wone_bit_signed_bitfield = 1;
245 int Woverride_init = 1;
246 int Woverride_init_all = 0;
247 int Woverride_init_whole_range = 0;
248 int Wparen_string = 0;
249 int Wpointer_arith = 0;
250 int Wptr_subtraction_blows = 0;
251 int Wreturn_void = 0;
252 int Wshadow = 0;
253 int Wsizeof_bool = 0;
254 int Wtautological_compare = 0;
255 int Wtransparent_union = 0;
256 int Wtypesign = 0;
257 int Wundef = 0;
258 int Wuninitialized = 1;
259 int Wunknown_attribute = 0;
260 int Wvla = 1;
262 int dump_macro_defs = 0;
264 int dbg_entry = 0;
265 int dbg_dead = 0;
267 int fmem_report = 0;
268 int fdump_linearize;
269 unsigned long long fmemcpy_max_count = 100000;
271 int preprocess_only;
273 static enum { STANDARD_C89,
274 STANDARD_C94,
275 STANDARD_C99,
276 STANDARD_C11,
277 STANDARD_GNU11,
278 STANDARD_GNU89,
279 STANDARD_GNU99, } standard = STANDARD_GNU89;
281 #define ARCH_LP32 0
282 #define ARCH_LP64 1
283 #define ARCH_LLP64 2
285 #ifdef __x86_64__
286 #define ARCH_M64_DEFAULT ARCH_LP64
287 #else
288 #define ARCH_M64_DEFAULT ARCH_LP32
289 #endif
291 int arch_m64 = ARCH_M64_DEFAULT;
292 int arch_msize_long = 0;
294 #ifdef __BIG_ENDIAN__
295 #define ARCH_BIG_ENDIAN 1
296 #else
297 #define ARCH_BIG_ENDIAN 0
298 #endif
299 int arch_big_endian = ARCH_BIG_ENDIAN;
302 #define CMDLINE_INCLUDE 20
303 static int cmdline_include_nr = 0;
304 static char *cmdline_include[CMDLINE_INCLUDE];
307 void add_pre_buffer(const char *fmt, ...)
309 va_list args;
310 unsigned int size;
311 struct token *begin, *end;
312 char buffer[4096];
314 va_start(args, fmt);
315 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
316 va_end(args);
317 begin = tokenize_buffer(buffer, size, &end);
318 if (!pre_buffer_begin)
319 pre_buffer_begin = begin;
320 if (pre_buffer_end)
321 pre_buffer_end->next = begin;
322 pre_buffer_end = end;
325 static char **handle_switch_D(char *arg, char **next)
327 const char *name = arg + 1;
328 const char *value = "1";
330 if (!*name) {
331 arg = *++next;
332 if (!arg)
333 die("argument to `-D' is missing");
334 name = arg;
337 for (;;arg++) {
338 char c;
339 c = *arg;
340 if (!c)
341 break;
342 if (c == '=') {
343 *arg = '\0';
344 value = arg + 1;
345 break;
348 add_pre_buffer("#define %s %s\n", name, value);
349 return next;
352 static char **handle_switch_E(char *arg, char **next)
354 if (arg[1] == '\0')
355 preprocess_only = 1;
356 return next;
359 static char **handle_switch_I(char *arg, char **next)
361 char *path = arg+1;
363 switch (arg[1]) {
364 case '-':
365 add_pre_buffer("#split_include\n");
366 break;
368 case '\0': /* Plain "-I" */
369 path = *++next;
370 if (!path)
371 die("missing argument for -I option");
372 /* Fall through */
373 default:
374 add_pre_buffer("#add_include \"%s/\"\n", path);
376 return next;
379 static void add_cmdline_include(char *filename)
381 if (cmdline_include_nr >= CMDLINE_INCLUDE)
382 die("too many include files for %s\n", filename);
383 cmdline_include[cmdline_include_nr++] = filename;
386 static char **handle_switch_i(char *arg, char **next)
388 if (*next && !strcmp(arg, "include"))
389 add_cmdline_include(*++next);
390 else if (*next && !strcmp(arg, "imacros"))
391 add_cmdline_include(*++next);
392 else if (*next && !strcmp(arg, "isystem")) {
393 char *path = *++next;
394 if (!path)
395 die("missing argument for -isystem option");
396 add_pre_buffer("#add_isystem \"%s/\"\n", path);
397 } else if (*next && !strcmp(arg, "idirafter")) {
398 char *path = *++next;
399 if (!path)
400 die("missing argument for -idirafter option");
401 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
403 return next;
406 static char **handle_switch_M(char *arg, char **next)
408 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
409 if (!*next)
410 die("missing argument for -%s option", arg);
411 return next + 1;
413 return next;
416 static char **handle_multiarch_dir(char *arg, char **next)
418 multiarch_dir = *++next;
419 if (!multiarch_dir)
420 die("missing argument for -multiarch-dir option");
421 return next;
424 static char **handle_switch_m(char *arg, char **next)
426 if (!strcmp(arg, "m64")) {
427 arch_m64 = ARCH_LP64;
428 } else if (!strcmp(arg, "m32")) {
429 arch_m64 = ARCH_LP32;
430 } else if (!strcmp(arg, "msize-llp64")) {
431 arch_m64 = ARCH_LLP64;
432 } else if (!strcmp(arg, "msize-long")) {
433 arch_msize_long = 1;
434 } else if (!strcmp(arg, "multiarch-dir")) {
435 return handle_multiarch_dir(arg, next);
436 } else if (!strcmp(arg, "mbig-endian")) {
437 arch_big_endian = 1;
438 } else if (!strcmp(arg, "mlittle-endian")) {
439 arch_big_endian = 0;
441 return next;
444 static void handle_arch_m64_finalize(void)
446 switch (arch_m64) {
447 case ARCH_LP32:
448 /* default values */
449 #if defined(__x86_64__) || defined (__i386)
450 add_pre_buffer("#weak_define __i386__ 1\n");
451 add_pre_buffer("#weak_define __i386 1\n");
452 #endif
453 return;
454 case ARCH_LP64:
455 bits_in_long = 64;
456 max_int_alignment = 8;
457 size_t_ctype = &ulong_ctype;
458 ssize_t_ctype = &long_ctype;
459 add_pre_buffer("#weak_define __LP64__ 1\n");
460 add_pre_buffer("#weak_define _LP64 1\n");
461 goto case_64bit_common;
462 case ARCH_LLP64:
463 bits_in_long = 32;
464 max_int_alignment = 4;
465 size_t_ctype = &ullong_ctype;
466 ssize_t_ctype = &llong_ctype;
467 add_pre_buffer("#weak_define __LLP64__ 1\n");
468 goto case_64bit_common;
469 case_64bit_common:
470 bits_in_pointer = 64;
471 pointer_alignment = 8;
472 #if defined(__x86_64__) || defined (__i386)
473 add_pre_buffer("#weak_define __x86_64__ 1\n");
474 add_pre_buffer("#weak_define __x86_64 1\n");
475 #endif
476 break;
480 static void handle_arch_msize_long_finalize(void)
482 if (arch_msize_long) {
483 size_t_ctype = &ulong_ctype;
484 ssize_t_ctype = &long_ctype;
488 static void handle_arch_finalize(void)
490 handle_arch_m64_finalize();
491 handle_arch_msize_long_finalize();
495 static int handle_simple_switch(const char *arg, const char *name, int *flag)
497 int val = 1;
499 // Prefixe "no-" mean to turn flag off.
500 if (strncmp(arg, "no-", 3) == 0) {
501 arg += 3;
502 val = 0;
505 if (strcmp(arg, name) == 0) {
506 *flag = val;
507 return 1;
510 // not handled
511 return 0;
514 static char **handle_switch_o(char *arg, char **next)
516 if (!strcmp (arg, "o")) { // "-o foo"
517 if (!*++next)
518 die("argument to '-o' is missing");
520 // else "-ofoo"
522 return next;
525 static const struct warning {
526 const char *name;
527 int *flag;
528 } warnings[] = {
529 { "address", &Waddress },
530 { "address-space", &Waddress_space },
531 { "big-constants", &Wbig_constants },
532 { "bitwise", &Wbitwise },
533 { "cast-to-as", &Wcast_to_as },
534 { "cast-truncate", &Wcast_truncate },
535 { "constexpr-not-const", &Wconstexpr_not_const},
536 { "context", &Wcontext },
537 { "decl", &Wdecl },
538 { "declaration-after-statement", &Wdeclarationafterstatement },
539 { "default-bitfield-sign", &Wdefault_bitfield_sign },
540 { "designated-init", &Wdesignated_init },
541 { "do-while", &Wdo_while },
542 { "enum-mismatch", &Wenum_mismatch },
543 { "init-cstring", &Winit_cstring },
544 { "memcpy-max-count", &Wmemcpy_max_count },
545 { "non-ansi-function-declaration", &Wnon_ansi_function_declaration },
546 { "non-pointer-null", &Wnon_pointer_null },
547 { "old-initializer", &Wold_initializer },
548 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
549 { "override-init", &Woverride_init },
550 { "override-init-all", &Woverride_init_all },
551 { "paren-string", &Wparen_string },
552 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
553 { "return-void", &Wreturn_void },
554 { "shadow", &Wshadow },
555 { "sizeof-bool", &Wsizeof_bool },
556 { "pointer-arith", &Wpointer_arith },
557 { "sparse-error", &Wsparse_error },
558 { "tautological-compare", &Wtautological_compare },
559 { "transparent-union", &Wtransparent_union },
560 { "typesign", &Wtypesign },
561 { "undef", &Wundef },
562 { "uninitialized", &Wuninitialized },
563 { "unknown-attribute", &Wunknown_attribute },
564 { "vla", &Wvla },
567 enum {
568 WARNING_OFF,
569 WARNING_ON,
570 WARNING_FORCE_OFF
574 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
576 int flag = WARNING_ON;
577 char *p = arg + 1;
578 unsigned i;
580 if (!strcmp(p, "sparse-all")) {
581 for (i = 0; i < n; i++) {
582 if (*warnings[i].flag != WARNING_FORCE_OFF && warnings[i].flag != &Wsparse_error)
583 *warnings[i].flag = WARNING_ON;
587 // Prefixes "no" and "no-" mean to turn warning off.
588 if (p[0] == 'n' && p[1] == 'o') {
589 p += 2;
590 if (p[0] == '-')
591 p++;
592 flag = WARNING_FORCE_OFF;
595 for (i = 0; i < n; i++) {
596 if (!strcmp(p,warnings[i].name)) {
597 *warnings[i].flag = flag;
598 return next;
602 // Unknown.
603 return NULL;
606 static char **handle_switch_W(char *arg, char **next)
608 char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
609 if (ret)
610 return ret;
612 // Unknown.
613 return next;
616 static struct warning debugs[] = {
617 { "entry", &dbg_entry},
618 { "dead", &dbg_dead},
622 static char **handle_switch_v(char *arg, char **next)
624 char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
625 if (ret)
626 return ret;
628 // Unknown.
629 do {
630 verbose++;
631 } while (*++arg == 'v');
632 return next;
635 static struct warning dumps[] = {
636 { "D", &dump_macro_defs},
639 static char **handle_switch_d(char *arg, char **next)
641 char ** ret = handle_onoff_switch(arg, next, dumps, ARRAY_SIZE(dumps));
642 if (ret)
643 return ret;
645 return next;
649 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
651 unsigned i;
653 for (i = 0; i < n; i++) {
654 if (*warnings[i].flag == WARNING_FORCE_OFF)
655 *warnings[i].flag = WARNING_OFF;
659 static void handle_switch_W_finalize(void)
661 handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
663 /* default Wdeclarationafterstatement based on the C dialect */
664 if (-1 == Wdeclarationafterstatement)
666 switch (standard)
668 case STANDARD_C89:
669 case STANDARD_C94:
670 Wdeclarationafterstatement = 1;
671 break;
673 case STANDARD_C99:
674 case STANDARD_GNU89:
675 case STANDARD_GNU99:
676 case STANDARD_C11:
677 case STANDARD_GNU11:
678 Wdeclarationafterstatement = 0;
679 break;
681 default:
682 assert (0);
688 static void handle_switch_v_finalize(void)
690 handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
693 static char **handle_switch_U(char *arg, char **next)
695 const char *name = arg + 1;
696 if (*name == '\0')
697 name = *++next;
698 add_pre_buffer ("#undef %s\n", name);
699 return next;
702 static char **handle_switch_O(char *arg, char **next)
704 int level = 1;
705 if (arg[1] >= '0' && arg[1] <= '9')
706 level = arg[1] - '0';
707 optimize = level;
708 optimize_size = arg[1] == 's';
709 return next;
712 static char **handle_switch_fmemcpy_max_count(char *arg, char **next)
714 unsigned long long val;
715 char *end;
717 val = strtoull(arg, &end, 0);
718 if (*end != '\0' || end == arg)
719 die("error: missing argument to \"-fmemcpy-max-count=\"");
721 if (val == 0)
722 val = ~0ULL;
723 fmemcpy_max_count = val;
724 return next;
727 static char **handle_switch_ftabstop(char *arg, char **next)
729 char *end;
730 unsigned long val;
732 if (*arg == '\0')
733 die("error: missing argument to \"-ftabstop=\"");
735 /* we silently ignore silly values */
736 val = strtoul(arg, &end, 10);
737 if (*end == '\0' && 1 <= val && val <= 100)
738 tabstop = val;
740 return next;
743 static int funsigned_char;
744 static void handle_funsigned_char(void)
746 if (funsigned_char) {
747 char_ctype.ctype.modifiers &= ~MOD_SIGNED;
748 char_ctype.ctype.modifiers |= MOD_UNSIGNED;
752 static char **handle_switch_fdump(char *arg, char **next)
754 if (!strncmp(arg, "linearize", 9)) {
755 arg += 9;
756 if (*arg == '\0')
757 fdump_linearize = 1;
758 else if (!strcmp(arg, "=only"))
759 fdump_linearize = 2;
760 else
761 goto err;
764 /* ignore others flags */
765 return next;
767 err:
768 die("error: unknown flag \"-fdump-%s\"", arg);
771 static char **handle_switch_f(char *arg, char **next)
773 arg++;
775 if (!strncmp(arg, "tabstop=", 8))
776 return handle_switch_ftabstop(arg+8, next);
777 if (!strncmp(arg, "dump-", 5))
778 return handle_switch_fdump(arg+5, next);
779 if (!strncmp(arg, "memcpy-max-count=", 17))
780 return handle_switch_fmemcpy_max_count(arg+17, next);
782 if (!strcmp(arg, "unsigned-char")) {
783 funsigned_char = 1;
784 return next;
787 /* handle switches w/ arguments above, boolean and only boolean below */
788 if (handle_simple_switch(arg, "mem-report", &fmem_report))
789 return next;
791 return next;
794 static char **handle_switch_G(char *arg, char **next)
796 if (!strcmp (arg, "G") && *next)
797 return next + 1; // "-G 0"
798 else
799 return next; // "-G0" or (bogus) terminal "-G"
802 static char **handle_switch_a(char *arg, char **next)
804 if (!strcmp (arg, "ansi"))
805 standard = STANDARD_C89;
807 return next;
810 static char **handle_switch_s(char *arg, char **next)
812 if (!strncmp (arg, "std=", 4))
814 arg += 4;
816 if (!strcmp (arg, "c89") ||
817 !strcmp (arg, "iso9899:1990"))
818 standard = STANDARD_C89;
820 else if (!strcmp (arg, "iso9899:199409"))
821 standard = STANDARD_C94;
823 else if (!strcmp (arg, "c99") ||
824 !strcmp (arg, "c9x") ||
825 !strcmp (arg, "iso9899:1999") ||
826 !strcmp (arg, "iso9899:199x"))
827 standard = STANDARD_C99;
829 else if (!strcmp (arg, "gnu89"))
830 standard = STANDARD_GNU89;
832 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
833 standard = STANDARD_GNU99;
835 else if (!strcmp(arg, "c11") ||
836 !strcmp(arg, "c1x") ||
837 !strcmp(arg, "iso9899:2011"))
838 standard = STANDARD_C11;
840 else if (!strcmp(arg, "gnu11"))
841 standard = STANDARD_GNU11;
843 else
844 die ("Unsupported C dialect");
847 return next;
850 static char **handle_nostdinc(char *arg, char **next)
852 add_pre_buffer("#nostdinc\n");
853 return next;
856 static char **handle_switch_n(char *arg, char **next)
858 if (!strcmp (arg, "nostdinc"))
859 return handle_nostdinc(arg, next);
861 return next;
864 static char **handle_base_dir(char *arg, char **next)
866 gcc_base_dir = *++next;
867 if (!gcc_base_dir)
868 die("missing argument for -gcc-base-dir option");
869 return next;
872 static char **handle_no_lineno(char *arg, char **next)
874 no_lineno = 1;
875 return next;
878 static char **handle_switch_g(char *arg, char **next)
880 if (!strcmp (arg, "gcc-base-dir"))
881 return handle_base_dir(arg, next);
883 return next;
886 static char **handle_version(char *arg, char **next)
888 printf("%s\n", SPARSE_VERSION);
889 exit(0);
892 static char **handle_param(char *arg, char **next)
894 char *value = NULL;
896 /* For now just skip any '--param=*' or '--param *' */
897 if (*arg == '\0') {
898 value = *++next;
899 } else if (isspace((unsigned char)*arg) || *arg == '=') {
900 value = ++arg;
903 if (!value)
904 die("missing argument for --param option");
906 return next;
909 struct switches {
910 const char *name;
911 char **(*fn)(char *, char **);
912 unsigned int prefix:1;
915 static char **handle_long_options(char *arg, char **next)
917 static struct switches cmd[] = {
918 { "param", handle_param, 1 },
919 { "version", handle_version },
920 { "nostdinc", handle_nostdinc },
921 { "gcc-base-dir", handle_base_dir},
922 { "no-lineno", handle_no_lineno},
923 { NULL, NULL }
925 struct switches *s = cmd;
927 while (s->name) {
928 int optlen = strlen(s->name);
929 if (!strncmp(s->name, arg, optlen + !s->prefix))
930 return s->fn(arg + optlen, next);
931 s++;
933 return next;
936 static char **handle_switch(char *arg, char **next)
938 switch (*arg) {
939 case 'a': return handle_switch_a(arg, next);
940 case 'D': return handle_switch_D(arg, next);
941 case 'd': return handle_switch_d(arg, next);
942 case 'E': return handle_switch_E(arg, next);
943 case 'f': return handle_switch_f(arg, next);
944 case 'g': return handle_switch_g(arg, next);
945 case 'G': return handle_switch_G(arg, next);
946 case 'I': return handle_switch_I(arg, next);
947 case 'i': return handle_switch_i(arg, next);
948 case 'M': return handle_switch_M(arg, next);
949 case 'm': return handle_switch_m(arg, next);
950 case 'n': return handle_switch_n(arg, next);
951 case 'o': return handle_switch_o(arg, next);
952 case 'O': return handle_switch_O(arg, next);
953 case 's': return handle_switch_s(arg, next);
954 case 'U': return handle_switch_U(arg, next);
955 case 'v': return handle_switch_v(arg, next);
956 case 'W': return handle_switch_W(arg, next);
957 case '-': return handle_long_options(arg + 1, next);
958 default:
959 break;
963 * Ignore unknown command line options:
964 * they're probably gcc switches
966 return next;
969 static void predefined_sizeof(const char *name, unsigned bits)
971 add_pre_buffer("#weak_define __SIZEOF_%s__ %d\n", name, bits/8);
974 static void predefined_max(const char *name, const char *suffix, unsigned bits)
976 unsigned long long max = (1ULL << (bits - 1 )) - 1;
978 add_pre_buffer("#weak_define __%s_MAX__ %#llx%s\n", name, max, suffix);
981 static void predefined_type_size(const char *name, const char *suffix, unsigned bits)
983 predefined_max(name, suffix, bits);
984 predefined_sizeof(name, bits);
987 static void predefined_macros(void)
989 add_pre_buffer("#define __CHECKER__ 1\n");
991 predefined_sizeof("SHORT", bits_in_short);
992 predefined_max("SHRT", "", bits_in_short);
993 predefined_max("SCHAR", "", bits_in_char);
994 predefined_max("WCHAR", "", bits_in_wchar);
995 add_pre_buffer("#weak_define __CHAR_BIT__ %d\n", bits_in_char);
997 predefined_type_size("INT", "", bits_in_int);
998 predefined_type_size("LONG", "L", bits_in_long);
999 predefined_type_size("LONG_LONG", "LL", bits_in_longlong);
1001 predefined_sizeof("INT128", 128);
1003 predefined_sizeof("SIZE_T", bits_in_pointer);
1004 predefined_sizeof("PTRDIFF_T", bits_in_pointer);
1005 predefined_sizeof("POINTER", bits_in_pointer);
1007 predefined_sizeof("FLOAT", bits_in_float);
1008 predefined_sizeof("DOUBLE", bits_in_double);
1009 predefined_sizeof("LONG_DOUBLE", bits_in_longdouble);
1011 add_pre_buffer("#weak_define __%s_ENDIAN__ 1\n",
1012 arch_big_endian ? "BIG" : "LITTLE");
1014 add_pre_buffer("#weak_define __ORDER_LITTLE_ENDIAN__ 1234\n");
1015 add_pre_buffer("#weak_define __ORDER_BIG_ENDIAN__ 4321\n");
1016 add_pre_buffer("#weak_define __ORDER_PDP_ENDIAN__ 3412\n");
1017 add_pre_buffer("#weak_define __BYTE_ORDER__ __ORDER_%s_ENDIAN__\n",
1018 arch_big_endian ? "BIG" : "LITTLE");
1021 void declare_builtin_functions(void)
1023 /* Gaah. gcc knows tons of builtin <string.h> functions */
1024 add_pre_buffer("extern void *__builtin_memchr(const void *, int, __SIZE_TYPE__);\n");
1025 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
1026 add_pre_buffer("extern void *__builtin_mempcpy(void *, const void *, __SIZE_TYPE__);\n");
1027 add_pre_buffer("extern void *__builtin_memmove(void *, const void *, __SIZE_TYPE__);\n");
1028 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
1029 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
1030 add_pre_buffer("extern char *__builtin_strcat(char *, const char *);\n");
1031 add_pre_buffer("extern char *__builtin_strncat(char *, const char *, __SIZE_TYPE__);\n");
1032 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
1033 add_pre_buffer("extern int __builtin_strncmp(const char *, const char *, __SIZE_TYPE__);\n");
1034 add_pre_buffer("extern int __builtin_strcasecmp(const char *, const char *);\n");
1035 add_pre_buffer("extern int __builtin_strncasecmp(const char *, const char *, __SIZE_TYPE__);\n");
1036 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
1037 add_pre_buffer("extern char *__builtin_strrchr(const char *, int);\n");
1038 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
1039 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
1040 add_pre_buffer("extern char *__builtin_strdup(const char *);\n");
1041 add_pre_buffer("extern char *__builtin_strndup(const char *, __SIZE_TYPE__);\n");
1042 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
1043 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
1044 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
1045 add_pre_buffer("extern char* __builtin_stpcpy(const char *, const char*);\n");
1046 add_pre_buffer("extern char* __builtin_stpncpy(const char *, const char*, __SIZE_TYPE__);\n");
1047 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strlen(const char *);\n");
1048 add_pre_buffer("extern char *__builtin_strstr(const char *, const char *);\n");
1049 add_pre_buffer("extern char *__builtin_strcasestr(const char *, const char *);\n");
1050 add_pre_buffer("extern char *__builtin_strnstr(const char *, const char *, __SIZE_TYPE__);\n");
1052 /* And even some from <strings.h> */
1053 add_pre_buffer("extern int __builtin_bcmp(const void *, const void *, __SIZE_TYPE__);\n");
1054 add_pre_buffer("extern void __builtin_bcopy(const void *, void *, __SIZE_TYPE__);\n");
1055 add_pre_buffer("extern void __builtin_bzero(void *, __SIZE_TYPE__);\n");
1056 add_pre_buffer("extern char*__builtin_index(const char *, int);\n");
1057 add_pre_buffer("extern char*__builtin_rindex(const char *, int);\n");
1059 /* And bitwise operations.. */
1060 add_pre_buffer("extern int __builtin_clrsb(int);\n");
1061 add_pre_buffer("extern int __builtin_clrsbl(long);\n");
1062 add_pre_buffer("extern int __builtin_clrsbll(long long);\n");
1063 add_pre_buffer("extern int __builtin_clz(int);\n");
1064 add_pre_buffer("extern int __builtin_clzl(long);\n");
1065 add_pre_buffer("extern int __builtin_clzll(long long);\n");
1066 add_pre_buffer("extern int __builtin_ctz(int);\n");
1067 add_pre_buffer("extern int __builtin_ctzl(long);\n");
1068 add_pre_buffer("extern int __builtin_ctzll(long long);\n");
1069 add_pre_buffer("extern int __builtin_ffs(int);\n");
1070 add_pre_buffer("extern int __builtin_ffsl(long);\n");
1071 add_pre_buffer("extern int __builtin_ffsll(long long);\n");
1072 add_pre_buffer("extern int __builtin_parity(unsigned int);\n");
1073 add_pre_buffer("extern int __builtin_parityl(unsigned long);\n");
1074 add_pre_buffer("extern int __builtin_parityll(unsigned long long);\n");
1075 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
1076 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
1077 add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n");
1079 /* And byte swaps.. */
1080 add_pre_buffer("extern unsigned short __builtin_bswap16(unsigned short);\n");
1081 add_pre_buffer("extern unsigned int __builtin_bswap32(unsigned int);\n");
1082 add_pre_buffer("extern unsigned long long __builtin_bswap64(unsigned long long);\n");
1084 /* And atomic memory access functions.. */
1085 add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n");
1086 add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n");
1087 add_pre_buffer("extern int __sync_fetch_and_or(void *, ...);\n");
1088 add_pre_buffer("extern int __sync_fetch_and_and(void *, ...);\n");
1089 add_pre_buffer("extern int __sync_fetch_and_xor(void *, ...);\n");
1090 add_pre_buffer("extern int __sync_fetch_and_nand(void *, ...);\n");
1091 add_pre_buffer("extern int __sync_add_and_fetch(void *, ...);\n");
1092 add_pre_buffer("extern int __sync_sub_and_fetch(void *, ...);\n");
1093 add_pre_buffer("extern int __sync_or_and_fetch(void *, ...);\n");
1094 add_pre_buffer("extern int __sync_and_and_fetch(void *, ...);\n");
1095 add_pre_buffer("extern int __sync_xor_and_fetch(void *, ...);\n");
1096 add_pre_buffer("extern int __sync_nand_and_fetch(void *, ...);\n");
1097 add_pre_buffer("extern int __sync_bool_compare_and_swap(void *, ...);\n");
1098 add_pre_buffer("extern int __sync_val_compare_and_swap(void *, ...);\n");
1099 add_pre_buffer("extern void __sync_synchronize();\n");
1100 add_pre_buffer("extern int __sync_lock_test_and_set(void *, ...);\n");
1101 add_pre_buffer("extern void __sync_lock_release(void *, ...);\n");
1103 /* And some random ones.. */
1104 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
1105 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
1106 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
1107 add_pre_buffer("extern void __builtin_trap(void);\n");
1108 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
1109 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
1110 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
1111 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
1112 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
1113 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
1114 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
1115 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
1116 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
1117 add_pre_buffer("extern int __builtin_abs(int);\n");
1118 add_pre_buffer("extern long __builtin_labs(long);\n");
1119 add_pre_buffer("extern long long __builtin_llabs(long long);\n");
1120 add_pre_buffer("extern double __builtin_fabs(double);\n");
1121 add_pre_buffer("extern __SIZE_TYPE__ __builtin_va_arg_pack_len(void);\n");
1123 /* Add Blackfin-specific stuff */
1124 add_pre_buffer(
1125 "#ifdef __bfin__\n"
1126 "extern void __builtin_bfin_csync(void);\n"
1127 "extern void __builtin_bfin_ssync(void);\n"
1128 "extern int __builtin_bfin_norm_fr1x32(int);\n"
1129 "#endif\n"
1132 /* And some floating point stuff.. */
1133 add_pre_buffer("extern int __builtin_isgreater(float, float);\n");
1134 add_pre_buffer("extern int __builtin_isgreaterequal(float, float);\n");
1135 add_pre_buffer("extern int __builtin_isless(float, float);\n");
1136 add_pre_buffer("extern int __builtin_islessequal(float, float);\n");
1137 add_pre_buffer("extern int __builtin_islessgreater(float, float);\n");
1138 add_pre_buffer("extern int __builtin_isunordered(float, float);\n");
1140 /* And some INFINITY / NAN stuff.. */
1141 add_pre_buffer("extern double __builtin_huge_val(void);\n");
1142 add_pre_buffer("extern float __builtin_huge_valf(void);\n");
1143 add_pre_buffer("extern long double __builtin_huge_vall(void);\n");
1144 add_pre_buffer("extern double __builtin_inf(void);\n");
1145 add_pre_buffer("extern float __builtin_inff(void);\n");
1146 add_pre_buffer("extern long double __builtin_infl(void);\n");
1147 add_pre_buffer("extern double __builtin_nan(const char *);\n");
1148 add_pre_buffer("extern float __builtin_nanf(const char *);\n");
1149 add_pre_buffer("extern long double __builtin_nanl(const char *);\n");
1150 add_pre_buffer("extern int __builtin_isinf_sign(float);\n");
1151 add_pre_buffer("extern int __builtin_isfinite(float);\n");
1152 add_pre_buffer("extern int __builtin_isnan(float);\n");
1154 /* And some __FORTIFY_SOURCE ones.. */
1155 add_pre_buffer ("extern __SIZE_TYPE__ __builtin_object_size(const void *, int);\n");
1156 add_pre_buffer ("extern void * __builtin___memcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1157 add_pre_buffer ("extern void * __builtin___memmove_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1158 add_pre_buffer ("extern void * __builtin___mempcpy_chk(void *, const void *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1159 add_pre_buffer ("extern void * __builtin___memset_chk(void *, int, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1160 add_pre_buffer ("extern int __builtin___sprintf_chk(char *, int, __SIZE_TYPE__, const char *, ...);\n");
1161 add_pre_buffer ("extern int __builtin___snprintf_chk(char *, __SIZE_TYPE__, int , __SIZE_TYPE__, const char *, ...);\n");
1162 add_pre_buffer ("extern char * __builtin___stpcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
1163 add_pre_buffer ("extern char * __builtin___strcat_chk(char *, const char *, __SIZE_TYPE__);\n");
1164 add_pre_buffer ("extern char * __builtin___strcpy_chk(char *, const char *, __SIZE_TYPE__);\n");
1165 add_pre_buffer ("extern char * __builtin___strncat_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1166 add_pre_buffer ("extern char * __builtin___strncpy_chk(char *, const char *, __SIZE_TYPE__, __SIZE_TYPE__);\n");
1167 add_pre_buffer ("extern int __builtin___vsprintf_chk(char *, int, __SIZE_TYPE__, const char *, __builtin_va_list);\n");
1168 add_pre_buffer ("extern int __builtin___vsnprintf_chk(char *, __SIZE_TYPE__, int, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
1169 add_pre_buffer ("extern void __builtin_unreachable(void);\n");
1171 /* And some from <stdlib.h> */
1172 add_pre_buffer("extern void __builtin_abort(void);\n");
1173 add_pre_buffer("extern void *__builtin_calloc(__SIZE_TYPE__, __SIZE_TYPE__);\n");
1174 add_pre_buffer("extern void __builtin_exit(int);\n");
1175 add_pre_buffer("extern void *__builtin_malloc(__SIZE_TYPE__);\n");
1176 add_pre_buffer("extern void *__builtin_realloc(void *, __SIZE_TYPE__);\n");
1177 add_pre_buffer("extern void __builtin_free(void *);\n");
1179 /* And some from <stdio.h> */
1180 add_pre_buffer("extern int __builtin_printf(const char *, ...);\n");
1181 add_pre_buffer("extern int __builtin_sprintf(char *, const char *, ...);\n");
1182 add_pre_buffer("extern int __builtin_snprintf(char *, __SIZE_TYPE__, const char *, ...);\n");
1183 add_pre_buffer("extern int __builtin_puts(const char *);\n");
1184 add_pre_buffer("extern int __builtin_vprintf(const char *, __builtin_va_list);\n");
1185 add_pre_buffer("extern int __builtin_vsprintf(char *, const char *, __builtin_va_list);\n");
1186 add_pre_buffer("extern int __builtin_vsnprintf(char *, __SIZE_TYPE__, const char *, __builtin_va_list ap);\n");
1189 void create_builtin_stream(void)
1191 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
1192 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
1193 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
1195 /* add the multiarch include directories, if any */
1196 if (multiarch_dir && *multiarch_dir) {
1197 add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir);
1198 add_pre_buffer("#add_system \"/usr/local/include/%s\"\n", multiarch_dir);
1201 /* We add compiler headers path here because we have to parse
1202 * the arguments to get it, falling back to default. */
1203 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
1204 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
1206 add_pre_buffer("#define __extension__\n");
1207 add_pre_buffer("#define __pragma__\n");
1208 add_pre_buffer("#define _Pragma(x)\n");
1210 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
1211 // solaris/sparc that is really "unsigned int" and for linux/x86_64
1212 // it is "long unsigned int". In either case we can probably
1213 // get away with this. We need the #weak_define as cgcc will define
1214 // the right __SIZE_TYPE__.
1215 if (size_t_ctype == &ulong_ctype)
1216 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
1217 else
1218 add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
1219 add_pre_buffer("#weak_define __STDC__ 1\n");
1221 switch (standard)
1223 case STANDARD_C89:
1224 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1225 break;
1227 case STANDARD_C94:
1228 add_pre_buffer("#weak_define __STDC_VERSION__ 199409L\n");
1229 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1230 break;
1232 case STANDARD_C99:
1233 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1234 add_pre_buffer("#weak_define __STRICT_ANSI__\n");
1235 break;
1237 case STANDARD_GNU89:
1238 break;
1240 case STANDARD_GNU99:
1241 add_pre_buffer("#weak_define __STDC_VERSION__ 199901L\n");
1242 break;
1244 case STANDARD_C11:
1245 add_pre_buffer("#weak_define __STRICT_ANSI__ 1\n");
1246 case STANDARD_GNU11:
1247 add_pre_buffer("#weak_define __STDC_NO_ATOMICS__ 1\n");
1248 add_pre_buffer("#weak_define __STDC_NO_COMPLEX__ 1\n");
1249 add_pre_buffer("#weak_define __STDC_NO_THREADS__ 1\n");
1250 add_pre_buffer("#weak_define __STDC_VERSION__ 201112L\n");
1251 break;
1253 default:
1254 assert (0);
1257 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1258 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1259 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
1260 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
1261 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
1262 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
1263 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1264 add_pre_buffer("#define __builtin_ms_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1265 add_pre_buffer("#define __builtin_va_end(arg)\n");
1266 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
1267 add_pre_buffer("#define __builtin_va_arg_pack()\n");
1269 /* FIXME! We need to do these as special magic macros at expansion time! */
1270 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
1272 if (optimize)
1273 add_pre_buffer("#define __OPTIMIZE__ 1\n");
1274 if (optimize_size)
1275 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
1278 static struct symbol_list *sparse_tokenstream(struct token *token)
1280 int builtin = token && !token->pos.stream;
1282 // Preprocess the stream
1283 token = preprocess(token);
1285 if (dump_macro_defs && !builtin)
1286 dump_macro_definitions();
1288 if (preprocess_only) {
1289 while (!eof_token(token)) {
1290 int prec = 1;
1291 struct token *next = token->next;
1292 const char *separator = "";
1293 if (next->pos.whitespace)
1294 separator = " ";
1295 if (next->pos.newline) {
1296 separator = "\n\t\t\t\t\t";
1297 prec = next->pos.pos;
1298 if (prec > 4)
1299 prec = 4;
1301 printf("%s%.*s", show_token(token), prec, separator);
1302 token = next;
1304 putchar('\n');
1306 return NULL;
1309 // Parse the resulting C code
1310 while (!eof_token(token))
1311 token = external_declaration(token, &translation_unit_used_list, NULL);
1312 return translation_unit_used_list;
1315 static struct symbol_list *sparse_file(const char *filename)
1317 int fd;
1318 struct token *token;
1320 if (strcmp (filename, "-") == 0) {
1321 fd = 0;
1322 } else {
1323 fd = open(filename, O_RDONLY);
1324 if (fd < 0)
1325 die("No such file: %s", filename);
1328 // Tokenize the input stream
1329 token = tokenize(filename, fd, NULL, includepath);
1330 store_all_tokens(token);
1331 close(fd);
1333 return sparse_tokenstream(token);
1337 * This handles the "-include" directive etc: we're in global
1338 * scope, and all types/macros etc will affect all the following
1339 * files.
1341 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1342 * affect all subsequent files too, i.e. we can have non-local
1343 * behaviour between files!
1345 static struct symbol_list *sparse_initial(void)
1347 int i;
1349 // Prepend any "include" file to the stream.
1350 // We're in global scope, it will affect all files!
1351 for (i = 0; i < cmdline_include_nr; i++)
1352 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]);
1354 return sparse_tokenstream(pre_buffer_begin);
1357 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
1359 char **args;
1360 struct symbol_list *list;
1362 // Initialize symbol stream first, so that we can add defines etc
1363 init_symbols();
1364 init_include_path();
1366 args = argv;
1367 for (;;) {
1368 char *arg = *++args;
1369 if (!arg)
1370 break;
1372 if (arg[0] == '-' && arg[1]) {
1373 args = handle_switch(arg+1, args);
1374 continue;
1376 add_ptr_list_notag(filelist, arg);
1378 handle_switch_W_finalize();
1379 handle_switch_v_finalize();
1381 handle_arch_finalize();
1383 list = NULL;
1384 if (!ptr_list_empty(filelist)) {
1385 // Initialize type system
1386 init_ctype();
1387 handle_funsigned_char();
1389 create_builtin_stream();
1390 predefined_macros();
1391 if (!preprocess_only)
1392 declare_builtin_functions();
1394 list = sparse_initial();
1397 * Protect the initial token allocations, since
1398 * they need to survive all the others
1400 protect_token_alloc();
1403 * Evaluate the complete symbol list
1404 * Note: This is not needed for normal cases.
1405 * These symbols should only be predefined defines and
1406 * declaratons which will be evaluated later, when needed.
1407 * This is also the case when a file is directly included via
1408 * '-include <file>' on the command line *AND* the file only
1409 * contains defines, declarations and inline definitions.
1410 * However, in the rare cases where the given file should
1411 * contain some definitions, these will never be evaluated
1412 * and thus won't be able to be linearized correctly.
1413 * Hence the evaluate_symbol_list() here under.
1415 evaluate_symbol_list(list);
1416 return list;
1419 struct symbol_list * sparse_keep_tokens(char *filename)
1421 struct symbol_list *res;
1423 /* Clear previous symbol list */
1424 translation_unit_used_list = NULL;
1426 new_file_scope();
1427 res = sparse_file(filename);
1429 /* And return it */
1430 return res;
1434 struct symbol_list * __sparse(char *filename)
1436 struct symbol_list *res;
1438 res = sparse_keep_tokens(filename);
1440 /* Drop the tokens for this file after parsing */
1441 clear_token_alloc();
1443 /* And return it */
1444 return res;
1447 struct symbol_list * sparse(char *filename)
1449 struct symbol_list *res = __sparse(filename);
1451 if (has_error & ERROR_CURR_PHASE)
1452 has_error = ERROR_PREV_PHASE;
1453 /* Evaluate the complete symbol list */
1454 evaluate_symbol_list(res);
1456 return res;