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