[PATCH] D95942: [clangd] Deduplicate scopes in IncludeFixer queries

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 3 09:15:39 PST 2021


kadircet updated this revision to Diff 321118.
kadircet added a comment.
Herald added a subscriber: mgrang.

- sort+unique+erase instead of using a set
- Inline visitor and directly collect scopes there.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D95942/new/

https://reviews.llvm.org/D95942

Files:
  clang-tools-extra/clangd/IncludeFixer.cpp


Index: clang-tools-extra/clangd/IncludeFixer.cpp
===================================================================
--- clang-tools-extra/clangd/IncludeFixer.cpp
+++ 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,25 @@
 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;
+  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;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95942.321118.patch
Type: text/x-patch
Size: 2443 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210203/08217910/attachment.bin>


More information about the cfe-commits mailing list