comparison: select the caller_info
[smatch.git] / options.c
blob17da5f367e2433b771e4bd85451407afd8f1088e
1 // SPDX-License-Identifier: MIT
2 /*
3 * 'sparse' library helper routines.
5 * Copyright (C) 2003 Transmeta Corp.
6 * 2003-2004 Linus Torvalds
7 * 2017-2020 Luc Van Oostenryck
8 */
10 #include "options.h"
11 #include "lib.h"
12 #include "machine.h"
13 #include "target.h"
14 #include "version.h"
15 #include <ctype.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
21 #ifndef __GNUC__
22 # define __GNUC__ 2
23 # define __GNUC_MINOR__ 95
24 # define __GNUC_PATCHLEVEL__ 0
25 #endif
27 enum flag_type {
28 FLAG_OFF,
29 FLAG_ON,
30 FLAG_FORCE_OFF
33 int die_if_error = 0;
34 int do_output = 1;
35 int gcc_major = __GNUC__;
36 int gcc_minor = __GNUC_MINOR__;
37 int gcc_patchlevel = __GNUC_PATCHLEVEL__;
38 int has_error = 0;
39 int optimize_level;
40 int optimize_size;
41 int preprocess_only;
42 int preprocessing;
43 int verbose;
45 #define CMDLINE_INCLUDE 20
46 int cmdline_include_nr = 0;
47 char *cmdline_include[CMDLINE_INCLUDE];
49 const char *base_filename;
50 const char *diag_prefix = "";
51 const char *gcc_base_dir = GCC_BASE;
52 const char *multiarch_dir = MULTIARCH_TRIPLET;
53 const char *outfile = NULL;
55 enum standard standard = STANDARD_GNU89;
57 int arch_big_endian = ARCH_BIG_ENDIAN;
58 int arch_cmodel = CMODEL_UNKNOWN;
59 int arch_fp_abi = FP_ABI_NATIVE;
60 int arch_m64 = ARCH_M64_DEFAULT;
61 int arch_msize_long = 0;
62 int arch_os = OS_NATIVE;
64 int dbg_compound = 0;
65 int dbg_dead = 0;
66 int dbg_domtree = 0;
67 int dbg_entry = 0;
68 int dbg_ir = 0;
69 int dbg_postorder = 0;
71 int dump_macro_defs = 0;
72 int dump_macros_only = 0;
74 unsigned long fdump_ir;
75 int fhosted = 1;
76 unsigned int fmax_errors = 100;
77 unsigned int fmax_warnings = 100;
78 int fmem_report = 0;
79 unsigned long long fmemcpy_max_count = 100000;
80 unsigned long fpasses = ~0UL;
81 int fpic = 0;
82 int fpie = 0;
83 int fshort_wchar = 0;
84 int funsigned_bitfields = 0;
85 int funsigned_char = 0;
87 int Waddress = 0;
88 int Waddress_space = 1;
89 int Wbitwise = 1;
90 int Wbitwise_pointer = 0;
91 int Wcast_from_as = 0;
92 int Wcast_to_as = 0;
93 int Wcast_truncate = 1;
94 int Wconstant_suffix = 0;
95 int Wconstexpr_not_const = 0;
96 int Wcontext = 1;
97 int Wdecl = 1;
98 int Wdeclarationafterstatement = -1;
99 int Wdefault_bitfield_sign = 0;
100 int Wdesignated_init = 1;
101 int Wdo_while = 0;
102 int Wenum_mismatch = 1;
103 int Wexternal_function_has_definition = 1;
104 int Wflexible_array_array = 1;
105 int Wflexible_array_nested = 0;
106 int Wflexible_array_sizeof = 0;
107 int Wflexible_array_union = 0;
108 int Wimplicit_int = 1;
109 int Winit_cstring = 0;
110 int Wint_to_pointer_cast = 1;
111 int Wmemcpy_max_count = 1;
112 int Wnewline_eof = 1;
113 int Wnon_pointer_null = 1;
114 int Wold_initializer = 1;
115 int Wold_style_definition = 1;
116 int Wone_bit_signed_bitfield = 1;
117 int Woverride_init = 1;
118 int Woverride_init_all = 0;
119 int Woverride_init_whole_range = 0;
120 int Wparen_string = 0;
121 int Wpast_deep_designator = 0;
122 int Wpedantic = 0;
123 int Wpointer_arith = 0;
124 int Wpointer_to_int_cast = 1;
125 int Wptr_subtraction_blows = 0;
126 int Wreturn_void = 0;
127 int Wshadow = 0;
128 int Wshift_count_negative = 1;
129 int Wshift_count_overflow = 1;
130 int Wsizeof_bool = 0;
131 int Wsparse_error = FLAG_FORCE_OFF;
132 int Wstrict_prototypes = 1;
133 int Wtautological_compare = 0;
134 int Wtransparent_union = 0;
135 int Wtypesign = 0;
136 int Wundef = 0;
137 int Wuninitialized = 1;
138 int Wunion_cast = 0;
139 int Wuniversal_initializer = 0;
140 int Wunknown_attribute = 0;
141 int Wvla = 1;
143 ////////////////////////////////////////////////////////////////////////////////
144 // Helpers for option parsing
146 static const char *match_option(const char *arg, const char *prefix)
148 unsigned int n = strlen(prefix);
149 if (strncmp(arg, prefix, n) == 0)
150 return arg + n;
151 return NULL;
155 struct val_map {
156 const char *name;
157 int val;
160 static int handle_subopt_val(const char *opt, const char *arg, const struct val_map *map, int *flag)
162 const char *name;
164 if (*arg++ != '=')
165 die("missing argument for option '%s'", opt);
166 for (;(name = map->name); map++) {
167 if (strcmp(name, arg) == 0 || strcmp(name, "*") == 0) {
168 *flag = map->val;
169 return 1;
171 if (strcmp(name, "?") == 0)
172 die("invalid argument '%s' in option '%s'", arg, opt);
174 return 0;
178 struct mask_map {
179 const char *name;
180 unsigned long mask;
183 static int apply_mask(unsigned long *val, const char *str, unsigned len, const struct mask_map *map, int neg)
185 const char *name;
187 for (;(name = map->name); map++) {
188 if (!strncmp(name, str, len) && !name[len]) {
189 if (neg == 0)
190 *val |= map->mask;
191 else
192 *val &= ~map->mask;
193 return 0;
196 return 1;
199 static int handle_suboption_mask(const char *arg, const char *opt, const struct mask_map *map, unsigned long *flag)
201 if (*opt == '\0') {
202 apply_mask(flag, "", 0, map, 0);
203 return 1;
205 if (*opt++ != '=')
206 return 0;
207 while (1) {
208 unsigned int len = strcspn(opt, ",+");
209 int neg = 0;
210 if (len == 0)
211 goto end;
212 if (!strncmp(opt, "no-", 3)) {
213 opt += 3;
214 len -= 3;
215 neg = 1;
217 if (apply_mask(flag, opt, len, map, neg))
218 die("error: wrong option '%.*s' for \'%s\'", len, opt, arg);
220 end:
221 opt += len;
222 if (*opt++ == '\0')
223 break;
225 return 1;
229 #define OPT_INVERSE 1
230 #define OPT_VAL 2
231 struct flag {
232 const char *name;
233 int *flag;
234 int (*fun)(const char *arg, const char *opt, const struct flag *, int options);
235 unsigned long mask;
236 int val;
239 static int handle_switches(const char *ori, const char *opt, const struct flag *flags)
241 const char *arg = opt;
242 int val = 1;
244 // Prefixe "no-" mean to turn flag off.
245 if (strncmp(arg, "no-", 3) == 0) {
246 arg += 3;
247 val = 0;
250 for (; flags->name; flags++) {
251 const char *opt = match_option(arg, flags->name);
252 int rc;
254 if (!opt)
255 continue;
257 if (flags->fun) {
258 int options = 0;
259 if (!val)
260 options |= OPT_INVERSE;
261 if ((rc = flags->fun(ori, opt, flags, options)))
262 return rc;
265 // boolean flag
266 if (opt[0] == '\0' && flags->flag) {
267 if (flags->mask & OPT_VAL)
268 val = flags->val;
269 if (flags->mask & OPT_INVERSE)
270 val = !val;
271 *flags->flag = val;
272 return 1;
276 // not handled
277 return 0;
280 static char **handle_onoff_switch(char *arg, char **next, const struct flag flags[])
282 int flag = FLAG_ON;
283 char *p = arg + 1;
284 unsigned i;
286 // Prefixes "no" and "no-" mean to turn warning off.
287 if (p[0] == 'n' && p[1] == 'o') {
288 p += 2;
289 if (p[0] == '-')
290 p++;
291 flag = FLAG_FORCE_OFF;
294 for (i = 0; flags[i].name; i++) {
295 if (!strcmp(p,flags[i].name)) {
296 *flags[i].flag = flag;
297 return next;
301 // Unknown.
302 return NULL;
305 static void handle_onoff_switch_finalize(const struct flag flags[])
307 unsigned i;
309 for (i = 0; flags[i].name; i++) {
310 if (*flags[i].flag == FLAG_FORCE_OFF)
311 *flags[i].flag = FLAG_OFF;
315 static int handle_switch_setval(const char *arg, const char *opt, const struct flag *flag, int options)
317 *(flag->flag) = flag->mask;
318 return 1;
322 #define OPTNUM_ZERO_IS_INF 1
323 #define OPTNUM_UNLIMITED 2
325 #define OPT_NUMERIC(NAME, TYPE, FUNCTION) \
326 static int opt_##NAME(const char *arg, const char *opt, TYPE *ptr, int flag) \
328 char *end; \
329 TYPE val; \
331 val = FUNCTION(opt, &end, 0); \
332 if (*end != '\0' || end == opt) { \
333 if ((flag & OPTNUM_UNLIMITED) && !strcmp(opt, "unlimited")) \
334 val = ~val; \
335 else \
336 die("error: wrong argument to \'%s\'", arg); \
338 if ((flag & OPTNUM_ZERO_IS_INF) && val == 0) \
339 val = ~val; \
340 *ptr = val; \
341 return 1; \
344 OPT_NUMERIC(ullong, unsigned long long, strtoull)
345 OPT_NUMERIC(uint, unsigned int, strtoul)
347 ////////////////////////////////////////////////////////////////////////////////
348 // Option parsing
350 static char **handle_switch_a(char *arg, char **next)
352 if (!strcmp(arg, "ansi"))
353 standard = STANDARD_C89;
355 return next;
358 static char **handle_switch_D(char *arg, char **next)
360 const char *name = arg + 1;
361 const char *value = "1";
363 if (!*name) {
364 arg = *++next;
365 if (!arg)
366 die("argument to `-D' is missing");
367 name = arg;
370 for (;;arg++) {
371 char c;
372 c = *arg;
373 if (!c)
374 break;
375 if (c == '=') {
376 *arg = '\0';
377 value = arg + 1;
378 break;
381 add_pre_buffer("#define %s %s\n", name, value);
382 return next;
385 static char **handle_switch_d(char *arg, char **next)
387 char *arg_char = arg + 1;
390 * -d<CHARS>, where <CHARS> is a sequence of characters, not preceded
391 * by a space. If you specify characters whose behaviour conflicts,
392 * the result is undefined.
394 while (*arg_char) {
395 switch (*arg_char) {
396 case 'M': /* dump just the macro definitions */
397 dump_macros_only = 1;
398 dump_macro_defs = 0;
399 break;
400 case 'D': /* like 'M', but also output pre-processed text */
401 dump_macro_defs = 1;
402 dump_macros_only = 0;
403 break;
404 case 'N': /* like 'D', but only output macro names not bodies */
405 break;
406 case 'I': /* like 'D', but also output #include directives */
407 break;
408 case 'U': /* like 'D', but only output expanded macros */
409 break;
411 arg_char++;
413 return next;
416 static char **handle_switch_E(char *arg, char **next)
418 if (arg[1] == '\0')
419 preprocess_only = 1;
420 return next;
423 static int handle_ftabstop(const char *arg, const char *opt, const struct flag *flag, int options)
425 unsigned long val;
426 char *end;
428 if (*opt == '\0')
429 die("error: missing argument to \"%s\"", arg);
431 /* we silently ignore silly values */
432 val = strtoul(opt, &end, 10);
433 if (*end == '\0' && 1 <= val && val <= 100)
434 tabstop = val;
436 return 1;
439 static int handle_fpasses(const char *arg, const char *opt, const struct flag *flag, int options)
441 unsigned long mask;
443 mask = flag->mask;
444 if (*opt == '\0') {
445 if (options & OPT_INVERSE)
446 fpasses &= ~mask;
447 else
448 fpasses |= mask;
449 return 1;
451 if (options & OPT_INVERSE)
452 return 0;
453 if (!strcmp(opt, "-enable")) {
454 fpasses |= mask;
455 return 1;
457 if (!strcmp(opt, "-disable")) {
458 fpasses &= ~mask;
459 return 1;
461 if (!strcmp(opt, "=last")) {
462 // clear everything above
463 mask |= mask - 1;
464 fpasses &= mask;
465 return 1;
467 return 0;
470 static int handle_fdiagnostic_prefix(const char *arg, const char *opt, const struct flag *flag, int options)
472 switch (*opt) {
473 case '\0':
474 diag_prefix = "sparse: ";
475 return 1;
476 case '=':
477 diag_prefix = xasprintf("%s: ", opt+1);
478 return 1;
479 default:
480 return 0;
484 static int handle_fdump_ir(const char *arg, const char *opt, const struct flag *flag, int options)
486 static const struct mask_map dump_ir_options[] = {
487 { "", PASS_LINEARIZE },
488 { "linearize", PASS_LINEARIZE },
489 { "mem2reg", PASS_MEM2REG },
490 { "final", PASS_FINAL },
491 { },
494 return handle_suboption_mask(arg, opt, dump_ir_options, &fdump_ir);
497 static int handle_fmemcpy_max_count(const char *arg, const char *opt, const struct flag *flag, int options)
499 opt_ullong(arg, opt, &fmemcpy_max_count, OPTNUM_ZERO_IS_INF|OPTNUM_UNLIMITED);
500 return 1;
503 static int handle_fmax_errors(const char *arg, const char *opt, const struct flag *flag, int options)
505 opt_uint(arg, opt, &fmax_errors, OPTNUM_UNLIMITED);
506 return 1;
509 static int handle_fmax_warnings(const char *arg, const char *opt, const struct flag *flag, int options)
511 opt_uint(arg, opt, &fmax_warnings, OPTNUM_UNLIMITED);
512 return 1;
515 static struct flag fflags[] = {
516 { "diagnostic-prefix", NULL, handle_fdiagnostic_prefix },
517 { "dump-ir", NULL, handle_fdump_ir },
518 { "freestanding", &fhosted, NULL, OPT_INVERSE },
519 { "hosted", &fhosted },
520 { "linearize", NULL, handle_fpasses, PASS_LINEARIZE },
521 { "max-errors=", NULL, handle_fmax_errors },
522 { "max-warnings=", NULL, handle_fmax_warnings },
523 { "mem-report", &fmem_report },
524 { "memcpy-max-count=", NULL, handle_fmemcpy_max_count },
525 { "tabstop=", NULL, handle_ftabstop },
526 { "mem2reg", NULL, handle_fpasses, PASS_MEM2REG },
527 { "optim", NULL, handle_fpasses, PASS_OPTIM },
528 { "pic", &fpic, handle_switch_setval, 1 },
529 { "PIC", &fpic, handle_switch_setval, 2 },
530 { "pie", &fpie, handle_switch_setval, 1 },
531 { "PIE", &fpie, handle_switch_setval, 2 },
532 { "signed-bitfields", &funsigned_bitfields, NULL, OPT_INVERSE },
533 { "unsigned-bitfields", &funsigned_bitfields, NULL, },
534 { "signed-char", &funsigned_char, NULL, OPT_INVERSE },
535 { "short-wchar", &fshort_wchar },
536 { "unsigned-char", &funsigned_char, NULL, },
537 { },
540 static char **handle_switch_f(char *arg, char **next)
542 if (handle_switches(arg-1, arg+1, fflags))
543 return next;
545 return next;
548 static char **handle_switch_G(char *arg, char **next)
550 if (!strcmp(arg, "G") && *next)
551 return next + 1; // "-G 0"
552 else
553 return next; // "-G0" or (bogus) terminal "-G"
556 static char **handle_base_dir(char *arg, char **next)
558 gcc_base_dir = *++next;
559 if (!gcc_base_dir)
560 die("missing argument for -gcc-base-dir option");
561 return next;
564 static char **handle_switch_g(char *arg, char **next)
566 if (!strcmp(arg, "gcc-base-dir"))
567 return handle_base_dir(arg, next);
569 return next;
572 static char **handle_switch_I(char *arg, char **next)
574 char *path = arg+1;
576 switch (arg[1]) {
577 case '-':
578 add_pre_buffer("#split_include\n");
579 break;
581 case '\0': /* Plain "-I" */
582 path = *++next;
583 if (!path)
584 die("missing argument for -I option");
585 /* Fall through */
586 default:
587 add_pre_buffer("#add_include \"%s/\"\n", path);
589 return next;
592 static void add_cmdline_include(char *filename)
594 if (cmdline_include_nr >= CMDLINE_INCLUDE)
595 die("too many include files for %s\n", filename);
596 cmdline_include[cmdline_include_nr++] = filename;
599 static char **handle_switch_i(char *arg, char **next)
601 if (*next && !strcmp(arg, "include"))
602 add_cmdline_include(*++next);
603 else if (*next && !strcmp(arg, "imacros"))
604 add_cmdline_include(*++next);
605 else if (*next && !strcmp(arg, "isystem")) {
606 char *path = *++next;
607 if (!path)
608 die("missing argument for -isystem option");
609 add_pre_buffer("#add_isystem \"%s/\"\n", path);
610 } else if (*next && !strcmp(arg, "idirafter")) {
611 char *path = *++next;
612 if (!path)
613 die("missing argument for -idirafter option");
614 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
616 return next;
619 static char **handle_switch_M(char *arg, char **next)
621 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
622 if (!*next)
623 die("missing argument for -%s option", arg);
624 return next + 1;
626 return next;
629 static int handle_march(const char *opt, const char *arg, const struct flag *flag, int options)
631 if (arch_target->parse_march)
632 arch_target->parse_march(arg);
633 return 1;
636 static int handle_mcmodel(const char *opt, const char *arg, const struct flag *flag, int options)
638 static const struct val_map cmodels[] = {
639 { "kernel", CMODEL_KERNEL },
640 { "large", CMODEL_LARGE },
641 { "medany", CMODEL_MEDANY },
642 { "medium", CMODEL_MEDIUM },
643 { "medlow", CMODEL_MEDLOW },
644 { "small", CMODEL_SMALL },
645 { "tiny", CMODEL_TINY },
646 { },
648 return handle_subopt_val(opt, arg, cmodels, flag->flag);
651 static int handle_mfloat_abi(const char *opt, const char *arg, const struct flag *flag, int options) {
652 static const struct val_map fp_abis[] = {
653 { "hard", FP_ABI_HARD },
654 { "soft", FP_ABI_SOFT },
655 { "softfp", FP_ABI_HYBRID },
656 { "?" },
658 return handle_subopt_val(opt, arg, fp_abis, flag->flag);
661 static char **handle_multiarch_dir(char *arg, char **next)
663 multiarch_dir = *++next;
664 if (!multiarch_dir)
665 die("missing argument for -multiarch-dir option");
666 return next;
669 static const struct flag mflags[] = {
670 { "64", &arch_m64, NULL, OPT_VAL, ARCH_LP64 },
671 { "32", &arch_m64, NULL, OPT_VAL, ARCH_LP32 },
672 { "31", &arch_m64, NULL, OPT_VAL, ARCH_LP32 },
673 { "16", &arch_m64, NULL, OPT_VAL, ARCH_LP32 },
674 { "x32",&arch_m64, NULL, OPT_VAL, ARCH_X32 },
675 { "size-llp64", &arch_m64, NULL, OPT_VAL, ARCH_LLP64 },
676 { "size-long", &arch_msize_long },
677 { "arch=", NULL, handle_march },
678 { "big-endian", &arch_big_endian, NULL },
679 { "little-endian", &arch_big_endian, NULL, OPT_INVERSE },
680 { "cmodel", &arch_cmodel, handle_mcmodel },
681 { "float-abi", &arch_fp_abi, handle_mfloat_abi },
682 { "hard-float", &arch_fp_abi, NULL, OPT_VAL, FP_ABI_HARD },
683 { "soft-float", &arch_fp_abi, NULL, OPT_VAL, FP_ABI_SOFT },
687 static char **handle_switch_m(char *arg, char **next)
689 if (!strcmp(arg, "multiarch-dir")) {
690 return handle_multiarch_dir(arg, next);
691 } else {
692 handle_switches(arg-1, arg+1, mflags);
695 return next;
698 static char **handle_nostdinc(char *arg, char **next)
700 add_pre_buffer("#nostdinc\n");
701 return next;
704 static char **handle_switch_n(char *arg, char **next)
706 if (!strcmp(arg, "nostdinc"))
707 return handle_nostdinc(arg, next);
709 return next;
712 static char **handle_switch_O(char *arg, char **next)
714 int level = 1;
715 if (arg[1] >= '0' && arg[1] <= '9')
716 level = arg[1] - '0';
717 optimize_level = level;
718 optimize_size = arg[1] == 's';
719 return next;
722 static char **handle_switch_o(char *arg, char **next)
724 if (!strcmp(arg, "o")) { // "-o foo"
725 if (!*++next)
726 die("argument to '-o' is missing");
727 outfile = *next;
729 // else "-ofoo"
731 return next;
734 static const struct flag pflags[] = {
735 { "pedantic", &Wpedantic, NULL, OPT_VAL, FLAG_ON },
739 static char **handle_switch_p(char *arg, char **next)
741 handle_switches(arg-1, arg, pflags);
742 return next;
745 static char **handle_switch_s(const char *arg, char **next)
747 if ((arg = match_option(arg, "std="))) {
748 if (!strcmp(arg, "c89") ||
749 !strcmp(arg, "iso9899:1990"))
750 standard = STANDARD_C89;
752 else if (!strcmp(arg, "iso9899:199409"))
753 standard = STANDARD_C94;
755 else if (!strcmp(arg, "c99") ||
756 !strcmp(arg, "c9x") ||
757 !strcmp(arg, "iso9899:1999") ||
758 !strcmp(arg, "iso9899:199x"))
759 standard = STANDARD_C99;
761 else if (!strcmp(arg, "gnu89"))
762 standard = STANDARD_GNU89;
764 else if (!strcmp(arg, "gnu99") || !strcmp(arg, "gnu9x"))
765 standard = STANDARD_GNU99;
767 else if (!strcmp(arg, "c11") ||
768 !strcmp(arg, "c1x") ||
769 !strcmp(arg, "iso9899:2011"))
770 standard = STANDARD_C11;
772 else if (!strcmp(arg, "gnu11"))
773 standard = STANDARD_GNU11;
775 else if (!strcmp(arg, "c17") ||
776 !strcmp(arg, "c18") ||
777 !strcmp(arg, "iso9899:2017") ||
778 !strcmp(arg, "iso9899:2018"))
779 standard = STANDARD_C17;
780 else if (!strcmp(arg, "gnu17") ||
781 !strcmp(arg, "gnu18"))
782 standard = STANDARD_GNU17;
784 else
785 die("Unsupported C dialect");
788 return next;
791 static char **handle_switch_U(char *arg, char **next)
793 const char *name = arg + 1;
795 if (*name == '\0') {
796 name = *++next;
797 if (!name)
798 die("argument to `-U' is missing");
800 add_pre_buffer("#undef %s\n", name);
801 return next;
804 static struct flag debugs[] = {
805 { "compound", &dbg_compound},
806 { "dead", &dbg_dead},
807 { "domtree", &dbg_domtree},
808 { "entry", &dbg_entry},
809 { "ir", &dbg_ir},
810 { "postorder", &dbg_postorder},
814 static char **handle_switch_v(char *arg, char **next)
816 char ** ret = handle_onoff_switch(arg, next, debugs);
817 if (ret)
818 return ret;
820 // Unknown.
821 do {
822 verbose++;
823 } while (*++arg == 'v');
824 return next;
827 static void handle_switch_v_finalize(void)
829 handle_onoff_switch_finalize(debugs);
832 static const struct flag warnings[] = {
833 { "address", &Waddress },
834 { "address-space", &Waddress_space },
835 { "bitwise", &Wbitwise },
836 { "bitwise-pointer", &Wbitwise_pointer},
837 { "cast-from-as", &Wcast_from_as },
838 { "cast-to-as", &Wcast_to_as },
839 { "cast-truncate", &Wcast_truncate },
840 { "constant-suffix", &Wconstant_suffix },
841 { "constexpr-not-const", &Wconstexpr_not_const},
842 { "context", &Wcontext },
843 { "decl", &Wdecl },
844 { "declaration-after-statement", &Wdeclarationafterstatement },
845 { "default-bitfield-sign", &Wdefault_bitfield_sign },
846 { "designated-init", &Wdesignated_init },
847 { "do-while", &Wdo_while },
848 { "enum-mismatch", &Wenum_mismatch },
849 { "external-function-has-definition", &Wexternal_function_has_definition },
850 { "flexible-array-array", &Wflexible_array_array },
851 { "flexible-array-nested", &Wflexible_array_nested },
852 { "flexible-array-sizeof", &Wflexible_array_sizeof },
853 { "flexible-array-union", &Wflexible_array_union },
854 { "implicit-int", &Wimplicit_int },
855 { "init-cstring", &Winit_cstring },
856 { "int-to-pointer-cast", &Wint_to_pointer_cast },
857 { "memcpy-max-count", &Wmemcpy_max_count },
858 { "non-pointer-null", &Wnon_pointer_null },
859 { "newline-eof", &Wnewline_eof },
860 { "old-initializer", &Wold_initializer },
861 { "old-style-definition", &Wold_style_definition },
862 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
863 { "override-init", &Woverride_init },
864 { "override-init-all", &Woverride_init_all },
865 { "paren-string", &Wparen_string },
866 { "past-deep-designator", &Wpast_deep_designator },
867 { "pedantic", &Wpedantic },
868 { "pointer-to-int-cast", &Wpointer_to_int_cast },
869 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
870 { "return-void", &Wreturn_void },
871 { "shadow", &Wshadow },
872 { "shift-count-negative", &Wshift_count_negative },
873 { "shift-count-overflow", &Wshift_count_overflow },
874 { "sizeof-bool", &Wsizeof_bool },
875 { "strict-prototypes", &Wstrict_prototypes },
876 { "pointer-arith", &Wpointer_arith },
877 { "sparse-error", &Wsparse_error },
878 { "tautological-compare", &Wtautological_compare },
879 { "transparent-union", &Wtransparent_union },
880 { "typesign", &Wtypesign },
881 { "undef", &Wundef },
882 { "uninitialized", &Wuninitialized },
883 { "union-cast", &Wunion_cast },
884 { "universal-initializer", &Wuniversal_initializer },
885 { "unknown-attribute", &Wunknown_attribute },
886 { "vla", &Wvla },
890 static char **handle_switch_W(char *arg, char **next)
892 char ** ret = handle_onoff_switch(arg, next, warnings);
893 if (ret)
894 return ret;
896 if (!strcmp(arg, "Wsparse-all")) {
897 int i;
898 for (i = 0; warnings[i].name; i++) {
899 if (*warnings[i].flag != FLAG_FORCE_OFF)
900 *warnings[i].flag = FLAG_ON;
904 // Unknown.
905 return next;
908 static void handle_switch_W_finalize(void)
910 handle_onoff_switch_finalize(warnings);
912 /* default Wdeclarationafterstatement based on the C dialect */
913 if (-1 == Wdeclarationafterstatement) {
914 switch (standard) {
915 case STANDARD_C89:
916 case STANDARD_C94:
917 Wdeclarationafterstatement = 1;
918 break;
919 default:
920 Wdeclarationafterstatement = 0;
921 break;
926 static char **handle_switch_x(char *arg, char **next)
928 if (!*++next)
929 die("missing argument for -x option");
930 return next;
934 static char **handle_arch(char *arg, char **next)
936 enum machine mach;
938 if (*arg++ != '=')
939 die("missing argument for --arch option");
941 mach = target_parse(arg);
942 if (mach != MACH_UNKNOWN)
943 target_config(mach);
945 return next;
948 static char **handle_param(char *arg, char **next)
950 char *value = NULL;
952 /* For now just skip any '--param=*' or '--param *' */
953 if (*arg == '\0') {
954 value = *++next;
955 } else if (isspace((unsigned char)*arg) || *arg == '=') {
956 value = ++arg;
959 if (!value)
960 die("missing argument for --param option");
962 return next;
965 static char **handle_os(char *arg, char **next)
967 if (*arg++ != '=')
968 die("missing argument for --os option");
970 target_os(arg);
972 return next;
975 static char **handle_version(char *arg, char **next)
977 printf("%s\n", SPARSE_VERSION);
978 exit(0);
981 struct switches {
982 const char *name;
983 char **(*fn)(char *, char **);
984 unsigned int prefix:1;
987 static char **handle_long_options(char *arg, char **next)
989 static struct switches cmd[] = {
990 { "arch", handle_arch, 1 },
991 { "os", handle_os, 1 },
992 { "param", handle_param, 1 },
993 { "version", handle_version },
994 { NULL, NULL }
996 struct switches *s = cmd;
998 while (s->name) {
999 int optlen = strlen(s->name);
1000 if (!strncmp(s->name, arg, optlen + !s->prefix))
1001 return s->fn(arg + optlen, next);
1002 s++;
1004 return next;
1007 char **handle_switch(char *arg, char **next)
1009 switch (*arg) {
1010 case 'a': return handle_switch_a(arg, next);
1011 case 'D': return handle_switch_D(arg, next);
1012 case 'd': return handle_switch_d(arg, next);
1013 case 'E': return handle_switch_E(arg, next);
1014 case 'f': return handle_switch_f(arg, next);
1015 case 'g': return handle_switch_g(arg, next);
1016 case 'G': return handle_switch_G(arg, next);
1017 case 'I': return handle_switch_I(arg, next);
1018 case 'i': return handle_switch_i(arg, next);
1019 case 'M': return handle_switch_M(arg, next);
1020 case 'm': return handle_switch_m(arg, next);
1021 case 'n': return handle_switch_n(arg, next);
1022 case 'o': return handle_switch_o(arg, next);
1023 case 'O': return handle_switch_O(arg, next);
1024 case 'p': return handle_switch_p(arg, next);
1025 case 's': return handle_switch_s(arg, next);
1026 case 'U': return handle_switch_U(arg, next);
1027 case 'v': return handle_switch_v(arg, next);
1028 case 'W': return handle_switch_W(arg, next);
1029 case 'x': return handle_switch_x(arg, next);
1030 case '-': return handle_long_options(arg + 1, next);
1031 default:
1032 break;
1036 * Ignore unknown command line options:
1037 * they're probably gcc switches
1039 return next;
1042 void handle_switch_finalize(void)
1044 handle_switch_v_finalize();
1045 handle_switch_W_finalize();