Skip to content

Commit 8401f49

Browse files
authored
Merge pull request diffblue#2137 from diffblue/optional_pointer_offset
use optional for unknown pointer offset sizes
2 parents 270d487 + 3f59028 commit 8401f49

31 files changed

+578
-458
lines changed

src/analyses/goto_rw.cpp

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,14 @@ void rw_range_sett::get_objects_complex_imag(
9999
{
100100
const exprt &op = expr.op();
101101

102-
range_spect sub_size =
103-
to_range_spect(pointer_offset_bits(op.type().subtype(), ns));
104-
assert(sub_size>0);
105-
range_spect offset = range_start == -1 ? 0 : sub_size;
102+
auto subtype_bits = pointer_offset_bits(op.type().subtype(), ns);
103+
CHECK_RETURN(subtype_bits.has_value());
104+
105+
range_spect sub_size = to_range_spect(*subtype_bits);
106+
CHECK_RETURN(sub_size > 0);
107+
108+
range_spect offset=
109+
(range_start==-1 || expr.id()==ID_complex_real) ? 0 : sub_size;
106110

107111
get_objects_rec(mode, op, range_start + offset, size);
108112
}
@@ -173,8 +177,9 @@ void rw_range_sett::get_objects_shift(
173177
{
174178
const exprt simp_distance=simplify_expr(shift.distance(), ns);
175179

176-
range_spect src_size=
177-
to_range_spect(pointer_offset_bits(shift.op().type(), ns));
180+
auto op_bits = pointer_offset_bits(shift.op().type(), ns);
181+
182+
range_spect src_size = op_bits.has_value() ? to_range_spect(*op_bits) : -1;
178183

179184
mp_integer dist;
180185
if(range_start==-1 ||
@@ -230,15 +235,18 @@ void rw_range_sett::get_objects_member(
230235

231236
const struct_typet &struct_type=to_struct_type(type);
232237

233-
range_spect offset=
234-
to_range_spect(
235-
member_offset_bits(
236-
struct_type,
237-
expr.get_component_name(),
238-
ns));
238+
auto offset_bits =
239+
member_offset_bits(struct_type, expr.get_component_name(), ns);
239240

240-
if(offset!=-1)
241-
offset+=range_start;
241+
range_spect offset;
242+
243+
if(offset_bits.has_value())
244+
{
245+
offset = to_range_spect(*offset_bits);
246+
offset += range_start;
247+
}
248+
else
249+
offset = -1;
242250

243251
get_objects_rec(mode, expr.struct_op(), offset, size);
244252
}
@@ -259,15 +267,17 @@ void rw_range_sett::get_objects_index(
259267
{
260268
const vector_typet &vector_type=to_vector_type(type);
261269

262-
sub_size=
263-
to_range_spect(pointer_offset_bits(vector_type.subtype(), ns));
270+
auto subtype_bits = pointer_offset_bits(vector_type.subtype(), ns);
271+
272+
sub_size = subtype_bits.has_value() ? to_range_spect(*subtype_bits) : -1;
264273
}
265274
else if(type.id()==ID_array)
266275
{
267276
const array_typet &array_type=to_array_type(type);
268277

269-
sub_size=
270-
to_range_spect(pointer_offset_bits(array_type.subtype(), ns));
278+
auto subtype_bits = pointer_offset_bits(array_type.subtype(), ns);
279+
280+
sub_size = subtype_bits.has_value() ? to_range_spect(*subtype_bits) : -1;
271281
}
272282
else
273283
return;
@@ -302,10 +312,13 @@ void rw_range_sett::get_objects_array(
302312
const array_typet &array_type=
303313
to_array_type(ns.follow(expr.type()));
304314

305-
range_spect sub_size=
306-
to_range_spect(pointer_offset_bits(array_type.subtype(), ns));
315+
auto subtype_bits = pointer_offset_bits(array_type.subtype(), ns);
316+
317+
range_spect sub_size;
307318

308-
if(sub_size==-1)
319+
if(subtype_bits.has_value())
320+
sub_size = to_range_spect(*subtype_bits);
321+
else
309322
{
310323
forall_operands(it, expr)
311324
get_objects_rec(mode, *it, 0, -1);
@@ -342,17 +355,20 @@ void rw_range_sett::get_objects_struct(
342355
const struct_typet &struct_type=
343356
to_struct_type(ns.follow(expr.type()));
344357

345-
range_spect full_size=
346-
to_range_spect(pointer_offset_bits(struct_type, ns));
358+
auto struct_bits = pointer_offset_bits(struct_type, ns);
359+
360+
range_spect full_size =
361+
struct_bits.has_value() ? to_range_spect(*struct_bits) : -1;
347362

348363
range_spect offset=0;
349364
range_spect full_r_s=range_start==-1 ? 0 : range_start;
350365
range_spect full_r_e=size==-1 || full_size==-1 ? -1 : full_r_s+size;
351366

352367
forall_operands(it, expr)
353368
{
354-
range_spect sub_size=
355-
to_range_spect(pointer_offset_bits(it->type(), ns));
369+
auto it_bits = pointer_offset_bits(it->type(), ns);
370+
371+
range_spect sub_size = it_bits.has_value() ? to_range_spect(*it_bits) : -1;
356372

357373
if(offset==-1)
358374
{
@@ -401,8 +417,9 @@ void rw_range_sett::get_objects_typecast(
401417
{
402418
const exprt &op=tc.op();
403419

404-
range_spect new_size=
405-
to_range_spect(pointer_offset_bits(op.type(), ns));
420+
auto op_bits = pointer_offset_bits(op.type(), ns);
421+
422+
range_spect new_size = op_bits.has_value() ? to_range_spect(*op_bits) : -1;
406423

407424
if(range_start==-1)
408425
new_size=-1;
@@ -537,8 +554,11 @@ void rw_range_sett::get_objects_rec(
537554
{
538555
const symbol_exprt &symbol=to_symbol_expr(expr);
539556
const irep_idt identifier=symbol.get_identifier();
540-
range_spect full_size=
541-
to_range_spect(pointer_offset_bits(symbol.type(), ns));
557+
558+
auto symbol_bits = pointer_offset_bits(symbol.type(), ns);
559+
560+
range_spect full_size =
561+
symbol_bits.has_value() ? to_range_spect(*symbol_bits) : -1;
542562

543563
if(full_size==0 ||
544564
(full_size>0 && range_start>=full_size))
@@ -584,8 +604,10 @@ void rw_range_sett::get_objects_rec(
584604

585605
void rw_range_sett::get_objects_rec(get_modet mode, const exprt &expr)
586606
{
587-
range_spect size=
588-
to_range_spect(pointer_offset_bits(expr.type(), ns));
607+
auto expr_bits = pointer_offset_bits(expr.type(), ns);
608+
609+
range_spect size = expr_bits.has_value() ? to_range_spect(*expr_bits) : -1;
610+
589611
get_objects_rec(mode, expr, 0, size);
590612
}
591613

@@ -614,16 +636,24 @@ void rw_range_set_value_sett::get_objects_dereference(
614636
exprt object=deref;
615637
dereference(target, object, ns, value_sets);
616638

617-
range_spect new_size=
618-
to_range_spect(pointer_offset_bits(object.type(), ns));
639+
auto type_bits = pointer_offset_bits(object.type(), ns);
619640

620-
if(range_start==-1 || new_size<=range_start)
621-
new_size=-1;
622-
else
641+
range_spect new_size;
642+
643+
if(type_bits.has_value())
623644
{
624-
new_size-=range_start;
625-
new_size=std::min(size, new_size);
645+
new_size = to_range_spect(*type_bits);
646+
647+
if(range_start == -1 || new_size <= range_start)
648+
new_size = -1;
649+
else
650+
{
651+
new_size -= range_start;
652+
new_size = std::min(size, new_size);
653+
}
626654
}
655+
else
656+
new_size = -1;
627657

628658
// value_set_dereferencet::build_reference_to will turn *p into
629659
// 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
@@ -3617,9 +3617,9 @@ std::string expr2ct::convert_with_precedence(
36173617
else if(src.id()==ID_bswap)
36183618
return convert_function(
36193619
src,
3620-
"__builtin_bswap"+
3621-
integer2string(pointer_offset_bits(src.op0().type(), ns)),
3622-
precedence=16);
3620+
"__builtin_bswap" +
3621+
integer2string(*pointer_offset_bits(src.op0().type(), ns)),
3622+
precedence = 16);
36233623

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

src/ansi-c/padding.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mp_integer alignment(const typet &type, const namespacet &ns)
7070
type.id()==ID_c_bool ||
7171
type.id()==ID_pointer)
7272
{
73-
result=pointer_offset_size(type, ns);
73+
result = *pointer_offset_size(type, ns);
7474
}
7575
else if(type.id()==ID_c_enum)
7676
result=alignment(type.subtype(), ns);
@@ -229,9 +229,9 @@ static void add_padding_msvc(struct_typet &type, const namespacet &ns)
229229
else
230230
{
231231
// keep track of offset
232-
const mp_integer size = pointer_offset_size(it->type(), ns);
233-
if(size >= 1)
234-
offset += size;
232+
const auto size = pointer_offset_size(it->type(), ns);
233+
if(size.has_value() && *size >= 1)
234+
offset += *size;
235235
}
236236
}
237237
}
@@ -375,10 +375,10 @@ static void add_padding_gcc(struct_typet &type, const namespacet &ns)
375375
}
376376
}
377377

378-
mp_integer size=pointer_offset_size(it_type, ns);
378+
auto size = pointer_offset_size(it_type, ns);
379379

380-
if(size!=-1)
381-
offset+=size;
380+
if(size.has_value())
381+
offset += *size;
382382
}
383383

384384
// any explicit alignment for the struct?
@@ -435,9 +435,9 @@ void add_padding(union_typet &type, const namespacet &ns)
435435
// check per component, and ignore those without fixed size
436436
for(const auto &c : type.components())
437437
{
438-
mp_integer s=pointer_offset_bits(c.type(), ns);
439-
if(s>0)
440-
size_bits=std::max(size_bits, s);
438+
auto s = pointer_offset_bits(c.type(), ns);
439+
if(s.has_value())
440+
size_bits = std::max(size_bits, *s);
441441
}
442442

443443
// 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 "

src/goto-instrument/count_eloc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ void print_global_state_size(const goto_modelt &goto_model)
191191
continue;
192192
}
193193

194-
const mp_integer bits = pointer_offset_bits(symbol.type, ns);
195-
if(bits > 0)
196-
total_size += bits;
194+
const auto bits = pointer_offset_bits(symbol.type, ns);
195+
if(bits.has_value() && bits.value() > 0)
196+
total_size += bits.value();
197197
}
198198

199199
std::cout << "Total size of global objects: " << total_size << " bits\n";

0 commit comments

Comments
 (0)