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