From: Francis Deslauriers Date: Tue, 4 May 2021 19:48:52 +0000 (-0400) Subject: Define `static_assert()` when not defined by kernel X-Git-Tag: v2.13.0-rc2~36 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=c383eb0d21d085c9814f1406fde4eabac149f8c1;p=lttng-modules.git Define `static_assert()` when not defined by kernel This macro is useful for build-time assertions. It was added in kernel v5.1. It uses the C11 _Static_assert macro that was implemented with gcc 4.6. There is support for the _Static_assert in all C modes, including gnu89. Since our minimal gcc version is 4.8, we can simply define it manually if the kernel is older than v5.1. Kernel commit adding this macro: commit 6bab69c65013bed5fce9f101a64a84d0385b3946 Author: Rasmus Villemoes Date: Thu Mar 7 16:27:00 2019 -0800 build_bug.h: add wrapper for _Static_assert Signed-off-by: Francis Deslauriers Signed-off-by: Mathieu Desnoyers Change-Id: I5aa250f60f9dca4f13b8bcc093a006034c28e526 --- diff --git a/include/wrapper/compiler.h b/include/wrapper/compiler.h index 40a472d6..ade06fe7 100644 --- a/include/wrapper/compiler.h +++ b/include/wrapper/compiler.h @@ -69,4 +69,16 @@ #define __LTTNG_COMPOUND_LITERAL(type, ...) (type[]) { __VA_ARGS__ } +/* + * The static_assert macro was defined by the kernel in v5.1. + * If the macro is not defined, we define it. + * (kernel source: include/linux/build_bug.h) + */ +#ifndef static_assert + +# define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr) +# define __static_assert(expr, msg, ...) _Static_assert(expr, msg) + +#endif + #endif /* _LTTNG_WRAPPER_COMPILER_H */