this repo has no description
at trunk 63 lines 1.4 kB view raw view rendered
1# [dcheck](https://github.com/tekknolagi/dcheck) 2 3This very small assert library provides `assert`-like macros for checking 4conditions at runtime. It works with C and C++. 5 6Improvements over raw `assert`: 7 8* Semantic checks for indices, bounds, ranges, unimplemented features, and 9 unreachable states 10* Includes `printf`-compatible error message formatting 11* `NDEBUG` and always-on variants 12* Bounds/index checking built-in 13* File/function/line number and symbolized condition code in error message 14* Still checks code for compiler errors if DCHECK is turned off 15 16Here is some sample code: 17 18```c 19#include "dcheck.h" 20 21int main() { 22 // ... 23 24 CHECK(true, "thank goodness"); 25 26 int arr[3]; 27 // Array index won't crash 28 CHECK_INDEX(ind, 3); 29 30 // Still can order off the kids menu 31 CHECK_BOUND(age, 5); 32 33 // Month is valid 34 CHECK_RANGE(month, 1, 12); 35 36 if (needs_feature) { 37 UNIMPLEMENTED("needs Brian's feature"); 38 } 39 40 if (val == 4) { 41 UNREACHABLE("unexpected number %d", val); 42 } 43 44 // ... 45} 46``` 47 48Add `D` before `CHECK`, `CHECK_INDEX`, `CHECK_BOUND`, and `CHECK_RANGE` if you 49want the check to go away in `NDEBUG` mode. Like `DCHECK_INDEX` and so forth. 50 51Happy hacking. 52 53## Use in CMake projects 54 55```cmake 56include(FetchContent) 57FetchContent_Declare( 58 dcheck 59 GIT_REPOSITORY https://github.com/tekknolagi/dcheck 60 GIT_TAG trunk 61) 62FetchContent_MakeAvailable(dcheck) 63```