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