Skip to content

Commit d5cd7bd

Browse files
committed
Translate asserts and throws to PRECONDITIONS and INVARIANTS
1 parent de57387 commit d5cd7bd

21 files changed

+236
-227
lines changed

src/goto-symex/build_goto_trace.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ Author: Daniel Kroening
1515

1616
#include <cassert>
1717

18-
#include <util/threeval.h>
19-
#include <util/simplify_expr.h>
2018
#include <util/arith_tools.h>
19+
#include <util/byte_operators.h>
20+
#include <util/simplify_expr.h>
21+
#include <util/threeval.h>
2122

2223
#include <solvers/prop/prop_conv.h>
2324
#include <solvers/prop/prop.h>
@@ -92,10 +93,11 @@ 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(
97+
prop_conv,
98+
ns,
99+
to_byte_extract_expr(tmp).op(),
100+
to_byte_extract_expr(src_ssa).op());
99101

100102
// re-write into big case-split
101103
}
@@ -231,7 +233,7 @@ void build_goto_trace(
231233
else if(it->is_atomic_end() && current_time<0)
232234
current_time*=-1;
233235

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

237239
if(time_before<0)

src/goto-symex/goto_symex_state.cpp

Lines changed: 10 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,7 @@ 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(to_member_expr(expr).compound());
194191
}
195192
else if(expr.id()==ID_string_constant)
196193
return true;
@@ -345,7 +342,8 @@ void goto_symex_statet::assignment(
345342

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

350348
// for value propagation -- the RHS is L2
351349

@@ -495,12 +493,12 @@ void goto_symex_statet::rename(
495493
}
496494
else if(expr.id()==ID_address_of)
497495
{
496+
DATA_INVARIANT(
497+
expr.type().id() == ID_pointer,
498+
"type of expression required to be pointer");
498499
DATA_INVARIANT(
499500
expr.operands().size() == 1, "address_of should have a single operand");
500501
rename_address(expr.op0(), ns, level);
501-
DATA_INVARIANT(
502-
expr.type().id() == ID_pointer,
503-
"type of address_of should be ID_pointer");
504502
expr.type().subtype()=expr.op0().type();
505503
}
506504
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: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,16 @@ 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-
82-
return
83-
is_used_address_of(expr.op0(), identifier) ||
84-
is_used(expr.op1(), identifier);
79+
return is_used_address_of(to_index_expr(expr).array(), identifier) ||
80+
is_used(to_index_expr(expr).index(), identifier);
8581
}
8682
else if(expr.id()==ID_member)
8783
{
88-
DATA_INVARIANT(
89-
expr.operands().size() == 1, "member_exprt takes one operand.");
90-
return is_used_address_of(expr.op0(), identifier);
84+
return is_used_address_of(to_member_expr(expr).compound(), identifier);
9185
}
9286
else if(expr.id()==ID_dereference)
9387
{
94-
DATA_INVARIANT(
95-
expr.operands().size() == 1, "dereference_exprt takes one operand.");
96-
return is_used(expr.op0(), identifier);
88+
return is_used(to_dereference_expr(expr).pointer(), identifier);
9789
}
9890

9991
return false;
@@ -157,10 +149,7 @@ bool postconditiont::is_used(
157149
{
158150
if(expr.id()==ID_address_of)
159151
{
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);
152+
return is_used_address_of(to_address_of_expr(expr).object(), identifier);
164153
}
165154
else if(expr.id()==ID_symbol &&
166155
expr.get_bool(ID_C_SSA_symbol))
@@ -173,13 +162,9 @@ bool postconditiont::is_used(
173162
}
174163
else if(expr.id()==ID_dereference)
175164
{
176-
DATA_INVARIANT(
177-
expr.operands().size() == 1, "dereference_exprt takes one operand.");
178-
179165
// aliasing may happen here
180-
181166
value_setst::valuest expr_set;
182-
value_set.get_value_set(expr.op0(), expr_set, ns);
167+
value_set.get_value_set(to_dereference_expr(expr).pointer(), expr_set, ns);
183168
std::unordered_set<irep_idt> symbols;
184169

185170
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)