Fix numerous compiler warnings on various platforms
[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 tptp TPTP format\n\
264 ast internal format (simple syntax trees)\n\
265 ";
266
267 std::string Options::getDescription() const {
268 return optionsDescription;
269 }
270
271 void Options::printUsage(const std::string msg, std::ostream& out) {
272 out << msg << optionsDescription << std::endl
273 << optionsFootnote << std::endl << std::flush;
274 }
275
276 void Options::printShortUsage(const std::string msg, std::ostream& out) {
277 out << msg << mostCommonOptionsDescription << std::endl
278 << optionsFootnote << std::endl
279 << "For full usage, please use --help." << std::endl << std::endl << std::flush;
280 }
281
282 void Options::printLanguageHelp(std::ostream& out) {
283 out << languageDescription << std::flush;
284 }
285
286 /**
287 * This is a table of long options. By policy, each short option
288 * should have an equivalent long option (but the reverse isn't the
289 * case), so this table should thus contain all command-line options.
290 *
291 * Each option in this array has four elements:
292 *
293 * 1. the long option string
294 * 2. argument behavior for the option:
295 * no_argument - no argument permitted
296 * required_argument - an argument is expected
297 * optional_argument - an argument is permitted but not required
298 * 3. this is a pointer to an int which is set to the 4th entry of the
299 * array if the option is present; or NULL, in which case
300 * getopt_long() returns the 4th entry
301 * 4. the return value for getopt_long() when this long option (or the
302 * value to set the 3rd entry to; see #3)
303 *
304 * If you add something here, you should add it in src/main/usage.h
305 * also, to document it.
306 *
307 * If you add something that has a short option equivalent, you should
308 * add it to the getopt_long() call in parseOptions().
309 */
310 static struct option cmdlineOptions[] = {${all_modules_long_options}
311 { NULL, no_argument, NULL, '\0' }
312 };/* cmdlineOptions */
313
314 #line 315 "${template}"
315
316 static void preemptGetopt(int& argc, char**& argv, const char* opt) {
317 const size_t maxoptlen = 128;
318
319 Debug("preemptGetopt") << "preempting getopt() with " << opt << std::endl;
320
321 AlwaysAssert(opt != NULL && *opt != '\0');
322 AlwaysAssert(strlen(opt) <= maxoptlen);
323
324 ++argc;
325 unsigned i = 1;
326 while(argv[i] != NULL && argv[i][0] != '\0') {
327 ++i;
328 }
329
330 if(argv[i] == NULL) {
331 argv = (char**) realloc(argv, (i + 6) * sizeof(char*));
332 for(unsigned j = i; j < i + 5; ++j) {
333 argv[j] = (char*) malloc(sizeof(char) * maxoptlen);
334 argv[j][0] = '\0';
335 }
336 argv[i + 5] = NULL;
337 }
338
339 strncpy(argv[i], opt, maxoptlen - 1);
340 argv[i][maxoptlen - 1] = '\0'; // ensure NUL-termination even on overflow
341 }
342
343 namespace options {
344
345 /** Set a given Options* as "current" just for a particular scope. */
346 class OptionsGuard {
347 CVC4_THREADLOCAL_TYPE(Options*)* d_field;
348 Options* d_old;
349 public:
350 OptionsGuard(CVC4_THREADLOCAL_TYPE(Options*)* field, Options* opts) :
351 d_field(field),
352 d_old(*field) {
353 *field = opts;
354 }
355 ~OptionsGuard() {
356 *d_field = d_old;
357 }
358 };/* class OptionsGuard */
359
360 }/* CVC4::options namespace */
361
362 /**
363 * Parse argc/argv and put the result into a CVC4::Options.
364 * The return value is what's left of the command line (that is, the
365 * non-option arguments).
366 */
367 std::vector<std::string> Options::parseOptions(int argc, char* main_argv[]) throw(OptionException) {
368 options::OptionsGuard guard(&s_current, this);
369
370 const char *progName = main_argv[0];
371 SmtEngine* const smt = NULL;
372
373 Debug("options") << "main_argv == " << main_argv << std::endl;
374
375 // Reset getopt(), in the case of multiple calls to parseOptions().
376 // This can be = 1 in newer GNU getopt, but older (< 2007) require = 0.
377 optind = 0;
378 #if HAVE_DECL_OPTRESET
379 optreset = 1; // on BSD getopt() (e.g. Mac OS), might need this
380 #endif /* HAVE_DECL_OPTRESET */
381
382 // find the base name of the program
383 const char *x = strrchr(progName, '/');
384 if(x != NULL) {
385 progName = x + 1;
386 }
387 d_holder->binary_name = std::string(progName);
388
389 int extra_argc = 1;
390 char **extra_argv = (char**) malloc(2 * sizeof(char*));
391 extra_argv[0] = NULL;
392 extra_argv[1] = NULL;
393
394 int extra_optind = 0, main_optind = 0;
395 int old_optind;
396 int *optind_ref = &main_optind;
397
398 char** argv = main_argv;
399
400 std::vector<std::string> nonOptions;
401
402 for(;;) {
403 int c = -1;
404 optopt = 0;
405 std::string option, optionarg;
406 Debug("preemptGetopt") << "top of loop, extra_optind == " << extra_optind << ", extra_argc == " << extra_argc << std::endl;
407 if((extra_optind == 0 ? 1 : extra_optind) < extra_argc) {
408 #if HAVE_DECL_OPTRESET
409 if(optind_ref != &extra_optind) {
410 optreset = 1; // on BSD getopt() (e.g. Mac OS), might need this
411 }
412 #endif /* HAVE_DECL_OPTRESET */
413 old_optind = optind = extra_optind;
414 optind_ref = &extra_optind;
415 argv = extra_argv;
416 Debug("preemptGetopt") << "in preempt code, next arg is " << extra_argv[optind == 0 ? 1 : optind] << std::endl;
417 if(extra_argv[extra_optind == 0 ? 1 : extra_optind][0] != '-') {
418 InternalError("preempted args cannot give non-options command-line args (found `%s')", extra_argv[extra_optind == 0 ? 1 : extra_optind]);
419 }
420 c = getopt_long(extra_argc, extra_argv,
421 "+:${all_modules_short_options}",
422 cmdlineOptions, NULL);
423 Debug("preemptGetopt") << "in preempt code, c == " << c << " (`" << char(c) << "') optind == " << optind << std::endl;
424 if(optopt == 0 ||
425 ( optopt >= ${long_option_value_begin} && optopt <= ${long_option_value_end} )) {
426 // long option
427 option = argv[old_optind == 0 ? 1 : old_optind];
428 optionarg = (optarg == NULL) ? "" : optarg;
429 } else {
430 // short option
431 option = std::string("-") + char(optopt);
432 optionarg = (optarg == NULL) ? "" : optarg;
433 }
434 if(optind >= extra_argc) {
435 Debug("preemptGetopt") << "-- no more preempt args" << std::endl;
436 unsigned i = 1;
437 while(extra_argv[i] != NULL && extra_argv[i][0] != '\0') {
438 extra_argv[i][0] = '\0';
439 ++i;
440 }
441 extra_argc = 1;
442 extra_optind = 0;
443 } else {
444 Debug("preemptGetopt") << "-- more preempt args" << std::endl;
445 extra_optind = optind;
446 }
447 }
448 if(c == -1) {
449 #if HAVE_DECL_OPTRESET
450 if(optind_ref != &main_optind) {
451 optreset = 1; // on BSD getopt() (e.g. Mac OS), might need this
452 }
453 #endif /* HAVE_DECL_OPTRESET */
454 old_optind = optind = main_optind;
455 optind_ref = &main_optind;
456 argv = main_argv;
457 if(main_optind < argc && main_argv[main_optind][0] != '-') {
458 do {
459 if(main_optind != 0) {
460 nonOptions.push_back(main_argv[main_optind]);
461 }
462 ++main_optind;
463 } while(main_optind < argc && main_argv[main_optind][0] != '-');
464 continue;
465 }
466 Debug("options") << "[ before, optind == " << optind << " ]" << std::endl;
467 #if defined(__MINGW32__) || defined(__MINGW64__)
468 if(optreset == 1 && optind > 1) {
469 // on mingw, optreset will reset the optind, so we have to
470 // manually advance argc, argv
471 main_argv[optind - 1] = main_argv[0];
472 argv = main_argv += optind - 1;
473 argc -= optind - 1;
474 old_optind = optind = main_optind = 1;
475 if(argc > 0) {
476 Debug("options") << "looking at : " << argv[0] << std::endl;
477 }
478 /*c = getopt_long(argc, main_argv,
479 "+:${all_modules_short_options}",
480 cmdlineOptions, NULL);
481 Debug("options") << "pre-emptory c is " << c << " (" << char(c) << ")" << std::endl;
482 Debug("options") << "optind was reset to " << optind << std::endl;
483 optind = main_optind;
484 Debug("options") << "I restored optind to " << optind << std::endl;*/
485 }
486 #endif /* __MINGW32__ || __MINGW64__ */
487 Debug("options") << "[ argc == " << argc << ", main_argv == " << main_argv << " ]" << std::endl;
488 c = getopt_long(argc, main_argv,
489 "+:${all_modules_short_options}",
490 cmdlineOptions, NULL);
491 main_optind = optind;
492 Debug("options") << "[ got " << int(c) << " (" << char(c) << ") ]" << std::endl;
493 Debug("options") << "[ next option will be at pos: " << optind << " ]" << std::endl;
494 if(c == -1) {
495 Debug("options") << "done with option parsing" << std::endl;
496 break;
497 }
498 option = argv[old_optind == 0 ? 1 : old_optind];
499 optionarg = (optarg == NULL) ? "" : optarg;
500 }
501
502 Debug("preemptGetopt") << "processing option " << c << " (`" << char(c) << "'), " << option << std::endl;
503
504 switch(c) {
505 ${all_modules_option_handlers}
506
507 #line 508 "${template}"
508
509 case ':':
510 // This can be a long or short option, and the way to get at the
511 // name of it is different.
512 throw OptionException(std::string("option `") + option + "' missing its required argument");
513
514 case '?':
515 default:
516 if( ( optopt == 0 || ( optopt >= ${long_option_value_begin} && optopt <= ${long_option_value_end} ) ) &&
517 !strncmp(argv[optind - 1], "--thread", 8) &&
518 strlen(argv[optind - 1]) > 8 ) {
519 if(! isdigit(argv[optind - 1][8])) {
520 throw OptionException(std::string("can't understand option `") + option + "': expected something like --threadN=\"--option1 --option2\", where N is a nonnegative integer");
521 }
522 std::vector<std::string>& threadArgv = d_holder->threadArgv;
523 char *end;
524 long tnum = strtol(argv[optind - 1] + 8, &end, 10);
525 if(tnum < 0 || (*end != '\0' && *end != '=')) {
526 throw OptionException(std::string("can't understand option `") + option + "': expected something like --threadN=\"--option1 --option2\", where N is a nonnegative integer");
527 }
528 if(threadArgv.size() <= size_t(tnum)) {
529 threadArgv.resize(tnum + 1);
530 }
531 if(threadArgv[tnum] != "") {
532 threadArgv[tnum] += " ";
533 }
534 if(*end == '\0') { // e.g., we have --thread0 "foo"
535 if(argc <= optind) {
536 throw OptionException(std::string("option `") + option + "' missing its required argument");
537 }
538 Debug("options") << "thread " << tnum << " gets option " << argv[optind] << std::endl;
539 threadArgv[tnum] += argv[(*optind_ref)++];
540 } else { // e.g., we have --thread0="foo"
541 if(end[1] == '\0') {
542 throw OptionException(std::string("option `") + option + "' missing its required argument");
543 }
544 Debug("options") << "thread " << tnum << " gets option " << (end + 1) << std::endl;
545 threadArgv[tnum] += end + 1;
546 }
547 Debug("options") << "thread " << tnum << " now has " << threadArgv[tnum] << std::endl;
548 break;
549 }
550
551 throw OptionException(std::string("can't understand option `") + option + "'");
552 }
553 }
554
555 if((*this)[options::incrementalSolving] && (*this)[options::proof]) {
556 throw OptionException(std::string("The use of --incremental with --proof is not yet supported"));
557 }
558
559 Debug("options") << "returning " << nonOptions.size() << " non-option arguments." << std::endl;
560
561 return nonOptions;
562 }
563
564 std::vector<std::string> Options::suggestCommandLineOptions(const std::string& optionName) throw() {
565 std::vector<std::string> suggestions;
566
567 const char* opt;
568 for(size_t i = 0; (opt = cmdlineOptions[i].name) != NULL; ++i) {
569 if(std::strstr(opt, optionName.c_str()) != NULL) {
570 suggestions.push_back(opt);
571 }
572 }
573
574 return suggestions;
575 }
576
577 static const char* smtOptions[] = {
578 ${all_modules_smt_options},
579 #line 580 "${template}"
580 NULL
581 };/* smtOptions[] */
582
583 std::vector<std::string> Options::suggestSmtOptions(const std::string& optionName) throw() {
584 std::vector<std::string> suggestions;
585
586 const char* opt;
587 for(size_t i = 0; (opt = smtOptions[i]) != NULL; ++i) {
588 if(std::strstr(opt, optionName.c_str()) != NULL) {
589 suggestions.push_back(opt);
590 }
591 }
592
593 return suggestions;
594 }
595
596 SExpr Options::getOptions() const throw() {
597 std::vector<SExpr> opts;
598
599 ${all_modules_get_options}
600
601 #line 602 "${template}"
602
603 return SExpr(opts);
604 }
605
606 #undef USE_EARLY_TYPE_CHECKING_BY_DEFAULT
607 #undef DO_SEMANTIC_CHECKS_BY_DEFAULT
608
609 }/* CVC4 namespace */