libpldm: Introduce LIBPLDM_CC_COUNTED_BY()

The macro wraps __attribute__((counted_by(x))) if compiler support
is available. The `counted_by(...)` attribute gives the compiler the
necessary information to track invalid accesses for flexible array
members.

See the related GCC documentation at [1].

[1]: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-counted_005fby-variable-attribute

Change-Id: If87e022d27df03b7e882df95f8c2452a4d4b828f
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/docs/checklists/changes.md b/docs/checklists/changes.md
index aed44f6..6ee0e38 100644
--- a/docs/checklists/changes.md
+++ b/docs/checklists/changes.md
@@ -112,6 +112,8 @@
       access beyond the first element invokes undefined behaviour in both C and
       C++.
 
+  - [ ] I've annotated the flexible array member with `LIBPLDM_CC_COUNTED_BY()`
+
 [^1]:
     [C17 draft specification][c17-draft-standard], 6.7.2.1 Structure and union
     specifiers, paragraph 18.
diff --git a/include/libpldm/compiler.h b/include/libpldm/compiler.h
new file mode 100644
index 0000000..f8d7dc7
--- /dev/null
+++ b/include/libpldm/compiler.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
+
+#ifndef LIBPLDM_COMPILER_H
+#define LIBPLDM_COMPILER_H
+
+#if defined __has_attribute
+#if __has_attribute(counted_by)
+#define LIBPLDM_CC_COUNTED_BY(x) __attribute__((counted_by(x)))
+#endif
+#endif
+
+#ifndef LIBPLDM_CC_COUNTED_BY
+#define LIBPLDM_CC_COUNTED_BY(x)
+#endif
+
+#endif
diff --git a/include/libpldm/meson.build b/include/libpldm/meson.build
index a4e9c8e..014c969 100644
--- a/include/libpldm/meson.build
+++ b/include/libpldm/meson.build
@@ -2,6 +2,7 @@
   'base.h',
   'bios.h',
   'bios_table.h',
+  'compiler.h',
   'entity.h',
   'firmware_update.h',
   'fru.h',