Skip to content

Commit 3e2cbc5

Browse files
author
Enrico Steffinlongo
authored
Merge pull request #8001 from esteffin/esteffin/fix-failed-coverage-if-then-else
Fix failed coverage in if then else with return statements
2 parents 5cfcbc4 + 701bf9c commit 3e2cbc5

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

regression/cbmc-cover/block-coverage-report1/test.desc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ main.c
33
--cover location
44
block 1 \(lines main.c:main:3,5\): SATISFIED
55
block 2 \(lines main.c:main:6\): SATISFIED
6-
block 3 \(lines main.c:main:6,9\): FAILED
7-
block 4 \(lines main.c:main:8\): SATISFIED
8-
block 5 \(lines main.c:main:9\): SATISFIED
6+
block 3 \(lines main.c:main:8\): SATISFIED
7+
block 4 \(lines main.c:main:9\): SATISFIED
98
^EXIT=0$
109
^SIGNAL=0$
1110
--

regression/cbmc-cover/built-ins1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ main.c
33
--cover location --unwind 1
44
^EXIT=0$
55
^SIGNAL=0$
6-
^\*\* 5 of 8 covered
6+
^\*\* 5 of 7 covered
77
--
88
^warning: ignoring
99
^\[.*<builtin-library-

regression/cbmc/destructors/compound_literal.desc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CORE
22
main.c
33
--unwind 10 --show-goto-functions
44
activate-multi-line-match
5-
(?P<comment_block>\/\/ [0-9]+ file main\.c line [0-9]+ function main)[\s]*DEAD .*newAlloc0[\s]*(?P>comment_block)[\s]*DEAD .*pc[\s]*(?P>comment_block)[\s]*DEAD .*literal[\s]*(?P>comment_block)[\s]*9: END_FUNCTION
5+
(?P<comment_block>\/\/ [0-9]+ file main\.c line [0-9]+ function main)[\s]*DEAD .*newAlloc0[\s]*(?P>comment_block)[\s]*DEAD .*pc[\s]*(?P>comment_block)[\s]*DEAD .*literal[\s]*(?P>comment_block)[\s]*8: END_FUNCTION
66
^EXIT=0$
77
^SIGNAL=0$
88
--
@@ -16,7 +16,7 @@ Checks for:
1616
// 51 file main.c line 44 function main
1717
DEAD main::$tmp::literal
1818
// 52 file main.c line 45 function main
19-
9: END_FUNCTION
19+
8: END_FUNCTION
2020

2121
This asserts that when you've created a compound literal that both temp and real
2222
variable gets killed.

regression/cbmc/destructors/enter_lexical_block.desc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ CORE
22
main.c
33
--unwind 10 --show-goto-functions
44
activate-multi-line-match
5-
(?P<comment_block>\/\/ [0-9]+ file main\.c line [0-9]+ function main)[\s]*6: .*newAlloc4 : struct tag-test[\s]*(?P>comment_block)[\s]*.*newAlloc4 := \{ 4 \}[\s]*(?P>comment_block)[\s]*.*newAlloc6 : struct tag-test[\s]*(?P>comment_block)[\s]*.*newAlloc6 := \{ 6 \}[\s]*(?P>comment_block)[\s]*.*newAlloc7 : struct tag-test[\s]*(?P>comment_block)[\s]*.*newAlloc7 := \{ 7 \}[\s]*(?P>comment_block)[\s]*GOTO 3
5+
(?P<comment_block>\/\/ [0-9]+ file main\.c line [0-9]+ function main)[\s]*5: .*newAlloc4 : struct tag-test[\s]*(?P>comment_block)[\s]*.*newAlloc4 := \{ 4 \}[\s]*(?P>comment_block)[\s]*.*newAlloc6 : struct tag-test[\s]*(?P>comment_block)[\s]*.*newAlloc6 := \{ 6 \}[\s]*(?P>comment_block)[\s]*.*newAlloc7 : struct tag-test[\s]*(?P>comment_block)[\s]*.*newAlloc7 := \{ 7 \}[\s]*(?P>comment_block)[\s]*GOTO 3
66
^EXIT=0$
77
^SIGNAL=0$
88
--
99
--
1010
Checks for:
1111

1212
// 37 file main.c line 36 function main
13-
6: DECL main::1::2::2::newAlloc4 : struct tag-test
13+
5: DECL main::1::2::2::newAlloc4 : struct tag-test
1414
// 38 file main.c line 36 function main
1515
ASSIGN main::1::2::2::newAlloc4 := { 4 }
1616
// 39 file main.c line 37 function main

regression/cprover/loops/while-break1.desc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ while-break1.c
33
--text --solve --inline
44
^EXIT=0$
55
^SIGNAL=0$
6-
^\(\d+\) ∀ ς : state \. S18\(ς\) ⇒ S12in\(ς\)$
6+
^\(\d+\) ∀ ς : state \. S15T\(ς\) ⇒ S12in\(ς\)$
77
^\(\d+\) ∀ ς : state \. S11\(ς\) ⇒ S12in\(ς\)$
88
^\(\d+\) ∀ ς : state \. \(S12in\(ς\) ∧ ¬\(1 ≠ 0\)\) ⇒ S12T\(ς\)$
99
^\(\d+\) ∀ ς : state \. \(S12in\(ς\) ∧ 1 ≠ 0\) ⇒ S12\(ς\)$
10-
^\(\d+\) ∀ ς : state \. \(S12\(ς\) ∧ ¬\(ς\(❝main::1::x❞\) = 0\)\) ⇒ S13T\(ς\)$
11-
^\(\d+\) ∀ ς : state \. \(S12\(ς\) ∧ ς\(❝main::1::x❞\) = 0\) ⇒ S13\(ς\)$
12-
^\(\d+\) ∀ ς : state \. S12T\(ς\) ⇒ S19in\(ς\)$
13-
^\(\d+\) ∀ ς : state \. S14\(ς\) ⇒ S19in\(ς\)$
14-
^\(\d+\) ∀ ς : state \. S16\(ς\) ⇒ S19in\(ς\)$
10+
^\(\d+\) ∀ ς : state \. \(S12\(ς\) ∧ ς\(❝main::1::x❞\) = 0\) ⇒ S13T\(ς\)$
11+
^\(\d+\) ∀ ς : state \. \(S12\(ς\) ∧ ¬\(ς\(❝main::1::x❞\) = 0\)\) ⇒ S13\(ς\)$
12+
^\(\d+\) ∀ ς : state \. S12T\(ς\) ⇒ S17in\(ς\)$
13+
^\(\d+\) ∀ ς : state \. S13T\(ς\) ⇒ S17in\(ς\)$
14+
^\(\d+\) ∀ ς : state \. S16\(ς\) ⇒ S17in\(ς\)$
1515
^\[main\.assertion\.1\] line \d+ property 1: SUCCESS$
1616
--
1717
--

src/goto-programs/goto_convert.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,13 +1639,28 @@ void goto_convertt::generate_ifthenelse(
16391639
dest.destructive_append(tmp_v);
16401640
dest.destructive_append(tmp_w);
16411641

1642+
// When the `then` branch of a balanced `if` condition ends with a `return` or
1643+
// a `goto` statement, it is not necessary to add the `goto z` and `z:` goto
1644+
// elements as they are never used.
1645+
// This helps for example when using `--cover location` as such command are
1646+
// marked unreachable, but are not part of the user-provided code to analyze.
1647+
bool then_branch_returns = dest.instructions.rbegin()->is_goto();
1648+
16421649
if(has_else)
16431650
{
1644-
dest.destructive_append(tmp_x);
1651+
// Don't add the `goto` at the end of the `then` branch if not needed
1652+
if(!then_branch_returns)
1653+
{
1654+
dest.destructive_append(tmp_x);
1655+
}
16451656
dest.destructive_append(tmp_y);
16461657
}
16471658

1648-
dest.destructive_append(tmp_z);
1659+
// Don't add the `z` label at the end of the `if` when not needed.
1660+
if(!has_else || !then_branch_returns)
1661+
{
1662+
dest.destructive_append(tmp_z);
1663+
}
16491664
}
16501665

16511666
/// if(guard) goto target;

0 commit comments

Comments
 (0)