Skip to content

Replace make_typecast by typecast_exprt or typecast_exprt::conditional_cast [blocks: #3800] #3991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions jbmc/src/java_bytecode/java_bytecode_convert_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2166,8 +2166,7 @@ void java_bytecode_convert_methodt::convert_invoke(
type == java_byte_type() || type == java_short_type() ||
type.id() == ID_pointer)
{
if(type != arguments[i].type())
arguments[i].make_typecast(type);
arguments[i] = typecast_exprt::conditional_cast(arguments[i], type);
}
}

Expand Down
4 changes: 1 addition & 3 deletions jbmc/src/java_bytecode/java_bytecode_instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ code_ifthenelset java_bytecode_instrumentt::check_class_cast(
class1, ID_java_instanceof, class2);

pointer_typet voidptr = pointer_type(java_void_type());
exprt null_check_op=class1;
if(null_check_op.type()!=voidptr)
null_check_op.make_typecast(voidptr);
exprt null_check_op = typecast_exprt::conditional_cast(class1, voidptr);

optionalt<codet> check_code;
if(throw_runtime_exceptions)
Expand Down
8 changes: 4 additions & 4 deletions src/ansi-c/ansi_c_entry_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,10 @@ bool generate_ansi_c_start_function(
zero_string.type().subtype()=char_type();
zero_string.type().set(ID_size, "infinity");
const index_exprt index(zero_string, from_integer(0, uint_type()));
exprt address_of=address_of_exprt(index, pointer_type(char_type()));

if(argv_symbol.type.subtype()!=address_of.type())
address_of.make_typecast(argv_symbol.type.subtype());
exprt address_of =
typecast_exprt::conditional_cast(
address_of_exprt(index, pointer_type(char_type())),
argv_symbol.type.subtype());

// assign argv[*] to the address of a string-object
array_of_exprt array_of(address_of, argv_symbol.type);
Expand Down
11 changes: 6 additions & 5 deletions src/ansi-c/c_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2596,10 +2596,11 @@ exprt c_typecheck_baset::do_special_functions(
throw 0;
}

expr.arguments()[0].make_typecast(bool_typet());
make_constant(expr.arguments()[0]);
exprt arg0 =
typecast_exprt::conditional_cast(expr.arguments()[0], bool_typet());
make_constant(arg0);

if(expr.arguments()[0].is_true())
if(arg0.is_true())
return expr.arguments()[1];
else
return expr.arguments()[2];
Expand Down Expand Up @@ -2953,7 +2954,7 @@ void c_typecheck_baset::typecheck_expr_binary_arithmetic(exprt &expr)
is_number(o_type1))
{
// convert op1 to the vector type
op1.make_typecast(o_type0);
op1 = typecast_exprt(op1, o_type0);
expr.type() = o_type0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A conditional_cast appears to be wrong here. When we are in this branch, the cast is always needed. Simply use the constructor.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return;
}
Expand All @@ -2962,7 +2963,7 @@ void c_typecheck_baset::typecheck_expr_binary_arithmetic(exprt &expr)
is_number(o_type0))
{
// convert op0 to the vector type
op0.make_typecast(o_type1);
op0 = typecast_exprt(op0, o_type1);
expr.type() = o_type1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return;
}
Expand Down
71 changes: 24 additions & 47 deletions src/cpp/cpp_typecheck_conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ bool cpp_typecheckt::standard_conversion_integral_promotion(
std::size_t width=to_signedbv_type(expr.type()).get_width();
if(width >= config.ansi_c.int_width)
return false;
new_expr=expr;
new_expr.make_typecast(int_type);
new_expr = typecast_exprt(expr, int_type);
return true;
}

Expand All @@ -220,24 +219,19 @@ bool cpp_typecheckt::standard_conversion_integral_promotion(
std::size_t width=to_unsignedbv_type(expr.type()).get_width();
if(width >= config.ansi_c.int_width)
return false;
new_expr=expr;
if(width==config.ansi_c.int_width)
int_type.id(ID_unsignedbv);
new_expr.make_typecast(int_type);
new_expr = typecast_exprt(expr, int_type);
return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here -- the cast will always happen.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so - the line above the change would make the typecast a no-op.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to PR: the two lines above the change are dead code (note the branch directly before). There's probably a bug there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, indeed. Aren't those two lines really just unnecessary, and we should do an unconditional cast as you suggested? To me it seems there no bug other than it being dead code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug is that an "unsigned int" will be promoted to "signed int", which is wrong.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, the width >= config.ansi_c.int_width would catch that?

}

if(expr.type().id() == ID_bool || expr.type().id() == ID_c_bool)
{
new_expr = expr;
new_expr.make_typecast(int_type);
new_expr = typecast_exprt(expr, int_type);
return true;
}

if(expr.type().id()==ID_c_enum_tag)
{
new_expr=expr;
new_expr.make_typecast(int_type);
new_expr = typecast_exprt(expr, int_type);
return true;
}

Expand Down Expand Up @@ -271,8 +265,7 @@ bool cpp_typecheckt::standard_conversion_floating_point_promotion(
c_qualifierst qual_from;
qual_from.read(expr.type());

new_expr=expr;
new_expr.make_typecast(double_type());
new_expr = typecast_exprt(expr, double_type());
qual_from.write(new_expr.type());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


return true;
Expand Down Expand Up @@ -328,8 +321,7 @@ bool cpp_typecheckt::standard_conversion_integral_conversion(

c_qualifierst qual_from;
qual_from.read(expr.type());
new_expr=expr;
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(expr, type);
qual_from.write(new_expr.type());

return true;
Expand Down Expand Up @@ -382,8 +374,7 @@ bool cpp_typecheckt::standard_conversion_floating_integral_conversion(

c_qualifierst qual_from;
qual_from.read(expr.type());
new_expr=expr;
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(expr, type);
qual_from.write(new_expr.type());

return true;
Expand Down Expand Up @@ -425,8 +416,7 @@ bool cpp_typecheckt::standard_conversion_floating_point_conversion(
c_qualifierst qual_from;

qual_from.read(expr.type());
new_expr=expr;
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(expr, type);
qual_from.write(new_expr.type());

return true;
Expand Down Expand Up @@ -508,8 +498,7 @@ bool cpp_typecheckt::standard_conversion_pointer(
{
c_qualifierst qual_from;
qual_from.read(expr.type().subtype());
new_expr=expr;
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(expr, type);
qual_from.write(new_expr.type().subtype());
return true;
}
Expand Down Expand Up @@ -614,8 +603,7 @@ bool cpp_typecheckt::standard_conversion_pointer_to_member(
if(expr.id()==ID_constant &&
expr.get(ID_value)==ID_NULL)
{
new_expr=expr;
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(expr, type);
return true;
}

Expand All @@ -627,8 +615,7 @@ bool cpp_typecheckt::standard_conversion_pointer_to_member(

if(subtype_typecast(to_struct, from_struct))
{
new_expr=expr;
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(expr, type);
return true;
}

Expand Down Expand Up @@ -664,8 +651,7 @@ bool cpp_typecheckt::standard_conversion_boolean(
typet Bool = c_bool_type();
qual_from.write(Bool);

new_expr=expr;
new_expr.make_typecast(Bool);
new_expr = typecast_exprt::conditional_cast(expr, Bool);
return true;
}

Expand Down Expand Up @@ -705,7 +691,7 @@ bool cpp_typecheckt::standard_conversion_sequence(

// we turn bit fields into their underlying type
if(curr_expr.type().id()==ID_c_bit_field)
curr_expr.make_typecast(curr_expr.type().subtype());
curr_expr = typecast_exprt(curr_expr, curr_expr.type().subtype());

if(curr_expr.type().id()==ID_array)
{
Expand Down Expand Up @@ -791,7 +777,7 @@ bool cpp_typecheckt::standard_conversion_sequence(
if(expr.type().subtype().id()==ID_nullptr)
{
// std::nullptr_t to _any_ pointer type is ok
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(new_expr, type);
}
else if(!standard_conversion_pointer(curr_expr, type, new_expr))
{
Expand Down Expand Up @@ -1278,7 +1264,7 @@ bool cpp_typecheckt::reference_binding(
{
c_qualifierst qual_from;
qual_from.read(expr.type());
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(new_expr, type);
qual_from.write(new_expr.type().subtype());
}

Expand Down Expand Up @@ -1805,8 +1791,7 @@ bool cpp_typecheckt::reinterpret_typecast(
(type.id()==ID_unsignedbv || type.id()==ID_signedbv))
{
// pointer to integer, always ok
new_expr=e;
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(e, type);
return true;
}

Expand All @@ -1825,8 +1810,7 @@ bool cpp_typecheckt::reinterpret_typecast(
}
else
{
new_expr=e;
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(e, type);
}
return true;
}
Expand All @@ -1837,16 +1821,13 @@ bool cpp_typecheckt::reinterpret_typecast(
{
// pointer to pointer: we ok it all.
// This is more generous than the standard.
new_expr=expr;
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(expr, type);
return true;
}

if(is_reference(type) && e.get_bool(ID_C_lvalue))
{
exprt tmp=address_of_exprt(e);
tmp.make_typecast(type);
new_expr.swap(tmp);
new_expr = typecast_exprt::conditional_cast(address_of_exprt(e), type);
return true;
}

Expand Down Expand Up @@ -1919,8 +1900,7 @@ bool cpp_typecheckt::static_typecast(

if(type.id()==ID_empty)
{
new_expr=e;
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(e, type);
return true;
}

Expand All @@ -1930,8 +1910,7 @@ bool cpp_typecheckt::static_typecast(
e.type().id()==ID_unsignedbv ||
e.type().id()==ID_c_enum_tag))
{
new_expr=e;
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(e, type);
new_expr.remove(ID_C_lvalue);
return true;
}
Expand Down Expand Up @@ -1966,9 +1945,8 @@ bool cpp_typecheckt::static_typecast(

if(from.id()==ID_empty)
{
e.make_typecast(type);
new_expr.swap(e);
return true;
new_expr = typecast_exprt::conditional_cast(e, type);
return true;
}

if(to.id()==ID_struct && from.id()==ID_struct)
Expand Down Expand Up @@ -2007,8 +1985,7 @@ bool cpp_typecheckt::static_typecast(

if(subtype_typecast(from_struct, to_struct))
{
new_expr=e;
new_expr.make_typecast(type);
new_expr = typecast_exprt::conditional_cast(e, type);
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/cpp/cpp_typecheck_initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ void cpp_typecheckt::zero_initializer(
const unsignedbv_typet enum_type(
to_bitvector_type(final_type.subtype()).get_width());

exprt zero(from_integer(0, enum_type));
zero.make_typecast(type);
exprt zero =
typecast_exprt::conditional_cast(from_integer(0, enum_type), type);
already_typechecked(zero);

code_assignt assign;
Expand Down
3 changes: 2 additions & 1 deletion src/cpp/cpp_typecheck_static_assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ void cpp_typecheckt::convert(cpp_static_assertt &cpp_static_assert)
typecheck_expr(cpp_static_assert.op0());
typecheck_expr(cpp_static_assert.op1());

cpp_static_assert.op0().make_typecast(bool_typet());
cpp_static_assert.op0() =
typecast_exprt::conditional_cast(cpp_static_assert.op0(), bool_typet());
make_constant(cpp_static_assert.op0());

if(cpp_static_assert.op0().is_true())
Expand Down
7 changes: 3 additions & 4 deletions src/goto-cc/linker_script_merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,7 @@ int linker_script_merget::ls_data2instructions(
unsigned_int_type().get_width()),
unsigned_int_type());

exprt rhs_tc(rhs);
rhs_tc.make_typecast(pointer_type(char_type()));
typecast_exprt rhs_tc(rhs, pointer_type(char_type()));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

linker_values.emplace(
irep_idt(d["sym"].value), std::make_pair(lhs, rhs_tc));
Expand Down Expand Up @@ -640,8 +639,8 @@ int linker_script_merget::ls_data2instructions(
string2integer(d["val"].value), unsigned_int_type().get_width()));
rhs.type()=unsigned_int_type();

exprt rhs_tc(rhs);
rhs_tc.make_typecast(pointer_type(char_type()));
exprt rhs_tc =
typecast_exprt::conditional_cast(rhs, pointer_type(char_type()));

linker_values.emplace(
irep_idt(d["sym"].value), std::make_pair(lhs, rhs_tc));
Expand Down
10 changes: 5 additions & 5 deletions src/goto-instrument/goto_program2code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1846,7 +1846,7 @@ void goto_program2codet::cleanup_expr(exprt &expr, bool no_typecast)

const typet &t=expr.type();

expr.make_typecast(t);
expr = typecast_exprt(expr, t);
add_local_types(t);

const irep_idt &typedef_str=expr.type().get(ID_C_typedef);
Expand Down Expand Up @@ -1944,10 +1944,10 @@ void goto_program2codet::cleanup_expr(exprt &expr, bool no_typecast)
else if(expr.type().id()==ID_bool ||
expr.type().id()==ID_c_bool)
{
expr=from_integer(
expr.is_true()?1:0,
signedbv_typet(config.ansi_c.int_width));
expr.make_typecast(bool_typet());
expr = typecast_exprt(
from_integer(
expr.is_true() ? 1 : 0, signedbv_typet(config.ansi_c.int_width)),
bool_typet());
}

const irept &c_sizeof_type=expr.find(ID_C_c_sizeof_type);
Expand Down
Loading