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