Skip to content

Commit cd35287

Browse files
committed
Implement union values
Reuse get_struct_member for unions (copy-paste).
1 parent 80b88e4 commit cd35287

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/memory-analyzer/analyze_symbol.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ exprt gdb_value_extractort::get_expr_value(
430430

431431
return get_pointer_value(expr, zero_expr, location);
432432
}
433+
else if(type.id() == ID_union_tag)
434+
{
435+
return get_union_value(expr, zero_expr, location);
436+
}
433437
UNREACHABLE;
434438
}
435439

@@ -466,6 +470,39 @@ exprt gdb_value_extractort::get_struct_value(
466470
return new_expr;
467471
}
468472

473+
exprt gdb_value_extractort::get_union_value(
474+
const exprt &expr,
475+
const exprt &zero_expr,
476+
const source_locationt &location)
477+
{
478+
PRECONDITION(zero_expr.id() == ID_union);
479+
480+
PRECONDITION(expr.type().id() == ID_union_tag);
481+
PRECONDITION(expr.type() == zero_expr.type());
482+
483+
exprt new_expr(zero_expr);
484+
485+
const union_tag_typet &union_tag_type = to_union_tag_type(expr.type());
486+
const union_typet union_type = ns.follow_tag(union_tag_type);
487+
488+
for(size_t i = 0; i < new_expr.operands().size(); ++i)
489+
{
490+
const union_typet::componentt &component = union_type.components()[i];
491+
492+
if(component.get_is_padding())
493+
{
494+
continue;
495+
}
496+
497+
exprt &operand = new_expr.operands()[i];
498+
member_exprt member_expr(expr, component);
499+
500+
operand = get_expr_value(member_expr, operand, location);
501+
}
502+
503+
return new_expr;
504+
}
505+
469506
void gdb_value_extractort::process_outstanding_assignments()
470507
{
471508
for(const auto &pair : outstanding_assignments)

src/memory-analyzer/analyze_symbol.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ class gdb_value_extractort
138138
const exprt &zero_expr,
139139
const source_locationt &location);
140140

141+
/// For each of the members of the struct: call \ref get_expr_value
142+
/// \param expr: struct expression to be analysed
143+
/// \param zero_expr: struct with zero-initialised members
144+
/// \param location: the source location
145+
/// \return the value of the struct from \ref gdb_apit
146+
exprt get_union_value(
147+
const exprt &expr,
148+
const exprt &zero_expr,
149+
const source_locationt &location);
150+
141151
/// Call \ref gdb_apit::get_memory on \p expr then split based on the
142152
/// points-to type being `char` type or not. These have dedicated functions.
143153
/// \param expr: the input pointer expression

0 commit comments

Comments
 (0)