]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/opts-common.c
Check 256bit AVX register in move expanders.
[thirdparty/gcc.git] / gcc / opts-common.c
CommitLineData
14c7833c 1/* Command line option handling.
5d4b393f 2 Copyright (C) 2006, 2007, 2008, 2010 Free Software Foundation, Inc.
14c7833c
L
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
14c7833c
L
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
14c7833c
L
19
20#include "config.h"
21#include "system.h"
22#include "intl.h"
23#include "coretypes.h"
24#include "opts.h"
a75bfaa6 25#include "flags.h"
5f20c657 26#include "diagnostic.h"
6d721f67 27#include "tm.h" /* For WORD_SWITCH_TAKES_ARG and
e200444e 28 TARGET_OPTION_TRANSLATE_TABLE. */
14c7833c 29
60cf253a
JM
30static void prune_options (struct cl_decoded_option **, unsigned int *);
31
14c7833c 32/* Perform a binary search to find which option the command-line INPUT
6e2f1956
JM
33 matches. Returns its index in the option array, and
34 OPT_SPECIAL_unknown on failure.
14c7833c
L
35
36 This routine is quite subtle. A normal binary search is not good
37 enough because some options can be suffixed with an argument, and
38 multiple sub-matches can occur, e.g. input of "-pedantic" matching
39 the initial substring of "-pedantic-errors".
40
41 A more complicated example is -gstabs. It should match "-g" with
42 an argument of "stabs". Suppose, however, that the number and list
43 of switches are such that the binary search tests "-gen-decls"
44 before having tested "-g". This doesn't match, and as "-gen-decls"
45 is less than "-gstabs", it will become the lower bound of the
46 binary search range, and "-g" will never be seen. To resolve this
af47e6ac 47 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
14c7833c
L
48 to "-g" so that failed searches that end between "-gen-decls" and
49 the lexicographically subsequent switch know to go back and see if
50 "-g" causes a match (which it does in this example).
51
52 This search is done in such a way that the longest match for the
53 front end in question wins. If there is no match for the current
54 front end, the longest match for a different front end is returned
55 (or N_OPTS if none) and the caller emits an error message. */
56size_t
57find_opt (const char *input, int lang_mask)
58{
e200444e 59 size_t mn, mn_orig, mx, md, opt_len;
14c7833c
L
60 size_t match_wrong_lang;
61 int comp;
62
63 mn = 0;
64 mx = cl_options_count;
65
66 /* Find mn such this lexicographical inequality holds:
67 cl_options[mn] <= input < cl_options[mn + 1]. */
68 while (mx - mn > 1)
69 {
70 md = (mn + mx) / 2;
71 opt_len = cl_options[md].opt_len;
72 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
73
74 if (comp < 0)
75 mx = md;
76 else
77 mn = md;
78 }
79
e200444e
JM
80 mn_orig = mn;
81
14c7833c 82 /* This is the switch that is the best match but for a different
6e2f1956
JM
83 front end, or OPT_SPECIAL_unknown if there is no match at all. */
84 match_wrong_lang = OPT_SPECIAL_unknown;
14c7833c
L
85
86 /* Backtrace the chain of possible matches, returning the longest
87 one, if any, that fits best. With current GCC switches, this
88 loop executes at most twice. */
89 do
90 {
91 const struct cl_option *opt = &cl_options[mn];
92
93 /* Is the input either an exact match or a prefix that takes a
94 joined argument? */
95 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
96 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
97 {
98 /* If language is OK, return it. */
99 if (opt->flags & lang_mask)
100 return mn;
101
102 /* If we haven't remembered a prior match, remember this
103 one. Any prior match is necessarily better. */
6e2f1956 104 if (match_wrong_lang == OPT_SPECIAL_unknown)
14c7833c
L
105 match_wrong_lang = mn;
106 }
107
108 /* Try the next possibility. This is cl_options_count if there
109 are no more. */
110 mn = opt->back_chain;
111 }
112 while (mn != cl_options_count);
113
e200444e
JM
114 if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
115 {
116 /* Long options, starting "--", may be abbreviated if the
117 abbreviation is unambiguous. This only applies to options
118 not taking a joined argument, and abbreviations of "--option"
119 are permitted even if there is a variant "--option=". */
120 size_t mnc = mn_orig + 1;
121 size_t cmp_len = strlen (input);
122 while (mnc < cl_options_count
123 && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0)
124 {
125 /* Option matching this abbreviation. OK if it is the first
126 match and that does not take a joined argument, or the
127 second match, taking a joined argument and with only '='
128 added to the first match; otherwise considered
129 ambiguous. */
130 if (mnc == mn_orig + 1
131 && !(cl_options[mnc].flags & CL_JOINED))
132 match_wrong_lang = mnc;
133 else if (mnc == mn_orig + 2
134 && match_wrong_lang == mn_orig + 1
135 && (cl_options[mnc].flags & CL_JOINED)
136 && (cl_options[mnc].opt_len
137 == cl_options[mn_orig + 1].opt_len + 1)
138 && strncmp (cl_options[mnc].opt_text + 1,
139 cl_options[mn_orig + 1].opt_text + 1,
140 cl_options[mn_orig + 1].opt_len) == 0)
141 ; /* OK, as long as there are no more matches. */
142 else
143 return OPT_SPECIAL_unknown;
144 mnc++;
145 }
146 }
147
6e2f1956 148 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
14c7833c
L
149 return match_wrong_lang;
150}
151
5d4b393f
JM
152/* If ARG is a non-negative integer made up solely of digits, return its
153 value, otherwise return -1. */
154
155int
156integral_argument (const char *arg)
157{
158 const char *p = arg;
159
160 while (*p && ISDIGIT (*p))
161 p++;
162
163 if (*p == '\0')
164 return atoi (arg);
165
166 return -1;
167}
168
d9d16a19
JM
169/* Return whether OPTION is OK for the language given by
170 LANG_MASK. */
171static bool
172option_ok_for_language (const struct cl_option *option,
173 unsigned int lang_mask)
174{
175 if (!(option->flags & lang_mask))
176 return false;
177 else if ((option->flags & CL_TARGET)
178 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
179 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
180 /* Complain for target flag language mismatches if any languages
181 are specified. */
182 return false;
183 return true;
184}
185
5de8299c
JM
186
187/* Fill in the canonical option part of *DECODED with an option
188 described by OPT_INDEX, ARG and VALUE. */
189
190static void
191generate_canonical_option (size_t opt_index, const char *arg, int value,
192 struct cl_decoded_option *decoded)
193{
194 const struct cl_option *option = &cl_options[opt_index];
195 const char *opt_text = option->opt_text;
196
197 if (value == 0
198 && !(option->flags & CL_REJECT_NEGATIVE)
199 && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
200 {
201 char *t = XNEWVEC (char, option->opt_len + 5);
202 t[0] = '-';
203 t[1] = opt_text[1];
204 t[2] = 'n';
205 t[3] = 'o';
206 t[4] = '-';
207 memcpy (t + 5, opt_text + 2, option->opt_len);
208 opt_text = t;
209 }
210
211 decoded->canonical_option[2] = NULL;
212 decoded->canonical_option[3] = NULL;
213
214 if (arg)
215 {
d1583032
JM
216 if ((option->flags & CL_SEPARATE)
217 && !(option->flags & CL_SEPARATE_ALIAS))
5de8299c
JM
218 {
219 decoded->canonical_option[0] = opt_text;
220 decoded->canonical_option[1] = arg;
221 decoded->canonical_option_num_elements = 2;
222 }
223 else
224 {
225 gcc_assert (option->flags & CL_JOINED);
226 decoded->canonical_option[0] = concat (opt_text, arg, NULL);
227 decoded->canonical_option[1] = NULL;
228 decoded->canonical_option_num_elements = 1;
229 }
230 }
231 else
232 {
233 decoded->canonical_option[0] = opt_text;
234 decoded->canonical_option[1] = NULL;
235 decoded->canonical_option_num_elements = 1;
236 }
237}
238
e200444e
JM
239/* Structure describing mappings from options on the command line to
240 options to look up with find_opt. */
241struct option_map
242{
243 /* Prefix of the option on the command line. */
244 const char *opt0;
245 /* If two argv elements are considered to be merged into one option,
246 prefix for the second element, otherwise NULL. */
247 const char *opt1;
248 /* The new prefix to map to. */
249 const char *new_prefix;
250 /* Whether at least one character is needed following opt1 or opt0
251 for this mapping to be used. (--optimize= is valid for -O, but
252 --warn- is not valid for -W.) */
253 bool another_char_needed;
254 /* Whether the original option is a negated form of the option
255 resulting from this map. */
256 bool negated;
257};
258static const struct option_map option_map[] =
259 {
260 { "-Wno-", NULL, "-W", false, true },
261 { "-fno-", NULL, "-f", false, true },
262 { "-mno-", NULL, "-m", false, true },
263 { "--debug=", NULL, "-g", false, false },
264 { "--machine-", NULL, "-m", true, false },
265 { "--machine-no-", NULL, "-m", false, true },
266 { "--machine=", NULL, "-m", false, false },
267 { "--machine=no-", NULL, "-m", false, true },
268 { "--machine", "", "-m", false, false },
269 { "--machine", "no-", "-m", false, true },
270 { "--optimize=", NULL, "-O", false, false },
271 { "--std=", NULL, "-std=", false, false },
272 { "--std", "", "-std=", false, false },
273 { "--warn-", NULL, "-W", true, false },
274 { "--warn-no-", NULL, "-W", false, true },
275 { "--", NULL, "-f", true, false },
276 { "--no-", NULL, "-f", false, true }
277 };
278
5d4b393f 279/* Decode the switch beginning at ARGV for the language indicated by
603349bf
JM
280 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
281 the structure *DECODED. Returns the number of switches
282 consumed. */
5d4b393f 283
6e2f1956 284static unsigned int
5d4b393f
JM
285decode_cmdline_option (const char **argv, unsigned int lang_mask,
286 struct cl_decoded_option *decoded)
287{
288 size_t opt_index;
e200444e 289 const char *arg = 0;
5d4b393f 290 int value = 1;
e200444e
JM
291 unsigned int result = 1, i, extra_args;
292 int adjust_len = 0;
eea13ead
JM
293 size_t total_len;
294 char *p;
5d4b393f
JM
295 const struct cl_option *option;
296 int errors = 0;
2d2bd949 297 const char *warn_message = NULL;
c878765b
JM
298 bool separate_arg_flag;
299 bool joined_arg_flag;
d1583032 300 bool have_separate_arg = false;
5d4b393f 301
e200444e 302 extra_args = 0;
5d4b393f 303
e200444e
JM
304 opt_index = find_opt (argv[0] + 1, lang_mask);
305 i = 0;
306 while (opt_index == OPT_SPECIAL_unknown
307 && i < ARRAY_SIZE (option_map))
5d4b393f 308 {
e200444e
JM
309 const char *opt0 = option_map[i].opt0;
310 const char *opt1 = option_map[i].opt1;
311 const char *new_prefix = option_map[i].new_prefix;
312 bool another_char_needed = option_map[i].another_char_needed;
313 size_t opt0_len = strlen (opt0);
314 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
315 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
316 size_t new_prefix_len = strlen (new_prefix);
317
318 extra_args = (opt1 == NULL ? 0 : 1);
319 value = !option_map[i].negated;
320
321 if (strncmp (argv[0], opt0, opt0_len) == 0
322 && (opt1 == NULL
323 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
324 && (!another_char_needed
325 || argv[extra_args][optn_len] != 0))
326 {
327 size_t arglen = strlen (argv[extra_args]);
328 char *dup;
329
330 adjust_len = (int) optn_len - (int) new_prefix_len;
331 dup = XNEWVEC (char, arglen + 1 - adjust_len);
332 memcpy (dup, new_prefix, new_prefix_len);
333 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
334 arglen - optn_len + 1);
335 opt_index = find_opt (dup + 1, lang_mask);
336 free (dup);
337 }
338 i++;
5d4b393f
JM
339 }
340
6e2f1956
JM
341 if (opt_index == OPT_SPECIAL_unknown)
342 {
343 arg = argv[0];
e200444e
JM
344 extra_args = 0;
345 value = 1;
6e2f1956
JM
346 goto done;
347 }
5d4b393f
JM
348
349 option = &cl_options[opt_index];
350
351 /* Reject negative form of switches that don't take negatives as
352 unrecognized. */
353 if (!value && (option->flags & CL_REJECT_NEGATIVE))
354 {
6e2f1956 355 opt_index = OPT_SPECIAL_unknown;
184eb658 356 errors |= CL_ERR_NEGATIVE;
6e2f1956 357 arg = argv[0];
5d4b393f
JM
358 goto done;
359 }
360
e200444e 361 result = extra_args + 1;
2d2bd949
JM
362 warn_message = option->warn_message;
363
5d4b393f
JM
364 /* Check to see if the option is disabled for this configuration. */
365 if (option->flags & CL_DISABLED)
366 errors |= CL_ERR_DISABLED;
367
c878765b
JM
368 /* Determine whether there may be a separate argument based on
369 whether this option is being processed for the driver. */
370 separate_arg_flag = ((option->flags & CL_SEPARATE)
371 && !((option->flags & CL_NO_DRIVER_ARG)
372 && (lang_mask & CL_DRIVER)));
373 joined_arg_flag = (option->flags & CL_JOINED) != 0;
374
5d4b393f 375 /* Sort out any argument the switch takes. */
c878765b 376 if (joined_arg_flag)
5d4b393f
JM
377 {
378 /* Have arg point to the original switch. This is because
379 some code, such as disable_builtin_function, expects its
380 argument to be persistent until the program exits. */
e200444e 381 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
5d4b393f
JM
382
383 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
384 {
c878765b 385 if (separate_arg_flag)
5d4b393f 386 {
e200444e
JM
387 arg = argv[extra_args + 1];
388 result = extra_args + 2;
5d4b393f 389 if (arg == NULL)
e200444e 390 result = extra_args + 1;
d1583032
JM
391 else
392 have_separate_arg = true;
5d4b393f
JM
393 }
394 else
395 /* Missing argument. */
396 arg = NULL;
397 }
398 }
c878765b 399 else if (separate_arg_flag)
5d4b393f 400 {
e200444e
JM
401 arg = argv[extra_args + 1];
402 result = extra_args + 2;
5d4b393f 403 if (arg == NULL)
e200444e 404 result = extra_args + 1;
d1583032
JM
405 else
406 have_separate_arg = true;
5d4b393f
JM
407 }
408
5de8299c
JM
409 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
410 errors |= CL_ERR_MISSING_ARG;
411
2d2bd949
JM
412 /* Is this option an alias (or an ignored option, marked as an alias
413 of OPT_SPECIAL_ignore)? */
d1583032
JM
414 if (option->alias_target != N_OPTS
415 && (!(option->flags & CL_SEPARATE_ALIAS) || have_separate_arg))
5de8299c
JM
416 {
417 size_t new_opt_index = option->alias_target;
5de8299c 418
2d2bd949 419 if (new_opt_index == OPT_SPECIAL_ignore)
5de8299c 420 {
2d2bd949
JM
421 gcc_assert (option->alias_arg == NULL);
422 gcc_assert (option->neg_alias_arg == NULL);
423 opt_index = new_opt_index;
424 arg = NULL;
5de8299c
JM
425 value = 1;
426 }
2d2bd949 427 else
5de8299c 428 {
2d2bd949 429 const struct cl_option *new_option = &cl_options[new_opt_index];
5de8299c 430
2d2bd949 431 /* The new option must not be an alias itself. */
e200444e
JM
432 gcc_assert (new_option->alias_target == N_OPTS
433 || (new_option->flags & CL_SEPARATE_ALIAS));
5de8299c 434
2d2bd949
JM
435 if (option->neg_alias_arg)
436 {
437 gcc_assert (option->alias_arg != NULL);
438 gcc_assert (arg == NULL);
439 if (value)
440 arg = option->alias_arg;
441 else
442 arg = option->neg_alias_arg;
443 value = 1;
444 }
445 else if (option->alias_arg)
446 {
447 gcc_assert (value == 1);
448 gcc_assert (arg == NULL);
449 arg = option->alias_arg;
450 }
5de8299c 451
2d2bd949
JM
452 opt_index = new_opt_index;
453 option = new_option;
5de8299c 454
2d2bd949
JM
455 if (value == 0)
456 gcc_assert (!(option->flags & CL_REJECT_NEGATIVE));
457
458 /* Recompute what arguments are allowed. */
459 separate_arg_flag = ((option->flags & CL_SEPARATE)
460 && !((option->flags & CL_NO_DRIVER_ARG)
461 && (lang_mask & CL_DRIVER)));
462 joined_arg_flag = (option->flags & CL_JOINED) != 0;
463
464 if (!(errors & CL_ERR_MISSING_ARG))
465 {
466 if (separate_arg_flag || joined_arg_flag)
e200444e
JM
467 {
468 if ((option->flags & CL_MISSING_OK) && arg == NULL)
469 arg = "";
470 gcc_assert (arg != NULL);
471 }
2d2bd949
JM
472 else
473 gcc_assert (arg == NULL);
474 }
5de8299c 475
2d2bd949
JM
476 /* Recheck for warnings and disabled options. */
477 if (option->warn_message)
478 {
479 gcc_assert (warn_message == NULL);
480 warn_message = option->warn_message;
481 }
482 if (option->flags & CL_DISABLED)
483 errors |= CL_ERR_DISABLED;
484 }
5de8299c
JM
485 }
486
5d4b393f 487 /* Check if this is a switch for a different front end. */
d9d16a19 488 if (!option_ok_for_language (option, lang_mask))
5d4b393f 489 errors |= CL_ERR_WRONG_LANG;
5d4b393f 490
5d4b393f
JM
491 /* If the switch takes an integer, convert it. */
492 if (arg && (option->flags & CL_UINTEGER))
493 {
494 value = integral_argument (arg);
495 if (value == -1)
496 errors |= CL_ERR_UINT_ARG;
497 }
498
499 done:
5d4b393f
JM
500 decoded->opt_index = opt_index;
501 decoded->arg = arg;
502 decoded->value = value;
503 decoded->errors = errors;
2d2bd949 504 decoded->warn_message = warn_message;
eea13ead
JM
505
506 if (opt_index == OPT_SPECIAL_unknown)
6e2f1956 507 {
eea13ead
JM
508 /* Skip the correct number of arguments for options handled
509 through specs. */
6d721f67 510 const char *popt ATTRIBUTE_UNUSED = argv[0] + 1;
eea13ead
JM
511
512 gcc_assert (result == 1);
6d721f67 513 if (WORD_SWITCH_TAKES_ARG (popt))
eea13ead
JM
514 result += WORD_SWITCH_TAKES_ARG (popt);
515 if (result > 1)
516 for (i = 1; i < result; i++)
517 if (argv[i] == NULL)
518 {
519 result = i;
520 break;
521 }
6e2f1956 522 }
eea13ead
JM
523
524 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
525 decoded->canonical_option_num_elements = result;
526 total_len = 0;
527 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
528 {
529 if (i < result)
530 {
5de8299c
JM
531 if (opt_index == OPT_SPECIAL_unknown)
532 decoded->canonical_option[i] = argv[i];
533 else
534 decoded->canonical_option[i] = NULL;
eea13ead
JM
535 total_len += strlen (argv[i]) + 1;
536 }
537 else
538 decoded->canonical_option[i] = NULL;
539 }
2d2bd949 540 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
5de8299c 541 generate_canonical_option (opt_index, arg, value, decoded);
eea13ead
JM
542 decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
543 for (i = 0; i < result; i++)
544 {
545 size_t len = strlen (argv[i]);
546
547 memcpy (p, argv[i], len);
548 p += len;
549 if (i == result - 1)
550 *p++ = 0;
551 else
552 *p++ = ' ';
553 }
554
5d4b393f
JM
555 return result;
556}
557
e200444e
JM
558#ifdef TARGET_OPTION_TRANSLATE_TABLE
559static const struct {
560 const char *const option_found;
561 const char *const replacements;
562} target_option_translations[] =
563{
564 TARGET_OPTION_TRANSLATE_TABLE,
565 { 0, 0 }
566};
567#endif
568
6e2f1956
JM
569/* Decode command-line options (ARGC and ARGV being the arguments of
570 main) into an array, setting *DECODED_OPTIONS to a pointer to that
571 array and *DECODED_OPTIONS_COUNT to the number of entries in the
572 array. The first entry in the array is always one for the program
573 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
603349bf
JM
574 flags applicable for decoding (including CL_COMMON and CL_TARGET if
575 those options should be considered applicable). Do not produce any
576 diagnostics or set state outside of these variables. */
6e2f1956
JM
577
578void
579decode_cmdline_options_to_array (unsigned int argc, const char **argv,
580 unsigned int lang_mask,
581 struct cl_decoded_option **decoded_options,
582 unsigned int *decoded_options_count)
583{
e200444e 584 unsigned int n, i, target_translate_from;
6e2f1956
JM
585 struct cl_decoded_option *opt_array;
586 unsigned int num_decoded_options;
e200444e 587 bool argv_copied = false;
6e2f1956
JM
588
589 opt_array = XNEWVEC (struct cl_decoded_option, argc);
590
591 opt_array[0].opt_index = OPT_SPECIAL_program_name;
2d2bd949 592 opt_array[0].warn_message = NULL;
6e2f1956
JM
593 opt_array[0].arg = argv[0];
594 opt_array[0].orig_option_with_args_text = argv[0];
eea13ead 595 opt_array[0].canonical_option_num_elements = 1;
7a9bf9a4
JM
596 opt_array[0].canonical_option[0] = argv[0];
597 opt_array[0].canonical_option[1] = NULL;
eea13ead
JM
598 opt_array[0].canonical_option[2] = NULL;
599 opt_array[0].canonical_option[3] = NULL;
6e2f1956
JM
600 opt_array[0].value = 1;
601 opt_array[0].errors = 0;
602 num_decoded_options = 1;
603
e200444e 604 target_translate_from = 1;
6e2f1956
JM
605 for (i = 1; i < argc; i += n)
606 {
607 const char *opt = argv[i];
608
609 /* Interpret "-" or a non-switch as a file name. */
610 if (opt[0] != '-' || opt[1] == '\0')
611 {
d9d16a19 612 generate_option_input_file (opt, &opt_array[num_decoded_options]);
6e2f1956
JM
613 num_decoded_options++;
614 n = 1;
615 continue;
616 }
617
e200444e
JM
618 if (i >= target_translate_from && (lang_mask & CL_DRIVER))
619 {
620#ifdef TARGET_OPTION_TRANSLATE_TABLE
621 int tott_idx;
622
623 for (tott_idx = 0;
624 target_option_translations[tott_idx].option_found;
625 tott_idx++)
626 {
627 if (strcmp (target_option_translations[tott_idx].option_found,
628 argv[i]) == 0)
629 {
630 unsigned int spaces = 0;
631 unsigned int m = 0;
632 const char *sp;
633 char *np;
634
635 for (sp = target_option_translations[tott_idx].replacements;
636 *sp; sp++)
637 {
638 if (*sp == ' ')
639 {
640 spaces++;
641 while (*sp == ' ')
642 sp++;
643 sp--;
644 }
645 }
646
647 if (spaces)
648 {
649 int new_argc = argc + spaces;
650 if (argv_copied)
651 argv = XRESIZEVEC (const char *, argv, new_argc + 1);
652 else
653 {
654 const char **new_argv = XNEWVEC (const char *,
655 new_argc + 1);
656 memcpy (new_argv, argv,
657 (argc + 1) * sizeof (const char *));
658 argv = new_argv;
659 argv_copied = true;
660 }
661 memmove (&argv[i] + spaces, &argv[i],
662 (argc + 1 - i) * sizeof (const char *));
663 argc = new_argc;
664 opt_array = XRESIZEVEC (struct cl_decoded_option,
665 opt_array, argc);
666 }
667
668 sp = target_option_translations[tott_idx].replacements;
669 np = xstrdup (sp);
670
671 while (1)
672 {
673 while (*np == ' ')
674 np++;
675 if (*np == 0)
676 break;
677 argv[i + m++] = np;
678 while (*np != ' ' && *np)
679 np++;
680 if (*np == 0)
681 break;
682 *np++ = 0;
683 }
684
685 target_translate_from = i + m;
686 gcc_assert (m == spaces + 1);
687 break;
688 }
689 }
690#endif
691 }
692
6e2f1956
JM
693 n = decode_cmdline_option (argv + i, lang_mask,
694 &opt_array[num_decoded_options]);
695 num_decoded_options++;
696 }
697
e200444e
JM
698 if (argv_copied)
699 free (argv);
6e2f1956
JM
700 *decoded_options = opt_array;
701 *decoded_options_count = num_decoded_options;
60cf253a 702 prune_options (decoded_options, decoded_options_count);
6e2f1956
JM
703}
704
14c7833c
L
705/* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
706 next one is the same as ORIG_NEXT_OPT_IDX. */
707
708static bool
709cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
710{
711 /* An option can be canceled by the same option or an option with
712 Negative. */
713 if (cl_options [next_opt_idx].neg_index == opt_idx)
714 return true;
715
716 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
717 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
718 orig_next_opt_idx);
b8698a0f 719
14c7833c
L
720 return false;
721}
722
723/* Filter out options canceled by the ones after them. */
724
60cf253a
JM
725static void
726prune_options (struct cl_decoded_option **decoded_options,
727 unsigned int *decoded_options_count)
14c7833c 728{
60cf253a
JM
729 unsigned int old_decoded_options_count = *decoded_options_count;
730 struct cl_decoded_option *old_decoded_options = *decoded_options;
731 unsigned int new_decoded_options_count;
732 struct cl_decoded_option *new_decoded_options
733 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
734 unsigned int i;
14c7833c 735 const struct cl_option *option;
14c7833c
L
736
737 /* Remove arguments which are negated by others after them. */
60cf253a
JM
738 new_decoded_options_count = 0;
739 for (i = 0; i < old_decoded_options_count; i++)
14c7833c 740 {
60cf253a
JM
741 unsigned int j, opt_idx, next_opt_idx;
742
743 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
744 goto keep;
14c7833c 745
60cf253a
JM
746 opt_idx = old_decoded_options[i].opt_index;
747 switch (opt_idx)
14c7833c 748 {
60cf253a
JM
749 case OPT_SPECIAL_unknown:
750 case OPT_SPECIAL_ignore:
751 case OPT_SPECIAL_program_name:
752 case OPT_SPECIAL_input_file:
753 goto keep;
754
755 default:
756 gcc_assert (opt_idx < cl_options_count);
757 option = &cl_options[opt_idx];
758 if (option->neg_index < 0)
759 goto keep;
760
761 /* Skip joined switches. */
762 if ((option->flags & CL_JOINED))
763 goto keep;
764
765 for (j = i + 1; j < old_decoded_options_count; j++)
14c7833c 766 {
60cf253a
JM
767 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
768 continue;
769 next_opt_idx = old_decoded_options[j].opt_index;
770 if (next_opt_idx >= cl_options_count)
771 continue;
772 if (cl_options[next_opt_idx].neg_index < 0)
773 continue;
774 if ((cl_options[next_opt_idx].flags & CL_JOINED))
775 continue;
776 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
14c7833c
L
777 break;
778 }
60cf253a
JM
779 if (j == old_decoded_options_count)
780 {
14c7833c 781keep:
60cf253a
JM
782 new_decoded_options[new_decoded_options_count]
783 = old_decoded_options[i];
784 new_decoded_options_count++;
785 }
786 break;
14c7833c
L
787 }
788 }
789
60cf253a
JM
790 free (old_decoded_options);
791 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
792 new_decoded_options,
793 new_decoded_options_count);
794 *decoded_options = new_decoded_options;
795 *decoded_options_count = new_decoded_options_count;
14c7833c 796}
5f20c657 797
481e1176 798/* Handle option DECODED for the language indicated by LANG_MASK,
d4d24ba4
JM
799 using the handlers in HANDLERS and setting fields in OPTS and
800 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
801 option, DK_UNSPECIFIED otherwise. GENERATED_P is true for an
802 option generated as part of processing another option or otherwise
803 generated internally, false for one explicitly passed by the user.
1ebe4b4f
JM
804 Returns false if the switch was invalid. DC is the diagnostic
805 context for options affecting diagnostics state, or NULL. */
5f20c657
JM
806
807bool
46625112 808handle_option (struct gcc_options *opts,
d4d24ba4 809 struct gcc_options *opts_set,
46625112 810 const struct cl_decoded_option *decoded,
5f20c657 811 unsigned int lang_mask, int kind,
d4d24ba4 812 const struct cl_option_handlers *handlers,
1ebe4b4f 813 bool generated_p, diagnostic_context *dc)
5f20c657 814{
481e1176
JM
815 size_t opt_index = decoded->opt_index;
816 const char *arg = decoded->arg;
817 int value = decoded->value;
5f20c657 818 const struct cl_option *option = &cl_options[opt_index];
46625112 819 void *flag_var = option_flag_var (opt_index, opts);
5f20c657
JM
820 size_t i;
821
46625112 822 if (flag_var)
d4d24ba4 823 set_option (opts, (generated_p ? NULL : opts_set),
1ebe4b4f 824 opt_index, value, arg, kind, dc);
5f20c657
JM
825
826 for (i = 0; i < handlers->num_handlers; i++)
827 if (option->flags & handlers->handlers[i].mask)
828 {
d4d24ba4 829 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
5f20c657
JM
830 lang_mask, kind, handlers))
831 return false;
832 else
481e1176 833 handlers->post_handling_callback (decoded,
5f20c657
JM
834 handlers->handlers[i].mask);
835 }
836
837 return true;
838}
839
481e1176
JM
840/* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
841 option instead of DECODED. This is used for callbacks when one
842 option implies another instead of an option being decoded from the
843 command line. */
844
845bool
d4d24ba4
JM
846handle_generated_option (struct gcc_options *opts,
847 struct gcc_options *opts_set,
848 size_t opt_index, const char *arg, int value,
481e1176 849 unsigned int lang_mask, int kind,
1ebe4b4f
JM
850 const struct cl_option_handlers *handlers,
851 diagnostic_context *dc)
481e1176 852{
481e1176
JM
853 struct cl_decoded_option decoded;
854
d9d16a19 855 generate_option (opt_index, arg, value, lang_mask, &decoded);
d4d24ba4 856 return handle_option (opts, opts_set, &decoded, lang_mask, kind, handlers,
1ebe4b4f 857 true, dc);
d9d16a19
JM
858}
859
860/* Fill in *DECODED with an option described by OPT_INDEX, ARG and
861 VALUE for a front end using LANG_MASK. This is used when the
862 compiler generates options internally. */
863
864void
865generate_option (size_t opt_index, const char *arg, int value,
866 unsigned int lang_mask, struct cl_decoded_option *decoded)
867{
868 const struct cl_option *option = &cl_options[opt_index];
869
870 decoded->opt_index = opt_index;
2d2bd949 871 decoded->warn_message = NULL;
d9d16a19 872 decoded->arg = arg;
d9d16a19
JM
873 decoded->value = value;
874 decoded->errors = (option_ok_for_language (option, lang_mask)
875 ? 0
876 : CL_ERR_WRONG_LANG);
481e1176 877
5de8299c
JM
878 generate_canonical_option (opt_index, arg, value, decoded);
879 switch (decoded->canonical_option_num_elements)
481e1176 880 {
5de8299c
JM
881 case 1:
882 decoded->orig_option_with_args_text = decoded->canonical_option[0];
883 break;
884
885 case 2:
886 decoded->orig_option_with_args_text
887 = concat (decoded->canonical_option[0], " ",
888 decoded->canonical_option[1], NULL);
889 break;
890
891 default:
892 gcc_unreachable ();
481e1176 893 }
d9d16a19 894}
481e1176 895
d9d16a19
JM
896/* Fill in *DECODED with an option for input file FILE. */
897
898void
899generate_option_input_file (const char *file,
900 struct cl_decoded_option *decoded)
901{
902 decoded->opt_index = OPT_SPECIAL_input_file;
2d2bd949 903 decoded->warn_message = NULL;
d9d16a19
JM
904 decoded->arg = file;
905 decoded->orig_option_with_args_text = file;
906 decoded->canonical_option_num_elements = 1;
907 decoded->canonical_option[0] = file;
908 decoded->canonical_option[1] = NULL;
909 decoded->canonical_option[2] = NULL;
910 decoded->canonical_option[3] = NULL;
911 decoded->value = 1;
912 decoded->errors = 0;
481e1176
JM
913}
914
5f20c657 915/* Handle the switch DECODED for the language indicated by LANG_MASK,
d4d24ba4 916 using the handlers in *HANDLERS and setting fields in OPTS and
1ebe4b4f
JM
917 OPTS_SET and using diagnostic context DC (if not NULL) for
918 diagnostic options. */
5f20c657
JM
919
920void
46625112 921read_cmdline_option (struct gcc_options *opts,
d4d24ba4 922 struct gcc_options *opts_set,
46625112 923 struct cl_decoded_option *decoded,
5f20c657 924 unsigned int lang_mask,
1ebe4b4f
JM
925 const struct cl_option_handlers *handlers,
926 diagnostic_context *dc)
5f20c657
JM
927{
928 const struct cl_option *option;
2d2bd949
JM
929 const char *opt = decoded->orig_option_with_args_text;
930
931 if (decoded->warn_message)
932 warning (0, decoded->warn_message, opt);
5f20c657
JM
933
934 if (decoded->opt_index == OPT_SPECIAL_unknown)
935 {
481e1176
JM
936 if (handlers->unknown_option_callback (decoded))
937 error ("unrecognized command line option %qs", decoded->arg);
5f20c657
JM
938 return;
939 }
940
2d2bd949
JM
941 if (decoded->opt_index == OPT_SPECIAL_ignore)
942 return;
943
5f20c657 944 option = &cl_options[decoded->opt_index];
5f20c657
JM
945
946 if (decoded->errors & CL_ERR_DISABLED)
947 {
948 error ("command line option %qs"
949 " is not supported by this configuration", opt);
950 return;
951 }
952
953 if (decoded->errors & CL_ERR_WRONG_LANG)
954 {
481e1176 955 handlers->wrong_lang_callback (decoded, lang_mask);
5f20c657
JM
956 return;
957 }
958
959 if (decoded->errors & CL_ERR_MISSING_ARG)
960 {
961 if (option->missing_argument_error)
962 error (option->missing_argument_error, opt);
963 else
964 error ("missing argument to %qs", opt);
965 return;
966 }
967
968 if (decoded->errors & CL_ERR_UINT_ARG)
969 {
970 error ("argument to %qs should be a non-negative integer",
971 option->opt_text);
972 return;
973 }
974
975 gcc_assert (!decoded->errors);
976
d4d24ba4 977 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1ebe4b4f 978 handlers, false, dc))
5f20c657
JM
979 error ("unrecognized command line option %qs", opt);
980}
981
d4d24ba4 982/* Set any field in OPTS, and OPTS_SET if not NULL, for option
1ebe4b4f
JM
983 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND, using
984 diagnostic context DC if not NULL for diagnostic
985 classification. */
5f20c657
JM
986
987void
d4d24ba4 988set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1ebe4b4f
JM
989 int opt_index, int value, const char *arg, int kind,
990 diagnostic_context *dc)
5f20c657
JM
991{
992 const struct cl_option *option = &cl_options[opt_index];
46625112 993 void *flag_var = option_flag_var (opt_index, opts);
d4d24ba4 994 void *set_flag_var = NULL;
5f20c657 995
46625112 996 if (!flag_var)
5f20c657
JM
997 return;
998
d4d24ba4
JM
999 if (opts_set != NULL)
1000 set_flag_var = option_flag_var (opt_index, opts_set);
1001
5f20c657
JM
1002 switch (option->var_type)
1003 {
1004 case CLVC_BOOLEAN:
46625112 1005 *(int *) flag_var = value;
d4d24ba4
JM
1006 if (set_flag_var)
1007 *(int *) set_flag_var = 1;
5f20c657
JM
1008 break;
1009
1010 case CLVC_EQUAL:
46625112
JM
1011 *(int *) flag_var = (value
1012 ? option->var_value
1013 : !option->var_value);
d4d24ba4
JM
1014 if (set_flag_var)
1015 *(int *) set_flag_var = 1;
5f20c657
JM
1016 break;
1017
1018 case CLVC_BIT_CLEAR:
1019 case CLVC_BIT_SET:
1020 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
46625112 1021 *(int *) flag_var |= option->var_value;
5f20c657 1022 else
46625112 1023 *(int *) flag_var &= ~option->var_value;
d4d24ba4
JM
1024 if (set_flag_var)
1025 *(int *) set_flag_var |= option->var_value;
5f20c657
JM
1026 break;
1027
1028 case CLVC_STRING:
46625112 1029 *(const char **) flag_var = arg;
d4d24ba4
JM
1030 if (set_flag_var)
1031 *(const char **) set_flag_var = "";
5f20c657
JM
1032 break;
1033 }
1034
1ebe4b4f
JM
1035 if ((diagnostic_t) kind != DK_UNSPECIFIED
1036 && dc != NULL)
1037 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind,
5f20c657
JM
1038 UNKNOWN_LOCATION);
1039}
46625112
JM
1040
1041/* Return the address of the flag variable for option OPT_INDEX in
1042 options structure OPTS, or NULL if there is no flag variable. */
1043
1044void *
1045option_flag_var (int opt_index, struct gcc_options *opts)
1046{
1047 const struct cl_option *option = &cl_options[opt_index];
1048
1049 if (option->flag_var_offset == (unsigned short) -1)
1050 return NULL;
1051 return (void *)(((char *) opts) + option->flag_var_offset);
1052}