From 54148758dba8e3523fa1c746922692d8ad3df6e8 Mon Sep 17 00:00:00 2001 From: Gereon Kremer Date: Wed, 8 Sep 2021 11:41:50 -0700 Subject: [PATCH] Refactor code generation for options.h/.cpp (#7126) This PR refactors the options code generation for the options/options.h/.cpp files. --- src/options/mkoptions.py | 107 ++++++++++++++++++------------- src/options/options_template.cpp | 19 ++---- 2 files changed, 67 insertions(+), 59 deletions(-) diff --git a/src/options/mkoptions.py b/src/options/mkoptions.py index 55e047047..d50681eb7 100644 --- a/src/options/mkoptions.py +++ b/src/options/mkoptions.py @@ -176,41 +176,6 @@ TPL_MODE_HANDLER_CASE = \ }}""" -def get_module_headers(modules): - """Render includes for module headers""" - return concat_format('#include "{header}"', modules) - - -def get_holder_fwd_decls(modules): - """Render forward declaration of holder structs""" - return concat_format(' struct Holder{id_cap};', modules) - - -def get_holder_mem_decls(modules): - """Render declarations of holder members of the Option class""" - return concat_format(' std::unique_ptr d_{id};', modules) - - -def get_holder_mem_inits(modules): - """Render initializations of holder members of the Option class""" - return concat_format(' d_{id}(std::make_unique()),', modules) - - -def get_holder_ref_inits(modules): - """Render initializations of holder references of the Option class""" - return concat_format(' {id}(*d_{id}),', modules) - - -def get_holder_mem_copy(modules): - """Render copy operation of holder members of the Option class""" - return concat_format(' *d_{id} = *options.d_{id};', modules) - - -def get_holder_ref_decls(modules): - """Render reference declarations for holder members of the Option class""" - return concat_format(' options::Holder{id_cap}& {id};', modules) - - def get_handler(option): """Render handler call for assignment functions""" optname = option.long_name if option.long else "" @@ -339,6 +304,56 @@ def generate_get_impl(modules): return self.long_name if self.long_name else self.name +################################################################################ +################################################################################ +# code generation functions + +################################################################################ +# for options/options.h + + +def generate_holder_fwd_decls(modules): + """Render forward declaration of holder structs""" + return concat_format(' struct Holder{id_cap};', modules) + + +def generate_holder_mem_decls(modules): + """Render declarations of holder members of the Option class""" + return concat_format( + ' std::unique_ptr d_{id};', modules) + + +def generate_holder_ref_decls(modules): + """Render reference declarations for holder members of the Option class""" + return concat_format(' options::Holder{id_cap}& {id};', modules) + + +################################################################################ +# for options/options.cpp + + +def generate_module_headers(modules): + """Render includes for module headers""" + return concat_format('#include "{header}"', modules) + + +def generate_holder_mem_inits(modules): + """Render initializations of holder members of the Option class""" + return concat_format( + ' d_{id}(std::make_unique()),', + modules) + + +def generate_holder_ref_inits(modules): + """Render initializations of holder references of the Option class""" + return concat_format(' {id}(*d_{id}),', modules) + + +def generate_holder_mem_copy(modules): + """Render copy operation of holder members of the Option class""" + return concat_format(' *d_{id} = *options.d_{id};', modules) + + class SphinxGenerator: def __init__(self): self.common = [] @@ -745,9 +760,7 @@ def add_getopt_long(long_name, argument_req, getopt_long): def codegen_all_modules(modules, build_dir, dst_dir, tpls): - """ - Generate code for all option modules (options.cpp). - """ + """Generate code for all option modules.""" headers_module = [] # generated *_options.h header includes headers_handler = set() # option includes (for handlers, predicates, ...) @@ -920,18 +933,20 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpls): options_handler.extend(cases) data = { - 'holder_fwd_decls': get_holder_fwd_decls(modules), - 'holder_mem_decls': get_holder_mem_decls(modules), - 'holder_ref_decls': get_holder_ref_decls(modules), - 'headers_module': get_module_headers(modules), - 'headers_handler': '\n'.join(sorted(list(headers_handler))), - 'holder_mem_inits': get_holder_mem_inits(modules), - 'holder_ref_inits': get_holder_ref_inits(modules), - 'holder_mem_copy': get_holder_mem_copy(modules), + # options/options.h + 'holder_fwd_decls': generate_holder_fwd_decls(modules), + 'holder_mem_decls': generate_holder_mem_decls(modules), + 'holder_ref_decls': generate_holder_ref_decls(modules), + # options/options.cpp + 'headers_module': generate_module_headers(modules), + 'holder_mem_inits': generate_holder_mem_inits(modules), + 'holder_ref_inits': generate_holder_ref_inits(modules), + 'holder_mem_copy': generate_holder_mem_copy(modules), # options/options_public.cpp 'options_includes': generate_public_includes(modules), 'getnames_impl': generate_getnames_impl(modules), 'get_impl': generate_get_impl(modules), + 'headers_handler': '\n'.join(sorted(list(headers_handler))), 'cmdline_options': '\n '.join(getopt_long), 'help_common': '\n'.join(help_common), 'help_others': '\n'.join(help_others), diff --git a/src/options/options_template.cpp b/src/options/options_template.cpp index 06a423438..c6f24a41d 100644 --- a/src/options/options_template.cpp +++ b/src/options/options_template.cpp @@ -16,21 +16,12 @@ #include "options/options.h" #include "base/check.h" -#include "base/exception.h" -#include "base/output.h" -#include "options/language.h" +#include "base/cvc5config.h" #include "options/options_handler.h" #include "options/options_listener.h" // clang-format off ${headers_module}$ - -#include "base/cvc5config.h" - -${headers_handler}$ - -using namespace cvc5; -using namespace cvc5::options; // clang-format on namespace cvc5 @@ -49,13 +40,15 @@ ${holder_ref_inits}$ Options::~Options() {} -void Options::copyValues(const Options& options){ - if(this != &options) { + void Options::copyValues(const Options& options) + { + if (this != &options) + { // clang-format off ${holder_mem_copy}$ // clang-format on + } } -} } // namespace cvc5 -- 2.30.2