[coreboot-gerrit] Change in coreboot[master]: assert: Add new dead_code() macro to assert dead code paths

Julius Werner (Code Review) gerrit at coreboot.org
Fri Jul 14 23:50:01 CEST 2017


Julius Werner has uploaded this change for review. ( https://review.coreboot.org/20585


Change subject: assert: Add new dead_code() macro to assert dead code paths
......................................................................

assert: Add new dead_code() macro to assert dead code paths

This patch adds a new kind of compile-time assertion based on Linux'
compiletime_assert(). The difference to the existing use of
_Static_assert() in coreboot (which should continue to be used where
appropriate) is that this new assertion only hits if the call to it is
not optimized out at compile time. It is therefore ideal to assert that
certain code paths are not included in the image if a certain Kconfig
option is (not) set. For example,

 assert(!IS_ENABLED(CONFIG_THAT_MAKES_THIS_INAPPROPRIATE));

can be rewritten as

 if (!IS_ENABLED(CONFIG_THAT_MAKES_THIS_INAPPROPRIATE))
   dead_code("This code shouldn't be built for config X");

to turn it into a compile-time check.

Change-Id: Ida2911e2e4b3191a00d09231b493bf755e6f0fcb
Signed-off-by: Julius Werner <jwerner at chromium.org>
---
M src/include/assert.h
1 file changed, 21 insertions(+), 0 deletions(-)



  git pull ssh://review.coreboot.org:29418/coreboot refs/changes/85/20585/1

diff --git a/src/include/assert.h b/src/include/assert.h
index 90cc2a8..a4ae39f 100644
--- a/src/include/assert.h
+++ b/src/include/assert.h
@@ -37,4 +37,25 @@
 
 #define assert(statement)	ASSERT(statement)
 
+/*
+ * These macros can be used to assert that a certain branch of code is dead and
+ * will be compile-time eliminated. This differs from _Static_assert(), which
+ * 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.
+ */
+#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(); \
+} while (0)
+#define _dead_code(message, line) __dead_code(message, line)
+#define dead_code(message) _dead_code(message, __LINE__)
+
+/* This can be used in the context of an expression of type 'type'. */
+#define dead_code_t(type, message) ({ \
+	dead_code(message); \
+	*(type *)(uintptr_t)0; \
+})
+
 #endif // __ASSERT_H__

-- 
To view, visit https://review.coreboot.org/20585
To unsubscribe, visit https://review.coreboot.org/settings

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ida2911e2e4b3191a00d09231b493bf755e6f0fcb
Gerrit-Change-Number: 20585
Gerrit-PatchSet: 1
Gerrit-Owner: Julius Werner <jwerner at chromium.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.coreboot.org/pipermail/coreboot-gerrit/attachments/20170714/1a7dab8c/attachment.html>


More information about the coreboot-gerrit mailing list