Skip to content

Commit 69528ad

Browse files
1 parent 23af75f commit 69528ad

File tree

4 files changed

+115
-195
lines changed

4 files changed

+115
-195
lines changed

jbmc/src/java_bytecode/java_string_library_preprocess.cpp

Lines changed: 77 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,57 +1123,37 @@ code_blockt java_string_library_preprocesst::make_assign_function_from_call(
11231123
/// primitive value contained in the given object.
11241124
optionalt<symbol_exprt>
11251125
java_string_library_preprocesst::get_primitive_value_of_object(
1126-
const exprt &object,
1127-
irep_idt type_name,
1128-
const source_locationt &loc,
1129-
const irep_idt &function_id,
1130-
symbol_table_baset &symbol_table,
1131-
code_blockt &code)
1132-
{
1126+
const exprt &object, irep_idt type_name, const source_locationt &loc,
1127+
const irep_idt &function_id, symbol_table_baset &symbol_table,
1128+
code_blockt &code) {
11331129
optionalt<struct_tag_typet> object_type;
11341130

11351131
typet value_type;
1136-
if(type_name==ID_boolean)
1137-
{
1138-
value_type=java_boolean_type();
1132+
if (type_name == ID_boolean) {
1133+
value_type = java_boolean_type();
11391134
object_type = struct_tag_typet("java::java.lang.Boolean");
1140-
}
1141-
else if(type_name==ID_char)
1142-
{
1143-
value_type=java_char_type();
1135+
} else if (type_name == ID_char) {
1136+
value_type = java_char_type();
11441137
object_type = struct_tag_typet("java::java.lang.Character");
1145-
}
1146-
else if(type_name==ID_byte)
1147-
{
1148-
value_type=java_byte_type();
1138+
} else if (type_name == ID_byte) {
1139+
value_type = java_byte_type();
11491140
object_type = struct_tag_typet("java::java.lang.Byte");
1150-
}
1151-
else if(type_name==ID_short)
1152-
{
1153-
value_type=java_short_type();
1141+
} else if (type_name == ID_short) {
1142+
value_type = java_short_type();
11541143
object_type = struct_tag_typet("java::java.lang.Short");
1155-
}
1156-
else if(type_name==ID_int)
1157-
{
1158-
value_type=java_int_type();
1144+
} else if (type_name == ID_int) {
1145+
value_type = java_int_type();
11591146
object_type = struct_tag_typet("java::java.lang.Integer");
1160-
}
1161-
else if(type_name==ID_long)
1162-
{
1163-
value_type=java_long_type();
1147+
} else if (type_name == ID_long) {
1148+
value_type = java_long_type();
11641149
object_type = struct_tag_typet("java::java.lang.Long");
1165-
}
1166-
else if(type_name==ID_float)
1167-
{
1168-
value_type=java_float_type();
1150+
} else if (type_name == ID_float) {
1151+
value_type = java_float_type();
11691152
object_type = struct_tag_typet("java::java.lang.Float");
1170-
}
1171-
else if(type_name==ID_double)
1172-
{
1173-
value_type=java_double_type();
1153+
} else if (type_name == ID_double) {
1154+
value_type = java_double_type();
11741155
object_type = struct_tag_typet("java::java.lang.Double");
1175-
}
1176-
else if(type_name==ID_void)
1156+
} else if (type_name == ID_void)
11771157
return {};
11781158
else
11791159
UNREACHABLE;
@@ -1183,26 +1163,23 @@ java_string_library_preprocesst::get_primitive_value_of_object(
11831163
// declare tmp_type_name to hold the value
11841164
const std::string aux_name = "tmp_" + id2string(type_name);
11851165
const symbolt &symbol =
1186-
fresh_java_symbol(value_type, aux_name, loc, function_id, symbol_table);
1166+
fresh_java_symbol(value_type, aux_name, loc, function_id, symbol_table);
11871167
const auto value = symbol.symbol_expr();
11881168

11891169
// Check that the type of the object is in the symbol table,
11901170
// otherwise there is no safe way of finding its value.
1191-
if(
1192-
const auto maybe_symbol =
1193-
symbol_table.lookup(object_type->get_identifier()))
1194-
{
1171+
if (const auto maybe_symbol =
1172+
symbol_table.lookup(object_type->get_identifier())) {
11951173
const struct_typet &struct_type = to_struct_type(maybe_symbol->type);
11961174
// Check that the type has a value field
11971175
const struct_union_typet::componentt &value_comp =
1198-
struct_type.get_component("value");
1199-
if(!value_comp.is_nil())
1200-
{
1176+
struct_type.get_component("value");
1177+
if (!value_comp.is_nil()) {
12011178
const pointer_typet struct_pointer_type = pointer_type(struct_type);
12021179
dereference_exprt deref{typecast_exprt(object, struct_pointer_type),
12031180
struct_pointer_type.subtype()};
1204-
const member_exprt deref_value{
1205-
std::move(deref), value_comp.get_name(), value_comp.type()};
1181+
const member_exprt deref_value{std::move(deref), value_comp.get_name(),
1182+
value_comp.type()};
12061183
code.add(code_assignt(value, deref_value), loc);
12071184
return value;
12081185
}
@@ -1222,16 +1199,15 @@ java_string_library_preprocesst::get_primitive_value_of_object(
12221199
/// \param argv: reference to an array of references
12231200
/// \param index: index of the desired object
12241201
/// \return An expression representing the object at position `index` of `argv`.
1225-
dereference_exprt java_string_library_preprocesst::get_object_at_index(
1226-
const exprt &argv,
1227-
std::size_t index)
1228-
{
1202+
dereference_exprt
1203+
java_string_library_preprocesst::get_object_at_index(const exprt &argv,
1204+
std::size_t index) {
12291205
const dereference_exprt deref_objs{argv, argv.type().subtype()};
12301206
const pointer_typet empty_pointer = pointer_type(java_void_type());
12311207
const pointer_typet pointer_of_pointer = pointer_type(empty_pointer);
12321208
const member_exprt data_member{deref_objs, "data", pointer_of_pointer};
12331209
const plus_exprt data_pointer_plus_index{
1234-
data_member, from_integer(index, java_int_type()), data_member.type()};
1210+
data_member, from_integer(index, java_int_type()), data_member.type()};
12351211
return dereference_exprt{data_pointer_plus_index,
12361212
data_pointer_plus_index.type().subtype()};
12371213
}
@@ -1268,35 +1244,27 @@ dereference_exprt java_string_library_preprocesst::get_object_at_index(
12681244
/// \return An expression of type `structured_type` representing the possible
12691245
/// values of the argument at position `index` of `argv`.
12701246
struct_exprt java_string_library_preprocesst::make_argument_for_format(
1271-
const exprt &argv,
1272-
std::size_t index,
1273-
const struct_typet &structured_type,
1274-
const source_locationt &loc,
1275-
const irep_idt &function_id,
1276-
symbol_table_baset &symbol_table,
1277-
code_blockt &code)
1278-
{
1247+
const exprt &argv, std::size_t index, const struct_typet &structured_type,
1248+
const source_locationt &loc, const irep_idt &function_id,
1249+
symbol_table_baset &symbol_table, code_blockt &code) {
12791250
// Declarations of the fields of arg_i_struct
12801251
// arg_i_struct is { arg_i_string_expr, tmp_int, tmp_char, ... }
12811252
struct_exprt arg_i_struct({}, structured_type);
12821253
std::list<exprt> field_exprs;
1283-
for(const auto & comp : structured_type.components())
1284-
{
1285-
const irep_idt &name=comp.get_name();
1286-
const typet &type=comp.type();
1254+
for (const auto &comp : structured_type.components()) {
1255+
const irep_idt &name = comp.get_name();
1256+
const typet &type = comp.type();
12871257
exprt field_expr;
1288-
if(name!="string_expr")
1289-
{
1290-
std::string tmp_name="tmp_"+id2string(name);
1258+
if (name != "string_expr") {
1259+
std::string tmp_name = "tmp_" + id2string(name);
12911260
const symbolt &field_symbol =
1292-
fresh_java_symbol(type, tmp_name, loc, function_id, symbol_table);
1261+
fresh_java_symbol(type, tmp_name, loc, function_id, symbol_table);
12931262
auto field_symbol_expr = field_symbol.symbol_expr();
12941263
field_expr = field_symbol_expr;
12951264
code.add(code_declt(field_symbol_expr), loc);
1296-
}
1297-
else
1265+
} else
12981266
field_expr =
1299-
make_nondet_string_expr(loc, function_id, symbol_table, code);
1267+
make_nondet_string_expr(loc, function_id, symbol_table, code);
13001268

13011269
field_exprs.push_back(field_expr);
13021270
arg_i_struct.copy_to_operands(field_expr);
@@ -1305,7 +1273,7 @@ struct_exprt java_string_library_preprocesst::make_argument_for_format(
13051273
// arg_i = argv[index]
13061274
const exprt obj = get_object_at_index(argv, index);
13071275
const symbolt &object_symbol = fresh_java_symbol(
1308-
obj.type(), "tmp_format_obj", loc, function_id, symbol_table);
1276+
obj.type(), "tmp_format_obj", loc, function_id, symbol_table);
13091277
const symbol_exprt arg_i = object_symbol.symbol_expr();
13101278

13111279
allocate_objectst allocate_objects(ID_java, loc, function_id, symbol_table);
@@ -1316,44 +1284,34 @@ struct_exprt java_string_library_preprocesst::make_argument_for_format(
13161284
code.append(tmp);
13171285

13181286
code.add(code_assignt(arg_i, obj), loc);
1319-
code.add(
1320-
code_assumet(
1321-
not_exprt(
1322-
equal_exprt(arg_i, null_pointer_exprt(to_pointer_type(arg_i.type()))))),
1323-
loc);
1287+
code.add(code_assumet(not_exprt(equal_exprt(
1288+
arg_i, null_pointer_exprt(to_pointer_type(arg_i.type()))))),
1289+
loc);
13241290

13251291
code_blockt code_not_null;
13261292

13271293
// Assigning all the fields of arg_i_struct
1328-
for(const auto &comp : structured_type.components())
1329-
{
1330-
const irep_idt &name=comp.get_name();
1331-
exprt field_expr=field_exprs.front();
1294+
for (const auto &comp : structured_type.components()) {
1295+
const irep_idt &name = comp.get_name();
1296+
exprt field_expr = field_exprs.front();
13321297
field_exprs.pop_front();
13331298

1334-
if(name=="string_expr")
1335-
{
1299+
if (name == "string_expr") {
13361300
const pointer_typet string_pointer =
1337-
java_reference_type(struct_tag_typet("java::java.lang.String"));
1301+
java_reference_type(struct_tag_typet("java::java.lang.String"));
13381302
const typecast_exprt arg_i_as_string{arg_i, string_pointer};
1339-
code_assign_java_string_to_string_expr(
1340-
to_string_expr(field_expr),
1341-
arg_i_as_string,
1342-
loc,
1343-
symbol_table,
1344-
code_not_null);
1345-
}
1346-
else if(name==ID_int || name==ID_float || name==ID_char || name==ID_boolean)
1347-
{
1303+
code_assign_java_string_to_string_expr(to_string_expr(field_expr),
1304+
arg_i_as_string, loc, symbol_table,
1305+
code_not_null);
1306+
} else if (name == ID_int || name == ID_float || name == ID_char ||
1307+
name == ID_boolean) {
13481308
const auto value = get_primitive_value_of_object(
1349-
arg_i, name, loc, function_id, symbol_table, code_not_null);
1350-
if(value.has_value())
1309+
arg_i, name, loc, function_id, symbol_table, code_not_null);
1310+
if (value.has_value())
13511311
code_not_null.add(code_assignt(field_expr, *value), loc);
13521312
else
13531313
code_not_null.add(code_assignt(field_expr, nil_exprt()), loc);
1354-
}
1355-
else
1356-
{
1314+
} else {
13571315
// TODO: date_time and hash_code not implemented
13581316
}
13591317
}
@@ -1374,16 +1332,13 @@ struct_exprt java_string_library_preprocesst::make_argument_for_format(
13741332
/// class of the arguments is not known, we give as argument to the internal
13751333
// format function a structure containing the different possible types.
13761334
code_blockt java_string_library_preprocesst::make_string_format_code(
1377-
const java_method_typet &type,
1378-
const source_locationt &loc,
1379-
const irep_idt &function_id,
1380-
symbol_table_baset &symbol_table)
1381-
{
1382-
PRECONDITION(type.parameters().size()==2);
1335+
const java_method_typet &type, const source_locationt &loc,
1336+
const irep_idt &function_id, symbol_table_baset &symbol_table) {
1337+
PRECONDITION(type.parameters().size() == 2);
13831338
code_blockt code;
1384-
exprt::operandst args =
1385-
process_parameters(type.parameters(), loc, function_id, symbol_table, code);
1386-
INVARIANT(args.size()==2, "String.format should have two arguments");
1339+
exprt::operandst args = process_parameters(type.parameters(), loc,
1340+
function_id, symbol_table, code);
1341+
INVARIANT(args.size() == 2, "String.format should have two arguments");
13871342

13881343
// The argument can be:
13891344
// a string, an integer, a floating point, a character, a boolean,
@@ -1403,19 +1358,17 @@ code_blockt java_string_library_preprocesst::make_string_format_code(
14031358
// containing each possible type used in format specifiers.
14041359
std::vector<exprt> processed_args;
14051360
processed_args.push_back(args[0]);
1406-
for(std::size_t i=0; i<MAX_FORMAT_ARGS; ++i)
1407-
processed_args.push_back(
1408-
make_argument_for_format(
1361+
for (std::size_t i = 0; i < MAX_FORMAT_ARGS; ++i)
1362+
processed_args.push_back(make_argument_for_format(
14091363
args[1], i, structured_type, loc, function_id, symbol_table, code));
14101364

14111365
const refined_string_exprt string_expr = string_expr_of_function(
1412-
ID_cprover_string_format_func, processed_args, loc, symbol_table, code);
1366+
ID_cprover_string_format_func, processed_args, loc, symbol_table, code);
14131367
const exprt java_string = allocate_fresh_string(
1414-
type.return_type(), loc, function_id, symbol_table, code);
1415-
code.add(
1416-
code_assign_string_expr_to_java_string(
1417-
java_string, string_expr, symbol_table, true),
1418-
loc);
1368+
type.return_type(), loc, function_id, symbol_table, code);
1369+
code.add(code_assign_string_expr_to_java_string(java_string, string_expr,
1370+
symbol_table, true),
1371+
loc);
14191372
code.add(code_returnt(java_string), loc);
14201373
return code;
14211374
}
@@ -1849,15 +1802,11 @@ void java_string_library_preprocesst::initialize_conversion_table()
18491802
["java::java.lang.String.equalsIgnoreCase:(Ljava/lang/String;)Z"]=
18501803
ID_cprover_string_equals_ignore_case_func;
18511804
conversion_table
1852-
["java::java.lang.String.format:(Ljava/lang/String;[Ljava/lang/Object;)"
1853-
"Ljava/lang/String;"] =
1854-
std::bind(
1855-
&java_string_library_preprocesst::make_string_format_code,
1856-
this,
1857-
std::placeholders::_1,
1858-
std::placeholders::_2,
1859-
std::placeholders::_3,
1860-
std::placeholders::_4);
1805+
["java::java.lang.String.format:(Ljava/lang/String;[Ljava/lang/Object;)"
1806+
"Ljava/lang/String;"] =
1807+
std::bind(&java_string_library_preprocesst::make_string_format_code,
1808+
this, std::placeholders::_1, std::placeholders::_2,
1809+
std::placeholders::_3, std::placeholders::_4);
18611810
cprover_equivalent_to_java_function
18621811
["java::java.lang.String.hashCode:()I"]=
18631812
ID_cprover_string_hash_code_func;

jbmc/src/java_bytecode/java_string_library_preprocess.h

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,10 @@ class java_string_library_preprocesst:public messaget
151151
const irep_idt &function_id,
152152
symbol_table_baset &symbol_table);
153153

154-
code_blockt make_string_format_code(
155-
const java_method_typet &type,
156-
const source_locationt &loc,
157-
const irep_idt &function_id,
158-
symbol_table_baset &symbol_table);
154+
code_blockt make_string_format_code(const java_method_typet &type,
155+
const source_locationt &loc,
156+
const irep_idt &function_id,
157+
symbol_table_baset &symbol_table);
159158

160159
code_blockt make_copy_string_code(
161160
const java_method_typet &type,
@@ -314,22 +313,17 @@ class java_string_library_preprocesst:public messaget
314313
const source_locationt &loc,
315314
symbol_table_baset &symbol_table);
316315

317-
struct_exprt make_argument_for_format(
318-
const exprt &argv,
319-
std::size_t index,
320-
const struct_typet &structured_type,
321-
const source_locationt &loc,
322-
const irep_idt &function_id,
323-
symbol_table_baset &symbol_table,
324-
code_blockt &code);
316+
struct_exprt make_argument_for_format(const exprt &argv, std::size_t index,
317+
const struct_typet &structured_type,
318+
const source_locationt &loc,
319+
const irep_idt &function_id,
320+
symbol_table_baset &symbol_table,
321+
code_blockt &code);
325322

326323
optionalt<symbol_exprt> get_primitive_value_of_object(
327-
const exprt &object,
328-
irep_idt type_name,
329-
const source_locationt &loc,
330-
const irep_idt &function_id,
331-
symbol_table_baset &symbol_table,
332-
code_blockt &code);
324+
const exprt &object, irep_idt type_name, const source_locationt &loc,
325+
const irep_idt &function_id, symbol_table_baset &symbol_table,
326+
code_blockt &code);
333327

334328
dereference_exprt get_object_at_index(const exprt &argv, std::size_t index);
335329
};

0 commit comments

Comments
 (0)