Skip to content

Commit 6c7d6d4

Browse files
authored
Merge pull request #1088 from smowton/smowton/fix/instrumentation_source_locations
Ensure source locations are kept across code instrumentation
2 parents a3e7557 + eb3b64c commit 6c7d6d4

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

src/java_bytecode/java_bytecode_convert_method.cpp

-18
Original file line numberDiff line numberDiff line change
@@ -999,24 +999,6 @@ static unsigned get_bytecode_type_width(const typet &ty)
999999
return ty.get_unsigned_int(ID_width);
10001000
}
10011001

1002-
/// Merge code's source location with source_location, and recursively
1003-
/// do the same to operand code. Typically this is used for a code_blockt
1004-
/// as is generated for some Java operations such as "putstatic", but will
1005-
/// also work if they generate conditionals, loops, etc.
1006-
/// Merge means that any fields already set in code.add_source_location()
1007-
/// remain so; any new ones from source_location are added.
1008-
static void merge_source_location_rec(
1009-
codet &code,
1010-
const source_locationt &source_location)
1011-
{
1012-
code.add_source_location().merge(source_location);
1013-
for(exprt &op : code.operands())
1014-
{
1015-
if(op.id()==ID_code)
1016-
merge_source_location_rec(to_code(op), source_location);
1017-
}
1018-
}
1019-
10201002
codet java_bytecode_convert_methodt::convert_instructions(
10211003
const methodt &method,
10221004
const code_typet &method_type,

src/java_bytecode/java_bytecode_instrument.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ codet java_bytecode_instrumentt::check_array_length(
305305
void java_bytecode_instrumentt::instrument_code(exprt &expr)
306306
{
307307
codet &code=to_code(expr);
308+
source_locationt old_source_location=code.source_location();
308309

309310
const irep_idt &statement=code.get_statement();
310311

@@ -408,6 +409,10 @@ void java_bytecode_instrumentt::instrument_code(exprt &expr)
408409
block.copy_to_operands(code);
409410
code=block;
410411
}
412+
413+
// Ensure source location is retained:
414+
if(!old_source_location.get_line().empty())
415+
merge_source_location_rec(code, old_source_location);
411416
}
412417

413418
/// Computes the instrumentation for `expr` in the form of

src/java_bytecode/java_utils.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ unsigned java_method_parameter_slots(const code_typet &t)
5555
return slots;
5656
}
5757

58-
5958
const std::string java_class_to_package(const std::string &canonical_classname)
6059
{
6160
return trim_from_last_delimiter(canonical_classname, '.');
@@ -99,3 +98,12 @@ void generate_class_stub(
9998
java_root_class(*class_symbol);
10099
}
101100
}
101+
102+
void merge_source_location_rec(
103+
exprt &expr,
104+
const source_locationt &source_location)
105+
{
106+
expr.add_source_location().merge(source_location);
107+
for(exprt &op : expr.operands())
108+
merge_source_location_rec(op, source_location);
109+
}

src/java_bytecode/java_utils.h

+13
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,17 @@ unsigned java_method_parameter_slots(const code_typet &t);
3232

3333
const std::string java_class_to_package(const std::string &canonical_classname);
3434

35+
/// Attaches a source location to an expression and all of its subexpressions.
36+
/// Usually only codet needs this, but there are a few known examples of
37+
/// expressions needing a location, such as
38+
/// `goto_convertt::do_function_call_symbol` (function() needs a location)
39+
/// and `goto_convertt::clean_expr` (any subexpression being split into a
40+
/// separate instruction needs a location), so for safety we give every
41+
/// mentioned expression a location.
42+
/// Any code or expressions with source location fields already set keep those
43+
/// fields using rules of source_locationt::merge.
44+
void merge_source_location_rec(
45+
exprt &expr,
46+
const source_locationt &source_location);
47+
3548
#endif // CPROVER_JAVA_BYTECODE_JAVA_UTILS_H

0 commit comments

Comments
 (0)