comparison: call simplify_binops() in get_comparison_helper()
[smatch.git] / options.c
blob294dfd3be77a11fb4f76d50e6aa38ad54f271582
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_char = 0;
86 int Waddress = 0;
87 int Waddress_space = 1;
88 int Wbitwise = 1;
89 int Wbitwise_pointer = 0;
90 int Wcast_from_as = 0;
91 int Wcast_to_as = 0;
92 int Wcast_truncate = 1;
93 int Wconstant_suffix = 0;
94 int Wconstexpr_not_const = 0;
95 int Wcontext = 1;
96 int Wdecl = 1;
97 int Wdeclarationafterstatement = -1;
98 int Wdefault_bitfield_sign = 0;
99 int Wdesignated_init = 1;
100 int Wdo_while = 0;
101 int Wenum_mismatch = 1;
102 int Wexternal_function_has_definition = 1;
103 int Wimplicit_int = 1;
104 int Winit_cstring = 0;
105 int Wint_to_pointer_cast = 1;
106 int Wmemcpy_max_count = 1;
107 int Wnewline_eof = 1;
108 int Wnon_pointer_null = 1;
109 int Wold_initializer = 1;
110 int Wold_style_definition = 1;
111 int Wone_bit_signed_bitfield = 1;
112 int Woverride_init = 1;
113 int Woverride_init_all = 0;
114 int Woverride_init_whole_range = 0;
115 int Wparen_string = 0;
116 int Wpast_deep_designator = 0;
117 int Wpedantic = 0;
118 int Wpointer_arith = 0;
119 int Wpointer_to_int_cast = 1;
120 int Wptr_subtraction_blows = 0;
121 int Wreturn_void = 0;
122 int Wshadow = 0;
123 int Wshift_count_negative = 1;
124 int Wshift_count_overflow = 1;
125 int Wsizeof_bool = 0;
126 int Wsparse_error = FLAG_FORCE_OFF;
127 int Wstrict_prototypes = 1;
128 int Wtautological_compare = 0;
129 int Wtransparent_union = 0;
130 int Wtypesign = 0;
131 int Wundef = 0;
132 int Wuninitialized = 1;
133 int Wunion_cast = 0;
134 int Wuniversal_initializer = 0;
135 int Wunknown_attribute = 0;
136 int Wvla = 1;
138 ////////////////////////////////////////////////////////////////////////////////
139 // Helpers for option parsing
141 static const char *match_option(const char *arg, const char *prefix)
143 unsigned int n = strlen(prefix);
144 if (strncmp(arg, prefix, n) == 0)
145 return arg + n;
146 return NULL;
150 struct val_map {
151 const char *name;
152 int val;
155 static int handle_subopt_val(const char *opt, const char *arg, const struct val_map *map, int *flag)
157 const char *name;
159 if (*arg++ != '=')
160 die("missing argument for option '%s'", opt);
161 for (;(name = map->name); map++) {
162 if (strcmp(name, arg) == 0 || strcmp(name, "*") == 0) {
163 *flag = map->val;
164 return 1;
166 if (strcmp(name, "?") == 0)
167 die("invalid argument '%s' in option '%s'", arg, opt);
169 return 0;
173 struct mask_map {
174 const char *name;
175 unsigned long mask;
178 static int apply_mask(unsigned long *val, const char *str, unsigned len, const struct mask_map *map, int neg)
180 const char *name;
182 for (;(name = map->name); map++) {
183 if (!strncmp(name, str, len) && !name[len]) {
184 if (neg == 0)
185 *val |= map->mask;
186 else
187 *val &= ~map->mask;
188 return 0;
191 return 1;
194 static int handle_suboption_mask(const char *arg, const char *opt, const struct mask_map *map, unsigned long *flag)
196 if (*opt == '\0') {
197 apply_mask(flag, "", 0, map, 0);
198 return 1;
200 if (*opt++ != '=')
201 return 0;
202 while (1) {
203 unsigned int len = strcspn(opt, ",+");
204 int neg = 0;
205 if (len == 0)
206 goto end;
207 if (!strncmp(opt, "no-", 3)) {
208 opt += 3;
209 len -= 3;
210 neg = 1;
212 if (apply_mask(flag, opt, len, map, neg))
213 die("error: wrong option '%.*s' for \'%s\'", len, opt, arg);
215 end:
216 opt += len;
217 if (*opt++ == '\0')
218 break;
220 return 1;
224 #define OPT_INVERSE 1
225 #define OPT_VAL 2
226 struct flag {
227 const char *name;
228 int *flag;
229 int (*fun)(const char *arg, const char *opt, const struct flag *, int options);
230 unsigned long mask;
231 int val;
234 static int handle_switches(const char *ori, const char *opt, const struct flag *flags)
236 const char *arg = opt;
237 int val = 1;
239 // Prefixe "no-" mean to turn flag off.
240 if (strncmp(arg, "no-", 3) == 0) {
241 arg += 3;
242 val = 0;
245 for (; flags->name; flags++) {
246 const char *opt = match_option(arg, flags->name);
247 int rc;
249 if (!opt)
250 continue;
252 if (flags->fun) {
253 int options = 0;
254 if (!val)
255 options |= OPT_INVERSE;
256 if ((rc = flags->fun(ori, opt, flags, options)))
257 return rc;
260 // boolean flag
261 if (opt[0] == '\0' && flags->flag) {
262 if (flags->mask & OPT_VAL)
263 val = flags->val;
264 if (flags->mask & OPT_INVERSE)
265 val = !val;
266 *flags->flag = val;
267 return 1;
271 // not handled
272 return 0;
275 static char **handle_onoff_switch(char *arg, char **next, const struct flag flags[])
277 int flag = FLAG_ON;
278 char *p = arg + 1;
279 unsigned i;
281 // Prefixes "no" and "no-" mean to turn warning off.
282 if (p[0] == 'n' && p[1] == 'o') {
283 p += 2;
284 if (p[0] == '-')
285 p++;
286 flag = FLAG_FORCE_OFF;
289 for (i = 0; flags[i].name; i++) {
290 if (!strcmp(p,flags[i].name)) {
291 *flags[i].flag = flag;
292 return next;
296 // Unknown.
297 return NULL;
300 static void handle_onoff_switch_finalize(const struct flag flags[])
302 unsigned i;
304 for (i = 0; flags[i].name; i++) {
305 if (*flags[i].flag == FLAG_FORCE_OFF)
306 *flags[i].flag = FLAG_OFF;
310 static int handle_switch_setval(const char *arg, const char *opt, const struct flag *flag, int options)
312 *(flag->flag) = flag->mask;
313 return 1;
317 #define OPTNUM_ZERO_IS_INF 1
318 #define OPTNUM_UNLIMITED 2
320 #define OPT_NUMERIC(NAME, TYPE, FUNCTION) \
321 static int opt_##NAME(const char *arg, const char *opt, TYPE *ptr, int flag) \
323 char *end; \
324 TYPE val; \
326 val = FUNCTION(opt, &end, 0); \
327 if (*end != '\0' || end == opt) { \
328 if ((flag & OPTNUM_UNLIMITED) && !strcmp(opt, "unlimited")) \
329 val = ~val; \
330 else \
331 die("error: wrong argument to \'%s\'", arg); \
333 if ((flag & OPTNUM_ZERO_IS_INF) && val == 0) \
334 val = ~val; \
335 *ptr = val; \
336 return 1; \
339 OPT_NUMERIC(ullong, unsigned long long, strtoull)
340 OPT_NUMERIC(uint, unsigned int, strtoul)
342 ////////////////////////////////////////////////////////////////////////////////
343 // Option parsing
345 static char **handle_switch_a(char *arg, char **next)
347 if (!strcmp(arg, "ansi"))
348 standard = STANDARD_C89;
350 return next;
353 static char **handle_switch_D(char *arg, char **next)
355 const char *name = arg + 1;
356 const char *value = "1";
358 if (!*name) {
359 arg = *++next;
360 if (!arg)
361 die("argument to `-D' is missing");
362 name = arg;
365 for (;;arg++) {
366 char c;
367 c = *arg;
368 if (!c)
369 break;
370 if (c == '=') {
371 *arg = '\0';
372 value = arg + 1;
373 break;
376 add_pre_buffer("#define %s %s\n", name, value);
377 return next;
380 static char **handle_switch_d(char *arg, char **next)
382 char *arg_char = arg + 1;
385 * -d<CHARS>, where <CHARS> is a sequence of characters, not preceded
386 * by a space. If you specify characters whose behaviour conflicts,
387 * the result is undefined.
389 while (*arg_char) {
390 switch (*arg_char) {
391 case 'M': /* dump just the macro definitions */
392 dump_macros_only = 1;
393 dump_macro_defs = 0;
394 break;
395 case 'D': /* like 'M', but also output pre-processed text */
396 dump_macro_defs = 1;
397 dump_macros_only = 0;
398 break;
399 case 'N': /* like 'D', but only output macro names not bodies */
400 break;
401 case 'I': /* like 'D', but also output #include directives */
402 break;
403 case 'U': /* like 'D', but only output expanded macros */
404 break;
406 arg_char++;
408 return next;
411 static char **handle_switch_E(char *arg, char **next)
413 if (arg[1] == '\0')
414 preprocess_only = 1;
415 return next;
418 static int handle_ftabstop(const char *arg, const char *opt, const struct flag *flag, int options)
420 unsigned long val;
421 char *end;
423 if (*opt == '\0')
424 die("error: missing argument to \"%s\"", arg);
426 /* we silently ignore silly values */
427 val = strtoul(opt, &end, 10);
428 if (*end == '\0' && 1 <= val && val <= 100)
429 tabstop = val;
431 return 1;
434 static int handle_fpasses(const char *arg, const char *opt, const struct flag *flag, int options)
436 unsigned long mask;
438 mask = flag->mask;
439 if (*opt == '\0') {
440 if (options & OPT_INVERSE)
441 fpasses &= ~mask;
442 else
443 fpasses |= mask;
444 return 1;
446 if (options & OPT_INVERSE)
447 return 0;
448 if (!strcmp(opt, "-enable")) {
449 fpasses |= mask;
450 return 1;
452 if (!strcmp(opt, "-disable")) {
453 fpasses &= ~mask;
454 return 1;
456 if (!strcmp(opt, "=last")) {
457 // clear everything above
458 mask |= mask - 1;
459 fpasses &= mask;
460 return 1;
462 return 0;
465 static int handle_fdiagnostic_prefix(const char *arg, const char *opt, const struct flag *flag, int options)
467 switch (*opt) {
468 case '\0':
469 diag_prefix = "sparse: ";
470 return 1;
471 case '=':
472 diag_prefix = xasprintf("%s: ", opt+1);
473 return 1;
474 default:
475 return 0;
479 static int handle_fdump_ir(const char *arg, const char *opt, const struct flag *flag, int options)
481 static const struct mask_map dump_ir_options[] = {
482 { "", PASS_LINEARIZE },
483 { "linearize", PASS_LINEARIZE },
484 { "mem2reg", PASS_MEM2REG },
485 { "final", PASS_FINAL },
486 { },
489 return handle_suboption_mask(arg, opt, dump_ir_options, &fdump_ir);
492 static int handle_fmemcpy_max_count(const char *arg, const char *opt, const struct flag *flag, int options)
494 opt_ullong(arg, opt, &fmemcpy_max_count, OPTNUM_ZERO_IS_INF|OPTNUM_UNLIMITED);
495 return 1;
498 static int handle_fmax_errors(const char *arg, const char *opt, const struct flag *flag, int options)
500 opt_uint(arg, opt, &fmax_errors, OPTNUM_UNLIMITED);
501 return 1;
504 static int handle_fmax_warnings(const char *arg, const char *opt, const struct flag *flag, int options)
506 opt_uint(arg, opt, &fmax_warnings, OPTNUM_UNLIMITED);
507 return 1;
510 static struct flag fflags[] = {
511 { "diagnostic-prefix", NULL, handle_fdiagnostic_prefix },
512 { "dump-ir", NULL, handle_fdump_ir },
513 { "freestanding", &fhosted, NULL, OPT_INVERSE },
514 { "hosted", &fhosted },
515 { "linearize", NULL, handle_fpasses, PASS_LINEARIZE },
516 { "max-errors=", NULL, handle_fmax_errors },
517 { "max-warnings=", NULL, handle_fmax_warnings },
518 { "mem-report", &fmem_report },
519 { "memcpy-max-count=", NULL, handle_fmemcpy_max_count },
520 { "tabstop=", NULL, handle_ftabstop },
521 { "mem2reg", NULL, handle_fpasses, PASS_MEM2REG },
522 { "optim", NULL, handle_fpasses, PASS_OPTIM },
523 { "pic", &fpic, handle_switch_setval, 1 },
524 { "PIC", &fpic, handle_switch_setval, 2 },
525 { "pie", &fpie, handle_switch_setval, 1 },
526 { "PIE", &fpie, handle_switch_setval, 2 },
527 { "signed-char", &funsigned_char, NULL, OPT_INVERSE },
528 { "short-wchar", &fshort_wchar },
529 { "unsigned-char", &funsigned_char, NULL, },
530 { },
533 static char **handle_switch_f(char *arg, char **next)
535 if (handle_switches(arg-1, arg+1, fflags))
536 return next;
538 return next;
541 static char **handle_switch_G(char *arg, char **next)
543 if (!strcmp(arg, "G") && *next)
544 return next + 1; // "-G 0"
545 else
546 return next; // "-G0" or (bogus) terminal "-G"
549 static char **handle_base_dir(char *arg, char **next)
551 gcc_base_dir = *++next;
552 if (!gcc_base_dir)
553 die("missing argument for -gcc-base-dir option");
554 return next;
557 static char **handle_switch_g(char *arg, char **next)
559 if (!strcmp(arg, "gcc-base-dir"))
560 return handle_base_dir(arg, next);
562 return next;
565 static char **handle_switch_I(char *arg, char **next)
567 char *path = arg+1;
569 switch (arg[1]) {
570 case '-':
571 add_pre_buffer("#split_include\n");
572 break;
574 case '\0': /* Plain "-I" */
575 path = *++next;
576 if (!path)
577 die("missing argument for -I option");
578 /* Fall through */
579 default:
580 add_pre_buffer("#add_include \"%s/\"\n", path);
582 return next;
585 static void add_cmdline_include(char *filename)
587 if (cmdline_include_nr >= CMDLINE_INCLUDE)
588 die("too many include files for %s\n", filename);
589 cmdline_include[cmdline_include_nr++] = filename;
592 static char **handle_switch_i(char *arg, char **next)
594 if (*next && !strcmp(arg, "include"))
595 add_cmdline_include(*++next);
596 else if (*next && !strcmp(arg, "imacros"))
597 add_cmdline_include(*++next);
598 else if (*next && !strcmp(arg, "isystem")) {
599 char *path = *++next;
600 if (!path)
601 die("missing argument for -isystem option");
602 add_pre_buffer("#add_isystem \"%s/\"\n", path);
603 } else if (*next && !strcmp(arg, "idirafter")) {
604 char *path = *++next;
605 if (!path)
606 die("missing argument for -idirafter option");
607 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
609 return next;
612 static char **handle_switch_M(char *arg, char **next)
614 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
615 if (!*next)
616 die("missing argument for -%s option", arg);
617 return next + 1;
619 return next;
622 static int handle_march(const char *opt, const char *arg, const struct flag *flag, int options)
624 if (arch_target->parse_march)
625 arch_target->parse_march(arg);
626 return 1;
629 static int handle_mcmodel(const char *opt, const char *arg, const struct flag *flag, int options)
631 static const struct val_map cmodels[] = {
632 { "kernel", CMODEL_KERNEL },
633 { "large", CMODEL_LARGE },
634 { "medany", CMODEL_MEDANY },
635 { "medium", CMODEL_MEDIUM },
636 { "medlow", CMODEL_MEDLOW },
637 { "small", CMODEL_SMALL },
638 { "tiny", CMODEL_TINY },
639 { },
641 return handle_subopt_val(opt, arg, cmodels, flag->flag);
644 static int handle_mfloat_abi(const char *opt, const char *arg, const struct flag *flag, int options) {
645 static const struct val_map fp_abis[] = {
646 { "hard", FP_ABI_HARD },
647 { "soft", FP_ABI_SOFT },
648 { "softfp", FP_ABI_HYBRID },
649 { "?" },
651 return handle_subopt_val(opt, arg, fp_abis, flag->flag);
654 static char **handle_multiarch_dir(char *arg, char **next)
656 multiarch_dir = *++next;
657 if (!multiarch_dir)
658 die("missing argument for -multiarch-dir option");
659 return next;
662 static const struct flag mflags[] = {
663 { "64", &arch_m64, NULL, OPT_VAL, ARCH_LP64 },
664 { "32", &arch_m64, NULL, OPT_VAL, ARCH_LP32 },
665 { "31", &arch_m64, NULL, OPT_VAL, ARCH_LP32 },
666 { "16", &arch_m64, NULL, OPT_VAL, ARCH_LP32 },
667 { "x32",&arch_m64, NULL, OPT_VAL, ARCH_X32 },
668 { "size-llp64", &arch_m64, NULL, OPT_VAL, ARCH_LLP64 },
669 { "size-long", &arch_msize_long },
670 { "arch=", NULL, handle_march },
671 { "big-endian", &arch_big_endian, NULL },
672 { "little-endian", &arch_big_endian, NULL, OPT_INVERSE },
673 { "cmodel", &arch_cmodel, handle_mcmodel },
674 { "float-abi", &arch_fp_abi, handle_mfloat_abi },
675 { "hard-float", &arch_fp_abi, NULL, OPT_VAL, FP_ABI_HARD },
676 { "soft-float", &arch_fp_abi, NULL, OPT_VAL, FP_ABI_SOFT },
680 static char **handle_switch_m(char *arg, char **next)
682 if (!strcmp(arg, "multiarch-dir")) {
683 return handle_multiarch_dir(arg, next);
684 } else {
685 handle_switches(arg-1, arg+1, mflags);
688 return next;
691 static char **handle_nostdinc(char *arg, char **next)
693 add_pre_buffer("#nostdinc\n");
694 return next;
697 static char **handle_switch_n(char *arg, char **next)
699 if (!strcmp(arg, "nostdinc"))
700 return handle_nostdinc(arg, next);
702 return next;
705 static char **handle_switch_O(char *arg, char **next)
707 int level = 1;
708 if (arg[1] >= '0' && arg[1] <= '9')
709 level = arg[1] - '0';
710 optimize_level = level;
711 optimize_size = arg[1] == 's';
712 return next;
715 static char **handle_switch_o(char *arg, char **next)
717 if (!strcmp(arg, "o")) { // "-o foo"
718 if (!*++next)
719 die("argument to '-o' is missing");
720 outfile = *next;
722 // else "-ofoo"
724 return next;
727 static const struct flag pflags[] = {
728 { "pedantic", &Wpedantic, NULL, OPT_VAL, FLAG_ON },
732 static char **handle_switch_p(char *arg, char **next)
734 handle_switches(arg-1, arg, pflags);
735 return next;
738 static char **handle_switch_s(const char *arg, char **next)
740 if ((arg = match_option(arg, "std="))) {
741 if (!strcmp(arg, "c89") ||
742 !strcmp(arg, "iso9899:1990"))
743 standard = STANDARD_C89;
745 else if (!strcmp(arg, "iso9899:199409"))
746 standard = STANDARD_C94;
748 else if (!strcmp(arg, "c99") ||
749 !strcmp(arg, "c9x") ||
750 !strcmp(arg, "iso9899:1999") ||
751 !strcmp(arg, "iso9899:199x"))
752 standard = STANDARD_C99;
754 else if (!strcmp(arg, "gnu89"))
755 standard = STANDARD_GNU89;
757 else if (!strcmp(arg, "gnu99") || !strcmp(arg, "gnu9x"))
758 standard = STANDARD_GNU99;
760 else if (!strcmp(arg, "c11") ||
761 !strcmp(arg, "c1x") ||
762 !strcmp(arg, "iso9899:2011"))
763 standard = STANDARD_C11;
765 else if (!strcmp(arg, "gnu11"))
766 standard = STANDARD_GNU11;
768 else if (!strcmp(arg, "c17") ||
769 !strcmp(arg, "c18") ||
770 !strcmp(arg, "iso9899:2017") ||
771 !strcmp(arg, "iso9899:2018"))
772 standard = STANDARD_C17;
773 else if (!strcmp(arg, "gnu17") ||
774 !strcmp(arg, "gnu18"))
775 standard = STANDARD_GNU17;
777 else
778 die("Unsupported C dialect");
781 return next;
784 static char **handle_switch_U(char *arg, char **next)
786 const char *name = arg + 1;
788 if (*name == '\0') {
789 name = *++next;
790 if (!name)
791 die("argument to `-U' is missing");
793 add_pre_buffer("#undef %s\n", name);
794 return next;
797 static struct flag debugs[] = {
798 { "compound", &dbg_compound},
799 { "dead", &dbg_dead},
800 { "domtree", &dbg_domtree},
801 { "entry", &dbg_entry},
802 { "ir", &dbg_ir},
803 { "postorder", &dbg_postorder},
807 static char **handle_switch_v(char *arg, char **next)
809 char ** ret = handle_onoff_switch(arg, next, debugs);
810 if (ret)
811 return ret;
813 // Unknown.
814 do {
815 verbose++;
816 } while (*++arg == 'v');
817 return next;
820 static void handle_switch_v_finalize(void)
822 handle_onoff_switch_finalize(debugs);
825 static const struct flag warnings[] = {
826 { "address", &Waddress },
827 { "address-space", &Waddress_space },
828 { "bitwise", &Wbitwise },
829 { "bitwise-pointer", &Wbitwise_pointer},
830 { "cast-from-as", &Wcast_from_as },
831 { "cast-to-as", &Wcast_to_as },
832 { "cast-truncate", &Wcast_truncate },
833 { "constant-suffix", &Wconstant_suffix },
834 { "constexpr-not-const", &Wconstexpr_not_const},
835 { "context", &Wcontext },
836 { "decl", &Wdecl },
837 { "declaration-after-statement", &Wdeclarationafterstatement },
838 { "default-bitfield-sign", &Wdefault_bitfield_sign },
839 { "designated-init", &Wdesignated_init },
840 { "do-while", &Wdo_while },
841 { "enum-mismatch", &Wenum_mismatch },
842 { "external-function-has-definition", &Wexternal_function_has_definition },
843 { "implicit-int", &Wimplicit_int },
844 { "init-cstring", &Winit_cstring },
845 { "int-to-pointer-cast", &Wint_to_pointer_cast },
846 { "memcpy-max-count", &Wmemcpy_max_count },
847 { "non-pointer-null", &Wnon_pointer_null },
848 { "newline-eof", &Wnewline_eof },
849 { "old-initializer", &Wold_initializer },
850 { "old-style-definition", &Wold_style_definition },
851 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
852 { "override-init", &Woverride_init },
853 { "override-init-all", &Woverride_init_all },
854 { "paren-string", &Wparen_string },
855 { "past-deep-designator", &Wpast_deep_designator },
856 { "pedantic", &Wpedantic },
857 { "pointer-to-int-cast", &Wpointer_to_int_cast },
858 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
859 { "return-void", &Wreturn_void },
860 { "shadow", &Wshadow },
861 { "shift-count-negative", &Wshift_count_negative },
862 { "shift-count-overflow", &Wshift_count_overflow },
863 { "sizeof-bool", &Wsizeof_bool },
864 { "strict-prototypes", &Wstrict_prototypes },
865 { "pointer-arith", &Wpointer_arith },
866 { "sparse-error", &Wsparse_error },
867 { "tautological-compare", &Wtautological_compare },
868 { "transparent-union", &Wtransparent_union },
869 { "typesign", &Wtypesign },
870 { "undef", &Wundef },
871 { "uninitialized", &Wuninitialized },
872 { "union-cast", &Wunion_cast },
873 { "universal-initializer", &Wuniversal_initializer },
874 { "unknown-attribute", &Wunknown_attribute },
875 { "vla", &Wvla },
879 static char **handle_switch_W(char *arg, char **next)
881 char ** ret = handle_onoff_switch(arg, next, warnings);
882 if (ret)
883 return ret;
885 if (!strcmp(arg, "Wsparse-all")) {
886 int i;
887 for (i = 0; warnings[i].name; i++) {
888 if (*warnings[i].flag != FLAG_FORCE_OFF)
889 *warnings[i].flag = FLAG_ON;
893 // Unknown.
894 return next;
897 static void handle_switch_W_finalize(void)
899 handle_onoff_switch_finalize(warnings);
901 /* default Wdeclarationafterstatement based on the C dialect */
902 if (-1 == Wdeclarationafterstatement) {
903 switch (standard) {
904 case STANDARD_C89:
905 case STANDARD_C94:
906 Wdeclarationafterstatement = 1;
907 break;
908 default:
909 Wdeclarationafterstatement = 0;
910 break;
915 static char **handle_switch_x(char *arg, char **next)
917 if (!*++next)
918 die("missing argument for -x option");
919 return next;
923 static char **handle_arch(char *arg, char **next)
925 enum machine mach;
927 if (*arg++ != '=')
928 die("missing argument for --arch option");
930 mach = target_parse(arg);
931 if (mach != MACH_UNKNOWN)
932 target_config(mach);
934 return next;
937 static char **handle_param(char *arg, char **next)
939 char *value = NULL;
941 /* For now just skip any '--param=*' or '--param *' */
942 if (*arg == '\0') {
943 value = *++next;
944 } else if (isspace((unsigned char)*arg) || *arg == '=') {
945 value = ++arg;
948 if (!value)
949 die("missing argument for --param option");
951 return next;
954 static char **handle_os(char *arg, char **next)
956 if (*arg++ != '=')
957 die("missing argument for --os option");
959 target_os(arg);
961 return next;
964 static char **handle_version(char *arg, char **next)
966 printf("%s\n", SPARSE_VERSION);
967 exit(0);
970 struct switches {
971 const char *name;
972 char **(*fn)(char *, char **);
973 unsigned int prefix:1;
976 static char **handle_long_options(char *arg, char **next)
978 static struct switches cmd[] = {
979 { "arch", handle_arch, 1 },
980 { "os", handle_os, 1 },
981 { "param", handle_param, 1 },
982 { "version", handle_version },
983 { NULL, NULL }
985 struct switches *s = cmd;
987 while (s->name) {
988 int optlen = strlen(s->name);
989 if (!strncmp(s->name, arg, optlen + !s->prefix))
990 return s->fn(arg + optlen, next);
991 s++;
993 return next;
996 char **handle_switch(char *arg, char **next)
998 switch (*arg) {
999 case 'a': return handle_switch_a(arg, next);
1000 case 'D': return handle_switch_D(arg, next);
1001 case 'd': return handle_switch_d(arg, next);
1002 case 'E': return handle_switch_E(arg, next);
1003 case 'f': return handle_switch_f(arg, next);
1004 case 'g': return handle_switch_g(arg, next);
1005 case 'G': return handle_switch_G(arg, next);
1006 case 'I': return handle_switch_I(arg, next);
1007 case 'i': return handle_switch_i(arg, next);
1008 case 'M': return handle_switch_M(arg, next);
1009 case 'm': return handle_switch_m(arg, next);
1010 case 'n': return handle_switch_n(arg, next);
1011 case 'o': return handle_switch_o(arg, next);
1012 case 'O': return handle_switch_O(arg, next);
1013 case 'p': return handle_switch_p(arg, next);
1014 case 's': return handle_switch_s(arg, next);
1015 case 'U': return handle_switch_U(arg, next);
1016 case 'v': return handle_switch_v(arg, next);
1017 case 'W': return handle_switch_W(arg, next);
1018 case 'x': return handle_switch_x(arg, next);
1019 case '-': return handle_long_options(arg + 1, next);
1020 default:
1021 break;
1025 * Ignore unknown command line options:
1026 * they're probably gcc switches
1028 return next;
1031 void handle_switch_finalize(void)
1033 handle_switch_v_finalize();
1034 handle_switch_W_finalize();