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/fru.cpp b/tests/dsp/fru.cpp
index 11cbc5d..1f32852 100644
--- a/tests/dsp/fru.cpp
+++ b/tests/dsp/fru.cpp
@@ -13,6 +13,7 @@
TEST(GetFruRecordTableMetadata, testGoodEncodeRequest)
{
std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
auto rc = encode_get_fru_record_table_metadata_req(
0, requestPtr, PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
@@ -28,6 +29,7 @@
auto rc = encode_get_fru_record_table_metadata_req(0, NULL, 0);
ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
rc = encode_get_fru_record_table_metadata_req(0, requestPtr, 1);
ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
@@ -38,8 +40,10 @@
std::vector<uint8_t> responseMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_get_fru_record_table_metadata_resp*>(
responsePtr->payload);
@@ -117,8 +121,10 @@
{
std::vector<uint8_t> responseMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_get_fru_record_table_metadata_resp*>(
responsePtr->payload);
@@ -196,6 +202,7 @@
std::array<uint8_t, sizeof(pldm_msg_hdr) +
PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES>
responseMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
responsePtr->hdr.request = PLDM_RESPONSE;
@@ -217,6 +224,7 @@
fru_table_maximum_size, fru_table_length, total_record_set_identifiers,
total_table_records, checksum, responsePtr);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_get_fru_record_table_metadata_resp*>(
responsePtr->payload);
@@ -268,6 +276,7 @@
std::array<uint8_t, sizeof(pldm_msg_hdr) +
PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES>
responseMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
uint8_t completion_code = PLDM_SUCCESS;
@@ -284,6 +293,7 @@
fru_table_maximum_size, fru_table_length, total_record_set_identifiers,
total_table_records, checksum, NULL);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto response = reinterpret_cast<pldm_get_fru_record_table_metadata_resp*>(
responsePtr->payload);
@@ -305,9 +315,11 @@
std::array<uint8_t,
PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES + sizeof(pldm_msg_hdr)>
requestMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
auto request =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<pldm_get_fru_record_table_req*>(requestPtr->payload);
request->data_transfer_handle = htole32(data_transfer_handle);
@@ -334,6 +346,7 @@
std::array<uint8_t,
sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES>
requestMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
// Payload message is missing
@@ -356,8 +369,10 @@
std::vector<uint8_t> responseMsg(sizeof(pldm_msg_hdr) +
PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
auto response =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<pldm_get_fru_record_table_resp*>(responsePtr->payload);
// Invoke encode get FRU record table response api
@@ -384,6 +399,7 @@
std::vector<uint8_t> responseMsg(sizeof(pldm_msg_hdr) +
PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
auto rc = encode_get_fru_record_table_resp(
0, PLDM_ERROR, next_data_transfer_handle, transfer_flag, responsePtr);
@@ -408,8 +424,10 @@
sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES>
requestMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
auto request =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<pldm_get_fru_record_table_req*>(requestPtr->payload);
// Random value for data transfer handle and transfer operation flag
@@ -457,9 +475,11 @@
PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES +
fru_record_table_data.size());
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
auto response =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<pldm_get_fru_record_table_resp*>(responsePtr->payload);
response->completion_code = completion_code;
@@ -501,6 +521,7 @@
PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES +
fru_record_table_data.size());
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
// Payload message is missing
@@ -531,6 +552,7 @@
constexpr auto payLoadLength = sizeof(pldm_get_fru_record_by_option_req);
std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> request;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto reqMsg = reinterpret_cast<pldm_msg*>(request.data());
auto rc = encode_get_fru_record_by_option_req(
@@ -541,6 +563,7 @@
EXPECT_EQ(instanceId, reqMsg->hdr.instance_id);
auto payLoadMsg =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<pldm_get_fru_record_by_option_req*>(reqMsg->payload);
EXPECT_EQ(le32toh(payLoadMsg->data_transfer_handle), dataTransferHandle);
@@ -561,6 +584,7 @@
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> request;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto reqMsg = reinterpret_cast<pldm_msg*>(request.data());
rc = encode_get_fru_record_by_option_req(1, 2, 3, 4, 5, 6, 0, reqMsg,
@@ -582,6 +606,7 @@
constexpr auto payLoadLength = sizeof(pldm_get_fru_record_by_option_req);
std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> request;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto reqMsg = reinterpret_cast<pldm_msg*>(request.data());
auto rc = encode_get_fru_record_by_option_req(
@@ -615,6 +640,7 @@
{
constexpr auto payLoadLength = sizeof(pldm_get_fru_record_by_option_req);
std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> request{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto reqMsg = reinterpret_cast<pldm_msg*>(request.data());
uint32_t retDataTransferHandle{};
@@ -651,6 +677,7 @@
sizeof(pldm_get_fru_record_by_option_resp) - 1 + fruData.size();
std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> response;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto respMsg = reinterpret_cast<pldm_msg*>(response.data());
auto rc = encode_get_fru_record_by_option_resp(
@@ -658,6 +685,7 @@
fruData.data(), fruData.size(), respMsg, payLoadLength);
auto payLoadMsg =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<pldm_get_fru_record_by_option_resp*>(respMsg->payload);
EXPECT_EQ(rc, PLDM_SUCCESS);
@@ -683,6 +711,7 @@
sizeof(pldm_get_fru_record_by_option_resp) - 1 + fruData.size();
std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> response;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto respMsg = reinterpret_cast<pldm_msg*>(response.data());
auto rc = encode_get_fru_record_by_option_resp(
@@ -710,6 +739,7 @@
sizeof(pldm_get_fru_record_by_option_resp) - 1 + fruData.size();
std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> response;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto respMsg = reinterpret_cast<pldm_msg*>(response.data());
auto rc = encode_get_fru_record_by_option_resp(
@@ -748,6 +778,7 @@
sizeof(pldm_get_fru_record_by_option_resp) - 1 + fruData.size();
std::array<uint8_t, sizeof(pldm_msg_hdr) + payLoadLength> response;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto respMsg = reinterpret_cast<pldm_msg*>(response.data());
auto rc = encode_get_fru_record_by_option_resp(
@@ -785,6 +816,7 @@
sizeof(pldm_msg_hdr) + PLDM_SET_FRU_RECORD_TABLE_RESP_BYTES>
responseMsg{};
struct pldm_msg* response =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<struct pldm_msg*>(responseMsg.data());
auto rc = encode_set_fru_record_table_resp(
instanceId, completionCode, nextDataTransferHandle,
@@ -792,6 +824,7 @@
EXPECT_EQ(rc, PLDM_SUCCESS);
struct pldm_set_fru_record_table_resp* resp =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<struct pldm_set_fru_record_table_resp*>(
response->payload);
EXPECT_EQ(completionCode, resp->completion_code);
@@ -808,6 +841,7 @@
sizeof(pldm_msg_hdr) + PLDM_SET_FRU_RECORD_TABLE_RESP_BYTES>
responseMsg{};
struct pldm_msg* response =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<struct pldm_msg*>(responseMsg.data());
auto rc = encode_set_fru_record_table_resp(
@@ -831,8 +865,10 @@
PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES +
sizeof(tableData)>
requestMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
struct pldm_set_fru_record_table_req* req =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<struct pldm_set_fru_record_table_req*>(
request->payload);
req->data_transfer_handle = htole32(transferHandle);
@@ -864,8 +900,10 @@
PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES +
sizeof(tableData)>
requestMsg{};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
struct pldm_set_fru_record_table_req* req =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<struct pldm_set_fru_record_table_req*>(
request->payload);
req->data_transfer_handle = htole32(transferHandle);