Skip to content

Parse bridge flag [TG-4115] #2582

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
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
3 changes: 3 additions & 0 deletions jbmc/src/java_bytecode/java_bytecode_convert_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ void java_bytecode_convert_method_lazy(
if(m.is_static)
member_type.set(ID_is_static, true);

if(m.is_bridge)
member_type.set(ID_is_bridge_method, m.is_bridge);

// do we need to add 'this' as a parameter?
if(!m.is_static)
{
Expand Down
11 changes: 6 additions & 5 deletions jbmc/src/java_bytecode/java_bytecode_parse_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ struct java_bytecode_parse_treet
struct methodt : public membert
{
irep_idt base_name;
bool is_native, is_abstract, is_synchronized;
bool is_native, is_abstract, is_synchronized, is_bridge;
source_locationt source_location;

typedef std::vector<instructiont> instructionst;
Expand Down Expand Up @@ -164,10 +164,11 @@ struct java_bytecode_parse_treet

void output(std::ostream &out) const;

methodt():
is_native(false),
is_abstract(false),
is_synchronized(false)
methodt()
: is_native(false),
is_abstract(false),
is_synchronized(false),
is_bridge(false)
Copy link
Contributor

@LAJW LAJW Jul 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something I'll have to remind Thomas about, but setting class fields like that is prone to errors. It's much better to define their default values in the class definition, like so:

private:
  bool is_native = false;
  bool is_abstract = false;
  bool is_synchronized = false;
  bool is_bridge = false;

That way, when you decide to define extra constructors, there's no risk of ever forgetting to initialize values.

{
}
};
Expand Down
1 change: 1 addition & 0 deletions jbmc/src/java_bytecode/java_bytecode_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,7 @@ void java_bytecode_parsert::rmethod(classt &parsed_class)
method.is_private=(access_flags&ACC_PRIVATE)!=0;
method.is_synchronized=(access_flags&ACC_SYNCHRONIZED)!=0;
method.is_native=(access_flags&ACC_NATIVE)!=0;
method.is_bridge = (access_flags & ACC_BRIDGE) != 0;
method.name=pool_entry(name_index).s;
method.base_name=pool_entry(name_index).s;
method.descriptor=id2string(pool_entry(descriptor_index).s);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class ClassWithBridgeMethod implements Comparable<ClassWithBridgeMethod> {
public int compareTo(ClassWithBridgeMethod other) {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************\

Module: Unit tests for converting constructors and static initializers

Author: Diffblue Limited.

\*******************************************************************/

#include <testing-utils/catch.hpp>

#include <util/symbol_table.h>

#include <java-testing-utils/load_java_class.h>
#include <java-testing-utils/require_type.h>

SCENARIO(
"java_bytecode_convert_bridge_method",
"[core][java_bytecode][java_bytecode_convert_method]")
{
GIVEN("A class with a bridge method")
{
const symbol_tablet symbol_table = load_java_class(
"ClassWithBridgeMethod", "./java_bytecode/java_bytecode_convert_method");

const std::string method_name = "java::ClassWithBridgeMethod.compareTo";

WHEN("When parsing the bridge method")
{
const symbolt function_symbol =
symbol_table.lookup_ref(method_name + ":(Ljava/lang/Object;)I");

const code_typet &function_type =
require_type::require_code(function_symbol.type);
THEN("The method should be marked as a bridge method")
{
REQUIRE(function_type.get_bool(ID_is_bridge_method));
}
}
WHEN("When parsing a non-bridge method")
{
THEN("THe method should not be marked as a bridge method")
{
const symbolt function_symbol =
symbol_table.lookup_ref(method_name + ":(LClassWithBridgeMethod;)I");

const code_typet &function_type =
require_type::require_code(function_symbol.type);
THEN("The method should be marked as a bridge method")
{
REQUIRE_FALSE(function_type.get_bool(ID_is_bridge_method));
}
}
}
}
}
1 change: 1 addition & 0 deletions src/util/irep_ids.def
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ IREP_ID_TWO(C_must_not_throw, #must_not_throw)
IREP_ID_ONE(is_inner_class)
IREP_ID_ONE(is_anonymous)
IREP_ID_ONE(outer_class)
IREP_ID_ONE(is_bridge_method)

// Projects depending on this code base that wish to extend the list of
// available ids should provide a file local_irep_ids.h in their source tree and
Expand Down