Skip to content

Commit 74144fc

Browse files
Handle if_exprt in add_axioms_for_string_literal
cprover_string_literal was requiring a string constant as argument, but we now deal with if expressions as well. So expressions like: `cprover_string_literal_func!0#0(cprover_string_length$3!0@1#2, nondet_infinite_array$5!0@1, this!0@1#1 == &tmp_object_factory$17!0 ? tmp_object_factory$17!0#4.@class_identifier : this$object#0.@class_identifier)` are correctly handled by the string solver.
1 parent c6c1b3f commit 74144fc

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/solvers/refinement/string_constraint_generator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ class string_constraint_generatort final
199199
exprt add_axioms_for_insert_char(const function_application_exprt &f);
200200
exprt add_axioms_for_insert_float(const function_application_exprt &f);
201201
exprt add_axioms_for_insert_double(const function_application_exprt &f);
202+
203+
exprt add_axioms_for_cprover_string(
204+
const array_string_exprt &res,
205+
const exprt &arg,
206+
const exprt &guard);
202207
exprt add_axioms_from_literal(const function_application_exprt &f);
203208
exprt add_axioms_from_int(const function_application_exprt &f);
204209
exprt add_axioms_from_int(

src/solvers/refinement/string_constraint_generator_constants.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,39 @@ exprt string_constraint_generatort::add_axioms_for_empty_string(
6767
return from_integer(0, get_return_code_type());
6868
}
6969

70+
/// Convert an expression of type string_typet to a string_exprt
71+
/// \param res: string expression for the result
72+
/// \param arg: expression of type string typet
73+
/// \param guard: condition under which `res` should be equal to arg
74+
/// \return 0 if constraints were added, 1 if expression could not be handled
75+
/// and no constraint was added. Expression we can handle are of the
76+
/// form \f$ e := "<string constant>" | (expr)? e : e \f$
77+
exprt string_constraint_generatort::add_axioms_for_cprover_string(
78+
const array_string_exprt &res,
79+
const exprt &arg,
80+
const exprt &guard)
81+
{
82+
if(const auto if_expr = expr_try_dynamic_cast<if_exprt>(arg))
83+
{
84+
const and_exprt guard_true(guard, if_expr->cond());
85+
const exprt return_code_true =
86+
add_axioms_for_cprover_string(res, if_expr->true_case(), guard_true);
87+
88+
const and_exprt guard_false(guard, not_exprt(if_expr->cond()));
89+
const exprt return_code_false =
90+
add_axioms_for_cprover_string(res, if_expr->false_case(), guard_false);
91+
92+
return if_exprt(
93+
equal_exprt(return_code_true, from_integer(0, get_return_code_type())),
94+
return_code_false,
95+
return_code_true);
96+
}
97+
else if(const auto constant_expr = expr_try_dynamic_cast<constant_exprt>(arg))
98+
return add_axioms_for_constant(res, constant_expr->get_value(), guard);
99+
else
100+
return from_integer(1, get_return_code_type());
101+
}
102+
70103
/// String corresponding to an internal cprover string
71104
///
72105
/// Add axioms ensuring that the returned string expression is equal to the
@@ -81,7 +114,5 @@ exprt string_constraint_generatort::add_axioms_from_literal(
81114
const function_application_exprt::argumentst &args=f.arguments();
82115
PRECONDITION(args.size() == 3); // Bad args to string literal?
83116
const array_string_exprt res = char_array_of_pointer(args[1], args[0]);
84-
const exprt &arg = args[2];
85-
irep_idt sval=to_constant_expr(arg).get_value();
86-
return add_axioms_for_constant(res, sval);
117+
return add_axioms_for_cprover_string(res, args[2], true_exprt());
87118
}

0 commit comments

Comments
 (0)