Contract Groups¶
The [[clang::contract_group("name")]]
attribute allows you to organize contracts into
named groups, enabling fine-grained control over which contracts are evaluated.
Basic Usage¶
You can assign contracts to groups using the attribute:
contract_assert [[clang::contract_group("mylib")]] (x > 0);
contract_assert [[clang::contract_group("debug")]] (invariant_holds());
Hierarchical Group Names¶
Contract groups support a dot-delimited hierarchy system that allows for precise control over evaluation semantics. Groups are organized in a tree structure where more specific groups inherit settings from their parent groups, but can override them.
Group Matching Rules:
Exact Match: The most specific match always wins
Parent Inheritance: If no exact match exists, the nearest parent group’s settings apply
Default Fallback: If no group matches, the default evaluation semantic applies
Examples of hierarchical matching:
// Group hierarchy: mylib -> mylib.debug -> mylib.debug.verbose
contract_assert [[clang::contract_group("mylib")]] (basic_check());
contract_assert [[clang::contract_group("mylib.debug")]] (debug_check());
contract_assert [[clang::contract_group("mylib.debug.verbose")]] (detailed_check());
contract_assert [[clang::contract_group("mylib.performance")]] (perf_check());
With these compiler flags:
# Set base mylib group to observe
-fcontract-group-evaluation-semantic=mylib=observe
# Override debug subgroup to enforce
-fcontract-group-evaluation-semantic=mylib.debug=enforce
# Override specific verbose group to ignore
-fcontract-group-evaluation-semantic=mylib.debug.verbose=ignore
The evaluation semantics would be:
mylib
contracts: observe (exact match)mylib.debug
contracts: enforce (exact match, overrides parent)mylib.debug.verbose
contracts: ignore (exact match, overrides parent)mylib.performance
contracts: observe (inherits from parentmylib
)
Multiple Group Specifications¶
You can specify multiple group settings in a single command:
-fcontract-group-evaluation-semantic=std=quick_enforce,mylib=observe,mylib.debug=ignore