]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/opts-common.c
tree-cfg.c (verify_types_in_gimple_reference): Verify TARGET_MEM_REF a bit.
[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"
5d4b393f 25#include "options.h"
5f20c657 26#include "diagnostic.h"
eea13ead 27#include "tm.h" /* For SWITCH_TAKES_ARG and WORD_SWITCH_TAKES_ARG. */
14c7833c
L
28
29/* Perform a binary search to find which option the command-line INPUT
6e2f1956
JM
30 matches. Returns its index in the option array, and
31 OPT_SPECIAL_unknown on failure.
14c7833c
L
32
33 This routine is quite subtle. A normal binary search is not good
34 enough because some options can be suffixed with an argument, and
35 multiple sub-matches can occur, e.g. input of "-pedantic" matching
36 the initial substring of "-pedantic-errors".
37
38 A more complicated example is -gstabs. It should match "-g" with
39 an argument of "stabs". Suppose, however, that the number and list
40 of switches are such that the binary search tests "-gen-decls"
41 before having tested "-g". This doesn't match, and as "-gen-decls"
42 is less than "-gstabs", it will become the lower bound of the
43 binary search range, and "-g" will never be seen. To resolve this
af47e6ac 44 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
14c7833c
L
45 to "-g" so that failed searches that end between "-gen-decls" and
46 the lexicographically subsequent switch know to go back and see if
47 "-g" causes a match (which it does in this example).
48
49 This search is done in such a way that the longest match for the
50 front end in question wins. If there is no match for the current
51 front end, the longest match for a different front end is returned
52 (or N_OPTS if none) and the caller emits an error message. */
53size_t
54find_opt (const char *input, int lang_mask)
55{
56 size_t mn, mx, md, opt_len;
57 size_t match_wrong_lang;
58 int comp;
59
60 mn = 0;
61 mx = cl_options_count;
62
63 /* Find mn such this lexicographical inequality holds:
64 cl_options[mn] <= input < cl_options[mn + 1]. */
65 while (mx - mn > 1)
66 {
67 md = (mn + mx) / 2;
68 opt_len = cl_options[md].opt_len;
69 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
70
71 if (comp < 0)
72 mx = md;
73 else
74 mn = md;
75 }
76
77 /* This is the switch that is the best match but for a different
6e2f1956
JM
78 front end, or OPT_SPECIAL_unknown if there is no match at all. */
79 match_wrong_lang = OPT_SPECIAL_unknown;
14c7833c
L
80
81 /* Backtrace the chain of possible matches, returning the longest
82 one, if any, that fits best. With current GCC switches, this
83 loop executes at most twice. */
84 do
85 {
86 const struct cl_option *opt = &cl_options[mn];
87
88 /* Is the input either an exact match or a prefix that takes a
89 joined argument? */
90 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
91 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
92 {
93 /* If language is OK, return it. */
94 if (opt->flags & lang_mask)
95 return mn;
96
97 /* If we haven't remembered a prior match, remember this
98 one. Any prior match is necessarily better. */
6e2f1956 99 if (match_wrong_lang == OPT_SPECIAL_unknown)
14c7833c
L
100 match_wrong_lang = mn;
101 }
102
103 /* Try the next possibility. This is cl_options_count if there
104 are no more. */
105 mn = opt->back_chain;
106 }
107 while (mn != cl_options_count);
108
6e2f1956 109 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
14c7833c
L
110 return match_wrong_lang;
111}
112
5d4b393f
JM
113/* If ARG is a non-negative integer made up solely of digits, return its
114 value, otherwise return -1. */
115
116int
117integral_argument (const char *arg)
118{
119 const char *p = arg;
120
121 while (*p && ISDIGIT (*p))
122 p++;
123
124 if (*p == '\0')
125 return atoi (arg);
126
127 return -1;
128}
129
130/* Decode the switch beginning at ARGV for the language indicated by
131 LANG_MASK, into the structure *DECODED. Returns the number of
132 switches consumed. */
133
6e2f1956 134static unsigned int
5d4b393f
JM
135decode_cmdline_option (const char **argv, unsigned int lang_mask,
136 struct cl_decoded_option *decoded)
137{
138 size_t opt_index;
139 const char *opt, *arg = 0;
140 char *dup = 0;
141 int value = 1;
eea13ead
JM
142 unsigned int result = 1, i;
143 size_t total_len;
144 char *p;
5d4b393f
JM
145 const struct cl_option *option;
146 int errors = 0;
147
148 opt = argv[0];
149
150 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
6e2f1956 151 if (opt_index == OPT_SPECIAL_unknown
5d4b393f
JM
152 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
153 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
154 {
155 /* Drop the "no-" from negative switches. */
156 size_t len = strlen (opt) - 3;
157
158 dup = XNEWVEC (char, len + 1);
159 dup[0] = '-';
160 dup[1] = opt[1];
161 memcpy (dup + 2, opt + 5, len - 2 + 1);
162 opt = dup;
163 value = 0;
164 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
165 }
166
6e2f1956
JM
167 if (opt_index == OPT_SPECIAL_unknown)
168 {
169 arg = argv[0];
170 goto done;
171 }
5d4b393f
JM
172
173 option = &cl_options[opt_index];
174
175 /* Reject negative form of switches that don't take negatives as
176 unrecognized. */
177 if (!value && (option->flags & CL_REJECT_NEGATIVE))
178 {
6e2f1956
JM
179 opt_index = OPT_SPECIAL_unknown;
180 arg = argv[0];
5d4b393f
JM
181 goto done;
182 }
183
184 /* Check to see if the option is disabled for this configuration. */
185 if (option->flags & CL_DISABLED)
186 errors |= CL_ERR_DISABLED;
187
188 /* Sort out any argument the switch takes. */
189 if (option->flags & CL_JOINED)
190 {
191 /* Have arg point to the original switch. This is because
192 some code, such as disable_builtin_function, expects its
193 argument to be persistent until the program exits. */
194 arg = argv[0] + cl_options[opt_index].opt_len + 1;
195 if (!value)
196 arg += strlen ("no-");
197
198 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
199 {
200 if (option->flags & CL_SEPARATE)
201 {
202 arg = argv[1];
203 result = 2;
204 if (arg == NULL)
205 result = 1;
206 }
207 else
208 /* Missing argument. */
209 arg = NULL;
210 }
211 }
212 else if (option->flags & CL_SEPARATE)
213 {
214 arg = argv[1];
215 result = 2;
216 if (arg == NULL)
217 result = 1;
218 }
219
220 /* Check if this is a switch for a different front end. */
221 if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
222 errors |= CL_ERR_WRONG_LANG;
223 else if ((option->flags & CL_TARGET)
224 && (option->flags & CL_LANG_ALL)
225 && !(option->flags & lang_mask))
226 /* Complain for target flag language mismatches if any languages
227 are specified. */
228 errors |= CL_ERR_WRONG_LANG;
229
230 if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
231 errors |= CL_ERR_MISSING_ARG;
232
233 /* If the switch takes an integer, convert it. */
234 if (arg && (option->flags & CL_UINTEGER))
235 {
236 value = integral_argument (arg);
237 if (value == -1)
238 errors |= CL_ERR_UINT_ARG;
239 }
240
241 done:
242 if (dup)
243 free (dup);
244 decoded->opt_index = opt_index;
245 decoded->arg = arg;
246 decoded->value = value;
247 decoded->errors = errors;
eea13ead
JM
248
249 if (opt_index == OPT_SPECIAL_unknown)
6e2f1956 250 {
eea13ead
JM
251 /* Skip the correct number of arguments for options handled
252 through specs. */
253 const char *popt = argv[0] + 1;
254 int c = *popt;
255
256 gcc_assert (result == 1);
257 if (SWITCH_TAKES_ARG (c) > (popt[1] != 0))
258 result += SWITCH_TAKES_ARG (c) - (popt[1] != 0);
259 else if (WORD_SWITCH_TAKES_ARG (popt))
260 result += WORD_SWITCH_TAKES_ARG (popt);
261 if (result > 1)
262 for (i = 1; i < result; i++)
263 if (argv[i] == NULL)
264 {
265 result = i;
266 break;
267 }
6e2f1956 268 }
eea13ead
JM
269
270 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
271 decoded->canonical_option_num_elements = result;
272 total_len = 0;
273 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
274 {
275 if (i < result)
276 {
277 decoded->canonical_option[i] = argv[i];
278 total_len += strlen (argv[i]) + 1;
279 }
280 else
281 decoded->canonical_option[i] = NULL;
282 }
283 decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
284 for (i = 0; i < result; i++)
285 {
286 size_t len = strlen (argv[i]);
287
288 memcpy (p, argv[i], len);
289 p += len;
290 if (i == result - 1)
291 *p++ = 0;
292 else
293 *p++ = ' ';
294 }
295
5d4b393f
JM
296 return result;
297}
298
6e2f1956
JM
299/* Decode command-line options (ARGC and ARGV being the arguments of
300 main) into an array, setting *DECODED_OPTIONS to a pointer to that
301 array and *DECODED_OPTIONS_COUNT to the number of entries in the
302 array. The first entry in the array is always one for the program
303 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
304 applicable for decoding. Do not produce any diagnostics or set
305 state outside of these variables. */
306
307void
308decode_cmdline_options_to_array (unsigned int argc, const char **argv,
309 unsigned int lang_mask,
310 struct cl_decoded_option **decoded_options,
311 unsigned int *decoded_options_count)
312{
313 unsigned int n, i;
314 struct cl_decoded_option *opt_array;
315 unsigned int num_decoded_options;
316
317 opt_array = XNEWVEC (struct cl_decoded_option, argc);
318
319 opt_array[0].opt_index = OPT_SPECIAL_program_name;
320 opt_array[0].arg = argv[0];
321 opt_array[0].orig_option_with_args_text = argv[0];
eea13ead 322 opt_array[0].canonical_option_num_elements = 1;
7a9bf9a4
JM
323 opt_array[0].canonical_option[0] = argv[0];
324 opt_array[0].canonical_option[1] = NULL;
eea13ead
JM
325 opt_array[0].canonical_option[2] = NULL;
326 opt_array[0].canonical_option[3] = NULL;
6e2f1956
JM
327 opt_array[0].value = 1;
328 opt_array[0].errors = 0;
329 num_decoded_options = 1;
330
331 for (i = 1; i < argc; i += n)
332 {
333 const char *opt = argv[i];
334
335 /* Interpret "-" or a non-switch as a file name. */
336 if (opt[0] != '-' || opt[1] == '\0')
337 {
338 opt_array[num_decoded_options].opt_index = OPT_SPECIAL_input_file;
339 opt_array[num_decoded_options].arg = opt;
340 opt_array[num_decoded_options].orig_option_with_args_text = opt;
eea13ead 341 opt_array[num_decoded_options].canonical_option_num_elements = 1;
7a9bf9a4
JM
342 opt_array[num_decoded_options].canonical_option[0] = opt;
343 opt_array[num_decoded_options].canonical_option[1] = NULL;
eea13ead
JM
344 opt_array[num_decoded_options].canonical_option[2] = NULL;
345 opt_array[num_decoded_options].canonical_option[3] = NULL;
6e2f1956
JM
346 opt_array[num_decoded_options].value = 1;
347 opt_array[num_decoded_options].errors = 0;
348 num_decoded_options++;
349 n = 1;
350 continue;
351 }
352
353 n = decode_cmdline_option (argv + i, lang_mask,
354 &opt_array[num_decoded_options]);
355 num_decoded_options++;
356 }
357
358 opt_array = XRESIZEVEC (struct cl_decoded_option, opt_array,
359 num_decoded_options);
360 *decoded_options = opt_array;
361 *decoded_options_count = num_decoded_options;
362}
363
14c7833c
L
364/* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
365 next one is the same as ORIG_NEXT_OPT_IDX. */
366
367static bool
368cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
369{
370 /* An option can be canceled by the same option or an option with
371 Negative. */
372 if (cl_options [next_opt_idx].neg_index == opt_idx)
373 return true;
374
375 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
376 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
377 orig_next_opt_idx);
b8698a0f 378
14c7833c
L
379 return false;
380}
381
382/* Filter out options canceled by the ones after them. */
383
384void
385prune_options (int *argcp, char ***argvp)
386{
387 int argc = *argcp;
d3bfe4de 388 int *options = XNEWVEC (int, argc);
807303cf
DK
389 /* We will only return this replacement argv if we remove at least
390 one argument, so it does not need to be size (argc + 1) to
391 make room for the terminating NULL because we will always have
392 freed up at least one slot when we end up using it at all. */
d3bfe4de 393 char **argv = XNEWVEC (char *, argc);
14c7833c
L
394 int i, arg_count, need_prune = 0;
395 const struct cl_option *option;
396 size_t opt_index;
397
398 /* Scan all arguments. */
399 for (i = 1; i < argc; i++)
400 {
401 int value = 1;
402 const char *opt = (*argvp) [i];
403
404 opt_index = find_opt (opt + 1, -1);
6e2f1956 405 if (opt_index == OPT_SPECIAL_unknown
14c7833c
L
406 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
407 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
408 {
409 char *dup;
410
411 /* Drop the "no-" from negative switches. */
412 size_t len = strlen (opt) - 3;
413
414 dup = XNEWVEC (char, len + 1);
415 dup[0] = '-';
416 dup[1] = opt[1];
417 memcpy (dup + 2, opt + 5, len - 2 + 1);
418 opt = dup;
419 value = 0;
420 opt_index = find_opt (opt + 1, -1);
421 free (dup);
422 }
423
6e2f1956 424 if (opt_index == OPT_SPECIAL_unknown)
14c7833c
L
425 {
426cont:
427 options [i] = 0;
428 continue;
429 }
430
431 option = &cl_options[opt_index];
432 if (option->neg_index < 0)
433 goto cont;
434
faa88843
L
435 /* Skip joined switches. */
436 if ((option->flags & CL_JOINED))
437 goto cont;
438
14c7833c
L
439 /* Reject negative form of switches that don't take negatives as
440 unrecognized. */
441 if (!value && (option->flags & CL_REJECT_NEGATIVE))
442 goto cont;
443
444 options [i] = (int) opt_index;
445 need_prune |= options [i];
446 }
447
448 if (!need_prune)
449 goto done;
450
451 /* Remove arguments which are negated by others after them. */
452 argv [0] = (*argvp) [0];
453 arg_count = 1;
454 for (i = 1; i < argc; i++)
455 {
456 int j, opt_idx;
457
458 opt_idx = options [i];
459 if (opt_idx)
460 {
461 int next_opt_idx;
462 for (j = i + 1; j < argc; j++)
463 {
464 next_opt_idx = options [j];
465 if (next_opt_idx
466 && cancel_option (opt_idx, next_opt_idx,
467 next_opt_idx))
468 break;
469 }
470 }
471 else
472 goto keep;
473
474 if (j == argc)
475 {
476keep:
477 argv [arg_count] = (*argvp) [i];
478 arg_count++;
479 }
480 }
481
482 if (arg_count != argc)
483 {
484 *argcp = arg_count;
485 *argvp = argv;
807303cf
DK
486 /* Add NULL-termination. Guaranteed not to overflow because
487 arg_count here can only be less than argc. */
488 argv[arg_count] = 0;
14c7833c
L
489 }
490 else
491 {
492done:
493 free (argv);
494 }
495
496 free (options);
497}
5f20c657 498
481e1176
JM
499/* Handle option DECODED for the language indicated by LANG_MASK,
500 using the handlers in HANDLERS. KIND is the diagnostic_t if this
501 is a diagnostics option, DK_UNSPECIFIED otherwise. Returns false
502 if the switch was invalid. */
5f20c657
JM
503
504bool
481e1176 505handle_option (const struct cl_decoded_option *decoded,
5f20c657
JM
506 unsigned int lang_mask, int kind,
507 const struct cl_option_handlers *handlers)
508{
481e1176
JM
509 size_t opt_index = decoded->opt_index;
510 const char *arg = decoded->arg;
511 int value = decoded->value;
5f20c657
JM
512 const struct cl_option *option = &cl_options[opt_index];
513 size_t i;
514
515 if (option->flag_var)
516 set_option (opt_index, value, arg, kind);
517
518 for (i = 0; i < handlers->num_handlers; i++)
519 if (option->flags & handlers->handlers[i].mask)
520 {
481e1176 521 if (!handlers->handlers[i].handler (decoded,
5f20c657
JM
522 lang_mask, kind, handlers))
523 return false;
524 else
481e1176 525 handlers->post_handling_callback (decoded,
5f20c657
JM
526 handlers->handlers[i].mask);
527 }
528
529 return true;
530}
531
481e1176
JM
532/* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
533 option instead of DECODED. This is used for callbacks when one
534 option implies another instead of an option being decoded from the
535 command line. */
536
537bool
538handle_generated_option (size_t opt_index, const char *arg, int value,
539 unsigned int lang_mask, int kind,
540 const struct cl_option_handlers *handlers)
541{
542 const struct cl_option *option = &cl_options[opt_index];
543 struct cl_decoded_option decoded;
544
545 decoded.opt_index = opt_index;
546 decoded.arg = arg;
547 decoded.canonical_option[2] = NULL;
548 decoded.canonical_option[3] = NULL;
549 decoded.value = value;
550 decoded.errors = 0;
551
552 if (arg)
553 {
554 if (option->flags & CL_SEPARATE)
555 {
556 decoded.orig_option_with_args_text = concat (option->opt_text, " ",
557 arg, NULL);
558 decoded.canonical_option[0] = option->opt_text;
559 decoded.canonical_option[1] = arg;
560 decoded.canonical_option_num_elements = 2;
561 }
562 else
563 {
564 gcc_assert (option->flags & CL_JOINED);
565 decoded.orig_option_with_args_text = concat (option->opt_text, arg,
566 NULL);
567 decoded.canonical_option[0] = decoded.orig_option_with_args_text;
568 decoded.canonical_option[1] = NULL;
569 decoded.canonical_option_num_elements = 1;
570 }
571 }
572 else
573 {
574 decoded.orig_option_with_args_text = option->opt_text;
575 decoded.canonical_option[0] = option->opt_text;
576 decoded.canonical_option[1] = NULL;
577 decoded.canonical_option_num_elements = 1;
578 }
579
580 return handle_option (&decoded, lang_mask, kind, handlers);
581}
582
5f20c657
JM
583/* Handle the switch DECODED for the language indicated by LANG_MASK,
584 using the handlers in *HANDLERS. */
585
586void
587read_cmdline_option (struct cl_decoded_option *decoded,
588 unsigned int lang_mask,
589 const struct cl_option_handlers *handlers)
590{
591 const struct cl_option *option;
592 const char *opt;
593
594 if (decoded->opt_index == OPT_SPECIAL_unknown)
595 {
481e1176
JM
596 if (handlers->unknown_option_callback (decoded))
597 error ("unrecognized command line option %qs", decoded->arg);
5f20c657
JM
598 return;
599 }
600
601 option = &cl_options[decoded->opt_index];
602 opt = decoded->orig_option_with_args_text;
603
604 if (decoded->errors & CL_ERR_DISABLED)
605 {
606 error ("command line option %qs"
607 " is not supported by this configuration", opt);
608 return;
609 }
610
611 if (decoded->errors & CL_ERR_WRONG_LANG)
612 {
481e1176 613 handlers->wrong_lang_callback (decoded, lang_mask);
5f20c657
JM
614 return;
615 }
616
617 if (decoded->errors & CL_ERR_MISSING_ARG)
618 {
619 if (option->missing_argument_error)
620 error (option->missing_argument_error, opt);
621 else
622 error ("missing argument to %qs", opt);
623 return;
624 }
625
626 if (decoded->errors & CL_ERR_UINT_ARG)
627 {
628 error ("argument to %qs should be a non-negative integer",
629 option->opt_text);
630 return;
631 }
632
633 gcc_assert (!decoded->errors);
634
481e1176 635 if (!handle_option (decoded, lang_mask, DK_UNSPECIFIED, handlers))
5f20c657
JM
636 error ("unrecognized command line option %qs", opt);
637}
638
639/* Set any variable for option OPT_INDEX according to VALUE and ARG,
640 diagnostic kind KIND. */
641
642void
643set_option (int opt_index, int value, const char *arg, int kind)
644{
645 const struct cl_option *option = &cl_options[opt_index];
646
647 if (!option->flag_var)
648 return;
649
650 switch (option->var_type)
651 {
652 case CLVC_BOOLEAN:
653 *(int *) option->flag_var = value;
654 break;
655
656 case CLVC_EQUAL:
657 *(int *) option->flag_var = (value
658 ? option->var_value
659 : !option->var_value);
660 break;
661
662 case CLVC_BIT_CLEAR:
663 case CLVC_BIT_SET:
664 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
665 *(int *) option->flag_var |= option->var_value;
666 else
667 *(int *) option->flag_var &= ~option->var_value;
668 if (option->flag_var == &target_flags)
669 target_flags_explicit |= option->var_value;
670 break;
671
672 case CLVC_STRING:
673 *(const char **) option->flag_var = arg;
674 break;
675 }
676
677 if ((diagnostic_t) kind != DK_UNSPECIFIED)
678 diagnostic_classify_diagnostic (global_dc, opt_index, (diagnostic_t) kind,
679 UNKNOWN_LOCATION);
680}