Skip to content

Commit 63543a5

Browse files
committed
Translate asserts and throws to PRECONDITIONS and INVARIANTS
1 parent ddd6d14 commit 63543a5

20 files changed

+227
-203
lines changed

src/goto-symex/build_goto_trace.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ exprt build_full_lhs_rec(
9191
else if(id==ID_byte_extract_little_endian ||
9292
id==ID_byte_extract_big_endian)
9393
{
94-
exprt tmp=src_original;
9594
DATA_INVARIANT(
96-
tmp.operands().size() == 2,
97-
"byte_extract_exprt should have two operands.");
95+
src_original.operands().size() == 2,
96+
"byte extract expressions require two operands");
97+
exprt tmp=src_original;
9898
tmp.op0()=build_full_lhs_rec(prop_conv, ns, tmp.op0(), src_ssa.op0());
9999

100100
// re-write into big case-split
@@ -231,7 +231,7 @@ void build_goto_trace(
231231
else if(it->is_atomic_end() && current_time<0)
232232
current_time*=-1;
233233

234-
assert(current_time>=0);
234+
INVARIANT(current_time >= 0, "time keeping inconsistency");
235235
// move any steps gathered in an atomic section
236236

237237
if(time_before<0)

src/goto-symex/goto_symex_state.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ Author: Daniel Kroening, [email protected]
1414
#include <cstdlib>
1515
#include <iostream>
1616

17-
#include <util/invariant.h>
1817
#include <util/base_exceptions.h>
19-
#include <util/std_expr.h>
18+
#include <util/exception_utils.h>
19+
#include <util/invariant.h>
2020
#include <util/prefix.h>
21+
#include <util/std_expr.h>
2122

2223
#include <analyses/dirty.h>
2324

@@ -51,8 +52,7 @@ void goto_symex_statet::level0t::operator()(
5152

5253
const symbolt *s;
5354
const bool found_l0 = !ns.lookup(obj_identifier, s);
54-
DATA_INVARIANT(
55-
found_l0, "level0: failed to find " + id2string(obj_identifier));
55+
INVARIANT(found_l0, "level0: failed to find " + id2string(obj_identifier));
5656

5757
// don't rename shared variables or functions
5858
if(s->type.id()==ID_code ||
@@ -188,7 +188,7 @@ bool goto_symex_statet::constant_propagation_reference(const exprt &expr) const
188188
else if(expr.id()==ID_member)
189189
{
190190
DATA_INVARIANT(
191-
expr.operands().size() == 1, "member_exprt takes one operand.");
191+
expr.operands().size() == 1, "member expression expects one operand");
192192

193193
return constant_propagation_reference(expr.op0());
194194
}
@@ -345,7 +345,8 @@ void goto_symex_statet::assignment(
345345

346346
// see #305 on GitHub for a simple example and possible discussion
347347
if(is_shared && lhs.type().id() == ID_pointer && !allow_pointer_unsoundness)
348-
throw "pointer handling for concurrency is unsound";
348+
throw unsupported_operation_exceptiont(
349+
"pointer handling for concurrency is unsound");
349350

350351
// for value propagation -- the RHS is L2
351352

@@ -495,12 +496,12 @@ void goto_symex_statet::rename(
495496
}
496497
else if(expr.id()==ID_address_of)
497498
{
499+
DATA_INVARIANT(
500+
expr.type().id() == ID_pointer,
501+
"type of expression required to be pointer");
498502
DATA_INVARIANT(
499503
expr.operands().size() == 1, "address_of should have a single operand");
500504
rename_address(expr.op0(), ns, level);
501-
DATA_INVARIANT(
502-
expr.type().id() == ID_pointer,
503-
"type of address_of should be ID_pointer");
504505
expr.type().subtype()=expr.op0().type();
505506
}
506507
else

src/goto-symex/partial_order_concurrency.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ symbol_exprt partial_order_concurrencyt::clock(
142142
event_it event,
143143
axiomt axiom)
144144
{
145-
irep_idt identifier;
146145
PRECONDITION(!numbering.empty());
146+
irep_idt identifier;
147147

148148
if(event->is_shared_write())
149149
identifier=rw_clock_id(event, axiom);
@@ -198,7 +198,7 @@ exprt partial_order_concurrencyt::before(
198198
binary_relation_exprt(clock(e1, ax), ID_lt, clock(e2, ax)));
199199
}
200200

201-
assert(!ops.empty());
201+
POSTCONDITION(!ops.empty());
202202

203203
return conjunction(ops);
204204
}

src/goto-symex/postcondition.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,23 @@ bool postconditiont::is_used_address_of(
7777
else if(expr.id()==ID_index)
7878
{
7979
DATA_INVARIANT(
80-
expr.operands().size() == 2, "index_exprt takes two operands.");
81-
80+
expr.operands().size() == 2, "index expression should have two operands");
8281
return
8382
is_used_address_of(expr.op0(), identifier) ||
8483
is_used(expr.op1(), identifier);
8584
}
8685
else if(expr.id()==ID_member)
8786
{
8887
DATA_INVARIANT(
89-
expr.operands().size() == 1, "member_exprt takes one operand.");
88+
expr.operands().size() == 1,
89+
"member expression should only have one operand");
9090
return is_used_address_of(expr.op0(), identifier);
9191
}
9292
else if(expr.id()==ID_dereference)
9393
{
9494
DATA_INVARIANT(
95-
expr.operands().size() == 1, "dereference_exprt takes one operand.");
95+
expr.operands().size() == 1,
96+
"dereference expression should only have one operand");
9697
return is_used(expr.op0(), identifier);
9798
}
9899

@@ -159,7 +160,8 @@ bool postconditiont::is_used(
159160
{
160161
// only do index!
161162
DATA_INVARIANT(
162-
expr.operands().size() == 1, "address_of_exprt takes one operand.");
163+
expr.operands().size() == 1,
164+
"address_of expression expected to have one operand at this point");
163165
return is_used_address_of(expr.op0(), identifier);
164166
}
165167
else if(expr.id()==ID_symbol &&
@@ -174,7 +176,8 @@ bool postconditiont::is_used(
174176
else if(expr.id()==ID_dereference)
175177
{
176178
DATA_INVARIANT(
177-
expr.operands().size() == 1, "dereference_exprt takes one operand.");
179+
expr.operands().size() == 1,
180+
"dereference expression expected to have one operand");
178181

179182
// aliasing may happen here
180183

src/goto-symex/precondition.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,23 @@ void preconditiont::compute_address_of(exprt &dest)
7979
else if(dest.id()==ID_index)
8080
{
8181
DATA_INVARIANT(
82-
dest.operands().size() == 2, "index_exprt takes two operands.");
82+
dest.operands().size() == 2,
83+
"index expression expected to have two operands");
8384
compute_address_of(dest.op0());
8485
compute(dest.op1());
8586
}
8687
else if(dest.id()==ID_member)
8788
{
8889
DATA_INVARIANT(
89-
dest.operands().size() == 1, "member_exprt takes one operand.");
90+
dest.operands().size() == 1,
91+
"member expression expected to have one operand");
9092
compute_address_of(dest.op0());
9193
}
9294
else if(dest.id()==ID_dereference)
9395
{
9496
DATA_INVARIANT(
95-
dest.operands().size() == 1, "dereference_exprt takes one operand.");
97+
dest.operands().size() == 1,
98+
"dereference expression expected to have 1 operand");
9699
compute(dest.op0());
97100
}
98101
}
@@ -108,13 +111,15 @@ void preconditiont::compute_rec(exprt &dest)
108111
{
109112
// only do index!
110113
DATA_INVARIANT(
111-
dest.operands().size() == 1, "address_of_exprt takes one operand.");
114+
dest.operands().size() == 1,
115+
"address_of expression expected to have one operand at this point");
112116
compute_address_of(dest.op0());
113117
}
114118
else if(dest.id()==ID_dereference)
115119
{
116120
DATA_INVARIANT(
117-
dest.operands().size() == 1, "dereference_exprt takes one operand.");
121+
dest.operands().size() == 1,
122+
"dereference expression expected to have one operand at this point");
118123

119124
const irep_idt &lhs_identifier=SSA_step.ssa_lhs.get_object_name();
120125

src/goto-symex/show_vcc.cpp

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

22+
#include <util/exception_utils.h>
2223
#include <util/json.h>
2324
#include <util/json_expr.h>
2425
#include <util/ui_message.h>
@@ -165,7 +166,8 @@ void show_vcc(
165166
{
166167
of.open(filename);
167168
if(!of)
168-
throw "failed to open file " + filename;
169+
throw invalid_user_input_exceptiont(
170+
"invalid file to read trace from: " + filename, "--outfile");
169171
}
170172

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

src/goto-symex/slice_by_trace.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ Author: Daniel Kroening, [email protected]
1616
#include <fstream>
1717
#include <iostream>
1818

19-
#include <util/string2int.h>
20-
#include <util/simplify_expr.h>
2119
#include <util/arith_tools.h>
22-
#include <util/std_expr.h>
23-
#include <util/guard.h>
20+
#include <util/exception_utils.h>
2421
#include <util/format_expr.h>
22+
#include <util/guard.h>
23+
#include <util/simplify_expr.h>
24+
#include <util/std_expr.h>
25+
#include <util/string2int.h>
2526

2627
void symex_slice_by_tracet::slice_by_trace(
2728
std::string trace_files,
@@ -79,6 +80,11 @@ void symex_slice_by_tracet::slice_by_trace(
7980
{
8081
exprt g_copy(*i);
8182

83+
DATA_INVARIANT(
84+
g_copy.id() == ID_symbol || g_copy.id() == ID_not ||
85+
g_copy.id() == ID_and || g_copy.id() == ID_constant,
86+
"guards should only be and, symbol, constant, or `not'");
87+
8288
if(g_copy.id()==ID_symbol || g_copy.id() == ID_not)
8389
{
8490
g_copy.make_not();
@@ -92,10 +98,6 @@ void symex_slice_by_tracet::slice_by_trace(
9298
simplify(copy_last, ns);
9399
implications.insert(copy_last);
94100
}
95-
else if(!(g_copy.id()==ID_constant))
96-
{
97-
throw "guards should only be and, symbol, constant, or `not'";
98-
}
99101
}
100102

101103
slice_SSA_steps(equation, implications); // Slice based on implications
@@ -122,7 +124,8 @@ void symex_slice_by_tracet::read_trace(std::string filename)
122124
std::cout << "Reading trace from file " << filename << '\n';
123125
std::ifstream file(filename);
124126
if(file.fail())
125-
throw "failed to read from trace file";
127+
throw invalid_user_input_exceptiont(
128+
"invalid file to read trace from: " + filename, "");
126129

127130
// In case not the first trace read
128131
alphabet.clear();
@@ -219,9 +222,10 @@ void symex_slice_by_tracet::parse_events(std::string read_line)
219222
const std::string::size_type vnext=read_line.find(",", vidx);
220223
std::string event=read_line.substr(vidx, vnext - vidx);
221224
eis.insert(event);
222-
if((!alphabet.empty()) &&
223-
((alphabet.count(event)!=0)!=alphabet_parity))
224-
throw "trace uses symbol not in alphabet: "+event;
225+
PRECONDITION(!alphabet.empty());
226+
INVARIANT(
227+
(alphabet.count(event) != 0) == alphabet_parity,
228+
"trace uses symbol not in alphabet: " + event);
225229
if(vnext==std::string::npos)
226230
break;
227231
vidx=vnext;

0 commit comments

Comments
 (0)