aboutsummaryrefslogtreecommitdiff
path: root/src/include/assert.h
diff options
context:
space:
mode:
authorJulius Werner <jwerner@chromium.org>2019-03-28 13:39:06 -0700
committerPatrick Georgi <pgeorgi@google.com>2019-04-01 07:57:00 +0000
commit988ac294c7669f59d39eb536cde7b4a475ce6f40 (patch)
tree0b49c666480744cb1eaff0308cc4f133e74c038e /src/include/assert.h
parent9993b6f0b52257b05ccf5d6dab658c3bfe17e372 (diff)
assert: Make dead_code() work at link-time instead of compile-time
The dead_code() macro can be used to ensure that a certain code path is compile-time eliminated (e.g. if you want to make sure it's never executed for certain Kconfig combinations). Unfortunately, the current implementation via __attribute__((error)) hits only at the GCC level. This can catch code that can be compile-time eliminated based on state within the same file, but it cannot be used in cases where a certain library function is built but then garbage collected at link time. This patch improves the macro by relying solely on the linker finding an undefined reference. Unfortunately this makes the error message a little less expressive (can no longer pass a custom string), but it is still readable and one can add code comments next to the assertion to elaborate further if necessary Change-Id: I63399dc484e2150d8c027bc0256d9285e471f7cc Signed-off-by: Julius Werner <jwerner@chromium.org> Reviewed-on: https://review.coreboot.org/c/coreboot/+/32113 Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net> Reviewed-by: Furquan Shaikh <furquan@google.com> Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Diffstat (limited to 'src/include/assert.h')
-rw-r--r--src/include/assert.h20
1 files changed, 12 insertions, 8 deletions
diff --git a/src/include/assert.h b/src/include/assert.h
index afbed03318..6036635273 100644
--- a/src/include/assert.h
+++ b/src/include/assert.h
@@ -43,18 +43,22 @@
* will generate a compiler error even if the scope it was called from is dead
* code. This may be useful to double-check things like constants that are only
* valid if a certain Kconfig option is set.
+ *
+ * The error message when this hits will look like this:
+ *
+ * ramstage/lib/bootmode.o: In function `display_init_required':
+ * bootmode.c:42: undefined reference to `dead_code_assertion_failed_at_line_42'
*/
-#define __dead_code(message, line) do { \
- __attribute__((error(#message " in " __FILE__ ":" #line))) \
- extern void dead_code_assertion_failed_##line(void); \
- dead_code_assertion_failed_##line(); \
+#define __dead_code(line) do { \
+ extern void dead_code_assertion_failed_at_line_##line(void); \
+ dead_code_assertion_failed_at_line_##line(); \
} while (0)
-#define _dead_code(message, line) __dead_code(message, line)
-#define dead_code(message) _dead_code(message, __LINE__)
+#define _dead_code(line) __dead_code(line)
+#define dead_code() _dead_code(__LINE__)
/* This can be used in the context of an expression of type 'type'. */
-#define dead_code_t(type, message) ({ \
- dead_code(message); \
+#define dead_code_t(type) ({ \
+ dead_code(); \
*(type *)(uintptr_t)0; \
})