Skip to content

Commit d91a7b9

Browse files
authored
Merge pull request #3932 from tautschnig/deprecation-switch
Avoid default code_switch(_case)t's default constructor [blocks: #3800]
2 parents 1ab9364 + dac0f59 commit d91a7b9

File tree

3 files changed

+26
-32
lines changed

3 files changed

+26
-32
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,9 +1929,6 @@ code_switcht java_bytecode_convert_methodt::convert_switch(
19291929
const source_locationt &location)
19301930
{
19311931
// we turn into switch-case
1932-
code_switcht code_switch;
1933-
code_switch.add_source_location() = location;
1934-
code_switch.value() = op[0];
19351932
code_blockt code_block;
19361933
code_block.add_source_location() = location;
19371934

@@ -1941,36 +1938,40 @@ code_switcht java_bytecode_convert_methodt::convert_switch(
19411938
{
19421939
if(is_label)
19431940
{
1944-
code_switch_caset code_case;
1945-
code_case.add_source_location() = location;
1946-
19471941
const mp_integer number = numeric_cast_v<mp_integer>(*a_it);
19481942
// The switch case does not contain any code, it just branches via a GOTO
19491943
// to the jump target of the tableswitch/lookupswitch case at
19501944
// hand. Therefore we consider this code to belong to the source bytecode
19511945
// instruction and not the target instruction.
19521946
const method_offsett label_number =
19531947
numeric_cast_v<method_offsett>(number);
1954-
code_case.code() = code_gotot(label(std::to_string(label_number)));
1955-
code_case.code().add_source_location() = location;
1948+
code_gotot code(label(std::to_string(label_number)));
1949+
code.add_source_location() = location;
19561950

19571951
if(a_it == args.begin())
1952+
{
1953+
code_switch_caset code_case(nil_exprt(), std::move(code));
1954+
code_case.add_source_location() = location;
19581955
code_case.set_default();
1956+
1957+
code_block.add(std::move(code_case));
1958+
}
19591959
else
19601960
{
1961-
auto prev = a_it;
1962-
prev--;
1963-
code_case.case_op() = *prev;
1964-
if(code_case.case_op().type() != op[0].type())
1965-
code_case.case_op().make_typecast(op[0].type());
1966-
code_case.case_op().add_source_location() = location;
1967-
}
1961+
exprt case_op =
1962+
typecast_exprt::conditional_cast(*std::prev(a_it), op[0].type());
1963+
case_op.add_source_location() = location;
1964+
1965+
code_switch_caset code_case(std::move(case_op), std::move(code));
1966+
code_case.add_source_location() = location;
19681967

1969-
code_block.add(code_case);
1968+
code_block.add(std::move(code_case));
1969+
}
19701970
}
19711971
}
19721972

1973-
code_switch.body() = code_block;
1973+
code_switcht code_switch(op[0], std::move(code_block));
1974+
code_switch.add_source_location() = location;
19741975
return code_switch;
19751976
}
19761977

jbmc/src/java_bytecode/java_entry_point.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,6 @@ exprt::operandst java_build_arguments(
358358
INVARIANT(!is_this, "We cannot have different types for `this` here");
359359
// create a non-deterministic switch between all possible values for the
360360
// type of the parameter.
361-
code_switcht code_switch;
362361

363362
// the idea is to get a new symbol for the parameter value `tmp`
364363

@@ -385,8 +384,10 @@ exprt::operandst java_build_arguments(
385384
symbol_table);
386385
main_arguments[param_number] = result_symbol.symbol_expr();
387386

388-
std::vector<codet> cases(alternatives.size());
389-
const auto initialize_parameter = [&](const struct_tag_typet &type) {
387+
std::vector<codet> cases;
388+
cases.reserve(alternatives.size());
389+
for(const auto &type : alternatives)
390+
{
390391
code_blockt init_code_for_type;
391392
exprt init_expr_for_parameter = object_factory(
392393
java_reference_type(type),
@@ -402,14 +403,8 @@ exprt::operandst java_build_arguments(
402403
code_assignt(
403404
result_symbol.symbol_expr(),
404405
typecast_exprt(init_expr_for_parameter, p.type())));
405-
return init_code_for_type;
406-
};
407-
408-
std::transform(
409-
alternatives.begin(),
410-
alternatives.end(),
411-
cases.begin(),
412-
initialize_parameter);
406+
cases.push_back(init_code_for_type);
407+
}
413408

414409
init_code.add(
415410
generate_nondet_switch(

src/goto-instrument/goto_program2code.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -894,9 +894,7 @@ goto_programt::const_targett goto_program2codet::convert_goto_switch(
894894
return convert_goto_if(target, upper_bound, dest);
895895

896896
// maybe, let's try some more
897-
code_switcht s;
898-
s.value()=to_equal_expr(eq_cand).lhs();
899-
s.body()=code_blockt();
897+
code_switcht s(to_equal_expr(eq_cand).lhs(), code_blockt());
900898

901899
copy_source_location(target, s);
902900

@@ -962,7 +960,7 @@ goto_programt::const_targett goto_program2codet::convert_goto_switch(
962960
it!=cases.end();
963961
++it)
964962
{
965-
code_switch_caset csc;
963+
code_switch_caset csc(nil_exprt{}, code_blockt{});
966964
// branch condition is nil_exprt for default case;
967965
if(it->value.is_nil())
968966
csc.set_default();

0 commit comments

Comments
 (0)