Skip to content

Commit e0ccfc8

Browse files
Merge pull request #7557 from remi-delmas-3000/contrracts-do-while-syntax
CONTRACTS: place contracts after do for do-while loops
2 parents b04cbf3 + bd4d4e0 commit e0ccfc8

File tree

7 files changed

+70
-13
lines changed

7 files changed

+70
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
int main()
2+
{
3+
int i = 0;
4+
do
5+
{
6+
i++;
7+
} while(i < 10)
8+
// clang-format off
9+
__CPROVER_assigns(i)
10+
__CPROVER_loop_invariant(0 <= i && i <= 10)
11+
__CPROVER_decreases(20 - i)
12+
// clang-format on
13+
;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
4+
^PARSING ERROR$
5+
^EXIT=(1|64)$
6+
^SIGNAL=0$
7+
--
8+
--
9+
Checks that loop contracts for do-while loops cannot be attached after
10+
the `while` keyword.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
int main()
2+
{
3+
int i = 0;
4+
do
5+
// clang-format off
6+
__CPROVER_assigns(i)
7+
__CPROVER_loop_invariant(0 <= i && i <= 10)
8+
__CPROVER_decreases(20 - i)
9+
// clang-format on
10+
{
11+
i++;
12+
}
13+
while(i < 10);
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
4+
^VERIFICATION SUCCESSFUL$
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
--
9+
Checks that loop contracts for do-while loops can be attached after the `do`
10+
keyword.

regression/contracts/loop_contracts_do_while/main.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ int main()
55
int x = 0;
66

77
do
8-
{
9-
x++;
10-
} while(x < 10) __CPROVER_loop_invariant(0 <= x && x <= 10);
8+
// clang-format off
9+
__CPROVER_assigns(x)
10+
__CPROVER_loop_invariant(0 <= x && x <= 10)
11+
__CPROVER_decreases(20 - x)
12+
// clang-format on
13+
{
14+
x++;
15+
}
16+
while(x < 10);
1117

1218
assert(x == 10);
1319
}

regression/contracts/loop_contracts_do_while/test.desc

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ main.c
77
--
88
--
99
This test checks that loop contracts work correctly on do/while loops.
10+
Fails because contracts are not yet supported on do while loops.

src/ansi-c/parser.y

+12-10
Original file line numberDiff line numberDiff line change
@@ -2493,23 +2493,25 @@ iteration_statement:
24932493
if(!parser_stack($7).operands().empty())
24942494
static_cast<exprt &>(parser_stack($$).add(ID_C_spec_decreases)).operands().swap(parser_stack($7).operands());
24952495
}
2496-
| TOK_DO statement TOK_WHILE '(' comma_expression ')'
2496+
| TOK_DO
24972497
cprover_contract_assigns_opt
2498-
cprover_contract_loop_invariant_list_opt
2499-
cprover_contract_decreases_opt ';'
2498+
cprover_contract_loop_invariant_list_opt
2499+
cprover_contract_decreases_opt
2500+
statement
2501+
TOK_WHILE '(' comma_expression ')' ';'
25002502
{
25012503
$$=$1;
25022504
statement($$, ID_dowhile);
2503-
parser_stack($$).add_to_operands(std::move(parser_stack($5)), std::move(parser_stack($2)));
2505+
parser_stack($$).add_to_operands(std::move(parser_stack($8)), std::move(parser_stack($5)));
25042506

2505-
if(!parser_stack($7).operands().empty())
2506-
static_cast<exprt &>(parser_stack($$).add(ID_C_spec_assigns)).operands().swap(parser_stack($7).operands());
2507+
if(!parser_stack($2).operands().empty())
2508+
static_cast<exprt &>(parser_stack($$).add(ID_C_spec_assigns)).operands().swap(parser_stack($2).operands());
25072509

2508-
if(!parser_stack($8).operands().empty())
2509-
static_cast<exprt &>(parser_stack($$).add(ID_C_spec_loop_invariant)).operands().swap(parser_stack($8).operands());
2510+
if(!parser_stack($3).operands().empty())
2511+
static_cast<exprt &>(parser_stack($$).add(ID_C_spec_loop_invariant)).operands().swap(parser_stack($3).operands());
25102512

2511-
if(!parser_stack($9).operands().empty())
2512-
static_cast<exprt &>(parser_stack($$).add(ID_C_spec_decreases)).operands().swap(parser_stack($9).operands());
2513+
if(!parser_stack($4).operands().empty())
2514+
static_cast<exprt &>(parser_stack($$).add(ID_C_spec_decreases)).operands().swap(parser_stack($4).operands());
25132515
}
25142516
| TOK_FOR
25152517
{

0 commit comments

Comments
 (0)