Skip to content

Commit 3d811f0

Browse files
author
Daniel Kroening
committed
use optional for unknown pointer offset sizes
1 parent 7797757 commit 3d811f0

31 files changed

+581
-466
lines changed

src/analyses/goto_rw.cpp

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

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

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

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

172176
mp_integer dist;
173177
if(range_start==-1 ||
@@ -223,15 +227,18 @@ void rw_range_sett::get_objects_member(
223227

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

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

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

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

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

262-
sub_size=
263-
to_range_spect(pointer_offset_bits(array_type.subtype(), ns));
270+
auto subtype_bits = pointer_offset_bits(array_type.subtype(), ns);
271+
272+
sub_size = subtype_bits.has_value() ? to_range_spect(*subtype_bits) : -1;
264273
}
265274
else
266275
return;
@@ -295,10 +304,13 @@ void rw_range_sett::get_objects_array(
295304
const array_typet &array_type=
296305
to_array_type(ns.follow(expr.type()));
297306

298-
range_spect sub_size=
299-
to_range_spect(pointer_offset_bits(array_type.subtype(), ns));
307+
auto subtype_bits = pointer_offset_bits(array_type.subtype(), ns);
300308

301-
if(sub_size==-1)
309+
range_spect sub_size;
310+
311+
if(subtype_bits.has_value())
312+
sub_size = to_range_spect(*subtype_bits);
313+
else
302314
{
303315
forall_operands(it, expr)
304316
get_objects_rec(mode, *it, 0, -1);
@@ -335,17 +347,20 @@ void rw_range_sett::get_objects_struct(
335347
const struct_typet &struct_type=
336348
to_struct_type(ns.follow(expr.type()));
337349

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

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

345359
forall_operands(it, expr)
346360
{
347-
range_spect sub_size=
348-
to_range_spect(pointer_offset_bits(it->type(), ns));
361+
auto it_bits = pointer_offset_bits(it->type(), ns);
362+
363+
range_spect sub_size = it_bits.has_value() ? to_range_spect(*it_bits) : -1;
349364

350365
if(offset==-1)
351366
{
@@ -394,8 +409,9 @@ void rw_range_sett::get_objects_typecast(
394409
{
395410
const exprt &op=tc.op();
396411

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

400416
if(range_start==-1)
401417
new_size=-1;
@@ -527,8 +543,11 @@ void rw_range_sett::get_objects_rec(
527543
{
528544
const symbol_exprt &symbol=to_symbol_expr(expr);
529545
const irep_idt identifier=symbol.get_identifier();
530-
range_spect full_size=
531-
to_range_spect(pointer_offset_bits(symbol.type(), ns));
546+
547+
auto symbol_bits = pointer_offset_bits(symbol.type(), ns);
548+
549+
range_spect full_size =
550+
symbol_bits.has_value() ? to_range_spect(*symbol_bits) : -1;
532551

533552
if(full_size==0 ||
534553
(full_size>0 && range_start>=full_size))
@@ -574,8 +593,10 @@ void rw_range_sett::get_objects_rec(
574593

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

@@ -604,16 +625,24 @@ void rw_range_set_value_sett::get_objects_dereference(
604625
exprt object=deref;
605626
dereference(target, object, ns, value_sets);
606627

607-
range_spect new_size=
608-
to_range_spect(pointer_offset_bits(object.type(), ns));
628+
auto type_bits = pointer_offset_bits(object.type(), ns);
609629

610-
if(range_start==-1 || new_size<=range_start)
611-
new_size=-1;
612-
else
630+
range_spect new_size;
631+
632+
if(type_bits.has_value())
613633
{
614-
new_size-=range_start;
615-
new_size=std::min(size, new_size);
634+
new_size = to_range_spect(*type_bits);
635+
636+
if(range_start == -1 || new_size <= range_start)
637+
new_size = -1;
638+
else
639+
{
640+
new_size -= range_start;
641+
new_size = std::min(size, new_size);
642+
}
616643
}
644+
else
645+
new_size = -1;
617646

618647
// value_set_dereferencet::build_reference_to will turn *p into
619648
// 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
@@ -3634,9 +3634,9 @@ std::string expr2ct::convert_with_precedence(
36343634
else if(src.id()==ID_bswap)
36353635
return convert_function(
36363636
src,
3637-
"__builtin_bswap"+
3638-
integer2string(pointer_offset_bits(src.op0().type(), ns)),
3639-
precedence=16);
3637+
"__builtin_bswap" +
3638+
integer2string(*pointer_offset_bits(src.op0().type(), ns)),
3639+
precedence = 16);
36403640

36413641
else if(src.id()==ID_isnormal)
36423642
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
@@ -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);
@@ -237,9 +237,9 @@ static void add_padding_msvc(struct_typet &type, const namespacet &ns)
237237
else
238238
{
239239
// keep track of offset
240-
const mp_integer size = pointer_offset_size(it->type(), ns);
241-
if(size >= 1)
242-
offset += size;
240+
const auto size = pointer_offset_size(it->type(), ns);
241+
if(size.has_value() && *size >= 1)
242+
offset += *size;
243243
}
244244
}
245245
}
@@ -383,10 +383,10 @@ static void add_padding_gcc(struct_typet &type, const namespacet &ns)
383383
}
384384
}
385385

386-
mp_integer size=pointer_offset_size(it_type, ns);
386+
auto size = pointer_offset_size(it_type, ns);
387387

388-
if(size!=-1)
389-
offset+=size;
388+
if(size.has_value())
389+
offset += *size;
390390
}
391391

392392
// any explicit alignment for the struct?
@@ -443,9 +443,9 @@ void add_padding(union_typet &type, const namespacet &ns)
443443
// check per component, and ignore those without fixed size
444444
for(const auto &c : type.components())
445445
{
446-
mp_integer s=pointer_offset_bits(c.type(), ns);
447-
if(s>0)
448-
size_bits=std::max(size_bits, s);
446+
auto s = pointer_offset_bits(c.type(), ns);
447+
if(s.has_value())
448+
size_bits = std::max(size_bits, *s);
449449
}
450450

451451
// 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)