Skip to content

Commit 522c765

Browse files
author
klaas
committed
Cleans up comments in code_contracts
This commit adds some documentation to code-contracts, as well as fixing up small errors or confusing wording in existing comments.
1 parent d9480d4 commit 522c765

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

src/goto-instrument/code_contracts.cpp

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,56 @@ class code_contractst
5656

5757
void code_contracts(goto_functionst::goto_functiont &goto_function);
5858

59+
/// Applies (but does not check) a function contract.
60+
/// This will assume that the contract holds, and then use that assumption
61+
/// to remove the function call located at target.
62+
/// \param goto_program The goto program containing the target callsite.
63+
/// \param value_sets A value_setst object containing information about
64+
/// aliasing in the goto program being analyzed
65+
/// \param target An iterator pointing to the function call to be removed.
5966
void apply_contract(
6067
goto_programt &goto_program,
6168
value_setst &value_sets,
6269
goto_programt::targett target);
6370

71+
/// Applies (but does not check) a loop invariant.
72+
/// This will assume that the loop invariant is indeed an invariant, and then
73+
/// use that assumption to remove the loop.
74+
/// \param goto_function The goto function containing the target loop.
75+
/// \param value_sets A value_setst object containing information about
76+
/// aliasing in the goto program being analyzed
77+
/// \param loop_head An iterator pointing to the first instruction of the
78+
/// target loop.
79+
/// \param loop The loop being removed.
6480
void apply_invariant(
6581
goto_functionst::goto_functiont &goto_function,
6682
value_setst &value_sets,
6783
const goto_programt::targett loop_head,
6884
const loopt &loop);
6985

86+
/// Checks (but does not apply) a function contract.
87+
/// This will build a code snippet to be inserted at dest which will check
88+
// that the function contract is satisfied.
89+
/// \param function_id The id of the function being checked.
90+
/// \param goto_function The goto_function object for the function
91+
/// being checked.
92+
/// \param dest An iterator pointing to the place to insert checking code.
7093
void check_contract(
7194
const irep_idt &function_id,
7295
goto_functionst::goto_functiont &goto_function,
7396
goto_programt &dest);
7497

75-
void check_apply_invariant(
98+
/// Checks and applies a loop invariant
99+
/// This will replace the loop with a code snippet (based on the loop) which
100+
/// will check that the loop invariant is indeed ian invariant, and then
101+
/// use that invariant in what follows.
102+
/// \param goto_function The goto function containing the target loop.
103+
/// \param value_sets A value_setst object containing information about
104+
/// aliasing in the goto program being analyzed
105+
/// \param loop_head An iterator pointing to the first instruction of the
106+
/// target loop.
107+
/// \param loop The loop being removed.
108+
void check_apply_invariant(
76109
goto_functionst::goto_functiont &goto_function,
77110
value_setst &value_sets,
78111
const goto_programt::targett loop_head,
@@ -297,7 +330,7 @@ void code_contractst::apply_invariant(
297330
inst->make_skip();
298331
}
299332

300-
// Now havoc at the loop head. Use insert_swap to
333+
// Now havoc at the loop head. Use insert_before_swap to
301334
// preserve jumps to loop head.
302335
goto_function.body.insert_before_swap(loop_head, havoc_code);
303336

@@ -323,13 +356,15 @@ void code_contractst::check_contract(
323356
return;
324357
}
325358

326-
// build:
327-
// if(nondet)
359+
// We build the following checking code:
360+
// if(nondet) goto end
328361
// decl ret
329362
// decl parameter1 ...
330363
// assume(requires) [optional]
331364
// ret = function(parameter1, ...)
332365
// assert(ensures)
366+
// end:
367+
// skip
333368

334369
// build skip so that if(nondet) can refer to it
335370
goto_programt tmp_skip;
@@ -481,7 +516,7 @@ void code_contractst::check_apply_invariant(
481516
a->source_location.set_comment("Loop invariant violated before entry");
482517
}
483518

484-
// havoc variables being written to
519+
// havoc variables that can be modified by the loop
485520
build_havoc_code(loop_head, modifies, havoc_code);
486521

487522
// assume the invariant
@@ -516,7 +551,7 @@ void code_contractst::check_apply_invariant(
516551
loop_end->guard.make_not();
517552
}
518553

519-
// Now havoc at the loop head. Use insert_swap to
554+
// Now havoc at the loop head. Use insert_before_swap to
520555
// preserve jumps to loop head.
521556
goto_function.body.insert_before_swap(loop_head, havoc_code);
522557
}

src/goto-instrument/code_contracts.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,16 @@ class goto_modelt;
1919
void apply_code_contracts(goto_modelt &);
2020
void check_code_contracts(goto_modelt &);
2121

22+
// clang-format off
23+
#define HELP_APPLY_CODE_CONTRACTS \
24+
" --apply-code-contracts Assume (without checking) that the contracts used in code hold\n" \
25+
" This will use __CPROVER_requires, __CPROVER_ensures,\n" \
26+
" and __CPROVER_loop_invariant as assumptions in order to avoid\n" \
27+
" checking code that is assumed to satisfy a specification.\n"
28+
#define HELP_CHECK_CODE_CONTRACTS \
29+
" --check-code-contracts Assume (with checking) that the contracts used in code hold.\n" \
30+
" This differs from --apply-code-contracts in that in addition to\n" \
31+
" assuming specifications, it checks that they are correct.\n"
32+
// clang-format on
33+
2234
#endif // CPROVER_GOTO_INSTRUMENT_CODE_CONTRACTS_H

src/goto-instrument/goto_instrument_parse_options.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,8 @@ void goto_instrument_parse_optionst::help()
15801580
" --undefined-function-is-assume-false\n"
15811581
// NOLINTNEXTLINE(whitespace/line_length)
15821582
" convert each call to an undefined function to assume(false)\n"
1583+
HELP_APPLY_CODE_CONTRACTS
1584+
HELP_CHECK_CODE_CONTRACTS
15831585
HELP_REPLACE_FUNCTION_BODY
15841586
"\n"
15851587
"Loop transformations:\n"

0 commit comments

Comments
 (0)