Skip to content

Commit aca06bd

Browse files
authored
Merge pull request #5144 from smowton/smowton/feature/java-record-getstatic-source-locations
Java frontend: record source locations of read static variables
2 parents a33ac4e + 4c5ddc9 commit aca06bd

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,12 @@ java_bytecode_convert_methodt::convert_instructions(const methodt &method)
16441644
symbol_table.lookup_ref(field_id).symbol_expr());
16451645

16461646
convert_getstatic(
1647-
arg0, symbol_expr, is_assertions_disabled_field, c, results);
1647+
i_it->source_location,
1648+
arg0,
1649+
symbol_expr,
1650+
is_assertions_disabled_field,
1651+
c,
1652+
results);
16481653
}
16491654
else if(bytecode == BC_putfield)
16501655
{
@@ -2649,6 +2654,7 @@ code_blockt java_bytecode_convert_methodt::convert_putfield(
26492654
}
26502655

26512656
void java_bytecode_convert_methodt::convert_getstatic(
2657+
const source_locationt &source_location,
26522658
const exprt &arg0,
26532659
const symbol_exprt &symbol_expr,
26542660
const bool is_assertions_disabled_field,
@@ -2676,7 +2682,9 @@ void java_bytecode_convert_methodt::convert_getstatic(
26762682
needed_lazy_methods->add_needed_class(arg0.get_string(ID_class));
26772683
}
26782684
}
2679-
results[0] = java_bytecode_promotion(symbol_expr);
2685+
symbol_exprt symbol_with_location = symbol_expr;
2686+
symbol_with_location.add_source_location() = source_location;
2687+
results[0] = java_bytecode_promotion(symbol_with_location);
26802688

26812689
// Note this initializer call deliberately inits the class used to make
26822690
// the reference, which may be a child of the class that actually defines

jbmc/src/java_bytecode/java_bytecode_convert_method_class.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ class java_bytecode_convert_methodt:public messaget
432432
exprt::operandst &results) const;
433433

434434
void convert_getstatic(
435+
const source_locationt &source_location,
435436
const exprt &arg0,
436437
const symbol_exprt &symbol_expr,
437438
bool is_assertions_disabled_field,
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class ClassReadingStaticField {
2+
3+
static int x;
4+
5+
public static int test() {
6+
7+
return x;
8+
9+
}
10+
11+
}

jbmc/unit/java_bytecode/java_bytecode_convert_method/convert_method.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,45 @@ SCENARIO(
291291
}
292292
}
293293

294+
SCENARIO(
295+
"java_bytecode_convert_method_with_getstatic",
296+
"[core][java_bytecode][java_bytecode_convert_method]")
297+
{
298+
GIVEN("A class that reads a static field.")
299+
{
300+
const symbol_tablet symbol_table = load_java_class(
301+
"ClassReadingStaticField",
302+
"./java_bytecode/java_bytecode_convert_method");
303+
304+
WHEN("Converting the method that reads said field")
305+
{
306+
const auto &method_symbol =
307+
symbol_table.lookup_ref("java::ClassReadingStaticField.test:()I");
308+
309+
THEN(
310+
"There should be a symbol expression attributed to bytecode index "
311+
"0 (the getstatic instruction)")
312+
{
313+
bool found = false;
314+
method_symbol.value.visit_pre([&found](const exprt &subexpr) {
315+
if(
316+
const auto symbol_expr =
317+
expr_try_dynamic_cast<symbol_exprt>(subexpr))
318+
{
319+
if(
320+
symbol_expr->source_location().get_java_bytecode_index() == "0" &&
321+
symbol_expr->get_identifier() ==
322+
"java::ClassReadingStaticField.x")
323+
found = true;
324+
}
325+
});
326+
327+
REQUIRE(found);
328+
}
329+
}
330+
}
331+
}
332+
294333
/// Allow access to private methods so that they can be unit tested
295334
class java_bytecode_convert_method_unit_testt
296335
{

0 commit comments

Comments
 (0)