compiler: Provide LIBPLDM_CC_UNUSED

Ensure that we have __has_attribute available, and then further
ensure that the unused attribute is provided. Once satisfied, define
LIBPDLM_CC_UNUSED and replace raw use of __attribute__((unused)).

Change-Id: I2433039297d5fdedb8b8d99b30e73e4542d9069f
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/src/compiler.h b/src/compiler.h
index 31eeff9..e704518 100644
--- a/src/compiler.h
+++ b/src/compiler.h
@@ -2,6 +2,20 @@
 #ifndef PLDM_COMPILER_H
 #define PLDM_COMPILER_H
 
+#ifndef __has_attribute
+#error The libpldm implementation requires __has_attribute
+#endif
+
+#include <assert.h>
+
+static struct {
+	static_assert(__has_attribute(unused),
+		      "`unused` attribute is required");
+	int compliance;
+} pldm_required_attributes __attribute__((unused));
+
+#define LIBPLDM_CC_UNUSED __attribute__((unused))
+
 // NOLINTBEGIN(bugprone-macro-parentheses)
 /**
  * Require that the given object is of the specified type.
diff --git a/src/dsp/pdr.c b/src/dsp/pdr.c
index 0c73919..45f1d86 100644
--- a/src/dsp/pdr.c
+++ b/src/dsp/pdr.c
@@ -1,4 +1,5 @@
 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
+#include "compiler.h"
 #include "msgbuf.h"
 #include <libpldm/pdr.h>
 #include <libpldm/platform.h>
@@ -253,8 +254,7 @@
 }
 
 LIBPLDM_ABI_STABLE
-uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo
-				    __attribute__((unused)),
+uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo LIBPLDM_CC_UNUSED,
 				    const pldm_pdr_record *record)
 {
 	assert(repo != NULL);
@@ -264,8 +264,7 @@
 }
 
 LIBPLDM_ABI_TESTING
-uint16_t pldm_pdr_get_terminus_handle(const pldm_pdr *repo
-				      __attribute__((unused)),
+uint16_t pldm_pdr_get_terminus_handle(const pldm_pdr *repo LIBPLDM_CC_UNUSED,
 				      const pldm_pdr_record *record)
 {
 	assert(repo != NULL);
@@ -1276,7 +1275,7 @@
 	}
 
 	const uint8_t *start = (uint8_t *)pdr;
-	const uint8_t *end __attribute__((unused)) =
+	const uint8_t *end LIBPLDM_CC_UNUSED =
 		start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length);
 	start += sizeof(struct pldm_pdr_hdr);
 	struct pldm_pdr_entity_association *entity_association_pdr =
diff --git a/src/msgbuf.h b/src/msgbuf.h
index c2c02fe..16e3680 100644
--- a/src/msgbuf.h
+++ b/src/msgbuf.h
@@ -2,6 +2,8 @@
 #ifndef PLDM_MSGBUF_H
 #define PLDM_MSGBUF_H
 
+#include "compiler.h"
+
 /*
  * Historically, many of the structs exposed in libpldm's public headers are
  * defined with __attribute__((packed)). This is unfortunate: it gives the
@@ -68,7 +70,7 @@
 		      "Extraction and insertion arithmetic may be broken");
 	static_assert(PLDM_SUCCESS == 0, "Error handling is broken");
 	int compliance;
-} build_assertions __attribute__((unused));
+} build_assertions LIBPLDM_CC_UNUSED;
 
 enum pldm_msgbuf_error_mode {
 	PLDM_MSGBUF_PLDM_CC = 0x5a,
diff --git a/src/requester/pldm.c b/src/requester/pldm.c
index 17d2492..8e88635 100644
--- a/src/requester/pldm.c
+++ b/src/requester/pldm.c
@@ -1,4 +1,5 @@
 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
+#include "compiler.h"
 #include <libpldm/base.h>
 #include <libpldm/pldm.h>
 #include <libpldm/transport.h>
@@ -155,7 +156,7 @@
 
 LIBPLDM_ABI_DEPRECATED
 pldm_requester_rc_t pldm_recv(mctp_eid_t eid, int mctp_fd,
-			      __attribute__((unused)) uint8_t instance_id,
+			      LIBPLDM_CC_UNUSED uint8_t instance_id,
 			      uint8_t **pldm_resp_msg, size_t *resp_msg_len)
 {
 	pldm_requester_rc_t rc =
diff --git a/src/transport/af-mctp.c b/src/transport/af-mctp.c
index ea79863..a66e79d0 100644
--- a/src/transport/af-mctp.c
+++ b/src/transport/af-mctp.c
@@ -1,4 +1,5 @@
 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
+#include "compiler.h"
 #include "container-of.h"
 #include "mctp-defines.h"
 #include "responder.h"
@@ -95,7 +96,7 @@
 
 LIBPLDM_ABI_STABLE
 int pldm_transport_af_mctp_unmap_tid(struct pldm_transport_af_mctp *ctx,
-				     __attribute__((unused)) pldm_tid_t tid,
+				     LIBPLDM_CC_UNUSED pldm_tid_t tid,
 				     mctp_eid_t eid)
 {
 	ctx->tid_eid_map[eid] = 0;
diff --git a/src/transport/mctp-demux.c b/src/transport/mctp-demux.c
index 0ea1cf6..a4f7cbe 100644
--- a/src/transport/mctp-demux.c
+++ b/src/transport/mctp-demux.c
@@ -1,4 +1,5 @@
 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
+#include "compiler.h"
 #include "container-of.h"
 #include "mctp-defines.h"
 #include "socket.h"
@@ -116,7 +117,7 @@
 
 LIBPLDM_ABI_STABLE
 int pldm_transport_mctp_demux_unmap_tid(struct pldm_transport_mctp_demux *ctx,
-					__attribute__((unused)) pldm_tid_t tid,
+					LIBPLDM_CC_UNUSED pldm_tid_t tid,
 					mctp_eid_t eid)
 {
 	ctx->tid_eid_map[eid] = 0;
diff --git a/src/transport/transport.c b/src/transport/transport.c
index 33197ab..24c264a 100644
--- a/src/transport/transport.c
+++ b/src/transport/transport.c
@@ -1,4 +1,5 @@
 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
+#include "compiler.h"
 #include "transport.h"
 
 #include <libpldm/transport.h>
@@ -23,9 +24,9 @@
 	short revents; /* returned events */
 };
 
-static inline int poll(struct pollfd *fds __attribute__((unused)),
-		       int nfds __attribute__((unused)),
-		       int timeout __attribute__((unused)))
+static inline int poll(struct pollfd *fds LIBPLDM_CC_UNUSED,
+		       int nfds LIBPLDM_CC_UNUSED,
+		       int timeout LIBPLDM_CC_UNUSED)
 {
 	return 0;
 }
diff --git a/tests/dsp/bios_table_iter.c b/tests/dsp/bios_table_iter.c
index a3228fc..7260054 100644
--- a/tests/dsp/bios_table_iter.c
+++ b/tests/dsp/bios_table_iter.c
@@ -9,12 +9,14 @@
 #include <stdint.h>
 #include <stdlib.h>
 
+#include "compiler.h"
+
 /* NOLINTNEXTLINE(bugprone-suspicious-include) */
 #include "dsp/bios_table.c"
 
 /* Satisfy the symbol needs of bios_table.c */
-uint32_t crc32(const void* data __attribute__((unused)),
-               size_t size __attribute__((unused)))
+uint32_t crc32(const void* data LIBPLDM_CC_UNUSED,
+               size_t size LIBPLDM_CC_UNUSED)
 {
     return 0;
 }