Skip to content

Commit 3bd4322

Browse files
committed
Change the unstructured asserts to invalid_user_input_exceptions and preconditions/invariants.
1 parent 9fc6eb2 commit 3bd4322

File tree

6 files changed

+41
-19
lines changed

6 files changed

+41
-19
lines changed

src/cbmc/bmc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Author: Daniel Kroening, [email protected]
1414
#include <chrono>
1515
#include <iostream>
1616

17+
#include <util/exception_utils.h>
1718
#include <util/exit_codes.h>
1819

1920
#include <langapi/language_util.h>
@@ -214,9 +215,8 @@ void bmct::get_memory_model()
214215
memory_model=util_make_unique<memory_model_psot>(ns);
215216
else
216217
{
217-
error() << "Invalid memory model " << mm
218-
<< " -- use one of sc, tso, pso" << eom;
219-
throw "invalid memory model";
218+
throw invalid_user_input_exceptiont(
219+
"Invalid parameter " + mm, "--mm", "Try values of sc, tso, pso");
220220
}
221221
}
222222

src/cbmc/bmc_cover.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ bool bmc_covert::operator()()
239239
cover_goals.add(l);
240240
}
241241

242-
INVARIANT(
243-
cover_goals.size() == goal_map.size(), "we add coverage for each goal");
242+
INVARIANT(cover_goals.size() == goal_map.size(),
243+
"we add coverage for each goal");
244244

245245

246246
status() << "Running " << solver.decision_procedure_text() << eom;

src/cbmc/cbmc_solvers.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ Author: Daniel Kroening, [email protected]
1414
#include <fstream>
1515
#include <iostream>
1616
#include <memory>
17+
#include <string>
1718

19+
#include <util/exception_utils.h>
1820
#include <util/make_unique.h>
1921
#include <util/unicode.h>
2022
#include <util/version.h>
@@ -164,8 +166,10 @@ std::unique_ptr<cbmc_solverst::solvert> cbmc_solverst::get_smt2(
164166
{
165167
if(solver==smt2_dect::solvert::GENERIC)
166168
{
167-
error() << "please use --outfile" << eom;
168-
throw 0;
169+
throw invalid_user_input_exceptiont(
170+
"required filename not provided",
171+
"--outfile",
172+
"provide a filename with --outfile");
169173
}
170174

171175
auto smt2_dec = util_make_unique<smt2_dect>(
@@ -207,8 +211,8 @@ std::unique_ptr<cbmc_solverst::solvert> cbmc_solverst::get_smt2(
207211

208212
if(!*out)
209213
{
210-
error() << "failed to open " << filename << eom;
211-
throw 0;
214+
throw invalid_user_input_exceptiont(
215+
"failed to open file: " + filename, "--outfile");
212216
}
213217

214218
auto smt2_conv = util_make_unique<smt2_convt>(
@@ -232,18 +236,32 @@ void cbmc_solverst::no_beautification()
232236
{
233237
if(options.get_bool_option("beautify"))
234238
{
235-
error() << "sorry, this solver does not support beautification" << eom;
236-
throw 0;
239+
throw invalid_user_input_exceptiont(
240+
"the chosen solver does not support beautification", "--beautify");
237241
}
238242
}
239243

240244
void cbmc_solverst::no_incremental_check()
241245
{
242-
if(options.get_bool_option("all-properties") ||
243-
options.get_option("cover")!="" ||
244-
options.get_option("incremental-check")!="")
246+
const bool all_properties = options.get_bool_option("all-properties");
247+
const bool cover = options.is_set("cover");
248+
const bool incremental_check = options.is_set("incremental-check");
249+
250+
if(all_properties)
251+
{
252+
throw invalid_user_input_exceptiont(
253+
"the chosen solver does not support incremental solving",
254+
"--all_properties");
255+
}
256+
else if(cover)
257+
{
258+
throw invalid_user_input_exceptiont(
259+
"the chosen solver does not support incremental solving", "--cover");
260+
}
261+
else if(incremental_check)
245262
{
246-
error() << "sorry, this solver does not support incremental solving" << eom;
247-
throw 0;
263+
throw invalid_user_input_exceptiont(
264+
"the chosen solver does not support incremental solving",
265+
"--incremental-check");
248266
}
249267
}

src/cbmc/cbmc_solvers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ class cbmc_solverst:public messaget
6868

6969
prop_convt &prop_conv() const
7070
{
71-
PRECONDITION(prop_conv_ptr!=nullptr);
71+
PRECONDITION(prop_conv_ptr != nullptr);
7272
return *prop_conv_ptr;
7373
}
7474

7575
propt &prop() const
7676
{
77-
PRECONDITION(prop_ptr!=nullptr);
77+
PRECONDITION(prop_ptr != nullptr);
7878
return *prop_ptr;
7979
}
8080

src/util/exception_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ std::string invalid_user_input_exceptiont::what() const noexcept
1515
res += "Option: " + option + "\n";
1616
res += "Reason: " + reason;
1717
// Print an optional correct usage message assuming correct input parameters have been passed
18-
res += correct_input.empty() ? "\n" : " Try: " + correct_input + "\n";
18+
res += correct_input.empty() ? "\n" : correct_input + "\n";
1919
return res;
2020
}

src/util/exception_utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ Author: Fotis Koutoulakis, [email protected]
1313

1414
class invalid_user_input_exceptiont
1515
{
16+
/// The reason this exception was generated.
1617
std::string reason;
18+
/// The full command line option (not the argument) that got
19+
/// erroneous input.
1720
std::string option;
21+
/// In case we have samples of correct input to the option.
1822
std::string correct_input;
1923

2024
public:

0 commit comments

Comments
 (0)