]> git.saurik.com Git - bison.git/blob - src/getargs.c
options: no longer document warnings when diagnosing an invalid -W
[bison.git] / src / getargs.c
1 /* Parse command line arguments for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2013 Free Software
4 Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22 #include "system.h"
23 #include "output.h"
24
25 #include <argmatch.h>
26 #include <c-strcase.h>
27 #include <configmake.h>
28 #include <error.h>
29 #include <getopt.h>
30 #include <progname.h>
31
32 #include "complain.h"
33 #include "files.h"
34 #include "getargs.h"
35 #include "muscle-tab.h"
36 #include "quote.h"
37 #include "uniqstr.h"
38
39 bool defines_flag;
40 bool graph_flag;
41 bool xml_flag;
42 bool no_lines_flag;
43 bool token_table_flag;
44 bool yacc_flag; /* for -y */
45
46 bool nondeterministic_parser = false;
47 bool glr_parser = false;
48
49 int feature_flag = feature_caret;
50 int report_flag = report_none;
51 int trace_flag = trace_none;
52
53 static struct bison_language const valid_languages[] = {
54 { "c", "c-skel.m4", ".c", ".h", true },
55 { "c++", "c++-skel.m4", ".cc", ".hh", true },
56 { "java", "java-skel.m4", ".java", ".java", false },
57 { "", "", "", "", false }
58 };
59
60 int skeleton_prio = default_prio;
61 const char *skeleton = NULL;
62 int language_prio = default_prio;
63 struct bison_language const *language = &valid_languages[0];
64 const char *include = NULL;
65
66 /** Decode an option's key.
67 *
68 * \param option option being decoded.
69 * \param keys array of valid subarguments.
70 * \param values array of corresponding (int) values.
71 * \param all the all value.
72 * \param flags the flags to update
73 * \param arg the subarguments to decode.
74 * If null, then activate all the flags.
75 * \param no length of the potential "no-" prefix.
76 * Can be 0 or 3. If 3, negate the action of the subargument.
77 * \param err length of a potential "error=".
78 * Can be 0 or 6. If 6, treat the subargument as a CATEGORY
79 *
80 * If VALUE != 0 then KEY sets flags and no-KEY clears them.
81 * If VALUE == 0 then KEY clears all flags from \c all and no-KEY sets all
82 * flags from \c all. Thus no-none = all and no-all = none.
83 */
84 static void
85 flag_argmatch (const char *option,
86 const char * const keys[], const int values[],
87 int all, int *flags, char *arg, size_t no, size_t err)
88 {
89 int value = 0;
90 if (!err || arg[no + err++] != '\0')
91 value = XARGMATCH (option, arg + no + err, keys, values);
92
93 if (value)
94 {
95 if (no)
96 *flags &= ~value;
97 else
98 {
99 if (err)
100 warnings_flag |= value;
101 *flags |= value;
102 }
103 }
104 else
105 {
106 /* With a simpler 'if (no)' version, -Werror means -Werror=all
107 (or rather, -Werror=no-none, but that syntax is invalid).
108 The difference is:
109 - Werror activates all errors, but not the warnings
110 - Werror=all activates errors, and all warnings */
111 if (no ? !err : err)
112 *flags |= all;
113 else
114 *flags &= ~all;
115 }
116 }
117 /** Decode an option's set of keys.
118 *
119 * \param option option being decoded.
120 * \param keys array of valid subarguments.
121 * \param values array of corresponding (int) values.
122 * \param all the all value.
123 * \param flags the flags to update
124 * \param args comma separated list of effective subarguments to decode.
125 * If 0, then activate all the flags.
126 */
127 static void
128 flags_argmatch (const char *option,
129 const char * const keys[], const int values[],
130 int all, int *flags, char *args)
131 {
132 if (args)
133 for (args = strtok (args, ","); args; args = strtok (NULL, ","))
134 {
135 size_t no = STRPREFIX_LIT ("no-", args) ? 3 : 0;
136 size_t err = STRPREFIX_LIT ("error", args + no) ? 5 : 0;
137
138 flag_argmatch (option, keys,
139 values, all, err ? &errors_flag : flags,
140 args, no, err);
141 }
142 else
143 *flags |= all;
144 }
145
146 /** Decode a set of sub arguments.
147 *
148 * \param FlagName the flag familly to update.
149 * \param Args the effective sub arguments to decode.
150 * \param All the "all" value.
151 *
152 * \arg FlagName_args the list of keys.
153 * \arg FlagName_types the list of values.
154 * \arg FlagName_flag the flag to update.
155 */
156 #define FLAGS_ARGMATCH(FlagName, Args, All) \
157 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
158 All, &FlagName ## _flag, Args)
159
160
161 /*----------------------.
162 | --report's handling. |
163 `----------------------*/
164
165 static const char * const report_args[] =
166 {
167 /* In a series of synonyms, present the most meaningful first, so
168 that argmatch_valid be more readable. */
169 "none",
170 "state", "states",
171 "itemset", "itemsets",
172 "lookahead", "lookaheads", "look-ahead",
173 "solved",
174 "all",
175 0
176 };
177
178 static const int report_types[] =
179 {
180 report_none,
181 report_states, report_states,
182 report_states | report_itemsets, report_states | report_itemsets,
183 report_states | report_lookahead_tokens,
184 report_states | report_lookahead_tokens,
185 report_states | report_lookahead_tokens,
186 report_states | report_solved_conflicts,
187 report_all
188 };
189
190 ARGMATCH_VERIFY (report_args, report_types);
191
192
193 /*---------------------.
194 | --trace's handling. |
195 `---------------------*/
196
197 static const char * const trace_args[] =
198 {
199 "none - no traces",
200 "scan - grammar scanner traces",
201 "parse - grammar parser traces",
202 "automaton - construction of the automaton",
203 "bitsets - use of bitsets",
204 "grammar - reading, reducing the grammar",
205 "resource - memory consumption (where available)",
206 "sets - grammar sets: firsts, nullable etc.",
207 "muscles - m4 definitions passed to the skeleton",
208 "tools - m4 invocation",
209 "m4 - m4 traces",
210 "skeleton - skeleton postprocessing",
211 "time - time consumption",
212 "ielr - IELR conversion",
213 "all - all of the above",
214 0
215 };
216
217 static const int trace_types[] =
218 {
219 trace_none,
220 trace_scan,
221 trace_parse,
222 trace_automaton,
223 trace_bitsets,
224 trace_grammar,
225 trace_resource,
226 trace_sets,
227 trace_muscles,
228 trace_tools,
229 trace_m4,
230 trace_skeleton,
231 trace_time,
232 trace_ielr,
233 trace_all
234 };
235
236 ARGMATCH_VERIFY (trace_args, trace_types);
237
238
239 /*------------------------.
240 | --warnings's handling. |
241 `------------------------*/
242
243 static const char * const warnings_args[] =
244 {
245 "none",
246 "midrule-values",
247 "yacc",
248 "conflicts-sr",
249 "conflicts-rr",
250 "deprecated",
251 "precedence",
252 "other",
253 "all",
254 "error",
255 0
256 };
257
258 static const int warnings_types[] =
259 {
260 Wnone,
261 Wmidrule_values,
262 Wyacc,
263 Wconflicts_sr,
264 Wconflicts_rr,
265 Wdeprecated,
266 Wprecedence,
267 Wother,
268 Wall,
269 Werror
270 };
271
272 ARGMATCH_VERIFY (warnings_args, warnings_types);
273
274 /*-----------------------.
275 | --feature's handling. |
276 `-----------------------*/
277
278 static const char * const feature_args[] =
279 {
280 "none",
281 "caret", "diagnostics-show-caret",
282 "all",
283 0
284 };
285
286 static const int feature_types[] =
287 {
288 feature_none,
289 feature_caret, feature_caret,
290 feature_all
291 };
292
293 ARGMATCH_VERIFY (feature_args, feature_types);
294
295 /*-------------------------------------------.
296 | Display the help message and exit STATUS. |
297 `-------------------------------------------*/
298
299 static void usage (int) ATTRIBUTE_NORETURN;
300
301 static void
302 usage (int status)
303 {
304 if (status != 0)
305 fprintf (stderr, _("Try `%s --help' for more information.\n"),
306 program_name);
307 else
308 {
309 /* For ../build-aux/cross-options.pl to work, use the format:
310 ^ -S, --long[=ARGS] (whitespace)
311 A --long option is required.
312 Otherwise, add exceptions to ../build-aux/cross-options.pl. */
313
314 printf (_("Usage: %s [OPTION]... FILE\n"), program_name);
315 fputs (_("\
316 Generate a deterministic LR or generalized LR (GLR) parser employing\n\
317 LALR(1), IELR(1), or canonical LR(1) parser tables. IELR(1) and\n\
318 canonical LR(1) support is experimental.\n\
319 \n\
320 "), stdout);
321
322 fputs (_("\
323 Mandatory arguments to long options are mandatory for short options too.\n\
324 "), stdout);
325 fputs (_("\
326 The same is true for optional arguments.\n\
327 "), stdout);
328
329 fputs (_("\
330 \n\
331 Operation modes:\n\
332 -h, --help display this help and exit\n\
333 -V, --version output version information and exit\n\
334 --print-localedir output directory containing locale-dependent data\n\
335 --print-datadir output directory containing skeletons and XSLT\n\
336 -y, --yacc emulate POSIX Yacc\n\
337 -W, --warnings[=CATEGORY] report the warnings falling in CATEGORY\n\
338 -f, --feature[=FEATURE] activate miscellaneous features\n\
339 \n\
340 "), stdout);
341
342 fputs (_("\
343 Parser:\n\
344 -L, --language=LANGUAGE specify the output programming language\n\
345 -S, --skeleton=FILE specify the skeleton to use\n\
346 -t, --debug instrument the parser for tracing\n\
347 same as `-Dparse.trace'\n\
348 --locations enable location support\n\
349 -D, --define=NAME[=VALUE] similar to '%define NAME \"VALUE\"'\n\
350 -F, --force-define=NAME[=VALUE] override '%define NAME \"VALUE\"'\n\
351 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
352 deprecated by '-Dapi.prefix=PREFIX'\n\
353 -l, --no-lines don't generate '#line' directives\n\
354 -k, --token-table include a table of token names\n\
355 "), stdout);
356 putc ('\n', stdout);
357
358 /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
359 * won't assume that -d also takes an argument. */
360 fputs (_("\
361 Output:\n\
362 --defines[=FILE] also produce a header file\n\
363 -d likewise but cannot specify FILE (for POSIX Yacc)\n\
364 -r, --report=THINGS also produce details on the automaton\n\
365 --report-file=FILE write report to FILE\n\
366 -v, --verbose same as `--report=state'\n\
367 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
368 -o, --output=FILE leave output to FILE\n\
369 -g, --graph[=FILE] also output a graph of the automaton\n\
370 -x, --xml[=FILE] also output an XML report of the automaton\n\
371 (the XML schema is experimental)\n\
372 "), stdout);
373 putc ('\n', stdout);
374
375 fputs (_("\
376 Warning categories include:\n\
377 `midrule-values' unset or unused midrule values\n\
378 `yacc' incompatibilities with POSIX Yacc\n\
379 `conflicts-sr' S/R conflicts (enabled by default)\n\
380 `conflicts-rr' R/R conflicts (enabled by default)\n\
381 `deprecated' obsolete constructs\n\
382 `precedence' useless precedence and associativity\n\
383 `other' all other warnings (enabled by default)\n\
384 `all' all the warnings\n\
385 `no-CATEGORY' turn off warnings in CATEGORY\n\
386 `none' turn off all the warnings\n\
387 `error[=CATEGORY]' treat warnings as errors\n\
388 "), stdout);
389 putc ('\n', stdout);
390
391 fputs (_("\
392 THINGS is a list of comma separated words that can include:\n\
393 `state' describe the states\n\
394 `itemset' complete the core item sets with their closure\n\
395 `lookahead' explicitly associate lookahead tokens to items\n\
396 `solved' describe shift/reduce conflicts solving\n\
397 `all' include all the above information\n\
398 `none' disable the report\n\
399 "), stdout);
400 putc ('\n', stdout);
401
402 fputs (_("\
403 FEATURE is a list of comma separated words that can include:\n\
404 `caret' show errors with carets\n\
405 `all' all of the above\n\
406 `none' disable all of the above\n\
407 "), stdout);
408
409 putc ('\n', stdout);
410 printf (_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
411 printf (_("%s home page: <%s>.\n"), PACKAGE_NAME, PACKAGE_URL);
412 fputs (_("General help using GNU software: "
413 "<http://www.gnu.org/gethelp/>.\n"),
414 stdout);
415 /* Don't output this redundant message for English locales.
416 Note we still output for 'C' so that it gets included in the
417 man page. */
418 const char *lc_messages = setlocale (LC_MESSAGES, NULL);
419 if (lc_messages && !STREQ (lc_messages, "en_"))
420 /* TRANSLATORS: Replace LANG_CODE in this URL with your language
421 code <http://translationproject.org/team/LANG_CODE.html> to
422 form one of the URLs at http://translationproject.org/team/.
423 Otherwise, replace the entire URL with your translation team's
424 email address. */
425 fputs (_("Report translation bugs to "
426 "<http://translationproject.org/team/>.\n"), stdout);
427 fputs (_("For complete documentation, run: info bison.\n"), stdout);
428 }
429
430 exit (status);
431 }
432
433
434 /*------------------------------.
435 | Display the version message. |
436 `------------------------------*/
437
438 static void
439 version (void)
440 {
441 /* Some efforts were made to ease the translators' task, please
442 continue. */
443 printf (_("bison (GNU Bison) %s"), VERSION);
444 putc ('\n', stdout);
445 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout);
446 putc ('\n', stdout);
447
448 fprintf (stdout,
449 _("Copyright (C) %d Free Software Foundation, Inc.\n"),
450 PACKAGE_COPYRIGHT_YEAR);
451
452 fputs (_("\
453 This is free software; see the source for copying conditions. There is NO\n\
454 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
455 "),
456 stdout);
457 }
458
459
460 /*-------------------------------------.
461 | --skeleton and --language handling. |
462 `--------------------------------------*/
463
464 void
465 skeleton_arg (char const *arg, int prio, location loc)
466 {
467 if (prio < skeleton_prio)
468 {
469 skeleton_prio = prio;
470 skeleton = arg;
471 }
472 else if (prio == skeleton_prio)
473 complain (&loc, complaint,
474 _("multiple skeleton declarations are invalid"));
475 }
476
477 void
478 language_argmatch (char const *arg, int prio, location loc)
479 {
480 char const *msg;
481
482 if (prio < language_prio)
483 {
484 int i;
485 for (i = 0; valid_languages[i].language[0]; i++)
486 if (c_strcasecmp (arg, valid_languages[i].language) == 0)
487 {
488 language_prio = prio;
489 language = &valid_languages[i];
490 return;
491 }
492 msg = _("%s: invalid language");
493 }
494 else if (language_prio == prio)
495 msg = _("multiple language declarations are invalid");
496 else
497 return;
498
499 complain (&loc, complaint, msg, quotearg_colon (arg));
500 }
501
502 /*----------------------.
503 | Process the options. |
504 `----------------------*/
505
506 /* Shorts options.
507 Should be computed from long_options. */
508 static char const short_options[] =
509 "D:"
510 "F:"
511 "L:"
512 "S:"
513 "T::"
514 "V"
515 "W::"
516 "b:"
517 "d"
518 "f::"
519 "e"
520 "g::"
521 "h"
522 "k"
523 "l"
524 "n"
525 "o:"
526 "p:"
527 "r:"
528 "t"
529 "v"
530 "x::"
531 "y"
532 ;
533
534 /* Values for long options that do not have single-letter equivalents. */
535 enum
536 {
537 LOCATIONS_OPTION = CHAR_MAX + 1,
538 PRINT_LOCALEDIR_OPTION,
539 PRINT_DATADIR_OPTION,
540 REPORT_FILE_OPTION
541 };
542
543 static struct option const long_options[] =
544 {
545 /* Operation modes. */
546 { "help", no_argument, 0, 'h' },
547 { "version", no_argument, 0, 'V' },
548 { "print-localedir", no_argument, 0, PRINT_LOCALEDIR_OPTION },
549 { "print-datadir", no_argument, 0, PRINT_DATADIR_OPTION },
550 { "warnings", optional_argument, 0, 'W' },
551
552 /* Parser. */
553 { "name-prefix", required_argument, 0, 'p' },
554 { "include", required_argument, 0, 'I' },
555
556 /* Output. */
557 { "file-prefix", required_argument, 0, 'b' },
558 { "output", required_argument, 0, 'o' },
559 { "output-file", required_argument, 0, 'o' },
560 { "graph", optional_argument, 0, 'g' },
561 { "xml", optional_argument, 0, 'x' },
562 { "report", required_argument, 0, 'r' },
563 { "report-file", required_argument, 0, REPORT_FILE_OPTION },
564 { "verbose", no_argument, 0, 'v' },
565
566 /* Hidden. */
567 { "trace", optional_argument, 0, 'T' },
568
569 /* Output. */
570 { "defines", optional_argument, 0, 'd' },
571 { "feature", optional_argument, 0, 'f' },
572
573 /* Operation modes. */
574 { "fixed-output-files", no_argument, 0, 'y' },
575 { "yacc", no_argument, 0, 'y' },
576
577 /* Parser. */
578 { "debug", no_argument, 0, 't' },
579 { "define", required_argument, 0, 'D' },
580 { "force-define", required_argument, 0, 'F' },
581 { "locations", no_argument, 0, LOCATIONS_OPTION },
582 { "no-lines", no_argument, 0, 'l' },
583 { "raw", no_argument, 0, 0 },
584 { "skeleton", required_argument, 0, 'S' },
585 { "language", required_argument, 0, 'L' },
586 { "token-table", no_argument, 0, 'k' },
587
588 {0, 0, 0, 0}
589 };
590
591 /* Under DOS, there is no difference on the case. This can be
592 troublesome when looking for `.tab' etc. */
593 #ifdef MSDOS
594 # define AS_FILE_NAME(File) (strlwr (File), (File))
595 #else
596 # define AS_FILE_NAME(File) (File)
597 #endif
598
599 /* Build a location for the current command line argument. */
600 static
601 location
602 command_line_location (void)
603 {
604 location res;
605 /* "<command line>" is used in GCC's messages about -D. */
606 boundary_set (&res.start, uniqstr_new ("<command line>"), optind - 1, -1);
607 res.end = res.start;
608 return res;
609 }
610
611
612 void
613 getargs (int argc, char *argv[])
614 {
615 int c;
616
617 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
618 != -1)
619 switch (c)
620 {
621 /* ASCII Sorting for short options (i.e., upper case then
622 lower case), and then long-only options. */
623
624 case 0:
625 /* Certain long options cause getopt_long to return 0. */
626 break;
627
628 case 'D': /* -DNAME[=VALUE]. */
629 case 'F': /* -FNAME[=VALUE]. */
630 {
631 char* name = optarg;
632 char* value = strchr (optarg, '=');
633 if (value)
634 *value++ = 0;
635 muscle_percent_define_insert (name, command_line_location (),
636 value ? value : "",
637 c == 'D' ? MUSCLE_PERCENT_DEFINE_D
638 : MUSCLE_PERCENT_DEFINE_F);
639 }
640 break;
641
642 case 'I':
643 include = AS_FILE_NAME (optarg);
644 break;
645
646 case 'L':
647 language_argmatch (optarg, command_line_prio,
648 command_line_location ());
649 break;
650
651 case 'S':
652 skeleton_arg (AS_FILE_NAME (optarg), command_line_prio,
653 command_line_location ());
654 break;
655
656 case 'T':
657 FLAGS_ARGMATCH (trace, optarg, trace_all);
658 break;
659
660 case 'V':
661 version ();
662 exit (EXIT_SUCCESS);
663
664 case 'f':
665 FLAGS_ARGMATCH (feature, optarg, feature_all);
666 break;
667
668 case 'W':
669 FLAGS_ARGMATCH (warnings, optarg, Wall);
670 break;
671
672 case 'b':
673 spec_file_prefix = AS_FILE_NAME (optarg);
674 break;
675
676 case 'd':
677 /* Here, the -d and --defines options are differentiated. */
678 defines_flag = true;
679 if (optarg)
680 {
681 free (spec_defines_file);
682 spec_defines_file = xstrdup (AS_FILE_NAME (optarg));
683 }
684 break;
685
686 case 'g':
687 graph_flag = true;
688 if (optarg)
689 {
690 free (spec_graph_file);
691 spec_graph_file = xstrdup (AS_FILE_NAME (optarg));
692 }
693 break;
694
695 case 'h':
696 usage (EXIT_SUCCESS);
697
698 case 'k':
699 token_table_flag = true;
700 break;
701
702 case 'l':
703 no_lines_flag = true;
704 break;
705
706 case 'o':
707 spec_outfile = AS_FILE_NAME (optarg);
708 break;
709
710 case 'p':
711 spec_name_prefix = optarg;
712 break;
713
714 case 'r':
715 FLAGS_ARGMATCH (report, optarg, report_all);
716 break;
717
718 case 't':
719 muscle_percent_define_insert ("parse.trace",
720 command_line_location (), "",
721 MUSCLE_PERCENT_DEFINE_D);
722 break;
723
724 case 'v':
725 report_flag |= report_states;
726 break;
727
728 case 'x':
729 xml_flag = true;
730 if (optarg)
731 {
732 free (spec_xml_file);
733 spec_xml_file = xstrdup (AS_FILE_NAME (optarg));
734 }
735 break;
736
737 case 'y':
738 warnings_flag |= Wyacc;
739 errors_flag |= Wyacc;
740 yacc_flag = true;
741 break;
742
743 case LOCATIONS_OPTION:
744 muscle_percent_define_ensure ("locations",
745 command_line_location (), true);
746 break;
747
748 case PRINT_LOCALEDIR_OPTION:
749 printf ("%s\n", LOCALEDIR);
750 exit (EXIT_SUCCESS);
751
752 case PRINT_DATADIR_OPTION:
753 printf ("%s\n", pkgdatadir ());
754 exit (EXIT_SUCCESS);
755
756 case REPORT_FILE_OPTION:
757 free (spec_verbose_file);
758 spec_verbose_file = xstrdup (AS_FILE_NAME (optarg));
759 break;
760
761 default:
762 usage (EXIT_FAILURE);
763 }
764
765 if (argc - optind != 1)
766 {
767 if (argc - optind < 1)
768 error (0, 0, _("%s: missing operand"), quotearg_colon (argv[argc - 1]));
769 else
770 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
771 usage (EXIT_FAILURE);
772 }
773
774 current_file = grammar_file = uniqstr_new (argv[optind]);
775 MUSCLE_INSERT_C_STRING ("file_name", grammar_file);
776 }
777
778 void
779 tr (char *s, char from, char to)
780 {
781 for (; *s; s++)
782 if (*s == from)
783 *s = to;
784 }