Implement compile time assertion macro wrapper
Starting from C++11[1] and C11[2] both C++ and C standards implement the
`static_assert()` macro that allows the compile time evaluation of an
expression and emitting of a compiler error when the expression
evaluates to false.
This commit implements a wrapper for compile time assertions on C
compilers implementing C standards prior C11.
On such compilers, we emulate a static assert by typedef'ing an array of
negative size in case of predicate failure.
The downside of this method is that error messages are a bit cryptic as
it mentions the negative-sized array. We overcome this issue by using a
user-provided message as part of variable name that gets printed on
error. For this reason, we decide to require 2 different messages in
addition of the predicate.
Here is the signature of the macro:
#define lttng_static_assert(predicate, msg, c_identifier_msg)
The first message, `msg`, is the used with C++/C11 compilers can be any
string. The second message, `c_identifier_msg`, is used with older
standards and MUST be a valid C identifier as it's will be concatenated
to a typedef name.
For example:
If the user uses the macro such as:
lttng_static_assert(false, "My assert message", MY_ASSERT_MSG);
The C99 user will get an error looking like this:
error: size of array ‘lttng_static_assert_MY_ASSERT_MSG’ is negative
The C11 or C++ user will get an error looking like this:
error: static assertion failed: "My assert message"
[1] https://en.cppreference.com/w/cpp/language/static_assert
[2] https://en.cppreference.com/w/c/language/_Static_assert
Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: I725f6e77f1858b8d88ffae781b648ac5b5c64b28
This page took 0.025092 seconds and 4 git commands to generate.