blob: 53170a58c2d0db27a5fe91fc5cc55e9eb9597b75 [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 Biryukov83ca8a22017-09-20 10:46:58 +000012#include "Logger.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000013#include "clang/Frontend/CompilerInstance.h"
14#include "clang/Frontend/CompilerInvocation.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000015#include "clang/Frontend/FrontendActions.h"
Ilya Biryukov0f62ed22017-05-26 12:26:51 +000016#include "clang/Frontend/Utils.h"
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000017#include "clang/Index/IndexDataConsumer.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000018#include "clang/Index/IndexingAction.h"
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000019#include "clang/Lex/Lexer.h"
20#include "clang/Lex/MacroInfo.h"
21#include "clang/Lex/Preprocessor.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000022#include "clang/Lex/PreprocessorOptions.h"
23#include "clang/Sema/Sema.h"
24#include "clang/Serialization/ASTWriter.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000025#include "clang/Tooling/CompilationDatabase.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000026#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/Support/CrashRecoveryContext.h"
Krasimir Georgieva1de3c92017-06-15 09:11:57 +000029#include "llvm/Support/Format.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000030
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000031#include <algorithm>
Ilya Biryukov02d58702017-08-01 15:51:38 +000032#include <chrono>
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000033
Ilya Biryukov38d79772017-05-16 09:38:59 +000034using namespace clang::clangd;
35using namespace clang;
36
Ilya Biryukov04db3682017-07-21 13:29:29 +000037namespace {
38
39class DeclTrackingASTConsumer : public ASTConsumer {
40public:
41 DeclTrackingASTConsumer(std::vector<const Decl *> &TopLevelDecls)
42 : TopLevelDecls(TopLevelDecls) {}
43
44 bool HandleTopLevelDecl(DeclGroupRef DG) override {
45 for (const Decl *D : DG) {
46 // ObjCMethodDecl are not actually top-level decls.
47 if (isa<ObjCMethodDecl>(D))
48 continue;
49
50 TopLevelDecls.push_back(D);
51 }
52 return true;
53 }
54
55private:
56 std::vector<const Decl *> &TopLevelDecls;
57};
58
59class ClangdFrontendAction : public SyntaxOnlyAction {
60public:
61 std::vector<const Decl *> takeTopLevelDecls() {
62 return std::move(TopLevelDecls);
63 }
64
65protected:
66 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
67 StringRef InFile) override {
68 return llvm::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls);
69 }
70
71private:
72 std::vector<const Decl *> TopLevelDecls;
73};
74
Ilya Biryukov02d58702017-08-01 15:51:38 +000075class CppFilePreambleCallbacks : public PreambleCallbacks {
Ilya Biryukov04db3682017-07-21 13:29:29 +000076public:
77 std::vector<serialization::DeclID> takeTopLevelDeclIDs() {
78 return std::move(TopLevelDeclIDs);
79 }
80
81 void AfterPCHEmitted(ASTWriter &Writer) override {
82 TopLevelDeclIDs.reserve(TopLevelDecls.size());
83 for (Decl *D : TopLevelDecls) {
84 // Invalid top-level decls may not have been serialized.
85 if (D->isInvalidDecl())
86 continue;
87 TopLevelDeclIDs.push_back(Writer.getDeclID(D));
88 }
89 }
90
91 void HandleTopLevelDecl(DeclGroupRef DG) override {
92 for (Decl *D : DG) {
93 if (isa<ObjCMethodDecl>(D))
94 continue;
95 TopLevelDecls.push_back(D);
96 }
97 }
98
99private:
100 std::vector<Decl *> TopLevelDecls;
101 std::vector<serialization::DeclID> TopLevelDeclIDs;
102};
103
104/// Convert from clang diagnostic level to LSP severity.
105static int getSeverity(DiagnosticsEngine::Level L) {
106 switch (L) {
107 case DiagnosticsEngine::Remark:
108 return 4;
109 case DiagnosticsEngine::Note:
110 return 3;
111 case DiagnosticsEngine::Warning:
112 return 2;
113 case DiagnosticsEngine::Fatal:
114 case DiagnosticsEngine::Error:
115 return 1;
116 case DiagnosticsEngine::Ignored:
117 return 0;
118 }
119 llvm_unreachable("Unknown diagnostic level!");
120}
121
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000122/// Get the optional chunk as a string. This function is possibly recursive.
123///
124/// The parameter info for each parameter is appended to the Parameters.
125std::string
126getOptionalParameters(const CodeCompletionString &CCS,
127 std::vector<ParameterInformation> &Parameters) {
128 std::string Result;
129 for (const auto &Chunk : CCS) {
130 switch (Chunk.Kind) {
131 case CodeCompletionString::CK_Optional:
132 assert(Chunk.Optional &&
133 "Expected the optional code completion string to be non-null.");
134 Result += getOptionalParameters(*Chunk.Optional, Parameters);
135 break;
136 case CodeCompletionString::CK_VerticalSpace:
137 break;
138 case CodeCompletionString::CK_Placeholder:
139 // A string that acts as a placeholder for, e.g., a function call
140 // argument.
141 // Intentional fallthrough here.
142 case CodeCompletionString::CK_CurrentParameter: {
143 // A piece of text that describes the parameter that corresponds to
144 // the code-completion location within a function call, message send,
145 // macro invocation, etc.
146 Result += Chunk.Text;
147 ParameterInformation Info;
148 Info.label = Chunk.Text;
149 Parameters.push_back(std::move(Info));
150 break;
151 }
152 default:
153 Result += Chunk.Text;
154 break;
155 }
156 }
157 return Result;
158}
159
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000160llvm::Optional<DiagWithFixIts> toClangdDiag(const StoredDiagnostic &D) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000161 auto Location = D.getLocation();
162 if (!Location.isValid() || !Location.getManager().isInMainFile(Location))
163 return llvm::None;
164
165 Position P;
166 P.line = Location.getSpellingLineNumber() - 1;
167 P.character = Location.getSpellingColumnNumber();
168 Range R = {P, P};
169 clangd::Diagnostic Diag = {R, getSeverity(D.getLevel()), D.getMessage()};
170
171 llvm::SmallVector<tooling::Replacement, 1> FixItsForDiagnostic;
172 for (const FixItHint &Fix : D.getFixIts()) {
173 FixItsForDiagnostic.push_back(clang::tooling::Replacement(
174 Location.getManager(), Fix.RemoveRange, Fix.CodeToInsert));
175 }
176 return DiagWithFixIts{Diag, std::move(FixItsForDiagnostic)};
177}
178
179class StoreDiagsConsumer : public DiagnosticConsumer {
180public:
181 StoreDiagsConsumer(std::vector<DiagWithFixIts> &Output) : Output(Output) {}
182
183 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
184 const clang::Diagnostic &Info) override {
185 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
186
187 if (auto convertedDiag = toClangdDiag(StoredDiagnostic(DiagLevel, Info)))
188 Output.push_back(std::move(*convertedDiag));
189 }
190
191private:
192 std::vector<DiagWithFixIts> &Output;
193};
194
195class EmptyDiagsConsumer : public DiagnosticConsumer {
196public:
197 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
198 const clang::Diagnostic &Info) override {}
199};
200
201std::unique_ptr<CompilerInvocation>
202createCompilerInvocation(ArrayRef<const char *> ArgList,
203 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
204 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
205 auto CI = createInvocationFromCommandLine(ArgList, std::move(Diags),
206 std::move(VFS));
207 // We rely on CompilerInstance to manage the resource (i.e. free them on
208 // EndSourceFile), but that won't happen if DisableFree is set to true.
209 // Since createInvocationFromCommandLine sets it to true, we have to override
210 // it.
211 CI->getFrontendOpts().DisableFree = false;
212 return CI;
213}
214
215/// Creates a CompilerInstance from \p CI, with main buffer overriden to \p
216/// Buffer and arguments to read the PCH from \p Preamble, if \p Preamble is not
217/// null. Note that vfs::FileSystem inside returned instance may differ from \p
218/// VFS if additional file remapping were set in command-line arguments.
219/// On some errors, returns null. When non-null value is returned, it's expected
220/// to be consumed by the FrontendAction as it will have a pointer to the \p
221/// Buffer that will only be deleted if BeginSourceFile is called.
222std::unique_ptr<CompilerInstance>
223prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
224 const PrecompiledPreamble *Preamble,
225 std::unique_ptr<llvm::MemoryBuffer> Buffer,
226 std::shared_ptr<PCHContainerOperations> PCHs,
227 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
228 DiagnosticConsumer &DiagsClient) {
229 assert(VFS && "VFS is null");
230 assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
231 "Setting RetainRemappedFileBuffers to true will cause a memory leak "
232 "of ContentsBuffer");
233
234 // NOTE: we use Buffer.get() when adding remapped files, so we have to make
235 // sure it will be released if no error is emitted.
236 if (Preamble) {
237 Preamble->AddImplicitPreamble(*CI, Buffer.get());
238 } else {
239 CI->getPreprocessorOpts().addRemappedFile(
240 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
241 }
242
243 auto Clang = llvm::make_unique<CompilerInstance>(PCHs);
244 Clang->setInvocation(std::move(CI));
245 Clang->createDiagnostics(&DiagsClient, false);
246
247 if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
248 Clang->getInvocation(), Clang->getDiagnostics(), VFS))
249 VFS = VFSWithRemapping;
250 Clang->setVirtualFileSystem(VFS);
251
252 Clang->setTarget(TargetInfo::CreateTargetInfo(
253 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
254 if (!Clang->hasTarget())
255 return nullptr;
256
257 // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
258 // release it.
259 Buffer.release();
260 return Clang;
261}
262
Ilya Biryukov02d58702017-08-01 15:51:38 +0000263template <class T> bool futureIsReady(std::shared_future<T> const &Future) {
264 return Future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
265}
266
Ilya Biryukov04db3682017-07-21 13:29:29 +0000267} // namespace
268
Ilya Biryukov38d79772017-05-16 09:38:59 +0000269namespace {
270
Ilya Biryukov01e3bf82017-10-23 06:06:21 +0000271CompletionItemKind getKindOfDecl(CXCursorKind CursorKind) {
272 switch (CursorKind) {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000273 case CXCursor_MacroInstantiation:
274 case CXCursor_MacroDefinition:
275 return CompletionItemKind::Text;
276 case CXCursor_CXXMethod:
277 return CompletionItemKind::Method;
278 case CXCursor_FunctionDecl:
279 case CXCursor_FunctionTemplate:
280 return CompletionItemKind::Function;
281 case CXCursor_Constructor:
282 case CXCursor_Destructor:
283 return CompletionItemKind::Constructor;
284 case CXCursor_FieldDecl:
285 return CompletionItemKind::Field;
286 case CXCursor_VarDecl:
287 case CXCursor_ParmDecl:
288 return CompletionItemKind::Variable;
289 case CXCursor_ClassDecl:
290 case CXCursor_StructDecl:
291 case CXCursor_UnionDecl:
292 case CXCursor_ClassTemplate:
293 case CXCursor_ClassTemplatePartialSpecialization:
294 return CompletionItemKind::Class;
295 case CXCursor_Namespace:
296 case CXCursor_NamespaceAlias:
297 case CXCursor_NamespaceRef:
298 return CompletionItemKind::Module;
299 case CXCursor_EnumConstantDecl:
300 return CompletionItemKind::Value;
301 case CXCursor_EnumDecl:
302 return CompletionItemKind::Enum;
303 case CXCursor_TypeAliasDecl:
304 case CXCursor_TypeAliasTemplateDecl:
305 case CXCursor_TypedefDecl:
306 case CXCursor_MemberRef:
307 case CXCursor_TypeRef:
308 return CompletionItemKind::Reference;
309 default:
310 return CompletionItemKind::Missing;
311 }
312}
313
Ilya Biryukov01e3bf82017-10-23 06:06:21 +0000314CompletionItemKind getKind(CodeCompletionResult::ResultKind ResKind,
315 CXCursorKind CursorKind) {
316 switch (ResKind) {
317 case CodeCompletionResult::RK_Declaration:
318 return getKindOfDecl(CursorKind);
319 case CodeCompletionResult::RK_Keyword:
320 return CompletionItemKind::Keyword;
321 case CodeCompletionResult::RK_Macro:
322 return CompletionItemKind::Text; // unfortunately, there's no 'Macro'
323 // completion items in LSP.
324 case CodeCompletionResult::RK_Pattern:
325 return CompletionItemKind::Snippet;
326 }
327 llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
328}
329
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000330std::string escapeSnippet(const llvm::StringRef Text) {
331 std::string Result;
332 Result.reserve(Text.size()); // Assume '$', '}' and '\\' are rare.
333 for (const auto Character : Text) {
334 if (Character == '$' || Character == '}' || Character == '\\')
335 Result.push_back('\\');
336 Result.push_back(Character);
337 }
338 return Result;
339}
340
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000341std::string getDocumentation(const CodeCompletionString &CCS) {
342 // Things like __attribute__((nonnull(1,3))) and [[noreturn]]. Present this
343 // information in the documentation field.
344 std::string Result;
345 const unsigned AnnotationCount = CCS.getAnnotationCount();
346 if (AnnotationCount > 0) {
347 Result += "Annotation";
348 if (AnnotationCount == 1) {
349 Result += ": ";
350 } else /* AnnotationCount > 1 */ {
351 Result += "s: ";
352 }
353 for (unsigned I = 0; I < AnnotationCount; ++I) {
354 Result += CCS.getAnnotation(I);
355 Result.push_back(I == AnnotationCount - 1 ? '\n' : ' ');
356 }
357 }
358 // Add brief documentation (if there is any).
359 if (CCS.getBriefComment() != nullptr) {
360 if (!Result.empty()) {
361 // This means we previously added annotations. Add an extra newline
362 // character to make the annotations stand out.
363 Result.push_back('\n');
364 }
365 Result += CCS.getBriefComment();
366 }
367 return Result;
368}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000369
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000370class CompletionItemsCollector : public CodeCompleteConsumer {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000371public:
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000372 CompletionItemsCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000373 std::vector<CompletionItem> &Items)
Ilya Biryukov38d79772017-05-16 09:38:59 +0000374 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
375 Items(Items),
376 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
377 CCTUInfo(Allocator) {}
378
379 void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
380 CodeCompletionResult *Results,
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000381 unsigned NumResults) override final {
382 Items.reserve(NumResults);
383 for (unsigned I = 0; I < NumResults; ++I) {
384 auto &Result = Results[I];
385 const auto *CCS = Result.CreateCodeCompletionString(
Ilya Biryukov38d79772017-05-16 09:38:59 +0000386 S, Context, *Allocator, CCTUInfo,
387 CodeCompleteOpts.IncludeBriefComments);
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000388 assert(CCS && "Expected the CodeCompletionString to be non-null");
389 Items.push_back(ProcessCodeCompleteResult(Result, *CCS));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000390 }
391 }
392
393 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
394
395 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000396
397private:
398 CompletionItem
399 ProcessCodeCompleteResult(const CodeCompletionResult &Result,
400 const CodeCompletionString &CCS) const {
401
402 // Adjust this to InsertTextFormat::Snippet iff we encounter a
403 // CK_Placeholder chunk in SnippetCompletionItemsCollector.
404 CompletionItem Item;
405 Item.insertTextFormat = InsertTextFormat::PlainText;
406
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000407 Item.documentation = getDocumentation(CCS);
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000408
409 // Fill in the label, detail, insertText and filterText fields of the
410 // CompletionItem.
411 ProcessChunks(CCS, Item);
412
413 // Fill in the kind field of the CompletionItem.
Ilya Biryukov01e3bf82017-10-23 06:06:21 +0000414 Item.kind = getKind(Result.Kind, Result.CursorKind);
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000415
416 FillSortText(CCS, Item);
417
418 return Item;
419 }
420
421 virtual void ProcessChunks(const CodeCompletionString &CCS,
422 CompletionItem &Item) const = 0;
423
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000424 static int GetSortPriority(const CodeCompletionString &CCS) {
425 int Score = CCS.getPriority();
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000426 // Fill in the sortText of the CompletionItem.
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000427 assert(Score <= 99999 && "Expecting code completion result "
428 "priority to have at most 5-digits");
429
430 const int Penalty = 100000;
431 switch (static_cast<CXAvailabilityKind>(CCS.getAvailability())) {
432 case CXAvailability_Available:
433 // No penalty.
434 break;
435 case CXAvailability_Deprecated:
436 Score += Penalty;
437 break;
438 case CXAvailability_NotAccessible:
439 Score += 2 * Penalty;
440 break;
441 case CXAvailability_NotAvailable:
442 Score += 3 * Penalty;
443 break;
444 }
445
446 return Score;
447 }
448
449 static void FillSortText(const CodeCompletionString &CCS,
450 CompletionItem &Item) {
451 int Priority = GetSortPriority(CCS);
452 // Fill in the sortText of the CompletionItem.
453 assert(Priority <= 999999 &&
454 "Expecting sort priority to have at most 6-digits");
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000455 llvm::raw_string_ostream(Item.sortText)
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000456 << llvm::format("%06d%s", Priority, Item.filterText.c_str());
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000457 }
458
459 std::vector<CompletionItem> &Items;
460 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
461 CodeCompletionTUInfo CCTUInfo;
462
463}; // CompletionItemsCollector
464
Ilya Biryukov686ff9a2017-09-28 18:39:59 +0000465bool isInformativeQualifierChunk(CodeCompletionString::Chunk const &Chunk) {
466 return Chunk.Kind == CodeCompletionString::CK_Informative &&
467 StringRef(Chunk.Text).endswith("::");
468}
469
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000470class PlainTextCompletionItemsCollector final
471 : public CompletionItemsCollector {
472
473public:
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000474 PlainTextCompletionItemsCollector(
475 const clang::CodeCompleteOptions &CodeCompleteOpts,
476 std::vector<CompletionItem> &Items)
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000477 : CompletionItemsCollector(CodeCompleteOpts, Items) {}
478
479private:
480 void ProcessChunks(const CodeCompletionString &CCS,
481 CompletionItem &Item) const override {
482 for (const auto &Chunk : CCS) {
Ilya Biryukov686ff9a2017-09-28 18:39:59 +0000483 // Informative qualifier chunks only clutter completion results, skip
484 // them.
485 if (isInformativeQualifierChunk(Chunk))
486 continue;
487
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000488 switch (Chunk.Kind) {
489 case CodeCompletionString::CK_TypedText:
490 // There's always exactly one CK_TypedText chunk.
491 Item.insertText = Item.filterText = Chunk.Text;
492 Item.label += Chunk.Text;
493 break;
494 case CodeCompletionString::CK_ResultType:
495 assert(Item.detail.empty() && "Unexpected extraneous CK_ResultType");
496 Item.detail = Chunk.Text;
497 break;
498 case CodeCompletionString::CK_Optional:
499 break;
500 default:
501 Item.label += Chunk.Text;
502 break;
503 }
504 }
505 }
506}; // PlainTextCompletionItemsCollector
507
508class SnippetCompletionItemsCollector final : public CompletionItemsCollector {
509
510public:
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000511 SnippetCompletionItemsCollector(
512 const clang::CodeCompleteOptions &CodeCompleteOpts,
513 std::vector<CompletionItem> &Items)
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000514 : CompletionItemsCollector(CodeCompleteOpts, Items) {}
515
516private:
517 void ProcessChunks(const CodeCompletionString &CCS,
518 CompletionItem &Item) const override {
519 unsigned ArgCount = 0;
520 for (const auto &Chunk : CCS) {
Ilya Biryukov686ff9a2017-09-28 18:39:59 +0000521 // Informative qualifier chunks only clutter completion results, skip
522 // them.
523 if (isInformativeQualifierChunk(Chunk))
524 continue;
525
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000526 switch (Chunk.Kind) {
527 case CodeCompletionString::CK_TypedText:
528 // The piece of text that the user is expected to type to match
529 // the code-completion string, typically a keyword or the name of
530 // a declarator or macro.
531 Item.filterText = Chunk.Text;
Simon Pilgrim948c0bc2017-11-01 09:22:03 +0000532 LLVM_FALLTHROUGH;
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000533 case CodeCompletionString::CK_Text:
534 // A piece of text that should be placed in the buffer,
535 // e.g., parentheses or a comma in a function call.
536 Item.label += Chunk.Text;
537 Item.insertText += Chunk.Text;
538 break;
539 case CodeCompletionString::CK_Optional:
540 // A code completion string that is entirely optional.
541 // For example, an optional code completion string that
542 // describes the default arguments in a function call.
543
544 // FIXME: Maybe add an option to allow presenting the optional chunks?
545 break;
546 case CodeCompletionString::CK_Placeholder:
547 // A string that acts as a placeholder for, e.g., a function call
548 // argument.
549 ++ArgCount;
550 Item.insertText += "${" + std::to_string(ArgCount) + ':' +
551 escapeSnippet(Chunk.Text) + '}';
552 Item.label += Chunk.Text;
553 Item.insertTextFormat = InsertTextFormat::Snippet;
554 break;
555 case CodeCompletionString::CK_Informative:
556 // A piece of text that describes something about the result
557 // but should not be inserted into the buffer.
558 // For example, the word "const" for a const method, or the name of
559 // the base class for methods that are part of the base class.
560 Item.label += Chunk.Text;
561 // Don't put the informative chunks in the insertText.
562 break;
563 case CodeCompletionString::CK_ResultType:
564 // A piece of text that describes the type of an entity or,
565 // for functions and methods, the return type.
566 assert(Item.detail.empty() && "Unexpected extraneous CK_ResultType");
567 Item.detail = Chunk.Text;
568 break;
569 case CodeCompletionString::CK_CurrentParameter:
570 // A piece of text that describes the parameter that corresponds to
571 // the code-completion location within a function call, message send,
572 // macro invocation, etc.
573 //
574 // This should never be present while collecting completion items,
575 // only while collecting overload candidates.
576 llvm_unreachable("Unexpected CK_CurrentParameter while collecting "
577 "CompletionItems");
578 break;
579 case CodeCompletionString::CK_LeftParen:
580 // A left parenthesis ('(').
581 case CodeCompletionString::CK_RightParen:
582 // A right parenthesis (')').
583 case CodeCompletionString::CK_LeftBracket:
584 // A left bracket ('[').
585 case CodeCompletionString::CK_RightBracket:
586 // A right bracket (']').
587 case CodeCompletionString::CK_LeftBrace:
588 // A left brace ('{').
589 case CodeCompletionString::CK_RightBrace:
590 // A right brace ('}').
591 case CodeCompletionString::CK_LeftAngle:
592 // A left angle bracket ('<').
593 case CodeCompletionString::CK_RightAngle:
594 // A right angle bracket ('>').
595 case CodeCompletionString::CK_Comma:
596 // A comma separator (',').
597 case CodeCompletionString::CK_Colon:
598 // A colon (':').
599 case CodeCompletionString::CK_SemiColon:
600 // A semicolon (';').
601 case CodeCompletionString::CK_Equal:
602 // An '=' sign.
603 case CodeCompletionString::CK_HorizontalSpace:
604 // Horizontal whitespace (' ').
605 Item.insertText += Chunk.Text;
606 Item.label += Chunk.Text;
607 break;
608 case CodeCompletionString::CK_VerticalSpace:
609 // Vertical whitespace ('\n' or '\r\n', depending on the
610 // platform).
611 Item.insertText += Chunk.Text;
612 // Don't even add a space to the label.
613 break;
614 }
615 }
616 }
617}; // SnippetCompletionItemsCollector
Ilya Biryukov38d79772017-05-16 09:38:59 +0000618
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000619class SignatureHelpCollector final : public CodeCompleteConsumer {
620
621public:
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000622 SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000623 SignatureHelp &SigHelp)
624 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
625 SigHelp(SigHelp),
626 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
627 CCTUInfo(Allocator) {}
628
629 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
630 OverloadCandidate *Candidates,
631 unsigned NumCandidates) override {
632 SigHelp.signatures.reserve(NumCandidates);
633 // FIXME(rwols): How can we determine the "active overload candidate"?
634 // Right now the overloaded candidates seem to be provided in a "best fit"
635 // order, so I'm not too worried about this.
636 SigHelp.activeSignature = 0;
Simon Pilgrima3aa7242017-10-07 12:24:10 +0000637 assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000638 "too many arguments");
639 SigHelp.activeParameter = static_cast<int>(CurrentArg);
640 for (unsigned I = 0; I < NumCandidates; ++I) {
641 const auto &Candidate = Candidates[I];
642 const auto *CCS = Candidate.CreateSignatureString(
643 CurrentArg, S, *Allocator, CCTUInfo, true);
644 assert(CCS && "Expected the CodeCompletionString to be non-null");
645 SigHelp.signatures.push_back(ProcessOverloadCandidate(Candidate, *CCS));
646 }
647 }
648
649 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
650
651 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
652
653private:
654 SignatureInformation
655 ProcessOverloadCandidate(const OverloadCandidate &Candidate,
656 const CodeCompletionString &CCS) const {
657 SignatureInformation Result;
658 const char *ReturnType = nullptr;
659
660 Result.documentation = getDocumentation(CCS);
661
662 for (const auto &Chunk : CCS) {
663 switch (Chunk.Kind) {
664 case CodeCompletionString::CK_ResultType:
665 // A piece of text that describes the type of an entity or,
666 // for functions and methods, the return type.
667 assert(!ReturnType && "Unexpected CK_ResultType");
668 ReturnType = Chunk.Text;
669 break;
670 case CodeCompletionString::CK_Placeholder:
671 // A string that acts as a placeholder for, e.g., a function call
672 // argument.
673 // Intentional fallthrough here.
674 case CodeCompletionString::CK_CurrentParameter: {
675 // A piece of text that describes the parameter that corresponds to
676 // the code-completion location within a function call, message send,
677 // macro invocation, etc.
678 Result.label += Chunk.Text;
679 ParameterInformation Info;
680 Info.label = Chunk.Text;
681 Result.parameters.push_back(std::move(Info));
682 break;
683 }
684 case CodeCompletionString::CK_Optional: {
685 // The rest of the parameters are defaulted/optional.
686 assert(Chunk.Optional &&
687 "Expected the optional code completion string to be non-null.");
688 Result.label +=
689 getOptionalParameters(*Chunk.Optional, Result.parameters);
690 break;
691 }
692 case CodeCompletionString::CK_VerticalSpace:
693 break;
694 default:
695 Result.label += Chunk.Text;
696 break;
697 }
698 }
699 if (ReturnType) {
700 Result.label += " -> ";
701 Result.label += ReturnType;
702 }
703 return Result;
704 }
705
706 SignatureHelp &SigHelp;
707 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
708 CodeCompletionTUInfo CCTUInfo;
709
710}; // SignatureHelpCollector
711
712bool invokeCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000713 const clang::CodeCompleteOptions &Options,
714 PathRef FileName,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000715 const tooling::CompileCommand &Command,
716 PrecompiledPreamble const *Preamble, StringRef Contents,
717 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
718 std::shared_ptr<PCHContainerOperations> PCHs,
719 clangd::Logger &Logger) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000720 std::vector<const char *> ArgStrs;
721 for (const auto &S : Command.CommandLine)
722 ArgStrs.push_back(S.c_str());
723
Krasimir Georgieve4130d52017-07-25 11:37:43 +0000724 VFS->setCurrentWorkingDirectory(Command.Directory);
725
Ilya Biryukov04db3682017-07-21 13:29:29 +0000726 std::unique_ptr<CompilerInvocation> CI;
727 EmptyDiagsConsumer DummyDiagsConsumer;
728 {
729 IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
730 CompilerInstance::createDiagnostics(new DiagnosticOptions,
731 &DummyDiagsConsumer, false);
732 CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
733 }
734 assert(CI && "Couldn't create CompilerInvocation");
735
736 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
737 llvm::MemoryBuffer::getMemBufferCopy(Contents, FileName);
738
739 // Attempt to reuse the PCH from precompiled preamble, if it was built.
Ilya Biryukov04db3682017-07-21 13:29:29 +0000740 if (Preamble) {
741 auto Bounds =
742 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
Ilya Biryukov02d58702017-08-01 15:51:38 +0000743 if (!Preamble->CanReuse(*CI, ContentsBuffer.get(), Bounds, VFS.get()))
744 Preamble = nullptr;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000745 }
746
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000747 auto Clang = prepareCompilerInstance(
748 std::move(CI), Preamble, std::move(ContentsBuffer), std::move(PCHs),
749 std::move(VFS), DummyDiagsConsumer);
Ilya Biryukov04db3682017-07-21 13:29:29 +0000750 auto &DiagOpts = Clang->getDiagnosticOpts();
751 DiagOpts.IgnoreWarnings = true;
752
753 auto &FrontendOpts = Clang->getFrontendOpts();
754 FrontendOpts.SkipFunctionBodies = true;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000755 FrontendOpts.CodeCompleteOpts = Options;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000756 FrontendOpts.CodeCompletionAt.FileName = FileName;
757 FrontendOpts.CodeCompletionAt.Line = Pos.line + 1;
758 FrontendOpts.CodeCompletionAt.Column = Pos.character + 1;
759
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000760 Clang->setCodeCompletionConsumer(Consumer.release());
Ilya Biryukov38d79772017-05-16 09:38:59 +0000761
Ilya Biryukov04db3682017-07-21 13:29:29 +0000762 SyntaxOnlyAction Action;
763 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
Ilya Biryukove5128f72017-09-20 07:24:15 +0000764 Logger.log("BeginSourceFile() failed when running codeComplete for " +
765 FileName);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000766 return false;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000767 }
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000768 if (!Action.Execute()) {
Ilya Biryukove5128f72017-09-20 07:24:15 +0000769 Logger.log("Execute() failed when running codeComplete for " + FileName);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000770 return false;
771 }
Ilya Biryukove5128f72017-09-20 07:24:15 +0000772
Ilya Biryukov04db3682017-07-21 13:29:29 +0000773 Action.EndSourceFile();
Ilya Biryukov38d79772017-05-16 09:38:59 +0000774
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000775 return true;
776}
777
778} // namespace
779
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000780clangd::CodeCompleteOptions::CodeCompleteOptions(
781 bool EnableSnippetsAndCodePatterns)
782 : CodeCompleteOptions() {
783 EnableSnippets = EnableSnippetsAndCodePatterns;
784 IncludeCodePatterns = EnableSnippetsAndCodePatterns;
785}
786
787clangd::CodeCompleteOptions::CodeCompleteOptions(bool EnableSnippets,
788 bool IncludeCodePatterns,
789 bool IncludeMacros,
790 bool IncludeGlobals,
791 bool IncludeBriefComments)
792 : EnableSnippets(EnableSnippets), IncludeCodePatterns(IncludeCodePatterns),
793 IncludeMacros(IncludeMacros), IncludeGlobals(IncludeGlobals),
794 IncludeBriefComments(IncludeBriefComments) {}
795
796clang::CodeCompleteOptions clangd::CodeCompleteOptions::getClangCompleteOpts() {
797 clang::CodeCompleteOptions Result;
798 Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns;
799 Result.IncludeMacros = IncludeMacros;
800 Result.IncludeGlobals = IncludeGlobals;
801 Result.IncludeBriefComments = IncludeBriefComments;
802
803 return Result;
804}
805
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000806std::vector<CompletionItem>
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000807clangd::codeComplete(PathRef FileName, const tooling::CompileCommand &Command,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000808 PrecompiledPreamble const *Preamble, StringRef Contents,
809 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
810 std::shared_ptr<PCHContainerOperations> PCHs,
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000811 clangd::CodeCompleteOptions Opts, clangd::Logger &Logger) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000812 std::vector<CompletionItem> Results;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000813 std::unique_ptr<CodeCompleteConsumer> Consumer;
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000814 clang::CodeCompleteOptions ClangCompleteOpts = Opts.getClangCompleteOpts();
815 if (Opts.EnableSnippets) {
816 Consumer = llvm::make_unique<SnippetCompletionItemsCollector>(
817 ClangCompleteOpts, Results);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000818 } else {
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000819 Consumer = llvm::make_unique<PlainTextCompletionItemsCollector>(
820 ClangCompleteOpts, Results);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000821 }
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000822 invokeCodeComplete(std::move(Consumer), ClangCompleteOpts, FileName, Command,
823 Preamble, Contents, Pos, std::move(VFS), std::move(PCHs),
824 Logger);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000825 return Results;
826}
827
828SignatureHelp
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000829clangd::signatureHelp(PathRef FileName, const tooling::CompileCommand &Command,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000830 PrecompiledPreamble const *Preamble, StringRef Contents,
831 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
832 std::shared_ptr<PCHContainerOperations> PCHs,
833 clangd::Logger &Logger) {
834 SignatureHelp Result;
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000835 clang::CodeCompleteOptions Options;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000836 Options.IncludeGlobals = false;
837 Options.IncludeMacros = false;
838 Options.IncludeCodePatterns = false;
839 Options.IncludeBriefComments = true;
840 invokeCodeComplete(llvm::make_unique<SignatureHelpCollector>(Options, Result),
841 Options, FileName, Command, Preamble, Contents, Pos,
842 std::move(VFS), std::move(PCHs), Logger);
843 return Result;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000844}
845
Ilya Biryukov02d58702017-08-01 15:51:38 +0000846void clangd::dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) {
847 AST.getASTContext().getTranslationUnitDecl()->dump(OS, true);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000848}
Ilya Biryukovf01af682017-05-23 13:42:59 +0000849
Ilya Biryukov02d58702017-08-01 15:51:38 +0000850llvm::Optional<ParsedAST>
851ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI,
852 const PrecompiledPreamble *Preamble,
853 ArrayRef<serialization::DeclID> PreambleDeclIDs,
854 std::unique_ptr<llvm::MemoryBuffer> Buffer,
855 std::shared_ptr<PCHContainerOperations> PCHs,
Ilya Biryukove5128f72017-09-20 07:24:15 +0000856 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
857 clangd::Logger &Logger) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000858
859 std::vector<DiagWithFixIts> ASTDiags;
860 StoreDiagsConsumer UnitDiagsConsumer(/*ref*/ ASTDiags);
861
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000862 auto Clang = prepareCompilerInstance(
863 std::move(CI), Preamble, std::move(Buffer), std::move(PCHs),
864 std::move(VFS), /*ref*/ UnitDiagsConsumer);
Ilya Biryukov04db3682017-07-21 13:29:29 +0000865
866 // Recover resources if we crash before exiting this method.
867 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
868 Clang.get());
869
870 auto Action = llvm::make_unique<ClangdFrontendAction>();
Ilya Biryukove5128f72017-09-20 07:24:15 +0000871 const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
872 if (!Action->BeginSourceFile(*Clang, MainInput)) {
873 Logger.log("BeginSourceFile() failed when building AST for " +
874 MainInput.getFile());
Ilya Biryukov04db3682017-07-21 13:29:29 +0000875 return llvm::None;
876 }
Ilya Biryukove5128f72017-09-20 07:24:15 +0000877 if (!Action->Execute())
878 Logger.log("Execute() failed when building AST for " + MainInput.getFile());
Ilya Biryukov04db3682017-07-21 13:29:29 +0000879
880 // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
881 // has a longer lifetime.
882 Clang->getDiagnostics().setClient(new EmptyDiagsConsumer);
883
884 std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls();
885 std::vector<serialization::DeclID> PendingDecls;
886 if (Preamble) {
887 PendingDecls.reserve(PreambleDeclIDs.size());
888 PendingDecls.insert(PendingDecls.begin(), PreambleDeclIDs.begin(),
889 PreambleDeclIDs.end());
890 }
891
892 return ParsedAST(std::move(Clang), std::move(Action), std::move(ParsedDecls),
893 std::move(PendingDecls), std::move(ASTDiags));
894}
895
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000896namespace {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000897
898SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
899 const FileEntry *FE,
900 unsigned Offset) {
901 SourceLocation FileLoc = Mgr.translateFileLineCol(FE, 1, 1);
902 return Mgr.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
903}
904
905SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
906 const FileEntry *FE, Position Pos) {
907 SourceLocation InputLoc =
908 Mgr.translateFileLineCol(FE, Pos.line + 1, Pos.character + 1);
909 return Mgr.getMacroArgExpandedLocation(InputLoc);
910}
911
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000912/// Finds declarations locations that a given source location refers to.
913class DeclarationLocationsFinder : public index::IndexDataConsumer {
914 std::vector<Location> DeclarationLocations;
915 const SourceLocation &SearchedLocation;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000916 const ASTContext &AST;
917 Preprocessor &PP;
918
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000919public:
920 DeclarationLocationsFinder(raw_ostream &OS,
Ilya Biryukov04db3682017-07-21 13:29:29 +0000921 const SourceLocation &SearchedLocation,
922 ASTContext &AST, Preprocessor &PP)
923 : SearchedLocation(SearchedLocation), AST(AST), PP(PP) {}
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000924
925 std::vector<Location> takeLocations() {
926 // Don't keep the same location multiple times.
927 // This can happen when nodes in the AST are visited twice.
928 std::sort(DeclarationLocations.begin(), DeclarationLocations.end());
Kirill Bobyrev46213872017-06-28 20:57:28 +0000929 auto last =
930 std::unique(DeclarationLocations.begin(), DeclarationLocations.end());
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000931 DeclarationLocations.erase(last, DeclarationLocations.end());
932 return std::move(DeclarationLocations);
933 }
934
Ilya Biryukov02d58702017-08-01 15:51:38 +0000935 bool
936 handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
937 ArrayRef<index::SymbolRelation> Relations, FileID FID,
938 unsigned Offset,
939 index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000940 if (isSearchedLocation(FID, Offset)) {
941 addDeclarationLocation(D->getSourceRange());
942 }
943 return true;
944 }
945
946private:
947 bool isSearchedLocation(FileID FID, unsigned Offset) const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000948 const SourceManager &SourceMgr = AST.getSourceManager();
949 return SourceMgr.getFileOffset(SearchedLocation) == Offset &&
950 SourceMgr.getFileID(SearchedLocation) == FID;
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000951 }
952
Ilya Biryukov04db3682017-07-21 13:29:29 +0000953 void addDeclarationLocation(const SourceRange &ValSourceRange) {
954 const SourceManager &SourceMgr = AST.getSourceManager();
955 const LangOptions &LangOpts = AST.getLangOpts();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000956 SourceLocation LocStart = ValSourceRange.getBegin();
957 SourceLocation LocEnd = Lexer::getLocForEndOfToken(ValSourceRange.getEnd(),
Ilya Biryukov04db3682017-07-21 13:29:29 +0000958 0, SourceMgr, LangOpts);
Kirill Bobyrev46213872017-06-28 20:57:28 +0000959 Position Begin;
960 Begin.line = SourceMgr.getSpellingLineNumber(LocStart) - 1;
961 Begin.character = SourceMgr.getSpellingColumnNumber(LocStart) - 1;
962 Position End;
963 End.line = SourceMgr.getSpellingLineNumber(LocEnd) - 1;
964 End.character = SourceMgr.getSpellingColumnNumber(LocEnd) - 1;
965 Range R = {Begin, End};
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000966 Location L;
967 L.uri = URI::fromFile(
968 SourceMgr.getFilename(SourceMgr.getSpellingLoc(LocStart)));
969 L.range = R;
970 DeclarationLocations.push_back(L);
971 }
972
Kirill Bobyrev46213872017-06-28 20:57:28 +0000973 void finish() override {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000974 // Also handle possible macro at the searched location.
975 Token Result;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000976 if (!Lexer::getRawToken(SearchedLocation, Result, AST.getSourceManager(),
977 AST.getLangOpts(), false)) {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000978 if (Result.is(tok::raw_identifier)) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000979 PP.LookUpIdentifierInfo(Result);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000980 }
Ilya Biryukov04db3682017-07-21 13:29:29 +0000981 IdentifierInfo *IdentifierInfo = Result.getIdentifierInfo();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000982 if (IdentifierInfo && IdentifierInfo->hadMacroDefinition()) {
983 std::pair<FileID, unsigned int> DecLoc =
Ilya Biryukov04db3682017-07-21 13:29:29 +0000984 AST.getSourceManager().getDecomposedExpansionLoc(SearchedLocation);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000985 // Get the definition just before the searched location so that a macro
986 // referenced in a '#undef MACRO' can still be found.
Ilya Biryukov04db3682017-07-21 13:29:29 +0000987 SourceLocation BeforeSearchedLocation = getMacroArgExpandedLocation(
988 AST.getSourceManager(),
989 AST.getSourceManager().getFileEntryForID(DecLoc.first),
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000990 DecLoc.second - 1);
991 MacroDefinition MacroDef =
Ilya Biryukov04db3682017-07-21 13:29:29 +0000992 PP.getMacroDefinitionAtLoc(IdentifierInfo, BeforeSearchedLocation);
993 MacroInfo *MacroInf = MacroDef.getMacroInfo();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000994 if (MacroInf) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000995 addDeclarationLocation(SourceRange(MacroInf->getDefinitionLoc(),
996 MacroInf->getDefinitionEndLoc()));
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000997 }
998 }
999 }
1000 }
1001};
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001002
Ilya Biryukov02d58702017-08-01 15:51:38 +00001003SourceLocation getBeginningOfIdentifier(ParsedAST &Unit, const Position &Pos,
1004 const FileEntry *FE) {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001005 // The language server protocol uses zero-based line and column numbers.
1006 // Clang uses one-based numbers.
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001007
Ilya Biryukov02d58702017-08-01 15:51:38 +00001008 const ASTContext &AST = Unit.getASTContext();
Ilya Biryukov04db3682017-07-21 13:29:29 +00001009 const SourceManager &SourceMgr = AST.getSourceManager();
1010
1011 SourceLocation InputLocation =
1012 getMacroArgExpandedLocation(SourceMgr, FE, Pos);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001013 if (Pos.character == 0) {
1014 return InputLocation;
1015 }
1016
1017 // This handle cases where the position is in the middle of a token or right
1018 // after the end of a token. In theory we could just use GetBeginningOfToken
1019 // to find the start of the token at the input position, but this doesn't
1020 // work when right after the end, i.e. foo|.
1021 // So try to go back by one and see if we're still inside the an identifier
1022 // token. If so, Take the beginning of this token.
1023 // (It should be the same identifier because you can't have two adjacent
1024 // identifiers without another token in between.)
Ilya Biryukov04db3682017-07-21 13:29:29 +00001025 SourceLocation PeekBeforeLocation = getMacroArgExpandedLocation(
1026 SourceMgr, FE, Position{Pos.line, Pos.character - 1});
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001027 Token Result;
Ilya Biryukov4203d2a2017-06-29 17:11:32 +00001028 if (Lexer::getRawToken(PeekBeforeLocation, Result, SourceMgr,
Ilya Biryukov04db3682017-07-21 13:29:29 +00001029 AST.getLangOpts(), false)) {
Ilya Biryukov4203d2a2017-06-29 17:11:32 +00001030 // getRawToken failed, just use InputLocation.
1031 return InputLocation;
1032 }
1033
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001034 if (Result.is(tok::raw_identifier)) {
1035 return Lexer::GetBeginningOfToken(PeekBeforeLocation, SourceMgr,
Ilya Biryukov02d58702017-08-01 15:51:38 +00001036 AST.getLangOpts());
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001037 }
1038
1039 return InputLocation;
1040}
Ilya Biryukov02d58702017-08-01 15:51:38 +00001041} // namespace
Ilya Biryukov04db3682017-07-21 13:29:29 +00001042
Ilya Biryukove5128f72017-09-20 07:24:15 +00001043std::vector<Location> clangd::findDefinitions(ParsedAST &AST, Position Pos,
1044 clangd::Logger &Logger) {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001045 const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
1046 const FileEntry *FE = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
1047 if (!FE)
1048 return {};
1049
1050 SourceLocation SourceLocationBeg = getBeginningOfIdentifier(AST, Pos, FE);
1051
1052 auto DeclLocationsFinder = std::make_shared<DeclarationLocationsFinder>(
1053 llvm::errs(), SourceLocationBeg, AST.getASTContext(),
1054 AST.getPreprocessor());
1055 index::IndexingOptions IndexOpts;
1056 IndexOpts.SystemSymbolFilter =
1057 index::IndexingOptions::SystemSymbolFilterKind::All;
1058 IndexOpts.IndexFunctionLocals = true;
1059
1060 indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(),
1061 DeclLocationsFinder, IndexOpts);
1062
1063 return DeclLocationsFinder->takeLocations();
1064}
1065
1066void ParsedAST::ensurePreambleDeclsDeserialized() {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001067 if (PendingTopLevelDecls.empty())
1068 return;
1069
1070 std::vector<const Decl *> Resolved;
1071 Resolved.reserve(PendingTopLevelDecls.size());
1072
1073 ExternalASTSource &Source = *getASTContext().getExternalSource();
1074 for (serialization::DeclID TopLevelDecl : PendingTopLevelDecls) {
1075 // Resolve the declaration ID to an actual declaration, possibly
1076 // deserializing the declaration in the process.
1077 if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
1078 Resolved.push_back(D);
1079 }
1080
1081 TopLevelDecls.reserve(TopLevelDecls.size() + PendingTopLevelDecls.size());
1082 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1083
1084 PendingTopLevelDecls.clear();
1085}
1086
Ilya Biryukov02d58702017-08-01 15:51:38 +00001087ParsedAST::ParsedAST(ParsedAST &&Other) = default;
Ilya Biryukov04db3682017-07-21 13:29:29 +00001088
Ilya Biryukov02d58702017-08-01 15:51:38 +00001089ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
Ilya Biryukov04db3682017-07-21 13:29:29 +00001090
Ilya Biryukov02d58702017-08-01 15:51:38 +00001091ParsedAST::~ParsedAST() {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001092 if (Action) {
1093 Action->EndSourceFile();
1094 }
1095}
1096
Ilya Biryukov02d58702017-08-01 15:51:38 +00001097ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
1098
1099const ASTContext &ParsedAST::getASTContext() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001100 return Clang->getASTContext();
1101}
1102
Ilya Biryukov02d58702017-08-01 15:51:38 +00001103Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
Ilya Biryukov04db3682017-07-21 13:29:29 +00001104
Ilya Biryukov02d58702017-08-01 15:51:38 +00001105const Preprocessor &ParsedAST::getPreprocessor() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001106 return Clang->getPreprocessor();
1107}
1108
Ilya Biryukov02d58702017-08-01 15:51:38 +00001109ArrayRef<const Decl *> ParsedAST::getTopLevelDecls() {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001110 ensurePreambleDeclsDeserialized();
1111 return TopLevelDecls;
1112}
1113
Ilya Biryukov02d58702017-08-01 15:51:38 +00001114const std::vector<DiagWithFixIts> &ParsedAST::getDiagnostics() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001115 return Diags;
1116}
1117
Ilya Biryukov02d58702017-08-01 15:51:38 +00001118ParsedAST::ParsedAST(std::unique_ptr<CompilerInstance> Clang,
1119 std::unique_ptr<FrontendAction> Action,
1120 std::vector<const Decl *> TopLevelDecls,
1121 std::vector<serialization::DeclID> PendingTopLevelDecls,
1122 std::vector<DiagWithFixIts> Diags)
Ilya Biryukov04db3682017-07-21 13:29:29 +00001123 : Clang(std::move(Clang)), Action(std::move(Action)),
1124 Diags(std::move(Diags)), TopLevelDecls(std::move(TopLevelDecls)),
1125 PendingTopLevelDecls(std::move(PendingTopLevelDecls)) {
1126 assert(this->Clang);
1127 assert(this->Action);
1128}
1129
Ilya Biryukov02d58702017-08-01 15:51:38 +00001130ParsedASTWrapper::ParsedASTWrapper(ParsedASTWrapper &&Wrapper)
1131 : AST(std::move(Wrapper.AST)) {}
1132
1133ParsedASTWrapper::ParsedASTWrapper(llvm::Optional<ParsedAST> AST)
1134 : AST(std::move(AST)) {}
1135
1136PreambleData::PreambleData(PrecompiledPreamble Preamble,
1137 std::vector<serialization::DeclID> TopLevelDeclIDs,
1138 std::vector<DiagWithFixIts> Diags)
Ilya Biryukov04db3682017-07-21 13:29:29 +00001139 : Preamble(std::move(Preamble)),
1140 TopLevelDeclIDs(std::move(TopLevelDeclIDs)), Diags(std::move(Diags)) {}
Ilya Biryukov02d58702017-08-01 15:51:38 +00001141
1142std::shared_ptr<CppFile>
1143CppFile::Create(PathRef FileName, tooling::CompileCommand Command,
Ilya Biryukov83ca8a22017-09-20 10:46:58 +00001144 std::shared_ptr<PCHContainerOperations> PCHs,
1145 clangd::Logger &Logger) {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001146 return std::shared_ptr<CppFile>(
Ilya Biryukove5128f72017-09-20 07:24:15 +00001147 new CppFile(FileName, std::move(Command), std::move(PCHs), Logger));
Ilya Biryukov02d58702017-08-01 15:51:38 +00001148}
1149
1150CppFile::CppFile(PathRef FileName, tooling::CompileCommand Command,
Ilya Biryukove5128f72017-09-20 07:24:15 +00001151 std::shared_ptr<PCHContainerOperations> PCHs,
1152 clangd::Logger &Logger)
Ilya Biryukov02d58702017-08-01 15:51:38 +00001153 : FileName(FileName), Command(std::move(Command)), RebuildCounter(0),
Ilya Biryukove5128f72017-09-20 07:24:15 +00001154 RebuildInProgress(false), PCHs(std::move(PCHs)), Logger(Logger) {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001155
1156 std::lock_guard<std::mutex> Lock(Mutex);
1157 LatestAvailablePreamble = nullptr;
1158 PreamblePromise.set_value(nullptr);
1159 PreambleFuture = PreamblePromise.get_future();
1160
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001161 ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
Ilya Biryukov02d58702017-08-01 15:51:38 +00001162 ASTFuture = ASTPromise.get_future();
1163}
1164
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001165void CppFile::cancelRebuild() { deferCancelRebuild()(); }
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001166
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001167UniqueFunction<void()> CppFile::deferCancelRebuild() {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001168 std::unique_lock<std::mutex> Lock(Mutex);
1169 // Cancel an ongoing rebuild, if any, and wait for it to finish.
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001170 unsigned RequestRebuildCounter = ++this->RebuildCounter;
Ilya Biryukov02d58702017-08-01 15:51:38 +00001171 // Rebuild asserts that futures aren't ready if rebuild is cancelled.
1172 // We want to keep this invariant.
1173 if (futureIsReady(PreambleFuture)) {
1174 PreamblePromise = std::promise<std::shared_ptr<const PreambleData>>();
1175 PreambleFuture = PreamblePromise.get_future();
1176 }
1177 if (futureIsReady(ASTFuture)) {
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001178 ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001179 ASTFuture = ASTPromise.get_future();
1180 }
Ilya Biryukov02d58702017-08-01 15:51:38 +00001181
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001182 Lock.unlock();
1183 // Notify about changes to RebuildCounter.
1184 RebuildCond.notify_all();
1185
1186 std::shared_ptr<CppFile> That = shared_from_this();
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001187 return [That, RequestRebuildCounter]() {
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001188 std::unique_lock<std::mutex> Lock(That->Mutex);
1189 CppFile *This = &*That;
1190 This->RebuildCond.wait(Lock, [This, RequestRebuildCounter]() {
1191 return !This->RebuildInProgress ||
1192 This->RebuildCounter != RequestRebuildCounter;
1193 });
1194
1195 // This computation got cancelled itself, do nothing.
1196 if (This->RebuildCounter != RequestRebuildCounter)
1197 return;
1198
1199 // Set empty results for Promises.
1200 That->PreamblePromise.set_value(nullptr);
1201 That->ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001202 };
Ilya Biryukov02d58702017-08-01 15:51:38 +00001203}
1204
1205llvm::Optional<std::vector<DiagWithFixIts>>
1206CppFile::rebuild(StringRef NewContents,
1207 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001208 return deferRebuild(NewContents, std::move(VFS))();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001209}
1210
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001211UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>()>
Ilya Biryukov02d58702017-08-01 15:51:38 +00001212CppFile::deferRebuild(StringRef NewContents,
1213 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
1214 std::shared_ptr<const PreambleData> OldPreamble;
1215 std::shared_ptr<PCHContainerOperations> PCHs;
1216 unsigned RequestRebuildCounter;
1217 {
1218 std::unique_lock<std::mutex> Lock(Mutex);
1219 // Increase RebuildCounter to cancel all ongoing FinishRebuild operations.
1220 // They will try to exit as early as possible and won't call set_value on
1221 // our promises.
1222 RequestRebuildCounter = ++this->RebuildCounter;
1223 PCHs = this->PCHs;
1224
1225 // Remember the preamble to be used during rebuild.
1226 OldPreamble = this->LatestAvailablePreamble;
1227 // Setup std::promises and std::futures for Preamble and AST. Corresponding
1228 // futures will wait until the rebuild process is finished.
1229 if (futureIsReady(this->PreambleFuture)) {
1230 this->PreamblePromise =
1231 std::promise<std::shared_ptr<const PreambleData>>();
1232 this->PreambleFuture = this->PreamblePromise.get_future();
1233 }
1234 if (futureIsReady(this->ASTFuture)) {
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001235 this->ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001236 this->ASTFuture = this->ASTPromise.get_future();
1237 }
1238 } // unlock Mutex.
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001239 // Notify about changes to RebuildCounter.
1240 RebuildCond.notify_all();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001241
1242 // A helper to function to finish the rebuild. May be run on a different
1243 // thread.
1244
1245 // Don't let this CppFile die before rebuild is finished.
1246 std::shared_ptr<CppFile> That = shared_from_this();
1247 auto FinishRebuild = [OldPreamble, VFS, RequestRebuildCounter, PCHs,
1248 That](std::string NewContents)
1249 -> llvm::Optional<std::vector<DiagWithFixIts>> {
1250 // Only one execution of this method is possible at a time.
1251 // RebuildGuard will wait for any ongoing rebuilds to finish and will put us
1252 // into a state for doing a rebuild.
1253 RebuildGuard Rebuild(*That, RequestRebuildCounter);
1254 if (Rebuild.wasCancelledBeforeConstruction())
1255 return llvm::None;
1256
1257 std::vector<const char *> ArgStrs;
1258 for (const auto &S : That->Command.CommandLine)
1259 ArgStrs.push_back(S.c_str());
1260
1261 VFS->setCurrentWorkingDirectory(That->Command.Directory);
1262
1263 std::unique_ptr<CompilerInvocation> CI;
1264 {
1265 // FIXME(ibiryukov): store diagnostics from CommandLine when we start
1266 // reporting them.
1267 EmptyDiagsConsumer CommandLineDiagsConsumer;
1268 IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
1269 CompilerInstance::createDiagnostics(new DiagnosticOptions,
1270 &CommandLineDiagsConsumer, false);
1271 CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
1272 }
1273 assert(CI && "Couldn't create CompilerInvocation");
1274
1275 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1276 llvm::MemoryBuffer::getMemBufferCopy(NewContents, That->FileName);
1277
1278 // A helper function to rebuild the preamble or reuse the existing one. Does
1279 // not mutate any fields, only does the actual computation.
1280 auto DoRebuildPreamble = [&]() -> std::shared_ptr<const PreambleData> {
1281 auto Bounds =
1282 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
1283 if (OldPreamble && OldPreamble->Preamble.CanReuse(
1284 *CI, ContentsBuffer.get(), Bounds, VFS.get())) {
1285 return OldPreamble;
1286 }
1287
1288 std::vector<DiagWithFixIts> PreambleDiags;
1289 StoreDiagsConsumer PreambleDiagnosticsConsumer(/*ref*/ PreambleDiags);
1290 IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
1291 CompilerInstance::createDiagnostics(
1292 &CI->getDiagnosticOpts(), &PreambleDiagnosticsConsumer, false);
1293 CppFilePreambleCallbacks SerializedDeclsCollector;
1294 auto BuiltPreamble = PrecompiledPreamble::Build(
1295 *CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine, VFS, PCHs,
1296 SerializedDeclsCollector);
1297
1298 if (BuiltPreamble) {
1299 return std::make_shared<PreambleData>(
1300 std::move(*BuiltPreamble),
1301 SerializedDeclsCollector.takeTopLevelDeclIDs(),
1302 std::move(PreambleDiags));
1303 } else {
1304 return nullptr;
1305 }
1306 };
1307
1308 // Compute updated Preamble.
1309 std::shared_ptr<const PreambleData> NewPreamble = DoRebuildPreamble();
1310 // Publish the new Preamble.
1311 {
1312 std::lock_guard<std::mutex> Lock(That->Mutex);
1313 // We always set LatestAvailablePreamble to the new value, hoping that it
1314 // will still be usable in the further requests.
1315 That->LatestAvailablePreamble = NewPreamble;
1316 if (RequestRebuildCounter != That->RebuildCounter)
1317 return llvm::None; // Our rebuild request was cancelled, do nothing.
1318 That->PreamblePromise.set_value(NewPreamble);
1319 } // unlock Mutex
1320
1321 // Prepare the Preamble and supplementary data for rebuilding AST.
1322 const PrecompiledPreamble *PreambleForAST = nullptr;
1323 ArrayRef<serialization::DeclID> SerializedPreambleDecls = llvm::None;
1324 std::vector<DiagWithFixIts> Diagnostics;
1325 if (NewPreamble) {
1326 PreambleForAST = &NewPreamble->Preamble;
1327 SerializedPreambleDecls = NewPreamble->TopLevelDeclIDs;
1328 Diagnostics.insert(Diagnostics.begin(), NewPreamble->Diags.begin(),
1329 NewPreamble->Diags.end());
1330 }
1331
1332 // Compute updated AST.
1333 llvm::Optional<ParsedAST> NewAST =
1334 ParsedAST::Build(std::move(CI), PreambleForAST, SerializedPreambleDecls,
Ilya Biryukove5128f72017-09-20 07:24:15 +00001335 std::move(ContentsBuffer), PCHs, VFS, That->Logger);
Ilya Biryukov02d58702017-08-01 15:51:38 +00001336
1337 if (NewAST) {
1338 Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(),
1339 NewAST->getDiagnostics().end());
1340 } else {
1341 // Don't report even Preamble diagnostics if we coulnd't build AST.
1342 Diagnostics.clear();
1343 }
1344
1345 // Publish the new AST.
1346 {
1347 std::lock_guard<std::mutex> Lock(That->Mutex);
1348 if (RequestRebuildCounter != That->RebuildCounter)
1349 return Diagnostics; // Our rebuild request was cancelled, don't set
1350 // ASTPromise.
1351
Ilya Biryukov574b7532017-08-02 09:08:39 +00001352 That->ASTPromise.set_value(
1353 std::make_shared<ParsedASTWrapper>(std::move(NewAST)));
Ilya Biryukov02d58702017-08-01 15:51:38 +00001354 } // unlock Mutex
1355
1356 return Diagnostics;
1357 };
1358
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001359 return BindWithForward(FinishRebuild, NewContents.str());
Ilya Biryukov02d58702017-08-01 15:51:38 +00001360}
1361
1362std::shared_future<std::shared_ptr<const PreambleData>>
1363CppFile::getPreamble() const {
1364 std::lock_guard<std::mutex> Lock(Mutex);
1365 return PreambleFuture;
1366}
1367
1368std::shared_ptr<const PreambleData> CppFile::getPossiblyStalePreamble() const {
1369 std::lock_guard<std::mutex> Lock(Mutex);
1370 return LatestAvailablePreamble;
1371}
1372
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001373std::shared_future<std::shared_ptr<ParsedASTWrapper>> CppFile::getAST() const {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001374 std::lock_guard<std::mutex> Lock(Mutex);
1375 return ASTFuture;
1376}
1377
1378tooling::CompileCommand const &CppFile::getCompileCommand() const {
1379 return Command;
1380}
1381
1382CppFile::RebuildGuard::RebuildGuard(CppFile &File,
1383 unsigned RequestRebuildCounter)
1384 : File(File), RequestRebuildCounter(RequestRebuildCounter) {
1385 std::unique_lock<std::mutex> Lock(File.Mutex);
1386 WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
1387 if (WasCancelledBeforeConstruction)
1388 return;
1389
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001390 File.RebuildCond.wait(Lock, [&File, RequestRebuildCounter]() {
1391 return !File.RebuildInProgress ||
1392 File.RebuildCounter != RequestRebuildCounter;
1393 });
Ilya Biryukov02d58702017-08-01 15:51:38 +00001394
1395 WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
1396 if (WasCancelledBeforeConstruction)
1397 return;
1398
1399 File.RebuildInProgress = true;
1400}
1401
1402bool CppFile::RebuildGuard::wasCancelledBeforeConstruction() const {
1403 return WasCancelledBeforeConstruction;
1404}
1405
1406CppFile::RebuildGuard::~RebuildGuard() {
1407 if (WasCancelledBeforeConstruction)
1408 return;
1409
1410 std::unique_lock<std::mutex> Lock(File.Mutex);
1411 assert(File.RebuildInProgress);
1412 File.RebuildInProgress = false;
1413
1414 if (File.RebuildCounter == RequestRebuildCounter) {
1415 // Our rebuild request was successful.
1416 assert(futureIsReady(File.ASTFuture));
1417 assert(futureIsReady(File.PreambleFuture));
1418 } else {
1419 // Our rebuild request was cancelled, because further reparse was requested.
1420 assert(!futureIsReady(File.ASTFuture));
1421 assert(!futureIsReady(File.PreambleFuture));
1422 }
1423
1424 Lock.unlock();
1425 File.RebuildCond.notify_all();
1426}