Skip to content

DenseMapInfo: support std::optional<T> #147851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions llvm/include/llvm/ADT/DenseMapInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <tuple>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -320,6 +321,28 @@ struct DenseMapInfo<Enum, std::enable_if_t<std::is_enum_v<Enum>>> {

static bool isEqual(const Enum &LHS, const Enum &RHS) { return LHS == RHS; }
};

template <typename T> struct DenseMapInfo<std::optional<T>> {
using Optional = std::optional<T>;
using Info = DenseMapInfo<T>;

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
13 changes: 12 additions & 1 deletion llvm/unittests/ADT/DenseMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <map>
#include <optional>
#include <set>
#include <utility>
#include <variant>
Expand Down Expand Up @@ -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<uint32_t> getTestKey(int i, std::optional<uint32_t> *) {
return i;
}

std::optional<uint32_t> getTestValue(int i, std::optional<uint32_t> *) {
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
Expand Down Expand Up @@ -117,11 +126,13 @@ typedef ::testing::Types<DenseMap<uint32_t, uint32_t>,
DenseMap<uint32_t *, uint32_t *>,
DenseMap<CtorTester, CtorTester, CtorTesterMapInfo>,
DenseMap<EnumClass, uint32_t>,
DenseMap<std::optional<uint32_t>, uint32_t>,
SmallDenseMap<uint32_t, uint32_t>,
SmallDenseMap<uint32_t *, uint32_t *>,
SmallDenseMap<CtorTester, CtorTester, 4,
CtorTesterMapInfo>,
SmallDenseMap<EnumClass, uint32_t>
SmallDenseMap<EnumClass, uint32_t>,
SmallDenseMap<std::optional<uint32_t>, uint32_t>
> DenseMapTestTypes;
// clang-format on

Expand Down