Skip to content

Commit 974a0e3

Browse files
authored
Merge pull request #2142 from thk123/docs/invariant-message
Adding extra information about invariant messages
2 parents da8d2ab + 43bfc73 commit 974a0e3

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

CODING_STANDARD.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,14 @@ Formatting is enforced using clang-format. For more information about this, see
191191
- The type is explicitly repeated on the RHS (e.g. a constructor call)
192192
- Adding the type will increase confusion (e.g. iterators, function pointers)
193193
- Avoid `assert`. If the condition is an actual invariant, use INVARIANT,
194-
PRECONDITION, POSTCONDITION, CHECK_RETURN, UNREACHABLE or DATA_INVARIANT. If
195-
there are possible reasons why it might fail, throw an exception.
194+
PRECONDITION, POSTCONDITION, CHECK_RETURN, UNREACHABLE or DATA_INVARIANT (also
195+
see the documentation of the macros in `src/util/invariant.h`). If there are
196+
possible reasons why it might fail, throw an exception.
197+
- Use "should" style statements for messages in invariants (e.g. "array
198+
should have a non-zero size") to make both the violation and the expected
199+
behavior clear. (As opposed to "no zero size arrays" where it isn't clear
200+
if the zero-size array is the problem, or the lack of it).
201+
- The statements should start with a lower case letter.
196202
- All raw pointers (such as those returned by `symbol_tablet::lookup`) are
197203
assumed to be non-owning, and should not be `delete`d. Raw pointers that
198204
point to heap-allocated memory should be private data members of an object

src/util/invariant.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,19 @@ Author: Martin Brain, martin.brain@diffblue.com
2323
** evaluate to a boolean.
2424
**
2525
** As well as the condition they have a text argument that should be
26-
** used to say why they are true. The reason should be a string literals.
26+
** used to say why they are true. The reason should be a string literal
27+
** starting with a lower case character.
2728
** Longer diagnostics should be output to error(). For example:
2829
**
29-
** INVARIANT(x > 0, "x negative and zero case handled by other branches");
30+
** INVARIANT(
31+
** x > 0,
32+
** "x should have a positive value as others are handled by other branches");
33+
**
34+
** Use "should" style statements for messages in invariants (also see
35+
** CODING_STANDARD.md). An example would be "array should have a non-zero size")
36+
** to make both the violation and the expected behavior clear. (As opposed to
37+
** "no zero size arrays" where it isn't clear if the zero-size array is the
38+
** problem, or the lack of it).
3039
**
3140
** To help classify different kinds of invariants, various short-hand
3241
** macros are provided.

0 commit comments

Comments
 (0)