Skip to content

Commit

Permalink
C front-end: place requires and ensures in designated scope
Browse files Browse the repository at this point in the history
These contract clauses may introduce new symbols, which must not
conflict with ones declared in the body of the function.

Fixes: diffblue#8337
  • Loading branch information
tautschnig committed Jul 12, 2024
1 parent 3dcc1c7 commit f008664
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
28 changes: 28 additions & 0 deletions regression/ansi-c/contracts_scope1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#define C 8

typedef unsigned st[C];

// clang-format off
void init_st(st dst)
__CPROVER_requires(__CPROVER_is_fresh(dst, sizeof(st)))
__CPROVER_assigns(__CPROVER_object_whole(dst))
__CPROVER_ensures(__CPROVER_forall {
unsigned i; (0 <= i && i < C) ==> dst[i] == 0
});
// clang-format on

void init_st(st dst)
{
__CPROVER_size_t i;
for(i = 0; i < C; i++)
{
dst[i] = 0;
}
}

int main()
{
st dest;

init_st(dest);
}
8 changes: 8 additions & 0 deletions regression/ansi-c/contracts_scope1/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c

^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
^CONVERSION ERROR$
18 changes: 14 additions & 4 deletions src/ansi-c/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -3337,17 +3337,27 @@ parameter_abstract_declarator:
;

cprover_function_contract:
TOK_CPROVER_ENSURES '(' ACSL_binding_expression ')'
TOK_CPROVER_ENSURES
{
PARSER.new_scope("ensures::");
}
'(' ACSL_binding_expression ')'
{
$$=$1;
set($$, ID_C_spec_ensures);
mto($$, $3);
mto($$, $4);
PARSER.pop_scope();
}
| TOK_CPROVER_REQUIRES '(' ACSL_binding_expression ')'
| TOK_CPROVER_REQUIRES
{
PARSER.new_scope("requires::");
}
'(' ACSL_binding_expression ')'
{
$$=$1;
set($$, ID_C_spec_requires);
mto($$, $3);
mto($$, $4);
PARSER.pop_scope();
}
| cprover_contract_assigns
| cprover_contract_frees
Expand Down

0 comments on commit f008664

Please sign in to comment.