Skip to content

Commit 2e9e73d

Browse files
authored
Merge pull request #2902 from NlightNFotis/custom_exception
Cleanup of asserts and throws under the goto-symex directory
2 parents 3dfc784 + 8558c57 commit 2e9e73d

22 files changed

+224
-264
lines changed

src/goto-symex/build_goto_trace.cpp

Lines changed: 9 additions & 5 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,8 +93,11 @@ exprt build_full_lhs_rec(
9293
id==ID_byte_extract_big_endian)
9394
{
9495
exprt tmp=src_original;
95-
assert(tmp.operands().size()==2);
96-
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());
97101

98102
// re-write into big case-split
99103
}
@@ -229,7 +233,7 @@ void build_goto_trace(
229233
else if(it->is_atomic_end() && current_time<0)
230234
current_time*=-1;
231235

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

235239
if(time_before<0)

src/goto-symex/goto_symex_state.cpp

Lines changed: 10 additions & 16 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-
if(expr.operands().size()!=1)
191-
throw "member expects 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,13 +493,9 @@ void goto_symex_statet::rename(
495493
}
496494
else if(expr.id()==ID_address_of)
497495
{
498-
DATA_INVARIANT(
499-
expr.operands().size() == 1, "address_of should have a single operand");
500-
rename_address(expr.op0(), ns, level);
501-
DATA_INVARIANT(
502-
expr.type().id() == ID_pointer,
503-
"type of address_of should be ID_pointer");
504-
expr.type().subtype()=expr.op0().type();
496+
auto &address_of_expr = to_address_of_expr(expr);
497+
rename_address(address_of_expr.object(), ns, level);
498+
expr.type().subtype() = address_of_expr.object().type();
505499
}
506500
else
507501
{

src/goto-symex/memory_model_pso.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ bool memory_model_psot::program_order_is_relaxed(
3030
partial_order_concurrencyt::event_it e1,
3131
partial_order_concurrencyt::event_it e2) const
3232
{
33-
assert(e1->is_shared_read() || e1->is_shared_write());
34-
assert(e2->is_shared_read() || e2->is_shared_write());
33+
PRECONDITION(e1->is_shared_read() || e1->is_shared_write());
34+
PRECONDITION(e2->is_shared_read() || e2->is_shared_write());
3535

3636
// no po relaxation within atomic sections
3737
if(e1->atomic_section_id!=0 &&

src/goto-symex/memory_model_sc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ bool memory_model_sct::program_order_is_relaxed(
3636
partial_order_concurrencyt::event_it e1,
3737
partial_order_concurrencyt::event_it e2) const
3838
{
39-
assert(e1->is_shared_read() || e1->is_shared_write());
40-
assert(e2->is_shared_read() || e2->is_shared_write());
39+
PRECONDITION(e1->is_shared_read() || e1->is_shared_write());
40+
PRECONDITION(e2->is_shared_read() || e2->is_shared_write());
4141

4242
return false;
4343
}

src/goto-symex/memory_model_tso.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ bool memory_model_tsot::program_order_is_relaxed(
3939
partial_order_concurrencyt::event_it e1,
4040
partial_order_concurrencyt::event_it e2) const
4141
{
42-
assert(e1->is_shared_read() || e1->is_shared_write());
43-
assert(e2->is_shared_read() || e2->is_shared_write());
42+
PRECONDITION(e1->is_shared_read() || e1->is_shared_write());
43+
PRECONDITION(e2->is_shared_read() || e2->is_shared_write());
4444

4545
// no po relaxation within atomic sections
4646
if(e1->atomic_section_id!=0 &&

src/goto-symex/partial_order_concurrency.cpp

Lines changed: 3 additions & 3 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+
PRECONDITION(!numbering.empty());
145146
irep_idt identifier;
146-
assert(!numbering.empty());
147147

148148
if(event->is_shared_write())
149149
identifier=rw_clock_id(event, axiom);
@@ -163,7 +163,7 @@ symbol_exprt partial_order_concurrencyt::clock(
163163

164164
void partial_order_concurrencyt::build_clock_type()
165165
{
166-
assert(!numbering.empty());
166+
PRECONDITION(!numbering.empty());
167167

168168
std::size_t width = address_bits(numbering.size());
169169
clock_type = unsignedbv_typet(width);
@@ -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 & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,16 @@ bool postconditiont::is_used_address_of(
7676
}
7777
else if(expr.id()==ID_index)
7878
{
79-
assert(expr.operands().size()==2);
80-
81-
return
82-
is_used_address_of(expr.op0(), identifier) ||
83-
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);
8481
}
8582
else if(expr.id()==ID_member)
8683
{
87-
assert(expr.operands().size()==1);
88-
return is_used_address_of(expr.op0(), identifier);
84+
return is_used_address_of(to_member_expr(expr).compound(), identifier);
8985
}
9086
else if(expr.id()==ID_dereference)
9187
{
92-
assert(expr.operands().size()==1);
93-
return is_used(expr.op0(), identifier);
88+
return is_used(to_dereference_expr(expr).pointer(), identifier);
9489
}
9590

9691
return false;
@@ -154,9 +149,7 @@ bool postconditiont::is_used(
154149
{
155150
if(expr.id()==ID_address_of)
156151
{
157-
// only do index!
158-
assert(expr.operands().size()==1);
159-
return is_used_address_of(expr.op0(), identifier);
152+
return is_used_address_of(to_address_of_expr(expr).object(), identifier);
160153
}
161154
else if(expr.id()==ID_symbol &&
162155
expr.get_bool(ID_C_SSA_symbol))
@@ -169,12 +162,9 @@ bool postconditiont::is_used(
169162
}
170163
else if(expr.id()==ID_dereference)
171164
{
172-
assert(expr.operands().size()==1);
173-
174165
// aliasing may happen here
175-
176166
value_setst::valuest expr_set;
177-
value_set.get_value_set(expr.op0(), expr_set, ns);
167+
value_set.get_value_set(to_dereference_expr(expr).pointer(), expr_set, ns);
178168
std::unordered_set<irep_idt> symbols;
179169

180170
for(value_setst::valuest::const_iterator

src/goto-symex/precondition.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,17 @@ void preconditiont::compute_address_of(exprt &dest)
7878
}
7979
else if(dest.id()==ID_index)
8080
{
81-
assert(dest.operands().size()==2);
82-
compute_address_of(dest.op0());
83-
compute(dest.op1());
81+
auto &index_expr = to_index_expr(dest);
82+
compute_address_of(index_expr.array());
83+
compute(index_expr.index());
8484
}
8585
else if(dest.id()==ID_member)
8686
{
87-
assert(dest.operands().size()==1);
88-
compute_address_of(dest.op0());
87+
compute_address_of(to_member_expr(dest).compound());
8988
}
9089
else if(dest.id()==ID_dereference)
9190
{
92-
assert(dest.operands().size()==1);
93-
compute(dest.op0());
91+
compute(to_dereference_expr(dest).pointer());
9492
}
9593
}
9694

@@ -104,19 +102,18 @@ void preconditiont::compute_rec(exprt &dest)
104102
if(dest.id()==ID_address_of)
105103
{
106104
// only do index!
107-
assert(dest.operands().size()==1);
108-
compute_address_of(dest.op0());
105+
compute_address_of(to_address_of_expr(dest).object());
109106
}
110107
else if(dest.id()==ID_dereference)
111108
{
112-
assert(dest.operands().size()==1);
109+
auto &deref_expr = to_dereference_expr(dest);
113110

114111
const irep_idt &lhs_identifier=SSA_step.ssa_lhs.get_object_name();
115112

116113
// aliasing may happen here
117114

118115
value_setst::valuest expr_set;
119-
value_sets.get_values(target, dest.op0(), expr_set);
116+
value_sets.get_values(target, deref_expr.pointer(), expr_set);
120117
std::unordered_set<irep_idt> symbols;
121118

122119
for(value_setst::valuest::const_iterator
@@ -129,15 +126,15 @@ void preconditiont::compute_rec(exprt &dest)
129126
{
130127
// may alias!
131128
exprt tmp;
132-
tmp.swap(dest.op0());
129+
tmp.swap(deref_expr.pointer());
133130
dereference(target, tmp, ns, value_sets);
134-
dest.swap(tmp);
135-
compute_rec(dest);
131+
deref_expr.swap(tmp);
132+
compute_rec(deref_expr);
136133
}
137134
else
138135
{
139136
// nah, ok
140-
compute_rec(dest.op0());
137+
compute_rec(deref_expr.pointer());
141138
}
142139
}
143140
else if(dest==SSA_step.ssa_lhs.get_original_expr())

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>
@@ -164,7 +165,8 @@ void show_vcc(
164165
{
165166
of.open(filename);
166167
if(!of)
167-
throw "failed to open file " + filename;
168+
throw invalid_user_input_exceptiont(
169+
"invalid file to read trace from: " + filename, "--outfile");
168170
}
169171

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

src/goto-symex/slice.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void symex_slicet::slice(symex_target_equationt::SSA_stept &SSA_step)
112112
void symex_slicet::slice_assignment(
113113
symex_target_equationt::SSA_stept &SSA_step)
114114
{
115-
assert(SSA_step.ssa_lhs.id()==ID_symbol);
115+
PRECONDITION(SSA_step.ssa_lhs.id() == ID_symbol);
116116
const irep_idt &id=SSA_step.ssa_lhs.get_identifier();
117117

118118
if(depends.find(id)==depends.end())
@@ -127,8 +127,7 @@ void symex_slicet::slice_assignment(
127127
void symex_slicet::slice_decl(
128128
symex_target_equationt::SSA_stept &SSA_step)
129129
{
130-
assert(SSA_step.ssa_lhs.id()==ID_symbol);
131-
const irep_idt &id=SSA_step.ssa_lhs.get_identifier();
130+
const irep_idt &id = to_symbol_expr(SSA_step.ssa_lhs).get_identifier();
132131

133132
if(depends.find(id)==depends.end())
134133
{

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)