Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions clang/include/clang/Basic/SourceLocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions clang/unittests/Basic/SourceManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down