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*3a2c6589SAndrew Jeffery #include <libpldm/compiler.h> 6*3a2c6589SAndrew Jeffery 7860a43d9SAndrew Jeffery #ifndef __has_attribute 8860a43d9SAndrew Jeffery #error The libpldm implementation requires __has_attribute 9860a43d9SAndrew Jeffery #endif 10860a43d9SAndrew Jeffery 11860a43d9SAndrew Jeffery #include <assert.h> 12860a43d9SAndrew Jeffery 13860a43d9SAndrew Jeffery static struct { 14cb569bc5SAndrew Jeffery static_assert(__has_attribute(always_inline), 15cb569bc5SAndrew Jeffery "`always_inline` attribute is required"); 1690bbe6c0SAndrew Jeffery static_assert(__has_attribute(nonnull), 1790bbe6c0SAndrew Jeffery "`nonnull` attribute is required"); 18860a43d9SAndrew Jeffery static_assert(__has_attribute(unused), 19860a43d9SAndrew Jeffery "`unused` attribute is required"); 200a1be3cbSAndrew Jeffery static_assert(__has_attribute(warn_unused_result), 210a1be3cbSAndrew Jeffery "`warn_unused_result` attribute is required"); 22860a43d9SAndrew Jeffery int compliance; 23860a43d9SAndrew Jeffery } pldm_required_attributes __attribute__((unused)); 24860a43d9SAndrew Jeffery 25*3a2c6589SAndrew Jeffery #ifndef LIBPLDM_CC_ALWAYS_INLINE 26*3a2c6589SAndrew Jeffery #error Missing definition for LIBPLDM_ALWAYS_INLINE 27*3a2c6589SAndrew Jeffery #endif 28*3a2c6589SAndrew Jeffery 29*3a2c6589SAndrew Jeffery #ifndef LIBPLDM_CC_NONNULL 30*3a2c6589SAndrew Jeffery #error Missing definition for LIBPLDM_CC_NONNULL 31*3a2c6589SAndrew Jeffery #endif 32*3a2c6589SAndrew Jeffery 3390bbe6c0SAndrew Jeffery #define LIBPLDM_CC_NONNULL_ARGS(...) __attribute__((nonnull(__VA_ARGS__))) 34860a43d9SAndrew Jeffery #define LIBPLDM_CC_UNUSED __attribute__((unused)) 350a1be3cbSAndrew Jeffery #define LIBPLDM_CC_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 36860a43d9SAndrew Jeffery 3766c7723aSAndrew Jeffery // NOLINTBEGIN(bugprone-macro-parentheses) 3866c7723aSAndrew Jeffery /** 3966c7723aSAndrew Jeffery * Require that the given object is of the specified type. 4066c7723aSAndrew Jeffery * 4166c7723aSAndrew Jeffery * If the object is not of the required type then a diagnostic will be emitted. 4266c7723aSAndrew Jeffery * 4366c7723aSAndrew Jeffery * If you are reading this documentation due to hitting a compilation error 4466c7723aSAndrew Jeffery * passing through the macro, then you have a type error in your code that must 4566c7723aSAndrew Jeffery * be fixed. Despite the compiler output, the error is _not_ that some array 4666c7723aSAndrew Jeffery * is negatively sized, the array is negatively sized _because_ you have a type 4766c7723aSAndrew Jeffery * error. 4866c7723aSAndrew Jeffery * 4966c7723aSAndrew Jeffery * How this works: 5066c7723aSAndrew Jeffery * 5166c7723aSAndrew Jeffery * If the type of @p obj is not equivalent to the provided type @p type then 5266c7723aSAndrew Jeffery * we force the compiler to evaluate sizeof on a negatively-sized array. The 5366c7723aSAndrew Jeffery * C standard requires that the integer constant expression that specifies 5466c7723aSAndrew Jeffery * the array length must be greater than zero. Failure to meet this constraint 5566c7723aSAndrew Jeffery * generally terminates compilation of the translation unit as any other result 5666c7723aSAndrew Jeffery * cannot be handled in a sensible way. The array size is derived to an integer 5766c7723aSAndrew Jeffery * constant expression from a type eqivalence evaluated using _Generic() 5866c7723aSAndrew Jeffery * allowing us to stay within the language standard. The default generic 5966c7723aSAndrew Jeffery * association, representing a type mismatch, yields -1. 6066c7723aSAndrew Jeffery * 6166c7723aSAndrew Jeffery * pldm_require_obj_type() was introduced into the libpldm implementation to 6266c7723aSAndrew Jeffery * enable use of the pldm_msgbuf_extract*() APIs for objects that may or may not 6366c7723aSAndrew Jeffery * reside in a packed struct. See src/msgbuf.h for more details. 6466c7723aSAndrew Jeffery * 6566c7723aSAndrew Jeffery * @param obj The name of the object to evaluate 6666c7723aSAndrew Jeffery * @param type The required type of @p obj 6766c7723aSAndrew Jeffery * 6866c7723aSAndrew Jeffery * @return The expression either yields 1, or compilation is terminated 6966c7723aSAndrew Jeffery */ 7066c7723aSAndrew Jeffery #define pldm_require_obj_type(obj, type) \ 7166c7723aSAndrew Jeffery ((void)(sizeof( \ 7266c7723aSAndrew Jeffery struct { char buf[_Generic((obj), type: 1, default: -1)]; }))) 7366c7723aSAndrew Jeffery // NOLINTEND(bugprone-macro-parentheses) 7466c7723aSAndrew Jeffery 7566c7723aSAndrew Jeffery #endif 76