Add option manager and simpler option listener (#4745)
[cvc5.git] / src / options / options_template.cpp
1 /********************* */
2 /*! \file options_template.cpp
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Morgan Deters, Tim King, Mathias Preiner
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
11 **
12 ** \brief Contains code for handling command-line options.
13 **
14 ** Contains code for handling command-line options
15 **/
16
17 #if !defined(_BSD_SOURCE) && defined(__MINGW32__) && !defined(__MINGW64__)
18 // force use of optreset; mingw32 croaks on argv-switching otherwise
19 # include "cvc4autoconfig.h"
20 # define _BSD_SOURCE
21 # undef HAVE_DECL_OPTRESET
22 # define HAVE_DECL_OPTRESET 1
23 # define CVC4_IS_NOT_REALLY_BSD
24 #endif /* !_BSD_SOURCE && __MINGW32__ && !__MINGW64__ */
25
26 #ifdef __MINGW64__
27 extern int optreset;
28 #endif /* __MINGW64__ */
29
30 #include <getopt.h>
31
32 // clean up
33 #ifdef CVC4_IS_NOT_REALLY_BSD
34 # undef _BSD_SOURCE
35 #endif /* CVC4_IS_NOT_REALLY_BSD */
36
37 #include <unistd.h>
38 #include <string.h>
39 #include <stdint.h>
40 #include <time.h>
41
42 #include <cstdio>
43 #include <cstdlib>
44 #include <cstring>
45 #include <iomanip>
46 #include <new>
47 #include <string>
48 #include <sstream>
49 #include <limits>
50
51 #include "base/check.h"
52 #include "base/exception.h"
53 #include "base/output.h"
54 #include "options/didyoumean.h"
55 #include "options/language.h"
56 #include "options/options_handler.h"
57 #include "options/options_listener.h"
58
59 ${headers_module}$
60
61
62 #include "options/options_holder.h"
63 #include "cvc4autoconfig.h"
64 #include "options/base_handlers.h"
65
66 ${headers_handler}$
67
68
69 using namespace CVC4;
70 using namespace CVC4::options;
71
72 namespace CVC4 {
73
74 thread_local Options* Options::s_current = NULL;
75
76 /**
77 * This is a default handler for options of built-in C++ type. This
78 * template is really just a helper for the handleOption() template,
79 * below. Variants of this template handle numeric and non-numeric,
80 * integral and non-integral, signed and unsigned C++ types.
81 * handleOption() makes sure to instantiate the right one.
82 *
83 * This implements default behavior when e.g. an option is
84 * unsigned but the user specifies a negative argument; etc.
85 */
86 template <class T, bool is_numeric, bool is_integer>
87 struct OptionHandler {
88 static T handle(std::string option, std::string optionarg);
89 };/* struct OptionHandler<> */
90
91 /** Variant for integral C++ types */
92 template <class T>
93 struct OptionHandler<T, true, true> {
94 static bool stringToInt(T& t, const std::string& str) {
95 std::istringstream ss(str);
96 ss >> t;
97 char tmp;
98 return !(ss.fail() || ss.get(tmp));
99 }
100
101 static bool containsMinus(const std::string& str) {
102 return str.find('-') != std::string::npos;
103 }
104
105 static T handle(const std::string& option, const std::string& optionarg) {
106 try {
107 T i;
108 bool success = stringToInt(i, optionarg);
109
110 if(!success){
111 throw OptionException(option + ": failed to parse "+ optionarg +
112 " as an integer of the appropriate type.");
113 }
114
115 // Depending in the platform unsigned numbers with '-' signs may parse.
116 // Reject these by looking for any minus if it is not signed.
117 if( (! std::numeric_limits<T>::is_signed) && containsMinus(optionarg) ) {
118 // unsigned type but user gave negative argument
119 throw OptionException(option + " requires a nonnegative argument");
120 } else if(i < std::numeric_limits<T>::min()) {
121 // negative overflow for type
122 std::stringstream ss;
123 ss << option << " requires an argument >= "
124 << std::numeric_limits<T>::min();
125 throw OptionException(ss.str());
126 } else if(i > std::numeric_limits<T>::max()) {
127 // positive overflow for type
128 std::stringstream ss;
129 ss << option << " requires an argument <= "
130 << std::numeric_limits<T>::max();
131 throw OptionException(ss.str());
132 }
133
134 return i;
135
136 // if(std::numeric_limits<T>::is_signed) {
137 // return T(i.getLong());
138 // } else {
139 // return T(i.getUnsignedLong());
140 // }
141 } catch(std::invalid_argument&) {
142 // user gave something other than an integer
143 throw OptionException(option + " requires an integer argument");
144 }
145 }
146 };/* struct OptionHandler<T, true, true> */
147
148 /** Variant for numeric but non-integral C++ types */
149 template <class T>
150 struct OptionHandler<T, true, false> {
151 static T handle(std::string option, std::string optionarg) {
152 std::stringstream in(optionarg);
153 long double r;
154 in >> r;
155 if(! in.eof()) {
156 // we didn't consume the whole string (junk at end)
157 throw OptionException(option + " requires a numeric argument");
158 }
159
160 if(! std::numeric_limits<T>::is_signed && r < 0.0) {
161 // unsigned type but user gave negative value
162 throw OptionException(option + " requires a nonnegative argument");
163 } else if(r < -std::numeric_limits<T>::max()) {
164 // negative overflow for type
165 std::stringstream ss;
166 ss << option << " requires an argument >= "
167 << -std::numeric_limits<T>::max();
168 throw OptionException(ss.str());
169 } else if(r > std::numeric_limits<T>::max()) {
170 // positive overflow for type
171 std::stringstream ss;
172 ss << option << " requires an argument <= "
173 << std::numeric_limits<T>::max();
174 throw OptionException(ss.str());
175 }
176
177 return T(r);
178 }
179 };/* struct OptionHandler<T, true, false> */
180
181 /** Variant for non-numeric C++ types */
182 template <class T>
183 struct OptionHandler<T, false, false> {
184 static T handle(std::string option, std::string optionarg) {
185 T::unsupported_handleOption_call___please_write_me;
186 // The above line causes a compiler error if this version of the template
187 // is ever instantiated (meaning that a specialization is missing). So
188 // don't worry about the segfault in the next line, the "return" is only
189 // there to keep the compiler from giving additional, distracting errors
190 // and warnings.
191 return *(T*)0;
192 }
193 };/* struct OptionHandler<T, false, false> */
194
195 /** Handle an option of type T in the default way. */
196 template <class T>
197 T handleOption(std::string option, std::string optionarg) {
198 return OptionHandler<T, std::numeric_limits<T>::is_specialized, std::numeric_limits<T>::is_integer>::handle(option, optionarg);
199 }
200
201 /** Handle an option of type std::string in the default way. */
202 template <>
203 std::string handleOption<std::string>(std::string option, std::string optionarg) {
204 return optionarg;
205 }
206
207 /**
208 * Run handler, and any user-given predicates, for option T.
209 * If a user specifies a :handler or :predicates, it overrides this.
210 */
211 template <class T>
212 typename T::type runHandlerAndPredicates(T, std::string option, std::string optionarg, options::OptionsHandler* handler) {
213 // By default, parse the option argument in a way appropriate for its type.
214 // E.g., for "unsigned int" options, ensure that the provided argument is
215 // a nonnegative integer that fits in the unsigned int type.
216
217 return handleOption<typename T::type>(option, optionarg);
218 }
219
220 template <class T>
221 void runBoolPredicates(T, std::string option, bool b, options::OptionsHandler* handler) {
222 // By default, nothing to do for bool. Users add things with
223 // :predicate in options files to provide custom checking routines
224 // that can throw exceptions.
225 }
226
227
228 Options::Options(OptionsListener* ol)
229 : d_holder(new options::OptionsHolder())
230 , d_handler(new options::OptionsHandler(this))
231 , d_beforeSearchListeners(),
232 d_olisten(ol)
233 {}
234
235 Options::~Options() {
236 delete d_handler;
237 delete d_holder;
238 }
239
240 void Options::copyValues(const Options& options){
241 if(this != &options) {
242 delete d_holder;
243 d_holder = new options::OptionsHolder(*options.d_holder);
244 }
245 }
246
247 std::string Options::formatThreadOptionException(const std::string& option) {
248 std::stringstream ss;
249 ss << "can't understand option `" << option
250 << "': expected something like --threadN=\"--option1 --option2\","
251 << " where N is a nonnegative integer";
252 return ss.str();
253 }
254
255 void Options::setListener(OptionsListener* ol) { d_olisten = ol; }
256
257 ListenerCollection::Registration* Options::registerAndNotify(
258 ListenerCollection& collection, Listener* listener, bool notify)
259 {
260 ListenerCollection::Registration* registration =
261 collection.registerListener(listener);
262 if(notify) {
263 try
264 {
265 listener->notify();
266 }
267 catch (OptionException& e)
268 {
269 // It can happen that listener->notify() throws an OptionException. In
270 // that case, we have to make sure that we delete the registration of our
271 // listener before rethrowing the exception. Otherwise the
272 // ListenerCollection deconstructor will complain that not all
273 // registrations have been removed before invoking the deconstructor.
274 delete registration;
275 throw OptionException(e.getRawMessage());
276 }
277 }
278 return registration;
279 }
280
281 ListenerCollection::Registration* Options::registerBeforeSearchListener(
282 Listener* listener)
283 {
284 return d_beforeSearchListeners.registerListener(listener);
285 }
286
287 ListenerCollection::Registration* Options::registerSetDefaultExprDepthListener(
288 Listener* listener, bool notifyIfSet)
289 {
290 bool notify = notifyIfSet && wasSetByUser(options::defaultExprDepth);
291 return registerAndNotify(d_setDefaultExprDepthListeners, listener, notify);
292 }
293
294 ListenerCollection::Registration* Options::registerSetDefaultExprDagListener(
295 Listener* listener, bool notifyIfSet)
296 {
297 bool notify = notifyIfSet && wasSetByUser(options::defaultDagThresh);
298 return registerAndNotify(d_setDefaultDagThreshListeners, listener, notify);
299 }
300
301 ListenerCollection::Registration* Options::registerSetPrintExprTypesListener(
302 Listener* listener, bool notifyIfSet)
303 {
304 bool notify = notifyIfSet && wasSetByUser(options::printExprTypes);
305 return registerAndNotify(d_setPrintExprTypesListeners, listener, notify);
306 }
307
308 ListenerCollection::Registration* Options::registerSetDumpModeListener(
309 Listener* listener, bool notifyIfSet)
310 {
311 bool notify = notifyIfSet && wasSetByUser(options::dumpModeString);
312 return registerAndNotify(d_setDumpModeListeners, listener, notify);
313 }
314
315 ListenerCollection::Registration* Options::registerSetPrintSuccessListener(
316 Listener* listener, bool notifyIfSet)
317 {
318 bool notify = notifyIfSet && wasSetByUser(options::printSuccess);
319 return registerAndNotify(d_setPrintSuccessListeners, listener, notify);
320 }
321
322 ListenerCollection::Registration* Options::registerDumpToFileNameListener(
323 Listener* listener, bool notifyIfSet)
324 {
325 bool notify = notifyIfSet && wasSetByUser(options::dumpToFileName);
326 return registerAndNotify(d_dumpToFileListeners, listener, notify);
327 }
328
329 ListenerCollection::Registration*
330 Options::registerSetRegularOutputChannelListener(
331 Listener* listener, bool notifyIfSet)
332 {
333 bool notify = notifyIfSet && wasSetByUser(options::regularChannelName);
334 return registerAndNotify(d_setRegularChannelListeners, listener, notify);
335 }
336
337 ListenerCollection::Registration*
338 Options::registerSetDiagnosticOutputChannelListener(
339 Listener* listener, bool notifyIfSet)
340 {
341 bool notify = notifyIfSet && wasSetByUser(options::diagnosticChannelName);
342 return registerAndNotify(d_setDiagnosticChannelListeners, listener, notify);
343 }
344
345 ${custom_handlers}$
346
347 #if defined(CVC4_MUZZLED) || defined(CVC4_COMPETITION_MODE)
348 # define DO_SEMANTIC_CHECKS_BY_DEFAULT false
349 #else /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
350 # define DO_SEMANTIC_CHECKS_BY_DEFAULT true
351 #endif /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
352
353 options::OptionsHolder::OptionsHolder() :
354 ${module_defaults}$
355 {
356 }
357
358
359 static const std::string mostCommonOptionsDescription = "\
360 Most commonly-used CVC4 options:\n"
361 ${help_common}$;
362
363
364 static const std::string optionsDescription = mostCommonOptionsDescription + "\n\
365 \n\
366 Additional CVC4 options:\n"
367 ${help_others}$;
368
369
370 static const std::string optionsFootnote = "\n\
371 [*] Each of these options has a --no-OPTIONNAME variant, which reverses the\n\
372 sense of the option.\n\
373 ";
374
375 static const std::string languageDescription =
376 "\
377 Languages currently supported as arguments to the -L / --lang option:\n\
378 auto attempt to automatically determine language\n\
379 cvc4 | presentation | pl CVC4 presentation language\n\
380 smt | smtlib | smt2 |\n\
381 smt2.0 | smtlib2 | smtlib2.0 SMT-LIB format 2.0\n\
382 smt2.5 | smtlib2.5 SMT-LIB format 2.5\n\
383 smt2.6 | smtlib2.6 SMT-LIB format 2.6 with support for the strings standard\n\
384 tptp TPTP format (cnf, fof and tff)\n\
385 sygus | sygus2 SyGuS version 2.0\n\
386 \n\
387 Languages currently supported as arguments to the --output-lang option:\n\
388 auto match output language to input language\n\
389 cvc4 | presentation | pl CVC4 presentation language\n\
390 cvc3 CVC3 presentation language\n\
391 smt | smtlib | smt2 |\n\
392 smt2.0 | smtlib2.0 | smtlib2 SMT-LIB format 2.0\n\
393 smt2.5 | smtlib2.5 SMT-LIB format 2.5\n\
394 smt2.6 | smtlib2.6 SMT-LIB format 2.6 with support for the strings standard\n\
395 tptp TPTP format\n\
396 ast internal format (simple syntax trees)\n\
397 ";
398
399 std::string Options::getDescription() const {
400 return optionsDescription;
401 }
402
403 void Options::printUsage(const std::string msg, std::ostream& out) {
404 out << msg << optionsDescription << std::endl
405 << optionsFootnote << std::endl << std::flush;
406 }
407
408 void Options::printShortUsage(const std::string msg, std::ostream& out) {
409 out << msg << mostCommonOptionsDescription << std::endl
410 << optionsFootnote << std::endl
411 << "For full usage, please use --help."
412 << std::endl << std::endl << std::flush;
413 }
414
415 void Options::printLanguageHelp(std::ostream& out) {
416 out << languageDescription << std::flush;
417 }
418
419 /**
420 * This is a table of long options. By policy, each short option
421 * should have an equivalent long option (but the reverse isn't the
422 * case), so this table should thus contain all command-line options.
423 *
424 * Each option in this array has four elements:
425 *
426 * 1. the long option string
427 * 2. argument behavior for the option:
428 * no_argument - no argument permitted
429 * required_argument - an argument is expected
430 * optional_argument - an argument is permitted but not required
431 * 3. this is a pointer to an int which is set to the 4th entry of the
432 * array if the option is present; or NULL, in which case
433 * getopt_long() returns the 4th entry
434 * 4. the return value for getopt_long() when this long option (or the
435 * value to set the 3rd entry to; see #3)
436 *
437 * If you add something here, you should add it in src/main/usage.h
438 * also, to document it.
439 *
440 * If you add something that has a short option equivalent, you should
441 * add it to the getopt_long() call in parseOptions().
442 */
443 static struct option cmdlineOptions[] = {
444 ${cmdline_options}$
445 { NULL, no_argument, NULL, '\0' }
446 };/* cmdlineOptions */
447
448 namespace options {
449
450 /** Set a given Options* as "current" just for a particular scope. */
451 class OptionsGuard {
452 Options** d_field;
453 Options* d_old;
454 public:
455 OptionsGuard(Options** field, Options* opts) :
456 d_field(field),
457 d_old(*field) {
458 *field = opts;
459 }
460 ~OptionsGuard() {
461 *d_field = d_old;
462 }
463 };/* class OptionsGuard */
464
465 }/* CVC4::options namespace */
466
467 /**
468 * Parse argc/argv and put the result into a CVC4::Options.
469 * The return value is what's left of the command line (that is, the
470 * non-option arguments).
471 *
472 * Throws OptionException on failures.
473 */
474 std::vector<std::string> Options::parseOptions(Options* options,
475 int argc,
476 char* argv[])
477 {
478 Assert(options != NULL);
479 Assert(argv != NULL);
480
481 options::OptionsGuard guard(&s_current, options);
482
483 const char *progName = argv[0];
484
485 // To debug options parsing, you may prefer to simply uncomment this
486 // and recompile. Debug flags have not been parsed yet so these have
487 // not been set.
488 //DebugChannel.on("options");
489
490 Debug("options") << "Options::parseOptions == " << options << std::endl;
491 Debug("options") << "argv == " << argv << std::endl;
492
493 // Find the base name of the program.
494 const char *x = strrchr(progName, '/');
495 if(x != NULL) {
496 progName = x + 1;
497 }
498 options->d_holder->binary_name = std::string(progName);
499
500
501 std::vector<std::string> nonoptions;
502 parseOptionsRecursive(options, argc, argv, &nonoptions);
503 if(Debug.isOn("options")){
504 for(std::vector<std::string>::const_iterator i = nonoptions.begin(),
505 iend = nonoptions.end(); i != iend; ++i){
506 Debug("options") << "nonoptions " << *i << std::endl;
507 }
508 }
509
510 return nonoptions;
511 }
512
513 void Options::parseOptionsRecursive(Options* options,
514 int argc,
515 char* argv[],
516 std::vector<std::string>* nonoptions)
517 {
518
519 if(Debug.isOn("options")) {
520 Debug("options") << "starting a new parseOptionsRecursive with "
521 << argc << " arguments" << std::endl;
522 for( int i = 0; i < argc ; i++ ){
523 Assert(argv[i] != NULL);
524 Debug("options") << " argv[" << i << "] = " << argv[i] << std::endl;
525 }
526 }
527
528 // Having this synonym simplifies the generation code in mkoptions.
529 options::OptionsHandler* handler = options->d_handler;
530
531 // Reset getopt(), in the case of multiple calls to parseOptions().
532 // This can be = 1 in newer GNU getopt, but older (< 2007) require = 0.
533 optind = 0;
534 #if HAVE_DECL_OPTRESET
535 optreset = 1; // on BSD getopt() (e.g. Mac OS), might need this
536 #endif /* HAVE_DECL_OPTRESET */
537
538 // We must parse the binary name, which is manually ignored below. Setting
539 // this to 1 leads to incorrect behavior on some platforms.
540 int main_optind = 0;
541 int old_optind;
542
543
544 while(true) { // Repeat Forever
545
546 optopt = 0;
547 std::string option, optionarg;
548
549 optind = main_optind;
550 old_optind = main_optind;
551
552 // If we encounter an element that is not at zero and does not start
553 // with a "-", this is a non-option. We consume this element as a
554 // non-option.
555 if (main_optind > 0 && main_optind < argc &&
556 argv[main_optind][0] != '-') {
557 Debug("options") << "enqueueing " << argv[main_optind]
558 << " as a non-option." << std::endl;
559 nonoptions->push_back(argv[main_optind]);
560 ++main_optind;
561 continue;
562 }
563
564
565 Debug("options") << "[ before, main_optind == " << main_optind << " ]"
566 << std::endl;
567 Debug("options") << "[ before, optind == " << optind << " ]" << std::endl;
568 Debug("options") << "[ argc == " << argc << ", argv == " << argv << " ]"
569 << std::endl;
570 int c = getopt_long(argc, argv,
571 "+:${options_short}$",
572 cmdlineOptions, NULL);
573
574 main_optind = optind;
575
576 Debug("options") << "[ got " << int(c) << " (" << char(c) << ") ]"
577 << "[ next option will be at pos: " << optind << " ]"
578 << std::endl;
579
580 // The initial getopt_long call should always determine that argv[0]
581 // is not an option and returns -1. We always manually advance beyond
582 // this element.
583 if ( old_optind == 0 && c == -1 ) {
584 Assert(main_optind > 0);
585 continue;
586 }
587
588 if ( c == -1 ) {
589 if(Debug.isOn("options")) {
590 Debug("options") << "done with option parsing" << std::endl;
591 for(int index = optind; index < argc; ++index) {
592 Debug("options") << "remaining " << argv[index] << std::endl;
593 }
594 }
595 break;
596 }
597
598 option = argv[old_optind == 0 ? 1 : old_optind];
599 optionarg = (optarg == NULL) ? "" : optarg;
600
601 Debug("preemptGetopt") << "processing option " << c
602 << " (`" << char(c) << "'), " << option << std::endl;
603
604 switch(c) {
605 ${options_handler}$
606
607
608 case ':':
609 // This can be a long or short option, and the way to get at the
610 // name of it is different.
611 throw OptionException(std::string("option `") + option +
612 "' missing its required argument");
613
614 case '?':
615 default:
616 throw OptionException(std::string("can't understand option `") + option +
617 "'" + suggestCommandLineOptions(option));
618 }
619 }
620
621 Debug("options") << "got " << nonoptions->size()
622 << " non-option arguments." << std::endl;
623 }
624
625 std::string Options::suggestCommandLineOptions(const std::string& optionName)
626 {
627 DidYouMean didYouMean;
628
629 const char* opt;
630 for(size_t i = 0; (opt = cmdlineOptions[i].name) != NULL; ++i) {
631 didYouMean.addWord(std::string("--") + cmdlineOptions[i].name);
632 }
633
634 return didYouMean.getMatchAsString(optionName.substr(0, optionName.find('=')));
635 }
636
637 static const char* smtOptions[] = {
638 ${options_smt}$
639 NULL
640 };/* smtOptions[] */
641
642 std::vector<std::string> Options::suggestSmtOptions(
643 const std::string& optionName)
644 {
645 std::vector<std::string> suggestions;
646
647 const char* opt;
648 for(size_t i = 0; (opt = smtOptions[i]) != NULL; ++i) {
649 if(std::strstr(opt, optionName.c_str()) != NULL) {
650 suggestions.push_back(opt);
651 }
652 }
653
654 return suggestions;
655 }
656
657 std::vector<std::vector<std::string> > Options::getOptions() const
658 {
659 std::vector< std::vector<std::string> > opts;
660
661 ${options_getoptions}$
662
663
664 return opts;
665 }
666
667 void Options::setOption(const std::string& key, const std::string& optionarg)
668 {
669 Trace("options") << "setOption(" << key << ", " << optionarg << ")"
670 << std::endl;
671 // first update this object
672 setOptionInternal(key, optionarg);
673 // then, notify the provided listener
674 if (d_olisten != nullptr)
675 {
676 d_olisten->notifySetOption(key);
677 }
678 }
679
680 void Options::setOptionInternal(const std::string& key,
681 const std::string& optionarg)
682 {
683 options::OptionsHandler* handler = d_handler;
684 Options* options = this;
685 ${setoption_handlers}$
686 throw UnrecognizedOptionException(key);
687 }
688
689 std::string Options::getOption(const std::string& key) const
690 {
691 Trace("options") << "SMT getOption(" << key << ")" << std::endl;
692
693 ${getoption_handlers}$
694
695
696 throw UnrecognizedOptionException(key);
697 }
698
699 #undef USE_EARLY_TYPE_CHECKING_BY_DEFAULT
700 #undef DO_SEMANTIC_CHECKS_BY_DEFAULT
701
702 } // namespace CVC4