[clang-tools-extra] 7819411 - [clang] Use SourceLocation as key in hash maps, NFCI
Mikhail Maltsev via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 20 08:24:45 PDT 2020
Author: Mikhail Maltsev
Date: 2020-10-20T16:24:09+01:00
New Revision: 781941183700b52d11b27227e3341385447610fa
URL: https://github.com/llvm/llvm-project/commit/781941183700b52d11b27227e3341385447610fa
DIFF: https://github.com/llvm/llvm-project/commit/781941183700b52d11b27227e3341385447610fa.diff
LOG: [clang] Use SourceLocation as key in hash maps, NFCI
The patch adjusts the existing `llvm::DenseMap<unsigned, T>` and
`llvm::DenseSet<unsigned>` objects that store source locations, so
that they use `SourceLocation` directly instead of `unsigned`.
This patch relies on the `DenseMapInfo` trait added in D89719.
It also replaces the construction of `SourceLocation` objects from
the constants -1 and -2 with calls to the trait's methods `getEmptyKey`
and `getTombstoneKey` where appropriate.
Reviewed By: dexonsmith
Differential Revision: https://reviews.llvm.org/D69840
Added:
Modified:
clang-tools-extra/clang-tidy/cppcoreguidelines/SpecialMemberFunctionsCheck.h
clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h
clang-tools-extra/clangd/FindTarget.cpp
clang/include/clang/Edit/EditedSource.h
clang/include/clang/Sema/Sema.h
clang/include/clang/Tooling/Syntax/Tokens.h
clang/lib/ARCMigrate/TransGCAttrs.cpp
clang/lib/ARCMigrate/TransProperties.cpp
clang/lib/ARCMigrate/Transforms.h
clang/lib/CodeGen/CGOpenMPRuntime.h
clang/lib/Edit/EditedSource.cpp
clang/lib/Tooling/Syntax/BuildTree.cpp
clang/lib/Tooling/Syntax/Tokens.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/SpecialMemberFunctionsCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/SpecialMemberFunctionsCheck.h
index 83fca3d5799b0..f232a0a09fbb7 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/SpecialMemberFunctionsCheck.h
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/SpecialMemberFunctionsCheck.h
@@ -85,15 +85,13 @@ struct DenseMapInfo<
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId;
static inline ClassDefId getEmptyKey() {
- return ClassDefId(
- clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-1)),
- "EMPTY");
+ return ClassDefId(DenseMapInfo<clang::SourceLocation>::getEmptyKey(),
+ "EMPTY");
}
static inline ClassDefId getTombstoneKey() {
- return ClassDefId(
- clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-2)),
- "TOMBSTONE");
+ return ClassDefId(DenseMapInfo<clang::SourceLocation>::getTombstoneKey(),
+ "TOMBSTONE");
}
static unsigned getHashValue(ClassDefId Val) {
@@ -101,7 +99,7 @@ struct DenseMapInfo<
assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!");
std::hash<ClassDefId::second_type> SecondHash;
- return Val.first.getRawEncoding() + SecondHash(Val.second);
+ return Val.first.getHashValue() + SecondHash(Val.second);
}
static bool isEqual(const ClassDefId &LHS, const ClassDefId &RHS) {
diff --git a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
index bb8caf1d84e2e..6a25813eb7480 100644
--- a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
@@ -28,15 +28,13 @@ struct DenseMapInfo<clang::tidy::RenamerClangTidyCheck::NamingCheckId> {
using NamingCheckId = clang::tidy::RenamerClangTidyCheck::NamingCheckId;
static inline NamingCheckId getEmptyKey() {
- return NamingCheckId(
- clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-1)),
- "EMPTY");
+ return NamingCheckId(DenseMapInfo<clang::SourceLocation>::getEmptyKey(),
+ "EMPTY");
}
static inline NamingCheckId getTombstoneKey() {
- return NamingCheckId(
- clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-2)),
- "TOMBSTONE");
+ return NamingCheckId(DenseMapInfo<clang::SourceLocation>::getTombstoneKey(),
+ "TOMBSTONE");
}
static unsigned getHashValue(NamingCheckId Val) {
@@ -44,7 +42,8 @@ struct DenseMapInfo<clang::tidy::RenamerClangTidyCheck::NamingCheckId> {
assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!");
std::hash<NamingCheckId::second_type> SecondHash;
- return Val.first.getRawEncoding() + SecondHash(Val.second);
+ return DenseMapInfo<clang::SourceLocation>::getHashValue(Val.first) +
+ SecondHash(Val.second);
}
static bool isEqual(const NamingCheckId &LHS, const NamingCheckId &RHS) {
@@ -173,8 +172,7 @@ void RenamerClangTidyCheck::addUsage(
// is already in there
RenamerClangTidyCheck::NamingCheckFailure &Failure =
NamingCheckFailures[Decl];
-
- if (!Failure.RawUsageLocs.insert(FixLocation.getRawEncoding()).second)
+ if (!Failure.RawUsageLocs.insert(FixLocation).second)
return;
if (!Failure.ShouldFix())
@@ -550,9 +548,8 @@ void RenamerClangTidyCheck::onEndOfTranslationUnit() {
//
// Other multi-token identifiers, such as operators are not checked at
// all.
- Diag << FixItHint::CreateReplacement(
- SourceRange(SourceLocation::getFromRawEncoding(Loc)),
- Failure.Info.Fixup);
+ Diag << FixItHint::CreateReplacement(SourceRange(Loc),
+ Failure.Info.Fixup);
}
}
}
diff --git a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h
index a352b6a5584ae..aabb80133939f 100644
--- a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h
+++ b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h
@@ -94,9 +94,8 @@ class RenamerClangTidyCheck : public ClangTidyCheck {
ShouldFixStatus FixStatus = ShouldFixStatus::ShouldFix;
- /// A set of all the identifier usages starting SourceLocation, in
- /// their encoded form.
- llvm::DenseSet<unsigned> RawUsageLocs;
+ /// A set of all the identifier usages starting SourceLocation.
+ llvm::DenseSet<SourceLocation> RawUsageLocs;
NamingCheckFailure() = default;
};
diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp
index 19ffdbb7c7eaf..a9aa25805be4d 100644
--- a/clang-tools-extra/clangd/FindTarget.cpp
+++ b/clang-tools-extra/clangd/FindTarget.cpp
@@ -1014,7 +1014,7 @@ class ExplicitReferenceCollector
}
bool VisitTypeLoc(TypeLoc TTL) {
- if (TypeLocsToSkip.count(TTL.getBeginLoc().getRawEncoding()))
+ if (TypeLocsToSkip.count(TTL.getBeginLoc()))
return true;
visitNode(DynTypedNode::create(TTL));
return true;
@@ -1024,7 +1024,7 @@ class ExplicitReferenceCollector
// ElaboratedTypeLoc will reports information for its inner type loc.
// Otherwise we loose information about inner types loc's qualifier.
TypeLoc Inner = L.getNamedTypeLoc().getUnqualifiedLoc();
- TypeLocsToSkip.insert(Inner.getBeginLoc().getRawEncoding());
+ TypeLocsToSkip.insert(Inner.getBeginLoc());
return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L);
}
@@ -1090,7 +1090,7 @@ class ExplicitReferenceCollector
visitNode(DynTypedNode::create(L));
// Inner type is missing information about its qualifier, skip it.
if (auto TL = L.getTypeLoc())
- TypeLocsToSkip.insert(TL.getBeginLoc().getRawEncoding());
+ TypeLocsToSkip.insert(TL.getBeginLoc());
return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L);
}
@@ -1163,7 +1163,7 @@ class ExplicitReferenceCollector
llvm::function_ref<void(ReferenceLoc)> Out;
/// TypeLocs starting at these locations must be skipped, see
/// TraverseElaboratedTypeSpecifierLoc for details.
- llvm::DenseSet</*SourceLocation*/ unsigned> TypeLocsToSkip;
+ llvm::DenseSet<SourceLocation> TypeLocsToSkip;
};
} // namespace
diff --git a/clang/include/clang/Edit/EditedSource.h b/clang/include/clang/Edit/EditedSource.h
index 60072f6758b1a..ab76b0d719b05 100644
--- a/clang/include/clang/Edit/EditedSource.h
+++ b/clang/include/clang/Edit/EditedSource.h
@@ -62,7 +62,7 @@ class EditedSource {
}
};
- llvm::DenseMap<unsigned, SmallVector<MacroArgUse, 2>> ExpansionToArgMap;
+ llvm::DenseMap<SourceLocation, SmallVector<MacroArgUse, 2>> ExpansionToArgMap;
SmallVector<std::pair<SourceLocation, MacroArgUse>, 2>
CurrCommitMacroArgExps;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 2d08b0e27a59a..7ced3d9a7bd4a 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -12768,7 +12768,7 @@ template <> struct DenseMapInfo<clang::Sema::FunctionDeclAndLoc> {
static unsigned getHashValue(const FunctionDeclAndLoc &FDL) {
return hash_combine(FDBaseInfo::getHashValue(FDL.FD),
- FDL.Loc.getRawEncoding());
+ FDL.Loc.getHashValue());
}
static bool isEqual(const FunctionDeclAndLoc &LHS,
diff --git a/clang/include/clang/Tooling/Syntax/Tokens.h b/clang/include/clang/Tooling/Syntax/Tokens.h
index 6e253abd6b3fe..98320bd54d6f6 100644
--- a/clang/include/clang/Tooling/Syntax/Tokens.h
+++ b/clang/include/clang/Tooling/Syntax/Tokens.h
@@ -438,7 +438,7 @@ class TokenCollector {
/// the stack) and not when they end (when we pop a macro from the stack).
/// To workaround this limitation, we rely on source location information
/// stored in this map.
- using PPExpansions = llvm::DenseMap</*SourceLocation*/ int, SourceLocation>;
+ using PPExpansions = llvm::DenseMap<SourceLocation, SourceLocation>;
class Builder;
class CollectPPExpansions;
diff --git a/clang/lib/ARCMigrate/TransGCAttrs.cpp b/clang/lib/ARCMigrate/TransGCAttrs.cpp
index 8f5f3cff17cb7..99a61e0842a76 100644
--- a/clang/lib/ARCMigrate/TransGCAttrs.cpp
+++ b/clang/lib/ARCMigrate/TransGCAttrs.cpp
@@ -88,8 +88,8 @@ class GCAttrsCollector : public RecursiveASTVisitor<GCAttrsCollector> {
return false;
SourceLocation Loc = OwnershipAttr->getLocation();
- unsigned RawLoc = Loc.getRawEncoding();
- if (MigrateCtx.AttrSet.count(RawLoc))
+ SourceLocation OrigLoc = Loc;
+ if (MigrateCtx.AttrSet.count(OrigLoc))
return true;
ASTContext &Ctx = MigrateCtx.Pass.Ctx;
@@ -105,7 +105,7 @@ class GCAttrsCollector : public RecursiveASTVisitor<GCAttrsCollector> {
else
return false;
- MigrateCtx.AttrSet.insert(RawLoc);
+ MigrateCtx.AttrSet.insert(OrigLoc);
MigrateCtx.GCAttrs.push_back(MigrationContext::GCAttrOccurrence());
MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs.back();
@@ -204,7 +204,7 @@ static void checkWeakGCAttrs(MigrationContext &MigrateCtx) {
if (!canApplyWeak(MigrateCtx.Pass.Ctx, Attr.ModifiedType,
/*AllowOnUnknownClass=*/true)) {
Transaction Trans(TA);
- if (!MigrateCtx.RemovedAttrSet.count(Attr.Loc.getRawEncoding()))
+ if (!MigrateCtx.RemovedAttrSet.count(Attr.Loc))
TA.replaceText(Attr.Loc, "__weak", "__unsafe_unretained");
TA.clearDiagnostic(diag::err_arc_weak_no_runtime,
diag::err_arc_unsupported_weak_class,
@@ -262,7 +262,7 @@ static void checkAllAtProps(MigrationContext &MigrateCtx,
if (GCAttrsCollector::hasObjCImpl(
cast<Decl>(IndProps.front()->getDeclContext()))) {
if (hasWeak)
- MigrateCtx.AtPropsWeak.insert(AtLoc.getRawEncoding());
+ MigrateCtx.AtPropsWeak.insert(AtLoc);
} else {
StringRef toAttr = "strong";
@@ -289,14 +289,14 @@ static void checkAllAtProps(MigrationContext &MigrateCtx,
TA.clearDiagnostic(diag::err_objc_property_attr_mutually_exclusive, AtLoc);
TA.clearDiagnostic(diag::err_arc_inconsistent_property_ownership,
ATLs[i].second->getLocation());
- MigrateCtx.RemovedAttrSet.insert(Loc.getRawEncoding());
+ MigrateCtx.RemovedAttrSet.insert(Loc);
}
}
static void checkAllProps(MigrationContext &MigrateCtx,
std::vector<ObjCPropertyDecl *> &AllProps) {
typedef llvm::TinyPtrVector<ObjCPropertyDecl *> IndivPropsTy;
- llvm::DenseMap<unsigned, IndivPropsTy> AtProps;
+ llvm::DenseMap<SourceLocation, IndivPropsTy> AtProps;
for (unsigned i = 0, e = AllProps.size(); i != e; ++i) {
ObjCPropertyDecl *PD = AllProps[i];
@@ -306,14 +306,12 @@ static void checkAllProps(MigrationContext &MigrateCtx,
SourceLocation AtLoc = PD->getAtLoc();
if (AtLoc.isInvalid())
continue;
- unsigned RawAt = AtLoc.getRawEncoding();
- AtProps[RawAt].push_back(PD);
+ AtProps[AtLoc].push_back(PD);
}
}
- for (llvm::DenseMap<unsigned, IndivPropsTy>::iterator
- I = AtProps.begin(), E = AtProps.end(); I != E; ++I) {
- SourceLocation AtLoc = SourceLocation::getFromRawEncoding(I->first);
+ for (auto I = AtProps.begin(), E = AtProps.end(); I != E; ++I) {
+ SourceLocation AtLoc = I->first;
IndivPropsTy &IndProps = I->second;
checkAllAtProps(MigrateCtx, AtLoc, IndProps);
}
diff --git a/clang/lib/ARCMigrate/TransProperties.cpp b/clang/lib/ARCMigrate/TransProperties.cpp
index db0e6acafc669..e5ccf1cf79b13 100644
--- a/clang/lib/ARCMigrate/TransProperties.cpp
+++ b/clang/lib/ARCMigrate/TransProperties.cpp
@@ -337,7 +337,7 @@ class PropertiesRewriter {
return false;
if (props.empty())
return false;
- return MigrateCtx.AtPropsWeak.count(atLoc.getRawEncoding());
+ return MigrateCtx.AtPropsWeak.count(atLoc);
}
bool isUserDeclared(ObjCIvarDecl *ivarD) const {
diff --git a/clang/lib/ARCMigrate/Transforms.h b/clang/lib/ARCMigrate/Transforms.h
index e087136f0e2c8..37e2d6b2a7e12 100644
--- a/clang/lib/ARCMigrate/Transforms.h
+++ b/clang/lib/ARCMigrate/Transforms.h
@@ -93,12 +93,12 @@ class MigrationContext {
bool FullyMigratable;
};
std::vector<GCAttrOccurrence> GCAttrs;
- llvm::DenseSet<unsigned> AttrSet;
- llvm::DenseSet<unsigned> RemovedAttrSet;
+ llvm::DenseSet<SourceLocation> AttrSet;
+ llvm::DenseSet<SourceLocation> RemovedAttrSet;
/// Set of raw '@' locations for 'assign' properties group that contain
/// GC __weak.
- llvm::DenseSet<unsigned> AtPropsWeak;
+ llvm::DenseSet<SourceLocation> AtPropsWeak;
explicit MigrationContext(MigrationPass &pass) : Pass(pass) {}
~MigrationContext();
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.h b/clang/lib/CodeGen/CGOpenMPRuntime.h
index e39c2e11390e1..d67034cb3de67 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.h
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.h
@@ -391,7 +391,7 @@ class CGOpenMPRuntime {
private:
/// Map for SourceLocation and OpenMP runtime library debug locations.
- typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
+ typedef llvm::DenseMap<SourceLocation, llvm::Value *> OpenMPDebugLocMapTy;
OpenMPDebugLocMapTy OpenMPDebugLocMap;
/// The type for a microtask which gets passed to __kmpc_fork_call().
/// Original representation is:
diff --git a/clang/lib/Edit/EditedSource.cpp b/clang/lib/Edit/EditedSource.cpp
index b3bc63a903a7e..74e6005faeb04 100644
--- a/clang/lib/Edit/EditedSource.cpp
+++ b/clang/lib/Edit/EditedSource.cpp
@@ -59,7 +59,7 @@ void EditedSource::finishedCommit() {
SourceLocation ExpLoc;
MacroArgUse ArgUse;
std::tie(ExpLoc, ArgUse) = ExpArg;
- auto &ArgUses = ExpansionToArgMap[ExpLoc.getRawEncoding()];
+ auto &ArgUses = ExpansionToArgMap[ExpLoc];
if (llvm::find(ArgUses, ArgUse) == ArgUses.end())
ArgUses.push_back(ArgUse);
}
@@ -82,7 +82,7 @@ bool EditedSource::canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs) {
SourceLocation ExpLoc;
MacroArgUse ArgUse;
deconstructMacroArgLoc(OrigLoc, ExpLoc, ArgUse);
- auto I = ExpansionToArgMap.find(ExpLoc.getRawEncoding());
+ auto I = ExpansionToArgMap.find(ExpLoc);
if (I != ExpansionToArgMap.end() &&
find_if(I->second, [&](const MacroArgUse &U) {
return ArgUse.Identifier == U.Identifier &&
diff --git a/clang/lib/Tooling/Syntax/BuildTree.cpp b/clang/lib/Tooling/Syntax/BuildTree.cpp
index e1ed55f2e4eb6..197590522e36e 100644
--- a/clang/lib/Tooling/Syntax/BuildTree.cpp
+++ b/clang/lib/Tooling/Syntax/BuildTree.cpp
@@ -367,7 +367,7 @@ class syntax::TreeBuilder {
public:
TreeBuilder(syntax::Arena &Arena) : Arena(Arena), Pending(Arena) {
for (const auto &T : Arena.getTokenBuffer().expandedTokens())
- LocationToToken.insert({T.location().getRawEncoding(), &T});
+ LocationToToken.insert({T.location(), &T});
}
llvm::BumpPtrAllocator &allocator() { return Arena.getAllocator(); }
@@ -689,8 +689,7 @@ class syntax::TreeBuilder {
syntax::Arena &Arena;
/// To quickly find tokens by their start location.
- llvm::DenseMap</*SourceLocation*/ unsigned, const syntax::Token *>
- LocationToToken;
+ llvm::DenseMap<SourceLocation, const syntax::Token *> LocationToToken;
Forest Pending;
llvm::DenseSet<Decl *> DeclsWithoutSemicolons;
ASTToSyntaxMapping Mapping;
@@ -1708,7 +1707,7 @@ void syntax::TreeBuilder::markExprChild(Expr *Child, NodeRole Role) {
const syntax::Token *syntax::TreeBuilder::findToken(SourceLocation L) const {
if (L.isInvalid())
return nullptr;
- auto It = LocationToToken.find(L.getRawEncoding());
+ auto It = LocationToToken.find(L);
assert(It != LocationToToken.end());
return It->second;
}
diff --git a/clang/lib/Tooling/Syntax/Tokens.cpp b/clang/lib/Tooling/Syntax/Tokens.cpp
index a83cc2d528615..234df9cb7182b 100644
--- a/clang/lib/Tooling/Syntax/Tokens.cpp
+++ b/clang/lib/Tooling/Syntax/Tokens.cpp
@@ -575,11 +575,11 @@ class TokenCollector::CollectPPExpansions : public PPCallbacks {
// A's startpoint.
if (!Range.getBegin().isFileID()) {
Range.setBegin(SM.getExpansionLoc(Range.getBegin()));
- assert(Collector->Expansions.count(Range.getBegin().getRawEncoding()) &&
+ assert(Collector->Expansions.count(Range.getBegin()) &&
"Overlapping macros should have same expansion location");
}
- Collector->Expansions[Range.getBegin().getRawEncoding()] = Range.getEnd();
+ Collector->Expansions[Range.getBegin()] = Range.getEnd();
LastExpansionEnd = Range.getEnd();
}
// FIXME: handle directives like #pragma, #include, etc.
@@ -711,8 +711,8 @@ class TokenCollector::Builder {
// If we know mapping bounds at [NextSpelled, KnownEnd] (macro expansion)
// then we want to partition our (empty) mapping.
// [Start, NextSpelled) [NextSpelled, KnownEnd] (KnownEnd, Target)
- SourceLocation KnownEnd = CollectedExpansions.lookup(
- SpelledTokens[NextSpelled].location().getRawEncoding());
+ SourceLocation KnownEnd =
+ CollectedExpansions.lookup(SpelledTokens[NextSpelled].location());
if (KnownEnd.isValid()) {
FlushMapping(); // Emits [Start, NextSpelled)
while (NextSpelled < SpelledTokens.size() &&
@@ -749,7 +749,7 @@ class TokenCollector::Builder {
// We need no mapping for file tokens copied to the expanded stream.
} else {
// We found a new macro expansion. We should have its spelling bounds.
- auto End = CollectedExpansions.lookup(Expansion.getRawEncoding());
+ auto End = CollectedExpansions.lookup(Expansion);
assert(End.isValid() && "Macro expansion wasn't captured?");
// Mapping starts here...
More information about the cfe-commits
mailing list