1 /* Command line option handling. 2 Copyright (C) 2006-2018 Free Software Foundation, Inc. 3 4 This file is part of GCC. 5 6 GCC is free software; you can redistribute it and/or modify it under 7 the terms of the GNU General Public License as published by the Free 8 Software Foundation; either version 3, or (at your option) any later 9 version. 10 11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12 WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GCC; see the file COPYING3. If not see 18 <http://www.gnu.org/licenses/>. */ 19 20 #include "config.h" 21 #include "system.h" 22 #include "intl.h" 23 #include "coretypes.h" 24 #include "opts.h" 25 #include "options.h" 26 #include "diagnostic.h" 27 #include "spellcheck.h" 28 29 static void prune_options (struct cl_decoded_option **, unsigned int *); 30 31 /* An option that is undocumented, that takes a joined argument, and 32 that doesn't fit any of the classes of uses (language/common, 33 driver, target) is assumed to be a prefix used to catch 34 e.g. negated options, and stop them from being further shortened to 35 a prefix that could use the negated option as an argument. For 36 example, we want -gno-statement-frontiers to be taken as a negation 37 of -gstatement-frontiers, but without catching the gno- prefix and 38 signaling it's to be used for option remapping, it would end up 39 backtracked to g with no-statemnet-frontiers as the debug level. */ 40 41 static bool 42 remapping_prefix_p (const struct cl_option *opt) 43 { 44 return opt->flags & CL_UNDOCUMENTED 45 && opt->flags & CL_JOINED 46 && !(opt->flags & (CL_DRIVER | CL_TARGET | CL_COMMON | CL_LANG_ALL)); 47 } 48 49 /* Perform a binary search to find which option the command-line INPUT 50 matches. Returns its index in the option array, and 51 OPT_SPECIAL_unknown on failure. 52 53 This routine is quite subtle. A normal binary search is not good 54 enough because some options can be suffixed with an argument, and 55 multiple sub-matches can occur, e.g. input of "-pedantic" matching 56 the initial substring of "-pedantic-errors". 57 58 A more complicated example is -gstabs. It should match "-g" with 59 an argument of "stabs". Suppose, however, that the number and list 60 of switches are such that the binary search tests "-gen-decls" 61 before having tested "-g". This doesn't match, and as "-gen-decls" 62 is less than "-gstabs", it will become the lower bound of the 63 binary search range, and "-g" will never be seen. To resolve this 64 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member, 65 to "-g" so that failed searches that end between "-gen-decls" and 66 the lexicographically subsequent switch know to go back and see if 67 "-g" causes a match (which it does in this example). 68 69 This search is done in such a way that the longest match for the 70 front end in question wins. If there is no match for the current 71 front end, the longest match for a different front end is returned 72 (or N_OPTS if none) and the caller emits an error message. */ 73 size_t 74 find_opt (const char *input, unsigned int lang_mask) 75 { 76 size_t mn, mn_orig, mx, md, opt_len; 77 size_t match_wrong_lang; 78 int comp; 79 80 mn = 0; 81 mx = cl_options_count; 82 83 /* Find mn such this lexicographical inequality holds: 84 cl_options[mn] <= input < cl_options[mn + 1]. */ 85 while (mx - mn > 1) 86 { 87 md = (mn + mx) / 2; 88 opt_len = cl_options[md].opt_len; 89 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len); 90 91 if (comp < 0) 92 mx = md; 93 else 94 mn = md; 95 } 96 97 mn_orig = mn; 98 99 /* This is the switch that is the best match but for a different 100 front end, or OPT_SPECIAL_unknown if there is no match at all. */ 101 match_wrong_lang = OPT_SPECIAL_unknown; 102 103 /* Backtrace the chain of possible matches, returning the longest 104 one, if any, that fits best. With current GCC switches, this 105 loop executes at most twice. */ 106 do 107 { 108 const struct cl_option *opt = &cl_options[mn]; 109 110 /* Is the input either an exact match or a prefix that takes a 111 joined argument? */ 112 if (!strncmp (input, opt->opt_text + 1, opt->opt_len) 113 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED))) 114 { 115 /* If language is OK, return it. */ 116 if (opt->flags & lang_mask) 117 return mn; 118 119 if (remapping_prefix_p (opt)) 120 return OPT_SPECIAL_unknown; 121 122 /* If we haven't remembered a prior match, remember this 123 one. Any prior match is necessarily better. */ 124 if (match_wrong_lang == OPT_SPECIAL_unknown) 125 match_wrong_lang = mn; 126 } 127 128 /* Try the next possibility. This is cl_options_count if there 129 are no more. */ 130 mn = opt->back_chain; 131 } 132 while (mn != cl_options_count); 133 134 if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-') 135 { 136 /* Long options, starting "--", may be abbreviated if the 137 abbreviation is unambiguous. This only applies to options 138 not taking a joined argument, and abbreviations of "--option" 139 are permitted even if there is a variant "--option=". */ 140 size_t mnc = mn_orig + 1; 141 size_t cmp_len = strlen (input); 142 while (mnc < cl_options_count 143 && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0) 144 { 145 /* Option matching this abbreviation. OK if it is the first 146 match and that does not take a joined argument, or the 147 second match, taking a joined argument and with only '=' 148 added to the first match; otherwise considered 149 ambiguous. */ 150 if (mnc == mn_orig + 1 151 && !(cl_options[mnc].flags & CL_JOINED)) 152 match_wrong_lang = mnc; 153 else if (mnc == mn_orig + 2 154 && match_wrong_lang == mn_orig + 1 155 && (cl_options[mnc].flags & CL_JOINED) 156 && (cl_options[mnc].opt_len 157 == cl_options[mn_orig + 1].opt_len + 1) 158 && strncmp (cl_options[mnc].opt_text + 1, 159 cl_options[mn_orig + 1].opt_text + 1, 160 cl_options[mn_orig + 1].opt_len) == 0) 161 ; /* OK, as long as there are no more matches. */ 162 else 163 return OPT_SPECIAL_unknown; 164 mnc++; 165 } 166 } 167 168 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */ 169 return match_wrong_lang; 170 } 171 172 /* If ARG is a non-negative decimal or hexadecimal integer, return its 173 value, otherwise return -1. */ 174 175 int 176 integral_argument (const char *arg) 177 { 178 const char *p = arg; 179 180 while (*p && ISDIGIT (*p)) 181 p++; 182 183 if (*p == '\0') 184 return atoi (arg); 185 186 /* It wasn't a decimal number - try hexadecimal. */ 187 if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X')) 188 { 189 p = arg + 2; 190 while (*p && ISXDIGIT (*p)) 191 p++; 192 193 if (p != arg + 2 && *p == '\0') 194 return strtol (arg, NULL, 16); 195 } 196 197 return -1; 198 } 199 200 /* Return whether OPTION is OK for the language given by 201 LANG_MASK. */ 202 static bool 203 option_ok_for_language (const struct cl_option *option, 204 unsigned int lang_mask) 205 { 206 if (!(option->flags & lang_mask)) 207 return false; 208 else if ((option->flags & CL_TARGET) 209 && (option->flags & (CL_LANG_ALL | CL_DRIVER)) 210 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET))) 211 /* Complain for target flag language mismatches if any languages 212 are specified. */ 213 return false; 214 return true; 215 } 216 217 /* Return whether ENUM_ARG is OK for the language given by 218 LANG_MASK. */ 219 220 static bool 221 enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg, 222 unsigned int lang_mask) 223 { 224 return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY); 225 } 226 227 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and 228 storing the value in *VALUE if found, and returning false without 229 modifying *VALUE if not found. */ 230 231 static bool 232 enum_arg_to_value (const struct cl_enum_arg *enum_args, 233 const char *arg, int *value, unsigned int lang_mask) 234 { 235 unsigned int i; 236 237 for (i = 0; enum_args[i].arg != NULL; i++) 238 if (strcmp (arg, enum_args[i].arg) == 0 239 && enum_arg_ok_for_language (&enum_args[i], lang_mask)) 240 { 241 *value = enum_args[i].value; 242 return true; 243 } 244 245 return false; 246 } 247 248 /* Look up ARG in the enum used by option OPT_INDEX for language 249 LANG_MASK, returning true and storing the value in *VALUE if found, 250 and returning false without modifying *VALUE if not found. */ 251 252 bool 253 opt_enum_arg_to_value (size_t opt_index, const char *arg, int *value, 254 unsigned int lang_mask) 255 { 256 const struct cl_option *option = &cl_options[opt_index]; 257 258 gcc_assert (option->var_type == CLVC_ENUM); 259 260 return enum_arg_to_value (cl_enums[option->var_enum].values, arg, 261 value, lang_mask); 262 } 263 264 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the 265 corresponding string in *ARGP, returning true if the found string 266 was marked as canonical, false otherwise. If VALUE is not found 267 (which may be the case for uninitialized values if the relevant 268 option has not been passed), set *ARGP to NULL and return 269 false. */ 270 271 bool 272 enum_value_to_arg (const struct cl_enum_arg *enum_args, 273 const char **argp, int value, unsigned int lang_mask) 274 { 275 unsigned int i; 276 277 for (i = 0; enum_args[i].arg != NULL; i++) 278 if (enum_args[i].value == value 279 && (enum_args[i].flags & CL_ENUM_CANONICAL) 280 && enum_arg_ok_for_language (&enum_args[i], lang_mask)) 281 { 282 *argp = enum_args[i].arg; 283 return true; 284 } 285 286 for (i = 0; enum_args[i].arg != NULL; i++) 287 if (enum_args[i].value == value 288 && enum_arg_ok_for_language (&enum_args[i], lang_mask)) 289 { 290 *argp = enum_args[i].arg; 291 return false; 292 } 293 294 *argp = NULL; 295 return false; 296 } 297 298 /* Fill in the canonical option part of *DECODED with an option 299 described by OPT_INDEX, ARG and VALUE. */ 300 301 static void 302 generate_canonical_option (size_t opt_index, const char *arg, int value, 303 struct cl_decoded_option *decoded) 304 { 305 const struct cl_option *option = &cl_options[opt_index]; 306 const char *opt_text = option->opt_text; 307 308 if (value == 0 309 && !option->cl_reject_negative 310 && (opt_text[1] == 'W' || opt_text[1] == 'f' 311 || opt_text[1] == 'g' || opt_text[1] == 'm')) 312 { 313 char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5); 314 t[0] = '-'; 315 t[1] = opt_text[1]; 316 t[2] = 'n'; 317 t[3] = 'o'; 318 t[4] = '-'; 319 memcpy (t + 5, opt_text + 2, option->opt_len); 320 opt_text = t; 321 } 322 323 decoded->canonical_option[2] = NULL; 324 decoded->canonical_option[3] = NULL; 325 326 if (arg) 327 { 328 if ((option->flags & CL_SEPARATE) 329 && !option->cl_separate_alias) 330 { 331 decoded->canonical_option[0] = opt_text; 332 decoded->canonical_option[1] = arg; 333 decoded->canonical_option_num_elements = 2; 334 } 335 else 336 { 337 gcc_assert (option->flags & CL_JOINED); 338 decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL); 339 decoded->canonical_option[1] = NULL; 340 decoded->canonical_option_num_elements = 1; 341 } 342 } 343 else 344 { 345 decoded->canonical_option[0] = opt_text; 346 decoded->canonical_option[1] = NULL; 347 decoded->canonical_option_num_elements = 1; 348 } 349 } 350 351 /* Structure describing mappings from options on the command line to 352 options to look up with find_opt. */ 353 struct option_map 354 { 355 /* Prefix of the option on the command line. */ 356 const char *opt0; 357 /* If two argv elements are considered to be merged into one option, 358 prefix for the second element, otherwise NULL. */ 359 const char *opt1; 360 /* The new prefix to map to. */ 361 const char *new_prefix; 362 /* Whether at least one character is needed following opt1 or opt0 363 for this mapping to be used. (--optimize= is valid for -O, but 364 --warn- is not valid for -W.) */ 365 bool another_char_needed; 366 /* Whether the original option is a negated form of the option 367 resulting from this map. */ 368 bool negated; 369 }; 370 static const struct option_map option_map[] = 371 { 372 { "-Wno-", NULL, "-W", false, true }, 373 { "-fno-", NULL, "-f", false, true }, 374 { "-gno-", NULL, "-g", false, true }, 375 { "-mno-", NULL, "-m", false, true }, 376 { "--debug=", NULL, "-g", false, false }, 377 { "--machine-", NULL, "-m", true, false }, 378 { "--machine-no-", NULL, "-m", false, true }, 379 { "--machine=", NULL, "-m", false, false }, 380 { "--machine=no-", NULL, "-m", false, true }, 381 { "--machine", "", "-m", false, false }, 382 { "--machine", "no-", "-m", false, true }, 383 { "--optimize=", NULL, "-O", false, false }, 384 { "--std=", NULL, "-std=", false, false }, 385 { "--std", "", "-std=", false, false }, 386 { "--warn-", NULL, "-W", true, false }, 387 { "--warn-no-", NULL, "-W", false, true }, 388 { "--", NULL, "-f", true, false }, 389 { "--no-", NULL, "-f", false, true } 390 }; 391 392 /* Helper function for gcc.c's driver::suggest_option, for populating the 393 vec of suggestions for misspelled options. 394 395 option_map above provides various prefixes for spelling command-line 396 options, which decode_cmdline_option uses to map spellings of options 397 to specific options. We want to do the reverse: to find all the ways 398 that a user could validly spell an option. 399 400 Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all 401 of its valid variant spellings to CANDIDATES, each without a leading 402 dash. 403 404 For example, given "-Wabi-tag", the following are added to CANDIDATES: 405 "Wabi-tag" 406 "Wno-abi-tag" 407 "-warn-abi-tag" 408 "-warn-no-abi-tag". 409 410 The added strings must be freed using free. */ 411 412 void 413 add_misspelling_candidates (auto_vec<char *> *candidates, 414 const struct cl_option *option, 415 const char *opt_text) 416 { 417 gcc_assert (candidates); 418 gcc_assert (option); 419 gcc_assert (opt_text); 420 if (remapping_prefix_p (option)) 421 return; 422 candidates->safe_push (xstrdup (opt_text + 1)); 423 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++) 424 { 425 const char *opt0 = option_map[i].opt0; 426 const char *new_prefix = option_map[i].new_prefix; 427 size_t new_prefix_len = strlen (new_prefix); 428 429 if (option->cl_reject_negative && option_map[i].negated) 430 continue; 431 432 if (strncmp (opt_text, new_prefix, new_prefix_len) == 0) 433 { 434 char *alternative = concat (opt0 + 1, opt_text + new_prefix_len, 435 NULL); 436 candidates->safe_push (alternative); 437 } 438 } 439 } 440 441 /* Decode the switch beginning at ARGV for the language indicated by 442 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into 443 the structure *DECODED. Returns the number of switches 444 consumed. */ 445 446 static unsigned int 447 decode_cmdline_option (const char **argv, unsigned int lang_mask, 448 struct cl_decoded_option *decoded) 449 { 450 size_t opt_index; 451 const char *arg = 0; 452 int value = 1; 453 unsigned int result = 1, i, extra_args, separate_args = 0; 454 int adjust_len = 0; 455 size_t total_len; 456 char *p; 457 const struct cl_option *option; 458 int errors = 0; 459 const char *warn_message = NULL; 460 bool separate_arg_flag; 461 bool joined_arg_flag; 462 bool have_separate_arg = false; 463 464 extra_args = 0; 465 466 const char *opt_value = argv[0] + 1; 467 opt_index = find_opt (opt_value, lang_mask); 468 i = 0; 469 while (opt_index == OPT_SPECIAL_unknown 470 && i < ARRAY_SIZE (option_map)) 471 { 472 const char *opt0 = option_map[i].opt0; 473 const char *opt1 = option_map[i].opt1; 474 const char *new_prefix = option_map[i].new_prefix; 475 bool another_char_needed = option_map[i].another_char_needed; 476 size_t opt0_len = strlen (opt0); 477 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1)); 478 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len); 479 size_t new_prefix_len = strlen (new_prefix); 480 481 extra_args = (opt1 == NULL ? 0 : 1); 482 value = !option_map[i].negated; 483 484 if (strncmp (argv[0], opt0, opt0_len) == 0 485 && (opt1 == NULL 486 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0)) 487 && (!another_char_needed 488 || argv[extra_args][optn_len] != 0)) 489 { 490 size_t arglen = strlen (argv[extra_args]); 491 char *dup; 492 493 adjust_len = (int) optn_len - (int) new_prefix_len; 494 dup = XNEWVEC (char, arglen + 1 - adjust_len); 495 memcpy (dup, new_prefix, new_prefix_len); 496 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len, 497 arglen - optn_len + 1); 498 opt_index = find_opt (dup + 1, lang_mask); 499 free (dup); 500 } 501 i++; 502 } 503 504 if (opt_index == OPT_SPECIAL_unknown) 505 { 506 arg = argv[0]; 507 extra_args = 0; 508 value = 1; 509 goto done; 510 } 511 512 option = &cl_options[opt_index]; 513 514 /* Reject negative form of switches that don't take negatives as 515 unrecognized. */ 516 if (!value && option->cl_reject_negative) 517 { 518 opt_index = OPT_SPECIAL_unknown; 519 errors |= CL_ERR_NEGATIVE; 520 arg = argv[0]; 521 goto done; 522 } 523 524 result = extra_args + 1; 525 warn_message = option->warn_message; 526 527 /* Check to see if the option is disabled for this configuration. */ 528 if (option->cl_disabled) 529 errors |= CL_ERR_DISABLED; 530 531 /* Determine whether there may be a separate argument based on 532 whether this option is being processed for the driver, and, if 533 so, how many such arguments. */ 534 separate_arg_flag = ((option->flags & CL_SEPARATE) 535 && !(option->cl_no_driver_arg 536 && (lang_mask & CL_DRIVER))); 537 separate_args = (separate_arg_flag 538 ? option->cl_separate_nargs + 1 539 : 0); 540 joined_arg_flag = (option->flags & CL_JOINED) != 0; 541 542 /* Sort out any argument the switch takes. */ 543 if (joined_arg_flag) 544 { 545 /* Have arg point to the original switch. This is because 546 some code, such as disable_builtin_function, expects its 547 argument to be persistent until the program exits. */ 548 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len; 549 550 if (*arg == '\0' && !option->cl_missing_ok) 551 { 552 if (separate_arg_flag) 553 { 554 arg = argv[extra_args + 1]; 555 result = extra_args + 2; 556 if (arg == NULL) 557 result = extra_args + 1; 558 else 559 have_separate_arg = true; 560 } 561 else 562 /* Missing argument. */ 563 arg = NULL; 564 } 565 } 566 else if (separate_arg_flag) 567 { 568 arg = argv[extra_args + 1]; 569 for (i = 0; i < separate_args; i++) 570 if (argv[extra_args + 1 + i] == NULL) 571 { 572 errors |= CL_ERR_MISSING_ARG; 573 break; 574 } 575 result = extra_args + 1 + i; 576 if (arg != NULL) 577 have_separate_arg = true; 578 } 579 580 if (arg == NULL && (separate_arg_flag || joined_arg_flag)) 581 errors |= CL_ERR_MISSING_ARG; 582 583 /* Is this option an alias (or an ignored option, marked as an alias 584 of OPT_SPECIAL_ignore)? */ 585 if (option->alias_target != N_OPTS 586 && (!option->cl_separate_alias || have_separate_arg)) 587 { 588 size_t new_opt_index = option->alias_target; 589 590 if (new_opt_index == OPT_SPECIAL_ignore) 591 { 592 gcc_assert (option->alias_arg == NULL); 593 gcc_assert (option->neg_alias_arg == NULL); 594 opt_index = new_opt_index; 595 arg = NULL; 596 value = 1; 597 } 598 else 599 { 600 const struct cl_option *new_option = &cl_options[new_opt_index]; 601 602 /* The new option must not be an alias itself. */ 603 gcc_assert (new_option->alias_target == N_OPTS 604 || new_option->cl_separate_alias); 605 606 if (option->neg_alias_arg) 607 { 608 gcc_assert (option->alias_arg != NULL); 609 gcc_assert (arg == NULL); 610 gcc_assert (!option->cl_negative_alias); 611 if (value) 612 arg = option->alias_arg; 613 else 614 arg = option->neg_alias_arg; 615 value = 1; 616 } 617 else if (option->alias_arg) 618 { 619 gcc_assert (value == 1); 620 gcc_assert (arg == NULL); 621 gcc_assert (!option->cl_negative_alias); 622 arg = option->alias_arg; 623 } 624 625 if (option->cl_negative_alias) 626 value = !value; 627 628 opt_index = new_opt_index; 629 option = new_option; 630 631 if (value == 0) 632 gcc_assert (!option->cl_reject_negative); 633 634 /* Recompute what arguments are allowed. */ 635 separate_arg_flag = ((option->flags & CL_SEPARATE) 636 && !(option->cl_no_driver_arg 637 && (lang_mask & CL_DRIVER))); 638 joined_arg_flag = (option->flags & CL_JOINED) != 0; 639 640 if (separate_args > 1 || option->cl_separate_nargs) 641 gcc_assert (separate_args 642 == (unsigned int) option->cl_separate_nargs + 1); 643 644 if (!(errors & CL_ERR_MISSING_ARG)) 645 { 646 if (separate_arg_flag || joined_arg_flag) 647 { 648 if (option->cl_missing_ok && arg == NULL) 649 arg = ""; 650 gcc_assert (arg != NULL); 651 } 652 else 653 gcc_assert (arg == NULL); 654 } 655 656 /* Recheck for warnings and disabled options. */ 657 if (option->warn_message) 658 { 659 gcc_assert (warn_message == NULL); 660 warn_message = option->warn_message; 661 } 662 if (option->cl_disabled) 663 errors |= CL_ERR_DISABLED; 664 } 665 } 666 667 /* Check if this is a switch for a different front end. */ 668 if (!option_ok_for_language (option, lang_mask)) 669 errors |= CL_ERR_WRONG_LANG; 670 else if (strcmp (option->opt_text, "-Werror=") == 0 671 && strchr (opt_value, ',') == NULL) 672 { 673 /* Verify that -Werror argument is a valid warning 674 for a language. */ 675 char *werror_arg = xstrdup (opt_value + 6); 676 werror_arg[0] = 'W'; 677 678 size_t warning_index = find_opt (werror_arg, lang_mask); 679 if (warning_index != OPT_SPECIAL_unknown) 680 { 681 const struct cl_option *warning_option 682 = &cl_options[warning_index]; 683 if (!option_ok_for_language (warning_option, lang_mask)) 684 errors |= CL_ERR_WRONG_LANG; 685 } 686 } 687 688 /* Convert the argument to lowercase if appropriate. */ 689 if (arg && option->cl_tolower) 690 { 691 size_t j; 692 size_t len = strlen (arg); 693 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1); 694 695 for (j = 0; j < len; j++) 696 arg_lower[j] = TOLOWER ((unsigned char) arg[j]); 697 arg_lower[len] = 0; 698 arg = arg_lower; 699 } 700 701 /* If the switch takes an integer, convert it. */ 702 if (arg && option->cl_uinteger) 703 { 704 value = integral_argument (arg); 705 if (value == -1) 706 errors |= CL_ERR_UINT_ARG; 707 708 /* Reject value out of a range. */ 709 if (option->range_max != -1 710 && (value < option->range_min || value > option->range_max)) 711 errors |= CL_ERR_INT_RANGE_ARG; 712 } 713 714 /* If the switch takes an enumerated argument, convert it. */ 715 if (arg && (option->var_type == CLVC_ENUM)) 716 { 717 const struct cl_enum *e = &cl_enums[option->var_enum]; 718 719 gcc_assert (value == 1); 720 if (enum_arg_to_value (e->values, arg, &value, lang_mask)) 721 { 722 const char *carg = NULL; 723 724 if (enum_value_to_arg (e->values, &carg, value, lang_mask)) 725 arg = carg; 726 gcc_assert (carg != NULL); 727 } 728 else 729 errors |= CL_ERR_ENUM_ARG; 730 } 731 732 done: 733 decoded->opt_index = opt_index; 734 decoded->arg = arg; 735 decoded->value = value; 736 decoded->errors = errors; 737 decoded->warn_message = warn_message; 738 739 if (opt_index == OPT_SPECIAL_unknown) 740 gcc_assert (result == 1); 741 742 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option)); 743 decoded->canonical_option_num_elements = result; 744 total_len = 0; 745 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++) 746 { 747 if (i < result) 748 { 749 size_t len; 750 if (opt_index == OPT_SPECIAL_unknown) 751 decoded->canonical_option[i] = argv[i]; 752 else 753 decoded->canonical_option[i] = NULL; 754 len = strlen (argv[i]); 755 /* If the argument is an empty string, we will print it as "" in 756 orig_option_with_args_text. */ 757 total_len += (len != 0 ? len : 2) + 1; 758 } 759 else 760 decoded->canonical_option[i] = NULL; 761 } 762 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore) 763 { 764 generate_canonical_option (opt_index, arg, value, decoded); 765 if (separate_args > 1) 766 { 767 for (i = 0; i < separate_args; i++) 768 { 769 if (argv[extra_args + 1 + i] == NULL) 770 break; 771 else 772 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i]; 773 } 774 gcc_assert (result == 1 + i); 775 decoded->canonical_option_num_elements = result; 776 } 777 } 778 decoded->orig_option_with_args_text 779 = p = XOBNEWVEC (&opts_obstack, char, total_len); 780 for (i = 0; i < result; i++) 781 { 782 size_t len = strlen (argv[i]); 783 784 /* Print the empty string verbally. */ 785 if (len == 0) 786 { 787 *p++ = '"'; 788 *p++ = '"'; 789 } 790 else 791 memcpy (p, argv[i], len); 792 p += len; 793 if (i == result - 1) 794 *p++ = 0; 795 else 796 *p++ = ' '; 797 } 798 799 return result; 800 } 801 802 /* Obstack for option strings. */ 803 804 struct obstack opts_obstack; 805 806 /* Like libiberty concat, but allocate using opts_obstack. */ 807 808 char * 809 opts_concat (const char *first, ...) 810 { 811 char *newstr, *end; 812 size_t length = 0; 813 const char *arg; 814 va_list ap; 815 816 /* First compute the size of the result and get sufficient memory. */ 817 va_start (ap, first); 818 for (arg = first; arg; arg = va_arg (ap, const char *)) 819 length += strlen (arg); 820 newstr = XOBNEWVEC (&opts_obstack, char, length + 1); 821 va_end (ap); 822 823 /* Now copy the individual pieces to the result string. */ 824 va_start (ap, first); 825 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *)) 826 { 827 length = strlen (arg); 828 memcpy (end, arg, length); 829 end += length; 830 } 831 *end = '\0'; 832 va_end (ap); 833 return newstr; 834 } 835 836 /* Decode command-line options (ARGC and ARGV being the arguments of 837 main) into an array, setting *DECODED_OPTIONS to a pointer to that 838 array and *DECODED_OPTIONS_COUNT to the number of entries in the 839 array. The first entry in the array is always one for the program 840 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language 841 flags applicable for decoding (including CL_COMMON and CL_TARGET if 842 those options should be considered applicable). Do not produce any 843 diagnostics or set state outside of these variables. */ 844 845 void 846 decode_cmdline_options_to_array (unsigned int argc, const char **argv, 847 unsigned int lang_mask, 848 struct cl_decoded_option **decoded_options, 849 unsigned int *decoded_options_count) 850 { 851 unsigned int n, i; 852 struct cl_decoded_option *opt_array; 853 unsigned int num_decoded_options; 854 855 opt_array = XNEWVEC (struct cl_decoded_option, argc); 856 857 opt_array[0].opt_index = OPT_SPECIAL_program_name; 858 opt_array[0].warn_message = NULL; 859 opt_array[0].arg = argv[0]; 860 opt_array[0].orig_option_with_args_text = argv[0]; 861 opt_array[0].canonical_option_num_elements = 1; 862 opt_array[0].canonical_option[0] = argv[0]; 863 opt_array[0].canonical_option[1] = NULL; 864 opt_array[0].canonical_option[2] = NULL; 865 opt_array[0].canonical_option[3] = NULL; 866 opt_array[0].value = 1; 867 opt_array[0].errors = 0; 868 num_decoded_options = 1; 869 870 for (i = 1; i < argc; i += n) 871 { 872 const char *opt = argv[i]; 873 874 /* Interpret "-" or a non-switch as a file name. */ 875 if (opt[0] != '-' || opt[1] == '\0') 876 { 877 generate_option_input_file (opt, &opt_array[num_decoded_options]); 878 num_decoded_options++; 879 n = 1; 880 continue; 881 } 882 883 n = decode_cmdline_option (argv + i, lang_mask, 884 &opt_array[num_decoded_options]); 885 num_decoded_options++; 886 } 887 888 *decoded_options = opt_array; 889 *decoded_options_count = num_decoded_options; 890 prune_options (decoded_options, decoded_options_count); 891 } 892 893 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the 894 next one is the same as ORIG_NEXT_OPT_IDX. */ 895 896 static bool 897 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx) 898 { 899 /* An option can be canceled by the same option or an option with 900 Negative. */ 901 if (cl_options [next_opt_idx].neg_index == opt_idx) 902 return true; 903 904 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx) 905 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index, 906 orig_next_opt_idx); 907 908 return false; 909 } 910 911 /* Filter out options canceled by the ones after them. */ 912 913 static void 914 prune_options (struct cl_decoded_option **decoded_options, 915 unsigned int *decoded_options_count) 916 { 917 unsigned int old_decoded_options_count = *decoded_options_count; 918 struct cl_decoded_option *old_decoded_options = *decoded_options; 919 unsigned int new_decoded_options_count; 920 struct cl_decoded_option *new_decoded_options 921 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count); 922 unsigned int i; 923 const struct cl_option *option; 924 unsigned int fdiagnostics_color_idx = 0; 925 926 /* Remove arguments which are negated by others after them. */ 927 new_decoded_options_count = 0; 928 for (i = 0; i < old_decoded_options_count; i++) 929 { 930 unsigned int j, opt_idx, next_opt_idx; 931 932 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG) 933 goto keep; 934 935 opt_idx = old_decoded_options[i].opt_index; 936 switch (opt_idx) 937 { 938 case OPT_SPECIAL_unknown: 939 case OPT_SPECIAL_ignore: 940 case OPT_SPECIAL_program_name: 941 case OPT_SPECIAL_input_file: 942 goto keep; 943 944 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */ 945 case OPT_fdiagnostics_color_: 946 fdiagnostics_color_idx = i; 947 continue; 948 949 default: 950 gcc_assert (opt_idx < cl_options_count); 951 option = &cl_options[opt_idx]; 952 if (option->neg_index < 0) 953 goto keep; 954 955 /* Skip joined switches. */ 956 if ((option->flags & CL_JOINED)) 957 goto keep; 958 959 for (j = i + 1; j < old_decoded_options_count; j++) 960 { 961 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG) 962 continue; 963 next_opt_idx = old_decoded_options[j].opt_index; 964 if (next_opt_idx >= cl_options_count) 965 continue; 966 if (cl_options[next_opt_idx].neg_index < 0) 967 continue; 968 if ((cl_options[next_opt_idx].flags & CL_JOINED)) 969 continue; 970 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx)) 971 break; 972 } 973 if (j == old_decoded_options_count) 974 { 975 keep: 976 new_decoded_options[new_decoded_options_count] 977 = old_decoded_options[i]; 978 new_decoded_options_count++; 979 } 980 break; 981 } 982 } 983 984 if (fdiagnostics_color_idx >= 1) 985 { 986 /* We put the last -fdiagnostics-color= at the first position 987 after argv[0] so it can take effect immediately. */ 988 memmove (new_decoded_options + 2, new_decoded_options + 1, 989 sizeof (struct cl_decoded_option) 990 * (new_decoded_options_count - 1)); 991 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx]; 992 new_decoded_options_count++; 993 } 994 995 free (old_decoded_options); 996 new_decoded_options = XRESIZEVEC (struct cl_decoded_option, 997 new_decoded_options, 998 new_decoded_options_count); 999 *decoded_options = new_decoded_options; 1000 *decoded_options_count = new_decoded_options_count; 1001 } 1002 1003 /* Handle option DECODED for the language indicated by LANG_MASK, 1004 using the handlers in HANDLERS and setting fields in OPTS and 1005 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics 1006 option, DK_UNSPECIFIED otherwise, and LOC is the location of the 1007 option for options from the source file, UNKNOWN_LOCATION 1008 otherwise. GENERATED_P is true for an option generated as part of 1009 processing another option or otherwise generated internally, false 1010 for one explicitly passed by the user. control_warning_option 1011 generated options are considered explicitly passed by the user. 1012 Returns false if the switch was invalid. DC is the diagnostic 1013 context for options affecting diagnostics state, or NULL. */ 1014 1015 static bool 1016 handle_option (struct gcc_options *opts, 1017 struct gcc_options *opts_set, 1018 const struct cl_decoded_option *decoded, 1019 unsigned int lang_mask, int kind, location_t loc, 1020 const struct cl_option_handlers *handlers, 1021 bool generated_p, diagnostic_context *dc) 1022 { 1023 size_t opt_index = decoded->opt_index; 1024 const char *arg = decoded->arg; 1025 int value = decoded->value; 1026 const struct cl_option *option = &cl_options[opt_index]; 1027 void *flag_var = option_flag_var (opt_index, opts); 1028 size_t i; 1029 1030 if (flag_var) 1031 set_option (opts, (generated_p ? NULL : opts_set), 1032 opt_index, value, arg, kind, loc, dc); 1033 1034 for (i = 0; i < handlers->num_handlers; i++) 1035 if (option->flags & handlers->handlers[i].mask) 1036 { 1037 if (!handlers->handlers[i].handler (opts, opts_set, decoded, 1038 lang_mask, kind, loc, 1039 handlers, dc, 1040 handlers->target_option_override_hook)) 1041 return false; 1042 } 1043 1044 return true; 1045 } 1046 1047 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the 1048 option instead of DECODED. This is used for callbacks when one 1049 option implies another instead of an option being decoded from the 1050 command line. */ 1051 1052 bool 1053 handle_generated_option (struct gcc_options *opts, 1054 struct gcc_options *opts_set, 1055 size_t opt_index, const char *arg, int value, 1056 unsigned int lang_mask, int kind, location_t loc, 1057 const struct cl_option_handlers *handlers, 1058 bool generated_p, diagnostic_context *dc) 1059 { 1060 struct cl_decoded_option decoded; 1061 1062 generate_option (opt_index, arg, value, lang_mask, &decoded); 1063 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc, 1064 handlers, generated_p, dc); 1065 } 1066 1067 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and 1068 VALUE for a front end using LANG_MASK. This is used when the 1069 compiler generates options internally. */ 1070 1071 void 1072 generate_option (size_t opt_index, const char *arg, int value, 1073 unsigned int lang_mask, struct cl_decoded_option *decoded) 1074 { 1075 const struct cl_option *option = &cl_options[opt_index]; 1076 1077 decoded->opt_index = opt_index; 1078 decoded->warn_message = NULL; 1079 decoded->arg = arg; 1080 decoded->value = value; 1081 decoded->errors = (option_ok_for_language (option, lang_mask) 1082 ? 0 1083 : CL_ERR_WRONG_LANG); 1084 1085 generate_canonical_option (opt_index, arg, value, decoded); 1086 switch (decoded->canonical_option_num_elements) 1087 { 1088 case 1: 1089 decoded->orig_option_with_args_text = decoded->canonical_option[0]; 1090 break; 1091 1092 case 2: 1093 decoded->orig_option_with_args_text 1094 = opts_concat (decoded->canonical_option[0], " ", 1095 decoded->canonical_option[1], NULL); 1096 break; 1097 1098 default: 1099 gcc_unreachable (); 1100 } 1101 } 1102 1103 /* Fill in *DECODED with an option for input file FILE. */ 1104 1105 void 1106 generate_option_input_file (const char *file, 1107 struct cl_decoded_option *decoded) 1108 { 1109 decoded->opt_index = OPT_SPECIAL_input_file; 1110 decoded->warn_message = NULL; 1111 decoded->arg = file; 1112 decoded->orig_option_with_args_text = file; 1113 decoded->canonical_option_num_elements = 1; 1114 decoded->canonical_option[0] = file; 1115 decoded->canonical_option[1] = NULL; 1116 decoded->canonical_option[2] = NULL; 1117 decoded->canonical_option[3] = NULL; 1118 decoded->value = 1; 1119 decoded->errors = 0; 1120 } 1121 1122 /* Helper function for listing valid choices and hint for misspelled 1123 value. CANDIDATES is a vector containing all valid strings, 1124 STR is set to a heap allocated string that contains all those 1125 strings concatenated, separated by spaces, and the return value 1126 is the closest string from those to ARG, or NULL if nothing is 1127 close enough. Callers should XDELETEVEC (STR) after using it 1128 to avoid memory leaks. */ 1129 1130 const char * 1131 candidates_list_and_hint (const char *arg, char *&str, 1132 const auto_vec <const char *> &candidates) 1133 { 1134 size_t len = 0; 1135 int i; 1136 const char *candidate; 1137 char *p; 1138 1139 FOR_EACH_VEC_ELT (candidates, i, candidate) 1140 len += strlen (candidate) + 1; 1141 1142 str = p = XNEWVEC (char, len); 1143 FOR_EACH_VEC_ELT (candidates, i, candidate) 1144 { 1145 len = strlen (candidate); 1146 memcpy (p, candidate, len); 1147 p[len] = ' '; 1148 p += len + 1; 1149 } 1150 p[-1] = '\0'; 1151 return find_closest_string (arg, &candidates); 1152 } 1153 1154 /* Perform diagnostics for read_cmdline_option and control_warning_option 1155 functions. Returns true if an error has been diagnosed. 1156 LOC and LANG_MASK arguments like in read_cmdline_option. 1157 OPTION is the option to report diagnostics for, OPT the name 1158 of the option as text, ARG the argument of the option (for joined 1159 options), ERRORS is bitmask of CL_ERR_* values. */ 1160 1161 static bool 1162 cmdline_handle_error (location_t loc, const struct cl_option *option, 1163 const char *opt, const char *arg, int errors, 1164 unsigned int lang_mask) 1165 { 1166 if (errors & CL_ERR_DISABLED) 1167 { 1168 error_at (loc, "command line option %qs" 1169 " is not supported by this configuration", opt); 1170 return true; 1171 } 1172 1173 if (errors & CL_ERR_MISSING_ARG) 1174 { 1175 if (option->missing_argument_error) 1176 error_at (loc, option->missing_argument_error, opt); 1177 else 1178 error_at (loc, "missing argument to %qs", opt); 1179 return true; 1180 } 1181 1182 if (errors & CL_ERR_UINT_ARG) 1183 { 1184 error_at (loc, "argument to %qs should be a non-negative integer", 1185 option->opt_text); 1186 return true; 1187 } 1188 1189 if (errors & CL_ERR_INT_RANGE_ARG) 1190 { 1191 error_at (loc, "argument to %qs is not between %d and %d", 1192 option->opt_text, option->range_min, option->range_max); 1193 return true; 1194 } 1195 1196 if (errors & CL_ERR_ENUM_ARG) 1197 { 1198 const struct cl_enum *e = &cl_enums[option->var_enum]; 1199 unsigned int i; 1200 char *s; 1201 1202 if (e->unknown_error) 1203 error_at (loc, e->unknown_error, arg); 1204 else 1205 error_at (loc, "unrecognized argument in option %qs", opt); 1206 1207 auto_vec <const char *> candidates; 1208 for (i = 0; e->values[i].arg != NULL; i++) 1209 { 1210 if (!enum_arg_ok_for_language (&e->values[i], lang_mask)) 1211 continue; 1212 candidates.safe_push (e->values[i].arg); 1213 } 1214 const char *hint = candidates_list_and_hint (arg, s, candidates); 1215 if (hint) 1216 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?", 1217 option->opt_text, s, hint); 1218 else 1219 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s); 1220 XDELETEVEC (s); 1221 1222 return true; 1223 } 1224 1225 return false; 1226 } 1227 1228 /* Handle the switch DECODED (location LOC) for the language indicated 1229 by LANG_MASK, using the handlers in *HANDLERS and setting fields in 1230 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for 1231 diagnostic options. */ 1232 1233 void 1234 read_cmdline_option (struct gcc_options *opts, 1235 struct gcc_options *opts_set, 1236 struct cl_decoded_option *decoded, 1237 location_t loc, 1238 unsigned int lang_mask, 1239 const struct cl_option_handlers *handlers, 1240 diagnostic_context *dc) 1241 { 1242 const struct cl_option *option; 1243 const char *opt = decoded->orig_option_with_args_text; 1244 1245 if (decoded->warn_message) 1246 warning_at (loc, 0, decoded->warn_message, opt); 1247 1248 if (decoded->opt_index == OPT_SPECIAL_unknown) 1249 { 1250 if (handlers->unknown_option_callback (decoded)) 1251 error_at (loc, "unrecognized command line option %qs", decoded->arg); 1252 return; 1253 } 1254 1255 if (decoded->opt_index == OPT_SPECIAL_ignore) 1256 return; 1257 1258 option = &cl_options[decoded->opt_index]; 1259 1260 if (decoded->errors 1261 && cmdline_handle_error (loc, option, opt, decoded->arg, 1262 decoded->errors, lang_mask)) 1263 return; 1264 1265 if (decoded->errors & CL_ERR_WRONG_LANG) 1266 { 1267 handlers->wrong_lang_callback (decoded, lang_mask); 1268 return; 1269 } 1270 1271 gcc_assert (!decoded->errors); 1272 1273 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED, 1274 loc, handlers, false, dc)) 1275 error_at (loc, "unrecognized command line option %qs", opt); 1276 } 1277 1278 /* Set any field in OPTS, and OPTS_SET if not NULL, for option 1279 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND, 1280 location LOC, using diagnostic context DC if not NULL for 1281 diagnostic classification. */ 1282 1283 void 1284 set_option (struct gcc_options *opts, struct gcc_options *opts_set, 1285 int opt_index, int value, const char *arg, int kind, 1286 location_t loc, diagnostic_context *dc) 1287 { 1288 const struct cl_option *option = &cl_options[opt_index]; 1289 void *flag_var = option_flag_var (opt_index, opts); 1290 void *set_flag_var = NULL; 1291 1292 if (!flag_var) 1293 return; 1294 1295 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL) 1296 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc); 1297 1298 if (opts_set != NULL) 1299 set_flag_var = option_flag_var (opt_index, opts_set); 1300 1301 switch (option->var_type) 1302 { 1303 case CLVC_BOOLEAN: 1304 *(int *) flag_var = value; 1305 if (set_flag_var) 1306 *(int *) set_flag_var = 1; 1307 break; 1308 1309 case CLVC_EQUAL: 1310 if (option->cl_host_wide_int) 1311 *(HOST_WIDE_INT *) flag_var = (value 1312 ? option->var_value 1313 : !option->var_value); 1314 else 1315 *(int *) flag_var = (value 1316 ? option->var_value 1317 : !option->var_value); 1318 if (set_flag_var) 1319 *(int *) set_flag_var = 1; 1320 break; 1321 1322 case CLVC_BIT_CLEAR: 1323 case CLVC_BIT_SET: 1324 if ((value != 0) == (option->var_type == CLVC_BIT_SET)) 1325 { 1326 if (option->cl_host_wide_int) 1327 *(HOST_WIDE_INT *) flag_var |= option->var_value; 1328 else 1329 *(int *) flag_var |= option->var_value; 1330 } 1331 else 1332 { 1333 if (option->cl_host_wide_int) 1334 *(HOST_WIDE_INT *) flag_var &= ~option->var_value; 1335 else 1336 *(int *) flag_var &= ~option->var_value; 1337 } 1338 if (set_flag_var) 1339 { 1340 if (option->cl_host_wide_int) 1341 *(HOST_WIDE_INT *) set_flag_var |= option->var_value; 1342 else 1343 *(int *) set_flag_var |= option->var_value; 1344 } 1345 break; 1346 1347 case CLVC_STRING: 1348 *(const char **) flag_var = arg; 1349 if (set_flag_var) 1350 *(const char **) set_flag_var = ""; 1351 break; 1352 1353 case CLVC_ENUM: 1354 { 1355 const struct cl_enum *e = &cl_enums[option->var_enum]; 1356 1357 e->set (flag_var, value); 1358 if (set_flag_var) 1359 e->set (set_flag_var, 1); 1360 } 1361 break; 1362 1363 case CLVC_DEFER: 1364 { 1365 vec<cl_deferred_option> *v 1366 = (vec<cl_deferred_option> *) *(void **) flag_var; 1367 cl_deferred_option p = {opt_index, arg, value}; 1368 if (!v) 1369 v = XCNEW (vec<cl_deferred_option>); 1370 v->safe_push (p); 1371 *(void **) flag_var = v; 1372 if (set_flag_var) 1373 *(void **) set_flag_var = v; 1374 } 1375 break; 1376 } 1377 } 1378 1379 /* Return the address of the flag variable for option OPT_INDEX in 1380 options structure OPTS, or NULL if there is no flag variable. */ 1381 1382 void * 1383 option_flag_var (int opt_index, struct gcc_options *opts) 1384 { 1385 const struct cl_option *option = &cl_options[opt_index]; 1386 1387 if (option->flag_var_offset == (unsigned short) -1) 1388 return NULL; 1389 return (void *)(((char *) opts) + option->flag_var_offset); 1390 } 1391 1392 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled, 1393 or -1 if it isn't a simple on-off switch. */ 1394 1395 int 1396 option_enabled (int opt_idx, void *opts) 1397 { 1398 const struct cl_option *option = &(cl_options[opt_idx]); 1399 struct gcc_options *optsg = (struct gcc_options *) opts; 1400 void *flag_var = option_flag_var (opt_idx, optsg); 1401 1402 if (flag_var) 1403 switch (option->var_type) 1404 { 1405 case CLVC_BOOLEAN: 1406 return *(int *) flag_var != 0; 1407 1408 case CLVC_EQUAL: 1409 if (option->cl_host_wide_int) 1410 return *(HOST_WIDE_INT *) flag_var == option->var_value; 1411 else 1412 return *(int *) flag_var == option->var_value; 1413 1414 case CLVC_BIT_CLEAR: 1415 if (option->cl_host_wide_int) 1416 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0; 1417 else 1418 return (*(int *) flag_var & option->var_value) == 0; 1419 1420 case CLVC_BIT_SET: 1421 if (option->cl_host_wide_int) 1422 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0; 1423 else 1424 return (*(int *) flag_var & option->var_value) != 0; 1425 1426 case CLVC_STRING: 1427 case CLVC_ENUM: 1428 case CLVC_DEFER: 1429 break; 1430 } 1431 return -1; 1432 } 1433 1434 /* Fill STATE with the current state of option OPTION in OPTS. Return 1435 true if there is some state to store. */ 1436 1437 bool 1438 get_option_state (struct gcc_options *opts, int option, 1439 struct cl_option_state *state) 1440 { 1441 void *flag_var = option_flag_var (option, opts); 1442 1443 if (flag_var == 0) 1444 return false; 1445 1446 switch (cl_options[option].var_type) 1447 { 1448 case CLVC_BOOLEAN: 1449 case CLVC_EQUAL: 1450 state->data = flag_var; 1451 state->size = (cl_options[option].cl_host_wide_int 1452 ? sizeof (HOST_WIDE_INT) 1453 : sizeof (int)); 1454 break; 1455 1456 case CLVC_BIT_CLEAR: 1457 case CLVC_BIT_SET: 1458 state->ch = option_enabled (option, opts); 1459 state->data = &state->ch; 1460 state->size = 1; 1461 break; 1462 1463 case CLVC_STRING: 1464 state->data = *(const char **) flag_var; 1465 if (state->data == 0) 1466 state->data = ""; 1467 state->size = strlen ((const char *) state->data) + 1; 1468 break; 1469 1470 case CLVC_ENUM: 1471 state->data = flag_var; 1472 state->size = cl_enums[cl_options[option].var_enum].var_size; 1473 break; 1474 1475 case CLVC_DEFER: 1476 return false; 1477 } 1478 return true; 1479 } 1480 1481 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option 1482 handlers HANDLERS) to have diagnostic kind KIND for option 1483 structures OPTS and OPTS_SET and diagnostic context DC (possibly 1484 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the 1485 argument of the option for joined options, or NULL otherwise. If IMPLY, 1486 the warning option in question is implied at this point. This is 1487 used by -Werror= and #pragma GCC diagnostic. */ 1488 1489 void 1490 control_warning_option (unsigned int opt_index, int kind, const char *arg, 1491 bool imply, location_t loc, unsigned int lang_mask, 1492 const struct cl_option_handlers *handlers, 1493 struct gcc_options *opts, 1494 struct gcc_options *opts_set, 1495 diagnostic_context *dc) 1496 { 1497 if (cl_options[opt_index].alias_target != N_OPTS) 1498 { 1499 gcc_assert (!cl_options[opt_index].cl_separate_alias 1500 && !cl_options[opt_index].cl_negative_alias); 1501 if (cl_options[opt_index].alias_arg) 1502 arg = cl_options[opt_index].alias_arg; 1503 opt_index = cl_options[opt_index].alias_target; 1504 } 1505 if (opt_index == OPT_SPECIAL_ignore) 1506 return; 1507 if (dc) 1508 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc); 1509 if (imply) 1510 { 1511 const struct cl_option *option = &cl_options[opt_index]; 1512 1513 /* -Werror=foo implies -Wfoo. */ 1514 if (option->var_type == CLVC_BOOLEAN || option->var_type == CLVC_ENUM) 1515 { 1516 int value = 1; 1517 1518 if (arg && *arg == '\0' && !option->cl_missing_ok) 1519 arg = NULL; 1520 1521 if ((option->flags & CL_JOINED) && arg == NULL) 1522 { 1523 cmdline_handle_error (loc, option, option->opt_text, arg, 1524 CL_ERR_MISSING_ARG, lang_mask); 1525 return; 1526 } 1527 1528 /* If the switch takes an integer, convert it. */ 1529 if (arg && option->cl_uinteger) 1530 { 1531 value = integral_argument (arg); 1532 if (value == -1) 1533 { 1534 cmdline_handle_error (loc, option, option->opt_text, arg, 1535 CL_ERR_UINT_ARG, lang_mask); 1536 return; 1537 } 1538 } 1539 1540 /* If the switch takes an enumerated argument, convert it. */ 1541 if (arg && option->var_type == CLVC_ENUM) 1542 { 1543 const struct cl_enum *e = &cl_enums[option->var_enum]; 1544 1545 if (enum_arg_to_value (e->values, arg, &value, lang_mask)) 1546 { 1547 const char *carg = NULL; 1548 1549 if (enum_value_to_arg (e->values, &carg, value, lang_mask)) 1550 arg = carg; 1551 gcc_assert (carg != NULL); 1552 } 1553 else 1554 { 1555 cmdline_handle_error (loc, option, option->opt_text, arg, 1556 CL_ERR_ENUM_ARG, lang_mask); 1557 return; 1558 } 1559 } 1560 1561 handle_generated_option (opts, opts_set, 1562 opt_index, arg, value, lang_mask, 1563 kind, loc, handlers, false, dc); 1564 } 1565 } 1566 } 1567