History

bug: introduce BUILD_BUG_ON_INVALID() macro

Sometimes we want to check some expressions correctness at compile time. "(void)(e);" or "if (e);" can be dangerous if the expression has side-effects, and gcc sometimes generates a lot of code, even if the expression has no effect. This patch introduces macro BUILD_BUG_ON_INVALID() for such checks, it forces a compilation error if expression is invalid without any extra code. [Cast to "long" required because sizeof does not work for bit-fields.]

Konstantin Khlebnikov committed 14 years ago

Commit baf05aa1 parent 5febcbe1 file · +7 −0Browse files

Unified · Split

include/linux/bug.h+7−0
@@ -30,6 +30,13 @@ struct pt_regs; · 29 unmodified lines
3030 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
3131 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
3232
33+/*
34+ * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
35+ * expression but avoids the generation of any code, even if that expression
36+ * has side-effects.
37+ */
38+#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
39+
3340 /**
3441 * BUILD_BUG_ON - break compile if a condition is true.
3542 * @condition: the condition which the compiler should know is false.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t