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 5860a43d9SAndrew Jeffery #ifndef __has_attribute 6860a43d9SAndrew Jeffery #error The libpldm implementation requires __has_attribute 7860a43d9SAndrew Jeffery #endif 8860a43d9SAndrew Jeffery 9860a43d9SAndrew Jeffery #include <assert.h> 10860a43d9SAndrew Jeffery 11860a43d9SAndrew Jeffery static struct { 12cb569bc5SAndrew Jeffery static_assert(__has_attribute(always_inline), 13cb569bc5SAndrew Jeffery "`always_inline` attribute is required"); 14*90bbe6c0SAndrew Jeffery static_assert(__has_attribute(nonnull), 15*90bbe6c0SAndrew Jeffery "`nonnull` attribute is required"); 16860a43d9SAndrew Jeffery static_assert(__has_attribute(unused), 17860a43d9SAndrew Jeffery "`unused` attribute is required"); 180a1be3cbSAndrew Jeffery static_assert(__has_attribute(warn_unused_result), 190a1be3cbSAndrew Jeffery "`warn_unused_result` attribute is required"); 20860a43d9SAndrew Jeffery int compliance; 21860a43d9SAndrew Jeffery } pldm_required_attributes __attribute__((unused)); 22860a43d9SAndrew Jeffery 23cb569bc5SAndrew Jeffery #define LIBPLDM_CC_ALWAYS_INLINE __attribute__((always_inline)) static inline 24*90bbe6c0SAndrew Jeffery #define LIBPLDM_CC_NONNULL __attribute__((nonnull)) 25*90bbe6c0SAndrew Jeffery #define LIBPLDM_CC_NONNULL_ARGS(...) __attribute__((nonnull(__VA_ARGS__))) 26860a43d9SAndrew Jeffery #define LIBPLDM_CC_UNUSED __attribute__((unused)) 270a1be3cbSAndrew Jeffery #define LIBPLDM_CC_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 28860a43d9SAndrew Jeffery 2966c7723aSAndrew Jeffery // NOLINTBEGIN(bugprone-macro-parentheses) 3066c7723aSAndrew Jeffery /** 3166c7723aSAndrew Jeffery * Require that the given object is of the specified type. 3266c7723aSAndrew Jeffery * 3366c7723aSAndrew Jeffery * If the object is not of the required type then a diagnostic will be emitted. 3466c7723aSAndrew Jeffery * 3566c7723aSAndrew Jeffery * If you are reading this documentation due to hitting a compilation error 3666c7723aSAndrew Jeffery * passing through the macro, then you have a type error in your code that must 3766c7723aSAndrew Jeffery * be fixed. Despite the compiler output, the error is _not_ that some array 3866c7723aSAndrew Jeffery * is negatively sized, the array is negatively sized _because_ you have a type 3966c7723aSAndrew Jeffery * error. 4066c7723aSAndrew Jeffery * 4166c7723aSAndrew Jeffery * How this works: 4266c7723aSAndrew Jeffery * 4366c7723aSAndrew Jeffery * If the type of @p obj is not equivalent to the provided type @p type then 4466c7723aSAndrew Jeffery * we force the compiler to evaluate sizeof on a negatively-sized array. The 4566c7723aSAndrew Jeffery * C standard requires that the integer constant expression that specifies 4666c7723aSAndrew Jeffery * the array length must be greater than zero. Failure to meet this constraint 4766c7723aSAndrew Jeffery * generally terminates compilation of the translation unit as any other result 4866c7723aSAndrew Jeffery * cannot be handled in a sensible way. The array size is derived to an integer 4966c7723aSAndrew Jeffery * constant expression from a type eqivalence evaluated using _Generic() 5066c7723aSAndrew Jeffery * allowing us to stay within the language standard. The default generic 5166c7723aSAndrew Jeffery * association, representing a type mismatch, yields -1. 5266c7723aSAndrew Jeffery * 5366c7723aSAndrew Jeffery * pldm_require_obj_type() was introduced into the libpldm implementation to 5466c7723aSAndrew Jeffery * enable use of the pldm_msgbuf_extract*() APIs for objects that may or may not 5566c7723aSAndrew Jeffery * reside in a packed struct. See src/msgbuf.h for more details. 5666c7723aSAndrew Jeffery * 5766c7723aSAndrew Jeffery * @param obj The name of the object to evaluate 5866c7723aSAndrew Jeffery * @param type The required type of @p obj 5966c7723aSAndrew Jeffery * 6066c7723aSAndrew Jeffery * @return The expression either yields 1, or compilation is terminated 6166c7723aSAndrew Jeffery */ 6266c7723aSAndrew Jeffery #define pldm_require_obj_type(obj, type) \ 6366c7723aSAndrew Jeffery ((void)(sizeof( \ 6466c7723aSAndrew Jeffery struct { char buf[_Generic((obj), type: 1, default: -1)]; }))) 6566c7723aSAndrew Jeffery // NOLINTEND(bugprone-macro-parentheses) 6666c7723aSAndrew Jeffery 6766c7723aSAndrew Jeffery #endif 68