Skip to content

Commit 6368c74

Browse files
committed
Adapt ScopedDeclCollector to track number of references
Use a DenseMap to track the number of references to a given decl in a scope.
1 parent e9ba6c7 commit 6368c74

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5487,18 +5487,20 @@ class ReferenceCollector : private SourceEntityWalker {
54875487
};
54885488

54895489
/// Similar to the \c ReferenceCollector but collects references in all scopes
5490-
/// without any starting point in each scope.
5490+
/// without any starting point in each scope. In addition, it tracks the number
5491+
/// of references to a decl in a given scope.
54915492
class ScopedDeclCollector : private SourceEntityWalker {
54925493
public:
54935494
using DeclsTy = llvm::DenseSet<const Decl *>;
5495+
using RefDeclsTy = llvm::DenseMap<const Decl *, /*numRefs*/ unsigned>;
54945496

54955497
private:
5496-
using ScopedDeclsTy = llvm::DenseMap<const Stmt *, DeclsTy>;
5498+
using ScopedDeclsTy = llvm::DenseMap<const Stmt *, RefDeclsTy>;
54975499

54985500
struct Scope {
54995501
DeclsTy DeclaredDecls;
5500-
DeclsTy *ReferencedDecls;
5501-
Scope(DeclsTy *ReferencedDecls) : DeclaredDecls(),
5502+
RefDeclsTy *ReferencedDecls;
5503+
Scope(RefDeclsTy *ReferencedDecls) : DeclaredDecls(),
55025504
ReferencedDecls(ReferencedDecls) {}
55035505
};
55045506

@@ -5514,7 +5516,7 @@ class ScopedDeclCollector : private SourceEntityWalker {
55145516
walk(Node);
55155517
}
55165518

5517-
DeclsTy *getReferencedDecls(Stmt *Scope) {
5519+
const RefDeclsTy *getReferencedDecls(Stmt *Scope) const {
55185520
auto Res = ReferencedDecls.find(Scope);
55195521
if (Res == ReferencedDecls.end())
55205522
return nullptr;
@@ -5528,7 +5530,7 @@ class ScopedDeclCollector : private SourceEntityWalker {
55285530

55295531
ScopeStack.back().DeclaredDecls.insert(D);
55305532
if (isa<DeclContext>(D))
5531-
ScopeStack.back().ReferencedDecls->insert(D);
5533+
(*ScopeStack.back().ReferencedDecls)[D] += 1;
55325534
return true;
55335535
}
55345536

@@ -5539,8 +5541,10 @@ class ScopedDeclCollector : private SourceEntityWalker {
55395541
if (!E->isImplicit()) {
55405542
if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
55415543
if (auto *D = DRE->getDecl()) {
5544+
// If we have a reference that isn't declared in the same scope,
5545+
// increment the number of references to that decl.
55425546
if (!D->isImplicit() && !ScopeStack.back().DeclaredDecls.count(D))
5543-
ScopeStack.back().ReferencedDecls->insert(D);
5547+
(*ScopeStack.back().ReferencedDecls)[D] += 1;
55445548
}
55455549
}
55465550
}
@@ -5563,9 +5567,10 @@ class ScopedDeclCollector : private SourceEntityWalker {
55635567
// Add any referenced decls to the parent scope that weren't declared
55645568
// there.
55655569
auto &ParentStack = ScopeStack[NumScopes - 2];
5566-
for (auto *D : *ScopeStack.back().ReferencedDecls) {
5570+
for (auto DeclAndNumRefs : *ScopeStack.back().ReferencedDecls) {
5571+
auto *D = DeclAndNumRefs.first;
55675572
if (!ParentStack.DeclaredDecls.count(D))
5568-
ParentStack.ReferencedDecls->insert(D);
5573+
(*ParentStack.ReferencedDecls)[D] += DeclAndNumRefs.second;
55695574
}
55705575
}
55715576
ScopeStack.pop_back();
@@ -6119,7 +6124,10 @@ class AsyncConverter : private SourceEntityWalker {
61196124
// The body of those statements will include the decls if they've been
61206125
// referenced, so shadowing is still avoided there.
61216126
if (auto *ReferencedDecls = ScopedDecls.getReferencedDecls(S)) {
6122-
addNewScope(*ReferencedDecls);
6127+
llvm::DenseSet<const Decl *> Decls;
6128+
for (auto DeclAndNumRefs : *ReferencedDecls)
6129+
Decls.insert(DeclAndNumRefs.first);
6130+
addNewScope(Decls);
61236131
} else {
61246132
addNewScope({});
61256133
}
@@ -6740,8 +6748,8 @@ class AsyncConverter : private SourceEntityWalker {
67406748

67416749
void addNewScope(const llvm::DenseSet<const Decl *> &Decls) {
67426750
ScopedNames.push_back({});
6743-
for (auto *D : Decls) {
6744-
auto Name = getDeclName(D);
6751+
for (auto DeclAndNumRefs : Decls) {
6752+
auto Name = getDeclName(DeclAndNumRefs);
67456753
if (!Name.empty())
67466754
ScopedNames.back().insert(Name);
67476755
}

0 commit comments

Comments
 (0)