Skip to content

Cleanup build_sizeof_expr #6664

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 2 commits into from
Feb 14, 2022
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
52 changes: 48 additions & 4 deletions src/ansi-c/expr2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,52 @@ std::string expr2ct::convert_object_descriptor(
return result;
}

static optionalt<exprt>
build_sizeof_expr(const constant_exprt &expr, const namespacet &ns)
{
const typet &type = static_cast<const typet &>(expr.find(ID_C_c_sizeof_type));

if(type.is_nil())
return {};

const auto type_size_expr = size_of_expr(type, ns);
optionalt<mp_integer> type_size;
if(type_size_expr.has_value())
type_size = numeric_cast<mp_integer>(*type_size_expr);
auto val = numeric_cast<mp_integer>(expr);

if(
!type_size.has_value() || *type_size < 0 || !val.has_value() ||
*val < *type_size || (*type_size == 0 && *val > 0))
{
return {};
}

const unsignedbv_typet t(size_type());
DATA_INVARIANT(
address_bits(*val + 1) <= t.get_width(),
"sizeof value does not fit size_type");

mp_integer remainder = 0;

if(*type_size != 0)
{
remainder = *val % *type_size;
*val -= remainder;
*val /= *type_size;
}

exprt result(ID_sizeof, t);
result.set(ID_type_arg, type);

if(*val > 1)
result = mult_exprt(result, from_integer(*val, t));
if(remainder > 0)
result = plus_exprt(result, from_integer(remainder, t));

return typecast_exprt::conditional_cast(result, expr.type());
}

std::string expr2ct::convert_constant(
const constant_exprt &src,
unsigned &precedence)
Expand Down Expand Up @@ -1852,11 +1898,9 @@ std::string expr2ct::convert_constant(
else if(c_type==ID_signed_long_long_int)
dest+="ll";

if(src.find(ID_C_c_sizeof_type).is_not_nil() &&
sizeof_nesting==0)
if(sizeof_nesting == 0)
{
const auto sizeof_expr_opt =
build_sizeof_expr(to_constant_expr(src), ns);
const auto sizeof_expr_opt = build_sizeof_expr(src, ns);

if(sizeof_expr_opt.has_value())
{
Expand Down
44 changes: 0 additions & 44 deletions src/util/pointer_offset_size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,50 +560,6 @@ compute_pointer_offset(const exprt &expr, const namespacet &ns)
return {}; // don't know
}

optionalt<exprt>
build_sizeof_expr(const constant_exprt &expr, const namespacet &ns)
{
const typet &type=
static_cast<const typet &>(expr.find(ID_C_c_sizeof_type));

if(type.is_nil())
return {};

const auto type_size = pointer_offset_size(type, ns);
auto val = numeric_cast<mp_integer>(expr);

if(
!type_size.has_value() || *type_size < 0 || !val.has_value() ||
*val < *type_size || (*type_size == 0 && *val > 0))
{
return {};
}

const typet t(size_type());
DATA_INVARIANT(
address_bits(*val + 1) <= *pointer_offset_bits(t, ns),
"sizeof value does not fit size_type");

mp_integer remainder=0;

if(*type_size != 0)
{
remainder = *val % *type_size;
*val -= remainder;
*val /= *type_size;
}

exprt result(ID_sizeof, t);
result.set(ID_type_arg, type);

if(*val > 1)
result = mult_exprt(result, from_integer(*val, t));
if(remainder>0)
result=plus_exprt(result, from_integer(remainder, t));

return typecast_exprt::conditional_cast(result, expr.type());
}

optionalt<exprt> get_subexpression_at_offset(
const exprt &expr,
const mp_integer &offset_bytes,
Expand Down
3 changes: 0 additions & 3 deletions src/util/pointer_offset_size.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ optionalt<exprt> member_offset_expr(

optionalt<exprt> size_of_expr(const typet &type, const namespacet &ns);

optionalt<exprt>
build_sizeof_expr(const constant_exprt &expr, const namespacet &ns);

optionalt<exprt> get_subexpression_at_offset(
const exprt &expr,
const mp_integer &offset,
Expand Down