New translation work, support Z3-str-style string constraints.
[cvc5.git] / src / options / options_template.cpp
1 /********************* */
2 /*! \file options_template.cpp
3 ** \verbatim
4 ** Original author: Morgan Deters
5 ** Major contributors: none
6 ** Minor contributors (to current version): none
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2013 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** 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 <cstdio>
38 #include <cstdlib>
39 #include <new>
40 #include <string>
41 #include <sstream>
42 #include <limits>
43 #include <unistd.h>
44 #include <string.h>
45 #include <stdint.h>
46 #include <time.h>
47
48 #include "expr/expr.h"
49 #include "util/configuration.h"
50 #include "util/exception.h"
51 #include "util/language.h"
52 #include "util/tls.h"
53
54 ${include_all_option_headers}
55
56 #line 57 "${template}"
57
58 #include "util/output.h"
59 #include "options/options_holder.h"
60 #include "cvc4autoconfig.h"
61 #include "options/base_options_handlers.h"
62
63 ${option_handler_includes}
64
65 #line 66 "${template}"
66
67 using namespace CVC4;
68 using namespace CVC4::options;
69
70 namespace CVC4 {
71
72 CVC4_THREADLOCAL(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 T handle(std::string option, std::string optionarg) {
93 try {
94 Integer i(optionarg, 10);
95
96 if(! std::numeric_limits<T>::is_signed && i < 0) {
97 // unsigned type but user gave negative argument
98 throw OptionException(option + " requires a nonnegative argument");
99 } else if(i < std::numeric_limits<T>::min()) {
100 // negative overflow for type
101 std::stringstream ss;
102 ss << option << " requires an argument >= " << std::numeric_limits<T>::min();
103 throw OptionException(ss.str());
104 } else if(i > std::numeric_limits<T>::max()) {
105 // positive overflow for type
106 std::stringstream ss;
107 ss << option << " requires an argument <= " << std::numeric_limits<T>::max();
108 throw OptionException(ss.str());
109 }
110
111 if(std::numeric_limits<T>::is_signed) {
112 return T(i.getLong());
113 } else {
114 return T(i.getUnsignedLong());
115 }
116 } catch(std::invalid_argument&) {
117 // user gave something other than an integer
118 throw OptionException(option + " requires an integer argument");
119 }
120 }
121 };/* struct OptionHandler<T, true, true> */
122
123 /** Variant for numeric but non-integral C++ types */
124 template <class T>
125 struct OptionHandler<T, true, false> {
126 static T handle(std::string option, std::string optionarg) {
127 std::stringstream in(optionarg);
128 long double r;
129 in >> r;
130 if(! in.eof()) {
131 // we didn't consume the whole string (junk at end)
132 throw OptionException(option + " requires a numeric argument");
133 }
134
135 if(! std::numeric_limits<T>::is_signed && r < 0.0) {
136 // unsigned type but user gave negative value
137 throw OptionException(option + " requires a nonnegative argument");
138 } else if(r < -std::numeric_limits<T>::max()) {
139 // negative overflow for type
140 std::stringstream ss;
141 ss << option << " requires an argument >= " << -std::numeric_limits<T>::max();
142 throw OptionException(ss.str());
143 } else if(r > std::numeric_limits<T>::max()) {
144 // positive overflow for type
145 std::stringstream ss;
146 ss << option << " requires an argument <= " << std::numeric_limits<T>::max();
147 throw OptionException(ss.str());
148 }
149
150 return T(r);
151 }
152 };/* struct OptionHandler<T, true, false> */
153
154 /** Variant for non-numeric C++ types */
155 template <class T>
156 struct OptionHandler<T, false, false> {
157 static T handle(std::string option, std::string optionarg) {
158 T::unsupported_handleOption_call___please_write_me;
159 // The above line causes a compiler error if this version of the template
160 // is ever instantiated (meaning that a specialization is missing). So
161 // don't worry about the segfault in the next line, the "return" is only
162 // there to keep the compiler from giving additional, distracting errors
163 // and warnings.
164 return *(T*)0;
165 }
166 };/* struct OptionHandler<T, false, false> */
167
168 /** Handle an option of type T in the default way. */
169 template <class T>
170 T handleOption(std::string option, std::string optionarg) {
171 return OptionHandler<T, std::numeric_limits<T>::is_specialized, std::numeric_limits<T>::is_integer>::handle(option, optionarg);
172 }
173
174 /** Handle an option of type std::string in the default way. */
175 template <>
176 std::string handleOption<std::string>(std::string option, std::string optionarg) {
177 return optionarg;
178 }
179
180 /**
181 * Run handler, and any user-given predicates, for option T.
182 * If a user specifies a :handler or :predicates, it overrides this.
183 */
184 template <class T>
185 typename T::type runHandlerAndPredicates(T, std::string option, std::string optionarg, SmtEngine* smt) {
186 // By default, parse the option argument in a way appropriate for its type.
187 // E.g., for "unsigned int" options, ensure that the provided argument is
188 // a nonnegative integer that fits in the unsigned int type.
189
190 return handleOption<typename T::type>(option, optionarg);
191 }
192
193 template <class T>
194 void runBoolPredicates(T, std::string option, bool b, SmtEngine* smt) {
195 // By default, nothing to do for bool. Users add things with
196 // :predicate in options files to provide custom checking routines
197 // that can throw exceptions.
198 }
199
200 ${all_custom_handlers}
201
202 #line 203 "${template}"
203
204 #ifdef CVC4_DEBUG
205 # define USE_EARLY_TYPE_CHECKING_BY_DEFAULT true
206 #else /* CVC4_DEBUG */
207 # define USE_EARLY_TYPE_CHECKING_BY_DEFAULT false
208 #endif /* CVC4_DEBUG */
209
210 #if defined(CVC4_MUZZLED) || defined(CVC4_COMPETITION_MODE)
211 # define DO_SEMANTIC_CHECKS_BY_DEFAULT false
212 #else /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
213 # define DO_SEMANTIC_CHECKS_BY_DEFAULT true
214 #endif /* CVC4_MUZZLED || CVC4_COMPETITION_MODE */
215
216 Options::Options() :
217 d_holder(new options::OptionsHolder()) {
218 }
219
220 Options::Options(const Options& options) :
221 d_holder(new options::OptionsHolder(*options.d_holder)) {
222 }
223
224 Options::~Options() {
225 delete d_holder;
226 }
227
228 options::OptionsHolder::OptionsHolder() : ${all_modules_defaults}
229 {
230 }
231
232 #line 233 "${template}"
233
234 static const std::string mostCommonOptionsDescription = "\
235 Most commonly-used CVC4 options:${common_documentation}";
236
237 #line 238 "${template}"
238
239 static const std::string optionsDescription = mostCommonOptionsDescription + "\n\
240 \n\
241 Additional CVC4 options:${remaining_documentation}";
242
243 #line 244 "${template}"
244
245 static const std::string optionsFootnote = "\n\
246 [*] Each of these options has a --no-OPTIONNAME variant, which reverses the\n\
247 sense of the option.\n\
248 ";
249
250 static const std::string languageDescription = "\
251 Languages currently supported as arguments to the -L / --lang option:\n\
252 auto attempt to automatically determine language\n\
253 cvc4 | presentation | pl CVC4 presentation language\n\
254 smt1 | smtlib1 SMT-LIB format 1.2\n\
255 smt | smtlib | smt2 | smtlib2 SMT-LIB format 2.0\n\
256 tptp TPTP format (cnf and fof)\n\
257 \n\
258 Languages currently supported as arguments to the --output-lang option:\n\
259 auto match output language to input language\n\
260 cvc4 | presentation | pl CVC4 presentation language\n\
261 smt1 | smtlib1 SMT-LIB format 1.2\n\
262 smt | smtlib | smt2 | smtlib2 SMT-LIB format 2.0\n\
263 z3str SMT-LIB 2.0 with Z3-str string constraints\n\
264 tptp TPTP format\n\
265 ast internal format (simple syntax trees)\n\
266 ";
267
268 std::string Options::getDescription() const {
269 return optionsDescription;
270 }
271
272 void Options::printUsage(const std::string msg, std::ostream& out) {
273 out << msg << optionsDescription << std::endl
274 << optionsFootnote << std::endl << std::flush;
275 }
276
277 void Options::printShortUsage(const std::string msg, std::ostream& out) {
278 out << msg << mostCommonOptionsDescription << std::endl
279 << optionsFootnote << std::endl
280 << "For full usage, please use --help." << std::endl << std::endl << std::flush;
281 }
282
283 void Options::printLanguageHelp(std::ostream& out) {
284 out << languageDescription << std::flush;
285 }
286
287 /**
288 * This is a table of long options. By policy, each short option
289 * should have an equivalent long option (but the reverse isn't the
290 * case), so this table should thus contain all command-line options.
291 *
292 * Each option in this array has four elements:
293 *
294 * 1. the long option string
295 * 2. argument behavior for the option:
296 * no_argument - no argument permitted
297 * required_argument - an argument is expected
298 * optional_argument - an argument is permitted but not required
299 * 3. this is a pointer to an int which is set to the 4th entry of the
300 * array if the option is present; or NULL, in which case
301 * getopt_long() returns the 4th entry
302 * 4. the return value for getopt_long() when this long option (or the
303 * value to set the 3rd entry to; see #3)
304 *
305 * If you add something here, you should add it in src/main/usage.h
306 * also, to document it.
307 *
308 * If you add something that has a short option equivalent, you should
309 * add it to the getopt_long() call in parseOptions().
310 */
311 static struct option cmdlineOptions[] = {${all_modules_long_options}
312 { NULL, no_argument, NULL, '\0' }
313 };/* cmdlineOptions */
314
315 #line 316 "${template}"
316
317 static void preemptGetopt(int& argc, char**& argv, const char* opt) {
318 const size_t maxoptlen = 128;
319
320 Debug("preemptGetopt") << "preempting getopt() with " << opt << std::endl;
321
322 AlwaysAssert(opt != NULL && *opt != '\0');
323 AlwaysAssert(strlen(opt) <= maxoptlen);
324
325 ++argc;
326 unsigned i = 1;
327 while(argv[i] != NULL && argv[i][0] != '\0') {
328 ++i;
329 }
330
331 if(argv[i] == NULL) {
332 argv = (char**) realloc(argv, (i + 6) * sizeof(char*));
333 for(unsigned j = i; j < i + 5; ++j) {
334 argv[j] = (char*) malloc(sizeof(char) * maxoptlen);
335 argv[j][0] = '\0';
336 }
337 argv[i + 5] = NULL;
338 }
339
340 strncpy(argv[i], opt, maxoptlen - 1);
341 argv[i][maxoptlen - 1] = '\0'; // ensure NUL-termination even on overflow
342 }
343
344 namespace options {
345
346 /** Set a given Options* as "current" just for a particular scope. */
347 class OptionsGuard {
348 CVC4_THREADLOCAL_TYPE(Options*)* d_field;
349 Options* d_old;
350 public:
351 OptionsGuard(CVC4_THREADLOCAL_TYPE(Options*)* field, Options* opts) :
352 d_field(field),
353 d_old(*field) {
354 *field = opts;
355 }
356 ~OptionsGuard() {
357 *d_field = d_old;
358 }
359 };/* class OptionsGuard */
360
361 }/* CVC4::options namespace */
362
363 /**
364 * Parse argc/argv and put the result into a CVC4::Options.
365 * The return value is what's left of the command line (that is, the
366 * non-option arguments).
367 */
368 std::vector<std::string> Options::parseOptions(int argc, char* main_argv[]) throw(OptionException) {
369 options::OptionsGuard guard(&s_current, this);
370
371 const char *progName = main_argv[0];
372 SmtEngine* const smt = NULL;
373
374 Debug("options") << "main_argv == " << main_argv << std::endl;
375
376 // Reset getopt(), in the case of multiple calls to parseOptions().
377 // This can be = 1 in newer GNU getopt, but older (< 2007) require = 0.
378 optind = 0;
379 #if HAVE_DECL_OPTRESET
380 optreset = 1; // on BSD getopt() (e.g. Mac OS), might need this
381 #endif /* HAVE_DECL_OPTRESET */
382
383 // find the base name of the program
384 const char *x = strrchr(progName, '/');
385 if(x != NULL) {
386 progName = x + 1;
387 }
388 d_holder->binary_name = std::string(progName);
389
390 int extra_argc = 1;
391 char **extra_argv = (char**) malloc(2 * sizeof(char*));
392 extra_argv[0] = NULL;
393 extra_argv[1] = NULL;
394
395 int extra_optind = 0, main_optind = 0;
396 int old_optind;
397 int *optind_ref = &main_optind;
398
399 char** argv = main_argv;
400
401 std::vector<std::string> nonOptions;
402
403 for(;;) {
404 int c = -1;
405 optopt = 0;
406 std::string option, optionarg;
407 Debug("preemptGetopt") << "top of loop, extra_optind == " << extra_optind << ", extra_argc == " << extra_argc << std::endl;
408 if((extra_optind == 0 ? 1 : extra_optind) < extra_argc) {
409 #if HAVE_DECL_OPTRESET
410 if(optind_ref != &extra_optind) {
411 optreset = 1; // on BSD getopt() (e.g. Mac OS), might need this
412 }
413 #endif /* HAVE_DECL_OPTRESET */
414 old_optind = optind = extra_optind;
415 optind_ref = &extra_optind;
416 argv = extra_argv;
417 Debug("preemptGetopt") << "in preempt code, next arg is " << extra_argv[optind == 0 ? 1 : optind] << std::endl;
418 if(extra_argv[extra_optind == 0 ? 1 : extra_optind][0] != '-') {
419 InternalError("preempted args cannot give non-options command-line args (found `%s')", extra_argv[extra_optind == 0 ? 1 : extra_optind]);
420 }
421 c = getopt_long(extra_argc, extra_argv,
422 "+:${all_modules_short_options}",
423 cmdlineOptions, NULL);
424 Debug("preemptGetopt") << "in preempt code, c == " << c << " (`" << char(c) << "') optind == " << optind << std::endl;
425 if(optopt == 0 ||
426 ( optopt >= ${long_option_value_begin} && optopt <= ${long_option_value_end} )) {
427 // long option
428 option = argv[old_optind == 0 ? 1 : old_optind];
429 optionarg = (optarg == NULL) ? "" : optarg;
430 } else {
431 // short option
432 option = std::string("-") + char(optopt);
433 optionarg = (optarg == NULL) ? "" : optarg;
434 }
435 if(optind >= extra_argc) {
436 Debug("preemptGetopt") << "-- no more preempt args" << std::endl;
437 unsigned i = 1;
438 while(extra_argv[i] != NULL && extra_argv[i][0] != '\0') {
439 extra_argv[i][0] = '\0';
440 ++i;
441 }
442 extra_argc = 1;
443 extra_optind = 0;
444 } else {
445 Debug("preemptGetopt") << "-- more preempt args" << std::endl;
446 extra_optind = optind;
447 }
448 }
449 if(c == -1) {
450 #if HAVE_DECL_OPTRESET
451 if(optind_ref != &main_optind) {
452 optreset = 1; // on BSD getopt() (e.g. Mac OS), might need this
453 }
454 #endif /* HAVE_DECL_OPTRESET */
455 old_optind = optind = main_optind;
456 optind_ref = &main_optind;
457 argv = main_argv;
458 if(main_optind < argc && main_argv[main_optind][0] != '-') {
459 do {
460 if(main_optind != 0) {
461 nonOptions.push_back(main_argv[main_optind]);
462 }
463 ++main_optind;
464 } while(main_optind < argc && main_argv[main_optind][0] != '-');
465 continue;
466 }
467 Debug("options") << "[ before, optind == " << optind << " ]" << std::endl;
468 #if defined(__MINGW32__) || defined(__MINGW64__)
469 if(optreset == 1 && optind > 1) {
470 // on mingw, optreset will reset the optind, so we have to
471 // manually advance argc, argv
472 main_argv[optind - 1] = main_argv[0];
473 argv = main_argv += optind - 1;
474 argc -= optind - 1;
475 old_optind = optind = main_optind = 1;
476 if(argc > 0) {
477 Debug("options") << "looking at : " << argv[0] << std::endl;
478 }
479 /*c = getopt_long(argc, main_argv,
480 "+:${all_modules_short_options}",
481 cmdlineOptions, NULL);
482 Debug("options") << "pre-emptory c is " << c << " (" << char(c) << ")" << std::endl;
483 Debug("options") << "optind was reset to " << optind << std::endl;
484 optind = main_optind;
485 Debug("options") << "I restored optind to " << optind << std::endl;*/
486 }
487 #endif /* __MINGW32__ || __MINGW64__ */
488 Debug("options") << "[ argc == " << argc << ", main_argv == " << main_argv << " ]" << std::endl;
489 c = getopt_long(argc, main_argv,
490 "+:${all_modules_short_options}",
491 cmdlineOptions, NULL);
492 main_optind = optind;
493 Debug("options") << "[ got " << int(c) << " (" << char(c) << ") ]" << std::endl;
494 Debug("options") << "[ next option will be at pos: " << optind << " ]" << std::endl;
495 if(c == -1) {
496 Debug("options") << "done with option parsing" << std::endl;
497 break;
498 }
499 option = argv[old_optind == 0 ? 1 : old_optind];
500 optionarg = (optarg == NULL) ? "" : optarg;
501 }
502
503 Debug("preemptGetopt") << "processing option " << c << " (`" << char(c) << "'), " << option << std::endl;
504
505 switch(c) {
506 ${all_modules_option_handlers}
507
508 #line 509 "${template}"
509
510 case ':':
511 // This can be a long or short option, and the way to get at the
512 // name of it is different.
513 throw OptionException(std::string("option `") + option + "' missing its required argument");
514
515 case '?':
516 default:
517 if( ( optopt == 0 || ( optopt >= ${long_option_value_begin} && optopt <= ${long_option_value_end} ) ) &&
518 !strncmp(argv[optind - 1], "--thread", 8) &&
519 strlen(argv[optind - 1]) > 8 ) {
520 if(! isdigit(argv[optind - 1][8])) {
521 throw OptionException(std::string("can't understand option `") + option + "': expected something like --threadN=\"--option1 --option2\", where N is a nonnegative integer");
522 }
523 std::vector<std::string>& threadArgv = d_holder->threadArgv;
524 char *end;
525 long tnum = strtol(argv[optind - 1] + 8, &end, 10);
526 if(tnum < 0 || (*end != '\0' && *end != '=')) {
527 throw OptionException(std::string("can't understand option `") + option + "': expected something like --threadN=\"--option1 --option2\", where N is a nonnegative integer");
528 }
529 if(threadArgv.size() <= size_t(tnum)) {
530 threadArgv.resize(tnum + 1);
531 }
532 if(threadArgv[tnum] != "") {
533 threadArgv[tnum] += " ";
534 }
535 if(*end == '\0') { // e.g., we have --thread0 "foo"
536 if(argc <= optind) {
537 throw OptionException(std::string("option `") + option + "' missing its required argument");
538 }
539 Debug("options") << "thread " << tnum << " gets option " << argv[optind] << std::endl;
540 threadArgv[tnum] += argv[(*optind_ref)++];
541 } else { // e.g., we have --thread0="foo"
542 if(end[1] == '\0') {
543 throw OptionException(std::string("option `") + option + "' missing its required argument");
544 }
545 Debug("options") << "thread " << tnum << " gets option " << (end + 1) << std::endl;
546 threadArgv[tnum] += end + 1;
547 }
548 Debug("options") << "thread " << tnum << " now has " << threadArgv[tnum] << std::endl;
549 break;
550 }
551
552 throw OptionException(std::string("can't understand option `") + option + "'");
553 }
554 }
555
556 Debug("options") << "returning " << nonOptions.size() << " non-option arguments." << std::endl;
557
558 return nonOptions;
559 }
560
561 std::vector<std::string> Options::suggestCommandLineOptions(const std::string& optionName) throw() {
562 std::vector<std::string> suggestions;
563
564 const char* opt;
565 for(size_t i = 0; (opt = cmdlineOptions[i].name) != NULL; ++i) {
566 if(std::strstr(opt, optionName.c_str()) != NULL) {
567 suggestions.push_back(opt);
568 }
569 }
570
571 return suggestions;
572 }
573
574 static const char* smtOptions[] = {
575 ${all_modules_smt_options},
576 #line 577 "${template}"
577 NULL
578 };/* smtOptions[] */
579
580 std::vector<std::string> Options::suggestSmtOptions(const std::string& optionName) throw() {
581 std::vector<std::string> suggestions;
582
583 const char* opt;
584 for(size_t i = 0; (opt = smtOptions[i]) != NULL; ++i) {
585 if(std::strstr(opt, optionName.c_str()) != NULL) {
586 suggestions.push_back(opt);
587 }
588 }
589
590 return suggestions;
591 }
592
593 SExpr Options::getOptions() const throw() {
594 std::vector<SExpr> opts;
595
596 ${all_modules_get_options}
597
598 #line 599 "${template}"
599
600 return SExpr(opts);
601 }
602
603 #undef USE_EARLY_TYPE_CHECKING_BY_DEFAULT
604 #undef DO_SEMANTIC_CHECKS_BY_DEFAULT
605
606 }/* CVC4 namespace */