blob: 892db31d98ad128cf2ef9c5a8baa12472ec385e4 [file] [log] [blame]
Ilya Biryukov38d79772017-05-16 09:38:59 +00001//===--- ClangdUnit.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#include "ClangdUnit.h"
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000011
Ilya Biryukov38d79772017-05-16 09:38:59 +000012#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Frontend/CompilerInvocation.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000014#include "clang/Frontend/FrontendActions.h"
Ilya Biryukov0f62ed22017-05-26 12:26:51 +000015#include "clang/Frontend/Utils.h"
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000016#include "clang/Index/IndexDataConsumer.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000017#include "clang/Index/IndexingAction.h"
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000018#include "clang/Lex/Lexer.h"
19#include "clang/Lex/MacroInfo.h"
20#include "clang/Lex/Preprocessor.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000021#include "clang/Lex/PreprocessorOptions.h"
22#include "clang/Sema/Sema.h"
23#include "clang/Serialization/ASTWriter.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000024#include "clang/Tooling/CompilationDatabase.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000025#include "llvm/ADT/ArrayRef.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/Support/CrashRecoveryContext.h"
Krasimir Georgieva1de3c92017-06-15 09:11:57 +000028#include "llvm/Support/Format.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000029
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000030#include <algorithm>
Ilya Biryukov02d58702017-08-01 15:51:38 +000031#include <chrono>
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000032
Ilya Biryukov38d79772017-05-16 09:38:59 +000033using namespace clang::clangd;
34using namespace clang;
35
Ilya Biryukov04db3682017-07-21 13:29:29 +000036namespace {
37
38class DeclTrackingASTConsumer : public ASTConsumer {
39public:
40 DeclTrackingASTConsumer(std::vector<const Decl *> &TopLevelDecls)
41 : TopLevelDecls(TopLevelDecls) {}
42
43 bool HandleTopLevelDecl(DeclGroupRef DG) override {
44 for (const Decl *D : DG) {
45 // ObjCMethodDecl are not actually top-level decls.
46 if (isa<ObjCMethodDecl>(D))
47 continue;
48
49 TopLevelDecls.push_back(D);
50 }
51 return true;
52 }
53
54private:
55 std::vector<const Decl *> &TopLevelDecls;
56};
57
58class ClangdFrontendAction : public SyntaxOnlyAction {
59public:
60 std::vector<const Decl *> takeTopLevelDecls() {
61 return std::move(TopLevelDecls);
62 }
63
64protected:
65 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
66 StringRef InFile) override {
67 return llvm::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls);
68 }
69
70private:
71 std::vector<const Decl *> TopLevelDecls;
72};
73
Ilya Biryukov02d58702017-08-01 15:51:38 +000074class CppFilePreambleCallbacks : public PreambleCallbacks {
Ilya Biryukov04db3682017-07-21 13:29:29 +000075public:
76 std::vector<serialization::DeclID> takeTopLevelDeclIDs() {
77 return std::move(TopLevelDeclIDs);
78 }
79
80 void AfterPCHEmitted(ASTWriter &Writer) override {
81 TopLevelDeclIDs.reserve(TopLevelDecls.size());
82 for (Decl *D : TopLevelDecls) {
83 // Invalid top-level decls may not have been serialized.
84 if (D->isInvalidDecl())
85 continue;
86 TopLevelDeclIDs.push_back(Writer.getDeclID(D));
87 }
88 }
89
90 void HandleTopLevelDecl(DeclGroupRef DG) override {
91 for (Decl *D : DG) {
92 if (isa<ObjCMethodDecl>(D))
93 continue;
94 TopLevelDecls.push_back(D);
95 }
96 }
97
98private:
99 std::vector<Decl *> TopLevelDecls;
100 std::vector<serialization::DeclID> TopLevelDeclIDs;
101};
102
103/// Convert from clang diagnostic level to LSP severity.
104static int getSeverity(DiagnosticsEngine::Level L) {
105 switch (L) {
106 case DiagnosticsEngine::Remark:
107 return 4;
108 case DiagnosticsEngine::Note:
109 return 3;
110 case DiagnosticsEngine::Warning:
111 return 2;
112 case DiagnosticsEngine::Fatal:
113 case DiagnosticsEngine::Error:
114 return 1;
115 case DiagnosticsEngine::Ignored:
116 return 0;
117 }
118 llvm_unreachable("Unknown diagnostic level!");
119}
120
121llvm::Optional<DiagWithFixIts> toClangdDiag(StoredDiagnostic D) {
122 auto Location = D.getLocation();
123 if (!Location.isValid() || !Location.getManager().isInMainFile(Location))
124 return llvm::None;
125
126 Position P;
127 P.line = Location.getSpellingLineNumber() - 1;
128 P.character = Location.getSpellingColumnNumber();
129 Range R = {P, P};
130 clangd::Diagnostic Diag = {R, getSeverity(D.getLevel()), D.getMessage()};
131
132 llvm::SmallVector<tooling::Replacement, 1> FixItsForDiagnostic;
133 for (const FixItHint &Fix : D.getFixIts()) {
134 FixItsForDiagnostic.push_back(clang::tooling::Replacement(
135 Location.getManager(), Fix.RemoveRange, Fix.CodeToInsert));
136 }
137 return DiagWithFixIts{Diag, std::move(FixItsForDiagnostic)};
138}
139
140class StoreDiagsConsumer : public DiagnosticConsumer {
141public:
142 StoreDiagsConsumer(std::vector<DiagWithFixIts> &Output) : Output(Output) {}
143
144 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
145 const clang::Diagnostic &Info) override {
146 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
147
148 if (auto convertedDiag = toClangdDiag(StoredDiagnostic(DiagLevel, Info)))
149 Output.push_back(std::move(*convertedDiag));
150 }
151
152private:
153 std::vector<DiagWithFixIts> &Output;
154};
155
156class EmptyDiagsConsumer : public DiagnosticConsumer {
157public:
158 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
159 const clang::Diagnostic &Info) override {}
160};
161
162std::unique_ptr<CompilerInvocation>
163createCompilerInvocation(ArrayRef<const char *> ArgList,
164 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
165 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
166 auto CI = createInvocationFromCommandLine(ArgList, std::move(Diags),
167 std::move(VFS));
168 // We rely on CompilerInstance to manage the resource (i.e. free them on
169 // EndSourceFile), but that won't happen if DisableFree is set to true.
170 // Since createInvocationFromCommandLine sets it to true, we have to override
171 // it.
172 CI->getFrontendOpts().DisableFree = false;
173 return CI;
174}
175
176/// Creates a CompilerInstance from \p CI, with main buffer overriden to \p
177/// Buffer and arguments to read the PCH from \p Preamble, if \p Preamble is not
178/// null. Note that vfs::FileSystem inside returned instance may differ from \p
179/// VFS if additional file remapping were set in command-line arguments.
180/// On some errors, returns null. When non-null value is returned, it's expected
181/// to be consumed by the FrontendAction as it will have a pointer to the \p
182/// Buffer that will only be deleted if BeginSourceFile is called.
183std::unique_ptr<CompilerInstance>
184prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
185 const PrecompiledPreamble *Preamble,
186 std::unique_ptr<llvm::MemoryBuffer> Buffer,
187 std::shared_ptr<PCHContainerOperations> PCHs,
188 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
189 DiagnosticConsumer &DiagsClient) {
190 assert(VFS && "VFS is null");
191 assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
192 "Setting RetainRemappedFileBuffers to true will cause a memory leak "
193 "of ContentsBuffer");
194
195 // NOTE: we use Buffer.get() when adding remapped files, so we have to make
196 // sure it will be released if no error is emitted.
197 if (Preamble) {
198 Preamble->AddImplicitPreamble(*CI, Buffer.get());
199 } else {
200 CI->getPreprocessorOpts().addRemappedFile(
201 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
202 }
203
204 auto Clang = llvm::make_unique<CompilerInstance>(PCHs);
205 Clang->setInvocation(std::move(CI));
206 Clang->createDiagnostics(&DiagsClient, false);
207
208 if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
209 Clang->getInvocation(), Clang->getDiagnostics(), VFS))
210 VFS = VFSWithRemapping;
211 Clang->setVirtualFileSystem(VFS);
212
213 Clang->setTarget(TargetInfo::CreateTargetInfo(
214 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
215 if (!Clang->hasTarget())
216 return nullptr;
217
218 // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
219 // release it.
220 Buffer.release();
221 return Clang;
222}
223
Ilya Biryukov02d58702017-08-01 15:51:38 +0000224template <class T> bool futureIsReady(std::shared_future<T> const &Future) {
225 return Future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
226}
227
Ilya Biryukov04db3682017-07-21 13:29:29 +0000228} // namespace
229
Ilya Biryukov38d79772017-05-16 09:38:59 +0000230namespace {
231
232CompletionItemKind getKind(CXCursorKind K) {
233 switch (K) {
234 case CXCursor_MacroInstantiation:
235 case CXCursor_MacroDefinition:
236 return CompletionItemKind::Text;
237 case CXCursor_CXXMethod:
238 return CompletionItemKind::Method;
239 case CXCursor_FunctionDecl:
240 case CXCursor_FunctionTemplate:
241 return CompletionItemKind::Function;
242 case CXCursor_Constructor:
243 case CXCursor_Destructor:
244 return CompletionItemKind::Constructor;
245 case CXCursor_FieldDecl:
246 return CompletionItemKind::Field;
247 case CXCursor_VarDecl:
248 case CXCursor_ParmDecl:
249 return CompletionItemKind::Variable;
250 case CXCursor_ClassDecl:
251 case CXCursor_StructDecl:
252 case CXCursor_UnionDecl:
253 case CXCursor_ClassTemplate:
254 case CXCursor_ClassTemplatePartialSpecialization:
255 return CompletionItemKind::Class;
256 case CXCursor_Namespace:
257 case CXCursor_NamespaceAlias:
258 case CXCursor_NamespaceRef:
259 return CompletionItemKind::Module;
260 case CXCursor_EnumConstantDecl:
261 return CompletionItemKind::Value;
262 case CXCursor_EnumDecl:
263 return CompletionItemKind::Enum;
264 case CXCursor_TypeAliasDecl:
265 case CXCursor_TypeAliasTemplateDecl:
266 case CXCursor_TypedefDecl:
267 case CXCursor_MemberRef:
268 case CXCursor_TypeRef:
269 return CompletionItemKind::Reference;
270 default:
271 return CompletionItemKind::Missing;
272 }
273}
274
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000275std::string escapeSnippet(const llvm::StringRef Text) {
276 std::string Result;
277 Result.reserve(Text.size()); // Assume '$', '}' and '\\' are rare.
278 for (const auto Character : Text) {
279 if (Character == '$' || Character == '}' || Character == '\\')
280 Result.push_back('\\');
281 Result.push_back(Character);
282 }
283 return Result;
284}
285
Ilya Biryukov38d79772017-05-16 09:38:59 +0000286class CompletionItemsCollector : public CodeCompleteConsumer {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000287
288public:
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000289 CompletionItemsCollector(const CodeCompleteOptions &CodeCompleteOpts,
290 std::vector<CompletionItem> &Items)
Ilya Biryukov38d79772017-05-16 09:38:59 +0000291 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
292 Items(Items),
293 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
294 CCTUInfo(Allocator) {}
295
296 void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
297 CodeCompletionResult *Results,
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000298 unsigned NumResults) override final {
299 Items.reserve(NumResults);
300 for (unsigned I = 0; I < NumResults; ++I) {
301 auto &Result = Results[I];
302 const auto *CCS = Result.CreateCodeCompletionString(
Ilya Biryukov38d79772017-05-16 09:38:59 +0000303 S, Context, *Allocator, CCTUInfo,
304 CodeCompleteOpts.IncludeBriefComments);
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000305 assert(CCS && "Expected the CodeCompletionString to be non-null");
306 Items.push_back(ProcessCodeCompleteResult(Result, *CCS));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000307 }
308 }
309
310 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
311
312 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000313
314private:
315 CompletionItem
316 ProcessCodeCompleteResult(const CodeCompletionResult &Result,
317 const CodeCompletionString &CCS) const {
318
319 // Adjust this to InsertTextFormat::Snippet iff we encounter a
320 // CK_Placeholder chunk in SnippetCompletionItemsCollector.
321 CompletionItem Item;
322 Item.insertTextFormat = InsertTextFormat::PlainText;
323
324 FillDocumentation(CCS, Item);
325
326 // Fill in the label, detail, insertText and filterText fields of the
327 // CompletionItem.
328 ProcessChunks(CCS, Item);
329
330 // Fill in the kind field of the CompletionItem.
331 Item.kind = getKind(Result.CursorKind);
332
333 FillSortText(CCS, Item);
334
335 return Item;
336 }
337
338 virtual void ProcessChunks(const CodeCompletionString &CCS,
339 CompletionItem &Item) const = 0;
340
341 void FillDocumentation(const CodeCompletionString &CCS,
342 CompletionItem &Item) const {
343 // Things like __attribute__((nonnull(1,3))) and [[noreturn]]. Present this
344 // information in the documentation field.
345 const unsigned AnnotationCount = CCS.getAnnotationCount();
346 if (AnnotationCount > 0) {
347 Item.documentation += "Annotation";
348 if (AnnotationCount == 1) {
349 Item.documentation += ": ";
350 } else /* AnnotationCount > 1 */ {
351 Item.documentation += "s: ";
352 }
353 for (unsigned I = 0; I < AnnotationCount; ++I) {
354 Item.documentation += CCS.getAnnotation(I);
355 Item.documentation.push_back(I == AnnotationCount - 1 ? '\n' : ' ');
356 }
357 }
358
359 // Add brief documentation (if there is any).
360 if (CCS.getBriefComment() != nullptr) {
361 if (!Item.documentation.empty()) {
362 // This means we previously added annotations. Add an extra newline
363 // character to make the annotations stand out.
364 Item.documentation.push_back('\n');
365 }
366 Item.documentation += CCS.getBriefComment();
367 }
368 }
369
370 void FillSortText(const CodeCompletionString &CCS,
371 CompletionItem &Item) const {
372 // Fill in the sortText of the CompletionItem.
373 assert(CCS.getPriority() < 99999 && "Expecting code completion result "
374 "priority to have at most 5-digits");
375 llvm::raw_string_ostream(Item.sortText)
376 << llvm::format("%05d%s", CCS.getPriority(), Item.filterText.c_str());
377 }
378
379 std::vector<CompletionItem> &Items;
380 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
381 CodeCompletionTUInfo CCTUInfo;
382
383}; // CompletionItemsCollector
384
385class PlainTextCompletionItemsCollector final
386 : public CompletionItemsCollector {
387
388public:
389 PlainTextCompletionItemsCollector(const CodeCompleteOptions &CodeCompleteOpts,
390 std::vector<CompletionItem> &Items)
391 : CompletionItemsCollector(CodeCompleteOpts, Items) {}
392
393private:
394 void ProcessChunks(const CodeCompletionString &CCS,
395 CompletionItem &Item) const override {
396 for (const auto &Chunk : CCS) {
397 switch (Chunk.Kind) {
398 case CodeCompletionString::CK_TypedText:
399 // There's always exactly one CK_TypedText chunk.
400 Item.insertText = Item.filterText = Chunk.Text;
401 Item.label += Chunk.Text;
402 break;
403 case CodeCompletionString::CK_ResultType:
404 assert(Item.detail.empty() && "Unexpected extraneous CK_ResultType");
405 Item.detail = Chunk.Text;
406 break;
407 case CodeCompletionString::CK_Optional:
408 break;
409 default:
410 Item.label += Chunk.Text;
411 break;
412 }
413 }
414 }
415}; // PlainTextCompletionItemsCollector
416
417class SnippetCompletionItemsCollector final : public CompletionItemsCollector {
418
419public:
420 SnippetCompletionItemsCollector(const CodeCompleteOptions &CodeCompleteOpts,
421 std::vector<CompletionItem> &Items)
422 : CompletionItemsCollector(CodeCompleteOpts, Items) {}
423
424private:
425 void ProcessChunks(const CodeCompletionString &CCS,
426 CompletionItem &Item) const override {
427 unsigned ArgCount = 0;
428 for (const auto &Chunk : CCS) {
429 switch (Chunk.Kind) {
430 case CodeCompletionString::CK_TypedText:
431 // The piece of text that the user is expected to type to match
432 // the code-completion string, typically a keyword or the name of
433 // a declarator or macro.
434 Item.filterText = Chunk.Text;
435 // Note intentional fallthrough here.
436 case CodeCompletionString::CK_Text:
437 // A piece of text that should be placed in the buffer,
438 // e.g., parentheses or a comma in a function call.
439 Item.label += Chunk.Text;
440 Item.insertText += Chunk.Text;
441 break;
442 case CodeCompletionString::CK_Optional:
443 // A code completion string that is entirely optional.
444 // For example, an optional code completion string that
445 // describes the default arguments in a function call.
446
447 // FIXME: Maybe add an option to allow presenting the optional chunks?
448 break;
449 case CodeCompletionString::CK_Placeholder:
450 // A string that acts as a placeholder for, e.g., a function call
451 // argument.
452 ++ArgCount;
453 Item.insertText += "${" + std::to_string(ArgCount) + ':' +
454 escapeSnippet(Chunk.Text) + '}';
455 Item.label += Chunk.Text;
456 Item.insertTextFormat = InsertTextFormat::Snippet;
457 break;
458 case CodeCompletionString::CK_Informative:
459 // A piece of text that describes something about the result
460 // but should not be inserted into the buffer.
461 // For example, the word "const" for a const method, or the name of
462 // the base class for methods that are part of the base class.
463 Item.label += Chunk.Text;
464 // Don't put the informative chunks in the insertText.
465 break;
466 case CodeCompletionString::CK_ResultType:
467 // A piece of text that describes the type of an entity or,
468 // for functions and methods, the return type.
469 assert(Item.detail.empty() && "Unexpected extraneous CK_ResultType");
470 Item.detail = Chunk.Text;
471 break;
472 case CodeCompletionString::CK_CurrentParameter:
473 // A piece of text that describes the parameter that corresponds to
474 // the code-completion location within a function call, message send,
475 // macro invocation, etc.
476 //
477 // This should never be present while collecting completion items,
478 // only while collecting overload candidates.
479 llvm_unreachable("Unexpected CK_CurrentParameter while collecting "
480 "CompletionItems");
481 break;
482 case CodeCompletionString::CK_LeftParen:
483 // A left parenthesis ('(').
484 case CodeCompletionString::CK_RightParen:
485 // A right parenthesis (')').
486 case CodeCompletionString::CK_LeftBracket:
487 // A left bracket ('[').
488 case CodeCompletionString::CK_RightBracket:
489 // A right bracket (']').
490 case CodeCompletionString::CK_LeftBrace:
491 // A left brace ('{').
492 case CodeCompletionString::CK_RightBrace:
493 // A right brace ('}').
494 case CodeCompletionString::CK_LeftAngle:
495 // A left angle bracket ('<').
496 case CodeCompletionString::CK_RightAngle:
497 // A right angle bracket ('>').
498 case CodeCompletionString::CK_Comma:
499 // A comma separator (',').
500 case CodeCompletionString::CK_Colon:
501 // A colon (':').
502 case CodeCompletionString::CK_SemiColon:
503 // A semicolon (';').
504 case CodeCompletionString::CK_Equal:
505 // An '=' sign.
506 case CodeCompletionString::CK_HorizontalSpace:
507 // Horizontal whitespace (' ').
508 Item.insertText += Chunk.Text;
509 Item.label += Chunk.Text;
510 break;
511 case CodeCompletionString::CK_VerticalSpace:
512 // Vertical whitespace ('\n' or '\r\n', depending on the
513 // platform).
514 Item.insertText += Chunk.Text;
515 // Don't even add a space to the label.
516 break;
517 }
518 }
519 }
520}; // SnippetCompletionItemsCollector
Ilya Biryukov38d79772017-05-16 09:38:59 +0000521} // namespace
522
Ilya Biryukov0f62ed22017-05-26 12:26:51 +0000523std::vector<CompletionItem>
Ilya Biryukov02d58702017-08-01 15:51:38 +0000524clangd::codeComplete(PathRef FileName, tooling::CompileCommand Command,
525 PrecompiledPreamble const *Preamble, StringRef Contents,
526 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000527 std::shared_ptr<PCHContainerOperations> PCHs,
528 bool SnippetCompletions) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000529 std::vector<const char *> ArgStrs;
530 for (const auto &S : Command.CommandLine)
531 ArgStrs.push_back(S.c_str());
532
Krasimir Georgieve4130d52017-07-25 11:37:43 +0000533 VFS->setCurrentWorkingDirectory(Command.Directory);
534
Ilya Biryukov04db3682017-07-21 13:29:29 +0000535 std::unique_ptr<CompilerInvocation> CI;
536 EmptyDiagsConsumer DummyDiagsConsumer;
537 {
538 IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
539 CompilerInstance::createDiagnostics(new DiagnosticOptions,
540 &DummyDiagsConsumer, false);
541 CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
542 }
543 assert(CI && "Couldn't create CompilerInvocation");
544
545 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
546 llvm::MemoryBuffer::getMemBufferCopy(Contents, FileName);
547
548 // Attempt to reuse the PCH from precompiled preamble, if it was built.
Ilya Biryukov04db3682017-07-21 13:29:29 +0000549 if (Preamble) {
550 auto Bounds =
551 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
Ilya Biryukov02d58702017-08-01 15:51:38 +0000552 if (!Preamble->CanReuse(*CI, ContentsBuffer.get(), Bounds, VFS.get()))
553 Preamble = nullptr;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000554 }
555
Ilya Biryukov02d58702017-08-01 15:51:38 +0000556 auto Clang = prepareCompilerInstance(std::move(CI), Preamble,
Ilya Biryukov04db3682017-07-21 13:29:29 +0000557 std::move(ContentsBuffer), PCHs, VFS,
558 DummyDiagsConsumer);
559 auto &DiagOpts = Clang->getDiagnosticOpts();
560 DiagOpts.IgnoreWarnings = true;
561
562 auto &FrontendOpts = Clang->getFrontendOpts();
563 FrontendOpts.SkipFunctionBodies = true;
564
565 FrontendOpts.CodeCompleteOpts.IncludeGlobals = true;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000566 FrontendOpts.CodeCompleteOpts.IncludeMacros = true;
567 FrontendOpts.CodeCompleteOpts.IncludeBriefComments = true;
568
569 FrontendOpts.CodeCompletionAt.FileName = FileName;
570 FrontendOpts.CodeCompletionAt.Line = Pos.line + 1;
571 FrontendOpts.CodeCompletionAt.Column = Pos.character + 1;
572
Ilya Biryukov38d79772017-05-16 09:38:59 +0000573 std::vector<CompletionItem> Items;
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000574 if (SnippetCompletions) {
575 FrontendOpts.CodeCompleteOpts.IncludeCodePatterns = true;
576 Clang->setCodeCompletionConsumer(new SnippetCompletionItemsCollector(
577 FrontendOpts.CodeCompleteOpts, Items));
578 } else {
579 FrontendOpts.CodeCompleteOpts.IncludeCodePatterns = false;
580 Clang->setCodeCompletionConsumer(new PlainTextCompletionItemsCollector(
581 FrontendOpts.CodeCompleteOpts, Items));
582 }
Ilya Biryukov38d79772017-05-16 09:38:59 +0000583
Ilya Biryukov04db3682017-07-21 13:29:29 +0000584 SyntaxOnlyAction Action;
585 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
586 // FIXME(ibiryukov): log errors
587 return Items;
588 }
589 if (!Action.Execute()) {
590 // FIXME(ibiryukov): log errors
591 }
592 Action.EndSourceFile();
Ilya Biryukov38d79772017-05-16 09:38:59 +0000593
Ilya Biryukov38d79772017-05-16 09:38:59 +0000594 return Items;
595}
596
Ilya Biryukov02d58702017-08-01 15:51:38 +0000597void clangd::dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) {
598 AST.getASTContext().getTranslationUnitDecl()->dump(OS, true);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000599}
Ilya Biryukovf01af682017-05-23 13:42:59 +0000600
Ilya Biryukov02d58702017-08-01 15:51:38 +0000601llvm::Optional<ParsedAST>
602ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI,
603 const PrecompiledPreamble *Preamble,
604 ArrayRef<serialization::DeclID> PreambleDeclIDs,
605 std::unique_ptr<llvm::MemoryBuffer> Buffer,
606 std::shared_ptr<PCHContainerOperations> PCHs,
607 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000608
609 std::vector<DiagWithFixIts> ASTDiags;
610 StoreDiagsConsumer UnitDiagsConsumer(/*ref*/ ASTDiags);
611
612 auto Clang =
613 prepareCompilerInstance(std::move(CI), Preamble, std::move(Buffer), PCHs,
614 VFS, /*ref*/ UnitDiagsConsumer);
615
616 // Recover resources if we crash before exiting this method.
617 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
618 Clang.get());
619
620 auto Action = llvm::make_unique<ClangdFrontendAction>();
621 if (!Action->BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
622 // FIXME(ibiryukov): log error
623 return llvm::None;
624 }
625 if (!Action->Execute()) {
626 // FIXME(ibiryukov): log error
627 }
628
629 // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
630 // has a longer lifetime.
631 Clang->getDiagnostics().setClient(new EmptyDiagsConsumer);
632
633 std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls();
634 std::vector<serialization::DeclID> PendingDecls;
635 if (Preamble) {
636 PendingDecls.reserve(PreambleDeclIDs.size());
637 PendingDecls.insert(PendingDecls.begin(), PreambleDeclIDs.begin(),
638 PreambleDeclIDs.end());
639 }
640
641 return ParsedAST(std::move(Clang), std::move(Action), std::move(ParsedDecls),
642 std::move(PendingDecls), std::move(ASTDiags));
643}
644
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000645namespace {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000646
647SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
648 const FileEntry *FE,
649 unsigned Offset) {
650 SourceLocation FileLoc = Mgr.translateFileLineCol(FE, 1, 1);
651 return Mgr.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
652}
653
654SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
655 const FileEntry *FE, Position Pos) {
656 SourceLocation InputLoc =
657 Mgr.translateFileLineCol(FE, Pos.line + 1, Pos.character + 1);
658 return Mgr.getMacroArgExpandedLocation(InputLoc);
659}
660
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000661/// Finds declarations locations that a given source location refers to.
662class DeclarationLocationsFinder : public index::IndexDataConsumer {
663 std::vector<Location> DeclarationLocations;
664 const SourceLocation &SearchedLocation;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000665 const ASTContext &AST;
666 Preprocessor &PP;
667
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000668public:
669 DeclarationLocationsFinder(raw_ostream &OS,
Ilya Biryukov04db3682017-07-21 13:29:29 +0000670 const SourceLocation &SearchedLocation,
671 ASTContext &AST, Preprocessor &PP)
672 : SearchedLocation(SearchedLocation), AST(AST), PP(PP) {}
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000673
674 std::vector<Location> takeLocations() {
675 // Don't keep the same location multiple times.
676 // This can happen when nodes in the AST are visited twice.
677 std::sort(DeclarationLocations.begin(), DeclarationLocations.end());
Kirill Bobyrev46213872017-06-28 20:57:28 +0000678 auto last =
679 std::unique(DeclarationLocations.begin(), DeclarationLocations.end());
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000680 DeclarationLocations.erase(last, DeclarationLocations.end());
681 return std::move(DeclarationLocations);
682 }
683
Ilya Biryukov02d58702017-08-01 15:51:38 +0000684 bool
685 handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
686 ArrayRef<index::SymbolRelation> Relations, FileID FID,
687 unsigned Offset,
688 index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000689 if (isSearchedLocation(FID, Offset)) {
690 addDeclarationLocation(D->getSourceRange());
691 }
692 return true;
693 }
694
695private:
696 bool isSearchedLocation(FileID FID, unsigned Offset) const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000697 const SourceManager &SourceMgr = AST.getSourceManager();
698 return SourceMgr.getFileOffset(SearchedLocation) == Offset &&
699 SourceMgr.getFileID(SearchedLocation) == FID;
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000700 }
701
Ilya Biryukov04db3682017-07-21 13:29:29 +0000702 void addDeclarationLocation(const SourceRange &ValSourceRange) {
703 const SourceManager &SourceMgr = AST.getSourceManager();
704 const LangOptions &LangOpts = AST.getLangOpts();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000705 SourceLocation LocStart = ValSourceRange.getBegin();
706 SourceLocation LocEnd = Lexer::getLocForEndOfToken(ValSourceRange.getEnd(),
Ilya Biryukov04db3682017-07-21 13:29:29 +0000707 0, SourceMgr, LangOpts);
Kirill Bobyrev46213872017-06-28 20:57:28 +0000708 Position Begin;
709 Begin.line = SourceMgr.getSpellingLineNumber(LocStart) - 1;
710 Begin.character = SourceMgr.getSpellingColumnNumber(LocStart) - 1;
711 Position End;
712 End.line = SourceMgr.getSpellingLineNumber(LocEnd) - 1;
713 End.character = SourceMgr.getSpellingColumnNumber(LocEnd) - 1;
714 Range R = {Begin, End};
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000715 Location L;
716 L.uri = URI::fromFile(
717 SourceMgr.getFilename(SourceMgr.getSpellingLoc(LocStart)));
718 L.range = R;
719 DeclarationLocations.push_back(L);
720 }
721
Kirill Bobyrev46213872017-06-28 20:57:28 +0000722 void finish() override {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000723 // Also handle possible macro at the searched location.
724 Token Result;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000725 if (!Lexer::getRawToken(SearchedLocation, Result, AST.getSourceManager(),
726 AST.getLangOpts(), false)) {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000727 if (Result.is(tok::raw_identifier)) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000728 PP.LookUpIdentifierInfo(Result);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000729 }
Ilya Biryukov04db3682017-07-21 13:29:29 +0000730 IdentifierInfo *IdentifierInfo = Result.getIdentifierInfo();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000731 if (IdentifierInfo && IdentifierInfo->hadMacroDefinition()) {
732 std::pair<FileID, unsigned int> DecLoc =
Ilya Biryukov04db3682017-07-21 13:29:29 +0000733 AST.getSourceManager().getDecomposedExpansionLoc(SearchedLocation);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000734 // Get the definition just before the searched location so that a macro
735 // referenced in a '#undef MACRO' can still be found.
Ilya Biryukov04db3682017-07-21 13:29:29 +0000736 SourceLocation BeforeSearchedLocation = getMacroArgExpandedLocation(
737 AST.getSourceManager(),
738 AST.getSourceManager().getFileEntryForID(DecLoc.first),
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000739 DecLoc.second - 1);
740 MacroDefinition MacroDef =
Ilya Biryukov04db3682017-07-21 13:29:29 +0000741 PP.getMacroDefinitionAtLoc(IdentifierInfo, BeforeSearchedLocation);
742 MacroInfo *MacroInf = MacroDef.getMacroInfo();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000743 if (MacroInf) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000744 addDeclarationLocation(SourceRange(MacroInf->getDefinitionLoc(),
745 MacroInf->getDefinitionEndLoc()));
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000746 }
747 }
748 }
749 }
750};
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000751
Ilya Biryukov02d58702017-08-01 15:51:38 +0000752SourceLocation getBeginningOfIdentifier(ParsedAST &Unit, const Position &Pos,
753 const FileEntry *FE) {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000754 // The language server protocol uses zero-based line and column numbers.
755 // Clang uses one-based numbers.
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000756
Ilya Biryukov02d58702017-08-01 15:51:38 +0000757 const ASTContext &AST = Unit.getASTContext();
Ilya Biryukov04db3682017-07-21 13:29:29 +0000758 const SourceManager &SourceMgr = AST.getSourceManager();
759
760 SourceLocation InputLocation =
761 getMacroArgExpandedLocation(SourceMgr, FE, Pos);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000762 if (Pos.character == 0) {
763 return InputLocation;
764 }
765
766 // This handle cases where the position is in the middle of a token or right
767 // after the end of a token. In theory we could just use GetBeginningOfToken
768 // to find the start of the token at the input position, but this doesn't
769 // work when right after the end, i.e. foo|.
770 // So try to go back by one and see if we're still inside the an identifier
771 // token. If so, Take the beginning of this token.
772 // (It should be the same identifier because you can't have two adjacent
773 // identifiers without another token in between.)
Ilya Biryukov04db3682017-07-21 13:29:29 +0000774 SourceLocation PeekBeforeLocation = getMacroArgExpandedLocation(
775 SourceMgr, FE, Position{Pos.line, Pos.character - 1});
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000776 Token Result;
Ilya Biryukov4203d2a2017-06-29 17:11:32 +0000777 if (Lexer::getRawToken(PeekBeforeLocation, Result, SourceMgr,
Ilya Biryukov04db3682017-07-21 13:29:29 +0000778 AST.getLangOpts(), false)) {
Ilya Biryukov4203d2a2017-06-29 17:11:32 +0000779 // getRawToken failed, just use InputLocation.
780 return InputLocation;
781 }
782
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000783 if (Result.is(tok::raw_identifier)) {
784 return Lexer::GetBeginningOfToken(PeekBeforeLocation, SourceMgr,
Ilya Biryukov02d58702017-08-01 15:51:38 +0000785 AST.getLangOpts());
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000786 }
787
788 return InputLocation;
789}
Ilya Biryukov02d58702017-08-01 15:51:38 +0000790} // namespace
Ilya Biryukov04db3682017-07-21 13:29:29 +0000791
Ilya Biryukov02d58702017-08-01 15:51:38 +0000792std::vector<Location> clangd::findDefinitions(ParsedAST &AST, Position Pos) {
793 const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
794 const FileEntry *FE = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
795 if (!FE)
796 return {};
797
798 SourceLocation SourceLocationBeg = getBeginningOfIdentifier(AST, Pos, FE);
799
800 auto DeclLocationsFinder = std::make_shared<DeclarationLocationsFinder>(
801 llvm::errs(), SourceLocationBeg, AST.getASTContext(),
802 AST.getPreprocessor());
803 index::IndexingOptions IndexOpts;
804 IndexOpts.SystemSymbolFilter =
805 index::IndexingOptions::SystemSymbolFilterKind::All;
806 IndexOpts.IndexFunctionLocals = true;
807
808 indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(),
809 DeclLocationsFinder, IndexOpts);
810
811 return DeclLocationsFinder->takeLocations();
812}
813
814void ParsedAST::ensurePreambleDeclsDeserialized() {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000815 if (PendingTopLevelDecls.empty())
816 return;
817
818 std::vector<const Decl *> Resolved;
819 Resolved.reserve(PendingTopLevelDecls.size());
820
821 ExternalASTSource &Source = *getASTContext().getExternalSource();
822 for (serialization::DeclID TopLevelDecl : PendingTopLevelDecls) {
823 // Resolve the declaration ID to an actual declaration, possibly
824 // deserializing the declaration in the process.
825 if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
826 Resolved.push_back(D);
827 }
828
829 TopLevelDecls.reserve(TopLevelDecls.size() + PendingTopLevelDecls.size());
830 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
831
832 PendingTopLevelDecls.clear();
833}
834
Ilya Biryukov02d58702017-08-01 15:51:38 +0000835ParsedAST::ParsedAST(ParsedAST &&Other) = default;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000836
Ilya Biryukov02d58702017-08-01 15:51:38 +0000837ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000838
Ilya Biryukov02d58702017-08-01 15:51:38 +0000839ParsedAST::~ParsedAST() {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000840 if (Action) {
841 Action->EndSourceFile();
842 }
843}
844
Ilya Biryukov02d58702017-08-01 15:51:38 +0000845ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
846
847const ASTContext &ParsedAST::getASTContext() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000848 return Clang->getASTContext();
849}
850
Ilya Biryukov02d58702017-08-01 15:51:38 +0000851Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
Ilya Biryukov04db3682017-07-21 13:29:29 +0000852
Ilya Biryukov02d58702017-08-01 15:51:38 +0000853const Preprocessor &ParsedAST::getPreprocessor() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000854 return Clang->getPreprocessor();
855}
856
Ilya Biryukov02d58702017-08-01 15:51:38 +0000857ArrayRef<const Decl *> ParsedAST::getTopLevelDecls() {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000858 ensurePreambleDeclsDeserialized();
859 return TopLevelDecls;
860}
861
Ilya Biryukov02d58702017-08-01 15:51:38 +0000862const std::vector<DiagWithFixIts> &ParsedAST::getDiagnostics() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000863 return Diags;
864}
865
Ilya Biryukov02d58702017-08-01 15:51:38 +0000866ParsedAST::ParsedAST(std::unique_ptr<CompilerInstance> Clang,
867 std::unique_ptr<FrontendAction> Action,
868 std::vector<const Decl *> TopLevelDecls,
869 std::vector<serialization::DeclID> PendingTopLevelDecls,
870 std::vector<DiagWithFixIts> Diags)
Ilya Biryukov04db3682017-07-21 13:29:29 +0000871 : Clang(std::move(Clang)), Action(std::move(Action)),
872 Diags(std::move(Diags)), TopLevelDecls(std::move(TopLevelDecls)),
873 PendingTopLevelDecls(std::move(PendingTopLevelDecls)) {
874 assert(this->Clang);
875 assert(this->Action);
876}
877
Ilya Biryukov02d58702017-08-01 15:51:38 +0000878ParsedASTWrapper::ParsedASTWrapper(ParsedASTWrapper &&Wrapper)
879 : AST(std::move(Wrapper.AST)) {}
880
881ParsedASTWrapper::ParsedASTWrapper(llvm::Optional<ParsedAST> AST)
882 : AST(std::move(AST)) {}
883
884PreambleData::PreambleData(PrecompiledPreamble Preamble,
885 std::vector<serialization::DeclID> TopLevelDeclIDs,
886 std::vector<DiagWithFixIts> Diags)
Ilya Biryukov04db3682017-07-21 13:29:29 +0000887 : Preamble(std::move(Preamble)),
888 TopLevelDeclIDs(std::move(TopLevelDeclIDs)), Diags(std::move(Diags)) {}
Ilya Biryukov02d58702017-08-01 15:51:38 +0000889
890std::shared_ptr<CppFile>
891CppFile::Create(PathRef FileName, tooling::CompileCommand Command,
892 std::shared_ptr<PCHContainerOperations> PCHs) {
893 return std::shared_ptr<CppFile>(
894 new CppFile(FileName, std::move(Command), std::move(PCHs)));
895}
896
897CppFile::CppFile(PathRef FileName, tooling::CompileCommand Command,
898 std::shared_ptr<PCHContainerOperations> PCHs)
899 : FileName(FileName), Command(std::move(Command)), RebuildCounter(0),
900 RebuildInProgress(false), PCHs(std::move(PCHs)) {
901
902 std::lock_guard<std::mutex> Lock(Mutex);
903 LatestAvailablePreamble = nullptr;
904 PreamblePromise.set_value(nullptr);
905 PreambleFuture = PreamblePromise.get_future();
906
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000907 ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
Ilya Biryukov02d58702017-08-01 15:51:38 +0000908 ASTFuture = ASTPromise.get_future();
909}
910
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000911void CppFile::cancelRebuild() { deferCancelRebuild().get(); }
912
913std::future<void> CppFile::deferCancelRebuild() {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000914 std::unique_lock<std::mutex> Lock(Mutex);
915 // Cancel an ongoing rebuild, if any, and wait for it to finish.
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000916 unsigned RequestRebuildCounter = ++this->RebuildCounter;
Ilya Biryukov02d58702017-08-01 15:51:38 +0000917 // Rebuild asserts that futures aren't ready if rebuild is cancelled.
918 // We want to keep this invariant.
919 if (futureIsReady(PreambleFuture)) {
920 PreamblePromise = std::promise<std::shared_ptr<const PreambleData>>();
921 PreambleFuture = PreamblePromise.get_future();
922 }
923 if (futureIsReady(ASTFuture)) {
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000924 ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
Ilya Biryukov02d58702017-08-01 15:51:38 +0000925 ASTFuture = ASTPromise.get_future();
926 }
Ilya Biryukov02d58702017-08-01 15:51:38 +0000927
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000928 Lock.unlock();
929 // Notify about changes to RebuildCounter.
930 RebuildCond.notify_all();
931
932 std::shared_ptr<CppFile> That = shared_from_this();
933 return std::async(std::launch::deferred, [That, RequestRebuildCounter]() {
934 std::unique_lock<std::mutex> Lock(That->Mutex);
935 CppFile *This = &*That;
936 This->RebuildCond.wait(Lock, [This, RequestRebuildCounter]() {
937 return !This->RebuildInProgress ||
938 This->RebuildCounter != RequestRebuildCounter;
939 });
940
941 // This computation got cancelled itself, do nothing.
942 if (This->RebuildCounter != RequestRebuildCounter)
943 return;
944
945 // Set empty results for Promises.
946 That->PreamblePromise.set_value(nullptr);
947 That->ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
948 });
Ilya Biryukov02d58702017-08-01 15:51:38 +0000949}
950
951llvm::Optional<std::vector<DiagWithFixIts>>
952CppFile::rebuild(StringRef NewContents,
953 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
954 return deferRebuild(NewContents, std::move(VFS)).get();
955}
956
957std::future<llvm::Optional<std::vector<DiagWithFixIts>>>
958CppFile::deferRebuild(StringRef NewContents,
959 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
960 std::shared_ptr<const PreambleData> OldPreamble;
961 std::shared_ptr<PCHContainerOperations> PCHs;
962 unsigned RequestRebuildCounter;
963 {
964 std::unique_lock<std::mutex> Lock(Mutex);
965 // Increase RebuildCounter to cancel all ongoing FinishRebuild operations.
966 // They will try to exit as early as possible and won't call set_value on
967 // our promises.
968 RequestRebuildCounter = ++this->RebuildCounter;
969 PCHs = this->PCHs;
970
971 // Remember the preamble to be used during rebuild.
972 OldPreamble = this->LatestAvailablePreamble;
973 // Setup std::promises and std::futures for Preamble and AST. Corresponding
974 // futures will wait until the rebuild process is finished.
975 if (futureIsReady(this->PreambleFuture)) {
976 this->PreamblePromise =
977 std::promise<std::shared_ptr<const PreambleData>>();
978 this->PreambleFuture = this->PreamblePromise.get_future();
979 }
980 if (futureIsReady(this->ASTFuture)) {
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000981 this->ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
Ilya Biryukov02d58702017-08-01 15:51:38 +0000982 this->ASTFuture = this->ASTPromise.get_future();
983 }
984 } // unlock Mutex.
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000985 // Notify about changes to RebuildCounter.
986 RebuildCond.notify_all();
Ilya Biryukov02d58702017-08-01 15:51:38 +0000987
988 // A helper to function to finish the rebuild. May be run on a different
989 // thread.
990
991 // Don't let this CppFile die before rebuild is finished.
992 std::shared_ptr<CppFile> That = shared_from_this();
993 auto FinishRebuild = [OldPreamble, VFS, RequestRebuildCounter, PCHs,
994 That](std::string NewContents)
995 -> llvm::Optional<std::vector<DiagWithFixIts>> {
996 // Only one execution of this method is possible at a time.
997 // RebuildGuard will wait for any ongoing rebuilds to finish and will put us
998 // into a state for doing a rebuild.
999 RebuildGuard Rebuild(*That, RequestRebuildCounter);
1000 if (Rebuild.wasCancelledBeforeConstruction())
1001 return llvm::None;
1002
1003 std::vector<const char *> ArgStrs;
1004 for (const auto &S : That->Command.CommandLine)
1005 ArgStrs.push_back(S.c_str());
1006
1007 VFS->setCurrentWorkingDirectory(That->Command.Directory);
1008
1009 std::unique_ptr<CompilerInvocation> CI;
1010 {
1011 // FIXME(ibiryukov): store diagnostics from CommandLine when we start
1012 // reporting them.
1013 EmptyDiagsConsumer CommandLineDiagsConsumer;
1014 IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
1015 CompilerInstance::createDiagnostics(new DiagnosticOptions,
1016 &CommandLineDiagsConsumer, false);
1017 CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
1018 }
1019 assert(CI && "Couldn't create CompilerInvocation");
1020
1021 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1022 llvm::MemoryBuffer::getMemBufferCopy(NewContents, That->FileName);
1023
1024 // A helper function to rebuild the preamble or reuse the existing one. Does
1025 // not mutate any fields, only does the actual computation.
1026 auto DoRebuildPreamble = [&]() -> std::shared_ptr<const PreambleData> {
1027 auto Bounds =
1028 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
1029 if (OldPreamble && OldPreamble->Preamble.CanReuse(
1030 *CI, ContentsBuffer.get(), Bounds, VFS.get())) {
1031 return OldPreamble;
1032 }
1033
1034 std::vector<DiagWithFixIts> PreambleDiags;
1035 StoreDiagsConsumer PreambleDiagnosticsConsumer(/*ref*/ PreambleDiags);
1036 IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
1037 CompilerInstance::createDiagnostics(
1038 &CI->getDiagnosticOpts(), &PreambleDiagnosticsConsumer, false);
1039 CppFilePreambleCallbacks SerializedDeclsCollector;
1040 auto BuiltPreamble = PrecompiledPreamble::Build(
1041 *CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine, VFS, PCHs,
1042 SerializedDeclsCollector);
1043
1044 if (BuiltPreamble) {
1045 return std::make_shared<PreambleData>(
1046 std::move(*BuiltPreamble),
1047 SerializedDeclsCollector.takeTopLevelDeclIDs(),
1048 std::move(PreambleDiags));
1049 } else {
1050 return nullptr;
1051 }
1052 };
1053
1054 // Compute updated Preamble.
1055 std::shared_ptr<const PreambleData> NewPreamble = DoRebuildPreamble();
1056 // Publish the new Preamble.
1057 {
1058 std::lock_guard<std::mutex> Lock(That->Mutex);
1059 // We always set LatestAvailablePreamble to the new value, hoping that it
1060 // will still be usable in the further requests.
1061 That->LatestAvailablePreamble = NewPreamble;
1062 if (RequestRebuildCounter != That->RebuildCounter)
1063 return llvm::None; // Our rebuild request was cancelled, do nothing.
1064 That->PreamblePromise.set_value(NewPreamble);
1065 } // unlock Mutex
1066
1067 // Prepare the Preamble and supplementary data for rebuilding AST.
1068 const PrecompiledPreamble *PreambleForAST = nullptr;
1069 ArrayRef<serialization::DeclID> SerializedPreambleDecls = llvm::None;
1070 std::vector<DiagWithFixIts> Diagnostics;
1071 if (NewPreamble) {
1072 PreambleForAST = &NewPreamble->Preamble;
1073 SerializedPreambleDecls = NewPreamble->TopLevelDeclIDs;
1074 Diagnostics.insert(Diagnostics.begin(), NewPreamble->Diags.begin(),
1075 NewPreamble->Diags.end());
1076 }
1077
1078 // Compute updated AST.
1079 llvm::Optional<ParsedAST> NewAST =
1080 ParsedAST::Build(std::move(CI), PreambleForAST, SerializedPreambleDecls,
1081 std::move(ContentsBuffer), PCHs, VFS);
1082
1083 if (NewAST) {
1084 Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(),
1085 NewAST->getDiagnostics().end());
1086 } else {
1087 // Don't report even Preamble diagnostics if we coulnd't build AST.
1088 Diagnostics.clear();
1089 }
1090
1091 // Publish the new AST.
1092 {
1093 std::lock_guard<std::mutex> Lock(That->Mutex);
1094 if (RequestRebuildCounter != That->RebuildCounter)
1095 return Diagnostics; // Our rebuild request was cancelled, don't set
1096 // ASTPromise.
1097
Ilya Biryukov574b7532017-08-02 09:08:39 +00001098 That->ASTPromise.set_value(
1099 std::make_shared<ParsedASTWrapper>(std::move(NewAST)));
Ilya Biryukov02d58702017-08-01 15:51:38 +00001100 } // unlock Mutex
1101
1102 return Diagnostics;
1103 };
1104
1105 return std::async(std::launch::deferred, FinishRebuild, NewContents.str());
1106}
1107
1108std::shared_future<std::shared_ptr<const PreambleData>>
1109CppFile::getPreamble() const {
1110 std::lock_guard<std::mutex> Lock(Mutex);
1111 return PreambleFuture;
1112}
1113
1114std::shared_ptr<const PreambleData> CppFile::getPossiblyStalePreamble() const {
1115 std::lock_guard<std::mutex> Lock(Mutex);
1116 return LatestAvailablePreamble;
1117}
1118
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001119std::shared_future<std::shared_ptr<ParsedASTWrapper>> CppFile::getAST() const {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001120 std::lock_guard<std::mutex> Lock(Mutex);
1121 return ASTFuture;
1122}
1123
1124tooling::CompileCommand const &CppFile::getCompileCommand() const {
1125 return Command;
1126}
1127
1128CppFile::RebuildGuard::RebuildGuard(CppFile &File,
1129 unsigned RequestRebuildCounter)
1130 : File(File), RequestRebuildCounter(RequestRebuildCounter) {
1131 std::unique_lock<std::mutex> Lock(File.Mutex);
1132 WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
1133 if (WasCancelledBeforeConstruction)
1134 return;
1135
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001136 File.RebuildCond.wait(Lock, [&File, RequestRebuildCounter]() {
1137 return !File.RebuildInProgress ||
1138 File.RebuildCounter != RequestRebuildCounter;
1139 });
Ilya Biryukov02d58702017-08-01 15:51:38 +00001140
1141 WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
1142 if (WasCancelledBeforeConstruction)
1143 return;
1144
1145 File.RebuildInProgress = true;
1146}
1147
1148bool CppFile::RebuildGuard::wasCancelledBeforeConstruction() const {
1149 return WasCancelledBeforeConstruction;
1150}
1151
1152CppFile::RebuildGuard::~RebuildGuard() {
1153 if (WasCancelledBeforeConstruction)
1154 return;
1155
1156 std::unique_lock<std::mutex> Lock(File.Mutex);
1157 assert(File.RebuildInProgress);
1158 File.RebuildInProgress = false;
1159
1160 if (File.RebuildCounter == RequestRebuildCounter) {
1161 // Our rebuild request was successful.
1162 assert(futureIsReady(File.ASTFuture));
1163 assert(futureIsReady(File.PreambleFuture));
1164 } else {
1165 // Our rebuild request was cancelled, because further reparse was requested.
1166 assert(!futureIsReady(File.ASTFuture));
1167 assert(!futureIsReady(File.PreambleFuture));
1168 }
1169
1170 Lock.unlock();
1171 File.RebuildCond.notify_all();
1172}