Skip to contentSkip to conversation

Make BUILD_BUG_ON fail at compile time.

PATCH In review Andi Kleen 3 participants 3 messages Sep 10, 2005 – Sep 12, 2005

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/2 Make BUILD_BUG_ON fail at compile time.

Make BUILD_BUG_ON fail at compile time.

Force a compiler error instead of a link error, because they are easier to track down. Idea stolen from code by Jan Beulich.

include/linux/kernel.h +2 -2
Index: linux/include/linux/kernel.h
===================================================================
@@ -307,8 +307,8 @@ struct sysinfo {
 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
 };
 
-extern void BUILD_BUG(void);
-#define BUILD_BUG_ON(condition) do { if (condition) BUILD_BUG(); } while(0)
+/* Force a compilation error if condition is false */
+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
 
 #ifdef CONFIG_SYSCTL
 extern int randomize_va_space;
#1 lore raw
Quoted 6 lines · Andi Kleen

Make BUILD_BUG_ON fail at compile time.

Force a compiler error instead of a link error, because they are easier to track down. Idea stolen from code by Jan Beulich.

reiser4 seems to have a duplicate version of this, Hans, can you switch over to the common one once this is in?

#2 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 5 lines · Andi Kleen

Make BUILD_BUG_ON fail at compile time.

Force a compiler error instead of a link error, because they are easier to track down. Idea stolen from code by Jan Beulich.

Quoted 11 lines
Quoted 3 lines

reiser4 seems to have a duplicate version of this, Hans, can you switch over to the common one once this is in?

Yes we can.