-
Notifications
You must be signed in to change notification settings - Fork 273
Fixing simplification of LHS #1070
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/*******************************************************************\ | ||
|
||
Module: Unit tests for ai_domain_baset::ai_simplify_lhs | ||
|
||
Author: DiffBlue Limited. All rights reserved. | ||
|
||
\*******************************************************************/ | ||
|
||
/// \file | ||
/// Unit tests for ai_domain_baset::ai_simplify_lhs | ||
|
||
#include <catch.hpp> | ||
|
||
#include <analyses/ai.h> | ||
|
||
#include <ansi-c/ansi_c_language.h> | ||
|
||
#include <util/arith_tools.h> | ||
#include <util/config.h> | ||
#include <util/namespace.h> | ||
#include <util/ui_message.h> | ||
#include <util/simplify_expr.h> | ||
|
||
class constant_simplification_mockt:public ai_domain_baset | ||
{ | ||
public: | ||
void transform(locationt, locationt, ai_baset &, const namespacet &) override | ||
{} | ||
void make_bottom() override | ||
{} | ||
void make_top() override | ||
{} | ||
void make_entry() override | ||
{} | ||
|
||
bool ai_simplify(exprt &condition, const namespacet &ns) const override; | ||
}; | ||
|
||
bool constant_simplification_mockt::ai_simplify( | ||
exprt &condition, const namespacet &ns) const | ||
{ | ||
exprt simplified_expr=simplify_expr(condition, ns); | ||
// no simplification | ||
if(simplified_expr==condition) | ||
{ | ||
return true; | ||
} | ||
// a simplification has occurred | ||
condition=simplified_expr; | ||
return false; | ||
} | ||
|
||
SCENARIO("ai_domain_baset::ai_simplify_lhs", | ||
"[core][analyses][ai][ai_simplify_lhs]") | ||
{ | ||
ui_message_handlert message_handler; | ||
ansi_c_languaget language; | ||
language.set_message_handler(message_handler); | ||
|
||
symbol_tablet symbol_table; | ||
namespacet ns(symbol_table); | ||
|
||
constant_simplification_mockt mock_ai_domain; | ||
|
||
config.set_arch("none"); | ||
|
||
GIVEN("A index_exprt") | ||
{ | ||
// Construct an expression that the simplify_expr can simplify | ||
exprt simplifiable_expression; | ||
bool compile_failed= | ||
language.to_expr("1 + 1", "", simplifiable_expression, ns); | ||
|
||
const unsigned int array_size=5; | ||
array_typet array_type( | ||
signedbv_typet(32), constant_exprt::integer_constant(array_size)); | ||
|
||
// Verify the results of the setup | ||
REQUIRE_FALSE(compile_failed);\ | ||
REQUIRE(simplifiable_expression.id()==ID_plus); | ||
exprt simplified_version=simplify_expr(simplifiable_expression, ns); | ||
REQUIRE(simplified_version.id()==ID_constant); | ||
|
||
WHEN( | ||
"Simplifying an index expression with constant index but variable array") | ||
{ | ||
const index_exprt &index_expr= | ||
index_exprt(symbol_exprt("a", array_type), simplifiable_expression); | ||
|
||
THEN("Then only the index of the part of the expression should be " | ||
"simplified") | ||
{ | ||
exprt out_expr=index_expr; | ||
bool no_simplification=mock_ai_domain.ai_simplify_lhs(out_expr, ns); | ||
REQUIRE_FALSE(no_simplification); | ||
REQUIRE(index_expr.id()==ID_index); | ||
|
||
index_exprt simplified_index_expr=to_index_expr(out_expr); | ||
REQUIRE(simplified_index_expr.index().id()==ID_constant); | ||
|
||
constant_exprt constant_index= | ||
to_constant_expr(simplified_index_expr.index()); | ||
|
||
mp_integer out_index; | ||
bool failed_to_integer=to_integer(constant_index, out_index); | ||
REQUIRE_FALSE(failed_to_integer); | ||
REQUIRE(out_index==2); | ||
} | ||
} | ||
WHEN("Simplifying an index expression with variable index and array") | ||
{ | ||
// a[i] | ||
const index_exprt &index_expr= | ||
index_exprt( | ||
symbol_exprt("a", array_type), symbol_exprt("i", signedbv_typet(32))); | ||
|
||
THEN("Then no simplification should occur") | ||
{ | ||
exprt out_expr=index_expr; | ||
bool no_simplification=mock_ai_domain.ai_simplify_lhs(out_expr, ns); | ||
REQUIRE(no_simplification); | ||
REQUIRE(index_expr.id()==ID_index); | ||
|
||
index_exprt simplified_index_expr=to_index_expr(out_expr); | ||
REQUIRE(simplified_index_expr.index().id()==ID_symbol); | ||
} | ||
} | ||
|
||
// This fails since the implementation does do a constant simplification | ||
// on the array part. It isn't clear to me if this is correct | ||
#if 0 | ||
WHEN( | ||
"Simplifying an index expression with constant index in a constant array") | ||
{ | ||
array_exprt constant_array=array_exprt(array_type); | ||
for(unsigned int i=0; i<array_size; ++i) | ||
{ | ||
REQUIRE(constant_array.operands().size()==i); | ||
constant_array.operands().push_back( | ||
constant_exprt::integer_constant(i)); | ||
} | ||
|
||
const index_exprt &index_expr= | ||
index_exprt(constant_array, simplifiable_expression); | ||
|
||
THEN("Then only the index of the part of the expression should be " | ||
"simplified") | ||
{ | ||
exprt out_expr=index_expr; | ||
bool no_simplification=mock_ai_domain.ai_simplify_lhs(out_expr, ns); | ||
REQUIRE_FALSE(no_simplification); | ||
REQUIRE(out_expr.id()==ID_index); | ||
|
||
index_exprt simplified_index_expr=to_index_expr(out_expr); | ||
REQUIRE(simplified_index_expr.index().id()==ID_constant); | ||
|
||
constant_exprt constant_index= | ||
to_constant_expr(simplified_index_expr.index()); | ||
|
||
mp_integer out_index; | ||
bool failed_to_integer=to_integer(constant_index, out_index); | ||
REQUIRE_FALSE(failed_to_integer); | ||
REQUIRE(out_index==2); | ||
} | ||
} | ||
#endif | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fails since the implementation does do a constant simplification on the array part. It isn't clear to me if this is correct . This somewhat limits I can test (if it really is supposed to be using constant simplification on all parts, can't test isn't inadvertently
ai_simplify
ing bits it shouldn't be.