-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathIndexerMain.cpp
181 lines (160 loc) · 6.22 KB
/
IndexerMain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//===--- IndexerMain.cpp -----------------------------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// clangd-indexer is a tool to gather index data (symbols, xrefs) from source.
//
//===----------------------------------------------------------------------===//
#include "CompileCommands.h"
#include "Compiler.h"
#include "index/IndexAction.h"
#include "index/Merge.h"
#include "index/Ref.h"
#include "index/Serialization.h"
#include "index/Symbol.h"
#include "index/SymbolCollector.h"
#include "support/Logger.h"
#include "clang/Tooling/ArgumentsAdjusters.h"
#include "clang/Tooling/Execution.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Signals.h"
#include <utility>
namespace clang {
namespace clangd {
namespace {
static llvm::cl::opt<IndexFileFormat>
Format("format", llvm::cl::desc("Format of the index to be written"),
llvm::cl::values(clEnumValN(IndexFileFormat::YAML, "yaml",
"human-readable YAML format"),
clEnumValN(IndexFileFormat::RIFF, "binary",
"binary RIFF format")),
llvm::cl::init(IndexFileFormat::RIFF));
static llvm::cl::list<std::string> QueryDriverGlobs{
"query-driver",
llvm::cl::desc(
"Comma separated list of globs for white-listing gcc-compatible "
"drivers that are safe to execute. Drivers matching any of these globs "
"will be used to extract system includes. e.g. "
"/usr/bin/**/clang-*,/path/to/repo/**/g++-*"),
llvm::cl::CommaSeparated,
};
class IndexActionFactory : public tooling::FrontendActionFactory {
public:
IndexActionFactory(IndexFileIn &Result) : Result(Result) {}
std::unique_ptr<FrontendAction> create() override {
SymbolCollector::Options Opts;
Opts.CountReferences = true;
Opts.FileFilter = [&](const SourceManager &SM, FileID FID) {
const auto F = SM.getFileEntryRefForID(FID);
if (!F)
return false; // Skip invalid files.
auto AbsPath = getCanonicalPath(*F, SM.getFileManager());
if (!AbsPath)
return false; // Skip files without absolute path.
std::lock_guard<std::mutex> Lock(FilesMu);
return Files.insert(*AbsPath).second; // Skip already processed files.
};
return createStaticIndexingAction(
Opts,
[&](SymbolSlab S) {
// Merge as we go.
std::lock_guard<std::mutex> Lock(SymbolsMu);
for (const auto &Sym : S) {
if (const auto *Existing = Symbols.find(Sym.ID))
Symbols.insert(mergeSymbol(*Existing, Sym));
else
Symbols.insert(Sym);
}
},
[&](RefSlab S) {
std::lock_guard<std::mutex> Lock(RefsMu);
for (const auto &Sym : S) {
// Deduplication happens during insertion.
for (const auto &Ref : Sym.second)
Refs.insert(Sym.first, Ref);
}
},
[&](RelationSlab S) {
std::lock_guard<std::mutex> Lock(RelsMu);
for (const auto &R : S) {
Relations.insert(R);
}
},
/*IncludeGraphCallback=*/nullptr);
}
bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
FileManager *Files,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
DiagnosticConsumer *DiagConsumer) override {
disableUnsupportedOptions(*Invocation);
return tooling::FrontendActionFactory::runInvocation(
std::move(Invocation), Files, std::move(PCHContainerOps), DiagConsumer);
}
// Awkward: we write the result in the destructor, because the executor
// takes ownership so it's the easiest way to get our data back out.
~IndexActionFactory() {
Result.Symbols = std::move(Symbols).build();
Result.Refs = std::move(Refs).build();
Result.Relations = std::move(Relations).build();
}
private:
IndexFileIn &Result;
std::mutex FilesMu;
llvm::StringSet<> Files;
std::mutex SymbolsMu;
SymbolSlab::Builder Symbols;
std::mutex RefsMu;
RefSlab::Builder Refs;
std::mutex RelsMu;
RelationSlab::Builder Relations;
};
} // namespace
} // namespace clangd
} // namespace clang
int main(int argc, const char **argv) {
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
const char *Overview = R"(
Creates an index of symbol information etc in a whole project.
Example usage for a project using CMake compile commands:
$ clangd-indexer --executor=all-TUs compile_commands.json > clangd.dex
Example usage for file sequence index without flags:
$ clangd-indexer File1.cpp File2.cpp ... FileN.cpp > clangd.dex
Note: only symbols from header files will be indexed.
)";
auto Executor = clang::tooling::createExecutorFromCommandLineArgs(
argc, argv, llvm::cl::getGeneralCategory(), Overview);
if (!Executor) {
llvm::errs() << llvm::toString(Executor.takeError()) << "\n";
return 1;
}
// Collect symbols found in each translation unit, merging as we go.
clang::clangd::IndexFileIn Data;
auto Mangler = std::make_shared<clang::clangd::CommandMangler>(
clang::clangd::CommandMangler::detect());
Mangler->SystemIncludeExtractor = clang::clangd::getSystemIncludeExtractor(
static_cast<llvm::ArrayRef<std::string>>(
clang::clangd::QueryDriverGlobs));
auto Err = Executor->get()->execute(
std::make_unique<clang::clangd::IndexActionFactory>(Data),
clang::tooling::ArgumentsAdjuster(
[Mangler = std::move(Mangler)](const std::vector<std::string> &Args,
llvm::StringRef File) {
clang::tooling::CompileCommand Cmd;
Cmd.CommandLine = Args;
Mangler->operator()(Cmd, File);
return Cmd.CommandLine;
}));
if (Err) {
clang::clangd::elog("{0}", std::move(Err));
}
// Emit collected data.
clang::clangd::IndexFileOut Out(Data);
Out.Format = clang::clangd::Format;
llvm::outs() << Out;
return 0;
}