Skip to content

Commit 3028bf8

Browse files
author
Daniel Kroening
authored
Merge pull request #3252 from tautschnig/vs-no-to_integer
Use numeric_cast<mp_integer> instead of deprecated to_integer(exprt)
2 parents 75bec44 + 638d91c commit 3028bf8

14 files changed

+154
-173
lines changed

src/ansi-c/padding.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ mp_integer alignment(const typet &type, const namespacet &ns)
3535
const exprt &given_alignment=
3636
static_cast<const exprt &>(type.find(ID_C_alignment));
3737

38-
mp_integer a_int;
38+
mp_integer a_int = 0;
3939

4040
// we trust it blindly, no matter how nonsensical
41-
if(given_alignment.is_nil() ||
42-
to_integer(given_alignment, a_int))
43-
a_int=0;
41+
if(given_alignment.is_not_nil())
42+
{
43+
const auto a = numeric_cast<mp_integer>(given_alignment);
44+
if(a.has_value())
45+
a_int = *a;
46+
}
4447

4548
// alignment but no packing
4649
if(a_int>0 && !type.get_bool(ID_C_packed))
@@ -402,17 +405,16 @@ static void add_padding_gcc(struct_typet &type, const namespacet &ns)
402405
}
403406

404407
// any explicit alignment for the struct?
405-
if(type.find(ID_C_alignment).is_not_nil())
408+
const exprt &alignment =
409+
static_cast<const exprt &>(type.find(ID_C_alignment));
410+
if(alignment.is_not_nil())
406411
{
407-
const exprt &alignment=
408-
static_cast<const exprt &>(type.find(ID_C_alignment));
409412
if(alignment.id()!=ID_default)
410413
{
411-
exprt tmp=alignment;
412-
simplify(tmp, ns);
413-
mp_integer tmp_i;
414-
if(!to_integer(tmp, tmp_i) && tmp_i>max_alignment)
415-
max_alignment=tmp_i;
414+
const auto tmp_i = numeric_cast<mp_integer>(simplify_expr(alignment, ns));
415+
416+
if(tmp_i.has_value() && *tmp_i > max_alignment)
417+
max_alignment = *tmp_i;
416418
}
417419
}
418420
// Is the struct packed, without any alignment specification?

src/ansi-c/type2name.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,19 @@ static std::string type2name(
178178
}
179179
else if(type.id()==ID_array)
180180
{
181-
const array_typet &t=to_array_type(type);
182-
mp_integer size;
183-
if(t.size().id()==ID_symbol)
184-
result += "ARR" + id2string(to_symbol_expr(t.size()).get_identifier());
185-
else if(to_integer(t.size(), size))
186-
result+="ARR?";
181+
const exprt &size = to_array_type(type).size();
182+
183+
if(size.id() == ID_symbol)
184+
result += "ARR" + id2string(to_symbol_expr(size).get_identifier());
187185
else
188-
result+="ARR"+integer2string(size);
186+
{
187+
const auto size_int = numeric_cast<mp_integer>(size);
188+
189+
if(!size_int.has_value())
190+
result += "ARR?";
191+
else
192+
result += "ARR" + integer2string(*size_int);
193+
}
189194
}
190195
else if(
191196
type.id() == ID_symbol_type || type.id() == ID_c_enum_tag ||

src/cpp/cpp_typecheck_constructor.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,11 @@ void cpp_typecheckt::default_assignop_value(
395395
continue;
396396
}
397397

398-
mp_integer size;
399-
bool to_int=to_integer(size_expr, size);
400-
CHECK_RETURN(!to_int);
401-
CHECK_RETURN(size>=0);
398+
const auto size = numeric_cast<mp_integer>(size_expr);
399+
CHECK_RETURN(!size.has_value());
400+
CHECK_RETURN(*size >= 0);
402401

403-
exprt::operandst empty_operands;
404-
for(mp_integer i=0; i < size; ++i)
402+
for(mp_integer i = 0; i < *size; ++i)
405403
copy_array(source_location, mem_name, i, arg_name, block);
406404
}
407405
else

src/goto-symex/build_goto_trace.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ void build_goto_trace(
228228
exprt clock_value=prop_conv.get(
229229
symbol_exprt(partial_order_concurrencyt::rw_clock_id(it)));
230230

231-
to_integer(clock_value, current_time);
231+
const auto cv = numeric_cast<mp_integer>(clock_value);
232+
if(cv.has_value())
233+
current_time = *cv;
234+
else
235+
current_time = 0;
232236
}
233237
else if(it->is_atomic_end() && current_time<0)
234238
current_time*=-1;

src/solvers/flattening/boolbv.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,17 +323,17 @@ bvt boolbvt::convert_lambda(const exprt &expr)
323323
const exprt &array_size=
324324
to_array_type(expr.type()).size();
325325

326-
mp_integer size;
326+
const auto size = numeric_cast<mp_integer>(array_size);
327327

328-
if(to_integer(array_size, size))
328+
if(!size.has_value())
329329
return conversion_failed(expr);
330330

331331
typet counter_type=expr.op0().type();
332332

333333
bvt bv;
334334
bv.resize(width);
335335

336-
for(mp_integer i=0; i<size; ++i)
336+
for(mp_integer i = 0; i < *size; ++i)
337337
{
338338
exprt counter=from_integer(i, counter_type);
339339

@@ -343,7 +343,7 @@ bvt boolbvt::convert_lambda(const exprt &expr)
343343
const bvt &tmp=convert_bv(expr_op1);
344344

345345
INVARIANT(
346-
size * tmp.size() == width,
346+
*size * tmp.size() == width,
347347
"total bitvector width shall equal the number of operands times the size "
348348
"per operand");
349349

src/solvers/flattening/boolbv_byte_update.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ bvt boolbvt::convert_byte_update(const byte_update_exprt &expr)
3838

3939
// see if the byte number is constant
4040

41-
mp_integer index;
42-
if(!to_integer(offset_expr, index))
41+
const auto index = numeric_cast<mp_integer>(offset_expr);
42+
if(index.has_value())
4343
{
4444
// yes!
45-
mp_integer offset=index*8;
45+
const mp_integer offset = *index * 8;
4646

4747
if(offset+update_width>mp_integer(bv.size()) || offset<0)
4848
{

src/solvers/flattening/boolbv_extractbit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ literalt boolbvt::convert_extractbit(const extractbit_exprt &expr)
2828
if(index_as_integer < 0 || index_as_integer >= src_bv.size())
2929
return prop.new_variable(); // out of range!
3030
else
31-
return src_bv[integer2size_t(index_as_integer)];
31+
return src_bv[numeric_cast_v<std::size_t>(index_as_integer)];
3232
}
3333

3434
if(

src/solvers/flattening/boolbv_width.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,16 @@ const boolbv_widtht::entryt &boolbv_widtht::get_entry(const typet &type) const
128128
const array_typet &array_type=to_array_type(type);
129129
std::size_t sub_width=operator()(array_type.subtype());
130130

131-
mp_integer array_size;
131+
const auto array_size = numeric_cast<mp_integer>(array_type.size());
132132

133-
if(to_integer(array_type.size(), array_size))
133+
if(!array_size.has_value())
134134
{
135135
// we can still use the theory of arrays for this
136136
entry.total_width=0;
137137
}
138138
else
139139
{
140-
mp_integer total=array_size*sub_width;
140+
mp_integer total = *array_size * sub_width;
141141
if(total>(1<<30)) // realistic limit
142142
throw analysis_exceptiont("array too large for flattening");
143143

@@ -149,21 +149,13 @@ const boolbv_widtht::entryt &boolbv_widtht::get_entry(const typet &type) const
149149
const vector_typet &vector_type=to_vector_type(type);
150150
std::size_t sub_width=operator()(vector_type.subtype());
151151

152-
mp_integer vector_size;
152+
const auto vector_size = numeric_cast_v<mp_integer>(vector_type.size());
153153

154-
if(to_integer(vector_type.size(), vector_size))
155-
{
156-
// we can still use the theory of arrays for this
157-
entry.total_width=0;
158-
}
159-
else
160-
{
161-
mp_integer total=vector_size*sub_width;
162-
if(total>(1<<30)) // realistic limit
163-
analysis_exceptiont("vector too large for flattening");
154+
mp_integer total = vector_size * sub_width;
155+
if(total > (1 << 30)) // realistic limit
156+
analysis_exceptiont("vector too large for flattening");
164157

165-
entry.total_width = numeric_cast_v<std::size_t>(vector_size * sub_width);
166-
}
158+
entry.total_width = numeric_cast_v<std::size_t>(vector_size * sub_width);
167159
}
168160
else if(type_id==ID_complex)
169161
{

src/solvers/flattening/boolbv_with.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ void boolbvt::convert_with_array(
125125

126126
const exprt &array_size=type.size();
127127

128-
mp_integer size;
128+
const auto size = numeric_cast<mp_integer>(array_size);
129129

130-
if(to_integer(array_size, size))
130+
if(!size.has_value())
131131
{
132132
error().source_location=type.source_location();
133133
error() << "convert_with_array expects constant array size" << eom;
@@ -136,7 +136,7 @@ void boolbvt::convert_with_array(
136136

137137
const bvt &op2_bv=convert_bv(op2);
138138

139-
if(size*op2_bv.size()!=prev_bv.size())
139+
if(*size * op2_bv.size() != prev_bv.size())
140140
{
141141
error().source_location=type.source_location();
142142
error() << "convert_with_array: unexpected operand 2 width" << eom;
@@ -150,7 +150,7 @@ void boolbvt::convert_with_array(
150150
// Yes, it is!
151151
next_bv=prev_bv;
152152

153-
if(op1_value>=0 && op1_value<size) // bounds check
153+
if(op1_value >= 0 && op1_value < *size) // bounds check
154154
{
155155
const std::size_t offset =
156156
numeric_cast_v<std::size_t>(op1_value * op2_bv.size());

src/util/endianness_map.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,23 @@ void endianness_mapt::build_big_endian(const typet &src)
9292
const array_typet &array_type=to_array_type(src);
9393

9494
// array size constant?
95-
mp_integer s;
96-
if(!to_integer(array_type.size(), s))
95+
auto s = numeric_cast<mp_integer>(array_type.size());
96+
if(s.has_value())
9797
{
98-
while(s>0)
98+
while(*s > 0)
9999
{
100100
build_big_endian(array_type.subtype());
101-
--s;
101+
--(*s);
102102
}
103103
}
104104
}
105105
else if(src.id()==ID_vector)
106106
{
107107
const vector_typet &vector_type=to_vector_type(src);
108108

109-
mp_integer s;
110-
if(to_integer(vector_type.size(), s))
111-
CHECK_RETURN(false);
109+
mp_integer s = numeric_cast_v<mp_integer>(vector_type.size());
112110

113-
while(s>0)
111+
while(s > 0)
114112
{
115113
build_big_endian(vector_type.subtype());
116114
--s;

src/util/expr_initializer.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
136136
if(tmpval.is_nil())
137137
return nil_exprt();
138138

139-
mp_integer array_size;
140-
141139
if(array_type.size().id()==ID_infinity)
142140
{
143141
if(nondet)
@@ -147,19 +145,21 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
147145
value.add_source_location()=source_location;
148146
return std::move(value);
149147
}
150-
else if(to_integer(array_type.size(), array_size))
148+
149+
const auto array_size = numeric_cast<mp_integer>(array_type.size());
150+
if(!array_size.has_value())
151151
{
152152
if(nondet)
153153
return side_effect_expr_nondett(type, source_location);
154154
else
155155
return nil_exprt();
156156
}
157157

158-
if(array_size < 0)
158+
if(*array_size < 0)
159159
return nil_exprt();
160160

161161
array_exprt value(array_type);
162-
value.operands().resize(integer2size_t(array_size), tmpval);
162+
value.operands().resize(numeric_cast_v<std::size_t>(*array_size), tmpval);
163163
value.add_source_location()=source_location;
164164
return std::move(value);
165165
}
@@ -172,21 +172,14 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
172172
if(tmpval.is_nil())
173173
return nil_exprt();
174174

175-
mp_integer vector_size;
176-
177-
if(to_integer(vector_type.size(), vector_size))
178-
{
179-
if(nondet)
180-
return side_effect_expr_nondett(type, source_location);
181-
else
182-
return nil_exprt();
183-
}
175+
const mp_integer vector_size =
176+
numeric_cast_v<mp_integer>(vector_type.size());
184177

185178
if(vector_size < 0)
186179
return nil_exprt();
187180

188181
vector_exprt value(vector_type);
189-
value.operands().resize(integer2size_t(vector_size), tmpval);
182+
value.operands().resize(numeric_cast_v<std::size_t>(vector_size), tmpval);
190183
value.add_source_location()=source_location;
191184

192185
return std::move(value);

0 commit comments

Comments
 (0)