-
Notifications
You must be signed in to change notification settings - Fork 276
Bug fixes in String builtin functions [TG-2459] #1958
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
romainbrenguier
merged 5 commits into
diffblue:develop
from
romainbrenguier:bugfix/insert-offset
Mar 23, 2018
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4cca831
Move string_builtin_function class to a new file
romainbrenguier 71983b3
Remove mention of size in eval_string
romainbrenguier aebadee
Correct offset in eval for insert
romainbrenguier 3ef9ec8
Correct for_each_successor method
romainbrenguier c5aa507
Document builtin function constructors
romainbrenguier 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
/// Module: String solver | ||
/// Author: Diffblue Ltd. | ||
|
||
#include "string_builtin_function.h" | ||
|
||
#include <algorithm> | ||
#include "string_constraint_generator.h" | ||
|
||
/// Get the valuation of the string, given a valuation | ||
static optionalt<std::vector<mp_integer>> eval_string( | ||
const array_string_exprt &a, | ||
const std::function<exprt(const exprt &)> &get_value); | ||
|
||
/// Make a string from a constant array | ||
static array_string_exprt make_string( | ||
const std::vector<mp_integer> &array, | ||
const array_typet &array_type); | ||
|
||
string_transformation_builtin_functiont:: | ||
string_transformation_builtin_functiont( | ||
const std::vector<exprt> &fun_args, | ||
array_poolt &array_pool) | ||
{ | ||
PRECONDITION(fun_args.size() > 2); | ||
const auto arg1 = expr_checked_cast<struct_exprt>(fun_args[2]); | ||
input = array_pool.find(arg1.op1(), arg1.op0()); | ||
result = array_pool.find(fun_args[1], fun_args[0]); | ||
args.insert(args.end(), fun_args.begin() + 3, fun_args.end()); | ||
} | ||
|
||
optionalt<exprt> string_transformation_builtin_functiont::eval( | ||
const std::function<exprt(const exprt &)> &get_value) const | ||
{ | ||
const auto &input_value = eval_string(input, get_value); | ||
if(!input_value.has_value()) | ||
return {}; | ||
|
||
std::vector<mp_integer> arg_values; | ||
const auto &insert = std::back_inserter(arg_values); | ||
const mp_integer unknown('?'); | ||
std::transform( | ||
args.begin(), args.end(), insert, [&](const exprt &e) { // NOLINT | ||
if(const auto val = numeric_cast<mp_integer>(get_value(e))) | ||
return *val; | ||
INVARIANT( | ||
get_value(e).id() == ID_unknown, | ||
"array valuation should only contain constants and unknown"); | ||
return unknown; | ||
}); | ||
|
||
const auto result_value = eval(*input_value, arg_values); | ||
const auto length = from_integer(result_value.size(), result.length().type()); | ||
const array_typet type(result.type().subtype(), length); | ||
return make_string(result_value, type); | ||
} | ||
|
||
string_insertion_builtin_functiont::string_insertion_builtin_functiont( | ||
const std::vector<exprt> &fun_args, | ||
array_poolt &array_pool) | ||
{ | ||
PRECONDITION(fun_args.size() > 4); | ||
const auto arg1 = expr_checked_cast<struct_exprt>(fun_args[2]); | ||
input1 = array_pool.find(arg1.op1(), arg1.op0()); | ||
const auto arg2 = expr_checked_cast<struct_exprt>(fun_args[4]); | ||
input2 = array_pool.find(arg2.op1(), arg2.op0()); | ||
result = array_pool.find(fun_args[1], fun_args[0]); | ||
args.push_back(fun_args[3]); | ||
args.insert(args.end(), fun_args.begin() + 5, fun_args.end()); | ||
} | ||
|
||
string_concatenation_builtin_functiont::string_concatenation_builtin_functiont( | ||
const std::vector<exprt> &fun_args, | ||
array_poolt &array_pool) | ||
{ | ||
PRECONDITION(fun_args.size() >= 4 && fun_args.size() <= 6); | ||
const auto arg1 = expr_checked_cast<struct_exprt>(fun_args[2]); | ||
input1 = array_pool.find(arg1.op1(), arg1.op0()); | ||
const auto arg2 = expr_checked_cast<struct_exprt>(fun_args[3]); | ||
input2 = array_pool.find(arg2.op1(), arg2.op0()); | ||
result = array_pool.find(fun_args[1], fun_args[0]); | ||
args.insert(args.end(), fun_args.begin() + 4, fun_args.end()); | ||
} | ||
|
||
optionalt<std::vector<mp_integer>> eval_string( | ||
const array_string_exprt &a, | ||
const std::function<exprt(const exprt &)> &get_value) | ||
{ | ||
if(a.id() == ID_if) | ||
{ | ||
const if_exprt &ite = to_if_expr(a); | ||
const exprt cond = get_value(ite.cond()); | ||
if(!cond.is_constant()) | ||
return {}; | ||
return cond.is_true() | ||
? eval_string(to_array_string_expr(ite.true_case()), get_value) | ||
: eval_string(to_array_string_expr(ite.false_case()), get_value); | ||
} | ||
|
||
const exprt content = get_value(a.content()); | ||
const auto &array = expr_try_dynamic_cast<array_exprt>(content); | ||
if(!array) | ||
return {}; | ||
|
||
const auto &ops = array->operands(); | ||
std::vector<mp_integer> result; | ||
const mp_integer unknown('?'); | ||
const auto &insert = std::back_inserter(result); | ||
std::transform( | ||
ops.begin(), ops.end(), insert, [unknown](const exprt &e) { // NOLINT | ||
if(const auto i = numeric_cast<mp_integer>(e)) | ||
return *i; | ||
return unknown; | ||
}); | ||
return result; | ||
} | ||
|
||
array_string_exprt | ||
make_string(const std::vector<mp_integer> &array, const array_typet &array_type) | ||
{ | ||
const typet &char_type = array_type.subtype(); | ||
array_exprt array_expr(array_type); | ||
const auto &insert = std::back_inserter(array_expr.operands()); | ||
std::transform( | ||
array.begin(), array.end(), insert, [&](const mp_integer &i) { // NOLINT | ||
return from_integer(i, char_type); | ||
}); | ||
return to_array_string_expr(array_expr); | ||
} | ||
|
||
std::vector<mp_integer> string_concatenation_builtin_functiont::eval( | ||
const std::vector<mp_integer> &input1_value, | ||
const std::vector<mp_integer> &input2_value, | ||
const std::vector<mp_integer> &args_value) const | ||
{ | ||
const auto start_index = | ||
args_value.size() > 0 && args_value[0] > 0 ? args_value[0] : mp_integer(0); | ||
const mp_integer input2_size(input2_value.size()); | ||
const auto end_index = | ||
args_value.size() > 1 | ||
? std::max(std::min(args_value[1], input2_size), start_index) | ||
: input2_size; | ||
|
||
std::vector<mp_integer> result(input1_value); | ||
result.insert( | ||
result.end(), | ||
input2_value.begin() + numeric_cast_v<std::size_t>(start_index), | ||
input2_value.begin() + numeric_cast_v<std::size_t>(end_index)); | ||
return result; | ||
} | ||
|
||
std::vector<mp_integer> string_concat_char_builtin_functiont::eval( | ||
const std::vector<mp_integer> &input_value, | ||
const std::vector<mp_integer> &args_value) const | ||
{ | ||
PRECONDITION(args_value.size() == 1); | ||
std::vector<mp_integer> result(input_value); | ||
result.push_back(args_value[0]); | ||
return result; | ||
} | ||
|
||
std::vector<mp_integer> string_insertion_builtin_functiont::eval( | ||
const std::vector<mp_integer> &input1_value, | ||
const std::vector<mp_integer> &input2_value, | ||
const std::vector<mp_integer> &args_value) const | ||
{ | ||
PRECONDITION(args_value.size() >= 1 || args_value.size() <= 3); | ||
const auto offset = std::min( | ||
std::max(args_value[0], mp_integer(0)), mp_integer(input1_value.size())); | ||
const auto start = args_value.size() > 1 | ||
? std::max(args_value[1], mp_integer(0)) | ||
: mp_integer(0); | ||
|
||
const mp_integer input2_size(input2_value.size()); | ||
const auto end = | ||
args_value.size() > 2 | ||
? std::max(std::min(args_value[2], input2_size), mp_integer(0)) | ||
: input2_size; | ||
|
||
std::vector<mp_integer> result(input1_value); | ||
result.insert( | ||
result.begin() + numeric_cast_v<std::size_t>(offset), | ||
input2_value.begin() + numeric_cast_v<std::size_t>(start), | ||
input2_value.begin() + numeric_cast_v<std::size_t>(end)); | ||
return result; | ||
} | ||
|
||
optionalt<exprt> string_insertion_builtin_functiont::eval( | ||
const std::function<exprt(const exprt &)> &get_value) const | ||
{ | ||
const auto &input1_value = eval_string(input1, get_value); | ||
const auto &input2_value = eval_string(input2, get_value); | ||
if(!input2_value.has_value() || !input1_value.has_value()) | ||
return {}; | ||
|
||
std::vector<mp_integer> arg_values; | ||
const auto &insert = std::back_inserter(arg_values); | ||
const mp_integer unknown('?'); | ||
std::transform( | ||
args.begin(), args.end(), insert, [&](const exprt &e) { // NOLINT | ||
if(const auto val = numeric_cast<mp_integer>(get_value(e))) | ||
return *val; | ||
return unknown; | ||
}); | ||
|
||
const auto result_value = eval(*input1_value, *input2_value, arg_values); | ||
const auto length = from_integer(result_value.size(), result.length().type()); | ||
const array_typet type(result.type().subtype(), length); | ||
return make_string(result_value, type); | ||
} |
Oops, something went wrong.
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.
Could you document the magic indices and offsets please? A brief layout of the vectors would help.
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.
@peterschrammel I now document the constructor in the header, hopefully that should be clearer.