Skip to content

Commit 157f567

Browse files
authored
Merge pull request diffblue#4509 from antlechner/antonia/refactor-constant-string-symbol
Add a version of get_or_create_string_literal_symbol that takes a string parameter
2 parents 0f928f8 + 9e499d5 commit 157f567

10 files changed

+103
-46
lines changed

jbmc/src/java_bytecode/expr2java.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Author: Daniel Kroening, [email protected]
2323
#include <ansi-c/expr2c_class.h>
2424

2525
#include "java_qualifiers.h"
26+
#include "java_string_literal_expr.h"
2627
#include "java_types.h"
2728

2829
std::string expr2javat::convert(const typet &src)
@@ -414,8 +415,11 @@ std::string expr2javat::convert_with_precedence(
414415
id2string(src.get(ID_component_name)) +
415416
")";
416417
}
417-
else if(src.id()==ID_java_string_literal)
418-
return '"'+MetaString(src.get_string(ID_value))+'"';
418+
else if(
419+
const auto &literal = expr_try_dynamic_cast<java_string_literal_exprt>(src))
420+
{
421+
return '"' + MetaString(id2string(literal->value())) + '"';
422+
}
419423
else if(src.id()==ID_constant)
420424
return convert_constant(to_constant_expr(src), precedence=16);
421425
else

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include "java_bytecode_convert_method_class.h"
1919
#include "java_static_initializers.h"
2020
#include "java_string_library_preprocess.h"
21+
#include "java_string_literal_expr.h"
2122
#include "java_types.h"
2223
#include "java_utils.h"
2324
#include "pattern.h"
@@ -1313,7 +1314,7 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
13131314
PRECONDITION(op.empty() && results.size() == 1);
13141315

13151316
INVARIANT(
1316-
arg0.id() != ID_java_string_literal && arg0.id() != ID_type,
1317+
!can_cast_expr<java_string_literal_exprt>(arg0) && arg0.id() != ID_type,
13171318
"String and Class literals should have been lowered in "
13181319
"generate_constant_global_variables");
13191320

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@ Author: Daniel Kroening, [email protected]
2626

2727
#include <goto-programs/class_hierarchy.h>
2828

29+
#include "ci_lazy_methods.h"
2930
#include "java_bytecode_concurrency_instrumentation.h"
3031
#include "java_bytecode_convert_class.h"
3132
#include "java_bytecode_convert_method.h"
32-
#include "java_bytecode_internal_additions.h"
3333
#include "java_bytecode_instrument.h"
34-
#include "java_bytecode_typecheck.h"
35-
#include "java_entry_point.h"
34+
#include "java_bytecode_internal_additions.h"
3635
#include "java_bytecode_parser.h"
36+
#include "java_bytecode_typecheck.h"
3737
#include "java_class_loader.h"
38-
#include "java_string_literals.h"
38+
#include "java_entry_point.h"
3939
#include "java_static_initializers.h"
40+
#include "java_string_literal_expr.h"
41+
#include "java_string_literals.h"
4042
#include "java_utils.h"
41-
#include "ci_lazy_methods.h"
4243

4344
#include "expr2java.h"
4445
#include "load_method_by_regex.h"
@@ -417,12 +418,12 @@ static exprt get_ldc_result(
417418
address_of_exprt(
418419
get_or_create_class_literal_symbol(class_id, symbol_table));
419420
}
420-
else if(ldc_arg0.id() == ID_java_string_literal)
421+
else if(
422+
const auto &literal =
423+
expr_try_dynamic_cast<java_string_literal_exprt>(ldc_arg0))
421424
{
422-
return
423-
address_of_exprt(
424-
get_or_create_string_literal_symbol(
425-
ldc_arg0, symbol_table, string_refinement_enabled));
425+
return address_of_exprt(get_or_create_string_literal_symbol(
426+
*literal, symbol_table, string_refinement_enabled));
426427
}
427428
else
428429
{

jbmc/src/java_bytecode/java_bytecode_parser.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ Author: Daniel Kroening, [email protected]
2121
#include <util/string_constant.h>
2222
#include <util/optional.h>
2323

24+
#include "bytecode_info.h"
2425
#include "java_bytecode_parse_tree.h"
26+
#include "java_string_literal_expr.h"
2527
#include "java_types.h"
26-
#include "bytecode_info.h"
2728

2829
#ifdef DEBUG
2930
#include <iostream>
@@ -839,9 +840,7 @@ void java_bytecode_parsert::rconstant_pool()
839840
case CONSTANT_String:
840841
{
841842
// ldc turns these into references to java.lang.String
842-
exprt string_literal(ID_java_string_literal);
843-
string_literal.set(ID_value, pool_entry(it->ref1).s);
844-
it->expr=string_literal;
843+
it->expr = java_string_literal_exprt{pool_entry(it->ref1).s};
845844
}
846845
break;
847846

jbmc/src/java_bytecode/java_bytecode_typecheck_expr.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ Author: Daniel Kroening, [email protected]
1717
#include <util/unicode.h>
1818

1919
#include "java_pointer_casts.h"
20-
#include "java_types.h"
21-
#include "java_utils.h"
2220
#include "java_root_class.h"
2321
#include "java_string_library_preprocess.h"
22+
#include "java_string_literal_expr.h"
23+
#include "java_types.h"
24+
#include "java_utils.h"
2425

2526
void java_bytecode_typecheckt::typecheck_expr(exprt &expr)
2627
{
@@ -39,7 +40,7 @@ void java_bytecode_typecheckt::typecheck_expr(exprt &expr)
3940
typecheck_expr(*it);
4041

4142
INVARIANT(
42-
expr.id() != ID_java_string_literal,
43+
!can_cast_expr<java_string_literal_exprt>(expr),
4344
"String literals should have been converted to constant globals "
4445
"before typecheck_expr");
4546

jbmc/src/java_bytecode/java_entry_point.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,10 @@ static void java_static_lifetime_init(
135135
// and we can refer to them later when we initialize input values.
136136
for(const auto &val : object_factory_parameters.string_input_values)
137137
{
138-
exprt my_literal(ID_java_string_literal);
139-
my_literal.set(ID_value, val);
140138
// We ignore the return value of the following call, we just need to
141139
// make sure the string is in the symbol table.
142140
get_or_create_string_literal_symbol(
143-
my_literal,
144-
symbol_table,
145-
string_refinement_enabled);
141+
val, symbol_table, string_refinement_enabled);
146142
}
147143

148144
// We need to zero out all static variables, or nondet-initialize if they're
@@ -174,15 +170,13 @@ static void java_static_lifetime_init(
174170

175171
bool class_is_array = is_java_array_tag(sym.name);
176172

177-
exprt name_literal(ID_java_string_literal);
178-
name_literal.set(ID_value, to_class_type(class_symbol.type).get_tag());
179-
180173
journalling_symbol_tablet journalling_table =
181174
journalling_symbol_tablet::wrap(symbol_table);
182175

183-
symbol_exprt class_name_literal =
184-
get_or_create_string_literal_symbol(
185-
name_literal, journalling_table, string_refinement_enabled);
176+
symbol_exprt class_name_literal = get_or_create_string_literal_symbol(
177+
to_class_type(class_symbol.type).get_tag(),
178+
journalling_table,
179+
string_refinement_enabled);
186180

187181
// If that created any new symbols make sure we initialise those too:
188182
const auto &new_symbols = journalling_table.get_inserted();

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,10 +720,8 @@ alternate_casest get_string_input_values_code(
720720
alternate_casest cases;
721721
for(const auto &val : string_input_values)
722722
{
723-
exprt name_literal(ID_java_string_literal);
724-
name_literal.set(ID_value, val);
725723
const symbol_exprt s =
726-
get_or_create_string_literal_symbol(name_literal, symbol_table, true);
724+
get_or_create_string_literal_symbol(val, symbol_table, true);
727725
cases.push_back(code_assignt(expr, s));
728726
}
729727
return cases;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*******************************************************************\
2+
3+
Module: Java Bytecode
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Representation of a constant Java string
11+
12+
#ifndef CPROVER_JAVA_BYTECODE_JAVA_STRING_LITERAL_EXPR_H
13+
#define CPROVER_JAVA_BYTECODE_JAVA_STRING_LITERAL_EXPR_H
14+
15+
#include <util/expr.h>
16+
17+
class java_string_literal_exprt : public exprt
18+
{
19+
public:
20+
explicit java_string_literal_exprt(const irep_idt &literal)
21+
: exprt(ID_java_string_literal)
22+
{
23+
set(ID_value, literal);
24+
}
25+
26+
irep_idt value() const
27+
{
28+
return get(ID_value);
29+
}
30+
};
31+
32+
template <>
33+
inline bool can_cast_expr<java_string_literal_exprt>(const exprt &base)
34+
{
35+
return base.id() == ID_java_string_literal;
36+
}
37+
38+
#endif // CPROVER_JAVA_BYTECODE_JAVA_STRING_LITERAL_EXPR_H

jbmc/src/java_bytecode/java_string_literals.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Author: Chris Smowton, [email protected]
88

99
#include "java_string_literals.h"
1010
#include "java_root_class.h"
11+
#include "java_string_literal_expr.h"
1112
#include "java_types.h"
1213
#include "java_utils.h"
1314

@@ -55,21 +56,12 @@ static array_exprt utf16_to_array(const std::wstring &in)
5556
return ret;
5657
}
5758

58-
/// Creates or gets an existing constant global symbol for a given string
59-
/// literal.
60-
/// \param string_expr: string literal expression to convert
61-
/// \param symbol_table: global symbol table. If not already present, constant
62-
/// global symbols will be added.
63-
/// \param string_refinement_enabled: if true, string refinement's string data
64-
/// structure will also be initialised and added to the symbol table.
65-
/// \return a symbol_expr corresponding to the new or existing literal symbol.
6659
symbol_exprt get_or_create_string_literal_symbol(
67-
const exprt &string_expr,
60+
const java_string_literal_exprt &string_expr,
6861
symbol_table_baset &symbol_table,
6962
bool string_refinement_enabled)
7063
{
71-
PRECONDITION(string_expr.id() == ID_java_string_literal);
72-
const irep_idt value = string_expr.get(ID_value);
64+
const irep_idt value = string_expr.value();
7365
const struct_tag_typet string_type("java::java.lang.String");
7466

7567
const std::string escaped_symbol_name = escape_non_alnum(id2string(value));
@@ -215,3 +207,14 @@ symbol_exprt get_or_create_string_literal_symbol(
215207

216208
return new_symbol.symbol_expr();
217209
}
210+
211+
symbol_exprt get_or_create_string_literal_symbol(
212+
const irep_idt &string_value,
213+
symbol_table_baset &symbol_table,
214+
bool string_refinement_enabled)
215+
{
216+
return get_or_create_string_literal_symbol(
217+
java_string_literal_exprt{string_value},
218+
symbol_table,
219+
string_refinement_enabled);
220+
}

jbmc/src/java_bytecode/java_string_literals.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,26 @@ Author: Chris Smowton, [email protected]
1212
#include <util/symbol_table.h>
1313
#include <util/std_expr.h>
1414

15+
class java_string_literal_exprt;
16+
17+
/// Creates or gets an existing constant global symbol for a given string
18+
/// literal.
19+
/// \param string_expr: string literal expression to convert
20+
/// \param symbol_table: global symbol table. If not already present, constant
21+
/// global symbols will be added.
22+
/// \param string_refinement_enabled: if true, string refinement's string data
23+
/// structure will also be initialised and added to the symbol table.
24+
/// \return a symbol_expr corresponding to the new or existing literal symbol.
25+
symbol_exprt get_or_create_string_literal_symbol(
26+
const java_string_literal_exprt &string_expr,
27+
symbol_table_baset &symbol_table,
28+
bool string_refinement_enabled);
29+
30+
/// Same as
31+
/// get_or_create_string_literal_symbol(const exprt&, symbol_table_baset&, bool)
32+
/// except it takes an id/string parameter rather than a string literal exprt.
1533
symbol_exprt get_or_create_string_literal_symbol(
16-
const exprt &string_expr,
34+
const irep_idt &string_value,
1735
symbol_table_baset &symbol_table,
1836
bool string_refinement_enabled);
1937

0 commit comments

Comments
 (0)