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