Author: Victor Chernyakin Date: 2026-01-06T14:46:23-07:00 New Revision: 9c2b1bab1bbf674a3f4e7c60d359a9b21b08f9a9
URL: https://github.com/llvm/llvm-project/commit/9c2b1bab1bbf674a3f4e7c60d359a9b21b08f9a9 DIFF: https://github.com/llvm/llvm-project/commit/9c2b1bab1bbf674a3f4e7c60d359a9b21b08f9a9.diff LOG: [LLVM][ADT] Add specialization of `DenseMapInfo` for `SourceRange` (#174524) So that `SourceRange` can be used in `DenseMap` and `DenseSet`. Added: Modified: clang/include/clang/Basic/SourceLocation.h clang/unittests/Basic/SourceManagerTest.cpp Removed: ################################################################################ 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<clang::SourceRange> { + static clang::SourceRange getEmptyKey() { + return DenseMapInfo<clang::SourceLocation>::getEmptyKey(); + } + + static clang::SourceRange getTombstoneKey() { + return DenseMapInfo<clang::SourceLocation>::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 <cstddef> 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<SourceRange> 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(); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
