Skip to content

Commit ac2c3d6

Browse files
author
Daniel Kroening
committed
use typecast_exprt::conditional_cast
.make_typecast() isn't type save; when used in the pattern if(a.type()!=b.type()) ... then use typecast_exprt::conditional_cast instead.
1 parent 5ecb825 commit ac2c3d6

9 files changed

+41
-58
lines changed

src/analyses/escape_analysis.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,9 @@ void escape_analysist::insert_cleanup(
421421
exprt arg=lhs;
422422
if(is_object)
423423
arg=address_of_exprt(arg);
424-
if(arg.type()!=param_type)
425-
arg.make_typecast(param_type);
424+
425+
arg = typecast_exprt::conditional_cast(arg, param_type);
426+
426427
code.arguments().push_back(arg);
427428
}
428429

src/analyses/goto_check.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,18 +1293,19 @@ void goto_checkt::bounds_check(
12931293
const exprt &pointer=
12941294
to_dereference_expr(ode.root_object()).pointer();
12951295

1296-
if_exprt size(
1296+
const if_exprt size(
12971297
dynamic_object(pointer),
12981298
typecast_exprt(dynamic_size(ns), object_size(pointer).type()),
12991299
object_size(pointer));
13001300

1301-
plus_exprt effective_offset(ode.offset(), pointer_offset(pointer));
1301+
const plus_exprt effective_offset(ode.offset(), pointer_offset(pointer));
13021302

13031303
assert(effective_offset.op0().type()==effective_offset.op1().type());
1304-
if(effective_offset.type()!=size.type())
1305-
size.make_typecast(effective_offset.type());
13061304

1307-
binary_relation_exprt inequality(effective_offset, ID_lt, size);
1305+
const auto size_casted =
1306+
typecast_exprt::conditional_cast(size, effective_offset.type());
1307+
1308+
binary_relation_exprt inequality(effective_offset, ID_lt, size_casted);
13081309

13091310
or_exprt precond(
13101311
and_exprt(

src/solvers/flattening/arrays.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,8 @@ void arrayst::add_array_Ackermann_constraints()
325325
// index equality
326326
equal_exprt indices_equal(*i1, *i2);
327327

328-
if(indices_equal.op0().type()!=
329-
indices_equal.op1().type())
330-
{
331-
indices_equal.op1().
332-
make_typecast(indices_equal.op0().type());
333-
}
328+
indices_equal.op1() = typecast_exprt::conditional_cast(
329+
indices_equal.op1(), indices_equal.op0().type());
334330

335331
literalt indices_equal_lit=convert(indices_equal);
336332

@@ -537,8 +533,7 @@ void arrayst::add_array_constraints_with(
537533
{
538534
// we first build the guard
539535

540-
if(other_index.type()!=index.type())
541-
other_index.make_typecast(index.type());
536+
other_index = typecast_exprt::conditional_cast(other_index, index.type());
542537

543538
literalt guard_lit=convert(equal_exprt(index, other_index));
544539

src/solvers/flattening/boolbv_byte_extract.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ bvt boolbvt::convert_byte_extract(const byte_extract_exprt &expr)
9191
object_descriptor_exprt o;
9292
o.build(expr.op(), ns);
9393
CHECK_RETURN(o.offset().id() != ID_unknown);
94-
if(o.offset().type() != expr.offset().type())
95-
o.offset().make_typecast(expr.offset().type());
94+
95+
o.offset() =
96+
typecast_exprt::conditional_cast(o.offset(), expr.offset().type());
97+
9698
byte_extract_exprt be(
9799
expr.id(),
98100
o.root_object(),

src/solvers/lowering/byte_operators.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,7 @@ static exprt lower_byte_update(
396396

397397
if(i==0 && element_size==1) // bytes?
398398
{
399-
new_value = src.value();
400-
if(new_value.type()!=subtype)
401-
new_value.make_typecast(subtype);
399+
new_value = typecast_exprt::conditional_cast(src.value(), subtype);
402400
}
403401
else
404402
{

src/util/pointer_offset_size.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,14 @@ exprt size_of_expr(
311311
return nil_exprt();
312312

313313
// get size
314-
exprt size = array_type.size();
314+
const auto size = array_type.size();
315315

316316
if(size.is_nil())
317317
return nil_exprt();
318318

319-
if(size.type()!=sub.type())
320-
size.make_typecast(sub.type());
319+
const auto size_casted = typecast_exprt::conditional_cast(size, sub.type());
321320

322-
mult_exprt result(size, sub);
321+
mult_exprt result(size_casted, sub);
323322
simplify(result, ns);
324323

325324
return std::move(result);
@@ -342,15 +341,14 @@ exprt size_of_expr(
342341
return nil_exprt();
343342

344343
// get size
345-
exprt size=to_vector_type(type).size();
344+
const auto size = to_vector_type(type).size();
346345

347346
if(size.is_nil())
348347
return nil_exprt();
349348

350-
if(size.type()!=sub.type())
351-
size.make_typecast(sub.type());
349+
const auto size_casted = typecast_exprt::conditional_cast(size, sub.type());
352350

353-
mult_exprt result(size, sub);
351+
mult_exprt result(size_casted, sub);
354352
simplify(result, ns);
355353

356354
return std::move(result);
@@ -633,10 +631,7 @@ exprt build_sizeof_expr(
633631
if(remainder>0)
634632
result=plus_exprt(result, from_integer(remainder, t));
635633

636-
if(result.type()!=expr.type())
637-
result.make_typecast(expr.type());
638-
639-
return result;
634+
return typecast_exprt::conditional_cast(result, expr.type());
640635
}
641636

642637
exprt get_subexpression_at_offset(

src/util/pointer_predicates.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,16 @@ exprt dynamic_object_upper_bound(
171171
{
172172
op=ID_gt;
173173

174-
if(ns.follow(object_offset.type())!=
175-
ns.follow(access_size.type()))
176-
object_offset.make_typecast(access_size.type());
177-
sum=plus_exprt(object_offset, access_size);
174+
const auto object_offset_casted =
175+
typecast_exprt::conditional_cast(object_offset, access_size.type());
176+
177+
sum = plus_exprt(object_offset_casted, access_size);
178178
}
179179

180-
if(ns.follow(sum.type())!=
181-
ns.follow(malloc_size.type()))
182-
sum.make_typecast(malloc_size.type());
180+
const auto sum_casted =
181+
typecast_exprt::conditional_cast(sum, malloc_size.type());
183182

184-
return binary_relation_exprt(sum, op, malloc_size);
183+
return binary_relation_exprt(sum_casted, op, malloc_size);
185184
}
186185

187186
exprt object_upper_bound(
@@ -204,16 +203,14 @@ exprt object_upper_bound(
204203
{
205204
op=ID_gt;
206205

207-
if(ns.follow(object_offset.type())!=
208-
ns.follow(access_size.type()))
209-
object_offset.make_typecast(access_size.type());
206+
const auto object_offset_casted =
207+
typecast_exprt::conditional_cast(object_offset, access_size.type());
208+
210209
sum=plus_exprt(object_offset, access_size);
211210
}
212211

213-
214-
if(ns.follow(sum.type())!=
215-
ns.follow(object_size_expr.type()))
216-
sum.make_typecast(object_size_expr.type());
212+
const auto sum_casted =
213+
typecast_exprt::conditional_cast(sum, object_size_expr.type());
217214

218215
return binary_relation_exprt(sum, op, object_size_expr);
219216
}

src/util/simplify_expr_array.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ bool simplify_exprt::simplify_index(exprt &expr)
8181
{
8282
// Turn (a with i:=x)[j] into (i==j)?x:a[j].
8383
// watch out that the type of i and j might be different.
84-
equal_exprt equality_expr(expr.op1(), with_expr.op1());
84+
const exprt rhs_casted =
85+
typecast_exprt::conditional_cast(with_expr.op1(), expr.op1().type());
8586

86-
if(equality_expr.lhs().type()!=equality_expr.rhs().type())
87-
equality_expr.rhs().make_typecast(equality_expr.lhs().type());
87+
equal_exprt equality_expr(expr.op1(), rhs_casted);
8888

8989
simplify_inequality(equality_expr);
9090

src/util/simplify_expr_pointer.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,21 +287,15 @@ bool simplify_exprt::simplify_pointer_offset(exprt &expr)
287287
tmp.op0().operands().size()==1 &&
288288
tmp.op0().op0().id()==ID_address_of)
289289
{
290-
expr=tmp.op1();
291-
if(type!=expr.type())
292-
expr.make_typecast(type);
293-
290+
expr = typecast_exprt::conditional_cast(tmp.op1(), type);
294291
simplify_node(expr);
295292
return false;
296293
}
297294
else if(tmp.op1().id()==ID_typecast &&
298295
tmp.op1().operands().size()==1 &&
299296
tmp.op1().op0().id()==ID_address_of)
300297
{
301-
expr=tmp.op0();
302-
if(type!=expr.type())
303-
expr.make_typecast(type);
304-
298+
expr = typecast_exprt::conditional_cast(tmp.op0(), type);
305299
simplify_node(expr);
306300
return false;
307301
}

0 commit comments

Comments
 (0)