Skip to content

Java frontend: Set source locations on instructions in code_blocks #1055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
6 changes: 6 additions & 0 deletions regression/cbmc-java/putstatic_source_location/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CORE
test.class
--show-goto-functions
^EXIT=0$
^SIGNAL=0$
test\.java line 5 function java::test.main:\(\)V bytecode_index 1
11 changes: 11 additions & 0 deletions regression/cbmc-java/putstatic_source_location/test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class test {

public static int x;
public static void main() {
x = 1;
}
static {
x = 0;
}

}
20 changes: 19 additions & 1 deletion src/java_bytecode/java_bytecode_convert_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,24 @@ static unsigned get_bytecode_type_width(const typet &ty)
return ty.get_unsigned_int(ID_width);
}

/// Merge code's source location with source_location, and recursively
/// do the same to operand code. Typically this is used for a code_blockt
/// as is generated for some Java operations such as "putstatic", but will
/// also work if they generate conditionals, loops, etc.
/// Merge means that any fields already set in code.add_source_location()
/// remain so; any new ones from source_location are added.
static void merge_source_location_rec(
codet &code,
const source_locationt &source_location)
{
code.add_source_location().merge(source_location);
for(exprt &op : code.operands())
{
if(op.id()==ID_code)
merge_source_location_rec(to_code(op), source_location);
}
}

codet java_bytecode_convert_methodt::convert_instructions(
const methodt &method,
const code_typet &method_type,
Expand Down Expand Up @@ -2376,7 +2394,7 @@ codet java_bytecode_convert_methodt::convert_instructions(
}

if(!i_it->source_location.get_line().empty())
c.add_source_location()=i_it->source_location;
merge_source_location_rec(c, i_it->source_location);

push(results);

Expand Down
9 changes: 9 additions & 0 deletions src/util/source_location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ std::string source_locationt::as_string(bool print_cwd) const
return dest;
}

void source_locationt::merge(const source_locationt &from)
{
forall_named_irep(it, from.get_named_sub())
{
if(get(it->first).empty())
set(it->first, it->second);
}
}

std::ostream &operator << (
std::ostream &out,
const source_locationt &source_location)
Expand Down
4 changes: 4 additions & 0 deletions src/util/source_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ class source_locationt:public irept
return is_built_in(id2string(get_file()));
}

/// Set all unset source-location fields in this object to their values in
/// 'from'. Leave set fields in this object alone.
void merge(const source_locationt &from);

static const source_locationt &nil()
{
return static_cast<const source_locationt &>(get_nil_irep());
Expand Down