OSDN Git Service

* Makefile.in: Update.
[pf3gnuchains/gcc-fork.git] / gcc / opts.c
1 /* Command line option handling.
2    Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3    Contributed by Neil Booth.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "ggc.h"
29 #include "output.h"
30 #include "langhooks.h"
31 #include "opts.h"
32 #include "options.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "params.h"
36
37 /* Value of the -G xx switch, and whether it was passed or not.  */
38 unsigned HOST_WIDE_INT g_switch_value;
39 bool g_switch_set;
40
41 /* True if we should exit after parsing options.  */
42 bool exit_after_options;
43
44 /* If -version.  */
45 bool version_flag;
46
47 /* Print various extra warnings.  -W/-Wextra.  */
48 bool extra_warnings;
49
50 /* Don't print warning messages.  -w.  */
51 bool inhibit_warnings;
52
53 /* Treat warnings as errors.  -Werror.  */
54 bool warnings_are_errors;
55
56 /* Warn if a function returns an aggregate, since there are often
57    incompatible calling conventions for doing this.  */
58 bool warn_aggregate_return;
59
60 /* Nonzero means warn about pointer casts that increase the required
61    alignment of the target type (and might therefore lead to a crash
62    due to a misaligned access).  */
63 bool warn_cast_align;
64
65 /* Nonzero means warn about uses of __attribute__((deprecated))
66    declarations.  */
67 bool warn_deprecated_decl = true;
68
69 /* Warn when an optimization pass is disabled.  */
70 bool warn_disabled_optimization;
71
72 /* Nonzero means warn if inline function is too large.  */
73 bool warn_inline;
74
75 /* True to warn about any objects definitions whose size is larger
76    than N bytes.  Also want about function definitions whose returned
77    values are larger than N bytes, where N is `larger_than_size'.  */
78 bool warn_larger_than;
79 HOST_WIDE_INT larger_than_size;
80
81 /* Warn about functions which might be candidates for attribute noreturn.  */
82 bool warn_missing_noreturn;
83
84 /* True to warn about code which is never reached.  */
85 bool warn_notreached;
86
87 /* Warn if packed attribute on struct is unnecessary and inefficient.  */
88 bool warn_packed;
89
90 /* Warn when gcc pads a structure to an alignment boundary.  */
91 bool warn_padded;
92
93 /* True means warn about all declarations which shadow others.  */
94 bool warn_shadow;
95
96 /* Nonzero means warn about constructs which might not be
97    strict-aliasing safe.  */
98 bool warn_strict_aliasing;
99
100 /* True to warn if a switch on an enum, that does not have a default
101    case, fails to have a case for every enum value.  */
102 bool warn_switch;
103
104 /* Warn if a switch does not have a default case.  */
105 bool warn_switch_default;
106
107 /* Warn if a switch on an enum fails to have a case for every enum
108    value (regardless of the presence or otherwise of a default case).  */
109 bool warn_switch_enum;
110
111 /* Don't suppress warnings from system headers.  -Wsystem-headers.  */
112 bool warn_system_headers;
113
114 /* True to warn about variables used before they are initialized.  */
115 int warn_uninitialized;
116
117 /* True to warn about unused variables, functions et.al.  */
118 bool warn_unused_function;
119 bool warn_unused_label;
120 bool warn_unused_parameter;
121 bool warn_unused_variable;
122 bool warn_unused_value;
123
124 /* Hack for cooperation between set_Wunused and set_Wextra.  */
125 static bool maybe_warn_unused_parameter;
126
127 static size_t find_opt (const char *, int);
128 static int common_handle_option (size_t scode, const char *arg, int value);
129 static void handle_param (const char *);
130 static void set_Wextra (int);
131
132 /* Perform a binary search to find which option the command-line INPUT
133    matches.  Returns its index in the option array, and N_OPTS on
134    failure.
135
136    Complications arise since some options can be suffixed with an
137    argument, and multiple complete matches can occur, e.g. -pedantic
138    and -pedantic-errors.  Also, some options are only accepted by some
139    languages.  If a switch matches for a different language and
140    doesn't match any alternatives for the true front end, the index of
141    the matched switch is returned anyway.  The caller should check for
142    this case.  */
143 static size_t
144 find_opt (const char *input, int lang_mask)
145 {
146   size_t md, mn, mx;
147   size_t opt_len;
148   size_t result = cl_options_count;
149   int comp;
150
151   mn = 0;
152   mx = cl_options_count;
153
154   while (mx > mn)
155     {
156       md = (mn + mx) / 2;
157
158       opt_len = cl_options[md].opt_len;
159       comp = strncmp (input, cl_options[md].opt_text, opt_len);
160
161       if (comp < 0)
162         mx = md;
163       else if (comp > 0)
164         mn = md + 1;
165       else
166         {
167           /* The switch matches.  It it an exact match?  */
168           if (input[opt_len] == '\0')
169             return md;
170           else
171             {
172               mn = md + 1;
173
174               /* If the switch takes no arguments this is not a proper
175                  match, so we continue the search (e.g. input="stdc++"
176                  match was "stdc").  */
177               if (!(cl_options[md].flags & CL_JOINED))
178                 continue;
179
180               /* Is this switch valid for this front end?  */
181               if (!(cl_options[md].flags & lang_mask))
182                 {
183                   /* If subsequently we don't find a better match,
184                      return this and let the caller report it as a bad
185                      match.  */
186                   result = md;
187                   continue;
188                 }
189
190               /* Two scenarios remain: we have the switch's argument,
191                  or we match a longer option.  This can happen with
192                  -iwithprefix and -withprefixbefore.  The longest
193                  possible option match succeeds.
194
195                  Scan forwards, and return an exact match.  Otherwise
196                  return the longest valid option-accepting match (mx).
197                  This loops at most twice with current options.  */
198               mx = md;
199               for (md = md + 1; md < cl_options_count; md++)
200                 {
201                   opt_len = cl_options[md].opt_len;
202                   comp = strncmp (input, cl_options[md].opt_text, opt_len);
203                   if (comp < 0)
204                     break;
205                   if (comp > 0)
206                     continue;
207                   if (input[opt_len] == '\0')
208                     return md;
209                   if (cl_options[md].flags & lang_mask
210                       && cl_options[md].flags & CL_JOINED)
211                     mx = md;
212                 }
213
214               return mx;
215             }
216         }
217     }
218
219   return result;
220 }
221
222 /* If ARG is a non-negative integer made up solely of digits, return its
223    value, otherwise return -1.  */
224 static int
225 integral_argument (const char *arg)
226 {
227   const char *p = arg;
228
229   while (*p && ISDIGIT (*p))
230     p++;
231
232   if (*p == '\0')
233     return atoi (arg);
234
235   return -1;
236 }
237
238 /* Handle the switch beginning at ARGV, with ARGC remaining.  */
239 int
240 handle_option (int argc ATTRIBUTE_UNUSED, char **argv, int lang_mask)
241 {
242   size_t opt_index;
243   const char *opt, *arg = 0;
244   char *dup = 0;
245   int value = 1;
246   int result = 0;
247   const struct cl_option *option;
248
249   opt = argv[0];
250
251   /* Interpret "-" or a non-switch as a file name.  */
252   if (opt[0] != '-' || opt[1] == '\0')
253     {
254       opt_index = cl_options_count;
255       arg = opt;
256       main_input_filename = opt;
257       result = (*lang_hooks.handle_option) (opt_index, arg, value);
258     }
259   else
260     {
261       /* Drop the "no-" from negative switches.  */
262       if ((opt[1] == 'W' || opt[1] == 'f')
263           && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
264         {
265           size_t len = strlen (opt) - 3;
266
267           dup = xmalloc (len + 1);
268           dup[0] = '-';
269           dup[1] = opt[1];
270           memcpy (dup + 2, opt + 5, len - 2 + 1);
271           opt = dup;
272           value = 0;
273         }
274
275       opt_index = find_opt (opt + 1, lang_mask | CL_COMMON);
276       if (opt_index == cl_options_count)
277         goto done;
278
279       option = &cl_options[opt_index];
280
281       /* Reject negative form of switches that don't take negatives.  */
282       if (!value && (option->flags & CL_REJECT_NEGATIVE))
283         goto done;
284
285       /* We've recognized this switch.  */
286       result = 1;
287
288       /* Sort out any argument the switch takes.  */
289       if (option->flags & CL_JOINED)
290         {
291           /* Have arg point to the original switch.  This is because
292              some code, such as disable_builtin_function, expects its
293              argument to be persistent until the program exits.  */
294           arg = argv[0] + cl_options[opt_index].opt_len + 1;
295           if (!value)
296             arg += strlen ("no-");
297
298           if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
299             {
300               if (option->flags & CL_SEPARATE)
301                 {
302                   arg = argv[1];
303                   result = 2;
304                 }
305               else
306                 /* Missing argument.  */
307                 arg = NULL;
308             }
309         }
310       else if (option->flags & CL_SEPARATE)
311         {
312           arg = argv[1];
313           result = 2;
314         }
315
316       /* If the switch takes an integer, convert it.  */
317       if (arg && (option->flags & CL_UINTEGER))
318         {
319           value = integral_argument (arg);
320           if (value == -1)
321             {
322               error ("argument to \"-%s\" should be a non-negative integer",
323                      option->opt_text);
324               goto done;
325             }
326         }
327
328       if (option->flags & lang_mask)
329         if ((*lang_hooks.handle_option) (opt_index, arg, value) == 0)
330           result = 0;
331
332       if (result && (option->flags & CL_COMMON))
333         if (common_handle_option (opt_index, arg, value) == 0)
334           result = 0;
335     }
336
337  done:
338   if (dup)
339     free (dup);
340   return result;
341 }
342
343 /* Handle target- and language-independent options.  Return zero to
344    generate an "unknown option" message.  */
345 static int
346 common_handle_option (size_t scode, const char *arg,
347                       int value ATTRIBUTE_UNUSED)
348 {
349   const struct cl_option *option = &cl_options[scode];
350   enum opt_code code = (enum opt_code) scode;
351
352   if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
353     {
354       error ("missing argument to \"-%s\"", option->opt_text);
355       return 1;
356     }
357
358   switch (code)
359     {
360     default:
361       abort ();
362
363     case OPT__help:
364       display_help ();
365       exit_after_options = true;
366       break;
367
368     case OPT__param:
369       handle_param (arg);
370       break;
371
372     case OPT__target_help:
373       display_target_options ();
374       exit_after_options = true;
375       break;
376
377     case OPT__version:
378       print_version (stderr, "");
379       exit_after_options = true;
380       break;
381
382     case OPT_G:
383       g_switch_value = value;
384       g_switch_set = true;
385       break;
386
387     case OPT_O:
388     case OPT_Os:
389       /* Currently handled in a prescan.  */
390       break;
391
392     case OPT_W:
393       /* For backward compatibility, -W is the same as -Wextra.  */
394       set_Wextra (value);
395       break;
396
397     case OPT_Waggregate_return:
398       warn_aggregate_return = value;
399       break;
400
401     case OPT_Wcast_align:
402       warn_cast_align = value;
403       break;
404
405     case OPT_Wdeprecated_declarations:
406       warn_deprecated_decl = value;
407
408     case OPT_Wdisabled_optimization:
409       warn_disabled_optimization = value;
410       break;
411
412     case OPT_Werror:
413       warnings_are_errors = value;
414       break;
415
416     case OPT_Wextra:
417       set_Wextra (value);
418       break;
419
420     case OPT_Winline:
421       warn_inline = value;
422       break;
423
424     case OPT_Wlarger_than_:
425       larger_than_size = value;
426       warn_larger_than = value != -1;
427       break;
428
429     case OPT_Wmissing_noreturn:
430       warn_missing_noreturn = value;
431       break;
432
433     case OPT_Wpacked:
434       warn_packed = value;
435       break;
436
437     case OPT_Wpadded:
438       warn_padded = value;
439       break;
440
441     case OPT_Wshadow:
442       warn_shadow = value;
443       break;
444
445     case OPT_Wstrict_aliasing:
446       warn_strict_aliasing = value;
447       break;
448
449     case OPT_Wswitch:
450       warn_switch = value;
451       break;
452
453     case OPT_Wswitch_default:
454       warn_switch_default = value;
455       break;
456
457     case OPT_Wswitch_enum:
458       warn_switch_enum = value;
459       break;
460
461     case OPT_Wsystem_headers:
462       warn_system_headers = value;
463       break;
464
465     case OPT_Wuninitialized:
466       warn_uninitialized = value;
467       break;
468
469     case OPT_Wunreachable_code:
470       warn_notreached = value;
471       break;
472
473     case OPT_Wunused:
474       set_Wunused (value);
475       break;
476
477     case OPT_Wunused_function:
478       warn_unused_function = value;
479       break;
480
481     case OPT_Wunused_label:
482       warn_unused_label = value;
483       break;
484
485     case OPT_Wunused_parameter:
486       warn_unused_parameter = value;
487       break;
488
489     case OPT_Wunused_value:
490       warn_unused_value = value;
491       break;
492
493     case OPT_Wunused_variable:
494       warn_unused_variable = value;
495       break;
496
497     case OPT_aux_info:
498     case OPT_aux_info_:
499       aux_info_file_name = arg;
500       flag_gen_aux_info = 1;
501       break;
502
503     case OPT_auxbase:
504       aux_base_name = arg;
505       break;
506
507     case OPT_auxbase_strip:
508       {
509         char *tmp = xstrdup (arg);
510         strip_off_ending (tmp, strlen (tmp));
511         if (tmp[0])
512           aux_base_name = tmp;
513       }
514       break;
515
516     case OPT_d:
517       decode_d_option (arg);
518       break;
519
520     case OPT_dumpbase:
521       dump_base_name = arg;
522       break;
523
524     case OPT_falign_functions_:
525       align_functions = value;
526       break;
527
528     case OPT_falign_jumps_:
529       align_jumps = value;
530       break;
531
532     case OPT_falign_labels_:
533       align_labels = value;
534       break;
535
536     case OPT_falign_loops_:
537       align_loops = value;
538       break;
539
540     case OPT_fcall_used_:
541       fix_register (arg, 0, 1);
542       break;
543
544     case OPT_fcall_saved_:
545       fix_register (arg, 0, 0);
546       break;
547
548     case OPT_ffast_math:
549       set_fast_math_flags (value);
550       break;
551
552     case OPT_ffixed_:
553       fix_register (arg, 1, 1);
554       break;
555
556     case OPT_fstack_limit_register_:
557       {
558         int reg = decode_reg_name (arg);
559         if (reg < 0)
560           error ("unrecognized register name \"%s\"", arg);
561         else
562           stack_limit_rtx = gen_rtx_REG (Pmode, reg);
563       }
564       break;
565
566     case OPT_fstack_limit_symbol_:
567       stack_limit_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (arg));
568       break;
569
570     case OPT_ftls_model_:
571       if (!strcmp (arg, "global-dynamic"))
572         flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC;
573       else if (!strcmp (arg, "local-dynamic"))
574         flag_tls_default = TLS_MODEL_LOCAL_DYNAMIC;
575       else if (!strcmp (arg, "initial-exec"))
576         flag_tls_default = TLS_MODEL_INITIAL_EXEC;
577       else if (!strcmp (arg, "local-exec"))
578         flag_tls_default = TLS_MODEL_LOCAL_EXEC;
579       else
580         warning ("unknown tls-model \"%s\"", arg);
581       break;
582
583     case OPT_g:
584       decode_g_option (arg);
585       break;
586
587     case OPT_m:
588       set_target_switch (arg);
589       break;
590
591     case OPT_o:
592       asm_file_name = arg;
593       break;
594
595     case OPT_p:
596       profile_flag = 1;
597       break;
598
599     case OPT_pedantic:
600       pedantic = 1;
601       break;
602
603     case OPT_pedantic_errors:
604       flag_pedantic_errors = pedantic = 1;
605       break;
606
607     case OPT_quiet:
608       quiet_flag = 1;
609       break;
610
611     case OPT_version:
612       version_flag = 1;
613       break;
614
615     case OPT_w:
616       inhibit_warnings = true;
617       break;      
618     }
619
620   return 1;
621 }
622
623 /* Handle --param NAME=VALUE.  */
624 static void
625 handle_param (const char *carg)
626 {
627   char *equal, *arg;
628   int value;
629
630   arg = xstrdup (carg);
631   equal = strchr (arg, '=');
632   if (!equal)
633     error ("%s: --param arguments should be of the form NAME=VALUE", arg);
634   else
635     {
636       value = integral_argument (equal + 1);
637       if (value == -1)
638         error ("invalid --param value `%s'", equal + 1);
639       else
640         {
641           *equal = '\0';
642           set_param_value (arg, value);
643         }
644     }
645
646   free (arg);
647 }
648
649 /* Handle -W and -Wextra.  */
650 static void
651 set_Wextra (int setting)
652 {
653   extra_warnings = setting;
654   warn_unused_value = setting;
655   warn_unused_parameter = (setting && maybe_warn_unused_parameter);
656
657   /* We save the value of warn_uninitialized, since if they put
658      -Wuninitialized on the command line, we need to generate a
659      warning about not using it without also specifying -O.  */
660   if (setting == 0)
661     warn_uninitialized = 0;
662   else if (warn_uninitialized != 1)
663     warn_uninitialized = 2;
664 }
665
666 /* Initialize unused warning flags.  */
667 void
668 set_Wunused (int setting)
669 {
670   warn_unused_function = setting;
671   warn_unused_label = setting;
672   /* Unused function parameter warnings are reported when either
673      ``-Wextra -Wunused'' or ``-Wunused-parameter'' is specified.
674      Thus, if -Wextra has already been seen, set warn_unused_parameter;
675      otherwise set maybe_warn_extra_parameter, which will be picked up
676      by set_Wextra.  */
677   maybe_warn_unused_parameter = setting;
678   warn_unused_parameter = (setting && extra_warnings);
679   warn_unused_variable = setting;
680   warn_unused_value = setting;
681 }
682
683 /* The following routines are useful in setting all the flags that
684    -ffast-math and -fno-fast-math imply.  */
685 void
686 set_fast_math_flags (int set)
687 {
688   flag_trapping_math = !set;
689   flag_unsafe_math_optimizations = set;
690   flag_finite_math_only = set;
691   flag_errno_math = !set;
692   if (set)
693     flag_signaling_nans = 0;
694 }
695
696 /* Return true iff flags are set as if -ffast-math.  */
697 bool
698 fast_math_flags_set_p (void)
699 {
700   return (!flag_trapping_math
701           && flag_unsafe_math_optimizations
702           && flag_finite_math_only
703           && !flag_errno_math);
704 }