-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathCompilation.cpp
More file actions
381 lines (310 loc) · 13.9 KB
/
Compilation.cpp
File metadata and controls
381 lines (310 loc) · 13.9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include "CompilationUnitImpl.h"
#include "Compiler/Command.h"
#include "Compiler/Compilation.h"
#include "Compiler/Diagnostic.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Frontend/MultiplexConsumer.h"
#include "Support/Compression.h"
#include "Support/FileSystem.h"
#include "Support/Logger.h"
namespace clice {
namespace {
/// A wrapper ast consumer, so that we can cancel the ast parse
class ProxyASTConsumer final : public clang::MultiplexConsumer {
public:
ProxyASTConsumer(std::unique_ptr<clang::ASTConsumer> consumer,
clang::CompilerInstance& instance,
std::vector<clang::Decl*>* top_level_decls,
std::shared_ptr<std::atomic_bool> stop) :
clang::MultiplexConsumer(std::move(consumer)), instance(instance),
src_mgr(instance.getSourceManager()), top_level_decls(top_level_decls), stop(stop) {}
void collect_decl(clang::Decl* decl) {
auto location = decl->getLocation();
if(location.isInvalid()) {
return;
}
location = src_mgr.getExpansionLoc(location);
auto fid = src_mgr.getFileID(location);
if(fid == src_mgr.getPreambleFileID() || fid == src_mgr.getMainFileID()) {
top_level_decls->push_back(decl);
}
}
auto HandleTopLevelDecl(clang::DeclGroupRef group) -> bool final {
if(top_level_decls) {
if(group.isDeclGroup()) {
for(auto decl: group) {
collect_decl(decl);
}
} else {
collect_decl(group.getSingleDecl());
}
}
/// TODO: check atomic variable after the parse of each declaration
/// may result in performance issue, benchmark in the future.
if(stop && stop->load()) {
return false;
}
return clang::MultiplexConsumer::HandleTopLevelDecl(group);
}
private:
clang::CompilerInstance& instance;
clang::SourceManager& src_mgr;
/// Non-nullptr if we need collect the top level declarations.
std::vector<clang::Decl*>* top_level_decls;
std::shared_ptr<std::atomic_bool> stop;
};
class ProxyAction final : public clang::WrapperFrontendAction {
public:
ProxyAction(std::unique_ptr<clang::FrontendAction> action,
std::vector<clang::Decl*>* top_level_decls,
std::shared_ptr<std::atomic_bool> stop) :
clang::WrapperFrontendAction(std::move(action)), top_level_decls(top_level_decls),
stop(std::move(stop)) {}
auto CreateASTConsumer(clang::CompilerInstance& instance, llvm::StringRef file)
-> std::unique_ptr<clang::ASTConsumer> final {
return std::make_unique<ProxyASTConsumer>(
WrapperFrontendAction::CreateASTConsumer(instance, file),
instance,
top_level_decls,
std::move(stop));
}
/// Make this public.
using clang::WrapperFrontendAction::EndSourceFile;
private:
std::vector<clang::Decl*>* top_level_decls;
std::shared_ptr<std::atomic_bool> stop;
};
/// create a `clang::CompilerInvocation` for compilation, it set and reset
/// all necessary arguments and flags for clice compilation.
auto create_invocation(CompilationParams& params,
llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine>& diagnostic_engine)
-> std::unique_ptr<clang::CompilerInvocation> {
/// Create clang invocation.
clang::CreateInvocationOptions options = {
.Diags = diagnostic_engine,
.VFS = params.vfs,
/// Avoid replacing -include with -include-pch, also
/// see https://github.com/clangd/clangd/issues/856.
.ProbePrecompiled = false,
};
auto invocation = clang::createInvocation(params.arguments, options);
if(!invocation) {
return nullptr;
}
auto& pp_opts = invocation->getPreprocessorOpts();
assert(!pp_opts.RetainRemappedFileBuffers && "RetainRemappedFileBuffers should be false");
for(auto& [file, buffer]: params.buffers) {
pp_opts.addRemappedFile(file, buffer.release());
}
params.buffers.clear();
auto [pch, bound] = params.pch;
if(pch.ends_with(".lz4")) {
// When a compressed PCH/PCM is found, we decompress it into a memory buffer.
// To allow clang to read this in-memory PCH/PCM as if it were a file on disk,
// we create a virtual file system (VFS). This VFS overlays the real file system.
if(auto buffer = decompressFile(pch)) {
std::string pch_path = pch;
pch_path.resize(pch_path.size() - 4);
auto imfs = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
imfs->addFile(pch_path, 0, std::move(buffer));
auto overlay = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(params.vfs);
overlay->pushOverlay(imfs);
params.vfs = std::move(overlay);
pp_opts.ImplicitPCHInclude = std::move(pch_path);
} else {
log::warn("Failed to decompress PCH '{}', proceeding without it.", pch);
}
} else {
pp_opts.ImplicitPCHInclude = std::move(pch);
}
if(bound != 0) {
pp_opts.PrecompiledPreambleBytes = {bound, false};
}
// We don't want to write comment locations into PCM. They are racy and slow
// to read back. We rely on dynamic index for the comments instead.
pp_opts.WriteCommentListToPCH = false;
auto& header_search_opts = invocation->getHeaderSearchOpts();
for(auto& [name, path]: params.pcms) {
header_search_opts.PrebuiltModuleFiles.try_emplace(name.str(), std::move(path));
}
auto& front_opts = invocation->getFrontendOpts();
front_opts.DisableFree = false;
clang::LangOptions& langOpts = invocation->getLangOpts();
langOpts.CommentOpts.ParseAllComments = true;
langOpts.RetainCommentsFromSystemHeaders = true;
return invocation;
}
/// Do nothing before or after compile state.
constexpr static auto no_hook = [](auto& /*ignore*/) {
};
template <typename Action,
typename BeforeExecute = decltype(no_hook),
typename AfterExecute = decltype(no_hook)>
CompilationResult run_clang(CompilationParams& params,
const BeforeExecute& before_execute = no_hook,
const AfterExecute& after_execute = no_hook) {
auto diagnostics =
params.diagnostics ? params.diagnostics : std::make_shared<std::vector<Diagnostic>>();
auto diagnostic_engine =
clang::CompilerInstance::createDiagnostics(*params.vfs,
new clang::DiagnosticOptions(),
Diagnostic::create(diagnostics));
auto invocation = create_invocation(params, diagnostic_engine);
if(!invocation) {
return std::unexpected("Fail to create compilation invocation!");
}
auto instance = std::make_unique<clang::CompilerInstance>();
instance->setInvocation(std::move(invocation));
instance->setDiagnostics(diagnostic_engine.get());
if(auto remapping = clang::createVFSFromCompilerInvocation(instance->getInvocation(),
instance->getDiagnostics(),
params.vfs)) {
instance->createFileManager(std::move(remapping));
}
if(!instance->createTarget()) {
return std::unexpected("Fail to create target!");
}
/// Adjust the compiler instance, for example, set preamble or modules.
before_execute(*instance);
/// Frontend information ...
std::vector<clang::Decl*> top_level_decls;
llvm::DenseMap<clang::FileID, Directive> directives;
std::optional<clang::syntax::TokenCollector> token_collector;
auto action = std::make_unique<ProxyAction>(
std::make_unique<Action>(),
/// We only collect top level declarations for parse main file.
params.kind == CompilationUnit::Content ? &top_level_decls : nullptr,
params.stop);
if(!action->BeginSourceFile(*instance, instance->getFrontendOpts().Inputs[0])) {
return std::unexpected("Fail to begin source file");
}
auto& pp = instance->getPreprocessor();
/// FIXME: clang-tidy, include-fixer, etc?
/// `BeginSourceFile` may create new preprocessor, so all operations related to preprocessor
/// should be done after `BeginSourceFile`.
Directive::attach(pp, directives);
/// It is not necessary to collect tokens if we are running code completion.
/// And in fact will cause assertion failure.
if(!instance->hasCodeCompletionConsumer()) {
token_collector.emplace(pp);
}
if(auto error = action->Execute()) {
return std::unexpected(std::format("Failed to execute action, because {} ", error));
}
/// If the output file is not empty, it represents that we are
/// generating a PCH or PCM. If error occurs, the AST must be
/// invalid to some extent, serialization of such AST may result
/// in crash frequently. So forbidden it here and return as error.
if(!instance->getFrontendOpts().OutputFile.empty() &&
instance->getDiagnostics().hasErrorOccurred()) {
action->EndSourceFile();
return std::unexpected("Fail to build PCH or PCM, error occurs in compilation.");
}
/// Check whether the compilation is canceled, if so we think
/// it is an error.
if(params.stop && params.stop->load()) {
action->EndSourceFile();
return std::unexpected("Compilation is canceled.");
}
std::optional<clang::syntax::TokenBuffer> token_buffer;
if(token_collector) {
token_buffer = std::move(*token_collector).consume();
}
/// FIXME: getDependencies currently return ArrayRef<std::string>, which actually results in
/// extra copy. It would be great to avoid this copy.
std::optional<TemplateResolver> resolver;
if(instance->hasSema()) {
resolver.emplace(instance->getSema());
}
auto impl = new CompilationUnit::Impl{
.interested = pp.getSourceManager().getMainFileID(),
.src_mgr = instance->getSourceManager(),
.action = std::move(action),
.instance = std::move(instance),
.m_resolver = std::move(resolver),
.buffer = std::move(token_buffer),
.m_directives = std::move(directives),
.pathCache = llvm::DenseMap<clang::FileID, llvm::StringRef>(),
.symbolHashCache = llvm::DenseMap<const void*, std::uint64_t>(),
.diagnostics = diagnostics,
.top_level_decls = std::move(top_level_decls),
};
CompilationUnit unit(params.kind, impl);
after_execute(unit);
return unit;
}
} // namespace
CompilationResult preprocess(CompilationParams& params) {
return run_clang<clang::PreprocessOnlyAction>(params);
}
CompilationResult compile(CompilationParams& params) {
return run_clang<clang::SyntaxOnlyAction>(params);
}
CompilationResult compile(CompilationParams& params, PCHInfo& out) {
assert(!params.output_file.empty() && "PCH file path cannot be empty");
/// Record the begin time of PCH building.
auto now = std::chrono::system_clock::now().time_since_epoch();
out.mtime = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
return run_clang<clang::GeneratePCHAction>(
params,
[&](clang::CompilerInstance& instance) {
/// Set options to generate PCH.
instance.getFrontendOpts().OutputFile = params.output_file.str();
instance.getFrontendOpts().ProgramAction = clang::frontend::GeneratePCH;
instance.getPreprocessorOpts().GeneratePreamble = true;
// We don't want to write comment locations into PCH. They are racy and slow
// to read back. We rely on dynamic index for the comments instead.
instance.getPreprocessorOpts().WriteCommentListToPCH = false;
instance.getLangOpts().CompilingPCH = true;
},
[&](CompilationUnit& unit) {
out.path = params.output_file.str();
out.preamble = unit.interested_content();
out.deps = unit.deps();
out.arguments = params.arguments;
});
}
CompilationResult compile(CompilationParams& params, PCMInfo& out) {
assert(!params.output_file.empty() && "PCM file path cannot be empty");
return run_clang<clang::GenerateReducedModuleInterfaceAction>(
params,
[&](clang::CompilerInstance& instance) {
/// Set options to generate PCH.
instance.getFrontendOpts().OutputFile = params.output_file.str();
instance.getFrontendOpts().ProgramAction =
clang::frontend::GenerateReducedModuleInterface;
out.srcPath = instance.getFrontendOpts().Inputs[0].getFile();
},
[&](CompilationUnit& unit) {
out.path = params.output_file.str();
for(auto& [name, path]: params.pcms) {
out.mods.emplace_back(name);
}
});
}
CompilationResult complete(CompilationParams& params, clang::CodeCompleteConsumer* consumer) {
auto& [file, offset] = params.completion;
/// The location of clang is 1-1 based.
std::uint32_t line = 1;
std::uint32_t column = 1;
/// FIXME:
assert(params.buffers.size() == 1);
llvm::StringRef content = params.buffers.begin()->second->getBuffer();
for(auto c: content.substr(0, offset)) {
if(c == '\n') {
line += 1;
column = 1;
continue;
}
column += 1;
}
return run_clang<clang::SyntaxOnlyAction>(params, [&](clang::CompilerInstance& instance) {
/// Set options to run code completion.
instance.getFrontendOpts().CodeCompletionAt.FileName = std::move(file);
instance.getFrontendOpts().CodeCompletionAt.Line = line;
instance.getFrontendOpts().CodeCompletionAt.Column = column;
instance.setCodeCompletionConsumer(consumer);
});
}
} // namespace clice