Skip to content

Commit 9060de1

Browse files
author
Remi Delmas
committed
Function contracts: allow history variables for typecast and constant expressions.
1 parent 95d8c91 commit 9060de1

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
3+
int foo(int l) __CPROVER_requires(-10 <= l && l <= 10) __CPROVER_ensures(
4+
__CPROVER_return_value == __CPROVER_old(l) + __CPROVER_old(10))
5+
{
6+
return l + 10;
7+
}
8+
9+
int main()
10+
{
11+
int l;
12+
__CPROVER_assume(-10 <= l && l <= 10);
13+
foo(l);
14+
return 0;
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--enforce-contract foo
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^Tracking history of constant expressions is not supported yet
9+
--
10+
This test checks that history variables are supported for constant expressions.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
3+
long bar(long l, long r) __CPROVER_requires(-10 <= l && l <= 10)
4+
__CPROVER_requires(-10 <= r && r <= 10) __CPROVER_ensures(
5+
__CPROVER_return_value == __CPROVER_old(l) + __CPROVER_old(r))
6+
{
7+
return l + r;
8+
}
9+
10+
int foo(int l, int r) __CPROVER_requires(-10 <= l && l <= 10)
11+
__CPROVER_requires(-10 <= r && r <= 10) __CPROVER_ensures(
12+
__CPROVER_return_value == __CPROVER_old(l) + __CPROVER_old(r))
13+
{
14+
return bar((long)l, (long)r);
15+
}
16+
17+
int main()
18+
{
19+
int n;
20+
__CPROVER_assume(-10 <= n && n <= 10);
21+
foo(n, n);
22+
return 0;
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--replace-call-with-contract bar --enforce-contract foo
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^Tracking history of typecast expressions is not supported yet
9+
--
10+
This test checks that history variables are supported for typecast expressions.

src/goto-instrument/contracts/contracts.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,10 @@ void code_contractst::replace_history_parameter(
562562
{
563563
const auto &parameter = to_history_expr(expr, id).expression();
564564

565+
const auto &id = parameter.id();
565566
if(
566-
parameter.id() == ID_dereference || parameter.id() == ID_member ||
567-
parameter.id() == ID_symbol || parameter.id() == ID_ptrmember)
567+
id == ID_dereference || id == ID_member || id == ID_symbol ||
568+
id == ID_ptrmember || id == ID_constant || id == ID_typecast)
568569
{
569570
auto it = parameter2history.find(parameter);
570571

0 commit comments

Comments
 (0)