Skip to content

Commit 0cb2dd5

Browse files
committed
[include-cleaner] Make Symbol (and Macro) hashable.
For now, we decided not to add operator< or handle other variants. (If we do so in future we may want to extract a base class). Differential Revision: https://reviews.llvm.org/D138648
1 parent a72609c commit 0cb2dd5

File tree

1 file changed

+35
-1
lines changed
  • clang-tools-extra/include-cleaner/include/clang-include-cleaner

1 file changed

+35
-1
lines changed

clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ struct Symbol {
6464
struct Macro macro() const { return std::get<Macro>(Storage); }
6565

6666
private:
67-
// FIXME: Add support for macros.
6867
// Order must match Kind enum!
6968
std::variant<const Decl *, struct Macro> Storage;
69+
70+
Symbol(decltype(Storage) Sentinel) : Storage(std::move(Sentinel)) {}
71+
friend llvm::DenseMapInfo<Symbol>;
7072
};
7173
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Symbol &);
7274

@@ -137,4 +139,36 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Include &);
137139
} // namespace include_cleaner
138140
} // namespace clang
139141

142+
namespace llvm {
143+
144+
template <> struct DenseMapInfo<clang::include_cleaner::Symbol> {
145+
using Outer = clang::include_cleaner::Symbol;
146+
using Base = DenseMapInfo<decltype(Outer::Storage)>;
147+
148+
static inline Outer getEmptyKey() { return {Base::getEmptyKey()}; }
149+
static inline Outer getTombstoneKey() { return {Base::getTombstoneKey()}; }
150+
static unsigned getHashValue(const Outer &Val) {
151+
return Base::getHashValue(Val.Storage);
152+
}
153+
static bool isEqual(const Outer &LHS, const Outer &RHS) {
154+
return Base::isEqual(LHS.Storage, RHS.Storage);
155+
}
156+
};
157+
template <> struct DenseMapInfo<clang::include_cleaner::Macro> {
158+
using Outer = clang::include_cleaner::Macro;
159+
using Base = DenseMapInfo<decltype(Outer::Definition)>;
160+
161+
static inline Outer getEmptyKey() { return {nullptr, Base::getEmptyKey()}; }
162+
static inline Outer getTombstoneKey() {
163+
return {nullptr, Base::getTombstoneKey()};
164+
}
165+
static unsigned getHashValue(const Outer &Val) {
166+
return Base::getHashValue(Val.Definition);
167+
}
168+
static bool isEqual(const Outer &LHS, const Outer &RHS) {
169+
return Base::isEqual(LHS.Definition, RHS.Definition);
170+
}
171+
};
172+
} // namespace llvm
173+
140174
#endif

0 commit comments

Comments
 (0)