Skip to content

Commit 098e40e

Browse files
committed
[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
1 parent cceb630 commit 098e40e

File tree

1 file changed

+51
-0
lines changed
  • clang-tools-extra/clangd/index/dex/dexp

1 file changed

+51
-0
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,56 @@ class Refs : public Command {
258258
}
259259
};
260260

261+
class Export : public Command {
262+
llvm::cl::opt<IndexFileFormat> Format{
263+
"format",
264+
llvm::cl::desc("Format of index export"),
265+
llvm::cl::values(
266+
clEnumValN(IndexFileFormat::YAML, "yaml",
267+
"human-readable YAML format"),
268+
clEnumValN(IndexFileFormat::RIFF, "binary", "binary RIFF format")),
269+
llvm::cl::init(IndexFileFormat::YAML),
270+
};
271+
llvm::cl::opt<std::string> OutputFile{
272+
"output-file",
273+
llvm::cl::Positional,
274+
llvm::cl::Required,
275+
llvm::cl::desc("Output file for export"),
276+
};
277+
278+
public:
279+
void run() {
280+
using namespace clang::clangd;
281+
// Read input file (as specified in global option)
282+
auto Buffer = llvm::MemoryBuffer::getFile(IndexPath);
283+
if (!Buffer) {
284+
llvm::errs() << llvm::formatv("Can't open {0}", IndexPath) << "\n";
285+
return;
286+
}
287+
288+
// Auto-detects input format when parsing
289+
auto IndexIn = clang::clangd::readIndexFile(Buffer->get()->getBuffer());
290+
if (!IndexIn) {
291+
llvm::errs() << llvm::toString(IndexIn.takeError()) << "\n";
292+
return;
293+
}
294+
295+
// Prepare output file
296+
std::error_code EC;
297+
llvm::raw_fd_ostream OutputStream(OutputFile, EC);
298+
if (EC) {
299+
llvm::errs() << llvm::formatv("Can't open {0} for writing", OutputFile)
300+
<< "\n";
301+
return;
302+
}
303+
304+
// Export
305+
clang::clangd::IndexFileOut IndexOut(IndexIn.get());
306+
IndexOut.Format = Format;
307+
OutputStream << IndexOut;
308+
}
309+
};
310+
261311
struct {
262312
const char *Name;
263313
const char *Description;
@@ -268,6 +318,7 @@ struct {
268318
std::make_unique<Lookup>},
269319
{"refs", "Find references by ID or qualified name",
270320
std::make_unique<Refs>},
321+
{"export", "Export index", std::make_unique<Export>},
271322
};
272323

273324
std::unique_ptr<SymbolIndex> openIndex(llvm::StringRef Index) {

0 commit comments

Comments
 (0)