Imported Upstream version 2.9.0
[debian/cc1111] / support / cpp / opts.c
1 /* Command line option handling.
2    Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
3    Free Software Foundation, Inc.
4    Contributed by Neil Booth.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "intl.h"
26 #include "opts.h"
27 #include "options.h"
28
29 /* True if we should exit after parsing options.  */
30 bool exit_after_options;
31
32 /* Treat warnings as errors.  -Werror.  */
33 bool warnings_are_errors;
34
35 /* Don't suppress warnings from system headers.  -Wsystem-headers.  */
36 bool warn_system_headers;
37
38 /* Columns of --help display.  */
39 static unsigned int columns = 80;
40
41 /* What to print when a switch has no documentation.  */
42 static const char undocumented_msg[] = N_("This switch lacks documentation");
43
44 /* Input file names.  */
45 const char **in_fnames;
46 unsigned num_in_fnames;
47
48 static int common_handle_option (size_t scode, const char *arg, int value);
49 static unsigned int handle_option (const char **argv, unsigned int lang_mask);
50 static char *write_langs (unsigned int lang_mask);
51 static void complain_wrong_lang (const char *, const struct cl_option *,
52                                  unsigned int lang_mask);
53 static void handle_options (unsigned int, const char **, unsigned int);
54 static void wrap_help (const char *help, const char *item, unsigned int);
55 static void print_help (void);
56 static void print_filtered_help (unsigned int);
57 static unsigned int print_switch (const char *text, unsigned int indent);
58
59 /* If ARG is a non-negative integer made up solely of digits, return its
60    value, otherwise return -1.  */
61 static int
62 integral_argument (const char *arg)
63 {
64   const char *p = arg;
65
66   while (*p && ISDIGIT (*p))
67     p++;
68
69   if (*p == '\0')
70     return atoi (arg);
71
72   return -1;
73 }
74
75 /* Return a malloced slash-separated list of languages in MASK.  */
76 static char *
77 write_langs (unsigned int mask)
78 {
79   unsigned int n = 0, len = 0;
80   const char *lang_name;
81   char *result;
82
83   for (n = 0; (lang_name = lang_names[n]) != 0; n++)
84     if (mask & (1U << n))
85       len += strlen (lang_name) + 1;
86
87   result = XNEWVEC (char, len);
88   len = 0;
89   for (n = 0; (lang_name = lang_names[n]) != 0; n++)
90     if (mask & (1U << n))
91       {
92         if (len)
93           result[len++] = '/';
94         strcpy (result + len, lang_name);
95         len += strlen (lang_name);
96       }
97
98   result[len] = 0;
99
100   return result;
101 }
102
103 /* Complain that switch OPT_INDEX does not apply to this front end.  */
104 static void
105 complain_wrong_lang (const char *text, const struct cl_option *option,
106                      unsigned int lang_mask)
107 {
108   char *ok_langs, *bad_lang;
109
110   ok_langs = write_langs (option->flags);
111   bad_lang = write_langs (lang_mask);
112
113   /* Eventually this should become a hard error IMO.  */
114   warning (0, "command line option \"%s\" is valid for %s but not for %s",
115            text, ok_langs, bad_lang);
116
117   free (ok_langs);
118   free (bad_lang);
119 }
120
121 /* Handle the switch beginning at ARGV for the language indicated by
122    LANG_MASK.  Returns the number of switches consumed.  */
123 static unsigned int
124 handle_option (const char **argv, unsigned int lang_mask)
125 {
126   size_t opt_index;
127   const char *opt, *arg = 0;
128   char *dup = 0;
129   int value = 1;
130   unsigned int result = 0;
131   const struct cl_option *option;
132
133   opt = argv[0];
134
135   opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
136   if (opt_index == cl_options_count
137       && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
138       && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
139     {
140       /* Drop the "no-" from negative switches.  */
141       size_t len = strlen (opt) - 3;
142
143       dup = XNEWVEC (char, len + 1);
144       dup[0] = '-';
145       dup[1] = opt[1];
146       memcpy (dup + 2, opt + 5, len - 2 + 1);
147       opt = dup;
148       value = 0;
149       opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
150     }
151
152   if (opt_index == cl_options_count)
153     goto done;
154
155   option = &cl_options[opt_index];
156
157   /* Reject negative form of switches that don't take negatives as
158      unrecognized.  */
159   if (!value && (option->flags & CL_REJECT_NEGATIVE))
160     goto done;
161
162   /* We've recognized this switch.  */
163   result = 1;
164
165   /* Check to see if the option is disabled for this configuration.  */
166   if (option->flags & CL_DISABLED)
167     {
168       error ("command line option \"%s\""
169              " is not supported by this configuration", opt);
170       goto done;
171     }
172
173   /* Sort out any argument the switch takes.  */
174   if (option->flags & CL_JOINED)
175     {
176       /* Have arg point to the original switch.  This is because
177          some code, such as disable_builtin_function, expects its
178          argument to be persistent until the program exits.  */
179       arg = argv[0] + cl_options[opt_index].opt_len + 1;
180       if (!value)
181         arg += strlen ("no-");
182
183       if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
184         {
185           if (option->flags & CL_SEPARATE)
186             {
187               arg = argv[1];
188               result = 2;
189             }
190           else
191             /* Missing argument.  */
192             arg = NULL;
193         }
194     }
195   else if (option->flags & CL_SEPARATE)
196     {
197       arg = argv[1];
198       result = 2;
199     }
200
201   /* Now we've swallowed any potential argument, complain if this
202      is a switch for a different front end.  */
203   if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
204     {
205       complain_wrong_lang (argv[0], option, lang_mask);
206       goto done;
207     }
208
209   if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
210     {
211       if (!lang_hooks.missing_argument (opt, opt_index))
212         error ("missing argument to \"%s\"", opt);
213       goto done;
214     }
215
216   /* If the switch takes an integer, convert it.  */
217   if (arg && (option->flags & CL_UINTEGER))
218     {
219       value = integral_argument (arg);
220       if (value == -1)
221         {
222           error ("argument to \"%s\" should be a non-negative integer",
223                  option->opt_text);
224           goto done;
225         }
226     }
227
228   if (option->flag_var)
229     switch (option->var_type)
230       {
231       case CLVC_BOOLEAN:
232         *(int *) option->flag_var = value;
233         break;
234
235       case CLVC_EQUAL:
236         *(int *) option->flag_var = (value
237                                      ? option->var_value
238                                      : !option->var_value);
239         break;
240
241       case CLVC_BIT_CLEAR:
242       case CLVC_BIT_SET:
243         if ((value != 0) == (option->var_type == CLVC_BIT_SET))
244           *(int *) option->flag_var |= option->var_value;
245         else
246           *(int *) option->flag_var &= ~option->var_value;
247         ////if (option->flag_var == &target_flags)
248         ////  target_flags_explicit |= option->var_value;
249         break;
250
251       case CLVC_STRING:
252         *(const char **) option->flag_var = arg;
253         break;
254       }
255
256   if (option->flags & lang_mask)
257     if (lang_hooks.handle_option (opt_index, arg, value) == 0)
258       result = 0;
259
260   if (result && (option->flags & CL_COMMON))
261     if (common_handle_option (opt_index, arg, value) == 0)
262       result = 0;
263
264   ////if (result && (option->flags & CL_TARGET))
265   ////  if (!targetm.handle_option (opt_index, arg, value))
266   ////    result = 0;
267
268  done:
269   if (dup)
270     free (dup);
271   return result;
272 }
273
274 /* Handle FILENAME from the command line.  */
275 static void
276 add_input_filename (const char *filename)
277 {
278   num_in_fnames++;
279   in_fnames = xrealloc (in_fnames, num_in_fnames * sizeof (in_fnames[0]));
280   in_fnames[num_in_fnames - 1] = filename;
281 }
282
283 /* Decode and handle the vector of command line options.  LANG_MASK
284    contains has a single bit set representing the current
285    language.  */
286 static void
287 handle_options (unsigned int argc, const char **argv, unsigned int lang_mask)
288 {
289   unsigned int n, i;
290
291   for (i = 1; i < argc; i += n)
292     {
293       const char *opt = argv[i];
294
295       /* Interpret "-" or a non-switch as a file name.  */
296       if (opt[0] != '-' || opt[1] == '\0')
297         {
298           if (main_input_filename == NULL)
299             main_input_filename = opt;
300           add_input_filename (opt);
301           n = 1;
302           continue;
303         }
304
305       n = handle_option (argv + i, lang_mask);
306
307       if (!n)
308         {
309           n = 1;
310           error ("unrecognized command line option \"%s\"", opt);
311         }
312     }
313 }
314
315 /* Parse command line options and set default flag values.  Do minimal
316    options processing.  */
317 void
318 decode_options (unsigned int argc, const char **argv)
319 {
320   unsigned int lang_mask;
321
322   /* Perform language-specific options initialization.  */
323   lang_mask = lang_hooks.init_options (argc, argv);
324
325   /* Scan to see what optimization level has been specified.  That will
326      determine the default value of many flags.  */
327
328   handle_options (argc, argv, lang_mask);
329 }
330
331 /* Handle target- and language-independent options.  Return zero to
332    generate an "unknown option" message.  Only options that need
333    extra handling need to be listed here; if you simply want
334    VALUE assigned to a variable, it happens automatically.  */
335
336 static int
337 common_handle_option (size_t scode, const char *arg,
338                       int value ATTRIBUTE_UNUSED)
339 {
340   enum opt_code code = (enum opt_code) scode;
341
342   switch (code)
343     {
344     default:
345       abort ();
346
347     case OPT__help:
348       print_help ();
349       exit_after_options = true;
350       break;
351
352     case OPT__version:
353       print_version (stderr, "");
354       exit_after_options = true;
355       break;
356
357     case OPT_Werror:
358       warnings_are_errors = value;
359       break;
360
361     case OPT_Wsystem_headers:
362       warn_system_headers = value;
363       break;
364
365     case OPT_d:
366       decode_d_option (arg);
367       break;
368
369     case OPT_pedantic_errors:
370       flag_pedantic_errors = 1;
371       break;
372
373     case OPT_w:
374       inhibit_warnings = true;
375       break;
376     }
377
378   return 1;
379 }
380
381 /* Output --help text.  */
382 static void
383 print_help (void)
384 {
385   size_t i;
386   const char *p;
387
388   GET_ENVIRONMENT (p, "COLUMNS");
389   if (p)
390     {
391       int value = atoi (p);
392       if (value > 0)
393         columns = value;
394     }
395
396   puts (_("The following options are language-independent:\n"));
397
398   print_filtered_help (CL_COMMON);
399
400   for (i = 0; lang_names[i]; i++)
401     {
402       printf (_("The %s front end recognizes the following options:\n\n"),
403               lang_names[i]);
404       print_filtered_help (1U << i);
405     }
406 }
407
408 /* Print help for a specific front-end, etc.  */
409 static void
410 print_filtered_help (unsigned int flag)
411 {
412   unsigned int i, len, filter, indent = 0;
413   bool duplicates = false;
414   const char *help, *opt, *tab;
415   static char *printed;
416
417   if (flag == CL_COMMON || flag == CL_TARGET)
418     {
419       filter = flag;
420       if (!printed)
421         printed = xmalloc (cl_options_count);
422       memset (printed, 0, cl_options_count);
423     }
424   else
425     {
426       /* Don't print COMMON options twice.  */
427       filter = flag | CL_COMMON;
428
429       for (i = 0; i < cl_options_count; i++)
430         {
431           if ((cl_options[i].flags & filter) != flag)
432             continue;
433
434           /* Skip help for internal switches.  */
435           if (cl_options[i].flags & CL_UNDOCUMENTED)
436             continue;
437
438           /* Skip switches that have already been printed, mark them to be
439              listed later.  */
440           if (printed[i])
441             {
442               duplicates = true;
443               indent = print_switch (cl_options[i].opt_text, indent);
444             }
445         }
446
447       if (duplicates)
448         {
449           putchar ('\n');
450           putchar ('\n');
451         }
452     }
453
454   for (i = 0; i < cl_options_count; i++)
455     {
456       if ((cl_options[i].flags & filter) != flag)
457         continue;
458
459       /* Skip help for internal switches.  */
460       if (cl_options[i].flags & CL_UNDOCUMENTED)
461         continue;
462
463       /* Skip switches that have already been printed.  */
464       if (printed[i])
465         continue;
466
467       printed[i] = true;
468
469       help = cl_options[i].help;
470       if (!help)
471         help = undocumented_msg;
472
473       /* Get the translation.  */
474       help = _(help);
475
476       tab = strchr (help, '\t');
477       if (tab)
478         {
479           len = tab - help;
480           opt = help;
481           help = tab + 1;
482         }
483       else
484         {
485           opt = cl_options[i].opt_text;
486           len = strlen (opt);
487         }
488
489       wrap_help (help, opt, len);
490     }
491
492   putchar ('\n');
493 }
494
495 /* Output ITEM, of length ITEM_WIDTH, in the left column, followed by
496    word-wrapped HELP in a second column.  */
497 static unsigned int
498 print_switch (const char *text, unsigned int indent)
499 {
500   unsigned int len = strlen (text) + 1; /* trailing comma */
501
502   if (indent)
503     {
504       putchar (',');
505       if (indent + len > columns)
506         {
507           putchar ('\n');
508           putchar (' ');
509           indent = 1;
510         }
511     }
512   else
513     putchar (' ');
514
515   putchar (' ');
516   fputs (text, stdout);
517
518   return indent + len + 1;
519 }
520
521 /* Output ITEM, of length ITEM_WIDTH, in the left column, followed by
522    word-wrapped HELP in a second column.  */
523 static void
524 wrap_help (const char *help, const char *item, unsigned int item_width)
525 {
526   unsigned int col_width = 27;
527   unsigned int remaining, room, len;
528
529   remaining = strlen (help);
530
531   do
532     {
533       room = columns - 3 - MAX (col_width, item_width);
534       if (room > columns)
535         room = 0;
536       len = remaining;
537
538       if (room < len)
539         {
540           unsigned int i;
541
542           for (i = 0; help[i]; i++)
543             {
544               if (i >= room && len != remaining)
545                 break;
546               if (help[i] == ' ')
547                 len = i;
548               else if ((help[i] == '-' || help[i] == '/')
549                        && help[i + 1] != ' '
550                        && i > 0 && ISALPHA (help[i - 1]))
551                 len = i + 1;
552             }
553         }
554
555       printf( "  %-*.*s %.*s\n", col_width, item_width, item, len, help);
556       item_width = 0;
557       while (help[len] == ' ')
558         len++;
559       help += len;
560       remaining -= len;
561     }
562   while (remaining);
563 }
564
565 /* Return 1 if OPTION is enabled, 0 if it is disabled, or -1 if it isn't
566    a simple on-off switch.  */
567
568 int
569 option_enabled (int opt_idx)
570 {
571   const struct cl_option *option = &(cl_options[opt_idx]);
572   if (option->flag_var)
573     switch (option->var_type)
574       {
575       case CLVC_BOOLEAN:
576         return *(int *) option->flag_var != 0;
577
578       case CLVC_EQUAL:
579         return *(int *) option->flag_var == option->var_value;
580
581       case CLVC_BIT_CLEAR:
582         return (*(int *) option->flag_var & option->var_value) == 0;
583
584       case CLVC_BIT_SET:
585         return (*(int *) option->flag_var & option->var_value) != 0;
586
587       case CLVC_STRING:
588         break;
589       }
590   return -1;
591 }
592
593 /* Fill STATE with the current state of option OPTION.  Return true if
594    there is some state to store.  */
595
596 bool
597 get_option_state (int option, struct cl_option_state *state)
598 {
599   if (cl_options[option].flag_var == 0)
600     return false;
601
602   switch (cl_options[option].var_type)
603     {
604     case CLVC_BOOLEAN:
605     case CLVC_EQUAL:
606       state->data = cl_options[option].flag_var;
607       state->size = sizeof (int);
608       break;
609
610     case CLVC_BIT_CLEAR:
611     case CLVC_BIT_SET:
612       state->ch = option_enabled (option);
613       state->data = &state->ch;
614       state->size = 1;
615       break;
616
617     case CLVC_STRING:
618       state->data = *(const char **) cl_options[option].flag_var;
619       if (state->data == 0)
620         state->data = "";
621       state->size = strlen (state->data) + 1;
622       break;
623     }
624   return true;
625 }