Skip to content

Commit 37d6b83

Browse files
committed
Translate asserts and throws to PRECONDITIONS and INVARIANTS
1 parent ddd6d14 commit 37d6b83

20 files changed

+227
-224
lines changed

src/goto-symex/build_goto_trace.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening
1818
#include <util/threeval.h>
1919
#include <util/simplify_expr.h>
2020
#include <util/arith_tools.h>
21+
#include <util/byte_operators.h>
2122

2223
#include <solvers/prop/prop_conv.h>
2324
#include <solvers/prop/prop.h>
@@ -92,10 +93,7 @@ exprt build_full_lhs_rec(
9293
id==ID_byte_extract_big_endian)
9394
{
9495
exprt tmp=src_original;
95-
DATA_INVARIANT(
96-
tmp.operands().size() == 2,
97-
"byte_extract_exprt should have two operands.");
98-
tmp.op0()=build_full_lhs_rec(prop_conv, ns, tmp.op0(), src_ssa.op0());
96+
tmp.op0()=build_full_lhs_rec(prop_conv, ns, to_byte_extract_expr(tmp).op(), to_byte_extract_expr(src_ssa).op());
9997

10098
// re-write into big case-split
10199
}
@@ -231,7 +229,7 @@ void build_goto_trace(
231229
else if(it->is_atomic_end() && current_time<0)
232230
current_time*=-1;
233231

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

237235
if(time_before<0)

src/goto-symex/goto_symex_state.cpp

Lines changed: 11 additions & 12 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 ||
@@ -187,10 +187,8 @@ bool goto_symex_statet::constant_propagation_reference(const exprt &expr) const
187187
}
188188
else if(expr.id()==ID_member)
189189
{
190-
DATA_INVARIANT(
191-
expr.operands().size() == 1, "member_exprt takes one operand.");
192-
193-
return constant_propagation_reference(expr.op0());
190+
return constant_propagation_reference(
191+
to_member_expr(expr).compound());
194192
}
195193
else if(expr.id()==ID_string_constant)
196194
return true;
@@ -345,7 +343,8 @@ void goto_symex_statet::assignment(
345343

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

350349
// for value propagation -- the RHS is L2
351350

@@ -495,12 +494,12 @@ void goto_symex_statet::rename(
495494
}
496495
else if(expr.id()==ID_address_of)
497496
{
497+
DATA_INVARIANT(
498+
expr.type().id() == ID_pointer,
499+
"type of expression required to be pointer");
498500
DATA_INVARIANT(
499501
expr.operands().size() == 1, "address_of should have a single operand");
500502
rename_address(expr.op0(), ns, level);
501-
DATA_INVARIANT(
502-
expr.type().id() == ID_pointer,
503-
"type of address_of should be ID_pointer");
504503
expr.type().subtype()=expr.op0().type();
505504
}
506505
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: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,19 @@ bool postconditiont::is_used_address_of(
7676
}
7777
else if(expr.id()==ID_index)
7878
{
79-
DATA_INVARIANT(
80-
expr.operands().size() == 2, "index_exprt takes two operands.");
81-
8279
return
83-
is_used_address_of(expr.op0(), identifier) ||
84-
is_used(expr.op1(), identifier);
80+
is_used_address_of(to_index_expr(expr).array(), identifier) ||
81+
is_used(to_index_expr(expr).index(), identifier);
8582
}
8683
else if(expr.id()==ID_member)
8784
{
88-
DATA_INVARIANT(
89-
expr.operands().size() == 1, "member_exprt takes one operand.");
90-
return is_used_address_of(expr.op0(), identifier);
85+
return is_used_address_of(
86+
to_member_expr(expr).compound(), identifier);
9187
}
9288
else if(expr.id()==ID_dereference)
9389
{
94-
DATA_INVARIANT(
95-
expr.operands().size() == 1, "dereference_exprt takes one operand.");
96-
return is_used(expr.op0(), identifier);
90+
return is_used(
91+
to_dereference_expr(expr).pointer(), identifier);
9792
}
9893

9994
return false;
@@ -157,10 +152,8 @@ bool postconditiont::is_used(
157152
{
158153
if(expr.id()==ID_address_of)
159154
{
160-
// only do index!
161-
DATA_INVARIANT(
162-
expr.operands().size() == 1, "address_of_exprt takes one operand.");
163-
return is_used_address_of(expr.op0(), identifier);
155+
return is_used_address_of(
156+
to_address_of_expr(expr).object(), identifier);
164157
}
165158
else if(expr.id()==ID_symbol &&
166159
expr.get_bool(ID_C_SSA_symbol))
@@ -173,13 +166,10 @@ bool postconditiont::is_used(
173166
}
174167
else if(expr.id()==ID_dereference)
175168
{
176-
DATA_INVARIANT(
177-
expr.operands().size() == 1, "dereference_exprt takes one operand.");
178-
179169
// aliasing may happen here
180-
181170
value_setst::valuest expr_set;
182-
value_set.get_value_set(expr.op0(), expr_set, ns);
171+
value_set.get_value_set(
172+
to_dereference_expr(expr).pointer(), expr_set, ns);
183173
std::unordered_set<irep_idt> symbols;
184174

185175
for(value_setst::valuest::const_iterator

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)