Contracts
Contracts
J. Daniel Garcia
ARCOS Group
University Carlos III of Madrid
Spain
Warning
Who am I?
A C++ programmer.
Started writing C++ code in 1989.
Who am I?
A C++ programmer.
Started writing C++ code in 1989.
Who am I?
A C++ programmer.
Started writing C++ code in 1989.
Who am I?
A C++ programmer.
Started writing C++ code in 1989.
ARCOS@uc3m
UC3M: A young international research oriented university.
ARCOS: An applied research group.
Lines: High Performance Computing, Big data,
Cyberphysical systems, Programming Models for
Applications Improvement.
Improving applications:
REPARA: Reengineering and Enabling Performance and
poweR of Applications. FP7-ICT (2013–2016).
RePhrase: REfactoring Parallel Heterogeneous Resource
Aware Applications. H2020-ICT (2015–2018).
ASPIDE: exAScale ProgrammIng models for extreme Data
procEssing. H2020-FET-HPC (2018–2020).
Standardization:
ISO/IEC JTC/SC22/WG21. ISO C++ Committee.
cb e d – J. Daniel Garcia – ARCOS@UC3M ([email protected]) – Twitter: @jdgarciauc3m 4/55
Contracts programming for C++20
A brief history of contracts
2 Introduction
3 Contracts in C++
4 Contract checking
5 Contracts on interfaces
6 Summary
Contracts in C++
Contracts in C++
Targeting C++20.
2 Introduction
3 Contracts in C++
4 Contract checking
5 Contracts on interfaces
6 Summary
Exceptions in use
Exceptions in use
Exceptions in use
T * allocator<T>::allocate(std::size_t n);
Throws: bad_alloc if storage cannot be obtained.
2 Introduction
3 Contracts in C++
4 Contract checking
5 Contracts on interfaces
6 Summary
What is a contract?
What is a contract?
What is a contract?
What is a contract?
Expectations
Precondition
A predicate that should hold upon entry into a function.
It expresses a function’s expectation on its arguments
and/or the state of objects that may be used by the function.
Expressed by attribute expects.
Expectations
Precondition
A predicate that should hold upon entry into a function.
It expresses a function’s expectation on its arguments
and/or the state of objects that may be used by the function.
Expressed by attribute expects.
double sqrt(double x) [[expects: x>0]];
Expectations
Precondition
A predicate that should hold upon entry into a function.
It expresses a function’s expectation on its arguments
and/or the state of objects that may be used by the function.
Expressed by attribute expects.
double sqrt(double x) [[expects: x>0]];
class queue {
// ...
void push(const T & x) [[expects: ! full () ]];
// ...
};
Expectations
Precondition
A predicate that should hold upon entry into a function.
It expresses a function’s expectation on its arguments
and/or the state of objects that may be used by the function.
Expressed by attribute expects.
double sqrt(double x) [[expects: x>0]];
class queue {
// ...
void push(const T & x) [[expects: ! full () ]];
// ...
};
Assurances
Postcondition
A predicate that should hold upon exit from a function.
It expresses the conditions that a function should ensure for
the return value and/or the state of objects that may be
used by the function.
Postconditions are expressed by ensures attributes.
Assurances
Postcondition
A predicate that should hold upon exit from a function.
It expresses the conditions that a function should ensure for
the return value and/or the state of objects that may be
used by the function.
Postconditions are expressed by ensures attributes.
double sqrt(double x)
[[ expects: x>=0]]
[[ ensures result: result >=0]];
Assurances
Postcondition
A predicate that should hold upon exit from a function.
It expresses the conditions that a function should ensure for
the return value and/or the state of objects that may be
used by the function.
Postconditions are expressed by ensures attributes.
double sqrt(double x)
[[ expects: x>=0]]
[[ ensures result: result >=0]];
Assertions
Assertions
A predicate that should hold at its point in a function body.
It expresses the conditions that must be satisfied, on
objects that are accessible at its point in a body.
Assertions are expressed by assert attributes.
Assertions
Assertions
A predicate that should hold at its point in a function body.
It expresses the conditions that must be satisfied, on
objects that are accessible at its point in a body.
Assertions are expressed by assert attributes.
double add_distances(const std::vector<double> & v)
[[ expects r: r>=0.0]]
{
double r = 0.0;
for (auto x : v) {
[[ assert: x >= 0.0]];
r += x;
}
return r ;
}
cb e d – J. Daniel Garcia – ARCOS@UC3M ([email protected]) – Twitter: @jdgarciauc3m 19/55
Contracts programming for C++20
Contracts in C++
Effect of contracts
Effect of contracts
Effect of contracts
Repeating a contract
Repeating a contract
int f ( int x)
[[ expects: x>=0]]; // Error missing ensures and different expects
int f ( int x)
[[ expects: x>0]]
[[ ensures r: r >0]]; // OK. Same contract.
cb e d – J. Daniel Garcia – ARCOS@UC3M ([email protected]) – Twitter: @jdgarciauc3m 21/55
Contracts programming for C++20
Contracts in C++
Repeating a contract
Repeating a contract
int f ( int x)
[[ expects: x>0]]
[[ ensures r: r >0]];
int f ( int y)
[[ expects: y>0]]
[[ ensures z: z >0]];
2 Introduction
3 Contracts in C++
4 Contract checking
5 Contracts on interfaces
6 Summary
Assertion level
Assertion level
Assertion level
Assertion level
Audit checks
Axiom checks
Axiom checks
Axiom checks
Always checks
Build levels
Build levels
Contract checking
Contract checking
void f ( int ∗ p)
[[ expects: p!=nullptr]]
[[ expects: ∗p == 0]] // Only checked when p!=nullptr
{
∗p = 1;
}
But remember:
But remember:
No way of setting through source code.
But remember:
No way of setting through source code.
No way of asking which is current mode.
Plugin management.
void g() {
f(−1); // Invokes terminate if handler throws
}
2 Introduction
3 Contracts in C++
4 Contract checking
5 Contracts on interfaces
6 Summary
Repeating a contract
Repeating a contract
int f ( int x)
[[ expects: x>=0]]; // Error missing ensures and different expects
int f ( int x)
[[ expects: x>0]]
[[ ensures r: r >0]]; // OK. Same contract.
cb e d – J. Daniel Garcia – ARCOS@UC3M ([email protected]) – Twitter: @jdgarciauc3m 37/55
Contracts programming for C++20
Contracts on interfaces
Preconditions on functions
Modifications in contracts
Modifications in contracts
int f ( int x)
[[ expects: x++ > 0]] // Error
[[ ensures r: r == ++x]]; // Error
Workaround:
int f ( int x) {
int oldx = x;
auto r = ++x;
[[ assert: r==oldx ]];
}
cb e d – J. Daniel Garcia – ARCOS@UC3M ([email protected]) – Twitter: @jdgarciauc3m 41/55
Contracts programming for C++20
Contracts on interfaces
private:
// ...
int size_;
};
cb e d – J. Daniel Garcia – ARCOS@UC3M ([email protected]) – Twitter: @jdgarciauc3m 44/55
Contracts programming for C++20
Contracts on interfaces
struct D : public B {
public:
virtual void f ( int x) override; // OK. expects: x>0
// ...
};
cb e d – J. Daniel Garcia – ARCOS@UC3M ([email protected]) – Twitter: @jdgarciauc3m 46/55
Contracts programming for C++20
Contracts on interfaces
struct D : public B {
public:
virtual void f ( int x) override [[expects: x>0]]; // OK
// ...
};
cb e d – J. Daniel Garcia – ARCOS@UC3M ([email protected]) – Twitter: @jdgarciauc3m 47/55
Contracts programming for C++20
Contracts on interfaces
struct D : public B {
public:
virtual void f ( int x) override [[expects: x!=0]]; // Error
// ...
};
cb e d – J. Daniel Garcia – ARCOS@UC3M ([email protected]) – Twitter: @jdgarciauc3m 48/55
Contracts programming for C++20
Contracts on interfaces
struct D : public B {
public:
virtual void f ( int x) override [[expects: x>0]]; // Error .
// ...
};
cb e d – J. Daniel Garcia – ARCOS@UC3M ([email protected]) – Twitter: @jdgarciauc3m 49/55
Contracts programming for C++20
Contracts on interfaces
Precondition weakening
Precondition weakening
Postcondition strengthening
Postcondition strengthening
2 Introduction
3 Contracts in C++
4 Contract checking
5 Contracts on interfaces
6 Summary
Summary
Summary
Summary
Summary
https://github.com/arcosuc3m/clang-contracts
J. Daniel Garcia
ARCOS Group
University Carlos III of Madrid
Spain