Skip to content

Commit 2f4cf0e

Browse files
committed
Implement union values
Reuse get_struct_member for unions (copy-paste).
1 parent a305967 commit 2f4cf0e

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/memory-analyzer/analyze_symbol.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ exprt gdb_value_extractort::get_expr_value(
421421

422422
return get_pointer_value(expr, zero_expr, location);
423423
}
424-
UNIMPLEMENTED;
424+
else if(type.id() == ID_union_tag)
425+
{
426+
return get_union_value(expr, zero_expr, location);
427+
}
428+
UNREACHABLE;
425429
}
426430

427431
exprt gdb_value_extractort::get_struct_value(
@@ -457,6 +461,28 @@ exprt gdb_value_extractort::get_struct_value(
457461
return new_expr;
458462
}
459463

464+
exprt gdb_value_extractort::get_union_value(
465+
const exprt &expr,
466+
const exprt &zero_expr,
467+
const source_locationt &location)
468+
{
469+
PRECONDITION(zero_expr.id() == ID_union);
470+
471+
PRECONDITION(expr.type().id() == ID_union_tag);
472+
PRECONDITION(expr.type() == zero_expr.type());
473+
474+
exprt new_expr(zero_expr);
475+
476+
const union_tag_typet &union_tag_type = to_union_tag_type(expr.type());
477+
const union_typet &union_type = ns.follow_tag(union_tag_type);
478+
479+
CHECK_RETURN(new_expr.operands().size() == 1);
480+
const union_typet::componentt &component = union_type.components()[0];
481+
auto &operand = new_expr.operands()[0];
482+
operand = get_expr_value(member_exprt{expr, component}, operand, location);
483+
return new_expr;
484+
}
485+
460486
void gdb_value_extractort::process_outstanding_assignments()
461487
{
462488
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)