Skip to content

Commit 0939152

Browse files
author
Joel Allred
committed
Refactor get_array: extract get_valid_array_size
1 parent 85909eb commit 0939152

File tree

1 file changed

+48
-15
lines changed

1 file changed

+48
-15
lines changed

src/solvers/strings/string_refinement.cpp

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -951,16 +951,18 @@ void string_refinementt::add_lemma(
951951
prop.l_set_to_true(convert(simple_lemma));
952952
}
953953

954-
/// Get a model of an array and put it in a certain form.
955-
/// If the model is incomplete or if it is too big, return no value.
954+
/// Get a model of the size of the input string.
955+
/// If the size value is not a constant or not a valid integer (size_t),
956+
/// return no value.
956957
/// \param super_get: function returning the valuation of an expression
957958
/// in a model
958959
/// \param ns: namespace
959960
/// \param stream: output stream for warning messages
960961
/// \param arr: expression of type array representing a string
961962
/// \param array_pool: pool of arrays representing strings
962-
/// \return an optional array expression or array_of_exprt
963-
static optionalt<exprt> get_array(
963+
/// \return an optional expression representing the size of the array that can
964+
/// be cast to size_t
965+
static optionalt<exprt> get_valid_array_size(
964966
const std::function<exprt(const exprt &)> &super_get,
965967
const namespacet &ns,
966968
messaget::mstreamt &stream,
@@ -972,42 +974,73 @@ static optionalt<exprt> get_array(
972974
? size_from_pool.value()
973975
: exprt(ID_unknown, arr.length_type());
974976

975-
exprt arr_val = simplify_expr(adjust_if_recursive(super_get(arr), ns), ns);
976977
exprt size_val = super_get(size);
977978
size_val = simplify_expr(size_val, ns);
978-
const typet char_type = arr.type().subtype();
979-
const typet &index_type = size.type();
980979

981980
if(size_val.id() != ID_constant)
982981
{
983-
stream << "(sr::get_array) string of unknown size: " << format(size_val)
984-
<< messaget::eom;
982+
stream << "(sr::get_valid_array_size) string of unknown size: "
983+
<< format(size_val) << messaget::eom;
985984
return {};
986985
}
987986

988987
auto n_opt = numeric_cast<std::size_t>(size_val);
989988
if(!n_opt)
990989
{
991-
stream << "(sr::get_array) size is not valid" << messaget::eom;
990+
stream << "(sr::get_valid_array_size) size is not valid" << messaget::eom;
991+
return {};
992+
}
993+
994+
return size_val;
995+
}
996+
997+
/// Get a model of an array and put it in a certain form.
998+
/// If the model is incomplete or if it is too big, return no value.
999+
/// \param super_get: function returning the valuation of an expression
1000+
/// in a model
1001+
/// \param ns: namespace
1002+
/// \param stream: output stream for warning messages
1003+
/// \param arr: expression of type array representing a string
1004+
/// \param array_pool: pool of arrays representing strings
1005+
/// \return an optional array expression or array_of_exprt
1006+
static optionalt<exprt> get_array(
1007+
const std::function<exprt(const exprt &)> &super_get,
1008+
const namespacet &ns,
1009+
messaget::mstreamt &stream,
1010+
const array_string_exprt &arr,
1011+
const array_poolt &array_pool)
1012+
{
1013+
const auto size =
1014+
get_valid_array_size(super_get, ns, stream, arr, array_pool);
1015+
if(!size.has_value())
1016+
{
9921017
return {};
9931018
}
994-
std::size_t n = *n_opt;
1019+
1020+
const size_t n = numeric_cast<std::size_t>(size.value()).value();
9951021

9961022
if(n > MAX_CONCRETE_STRING_SIZE)
9971023
{
998-
stream << "(sr::get_array) long string (size = " << n << ") " << format(arr)
999-
<< messaget::eom;
1000-
stream << "(sr::get_array) consider reducing max-nondet-string-length so "
1024+
stream << "(sr::get_valid_array_size) long string (size "
1025+
<< " = " << n << ") " << format(arr) << messaget::eom;
1026+
stream << "(sr::get_valid_array_size) consider reducing "
1027+
"max-nondet-string-length so "
10011028
"that no string exceeds "
10021029
<< MAX_CONCRETE_STRING_SIZE
10031030
<< " in length and "
10041031
"make sure all functions returning strings are loaded"
10051032
<< messaget::eom;
1006-
stream << "(sr::get_array) this can also happen on invalid object access"
1033+
stream << "(sr::get_valid_array_size) this can also happen on invalid "
1034+
"object access"
10071035
<< messaget::eom;
10081036
return nil_exprt();
10091037
}
10101038

1039+
const exprt arr_val =
1040+
simplify_expr(adjust_if_recursive(super_get(arr), ns), ns);
1041+
const typet char_type = arr.type().subtype();
1042+
const typet &index_type = size.value().type();
1043+
10111044
if(
10121045
const auto &array = interval_sparse_arrayt::of_expr(
10131046
arr_val, from_integer(CHARACTER_FOR_UNKNOWN, char_type)))

0 commit comments

Comments
 (0)