Skip to content

Commit 0a09541

Browse files
author
Mingsheng Hong
committed
Make swift::SourceRange usable as a key type for DenseSet/DenseMap.
One use case is using DenseSet to manage a set of source locations, so that we emit at most one compiler diagnostic per location (see #18402).
1 parent 423bee6 commit 0a09541

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

include/swift/Basic/SourceLoc.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define SWIFT_BASIC_SOURCELOC_H
1919

2020
#include "swift/Basic/LLVM.h"
21+
#include "llvm/ADT/DenseMapInfo.h"
2122
#include "llvm/ADT/StringRef.h"
2223
#include "llvm/Support/SMLoc.h"
2324
#include <functional>
@@ -214,4 +215,31 @@ class CharSourceRange {
214215

215216
} // end namespace swift
216217

218+
namespace llvm {
219+
template <typename T> struct DenseMapInfo;
220+
221+
template <> struct DenseMapInfo<swift::SourceRange> {
222+
static swift::SourceRange getEmptyKey() { return swift::SourceRange(); }
223+
224+
static swift::SourceRange getTombstoneKey() {
225+
// Make this different from empty key. See for context:
226+
// http://lists.llvm.org/pipermail/llvm-dev/2015-July/088744.html
227+
return swift::SourceRange(swift::SourceLoc(
228+
SMLoc::getFromPointer(DenseMapInfo<const char *>::getTombstoneKey())));
229+
}
230+
231+
static unsigned getHashValue(const swift::SourceRange &Val) {
232+
return hash_combine(DenseMapInfo<const void *>::getHashValue(
233+
Val.Start.getOpaquePointerValue()),
234+
DenseMapInfo<const void *>::getHashValue(
235+
Val.End.getOpaquePointerValue()));
236+
}
237+
238+
static bool isEqual(const swift::SourceRange &LHS,
239+
const swift::SourceRange &RHS) {
240+
return LHS == RHS;
241+
}
242+
};
243+
} // namespace llvm
244+
217245
#endif // SWIFT_BASIC_SOURCELOC_H

0 commit comments

Comments
 (0)