xref: /openbmc/libpldm/src/compiler.h (revision 860a43d996dacf98a94d62fdc7b8484ce04bde23)
166c7723aSAndrew Jeffery /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
266c7723aSAndrew Jeffery #ifndef PLDM_COMPILER_H
366c7723aSAndrew Jeffery #define PLDM_COMPILER_H
466c7723aSAndrew Jeffery 
5*860a43d9SAndrew Jeffery #ifndef __has_attribute
6*860a43d9SAndrew Jeffery #error The libpldm implementation requires __has_attribute
7*860a43d9SAndrew Jeffery #endif
8*860a43d9SAndrew Jeffery 
9*860a43d9SAndrew Jeffery #include <assert.h>
10*860a43d9SAndrew Jeffery 
11*860a43d9SAndrew Jeffery static struct {
12*860a43d9SAndrew Jeffery 	static_assert(__has_attribute(unused),
13*860a43d9SAndrew Jeffery 		      "`unused` attribute is required");
14*860a43d9SAndrew Jeffery 	int compliance;
15*860a43d9SAndrew Jeffery } pldm_required_attributes __attribute__((unused));
16*860a43d9SAndrew Jeffery 
17*860a43d9SAndrew Jeffery #define LIBPLDM_CC_UNUSED __attribute__((unused))
18*860a43d9SAndrew Jeffery 
1966c7723aSAndrew Jeffery // NOLINTBEGIN(bugprone-macro-parentheses)
2066c7723aSAndrew Jeffery /**
2166c7723aSAndrew Jeffery  * Require that the given object is of the specified type.
2266c7723aSAndrew Jeffery  *
2366c7723aSAndrew Jeffery  * If the object is not of the required type then a diagnostic will be emitted.
2466c7723aSAndrew Jeffery  *
2566c7723aSAndrew Jeffery  * If you are reading this documentation due to hitting a compilation error
2666c7723aSAndrew Jeffery  * passing through the macro, then you have a type error in your code that must
2766c7723aSAndrew Jeffery  * be fixed. Despite the compiler output, the error is _not_ that some array
2866c7723aSAndrew Jeffery  * is negatively sized, the array is negatively sized _because_ you have a type
2966c7723aSAndrew Jeffery  * error.
3066c7723aSAndrew Jeffery  *
3166c7723aSAndrew Jeffery  * How this works:
3266c7723aSAndrew Jeffery  *
3366c7723aSAndrew Jeffery  * If the type of @p obj is not equivalent to the provided type @p type then
3466c7723aSAndrew Jeffery  * we force the compiler to evaluate sizeof on a negatively-sized array. The
3566c7723aSAndrew Jeffery  * C standard requires that the integer constant expression that specifies
3666c7723aSAndrew Jeffery  * the array length must be greater than zero. Failure to meet this constraint
3766c7723aSAndrew Jeffery  * generally terminates compilation of the translation unit as any other result
3866c7723aSAndrew Jeffery  * cannot be handled in a sensible way. The array size is derived to an integer
3966c7723aSAndrew Jeffery  * constant expression from a type eqivalence evaluated using _Generic()
4066c7723aSAndrew Jeffery  * allowing us to stay within the language standard. The default generic
4166c7723aSAndrew Jeffery  * association, representing a type mismatch, yields -1.
4266c7723aSAndrew Jeffery  *
4366c7723aSAndrew Jeffery  * pldm_require_obj_type() was introduced into the libpldm implementation to
4466c7723aSAndrew Jeffery  * enable use of the pldm_msgbuf_extract*() APIs for objects that may or may not
4566c7723aSAndrew Jeffery  * reside in a packed struct. See src/msgbuf.h for more details.
4666c7723aSAndrew Jeffery  *
4766c7723aSAndrew Jeffery  * @param obj The name of the object to evaluate
4866c7723aSAndrew Jeffery  * @param type The required type of @p obj
4966c7723aSAndrew Jeffery  *
5066c7723aSAndrew Jeffery  * @return The expression either yields 1, or compilation is terminated
5166c7723aSAndrew Jeffery  */
5266c7723aSAndrew Jeffery #define pldm_require_obj_type(obj, type)                                       \
5366c7723aSAndrew Jeffery 	((void)(sizeof(                                                        \
5466c7723aSAndrew Jeffery 		struct { char buf[_Generic((obj), type: 1, default: -1)]; })))
5566c7723aSAndrew Jeffery // NOLINTEND(bugprone-macro-parentheses)
5666c7723aSAndrew Jeffery 
5766c7723aSAndrew Jeffery #endif
58