Skip to content

Commit a442dbb

Browse files
Add --cover-failed-assertions option
This adds an option `--cover-failed-assertions` that prevents the default behaviour of coverage stopping at failed assertions by turning assertions into skips rather than assumes (which is the default behaviour for coverage criteria other than `assertion`, which behaves the same with or without the flag).
1 parent cefd3ce commit a442dbb

File tree

5 files changed

+67
-11
lines changed

5 files changed

+67
-11
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
test.c
3+
--cover location --pointer-check --malloc-may-fail --malloc-fail-null
4+
\[main.coverage.4\] file test.c line \d+ function main block 4 \(lines test.c:main:12,13\): SATISFIED
5+
\[main.coverage.3\] file test.c line \d+ function main block 3 \(lines test.c:main:10\): FAILED
6+
\[main.coverage.2\] file test.c line \d+ function main block 2 \(lines test.c:main:4,5,7,9\): SATISFIED
7+
\[main.coverage.1\] file test.c line \d+ function main block 1 \(lines test.c:main:4\): SATISFIED
8+
^EXIT=0$
9+
^SIGNAL=0$
10+
--
11+
^warning: ignoring
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdlib.h>
2+
3+
int main()
4+
{
5+
int *ptr = malloc(sizeof(*ptr));
6+
int a;
7+
8+
a = *ptr;
9+
10+
if(ptr == NULL)
11+
a = 1;
12+
13+
return 0;
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
test.c
3+
--cover location --cover-failed-assertions --pointer-check --malloc-may-fail --malloc-fail-null
4+
\[main.coverage.4\] file test.c line \d+ function main block 4 \(lines test.c:main:12,13\): SATISFIED
5+
\[main.coverage.3\] file test.c line \d+ function main block 3 \(lines test.c:main:10\): SATISFIED
6+
\[main.coverage.2\] file test.c line \d+ function main block 2 \(lines test.c:main:4,5,7,9\): SATISFIED
7+
\[main.coverage.1\] file test.c line \d+ function main block 1 \(lines test.c:main:4\): SATISFIED
8+
^EXIT=0$
9+
^SIGNAL=0$
10+
--
11+
^warning: ignoring

src/goto-instrument/cover.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ void parse_cover_options(const cmdlinet &cmdline, optionst &options)
164164
options.set_option(
165165
"cover-traces-must-terminate",
166166
cmdline.isset("cover-traces-must-terminate"));
167+
options.set_option(
168+
"cover-failed-assertions", cmdline.isset("cover-failed-assertions"));
167169
}
168170

169171
/// Build data structures controlling coverage from command-line options.
@@ -223,6 +225,9 @@ cover_configt get_cover_config(
223225
cover_config.traces_must_terminate =
224226
options.get_bool_option("cover-traces-must-terminate");
225227

228+
cover_config.cover_failed_assertions =
229+
options.get_bool_option("cover-failed-assertions");
230+
226231
return cover_config;
227232
}
228233

@@ -285,16 +290,22 @@ static void instrument_cover_goals(
285290
// Simplify the common case where we have ASSERT(x); ASSUME(x):
286291
if(i_it->is_assert())
287292
{
288-
auto successor = std::next(i_it);
289-
if(
290-
successor != function.body.instructions.end() &&
291-
successor->is_assume() &&
292-
successor->get_condition() == i_it->get_condition())
293+
if(!cover_config.cover_failed_assertions)
293294
{
294-
successor->turn_into_skip();
295+
auto successor = std::next(i_it);
296+
if(
297+
successor != function.body.instructions.end() &&
298+
successor->is_assume() &&
299+
successor->get_condition() == i_it->get_condition())
300+
{
301+
successor->turn_into_skip();
302+
}
303+
i_it->type = goto_program_instruction_typet::ASSUME;
304+
}
305+
else
306+
{
307+
i_it->turn_into_skip();
295308
}
296-
297-
i_it->type = goto_program_instruction_typet::ASSUME;
298309
}
299310
}
300311
}

src/goto-instrument/cover.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ class message_handlert;
2222
class cmdlinet;
2323
class optionst;
2424

25-
#define OPT_COVER "(cover):"
26-
#define HELP_COVER \
27-
" --cover CC create test-suite with coverage criterion CC\n"
25+
#define OPT_COVER \
26+
"(cover):" \
27+
"(cover-failed-assertions)"
28+
29+
#define HELP_COVER \
30+
" --cover CC create test-suite with coverage criterion " \
31+
"CC\n" \
32+
" --cover-failed-assertions do not stop coverage checking at failed " \
33+
"assertions\n" \
34+
" (this is the default for --cover " \
35+
"assertions)\n"
2836

2937
enum class coverage_criteriont
3038
{
@@ -41,6 +49,7 @@ enum class coverage_criteriont
4149
struct cover_configt
4250
{
4351
bool keep_assertions;
52+
bool cover_failed_assertions;
4453
bool traces_must_terminate;
4554
irep_idt mode;
4655
function_filterst function_filters;

0 commit comments

Comments
 (0)