<p>Julius Werner has uploaded this change for <strong>review</strong>.</p><p><a href="https://review.coreboot.org/20585">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">assert: Add new dead_code() macro to assert dead code paths<br><br>This patch adds a new kind of compile-time assertion based on Linux'<br>compiletime_assert(). The difference to the existing use of<br>_Static_assert() in coreboot (which should continue to be used where<br>appropriate) is that this new assertion only hits if the call to it is<br>not optimized out at compile time. It is therefore ideal to assert that<br>certain code paths are not included in the image if a certain Kconfig<br>option is (not) set. For example,<br><br> assert(!IS_ENABLED(CONFIG_THAT_MAKES_THIS_INAPPROPRIATE));<br><br>can be rewritten as<br><br> if (!IS_ENABLED(CONFIG_THAT_MAKES_THIS_INAPPROPRIATE))<br>   dead_code("This code shouldn't be built for config X");<br><br>to turn it into a compile-time check.<br><br>Change-Id: Ida2911e2e4b3191a00d09231b493bf755e6f0fcb<br>Signed-off-by: Julius Werner <jwerner@chromium.org><br>---<br>M src/include/assert.h<br>1 file changed, 21 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://review.coreboot.org:29418/coreboot refs/changes/85/20585/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">diff --git a/src/include/assert.h b/src/include/assert.h<br>index 90cc2a8..a4ae39f 100644<br>--- a/src/include/assert.h<br>+++ b/src/include/assert.h<br>@@ -37,4 +37,25 @@<br> <br> #define assert(statement)      ASSERT(statement)<br> <br>+/*<br>+ * These macros can be used to assert that a certain branch of code is dead and<br>+ * will be compile-time eliminated. This differs from _Static_assert(), which<br>+ * will generate a compiler error even if the scope it was called from is dead<br>+ * code. This may be useful to double-check things like constants that are only<br>+ * valid if a certain Kconfig option is set.<br>+ */<br>+#define __dead_code(message, line) do { \<br>+      __attribute__((error(#message " in " __FILE__ ":" #line))) \<br>+     extern void dead_code_assertion_failed_##line(void); \<br>+       dead_code_assertion_failed_##line(); \<br>+} while (0)<br>+#define _dead_code(message, line) __dead_code(message, line)<br>+#define dead_code(message) _dead_code(message, __LINE__)<br>+<br>+/* This can be used in the context of an expression of type 'type'. */<br>+#define dead_code_t(type, message) ({ \<br>+ dead_code(message); \<br>+        *(type *)(uintptr_t)0; \<br>+})<br>+<br> #endif // __ASSERT_H__<br></pre><p>To view, visit <a href="https://review.coreboot.org/20585">change 20585</a>. To unsubscribe, visit <a href="https://review.coreboot.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://review.coreboot.org/20585"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: coreboot </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Ida2911e2e4b3191a00d09231b493bf755e6f0fcb </div>
<div style="display:none"> Gerrit-Change-Number: 20585 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Julius Werner <jwerner@chromium.org> </div>