Skip to content

Commit 99afbae

Browse files
committed
protect instructiont::code
This PR is a step towards removing direct access to instructiont::code, with the long-term goal of removing the use of codet expressions in goto programs.
1 parent 03eecc2 commit 99afbae

File tree

71 files changed

+273
-255
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+273
-255
lines changed

jbmc/src/java_bytecode/convert_java_nondet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static std::pair<goto_programt::targett, bool> insert_nondet_init_code(
9393
// We only expect to find nondets in the rhs of an assignments, and in return
9494
// statements if remove_returns has not been run, but we do a more general
9595
// check on all operands in case this changes
96-
for(exprt &op : target->code.operands())
96+
for(exprt &op : target->code_nonconst().operands())
9797
{
9898
if(!is_nondet_pointer(op))
9999
{

jbmc/src/java_bytecode/remove_exceptions.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ bool remove_exceptionst::function_or_callees_may_throw(
200200

201201
if(instruction.is_function_call())
202202
{
203-
const exprt &function_expr =
204-
to_code_function_call(instruction.code).function();
203+
const exprt &function_expr = instruction.get_function_call().function();
205204
DATA_INVARIANT(
206205
function_expr.id()==ID_symbol,
207206
"identifier expected to be a symbol");
@@ -235,8 +234,8 @@ void remove_exceptionst::instrument_exception_handler(
235234
if(may_catch)
236235
{
237236
// retrieve the exception variable
238-
const exprt &thrown_exception_local=
239-
to_code_landingpad(instr_it->code).catch_expr();
237+
const exprt &thrown_exception_local =
238+
to_code_landingpad(instr_it->get_code()).catch_expr();
240239

241240
const symbol_exprt thrown_global_symbol=
242241
get_inflight_exception_global();
@@ -408,8 +407,8 @@ bool remove_exceptionst::instrument_throw(
408407
{
409408
PRECONDITION(instr_it->type==THROW);
410409

411-
const exprt &exc_expr=
412-
uncaught_exceptions_domaint::get_exception_symbol(instr_it->code);
410+
const exprt &exc_expr =
411+
uncaught_exceptions_domaint::get_exception_symbol(instr_it->get_code());
413412

414413
add_exception_dispatch_sequence(
415414
function_identifier, goto_program, instr_it, stack_catch, locals);
@@ -424,7 +423,7 @@ bool remove_exceptionst::instrument_throw(
424423
typecast_exprt(exc_expr, exc_thrown.type()));
425424
// now turn the `throw' into `assignment'
426425
instr_it->type=ASSIGN;
427-
instr_it->code=assignment;
426+
instr_it->code_nonconst() = assignment;
428427

429428
return true;
430429
}
@@ -518,7 +517,7 @@ void remove_exceptionst::instrument_exceptions(
518517
// Is it a handler push/pop or catch landing-pad?
519518
else if(instr_it->type==CATCH)
520519
{
521-
const irep_idt &statement=instr_it->code.get_statement();
520+
const irep_idt &statement = instr_it->get_code().get_statement();
522521
// Is it an exception landing pad (start of a catch block)?
523522
if(statement==ID_exception_landingpad)
524523
{
@@ -558,8 +557,8 @@ void remove_exceptionst::instrument_exceptions(
558557
stack_catch.back();
559558

560559
// copy targets
561-
const code_push_catcht::exception_listt &exception_list=
562-
to_code_push_catch(instr_it->code).exception_list();
560+
const code_push_catcht::exception_listt &exception_list =
561+
to_code_push_catch(instr_it->get_code()).exception_list();
563562

564563
// The target list can be empty if `finish_catch_push_targets` found that
565564
// the targets were unreachable (in which case no exception can truly

jbmc/src/java_bytecode/remove_instanceof.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,9 @@ bool remove_instanceoft::lower_instanceof(
244244
goto_programt &goto_program,
245245
goto_programt::targett target)
246246
{
247-
if(target->is_target() &&
248-
(contains_instanceof(target->code) || contains_instanceof(target->guard)))
247+
if(
248+
target->is_target() && (contains_instanceof(target->get_code()) ||
249+
contains_instanceof(target->guard)))
249250
{
250251
// If this is a branch target, add a skip beforehand so we can splice new
251252
// GOTO programs before the target instruction without inserting into the
@@ -257,7 +258,7 @@ bool remove_instanceoft::lower_instanceof(
257258
}
258259

259260
return lower_instanceof(
260-
function_identifier, target->code, goto_program, target) |
261+
function_identifier, target->code_nonconst(), goto_program, target) |
261262
lower_instanceof(
262263
function_identifier, target->guard, goto_program, target);
263264
}

jbmc/src/java_bytecode/replace_java_nondet.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ is_nondet_returning_object(const code_function_callt &function_call)
8888
static nondet_instruction_infot
8989
get_nondet_instruction_info(const goto_programt::const_targett &instr)
9090
{
91-
if(!(instr->is_function_call() && instr->code.id() == ID_code))
91+
if(!(instr->is_function_call() && instr->get_code().id() == ID_code))
9292
{
9393
return nondet_instruction_infot();
9494
}
95-
const auto &code = instr->code;
95+
const auto &code = instr->get_code();
9696
INVARIANT(
9797
code.get_statement() == ID_function_call,
9898
"function_call should have ID_function_call");
@@ -209,14 +209,13 @@ static goto_programt::targett check_and_replace_target(
209209
// If we haven't removed returns yet, our function call will have a variable
210210
// on its left hand side.
211211
const bool remove_returns_not_run =
212-
to_code_function_call(target->code).lhs().is_not_nil();
212+
target->get_function_call().lhs().is_not_nil();
213213

214214
irep_idt return_identifier;
215215
if(remove_returns_not_run)
216216
{
217217
return_identifier =
218-
to_symbol_expr(to_code_function_call(target->code).lhs())
219-
.get_identifier();
218+
to_symbol_expr(target->get_function_call().lhs()).get_identifier();
220219
}
221220
else
222221
{
@@ -282,8 +281,8 @@ static goto_programt::targett check_and_replace_target(
282281
inserted_expr.set_nullable(
283282
instr_info.get_nullable_type() ==
284283
nondet_instruction_infot::is_nullablet::TRUE);
285-
target_instruction->code = code_returnt(inserted_expr);
286-
target_instruction->code.add_source_location() =
284+
target_instruction->code_nonconst() = code_returnt(inserted_expr);
285+
target_instruction->code_nonconst().add_source_location() =
287286
target_instruction->source_location;
288287
}
289288
else if(target_instruction->is_assign())
@@ -296,8 +295,9 @@ static goto_programt::targett check_and_replace_target(
296295
inserted_expr.set_nullable(
297296
instr_info.get_nullable_type() ==
298297
nondet_instruction_infot::is_nullablet::TRUE);
299-
target_instruction->code = code_assignt(nondet_var, inserted_expr);
300-
target_instruction->code.add_source_location() =
298+
target_instruction->code_nonconst() =
299+
code_assignt(nondet_var, inserted_expr);
300+
target_instruction->code_nonconst().add_source_location() =
301301
target_instruction->source_location;
302302
}
303303

jbmc/unit/java_bytecode/goto-programs/remove_virtual_functions_without_fallback.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static bool is_call_to(
6363
{
6464
if(!inst->is_function_call())
6565
return false;
66-
const exprt &callee_expr = to_code_function_call(inst->code).function();
66+
const exprt &callee_expr = inst->get_function_call().function();
6767
if(callee_expr.id() != ID_symbol)
6868
return false;
6969
return to_symbol_expr(callee_expr).get_identifier() == callee;

jbmc/unit/java_bytecode/java_bytecode_instrument/virtual_call_null_checks.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static bool is_expected_virtualmethod_call(
2222
{
2323
if(instruction.type != FUNCTION_CALL)
2424
return false;
25-
const auto &virtual_call = to_code_function_call(instruction.code);
25+
const auto &virtual_call = instruction.get_function_call();
2626
const auto &called_function = virtual_call.function();
2727
if(!can_cast_expr<class_method_descriptor_exprt>(called_function))
2828
return false;
@@ -81,9 +81,10 @@ SCENARIO(
8181
instrend = main_function.body.instructions.end();
8282
instrit != instrend; ++instrit)
8383
{
84-
for(auto it = instrit->code.depth_begin(),
85-
itend = instrit->code.depth_end();
86-
it != itend; ++it)
84+
for(auto it = instrit->get_code().depth_begin(),
85+
itend = instrit->get_code().depth_end();
86+
it != itend;
87+
++it)
8788
{
8889
if(it->id() == ID_dereference)
8990
{

jbmc/unit/java_bytecode/java_replace_nondet/replace_nondet.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void validate_nondet_method_removed(
4141
// Check that our NONDET(<type>) exists on a rhs somewhere.
4242
if(inst.is_assign())
4343
{
44-
const code_assignt &assignment = to_code_assign(inst.code);
44+
const code_assignt &assignment = inst.get_assign();
4545
if(assignment.rhs().id() == ID_side_effect)
4646
{
4747
const side_effect_exprt &see = to_side_effect_expr(assignment.rhs());
@@ -68,8 +68,7 @@ void validate_nondet_method_removed(
6868
// And check to see that our nondet method call has been removed.
6969
if(inst.is_function_call())
7070
{
71-
const code_function_callt &function_call =
72-
to_code_function_call(inst.code);
71+
const code_function_callt &function_call = inst.get_function_call();
7372

7473
// Small check to make sure the instruction is a symbol.
7574
if(function_call.function().id() != ID_symbol)
@@ -101,9 +100,8 @@ void validate_nondets_converted(
101100
// Check that our NONDET(<type>) exists on a rhs somewhere.
102101
exprt target_expression =
103102
(inst.is_assign()
104-
? to_code_assign(inst.code).rhs()
105-
: inst.is_return() ? to_code_return(inst.code).return_value()
106-
: inst.code);
103+
? inst.get_assign().rhs()
104+
: inst.is_return() ? inst.return_value() : inst.get_code());
107105

108106
if(
109107
const auto side_effect =

jbmc/unit/java_bytecode/java_virtual_functions/virtual_functions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void check_function_call(
3232
for(const auto &target : targets)
3333
{
3434
REQUIRE(target->type == goto_program_instruction_typet::FUNCTION_CALL);
35-
const code_function_callt &call = to_code_function_call(target->code);
35+
const code_function_callt &call = target->get_function_call();
3636
REQUIRE(call.function().id() == ID_symbol);
3737
REQUIRE(to_symbol_expr(call.function()).get_identifier() == function_name);
3838
}

src/analyses/cfg_dominators.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ inline void dominators_pretty_print_node(
246246
const goto_programt::targett& target,
247247
std::ostream& out)
248248
{
249-
out << target->code.pretty();
249+
out << target->get_code().pretty();
250250
}
251251

252252
/// Print the result of the dominator computation

src/analyses/constant_propagator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ void constant_propagator_ait::replace(
758758
if(d.is_bottom())
759759
continue;
760760

761-
replace_types_rec(d.values.replace_const, it->code);
761+
replace_types_rec(d.values.replace_const, it->code_nonconst());
762762

763763
if(it->is_goto() || it->is_assume() || it->is_assert())
764764
{

src/analyses/goto_check.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,7 @@ void goto_checkt::collect_allocations(
302302
if(!instruction.is_function_call())
303303
continue;
304304

305-
const code_function_callt &call=
306-
to_code_function_call(instruction.code);
305+
const code_function_callt &call = instruction.get_function_call();
307306
if(call.function().id()!=ID_symbol ||
308307
to_symbol_expr(call.function()).get_identifier()!=
309308
CPROVER_PREFIX "allocated_memory")
@@ -1973,7 +1972,7 @@ void goto_checkt::goto_check(
19731972
}
19741973
else if(i.is_assign())
19751974
{
1976-
const code_assignt &code_assign=to_code_assign(i.code);
1975+
const code_assignt &code_assign = i.get_assign();
19771976

19781977
// Reset the no_enum_check with the flag reset for exception
19791978
// safety
@@ -1991,16 +1990,19 @@ void goto_checkt::goto_check(
19911990
return expr.id() == ID_r_ok || expr.id() == ID_w_ok;
19921991
}))
19931992
{
1994-
exprt &rhs = to_code_assign(i.code).rhs();
1993+
const exprt &rhs = i.get_assign().rhs();
19951994
auto rw_ok_cond = rw_ok_check(rhs);
19961995
if(rw_ok_cond.has_value())
1997-
rhs = *rw_ok_cond;
1996+
{
1997+
auto new_assign = i.get_assign(); // copy
1998+
new_assign.rhs() = *rw_ok_cond;
1999+
i.set_assign(new_assign);
2000+
}
19982001
}
19992002
}
20002003
else if(i.is_function_call())
20012004
{
2002-
const code_function_callt &code_function_call=
2003-
to_code_function_call(i.code);
2005+
const code_function_callt &code_function_call = i.get_function_call();
20042006

20052007
// for Java, need to check whether 'this' is null
20062008
// on non-static method invocations
@@ -2055,13 +2057,14 @@ void goto_checkt::goto_check(
20552057
}
20562058
else if(i.is_throw())
20572059
{
2058-
if(i.code.get_statement()==ID_expression &&
2059-
i.code.operands().size()==1 &&
2060-
i.code.op0().operands().size()==1)
2060+
if(
2061+
i.get_code().get_statement() == ID_expression &&
2062+
i.get_code().operands().size() == 1 &&
2063+
i.get_code().op0().operands().size() == 1)
20612064
{
20622065
// must not throw NULL
20632066

2064-
exprt pointer = to_unary_expr(i.code.op0()).op();
2067+
exprt pointer = to_unary_expr(i.get_code().op0()).op();
20652068

20662069
const notequal_exprt not_eq_null(
20672070
pointer, null_pointer_exprt(to_pointer_type(pointer.type())));
@@ -2102,8 +2105,7 @@ void goto_checkt::goto_check(
21022105
{
21032106
if(enable_pointer_check || enable_pointer_primitive_check)
21042107
{
2105-
assert(i.code.operands().size()==1);
2106-
const symbol_exprt &variable=to_symbol_expr(i.code.op0());
2108+
const symbol_exprt &variable = i.dead_symbol();
21072109

21082110
// is it dirty?
21092111
if(local_bitvector_analysis->dirty(variable))
@@ -2119,7 +2121,7 @@ void goto_checkt::goto_check(
21192121
goto_programt::targett t =
21202122
new_code.add(goto_programt::make_assignment(
21212123
std::move(lhs), std::move(rhs), i.source_location));
2122-
t->code.add_source_location()=i.source_location;
2124+
t->code_nonconst().add_source_location() = i.source_location;
21232125
}
21242126
}
21252127
}

src/analyses/invariant_set_domain.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,22 @@ void invariant_set_domaint::transform(
5656

5757
case ASSIGN:
5858
{
59-
const code_assignt &assignment=to_code_assign(from_l->code);
59+
const code_assignt &assignment = from_l->get_assign();
6060
invariant_set.assignment(assignment.lhs(), assignment.rhs());
6161
}
6262
break;
6363

6464
case OTHER:
6565
if(from_l->get_other().is_not_nil())
66-
invariant_set.apply_code(from_l->code);
66+
invariant_set.apply_code(from_l->get_code());
6767
break;
6868

6969
case DECL:
70-
invariant_set.apply_code(from_l->code);
70+
invariant_set.apply_code(from_l->get_code());
7171
break;
7272

7373
case FUNCTION_CALL:
74-
invariant_set.apply_code(from_l->code);
74+
invariant_set.apply_code(from_l->get_code());
7575
break;
7676

7777
case START_THREAD:

src/analyses/local_bitvector_analysis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ void local_bitvector_analysist::build()
275275
{
276276
case ASSIGN:
277277
{
278-
const code_assignt &code_assign = to_code_assign(instruction.code);
278+
const code_assignt &code_assign = instruction.get_assign();
279279
assign_lhs(
280280
code_assign.lhs(), code_assign.rhs(), loc_info_src, loc_info_dest);
281281
break;
@@ -300,7 +300,7 @@ void local_bitvector_analysist::build()
300300
case FUNCTION_CALL:
301301
{
302302
const code_function_callt &code_function_call =
303-
to_code_function_call(instruction.code);
303+
instruction.get_function_call();
304304
if(code_function_call.lhs().is_not_nil())
305305
assign_lhs(
306306
code_function_call.lhs(), nil_exprt(), loc_info_src, loc_info_dest);

src/analyses/uncaught_exceptions_analysis.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void uncaught_exceptions_domaint::transform(
6666
{
6767
case THROW:
6868
{
69-
const exprt &exc_symbol=get_exception_symbol(instruction.code);
69+
const exprt &exc_symbol = get_exception_symbol(instruction.get_code());
7070
// retrieve the static type of the thrown exception
7171
const irep_idt &type_id=get_exception_type(exc_symbol.type());
7272
join(type_id);
@@ -79,15 +79,15 @@ void uncaught_exceptions_domaint::transform(
7979
}
8080
case CATCH:
8181
{
82-
if(!instruction.code.has_operands())
82+
if(!instruction.get_code().has_operands())
8383
{
8484
if(!instruction.targets.empty()) // push
8585
{
8686
std::set<irep_idt> caught;
8787
stack_caught.push_back(caught);
8888
std::set<irep_idt> &last_caught=stack_caught.back();
89-
const irept::subt &exception_list=
90-
instruction.code.find(ID_exception_list).get_sub();
89+
const irept::subt &exception_list =
90+
instruction.get_code().find(ID_exception_list).get_sub();
9191

9292
for(const auto &exc : exception_list)
9393
{
@@ -114,8 +114,7 @@ void uncaught_exceptions_domaint::transform(
114114
}
115115
case FUNCTION_CALL:
116116
{
117-
const exprt &function_expr=
118-
to_code_function_call(instruction.code).function();
117+
const exprt &function_expr = instruction.get_function_call().function();
119118
DATA_INVARIANT(
120119
function_expr.id()==ID_symbol,
121120
"identifier expected to be a symbol");

0 commit comments

Comments
 (0)