blob: 3b2ca91a06e9124a1f60f995d4148fd4a06764c5 [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
122llvm::Optional<DiagWithFixIts> toClangdDiag(StoredDiagnostic D) {
123 auto Location = D.getLocation();
124 if (!Location.isValid() || !Location.getManager().isInMainFile(Location))
125 return llvm::None;
126
127 Position P;
128 P.line = Location.getSpellingLineNumber() - 1;
129 P.character = Location.getSpellingColumnNumber();
130 Range R = {P, P};
131 clangd::Diagnostic Diag = {R, getSeverity(D.getLevel()), D.getMessage()};
132
133 llvm::SmallVector<tooling::Replacement, 1> FixItsForDiagnostic;
134 for (const FixItHint &Fix : D.getFixIts()) {
135 FixItsForDiagnostic.push_back(clang::tooling::Replacement(
136 Location.getManager(), Fix.RemoveRange, Fix.CodeToInsert));
137 }
138 return DiagWithFixIts{Diag, std::move(FixItsForDiagnostic)};
139}
140
141class StoreDiagsConsumer : public DiagnosticConsumer {
142public:
143 StoreDiagsConsumer(std::vector<DiagWithFixIts> &Output) : Output(Output) {}
144
145 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
146 const clang::Diagnostic &Info) override {
147 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
148
149 if (auto convertedDiag = toClangdDiag(StoredDiagnostic(DiagLevel, Info)))
150 Output.push_back(std::move(*convertedDiag));
151 }
152
153private:
154 std::vector<DiagWithFixIts> &Output;
155};
156
157class EmptyDiagsConsumer : public DiagnosticConsumer {
158public:
159 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
160 const clang::Diagnostic &Info) override {}
161};
162
163std::unique_ptr<CompilerInvocation>
164createCompilerInvocation(ArrayRef<const char *> ArgList,
165 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
166 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
167 auto CI = createInvocationFromCommandLine(ArgList, std::move(Diags),
168 std::move(VFS));
169 // We rely on CompilerInstance to manage the resource (i.e. free them on
170 // EndSourceFile), but that won't happen if DisableFree is set to true.
171 // Since createInvocationFromCommandLine sets it to true, we have to override
172 // it.
173 CI->getFrontendOpts().DisableFree = false;
174 return CI;
175}
176
177/// Creates a CompilerInstance from \p CI, with main buffer overriden to \p
178/// Buffer and arguments to read the PCH from \p Preamble, if \p Preamble is not
179/// null. Note that vfs::FileSystem inside returned instance may differ from \p
180/// VFS if additional file remapping were set in command-line arguments.
181/// On some errors, returns null. When non-null value is returned, it's expected
182/// to be consumed by the FrontendAction as it will have a pointer to the \p
183/// Buffer that will only be deleted if BeginSourceFile is called.
184std::unique_ptr<CompilerInstance>
185prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
186 const PrecompiledPreamble *Preamble,
187 std::unique_ptr<llvm::MemoryBuffer> Buffer,
188 std::shared_ptr<PCHContainerOperations> PCHs,
189 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
190 DiagnosticConsumer &DiagsClient) {
191 assert(VFS && "VFS is null");
192 assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
193 "Setting RetainRemappedFileBuffers to true will cause a memory leak "
194 "of ContentsBuffer");
195
196 // NOTE: we use Buffer.get() when adding remapped files, so we have to make
197 // sure it will be released if no error is emitted.
198 if (Preamble) {
199 Preamble->AddImplicitPreamble(*CI, Buffer.get());
200 } else {
201 CI->getPreprocessorOpts().addRemappedFile(
202 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
203 }
204
205 auto Clang = llvm::make_unique<CompilerInstance>(PCHs);
206 Clang->setInvocation(std::move(CI));
207 Clang->createDiagnostics(&DiagsClient, false);
208
209 if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
210 Clang->getInvocation(), Clang->getDiagnostics(), VFS))
211 VFS = VFSWithRemapping;
212 Clang->setVirtualFileSystem(VFS);
213
214 Clang->setTarget(TargetInfo::CreateTargetInfo(
215 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
216 if (!Clang->hasTarget())
217 return nullptr;
218
219 // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
220 // release it.
221 Buffer.release();
222 return Clang;
223}
224
Ilya Biryukov02d58702017-08-01 15:51:38 +0000225template <class T> bool futureIsReady(std::shared_future<T> const &Future) {
226 return Future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
227}
228
Ilya Biryukov04db3682017-07-21 13:29:29 +0000229} // namespace
230
Ilya Biryukov38d79772017-05-16 09:38:59 +0000231namespace {
232
233CompletionItemKind getKind(CXCursorKind K) {
234 switch (K) {
235 case CXCursor_MacroInstantiation:
236 case CXCursor_MacroDefinition:
237 return CompletionItemKind::Text;
238 case CXCursor_CXXMethod:
239 return CompletionItemKind::Method;
240 case CXCursor_FunctionDecl:
241 case CXCursor_FunctionTemplate:
242 return CompletionItemKind::Function;
243 case CXCursor_Constructor:
244 case CXCursor_Destructor:
245 return CompletionItemKind::Constructor;
246 case CXCursor_FieldDecl:
247 return CompletionItemKind::Field;
248 case CXCursor_VarDecl:
249 case CXCursor_ParmDecl:
250 return CompletionItemKind::Variable;
251 case CXCursor_ClassDecl:
252 case CXCursor_StructDecl:
253 case CXCursor_UnionDecl:
254 case CXCursor_ClassTemplate:
255 case CXCursor_ClassTemplatePartialSpecialization:
256 return CompletionItemKind::Class;
257 case CXCursor_Namespace:
258 case CXCursor_NamespaceAlias:
259 case CXCursor_NamespaceRef:
260 return CompletionItemKind::Module;
261 case CXCursor_EnumConstantDecl:
262 return CompletionItemKind::Value;
263 case CXCursor_EnumDecl:
264 return CompletionItemKind::Enum;
265 case CXCursor_TypeAliasDecl:
266 case CXCursor_TypeAliasTemplateDecl:
267 case CXCursor_TypedefDecl:
268 case CXCursor_MemberRef:
269 case CXCursor_TypeRef:
270 return CompletionItemKind::Reference;
271 default:
272 return CompletionItemKind::Missing;
273 }
274}
275
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000276std::string escapeSnippet(const llvm::StringRef Text) {
277 std::string Result;
278 Result.reserve(Text.size()); // Assume '$', '}' and '\\' are rare.
279 for (const auto Character : Text) {
280 if (Character == '$' || Character == '}' || Character == '\\')
281 Result.push_back('\\');
282 Result.push_back(Character);
283 }
284 return Result;
285}
286
Ilya Biryukov38d79772017-05-16 09:38:59 +0000287class CompletionItemsCollector : public CodeCompleteConsumer {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000288
289public:
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000290 CompletionItemsCollector(const CodeCompleteOptions &CodeCompleteOpts,
291 std::vector<CompletionItem> &Items)
Ilya Biryukov38d79772017-05-16 09:38:59 +0000292 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
293 Items(Items),
294 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
295 CCTUInfo(Allocator) {}
296
297 void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
298 CodeCompletionResult *Results,
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000299 unsigned NumResults) override final {
300 Items.reserve(NumResults);
301 for (unsigned I = 0; I < NumResults; ++I) {
302 auto &Result = Results[I];
303 const auto *CCS = Result.CreateCodeCompletionString(
Ilya Biryukov38d79772017-05-16 09:38:59 +0000304 S, Context, *Allocator, CCTUInfo,
305 CodeCompleteOpts.IncludeBriefComments);
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000306 assert(CCS && "Expected the CodeCompletionString to be non-null");
307 Items.push_back(ProcessCodeCompleteResult(Result, *CCS));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000308 }
309 }
310
311 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
312
313 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000314
315private:
316 CompletionItem
317 ProcessCodeCompleteResult(const CodeCompletionResult &Result,
318 const CodeCompletionString &CCS) const {
319
320 // Adjust this to InsertTextFormat::Snippet iff we encounter a
321 // CK_Placeholder chunk in SnippetCompletionItemsCollector.
322 CompletionItem Item;
323 Item.insertTextFormat = InsertTextFormat::PlainText;
324
325 FillDocumentation(CCS, Item);
326
327 // Fill in the label, detail, insertText and filterText fields of the
328 // CompletionItem.
329 ProcessChunks(CCS, Item);
330
331 // Fill in the kind field of the CompletionItem.
332 Item.kind = getKind(Result.CursorKind);
333
334 FillSortText(CCS, Item);
335
336 return Item;
337 }
338
339 virtual void ProcessChunks(const CodeCompletionString &CCS,
340 CompletionItem &Item) const = 0;
341
342 void FillDocumentation(const CodeCompletionString &CCS,
343 CompletionItem &Item) const {
344 // Things like __attribute__((nonnull(1,3))) and [[noreturn]]. Present this
345 // information in the documentation field.
346 const unsigned AnnotationCount = CCS.getAnnotationCount();
347 if (AnnotationCount > 0) {
348 Item.documentation += "Annotation";
349 if (AnnotationCount == 1) {
350 Item.documentation += ": ";
351 } else /* AnnotationCount > 1 */ {
352 Item.documentation += "s: ";
353 }
354 for (unsigned I = 0; I < AnnotationCount; ++I) {
355 Item.documentation += CCS.getAnnotation(I);
356 Item.documentation.push_back(I == AnnotationCount - 1 ? '\n' : ' ');
357 }
358 }
359
360 // Add brief documentation (if there is any).
361 if (CCS.getBriefComment() != nullptr) {
362 if (!Item.documentation.empty()) {
363 // This means we previously added annotations. Add an extra newline
364 // character to make the annotations stand out.
365 Item.documentation.push_back('\n');
366 }
367 Item.documentation += CCS.getBriefComment();
368 }
369 }
370
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000371 static int GetSortPriority(const CodeCompletionString &CCS) {
372 int Score = CCS.getPriority();
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000373 // Fill in the sortText of the CompletionItem.
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000374 assert(Score <= 99999 && "Expecting code completion result "
375 "priority to have at most 5-digits");
376
377 const int Penalty = 100000;
378 switch (static_cast<CXAvailabilityKind>(CCS.getAvailability())) {
379 case CXAvailability_Available:
380 // No penalty.
381 break;
382 case CXAvailability_Deprecated:
383 Score += Penalty;
384 break;
385 case CXAvailability_NotAccessible:
386 Score += 2 * Penalty;
387 break;
388 case CXAvailability_NotAvailable:
389 Score += 3 * Penalty;
390 break;
391 }
392
393 return Score;
394 }
395
396 static void FillSortText(const CodeCompletionString &CCS,
397 CompletionItem &Item) {
398 int Priority = GetSortPriority(CCS);
399 // Fill in the sortText of the CompletionItem.
400 assert(Priority <= 999999 &&
401 "Expecting sort priority to have at most 6-digits");
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000402 llvm::raw_string_ostream(Item.sortText)
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000403 << llvm::format("%06d%s", Priority, Item.filterText.c_str());
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000404 }
405
406 std::vector<CompletionItem> &Items;
407 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
408 CodeCompletionTUInfo CCTUInfo;
409
410}; // CompletionItemsCollector
411
Ilya Biryukov686ff9a2017-09-28 18:39:59 +0000412bool isInformativeQualifierChunk(CodeCompletionString::Chunk const &Chunk) {
413 return Chunk.Kind == CodeCompletionString::CK_Informative &&
414 StringRef(Chunk.Text).endswith("::");
415}
416
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000417class PlainTextCompletionItemsCollector final
418 : public CompletionItemsCollector {
419
420public:
421 PlainTextCompletionItemsCollector(const CodeCompleteOptions &CodeCompleteOpts,
422 std::vector<CompletionItem> &Items)
423 : CompletionItemsCollector(CodeCompleteOpts, Items) {}
424
425private:
426 void ProcessChunks(const CodeCompletionString &CCS,
427 CompletionItem &Item) const override {
428 for (const auto &Chunk : CCS) {
Ilya Biryukov686ff9a2017-09-28 18:39:59 +0000429 // Informative qualifier chunks only clutter completion results, skip
430 // them.
431 if (isInformativeQualifierChunk(Chunk))
432 continue;
433
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000434 switch (Chunk.Kind) {
435 case CodeCompletionString::CK_TypedText:
436 // There's always exactly one CK_TypedText chunk.
437 Item.insertText = Item.filterText = Chunk.Text;
438 Item.label += Chunk.Text;
439 break;
440 case CodeCompletionString::CK_ResultType:
441 assert(Item.detail.empty() && "Unexpected extraneous CK_ResultType");
442 Item.detail = Chunk.Text;
443 break;
444 case CodeCompletionString::CK_Optional:
445 break;
446 default:
447 Item.label += Chunk.Text;
448 break;
449 }
450 }
451 }
452}; // PlainTextCompletionItemsCollector
453
454class SnippetCompletionItemsCollector final : public CompletionItemsCollector {
455
456public:
457 SnippetCompletionItemsCollector(const CodeCompleteOptions &CodeCompleteOpts,
458 std::vector<CompletionItem> &Items)
459 : CompletionItemsCollector(CodeCompleteOpts, Items) {}
460
461private:
462 void ProcessChunks(const CodeCompletionString &CCS,
463 CompletionItem &Item) const override {
464 unsigned ArgCount = 0;
465 for (const auto &Chunk : CCS) {
Ilya Biryukov686ff9a2017-09-28 18:39:59 +0000466 // Informative qualifier chunks only clutter completion results, skip
467 // them.
468 if (isInformativeQualifierChunk(Chunk))
469 continue;
470
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000471 switch (Chunk.Kind) {
472 case CodeCompletionString::CK_TypedText:
473 // The piece of text that the user is expected to type to match
474 // the code-completion string, typically a keyword or the name of
475 // a declarator or macro.
476 Item.filterText = Chunk.Text;
477 // Note intentional fallthrough here.
478 case CodeCompletionString::CK_Text:
479 // A piece of text that should be placed in the buffer,
480 // e.g., parentheses or a comma in a function call.
481 Item.label += Chunk.Text;
482 Item.insertText += Chunk.Text;
483 break;
484 case CodeCompletionString::CK_Optional:
485 // A code completion string that is entirely optional.
486 // For example, an optional code completion string that
487 // describes the default arguments in a function call.
488
489 // FIXME: Maybe add an option to allow presenting the optional chunks?
490 break;
491 case CodeCompletionString::CK_Placeholder:
492 // A string that acts as a placeholder for, e.g., a function call
493 // argument.
494 ++ArgCount;
495 Item.insertText += "${" + std::to_string(ArgCount) + ':' +
496 escapeSnippet(Chunk.Text) + '}';
497 Item.label += Chunk.Text;
498 Item.insertTextFormat = InsertTextFormat::Snippet;
499 break;
500 case CodeCompletionString::CK_Informative:
501 // A piece of text that describes something about the result
502 // but should not be inserted into the buffer.
503 // For example, the word "const" for a const method, or the name of
504 // the base class for methods that are part of the base class.
505 Item.label += Chunk.Text;
506 // Don't put the informative chunks in the insertText.
507 break;
508 case CodeCompletionString::CK_ResultType:
509 // A piece of text that describes the type of an entity or,
510 // for functions and methods, the return type.
511 assert(Item.detail.empty() && "Unexpected extraneous CK_ResultType");
512 Item.detail = Chunk.Text;
513 break;
514 case CodeCompletionString::CK_CurrentParameter:
515 // A piece of text that describes the parameter that corresponds to
516 // the code-completion location within a function call, message send,
517 // macro invocation, etc.
518 //
519 // This should never be present while collecting completion items,
520 // only while collecting overload candidates.
521 llvm_unreachable("Unexpected CK_CurrentParameter while collecting "
522 "CompletionItems");
523 break;
524 case CodeCompletionString::CK_LeftParen:
525 // A left parenthesis ('(').
526 case CodeCompletionString::CK_RightParen:
527 // A right parenthesis (')').
528 case CodeCompletionString::CK_LeftBracket:
529 // A left bracket ('[').
530 case CodeCompletionString::CK_RightBracket:
531 // A right bracket (']').
532 case CodeCompletionString::CK_LeftBrace:
533 // A left brace ('{').
534 case CodeCompletionString::CK_RightBrace:
535 // A right brace ('}').
536 case CodeCompletionString::CK_LeftAngle:
537 // A left angle bracket ('<').
538 case CodeCompletionString::CK_RightAngle:
539 // A right angle bracket ('>').
540 case CodeCompletionString::CK_Comma:
541 // A comma separator (',').
542 case CodeCompletionString::CK_Colon:
543 // A colon (':').
544 case CodeCompletionString::CK_SemiColon:
545 // A semicolon (';').
546 case CodeCompletionString::CK_Equal:
547 // An '=' sign.
548 case CodeCompletionString::CK_HorizontalSpace:
549 // Horizontal whitespace (' ').
550 Item.insertText += Chunk.Text;
551 Item.label += Chunk.Text;
552 break;
553 case CodeCompletionString::CK_VerticalSpace:
554 // Vertical whitespace ('\n' or '\r\n', depending on the
555 // platform).
556 Item.insertText += Chunk.Text;
557 // Don't even add a space to the label.
558 break;
559 }
560 }
561 }
562}; // SnippetCompletionItemsCollector
Ilya Biryukov38d79772017-05-16 09:38:59 +0000563} // namespace
564
Ilya Biryukov0f62ed22017-05-26 12:26:51 +0000565std::vector<CompletionItem>
Ilya Biryukov02d58702017-08-01 15:51:38 +0000566clangd::codeComplete(PathRef FileName, tooling::CompileCommand Command,
567 PrecompiledPreamble const *Preamble, StringRef Contents,
568 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000569 std::shared_ptr<PCHContainerOperations> PCHs,
Ilya Biryukove5128f72017-09-20 07:24:15 +0000570 bool SnippetCompletions, clangd::Logger &Logger) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000571 std::vector<const char *> ArgStrs;
572 for (const auto &S : Command.CommandLine)
573 ArgStrs.push_back(S.c_str());
574
Krasimir Georgieve4130d52017-07-25 11:37:43 +0000575 VFS->setCurrentWorkingDirectory(Command.Directory);
576
Ilya Biryukov04db3682017-07-21 13:29:29 +0000577 std::unique_ptr<CompilerInvocation> CI;
578 EmptyDiagsConsumer DummyDiagsConsumer;
579 {
580 IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
581 CompilerInstance::createDiagnostics(new DiagnosticOptions,
582 &DummyDiagsConsumer, false);
583 CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
584 }
585 assert(CI && "Couldn't create CompilerInvocation");
586
587 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
588 llvm::MemoryBuffer::getMemBufferCopy(Contents, FileName);
589
590 // Attempt to reuse the PCH from precompiled preamble, if it was built.
Ilya Biryukov04db3682017-07-21 13:29:29 +0000591 if (Preamble) {
592 auto Bounds =
593 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
Ilya Biryukov02d58702017-08-01 15:51:38 +0000594 if (!Preamble->CanReuse(*CI, ContentsBuffer.get(), Bounds, VFS.get()))
595 Preamble = nullptr;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000596 }
597
Ilya Biryukov02d58702017-08-01 15:51:38 +0000598 auto Clang = prepareCompilerInstance(std::move(CI), Preamble,
Ilya Biryukov04db3682017-07-21 13:29:29 +0000599 std::move(ContentsBuffer), PCHs, VFS,
600 DummyDiagsConsumer);
601 auto &DiagOpts = Clang->getDiagnosticOpts();
602 DiagOpts.IgnoreWarnings = true;
603
604 auto &FrontendOpts = Clang->getFrontendOpts();
605 FrontendOpts.SkipFunctionBodies = true;
606
607 FrontendOpts.CodeCompleteOpts.IncludeGlobals = true;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000608 FrontendOpts.CodeCompleteOpts.IncludeMacros = true;
609 FrontendOpts.CodeCompleteOpts.IncludeBriefComments = true;
610
611 FrontendOpts.CodeCompletionAt.FileName = FileName;
612 FrontendOpts.CodeCompletionAt.Line = Pos.line + 1;
613 FrontendOpts.CodeCompletionAt.Column = Pos.character + 1;
614
Ilya Biryukov38d79772017-05-16 09:38:59 +0000615 std::vector<CompletionItem> Items;
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000616 if (SnippetCompletions) {
617 FrontendOpts.CodeCompleteOpts.IncludeCodePatterns = true;
618 Clang->setCodeCompletionConsumer(new SnippetCompletionItemsCollector(
619 FrontendOpts.CodeCompleteOpts, Items));
620 } else {
621 FrontendOpts.CodeCompleteOpts.IncludeCodePatterns = false;
622 Clang->setCodeCompletionConsumer(new PlainTextCompletionItemsCollector(
623 FrontendOpts.CodeCompleteOpts, Items));
624 }
Ilya Biryukov38d79772017-05-16 09:38:59 +0000625
Ilya Biryukov04db3682017-07-21 13:29:29 +0000626 SyntaxOnlyAction Action;
627 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
Ilya Biryukove5128f72017-09-20 07:24:15 +0000628 Logger.log("BeginSourceFile() failed when running codeComplete for " +
629 FileName);
Ilya Biryukov04db3682017-07-21 13:29:29 +0000630 return Items;
631 }
Ilya Biryukove5128f72017-09-20 07:24:15 +0000632 if (!Action.Execute())
633 Logger.log("Execute() failed when running codeComplete for " + FileName);
634
Ilya Biryukov04db3682017-07-21 13:29:29 +0000635 Action.EndSourceFile();
Ilya Biryukov38d79772017-05-16 09:38:59 +0000636
Ilya Biryukov38d79772017-05-16 09:38:59 +0000637 return Items;
638}
639
Ilya Biryukov02d58702017-08-01 15:51:38 +0000640void clangd::dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) {
641 AST.getASTContext().getTranslationUnitDecl()->dump(OS, true);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000642}
Ilya Biryukovf01af682017-05-23 13:42:59 +0000643
Ilya Biryukov02d58702017-08-01 15:51:38 +0000644llvm::Optional<ParsedAST>
645ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI,
646 const PrecompiledPreamble *Preamble,
647 ArrayRef<serialization::DeclID> PreambleDeclIDs,
648 std::unique_ptr<llvm::MemoryBuffer> Buffer,
649 std::shared_ptr<PCHContainerOperations> PCHs,
Ilya Biryukove5128f72017-09-20 07:24:15 +0000650 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
651 clangd::Logger &Logger) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000652
653 std::vector<DiagWithFixIts> ASTDiags;
654 StoreDiagsConsumer UnitDiagsConsumer(/*ref*/ ASTDiags);
655
656 auto Clang =
657 prepareCompilerInstance(std::move(CI), Preamble, std::move(Buffer), PCHs,
658 VFS, /*ref*/ UnitDiagsConsumer);
659
660 // Recover resources if we crash before exiting this method.
661 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
662 Clang.get());
663
664 auto Action = llvm::make_unique<ClangdFrontendAction>();
Ilya Biryukove5128f72017-09-20 07:24:15 +0000665 const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
666 if (!Action->BeginSourceFile(*Clang, MainInput)) {
667 Logger.log("BeginSourceFile() failed when building AST for " +
668 MainInput.getFile());
Ilya Biryukov04db3682017-07-21 13:29:29 +0000669 return llvm::None;
670 }
Ilya Biryukove5128f72017-09-20 07:24:15 +0000671 if (!Action->Execute())
672 Logger.log("Execute() failed when building AST for " + MainInput.getFile());
Ilya Biryukov04db3682017-07-21 13:29:29 +0000673
674 // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
675 // has a longer lifetime.
676 Clang->getDiagnostics().setClient(new EmptyDiagsConsumer);
677
678 std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls();
679 std::vector<serialization::DeclID> PendingDecls;
680 if (Preamble) {
681 PendingDecls.reserve(PreambleDeclIDs.size());
682 PendingDecls.insert(PendingDecls.begin(), PreambleDeclIDs.begin(),
683 PreambleDeclIDs.end());
684 }
685
686 return ParsedAST(std::move(Clang), std::move(Action), std::move(ParsedDecls),
687 std::move(PendingDecls), std::move(ASTDiags));
688}
689
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000690namespace {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000691
692SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
693 const FileEntry *FE,
694 unsigned Offset) {
695 SourceLocation FileLoc = Mgr.translateFileLineCol(FE, 1, 1);
696 return Mgr.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
697}
698
699SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
700 const FileEntry *FE, Position Pos) {
701 SourceLocation InputLoc =
702 Mgr.translateFileLineCol(FE, Pos.line + 1, Pos.character + 1);
703 return Mgr.getMacroArgExpandedLocation(InputLoc);
704}
705
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000706/// Finds declarations locations that a given source location refers to.
707class DeclarationLocationsFinder : public index::IndexDataConsumer {
708 std::vector<Location> DeclarationLocations;
709 const SourceLocation &SearchedLocation;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000710 const ASTContext &AST;
711 Preprocessor &PP;
712
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000713public:
714 DeclarationLocationsFinder(raw_ostream &OS,
Ilya Biryukov04db3682017-07-21 13:29:29 +0000715 const SourceLocation &SearchedLocation,
716 ASTContext &AST, Preprocessor &PP)
717 : SearchedLocation(SearchedLocation), AST(AST), PP(PP) {}
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000718
719 std::vector<Location> takeLocations() {
720 // Don't keep the same location multiple times.
721 // This can happen when nodes in the AST are visited twice.
722 std::sort(DeclarationLocations.begin(), DeclarationLocations.end());
Kirill Bobyrev46213872017-06-28 20:57:28 +0000723 auto last =
724 std::unique(DeclarationLocations.begin(), DeclarationLocations.end());
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000725 DeclarationLocations.erase(last, DeclarationLocations.end());
726 return std::move(DeclarationLocations);
727 }
728
Ilya Biryukov02d58702017-08-01 15:51:38 +0000729 bool
730 handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
731 ArrayRef<index::SymbolRelation> Relations, FileID FID,
732 unsigned Offset,
733 index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000734 if (isSearchedLocation(FID, Offset)) {
735 addDeclarationLocation(D->getSourceRange());
736 }
737 return true;
738 }
739
740private:
741 bool isSearchedLocation(FileID FID, unsigned Offset) const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000742 const SourceManager &SourceMgr = AST.getSourceManager();
743 return SourceMgr.getFileOffset(SearchedLocation) == Offset &&
744 SourceMgr.getFileID(SearchedLocation) == FID;
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000745 }
746
Ilya Biryukov04db3682017-07-21 13:29:29 +0000747 void addDeclarationLocation(const SourceRange &ValSourceRange) {
748 const SourceManager &SourceMgr = AST.getSourceManager();
749 const LangOptions &LangOpts = AST.getLangOpts();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000750 SourceLocation LocStart = ValSourceRange.getBegin();
751 SourceLocation LocEnd = Lexer::getLocForEndOfToken(ValSourceRange.getEnd(),
Ilya Biryukov04db3682017-07-21 13:29:29 +0000752 0, SourceMgr, LangOpts);
Kirill Bobyrev46213872017-06-28 20:57:28 +0000753 Position Begin;
754 Begin.line = SourceMgr.getSpellingLineNumber(LocStart) - 1;
755 Begin.character = SourceMgr.getSpellingColumnNumber(LocStart) - 1;
756 Position End;
757 End.line = SourceMgr.getSpellingLineNumber(LocEnd) - 1;
758 End.character = SourceMgr.getSpellingColumnNumber(LocEnd) - 1;
759 Range R = {Begin, End};
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000760 Location L;
761 L.uri = URI::fromFile(
762 SourceMgr.getFilename(SourceMgr.getSpellingLoc(LocStart)));
763 L.range = R;
764 DeclarationLocations.push_back(L);
765 }
766
Kirill Bobyrev46213872017-06-28 20:57:28 +0000767 void finish() override {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000768 // Also handle possible macro at the searched location.
769 Token Result;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000770 if (!Lexer::getRawToken(SearchedLocation, Result, AST.getSourceManager(),
771 AST.getLangOpts(), false)) {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000772 if (Result.is(tok::raw_identifier)) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000773 PP.LookUpIdentifierInfo(Result);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000774 }
Ilya Biryukov04db3682017-07-21 13:29:29 +0000775 IdentifierInfo *IdentifierInfo = Result.getIdentifierInfo();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000776 if (IdentifierInfo && IdentifierInfo->hadMacroDefinition()) {
777 std::pair<FileID, unsigned int> DecLoc =
Ilya Biryukov04db3682017-07-21 13:29:29 +0000778 AST.getSourceManager().getDecomposedExpansionLoc(SearchedLocation);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000779 // Get the definition just before the searched location so that a macro
780 // referenced in a '#undef MACRO' can still be found.
Ilya Biryukov04db3682017-07-21 13:29:29 +0000781 SourceLocation BeforeSearchedLocation = getMacroArgExpandedLocation(
782 AST.getSourceManager(),
783 AST.getSourceManager().getFileEntryForID(DecLoc.first),
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000784 DecLoc.second - 1);
785 MacroDefinition MacroDef =
Ilya Biryukov04db3682017-07-21 13:29:29 +0000786 PP.getMacroDefinitionAtLoc(IdentifierInfo, BeforeSearchedLocation);
787 MacroInfo *MacroInf = MacroDef.getMacroInfo();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000788 if (MacroInf) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000789 addDeclarationLocation(SourceRange(MacroInf->getDefinitionLoc(),
790 MacroInf->getDefinitionEndLoc()));
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000791 }
792 }
793 }
794 }
795};
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000796
Ilya Biryukov02d58702017-08-01 15:51:38 +0000797SourceLocation getBeginningOfIdentifier(ParsedAST &Unit, const Position &Pos,
798 const FileEntry *FE) {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000799 // The language server protocol uses zero-based line and column numbers.
800 // Clang uses one-based numbers.
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000801
Ilya Biryukov02d58702017-08-01 15:51:38 +0000802 const ASTContext &AST = Unit.getASTContext();
Ilya Biryukov04db3682017-07-21 13:29:29 +0000803 const SourceManager &SourceMgr = AST.getSourceManager();
804
805 SourceLocation InputLocation =
806 getMacroArgExpandedLocation(SourceMgr, FE, Pos);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000807 if (Pos.character == 0) {
808 return InputLocation;
809 }
810
811 // This handle cases where the position is in the middle of a token or right
812 // after the end of a token. In theory we could just use GetBeginningOfToken
813 // to find the start of the token at the input position, but this doesn't
814 // work when right after the end, i.e. foo|.
815 // So try to go back by one and see if we're still inside the an identifier
816 // token. If so, Take the beginning of this token.
817 // (It should be the same identifier because you can't have two adjacent
818 // identifiers without another token in between.)
Ilya Biryukov04db3682017-07-21 13:29:29 +0000819 SourceLocation PeekBeforeLocation = getMacroArgExpandedLocation(
820 SourceMgr, FE, Position{Pos.line, Pos.character - 1});
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000821 Token Result;
Ilya Biryukov4203d2a2017-06-29 17:11:32 +0000822 if (Lexer::getRawToken(PeekBeforeLocation, Result, SourceMgr,
Ilya Biryukov04db3682017-07-21 13:29:29 +0000823 AST.getLangOpts(), false)) {
Ilya Biryukov4203d2a2017-06-29 17:11:32 +0000824 // getRawToken failed, just use InputLocation.
825 return InputLocation;
826 }
827
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000828 if (Result.is(tok::raw_identifier)) {
829 return Lexer::GetBeginningOfToken(PeekBeforeLocation, SourceMgr,
Ilya Biryukov02d58702017-08-01 15:51:38 +0000830 AST.getLangOpts());
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000831 }
832
833 return InputLocation;
834}
Ilya Biryukov02d58702017-08-01 15:51:38 +0000835} // namespace
Ilya Biryukov04db3682017-07-21 13:29:29 +0000836
Ilya Biryukove5128f72017-09-20 07:24:15 +0000837std::vector<Location> clangd::findDefinitions(ParsedAST &AST, Position Pos,
838 clangd::Logger &Logger) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000839 const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
840 const FileEntry *FE = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
841 if (!FE)
842 return {};
843
844 SourceLocation SourceLocationBeg = getBeginningOfIdentifier(AST, Pos, FE);
845
846 auto DeclLocationsFinder = std::make_shared<DeclarationLocationsFinder>(
847 llvm::errs(), SourceLocationBeg, AST.getASTContext(),
848 AST.getPreprocessor());
849 index::IndexingOptions IndexOpts;
850 IndexOpts.SystemSymbolFilter =
851 index::IndexingOptions::SystemSymbolFilterKind::All;
852 IndexOpts.IndexFunctionLocals = true;
853
854 indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(),
855 DeclLocationsFinder, IndexOpts);
856
857 return DeclLocationsFinder->takeLocations();
858}
859
860void ParsedAST::ensurePreambleDeclsDeserialized() {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000861 if (PendingTopLevelDecls.empty())
862 return;
863
864 std::vector<const Decl *> Resolved;
865 Resolved.reserve(PendingTopLevelDecls.size());
866
867 ExternalASTSource &Source = *getASTContext().getExternalSource();
868 for (serialization::DeclID TopLevelDecl : PendingTopLevelDecls) {
869 // Resolve the declaration ID to an actual declaration, possibly
870 // deserializing the declaration in the process.
871 if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
872 Resolved.push_back(D);
873 }
874
875 TopLevelDecls.reserve(TopLevelDecls.size() + PendingTopLevelDecls.size());
876 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
877
878 PendingTopLevelDecls.clear();
879}
880
Ilya Biryukov02d58702017-08-01 15:51:38 +0000881ParsedAST::ParsedAST(ParsedAST &&Other) = default;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000882
Ilya Biryukov02d58702017-08-01 15:51:38 +0000883ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000884
Ilya Biryukov02d58702017-08-01 15:51:38 +0000885ParsedAST::~ParsedAST() {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000886 if (Action) {
887 Action->EndSourceFile();
888 }
889}
890
Ilya Biryukov02d58702017-08-01 15:51:38 +0000891ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
892
893const ASTContext &ParsedAST::getASTContext() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000894 return Clang->getASTContext();
895}
896
Ilya Biryukov02d58702017-08-01 15:51:38 +0000897Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
Ilya Biryukov04db3682017-07-21 13:29:29 +0000898
Ilya Biryukov02d58702017-08-01 15:51:38 +0000899const Preprocessor &ParsedAST::getPreprocessor() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000900 return Clang->getPreprocessor();
901}
902
Ilya Biryukov02d58702017-08-01 15:51:38 +0000903ArrayRef<const Decl *> ParsedAST::getTopLevelDecls() {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000904 ensurePreambleDeclsDeserialized();
905 return TopLevelDecls;
906}
907
Ilya Biryukov02d58702017-08-01 15:51:38 +0000908const std::vector<DiagWithFixIts> &ParsedAST::getDiagnostics() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000909 return Diags;
910}
911
Ilya Biryukov02d58702017-08-01 15:51:38 +0000912ParsedAST::ParsedAST(std::unique_ptr<CompilerInstance> Clang,
913 std::unique_ptr<FrontendAction> Action,
914 std::vector<const Decl *> TopLevelDecls,
915 std::vector<serialization::DeclID> PendingTopLevelDecls,
916 std::vector<DiagWithFixIts> Diags)
Ilya Biryukov04db3682017-07-21 13:29:29 +0000917 : Clang(std::move(Clang)), Action(std::move(Action)),
918 Diags(std::move(Diags)), TopLevelDecls(std::move(TopLevelDecls)),
919 PendingTopLevelDecls(std::move(PendingTopLevelDecls)) {
920 assert(this->Clang);
921 assert(this->Action);
922}
923
Ilya Biryukov02d58702017-08-01 15:51:38 +0000924ParsedASTWrapper::ParsedASTWrapper(ParsedASTWrapper &&Wrapper)
925 : AST(std::move(Wrapper.AST)) {}
926
927ParsedASTWrapper::ParsedASTWrapper(llvm::Optional<ParsedAST> AST)
928 : AST(std::move(AST)) {}
929
930PreambleData::PreambleData(PrecompiledPreamble Preamble,
931 std::vector<serialization::DeclID> TopLevelDeclIDs,
932 std::vector<DiagWithFixIts> Diags)
Ilya Biryukov04db3682017-07-21 13:29:29 +0000933 : Preamble(std::move(Preamble)),
934 TopLevelDeclIDs(std::move(TopLevelDeclIDs)), Diags(std::move(Diags)) {}
Ilya Biryukov02d58702017-08-01 15:51:38 +0000935
936std::shared_ptr<CppFile>
937CppFile::Create(PathRef FileName, tooling::CompileCommand Command,
Ilya Biryukov83ca8a22017-09-20 10:46:58 +0000938 std::shared_ptr<PCHContainerOperations> PCHs,
939 clangd::Logger &Logger) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000940 return std::shared_ptr<CppFile>(
Ilya Biryukove5128f72017-09-20 07:24:15 +0000941 new CppFile(FileName, std::move(Command), std::move(PCHs), Logger));
Ilya Biryukov02d58702017-08-01 15:51:38 +0000942}
943
944CppFile::CppFile(PathRef FileName, tooling::CompileCommand Command,
Ilya Biryukove5128f72017-09-20 07:24:15 +0000945 std::shared_ptr<PCHContainerOperations> PCHs,
946 clangd::Logger &Logger)
Ilya Biryukov02d58702017-08-01 15:51:38 +0000947 : FileName(FileName), Command(std::move(Command)), RebuildCounter(0),
Ilya Biryukove5128f72017-09-20 07:24:15 +0000948 RebuildInProgress(false), PCHs(std::move(PCHs)), Logger(Logger) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000949
950 std::lock_guard<std::mutex> Lock(Mutex);
951 LatestAvailablePreamble = nullptr;
952 PreamblePromise.set_value(nullptr);
953 PreambleFuture = PreamblePromise.get_future();
954
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000955 ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
Ilya Biryukov02d58702017-08-01 15:51:38 +0000956 ASTFuture = ASTPromise.get_future();
957}
958
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000959void CppFile::cancelRebuild() { deferCancelRebuild().get(); }
960
961std::future<void> CppFile::deferCancelRebuild() {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000962 std::unique_lock<std::mutex> Lock(Mutex);
963 // Cancel an ongoing rebuild, if any, and wait for it to finish.
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000964 unsigned RequestRebuildCounter = ++this->RebuildCounter;
Ilya Biryukov02d58702017-08-01 15:51:38 +0000965 // Rebuild asserts that futures aren't ready if rebuild is cancelled.
966 // We want to keep this invariant.
967 if (futureIsReady(PreambleFuture)) {
968 PreamblePromise = std::promise<std::shared_ptr<const PreambleData>>();
969 PreambleFuture = PreamblePromise.get_future();
970 }
971 if (futureIsReady(ASTFuture)) {
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000972 ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
Ilya Biryukov02d58702017-08-01 15:51:38 +0000973 ASTFuture = ASTPromise.get_future();
974 }
Ilya Biryukov02d58702017-08-01 15:51:38 +0000975
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +0000976 Lock.unlock();
977 // Notify about changes to RebuildCounter.
978 RebuildCond.notify_all();
979
980 std::shared_ptr<CppFile> That = shared_from_this();
981 return std::async(std::launch::deferred, [That, RequestRebuildCounter]() {
982 std::unique_lock<std::mutex> Lock(That->Mutex);
983 CppFile *This = &*That;
984 This->RebuildCond.wait(Lock, [This, RequestRebuildCounter]() {
985 return !This->RebuildInProgress ||
986 This->RebuildCounter != RequestRebuildCounter;
987 });
988
989 // This computation got cancelled itself, do nothing.
990 if (This->RebuildCounter != RequestRebuildCounter)
991 return;
992
993 // Set empty results for Promises.
994 That->PreamblePromise.set_value(nullptr);
995 That->ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
996 });
Ilya Biryukov02d58702017-08-01 15:51:38 +0000997}
998
999llvm::Optional<std::vector<DiagWithFixIts>>
1000CppFile::rebuild(StringRef NewContents,
1001 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
1002 return deferRebuild(NewContents, std::move(VFS)).get();
1003}
1004
1005std::future<llvm::Optional<std::vector<DiagWithFixIts>>>
1006CppFile::deferRebuild(StringRef NewContents,
1007 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
1008 std::shared_ptr<const PreambleData> OldPreamble;
1009 std::shared_ptr<PCHContainerOperations> PCHs;
1010 unsigned RequestRebuildCounter;
1011 {
1012 std::unique_lock<std::mutex> Lock(Mutex);
1013 // Increase RebuildCounter to cancel all ongoing FinishRebuild operations.
1014 // They will try to exit as early as possible and won't call set_value on
1015 // our promises.
1016 RequestRebuildCounter = ++this->RebuildCounter;
1017 PCHs = this->PCHs;
1018
1019 // Remember the preamble to be used during rebuild.
1020 OldPreamble = this->LatestAvailablePreamble;
1021 // Setup std::promises and std::futures for Preamble and AST. Corresponding
1022 // futures will wait until the rebuild process is finished.
1023 if (futureIsReady(this->PreambleFuture)) {
1024 this->PreamblePromise =
1025 std::promise<std::shared_ptr<const PreambleData>>();
1026 this->PreambleFuture = this->PreamblePromise.get_future();
1027 }
1028 if (futureIsReady(this->ASTFuture)) {
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001029 this->ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001030 this->ASTFuture = this->ASTPromise.get_future();
1031 }
1032 } // unlock Mutex.
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001033 // Notify about changes to RebuildCounter.
1034 RebuildCond.notify_all();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001035
1036 // A helper to function to finish the rebuild. May be run on a different
1037 // thread.
1038
1039 // Don't let this CppFile die before rebuild is finished.
1040 std::shared_ptr<CppFile> That = shared_from_this();
1041 auto FinishRebuild = [OldPreamble, VFS, RequestRebuildCounter, PCHs,
1042 That](std::string NewContents)
1043 -> llvm::Optional<std::vector<DiagWithFixIts>> {
1044 // Only one execution of this method is possible at a time.
1045 // RebuildGuard will wait for any ongoing rebuilds to finish and will put us
1046 // into a state for doing a rebuild.
1047 RebuildGuard Rebuild(*That, RequestRebuildCounter);
1048 if (Rebuild.wasCancelledBeforeConstruction())
1049 return llvm::None;
1050
1051 std::vector<const char *> ArgStrs;
1052 for (const auto &S : That->Command.CommandLine)
1053 ArgStrs.push_back(S.c_str());
1054
1055 VFS->setCurrentWorkingDirectory(That->Command.Directory);
1056
1057 std::unique_ptr<CompilerInvocation> CI;
1058 {
1059 // FIXME(ibiryukov): store diagnostics from CommandLine when we start
1060 // reporting them.
1061 EmptyDiagsConsumer CommandLineDiagsConsumer;
1062 IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
1063 CompilerInstance::createDiagnostics(new DiagnosticOptions,
1064 &CommandLineDiagsConsumer, false);
1065 CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
1066 }
1067 assert(CI && "Couldn't create CompilerInvocation");
1068
1069 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1070 llvm::MemoryBuffer::getMemBufferCopy(NewContents, That->FileName);
1071
1072 // A helper function to rebuild the preamble or reuse the existing one. Does
1073 // not mutate any fields, only does the actual computation.
1074 auto DoRebuildPreamble = [&]() -> std::shared_ptr<const PreambleData> {
1075 auto Bounds =
1076 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
1077 if (OldPreamble && OldPreamble->Preamble.CanReuse(
1078 *CI, ContentsBuffer.get(), Bounds, VFS.get())) {
1079 return OldPreamble;
1080 }
1081
1082 std::vector<DiagWithFixIts> PreambleDiags;
1083 StoreDiagsConsumer PreambleDiagnosticsConsumer(/*ref*/ PreambleDiags);
1084 IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
1085 CompilerInstance::createDiagnostics(
1086 &CI->getDiagnosticOpts(), &PreambleDiagnosticsConsumer, false);
1087 CppFilePreambleCallbacks SerializedDeclsCollector;
1088 auto BuiltPreamble = PrecompiledPreamble::Build(
1089 *CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine, VFS, PCHs,
1090 SerializedDeclsCollector);
1091
1092 if (BuiltPreamble) {
1093 return std::make_shared<PreambleData>(
1094 std::move(*BuiltPreamble),
1095 SerializedDeclsCollector.takeTopLevelDeclIDs(),
1096 std::move(PreambleDiags));
1097 } else {
1098 return nullptr;
1099 }
1100 };
1101
1102 // Compute updated Preamble.
1103 std::shared_ptr<const PreambleData> NewPreamble = DoRebuildPreamble();
1104 // Publish the new Preamble.
1105 {
1106 std::lock_guard<std::mutex> Lock(That->Mutex);
1107 // We always set LatestAvailablePreamble to the new value, hoping that it
1108 // will still be usable in the further requests.
1109 That->LatestAvailablePreamble = NewPreamble;
1110 if (RequestRebuildCounter != That->RebuildCounter)
1111 return llvm::None; // Our rebuild request was cancelled, do nothing.
1112 That->PreamblePromise.set_value(NewPreamble);
1113 } // unlock Mutex
1114
1115 // Prepare the Preamble and supplementary data for rebuilding AST.
1116 const PrecompiledPreamble *PreambleForAST = nullptr;
1117 ArrayRef<serialization::DeclID> SerializedPreambleDecls = llvm::None;
1118 std::vector<DiagWithFixIts> Diagnostics;
1119 if (NewPreamble) {
1120 PreambleForAST = &NewPreamble->Preamble;
1121 SerializedPreambleDecls = NewPreamble->TopLevelDeclIDs;
1122 Diagnostics.insert(Diagnostics.begin(), NewPreamble->Diags.begin(),
1123 NewPreamble->Diags.end());
1124 }
1125
1126 // Compute updated AST.
1127 llvm::Optional<ParsedAST> NewAST =
1128 ParsedAST::Build(std::move(CI), PreambleForAST, SerializedPreambleDecls,
Ilya Biryukove5128f72017-09-20 07:24:15 +00001129 std::move(ContentsBuffer), PCHs, VFS, That->Logger);
Ilya Biryukov02d58702017-08-01 15:51:38 +00001130
1131 if (NewAST) {
1132 Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(),
1133 NewAST->getDiagnostics().end());
1134 } else {
1135 // Don't report even Preamble diagnostics if we coulnd't build AST.
1136 Diagnostics.clear();
1137 }
1138
1139 // Publish the new AST.
1140 {
1141 std::lock_guard<std::mutex> Lock(That->Mutex);
1142 if (RequestRebuildCounter != That->RebuildCounter)
1143 return Diagnostics; // Our rebuild request was cancelled, don't set
1144 // ASTPromise.
1145
Ilya Biryukov574b7532017-08-02 09:08:39 +00001146 That->ASTPromise.set_value(
1147 std::make_shared<ParsedASTWrapper>(std::move(NewAST)));
Ilya Biryukov02d58702017-08-01 15:51:38 +00001148 } // unlock Mutex
1149
1150 return Diagnostics;
1151 };
1152
1153 return std::async(std::launch::deferred, FinishRebuild, NewContents.str());
1154}
1155
1156std::shared_future<std::shared_ptr<const PreambleData>>
1157CppFile::getPreamble() const {
1158 std::lock_guard<std::mutex> Lock(Mutex);
1159 return PreambleFuture;
1160}
1161
1162std::shared_ptr<const PreambleData> CppFile::getPossiblyStalePreamble() const {
1163 std::lock_guard<std::mutex> Lock(Mutex);
1164 return LatestAvailablePreamble;
1165}
1166
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001167std::shared_future<std::shared_ptr<ParsedASTWrapper>> CppFile::getAST() const {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001168 std::lock_guard<std::mutex> Lock(Mutex);
1169 return ASTFuture;
1170}
1171
1172tooling::CompileCommand const &CppFile::getCompileCommand() const {
1173 return Command;
1174}
1175
1176CppFile::RebuildGuard::RebuildGuard(CppFile &File,
1177 unsigned RequestRebuildCounter)
1178 : File(File), RequestRebuildCounter(RequestRebuildCounter) {
1179 std::unique_lock<std::mutex> Lock(File.Mutex);
1180 WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
1181 if (WasCancelledBeforeConstruction)
1182 return;
1183
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001184 File.RebuildCond.wait(Lock, [&File, RequestRebuildCounter]() {
1185 return !File.RebuildInProgress ||
1186 File.RebuildCounter != RequestRebuildCounter;
1187 });
Ilya Biryukov02d58702017-08-01 15:51:38 +00001188
1189 WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
1190 if (WasCancelledBeforeConstruction)
1191 return;
1192
1193 File.RebuildInProgress = true;
1194}
1195
1196bool CppFile::RebuildGuard::wasCancelledBeforeConstruction() const {
1197 return WasCancelledBeforeConstruction;
1198}
1199
1200CppFile::RebuildGuard::~RebuildGuard() {
1201 if (WasCancelledBeforeConstruction)
1202 return;
1203
1204 std::unique_lock<std::mutex> Lock(File.Mutex);
1205 assert(File.RebuildInProgress);
1206 File.RebuildInProgress = false;
1207
1208 if (File.RebuildCounter == RequestRebuildCounter) {
1209 // Our rebuild request was successful.
1210 assert(futureIsReady(File.ASTFuture));
1211 assert(futureIsReady(File.PreambleFuture));
1212 } else {
1213 // Our rebuild request was cancelled, because further reparse was requested.
1214 assert(!futureIsReady(File.ASTFuture));
1215 assert(!futureIsReady(File.PreambleFuture));
1216 }
1217
1218 Lock.unlock();
1219 File.RebuildCond.notify_all();
1220}