diff --git a/clang/include/clang/Basic/SourceLocation.h b/clang/include/clang/Basic/SourceLocation.h index 14543cc41a38e..bd0038d5ae1ae 100644 --- a/clang/include/clang/Basic/SourceLocation.h +++ b/clang/include/clang/Basic/SourceLocation.h @@ -521,6 +521,25 @@ namespace llvm { static void Profile(const clang::SourceLocation &X, FoldingSetNodeID &ID); }; + template <> struct DenseMapInfo { + static clang::SourceRange getEmptyKey() { + return DenseMapInfo::getEmptyKey(); + } + + static clang::SourceRange getTombstoneKey() { + return DenseMapInfo::getTombstoneKey(); + } + + static unsigned getHashValue(clang::SourceRange Range) { + return detail::combineHashValue(Range.getBegin().getHashValue(), + Range.getEnd().getHashValue()); + } + + static bool isEqual(clang::SourceRange LHS, clang::SourceRange RHS) { + return LHS == RHS; + } + }; + } // namespace llvm #endif // LLVM_CLANG_BASIC_SOURCELOCATION_H diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp index 04b23dd13ba3e..7c8aae5c5834f 100644 --- a/clang/unittests/Basic/SourceManagerTest.cpp +++ b/clang/unittests/Basic/SourceManagerTest.cpp @@ -22,10 +22,12 @@ #include "llvm/Config/llvm-config.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Process.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" #include using namespace clang; +using ::testing::IsEmpty; namespace clang { class SourceManagerTestHelper { @@ -379,6 +381,20 @@ TEST_F(SourceManagerTest, getInvalidBOM) { "UTF-32 (LE)"); } +TEST_F(SourceManagerTest, sourceRangeWorksWithDenseSet) { + llvm::DenseSet Set; + SourceRange TestRange = {SourceLocation::getFromRawEncoding(10), + SourceLocation::getFromRawEncoding(11)}; + ASSERT_THAT(Set, IsEmpty()); + Set.insert(TestRange); + ASSERT_EQ(Set.size(), 1U); + ASSERT_TRUE(Set.contains(TestRange)); + ASSERT_FALSE(Set.contains({SourceLocation::getFromRawEncoding(10), + SourceLocation::getFromRawEncoding(10)})); + Set.erase(TestRange); + ASSERT_THAT(Set, IsEmpty()); +} + // Regression test - there was an out of bound access for buffers not terminated by zero. TEST_F(SourceManagerTest, getLineNumber) { const unsigned pageSize = llvm::sys::Process::getPageSizeEstimate();