X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=include%2Flttng%2Fust-compiler.h;h=e7d72e0da3fd21843b8caa0879200a4c43c38f21;hb=7f2640686401c087210073de0631caa0a1ee4fbe;hp=c289628580ee420346a24e6e72bc9f7fa9299ebd;hpb=dcd9a9d798f85ccee281d66f85ff4a9b5520a088;p=deliverable%2Flttng-ust.git diff --git a/include/lttng/ust-compiler.h b/include/lttng/ust-compiler.h index c2896285..e7d72e0d 100644 --- a/include/lttng/ust-compiler.h +++ b/include/lttng/ust-compiler.h @@ -24,6 +24,8 @@ * SOFTWARE. */ +#include + #define lttng_ust_notrace __attribute__((no_instrument_function)) #define LTTNG_PACKED __attribute__((__packed__)) @@ -71,4 +73,31 @@ #define __LTTNG_COMPOUND_LITERAL(type, ...) (type[]) { __VA_ARGS__ } #endif +/* + * Compile time assertion. + * - predicate: boolean expression to evaluate, + * - msg: string to print to the user on failure when `static_assert()` is + * supported, + * - c_identifier_msg: message to be included in the typedef to emulate a + * static assertion. This parameter must be a valid C identifier as it will + * be used as a typedef name. + */ +#if defined (__cplusplus) || __STDC_VERSION__ >= 201112L +#define lttng_static_assert(predicate, msg, c_identifier_msg) \ + static_assert(predicate, msg) +#else +/* + * Evaluates the predicate and emit a compilation error on failure. + * + * If the predicate evaluates to true, this macro emits a typedef of an array + * of size 0. + * + * If the predicate evaluates to false, this macro emits a typedef of an array + * of negative size which is invalid in C and forces a compiler error. The msg + * parameter is used in the tentative typedef so it is printed to the user. + */ +#define lttng_static_assert(predicate, msg, c_identifier_msg) \ + typedef char lttng_static_assert_##c_identifier_msg[2*!!(predicate)-1]; +#endif + #endif /* _LTTNG_UST_COMPILER_H */