clang-tidy: Enable cppcoreguidelines-pro-type-reinterpret-cast
There are ways to avoid reinterpret_cast<>() in many circumstances.
For libpldm, often its use can be replaced with placement-new.
Enable the lint for reinterpret_cast<>() to prevent its use in future
changes. This way we can stay inside the language rather than rely on
implementation-defined behavior.
Existing uses of reinterpret_cast<>() are marked NOLINT for now, with
the intent that we clean them up over time. The insertion of the NOLINTs
was automated with sed.
Change-Id: If6654f774e438de9262fe9f9012c6b574764c326
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/tests/dsp/base.cpp b/tests/dsp/base.cpp
index c95abdf..5e71c80 100644
--- a/tests/dsp/base.cpp
+++ b/tests/dsp/base.cpp
@@ -200,6 +200,7 @@
ver32_t version{0xff, 0xff, 0xff, 0xff};
std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
requestMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
auto rc = encode_get_commands_req(0, pldmType, version, request);
@@ -221,6 +222,7 @@
memcpy(requestMsg.data() + sizeof(pldmType) + hdrSize, &version,
sizeof(version));
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
auto rc = decode_get_commands_req(request, requestMsg.size() - hdrSize,
&pldmTypeOut, &versionOut);
@@ -235,6 +237,7 @@
uint8_t completionCode = 0;
std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES>
responseMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
std::array<bitfield8_t, PLDM_MAX_CMDS_PER_TYPE / 8> commands{};
commands[0].byte = 1;
@@ -258,6 +261,7 @@
uint8_t completionCode = 0;
std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES>
responseMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
std::array<bitfield8_t, PLDM_MAX_TYPES / 8> types{};
types[0].byte = 1;
@@ -285,6 +289,7 @@
uint8_t completion_code;
responseMsg[hdrSize] = PLDM_SUCCESS;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
auto rc = decode_get_types_resp(response, responseMsg.size() - hdrSize,
@@ -308,6 +313,7 @@
uint8_t retcompletion_code = 0;
responseMsg[hdrSize] = PLDM_SUCCESS;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
auto rc = decode_get_types_resp(response, responseMsg.size() - hdrSize - 1,
@@ -327,6 +333,7 @@
uint8_t completion_code;
responseMsg[hdrSize] = PLDM_SUCCESS;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
auto rc = decode_get_commands_resp(response, responseMsg.size() - hdrSize,
@@ -350,6 +357,7 @@
uint8_t retcompletion_code = 0;
responseMsg[hdrSize] = PLDM_SUCCESS;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
auto rc =
@@ -363,6 +371,7 @@
{
std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
requestMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
uint8_t pldmType = 0x03;
uint32_t transferHandle = 0x0;
@@ -399,6 +408,7 @@
uint8_t flag = PLDM_START_AND_END;
std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES>
responseMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
ver32_t version = {0xff, 0xff, 0xff, 0xff};
@@ -434,6 +444,7 @@
memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(flag) + hdrSize,
&pldmType, sizeof(pldmType));
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
auto rc = decode_get_version_req(request, requestMsg.size() - hdrSize,
@@ -467,6 +478,7 @@
sizeof(transferHandle) + sizeof(flag) + hdrSize,
&version, sizeof(version));
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
auto rc = decode_get_version_resp(response, responseMsg.size() - hdrSize,
@@ -495,6 +507,7 @@
uint8_t completionCode = 0;
std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES>
responseMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
uint8_t tid = 1;
@@ -514,6 +527,7 @@
uint8_t completion_code;
responseMsg[hdrSize] = PLDM_SUCCESS;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
auto rc = decode_get_tid_resp(response, responseMsg.size() - hdrSize,
@@ -555,6 +569,7 @@
std::memcpy(req.data(), &hdr, sizeof(hdr));
std::memcpy(req.data() + sizeof(hdr), &req_pkt, sizeof(req_pkt));
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
pldm_msg* pldm_request = reinterpret_cast<pldm_msg*>(req.data());
int rc = decode_multipart_receive_req(
pldm_request, req.size() - hdrSize, &pldm_type, &flag, &transfer_ctx,
@@ -599,6 +614,7 @@
std::memcpy(req.data(), &hdr, sizeof(hdr));
std::memcpy(req.data() + sizeof(hdr), &req_pkt, sizeof(req_pkt));
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
pldm_msg* pldm_request = reinterpret_cast<pldm_msg*>(req.data());
EXPECT_EQ(decode_multipart_receive_req(
pldm_request, (req.size() - hdrSize) + 1, &pldm_type, &flag,
@@ -630,6 +646,7 @@
std::memcpy(req.data(), &hdr, sizeof(hdr));
std::memcpy(req.data() + sizeof(hdr), &req_pkt, sizeof(req_pkt));
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
pldm_msg* pldm_request = reinterpret_cast<pldm_msg*>(req.data());
EXPECT_EQ(decode_multipart_receive_req(pldm_request, req.size() - hdrSize,
&pldm_type, &flag, &transfer_ctx,
@@ -661,6 +678,7 @@
std::memcpy(req.data(), &hdr, sizeof(hdr));
std::memcpy(req.data() + sizeof(hdr), &req_pkt, sizeof(req_pkt));
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
pldm_msg* pldm_request = reinterpret_cast<pldm_msg*>(req.data());
EXPECT_EQ(decode_multipart_receive_req(pldm_request, req.size() - hdrSize,
&pldm_type, &flag, &transfer_ctx,
@@ -696,6 +714,7 @@
std::memcpy(req.data(), &hdr, sizeof(hdr));
std::memcpy(req.data() + sizeof(hdr), &req_pkt, sizeof(req_pkt));
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
pldm_msg* pldm_request = reinterpret_cast<pldm_msg*>(req.data());
EXPECT_EQ(decode_multipart_receive_req(pldm_request, req.size() - hdrSize,
&pldm_type, &flag, &transfer_ctx,
@@ -731,6 +750,7 @@
std::memcpy(req.data(), &hdr, sizeof(hdr));
std::memcpy(req.data() + sizeof(hdr), &req_pkt, sizeof(req_pkt));
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
pldm_msg* pldm_request = reinterpret_cast<pldm_msg*>(req.data());
EXPECT_EQ(decode_multipart_receive_req(pldm_request, req.size() - hdrSize,
&pldm_type, &flag, &transfer_ctx,
@@ -748,6 +768,7 @@
3 /*completion code*/, &responseMsg);
EXPECT_EQ(rc, PLDM_SUCCESS);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto p = reinterpret_cast<uint8_t*>(&responseMsg);
EXPECT_THAT(std::vector<uint8_t>(p, p + sizeof(responseMsg)),
ElementsAreArray({0, 1, 2, 3}));
@@ -764,6 +785,7 @@
uint8_t instanceId = 0;
uint8_t tid = 0x01;
std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(tid)> requestMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
auto rc = encode_set_tid_req(instanceId, tid, request);
@@ -781,6 +803,7 @@
{
uint8_t tid = 0x01;
std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(tid)> requestMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
auto rc = encode_set_tid_req(0, tid, nullptr);