Re: [igt-dev] [PATCH i-g-t 01/21] lib: Introduce igt_assume()

Chris Wilson
2019-01-16T14:27:04Z

Quoting Petri Latvala (2019-01-16 11:20:30)
> 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 no-op statement,
> otherwise expands to an assert().
>
> Signed-off-by: Petri Latvala <petri.latvala@intel.com>
> Cc: Arkadiusz Hiler <arkadiusz.hiler@intel.com>
> Cc: Chris Wilson <chris@chris-wilson.co.uk>
> Cc: Michał Winiarski <michal.winiarski@intel.com>
> ---
> lib/igt_core.h | 15 +++++++++++++++
> 1 file changed, 15 insertions(+)
>
> diff --git a/lib/igt_core.h b/lib/igt_core.h
> index 6f8c3852..82ec7973 100644
> --- a/lib/igt_core.h
> +++ b/lib/igt_core.h
> @@ -30,6 +30,7 @@
> #ifndef IGT_CORE_H
> #define IGT_CORE_H
>
> +#include <assert.h>
> #include <setjmp.h>
> #include <stdbool.h>
> #include <stdint.h>
> @@ -54,6 +55,20 @@
> #endif
> #endif
>
> +/**
> + * 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
> +#define igt_assume(e) do {} while(0)
> +#endif

Fair enough. You should definitely steal

/*
* BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
* expression but avoids the generation of any code, even if that expression
* has side-effects.
*/
#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))

so that #else becomes
#define igt_assume(e) BUILD_BUG_ON_INVALID(e)
so that we plebs don't feed garbage to the static analyzer.
-Chris