Skip to content

Commit 1c93d0c

Browse files
authored
Move public options functions to separate file (cvc5#6671)
This PR moves the remaining special purpose functions out of the Options class. This set of functions is only used to implement API functions in the smt engine (getting and setting options by string), and by the main driver for parsing and printing usage information.
1 parent 9a09833 commit 1c93d0c

13 files changed

+700
-772
lines changed

.style.yapf

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[style]
2+
based_on_style = pep8
3+
split_before_arithmetic_operator = true
4+
split_before_bitwise_operator = true
5+
split_before_logical_operator = true

src/main/driver_unified.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "main/time_limit.h"
3636
#include "options/base_options.h"
3737
#include "options/options.h"
38+
#include "options/options_public.h"
3839
#include "options/parser_options.h"
3940
#include "options/main_options.h"
4041
#include "options/set_language.h"
@@ -87,9 +88,9 @@ void printUsage(const Options& opts, bool full) {
8788
<< endl
8889
<< "cvc5 options:" << endl;
8990
if(full) {
90-
Options::printUsage(ss.str(), *opts.base.out);
91+
options::printUsage(ss.str(), *opts.base.out);
9192
} else {
92-
Options::printShortUsage(ss.str(), *opts.base.out);
93+
options::printShortUsage(ss.str(), *opts.base.out);
9394
}
9495
}
9596

@@ -103,8 +104,7 @@ int runCvc5(int argc, char* argv[], Options& opts)
103104
progPath = argv[0];
104105

105106
// Parse the options
106-
std::vector<string> filenames =
107-
Options::parseOptions(&opts, argc, argv, progName);
107+
std::vector<string> filenames = options::parse(opts, argc, argv, progName);
108108

109109
auto limit = install_time_limit(opts);
110110

@@ -115,7 +115,7 @@ int runCvc5(int argc, char* argv[], Options& opts)
115115
}
116116
else if (opts.base.languageHelp)
117117
{
118-
Options::printLanguageHelp(*opts.base.out);
118+
options::printLanguageHelp(*opts.base.out);
119119
exit(1);
120120
}
121121
else if (opts.driver.version)

src/options/CMakeLists.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ libcvc5_add_sources(
2929
options_handler.cpp
3030
options_handler.h
3131
options_listener.h
32-
options_public.cpp
3332
options_public.h
3433
outputc.cpp
3534
outputc.h
@@ -67,7 +66,7 @@ set(options_toml_files
6766
string(REPLACE "toml" "cpp;" options_gen_cpp_files ${options_toml_files})
6867
string(REPLACE "toml" "h;" options_gen_h_files ${options_toml_files})
6968

70-
libcvc5_add_sources(GENERATED options.h options.cpp ${options_gen_cpp_files})
69+
libcvc5_add_sources(GENERATED options.h options.cpp options_public.cpp ${options_gen_cpp_files})
7170

7271
list_prepend(options_toml_files "${CMAKE_CURRENT_LIST_DIR}/" abs_toml_files)
7372

@@ -82,7 +81,7 @@ endif()
8281

8382
add_custom_command(
8483
OUTPUT
85-
options.h options.cpp
84+
options.h options.cpp options_public.cpp
8685
${options_gen_cpp_files} ${options_gen_h_files}
8786
${options_gen_doc_files}
8887
COMMAND
@@ -97,6 +96,7 @@ add_custom_command(
9796
${options_toml_files}
9897
module_template.h
9998
module_template.cpp
99+
options_public_template.cpp
100100
options_template.h
101101
options_template.cpp
102102
)
@@ -105,6 +105,7 @@ add_custom_target(gen-options
105105
DEPENDS
106106
options.h
107107
options.cpp
108+
options_public.cpp
108109
${options_gen_cpp_files}
109110
${options_gen_h_files}
110111
)

src/options/base_options.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ public = true
77
category = "undocumented"
88
type = "std::istream*"
99
default = "&std::cin"
10-
includes = ["<iosfwd>"]
10+
includes = ["<iostream>"]
1111

1212
[[option]]
1313
name = "out"
1414
category = "undocumented"
1515
type = "std::ostream*"
1616
default = "&std::cout"
17-
includes = ["<iosfwd>"]
17+
includes = ["<iostream>"]
1818

1919
[[option]]
2020
name = "err"
2121
category = "undocumented"
2222
type = "std::ostream*"
2323
default = "&std::cerr"
24-
includes = ["<iosfwd>"]
24+
includes = ["<iostream>"]
2525

2626
[[option]]
2727
name = "inputLanguage"

src/options/mkoptions.py

+57-66
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
2828
Directory <tpl-src> must contain:
2929
- options_template.cpp
30+
- options_public_template.cpp
3031
- module_template.cpp
3132
- module_template.h
3233
@@ -183,6 +184,11 @@ def concat_format(s, objs):
183184
return '\n'.join([s.format(**o.__dict__) for o in objs])
184185

185186

187+
def get_module_headers(modules):
188+
"""Render includes for module headers"""
189+
return concat_format('#include "{header}"', modules)
190+
191+
186192
def get_holder_fwd_decls(modules):
187193
"""Render forward declaration of holder structs"""
188194
return concat_format(' struct Holder{id_cap};', modules)
@@ -237,6 +243,19 @@ def get_predicates(option):
237243
return ['opts.handler().{}("{}", option, value);'.format(x, optname)
238244
for x in option.predicates]
239245

246+
247+
def get_getall(module, option):
248+
"""Render snippet to add option to result of getAll()"""
249+
if option.type == 'bool':
250+
return 'res.push_back({{"{}", opts.{}.{} ? "true" : "false"}});'.format(
251+
option.long_name, module.id, option.name)
252+
elif is_numeric_cpp_type(option.type):
253+
return 'res.push_back({{"{}", std::to_string(opts.{}.{})}});'.format(
254+
option.long_name, module.id, option.name)
255+
else:
256+
return '{{ std::stringstream ss; ss << opts.{}.{}; res.push_back({{"{}", ss.str()}}); }}'.format(
257+
module.id, option.name, option.long_name)
258+
240259
class Module(object):
241260
"""Options module.
242261
@@ -705,25 +724,26 @@ def add_getopt_long(long_name, argument_req, getopt_long):
705724
'required' if argument_req else 'no', value))
706725

707726

708-
def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_cpp):
727+
def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h,
728+
tpl_options_cpp, tpl_options_public):
709729
"""
710-
Generate code for all option modules (options.cpp, options_holder.h).
730+
Generate code for all option modules (options.cpp).
711731
"""
712732

713733
headers_module = [] # generated *_options.h header includes
714734
headers_handler = set() # option includes (for handlers, predicates, ...)
715735
getopt_short = [] # short options for getopt_long
716736
getopt_long = [] # long options for getopt_long
717-
options_smt = [] # all options names accessible via {set,get}-option
737+
options_getall = [] # options for options::getAll()
718738
options_getoptions = [] # options for Options::getOptions()
719739
options_handler = [] # option handler calls
720-
defaults = [] # default values
721-
custom_handlers = [] # custom handler implementations assign/assignBool
722740
help_common = [] # help text for all common options
723741
help_others = [] # help text for all non-common options
724742
setoption_handlers = [] # handlers for set-option command
725743
getoption_handlers = [] # handlers for get-option command
726744

745+
assign_impls = []
746+
727747
sphinxgen = SphinxGenerator()
728748

729749
for module in modules:
@@ -809,7 +829,7 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_
809829
cond = ' || '.join(
810830
['key == "{}"'.format(x) for x in sorted(keys)])
811831

812-
setoption_handlers.append('if({}) {{'.format(cond))
832+
setoption_handlers.append(' if ({}) {{'.format(cond))
813833
if option.type == 'bool':
814834
setoption_handlers.append(
815835
TPL_CALL_ASSIGN_BOOL.format(
@@ -824,15 +844,15 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_
824844
name=option.name,
825845
option='key'))
826846
elif option.handler:
827-
h = 'handler->{handler}("{smtname}", key'
847+
h = ' opts.handler().{handler}("{smtname}", key'
828848
if argument_req:
829849
h += ', optionarg'
830850
h += ');'
831851
setoption_handlers.append(
832852
h.format(handler=option.handler, smtname=option.long_name))
833853

834-
setoption_handlers.append('return;')
835-
setoption_handlers.append('}')
854+
setoption_handlers.append(' return;')
855+
setoption_handlers.append(' }')
836856

837857
if option.name:
838858
getoption_handlers.append(
@@ -880,80 +900,51 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_
880900
cases.append(' break;')
881901
options_handler.extend(cases)
882902

883-
optname = option.long
884-
# collect options available to the SMT-frontend
885-
if optname:
886-
options_smt.append('"{}",'.format(optname))
887-
888903
if option.name:
889904
# Build options for options::getOptions()
890-
if optname:
891-
# collect SMT option names
892-
options_smt.append('"{}",'.format(optname))
893-
894-
if option.type == 'bool':
895-
s = 'opts.push_back({{"{}", {}.{} ? "true" : "false"}});'.format(
896-
optname, module.id, option.name)
897-
elif is_numeric_cpp_type(option.type):
898-
s = 'opts.push_back({{"{}", std::to_string({}.{})}});'.format(
899-
optname, module.id, option.name)
900-
else:
901-
s = '{{ std::stringstream ss; ss << {}.{}; opts.push_back({{"{}", ss.str()}}); }}'.format(
902-
module.id, option.name, optname)
903-
options_getoptions.append(s)
905+
if option.long_name:
906+
options_getall.append(get_getall(module, option))
904907

905908

906909
# Define handler assign/assignBool
907910
if not mode_handler:
908911
if option.type == 'bool':
909-
custom_handlers.append(TPL_ASSIGN_BOOL.format(
912+
assign_impls.append(TPL_ASSIGN_BOOL.format(
910913
module=module.id,
911914
name=option.name,
912915
handler=handler,
913916
predicates='\n'.join(predicates)
914917
))
915918
elif option.short or option.long:
916-
custom_handlers.append(TPL_ASSIGN.format(
919+
assign_impls.append(TPL_ASSIGN.format(
917920
module=module.id,
918921
name=option.name,
919922
handler=handler,
920923
predicates='\n'.join(predicates)
921924
))
922925

923-
# Default option values
924-
default = option.default if option.default else ''
925-
# Prepend enum name
926-
if option.mode and option.type not in default:
927-
default = '{}::{}'.format(option.type, default)
928-
defaults.append('{}({})'.format(option.name, default))
929-
defaults.append('{}WasSetByUser(false)'.format(option.name))
930-
931-
write_file(dst_dir, 'options.h', tpl_options_h.format(
932-
holder_fwd_decls=get_holder_fwd_decls(modules),
933-
holder_mem_decls=get_holder_mem_decls(modules),
934-
holder_ref_decls=get_holder_ref_decls(modules),
935-
))
936-
937-
write_file(dst_dir, 'options.cpp', tpl_options_cpp.format(
938-
headers_module='\n'.join(headers_module),
939-
headers_handler='\n'.join(sorted(list(headers_handler))),
940-
holder_mem_copy=get_holder_mem_copy(modules),
941-
holder_mem_inits=get_holder_mem_inits(modules),
942-
holder_ref_inits=get_holder_ref_inits(modules),
943-
custom_handlers='\n'.join(custom_handlers),
944-
module_defaults=',\n '.join(defaults),
945-
help_common='\n'.join(help_common),
946-
help_others='\n'.join(help_others),
947-
cmdline_options='\n '.join(getopt_long),
948-
options_short=''.join(getopt_short),
949-
options_handler='\n '.join(options_handler),
950-
option_value_begin=g_getopt_long_start,
951-
option_value_end=g_getopt_long_start + len(getopt_long),
952-
options_smt='\n '.join(options_smt),
953-
options_getoptions='\n '.join(options_getoptions),
954-
setoption_handlers='\n'.join(setoption_handlers),
955-
getoption_handlers='\n'.join(getoption_handlers)
956-
))
926+
data = {
927+
'holder_fwd_decls': get_holder_fwd_decls(modules),
928+
'holder_mem_decls': get_holder_mem_decls(modules),
929+
'holder_ref_decls': get_holder_ref_decls(modules),
930+
'headers_module': get_module_headers(modules),
931+
'headers_handler': '\n'.join(sorted(list(headers_handler))),
932+
'holder_mem_inits': get_holder_mem_inits(modules),
933+
'holder_ref_inits': get_holder_ref_inits(modules),
934+
'holder_mem_copy': get_holder_mem_copy(modules),
935+
'cmdline_options': '\n '.join(getopt_long),
936+
'help_common': '\n'.join(help_common),
937+
'help_others': '\n'.join(help_others),
938+
'options_handler': '\n '.join(options_handler),
939+
'options_short': ''.join(getopt_short),
940+
'assigns': '\n'.join(assign_impls),
941+
'options_getall': '\n '.join(options_getall),
942+
'getoption_handlers': '\n'.join(getoption_handlers),
943+
'setoption_handlers': '\n'.join(setoption_handlers),
944+
}
945+
write_file(dst_dir, 'options.h', tpl_options_h.format(**data))
946+
write_file(dst_dir, 'options.cpp', tpl_options_cpp.format(**data))
947+
write_file(dst_dir, 'options_public.cpp', tpl_options_public.format(**data))
957948

958949
if os.path.isdir('{}/docs/'.format(build_dir)):
959950
sphinxgen.render('{}/docs/'.format(build_dir), 'options_generated.rst')
@@ -1093,6 +1084,7 @@ def mkoptions_main():
10931084
# Read source code template files from source directory.
10941085
tpl_module_h = read_tpl(src_dir, 'module_template.h')
10951086
tpl_module_cpp = read_tpl(src_dir, 'module_template.cpp')
1087+
tpl_options_public = read_tpl(src_dir, 'options_public_template.cpp')
10961088
tpl_options_h = read_tpl(src_dir, 'options_template.h')
10971089
tpl_options_cpp = read_tpl(src_dir, 'options_template.cpp')
10981090

@@ -1113,8 +1105,7 @@ def mkoptions_main():
11131105
codegen_module(module, dst_dir, tpl_module_h, tpl_module_cpp)
11141106

11151107
# Create options.cpp in destination directory
1116-
codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_cpp)
1117-
1108+
codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_cpp, tpl_options_public)
11181109

11191110

11201111
if __name__ == "__main__":

src/options/options_handler.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ void OptionsHandler::checkBvSatSolver(const std::string& option,
155155
throw OptionException(ss.str());
156156
}
157157

158-
if (options::bvSolver() != options::BVSolver::BITBLAST
158+
if (d_options->bv.bvSolver != options::BVSolver::BITBLAST
159159
&& (m == SatSolverMode::CRYPTOMINISAT || m == SatSolverMode::CADICAL
160160
|| m == SatSolverMode::KISSAT))
161161
{
162-
if (options::bitblastMode() == options::BitblastMode::LAZY
162+
if (d_options->bv.bitblastMode == options::BitblastMode::LAZY
163163
&& d_options->bv.bitblastModeWasSetByUser)
164164
{
165165
throwLazyBBUnsupported(m);
@@ -178,9 +178,9 @@ void OptionsHandler::checkBitblastMode(const std::string& option,
178178
options::bv::setDefaultBitvectorEqualitySolver(*d_options, true);
179179
options::bv::setDefaultBitvectorInequalitySolver(*d_options, true);
180180
options::bv::setDefaultBitvectorAlgebraicSolver(*d_options, true);
181-
if (options::bvSatSolver() != options::SatSolverMode::MINISAT)
181+
if (d_options->bv.bvSatSolver != options::SatSolverMode::MINISAT)
182182
{
183-
throwLazyBBUnsupported(options::bvSatSolver());
183+
throwLazyBBUnsupported(d_options->bv.bvSatSolver);
184184
}
185185
}
186186
else if (m == BitblastMode::EAGER)
@@ -195,7 +195,7 @@ void OptionsHandler::setBitblastAig(const std::string& option,
195195
{
196196
if(arg) {
197197
if (d_options->bv.bitblastModeWasSetByUser) {
198-
if (options::bitblastMode() != options::BitblastMode::EAGER)
198+
if (d_options->bv.bitblastMode != options::BitblastMode::EAGER)
199199
{
200200
throw OptionException("bitblast-aig must be used with eager bitblaster");
201201
}

src/options/options_public.cpp

-26
This file was deleted.

0 commit comments

Comments
 (0)