diff --git a/clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.h b/clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.h --- a/clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.h +++ b/clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.h @@ -33,7 +33,7 @@ void check(const ast_matchers::MatchFinder::MatchResult &Result) override; private: - llvm::DenseSet MatchedTemplateLocations; + llvm::DenseSet MatchedTemplateLocations; }; } // namespace google diff --git a/clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.cpp b/clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.cpp --- a/clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.cpp @@ -298,8 +298,7 @@ } if (IsInInstantiation) { - if (MatchedTemplateLocations.count( - ReplacementRange.getBegin().getRawEncoding()) == 0) { + if (MatchedTemplateLocations.count(ReplacementRange.getBegin()) == 0) { // For each location matched in a template instantiation, we check if // the location can also be found in `MatchedTemplateLocations`. If it // is not found, that means the expression did not create a match @@ -313,8 +312,7 @@ if (IsInTemplate) { // We gather source locations from template matches not in template // instantiations for future matches. - MatchedTemplateLocations.insert( - ReplacementRange.getBegin().getRawEncoding()); + MatchedTemplateLocations.insert(ReplacementRange.getBegin()); } if (!AddFix) { diff --git a/clang/include/clang/Basic/SourceLocation.h b/clang/include/clang/Basic/SourceLocation.h --- a/clang/include/clang/Basic/SourceLocation.h +++ b/clang/include/clang/Basic/SourceLocation.h @@ -175,6 +175,7 @@ End.isFileID(); } + unsigned getHashValue() const; void print(raw_ostream &OS, const SourceManager &SM) const; std::string printToString(const SourceManager &SM) const; void dump(const SourceManager &SM) const; @@ -479,6 +480,27 @@ } }; + /// Define DenseMapInfo so that SourceLocation's can be used as keys in + /// DenseMap and DenseSet. This trait class is eqivalent to + /// DenseMapInfo which uses SourceLocation::ID is used as a key. + template <> struct DenseMapInfo { + static clang::SourceLocation getEmptyKey() { + return clang::SourceLocation::getFromRawEncoding(~0U); + } + + static clang::SourceLocation getTombstoneKey() { + return clang::SourceLocation::getFromRawEncoding(~0U - 1); + } + + static unsigned getHashValue(clang::SourceLocation Loc) { + return Loc.getHashValue(); + } + + static bool isEqual(clang::SourceLocation LHS, clang::SourceLocation RHS) { + return LHS == RHS; + } + }; + // Teach SmallPtrSet how to handle SourceLocation. template<> struct PointerLikeTypeTraits { diff --git a/clang/lib/Basic/SourceLocation.cpp b/clang/lib/Basic/SourceLocation.cpp --- a/clang/lib/Basic/SourceLocation.cpp +++ b/clang/lib/Basic/SourceLocation.cpp @@ -14,6 +14,7 @@ #include "clang/Basic/LLVM.h" #include "clang/Basic/PrettyStackTrace.h" #include "clang/Basic/SourceManager.h" +#include "llvm/ADT/DenseMapInfo.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/MemoryBuffer.h" @@ -40,6 +41,10 @@ // SourceLocation //===----------------------------------------------------------------------===// +unsigned SourceLocation::getHashValue() const { + return llvm::DenseMapInfo::getHashValue(ID); +} + void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{ if (!isValid()) { OS << "";