19 #include "clang/Tooling/ArgumentsAdjusters.h"
20 #include "clang/Tooling/CommonOptionsParser.h"
21 #include "clang/Tooling/Execution.h"
22 #include "clang/Tooling/Tooling.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Signals.h"
30 static llvm::cl::opt<IndexFileFormat>
31 Format(
"format", llvm::cl::desc(
"Format of the index to be written"),
33 "human-readable YAML format"),
35 "binary RIFF format")),
38 class IndexActionFactory :
public tooling::FrontendActionFactory {
40 IndexActionFactory(IndexFileIn &Result) : Result(Result) {}
42 std::unique_ptr<FrontendAction> create()
override {
43 SymbolCollector::Options Opts;
44 Opts.CountReferences =
true;
49 std::lock_guard<std::mutex> Lock(SymbolsMu);
50 for (
const auto &Sym : S) {
51 if (
const auto *Existing =
Symbols.find(Sym.ID))
58 std::lock_guard<std::mutex> Lock(SymbolsMu);
59 for (
const auto &Sym : S) {
61 for (
const auto &Ref : Sym.second)
62 Refs.insert(Sym.first, Ref);
66 std::lock_guard<std::mutex> Lock(SymbolsMu);
67 for (
const auto &R : S) {
76 ~IndexActionFactory() {
77 Result.Symbols = std::move(
Symbols).build();
78 Result.Refs = std::move(
Refs).build();
79 Result.Relations = std::move(Relations).build();
94 int main(
int argc,
const char **argv) {
95 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
97 const char *Overview = R
"(
98 Creates an index of symbol information etc in a whole project.
100 Example usage for a project using CMake compile commands:
102 $ clangd-indexer --executor=all-TUs compile_commands.json > clangd.dex
104 Example usage for file sequence index without flags:
106 $ clangd-indexer File1.cpp File2.cpp ... FileN.cpp > clangd.dex
108 Note: only symbols from header files will be indexed.
111 auto Executor = clang::tooling::createExecutorFromCommandLineArgs(
112 argc, argv, llvm::cl::GeneralCategory, Overview);
121 auto Err = Executor->get()->execute(
122 std::make_unique<clang::clangd::IndexActionFactory>(Data),
123 clang::tooling::getStripPluginsAdjuster());
130 Out.Format = clang::clangd::Format;