clang-tools 8.0.0

IndexerMain.cpp
Go to the documentation of this file.
1 //===--- IndexerMain.cpp -----------------------------------------*- C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // clangd-indexer is a tool to gather index data (symbols, xrefs) from source.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "index/Index.h"
15 #include "index/IndexAction.h"
16 #include "index/Merge.h"
17 #include "index/Serialization.h"
18 #include "index/SymbolCollector.h"
19 #include "clang/Tooling/CommonOptionsParser.h"
20 #include "clang/Tooling/Execution.h"
21 #include "clang/Tooling/Tooling.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Signals.h"
24
25 namespace clang {
26 namespace clangd {
27 namespace {
28
29 static llvm::cl::opt<IndexFileFormat>
30 Format("format", llvm::cl::desc("Format of the index to be written"),
31 llvm::cl::values(clEnumValN(IndexFileFormat::YAML, "yaml",
32 "human-readable YAML format"),
33 clEnumValN(IndexFileFormat::RIFF, "binary",
34 "binary RIFF format")),
35 llvm::cl::init(IndexFileFormat::RIFF));
36
37 class IndexActionFactory : public tooling::FrontendActionFactory {
38 public:
39 IndexActionFactory(IndexFileIn &Result) : Result(Result) {}
40
41 clang::FrontendAction *create() override {
42 SymbolCollector::Options Opts;
43 return createStaticIndexingAction(
44 Opts,
45 [&](SymbolSlab S) {
46 // Merge as we go.
47 std::lock_guard<std::mutex> Lock(SymbolsMu);
48 for (const auto &Sym : S) {
49 if (const auto *Existing = Symbols.find(Sym.ID))
50 Symbols.insert(mergeSymbol(*Existing, Sym));
51 else
52 Symbols.insert(Sym);
53 }
54 },
55 [&](RefSlab S) {
56 std::lock_guard<std::mutex> Lock(SymbolsMu);
57 for (const auto &Sym : S) {
58 // No need to merge as currently all Refs are from main file.
59 for (const auto &Ref : Sym.second)
60 Refs.insert(Sym.first, Ref);
61 }
62 },
63 /*IncludeGraphCallback=*/nullptr)
64 .release();
65 }
66
67 // Awkward: we write the result in the destructor, because the executor
68 // takes ownership so it's the easiest way to get our data back out.
69 ~IndexActionFactory() {
70 Result.Symbols = std::move(Symbols).build();
71 Result.Refs = std::move(Refs).build();
72 }
73
74 private:
75 IndexFileIn &Result;
76 std::mutex SymbolsMu;
77 SymbolSlab::Builder Symbols;
78 RefSlab::Builder Refs;
79 };
80
81 } // namespace
82 } // namespace clangd
83 } // namespace clang
84
85 int main(int argc, const char **argv) {
86 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
87
88 const char *Overview = R"(
89 Creates an index of symbol information etc in a whole project.
90
91 Example usage for a project using CMake compile commands:
92
93 $ clangd-indexer --executor=all-TUs compile_commands.json > clangd.dex
94
95 Example usage for file sequence index without flags:
96
97 $ clangd-indexer File1.cpp File2.cpp ... FileN.cpp > clangd.dex
98
99 Note: only symbols from header files will be indexed.
100 )";
101
102 auto Executor = clang::tooling::createExecutorFromCommandLineArgs(
103 argc, argv, llvm::cl::GeneralCategory, Overview);
104
105 if (!Executor) {
106 llvm::errs() << llvm::toString(Executor.takeError()) << "\n";
107 return 1;
108 }
109
110 // Collect symbols found in each translation unit, merging as we go.
111 clang::clangd::IndexFileIn Data;
112 auto Err = Executor->get()->execute(
113 llvm::make_unique<clang::clangd::IndexActionFactory>(Data));
114 if (Err) {
115 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
116 }
117
118 // Emit collected data.
119 clang::clangd::IndexFileOut Out(Data);
120 Out.Format = clang::clangd::Format;
121 llvm::outs() << Out;
122 return 0;
123 }
Index.h
main
int main(int argc, const char **argv)
Definition: IndexerMain.cpp:85
clang::clangd::IndexFileOut::Format
IndexFileFormat Format
Definition: Serialization.h:56
clang::clangd::IndexFileFormat::YAML
clang::tidy::cppcoreguidelines::toString
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
Definition: SpecialMemberFunctionsCheck.cpp:59
clang::clangd::IndexFileFormat::RIFF
Serialization.h
clang::clangd::IndexFileIn
Definition: Serialization.h:40
clang::clangd::mergeSymbol
Symbol mergeSymbol(const Symbol &L, const Symbol &R)
Definition: Merge.cpp:119
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
Merge.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
SymbolCollector.h
clang::clangd::createStaticIndexingAction
std::unique_ptr< FrontendAction > createStaticIndexingAction(SymbolCollector::Options Opts, std::function< void(SymbolSlab)> SymbolsCallback, std::function< void(RefSlab)> RefsCallback, std::function< void(IncludeGraph)> IncludeGraphCallback)
Definition: IndexAction.cpp:167
clang::clangd::IndexFileOut
Definition: Serialization.h:50
IndexAction.h