refactor: always expand `assert` macro
Always expand the `assert` macro, even if `ENABLE_ASSERTIONS` is false.
This ensures that the assertion condition is type-checked by the
compiler, and variables it references are not considered unused. The
assertion will still be optimized out by the compiler if
`ENABLE_ASSERTIONS` is false.
Change-Id: I076f15cc86c8688522a56c35f23f68937b371ae4
Signed-off-by: Karl Meakin <karl.meakin@arm.com>
diff --git a/inc/hf/assert.h b/inc/hf/assert.h
index 86687d8..d7d7770 100644
--- a/inc/hf/assert.h
+++ b/inc/hf/assert.h
@@ -10,24 +10,36 @@
#if !defined(__cplusplus)
+#include <stdint.h>
+
+#include "hf/dlog.h"
#include "hf/panic.h"
#ifndef PLAT_LOG_LEVEL_ASSERT
#define PLAT_LOG_LEVEL_ASSERT LOG_LEVEL
#endif
-#if ENABLE_ASSERTIONS
-#if PLAT_LOG_LEVEL_ASSERT >= LOG_LEVEL_VERBOSE
-#define assert(e) \
- ((e) ? (void)0 : panic("ASSERT: %s:%d:%s\n", __FILE__, __LINE__, #e))
-#elif PLAT_LOG_LEVEL_ASSERT >= LOG_LEVEL_INFO
-#define assert(e) ((e) ? (void)0 : panic("ASSERT: %s:%d\n", __FILE__, __LINE__))
-#else
-#define assert(e) ((e) ? (void)0 : panic("ASSERT\n"))
-#endif
-#else
-#define assert(e) ((void)0)
-#endif /* ENABLE_ASSERTIONS */
+#define assert(e) assert_impl(e, __FILE__, __LINE__, #e)
+
+static inline void assert_impl(bool cond, const char *file, uint32_t line,
+ const char *expr)
+{
+ if (!ENABLE_ASSERTIONS) {
+ return;
+ }
+
+ if (cond) {
+ return;
+ }
+
+ if (PLAT_LOG_LEVEL_ASSERT >= LOG_LEVEL_VERBOSE) {
+ panic("ASSERT: %s:%d:%s\n", file, line, expr);
+ } else if (PLAT_LOG_LEVEL_ASSERT >= LOG_LEVEL_INFO) {
+ panic("ASSERT: %s:%d\n", file, line);
+ } else {
+ panic("ASSERT\n");
+ }
+}
#else
#include <assert.h>