Skip to content

Crangler: add loop invariants with loop numbers #7065

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 1 commit into from
Sep 22, 2022
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
26 changes: 26 additions & 0 deletions regression/crangler/loop-invariant-03/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
int foo(int *arr, int size)
{
arr[0] = 0;
arr[size - 1] = 0;
for(int i = 0; i < 2; i++)
{
arr[i] = 0;
}

int i = 0;
while(i < 2)
{
arr[i] = 0;
i++;
}

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;
}
10 changes: 10 additions & 0 deletions regression/crangler/loop-invariant-03/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CORE
test.json

^\s+while\(i \< 2\) \_\_CPROVER\_loop\_invariant
^EXIT=0$
^SIGNAL=0$
--
^\s+for\(int i = 0; i \< 2; i\+\+\) \_\_CPROVER
--
Annotate loop invariant only to the second loop.
5 changes: 5 additions & 0 deletions regression/crangler/loop-invariant-03/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sources" : ["main.c"],
"functions" : [{"foo": ["loop 2 invariant 1==1"]}],
"output" : "stdout"
}
14 changes: 10 additions & 4 deletions src/crangler/c_wrangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ void c_wranglert::configure_functions(const jsont &config)
function_config.assertions.emplace_back(split[1], rest.str());
}
else if(
(split[0] == "for" && split.size() >= 3 && split[2] == "invariant") ||
(split[0] == "while" && split.size() >= 3 && split[2] == "invariant"))
(split[0] == "for" || split[0] == "while" || split[0] == "loop") &&
split.size() >= 3 && split[2] == "invariant")
{
std::ostringstream rest;
join_strings(rest, split.begin() + 3, split.end(), ' ');
Expand Down Expand Up @@ -434,7 +434,10 @@ static void mangle_function(
{
while_count++;
const auto &invariant =
loop_invariants["while" + std::to_string(while_count)];
loop_invariants.count("while" + std::to_string(while_count))
? loop_invariants["while" + std::to_string(while_count)]
: loop_invariants
["loop" + std::to_string(while_count + for_count)];

if(!invariant.empty())
{
Expand All @@ -449,7 +452,10 @@ static void mangle_function(
{
for_count++;
const auto &invariant =
loop_invariants["for" + std::to_string(for_count)];
loop_invariants.count("for" + std::to_string(for_count))
? loop_invariants["for" + std::to_string(for_count)]
: loop_invariants
["loop" + std::to_string(while_count + for_count)];

if(!invariant.empty())
{
Expand Down