From 5b4485f55b0336e2b39e30b6bb187c41b63b52f5 Mon Sep 17 00:00:00 2001 From: Petri Latvala Date: Mon, 21 Jan 2019 13:33:38 +0200 Subject: lib: Introduce BUILD_BUG_ON_INVALID and igt_assume() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BUILD_BUG_ON_INVALID() is a macro that, like the kernel counterpart, expands to an expression that generates no code. Useful for making sure an expression is valid code while producing no side effects. igt_assume() is an assert-like macro that is used to give hints to static analysis of code. If static analysis is not used (as detected by STATIC_ANALYSIS_BUILD), igt_assume() expands to a BUILD_BUG_ON_INVALID, otherwise expands to an assert(). v2: Make sure the expression in igt_assume is still parsed without static analysis. (Chris) v3: Also introduce BUILD_BUG_ON_INVALID as standalone Signed-off-by: Petri Latvala Cc: Arkadiusz Hiler Cc: Chris Wilson Cc: MichaƂ Winiarski Reviewed-by: Chris Wilson --- lib/igt_core.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'lib/igt_core.h') diff --git a/lib/igt_core.h b/lib/igt_core.h index 6f8c3852..0d02c90b 100644 --- a/lib/igt_core.h +++ b/lib/igt_core.h @@ -30,6 +30,7 @@ #ifndef IGT_CORE_H #define IGT_CORE_H +#include #include #include #include @@ -54,6 +55,30 @@ #endif #endif +/** + * BUILD_BUG_ON_INVALID: + * @expr: Expression + * + * A macro that takes an expression and generates no code. Used for + * checking at build-time that an expression is valid code. + */ +#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((long)(e)))) + +/** + * igt_assume: + * @expr: Condition to test + * + * An assert-like macro to be used for tautologies to give hints to + * static analysis of code. No-op if STATIC_ANALYSIS_BUILD is not + * defined, expands to an assert() if it is. + */ +#if STATIC_ANALYSIS_BUILD +#define igt_assume(e) assert(e) +#else +/* Make sure the expression is still parsed even though it generates no code */ +#define igt_assume(e) BUILD_BUG_ON_INVALID(e) +#endif + extern const char* __igt_test_description __attribute__((weak)); extern bool __igt_plain_output; extern char *igt_frame_dump_path; -- cgit v1.2.3