Skip to content

Commit d5cdcd8

Browse files
committed
Use constructors to construct objects
This is some drive-by cleanup resulting from reviewing #3067. Instead of incrementally constructing objects do RAII.
1 parent 273f375 commit d5cdcd8

27 files changed

+119
-162
lines changed

jbmc/src/java_bytecode/java_bytecode_concurrency_instrumentation.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,7 @@ static codet get_monitor_call(
122122
return code_skipt();
123123

124124
// Otherwise we create a function call
125-
code_function_callt call;
126-
call.function() = symbol_exprt(symbol, it->second.type);
127-
call.lhs().make_nil();
128-
call.arguments().push_back(object);
129-
return call;
125+
return code_function_callt(symbol_exprt(symbol, it->second.type), {object});
130126
}
131127

132128
/// Introduces a monitorexit before every return recursively.
@@ -288,11 +284,15 @@ static void instrument_start_thread(
288284

289285
// Create global variable __CPROVER__next_thread_id. Used as a counter
290286
// in-order to to assign ids to new threads.
291-
const symbolt &next_symbol =
292-
add_or_get_symbol(
293-
symbol_table, next_thread_id, next_thread_id,
294-
java_int_type(),
295-
from_integer(0, java_int_type()), false, true);
287+
const symbol_exprt next_symbol_expr = add_or_get_symbol(
288+
symbol_table,
289+
next_thread_id,
290+
next_thread_id,
291+
java_int_type(),
292+
from_integer(0, java_int_type()),
293+
false,
294+
true)
295+
.symbol_expr();
296296

297297
// Create thread-local variable __CPROVER__thread_id. Holds the id of
298298
// the thread.
@@ -320,14 +320,11 @@ static void instrument_start_thread(
320320
codet tmp_a(ID_start_thread);
321321
tmp_a.set(ID_destination, lbl1);
322322
code_gotot tmp_b(lbl2);
323-
code_labelt tmp_c(lbl1);
324-
tmp_c.op0() = codet(ID_skip);
323+
const code_labelt tmp_c(lbl1, code_skipt());
325324

326-
exprt plus(ID_plus, java_int_type());
327-
plus.copy_to_operands(next_symbol.symbol_expr());
328-
plus.copy_to_operands(from_integer(1, java_int_type()));
329-
code_assignt tmp_d(next_symbol.symbol_expr(), plus);
330-
code_assignt tmp_e(current_symbol.symbol_expr(), next_symbol.symbol_expr());
325+
const plus_exprt plus(next_symbol_expr, from_integer(1, java_int_type()));
326+
const code_assignt tmp_d(next_symbol_expr, plus);
327+
const code_assignt tmp_e(current_symbol.symbol_expr(), next_symbol_expr);
331328

332329
code_blockt block;
333330
block.add(tmp_a);
@@ -368,8 +365,7 @@ static void instrument_end_thread(
368365
// A: codet(id=ID_end_thread)
369366
// B: codet(id=ID_label,label= __CPROVER_THREAD_EXIT_<ID>)
370367
codet tmp_a(ID_end_thread);
371-
code_labelt tmp_b(lbl2);
372-
tmp_b.op0() = codet(ID_skip);
368+
const code_labelt tmp_b(lbl2, code_skipt());
373369

374370
code_blockt block;
375371
block.add(tmp_a);

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -973,8 +973,7 @@ codet java_bytecode_convert_methodt::get_clinit_call(
973973
return code_skipt();
974974
else
975975
{
976-
code_function_callt ret;
977-
ret.function() = findit->second.symbol_expr();
976+
const code_function_callt ret(findit->second.symbol_expr());
978977
if(needed_lazy_methods)
979978
needed_lazy_methods->add_needed_method(findit->second.name);
980979
return ret;
@@ -2004,10 +2003,7 @@ codet java_bytecode_convert_methodt::convert_monitorenterexit(
20042003
java_method_typet type(
20052004
{java_method_typet::parametert(java_reference_type(void_typet()))},
20062005
void_typet());
2007-
code_function_callt call;
2008-
call.function() = symbol_exprt(descriptor, type);
2009-
call.lhs().make_nil();
2010-
call.arguments().push_back(op[0]);
2006+
code_function_callt call(symbol_exprt(descriptor, type), {op[0]});
20112007
call.add_source_location() = source_location;
20122008
if(needed_lazy_methods && symbol_table.has_symbol(descriptor))
20132009
needed_lazy_methods->add_needed_method(descriptor);
@@ -2737,13 +2733,11 @@ codet java_bytecode_convert_methodt::convert_iinc(
27372733
bytecode_write_typet::VARIABLE,
27382734
to_symbol_expr(locvar).get_identifier());
27392735

2740-
code_assignt code_assign;
2741-
code_assign.lhs() = variable(arg0, 'i', address, NO_CAST);
2742-
exprt arg1_int_type = arg1;
2743-
if(arg1.type() != java_int_type())
2744-
arg1_int_type.make_typecast(java_int_type());
2745-
code_assign.rhs() =
2746-
plus_exprt(variable(arg0, 'i', address, CAST_AS_NEEDED), arg1_int_type);
2736+
const exprt arg1_int_type =
2737+
typecast_exprt::conditional_cast(arg1, java_int_type());
2738+
const code_assignt code_assign(
2739+
variable(arg0, 'i', address, NO_CAST),
2740+
plus_exprt(variable(arg0, 'i', address, CAST_AS_NEEDED), arg1_int_type));
27472741
block.copy_to_operands(code_assign);
27482742
return block;
27492743
}

jbmc/src/java_bytecode/java_entry_point.cpp

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -169,33 +169,26 @@ static void java_static_lifetime_init(
169169
// Argument order is: name, isAnnotation, isArray, isInterface,
170170
// isSynthetic, isLocalClass, isMemberClass, isEnum
171171

172-
code_function_callt initializer_call;
173-
initializer_call.function() = class_literal_init_method->symbol_expr();
174-
175-
code_function_callt::argumentst &args = initializer_call.arguments();
176-
177-
// this:
178-
args.push_back(address_of_exprt(sym.symbol_expr()));
179-
// name:
180-
args.push_back(address_of_exprt(class_name_literal));
181-
// isAnnotation:
182-
args.push_back(
183-
constant_bool(class_symbol.type.get_bool(ID_is_annotation)));
184-
// isArray:
185-
args.push_back(constant_bool(class_is_array));
186-
// isInterface:
187-
args.push_back(
188-
constant_bool(class_symbol.type.get_bool(ID_interface)));
189-
// isSynthetic:
190-
args.push_back(
191-
constant_bool(class_symbol.type.get_bool(ID_synthetic)));
192-
// isLocalClass:
193-
args.push_back(nondet_bool);
194-
// isMemberClass:
195-
args.push_back(nondet_bool);
196-
// isEnum:
197-
args.push_back(
198-
constant_bool(class_symbol.type.get_bool(ID_enumeration)));
172+
code_function_callt initializer_call(
173+
class_literal_init_method->symbol_expr(),
174+
{// this:
175+
address_of_exprt(sym.symbol_expr()),
176+
// name:
177+
address_of_exprt(class_name_literal),
178+
// isAnnotation:
179+
constant_bool(class_symbol.type.get_bool(ID_is_annotation)),
180+
// isArray:
181+
constant_bool(class_is_array),
182+
// isInterface:
183+
constant_bool(class_symbol.type.get_bool(ID_interface)),
184+
// isSynthetic:
185+
constant_bool(class_symbol.type.get_bool(ID_synthetic)),
186+
// isLocalClass:
187+
nondet_bool,
188+
// isMemberClass:
189+
nondet_bool,
190+
// isEnum:
191+
constant_bool(class_symbol.type.get_bool(ID_enumeration))});
199192

200193
// First initialize the object as prior to a constructor:
201194
namespacet ns(symbol_table);
@@ -676,10 +669,8 @@ bool generate_java_start_function(
676669
return true; // give up with error
677670
}
678671

679-
code_function_callt call_init;
680-
call_init.lhs().make_nil();
672+
code_function_callt call_init(init_it->second.symbol_expr());
681673
call_init.add_source_location()=symbol.location;
682-
call_init.function()=init_it->second.symbol_expr();
683674

684675
init_code.move_to_operands(call_init);
685676
}
@@ -689,15 +680,13 @@ bool generate_java_start_function(
689680
// where return is a new variable
690681
// and arg1 ... argn are constructed below as well
691682

692-
code_function_callt call_main;
693-
694683
source_locationt loc=symbol.location;
695684
loc.set_function(symbol.name);
696685
source_locationt &dloc=loc;
697686

698687
// function to call
688+
code_function_callt call_main(symbol.symbol_expr());
699689
call_main.add_source_location()=dloc;
700-
call_main.function()=symbol.symbol_expr();
701690
call_main.function().add_source_location()=dloc;
702691

703692
// if the method return type is not void, store return value in a new variable

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ exprt allocate_dynamic_object(
213213
ID_java,
214214
symbol_table);
215215
symbols_created.push_back(&malloc_sym);
216-
code_assignt assign=code_assignt(malloc_sym.symbol_expr(), malloc_expr);
216+
code_assignt assign(malloc_sym.symbol_expr(), malloc_expr);
217217
assign.add_source_location()=loc;
218218
output_code.copy_to_operands(assign);
219219
exprt malloc_symbol_expr=malloc_sym.symbol_expr();
@@ -1391,7 +1391,7 @@ void java_object_factoryt::gen_nondet_array_init(
13911391
symbol_table);
13921392
symbols_created.push_back(&array_init_symbol);
13931393
const auto &array_init_symexpr=array_init_symbol.symbol_expr();
1394-
codet data_assign=code_assignt(array_init_symexpr, init_array_expr);
1394+
code_assignt data_assign(array_init_symexpr, init_array_expr);
13951395
data_assign.add_source_location()=loc;
13961396
assignments.copy_to_operands(data_assign);
13971397

@@ -1738,8 +1738,7 @@ void java_object_factoryt::gen_method_call_if_present(
17381738
if(const auto func = symbol_table.lookup(method_name))
17391739
{
17401740
const java_method_typet &type = to_java_method_type(func->type);
1741-
code_function_callt fun_call;
1742-
fun_call.function() = func->symbol_expr();
1741+
code_function_callt fun_call(func->symbol_expr());
17431742
if(type.has_this())
17441743
fun_call.arguments().push_back(address_of_exprt(instance_expr));
17451744
assignments.add(fun_call);

jbmc/src/java_bytecode/java_static_initializers.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,16 @@ static void clinit_wrapper_do_recursive_calls(
208208
auto findit = symbol_table.symbols.find(base_init_routine);
209209
if(findit == symbol_table.symbols.end())
210210
continue;
211-
code_function_callt call_base;
212-
call_base.function() = findit->second.symbol_expr();
213-
init_body.move_to_operands(call_base);
211+
const code_function_callt call_base(findit->second.symbol_expr());
212+
init_body.add(call_base);
214213
}
215214

216215
const irep_idt &real_clinit_name = clinit_function_name(class_name);
217216
auto find_sym_it = symbol_table.symbols.find(real_clinit_name);
218217
if(find_sym_it != symbol_table.symbols.end())
219218
{
220-
code_function_callt call_real_init;
221-
call_real_init.function() = find_sym_it->second.symbol_expr();
222-
init_body.move_to_operands(call_real_init);
219+
const code_function_callt call_real_init(find_sym_it->second.symbol_expr());
220+
init_body.add(call_real_init);
223221
}
224222

225223
// If nondet-static option is given, add a standard nondet initialization for

jbmc/src/java_bytecode/java_string_library_preprocess.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,11 +1552,11 @@ codet java_string_library_preprocesst::make_object_get_class_code(
15521552
loc);
15531553

15541554
// > class1 = Class.forName(string1)
1555-
code_function_callt fun_call;
1556-
fun_call.function()=symbol_exprt(
1557-
"java::java.lang.Class.forName:(Ljava/lang/String;)Ljava/lang/Class;");
1558-
fun_call.lhs()=class1;
1559-
fun_call.arguments().push_back(string1);
1555+
code_function_callt fun_call(
1556+
class1,
1557+
symbol_exprt(
1558+
"java::java.lang.Class.forName:(Ljava/lang/String;)Ljava/lang/Class;"),
1559+
{string1});
15601560
const java_method_typet fun_type(
15611561
{java_method_typet::parametert(string_ptr_type)}, class_type);
15621562
fun_call.function().type()=fun_type;

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,12 @@ SCENARIO(
129129
callee.set(ID_C_class, "java::VirtualFunctionsTestParent");
130130
callee.set(ID_component_name, "f:()V");
131131

132-
code_function_callt call;
133-
call.function() = callee;
134-
// Specific argument doesn't matter, so just pass an appropriately typed
135-
// null pointer:
136-
call.arguments().push_back(
137-
null_pointer_exprt(
138-
to_pointer_type(to_code_type(callee.type()).parameters()[0].type())));
132+
const code_function_callt call(
133+
callee,
134+
// Specific argument doesn't matter, so just pass an appropriately typed
135+
// null pointer:
136+
{null_pointer_exprt(
137+
to_pointer_type(to_code_type(callee.type()).parameters()[0].type()))});
139138
virtual_call_inst->code = call;
140139

141140
test_program.add_instruction(END_FUNCTION);

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 = to_code_function_call(target->code);
3636
REQUIRE(call.function().id() == ID_symbol);
3737
REQUIRE(to_symbol_expr(call.function()).get_identifier() == function_name);
3838
}

src/cpp/cpp_typecheck_code.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ void cpp_typecheckt::typecheck_member_initializer(codet &code)
334334
else
335335
{
336336
auto source_location = code.source_location();
337-
code = codet(ID_skip);
337+
code = code_skipt();
338338
code.add_source_location() = source_location;
339339
}
340340
}

0 commit comments

Comments
 (0)