Skip to content

Commit f4b91a8

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

14 files changed

+69
-115
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,9 +1362,7 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
13621362
DATA_INVARIANT(
13631363
arg0.id()==ID_constant,
13641364
"ipush argument expected to be constant");
1365-
results[0]=arg0;
1366-
if(arg0.type()!=java_int_type())
1367-
results[0].make_typecast(java_int_type());
1365+
results[0] = typecast_exprt::conditional_cast(arg0, java_int_type());
13681366
}
13691367
else if(statement==patternt("if_?cmp??"))
13701368
{
@@ -1574,9 +1572,7 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
15741572
{
15751573
PRECONDITION(op.size() == 1 && results.size() == 1);
15761574
typet type=java_type_from_char(statement[2]);
1577-
results[0]=op[0];
1578-
if(results[0].type()!=type)
1579-
results[0].make_typecast(type);
1575+
results[0] = typecast_exprt::conditional_cast(op[0], type);
15801576
}
15811577
else if(statement=="new")
15821578
{
@@ -2225,9 +2221,9 @@ codet &java_bytecode_convert_methodt::replace_call_to_cprover_assume(
22252221
codet &c)
22262222
{
22272223
exprt operand = pop(1)[0];
2224+
22282225
// we may need to adjust the type of the argument
2229-
if(operand.type() != bool_typet())
2230-
operand.make_typecast(bool_typet());
2226+
operand = typecast_exprt::conditional_cast(operand, bool_typet());
22312227

22322228
c = code_assumet(operand);
22332229
location.set_function(method_id);
@@ -2649,16 +2645,12 @@ exprt::operandst &java_bytecode_convert_methodt::convert_ushr(
26492645
const std::size_t width = get_bytecode_type_width(type);
26502646
typet target = unsignedbv_typet(width);
26512647

2652-
exprt lhs = op[0];
2653-
if(lhs.type() != target)
2654-
lhs.make_typecast(target);
2655-
exprt rhs = op[1];
2656-
if(rhs.type() != target)
2657-
rhs.make_typecast(target);
2648+
exprt lhs = typecast_exprt::conditional_cast(op[0], target);
2649+
exprt rhs = typecast_exprt::conditional_cast(op[1], target);
2650+
2651+
results[0] =
2652+
typecast_exprt::conditional_cast(lshr_exprt(lhs, rhs), op[0].type());
26582653

2659-
results[0] = lshr_exprt(lhs, rhs);
2660-
if(results[0].type() != op[0].type())
2661-
results[0].make_typecast(op[0].type());
26622654
return results;
26632655
}
26642656

jbmc/src/java_bytecode/java_bytecode_typecheck_code.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ void java_bytecode_typecheckt::typecheck_code(codet &code)
2121
typecheck_expr(code_assign.lhs());
2222
typecheck_expr(code_assign.rhs());
2323

24-
if(code_assign.lhs().type()!=code_assign.rhs().type())
25-
code_assign.rhs().make_typecast(code_assign.lhs().type());
24+
code_assign.rhs() = typecast_exprt::conditional_cast(
25+
code_assign.rhs(), code_assign.lhs().type());
2626
}
2727
else if(statement==ID_block)
2828
{

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: 10 additions & 13 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(
@@ -1371,12 +1372,8 @@ void goto_checkt::bounds_check(
13711372
binary_relation_exprt inequality(index, ID_lt, size);
13721373

13731374
// typecast size
1374-
if(inequality.op1().type()!=inequality.op0().type())
1375-
inequality.op1().make_typecast(inequality.op0().type());
1376-
1377-
// typecast size
1378-
if(inequality.op1().type()!=inequality.op0().type())
1379-
inequality.op1().make_typecast(inequality.op0().type());
1375+
inequality.op1() = typecast_exprt::conditional_cast(
1376+
inequality.op1(), inequality.op0().type());
13801377

13811378
add_guarded_claim(
13821379
implies_exprt(type_matches_size, inequality),
@@ -1839,8 +1836,8 @@ void goto_checkt::goto_check(
18391836
goto_programt::targett t=new_code.add_instruction(ASSIGN);
18401837
exprt address_of_expr=address_of_exprt(variable);
18411838
exprt lhs=ns.lookup(CPROVER_PREFIX "dead_object").symbol_expr();
1842-
if(!base_type_eq(lhs.type(), address_of_expr.type(), ns))
1843-
address_of_expr.make_typecast(lhs.type());
1839+
address_of_expr =
1840+
typecast_exprt::conditional_cast(address_of_expr, lhs.type());
18441841
const if_exprt rhs(
18451842
side_effect_expr_nondett(bool_typet(), i.source_location),
18461843
address_of_expr,

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,7 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
618618
throw 0;
619619
}
620620

621-
if(o.type()!=size_type())
622-
o.make_typecast(size_type());
621+
o = typecast_exprt::conditional_cast(o, size_type());
623622

624623
result=plus_exprt(result, o);
625624
}
@@ -652,8 +651,7 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
652651
throw 0;
653652
}
654653

655-
if(o.type()!=size_type())
656-
o.make_typecast(size_type());
654+
o = typecast_exprt::conditional_cast(o, size_type());
657655

658656
result=plus_exprt(result, o);
659657
}
@@ -695,8 +693,9 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
695693
typecheck_expr(index);
696694

697695
exprt sub_size=size_of_expr(type.subtype(), *this);
698-
if(index.type()!=size_type())
699-
index.make_typecast(size_type());
696+
697+
index = typecast_exprt::conditional_cast(index, size_type());
698+
700699
result=plus_exprt(result, mult_exprt(sub_size, index));
701700

702701
typet tmp=type.subtype();
@@ -2972,8 +2971,7 @@ void c_typecheck_baset::typecheck_expr_binary_arithmetic(exprt &expr)
29722971
is_number(o_type0.subtype()))
29732972
{
29742973
// Vector arithmetic has fairly strict typing rules, no promotion
2975-
if(o_type0!=o_type1)
2976-
op1.make_typecast(op0.type());
2974+
op1 = typecast_exprt::conditional_cast(op1, op0.type());
29772975
expr.type()=op0.type();
29782976
return;
29792977
}
@@ -3405,8 +3403,7 @@ void c_typecheck_baset::typecheck_side_effect_assignment(
34053403
if(gcc_vector_types_compatible(
34063404
to_vector_type(o_type0), to_vector_type(o_type1)))
34073405
{
3408-
if(o_type0!=o_type1)
3409-
op1.make_typecast(o_type0);
3406+
op1 = typecast_exprt::conditional_cast(op1, o_type0);
34103407
return;
34113408
}
34123409
}
@@ -3426,8 +3423,7 @@ void c_typecheck_baset::typecheck_side_effect_assignment(
34263423
if(gcc_vector_types_compatible(
34273424
to_vector_type(o_type0), to_vector_type(o_type1)))
34283425
{
3429-
if(o_type0!=o_type1)
3430-
op1.make_typecast(o_type0);
3426+
op1 = typecast_exprt::conditional_cast(op1, o_type0);
34313427
return;
34323428
}
34333429
}

src/cpp/cpp_typecheck_resolve.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,9 +1724,7 @@ void cpp_typecheck_resolvet::guess_template_args(
17241724
if(e.id()==ID_unassigned)
17251725
{
17261726
typet old_type=e.type();
1727-
e=desired_expr;
1728-
if(e.type()!=old_type)
1729-
e.make_typecast(old_type);
1727+
e = typecast_exprt::conditional_cast(desired_expr, old_type);
17301728
}
17311729
}
17321730
}

src/pointer-analysis/goto_program_dereference.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,8 @@ void goto_program_dereferencet::dereference_rec(
181181
assert(expr.operands().size()==1);
182182

183183
if(expr.op0().id()==ID_dereference)
184-
{
185-
assert(expr.op0().operands().size()==1);
186-
187-
exprt tmp;
188-
tmp.swap(expr.op0().op0());
189-
190-
if(tmp.type()!=expr.type())
191-
tmp.make_typecast(expr.type());
192-
193-
expr.swap(tmp);
194-
}
184+
expr = typecast_exprt::conditional_cast(
185+
to_dereference_expr(expr.op0()).pointer(), expr.type());
195186
}
196187

197188
Forall_operands(it, expr)

src/solvers/flattening/arrays.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,8 @@ void arrayst::add_array_Ackermann_constraints()
323323
continue;
324324

325325
// index equality
326-
equal_exprt indices_equal(*i1, *i2);
327-
328-
if(indices_equal.op0().type()!=
329-
indices_equal.op1().type())
330-
{
331-
indices_equal.op1().
332-
make_typecast(indices_equal.op0().type());
333-
}
326+
const equal_exprt indices_equal(
327+
*i1, typecast_exprt::conditional_cast(*i2, i1->type()));
334328

335329
literalt indices_equal_lit=convert(indices_equal);
336330

@@ -537,8 +531,7 @@ void arrayst::add_array_constraints_with(
537531
{
538532
// we first build the guard
539533

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

543536
literalt guard_lit=convert(equal_exprt(index, other_index));
544537

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: 14 additions & 17 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,18 +203,16 @@ 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());
210-
sum=plus_exprt(object_offset, access_size);
211-
}
206+
const auto object_offset_casted =
207+
typecast_exprt::conditional_cast(object_offset, access_size.type());
212208

209+
sum = plus_exprt(object_offset_casted, access_size);
210+
}
213211

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

218-
return binary_relation_exprt(sum, op, object_size_expr);
215+
return binary_relation_exprt(sum_casted, op, object_size_expr);
219216
}
220217

221218
exprt object_lower_bound(

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

0 commit comments

Comments
 (0)