Skip to content

Crangler: support semicolon in post-declarator expressions #6986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
int foo(int *arr, int size);

int foo(int *arr, int size)
// clang-format off
__CPROVER_requires(size > 0 && __CPROVER_is_fresh(arr, size))
__CPROVER_assigns(
arr[0], arr[size-1];
size >= 10: arr[5];
)
__CPROVER_ensures(arr[0] == 0 && arr[size-1] == 0)
__CPROVER_ensures(size >= 10 ==> arr[5] == __CPROVER_return_value)
// clang-format on
;

int foo(int *arr, int size)
{
arr[0] = 0;
arr[size - 1] = 0;
return size < 10 ? 0 : arr[5];
}

int main()
{
int arr[10];
int retval = foo(arr, 10);
__CPROVER_assert(retval == arr[5], "should succeed");
return 0;
}
9 changes: 9 additions & 0 deletions regression/crangler/assigns-clause-syntax/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
test.json

__CPROVER_assigns
^EXIT=0$
^SIGNAL=0$
--
--
Make sure the semicolon in assigns clauses does not trip up parsing.
6 changes: 6 additions & 0 deletions regression/crangler/assigns-clause-syntax/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sources": [
"main-contract-after-declaration.c"
],
"output": "stdout"
}
15 changes: 15 additions & 0 deletions src/crangler/mini_c_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,27 @@ mini_c_parsert::tokenst mini_c_parsert::parse_post_declarator()
// 3) '=' (initializer)

tokenst result;
std::size_t open_parentheses = 0;

while(true)
{
if(eof())
return result;

if(peek() == '(')
{
++open_parentheses;
result.push_back(consume_token());
continue;
}
else if(open_parentheses > 0)
{
if(peek() == ')')
--open_parentheses;
result.push_back(consume_token());
continue;
}

if(peek() == ';' || peek() == '{' || peek() == '=')
return result;

Expand Down