kbobyrev updated this revision to Diff 161239.
kbobyrev added a comment.

Fix anonymous namespace beginning placement in Clangd driver.


https://reviews.llvm.org/D50897

Files:
  clang-tools-extra/clangd/index/Index.h
  clang-tools-extra/clangd/index/MemIndex.cpp
  clang-tools-extra/clangd/index/MemIndex.h
  clang-tools-extra/clangd/tool/ClangdMain.cpp
  clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp
  clang-tools-extra/unittests/clangd/TestTU.cpp

Index: clang-tools-extra/unittests/clangd/TestTU.cpp
===================================================================
--- clang-tools-extra/unittests/clangd/TestTU.cpp
+++ clang-tools-extra/unittests/clangd/TestTU.cpp
@@ -49,7 +49,7 @@
 }
 
 std::unique_ptr<SymbolIndex> TestTU::index() const {
-  return MemIndex::build(headerSymbols());
+  return SymbolIndex::build<MemIndex>(headerSymbols());
 }
 
 const Symbol &findSymbol(const SymbolSlab &Slab, llvm::StringRef QName) {
Index: clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp
===================================================================
--- clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp
+++ clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp
@@ -76,7 +76,7 @@
   SymbolSlab::Builder Slab;
   for (const auto &Sym : Symbols)
     Slab.insert(Sym);
-  return MemIndex::build(std::move(Slab).build());
+  return SymbolIndex::build<MemIndex>(std::move(Slab).build());
 }
 
 CodeCompleteResult completions(ClangdServer &Server, StringRef TestCode,
Index: clang-tools-extra/clangd/tool/ClangdMain.cpp
===================================================================
--- clang-tools-extra/clangd/tool/ClangdMain.cpp
+++ clang-tools-extra/clangd/tool/ClangdMain.cpp
@@ -7,48 +7,28 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <cstdlib>
+#include <iostream>
+#include <memory>
+#include <string>
+#include <thread>
 #include "ClangdLSPServer.h"
 #include "JSONRPCDispatcher.h"
 #include "Path.h"
 #include "Trace.h"
-#include "index/SymbolYAML.h"
 #include "clang/Basic/Version.h"
+#include "index/SymbolYAML.h"
+#include "index/dex/DexIndex.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Program.h"
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/raw_ostream.h"
-#include <cstdlib>
-#include <iostream>
-#include <memory>
-#include <string>
-#include <thread>
 
 using namespace clang;
 using namespace clang::clangd;
 
-namespace {
-enum class PCHStorageFlag { Disk, Memory };
-
-// Build an in-memory static index for global symbols from a YAML-format file.
-// The size of global symbols should be relatively small, so that all symbols
-// can be managed in memory.
-std::unique_ptr<SymbolIndex> buildStaticIndex(llvm::StringRef YamlSymbolFile) {
-  auto Buffer = llvm::MemoryBuffer::getFile(YamlSymbolFile);
-  if (!Buffer) {
-    llvm::errs() << "Can't open " << YamlSymbolFile << "\n";
-    return nullptr;
-  }
-  auto Slab = symbolsFromYAML(Buffer.get()->getBuffer());
-  SymbolSlab::Builder SymsBuilder;
-  for (auto Sym : Slab)
-    SymsBuilder.insert(Sym);
-
-  return MemIndex::build(std::move(SymsBuilder).build());
-}
-} // namespace
-
 static llvm::cl::opt<Path> CompileCommandsDir(
     "compile-commands-dir",
     llvm::cl::desc("Specify a path to look for compile_commands.json. If path "
@@ -113,6 +93,31 @@
         "Intended to simplify lit tests."),
     llvm::cl::init(false), llvm::cl::Hidden);
 
+
+namespace {
+
+enum class PCHStorageFlag { Disk, Memory };
+
+// Build an in-memory static index for global symbols from a YAML-format file.
+// The size of global symbols should be relatively small, so that all symbols
+// can be managed in memory.
+std::unique_ptr<SymbolIndex> buildStaticIndex(llvm::StringRef YamlSymbolFile) {
+  auto Buffer = llvm::MemoryBuffer::getFile(YamlSymbolFile);
+  if (!Buffer) {
+    llvm::errs() << "Can't open " << YamlSymbolFile << "\n";
+    return nullptr;
+  }
+  auto Slab = symbolsFromYAML(Buffer.get()->getBuffer());
+  SymbolSlab::Builder SymsBuilder;
+  for (auto Sym : Slab) SymsBuilder.insert(Sym);
+
+  return UseDex
+             ? SymbolIndex::build<dex::DexIndex>(std::move(SymsBuilder).build())
+             : SymbolIndex::build<MemIndex>(std::move(SymsBuilder).build());
+}
+
+}  // namespace
+
 static llvm::cl::opt<PCHStorageFlag> PCHStorage(
     "pch-storage",
     llvm::cl::desc("Storing PCHs in memory increases memory usages, but may "
@@ -185,6 +190,10 @@
                                 "'compile_commands.json' files")),
     llvm::cl::init(FilesystemCompileArgs), llvm::cl::Hidden);
 
+static llvm::cl::opt<bool> UseDex(
+    "use-dex-index", llvm::cl::desc("Use experimental Dex static index."),
+    llvm::cl::init(false), llvm::cl::Hidden);
+
 int main(int argc, char *argv[]) {
   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
   llvm::cl::SetVersionPrinter([](llvm::raw_ostream &OS) {
Index: clang-tools-extra/clangd/index/MemIndex.h
===================================================================
--- clang-tools-extra/clangd/index/MemIndex.h
+++ clang-tools-extra/clangd/index/MemIndex.h
@@ -24,9 +24,6 @@
   /// accessible as long as `Symbols` is kept alive.
   void build(std::shared_ptr<std::vector<const Symbol *>> Symbols);
 
-  /// \brief Build index from a symbol slab.
-  static std::unique_ptr<SymbolIndex> build(SymbolSlab Slab);
-
   bool
   fuzzyFind(const FuzzyFindRequest &Req,
             llvm::function_ref<void(const Symbol &)> Callback) const override;
Index: clang-tools-extra/clangd/index/MemIndex.cpp
===================================================================
--- clang-tools-extra/clangd/index/MemIndex.cpp
+++ clang-tools-extra/clangd/index/MemIndex.cpp
@@ -71,22 +71,6 @@
   }
 }
 
-std::unique_ptr<SymbolIndex> MemIndex::build(SymbolSlab Slab) {
-  struct Snapshot {
-    SymbolSlab Slab;
-    std::vector<const Symbol *> Pointers;
-  };
-  auto Snap = std::make_shared<Snapshot>();
-  Snap->Slab = std::move(Slab);
-  for (auto &Sym : Snap->Slab)
-    Snap->Pointers.push_back(&Sym);
-  auto S = std::shared_ptr<std::vector<const Symbol *>>(std::move(Snap),
-                                                        &Snap->Pointers);
-  auto MemIdx = llvm::make_unique<MemIndex>();
-  MemIdx->build(std::move(S));
-  return std::move(MemIdx);
-}
-
 void MemIndex::findOccurrences(
     const OccurrencesRequest &Req,
     llvm::function_ref<void(const SymbolOccurrence &)> Callback) const {
Index: clang-tools-extra/clangd/index/Index.h
===================================================================
--- clang-tools-extra/clangd/index/Index.h
+++ clang-tools-extra/clangd/index/Index.h
@@ -355,6 +355,23 @@
 /// matching symbols among a set of symbols based on names or unique IDs.
 class SymbolIndex {
 public:
+ /// \brief Build index from a symbol slab.
+ template <typename T>
+ static std::unique_ptr<SymbolIndex> build(SymbolSlab Slab) {
+   struct Snapshot {
+     SymbolSlab Slab;
+     std::vector<const Symbol *> Pointers;
+   };
+   auto Snap = std::make_shared<Snapshot>();
+   Snap->Slab = std::move(Slab);
+   for (auto &Sym : Snap->Slab) Snap->Pointers.push_back(&Sym);
+   auto S = std::shared_ptr<std::vector<const Symbol *>>(std::move(Snap),
+                                                         &Snap->Pointers);
+   auto Idx = llvm::make_unique<T>();
+   Idx->build(std::move(S));
+   return std::move(Idx);
+ }
+
   virtual ~SymbolIndex() = default;
 
   /// \brief Matches symbols in the index fuzzily and applies \p Callback on
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to