Skip to content

Commit debaefc

Browse files
Replace #ifdef by template argument
This make it easier to detect problems in the code that does not get compiled, for instance here a missing ns argument, and open the possibility to unit-test both versions of the function.
1 parent 680ddef commit debaefc

File tree

1 file changed

+67
-52
lines changed

1 file changed

+67
-52
lines changed

src/goto-symex/symex_assign.cpp

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ Author: Daniel Kroening, [email protected]
2222
// update_exprt.
2323
// #define USE_UPDATE
2424

25+
constexpr bool use_update()
26+
{
27+
#ifdef USE_UPDATE
28+
return true;
29+
#else
30+
return false;
31+
#endif
32+
}
33+
2534
/// Store the \p what expression by recursively descending into the operands
2635
/// of \p lhs until the first operand \c op0 is _nil_: this _nil_ operand
2736
/// is then replaced with \p what.
@@ -151,76 +160,81 @@ struct assignmentt final
151160
/// \param assignment: an assignment to rewrite
152161
/// \param ns: namespace
153162
/// \return the updated assignment
163+
template <bool use_update>
154164
static assignmentt rewrite_with_to_field_symbols(
155165
goto_symext::statet &state,
156166
assignmentt assignment,
157167
const namespacet &ns)
158168
{
159169
exprt &ssa_rhs = assignment.rhs;
160170
ssa_exprt &lhs_mod = assignment.lhs;
161-
#ifdef USE_UPDATE
162-
while(ssa_rhs.id() == ID_update &&
163-
to_update_expr(ssa_rhs).designator().size() == 1 &&
164-
(lhs_mod.type().id() == ID_array || lhs_mod.type().id() == ID_struct ||
165-
lhs_mod.type().id() == ID_struct_tag))
171+
if(use_update)
166172
{
167-
exprt field_sensitive_lhs;
168-
const update_exprt &update = to_update_expr(ssa_rhs);
169-
PRECONDITION(update.designator().size() == 1);
170-
const exprt &designator = update.designator().front();
171-
172-
if(lhs_mod.type().id() == ID_array)
173+
while(ssa_rhs.id() == ID_update &&
174+
to_update_expr(ssa_rhs).designator().size() == 1 &&
175+
(lhs_mod.type().id() == ID_array ||
176+
lhs_mod.type().id() == ID_struct ||
177+
lhs_mod.type().id() == ID_struct_tag))
173178
{
174-
field_sensitive_lhs =
175-
index_exprt(lhs_mod, to_index_designator(designator).index());
176-
}
177-
else
178-
{
179-
field_sensitive_lhs = member_exprt(
180-
lhs_mod,
181-
to_member_designator(designator).get_component_name(),
182-
update.new_value().type());
183-
}
179+
exprt field_sensitive_lhs;
180+
const update_exprt &update = to_update_expr(ssa_rhs);
181+
PRECONDITION(update.designator().size() == 1);
182+
const exprt &designator = update.designator().front();
183+
184+
if(lhs_mod.type().id() == ID_array)
185+
{
186+
field_sensitive_lhs =
187+
index_exprt(lhs_mod, to_index_designator(designator).index());
188+
}
189+
else
190+
{
191+
field_sensitive_lhs = member_exprt(
192+
lhs_mod,
193+
to_member_designator(designator).get_component_name(),
194+
update.new_value().type());
195+
}
184196

185-
state.field_sensitivity.apply(ns, state, field_sensitive_lhs, true);
197+
state.field_sensitivity.apply(ns, state, field_sensitive_lhs, true);
186198

187-
if(field_sensitive_lhs.id() != ID_symbol)
188-
break;
199+
if(field_sensitive_lhs.id() != ID_symbol)
200+
break;
189201

190-
ssa_rhs = update.new_value();
191-
lhs_mod = to_ssa_expr(field_sensitive_lhs);
202+
ssa_rhs = update.new_value();
203+
lhs_mod = to_ssa_expr(field_sensitive_lhs);
204+
}
192205
}
193-
#else
194-
while(ssa_rhs.id() == ID_with &&
195-
to_with_expr(ssa_rhs).operands().size() == 3 &&
196-
(lhs_mod.type().id() == ID_array || lhs_mod.type().id() == ID_struct ||
197-
lhs_mod.type().id() == ID_struct_tag))
206+
else
198207
{
199-
exprt field_sensitive_lhs;
200-
const with_exprt &with_expr = to_with_expr(ssa_rhs);
201-
202-
if(lhs_mod.type().id() == ID_array)
208+
while(
209+
ssa_rhs.id() == ID_with && to_with_expr(ssa_rhs).operands().size() == 3 &&
210+
(lhs_mod.type().id() == ID_array || lhs_mod.type().id() == ID_struct ||
211+
lhs_mod.type().id() == ID_struct_tag))
203212
{
204-
field_sensitive_lhs = index_exprt(lhs_mod, with_expr.where());
205-
}
206-
else
207-
{
208-
field_sensitive_lhs = member_exprt(
209-
lhs_mod,
210-
with_expr.where().get(ID_component_name),
211-
with_expr.new_value().type());
212-
}
213+
exprt field_sensitive_lhs;
214+
const with_exprt &with_expr = to_with_expr(ssa_rhs);
213215

214-
field_sensitive_lhs = state.field_sensitivity.apply(
215-
ns, state, std::move(field_sensitive_lhs), true);
216+
if(lhs_mod.type().id() == ID_array)
217+
{
218+
field_sensitive_lhs = index_exprt(lhs_mod, with_expr.where());
219+
}
220+
else
221+
{
222+
field_sensitive_lhs = member_exprt(
223+
lhs_mod,
224+
with_expr.where().get(ID_component_name),
225+
with_expr.new_value().type());
226+
}
227+
228+
field_sensitive_lhs = state.field_sensitivity.apply(
229+
ns, state, std::move(field_sensitive_lhs), true);
216230

217-
if(field_sensitive_lhs.id() != ID_symbol)
218-
break;
231+
if(field_sensitive_lhs.id() != ID_symbol)
232+
break;
219233

220-
ssa_rhs = with_expr.new_value();
221-
lhs_mod = to_ssa_expr(field_sensitive_lhs);
234+
ssa_rhs = with_expr.new_value();
235+
lhs_mod = to_ssa_expr(field_sensitive_lhs);
236+
}
222237
}
223-
#endif
224238
return assignment;
225239
}
226240

@@ -363,7 +377,8 @@ void symex_assignt::assign_non_struct_symbol(
363377
// in the future these should be omitted.
364378
auto assignment = shift_indexed_access_to_lhs(
365379
state, assignmentt{lhs, std::move(l2_rhs)}, ns, symex_config.simplify_opt);
366-
assignment = rewrite_with_to_field_symbols(state, std::move(assignment), ns);
380+
assignment = rewrite_with_to_field_symbols<use_update()>(
381+
state, std::move(assignment), ns);
367382

368383
if(symex_config.simplify_opt)
369384
assignment.rhs = simplify_expr(std::move(assignment.rhs), ns);

0 commit comments

Comments
 (0)