Assert and debug levels for error checking
Summary
In several dune core modules, error checking is enabled differently.
- In dune-common
boundschecking.hh
a macroDUNE_ASSERT_BOUNDS
is enabled, if the variableDUNE_CHECK_BOUNDS
is set. - In
densematrix.hh
anddiagonalmatrix.hh
additionally, the macroDUNE_FMatrix_WITH_CHECKING
is checked and if set, some additional conditions are tested. - Also in dune-common but also in dune-istl, the macro
DUNE_ISTL_WITH_CHECKING
is available for enabling extra checks in parallel code and in istl containers - Then we have the classical
assert
macro as well as the extra macroDUNE_ASSERT_AND_RETURN
- In
debugallocator.hh
we have another macro:ALLOCATION_ASSERT
- And
stdthread.hh
definesDUNE_ASSERT_CALL_ONCE
- The file
reservedvector.hh
additionally introduces the macroCHECKSIZE
that is activated byCHECK_RESERVEDVECTOR
- In some files, explicitly
NDEBUG
is used for deactivating extra checks
This situation is not very satisfactory. Probably, I have not listed all available macros and variables to do some error checking. We need a better way!
Another related issue:
- The
DUNE_THROW
macro cannot be used inconstexpr
contexts. This is, because theDune::Exception
classes are no literal types and one cannot even construct them with a string message directly.
How do others solve this issue?
-
wxWidgets introduce a
wxDEBUG_LEVEL
variable (default value 1) and macroswxASSERT
andwxASSER_LEVEL_2
to distinguish cheap and expensive debug checks -
Microsoft has
ASSERT
(checks if_DEBUG
is set),VERIFY
(always checked), andASSERTE
(check with message representing the checked expression) -
deal.ii has
Assert
,AssertThrow
andAssertNothrow
macros that are enabled ifDEBUG
is set. -
LiveV introduces macros
ASSERT(X,A)
(deactived with-DNODEBUG
),ASSERT_PRE(X,A)
,ASSERT_POST(X,A)
,ASSERT_INV(X,A)
(activated with-DTEST_[PRE|POST|INV]
-
CppCoreGuidelines introduce
Expects()
andEnsures
macros for pre- and post-conditions. In the GSL, these are implemented using the[[likely]]
keyword
Proposal
- I suggest to only have a single type of macro for all checks:
DUNE_ASSERT
- I like the design of wxWidgets to have a dedicated name for expensive asserts,
DUNE_ASSERT_LEVEL_2
. Whether it is activated or not, can be controlled by the variableDUNE_DEBUG_LEVEL
(name of that macro is up to discussion) - I like the design of LiveV with a message argument by default, but maybe this can be made optional.
- Additionally, one might consider to distinguish pre-/post-conditions and invariants, and may introduce also assumptions, e.g.
DUNE_ASSUME
that are always checked but might not leads to direct termination. These are infos to the compiler and the user and termination with error-messages must be enabled explicitly. - The
DUNE_THROW
macro and exceptions should be madeconstexpr
-friendly
An alternative with a flexible set of arguments to the assert macro it described in https://www.foonathan.net/2016/09/assertions/
Related issues/merge-requests
Edited by Simon Praetorius