Skip to content

Commit eb3b64c

Browse files
committed
Ensure source locations are assigned to all expressions
This (a) assigns locations more generously in convert_instructions(...) and (b) ensures they are preserved across instrumentation, both of which help the instrument-cover-goals pass to add goals where they belong.
1 parent 9ab281b commit eb3b64c

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
@@ -998,24 +998,6 @@ static unsigned get_bytecode_type_width(const typet &ty)
998998
return ty.get_unsigned_int(ID_width);
999999
}
10001000

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

src/java_bytecode/java_bytecode_instrument.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ codet java_bytecode_instrumentt::check_array_length(
300300
void java_bytecode_instrumentt::instrument_code(exprt &expr)
301301
{
302302
codet &code=to_code(expr);
303+
source_locationt old_source_location=code.source_location();
303304

304305
const irep_idt &statement=code.get_statement();
305306

@@ -405,6 +406,10 @@ void java_bytecode_instrumentt::instrument_code(exprt &expr)
405406
block.copy_to_operands(code);
406407
code=block;
407408
}
409+
410+
// Ensure source location is retained:
411+
if(!old_source_location.get_line().empty())
412+
merge_source_location_rec(code, old_source_location);
408413
}
409414

410415
/// 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)