Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
| 2 | #ifndef PLDM_COMPILER_H |
| 3 | #define PLDM_COMPILER_H |
| 4 | |
Andrew Jeffery | 860a43d | 2024-08-23 01:21:58 +0000 | [diff] [blame] | 5 | #ifndef __has_attribute |
| 6 | #error The libpldm implementation requires __has_attribute |
| 7 | #endif |
| 8 | |
| 9 | #include <assert.h> |
| 10 | |
| 11 | static struct { |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 12 | static_assert(__has_attribute(always_inline), |
| 13 | "`always_inline` attribute is required"); |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 14 | static_assert(__has_attribute(nonnull), |
| 15 | "`nonnull` attribute is required"); |
Andrew Jeffery | 860a43d | 2024-08-23 01:21:58 +0000 | [diff] [blame] | 16 | static_assert(__has_attribute(unused), |
| 17 | "`unused` attribute is required"); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 18 | static_assert(__has_attribute(warn_unused_result), |
| 19 | "`warn_unused_result` attribute is required"); |
Andrew Jeffery | 860a43d | 2024-08-23 01:21:58 +0000 | [diff] [blame] | 20 | int compliance; |
| 21 | } pldm_required_attributes __attribute__((unused)); |
| 22 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 23 | #define LIBPLDM_CC_ALWAYS_INLINE __attribute__((always_inline)) static inline |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 24 | #define LIBPLDM_CC_NONNULL __attribute__((nonnull)) |
| 25 | #define LIBPLDM_CC_NONNULL_ARGS(...) __attribute__((nonnull(__VA_ARGS__))) |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 26 | #define LIBPLDM_CC_UNUSED __attribute__((unused)) |
| 27 | #define LIBPLDM_CC_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
Andrew Jeffery | 860a43d | 2024-08-23 01:21:58 +0000 | [diff] [blame] | 28 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 29 | // NOLINTBEGIN(bugprone-macro-parentheses) |
| 30 | /** |
| 31 | * Require that the given object is of the specified type. |
| 32 | * |
| 33 | * If the object is not of the required type then a diagnostic will be emitted. |
| 34 | * |
| 35 | * If you are reading this documentation due to hitting a compilation error |
| 36 | * passing through the macro, then you have a type error in your code that must |
| 37 | * be fixed. Despite the compiler output, the error is _not_ that some array |
| 38 | * is negatively sized, the array is negatively sized _because_ you have a type |
| 39 | * error. |
| 40 | * |
| 41 | * How this works: |
| 42 | * |
| 43 | * If the type of @p obj is not equivalent to the provided type @p type then |
| 44 | * we force the compiler to evaluate sizeof on a negatively-sized array. The |
| 45 | * C standard requires that the integer constant expression that specifies |
| 46 | * the array length must be greater than zero. Failure to meet this constraint |
| 47 | * generally terminates compilation of the translation unit as any other result |
| 48 | * cannot be handled in a sensible way. The array size is derived to an integer |
| 49 | * constant expression from a type eqivalence evaluated using _Generic() |
| 50 | * allowing us to stay within the language standard. The default generic |
| 51 | * association, representing a type mismatch, yields -1. |
| 52 | * |
| 53 | * pldm_require_obj_type() was introduced into the libpldm implementation to |
| 54 | * enable use of the pldm_msgbuf_extract*() APIs for objects that may or may not |
| 55 | * reside in a packed struct. See src/msgbuf.h for more details. |
| 56 | * |
| 57 | * @param obj The name of the object to evaluate |
| 58 | * @param type The required type of @p obj |
| 59 | * |
| 60 | * @return The expression either yields 1, or compilation is terminated |
| 61 | */ |
| 62 | #define pldm_require_obj_type(obj, type) \ |
| 63 | ((void)(sizeof( \ |
| 64 | struct { char buf[_Generic((obj), type: 1, default: -1)]; }))) |
| 65 | // NOLINTEND(bugprone-macro-parentheses) |
| 66 | |
| 67 | #endif |