[clang-tools-extra] 098e40e - [clangd] Add index export to dexp

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 19 05:35:06 PDT 2020


Author: Sam McCall
Date: 2020-04-19T14:34:46+02:00
New Revision: 098e40eac524fd7bcad72d37378d25f4305de512

URL: https://github.com/llvm/llvm-project/commit/098e40eac524fd7bcad72d37378d25f4305de512
DIFF: https://github.com/llvm/llvm-project/commit/098e40eac524fd7bcad72d37378d25f4305de512.diff

LOG: [clangd] Add index export to dexp

Summary: Add a command to dexp that exports index data in chosen format (e.g. YAML).

Reviewers: sammccall

Subscribers: kbobyrev, mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D77385

Added: 
    

Modified: 
    clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp b/clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp
index ae49f9437211..015246d1a82f 100644
--- a/clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp
+++ b/clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp
@@ -258,6 +258,56 @@ class Refs : public Command {
   }
 };
 
+class Export : public Command {
+  llvm::cl::opt<IndexFileFormat> Format{
+      "format",
+      llvm::cl::desc("Format of index export"),
+      llvm::cl::values(
+          clEnumValN(IndexFileFormat::YAML, "yaml",
+                     "human-readable YAML format"),
+          clEnumValN(IndexFileFormat::RIFF, "binary", "binary RIFF format")),
+      llvm::cl::init(IndexFileFormat::YAML),
+  };
+  llvm::cl::opt<std::string> OutputFile{
+      "output-file",
+      llvm::cl::Positional,
+      llvm::cl::Required,
+      llvm::cl::desc("Output file for export"),
+  };
+
+public:
+  void run() {
+    using namespace clang::clangd;
+    // Read input file (as specified in global option)
+    auto Buffer = llvm::MemoryBuffer::getFile(IndexPath);
+    if (!Buffer) {
+      llvm::errs() << llvm::formatv("Can't open {0}", IndexPath) << "\n";
+      return;
+    }
+
+    // Auto-detects input format when parsing
+    auto IndexIn = clang::clangd::readIndexFile(Buffer->get()->getBuffer());
+    if (!IndexIn) {
+      llvm::errs() << llvm::toString(IndexIn.takeError()) << "\n";
+      return;
+    }
+
+    // Prepare output file
+    std::error_code EC;
+    llvm::raw_fd_ostream OutputStream(OutputFile, EC);
+    if (EC) {
+      llvm::errs() << llvm::formatv("Can't open {0} for writing", OutputFile)
+                   << "\n";
+      return;
+    }
+
+    // Export
+    clang::clangd::IndexFileOut IndexOut(IndexIn.get());
+    IndexOut.Format = Format;
+    OutputStream << IndexOut;
+  }
+};
+
 struct {
   const char *Name;
   const char *Description;
@@ -268,6 +318,7 @@ struct {
      std::make_unique<Lookup>},
     {"refs", "Find references by ID or qualified name",
      std::make_unique<Refs>},
+    {"export", "Export index", std::make_unique<Export>},
 };
 
 std::unique_ptr<SymbolIndex> openIndex(llvm::StringRef Index) {


        


More information about the cfe-commits mailing list