Skip to content

Commit d1978fa

Browse files
committed
[clangd] Deduplicate scopes in IncludeFixer queries
Differential Revision: https://reviews.llvm.org/D95942
1 parent 447dc85 commit d1978fa

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

clang-tools-extra/clangd/IncludeFixer.cpp

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,14 @@
4040
#include "llvm/ADT/StringSet.h"
4141
#include "llvm/Support/Error.h"
4242
#include "llvm/Support/FormatVariadic.h"
43+
#include <algorithm>
44+
#include <set>
45+
#include <string>
4346
#include <vector>
4447

4548
namespace clang {
4649
namespace clangd {
4750

48-
namespace {
49-
50-
// Collects contexts visited during a Sema name lookup.
51-
class VisitedContextCollector : public VisibleDeclConsumer {
52-
public:
53-
void EnteredContext(DeclContext *Ctx) override { Visited.push_back(Ctx); }
54-
55-
void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
56-
bool InBaseClass) override {}
57-
58-
std::vector<DeclContext *> takeVisitedContexts() {
59-
return std::move(Visited);
60-
}
61-
62-
private:
63-
std::vector<DeclContext *> Visited;
64-
};
65-
66-
} // namespace
67-
6851
std::vector<Fix> IncludeFixer::fix(DiagnosticsEngine::Level DiagLevel,
6952
const clang::Diagnostic &Info) const {
7053
switch (Info.getID()) {
@@ -313,17 +296,26 @@ llvm::Optional<CheapUnresolvedName> extractUnresolvedNameCheaply(
313296
std::vector<std::string>
314297
collectAccessibleScopes(Sema &Sem, const DeclarationNameInfo &Typo, Scope *S,
315298
Sema::LookupNameKind LookupKind) {
299+
// Collects contexts visited during a Sema name lookup.
300+
struct VisitedContextCollector : public VisibleDeclConsumer {
301+
VisitedContextCollector(std::vector<std::string> &Out) : Out(Out) {}
302+
void EnteredContext(DeclContext *Ctx) override {
303+
if (llvm::isa<NamespaceDecl>(Ctx))
304+
Out.push_back(printNamespaceScope(*Ctx));
305+
}
306+
void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
307+
bool InBaseClass) override {}
308+
std::vector<std::string> &Out;
309+
};
310+
316311
std::vector<std::string> Scopes;
317-
VisitedContextCollector Collector;
312+
Scopes.push_back("");
313+
VisitedContextCollector Collector(Scopes);
318314
Sem.LookupVisibleDecls(S, LookupKind, Collector,
319315
/*IncludeGlobalScope=*/false,
320316
/*LoadExternal=*/false);
321-
322-
Scopes.push_back("");
323-
for (const auto *Ctx : Collector.takeVisitedContexts()) {
324-
if (isa<NamespaceDecl>(Ctx))
325-
Scopes.push_back(printNamespaceScope(*Ctx));
326-
}
317+
std::sort(Scopes.begin(), Scopes.end());
318+
Scopes.erase(std::unique(Scopes.begin(), Scopes.end()), Scopes.end());
327319
return Scopes;
328320
}
329321

0 commit comments

Comments
 (0)