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