1132718Skan/* Command line option handling.
2169703Skan   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
3169703Skan   Free Software Foundation, Inc.
4132718Skan   Contributed by Neil Booth.
5132718Skan
6132718SkanThis file is part of GCC.
7132718Skan
8132718SkanGCC is free software; you can redistribute it and/or modify it under
9132718Skanthe terms of the GNU General Public License as published by the Free
10132718SkanSoftware Foundation; either version 2, or (at your option) any later
11132718Skanversion.
12132718Skan
13132718SkanGCC is distributed in the hope that it will be useful, but WITHOUT ANY
14132718SkanWARRANTY; without even the implied warranty of MERCHANTABILITY or
15132718SkanFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16132718Skanfor more details.
17132718Skan
18132718SkanYou should have received a copy of the GNU General Public License
19132718Skanalong with GCC; see the file COPYING.  If not, write to the Free
20169703SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21169703Skan02110-1301, USA.  */
22132718Skan
23132735Skan/* $FreeBSD$ */
24132735Skan
25132718Skan#include "config.h"
26132718Skan#include "system.h"
27132718Skan#include "intl.h"
28132718Skan#include "coretypes.h"
29132718Skan#include "tm.h"
30132718Skan#include "tree.h"
31132718Skan#include "rtl.h"
32132718Skan#include "ggc.h"
33132718Skan#include "output.h"
34132718Skan#include "langhooks.h"
35132718Skan#include "opts.h"
36132718Skan#include "options.h"
37132718Skan#include "flags.h"
38132718Skan#include "toplev.h"
39132718Skan#include "params.h"
40132718Skan#include "diagnostic.h"
41132718Skan#include "tm_p.h"		/* For OPTIMIZATION_OPTIONS.  */
42132718Skan#include "insn-attr.h"		/* For INSN_SCHEDULING.  */
43169703Skan#include "target.h"
44169703Skan#include "tree-pass.h"
45132718Skan
46132718Skan/* Value of the -G xx switch, and whether it was passed or not.  */
47132718Skanunsigned HOST_WIDE_INT g_switch_value;
48132718Skanbool g_switch_set;
49132718Skan
50132718Skan/* True if we should exit after parsing options.  */
51132718Skanbool exit_after_options;
52132718Skan
53132718Skan/* Print various extra warnings.  -W/-Wextra.  */
54132718Skanbool extra_warnings;
55132718Skan
56132718Skan/* True to warn about any objects definitions whose size is larger
57132718Skan   than N bytes.  Also want about function definitions whose returned
58132718Skan   values are larger than N bytes, where N is `larger_than_size'.  */
59132718Skanbool warn_larger_than;
60132718SkanHOST_WIDE_INT larger_than_size;
61132718Skan
62259405Spfg/* True to warn about any function whose frame size is larger
63259405Spfg * than N bytes. */
64259405Spfgbool warn_frame_larger_than;
65259405SpfgHOST_WIDE_INT frame_larger_than_size;
66259405Spfg
67132718Skan/* Nonzero means warn about constructs which might not be
68132718Skan   strict-aliasing safe.  */
69169703Skanint warn_strict_aliasing;
70132718Skan
71169703Skan/* Nonzero means warn about optimizations which rely on undefined
72169703Skan   signed overflow.  */
73169703Skanint warn_strict_overflow;
74132718Skan
75132718Skan/* Hack for cooperation between set_Wunused and set_Wextra.  */
76132718Skanstatic bool maybe_warn_unused_parameter;
77132718Skan
78132718Skan/* Type(s) of debugging information we are producing (if any).  See
79132718Skan   flags.h for the definitions of the different possible types of
80132718Skan   debugging information.  */
81132718Skanenum debug_info_type write_symbols = NO_DEBUG;
82132718Skan
83132718Skan/* Level of debugging information we are producing.  See flags.h for
84132718Skan   the definitions of the different possible levels.  */
85132718Skanenum debug_info_level debug_info_level = DINFO_LEVEL_NONE;
86132718Skan
87259268Spfg/* A major contribution to object and executable size is debug
88259268Spfg   information size.  A major contribution to debug information size
89259268Spfg   is struct descriptions replicated in several object files. The
90259268Spfg   following flags attempt to reduce this information.  The basic
91259268Spfg   idea is to not emit struct debugging information in the current
92259268Spfg   compilation unit when that information will be generated by
93259268Spfg   another compilation unit.
94259268Spfg
95259268Spfg   Debug information for a struct defined in the current source
96259268Spfg   file should be generated in the object file.  Likewise the
97259268Spfg   debug information for a struct defined in a header should be
98259268Spfg   generated in the object file of the corresponding source file.
99259268Spfg   Both of these case are handled when the base name of the file of
100259268Spfg   the struct definition matches the base name of the source file
101259268Spfg   of thet current compilation unit.  This matching emits minimal
102259268Spfg   struct debugging information.
103259268Spfg
104259268Spfg   The base file name matching rule above will fail to emit debug
105259268Spfg   information for structs defined in system headers.  So a second
106259268Spfg   category of files includes system headers in addition to files
107259268Spfg   with matching bases.
108259268Spfg
109259268Spfg   The remaining types of files are library headers and application
110259268Spfg   headers.  We cannot currently distinguish these two types.  */
111259268Spfg
112259268Spfgenum debug_struct_file
113259268Spfg{
114259268Spfg  DINFO_STRUCT_FILE_NONE,   /* Debug no structs. */
115259268Spfg  DINFO_STRUCT_FILE_BASE,   /* Debug structs defined in files with the
116259268Spfg                               same base name as the compilation unit. */
117259268Spfg  DINFO_STRUCT_FILE_SYS,    /* Also debug structs defined in system
118259268Spfg                               header files.  */
119259268Spfg  DINFO_STRUCT_FILE_ANY     /* Debug structs defined in all files. */
120259268Spfg};
121259268Spfg
122259268Spfg/* Generic structs (e.g. templates not explicitly specialized)
123259268Spfg   may not have a compilation unit associated with them, and so
124259268Spfg   may need to be treated differently from ordinary structs.
125259268Spfg
126259268Spfg   Structs only handled by reference (indirectly), will also usually
127259268Spfg   not need as much debugging information.  */
128259268Spfg
129259268Spfgstatic enum debug_struct_file debug_struct_ordinary[DINFO_USAGE_NUM_ENUMS]
130259268Spfg  = { DINFO_STRUCT_FILE_ANY, DINFO_STRUCT_FILE_ANY, DINFO_STRUCT_FILE_ANY };
131259268Spfgstatic enum debug_struct_file debug_struct_generic[DINFO_USAGE_NUM_ENUMS]
132259268Spfg  = { DINFO_STRUCT_FILE_ANY, DINFO_STRUCT_FILE_ANY, DINFO_STRUCT_FILE_ANY };
133259268Spfg
134259268Spfg/* Parse the -femit-struct-debug-detailed option value
135259268Spfg   and set the flag variables. */
136259268Spfg
137259268Spfg#define MATCH( prefix, string ) \
138259268Spfg  ((strncmp (prefix, string, sizeof prefix - 1) == 0) \
139259268Spfg   ? ((string += sizeof prefix - 1), 1) : 0)
140259268Spfg
141259268Spfgvoid
142259268Spfgset_struct_debug_option (const char *spec)
143259268Spfg{
144259268Spfg  /* various labels for comparison */
145259268Spfg  static char dfn_lbl[] = "dfn:", dir_lbl[] = "dir:", ind_lbl[] = "ind:";
146259268Spfg  static char ord_lbl[] = "ord:", gen_lbl[] = "gen:";
147259268Spfg  static char none_lbl[] = "none", any_lbl[] = "any";
148259268Spfg  static char base_lbl[] = "base", sys_lbl[] = "sys";
149259268Spfg
150259268Spfg  enum debug_struct_file files = DINFO_STRUCT_FILE_ANY;
151259268Spfg  /* Default is to apply to as much as possible. */
152259268Spfg  enum debug_info_usage usage = DINFO_USAGE_NUM_ENUMS;
153259268Spfg  int ord = 1, gen = 1;
154259268Spfg
155259268Spfg  /* What usage? */
156259268Spfg  if (MATCH (dfn_lbl, spec))
157259268Spfg    usage = DINFO_USAGE_DFN;
158259268Spfg  else if (MATCH (dir_lbl, spec))
159259268Spfg    usage = DINFO_USAGE_DIR_USE;
160259268Spfg  else if (MATCH (ind_lbl, spec))
161259268Spfg    usage = DINFO_USAGE_IND_USE;
162259268Spfg
163259268Spfg  /* Generics or not? */
164259268Spfg  if (MATCH (ord_lbl, spec))
165259268Spfg    gen = 0;
166259268Spfg  else if (MATCH (gen_lbl, spec))
167259268Spfg    ord = 0;
168259268Spfg
169259268Spfg  /* What allowable environment? */
170259268Spfg  if (MATCH (none_lbl, spec))
171259268Spfg    files = DINFO_STRUCT_FILE_NONE;
172259268Spfg  else if (MATCH (any_lbl, spec))
173259268Spfg    files = DINFO_STRUCT_FILE_ANY;
174259268Spfg  else if (MATCH (sys_lbl, spec))
175259268Spfg    files = DINFO_STRUCT_FILE_SYS;
176259268Spfg  else if (MATCH (base_lbl, spec))
177259268Spfg    files = DINFO_STRUCT_FILE_BASE;
178259268Spfg  else
179259268Spfg    error ("argument %qs to %<-femit-struct-debug-detailed%> not recognized",
180259268Spfg           spec);
181259268Spfg
182259268Spfg  /* Effect the specification. */
183259268Spfg  if (usage == DINFO_USAGE_NUM_ENUMS)
184259268Spfg    {
185259268Spfg      if (ord)
186259268Spfg        {
187259268Spfg          debug_struct_ordinary[DINFO_USAGE_DFN] = files;
188259268Spfg          debug_struct_ordinary[DINFO_USAGE_DIR_USE] = files;
189259268Spfg          debug_struct_ordinary[DINFO_USAGE_IND_USE] = files;
190259268Spfg        }
191259268Spfg      if (gen)
192259268Spfg        {
193259268Spfg          debug_struct_generic[DINFO_USAGE_DFN] = files;
194259268Spfg          debug_struct_generic[DINFO_USAGE_DIR_USE] = files;
195259268Spfg          debug_struct_generic[DINFO_USAGE_IND_USE] = files;
196259268Spfg        }
197259268Spfg    }
198259268Spfg  else
199259268Spfg    {
200259268Spfg      if (ord)
201259268Spfg        debug_struct_ordinary[usage] = files;
202259268Spfg      if (gen)
203259268Spfg        debug_struct_generic[usage] = files;
204259268Spfg    }
205259268Spfg
206259268Spfg  if (*spec == ',')
207259268Spfg    set_struct_debug_option (spec+1);
208259268Spfg  else
209259268Spfg    {
210259268Spfg      /* No more -femit-struct-debug-detailed specifications.
211259268Spfg         Do final checks. */
212259268Spfg      if (*spec != '\0')
213259268Spfg	error ("argument %qs to %<-femit-struct-debug-detailed%> unknown",
214259268Spfg               spec);
215259268Spfg      if (debug_struct_ordinary[DINFO_USAGE_DIR_USE]
216259268Spfg		< debug_struct_ordinary[DINFO_USAGE_IND_USE]
217259268Spfg	  || debug_struct_generic[DINFO_USAGE_DIR_USE]
218259268Spfg		< debug_struct_generic[DINFO_USAGE_IND_USE])
219259268Spfg	error ("%<-femit-struct-debug-detailed=dir:...%> must allow at least"
220259268Spfg               " as much as %<-femit-struct-debug-detailed=ind:...%>");
221259268Spfg    }
222259268Spfg}
223259268Spfg
224259268Spfg/* Find the base name of a path, stripping off both directories and
225259268Spfg   a single final extension. */
226259268Spfgstatic int
227259268Spfgbase_of_path (const char *path, const char **base_out)
228259268Spfg{
229259268Spfg  const char *base = path;
230259268Spfg  const char *dot = 0;
231259268Spfg  const char *p = path;
232259268Spfg  char c = *p;
233259268Spfg  while (c)
234259268Spfg    {
235259268Spfg      if (IS_DIR_SEPARATOR(c))
236259268Spfg        {
237259268Spfg          base = p + 1;
238259268Spfg          dot = 0;
239259268Spfg        }
240259268Spfg      else if (c == '.')
241259268Spfg        dot = p;
242259268Spfg      c = *++p;
243259268Spfg    }
244259268Spfg  if (!dot)
245259268Spfg    dot = p;
246259268Spfg  *base_out = base;
247259268Spfg  return dot - base;
248259268Spfg}
249259268Spfg
250259268Spfg/* Match the base name of a file to the base name of a compilation unit. */
251259268Spfg
252259268Spfgstatic const char *main_input_basename;
253259268Spfgstatic int main_input_baselength;
254259268Spfg
255259268Spfgstatic int
256259268Spfgmatches_main_base (const char *path)
257259268Spfg{
258259268Spfg  /* Cache the last query. */
259259268Spfg  static const char *last_path = NULL;
260259268Spfg  static int last_match = 0;
261259268Spfg  if (path != last_path)
262259268Spfg    {
263259268Spfg      const char *base;
264259268Spfg      int length = base_of_path (path, &base);
265259268Spfg      last_path = path;
266259268Spfg      last_match = (length == main_input_baselength
267259268Spfg                    && memcmp (base, main_input_basename, length) == 0);
268259268Spfg    }
269259268Spfg  return last_match;
270259268Spfg}
271259268Spfg
272259268Spfg#ifdef DEBUG_DEBUG_STRUCT
273259268Spfg
274259268Spfgstatic int
275259268Spfgdump_struct_debug (tree type, enum debug_info_usage usage,
276259268Spfg		   enum debug_struct_file criterion, int generic,
277259268Spfg		   int matches, int result)
278259268Spfg{
279259268Spfg  /* Find the type name. */
280259268Spfg  tree type_decl = TYPE_STUB_DECL (type);
281259268Spfg  tree t = type_decl;
282259268Spfg  const char *name = 0;
283259268Spfg  if (TREE_CODE (t) == TYPE_DECL)
284259268Spfg    t = DECL_NAME (t);
285259268Spfg  if (t)
286259268Spfg    name = IDENTIFIER_POINTER (t);
287259268Spfg
288259268Spfg  fprintf (stderr, "	struct %d %s %s %s %s %d %p %s\n",
289259268Spfg	   criterion,
290259268Spfg           DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
291259268Spfg           matches ? "bas" : "hdr",
292259268Spfg           generic ? "gen" : "ord",
293259268Spfg           usage == DINFO_USAGE_DFN ? ";" :
294259268Spfg             usage == DINFO_USAGE_DIR_USE ? "." : "*",
295259268Spfg           result,
296259268Spfg           (void*) type_decl, name);
297259268Spfg  return result;
298259268Spfg}
299259268Spfg#define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
300259268Spfg  dump_struct_debug (type, usage, criterion, generic, matches, result)
301259268Spfg
302259268Spfg#else
303259268Spfg
304259268Spfg#define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
305259268Spfg  (result)
306259268Spfg
307259268Spfg#endif
308259268Spfg
309259268Spfg
310259268Spfgbool
311259268Spfgshould_emit_struct_debug (tree type, enum debug_info_usage usage)
312259268Spfg{
313259268Spfg  enum debug_struct_file criterion;
314259268Spfg  tree type_decl;
315259268Spfg  bool generic = lang_hooks.types.generic_p (type);
316259268Spfg
317259268Spfg  if (generic)
318259268Spfg    criterion = debug_struct_generic[usage];
319259268Spfg  else
320259268Spfg    criterion = debug_struct_ordinary[usage];
321259268Spfg
322259268Spfg  if (criterion == DINFO_STRUCT_FILE_NONE)
323259268Spfg    return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
324259268Spfg  if (criterion == DINFO_STRUCT_FILE_ANY)
325259268Spfg    return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
326259268Spfg
327259268Spfg  type_decl = TYPE_STUB_DECL (type);
328259268Spfg
329259268Spfg  if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
330259268Spfg    return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
331259268Spfg
332259268Spfg  if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
333259268Spfg    return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
334259268Spfg  return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
335259268Spfg}
336259268Spfg
337132718Skan/* Nonzero means use GNU-only extensions in the generated symbolic
338132718Skan   debugging information.  Currently, this only has an effect when
339132718Skan   write_symbols is set to DBX_DEBUG, XCOFF_DEBUG, or DWARF_DEBUG.  */
340132718Skanbool use_gnu_debug_info_extensions;
341132718Skan
342169703Skan/* The default visibility for all symbols (unless overridden) */
343169703Skanenum symbol_visibility default_visibility = VISIBILITY_DEFAULT;
344169703Skan
345169703Skan/* Disable unit-at-a-time for frontends that might be still broken in this
346169703Skan   respect.  */
347169703Skan
348169703Skanbool no_unit_at_a_time_default;
349169703Skan
350169703Skan/* Global visibility options.  */
351169703Skanstruct visibility_flags visibility_options;
352169703Skan
353132718Skan/* Columns of --help display.  */
354132718Skanstatic unsigned int columns = 80;
355132718Skan
356132718Skan/* What to print when a switch has no documentation.  */
357132718Skanstatic const char undocumented_msg[] = N_("This switch lacks documentation");
358132718Skan
359132718Skan/* Used for bookkeeping on whether user set these flags so
360132718Skan   -fprofile-use/-fprofile-generate does not use them.  */
361132718Skanstatic bool profile_arc_flag_set, flag_profile_values_set;
362132718Skanstatic bool flag_unroll_loops_set, flag_tracer_set;
363132718Skanstatic bool flag_value_profile_transformations_set;
364132718Skanstatic bool flag_peel_loops_set, flag_branch_probabilities_set;
365132718Skan
366259405Spfg/* Functions excluded from profiling.  */
367259405Spfg
368259405Spfgtypedef char *char_p; /* For DEF_VEC_P.  */
369259405SpfgDEF_VEC_P(char_p);
370259405SpfgDEF_VEC_ALLOC_P(char_p,heap);
371259405Spfg
372259405Spfgstatic VEC(char_p,heap) *flag_instrument_functions_exclude_functions;
373259405Spfgstatic VEC(char_p,heap) *flag_instrument_functions_exclude_files;
374259405Spfg
375132718Skan/* Input file names.  */
376132718Skanconst char **in_fnames;
377132718Skanunsigned num_in_fnames;
378132718Skan
379169703Skanstatic int common_handle_option (size_t scode, const char *arg, int value,
380169703Skan				 unsigned int lang_mask);
381132718Skanstatic void handle_param (const char *);
382132718Skanstatic void set_Wextra (int);
383132718Skanstatic unsigned int handle_option (const char **argv, unsigned int lang_mask);
384132718Skanstatic char *write_langs (unsigned int lang_mask);
385132718Skanstatic void complain_wrong_lang (const char *, const struct cl_option *,
386132718Skan				 unsigned int lang_mask);
387132718Skanstatic void handle_options (unsigned int, const char **, unsigned int);
388132718Skanstatic void wrap_help (const char *help, const char *item, unsigned int);
389169703Skanstatic void print_target_help (void);
390132718Skanstatic void print_help (void);
391132718Skanstatic void print_param_help (void);
392169703Skanstatic void print_filtered_help (unsigned int);
393132718Skanstatic unsigned int print_switch (const char *text, unsigned int indent);
394132718Skanstatic void set_debug_level (enum debug_info_type type, int extended,
395132718Skan			     const char *arg);
396132718Skan
397132718Skan/* If ARG is a non-negative integer made up solely of digits, return its
398132718Skan   value, otherwise return -1.  */
399132718Skanstatic int
400132718Skanintegral_argument (const char *arg)
401132718Skan{
402132718Skan  const char *p = arg;
403132718Skan
404132718Skan  while (*p && ISDIGIT (*p))
405132718Skan    p++;
406132718Skan
407132718Skan  if (*p == '\0')
408132718Skan    return atoi (arg);
409132718Skan
410132718Skan  return -1;
411132718Skan}
412132718Skan
413132718Skan/* Return a malloced slash-separated list of languages in MASK.  */
414132718Skanstatic char *
415132718Skanwrite_langs (unsigned int mask)
416132718Skan{
417132718Skan  unsigned int n = 0, len = 0;
418132718Skan  const char *lang_name;
419132718Skan  char *result;
420132718Skan
421132718Skan  for (n = 0; (lang_name = lang_names[n]) != 0; n++)
422132718Skan    if (mask & (1U << n))
423132718Skan      len += strlen (lang_name) + 1;
424132718Skan
425169703Skan  result = XNEWVEC (char, len);
426132718Skan  len = 0;
427132718Skan  for (n = 0; (lang_name = lang_names[n]) != 0; n++)
428132718Skan    if (mask & (1U << n))
429132718Skan      {
430132718Skan	if (len)
431132718Skan	  result[len++] = '/';
432132718Skan	strcpy (result + len, lang_name);
433132718Skan	len += strlen (lang_name);
434132718Skan      }
435132718Skan
436132718Skan  result[len] = 0;
437132718Skan
438132718Skan  return result;
439132718Skan}
440132718Skan
441132718Skan/* Complain that switch OPT_INDEX does not apply to this front end.  */
442132718Skanstatic void
443132718Skancomplain_wrong_lang (const char *text, const struct cl_option *option,
444132718Skan		     unsigned int lang_mask)
445132718Skan{
446132718Skan  char *ok_langs, *bad_lang;
447132718Skan
448132718Skan  ok_langs = write_langs (option->flags);
449132718Skan  bad_lang = write_langs (lang_mask);
450132718Skan
451132718Skan  /* Eventually this should become a hard error IMO.  */
452169703Skan  warning (0, "command line option \"%s\" is valid for %s but not for %s",
453132718Skan	   text, ok_langs, bad_lang);
454132718Skan
455132718Skan  free (ok_langs);
456132718Skan  free (bad_lang);
457132718Skan}
458132718Skan
459132718Skan/* Handle the switch beginning at ARGV for the language indicated by
460132718Skan   LANG_MASK.  Returns the number of switches consumed.  */
461132718Skanstatic unsigned int
462132718Skanhandle_option (const char **argv, unsigned int lang_mask)
463132718Skan{
464132718Skan  size_t opt_index;
465132718Skan  const char *opt, *arg = 0;
466132718Skan  char *dup = 0;
467132718Skan  int value = 1;
468132718Skan  unsigned int result = 0;
469132718Skan  const struct cl_option *option;
470132718Skan
471132718Skan  opt = argv[0];
472132718Skan
473169703Skan  opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
474169703Skan  if (opt_index == cl_options_count
475169703Skan      && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
476132718Skan      && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
477132718Skan    {
478169703Skan      /* Drop the "no-" from negative switches.  */
479132718Skan      size_t len = strlen (opt) - 3;
480132718Skan
481169703Skan      dup = XNEWVEC (char, len + 1);
482132718Skan      dup[0] = '-';
483132718Skan      dup[1] = opt[1];
484132718Skan      memcpy (dup + 2, opt + 5, len - 2 + 1);
485132718Skan      opt = dup;
486132718Skan      value = 0;
487169703Skan      opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
488132718Skan    }
489132718Skan
490132718Skan  if (opt_index == cl_options_count)
491132718Skan    goto done;
492132718Skan
493132718Skan  option = &cl_options[opt_index];
494132718Skan
495132718Skan  /* Reject negative form of switches that don't take negatives as
496132718Skan     unrecognized.  */
497132718Skan  if (!value && (option->flags & CL_REJECT_NEGATIVE))
498132718Skan    goto done;
499132718Skan
500132718Skan  /* We've recognized this switch.  */
501132718Skan  result = 1;
502132718Skan
503169703Skan  /* Check to see if the option is disabled for this configuration.  */
504169703Skan  if (option->flags & CL_DISABLED)
505169703Skan    {
506169703Skan      error ("command line option %qs"
507169703Skan	     " is not supported by this configuration", opt);
508169703Skan      goto done;
509169703Skan    }
510169703Skan
511132718Skan  /* Sort out any argument the switch takes.  */
512132718Skan  if (option->flags & CL_JOINED)
513132718Skan    {
514132718Skan      /* Have arg point to the original switch.  This is because
515132718Skan	 some code, such as disable_builtin_function, expects its
516132718Skan	 argument to be persistent until the program exits.  */
517132718Skan      arg = argv[0] + cl_options[opt_index].opt_len + 1;
518132718Skan      if (!value)
519132718Skan	arg += strlen ("no-");
520132718Skan
521132718Skan      if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
522132718Skan	{
523132718Skan	  if (option->flags & CL_SEPARATE)
524132718Skan	    {
525132718Skan	      arg = argv[1];
526132718Skan	      result = 2;
527132718Skan	    }
528132718Skan	  else
529132718Skan	    /* Missing argument.  */
530132718Skan	    arg = NULL;
531132718Skan	}
532132718Skan    }
533132718Skan  else if (option->flags & CL_SEPARATE)
534132718Skan    {
535132718Skan      arg = argv[1];
536132718Skan      result = 2;
537132718Skan    }
538132718Skan
539132718Skan  /* Now we've swallowed any potential argument, complain if this
540132718Skan     is a switch for a different front end.  */
541169703Skan  if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
542132718Skan    {
543132718Skan      complain_wrong_lang (argv[0], option, lang_mask);
544132718Skan      goto done;
545132718Skan    }
546132718Skan
547132718Skan  if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
548132718Skan    {
549169703Skan      if (!lang_hooks.missing_argument (opt, opt_index))
550132718Skan	error ("missing argument to \"%s\"", opt);
551132718Skan      goto done;
552132718Skan    }
553132718Skan
554132718Skan  /* If the switch takes an integer, convert it.  */
555132718Skan  if (arg && (option->flags & CL_UINTEGER))
556132718Skan    {
557132718Skan      value = integral_argument (arg);
558132718Skan      if (value == -1)
559132718Skan	{
560132718Skan	  error ("argument to \"%s\" should be a non-negative integer",
561132718Skan		 option->opt_text);
562132718Skan	  goto done;
563132718Skan	}
564132718Skan    }
565132718Skan
566169703Skan  if (option->flag_var)
567169703Skan    switch (option->var_type)
568169703Skan      {
569169703Skan      case CLVC_BOOLEAN:
570169703Skan	*(int *) option->flag_var = value;
571169703Skan	break;
572169703Skan
573169703Skan      case CLVC_EQUAL:
574169703Skan	*(int *) option->flag_var = (value
575169703Skan				     ? option->var_value
576169703Skan				     : !option->var_value);
577169703Skan	break;
578169703Skan
579169703Skan      case CLVC_BIT_CLEAR:
580169703Skan      case CLVC_BIT_SET:
581169703Skan	if ((value != 0) == (option->var_type == CLVC_BIT_SET))
582169703Skan	  *(int *) option->flag_var |= option->var_value;
583169703Skan	else
584169703Skan	  *(int *) option->flag_var &= ~option->var_value;
585169703Skan	if (option->flag_var == &target_flags)
586169703Skan	  target_flags_explicit |= option->var_value;
587169703Skan	break;
588169703Skan
589169703Skan      case CLVC_STRING:
590169703Skan	*(const char **) option->flag_var = arg;
591169703Skan	break;
592169703Skan      }
593169703Skan
594132718Skan  if (option->flags & lang_mask)
595169703Skan    if (lang_hooks.handle_option (opt_index, arg, value) == 0)
596132718Skan      result = 0;
597132718Skan
598132718Skan  if (result && (option->flags & CL_COMMON))
599169703Skan    if (common_handle_option (opt_index, arg, value, lang_mask) == 0)
600132718Skan      result = 0;
601132718Skan
602169703Skan  if (result && (option->flags & CL_TARGET))
603169703Skan    if (!targetm.handle_option (opt_index, arg, value))
604169703Skan      result = 0;
605169703Skan
606132718Skan done:
607132718Skan  if (dup)
608132718Skan    free (dup);
609132718Skan  return result;
610132718Skan}
611132718Skan
612169703Skan/* Handle FILENAME from the command line.  */
613169703Skanstatic void
614169703Skanadd_input_filename (const char *filename)
615169703Skan{
616169703Skan  num_in_fnames++;
617169703Skan  in_fnames = xrealloc (in_fnames, num_in_fnames * sizeof (in_fnames[0]));
618169703Skan  in_fnames[num_in_fnames - 1] = filename;
619169703Skan}
620169703Skan
621259405Spfg/* Add functions or file names to a vector of names to exclude from
622259405Spfg   instrumentation.  */
623259405Spfg
624259405Spfgstatic void
625259405Spfgadd_instrument_functions_exclude_list (VEC(char_p,heap) **pvec,
626259405Spfg				       const char* arg)
627259405Spfg{
628259405Spfg  char *tmp;
629259405Spfg  char *r;
630259405Spfg  char *w;
631259405Spfg  char *token_start;
632259405Spfg
633259405Spfg  /* We never free this string.  */
634259405Spfg  tmp = xstrdup (arg);
635259405Spfg
636259405Spfg  r = tmp;
637259405Spfg  w = tmp;
638259405Spfg  token_start = tmp;
639259405Spfg
640259405Spfg  while (*r != '\0')
641259405Spfg    {
642259405Spfg      if (*r == ',')
643259405Spfg	{
644259405Spfg	  *w++ = '\0';
645259405Spfg	  ++r;
646259405Spfg	  VEC_safe_push (char_p, heap, *pvec, token_start);
647259405Spfg	  token_start = w;
648259405Spfg	}
649259405Spfg      if (*r == '\\' && r[1] == ',')
650259405Spfg	{
651259405Spfg	  *w++ = ',';
652259405Spfg	  r += 2;
653259405Spfg	}
654259405Spfg      else
655259405Spfg	*w++ = *r++;
656259405Spfg    }
657259405Spfg  if (*token_start != '\0')
658259405Spfg    VEC_safe_push (char_p, heap, *pvec, token_start);
659259405Spfg}
660259405Spfg
661259405Spfg/* Return whether we should exclude FNDECL from instrumentation.  */
662259405Spfg
663259405Spfgbool
664259405Spfgflag_instrument_functions_exclude_p (tree fndecl)
665259405Spfg{
666259405Spfg  if (VEC_length (char_p, flag_instrument_functions_exclude_functions) > 0)
667259405Spfg    {
668259405Spfg      const char *name;
669259405Spfg      int i;
670259405Spfg      char *s;
671259405Spfg
672259405Spfg      name = lang_hooks.decl_printable_name (fndecl, 0);
673259405Spfg      for (i = 0;
674259405Spfg	   VEC_iterate (char_p, flag_instrument_functions_exclude_functions,
675259405Spfg			i, s);
676259405Spfg	   ++i)
677259405Spfg	{
678259405Spfg	  if (strstr (name, s) != NULL)
679259405Spfg	    return true;
680259405Spfg	}
681259405Spfg    }
682259405Spfg
683259405Spfg  if (VEC_length (char_p, flag_instrument_functions_exclude_files) > 0)
684259405Spfg    {
685259405Spfg      const char *name;
686259405Spfg      int i;
687259405Spfg      char *s;
688259405Spfg
689259405Spfg      name = DECL_SOURCE_FILE (fndecl);
690259405Spfg      for (i = 0;
691259405Spfg	   VEC_iterate (char_p, flag_instrument_functions_exclude_files, i, s);
692259405Spfg	   ++i)
693259405Spfg	{
694259405Spfg	  if (strstr (name, s) != NULL)
695259405Spfg	    return true;
696259405Spfg	}
697259405Spfg    }
698259405Spfg
699259405Spfg  return false;
700259405Spfg}
701259405Spfg
702132718Skan/* Decode and handle the vector of command line options.  LANG_MASK
703132718Skan   contains has a single bit set representing the current
704132718Skan   language.  */
705132718Skanstatic void
706132718Skanhandle_options (unsigned int argc, const char **argv, unsigned int lang_mask)
707132718Skan{
708132718Skan  unsigned int n, i;
709132718Skan
710132718Skan  for (i = 1; i < argc; i += n)
711132718Skan    {
712132718Skan      const char *opt = argv[i];
713132718Skan
714132718Skan      /* Interpret "-" or a non-switch as a file name.  */
715132718Skan      if (opt[0] != '-' || opt[1] == '\0')
716132718Skan	{
717132718Skan	  if (main_input_filename == NULL)
718259268Spfg	    {
719132718Skan	    main_input_filename = opt;
720259268Spfg	      main_input_baselength
721259268Spfg		= base_of_path (main_input_filename, &main_input_basename);
722259268Spfg	    }
723132718Skan	  add_input_filename (opt);
724132718Skan	  n = 1;
725132718Skan	  continue;
726132718Skan	}
727132718Skan
728132718Skan      n = handle_option (argv + i, lang_mask);
729132718Skan
730132718Skan      if (!n)
731132718Skan	{
732132718Skan	  n = 1;
733132718Skan	  error ("unrecognized command line option \"%s\"", opt);
734132718Skan	}
735132718Skan    }
736132718Skan}
737132718Skan
738132718Skan/* Parse command line options and set default flag values.  Do minimal
739132718Skan   options processing.  */
740132718Skanvoid
741132718Skandecode_options (unsigned int argc, const char **argv)
742132718Skan{
743132718Skan  unsigned int i, lang_mask;
744132718Skan
745132718Skan  /* Perform language-specific options initialization.  */
746169703Skan  lang_mask = lang_hooks.init_options (argc, argv);
747132718Skan
748132718Skan  lang_hooks.initialize_diagnostics (global_dc);
749132718Skan
750132718Skan  /* Scan to see what optimization level has been specified.  That will
751132718Skan     determine the default value of many flags.  */
752132718Skan  for (i = 1; i < argc; i++)
753132718Skan    {
754132718Skan      if (!strcmp (argv[i], "-O"))
755132718Skan	{
756132718Skan	  optimize = 1;
757132718Skan	  optimize_size = 0;
758132718Skan	}
759132718Skan      else if (argv[i][0] == '-' && argv[i][1] == 'O')
760132718Skan	{
761132718Skan	  /* Handle -Os, -O2, -O3, -O69, ...  */
762132718Skan	  const char *p = &argv[i][2];
763132718Skan
764132718Skan	  if ((p[0] == 's') && (p[1] == 0))
765132718Skan	    {
766132718Skan	      optimize_size = 1;
767132718Skan
768132718Skan	      /* Optimizing for size forces optimize to be 2.  */
769132718Skan	      optimize = 2;
770132718Skan	    }
771132718Skan	  else
772132718Skan	    {
773132718Skan	      const int optimize_val = read_integral_parameter (p, p - 2, -1);
774132718Skan	      if (optimize_val != -1)
775132718Skan		{
776132718Skan		  optimize = optimize_val;
777132718Skan		  optimize_size = 0;
778132718Skan		}
779132718Skan	    }
780132718Skan	}
781132718Skan    }
782132718Skan
783132718Skan  if (!optimize)
784132718Skan    {
785132718Skan      flag_merge_constants = 0;
786132718Skan    }
787132718Skan
788132718Skan  if (optimize >= 1)
789132718Skan    {
790132718Skan      flag_defer_pop = 1;
791132718Skan#ifdef DELAY_SLOTS
792132718Skan      flag_delayed_branch = 1;
793132718Skan#endif
794132718Skan#ifdef CAN_DEBUG_WITHOUT_FP
795132718Skan      flag_omit_frame_pointer = 1;
796132718Skan#endif
797132718Skan      flag_guess_branch_prob = 1;
798132718Skan      flag_cprop_registers = 1;
799132718Skan      flag_if_conversion = 1;
800132718Skan      flag_if_conversion2 = 1;
801169703Skan      flag_ipa_pure_const = 1;
802169703Skan      flag_ipa_reference = 1;
803169703Skan      flag_tree_ccp = 1;
804169703Skan      flag_tree_dce = 1;
805169703Skan      flag_tree_dom = 1;
806169703Skan      flag_tree_dse = 1;
807169703Skan      flag_tree_ter = 1;
808169703Skan      flag_tree_live_range_split = 1;
809169703Skan      flag_tree_sra = 1;
810169703Skan      flag_tree_copyrename = 1;
811169703Skan      flag_tree_fre = 1;
812169703Skan      flag_tree_copy_prop = 1;
813169703Skan      flag_tree_sink = 1;
814169703Skan      flag_tree_salias = 1;
815169703Skan      if (!no_unit_at_a_time_default)
816169703Skan        flag_unit_at_a_time = 1;
817169703Skan
818169703Skan      if (!optimize_size)
819169703Skan	{
820169703Skan	  /* Loop header copying usually increases size of the code.  This used
821169703Skan	     not to be true, since quite often it is possible to verify that
822169703Skan	     the condition is satisfied in the first iteration and therefore
823169703Skan	     to eliminate it.  Jump threading handles these cases now.  */
824169703Skan	  flag_tree_ch = 1;
825169703Skan	}
826132718Skan    }
827132718Skan
828132718Skan  if (optimize >= 2)
829132718Skan    {
830169703Skan      flag_thread_jumps = 1;
831132718Skan      flag_crossjumping = 1;
832132718Skan      flag_optimize_sibling_calls = 1;
833132718Skan      flag_cse_follow_jumps = 1;
834132718Skan      flag_cse_skip_blocks = 1;
835132718Skan      flag_gcse = 1;
836132718Skan      flag_expensive_optimizations = 1;
837169703Skan      flag_ipa_type_escape = 1;
838132718Skan      flag_rerun_cse_after_loop = 1;
839132718Skan      flag_caller_saves = 1;
840132718Skan      flag_peephole2 = 1;
841132718Skan#ifdef INSN_SCHEDULING
842132718Skan      flag_schedule_insns = 1;
843132718Skan      flag_schedule_insns_after_reload = 1;
844132718Skan#endif
845132718Skan      flag_regmove = 1;
846132718Skan      flag_strict_aliasing = 1;
847169703Skan      flag_strict_overflow = 1;
848132718Skan      flag_delete_null_pointer_checks = 1;
849132718Skan      flag_reorder_blocks = 1;
850132718Skan      flag_reorder_functions = 1;
851169703Skan      flag_tree_store_ccp = 1;
852169703Skan      flag_tree_store_copy_prop = 1;
853172419Sobrien      /* XXX: some issues with ports have been traced to -ftree-vrp.
854172419Sobrien         So remove it from -O2 and above.  Note that jdk1{5,6} are affected
855172419Sobrien         and they build with w/-O3 - so we cannot just move it to -O3. */
856252176Spfg      /* flag_tree_vrp = 1; // See GCC tree-optimization/33099 */
857169703Skan
858169703Skan      if (!optimize_size)
859169703Skan	{
860169703Skan          /* PRE tends to generate bigger code.  */
861169703Skan          flag_tree_pre = 1;
862169703Skan	}
863132718Skan    }
864132718Skan
865132718Skan  if (optimize >= 3)
866132718Skan    {
867132718Skan      flag_inline_functions = 1;
868132718Skan      flag_unswitch_loops = 1;
869169703Skan      flag_gcse_after_reload = 1;
870132718Skan    }
871132718Skan
872132735Skan  if (optimize_size)
873132718Skan    {
874132718Skan      align_loops = 1;
875132718Skan      align_jumps = 1;
876132718Skan      align_labels = 1;
877132718Skan      align_functions = 1;
878132718Skan
879132718Skan      /* Don't reorder blocks when optimizing for size because extra
880132718Skan	 jump insns may be created; also barrier may create extra padding.
881132718Skan
882132718Skan	 More correctly we should have a block reordering mode that tried
883132718Skan	 to minimize the combined size of all the jumps.  This would more
884132718Skan	 or less automatically remove extra jumps, but would also try to
885132718Skan	 use more short jumps instead of long jumps.  */
886132718Skan      flag_reorder_blocks = 0;
887169703Skan      flag_reorder_blocks_and_partition = 0;
888132718Skan    }
889132718Skan
890169703Skan  if (optimize_size)
891169703Skan    {
892169703Skan      /* Inlining of very small functions usually reduces total size.  */
893169703Skan      set_param_value ("max-inline-insns-single", 5);
894169703Skan      set_param_value ("max-inline-insns-auto", 5);
895169703Skan      flag_inline_functions = 1;
896169703Skan
897169703Skan      /* We want to crossjump as much as possible.  */
898169703Skan      set_param_value ("min-crossjump-insns", 1);
899169703Skan    }
900169703Skan
901132718Skan  /* Initialize whether `char' is signed.  */
902132718Skan  flag_signed_char = DEFAULT_SIGNED_CHAR;
903169703Skan  /* Set this to a special "uninitialized" value.  The actual default is set
904169703Skan     after target options have been processed.  */
905169703Skan  flag_short_enums = 2;
906132718Skan
907132718Skan  /* Initialize target_flags before OPTIMIZATION_OPTIONS so the latter can
908132718Skan     modify it.  */
909169703Skan  target_flags = targetm.default_target_flags;
910132718Skan
911169703Skan  /* Some tagets have ABI-specified unwind tables.  */
912169703Skan  flag_unwind_tables = targetm.unwind_tables_default;
913132718Skan
914132718Skan#ifdef OPTIMIZATION_OPTIONS
915132718Skan  /* Allow default optimizations to be specified on a per-machine basis.  */
916132718Skan  OPTIMIZATION_OPTIONS (optimize, optimize_size);
917132718Skan#endif
918132718Skan
919132718Skan  handle_options (argc, argv, lang_mask);
920132718Skan
921132718Skan  if (flag_pie)
922132718Skan    flag_pic = flag_pie;
923132718Skan  if (flag_pic && !flag_pie)
924132718Skan    flag_shlib = 1;
925132718Skan
926132718Skan  if (flag_no_inline == 2)
927132718Skan    flag_no_inline = 0;
928132718Skan  else
929132718Skan    flag_really_no_inline = flag_no_inline;
930132718Skan
931132718Skan  /* Set flag_no_inline before the post_options () hook.  The C front
932132718Skan     ends use it to determine tree inlining defaults.  FIXME: such
933132718Skan     code should be lang-independent when all front ends use tree
934132718Skan     inlining, in which case it, and this condition, should be moved
935132718Skan     to the top of process_options() instead.  */
936132718Skan  if (optimize == 0)
937132718Skan    {
938132718Skan      /* Inlining does not work if not optimizing,
939132718Skan	 so force it not to be done.  */
940132718Skan      flag_no_inline = 1;
941132718Skan      warn_inline = 0;
942132718Skan
943132718Skan      /* The c_decode_option function and decode_option hook set
944132718Skan	 this to `2' if -Wall is used, so we can avoid giving out
945132718Skan	 lots of errors for people who don't realize what -Wall does.  */
946132718Skan      if (warn_uninitialized == 1)
947169703Skan	warning (OPT_Wuninitialized,
948169703Skan		 "-Wuninitialized is not supported without -O");
949132718Skan    }
950132718Skan
951132718Skan  if (flag_really_no_inline == 2)
952132718Skan    flag_really_no_inline = flag_no_inline;
953169703Skan
954169703Skan  /* The optimization to partition hot and cold basic blocks into separate
955169703Skan     sections of the .o and executable files does not work (currently)
956169703Skan     with exception handling.  This is because there is no support for
957169703Skan     generating unwind info.  If flag_exceptions is turned on we need to
958169703Skan     turn off the partitioning optimization.  */
959169703Skan
960169703Skan  if (flag_exceptions && flag_reorder_blocks_and_partition)
961169703Skan    {
962169703Skan      inform
963169703Skan	    ("-freorder-blocks-and-partition does not work with exceptions");
964169703Skan      flag_reorder_blocks_and_partition = 0;
965169703Skan      flag_reorder_blocks = 1;
966169703Skan    }
967169703Skan
968169703Skan  /* If user requested unwind info, then turn off the partitioning
969169703Skan     optimization.  */
970169703Skan
971169703Skan  if (flag_unwind_tables && ! targetm.unwind_tables_default
972169703Skan      && flag_reorder_blocks_and_partition)
973169703Skan    {
974169703Skan      inform ("-freorder-blocks-and-partition does not support unwind info");
975169703Skan      flag_reorder_blocks_and_partition = 0;
976169703Skan      flag_reorder_blocks = 1;
977169703Skan    }
978169703Skan
979169703Skan  /* If the target requested unwind info, then turn off the partitioning
980169703Skan     optimization with a different message.  Likewise, if the target does not
981169703Skan     support named sections.  */
982169703Skan
983169703Skan  if (flag_reorder_blocks_and_partition
984169703Skan      && (!targetm.have_named_sections
985169703Skan	  || (flag_unwind_tables && targetm.unwind_tables_default)))
986169703Skan    {
987169703Skan      inform
988169703Skan       ("-freorder-blocks-and-partition does not work on this architecture");
989169703Skan      flag_reorder_blocks_and_partition = 0;
990169703Skan      flag_reorder_blocks = 1;
991169703Skan    }
992132718Skan}
993132718Skan
994132718Skan/* Handle target- and language-independent options.  Return zero to
995169703Skan   generate an "unknown option" message.  Only options that need
996169703Skan   extra handling need to be listed here; if you simply want
997169703Skan   VALUE assigned to a variable, it happens automatically.  */
998169703Skan
999132718Skanstatic int
1000169703Skancommon_handle_option (size_t scode, const char *arg, int value,
1001169703Skan		      unsigned int lang_mask)
1002132718Skan{
1003132718Skan  enum opt_code code = (enum opt_code) scode;
1004132718Skan
1005132718Skan  switch (code)
1006132718Skan    {
1007132718Skan    case OPT__help:
1008132718Skan      print_help ();
1009132718Skan      exit_after_options = true;
1010132718Skan      break;
1011132718Skan
1012132718Skan    case OPT__param:
1013132718Skan      handle_param (arg);
1014132718Skan      break;
1015132718Skan
1016132718Skan    case OPT__target_help:
1017169703Skan      print_target_help ();
1018132718Skan      exit_after_options = true;
1019132718Skan      break;
1020132718Skan
1021132718Skan    case OPT__version:
1022132718Skan      print_version (stderr, "");
1023132718Skan      exit_after_options = true;
1024132718Skan      break;
1025132718Skan
1026132718Skan    case OPT_G:
1027132718Skan      g_switch_value = value;
1028132718Skan      g_switch_set = true;
1029132718Skan      break;
1030132718Skan
1031132718Skan    case OPT_O:
1032132718Skan    case OPT_Os:
1033132718Skan      /* Currently handled in a prescan.  */
1034132718Skan      break;
1035132718Skan
1036132718Skan    case OPT_W:
1037132718Skan      /* For backward compatibility, -W is the same as -Wextra.  */
1038132718Skan      set_Wextra (value);
1039132718Skan      break;
1040132718Skan
1041169703Skan    case OPT_Werror_:
1042169703Skan      {
1043169703Skan	char *new_option;
1044169703Skan	int option_index;
1045169703Skan	new_option = XNEWVEC (char, strlen (arg) + 2);
1046169703Skan	new_option[0] = 'W';
1047169703Skan	strcpy (new_option+1, arg);
1048169703Skan	option_index = find_opt (new_option, lang_mask);
1049169703Skan	if (option_index == N_OPTS)
1050169703Skan	  {
1051169703Skan	    error ("-Werror=%s: No option -%s", arg, new_option);
1052169703Skan	  }
1053169703Skan	else
1054169703Skan	  {
1055169703Skan	    int kind = value ? DK_ERROR : DK_WARNING;
1056169703Skan	    diagnostic_classify_diagnostic (global_dc, option_index, kind);
1057132718Skan
1058169703Skan	    /* -Werror=foo implies -Wfoo.  */
1059169703Skan	    if (cl_options[option_index].var_type == CLVC_BOOLEAN
1060169703Skan		&& cl_options[option_index].flag_var
1061169703Skan		&& kind == DK_ERROR)
1062169703Skan	      *(int *) cl_options[option_index].flag_var = 1;
1063169703Skan	    free (new_option);
1064169703Skan	  }
1065169703Skan      }
1066132718Skan      break;
1067132718Skan
1068132718Skan    case OPT_Wextra:
1069132718Skan      set_Wextra (value);
1070132718Skan      break;
1071132718Skan
1072132718Skan    case OPT_Wlarger_than_:
1073132718Skan      larger_than_size = value;
1074132718Skan      warn_larger_than = value != -1;
1075132718Skan      break;
1076132718Skan
1077259405Spfg    case OPT_Wframe_larger_than_:
1078259405Spfg      frame_larger_than_size = value;
1079259405Spfg      warn_frame_larger_than = value != -1;
1080259405Spfg      break;
1081259405Spfg
1082132718Skan    case OPT_Wstrict_aliasing:
1083259405Spfg      set_warn_strict_aliasing (value);
1084259405Spfg      break;
1085259405Spfg
1086169703Skan    case OPT_Wstrict_aliasing_:
1087132718Skan      warn_strict_aliasing = value;
1088132718Skan      break;
1089132718Skan
1090169703Skan    case OPT_Wstrict_overflow:
1091169703Skan      warn_strict_overflow = (value
1092169703Skan			      ? (int) WARN_STRICT_OVERFLOW_CONDITIONAL
1093169703Skan			      : 0);
1094132718Skan      break;
1095132718Skan
1096169703Skan    case OPT_Wstrict_overflow_:
1097169703Skan      warn_strict_overflow = value;
1098132718Skan      break;
1099132718Skan
1100132718Skan    case OPT_Wunused:
1101132718Skan      set_Wunused (value);
1102132718Skan      break;
1103132718Skan
1104132718Skan    case OPT_aux_info:
1105132718Skan    case OPT_aux_info_:
1106132718Skan      aux_info_file_name = arg;
1107132718Skan      flag_gen_aux_info = 1;
1108132718Skan      break;
1109132718Skan
1110132718Skan    case OPT_auxbase:
1111132718Skan      aux_base_name = arg;
1112132718Skan      break;
1113132718Skan
1114132718Skan    case OPT_auxbase_strip:
1115132718Skan      {
1116132718Skan	char *tmp = xstrdup (arg);
1117132718Skan	strip_off_ending (tmp, strlen (tmp));
1118132718Skan	if (tmp[0])
1119132718Skan	  aux_base_name = tmp;
1120132718Skan      }
1121132718Skan      break;
1122132718Skan
1123132718Skan    case OPT_d:
1124132718Skan      decode_d_option (arg);
1125132718Skan      break;
1126132718Skan
1127132718Skan    case OPT_dumpbase:
1128132718Skan      dump_base_name = arg;
1129132718Skan      break;
1130132718Skan
1131132718Skan    case OPT_falign_functions_:
1132132718Skan      align_functions = value;
1133132718Skan      break;
1134132718Skan
1135132718Skan    case OPT_falign_jumps_:
1136132718Skan      align_jumps = value;
1137132718Skan      break;
1138132718Skan
1139132718Skan    case OPT_falign_labels_:
1140132718Skan      align_labels = value;
1141132718Skan      break;
1142132718Skan
1143132718Skan    case OPT_falign_loops_:
1144132718Skan      align_loops = value;
1145132718Skan      break;
1146132718Skan
1147132718Skan    case OPT_fbranch_probabilities:
1148132718Skan      flag_branch_probabilities_set = true;
1149132718Skan      break;
1150132718Skan
1151132718Skan    case OPT_fcall_used_:
1152132718Skan      fix_register (arg, 0, 1);
1153132718Skan      break;
1154132718Skan
1155132718Skan    case OPT_fcall_saved_:
1156132718Skan      fix_register (arg, 0, 0);
1157132718Skan      break;
1158132718Skan
1159132718Skan    case OPT_fdiagnostics_show_location_:
1160132718Skan      if (!strcmp (arg, "once"))
1161132718Skan	diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
1162132718Skan      else if (!strcmp (arg, "every-line"))
1163132718Skan	diagnostic_prefixing_rule (global_dc)
1164132718Skan	  = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
1165132718Skan      else
1166132718Skan	return 0;
1167132718Skan      break;
1168132718Skan
1169169703Skan    case OPT_fdiagnostics_show_option:
1170169703Skan      global_dc->show_option_requested = true;
1171132718Skan      break;
1172132718Skan
1173169703Skan    case OPT_fdump_:
1174169703Skan      if (!dump_switch_p (arg))
1175169703Skan	return 0;
1176132718Skan      break;
1177132718Skan
1178132718Skan    case OPT_ffast_math:
1179132718Skan      set_fast_math_flags (value);
1180132718Skan      break;
1181132718Skan
1182132718Skan    case OPT_ffixed_:
1183132718Skan      fix_register (arg, 1, 1);
1184132718Skan      break;
1185132718Skan
1186132718Skan    case OPT_finline_limit_:
1187132718Skan    case OPT_finline_limit_eq:
1188132718Skan      set_param_value ("max-inline-insns-single", value / 2);
1189132718Skan      set_param_value ("max-inline-insns-auto", value / 2);
1190132718Skan      break;
1191132718Skan
1192259405Spfg    case OPT_finstrument_functions_exclude_function_list_:
1193259405Spfg      add_instrument_functions_exclude_list
1194259405Spfg	(&flag_instrument_functions_exclude_functions, arg);
1195259405Spfg      break;
1196259405Spfg
1197259405Spfg    case OPT_finstrument_functions_exclude_file_list_:
1198259405Spfg      add_instrument_functions_exclude_list
1199259405Spfg	(&flag_instrument_functions_exclude_files, arg);
1200259405Spfg      break;
1201259405Spfg
1202132718Skan    case OPT_fmessage_length_:
1203132718Skan      pp_set_line_maximum_length (global_dc->printer, value);
1204132718Skan      break;
1205132718Skan
1206169703Skan    case OPT_fpack_struct_:
1207169703Skan      if (value <= 0 || (value & (value - 1)) || value > 16)
1208169703Skan	error("structure alignment must be a small power of two, not %d", value);
1209169703Skan      else
1210169703Skan	{
1211169703Skan	  initial_max_fld_align = value;
1212169703Skan	  maximum_field_alignment = value * BITS_PER_UNIT;
1213169703Skan	}
1214132718Skan      break;
1215132718Skan
1216132718Skan    case OPT_fpeel_loops:
1217132718Skan      flag_peel_loops_set = true;
1218132718Skan      break;
1219132718Skan
1220132718Skan    case OPT_fprofile_arcs:
1221132718Skan      profile_arc_flag_set = true;
1222132718Skan      break;
1223132718Skan
1224132718Skan    case OPT_fprofile_use:
1225132718Skan      if (!flag_branch_probabilities_set)
1226132718Skan        flag_branch_probabilities = value;
1227132718Skan      if (!flag_profile_values_set)
1228132718Skan        flag_profile_values = value;
1229132718Skan      if (!flag_unroll_loops_set)
1230132718Skan        flag_unroll_loops = value;
1231132718Skan      if (!flag_peel_loops_set)
1232132718Skan        flag_peel_loops = value;
1233132718Skan      if (!flag_tracer_set)
1234132718Skan        flag_tracer = value;
1235132718Skan      if (!flag_value_profile_transformations_set)
1236132718Skan        flag_value_profile_transformations = value;
1237132718Skan      break;
1238132718Skan
1239132718Skan    case OPT_fprofile_generate:
1240132718Skan      if (!profile_arc_flag_set)
1241132718Skan        profile_arc_flag = value;
1242132718Skan      if (!flag_profile_values_set)
1243132718Skan        flag_profile_values = value;
1244132718Skan      if (!flag_value_profile_transformations_set)
1245132718Skan        flag_value_profile_transformations = value;
1246132718Skan      break;
1247132718Skan
1248132718Skan    case OPT_fprofile_values:
1249132718Skan      flag_profile_values_set = true;
1250132718Skan      break;
1251132718Skan
1252169703Skan    case OPT_fvisibility_:
1253169703Skan      {
1254169703Skan        if (!strcmp(arg, "default"))
1255169703Skan          default_visibility = VISIBILITY_DEFAULT;
1256169703Skan        else if (!strcmp(arg, "internal"))
1257169703Skan          default_visibility = VISIBILITY_INTERNAL;
1258169703Skan        else if (!strcmp(arg, "hidden"))
1259169703Skan          default_visibility = VISIBILITY_HIDDEN;
1260169703Skan        else if (!strcmp(arg, "protected"))
1261169703Skan          default_visibility = VISIBILITY_PROTECTED;
1262169703Skan        else
1263169703Skan          error ("unrecognized visibility value \"%s\"", arg);
1264169703Skan      }
1265169703Skan      break;
1266169703Skan
1267132718Skan    case OPT_fvpt:
1268169703Skan      flag_value_profile_transformations_set = true;
1269132718Skan      break;
1270132718Skan
1271132718Skan    case OPT_frandom_seed:
1272132718Skan      /* The real switch is -fno-random-seed.  */
1273132718Skan      if (value)
1274132718Skan	return 0;
1275132718Skan      flag_random_seed = NULL;
1276132718Skan      break;
1277132718Skan
1278132718Skan    case OPT_frandom_seed_:
1279132718Skan      flag_random_seed = arg;
1280132718Skan      break;
1281132718Skan
1282132718Skan    case OPT_fsched_verbose_:
1283132718Skan#ifdef INSN_SCHEDULING
1284132718Skan      fix_sched_param ("verbose", arg);
1285132718Skan      break;
1286132718Skan#else
1287132718Skan      return 0;
1288132718Skan#endif
1289132718Skan
1290132718Skan    case OPT_fsched_stalled_insns_:
1291132718Skan      flag_sched_stalled_insns = value;
1292132718Skan      if (flag_sched_stalled_insns == 0)
1293132718Skan	flag_sched_stalled_insns = -1;
1294132718Skan      break;
1295132718Skan
1296132718Skan    case OPT_fsched_stalled_insns_dep_:
1297132718Skan      flag_sched_stalled_insns_dep = value;
1298132718Skan      break;
1299132718Skan
1300132718Skan    case OPT_fstack_limit:
1301132718Skan      /* The real switch is -fno-stack-limit.  */
1302132718Skan      if (value)
1303132718Skan	return 0;
1304132718Skan      stack_limit_rtx = NULL_RTX;
1305132718Skan      break;
1306132718Skan
1307132718Skan    case OPT_fstack_limit_register_:
1308132718Skan      {
1309132718Skan	int reg = decode_reg_name (arg);
1310132718Skan	if (reg < 0)
1311132718Skan	  error ("unrecognized register name \"%s\"", arg);
1312132718Skan	else
1313132718Skan	  stack_limit_rtx = gen_rtx_REG (Pmode, reg);
1314132718Skan      }
1315132718Skan      break;
1316132718Skan
1317132718Skan    case OPT_fstack_limit_symbol_:
1318132718Skan      stack_limit_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (arg));
1319132718Skan      break;
1320132718Skan
1321169703Skan    case OPT_ftree_vectorizer_verbose_:
1322169703Skan      vect_set_verbosity_level (arg);
1323132718Skan      break;
1324132718Skan
1325132718Skan    case OPT_ftls_model_:
1326132718Skan      if (!strcmp (arg, "global-dynamic"))
1327132718Skan	flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC;
1328132718Skan      else if (!strcmp (arg, "local-dynamic"))
1329132718Skan	flag_tls_default = TLS_MODEL_LOCAL_DYNAMIC;
1330132718Skan      else if (!strcmp (arg, "initial-exec"))
1331132718Skan	flag_tls_default = TLS_MODEL_INITIAL_EXEC;
1332132718Skan      else if (!strcmp (arg, "local-exec"))
1333132718Skan	flag_tls_default = TLS_MODEL_LOCAL_EXEC;
1334132718Skan      else
1335169703Skan	warning (0, "unknown tls-model \"%s\"", arg);
1336132718Skan      break;
1337132718Skan
1338132718Skan    case OPT_ftracer:
1339132718Skan      flag_tracer_set = true;
1340132718Skan      break;
1341132718Skan
1342132718Skan    case OPT_funroll_loops:
1343132718Skan      flag_unroll_loops_set = true;
1344132718Skan      break;
1345132718Skan
1346132718Skan    case OPT_g:
1347132718Skan      set_debug_level (NO_DEBUG, DEFAULT_GDB_EXTENSIONS, arg);
1348132718Skan      break;
1349132718Skan
1350132718Skan    case OPT_gcoff:
1351132718Skan      set_debug_level (SDB_DEBUG, false, arg);
1352132718Skan      break;
1353132718Skan
1354132718Skan    case OPT_gdwarf_2:
1355132718Skan      set_debug_level (DWARF2_DEBUG, false, arg);
1356132718Skan      break;
1357132718Skan
1358132718Skan    case OPT_ggdb:
1359132718Skan      set_debug_level (NO_DEBUG, 2, arg);
1360132718Skan      break;
1361132718Skan
1362132718Skan    case OPT_gstabs:
1363132718Skan    case OPT_gstabs_:
1364132718Skan      set_debug_level (DBX_DEBUG, code == OPT_gstabs_, arg);
1365132718Skan      break;
1366132718Skan
1367132718Skan    case OPT_gvms:
1368132718Skan      set_debug_level (VMS_DEBUG, false, arg);
1369132718Skan      break;
1370132718Skan
1371132718Skan    case OPT_gxcoff:
1372132718Skan    case OPT_gxcoff_:
1373132718Skan      set_debug_level (XCOFF_DEBUG, code == OPT_gxcoff_, arg);
1374132718Skan      break;
1375132718Skan
1376132718Skan    case OPT_o:
1377132718Skan      asm_file_name = arg;
1378132718Skan      break;
1379132718Skan
1380132718Skan    case OPT_pedantic_errors:
1381132718Skan      flag_pedantic_errors = pedantic = 1;
1382132718Skan      break;
1383132718Skan
1384169703Skan    case OPT_fforce_mem:
1385169703Skan      warning (0, "-f[no-]force-mem is nop and option will be removed in 4.3");
1386132718Skan      break;
1387132718Skan
1388169703Skan    case OPT_floop_optimize:
1389169703Skan    case OPT_frerun_loop_opt:
1390169703Skan    case OPT_fstrength_reduce:
1391169703Skan      /* These are no-ops, preserved for backward compatibility.  */
1392132718Skan      break;
1393132718Skan
1394169703Skan    default:
1395169703Skan      /* If the flag was handled in a standard way, assume the lack of
1396169703Skan	 processing here is intentional.  */
1397169703Skan      gcc_assert (cl_options[scode].flag_var);
1398169703Skan      break;
1399132718Skan    }
1400132718Skan
1401132718Skan  return 1;
1402132718Skan}
1403132718Skan
1404132718Skan/* Handle --param NAME=VALUE.  */
1405132718Skanstatic void
1406132718Skanhandle_param (const char *carg)
1407132718Skan{
1408132718Skan  char *equal, *arg;
1409132718Skan  int value;
1410132718Skan
1411132718Skan  arg = xstrdup (carg);
1412132718Skan  equal = strchr (arg, '=');
1413132718Skan  if (!equal)
1414132718Skan    error ("%s: --param arguments should be of the form NAME=VALUE", arg);
1415132718Skan  else
1416132718Skan    {
1417132718Skan      value = integral_argument (equal + 1);
1418132718Skan      if (value == -1)
1419169703Skan	error ("invalid --param value %qs", equal + 1);
1420132718Skan      else
1421132718Skan	{
1422132718Skan	  *equal = '\0';
1423132718Skan	  set_param_value (arg, value);
1424132718Skan	}
1425132718Skan    }
1426132718Skan
1427132718Skan  free (arg);
1428132718Skan}
1429132718Skan
1430132718Skan/* Handle -W and -Wextra.  */
1431132718Skanstatic void
1432132718Skanset_Wextra (int setting)
1433132718Skan{
1434132718Skan  extra_warnings = setting;
1435132718Skan  warn_unused_value = setting;
1436132718Skan  warn_unused_parameter = (setting && maybe_warn_unused_parameter);
1437132718Skan
1438132718Skan  /* We save the value of warn_uninitialized, since if they put
1439132718Skan     -Wuninitialized on the command line, we need to generate a
1440132718Skan     warning about not using it without also specifying -O.  */
1441132718Skan  if (setting == 0)
1442132718Skan    warn_uninitialized = 0;
1443132718Skan  else if (warn_uninitialized != 1)
1444132718Skan    warn_uninitialized = 2;
1445132718Skan}
1446132718Skan
1447132718Skan/* Initialize unused warning flags.  */
1448132718Skanvoid
1449132718Skanset_Wunused (int setting)
1450132718Skan{
1451132718Skan  warn_unused_function = setting;
1452132718Skan  warn_unused_label = setting;
1453132718Skan  /* Unused function parameter warnings are reported when either
1454132718Skan     ``-Wextra -Wunused'' or ``-Wunused-parameter'' is specified.
1455132718Skan     Thus, if -Wextra has already been seen, set warn_unused_parameter;
1456132718Skan     otherwise set maybe_warn_extra_parameter, which will be picked up
1457132718Skan     by set_Wextra.  */
1458132718Skan  maybe_warn_unused_parameter = setting;
1459132718Skan  warn_unused_parameter = (setting && extra_warnings);
1460132718Skan  warn_unused_variable = setting;
1461132718Skan  warn_unused_value = setting;
1462132718Skan}
1463132718Skan
1464259405Spfg/* Used to set the level of strict aliasing warnings,
1465259405Spfg   when no level is specified (i.e., when -Wstrict-aliasing, and not
1466259405Spfg   -Wstrict-aliasing=level was given).
1467259405Spfg   ONOFF is assumed to take value 1 when -Wstrict-aliasing is specified,
1468259405Spfg   and 0 otherwise.  After calling this function, wstrict_aliasing will be
1469259405Spfg   set to the default value of -Wstrict_aliasing=level, currently 3.  */
1470259405Spfgvoid
1471259405Spfgset_warn_strict_aliasing (int onoff)
1472259405Spfg{
1473259405Spfg  gcc_assert (onoff == 0 || onoff == 1);
1474259405Spfg  if (onoff != 0)
1475259405Spfg    warn_strict_aliasing = 3;
1476259405Spfg}
1477259405Spfg
1478132718Skan/* The following routines are useful in setting all the flags that
1479132718Skan   -ffast-math and -fno-fast-math imply.  */
1480132718Skanvoid
1481132718Skanset_fast_math_flags (int set)
1482132718Skan{
1483132718Skan  flag_trapping_math = !set;
1484132718Skan  flag_unsafe_math_optimizations = set;
1485132718Skan  flag_finite_math_only = set;
1486132718Skan  flag_errno_math = !set;
1487132718Skan  if (set)
1488132718Skan    {
1489132718Skan      flag_signaling_nans = 0;
1490132718Skan      flag_rounding_math = 0;
1491169703Skan      flag_cx_limited_range = 1;
1492132718Skan    }
1493132718Skan}
1494132718Skan
1495132718Skan/* Return true iff flags are set as if -ffast-math.  */
1496132718Skanbool
1497132718Skanfast_math_flags_set_p (void)
1498132718Skan{
1499132718Skan  return (!flag_trapping_math
1500132718Skan	  && flag_unsafe_math_optimizations
1501132718Skan	  && flag_finite_math_only
1502132718Skan	  && !flag_errno_math);
1503132718Skan}
1504132718Skan
1505132718Skan/* Handle a debug output -g switch.  EXTENDED is true or false to support
1506132718Skan   extended output (2 is special and means "-ggdb" was given).  */
1507132718Skanstatic void
1508132718Skanset_debug_level (enum debug_info_type type, int extended, const char *arg)
1509132718Skan{
1510132718Skan  static bool type_explicit;
1511132718Skan
1512132718Skan  use_gnu_debug_info_extensions = extended;
1513132718Skan
1514132718Skan  if (type == NO_DEBUG)
1515132718Skan    {
1516132718Skan      if (write_symbols == NO_DEBUG)
1517132718Skan	{
1518132718Skan	  write_symbols = PREFERRED_DEBUGGING_TYPE;
1519132718Skan
1520132718Skan	  if (extended == 2)
1521132718Skan	    {
1522132718Skan#ifdef DWARF2_DEBUGGING_INFO
1523132718Skan	      write_symbols = DWARF2_DEBUG;
1524132718Skan#elif defined DBX_DEBUGGING_INFO
1525132718Skan	      write_symbols = DBX_DEBUG;
1526132718Skan#endif
1527132718Skan	    }
1528132718Skan
1529132718Skan	  if (write_symbols == NO_DEBUG)
1530169703Skan	    warning (0, "target system does not support debug output");
1531132718Skan	}
1532132718Skan    }
1533132718Skan  else
1534132718Skan    {
1535132718Skan      /* Does it conflict with an already selected type?  */
1536132718Skan      if (type_explicit && write_symbols != NO_DEBUG && type != write_symbols)
1537132718Skan	error ("debug format \"%s\" conflicts with prior selection",
1538132718Skan	       debug_type_names[type]);
1539132718Skan      write_symbols = type;
1540132718Skan      type_explicit = true;
1541132718Skan    }
1542132718Skan
1543132718Skan  /* A debug flag without a level defaults to level 2.  */
1544132718Skan  if (*arg == '\0')
1545132718Skan    {
1546132718Skan      if (!debug_info_level)
1547132718Skan	debug_info_level = 2;
1548132718Skan    }
1549132718Skan  else
1550132718Skan    {
1551132718Skan      debug_info_level = integral_argument (arg);
1552132718Skan      if (debug_info_level == (unsigned int) -1)
1553132718Skan	error ("unrecognised debug output level \"%s\"", arg);
1554132718Skan      else if (debug_info_level > 3)
1555132718Skan	error ("debug output level %s is too high", arg);
1556132718Skan    }
1557132718Skan}
1558132718Skan
1559169703Skan/* Display help for target options.  */
1560169703Skanstatic void
1561169703Skanprint_target_help (void)
1562169703Skan{
1563169703Skan  unsigned int i;
1564169703Skan  static bool displayed = false;
1565169703Skan
1566169703Skan  /* Avoid double printing for --help --target-help.  */
1567169703Skan  if (displayed)
1568169703Skan    return;
1569169703Skan
1570169703Skan  displayed = true;
1571169703Skan  for (i = 0; i < cl_options_count; i++)
1572169703Skan    if ((cl_options[i].flags & (CL_TARGET | CL_UNDOCUMENTED)) == CL_TARGET)
1573169703Skan      {
1574169703Skan	printf (_("\nTarget specific options:\n"));
1575169703Skan	print_filtered_help (CL_TARGET);
1576169703Skan	break;
1577169703Skan      }
1578169703Skan}
1579169703Skan
1580132718Skan/* Output --help text.  */
1581132718Skanstatic void
1582132718Skanprint_help (void)
1583132718Skan{
1584132718Skan  size_t i;
1585132718Skan  const char *p;
1586132718Skan
1587132718Skan  GET_ENVIRONMENT (p, "COLUMNS");
1588132718Skan  if (p)
1589132718Skan    {
1590132718Skan      int value = atoi (p);
1591132718Skan      if (value > 0)
1592132718Skan	columns = value;
1593132718Skan    }
1594132718Skan
1595132718Skan  puts (_("The following options are language-independent:\n"));
1596132718Skan
1597132718Skan  print_filtered_help (CL_COMMON);
1598132718Skan  print_param_help ();
1599132718Skan
1600132718Skan  for (i = 0; lang_names[i]; i++)
1601132718Skan    {
1602132718Skan      printf (_("The %s front end recognizes the following options:\n\n"),
1603132718Skan	      lang_names[i]);
1604132718Skan      print_filtered_help (1U << i);
1605132718Skan    }
1606169703Skan  print_target_help ();
1607132718Skan}
1608132718Skan
1609132718Skan/* Print the help for --param.  */
1610132718Skanstatic void
1611132718Skanprint_param_help (void)
1612132718Skan{
1613132718Skan  size_t i;
1614132718Skan
1615132718Skan  puts (_("The --param option recognizes the following as parameters:\n"));
1616132718Skan
1617132718Skan  for (i = 0; i < LAST_PARAM; i++)
1618132718Skan    {
1619132718Skan      const char *help = compiler_params[i].help;
1620132718Skan      const char *param = compiler_params[i].option;
1621132718Skan
1622132718Skan      if (help == NULL || *help == '\0')
1623132718Skan	help = undocumented_msg;
1624132718Skan
1625132718Skan      /* Get the translation.  */
1626132718Skan      help = _(help);
1627132718Skan
1628132718Skan      wrap_help (help, param, strlen (param));
1629132718Skan    }
1630132718Skan
1631132718Skan  putchar ('\n');
1632132718Skan}
1633132718Skan
1634132718Skan/* Print help for a specific front-end, etc.  */
1635132718Skanstatic void
1636132718Skanprint_filtered_help (unsigned int flag)
1637132718Skan{
1638132718Skan  unsigned int i, len, filter, indent = 0;
1639132718Skan  bool duplicates = false;
1640132718Skan  const char *help, *opt, *tab;
1641132718Skan  static char *printed;
1642132718Skan
1643169703Skan  if (flag == CL_COMMON || flag == CL_TARGET)
1644132718Skan    {
1645132718Skan      filter = flag;
1646132718Skan      if (!printed)
1647132718Skan	printed = xmalloc (cl_options_count);
1648132718Skan      memset (printed, 0, cl_options_count);
1649132718Skan    }
1650132718Skan  else
1651132718Skan    {
1652132718Skan      /* Don't print COMMON options twice.  */
1653132718Skan      filter = flag | CL_COMMON;
1654132718Skan
1655132718Skan      for (i = 0; i < cl_options_count; i++)
1656132718Skan	{
1657132718Skan	  if ((cl_options[i].flags & filter) != flag)
1658132718Skan	    continue;
1659132718Skan
1660132718Skan	  /* Skip help for internal switches.  */
1661132718Skan	  if (cl_options[i].flags & CL_UNDOCUMENTED)
1662132718Skan	    continue;
1663132718Skan
1664132718Skan	  /* Skip switches that have already been printed, mark them to be
1665132718Skan	     listed later.  */
1666132718Skan	  if (printed[i])
1667132718Skan	    {
1668132718Skan	      duplicates = true;
1669132718Skan	      indent = print_switch (cl_options[i].opt_text, indent);
1670132718Skan	    }
1671132718Skan	}
1672132718Skan
1673132718Skan      if (duplicates)
1674132718Skan	{
1675132718Skan	  putchar ('\n');
1676132718Skan	  putchar ('\n');
1677132718Skan	}
1678132718Skan    }
1679132718Skan
1680132718Skan  for (i = 0; i < cl_options_count; i++)
1681132718Skan    {
1682132718Skan      if ((cl_options[i].flags & filter) != flag)
1683132718Skan	continue;
1684132718Skan
1685132718Skan      /* Skip help for internal switches.  */
1686132718Skan      if (cl_options[i].flags & CL_UNDOCUMENTED)
1687132718Skan	continue;
1688132718Skan
1689132718Skan      /* Skip switches that have already been printed.  */
1690132718Skan      if (printed[i])
1691132718Skan	continue;
1692132718Skan
1693132718Skan      printed[i] = true;
1694132718Skan
1695132718Skan      help = cl_options[i].help;
1696132718Skan      if (!help)
1697132718Skan	help = undocumented_msg;
1698132718Skan
1699132718Skan      /* Get the translation.  */
1700132718Skan      help = _(help);
1701132718Skan
1702132718Skan      tab = strchr (help, '\t');
1703132718Skan      if (tab)
1704132718Skan	{
1705132718Skan	  len = tab - help;
1706132718Skan	  opt = help;
1707132718Skan	  help = tab + 1;
1708132718Skan	}
1709132718Skan      else
1710132718Skan	{
1711132718Skan	  opt = cl_options[i].opt_text;
1712132718Skan	  len = strlen (opt);
1713132718Skan	}
1714132718Skan
1715132718Skan      wrap_help (help, opt, len);
1716132718Skan    }
1717132718Skan
1718132718Skan  putchar ('\n');
1719132718Skan}
1720132718Skan
1721132718Skan/* Output ITEM, of length ITEM_WIDTH, in the left column, followed by
1722132718Skan   word-wrapped HELP in a second column.  */
1723132718Skanstatic unsigned int
1724132718Skanprint_switch (const char *text, unsigned int indent)
1725132718Skan{
1726132718Skan  unsigned int len = strlen (text) + 1; /* trailing comma */
1727132718Skan
1728132718Skan  if (indent)
1729132718Skan    {
1730132718Skan      putchar (',');
1731132718Skan      if (indent + len > columns)
1732132718Skan	{
1733132718Skan	  putchar ('\n');
1734132718Skan	  putchar (' ');
1735132718Skan	  indent = 1;
1736132718Skan	}
1737132718Skan    }
1738132718Skan  else
1739132718Skan    putchar (' ');
1740132718Skan
1741132718Skan  putchar (' ');
1742132718Skan  fputs (text, stdout);
1743132718Skan
1744132718Skan  return indent + len + 1;
1745132718Skan}
1746132718Skan
1747132718Skan/* Output ITEM, of length ITEM_WIDTH, in the left column, followed by
1748132718Skan   word-wrapped HELP in a second column.  */
1749132718Skanstatic void
1750132718Skanwrap_help (const char *help, const char *item, unsigned int item_width)
1751132718Skan{
1752132718Skan  unsigned int col_width = 27;
1753132718Skan  unsigned int remaining, room, len;
1754132718Skan
1755132718Skan  remaining = strlen (help);
1756132718Skan
1757132718Skan  do
1758132718Skan    {
1759132718Skan      room = columns - 3 - MAX (col_width, item_width);
1760132718Skan      if (room > columns)
1761132718Skan	room = 0;
1762132718Skan      len = remaining;
1763132718Skan
1764132718Skan      if (room < len)
1765132718Skan	{
1766132718Skan	  unsigned int i;
1767132718Skan
1768132718Skan	  for (i = 0; help[i]; i++)
1769132718Skan	    {
1770132718Skan	      if (i >= room && len != remaining)
1771132718Skan		break;
1772132718Skan	      if (help[i] == ' ')
1773132718Skan		len = i;
1774132718Skan	      else if ((help[i] == '-' || help[i] == '/')
1775132718Skan		       && help[i + 1] != ' '
1776132718Skan		       && i > 0 && ISALPHA (help[i - 1]))
1777132718Skan		len = i + 1;
1778132718Skan	    }
1779132718Skan	}
1780132718Skan
1781132718Skan      printf( "  %-*.*s %.*s\n", col_width, item_width, item, len, help);
1782132718Skan      item_width = 0;
1783132718Skan      while (help[len] == ' ')
1784132718Skan	len++;
1785132718Skan      help += len;
1786132718Skan      remaining -= len;
1787132718Skan    }
1788132718Skan  while (remaining);
1789132718Skan}
1790169703Skan
1791169703Skan/* Return 1 if OPTION is enabled, 0 if it is disabled, or -1 if it isn't
1792169703Skan   a simple on-off switch.  */
1793169703Skan
1794169703Skanint
1795169703Skanoption_enabled (int opt_idx)
1796169703Skan{
1797169703Skan  const struct cl_option *option = &(cl_options[opt_idx]);
1798169703Skan  if (option->flag_var)
1799169703Skan    switch (option->var_type)
1800169703Skan      {
1801169703Skan      case CLVC_BOOLEAN:
1802169703Skan	return *(int *) option->flag_var != 0;
1803169703Skan
1804169703Skan      case CLVC_EQUAL:
1805169703Skan	return *(int *) option->flag_var == option->var_value;
1806169703Skan
1807169703Skan      case CLVC_BIT_CLEAR:
1808169703Skan	return (*(int *) option->flag_var & option->var_value) == 0;
1809169703Skan
1810169703Skan      case CLVC_BIT_SET:
1811169703Skan	return (*(int *) option->flag_var & option->var_value) != 0;
1812169703Skan
1813169703Skan      case CLVC_STRING:
1814169703Skan	break;
1815169703Skan      }
1816169703Skan  return -1;
1817169703Skan}
1818169703Skan
1819169703Skan/* Fill STATE with the current state of option OPTION.  Return true if
1820169703Skan   there is some state to store.  */
1821169703Skan
1822169703Skanbool
1823169703Skanget_option_state (int option, struct cl_option_state *state)
1824169703Skan{
1825169703Skan  if (cl_options[option].flag_var == 0)
1826169703Skan    return false;
1827169703Skan
1828169703Skan  switch (cl_options[option].var_type)
1829169703Skan    {
1830169703Skan    case CLVC_BOOLEAN:
1831169703Skan    case CLVC_EQUAL:
1832169703Skan      state->data = cl_options[option].flag_var;
1833169703Skan      state->size = sizeof (int);
1834169703Skan      break;
1835169703Skan
1836169703Skan    case CLVC_BIT_CLEAR:
1837169703Skan    case CLVC_BIT_SET:
1838169703Skan      state->ch = option_enabled (option);
1839169703Skan      state->data = &state->ch;
1840169703Skan      state->size = 1;
1841169703Skan      break;
1842169703Skan
1843169703Skan    case CLVC_STRING:
1844169703Skan      state->data = *(const char **) cl_options[option].flag_var;
1845169703Skan      if (state->data == 0)
1846169703Skan	state->data = "";
1847169703Skan      state->size = strlen (state->data) + 1;
1848169703Skan      break;
1849169703Skan    }
1850169703Skan  return true;
1851169703Skan}
1852