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/bios_table.cpp b/tests/dsp/bios_table.cpp
index 99e17ba..19fb929 100644
--- a/tests/dsp/bios_table.cpp
+++ b/tests/dsp/bios_table.cpp
@@ -49,6 +49,7 @@
0 /* default value string handle index */
};
auto entry =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<struct pldm_bios_attr_table_entry*>(enumEntry.data());
auto attrHandle = pldm_bios_table_attr_entry_decode_attribute_handle(entry);
EXPECT_EQ(attrHandle, 2);
@@ -72,6 +73,7 @@
};
auto entry =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<struct pldm_bios_attr_table_entry*>(enumEntry.data());
uint8_t pvNumber;
ASSERT_EQ(pldm_bios_table_attr_entry_enum_decode_pv_num(entry, &pvNumber),
@@ -201,6 +203,7 @@
'a', 'b', 'c' /* default string */
};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto entry = reinterpret_cast<struct pldm_bios_attr_table_entry*>(
stringEntry.data());
auto stringType =
@@ -416,6 +419,7 @@
uint64_t upper;
uint64_t def;
uint32_t scalar;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto entry = reinterpret_cast<struct pldm_bios_attr_table_entry*>(
integerEntry.data());
pldm_bios_table_attr_entry_integer_decode(entry, &lower, &upper, &scalar,
@@ -526,6 +530,7 @@
auto entry =
pldm_bios_table_attr_find_by_handle(table.data(), table.size(), 1);
EXPECT_NE(entry, nullptr);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()),
ElementsAreArray(stringEntry));
@@ -536,6 +541,7 @@
entry = pldm_bios_table_attr_find_by_string_handle(table.data(),
table.size(), 2);
EXPECT_NE(entry, nullptr);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()),
ElementsAreArray(stringEntry));
@@ -554,6 +560,7 @@
0, /* current value string handle index */
1, /* current value string handle index */
};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
enumEntry.data());
auto attrHandle =
@@ -592,6 +599,7 @@
handles);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(encodeEntry, enumEntry);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
enumEntry.data());
entry->attr_type = PLDM_BIOS_ENUMERATION_READ_ONLY;
@@ -620,6 +628,7 @@
1, /* current value string handle index */
};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
enumEntry.data());
auto number = pldm_bios_table_attr_value_entry_enum_decode_number(entry);
@@ -673,6 +682,7 @@
encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING, 3, "abc");
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(encodeEntry, stringEntry);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
stringEntry.data());
entry->attr_type = PLDM_BIOS_STRING_READ_ONLY;
@@ -700,6 +710,7 @@
'a', 'b', 'c', /* default value string handle index */
};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
stringEntry.data());
auto length = pldm_bios_table_attr_value_entry_string_decode_length(entry);
@@ -745,6 +756,7 @@
encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER, 10);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(encodeEntry, integerEntry);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
integerEntry.data());
entry->attr_type = PLDM_BIOS_INTEGER_READ_ONLY;
@@ -771,6 +783,7 @@
10, 0, 0, 0, 0, 0, 0, 0, /* current value */
};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
integerEntry.data());
auto cv = pldm_bios_table_attr_value_entry_integer_decode_cv(entry);
@@ -805,24 +818,28 @@
PLDM_BIOS_ATTR_VAL_TABLE);
auto entry = pldm_bios_table_iter_attr_value_entry_value(iter);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + enumEntry.size()),
ElementsAreArray(enumEntry));
pldm_bios_table_iter_next(iter);
entry = pldm_bios_table_iter_attr_value_entry_value(iter);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()),
ElementsAreArray(stringEntry));
pldm_bios_table_iter_next(iter);
entry = pldm_bios_table_iter_attr_value_entry_value(iter);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + integerEntry.size()),
ElementsAreArray(integerEntry));
pldm_bios_table_iter_next(iter);
entry = pldm_bios_table_iter_attr_value_entry_value(iter);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + enumEntry.size()),
ElementsAreArray(enumEntry));
@@ -860,6 +877,7 @@
auto entry = pldm_bios_table_attr_value_find_by_handle(table.data(),
table.size(), 1);
EXPECT_NE(entry, nullptr);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()),
ElementsAreArray(stringEntry));
@@ -869,6 +887,7 @@
EXPECT_EQ(entry, nullptr);
auto firstEntry =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(table.data());
firstEntry->attr_type = PLDM_BIOS_PASSWORD;
#ifdef NDEBUG
@@ -1015,6 +1034,7 @@
7, 0, /* string length */
'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */
};
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto entry = reinterpret_cast<struct pldm_bios_string_table_entry*>(
stringEntry.data());
auto handle = pldm_bios_table_string_entry_decode_handle(entry);
@@ -1128,6 +1148,7 @@
/* first entry */
auto attr_entry =
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<struct pldm_bios_attr_table_entry*>(table.data());
auto iter = pldm_bios_table_iter_create(table.data(), table.size(),
PLDM_BIOS_ATTR_TABLE);