Skip to content

Fix typo in simplify_byte_extract #3155

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 2 commits into from
Oct 14, 2018
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
46 changes: 0 additions & 46 deletions jbmc/unit/util/simplify_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,12 @@
#include <testing-utils/catch.hpp>

#include <java_bytecode/java_types.h>
#include <util/arith_tools.h>
#include <util/c_types.h>
#include <util/config.h>
#include <util/namespace.h>
#include <util/pointer_predicates.h>
#include <util/simplify_expr.h>
#include <util/std_expr.h>
#include <util/symbol_table.h>

TEST_CASE("Simplify pointer_offset(address of array index)")
{
config.set_arch("none");

symbol_tablet symbol_table;
namespacet ns(symbol_table);

array_typet array_type(char_type(), from_integer(2, size_type()));
symbol_exprt array("A", array_type);
index_exprt index(array, from_integer(1, index_type()));
address_of_exprt address_of(index);

exprt p_o=pointer_offset(address_of);

exprt simp=simplify_expr(p_o, ns);

REQUIRE(simp.id()==ID_constant);
mp_integer offset_value;
REQUIRE(!to_integer(simp, offset_value));
REQUIRE(offset_value==1);
}

TEST_CASE("Simplify const pointer offset")
{
config.set_arch("none");

symbol_tablet symbol_table;
namespacet ns(symbol_table);

// build a numeric constant of some pointer type
constant_exprt number=from_integer(1234, size_type());
number.type()=pointer_type(char_type());

exprt p_o=pointer_offset(number);

exprt simp=simplify_expr(p_o, ns);

REQUIRE(simp.id()==ID_constant);
mp_integer offset_value;
REQUIRE(!to_integer(simp, offset_value));
REQUIRE(offset_value==1234);
}

namespace
{

Expand Down
6 changes: 3 additions & 3 deletions src/util/simplify_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1722,9 +1722,9 @@ bool simplify_exprt::simplify_byte_extract(byte_extract_exprt &expr)
// byte extract of full object is object
// don't do any of the following if endianness doesn't match, as
// bytes need to be swapped
if(offset==0 &&
base_type_eq(expr.type(), expr.op().type(), ns) &&
byte_extract_id()!=expr.id())
if(
offset == 0 && base_type_eq(expr.type(), expr.op().type(), ns) &&
byte_extract_id() == expr.id())
{
exprt tmp=expr.op();
expr.swap(tmp);
Expand Down
1 change: 1 addition & 0 deletions unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ SRC += analyses/ai/ai.cpp \
util/replace_symbol.cpp \
util/sharing_map.cpp \
util/sharing_node.cpp \
util/simplify_expr.cpp \
util/small_map.cpp \
util/small_shared_two_way_ptr.cpp \
util/std_expr.cpp \
Expand Down
84 changes: 84 additions & 0 deletions unit/util/simplify_expr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*******************************************************************\

Module: Unit tests of the expression simplifier

Author: Michael Tautschnig

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

#include <testing-utils/catch.hpp>

#include <util/arith_tools.h>
#include <util/byte_operators.h>
#include <util/c_types.h>
#include <util/cmdline.h>
#include <util/config.h>
#include <util/namespace.h>
#include <util/pointer_predicates.h>
#include <util/simplify_expr.h>
#include <util/std_expr.h>
#include <util/symbol_table.h>

TEST_CASE("Simplify pointer_offset(address of array index)")
{
config.set_arch("none");

symbol_tablet symbol_table;
namespacet ns(symbol_table);

array_typet array_type(char_type(), from_integer(2, size_type()));
symbol_exprt array("A", array_type);
index_exprt index(array, from_integer(1, index_type()));
address_of_exprt address_of(index);

exprt p_o=pointer_offset(address_of);

exprt simp=simplify_expr(p_o, ns);

REQUIRE(simp.id()==ID_constant);
mp_integer offset_value;
REQUIRE(!to_integer(simp, offset_value));
REQUIRE(offset_value==1);
}

TEST_CASE("Simplify const pointer offset")
{
config.set_arch("none");

symbol_tablet symbol_table;
namespacet ns(symbol_table);

// build a numeric constant of some pointer type
constant_exprt number=from_integer(1234, size_type());
number.type()=pointer_type(char_type());

exprt p_o=pointer_offset(number);

exprt simp=simplify_expr(p_o, ns);

REQUIRE(simp.id()==ID_constant);
mp_integer offset_value;
REQUIRE(!to_integer(simp, offset_value));
REQUIRE(offset_value==1234);
}

TEST_CASE("Simplify byte extract")
{
// this test does require a proper architecture to be set so that byte extract
// uses adequate endianness
cmdlinet cmdline;
config.set(cmdline);

symbol_tablet symbol_table;
namespacet ns(symbol_table);

// byte-extracting type T at offset 0 from an object of type T yields the
// object
symbol_exprt s("foo", size_type());
byte_extract_exprt be(
byte_extract_id(), s, from_integer(0, index_type()), size_type());

exprt simp = simplify_expr(be, ns);

REQUIRE(simp == s);
}