diff --git a/clang-tools-extra/clangd/IncludeFixer.cpp b/clang-tools-extra/clangd/IncludeFixer.cpp --- a/clang-tools-extra/clangd/IncludeFixer.cpp +++ b/clang-tools-extra/clangd/IncludeFixer.cpp @@ -40,31 +40,14 @@ #include "llvm/ADT/StringSet.h" #include "llvm/Support/Error.h" #include "llvm/Support/FormatVariadic.h" +#include +#include +#include #include namespace clang { namespace clangd { -namespace { - -// Collects contexts visited during a Sema name lookup. -class VisitedContextCollector : public VisibleDeclConsumer { -public: - void EnteredContext(DeclContext *Ctx) override { Visited.push_back(Ctx); } - - void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, - bool InBaseClass) override {} - - std::vector takeVisitedContexts() { - return std::move(Visited); - } - -private: - std::vector Visited; -}; - -} // namespace - std::vector IncludeFixer::fix(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) const { switch (Info.getID()) { @@ -313,17 +296,26 @@ std::vector collectAccessibleScopes(Sema &Sem, const DeclarationNameInfo &Typo, Scope *S, Sema::LookupNameKind LookupKind) { + // Collects contexts visited during a Sema name lookup. + struct VisitedContextCollector : public VisibleDeclConsumer { + VisitedContextCollector(std::vector &Out) : Out(Out) {} + void EnteredContext(DeclContext *Ctx) override { + if (llvm::isa(Ctx)) + Out.push_back(printNamespaceScope(*Ctx)); + } + void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, + bool InBaseClass) override {} + std::vector &Out; + }; + std::vector Scopes; - VisitedContextCollector Collector; + Scopes.push_back(""); + VisitedContextCollector Collector(Scopes); Sem.LookupVisibleDecls(S, LookupKind, Collector, /*IncludeGlobalScope=*/false, /*LoadExternal=*/false); - - Scopes.push_back(""); - for (const auto *Ctx : Collector.takeVisitedContexts()) { - if (isa(Ctx)) - Scopes.push_back(printNamespaceScope(*Ctx)); - } + std::sort(Scopes.begin(), Scopes.end()); + Scopes.erase(std::unique(Scopes.begin(), Scopes.end()), Scopes.end()); return Scopes; }