Skip to content

Commit acc1586

Browse files
committed
Added extra use-case hints to the already present invariant definitions.
1 parent 086c266 commit acc1586

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/util/invariant.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,26 @@ inline void invariant_violated_string(
225225
// for INVARIANT.
226226

227227
// The condition should only contain (unmodified) arguments to the method.
228+
// Inputs include arguments passed to the function, as well as member
229+
// variables that the function may read.
228230
// "The design of the system means that the arguments to this method
229231
// will always meet this condition".
232+
//
233+
// It is also supposed to signal that even though an object is in a
234+
// valid state, a particular function may not be operating on objects
235+
// with the particular properties this one has.
230236
#define PRECONDITION(CONDITION) INVARIANT(CONDITION, "Precondition")
231237
#define PRECONDITION_STRUCTURED(CONDITION, TYPENAME, ...) \
232238
INVARIANT_STRUCTURED(CONDITION, TYPENAME, __VA_ARGS__)
233239

234240
// The condition should only contain variables that will be returned /
235241
// output without further modification.
236242
// "The implementation of this method means that the condition will hold".
243+
//
244+
// A POSTCONDITION documents what the function can guarantee about its
245+
// output when it returns, given that it was called with valid inputs.
246+
// Outputs include the return value of the function, as well as member
247+
// variables that the function may write.
237248
#define POSTCONDITION(CONDITION) INVARIANT(CONDITION, "Postcondition")
238249
#define POSTCONDITION_STRUCTURED(CONDITION, TYPENAME, ...) \
239250
INVARIANT_STRUCTURED(CONDITION, TYPENAME, __VA_ARGS__)
@@ -242,6 +253,10 @@ inline void invariant_violated_string(
242253
// changed by a previous method call.
243254
// "The contract of the previous method call means the following
244255
// condition holds".
256+
//
257+
// A difference between CHECK_RETURN and POSTCONDITION is that CHECK_RETURN is
258+
// a statement about what the caller expects from a function, whereas a
259+
// POSTCONDITION is a statement about guarantees made by the function itself.
245260
#define CHECK_RETURN(CONDITION) INVARIANT(CONDITION, "Check return value")
246261
#define CHECK_RETURN_STRUCTURED(CONDITION, TYPENAME, ...) \
247262
INVARIANT_STRUCTURED(CONDITION, TYPENAME, __VA_ARGS__)
@@ -254,14 +269,18 @@ inline void invariant_violated_string(
254269
// This condition should be used to document that assumptions that are
255270
// made on goto_functions, goto_programs, exprts, etc. being well formed.
256271
// "The data structure is corrupt or malformed"
272+
//
273+
// It is okay for a DATA_INVARIANT to have an empty string as a reason string
274+
// if you can't state the reason, or doing so doesn't add any information -
275+
// just restates the condition.
257276
#define DATA_INVARIANT(CONDITION, REASON) INVARIANT(CONDITION, REASON)
258277
#define DATA_INVARIANT_STRUCTURED(CONDITION, TYPENAME, ...) \
259278
INVARIANT_STRUCTURED(CONDITION, TYPENAME, __VA_ARGS__)
260279

261280
// Legacy annotations
262281

263282
// The following should not be used in new code and are only intended
264-
// to migrate documentation and "error handling" in older code
283+
// to migrate documentation and "error handling" in older code.
265284
#define TODO INVARIANT(false, "Todo")
266285
#define UNIMPLEMENTED INVARIANT(false, "Unimplemented")
267286
#define UNHANDLED_CASE INVARIANT(false, "Unhandled case")

0 commit comments

Comments
 (0)