Skip to content

Commit 7819411

Browse files
committed
[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
1 parent c76cdea commit 7819411

File tree

14 files changed

+48
-57
lines changed

14 files changed

+48
-57
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/SpecialMemberFunctionsCheck.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,21 @@ struct DenseMapInfo<
8585
clang::tidy::cppcoreguidelines::SpecialMemberFunctionsCheck::ClassDefId;
8686

8787
static inline ClassDefId getEmptyKey() {
88-
return ClassDefId(
89-
clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-1)),
90-
"EMPTY");
88+
return ClassDefId(DenseMapInfo<clang::SourceLocation>::getEmptyKey(),
89+
"EMPTY");
9190
}
9291

9392
static inline ClassDefId getTombstoneKey() {
94-
return ClassDefId(
95-
clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-2)),
96-
"TOMBSTONE");
93+
return ClassDefId(DenseMapInfo<clang::SourceLocation>::getTombstoneKey(),
94+
"TOMBSTONE");
9795
}
9896

9997
static unsigned getHashValue(ClassDefId Val) {
10098
assert(Val != getEmptyKey() && "Cannot hash the empty key!");
10199
assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!");
102100

103101
std::hash<ClassDefId::second_type> SecondHash;
104-
return Val.first.getRawEncoding() + SecondHash(Val.second);
102+
return Val.first.getHashValue() + SecondHash(Val.second);
105103
}
106104

107105
static bool isEqual(const ClassDefId &LHS, const ClassDefId &RHS) {

clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,22 @@ struct DenseMapInfo<clang::tidy::RenamerClangTidyCheck::NamingCheckId> {
2828
using NamingCheckId = clang::tidy::RenamerClangTidyCheck::NamingCheckId;
2929

3030
static inline NamingCheckId getEmptyKey() {
31-
return NamingCheckId(
32-
clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-1)),
33-
"EMPTY");
31+
return NamingCheckId(DenseMapInfo<clang::SourceLocation>::getEmptyKey(),
32+
"EMPTY");
3433
}
3534

3635
static inline NamingCheckId getTombstoneKey() {
37-
return NamingCheckId(
38-
clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-2)),
39-
"TOMBSTONE");
36+
return NamingCheckId(DenseMapInfo<clang::SourceLocation>::getTombstoneKey(),
37+
"TOMBSTONE");
4038
}
4139

4240
static unsigned getHashValue(NamingCheckId Val) {
4341
assert(Val != getEmptyKey() && "Cannot hash the empty key!");
4442
assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!");
4543

4644
std::hash<NamingCheckId::second_type> SecondHash;
47-
return Val.first.getRawEncoding() + SecondHash(Val.second);
45+
return DenseMapInfo<clang::SourceLocation>::getHashValue(Val.first) +
46+
SecondHash(Val.second);
4847
}
4948

5049
static bool isEqual(const NamingCheckId &LHS, const NamingCheckId &RHS) {
@@ -173,8 +172,7 @@ void RenamerClangTidyCheck::addUsage(
173172
// is already in there
174173
RenamerClangTidyCheck::NamingCheckFailure &Failure =
175174
NamingCheckFailures[Decl];
176-
177-
if (!Failure.RawUsageLocs.insert(FixLocation.getRawEncoding()).second)
175+
if (!Failure.RawUsageLocs.insert(FixLocation).second)
178176
return;
179177

180178
if (!Failure.ShouldFix())
@@ -550,9 +548,8 @@ void RenamerClangTidyCheck::onEndOfTranslationUnit() {
550548
//
551549
// Other multi-token identifiers, such as operators are not checked at
552550
// all.
553-
Diag << FixItHint::CreateReplacement(
554-
SourceRange(SourceLocation::getFromRawEncoding(Loc)),
555-
Failure.Info.Fixup);
551+
Diag << FixItHint::CreateReplacement(SourceRange(Loc),
552+
Failure.Info.Fixup);
556553
}
557554
}
558555
}

clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ class RenamerClangTidyCheck : public ClangTidyCheck {
9494

9595
ShouldFixStatus FixStatus = ShouldFixStatus::ShouldFix;
9696

97-
/// A set of all the identifier usages starting SourceLocation, in
98-
/// their encoded form.
99-
llvm::DenseSet<unsigned> RawUsageLocs;
97+
/// A set of all the identifier usages starting SourceLocation.
98+
llvm::DenseSet<SourceLocation> RawUsageLocs;
10099

101100
NamingCheckFailure() = default;
102101
};

clang-tools-extra/clangd/FindTarget.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ class ExplicitReferenceCollector
10141014
}
10151015

10161016
bool VisitTypeLoc(TypeLoc TTL) {
1017-
if (TypeLocsToSkip.count(TTL.getBeginLoc().getRawEncoding()))
1017+
if (TypeLocsToSkip.count(TTL.getBeginLoc()))
10181018
return true;
10191019
visitNode(DynTypedNode::create(TTL));
10201020
return true;
@@ -1024,7 +1024,7 @@ class ExplicitReferenceCollector
10241024
// ElaboratedTypeLoc will reports information for its inner type loc.
10251025
// Otherwise we loose information about inner types loc's qualifier.
10261026
TypeLoc Inner = L.getNamedTypeLoc().getUnqualifiedLoc();
1027-
TypeLocsToSkip.insert(Inner.getBeginLoc().getRawEncoding());
1027+
TypeLocsToSkip.insert(Inner.getBeginLoc());
10281028
return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L);
10291029
}
10301030

@@ -1090,7 +1090,7 @@ class ExplicitReferenceCollector
10901090
visitNode(DynTypedNode::create(L));
10911091
// Inner type is missing information about its qualifier, skip it.
10921092
if (auto TL = L.getTypeLoc())
1093-
TypeLocsToSkip.insert(TL.getBeginLoc().getRawEncoding());
1093+
TypeLocsToSkip.insert(TL.getBeginLoc());
10941094
return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L);
10951095
}
10961096

@@ -1163,7 +1163,7 @@ class ExplicitReferenceCollector
11631163
llvm::function_ref<void(ReferenceLoc)> Out;
11641164
/// TypeLocs starting at these locations must be skipped, see
11651165
/// TraverseElaboratedTypeSpecifierLoc for details.
1166-
llvm::DenseSet</*SourceLocation*/ unsigned> TypeLocsToSkip;
1166+
llvm::DenseSet<SourceLocation> TypeLocsToSkip;
11671167
};
11681168
} // namespace
11691169

clang/include/clang/Edit/EditedSource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class EditedSource {
6262
}
6363
};
6464

65-
llvm::DenseMap<unsigned, SmallVector<MacroArgUse, 2>> ExpansionToArgMap;
65+
llvm::DenseMap<SourceLocation, SmallVector<MacroArgUse, 2>> ExpansionToArgMap;
6666
SmallVector<std::pair<SourceLocation, MacroArgUse>, 2>
6767
CurrCommitMacroArgExps;
6868

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12768,7 +12768,7 @@ template <> struct DenseMapInfo<clang::Sema::FunctionDeclAndLoc> {
1276812768

1276912769
static unsigned getHashValue(const FunctionDeclAndLoc &FDL) {
1277012770
return hash_combine(FDBaseInfo::getHashValue(FDL.FD),
12771-
FDL.Loc.getRawEncoding());
12771+
FDL.Loc.getHashValue());
1277212772
}
1277312773

1277412774
static bool isEqual(const FunctionDeclAndLoc &LHS,

clang/include/clang/Tooling/Syntax/Tokens.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ class TokenCollector {
438438
/// the stack) and not when they end (when we pop a macro from the stack).
439439
/// To workaround this limitation, we rely on source location information
440440
/// stored in this map.
441-
using PPExpansions = llvm::DenseMap</*SourceLocation*/ int, SourceLocation>;
441+
using PPExpansions = llvm::DenseMap<SourceLocation, SourceLocation>;
442442
class Builder;
443443
class CollectPPExpansions;
444444

clang/lib/ARCMigrate/TransGCAttrs.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class GCAttrsCollector : public RecursiveASTVisitor<GCAttrsCollector> {
8888
return false;
8989

9090
SourceLocation Loc = OwnershipAttr->getLocation();
91-
unsigned RawLoc = Loc.getRawEncoding();
92-
if (MigrateCtx.AttrSet.count(RawLoc))
91+
SourceLocation OrigLoc = Loc;
92+
if (MigrateCtx.AttrSet.count(OrigLoc))
9393
return true;
9494

9595
ASTContext &Ctx = MigrateCtx.Pass.Ctx;
@@ -105,7 +105,7 @@ class GCAttrsCollector : public RecursiveASTVisitor<GCAttrsCollector> {
105105
else
106106
return false;
107107

108-
MigrateCtx.AttrSet.insert(RawLoc);
108+
MigrateCtx.AttrSet.insert(OrigLoc);
109109
MigrateCtx.GCAttrs.push_back(MigrationContext::GCAttrOccurrence());
110110
MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs.back();
111111

@@ -204,7 +204,7 @@ static void checkWeakGCAttrs(MigrationContext &MigrateCtx) {
204204
if (!canApplyWeak(MigrateCtx.Pass.Ctx, Attr.ModifiedType,
205205
/*AllowOnUnknownClass=*/true)) {
206206
Transaction Trans(TA);
207-
if (!MigrateCtx.RemovedAttrSet.count(Attr.Loc.getRawEncoding()))
207+
if (!MigrateCtx.RemovedAttrSet.count(Attr.Loc))
208208
TA.replaceText(Attr.Loc, "__weak", "__unsafe_unretained");
209209
TA.clearDiagnostic(diag::err_arc_weak_no_runtime,
210210
diag::err_arc_unsupported_weak_class,
@@ -262,7 +262,7 @@ static void checkAllAtProps(MigrationContext &MigrateCtx,
262262
if (GCAttrsCollector::hasObjCImpl(
263263
cast<Decl>(IndProps.front()->getDeclContext()))) {
264264
if (hasWeak)
265-
MigrateCtx.AtPropsWeak.insert(AtLoc.getRawEncoding());
265+
MigrateCtx.AtPropsWeak.insert(AtLoc);
266266

267267
} else {
268268
StringRef toAttr = "strong";
@@ -289,14 +289,14 @@ static void checkAllAtProps(MigrationContext &MigrateCtx,
289289
TA.clearDiagnostic(diag::err_objc_property_attr_mutually_exclusive, AtLoc);
290290
TA.clearDiagnostic(diag::err_arc_inconsistent_property_ownership,
291291
ATLs[i].second->getLocation());
292-
MigrateCtx.RemovedAttrSet.insert(Loc.getRawEncoding());
292+
MigrateCtx.RemovedAttrSet.insert(Loc);
293293
}
294294
}
295295

296296
static void checkAllProps(MigrationContext &MigrateCtx,
297297
std::vector<ObjCPropertyDecl *> &AllProps) {
298298
typedef llvm::TinyPtrVector<ObjCPropertyDecl *> IndivPropsTy;
299-
llvm::DenseMap<unsigned, IndivPropsTy> AtProps;
299+
llvm::DenseMap<SourceLocation, IndivPropsTy> AtProps;
300300

301301
for (unsigned i = 0, e = AllProps.size(); i != e; ++i) {
302302
ObjCPropertyDecl *PD = AllProps[i];
@@ -306,14 +306,12 @@ static void checkAllProps(MigrationContext &MigrateCtx,
306306
SourceLocation AtLoc = PD->getAtLoc();
307307
if (AtLoc.isInvalid())
308308
continue;
309-
unsigned RawAt = AtLoc.getRawEncoding();
310-
AtProps[RawAt].push_back(PD);
309+
AtProps[AtLoc].push_back(PD);
311310
}
312311
}
313312

314-
for (llvm::DenseMap<unsigned, IndivPropsTy>::iterator
315-
I = AtProps.begin(), E = AtProps.end(); I != E; ++I) {
316-
SourceLocation AtLoc = SourceLocation::getFromRawEncoding(I->first);
313+
for (auto I = AtProps.begin(), E = AtProps.end(); I != E; ++I) {
314+
SourceLocation AtLoc = I->first;
317315
IndivPropsTy &IndProps = I->second;
318316
checkAllAtProps(MigrateCtx, AtLoc, IndProps);
319317
}

clang/lib/ARCMigrate/TransProperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ class PropertiesRewriter {
337337
return false;
338338
if (props.empty())
339339
return false;
340-
return MigrateCtx.AtPropsWeak.count(atLoc.getRawEncoding());
340+
return MigrateCtx.AtPropsWeak.count(atLoc);
341341
}
342342

343343
bool isUserDeclared(ObjCIvarDecl *ivarD) const {

clang/lib/ARCMigrate/Transforms.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ class MigrationContext {
9393
bool FullyMigratable;
9494
};
9595
std::vector<GCAttrOccurrence> GCAttrs;
96-
llvm::DenseSet<unsigned> AttrSet;
97-
llvm::DenseSet<unsigned> RemovedAttrSet;
96+
llvm::DenseSet<SourceLocation> AttrSet;
97+
llvm::DenseSet<SourceLocation> RemovedAttrSet;
9898

9999
/// Set of raw '@' locations for 'assign' properties group that contain
100100
/// GC __weak.
101-
llvm::DenseSet<unsigned> AtPropsWeak;
101+
llvm::DenseSet<SourceLocation> AtPropsWeak;
102102

103103
explicit MigrationContext(MigrationPass &pass) : Pass(pass) {}
104104
~MigrationContext();

clang/lib/CodeGen/CGOpenMPRuntime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ class CGOpenMPRuntime {
391391
private:
392392

393393
/// Map for SourceLocation and OpenMP runtime library debug locations.
394-
typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
394+
typedef llvm::DenseMap<SourceLocation, llvm::Value *> OpenMPDebugLocMapTy;
395395
OpenMPDebugLocMapTy OpenMPDebugLocMap;
396396
/// The type for a microtask which gets passed to __kmpc_fork_call().
397397
/// Original representation is:

clang/lib/Edit/EditedSource.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void EditedSource::finishedCommit() {
5959
SourceLocation ExpLoc;
6060
MacroArgUse ArgUse;
6161
std::tie(ExpLoc, ArgUse) = ExpArg;
62-
auto &ArgUses = ExpansionToArgMap[ExpLoc.getRawEncoding()];
62+
auto &ArgUses = ExpansionToArgMap[ExpLoc];
6363
if (llvm::find(ArgUses, ArgUse) == ArgUses.end())
6464
ArgUses.push_back(ArgUse);
6565
}
@@ -82,7 +82,7 @@ bool EditedSource::canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs) {
8282
SourceLocation ExpLoc;
8383
MacroArgUse ArgUse;
8484
deconstructMacroArgLoc(OrigLoc, ExpLoc, ArgUse);
85-
auto I = ExpansionToArgMap.find(ExpLoc.getRawEncoding());
85+
auto I = ExpansionToArgMap.find(ExpLoc);
8686
if (I != ExpansionToArgMap.end() &&
8787
find_if(I->second, [&](const MacroArgUse &U) {
8888
return ArgUse.Identifier == U.Identifier &&

clang/lib/Tooling/Syntax/BuildTree.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ class syntax::TreeBuilder {
367367
public:
368368
TreeBuilder(syntax::Arena &Arena) : Arena(Arena), Pending(Arena) {
369369
for (const auto &T : Arena.getTokenBuffer().expandedTokens())
370-
LocationToToken.insert({T.location().getRawEncoding(), &T});
370+
LocationToToken.insert({T.location(), &T});
371371
}
372372

373373
llvm::BumpPtrAllocator &allocator() { return Arena.getAllocator(); }
@@ -689,8 +689,7 @@ class syntax::TreeBuilder {
689689

690690
syntax::Arena &Arena;
691691
/// To quickly find tokens by their start location.
692-
llvm::DenseMap</*SourceLocation*/ unsigned, const syntax::Token *>
693-
LocationToToken;
692+
llvm::DenseMap<SourceLocation, const syntax::Token *> LocationToToken;
694693
Forest Pending;
695694
llvm::DenseSet<Decl *> DeclsWithoutSemicolons;
696695
ASTToSyntaxMapping Mapping;
@@ -1708,7 +1707,7 @@ void syntax::TreeBuilder::markExprChild(Expr *Child, NodeRole Role) {
17081707
const syntax::Token *syntax::TreeBuilder::findToken(SourceLocation L) const {
17091708
if (L.isInvalid())
17101709
return nullptr;
1711-
auto It = LocationToToken.find(L.getRawEncoding());
1710+
auto It = LocationToToken.find(L);
17121711
assert(It != LocationToToken.end());
17131712
return It->second;
17141713
}

clang/lib/Tooling/Syntax/Tokens.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,11 @@ class TokenCollector::CollectPPExpansions : public PPCallbacks {
575575
// A's startpoint.
576576
if (!Range.getBegin().isFileID()) {
577577
Range.setBegin(SM.getExpansionLoc(Range.getBegin()));
578-
assert(Collector->Expansions.count(Range.getBegin().getRawEncoding()) &&
578+
assert(Collector->Expansions.count(Range.getBegin()) &&
579579
"Overlapping macros should have same expansion location");
580580
}
581581

582-
Collector->Expansions[Range.getBegin().getRawEncoding()] = Range.getEnd();
582+
Collector->Expansions[Range.getBegin()] = Range.getEnd();
583583
LastExpansionEnd = Range.getEnd();
584584
}
585585
// FIXME: handle directives like #pragma, #include, etc.
@@ -711,8 +711,8 @@ class TokenCollector::Builder {
711711
// If we know mapping bounds at [NextSpelled, KnownEnd] (macro expansion)
712712
// then we want to partition our (empty) mapping.
713713
// [Start, NextSpelled) [NextSpelled, KnownEnd] (KnownEnd, Target)
714-
SourceLocation KnownEnd = CollectedExpansions.lookup(
715-
SpelledTokens[NextSpelled].location().getRawEncoding());
714+
SourceLocation KnownEnd =
715+
CollectedExpansions.lookup(SpelledTokens[NextSpelled].location());
716716
if (KnownEnd.isValid()) {
717717
FlushMapping(); // Emits [Start, NextSpelled)
718718
while (NextSpelled < SpelledTokens.size() &&
@@ -749,7 +749,7 @@ class TokenCollector::Builder {
749749
// We need no mapping for file tokens copied to the expanded stream.
750750
} else {
751751
// We found a new macro expansion. We should have its spelling bounds.
752-
auto End = CollectedExpansions.lookup(Expansion.getRawEncoding());
752+
auto End = CollectedExpansions.lookup(Expansion);
753753
assert(End.isValid() && "Macro expansion wasn't captured?");
754754

755755
// Mapping starts here...

0 commit comments

Comments
 (0)