Skip to content

Commit 1622bf8

Browse files
authored
Merge pull request diffblue#4508 from antlechner/antonia/refactor-is-string-type
Refactor logic that checks if a given type represents a Java string
2 parents 999116f + a6a0bec commit 1622bf8

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -366,21 +366,10 @@ void initialize_nondet_string_fields(
366366
bool printable,
367367
allocate_objectst &allocate_objects)
368368
{
369-
PRECONDITION(
370-
java_string_library_preprocesst
371-
::implements_java_char_sequence(struct_expr.type()));
372-
373369
namespacet ns(symbol_table);
374-
375370
const struct_typet &struct_type =
376371
to_struct_type(ns.follow(struct_expr.type()));
377-
378-
// In case the type for String was not added to the symbol table,
379-
// (typically when string refinement is not activated), `struct_type`
380-
// just contains the standard Object fields (or may have some other model
381-
// entirely), and in particular has no length and data fields.
382-
PRECONDITION(
383-
struct_type.has_component("length") && struct_type.has_component("data"));
372+
PRECONDITION(is_java_string_type(struct_type));
384373

385374
// We allow type StringBuffer and StringBuilder to be initialized
386375
// in the same way has String, because they have the same structure and
@@ -774,7 +763,6 @@ void java_object_factoryt::gen_nondet_struct_init(
774763
{
775764
const namespacet ns(symbol_table);
776765
PRECONDITION(ns.follow(expr.type()).id()==ID_struct);
777-
PRECONDITION(struct_type.id()==ID_struct);
778766

779767
typedef struct_typet::componentst componentst;
780768
const irep_idt &struct_tag=struct_type.get_tag();
@@ -792,15 +780,12 @@ void java_object_factoryt::gen_nondet_struct_init(
792780
// generated differently from object fields in the general case, see
793781
// \ref java_object_factoryt::initialize_nondet_string_fields.
794782

795-
const bool is_char_sequence =
796-
java_string_library_preprocesst::implements_java_char_sequence(struct_type);
797-
const bool has_length_and_data =
798-
struct_type.has_component("length") && struct_type.has_component("data");
799-
const bool is_string_type = is_char_sequence && has_length_and_data;
800783
const bool has_string_input_values =
801784
!object_factory_parameters.string_input_values.empty();
802785

803-
if(is_string_type && has_string_input_values && !skip_classid)
786+
if(
787+
is_java_string_type(struct_type) && has_string_input_values &&
788+
!skip_classid)
804789
{ // We're dealing with a string and we should set fixed input values.
805790
// We create a switch statement where each case is an assignment
806791
// of one of the fixed input strings to the input variable in question
@@ -817,7 +802,7 @@ void java_object_factoryt::gen_nondet_struct_init(
817802
else if(
818803
(!is_sub && !skip_classid &&
819804
update_in_place != update_in_placet::MUST_UPDATE_IN_PLACE) ||
820-
is_string_type)
805+
is_java_string_type(struct_type))
821806
{
822807
// Add an initial all-zero write. Most of the fields of this will be
823808
// overwritten, but it helps to have a whole-structure write that analysis
@@ -834,7 +819,7 @@ void java_object_factoryt::gen_nondet_struct_init(
834819
// If the initialised type is a special-cased String type (one with length
835820
// and data fields introduced by string-library preprocessing), initialise
836821
// those fields with nondet values
837-
if(is_string_type)
822+
if(is_java_string_type(struct_type))
838823
{ // We're dealing with a string
839824
initialize_nondet_string_fields(
840825
to_struct_expr(*initial_object),
@@ -874,7 +859,8 @@ void java_object_factoryt::gen_nondet_struct_init(
874859
code.add_source_location() = location;
875860
assignments.add(code);
876861
}
877-
else if(is_string_type && (name == "length" || name == "data"))
862+
else if(
863+
is_java_string_type(struct_type) && (name == "length" || name == "data"))
878864
{
879865
// In this case these were set up above.
880866
continue;

jbmc/src/java_bytecode/java_utils.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Author: Daniel Kroening, [email protected]
99
#include "java_utils.h"
1010

1111
#include "java_root_class.h"
12+
#include "java_string_library_preprocess.h"
1213

1314
#include <util/fresh_symbol.h>
1415
#include <util/invariant.h>
@@ -29,6 +30,14 @@ bool java_is_array_type(const typet &type)
2930
return is_java_array_tag(to_struct_type(type).get_tag());
3031
}
3132

33+
bool is_java_string_type(const struct_typet &struct_type)
34+
{
35+
return java_string_library_preprocesst::implements_java_char_sequence(
36+
struct_type) &&
37+
struct_type.has_component("length") &&
38+
struct_type.has_component("data");
39+
}
40+
3241
unsigned java_local_variable_slots(const typet &t)
3342
{
3443

jbmc/src/java_bytecode/java_utils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ Author: Daniel Kroening, [email protected]
2222

2323
bool java_is_array_type(const typet &type);
2424

25+
/// Returns true iff the argument represents a string type (CharSequence,
26+
/// StringBuilder, StringBuffer or String).
27+
/// The check for the length and data components is necessary in the case where
28+
/// string refinement is not activated. In this case, \p struct_type only
29+
/// contains the standard Object fields (or may have some other model entirely),
30+
/// and in particular may not have length and data fields.
31+
bool is_java_string_type(const struct_typet &struct_type);
32+
2533
void generate_class_stub(
2634
const irep_idt &class_name,
2735
symbol_table_baset &symbol_table,

0 commit comments

Comments
 (0)