[clang] [LLVM][ADT] Add specialization of `DenseMapInfo` for `SourceRange` (PR #174524)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 6 09:25:40 PST 2026
https://github.com/localspook updated https://github.com/llvm/llvm-project/pull/174524
>From 0f765712a6c5586478656998f15a3b581f09d704 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Mon, 5 Jan 2026 19:24:37 -0800
Subject: [PATCH 1/4] [LLVM][ADT] Add specialization of `DenseMapInfo` for
`SourceRange`
---
clang/include/clang/Basic/SourceLocation.h | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/clang/include/clang/Basic/SourceLocation.h b/clang/include/clang/Basic/SourceLocation.h
index 14543cc41a38e..85fe65901d207 100644
--- a/clang/include/clang/Basic/SourceLocation.h
+++ b/clang/include/clang/Basic/SourceLocation.h
@@ -521,6 +521,27 @@ namespace llvm {
static void Profile(const clang::SourceLocation &X, FoldingSetNodeID &ID);
};
+ template <> struct DenseMapInfo<clang::SourceRange, void> {
+ static clang::SourceRange getEmptyKey() {
+ return {DenseMapInfo<clang::SourceLocation>::getEmptyKey(),
+ DenseMapInfo<clang::SourceLocation>::getEmptyKey()};
+ }
+
+ static clang::SourceRange getTombstoneKey() {
+ return {DenseMapInfo<clang::SourceLocation>::getTombstoneKey(),
+ 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
>From adad67045ac2bec6d79b9f2ea3fe5e294bef18c1 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Tue, 6 Jan 2026 07:29:33 -0800
Subject: [PATCH 2/4] Add unit tests, slightly simplify implementation
---
clang/include/clang/Basic/SourceLocation.h | 8 +++-----
clang/unittests/Basic/SourceManagerTest.cpp | 14 ++++++++++++++
2 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/clang/include/clang/Basic/SourceLocation.h b/clang/include/clang/Basic/SourceLocation.h
index 85fe65901d207..bd0038d5ae1ae 100644
--- a/clang/include/clang/Basic/SourceLocation.h
+++ b/clang/include/clang/Basic/SourceLocation.h
@@ -521,15 +521,13 @@ namespace llvm {
static void Profile(const clang::SourceLocation &X, FoldingSetNodeID &ID);
};
- template <> struct DenseMapInfo<clang::SourceRange, void> {
+ template <> struct DenseMapInfo<clang::SourceRange> {
static clang::SourceRange getEmptyKey() {
- return {DenseMapInfo<clang::SourceLocation>::getEmptyKey(),
- DenseMapInfo<clang::SourceLocation>::getEmptyKey()};
+ return DenseMapInfo<clang::SourceLocation>::getEmptyKey();
}
static clang::SourceRange getTombstoneKey() {
- return {DenseMapInfo<clang::SourceLocation>::getTombstoneKey(),
- DenseMapInfo<clang::SourceLocation>::getTombstoneKey()};
+ return DenseMapInfo<clang::SourceLocation>::getTombstoneKey();
}
static unsigned getHashValue(clang::SourceRange Range) {
diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp
index 04b23dd13ba3e..59d2afa427891 100644
--- a/clang/unittests/Basic/SourceManagerTest.cpp
+++ b/clang/unittests/Basic/SourceManagerTest.cpp
@@ -379,6 +379,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_EQ(Set.size(), 0);
+ Set.insert(TestRange);
+ ASSERT_EQ(Set.size(), 1);
+ ASSERT_TRUE(Set.contains(TestRange));
+ ASSERT_FALSE(Set.contains({SourceLocation::getFromRawEncoding(10),
+ SourceLocation::getFromRawEncoding(10)}));
+ Set.erase(TestRange);
+ ASSERT_EQ(Set.size(), 0);
+}
+
// 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();
>From 437be70c8ff7e77352cf092c742bdf34579c7cfd Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Tue, 6 Jan 2026 09:00:20 -0700
Subject: [PATCH 3/4] Fix integer signedness
---
clang/unittests/Basic/SourceManagerTest.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp
index 59d2afa427891..42e6eade55a0e 100644
--- a/clang/unittests/Basic/SourceManagerTest.cpp
+++ b/clang/unittests/Basic/SourceManagerTest.cpp
@@ -383,14 +383,14 @@ TEST_F(SourceManagerTest, sourceRangeWorksWithDenseSet) {
llvm::DenseSet<SourceRange> Set;
SourceRange TestRange = {SourceLocation::getFromRawEncoding(10),
SourceLocation::getFromRawEncoding(11)};
- ASSERT_EQ(Set.size(), 0);
+ ASSERT_EQ(Set.size(), 0U);
Set.insert(TestRange);
- ASSERT_EQ(Set.size(), 1);
+ ASSERT_EQ(Set.size(), 1U);
ASSERT_TRUE(Set.contains(TestRange));
ASSERT_FALSE(Set.contains({SourceLocation::getFromRawEncoding(10),
SourceLocation::getFromRawEncoding(10)}));
Set.erase(TestRange);
- ASSERT_EQ(Set.size(), 0);
+ ASSERT_EQ(Set.size(), 0U);
}
// Regression test - there was an out of bound access for buffers not terminated by zero.
>From 118e1354fbd8f83cfe2ba5e93d37fd1e8df97e66 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Tue, 6 Jan 2026 10:25:09 -0700
Subject: [PATCH 4/4] Prefer IsEmpty matcher
---
clang/unittests/Basic/SourceManagerTest.cpp | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp
index 42e6eade55a0e..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 {
@@ -383,14 +385,14 @@ TEST_F(SourceManagerTest, sourceRangeWorksWithDenseSet) {
llvm::DenseSet<SourceRange> Set;
SourceRange TestRange = {SourceLocation::getFromRawEncoding(10),
SourceLocation::getFromRawEncoding(11)};
- ASSERT_EQ(Set.size(), 0U);
+ 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_EQ(Set.size(), 0U);
+ ASSERT_THAT(Set, IsEmpty());
}
// Regression test - there was an out of bound access for buffers not terminated by zero.
More information about the cfe-commits
mailing list