Skip to content

Commit 9fc6eb2

Browse files
committed
Implementation of a erroneous user input exception class.
1 parent 972d007 commit 9fc6eb2

File tree

6 files changed

+83
-18
lines changed

6 files changed

+83
-18
lines changed

src/cbmc/cbmc_main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int main(int argc, const char **argv)
4343
#endif
4444
cbmc_parse_optionst parse_options(argc, argv);
4545

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

4848
#ifdef IREP_HASH_STATS
4949
std::cout << "IREP_HASH_CNT=" << irep_hash_cnt << '\n';

src/cbmc/cbmc_parse_options.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ class optionst;
7676
"(claim):(show-claims)(floatbv)(all-claims)(all-properties)" // legacy, and will eventually disappear // NOLINT(whitespace/line_length)
7777
// clang-format on
7878

79-
class cbmc_parse_optionst:
80-
public parse_options_baset,
81-
public xml_interfacet,
82-
public messaget
79+
class cbmc_parse_optionst : public parse_options_baset,
80+
public xml_interfacet,
81+
public messaget
8382
{
8483
public:
8584
virtual int doit() override;

src/util/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ SRC = arith_tools.cpp \
1313
expr.cpp \
1414
expr_initializer.cpp \
1515
expr_util.cpp \
16+
exception_utils.cpp \
1617
file_util.cpp \
1718
find_macros.cpp \
1819
find_symbols.cpp \

src/util/exception_utils.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*******************************************************************\
2+
3+
Module: Exception helper utilities
4+
5+
Author: Fotis Koutoulakis, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#include "exception_utils.h"
10+
11+
std::string invalid_user_input_exceptiont::what() const noexcept
12+
{
13+
std::string res;
14+
res += "\nInvalid User Input\n";
15+
res += "Option: " + option + "\n";
16+
res += "Reason: " + reason;
17+
// Print an optional correct usage message assuming correct input parameters have been passed
18+
res += correct_input.empty() ? "\n" : " Try: " + correct_input + "\n";
19+
return res;
20+
}

src/util/exception_utils.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*******************************************************************\
2+
3+
Module: Exception helper utilities
4+
5+
Author: Fotis Koutoulakis, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_UTIL_EXCEPTION_UTILS_H
10+
#define CPROVER_UTIL_EXCEPTION_UTILS_H
11+
12+
#include <string>
13+
14+
class invalid_user_input_exceptiont
15+
{
16+
std::string reason;
17+
std::string option;
18+
std::string correct_input;
19+
20+
public:
21+
invalid_user_input_exceptiont(
22+
std::string reason,
23+
std::string option,
24+
std::string correct_input = "")
25+
: reason(reason), option(option), correct_input(correct_input)
26+
{
27+
}
28+
29+
std::string what() const noexcept;
30+
};
31+
32+
#endif // CPROVER_UTIL_EXCEPTION_UTILS_H

src/util/parse_options.cpp

Lines changed: 26 additions & 13 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(
@@ -47,23 +49,34 @@ void parse_options_baset::unknown_option_msg()
4749

4850
int parse_options_baset::main()
4951
{
50-
if(parse_result)
52+
// catch all exceptions here so that this code is not duplicated
53+
// for each tool
54+
try
5155
{
52-
usage_error();
53-
unknown_option_msg();
54-
return EX_USAGE;
56+
if(parse_result)
57+
{
58+
usage_error();
59+
unknown_option_msg();
60+
return EX_USAGE;
61+
}
62+
63+
if(cmdline.isset('?') || cmdline.isset('h') || cmdline.isset("help"))
64+
{
65+
help();
66+
return EX_OK;
67+
}
68+
69+
// install signal catcher
70+
install_signal_catcher();
71+
72+
return doit();
5573
}
56-
57-
if(cmdline.isset('?') || cmdline.isset('h') || cmdline.isset("help"))
74+
catch(invalid_user_input_exceptiont &e)
5875
{
59-
help();
60-
return EX_OK;
76+
std::cerr << e.what() << "\n";
77+
return CPROVER_EXIT_USAGE_ERROR;
6178
}
62-
63-
// install signal catcher
64-
install_signal_catcher();
65-
66-
return doit();
79+
return CPROVER_EXIT_SUCCESS;
6780
}
6881

6982
std::string

0 commit comments

Comments
 (0)