Skip to content

Commit bdc007a

Browse files
committed
Rewrite use of *_nonconst()
Construct instructions with the intended value right away rather than fixing them up afterwards.
1 parent 42e3680 commit bdc007a

37 files changed

+430
-483
lines changed

jbmc/src/java_bytecode/remove_exceptions.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -358,17 +358,16 @@ void remove_exceptionst::add_exception_dispatch_sequence(
358358
stack_catch[i][j].second;
359359
if(!stack_catch[i][j].first.empty())
360360
{
361-
// Normal exception handler, make an instanceof check.
362-
goto_programt::targett t_exc = goto_program.insert_after(
363-
instr_it,
364-
goto_programt::make_goto(
365-
new_state_pc, true_exprt(), instr_it->source_location()));
366-
367361
// use instanceof to check that this is the correct handler
368362
struct_tag_typet type(stack_catch[i][j].first);
369363

370364
java_instanceof_exprt check(exc_thrown, type);
371-
t_exc->condition_nonconst() = check;
365+
366+
// Normal exception handler, make an instanceof check.
367+
goto_programt::targett t_exc = goto_program.insert_after(
368+
instr_it,
369+
goto_programt::make_goto(
370+
new_state_pc, check, instr_it->source_location()));
372371

373372
if(remove_added_instanceof)
374373
{

jbmc/src/java_bytecode/replace_java_nondet.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,7 @@ static goto_programt::targett check_and_replace_target(
294294
inserted_expr.set_nullable(
295295
instr_info.get_nullable_type() ==
296296
nondet_instruction_infot::is_nullablet::TRUE);
297-
target_instruction->code_nonconst() =
298-
code_assignt(nondet_var, inserted_expr);
299-
target_instruction->code_nonconst().add_source_location() =
300-
target_instruction->source_location();
297+
target_instruction->assign_rhs_nonconst() = inserted_expr;
301298
}
302299

303300
goto_program.update();

src/analyses/interval_analysis.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ void instrument_intervals(
7777
{
7878
goto_programt::targett t=i_it;
7979
goto_function.body.insert_before_swap(i_it);
80-
*t = goto_programt::make_assumption(conjunction(assertion));
8180
i_it++; // goes to original instruction
82-
t->source_location_nonconst() = i_it->source_location();
81+
*t = goto_programt::make_assumption(
82+
conjunction(assertion), i_it->source_location());
8383
}
8484
}
8585
}

src/ansi-c/goto_check_c.cpp

+33-23
Original file line numberDiff line numberDiff line change
@@ -1724,20 +1724,25 @@ void goto_check_ct::add_guarded_property(
17241724

17251725
if(assertions.insert(std::make_pair(src_expr, guarded_expr)).second)
17261726
{
1727-
auto t = new_code.add(
1728-
enable_assert_to_assume ? goto_programt::make_assumption(
1729-
std::move(guarded_expr), source_location)
1730-
: goto_programt::make_assertion(
1731-
std::move(guarded_expr), source_location));
1732-
17331727
std::string source_expr_string;
17341728
get_language_from_mode(mode)->from_expr(src_expr, source_expr_string, ns);
17351729

1736-
t->source_location_nonconst().set_comment(
1737-
comment + " in " + source_expr_string);
1738-
t->source_location_nonconst().set_property_class(property_class);
1730+
source_locationt annotated_location = source_location;
1731+
annotated_location.set_comment(comment + " in " + source_expr_string);
1732+
annotated_location.set_property_class(property_class);
1733+
1734+
add_all_disable_named_check_pragmas(annotated_location);
17391735

1740-
add_all_disable_named_check_pragmas(t->source_location_nonconst());
1736+
if(enable_assert_to_assume)
1737+
{
1738+
new_code.add(goto_programt::make_assumption(
1739+
std::move(guarded_expr), annotated_location));
1740+
}
1741+
else
1742+
{
1743+
new_code.add(goto_programt::make_assertion(
1744+
std::move(guarded_expr), annotated_location));
1745+
}
17411746
}
17421747
}
17431748

@@ -2120,15 +2125,21 @@ void goto_check_ct::goto_check(
21202125
{
21212126
if(std::find(i.labels.begin(), i.labels.end(), label) != i.labels.end())
21222127
{
2123-
auto t = new_code.add(
2124-
enable_assert_to_assume
2125-
? goto_programt::make_assumption(false_exprt{}, i.source_location())
2126-
: goto_programt::make_assertion(
2127-
false_exprt{}, i.source_location()));
2128-
2129-
t->source_location_nonconst().set_property_class("error label");
2130-
t->source_location_nonconst().set_comment("error label " + label);
2131-
t->source_location_nonconst().set("user-provided", true);
2128+
source_locationt annotated_location = i.source_location();
2129+
annotated_location.set_property_class("error label");
2130+
annotated_location.set_comment("error label " + label);
2131+
annotated_location.set("user-provided", true);
2132+
2133+
if(enable_assert_to_assume)
2134+
{
2135+
new_code.add(
2136+
goto_programt::make_assumption(false_exprt{}, annotated_location));
2137+
}
2138+
else
2139+
{
2140+
new_code.add(
2141+
goto_programt::make_assertion(false_exprt{}, annotated_location));
2142+
}
21322143
}
21332144
}
21342145

@@ -2210,10 +2221,9 @@ void goto_check_ct::goto_check(
22102221
side_effect_expr_nondett(bool_typet(), i.source_location()),
22112222
std::move(address_of_expr),
22122223
lhs);
2213-
goto_programt::targett t =
2214-
new_code.add(goto_programt::make_assignment(
2215-
std::move(lhs), std::move(rhs), i.source_location()));
2216-
t->code_nonconst().add_source_location() = i.source_location();
2224+
new_code.add(goto_programt::make_assignment(
2225+
code_assignt{std::move(lhs), std::move(rhs), i.source_location()},
2226+
i.source_location()));
22172227
}
22182228
}
22192229
}

src/goto-analyzer/taint_analysis.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,13 @@ void taint_analysist::instrument(
179179
where,
180180
ID_get_may,
181181
address_of_exprt(string_constantt(rule.taint))};
182-
goto_programt::targett t =
183-
insert_before.add(goto_programt::make_assertion(
184-
not_exprt(get_may), instruction.source_location()));
185-
t->source_location_nonconst().set_property_class(
182+
source_locationt annotated_location =
183+
instruction.source_location();
184+
annotated_location.set_property_class(
186185
"taint rule " + id2string(rule.id));
187-
t->source_location_nonconst().set_comment(rule.message);
186+
annotated_location.set_comment(rule.message);
187+
insert_before.add(goto_programt::make_assertion(
188+
not_exprt(get_may), annotated_location));
188189
break;
189190
}
190191

src/goto-harness/memory_snapshot_harness_generator.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ void memory_snapshot_harness_generatort::add_init_section(
164164

165165
auto ins_it1 = goto_program.insert_before(
166166
start_it,
167-
goto_programt::make_goto(goto_program.const_cast_target(start_it)));
168-
ins_it1->condition_nonconst() = func_init_done_var;
167+
goto_programt::make_goto(
168+
goto_program.const_cast_target(start_it), func_init_done_var));
169169

170170
auto ins_it2 = goto_program.insert_after(
171171
ins_it1,

src/goto-instrument/accelerate/overflow_instrumenter.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,13 @@ void overflow_instrumentert::add_overflow_checks(
5252

5353
if(t->is_assign())
5454
{
55-
code_assignt &assignment = to_code_assign(t->code_nonconst());
56-
57-
if(assignment.lhs()==overflow_var)
55+
if(t->assign_lhs() == overflow_var)
5856
{
5957
return;
6058
}
6159

62-
add_overflow_checks(t, assignment.lhs(), added);
63-
add_overflow_checks(t, assignment.rhs(), added);
60+
add_overflow_checks(t, t->assign_lhs_nonconst(), added);
61+
add_overflow_checks(t, t->assign_rhs_nonconst(), added);
6462
}
6563

6664
if(t->has_condition())

src/goto-instrument/accelerate/scratch_program.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,10 @@ void scratch_programt::fix_types()
145145
{
146146
if(it->is_assign())
147147
{
148-
code_assignt &code = to_code_assign(it->code_nonconst());
149-
150-
if(code.lhs().type()!=code.rhs().type())
148+
if(it->assign_lhs().type() != it->assign_rhs().type())
151149
{
152-
typecast_exprt typecast(code.rhs(), code.lhs().type());
153-
code.rhs()=typecast;
150+
typecast_exprt typecast{it->assign_rhs(), it->assign_lhs().type()};
151+
it->assign_rhs_nonconst() = typecast;
154152
}
155153
}
156154
else if(it->is_assume() || it->is_assert())

src/goto-instrument/contracts/contracts.cpp

+12-16
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,9 @@ void code_contractst::check_apply_loop_contracts(
278278
{
279279
code_assertt assertion{initial_invariant_val};
280280
assertion.add_source_location() = loop_head_location;
281+
assertion.add_source_location().set_comment(
282+
"Check loop invariant before entry");
281283
converter.goto_convert(assertion, pre_loop_head_instrs, mode);
282-
pre_loop_head_instrs.instructions.back()
283-
.source_location_nonconst()
284-
.set_comment("Check loop invariant before entry");
285284
}
286285

287286
// Insert the first block of pre_loop_head_instrs,
@@ -396,10 +395,9 @@ void code_contractst::check_apply_loop_contracts(
396395
{
397396
code_assertt assertion{invariant};
398397
assertion.add_source_location() = loop_head_location;
398+
assertion.add_source_location().set_comment(
399+
"Check that loop invariant is preserved");
399400
converter.goto_convert(assertion, pre_loop_end_instrs, mode);
400-
pre_loop_end_instrs.instructions.back()
401-
.source_location_nonconst()
402-
.set_comment("Check that loop invariant is preserved");
403401
}
404402

405403
// Generate: assignments to store the multidimensional decreases clause's
@@ -421,11 +419,10 @@ void code_contractst::check_apply_loop_contracts(
421419
generate_lexicographic_less_than_check(
422420
new_decreases_vars, old_decreases_vars)};
423421
monotonic_decreasing_assertion.add_source_location() = loop_head_location;
422+
monotonic_decreasing_assertion.add_source_location().set_comment(
423+
"Check decreases clause on loop iteration");
424424
converter.goto_convert(
425425
monotonic_decreasing_assertion, pre_loop_end_instrs, mode);
426-
pre_loop_end_instrs.instructions.back()
427-
.source_location_nonconst()
428-
.set_comment("Check decreases clause on loop iteration");
429426

430427
// Discard the temporary variables that store decreases clause's value
431428
for(size_t i = 0; i < old_decreases_vars.size(); i++)
@@ -464,12 +461,12 @@ void code_contractst::check_apply_loop_contracts(
464461

465462
goto_programt pre_loop_exit_instrs;
466463
// Assertion to check that step case was checked if we entered the loop.
464+
source_locationt annotated_location = loop_head_location;
465+
annotated_location.set_comment(
466+
"Check that loop instrumentation was not truncated");
467467
pre_loop_exit_instrs.add(goto_programt::make_assertion(
468468
or_exprt{not_exprt{entered_loop}, not_exprt{in_base_case}},
469-
loop_head_location));
470-
pre_loop_exit_instrs.instructions.back()
471-
.source_location_nonconst()
472-
.set_comment("Check that loop instrumentation was not truncated");
469+
annotated_location));
473470
// Instructions to make all the temporaries go dead.
474471
pre_loop_exit_instrs.add(
475472
goto_programt::make_dead(in_base_case, loop_head_location));
@@ -873,9 +870,8 @@ void code_contractst::apply_function_contract(
873870
goto_programt::make_decl(to_symbol_expr(call_ret), loc));
874871

875872
side_effect_expr_nondett expr(type, location);
876-
auto target = havoc_instructions.add(
877-
goto_programt::make_assignment(call_ret, expr, loc));
878-
target->code_nonconst().add_source_location() = loc;
873+
havoc_instructions.add(goto_programt::make_assignment(
874+
code_assignt{call_ret, std::move(expr), loc}, loc));
879875
}
880876

881877
// Insert havoc instructions immediately before the call site.

src/goto-instrument/contracts/havoc_assigns_clause_targets.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ void havoc_assigns_clause_targetst::havoc_if_valid(
8383
{
8484
// OTHER __CPROVER_havoc_object(target_snapshot_var)
8585
codet code(ID_havoc_object, {car.lower_bound_var()});
86-
const auto &inst =
87-
dest.add(goto_programt::make_other(code, source_location));
88-
inst->source_location_nonconst().set_comment(tracking_comment);
86+
source_locationt annotated_location = source_location;
87+
annotated_location.set_comment(tracking_comment);
88+
dest.add(goto_programt::make_other(code, annotated_location));
8989
}
9090
else if(car.havoc_method == car_havoc_methodt::HAVOC_SLICE)
9191
{
@@ -106,12 +106,13 @@ void havoc_assigns_clause_targetst::havoc_if_valid(
106106
// ASSIGN *(target.type() *)__car_lb = nondet(car.target().type())
107107
const auto &target_type = car.target().type();
108108
side_effect_expr_nondett nondet(target_type, source_location);
109-
const auto &inst = dest.add(goto_programt::make_assignment(
109+
source_locationt annotated_location = source_location;
110+
annotated_location.set_comment(tracking_comment);
111+
dest.add(goto_programt::make_assignment(
110112
dereference_exprt{typecast_exprt::conditional_cast(
111113
car.lower_bound_var(), pointer_type(target_type))},
112114
nondet,
113-
source_location));
114-
inst->source_location_nonconst().set_comment(tracking_comment);
115+
annotated_location));
115116
}
116117
else
117118
{
@@ -154,10 +155,11 @@ void havoc_assigns_clause_targetst::havoc_static_local(
154155

155156
const auto &target_type = car.target().type();
156157
side_effect_expr_nondett nondet(target_type, source_location);
157-
const auto &inst = dest.add(
158-
goto_programt::make_assignment(car.target(), nondet, source_location));
159-
inst->source_location_nonconst().set_comment(tracking_comment);
160-
add_propagate_static_local_pragma(inst->source_location_nonconst());
158+
source_locationt annotated_location = source_location;
159+
annotated_location.set_comment(tracking_comment);
160+
add_propagate_static_local_pragma(annotated_location);
161+
dest.add(
162+
goto_programt::make_assignment(car.target(), nondet, annotated_location));
161163

162164
dest.destructive_append(skip_program);
163165

src/goto-instrument/cover_instrument.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,14 @@ class cover_instrumenter_baset
8181
const assertion_factoryt &) const = 0;
8282

8383
void initialize_source_location(
84-
goto_programt::targett t,
84+
source_locationt &source_location,
8585
const std::string &comment,
8686
const irep_idt &function_id) const
8787
{
88-
t->source_location_nonconst().set_comment(comment);
89-
t->source_location_nonconst().set(
90-
ID_coverage_criterion, coverage_criterion);
91-
t->source_location_nonconst().set_property_class(property_class);
92-
t->source_location_nonconst().set_function(function_id);
88+
source_location.set_comment(comment);
89+
source_location.set(ID_coverage_criterion, coverage_criterion);
90+
source_location.set_property_class(property_class);
91+
source_location.set_function(function_id);
9392
}
9493

9594
bool is_non_cover_assertion(goto_programt::const_targett t) const

src/goto-instrument/cover_instrument_assume.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ void cover_assume_instrumentert::instrument(
2323
{
2424
if(i_it->is_assume())
2525
{
26-
const auto location = i_it->source_location();
2726
const auto assume_condition =
2827
expr2c(i_it->condition(), namespacet{symbol_tablet()});
2928
const auto comment_before =
3029
"assert(false) before assume(" + assume_condition + ")";
3130
const auto comment_after =
3231
"assert(false) after assume(" + assume_condition + ")";
3332

33+
source_locationt location = i_it->source_location();
34+
initialize_source_location(location, comment_before, function_id);
3435
const auto assert_before = make_assertion(false_exprt{}, location);
35-
goto_programt::targett t = goto_program.insert_before(i_it, assert_before);
36-
initialize_source_location(t, comment_before, function_id);
36+
goto_program.insert_before(i_it, assert_before);
3737

38+
initialize_source_location(location, comment_after, function_id);
3839
const auto assert_after = make_assertion(false_exprt{}, location);
39-
t = goto_program.insert_after(i_it, assert_after);
40-
initialize_source_location(t, comment_after, function_id);
40+
goto_program.insert_after(i_it, assert_after);
4141
}
4242
// Otherwise, skip existing assertions.
4343
else if(i_it->is_assert())

src/goto-instrument/cover_instrument_branch.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ void cover_branch_instrumentert::instrument(
4040
std::string comment = "entry point";
4141

4242
source_locationt source_location = i_it->source_location();
43-
44-
goto_programt::targett t = goto_program.insert_before(
43+
initialize_source_location(source_location, comment, function_id);
44+
goto_program.insert_before(
4545
i_it, make_assertion(false_exprt(), source_location));
46-
initialize_source_location(t, comment, function_id);
4746
}
4847

4948
if(is_conditional_goto)
@@ -57,12 +56,12 @@ void cover_branch_instrumentert::instrument(
5756
source_locationt source_location = i_it->source_location();
5857

5958
goto_program.insert_before_swap(i_it);
59+
initialize_source_location(source_location, true_comment, function_id);
6060
*i_it = make_assertion(not_exprt(guard), source_location);
61-
initialize_source_location(i_it, true_comment, function_id);
6261

6362
goto_program.insert_before_swap(i_it);
63+
initialize_source_location(source_location, false_comment, function_id);
6464
*i_it = make_assertion(guard, source_location);
65-
initialize_source_location(i_it, false_comment, function_id);
6665

6766
std::advance(i_it, 2);
6867
}

0 commit comments

Comments
 (0)