Skip to content

Commit bffa2bc

Browse files
committed
Implement simplify_expr() for charAt()
This implements simplify_expr() for function application expressions with id ID_cprover_string_char_at_func.
1 parent 9a77a6e commit bffa2bc

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Main {
2+
public void constantCharAt() {
3+
StringBuilder sb = new StringBuilder("abc");
4+
char c = sb.charAt(1);
5+
assert c == 'b';
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
Main.class
3+
--function Main.constantCharAt --show-vcc
4+
^Generated [0-9]+ VCC\(s\), 0 remaining after simplification$
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
--

src/util/simplify_expr.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,45 @@ bool simplify_exprt::simplify_function_application(exprt &expr)
235235
expr = from_integer(is_prefix ? 1 : 0, expr.type());
236236
return false;
237237
}
238+
else if(func_id == ID_cprover_string_char_at_func)
239+
{
240+
if(function_app.arguments().at(1).id() != ID_constant)
241+
{
242+
return true;
243+
}
244+
245+
const auto &index = to_constant_expr(function_app.arguments().at(1));
246+
247+
const refined_string_exprt &s =
248+
to_string_expr(function_app.arguments().at(0));
249+
250+
const auto char_seq_opt = try_get_string_data_array(s, ns);
251+
252+
if(!char_seq_opt)
253+
{
254+
return true;
255+
}
256+
257+
const array_exprt &char_seq = char_seq_opt->get();
258+
259+
const auto i_opt = numeric_cast<std::size_t>(index);
260+
261+
if(!i_opt || *i_opt >= char_seq.operands().size())
262+
{
263+
return true;
264+
}
265+
266+
const auto &c = to_constant_expr(char_seq.operands().at(*i_opt));
267+
268+
if(c.type() != expr.type())
269+
{
270+
return true;
271+
}
272+
273+
expr = c;
274+
275+
return false;
276+
}
238277

239278
return true;
240279
}

0 commit comments

Comments
 (0)