blob: fa261654450e0f2fcc975aebadb57fe244bbadb8 [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"
Sam McCall8567cb32017-11-02 09:21:51 +000013#include "Trace.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000014#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Frontend/CompilerInvocation.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000016#include "clang/Frontend/FrontendActions.h"
Ilya Biryukov0f62ed22017-05-26 12:26:51 +000017#include "clang/Frontend/Utils.h"
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000018#include "clang/Index/IndexDataConsumer.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000019#include "clang/Index/IndexingAction.h"
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000020#include "clang/Lex/Lexer.h"
21#include "clang/Lex/MacroInfo.h"
22#include "clang/Lex/Preprocessor.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000023#include "clang/Lex/PreprocessorOptions.h"
24#include "clang/Sema/Sema.h"
25#include "clang/Serialization/ASTWriter.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000026#include "clang/Tooling/CompilationDatabase.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000027#include "llvm/ADT/ArrayRef.h"
28#include "llvm/ADT/SmallVector.h"
29#include "llvm/Support/CrashRecoveryContext.h"
Krasimir Georgieva1de3c92017-06-15 09:11:57 +000030#include "llvm/Support/Format.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000031
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000032#include <algorithm>
Ilya Biryukov02d58702017-08-01 15:51:38 +000033#include <chrono>
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000034
Ilya Biryukov38d79772017-05-16 09:38:59 +000035using namespace clang::clangd;
36using namespace clang;
37
Ilya Biryukov04db3682017-07-21 13:29:29 +000038namespace {
39
40class DeclTrackingASTConsumer : public ASTConsumer {
41public:
42 DeclTrackingASTConsumer(std::vector<const Decl *> &TopLevelDecls)
43 : TopLevelDecls(TopLevelDecls) {}
44
45 bool HandleTopLevelDecl(DeclGroupRef DG) override {
46 for (const Decl *D : DG) {
47 // ObjCMethodDecl are not actually top-level decls.
48 if (isa<ObjCMethodDecl>(D))
49 continue;
50
51 TopLevelDecls.push_back(D);
52 }
53 return true;
54 }
55
56private:
57 std::vector<const Decl *> &TopLevelDecls;
58};
59
60class ClangdFrontendAction : public SyntaxOnlyAction {
61public:
62 std::vector<const Decl *> takeTopLevelDecls() {
63 return std::move(TopLevelDecls);
64 }
65
66protected:
67 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
68 StringRef InFile) override {
69 return llvm::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls);
70 }
71
72private:
73 std::vector<const Decl *> TopLevelDecls;
74};
75
Ilya Biryukov02d58702017-08-01 15:51:38 +000076class CppFilePreambleCallbacks : public PreambleCallbacks {
Ilya Biryukov04db3682017-07-21 13:29:29 +000077public:
78 std::vector<serialization::DeclID> takeTopLevelDeclIDs() {
79 return std::move(TopLevelDeclIDs);
80 }
81
82 void AfterPCHEmitted(ASTWriter &Writer) override {
83 TopLevelDeclIDs.reserve(TopLevelDecls.size());
84 for (Decl *D : TopLevelDecls) {
85 // Invalid top-level decls may not have been serialized.
86 if (D->isInvalidDecl())
87 continue;
88 TopLevelDeclIDs.push_back(Writer.getDeclID(D));
89 }
90 }
91
92 void HandleTopLevelDecl(DeclGroupRef DG) override {
93 for (Decl *D : DG) {
94 if (isa<ObjCMethodDecl>(D))
95 continue;
96 TopLevelDecls.push_back(D);
97 }
98 }
99
100private:
101 std::vector<Decl *> TopLevelDecls;
102 std::vector<serialization::DeclID> TopLevelDeclIDs;
103};
104
105/// Convert from clang diagnostic level to LSP severity.
106static int getSeverity(DiagnosticsEngine::Level L) {
107 switch (L) {
108 case DiagnosticsEngine::Remark:
109 return 4;
110 case DiagnosticsEngine::Note:
111 return 3;
112 case DiagnosticsEngine::Warning:
113 return 2;
114 case DiagnosticsEngine::Fatal:
115 case DiagnosticsEngine::Error:
116 return 1;
117 case DiagnosticsEngine::Ignored:
118 return 0;
119 }
120 llvm_unreachable("Unknown diagnostic level!");
121}
122
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000123/// Get the optional chunk as a string. This function is possibly recursive.
124///
125/// The parameter info for each parameter is appended to the Parameters.
126std::string
127getOptionalParameters(const CodeCompletionString &CCS,
128 std::vector<ParameterInformation> &Parameters) {
129 std::string Result;
130 for (const auto &Chunk : CCS) {
131 switch (Chunk.Kind) {
132 case CodeCompletionString::CK_Optional:
133 assert(Chunk.Optional &&
134 "Expected the optional code completion string to be non-null.");
135 Result += getOptionalParameters(*Chunk.Optional, Parameters);
136 break;
137 case CodeCompletionString::CK_VerticalSpace:
138 break;
139 case CodeCompletionString::CK_Placeholder:
140 // A string that acts as a placeholder for, e.g., a function call
141 // argument.
142 // Intentional fallthrough here.
143 case CodeCompletionString::CK_CurrentParameter: {
144 // A piece of text that describes the parameter that corresponds to
145 // the code-completion location within a function call, message send,
146 // macro invocation, etc.
147 Result += Chunk.Text;
148 ParameterInformation Info;
149 Info.label = Chunk.Text;
150 Parameters.push_back(std::move(Info));
151 break;
152 }
153 default:
154 Result += Chunk.Text;
155 break;
156 }
157 }
158 return Result;
159}
160
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000161llvm::Optional<DiagWithFixIts> toClangdDiag(const StoredDiagnostic &D) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000162 auto Location = D.getLocation();
163 if (!Location.isValid() || !Location.getManager().isInMainFile(Location))
164 return llvm::None;
165
166 Position P;
167 P.line = Location.getSpellingLineNumber() - 1;
168 P.character = Location.getSpellingColumnNumber();
169 Range R = {P, P};
170 clangd::Diagnostic Diag = {R, getSeverity(D.getLevel()), D.getMessage()};
171
172 llvm::SmallVector<tooling::Replacement, 1> FixItsForDiagnostic;
173 for (const FixItHint &Fix : D.getFixIts()) {
174 FixItsForDiagnostic.push_back(clang::tooling::Replacement(
175 Location.getManager(), Fix.RemoveRange, Fix.CodeToInsert));
176 }
177 return DiagWithFixIts{Diag, std::move(FixItsForDiagnostic)};
178}
179
180class StoreDiagsConsumer : public DiagnosticConsumer {
181public:
182 StoreDiagsConsumer(std::vector<DiagWithFixIts> &Output) : Output(Output) {}
183
184 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
185 const clang::Diagnostic &Info) override {
186 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
187
188 if (auto convertedDiag = toClangdDiag(StoredDiagnostic(DiagLevel, Info)))
189 Output.push_back(std::move(*convertedDiag));
190 }
191
192private:
193 std::vector<DiagWithFixIts> &Output;
194};
195
196class EmptyDiagsConsumer : public DiagnosticConsumer {
197public:
198 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
199 const clang::Diagnostic &Info) override {}
200};
201
202std::unique_ptr<CompilerInvocation>
203createCompilerInvocation(ArrayRef<const char *> ArgList,
204 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
205 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
206 auto CI = createInvocationFromCommandLine(ArgList, std::move(Diags),
207 std::move(VFS));
208 // We rely on CompilerInstance to manage the resource (i.e. free them on
209 // EndSourceFile), but that won't happen if DisableFree is set to true.
210 // Since createInvocationFromCommandLine sets it to true, we have to override
211 // it.
212 CI->getFrontendOpts().DisableFree = false;
213 return CI;
214}
215
216/// Creates a CompilerInstance from \p CI, with main buffer overriden to \p
217/// Buffer and arguments to read the PCH from \p Preamble, if \p Preamble is not
218/// null. Note that vfs::FileSystem inside returned instance may differ from \p
219/// VFS if additional file remapping were set in command-line arguments.
220/// On some errors, returns null. When non-null value is returned, it's expected
221/// to be consumed by the FrontendAction as it will have a pointer to the \p
222/// Buffer that will only be deleted if BeginSourceFile is called.
223std::unique_ptr<CompilerInstance>
224prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
225 const PrecompiledPreamble *Preamble,
226 std::unique_ptr<llvm::MemoryBuffer> Buffer,
227 std::shared_ptr<PCHContainerOperations> PCHs,
228 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
229 DiagnosticConsumer &DiagsClient) {
230 assert(VFS && "VFS is null");
231 assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
232 "Setting RetainRemappedFileBuffers to true will cause a memory leak "
233 "of ContentsBuffer");
234
235 // NOTE: we use Buffer.get() when adding remapped files, so we have to make
236 // sure it will be released if no error is emitted.
237 if (Preamble) {
238 Preamble->AddImplicitPreamble(*CI, Buffer.get());
239 } else {
240 CI->getPreprocessorOpts().addRemappedFile(
241 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
242 }
243
244 auto Clang = llvm::make_unique<CompilerInstance>(PCHs);
245 Clang->setInvocation(std::move(CI));
246 Clang->createDiagnostics(&DiagsClient, false);
247
248 if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
249 Clang->getInvocation(), Clang->getDiagnostics(), VFS))
250 VFS = VFSWithRemapping;
251 Clang->setVirtualFileSystem(VFS);
252
253 Clang->setTarget(TargetInfo::CreateTargetInfo(
254 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
255 if (!Clang->hasTarget())
256 return nullptr;
257
258 // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
259 // release it.
260 Buffer.release();
261 return Clang;
262}
263
Ilya Biryukov02d58702017-08-01 15:51:38 +0000264template <class T> bool futureIsReady(std::shared_future<T> const &Future) {
265 return Future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
266}
267
Ilya Biryukov04db3682017-07-21 13:29:29 +0000268} // namespace
269
Ilya Biryukov38d79772017-05-16 09:38:59 +0000270namespace {
271
Ilya Biryukov01e3bf82017-10-23 06:06:21 +0000272CompletionItemKind getKindOfDecl(CXCursorKind CursorKind) {
273 switch (CursorKind) {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000274 case CXCursor_MacroInstantiation:
275 case CXCursor_MacroDefinition:
276 return CompletionItemKind::Text;
277 case CXCursor_CXXMethod:
278 return CompletionItemKind::Method;
279 case CXCursor_FunctionDecl:
280 case CXCursor_FunctionTemplate:
281 return CompletionItemKind::Function;
282 case CXCursor_Constructor:
283 case CXCursor_Destructor:
284 return CompletionItemKind::Constructor;
285 case CXCursor_FieldDecl:
286 return CompletionItemKind::Field;
287 case CXCursor_VarDecl:
288 case CXCursor_ParmDecl:
289 return CompletionItemKind::Variable;
290 case CXCursor_ClassDecl:
291 case CXCursor_StructDecl:
292 case CXCursor_UnionDecl:
293 case CXCursor_ClassTemplate:
294 case CXCursor_ClassTemplatePartialSpecialization:
295 return CompletionItemKind::Class;
296 case CXCursor_Namespace:
297 case CXCursor_NamespaceAlias:
298 case CXCursor_NamespaceRef:
299 return CompletionItemKind::Module;
300 case CXCursor_EnumConstantDecl:
301 return CompletionItemKind::Value;
302 case CXCursor_EnumDecl:
303 return CompletionItemKind::Enum;
304 case CXCursor_TypeAliasDecl:
305 case CXCursor_TypeAliasTemplateDecl:
306 case CXCursor_TypedefDecl:
307 case CXCursor_MemberRef:
308 case CXCursor_TypeRef:
309 return CompletionItemKind::Reference;
310 default:
311 return CompletionItemKind::Missing;
312 }
313}
314
Ilya Biryukov01e3bf82017-10-23 06:06:21 +0000315CompletionItemKind getKind(CodeCompletionResult::ResultKind ResKind,
316 CXCursorKind CursorKind) {
317 switch (ResKind) {
318 case CodeCompletionResult::RK_Declaration:
319 return getKindOfDecl(CursorKind);
320 case CodeCompletionResult::RK_Keyword:
321 return CompletionItemKind::Keyword;
322 case CodeCompletionResult::RK_Macro:
323 return CompletionItemKind::Text; // unfortunately, there's no 'Macro'
324 // completion items in LSP.
325 case CodeCompletionResult::RK_Pattern:
326 return CompletionItemKind::Snippet;
327 }
328 llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
329}
330
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000331std::string escapeSnippet(const llvm::StringRef Text) {
332 std::string Result;
333 Result.reserve(Text.size()); // Assume '$', '}' and '\\' are rare.
334 for (const auto Character : Text) {
335 if (Character == '$' || Character == '}' || Character == '\\')
336 Result.push_back('\\');
337 Result.push_back(Character);
338 }
339 return Result;
340}
341
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000342std::string getDocumentation(const CodeCompletionString &CCS) {
343 // Things like __attribute__((nonnull(1,3))) and [[noreturn]]. Present this
344 // information in the documentation field.
345 std::string Result;
346 const unsigned AnnotationCount = CCS.getAnnotationCount();
347 if (AnnotationCount > 0) {
348 Result += "Annotation";
349 if (AnnotationCount == 1) {
350 Result += ": ";
351 } else /* AnnotationCount > 1 */ {
352 Result += "s: ";
353 }
354 for (unsigned I = 0; I < AnnotationCount; ++I) {
355 Result += CCS.getAnnotation(I);
356 Result.push_back(I == AnnotationCount - 1 ? '\n' : ' ');
357 }
358 }
359 // Add brief documentation (if there is any).
360 if (CCS.getBriefComment() != nullptr) {
361 if (!Result.empty()) {
362 // This means we previously added annotations. Add an extra newline
363 // character to make the annotations stand out.
364 Result.push_back('\n');
365 }
366 Result += CCS.getBriefComment();
367 }
368 return Result;
369}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000370
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000371class CompletionItemsCollector : public CodeCompleteConsumer {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000372public:
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000373 CompletionItemsCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000374 std::vector<CompletionItem> &Items)
Ilya Biryukov38d79772017-05-16 09:38:59 +0000375 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
376 Items(Items),
377 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
378 CCTUInfo(Allocator) {}
379
380 void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
381 CodeCompletionResult *Results,
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000382 unsigned NumResults) override final {
383 Items.reserve(NumResults);
384 for (unsigned I = 0; I < NumResults; ++I) {
385 auto &Result = Results[I];
386 const auto *CCS = Result.CreateCodeCompletionString(
Ilya Biryukov38d79772017-05-16 09:38:59 +0000387 S, Context, *Allocator, CCTUInfo,
388 CodeCompleteOpts.IncludeBriefComments);
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000389 assert(CCS && "Expected the CodeCompletionString to be non-null");
390 Items.push_back(ProcessCodeCompleteResult(Result, *CCS));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000391 }
Sam McCallc78ccbd2017-11-08 07:44:12 +0000392 std::sort(Items.begin(), Items.end());
Ilya Biryukov38d79772017-05-16 09:38:59 +0000393 }
394
395 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
396
397 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000398
399private:
400 CompletionItem
401 ProcessCodeCompleteResult(const CodeCompletionResult &Result,
402 const CodeCompletionString &CCS) const {
403
404 // Adjust this to InsertTextFormat::Snippet iff we encounter a
405 // CK_Placeholder chunk in SnippetCompletionItemsCollector.
406 CompletionItem Item;
407 Item.insertTextFormat = InsertTextFormat::PlainText;
408
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000409 Item.documentation = getDocumentation(CCS);
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000410
411 // Fill in the label, detail, insertText and filterText fields of the
412 // CompletionItem.
413 ProcessChunks(CCS, Item);
414
415 // Fill in the kind field of the CompletionItem.
Ilya Biryukov01e3bf82017-10-23 06:06:21 +0000416 Item.kind = getKind(Result.Kind, Result.CursorKind);
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000417
418 FillSortText(CCS, Item);
419
420 return Item;
421 }
422
423 virtual void ProcessChunks(const CodeCompletionString &CCS,
424 CompletionItem &Item) const = 0;
425
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000426 static int GetSortPriority(const CodeCompletionString &CCS) {
427 int Score = CCS.getPriority();
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000428 // Fill in the sortText of the CompletionItem.
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000429 assert(Score <= 99999 && "Expecting code completion result "
430 "priority to have at most 5-digits");
431
432 const int Penalty = 100000;
433 switch (static_cast<CXAvailabilityKind>(CCS.getAvailability())) {
434 case CXAvailability_Available:
435 // No penalty.
436 break;
437 case CXAvailability_Deprecated:
438 Score += Penalty;
439 break;
440 case CXAvailability_NotAccessible:
441 Score += 2 * Penalty;
442 break;
443 case CXAvailability_NotAvailable:
444 Score += 3 * Penalty;
445 break;
446 }
447
448 return Score;
449 }
450
451 static void FillSortText(const CodeCompletionString &CCS,
452 CompletionItem &Item) {
453 int Priority = GetSortPriority(CCS);
454 // Fill in the sortText of the CompletionItem.
455 assert(Priority <= 999999 &&
456 "Expecting sort priority to have at most 6-digits");
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000457 llvm::raw_string_ostream(Item.sortText)
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000458 << llvm::format("%06d%s", Priority, Item.filterText.c_str());
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000459 }
460
461 std::vector<CompletionItem> &Items;
462 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
463 CodeCompletionTUInfo CCTUInfo;
464
465}; // CompletionItemsCollector
466
Ilya Biryukov686ff9a2017-09-28 18:39:59 +0000467bool isInformativeQualifierChunk(CodeCompletionString::Chunk const &Chunk) {
468 return Chunk.Kind == CodeCompletionString::CK_Informative &&
469 StringRef(Chunk.Text).endswith("::");
470}
471
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000472class PlainTextCompletionItemsCollector final
473 : public CompletionItemsCollector {
474
475public:
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000476 PlainTextCompletionItemsCollector(
477 const clang::CodeCompleteOptions &CodeCompleteOpts,
478 std::vector<CompletionItem> &Items)
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000479 : CompletionItemsCollector(CodeCompleteOpts, Items) {}
480
481private:
482 void ProcessChunks(const CodeCompletionString &CCS,
483 CompletionItem &Item) const override {
484 for (const auto &Chunk : CCS) {
Ilya Biryukov686ff9a2017-09-28 18:39:59 +0000485 // Informative qualifier chunks only clutter completion results, skip
486 // them.
487 if (isInformativeQualifierChunk(Chunk))
488 continue;
489
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000490 switch (Chunk.Kind) {
491 case CodeCompletionString::CK_TypedText:
492 // There's always exactly one CK_TypedText chunk.
493 Item.insertText = Item.filterText = Chunk.Text;
494 Item.label += Chunk.Text;
495 break;
496 case CodeCompletionString::CK_ResultType:
497 assert(Item.detail.empty() && "Unexpected extraneous CK_ResultType");
498 Item.detail = Chunk.Text;
499 break;
500 case CodeCompletionString::CK_Optional:
501 break;
502 default:
503 Item.label += Chunk.Text;
504 break;
505 }
506 }
507 }
508}; // PlainTextCompletionItemsCollector
509
510class SnippetCompletionItemsCollector final : public CompletionItemsCollector {
511
512public:
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000513 SnippetCompletionItemsCollector(
514 const clang::CodeCompleteOptions &CodeCompleteOpts,
515 std::vector<CompletionItem> &Items)
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000516 : CompletionItemsCollector(CodeCompleteOpts, Items) {}
517
518private:
519 void ProcessChunks(const CodeCompletionString &CCS,
520 CompletionItem &Item) const override {
521 unsigned ArgCount = 0;
522 for (const auto &Chunk : CCS) {
Ilya Biryukov686ff9a2017-09-28 18:39:59 +0000523 // Informative qualifier chunks only clutter completion results, skip
524 // them.
525 if (isInformativeQualifierChunk(Chunk))
526 continue;
527
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000528 switch (Chunk.Kind) {
529 case CodeCompletionString::CK_TypedText:
530 // The piece of text that the user is expected to type to match
531 // the code-completion string, typically a keyword or the name of
532 // a declarator or macro.
533 Item.filterText = Chunk.Text;
Simon Pilgrim948c0bc2017-11-01 09:22:03 +0000534 LLVM_FALLTHROUGH;
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000535 case CodeCompletionString::CK_Text:
536 // A piece of text that should be placed in the buffer,
537 // e.g., parentheses or a comma in a function call.
538 Item.label += Chunk.Text;
539 Item.insertText += Chunk.Text;
540 break;
541 case CodeCompletionString::CK_Optional:
542 // A code completion string that is entirely optional.
543 // For example, an optional code completion string that
544 // describes the default arguments in a function call.
545
546 // FIXME: Maybe add an option to allow presenting the optional chunks?
547 break;
548 case CodeCompletionString::CK_Placeholder:
549 // A string that acts as a placeholder for, e.g., a function call
550 // argument.
551 ++ArgCount;
552 Item.insertText += "${" + std::to_string(ArgCount) + ':' +
553 escapeSnippet(Chunk.Text) + '}';
554 Item.label += Chunk.Text;
555 Item.insertTextFormat = InsertTextFormat::Snippet;
556 break;
557 case CodeCompletionString::CK_Informative:
558 // A piece of text that describes something about the result
559 // but should not be inserted into the buffer.
560 // For example, the word "const" for a const method, or the name of
561 // the base class for methods that are part of the base class.
562 Item.label += Chunk.Text;
563 // Don't put the informative chunks in the insertText.
564 break;
565 case CodeCompletionString::CK_ResultType:
566 // A piece of text that describes the type of an entity or,
567 // for functions and methods, the return type.
568 assert(Item.detail.empty() && "Unexpected extraneous CK_ResultType");
569 Item.detail = Chunk.Text;
570 break;
571 case CodeCompletionString::CK_CurrentParameter:
572 // A piece of text that describes the parameter that corresponds to
573 // the code-completion location within a function call, message send,
574 // macro invocation, etc.
575 //
576 // This should never be present while collecting completion items,
577 // only while collecting overload candidates.
578 llvm_unreachable("Unexpected CK_CurrentParameter while collecting "
579 "CompletionItems");
580 break;
581 case CodeCompletionString::CK_LeftParen:
582 // A left parenthesis ('(').
583 case CodeCompletionString::CK_RightParen:
584 // A right parenthesis (')').
585 case CodeCompletionString::CK_LeftBracket:
586 // A left bracket ('[').
587 case CodeCompletionString::CK_RightBracket:
588 // A right bracket (']').
589 case CodeCompletionString::CK_LeftBrace:
590 // A left brace ('{').
591 case CodeCompletionString::CK_RightBrace:
592 // A right brace ('}').
593 case CodeCompletionString::CK_LeftAngle:
594 // A left angle bracket ('<').
595 case CodeCompletionString::CK_RightAngle:
596 // A right angle bracket ('>').
597 case CodeCompletionString::CK_Comma:
598 // A comma separator (',').
599 case CodeCompletionString::CK_Colon:
600 // A colon (':').
601 case CodeCompletionString::CK_SemiColon:
602 // A semicolon (';').
603 case CodeCompletionString::CK_Equal:
604 // An '=' sign.
605 case CodeCompletionString::CK_HorizontalSpace:
606 // Horizontal whitespace (' ').
607 Item.insertText += Chunk.Text;
608 Item.label += Chunk.Text;
609 break;
610 case CodeCompletionString::CK_VerticalSpace:
611 // Vertical whitespace ('\n' or '\r\n', depending on the
612 // platform).
613 Item.insertText += Chunk.Text;
614 // Don't even add a space to the label.
615 break;
616 }
617 }
618 }
619}; // SnippetCompletionItemsCollector
Ilya Biryukov38d79772017-05-16 09:38:59 +0000620
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000621class SignatureHelpCollector final : public CodeCompleteConsumer {
622
623public:
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000624 SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000625 SignatureHelp &SigHelp)
626 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
627 SigHelp(SigHelp),
628 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
629 CCTUInfo(Allocator) {}
630
631 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
632 OverloadCandidate *Candidates,
633 unsigned NumCandidates) override {
634 SigHelp.signatures.reserve(NumCandidates);
635 // FIXME(rwols): How can we determine the "active overload candidate"?
636 // Right now the overloaded candidates seem to be provided in a "best fit"
637 // order, so I'm not too worried about this.
638 SigHelp.activeSignature = 0;
Simon Pilgrima3aa7242017-10-07 12:24:10 +0000639 assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000640 "too many arguments");
641 SigHelp.activeParameter = static_cast<int>(CurrentArg);
642 for (unsigned I = 0; I < NumCandidates; ++I) {
643 const auto &Candidate = Candidates[I];
644 const auto *CCS = Candidate.CreateSignatureString(
645 CurrentArg, S, *Allocator, CCTUInfo, true);
646 assert(CCS && "Expected the CodeCompletionString to be non-null");
647 SigHelp.signatures.push_back(ProcessOverloadCandidate(Candidate, *CCS));
648 }
649 }
650
651 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
652
653 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
654
655private:
656 SignatureInformation
657 ProcessOverloadCandidate(const OverloadCandidate &Candidate,
658 const CodeCompletionString &CCS) const {
659 SignatureInformation Result;
660 const char *ReturnType = nullptr;
661
662 Result.documentation = getDocumentation(CCS);
663
664 for (const auto &Chunk : CCS) {
665 switch (Chunk.Kind) {
666 case CodeCompletionString::CK_ResultType:
667 // A piece of text that describes the type of an entity or,
668 // for functions and methods, the return type.
669 assert(!ReturnType && "Unexpected CK_ResultType");
670 ReturnType = Chunk.Text;
671 break;
672 case CodeCompletionString::CK_Placeholder:
673 // A string that acts as a placeholder for, e.g., a function call
674 // argument.
675 // Intentional fallthrough here.
676 case CodeCompletionString::CK_CurrentParameter: {
677 // A piece of text that describes the parameter that corresponds to
678 // the code-completion location within a function call, message send,
679 // macro invocation, etc.
680 Result.label += Chunk.Text;
681 ParameterInformation Info;
682 Info.label = Chunk.Text;
683 Result.parameters.push_back(std::move(Info));
684 break;
685 }
686 case CodeCompletionString::CK_Optional: {
687 // The rest of the parameters are defaulted/optional.
688 assert(Chunk.Optional &&
689 "Expected the optional code completion string to be non-null.");
690 Result.label +=
691 getOptionalParameters(*Chunk.Optional, Result.parameters);
692 break;
693 }
694 case CodeCompletionString::CK_VerticalSpace:
695 break;
696 default:
697 Result.label += Chunk.Text;
698 break;
699 }
700 }
701 if (ReturnType) {
702 Result.label += " -> ";
703 Result.label += ReturnType;
704 }
705 return Result;
706 }
707
708 SignatureHelp &SigHelp;
709 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
710 CodeCompletionTUInfo CCTUInfo;
711
712}; // SignatureHelpCollector
713
714bool invokeCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000715 const clang::CodeCompleteOptions &Options,
716 PathRef FileName,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000717 const tooling::CompileCommand &Command,
718 PrecompiledPreamble const *Preamble, StringRef Contents,
719 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
720 std::shared_ptr<PCHContainerOperations> PCHs,
721 clangd::Logger &Logger) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000722 std::vector<const char *> ArgStrs;
723 for (const auto &S : Command.CommandLine)
724 ArgStrs.push_back(S.c_str());
725
Krasimir Georgieve4130d52017-07-25 11:37:43 +0000726 VFS->setCurrentWorkingDirectory(Command.Directory);
727
Ilya Biryukov04db3682017-07-21 13:29:29 +0000728 std::unique_ptr<CompilerInvocation> CI;
729 EmptyDiagsConsumer DummyDiagsConsumer;
730 {
731 IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
732 CompilerInstance::createDiagnostics(new DiagnosticOptions,
733 &DummyDiagsConsumer, false);
734 CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
735 }
736 assert(CI && "Couldn't create CompilerInvocation");
737
738 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
739 llvm::MemoryBuffer::getMemBufferCopy(Contents, FileName);
740
741 // Attempt to reuse the PCH from precompiled preamble, if it was built.
Ilya Biryukov04db3682017-07-21 13:29:29 +0000742 if (Preamble) {
743 auto Bounds =
744 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
Ilya Biryukov02d58702017-08-01 15:51:38 +0000745 if (!Preamble->CanReuse(*CI, ContentsBuffer.get(), Bounds, VFS.get()))
746 Preamble = nullptr;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000747 }
748
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000749 auto Clang = prepareCompilerInstance(
750 std::move(CI), Preamble, std::move(ContentsBuffer), std::move(PCHs),
751 std::move(VFS), DummyDiagsConsumer);
Ilya Biryukov04db3682017-07-21 13:29:29 +0000752 auto &DiagOpts = Clang->getDiagnosticOpts();
753 DiagOpts.IgnoreWarnings = true;
754
755 auto &FrontendOpts = Clang->getFrontendOpts();
756 FrontendOpts.SkipFunctionBodies = true;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000757 FrontendOpts.CodeCompleteOpts = Options;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000758 FrontendOpts.CodeCompletionAt.FileName = FileName;
759 FrontendOpts.CodeCompletionAt.Line = Pos.line + 1;
760 FrontendOpts.CodeCompletionAt.Column = Pos.character + 1;
761
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000762 Clang->setCodeCompletionConsumer(Consumer.release());
Ilya Biryukov38d79772017-05-16 09:38:59 +0000763
Ilya Biryukov04db3682017-07-21 13:29:29 +0000764 SyntaxOnlyAction Action;
765 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
Ilya Biryukove5128f72017-09-20 07:24:15 +0000766 Logger.log("BeginSourceFile() failed when running codeComplete for " +
767 FileName);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000768 return false;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000769 }
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000770 if (!Action.Execute()) {
Ilya Biryukove5128f72017-09-20 07:24:15 +0000771 Logger.log("Execute() failed when running codeComplete for " + FileName);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000772 return false;
773 }
Ilya Biryukove5128f72017-09-20 07:24:15 +0000774
Ilya Biryukov04db3682017-07-21 13:29:29 +0000775 Action.EndSourceFile();
Ilya Biryukov38d79772017-05-16 09:38:59 +0000776
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000777 return true;
778}
779
780} // namespace
781
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000782clangd::CodeCompleteOptions::CodeCompleteOptions(
783 bool EnableSnippetsAndCodePatterns)
784 : CodeCompleteOptions() {
785 EnableSnippets = EnableSnippetsAndCodePatterns;
786 IncludeCodePatterns = EnableSnippetsAndCodePatterns;
787}
788
789clangd::CodeCompleteOptions::CodeCompleteOptions(bool EnableSnippets,
790 bool IncludeCodePatterns,
791 bool IncludeMacros,
792 bool IncludeGlobals,
793 bool IncludeBriefComments)
794 : EnableSnippets(EnableSnippets), IncludeCodePatterns(IncludeCodePatterns),
795 IncludeMacros(IncludeMacros), IncludeGlobals(IncludeGlobals),
796 IncludeBriefComments(IncludeBriefComments) {}
797
798clang::CodeCompleteOptions clangd::CodeCompleteOptions::getClangCompleteOpts() {
799 clang::CodeCompleteOptions Result;
800 Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns;
801 Result.IncludeMacros = IncludeMacros;
802 Result.IncludeGlobals = IncludeGlobals;
803 Result.IncludeBriefComments = IncludeBriefComments;
804
805 return Result;
806}
807
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000808std::vector<CompletionItem>
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000809clangd::codeComplete(PathRef FileName, const tooling::CompileCommand &Command,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000810 PrecompiledPreamble const *Preamble, StringRef Contents,
811 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
812 std::shared_ptr<PCHContainerOperations> PCHs,
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000813 clangd::CodeCompleteOptions Opts, clangd::Logger &Logger) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000814 std::vector<CompletionItem> Results;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000815 std::unique_ptr<CodeCompleteConsumer> Consumer;
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000816 clang::CodeCompleteOptions ClangCompleteOpts = Opts.getClangCompleteOpts();
817 if (Opts.EnableSnippets) {
818 Consumer = llvm::make_unique<SnippetCompletionItemsCollector>(
819 ClangCompleteOpts, Results);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000820 } else {
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000821 Consumer = llvm::make_unique<PlainTextCompletionItemsCollector>(
822 ClangCompleteOpts, Results);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000823 }
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000824 invokeCodeComplete(std::move(Consumer), ClangCompleteOpts, FileName, Command,
825 Preamble, Contents, Pos, std::move(VFS), std::move(PCHs),
826 Logger);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000827 return Results;
828}
829
830SignatureHelp
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000831clangd::signatureHelp(PathRef FileName, const tooling::CompileCommand &Command,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000832 PrecompiledPreamble const *Preamble, StringRef Contents,
833 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
834 std::shared_ptr<PCHContainerOperations> PCHs,
835 clangd::Logger &Logger) {
836 SignatureHelp Result;
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000837 clang::CodeCompleteOptions Options;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000838 Options.IncludeGlobals = false;
839 Options.IncludeMacros = false;
840 Options.IncludeCodePatterns = false;
841 Options.IncludeBriefComments = true;
842 invokeCodeComplete(llvm::make_unique<SignatureHelpCollector>(Options, Result),
843 Options, FileName, Command, Preamble, Contents, Pos,
844 std::move(VFS), std::move(PCHs), Logger);
845 return Result;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000846}
847
Ilya Biryukov02d58702017-08-01 15:51:38 +0000848void clangd::dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) {
849 AST.getASTContext().getTranslationUnitDecl()->dump(OS, true);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000850}
Ilya Biryukovf01af682017-05-23 13:42:59 +0000851
Ilya Biryukov02d58702017-08-01 15:51:38 +0000852llvm::Optional<ParsedAST>
853ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI,
854 const PrecompiledPreamble *Preamble,
855 ArrayRef<serialization::DeclID> PreambleDeclIDs,
856 std::unique_ptr<llvm::MemoryBuffer> Buffer,
857 std::shared_ptr<PCHContainerOperations> PCHs,
Ilya Biryukove5128f72017-09-20 07:24:15 +0000858 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
859 clangd::Logger &Logger) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000860
861 std::vector<DiagWithFixIts> ASTDiags;
862 StoreDiagsConsumer UnitDiagsConsumer(/*ref*/ ASTDiags);
863
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000864 auto Clang = prepareCompilerInstance(
865 std::move(CI), Preamble, std::move(Buffer), std::move(PCHs),
866 std::move(VFS), /*ref*/ UnitDiagsConsumer);
Ilya Biryukov04db3682017-07-21 13:29:29 +0000867
868 // Recover resources if we crash before exiting this method.
869 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
870 Clang.get());
871
872 auto Action = llvm::make_unique<ClangdFrontendAction>();
Ilya Biryukove5128f72017-09-20 07:24:15 +0000873 const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
874 if (!Action->BeginSourceFile(*Clang, MainInput)) {
875 Logger.log("BeginSourceFile() failed when building AST for " +
876 MainInput.getFile());
Ilya Biryukov04db3682017-07-21 13:29:29 +0000877 return llvm::None;
878 }
Ilya Biryukove5128f72017-09-20 07:24:15 +0000879 if (!Action->Execute())
880 Logger.log("Execute() failed when building AST for " + MainInput.getFile());
Ilya Biryukov04db3682017-07-21 13:29:29 +0000881
882 // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
883 // has a longer lifetime.
884 Clang->getDiagnostics().setClient(new EmptyDiagsConsumer);
885
886 std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls();
887 std::vector<serialization::DeclID> PendingDecls;
888 if (Preamble) {
889 PendingDecls.reserve(PreambleDeclIDs.size());
890 PendingDecls.insert(PendingDecls.begin(), PreambleDeclIDs.begin(),
891 PreambleDeclIDs.end());
892 }
893
894 return ParsedAST(std::move(Clang), std::move(Action), std::move(ParsedDecls),
895 std::move(PendingDecls), std::move(ASTDiags));
896}
897
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000898namespace {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000899
900SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
901 const FileEntry *FE,
902 unsigned Offset) {
903 SourceLocation FileLoc = Mgr.translateFileLineCol(FE, 1, 1);
904 return Mgr.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
905}
906
907SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
908 const FileEntry *FE, Position Pos) {
909 SourceLocation InputLoc =
910 Mgr.translateFileLineCol(FE, Pos.line + 1, Pos.character + 1);
911 return Mgr.getMacroArgExpandedLocation(InputLoc);
912}
913
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000914/// Finds declarations locations that a given source location refers to.
915class DeclarationLocationsFinder : public index::IndexDataConsumer {
916 std::vector<Location> DeclarationLocations;
917 const SourceLocation &SearchedLocation;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000918 const ASTContext &AST;
919 Preprocessor &PP;
920
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000921public:
922 DeclarationLocationsFinder(raw_ostream &OS,
Ilya Biryukov04db3682017-07-21 13:29:29 +0000923 const SourceLocation &SearchedLocation,
924 ASTContext &AST, Preprocessor &PP)
925 : SearchedLocation(SearchedLocation), AST(AST), PP(PP) {}
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000926
927 std::vector<Location> takeLocations() {
928 // Don't keep the same location multiple times.
929 // This can happen when nodes in the AST are visited twice.
930 std::sort(DeclarationLocations.begin(), DeclarationLocations.end());
Kirill Bobyrev46213872017-06-28 20:57:28 +0000931 auto last =
932 std::unique(DeclarationLocations.begin(), DeclarationLocations.end());
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000933 DeclarationLocations.erase(last, DeclarationLocations.end());
934 return std::move(DeclarationLocations);
935 }
936
Ilya Biryukov02d58702017-08-01 15:51:38 +0000937 bool
938 handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
939 ArrayRef<index::SymbolRelation> Relations, FileID FID,
940 unsigned Offset,
941 index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000942 if (isSearchedLocation(FID, Offset)) {
943 addDeclarationLocation(D->getSourceRange());
944 }
945 return true;
946 }
947
948private:
949 bool isSearchedLocation(FileID FID, unsigned Offset) const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000950 const SourceManager &SourceMgr = AST.getSourceManager();
951 return SourceMgr.getFileOffset(SearchedLocation) == Offset &&
952 SourceMgr.getFileID(SearchedLocation) == FID;
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000953 }
954
Ilya Biryukov04db3682017-07-21 13:29:29 +0000955 void addDeclarationLocation(const SourceRange &ValSourceRange) {
956 const SourceManager &SourceMgr = AST.getSourceManager();
957 const LangOptions &LangOpts = AST.getLangOpts();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000958 SourceLocation LocStart = ValSourceRange.getBegin();
959 SourceLocation LocEnd = Lexer::getLocForEndOfToken(ValSourceRange.getEnd(),
Ilya Biryukov04db3682017-07-21 13:29:29 +0000960 0, SourceMgr, LangOpts);
Kirill Bobyrev46213872017-06-28 20:57:28 +0000961 Position Begin;
962 Begin.line = SourceMgr.getSpellingLineNumber(LocStart) - 1;
963 Begin.character = SourceMgr.getSpellingColumnNumber(LocStart) - 1;
964 Position End;
965 End.line = SourceMgr.getSpellingLineNumber(LocEnd) - 1;
966 End.character = SourceMgr.getSpellingColumnNumber(LocEnd) - 1;
967 Range R = {Begin, End};
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000968 Location L;
Marc-Andre Laperleba070102017-11-07 16:16:45 +0000969 if (const FileEntry *F =
970 SourceMgr.getFileEntryForID(SourceMgr.getFileID(LocStart))) {
971 StringRef FilePath = F->tryGetRealPathName();
972 if (FilePath.empty())
973 FilePath = F->getName();
974 L.uri = URI::fromFile(FilePath);
975 L.range = R;
976 DeclarationLocations.push_back(L);
977 }
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000978 }
979
Kirill Bobyrev46213872017-06-28 20:57:28 +0000980 void finish() override {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000981 // Also handle possible macro at the searched location.
982 Token Result;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000983 if (!Lexer::getRawToken(SearchedLocation, Result, AST.getSourceManager(),
984 AST.getLangOpts(), false)) {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000985 if (Result.is(tok::raw_identifier)) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000986 PP.LookUpIdentifierInfo(Result);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000987 }
Ilya Biryukov04db3682017-07-21 13:29:29 +0000988 IdentifierInfo *IdentifierInfo = Result.getIdentifierInfo();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000989 if (IdentifierInfo && IdentifierInfo->hadMacroDefinition()) {
990 std::pair<FileID, unsigned int> DecLoc =
Ilya Biryukov04db3682017-07-21 13:29:29 +0000991 AST.getSourceManager().getDecomposedExpansionLoc(SearchedLocation);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000992 // Get the definition just before the searched location so that a macro
993 // referenced in a '#undef MACRO' can still be found.
Ilya Biryukov04db3682017-07-21 13:29:29 +0000994 SourceLocation BeforeSearchedLocation = getMacroArgExpandedLocation(
995 AST.getSourceManager(),
996 AST.getSourceManager().getFileEntryForID(DecLoc.first),
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000997 DecLoc.second - 1);
998 MacroDefinition MacroDef =
Ilya Biryukov04db3682017-07-21 13:29:29 +0000999 PP.getMacroDefinitionAtLoc(IdentifierInfo, BeforeSearchedLocation);
1000 MacroInfo *MacroInf = MacroDef.getMacroInfo();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001001 if (MacroInf) {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001002 addDeclarationLocation(SourceRange(MacroInf->getDefinitionLoc(),
1003 MacroInf->getDefinitionEndLoc()));
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001004 }
1005 }
1006 }
1007 }
1008};
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001009
Ilya Biryukov02d58702017-08-01 15:51:38 +00001010} // namespace
Ilya Biryukov04db3682017-07-21 13:29:29 +00001011
Ilya Biryukove5128f72017-09-20 07:24:15 +00001012std::vector<Location> clangd::findDefinitions(ParsedAST &AST, Position Pos,
1013 clangd::Logger &Logger) {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001014 const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
1015 const FileEntry *FE = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
1016 if (!FE)
1017 return {};
1018
1019 SourceLocation SourceLocationBeg = getBeginningOfIdentifier(AST, Pos, FE);
1020
1021 auto DeclLocationsFinder = std::make_shared<DeclarationLocationsFinder>(
1022 llvm::errs(), SourceLocationBeg, AST.getASTContext(),
1023 AST.getPreprocessor());
1024 index::IndexingOptions IndexOpts;
1025 IndexOpts.SystemSymbolFilter =
1026 index::IndexingOptions::SystemSymbolFilterKind::All;
1027 IndexOpts.IndexFunctionLocals = true;
1028
1029 indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(),
1030 DeclLocationsFinder, IndexOpts);
1031
1032 return DeclLocationsFinder->takeLocations();
1033}
1034
1035void ParsedAST::ensurePreambleDeclsDeserialized() {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001036 if (PendingTopLevelDecls.empty())
1037 return;
1038
1039 std::vector<const Decl *> Resolved;
1040 Resolved.reserve(PendingTopLevelDecls.size());
1041
1042 ExternalASTSource &Source = *getASTContext().getExternalSource();
1043 for (serialization::DeclID TopLevelDecl : PendingTopLevelDecls) {
1044 // Resolve the declaration ID to an actual declaration, possibly
1045 // deserializing the declaration in the process.
1046 if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
1047 Resolved.push_back(D);
1048 }
1049
1050 TopLevelDecls.reserve(TopLevelDecls.size() + PendingTopLevelDecls.size());
1051 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1052
1053 PendingTopLevelDecls.clear();
1054}
1055
Ilya Biryukov02d58702017-08-01 15:51:38 +00001056ParsedAST::ParsedAST(ParsedAST &&Other) = default;
Ilya Biryukov04db3682017-07-21 13:29:29 +00001057
Ilya Biryukov02d58702017-08-01 15:51:38 +00001058ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
Ilya Biryukov04db3682017-07-21 13:29:29 +00001059
Ilya Biryukov02d58702017-08-01 15:51:38 +00001060ParsedAST::~ParsedAST() {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001061 if (Action) {
1062 Action->EndSourceFile();
1063 }
1064}
1065
Ilya Biryukov02d58702017-08-01 15:51:38 +00001066ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
1067
1068const ASTContext &ParsedAST::getASTContext() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001069 return Clang->getASTContext();
1070}
1071
Ilya Biryukov02d58702017-08-01 15:51:38 +00001072Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
Ilya Biryukov04db3682017-07-21 13:29:29 +00001073
Ilya Biryukov02d58702017-08-01 15:51:38 +00001074const Preprocessor &ParsedAST::getPreprocessor() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001075 return Clang->getPreprocessor();
1076}
1077
Ilya Biryukov02d58702017-08-01 15:51:38 +00001078ArrayRef<const Decl *> ParsedAST::getTopLevelDecls() {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001079 ensurePreambleDeclsDeserialized();
1080 return TopLevelDecls;
1081}
1082
Ilya Biryukov02d58702017-08-01 15:51:38 +00001083const std::vector<DiagWithFixIts> &ParsedAST::getDiagnostics() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001084 return Diags;
1085}
1086
Ilya Biryukov02d58702017-08-01 15:51:38 +00001087ParsedAST::ParsedAST(std::unique_ptr<CompilerInstance> Clang,
1088 std::unique_ptr<FrontendAction> Action,
1089 std::vector<const Decl *> TopLevelDecls,
1090 std::vector<serialization::DeclID> PendingTopLevelDecls,
1091 std::vector<DiagWithFixIts> Diags)
Ilya Biryukov04db3682017-07-21 13:29:29 +00001092 : Clang(std::move(Clang)), Action(std::move(Action)),
1093 Diags(std::move(Diags)), TopLevelDecls(std::move(TopLevelDecls)),
1094 PendingTopLevelDecls(std::move(PendingTopLevelDecls)) {
1095 assert(this->Clang);
1096 assert(this->Action);
1097}
1098
Ilya Biryukov02d58702017-08-01 15:51:38 +00001099ParsedASTWrapper::ParsedASTWrapper(ParsedASTWrapper &&Wrapper)
1100 : AST(std::move(Wrapper.AST)) {}
1101
1102ParsedASTWrapper::ParsedASTWrapper(llvm::Optional<ParsedAST> AST)
1103 : AST(std::move(AST)) {}
1104
1105PreambleData::PreambleData(PrecompiledPreamble Preamble,
1106 std::vector<serialization::DeclID> TopLevelDeclIDs,
1107 std::vector<DiagWithFixIts> Diags)
Ilya Biryukov04db3682017-07-21 13:29:29 +00001108 : Preamble(std::move(Preamble)),
1109 TopLevelDeclIDs(std::move(TopLevelDeclIDs)), Diags(std::move(Diags)) {}
Ilya Biryukov02d58702017-08-01 15:51:38 +00001110
1111std::shared_ptr<CppFile>
1112CppFile::Create(PathRef FileName, tooling::CompileCommand Command,
Ilya Biryukov83ca8a22017-09-20 10:46:58 +00001113 std::shared_ptr<PCHContainerOperations> PCHs,
1114 clangd::Logger &Logger) {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001115 return std::shared_ptr<CppFile>(
Ilya Biryukove5128f72017-09-20 07:24:15 +00001116 new CppFile(FileName, std::move(Command), std::move(PCHs), Logger));
Ilya Biryukov02d58702017-08-01 15:51:38 +00001117}
1118
1119CppFile::CppFile(PathRef FileName, tooling::CompileCommand Command,
Ilya Biryukove5128f72017-09-20 07:24:15 +00001120 std::shared_ptr<PCHContainerOperations> PCHs,
1121 clangd::Logger &Logger)
Ilya Biryukov02d58702017-08-01 15:51:38 +00001122 : FileName(FileName), Command(std::move(Command)), RebuildCounter(0),
Ilya Biryukove5128f72017-09-20 07:24:15 +00001123 RebuildInProgress(false), PCHs(std::move(PCHs)), Logger(Logger) {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001124
1125 std::lock_guard<std::mutex> Lock(Mutex);
1126 LatestAvailablePreamble = nullptr;
1127 PreamblePromise.set_value(nullptr);
1128 PreambleFuture = PreamblePromise.get_future();
1129
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001130 ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
Ilya Biryukov02d58702017-08-01 15:51:38 +00001131 ASTFuture = ASTPromise.get_future();
1132}
1133
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001134void CppFile::cancelRebuild() { deferCancelRebuild()(); }
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001135
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001136UniqueFunction<void()> CppFile::deferCancelRebuild() {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001137 std::unique_lock<std::mutex> Lock(Mutex);
1138 // Cancel an ongoing rebuild, if any, and wait for it to finish.
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001139 unsigned RequestRebuildCounter = ++this->RebuildCounter;
Ilya Biryukov02d58702017-08-01 15:51:38 +00001140 // Rebuild asserts that futures aren't ready if rebuild is cancelled.
1141 // We want to keep this invariant.
1142 if (futureIsReady(PreambleFuture)) {
1143 PreamblePromise = std::promise<std::shared_ptr<const PreambleData>>();
1144 PreambleFuture = PreamblePromise.get_future();
1145 }
1146 if (futureIsReady(ASTFuture)) {
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001147 ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001148 ASTFuture = ASTPromise.get_future();
1149 }
Ilya Biryukov02d58702017-08-01 15:51:38 +00001150
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001151 Lock.unlock();
1152 // Notify about changes to RebuildCounter.
1153 RebuildCond.notify_all();
1154
1155 std::shared_ptr<CppFile> That = shared_from_this();
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001156 return [That, RequestRebuildCounter]() {
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001157 std::unique_lock<std::mutex> Lock(That->Mutex);
1158 CppFile *This = &*That;
1159 This->RebuildCond.wait(Lock, [This, RequestRebuildCounter]() {
1160 return !This->RebuildInProgress ||
1161 This->RebuildCounter != RequestRebuildCounter;
1162 });
1163
1164 // This computation got cancelled itself, do nothing.
1165 if (This->RebuildCounter != RequestRebuildCounter)
1166 return;
1167
1168 // Set empty results for Promises.
1169 That->PreamblePromise.set_value(nullptr);
1170 That->ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001171 };
Ilya Biryukov02d58702017-08-01 15:51:38 +00001172}
1173
1174llvm::Optional<std::vector<DiagWithFixIts>>
1175CppFile::rebuild(StringRef NewContents,
1176 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001177 return deferRebuild(NewContents, std::move(VFS))();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001178}
1179
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001180UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>()>
Ilya Biryukov02d58702017-08-01 15:51:38 +00001181CppFile::deferRebuild(StringRef NewContents,
1182 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
1183 std::shared_ptr<const PreambleData> OldPreamble;
1184 std::shared_ptr<PCHContainerOperations> PCHs;
1185 unsigned RequestRebuildCounter;
1186 {
1187 std::unique_lock<std::mutex> Lock(Mutex);
1188 // Increase RebuildCounter to cancel all ongoing FinishRebuild operations.
1189 // They will try to exit as early as possible and won't call set_value on
1190 // our promises.
1191 RequestRebuildCounter = ++this->RebuildCounter;
1192 PCHs = this->PCHs;
1193
1194 // Remember the preamble to be used during rebuild.
1195 OldPreamble = this->LatestAvailablePreamble;
1196 // Setup std::promises and std::futures for Preamble and AST. Corresponding
1197 // futures will wait until the rebuild process is finished.
1198 if (futureIsReady(this->PreambleFuture)) {
1199 this->PreamblePromise =
1200 std::promise<std::shared_ptr<const PreambleData>>();
1201 this->PreambleFuture = this->PreamblePromise.get_future();
1202 }
1203 if (futureIsReady(this->ASTFuture)) {
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001204 this->ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001205 this->ASTFuture = this->ASTPromise.get_future();
1206 }
1207 } // unlock Mutex.
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001208 // Notify about changes to RebuildCounter.
1209 RebuildCond.notify_all();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001210
1211 // A helper to function to finish the rebuild. May be run on a different
1212 // thread.
1213
1214 // Don't let this CppFile die before rebuild is finished.
1215 std::shared_ptr<CppFile> That = shared_from_this();
1216 auto FinishRebuild = [OldPreamble, VFS, RequestRebuildCounter, PCHs,
1217 That](std::string NewContents)
1218 -> llvm::Optional<std::vector<DiagWithFixIts>> {
1219 // Only one execution of this method is possible at a time.
1220 // RebuildGuard will wait for any ongoing rebuilds to finish and will put us
1221 // into a state for doing a rebuild.
1222 RebuildGuard Rebuild(*That, RequestRebuildCounter);
1223 if (Rebuild.wasCancelledBeforeConstruction())
1224 return llvm::None;
1225
1226 std::vector<const char *> ArgStrs;
1227 for (const auto &S : That->Command.CommandLine)
1228 ArgStrs.push_back(S.c_str());
1229
1230 VFS->setCurrentWorkingDirectory(That->Command.Directory);
1231
1232 std::unique_ptr<CompilerInvocation> CI;
1233 {
1234 // FIXME(ibiryukov): store diagnostics from CommandLine when we start
1235 // reporting them.
1236 EmptyDiagsConsumer CommandLineDiagsConsumer;
1237 IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
1238 CompilerInstance::createDiagnostics(new DiagnosticOptions,
1239 &CommandLineDiagsConsumer, false);
1240 CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
1241 }
1242 assert(CI && "Couldn't create CompilerInvocation");
1243
1244 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1245 llvm::MemoryBuffer::getMemBufferCopy(NewContents, That->FileName);
1246
1247 // A helper function to rebuild the preamble or reuse the existing one. Does
1248 // not mutate any fields, only does the actual computation.
1249 auto DoRebuildPreamble = [&]() -> std::shared_ptr<const PreambleData> {
1250 auto Bounds =
1251 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
1252 if (OldPreamble && OldPreamble->Preamble.CanReuse(
1253 *CI, ContentsBuffer.get(), Bounds, VFS.get())) {
1254 return OldPreamble;
1255 }
1256
Sam McCall8567cb32017-11-02 09:21:51 +00001257 trace::Span Tracer(llvm::Twine("Preamble: ") + That->FileName);
Ilya Biryukov02d58702017-08-01 15:51:38 +00001258 std::vector<DiagWithFixIts> PreambleDiags;
1259 StoreDiagsConsumer PreambleDiagnosticsConsumer(/*ref*/ PreambleDiags);
1260 IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
1261 CompilerInstance::createDiagnostics(
1262 &CI->getDiagnosticOpts(), &PreambleDiagnosticsConsumer, false);
1263 CppFilePreambleCallbacks SerializedDeclsCollector;
1264 auto BuiltPreamble = PrecompiledPreamble::Build(
1265 *CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine, VFS, PCHs,
1266 SerializedDeclsCollector);
1267
1268 if (BuiltPreamble) {
1269 return std::make_shared<PreambleData>(
1270 std::move(*BuiltPreamble),
1271 SerializedDeclsCollector.takeTopLevelDeclIDs(),
1272 std::move(PreambleDiags));
1273 } else {
1274 return nullptr;
1275 }
1276 };
1277
1278 // Compute updated Preamble.
1279 std::shared_ptr<const PreambleData> NewPreamble = DoRebuildPreamble();
1280 // Publish the new Preamble.
1281 {
1282 std::lock_guard<std::mutex> Lock(That->Mutex);
1283 // We always set LatestAvailablePreamble to the new value, hoping that it
1284 // will still be usable in the further requests.
1285 That->LatestAvailablePreamble = NewPreamble;
1286 if (RequestRebuildCounter != That->RebuildCounter)
1287 return llvm::None; // Our rebuild request was cancelled, do nothing.
1288 That->PreamblePromise.set_value(NewPreamble);
1289 } // unlock Mutex
1290
1291 // Prepare the Preamble and supplementary data for rebuilding AST.
1292 const PrecompiledPreamble *PreambleForAST = nullptr;
1293 ArrayRef<serialization::DeclID> SerializedPreambleDecls = llvm::None;
1294 std::vector<DiagWithFixIts> Diagnostics;
1295 if (NewPreamble) {
1296 PreambleForAST = &NewPreamble->Preamble;
1297 SerializedPreambleDecls = NewPreamble->TopLevelDeclIDs;
1298 Diagnostics.insert(Diagnostics.begin(), NewPreamble->Diags.begin(),
1299 NewPreamble->Diags.end());
1300 }
1301
1302 // Compute updated AST.
Sam McCall8567cb32017-11-02 09:21:51 +00001303 llvm::Optional<ParsedAST> NewAST;
1304 {
1305 trace::Span Tracer(llvm::Twine("Build: ") + That->FileName);
1306 NewAST = ParsedAST::Build(
1307 std::move(CI), PreambleForAST, SerializedPreambleDecls,
1308 std::move(ContentsBuffer), PCHs, VFS, That->Logger);
1309 }
Ilya Biryukov02d58702017-08-01 15:51:38 +00001310
1311 if (NewAST) {
1312 Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(),
1313 NewAST->getDiagnostics().end());
1314 } else {
1315 // Don't report even Preamble diagnostics if we coulnd't build AST.
1316 Diagnostics.clear();
1317 }
1318
1319 // Publish the new AST.
1320 {
1321 std::lock_guard<std::mutex> Lock(That->Mutex);
1322 if (RequestRebuildCounter != That->RebuildCounter)
1323 return Diagnostics; // Our rebuild request was cancelled, don't set
1324 // ASTPromise.
1325
Ilya Biryukov574b7532017-08-02 09:08:39 +00001326 That->ASTPromise.set_value(
1327 std::make_shared<ParsedASTWrapper>(std::move(NewAST)));
Ilya Biryukov02d58702017-08-01 15:51:38 +00001328 } // unlock Mutex
1329
1330 return Diagnostics;
1331 };
1332
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001333 return BindWithForward(FinishRebuild, NewContents.str());
Ilya Biryukov02d58702017-08-01 15:51:38 +00001334}
1335
1336std::shared_future<std::shared_ptr<const PreambleData>>
1337CppFile::getPreamble() const {
1338 std::lock_guard<std::mutex> Lock(Mutex);
1339 return PreambleFuture;
1340}
1341
1342std::shared_ptr<const PreambleData> CppFile::getPossiblyStalePreamble() const {
1343 std::lock_guard<std::mutex> Lock(Mutex);
1344 return LatestAvailablePreamble;
1345}
1346
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001347std::shared_future<std::shared_ptr<ParsedASTWrapper>> CppFile::getAST() const {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001348 std::lock_guard<std::mutex> Lock(Mutex);
1349 return ASTFuture;
1350}
1351
1352tooling::CompileCommand const &CppFile::getCompileCommand() const {
1353 return Command;
1354}
1355
1356CppFile::RebuildGuard::RebuildGuard(CppFile &File,
1357 unsigned RequestRebuildCounter)
1358 : File(File), RequestRebuildCounter(RequestRebuildCounter) {
1359 std::unique_lock<std::mutex> Lock(File.Mutex);
1360 WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
1361 if (WasCancelledBeforeConstruction)
1362 return;
1363
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001364 File.RebuildCond.wait(Lock, [&File, RequestRebuildCounter]() {
1365 return !File.RebuildInProgress ||
1366 File.RebuildCounter != RequestRebuildCounter;
1367 });
Ilya Biryukov02d58702017-08-01 15:51:38 +00001368
1369 WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
1370 if (WasCancelledBeforeConstruction)
1371 return;
1372
1373 File.RebuildInProgress = true;
1374}
1375
1376bool CppFile::RebuildGuard::wasCancelledBeforeConstruction() const {
1377 return WasCancelledBeforeConstruction;
1378}
1379
1380CppFile::RebuildGuard::~RebuildGuard() {
1381 if (WasCancelledBeforeConstruction)
1382 return;
1383
1384 std::unique_lock<std::mutex> Lock(File.Mutex);
1385 assert(File.RebuildInProgress);
1386 File.RebuildInProgress = false;
1387
1388 if (File.RebuildCounter == RequestRebuildCounter) {
1389 // Our rebuild request was successful.
1390 assert(futureIsReady(File.ASTFuture));
1391 assert(futureIsReady(File.PreambleFuture));
1392 } else {
1393 // Our rebuild request was cancelled, because further reparse was requested.
1394 assert(!futureIsReady(File.ASTFuture));
1395 assert(!futureIsReady(File.PreambleFuture));
1396 }
1397
1398 Lock.unlock();
1399 File.RebuildCond.notify_all();
1400}
Haojian Wu345099c2017-11-09 11:30:04 +00001401
1402SourceLocation clangd::getBeginningOfIdentifier(ParsedAST &Unit,
1403 const Position &Pos,
1404 const FileEntry *FE) {
1405 // The language server protocol uses zero-based line and column numbers.
1406 // Clang uses one-based numbers.
1407
1408 const ASTContext &AST = Unit.getASTContext();
1409 const SourceManager &SourceMgr = AST.getSourceManager();
1410
1411 SourceLocation InputLocation =
1412 getMacroArgExpandedLocation(SourceMgr, FE, Pos);
1413 if (Pos.character == 0) {
1414 return InputLocation;
1415 }
1416
1417 // This handle cases where the position is in the middle of a token or right
1418 // after the end of a token. In theory we could just use GetBeginningOfToken
1419 // to find the start of the token at the input position, but this doesn't
1420 // work when right after the end, i.e. foo|.
1421 // So try to go back by one and see if we're still inside the an identifier
1422 // token. If so, Take the beginning of this token.
1423 // (It should be the same identifier because you can't have two adjacent
1424 // identifiers without another token in between.)
1425 SourceLocation PeekBeforeLocation = getMacroArgExpandedLocation(
1426 SourceMgr, FE, Position{Pos.line, Pos.character - 1});
1427 Token Result;
1428 if (Lexer::getRawToken(PeekBeforeLocation, Result, SourceMgr,
1429 AST.getLangOpts(), false)) {
1430 // getRawToken failed, just use InputLocation.
1431 return InputLocation;
1432 }
1433
1434 if (Result.is(tok::raw_identifier)) {
1435 return Lexer::GetBeginningOfToken(PeekBeforeLocation, SourceMgr,
1436 AST.getLangOpts());
1437 }
1438
1439 return InputLocation;
1440}