Skip to content

Commit deeb26e

Browse files
author
Daniel Kroening
authored
Merge pull request #1266 from tautschnig/array-debugging
[develop->master] Array debugging
2 parents ddc7125 + b3cd67a commit deeb26e

File tree

1 file changed

+74
-42
lines changed

1 file changed

+74
-42
lines changed

src/solvers/flattening/arrays.cpp

+74-42
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ Author: Daniel Kroening, [email protected]
88

99
#include "arrays.h"
1010

11-
#include <cassert>
12-
#include <iostream>
13-
1411
#include <langapi/language_util.h>
1512

1613
#include <util/std_expr.h>
@@ -21,6 +18,10 @@ Author: Daniel Kroening, [email protected]
2118

2219
#include <solvers/prop/prop.h>
2320

21+
#ifdef DEBUG
22+
#include <iostream>
23+
#endif
24+
2425
arrayst::arrayst(
2526
const namespacet &_ns,
2627
propt &_prop):equalityt(_ns, _prop)
@@ -48,11 +49,15 @@ literalt arrayst::record_array_equality(
4849
// check types
4950
if(!base_type_eq(op0.type(), op1.type(), ns))
5051
{
51-
std::cout << equality.pretty() << '\n';
52-
throw "record_array_equality got equality without matching types";
52+
prop.error() << equality.pretty() << messaget::eom;
53+
DATA_INVARIANT(
54+
false,
55+
"record_array_equality got equality without matching types");
5356
}
5457

55-
assert(ns.follow(op0.type()).id()==ID_array);
58+
DATA_INVARIANT(
59+
ns.follow(op0.type()).id()==ID_array,
60+
"record_array_equality parameter should be array-typed");
5661

5762
array_equalities.push_back(array_equalityt());
5863

@@ -113,8 +118,8 @@ void arrayst::collect_arrays(const exprt &a)
113118
// check types
114119
if(!base_type_eq(array_type, with_expr.old().type(), ns))
115120
{
116-
std::cout << a.pretty() << '\n';
117-
throw "collect_arrays got 'with' without matching types";
121+
prop.error() << a.pretty() << messaget::eom;
122+
DATA_INVARIANT(false, "collect_arrays got 'with' without matching types");
118123
}
119124

120125
arrays.make_union(a, with_expr.old());
@@ -131,8 +136,10 @@ void arrayst::collect_arrays(const exprt &a)
131136
// check types
132137
if(!base_type_eq(array_type, update_expr.old().type(), ns))
133138
{
134-
std::cout << a.pretty() << '\n';
135-
throw "collect_arrays got 'update' without matching types";
139+
prop.error() << a.pretty() << messaget::eom;
140+
DATA_INVARIANT(
141+
false,
142+
"collect_arrays got 'update' without matching types");
136143
}
137144

138145
arrays.make_union(a, update_expr.old());
@@ -151,15 +158,15 @@ void arrayst::collect_arrays(const exprt &a)
151158
// check types
152159
if(!base_type_eq(array_type, if_expr.true_case().type(), ns))
153160
{
154-
std::cout << a.pretty() << '\n';
155-
throw "collect_arrays got if without matching types";
161+
prop.error() << a.pretty() << messaget::eom;
162+
DATA_INVARIANT(false, "collect_arrays got if without matching types");
156163
}
157164

158165
// check types
159166
if(!base_type_eq(array_type, if_expr.false_case().type(), ns))
160167
{
161-
std::cout << a.pretty() << '\n';
162-
throw "collect_arrays got if without matching types";
168+
prop.error() << a.pretty() << messaget::eom;
169+
DATA_INVARIANT(false, "collect_arrays got if without matching types");
163170
}
164171

165172
arrays.make_union(a, if_expr.true_case());
@@ -175,9 +182,10 @@ void arrayst::collect_arrays(const exprt &a)
175182
}
176183
else if(a.id()==ID_member)
177184
{
178-
if(to_member_expr(a).struct_op().id()!=ID_symbol)
179-
throw
180-
"unexpected array expression: member with `"+a.op0().id_string()+"'";
185+
DATA_INVARIANT(
186+
to_member_expr(a).struct_op().id()==ID_symbol,
187+
("unexpected array expression: member with `"+
188+
a.op0().id_string()+"'").c_str());
181189
}
182190
else if(a.id()==ID_constant ||
183191
a.id()==ID_array ||
@@ -190,20 +198,24 @@ void arrayst::collect_arrays(const exprt &a)
190198
else if(a.id()==ID_byte_update_little_endian ||
191199
a.id()==ID_byte_update_big_endian)
192200
{
193-
assert(0);
201+
DATA_INVARIANT(
202+
false,
203+
"byte_update should be removed before collect_arrays");
194204
}
195205
else if(a.id()==ID_typecast)
196206
{
197207
// cast between array types?
198-
assert(a.operands().size()==1);
208+
DATA_INVARIANT(
209+
a.operands().size()==1,
210+
"typecast must have one operand");
199211

200-
if(a.op0().type().id()==ID_array)
201-
{
202-
arrays.make_union(a, a.op0());
203-
collect_arrays(a.op0());
204-
}
205-
else
206-
throw "unexpected array type cast from "+a.op0().type().id_string();
212+
DATA_INVARIANT(
213+
a.op0().type().id()==ID_array,
214+
("unexpected array type cast from "+
215+
a.op0().type().id_string()).c_str());
216+
217+
arrays.make_union(a, a.op0());
218+
collect_arrays(a.op0());
207219
}
208220
else if(a.id()==ID_index)
209221
{
@@ -212,7 +224,12 @@ void arrayst::collect_arrays(const exprt &a)
212224
collect_arrays(a.op0());
213225
}
214226
else
215-
throw "unexpected array expression (collect_arrays): `"+a.id_string()+"'";
227+
{
228+
DATA_INVARIANT(
229+
false,
230+
("unexpected array expression (collect_arrays): `"+
231+
a.id_string()+"'").c_str());
232+
}
216233
}
217234

218235
/// adds array constraints (refine=true...lazily for the refinement loop)
@@ -280,7 +297,7 @@ void arrayst::add_array_Ackermann_constraints()
280297
{
281298
// this is quadratic!
282299

283-
#if 0
300+
#ifdef DEBUG
284301
std::cout << "arrays.size(): " << arrays.size() << '\n';
285302
#endif
286303

@@ -289,7 +306,7 @@ void arrayst::add_array_Ackermann_constraints()
289306
{
290307
const index_sett &index_set=index_map[arrays.find_number(i)];
291308

292-
#if 0
309+
#ifdef DEBUG
293310
std::cout << "index_set.size(): " << index_set.size() << '\n';
294311
#endif
295312

@@ -349,7 +366,7 @@ void arrayst::update_index_map(std::size_t i)
349366
return;
350367

351368
std::size_t root_number=arrays.find_number(i);
352-
assert(root_number!=i);
369+
INVARIANT(root_number!=i, "is_root_number incorrect?");
353370

354371
index_sett &root_index_set=index_map[root_number];
355372
index_sett &index_set=index_map[i];
@@ -407,7 +424,9 @@ void arrayst::add_array_constraints_equality(
407424
const typet &subtype2=ns.follow(array_equality.f2.type()).subtype();
408425
index_exprt index_expr2(array_equality.f2, index, subtype2);
409426

410-
assert(index_expr1.type()==index_expr2.type());
427+
DATA_INVARIANT(
428+
index_expr1.type()==index_expr2.type(),
429+
"array elements should all have same type");
411430

412431
array_equalityt equal;
413432
equal.f1 = index_expr1;
@@ -449,12 +468,14 @@ void arrayst::add_array_constraints(
449468
else if(expr.id()==ID_byte_update_little_endian ||
450469
expr.id()==ID_byte_update_big_endian)
451470
{
452-
assert(0);
471+
INVARIANT(false, "byte_update should be removed before arrayst");
453472
}
454473
else if(expr.id()==ID_typecast)
455474
{
456475
// we got a=(type[])b
457-
assert(expr.operands().size()==1);
476+
DATA_INVARIANT(
477+
expr.operands().size()==1,
478+
"typecast should have one operand");
458479

459480
// add a[i]=b[i]
460481
for(const auto &index : index_set)
@@ -463,7 +484,9 @@ void arrayst::add_array_constraints(
463484
index_exprt index_expr1(expr, index, subtype);
464485
index_exprt index_expr2(expr.op0(), index, subtype);
465486

466-
assert(index_expr1.type()==index_expr2.type());
487+
DATA_INVARIANT(
488+
index_expr1.type()==index_expr2.type(),
489+
"array elements should all have same type");
467490

468491
// add constraint
469492
lazy_constraintt lazy(lazy_typet::ARRAY_TYPECAST,
@@ -475,9 +498,12 @@ void arrayst::add_array_constraints(
475498
{
476499
}
477500
else
478-
throw
479-
"unexpected array expression (add_array_constraints): `"+
480-
expr.id_string()+"'";
501+
{
502+
DATA_INVARIANT(
503+
false,
504+
("unexpected array expression (add_array_constraints): `"+
505+
expr.id_string()+"'").c_str());
506+
}
481507
}
482508

483509
void arrayst::add_array_constraints_with(
@@ -495,8 +521,10 @@ void arrayst::add_array_constraints_with(
495521

496522
if(index_expr.type()!=value.type())
497523
{
498-
std::cout << expr.pretty() << '\n';
499-
assert(false);
524+
prop.error() << expr.pretty() << messaget::eom;
525+
DATA_INVARIANT(
526+
false,
527+
"with-expression operand should match array element type");
500528
}
501529

502530
lazy_constraintt lazy(
@@ -563,8 +591,10 @@ void arrayst::add_array_constraints_update(
563591

564592
if(index_expr.type()!=value.type())
565593
{
566-
std::cout << expr.pretty() << '\n';
567-
assert(false);
594+
prop.error() << expr.pretty() << messaget::eom;
595+
DATA_INVARIANT(
596+
false,
597+
"update operand should match array element type");
568598
}
569599

570600
set_to_true(equal_exprt(index_expr, value));
@@ -619,7 +649,9 @@ void arrayst::add_array_constraints_array_of(
619649
const typet &subtype=ns.follow(expr.type()).subtype();
620650
index_exprt index_expr(expr, index, subtype);
621651

622-
assert(base_type_eq(index_expr.type(), expr.op0().type(), ns));
652+
DATA_INVARIANT(
653+
base_type_eq(index_expr.type(), expr.op0().type(), ns),
654+
"array_of operand type should match array element type");
623655

624656
// add constraint
625657
lazy_constraintt lazy(

0 commit comments

Comments
 (0)