Contract syntax¶
Every construct is written qualified by the namespace cppverify:
cppverify::pre(x > 0), cppverify::check(e), cppverify::forall(k, ...).
A namespace alias shortens it. The examples of this reference write the
constructs through the alias cv and use the names of <cppverify.h>
(valid, seq, set, multiset, map) unqualified, as a file
does after namespace cv = cppverify; and using namespace cppverify;:
namespace cv = cppverify;
using namespace cppverify;
int abs_value(int x)
cv::pre(x > -2147483647 - 1)
cv::post(cv::result >= 0)
{
return x < 0 ? -x : x;
}
The parser recognizes a construct when the qualifier names the global namespace
cppverify, writtencppverify::,::cppverify::, or through any alias. A word without the qualifier is ordinary C++:pre,result,ghost, orcheckmay name your own variables and functions, and no header of the standard library is affected.using namespace cppverify;does not make the bare words constructs; writing one bare where only the construct fits is an error that shows the qualified spelling.With
-fverify-contracts, the namespace and<cppverify.h>are available in every C++ file without an#include. Without the flag the constructs are not recognized.C++26 contracts (
pre,post,contract_assertwithout a qualifier) belong to the compiler; cpp-verify neither parses nor verifies them.
Full table:
Syntax |
Placement |
Meaning |
|---|---|---|
|
After |
Precondition |
|
After |
Postcondition |
|
After |
Writable cells, ranges |
|
After |
Opt a pointer/reference address pair into aliasing |
|
Spec functions |
Soft precondition |
|
Spec functions |
Cells |
|
Spec functions |
Domain on which the body defines the spec |
|
Spec functions returning |
The least predicate the body defines (no |
|
Spec function definitions |
Proof steps for that clause’s check |
|
After |
A case of the contract |
|
After the behaviors |
Some / at most one behavior applies |
|
After loop |
Loop invariant |
|
Loop / function |
Termination measure (required on executable loops) |
|
Executable loop / function |
Allow divergence; proofs are |
|
In struct/class |
Field invariant |
|
Statement |
Proof-only block |
|
Statement |
Function-scoped ghost variable |
|
Statement |
Proof obligation |
|
Statement |
Proof obligation with a local proof |
|
Statement |
Chain of proved steps |
|
Statement anywhere in a function body, ghost blocks included |
Unfold recursive spec |
|
Statement anywhere in a function body, ghost blocks included |
Make spec |
|
Expression |
Bounded |
|
Expression |
Bounded |
|
Expression |
|
|
In a quantifier body |
Instantiation pattern |
|
Expression |
Some integer satisfying |
|
In |
Pre-state value |
|
In |
Return value |
|
Decl |
Spec function |
|
Decl |
Proof function |
|
|
Spec collections |
|
In |
|