From 443ab4d2e01246bf93cd410db945dc9ab6adf1b3 Mon Sep 17 00:00:00 2001 From: Mikhail Maltsev Date: Tue, 27 Oct 2020 10:43:39 +0000 Subject: [PATCH] [clang][Basic] Integrate SourceLocation with FoldingSet, NFCI This patch removes the necessity to access the SourceLocation internal representation in several places that use FoldingSet objects. Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D69844 --- clang/include/clang/Basic/SourceLocation.h | 9 +++++++++ clang/lib/Analysis/PathDiagnostic.cpp | 10 +++++----- clang/lib/Basic/SourceLocation.cpp | 6 ++++++ clang/lib/StaticAnalyzer/Core/BugReporter.cpp | 8 ++++---- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/clang/include/clang/Basic/SourceLocation.h b/clang/include/clang/Basic/SourceLocation.h index 88ecb7c..fc722b1 100644 --- a/clang/include/clang/Basic/SourceLocation.h +++ b/clang/include/clang/Basic/SourceLocation.h @@ -26,6 +26,9 @@ namespace llvm { template struct DenseMapInfo; +class FoldingSetNodeID; +template struct FoldingSetTrait; + } // namespace llvm namespace clang { @@ -87,6 +90,7 @@ class SourceLocation { friend class ASTReader; friend class ASTWriter; friend class SourceManager; + friend struct llvm::FoldingSetTrait; unsigned ID = 0; @@ -501,6 +505,11 @@ namespace llvm { } }; + // Allow calling FoldingSetNodeID::Add with SourceLocation object as parameter + template <> struct FoldingSetTrait { + static void Profile(const clang::SourceLocation &X, FoldingSetNodeID &ID); + }; + // Teach SmallPtrSet how to handle SourceLocation. template<> struct PointerLikeTypeTraits { diff --git a/clang/lib/Analysis/PathDiagnostic.cpp b/clang/lib/Analysis/PathDiagnostic.cpp index f80b99b..b42f47f 100644 --- a/clang/lib/Analysis/PathDiagnostic.cpp +++ b/clang/lib/Analysis/PathDiagnostic.cpp @@ -1083,9 +1083,9 @@ unsigned PathDiagnostic::full_size() { //===----------------------------------------------------------------------===// void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const { - ID.AddInteger(Range.getBegin().getRawEncoding()); - ID.AddInteger(Range.getEnd().getRawEncoding()); - ID.AddInteger(Loc.getRawEncoding()); + ID.Add(Range.getBegin()); + ID.Add(Range.getEnd()); + ID.Add(static_cast(Loc)); } void PathDiagnosticPiece::Profile(llvm::FoldingSetNodeID &ID) const { @@ -1095,8 +1095,8 @@ void PathDiagnosticPiece::Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger((unsigned) getDisplayHint()); ArrayRef Ranges = getRanges(); for (const auto &I : Ranges) { - ID.AddInteger(I.getBegin().getRawEncoding()); - ID.AddInteger(I.getEnd().getRawEncoding()); + ID.Add(I.getBegin()); + ID.Add(I.getEnd()); } } diff --git a/clang/lib/Basic/SourceLocation.cpp b/clang/lib/Basic/SourceLocation.cpp index 2ff1797..fde1399 100644 --- a/clang/lib/Basic/SourceLocation.cpp +++ b/clang/lib/Basic/SourceLocation.cpp @@ -15,6 +15,7 @@ #include "clang/Basic/PrettyStackTrace.h" #include "clang/Basic/SourceManager.h" #include "llvm/ADT/DenseMapInfo.h" +#include "llvm/ADT/FoldingSet.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/MemoryBuffer.h" @@ -45,6 +46,11 @@ unsigned SourceLocation::getHashValue() const { return llvm::DenseMapInfo::getHashValue(ID); } +void llvm::FoldingSetTrait::Profile( + const SourceLocation &X, llvm::FoldingSetNodeID &ID) { + ID.AddInteger(X.ID); +} + void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{ if (!isValid()) { OS << ""; diff --git a/clang/lib/StaticAnalyzer/Core/BugReporter.cpp b/clang/lib/StaticAnalyzer/Core/BugReporter.cpp index ebad1d1..bf38891 100644 --- a/clang/lib/StaticAnalyzer/Core/BugReporter.cpp +++ b/clang/lib/StaticAnalyzer/Core/BugReporter.cpp @@ -2193,8 +2193,8 @@ void BasicBugReport::Profile(llvm::FoldingSetNodeID& hash) const { for (SourceRange range : Ranges) { if (!range.isValid()) continue; - hash.AddInteger(range.getBegin().getRawEncoding()); - hash.AddInteger(range.getEnd().getRawEncoding()); + hash.Add(range.getBegin()); + hash.Add(range.getEnd()); } } @@ -2216,8 +2216,8 @@ void PathSensitiveBugReport::Profile(llvm::FoldingSetNodeID &hash) const { for (SourceRange range : Ranges) { if (!range.isValid()) continue; - hash.AddInteger(range.getBegin().getRawEncoding()); - hash.AddInteger(range.getEnd().getRawEncoding()); + hash.Add(range.getBegin()); + hash.Add(range.getEnd()); } } -- 2.7.4