[clang-tools-extra] d1978fa - [clangd] Deduplicate scopes in IncludeFixer queries
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 5 05:03:00 PST 2021
Author: Kadir Cetinkaya
Date: 2021-02-05T14:02:50+01:00
New Revision: d1978fa4bf0d7da0b3cd88879e9411467edcb01f
URL: https://github.com/llvm/llvm-project/commit/d1978fa4bf0d7da0b3cd88879e9411467edcb01f
DIFF: https://github.com/llvm/llvm-project/commit/d1978fa4bf0d7da0b3cd88879e9411467edcb01f.diff
LOG: [clangd] Deduplicate scopes in IncludeFixer queries
Differential Revision: https://reviews.llvm.org/D95942
Added:
Modified:
clang-tools-extra/clangd/IncludeFixer.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/IncludeFixer.cpp b/clang-tools-extra/clangd/IncludeFixer.cpp
index 2b79c6eb4fa6..babc3426b868 100644
--- 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 <algorithm>
+#include <set>
+#include <string>
#include <vector>
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<DeclContext *> takeVisitedContexts() {
- return std::move(Visited);
- }
-
-private:
- std::vector<DeclContext *> Visited;
-};
-
-} // namespace
-
std::vector<Fix> IncludeFixer::fix(DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic &Info) const {
switch (Info.getID()) {
@@ -313,17 +296,26 @@ llvm::Optional<CheapUnresolvedName> extractUnresolvedNameCheaply(
std::vector<std::string>
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<std::string> &Out) : Out(Out) {}
+ void EnteredContext(DeclContext *Ctx) override {
+ if (llvm::isa<NamespaceDecl>(Ctx))
+ Out.push_back(printNamespaceScope(*Ctx));
+ }
+ void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
+ bool InBaseClass) override {}
+ std::vector<std::string> &Out;
+ };
+
std::vector<std::string> 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<NamespaceDecl>(Ctx))
- Scopes.push_back(printNamespaceScope(*Ctx));
- }
+ std::sort(Scopes.begin(), Scopes.end());
+ Scopes.erase(std::unique(Scopes.begin(), Scopes.end()), Scopes.end());
return Scopes;
}
More information about the cfe-commits
mailing list