Skip to content

Commit 66fc8a1

Browse files
committed
Change the unstructured asserts to invalid_user_input_exceptions and preconditions/invariants.
1 parent f257667 commit 66fc8a1

8 files changed

+50
-32
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>
@@ -296,9 +297,8 @@ void bmct::get_memory_model()
296297
memory_model=util_make_unique<memory_model_psot>(ns);
297298
else
298299
{
299-
error() << "Invalid memory model " << mm
300-
<< " -- use one of sc, tso, pso" << eom;
301-
throw "invalid memory model";
300+
throw invalid_user_input_exceptiont(
301+
"Invalid parameter " + mm, "--mm", "Try values of sc, tso, pso");
302302
}
303303
}
304304

src/cbmc/bmc_cover.cpp

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

244-
INVARIANT(
245-
cover_goals.size() == goal_map.size(), "we add coverage for each goal");
244+
INVARIANT(cover_goals.size() == goal_map.size(),
245+
"we add coverage for each goal");
246246

247247

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

src/cbmc/bv_cbmc.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ Author: Daniel Kroening, [email protected]
99
#include "bv_cbmc.h"
1010

1111
#include <util/arith_tools.h>
12+
#include <util/invariant.h>
1213
#include <util/replace_expr.h>
1314

1415
bvt bv_cbmct::convert_waitfor(const exprt &expr)
1516
{
16-
if(expr.operands().size()!=4)
17-
{
18-
error().source_location=expr.find_source_location();
19-
error() << "waitfor expected to have four operands" << eom;
20-
throw 0;
21-
}
17+
DATA_INVARIANT(
18+
expr.operands().size() != 4,
19+
"waitfor expression is expected to have four operands");
2220

2321
const exprt &old_cycle=expr.op0();
2422
const exprt &cycle_var=expr.op1();
@@ -27,12 +25,8 @@ bvt bv_cbmct::convert_waitfor(const exprt &expr)
2725
const exprt new_cycle = make_free_bv_expr(expr.type());
2826

2927
mp_integer bound_value;
30-
if(to_integer(bound, bound_value))
31-
{
32-
error().source_location=expr.find_source_location();
33-
error() << "waitfor bound must be a constant" << eom;
34-
throw 0;
35-
}
28+
bool successful_cast = to_integer(bound, bound_value);
29+
INVARIANT(successful_cast, "waitfor bound must be a constant");
3630

3731
{
3832
// constraint: new_cycle>=old_cycle

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>
@@ -169,8 +171,10 @@ std::unique_ptr<cbmc_solverst::solvert> cbmc_solverst::get_smt2(
169171
{
170172
if(solver==smt2_dect::solvert::GENERIC)
171173
{
172-
error() << "please use --outfile" << eom;
173-
throw 0;
174+
throw invalid_user_input_exceptiont(
175+
"required filename not provided",
176+
"--outfile",
177+
"provide a filename with --outfile");
174178
}
175179

176180
auto smt2_dec = util_make_unique<smt2_dect>(
@@ -212,8 +216,8 @@ std::unique_ptr<cbmc_solverst::solvert> cbmc_solverst::get_smt2(
212216

213217
if(!*out)
214218
{
215-
error() << "failed to open " << filename << eom;
216-
throw 0;
219+
throw invalid_user_input_exceptiont(
220+
"failed to open file: " + filename, "--outfile");
217221
}
218222

219223
auto smt2_conv = util_make_unique<smt2_convt>(
@@ -237,18 +241,32 @@ void cbmc_solverst::no_beautification()
237241
{
238242
if(options.get_bool_option("beautify"))
239243
{
240-
error() << "sorry, this solver does not support beautification" << eom;
241-
throw 0;
244+
throw invalid_user_input_exceptiont(
245+
"the chosen solver does not support beautification", "--beautify");
242246
}
243247
}
244248

245249
void cbmc_solverst::no_incremental_check()
246250
{
247-
if(options.get_bool_option("all-properties") ||
248-
options.get_option("cover")!="" ||
249-
options.get_option("incremental-check")!="")
251+
const bool all_properties = options.get_bool_option("all-properties");
252+
const bool cover = options.is_set("cover");
253+
const bool incremental_check = options.is_set("incremental-check");
254+
255+
if(all_properties)
256+
{
257+
throw invalid_user_input_exceptiont(
258+
"the chosen solver does not support incremental solving",
259+
"--all_properties");
260+
}
261+
else if(cover)
262+
{
263+
throw invalid_user_input_exceptiont(
264+
"the chosen solver does not support incremental solving", "--cover");
265+
}
266+
else if(incremental_check)
250267
{
251-
error() << "sorry, this solver does not support incremental solving" << eom;
252-
throw 0;
268+
throw invalid_user_input_exceptiont(
269+
"the chosen solver does not support incremental solving",
270+
"--incremental-check");
253271
}
254272
}

src/cbmc/cbmc_solvers.h

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

7171
prop_convt &prop_conv() const
7272
{
73-
PRECONDITION(prop_conv_ptr!=nullptr);
73+
PRECONDITION(prop_conv_ptr != nullptr);
7474
return *prop_conv_ptr;
7575
}
7676

7777
propt &prop() const
7878
{
79-
PRECONDITION(prop_ptr!=nullptr);
79+
PRECONDITION(prop_ptr != nullptr);
8080
return *prop_ptr;
8181
}
8282

src/cbmc/show_vcc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Author: Daniel Kroening, [email protected]
1717
#include <langapi/mode.h>
1818
#include <langapi/language_util.h>
1919

20+
#include <util/exception_utils.h>
2021
#include <util/json.h>
2122
#include <util/json_expr.h>
2223

@@ -143,7 +144,8 @@ void bmct::show_vcc()
143144
{
144145
of.open(filename);
145146
if(!of)
146-
throw "failed to open file "+filename;
147+
throw invalid_user_input_exceptiont(
148+
"Failed to open file: " + filename, "--outfile");
147149
}
148150

149151
std::ostream &out=have_file?of:std::cout;

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)