From 636bd7a2594aec0a7e04785d5b4b19cec19780b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Mon, 4 Sep 2017 21:01:39 +0200 Subject: [PATCH] Better macros for unreachable code. This adds a `CRYB_UNREACHABLE` macro which will cause a segfault if reached, and modifies `CRYB_NO_DEFAULT_CASE` to have essentially the same semantics. It also removes the conditionals since the coverage macros are always defined. --- include/cryb/coverage.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/include/cryb/coverage.h b/include/cryb/coverage.h index 97dbc36..c979d85 100644 --- a/include/cryb/coverage.h +++ b/include/cryb/coverage.h @@ -45,16 +45,24 @@ #endif /* - * Use at end of switch which has no default case + * Some switch constructs may leave you trapped between a static analyzer + * that complains about the lack of a default case and a dynamic analyzer + * that complains about dead code. Use this macro at the end of a switch + * that does not need a default case. */ -#if CRYB_COVERAGE #define CRYB_NO_DEFAULT_CASE \ CRYB_DISABLE_COVERAGE \ - default: \ - (void)0; \ + break; default: do { *(volatile int *)0 = 0; } while (0) \ + CRYB_RESTORE_COVERAGE + +/* + * Use wherever a static analyzer is unable to correctly determine that a + * loop will never terminate, an if-else chain will always cover all + * possible cases, etc. + */ +#define CRYB_UNREACHABLE \ + CRYB_DISABLE_COVERAGE \ + do { *(volatile int *)0 = 0; } while (0) \ CRYB_RESTORE_COVERAGE -#else -#define CRYB_NO_DEFAULT_CASE -#endif #endif