diff --git a/llvm/include/llvm/ADT/DenseMapInfo.h b/llvm/include/llvm/ADT/DenseMapInfo.h index 07c37e353a40b..b850223c953da 100644 --- a/llvm/include/llvm/ADT/DenseMapInfo.h +++ b/llvm/include/llvm/ADT/DenseMapInfo.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -320,6 +321,28 @@ struct DenseMapInfo>> { static bool isEqual(const Enum &LHS, const Enum &RHS) { return LHS == RHS; } }; + +template struct DenseMapInfo> { + using Optional = std::optional; + using Info = DenseMapInfo; + + static inline Optional getEmptyKey() { return {Info::getEmptyKey()}; } + + static inline Optional getTombstoneKey() { return {Info::getTombstoneKey()}; } + + static unsigned getHashValue(const Optional &OptionalVal) { + return detail::combineHashValue( + OptionalVal.has_value(), + Info::getHashValue(OptionalVal.value_or(Info::getEmptyKey()))); + } + + static bool isEqual(const Optional &LHS, const Optional &RHS) { + if (LHS && RHS) { + return Info::isEqual(LHS.value(), RHS.value()); + } + return !LHS && !RHS; + } +}; } // end namespace llvm #endif // LLVM_ADT_DENSEMAPINFO_H diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp index c95f96c4bb3c6..e3e8e8cf059ec 100644 --- a/llvm/unittests/ADT/DenseMapTest.cpp +++ b/llvm/unittests/ADT/DenseMapTest.cpp @@ -15,6 +15,7 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" #include +#include #include #include #include @@ -86,6 +87,14 @@ struct CtorTesterMapInfo { CtorTester getTestKey(int i, CtorTester *) { return CtorTester(i); } CtorTester getTestValue(int i, CtorTester *) { return CtorTester(42 + i); } +std::optional getTestKey(int i, std::optional *) { + return i; +} + +std::optional getTestValue(int i, std::optional *) { + return 42 + i; +} + // Test fixture, with helper functions implemented by forwarding to global // function overloads selected by component types of the type parameter. This // allows all of the map implementations to be tested with shared @@ -117,11 +126,13 @@ typedef ::testing::Types, DenseMap, DenseMap, DenseMap, + DenseMap, uint32_t>, SmallDenseMap, SmallDenseMap, SmallDenseMap, - SmallDenseMap + SmallDenseMap, + SmallDenseMap, uint32_t> > DenseMapTestTypes; // clang-format on