Skip to content

Commit 0248d40

Browse files
committed
Add a parse_options class for a better suited exception handling location
1 parent 662f90a commit 0248d40

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

src/cbmc/cbmc_main.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ int wmain(int argc, const wchar_t **argv_wide)
4141
int main(int argc, const char **argv)
4242
{
4343
#endif
44-
try
45-
{
46-
cbmc_parse_optionst parse_options(argc, argv);
44+
cbmc_parse_optionst parse_options(argc, argv);
4745

48-
int res = parse_options.main();
46+
int res=parse_options.main();
4947

5048
#ifdef IREP_HASH_STATS
5149
std::cout << "IREP_HASH_CNT=" << irep_hash_cnt << '\n';
@@ -54,9 +52,4 @@ int main(int argc, const char **argv)
5452
#endif
5553

5654
return res;
57-
}
58-
catch(invalid_user_input_exceptiont &e)
59-
{
60-
std::cerr << e.what() << std::endl;
61-
}
6255
}

src/cbmc/cbmc_parse_options.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Author: Daniel Kroening, [email protected]
6767
#include "version.h"
6868

6969
cbmc_parse_optionst::cbmc_parse_optionst(int argc, const char **argv):
70-
parse_options_baset(CBMC_OPTIONS, argc, argv),
70+
parse_optionst(CBMC_OPTIONS, argc, argv, ui_message_handler),
7171
xml_interfacet(cmdline),
7272
messaget(ui_message_handler),
7373
ui_message_handler(cmdline, "CBMC " CBMC_VERSION),
@@ -79,7 +79,7 @@ ::cbmc_parse_optionst::cbmc_parse_optionst(
7979
int argc,
8080
const char **argv,
8181
const std::string &extra_options):
82-
parse_options_baset(CBMC_OPTIONS+extra_options, argc, argv),
82+
parse_optionst(CBMC_OPTIONS+extra_options, argc, argv, ui_message_handler),
8383
xml_interfacet(cmdline),
8484
messaget(ui_message_handler),
8585
ui_message_handler(cmdline, "CBMC " CBMC_VERSION),

src/cbmc/cbmc_parse_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class optionst;
7878
// clang-format on
7979

8080
class cbmc_parse_optionst:
81-
public parse_options_baset,
81+
public parse_optionst,
8282
public xml_interfacet,
8383
public messaget
8484
{

src/util/parse_options.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Author: Daniel Kroening, [email protected]
1818
#endif
1919

2020
#include "cmdline.h"
21+
#include "exception_utils.h"
22+
#include "exit_codes.h"
2123
#include "signal_catcher.h"
2224

2325
parse_options_baset::parse_options_baset(
@@ -82,3 +84,19 @@ banner_string(const std::string &front_end, const std::string &version)
8284
return "* *" + std::string(left_padding, ' ') + version_str +
8385
std::string(right_padding, ' ') + "* *";
8486
}
87+
88+
int parse_optionst::main()
89+
{
90+
// catch all exceptions here so that this code is not duplicated
91+
// for each tool
92+
try
93+
{
94+
return parse_options_baset::main();
95+
}
96+
catch(invalid_user_input_exceptiont &e)
97+
{
98+
message.error() << e.what() << messaget::eom;
99+
return CPROVER_EXIT_EXCEPTION;
100+
}
101+
return CPROVER_EXIT_SUCCESS;
102+
}

src/util/parse_options.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Daniel Kroening, [email protected]
1313
#include <string>
1414

1515
#include "cmdline.h"
16+
#include "message.h"
1617

1718
class parse_options_baset
1819
{
@@ -35,6 +36,25 @@ class parse_options_baset
3536
bool parse_result;
3637
};
3738

39+
class parse_optionst: public parse_options_baset
40+
{
41+
public:
42+
parse_optionst(
43+
const std::string &optstring,
44+
int argc,
45+
const char **argv,
46+
message_handlert &message_handler):
47+
parse_options_baset(optstring, argc, argv),
48+
message(message_handler)
49+
{
50+
}
51+
52+
int main() override;
53+
54+
protected:
55+
messaget message;
56+
};
57+
3858
std::string
3959
banner_string(const std::string &front_end, const std::string &version);
4060

0 commit comments

Comments
 (0)