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