Skip to content

Commit 53e10d9

Browse files
author
Daniel Kroening
committed
use optional for unknown pointer offset sizes
1 parent da34ceb commit 53e10d9

30 files changed

+575
-462
lines changed

src/analyses/goto_rw.cpp

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,12 @@ void rw_range_sett::get_objects_complex(
9292
const exprt &op=expr.op0();
9393
assert(op.type().id()==ID_complex);
9494

95-
range_spect sub_size=
96-
to_range_spect(pointer_offset_bits(op.type().subtype(), ns));
97-
assert(sub_size>0);
95+
auto subtype_bits = pointer_offset_bits(op.type().subtype(), ns);
96+
CHECK_RETURN(subtype_bits.has_value());
97+
98+
range_spect sub_size = to_range_spect(*subtype_bits);
99+
CHECK_RETURN(sub_size > 0);
100+
98101
range_spect offset=
99102
(range_start==-1 || expr.id()==ID_complex_real) ? 0 : sub_size;
100103

@@ -167,8 +170,9 @@ void rw_range_sett::get_objects_shift(
167170
{
168171
const exprt simp_distance=simplify_expr(shift.distance(), ns);
169172

170-
range_spect src_size=
171-
to_range_spect(pointer_offset_bits(shift.op().type(), ns));
173+
auto op_bits = pointer_offset_bits(shift.op().type(), ns);
174+
175+
range_spect src_size = op_bits.has_value() ? to_range_spect(*op_bits) : -1;
172176

173177
mp_integer dist;
174178
if(range_start==-1 ||
@@ -224,15 +228,18 @@ void rw_range_sett::get_objects_member(
224228

225229
const struct_typet &struct_type=to_struct_type(type);
226230

227-
range_spect offset=
228-
to_range_spect(
229-
member_offset_bits(
230-
struct_type,
231-
expr.get_component_name(),
232-
ns));
231+
auto offset_bits =
232+
member_offset_bits(struct_type, expr.get_component_name(), ns);
233233

234-
if(offset!=-1)
235-
offset+=range_start;
234+
range_spect offset;
235+
236+
if(offset_bits.has_value())
237+
{
238+
offset = to_range_spect(*offset_bits);
239+
offset += range_start;
240+
}
241+
else
242+
offset = -1;
236243

237244
get_objects_rec(mode, expr.struct_op(), offset, size);
238245
}
@@ -253,15 +260,17 @@ void rw_range_sett::get_objects_index(
253260
{
254261
const vector_typet &vector_type=to_vector_type(type);
255262

256-
sub_size=
257-
to_range_spect(pointer_offset_bits(vector_type.subtype(), ns));
263+
auto subtype_bits = pointer_offset_bits(vector_type.subtype(), ns);
264+
265+
sub_size = subtype_bits.has_value() ? to_range_spect(*subtype_bits) : -1;
258266
}
259267
else if(type.id()==ID_array)
260268
{
261269
const array_typet &array_type=to_array_type(type);
262270

263-
sub_size=
264-
to_range_spect(pointer_offset_bits(array_type.subtype(), ns));
271+
auto subtype_bits = pointer_offset_bits(array_type.subtype(), ns);
272+
273+
sub_size = subtype_bits.has_value() ? to_range_spect(*subtype_bits) : -1;
265274
}
266275
else
267276
return;
@@ -296,10 +305,13 @@ void rw_range_sett::get_objects_array(
296305
const array_typet &array_type=
297306
to_array_type(ns.follow(expr.type()));
298307

299-
range_spect sub_size=
300-
to_range_spect(pointer_offset_bits(array_type.subtype(), ns));
308+
auto subtype_bits = pointer_offset_bits(array_type.subtype(), ns);
301309

302-
if(sub_size==-1)
310+
range_spect sub_size;
311+
312+
if(subtype_bits.has_value())
313+
sub_size = to_range_spect(*subtype_bits);
314+
else
303315
{
304316
forall_operands(it, expr)
305317
get_objects_rec(mode, *it, 0, -1);
@@ -336,17 +348,20 @@ void rw_range_sett::get_objects_struct(
336348
const struct_typet &struct_type=
337349
to_struct_type(ns.follow(expr.type()));
338350

339-
range_spect full_size=
340-
to_range_spect(pointer_offset_bits(struct_type, ns));
351+
auto struct_bits = pointer_offset_bits(struct_type, ns);
352+
353+
range_spect full_size =
354+
struct_bits.has_value() ? to_range_spect(*struct_bits) : -1;
341355

342356
range_spect offset=0;
343357
range_spect full_r_s=range_start==-1 ? 0 : range_start;
344358
range_spect full_r_e=size==-1 || full_size==-1 ? -1 : full_r_s+size;
345359

346360
forall_operands(it, expr)
347361
{
348-
range_spect sub_size=
349-
to_range_spect(pointer_offset_bits(it->type(), ns));
362+
auto it_bits = pointer_offset_bits(it->type(), ns);
363+
364+
range_spect sub_size = it_bits.has_value() ? to_range_spect(*it_bits) : -1;
350365

351366
if(offset==-1)
352367
{
@@ -395,8 +410,9 @@ void rw_range_sett::get_objects_typecast(
395410
{
396411
const exprt &op=tc.op();
397412

398-
range_spect new_size=
399-
to_range_spect(pointer_offset_bits(op.type(), ns));
413+
auto op_bits = pointer_offset_bits(op.type(), ns);
414+
415+
range_spect new_size = op_bits.has_value() ? to_range_spect(*op_bits) : -1;
400416

401417
if(range_start==-1)
402418
new_size=-1;
@@ -528,8 +544,11 @@ void rw_range_sett::get_objects_rec(
528544
{
529545
const symbol_exprt &symbol=to_symbol_expr(expr);
530546
const irep_idt identifier=symbol.get_identifier();
531-
range_spect full_size=
532-
to_range_spect(pointer_offset_bits(symbol.type(), ns));
547+
548+
auto symbol_bits = pointer_offset_bits(symbol.type(), ns);
549+
550+
range_spect full_size =
551+
symbol_bits.has_value() ? to_range_spect(*symbol_bits) : -1;
533552

534553
if(full_size==0 ||
535554
(full_size>0 && range_start>=full_size))
@@ -575,8 +594,10 @@ void rw_range_sett::get_objects_rec(
575594

576595
void rw_range_sett::get_objects_rec(get_modet mode, const exprt &expr)
577596
{
578-
range_spect size=
579-
to_range_spect(pointer_offset_bits(expr.type(), ns));
597+
auto expr_bits = pointer_offset_bits(expr.type(), ns);
598+
599+
range_spect size = expr_bits.has_value() ? to_range_spect(*expr_bits) : -1;
600+
580601
get_objects_rec(mode, expr, 0, size);
581602
}
582603

@@ -605,16 +626,24 @@ void rw_range_set_value_sett::get_objects_dereference(
605626
exprt object=deref;
606627
dereference(target, object, ns, value_sets);
607628

608-
range_spect new_size=
609-
to_range_spect(pointer_offset_bits(object.type(), ns));
629+
auto type_bits = pointer_offset_bits(object.type(), ns);
610630

611-
if(range_start==-1 || new_size<=range_start)
612-
new_size=-1;
613-
else
631+
range_spect new_size;
632+
633+
if(type_bits.has_value())
614634
{
615-
new_size-=range_start;
616-
new_size=std::min(size, new_size);
635+
new_size = to_range_spect(*type_bits);
636+
637+
if(range_start == -1 || new_size <= range_start)
638+
new_size = -1;
639+
else
640+
{
641+
new_size -= range_start;
642+
new_size = std::min(size, new_size);
643+
}
617644
}
645+
else
646+
new_size = -1;
618647

619648
// value_set_dereferencet::build_reference_to will turn *p into
620649
// DYNAMIC_OBJECT(p) ? *p : invalid_objectN

src/analyses/reaching_definitions.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,11 @@ void rd_range_domaint::transform_function_call(
210210
if(identifier.empty())
211211
continue;
212212

213-
range_spect size=
214-
to_range_spect(pointer_offset_bits(param.type(), ns));
215-
gen(from, identifier, 0, size);
213+
auto param_bits = pointer_offset_bits(param.type(), ns);
214+
if(param_bits.has_value())
215+
gen(from, identifier, 0, to_range_spect(*param_bits));
216+
else
217+
gen(from, identifier, 0, -1);
216218
}
217219
}
218220
else

src/ansi-c/expr2c.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3624,9 +3624,9 @@ std::string expr2ct::convert_with_precedence(
36243624
else if(src.id()==ID_bswap)
36253625
return convert_function(
36263626
src,
3627-
"__builtin_bswap"+
3628-
integer2string(pointer_offset_bits(src.op0().type(), ns)),
3629-
precedence=16);
3627+
"__builtin_bswap" +
3628+
integer2string(*pointer_offset_bits(src.op0().type(), ns)),
3629+
precedence = 16);
36303630

36313631
else if(src.id()==ID_isnormal)
36323632
return convert_function(src, "isnormal", precedence=16);

src/ansi-c/padding.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mp_integer alignment(const typet &type, const namespacet &ns)
7676
type.id()==ID_c_bool ||
7777
type.id()==ID_pointer)
7878
{
79-
result=pointer_offset_size(type, ns);
79+
result = *pointer_offset_size(type, ns);
8080
}
8181
else if(type.id()==ID_c_enum)
8282
result=alignment(type.subtype(), ns);
@@ -243,10 +243,10 @@ void add_padding(struct_typet &type, const namespacet &ns)
243243
}
244244
}
245245

246-
mp_integer size=pointer_offset_size(it_type, ns);
246+
auto size = pointer_offset_size(it_type, ns);
247247

248-
if(size!=-1)
249-
offset+=size;
248+
if(size.has_value())
249+
offset += *size;
250250
}
251251

252252
// any explicit alignment for the struct?
@@ -301,9 +301,9 @@ void add_padding(union_typet &type, const namespacet &ns)
301301
// check per component, and ignore those without fixed size
302302
for(const auto &c : type.components())
303303
{
304-
mp_integer s=pointer_offset_bits(c.type(), ns);
305-
if(s>0)
306-
size_bits=std::max(size_bits, s);
304+
auto s = pointer_offset_bits(c.type(), ns);
305+
if(s.has_value())
306+
size_bits = std::max(size_bits, *s);
307307
}
308308

309309
// Is the union packed?

src/ansi-c/type2name.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ static std::string pointer_offset_bits_as_string(
8686
const typet &type,
8787
const namespacet &ns)
8888
{
89-
mp_integer bits = pointer_offset_bits(type, ns);
90-
CHECK_RETURN(bits != -1);
91-
return integer2string(bits);
89+
auto bits = pointer_offset_bits(type, ns);
90+
CHECK_RETURN(bits.has_value());
91+
return integer2string(*bits);
9292
}
9393

9494
static bool parent_is_sym_check=false;

src/goto-instrument/alignment_checks.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ void print_struct_alignment_problems(
5151
{
5252
const typet &it_type = it_next->type();
5353
const namespacet ns(symbol_table);
54-
mp_integer size = pointer_offset_size(it_type, ns);
54+
auto size = pointer_offset_size(it_type, ns);
5555

56-
if(size < 0)
56+
if(!size.has_value())
5757
throw "type of unknown size:\n" + it_type.pretty();
5858

59-
cumulated_length += size;
59+
cumulated_length += *size;
6060
// [it_mem;it_next] cannot be covered by an instruction
6161
if(cumulated_length > config.ansi_c.memory_operand_size)
6262
{
@@ -92,13 +92,13 @@ void print_struct_alignment_problems(
9292
#if 0
9393
const namespacet ns(symbol_table);
9494
const array_typet array=to_array_type(symbol_pair.second.type);
95-
const mp_integer size=
95+
const auto size=
9696
pointer_offset_size(array.subtype(), ns);
9797

98-
if(size<0)
98+
if(!size.has_value())
9999
throw "type of unknown size:\n"+it_type.pretty();
100100

101-
if(2*integer2long(size)<=config.ansi_c.memory_operand_size)
101+
if(2*integer2long(*size)<=config.ansi_c.memory_operand_size)
102102
{
103103
out << "\nWARNING: "
104104
<< "declaration of an array at "

0 commit comments

Comments
 (0)