blob: 02b73002087c65970e10b0561d32e1400cae7485 [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) {
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000238 Preamble->AddImplicitPreamble(*CI, VFS, Buffer.get());
Ilya Biryukov04db3682017-07-21 13:29:29 +0000239 } 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
Sam McCalla40371b2017-11-15 09:16:29 +0000371/// A scored code completion result.
372/// It may be promoted to a CompletionItem if it's among the top-ranked results.
373struct CompletionCandidate {
374 CompletionCandidate(CodeCompletionResult &Result)
375 : Result(&Result), Score(score(Result)) {}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000376
Sam McCalla40371b2017-11-15 09:16:29 +0000377 CodeCompletionResult *Result;
Sam McCallb8d548a2017-11-23 17:09:04 +0000378 float Score; // 0 to 1, higher is better.
Sam McCalla40371b2017-11-15 09:16:29 +0000379
380 // Comparison reflects rank: better candidates are smaller.
381 bool operator<(const CompletionCandidate &C) const {
382 if (Score != C.Score)
Sam McCallb8d548a2017-11-23 17:09:04 +0000383 return Score > C.Score;
Sam McCalla40371b2017-11-15 09:16:29 +0000384 return *Result < *C.Result;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000385 }
386
Sam McCallb8d548a2017-11-23 17:09:04 +0000387 // Returns a string that sorts in the same order as operator<, for LSP.
388 // Conceptually, this is [-Score, Name]. We convert -Score to an integer, and
389 // hex-encode it for readability. Example: [0.5, "foo"] -> "41000000foo"
Sam McCalla40371b2017-11-15 09:16:29 +0000390 std::string sortText() const {
Sam McCalla40371b2017-11-15 09:16:29 +0000391 std::string S, NameStorage;
Sam McCallb8d548a2017-11-23 17:09:04 +0000392 llvm::raw_string_ostream OS(S);
393 write_hex(OS, encodeFloat(-Score), llvm::HexPrintStyle::Lower,
394 /*Width=*/2 * sizeof(Score));
395 OS << Result->getOrderedName(NameStorage);
396 return OS.str();
Sam McCalla40371b2017-11-15 09:16:29 +0000397 }
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000398
399private:
Sam McCallb8d548a2017-11-23 17:09:04 +0000400 static float score(const CodeCompletionResult &Result) {
401 // Priority 80 is a really bad score.
402 float Score = 1 - std::min<float>(80, Result.Priority) / 80;
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000403
Sam McCalla40371b2017-11-15 09:16:29 +0000404 switch (static_cast<CXAvailabilityKind>(Result.Availability)) {
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000405 case CXAvailability_Available:
406 // No penalty.
407 break;
408 case CXAvailability_Deprecated:
Simon Pilgrim116e3b3c2017-11-24 18:18:42 +0000409 Score *= 0.1f;
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000410 break;
411 case CXAvailability_NotAccessible:
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000412 case CXAvailability_NotAvailable:
Sam McCallb8d548a2017-11-23 17:09:04 +0000413 Score = 0;
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000414 break;
415 }
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000416 return Score;
417 }
Sam McCallb8d548a2017-11-23 17:09:04 +0000418
419 // Produces an integer that sorts in the same order as F.
420 // That is: a < b <==> encodeFloat(a) < encodeFloat(b).
421 static uint32_t encodeFloat(float F) {
422 static_assert(std::numeric_limits<float>::is_iec559, "");
423 static_assert(sizeof(float) == sizeof(uint32_t), "");
424 constexpr uint32_t TopBit = ~(~uint32_t{0} >> 1);
425
426 // Get the bits of the float. Endianness is the same as for integers.
427 uint32_t U;
428 memcpy(&U, &F, sizeof(float));
429 // IEEE 754 floats compare like sign-magnitude integers.
430 if (U & TopBit) // Negative float.
431 return 0 - U; // Map onto the low half of integers, order reversed.
432 return U + TopBit; // Positive floats map onto the high half of integers.
433 }
Sam McCalla40371b2017-11-15 09:16:29 +0000434};
Ilya Biryukov77f61ba2017-09-20 15:09:14 +0000435
Sam McCalla40371b2017-11-15 09:16:29 +0000436class CompletionItemsCollector : public CodeCompleteConsumer {
437public:
438 CompletionItemsCollector(const clangd::CodeCompleteOptions &CodeCompleteOpts,
439 CompletionList &Items)
440 : CodeCompleteConsumer(CodeCompleteOpts.getClangCompleteOpts(),
441 /*OutputIsBinary=*/false),
442 ClangdOpts(CodeCompleteOpts), Items(Items),
443 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
444 CCTUInfo(Allocator) {}
445
446 void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
447 CodeCompletionResult *Results,
448 unsigned NumResults) override final {
Sam McCall9fbecd52017-12-01 16:35:50 +0000449 StringRef Filter = S.getPreprocessor().getCodeCompletionFilter();
Sam McCalla40371b2017-11-15 09:16:29 +0000450 std::priority_queue<CompletionCandidate> Candidates;
451 for (unsigned I = 0; I < NumResults; ++I) {
Sam McCalladccab62017-11-23 16:58:22 +0000452 auto &Result = Results[I];
453 if (!ClangdOpts.IncludeIneligibleResults &&
454 (Result.Availability == CXAvailability_NotAvailable ||
455 Result.Availability == CXAvailability_NotAccessible))
456 continue;
Sam McCall9fbecd52017-12-01 16:35:50 +0000457 if (!Filter.empty() && !fuzzyMatch(S, Context, Filter, Result))
458 continue;
Sam McCalladccab62017-11-23 16:58:22 +0000459 Candidates.emplace(Result);
Sam McCalla40371b2017-11-15 09:16:29 +0000460 if (ClangdOpts.Limit && Candidates.size() > ClangdOpts.Limit) {
461 Candidates.pop();
462 Items.isIncomplete = true;
463 }
464 }
465 while (!Candidates.empty()) {
466 auto &Candidate = Candidates.top();
467 const auto *CCS = Candidate.Result->CreateCodeCompletionString(
468 S, Context, *Allocator, CCTUInfo,
469 CodeCompleteOpts.IncludeBriefComments);
470 assert(CCS && "Expected the CodeCompletionString to be non-null");
471 Items.items.push_back(ProcessCodeCompleteResult(Candidate, *CCS));
472 Candidates.pop();
473 }
474 std::reverse(Items.items.begin(), Items.items.end());
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000475 }
476
Sam McCalla40371b2017-11-15 09:16:29 +0000477 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
478
479 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
480
481private:
Sam McCall9fbecd52017-12-01 16:35:50 +0000482 bool fuzzyMatch(Sema &S, const CodeCompletionContext &CCCtx, StringRef Filter,
483 CodeCompletionResult Result) {
484 switch (Result.Kind) {
485 case CodeCompletionResult::RK_Declaration:
486 if (auto *ID = Result.Declaration->getIdentifier())
487 return fuzzyMatch(Filter, ID->getName());
488 break;
489 case CodeCompletionResult::RK_Keyword:
490 return fuzzyMatch(Filter, Result.Keyword);
491 case CodeCompletionResult::RK_Macro:
492 return fuzzyMatch(Filter, Result.Macro->getName());
493 case CodeCompletionResult::RK_Pattern:
494 return fuzzyMatch(Filter, Result.Pattern->getTypedText());
495 }
496 auto *CCS = Result.CreateCodeCompletionString(
497 S, CCCtx, *Allocator, CCTUInfo, /*IncludeBriefComments=*/false);
498 return fuzzyMatch(Filter, CCS->getTypedText());
499 }
500
501 // Checks whether Target matches the Filter.
502 // Currently just requires a case-insensitive subsequence match.
503 // FIXME: make stricter and word-based: 'unique_ptr' should not match 'que'.
504 // FIXME: return a score to be incorporated into ranking.
505 static bool fuzzyMatch(StringRef Filter, StringRef Target) {
506 size_t TPos = 0;
507 for (char C : Filter) {
508 TPos = Target.find_lower(C, TPos);
509 if (TPos == StringRef::npos)
510 return false;
511 }
512 return true;
513 }
514
Sam McCalla40371b2017-11-15 09:16:29 +0000515 CompletionItem
516 ProcessCodeCompleteResult(const CompletionCandidate &Candidate,
517 const CodeCompletionString &CCS) const {
518
519 // Adjust this to InsertTextFormat::Snippet iff we encounter a
520 // CK_Placeholder chunk in SnippetCompletionItemsCollector.
521 CompletionItem Item;
522 Item.insertTextFormat = InsertTextFormat::PlainText;
523
524 Item.documentation = getDocumentation(CCS);
525 Item.sortText = Candidate.sortText();
526
527 // Fill in the label, detail, insertText and filterText fields of the
528 // CompletionItem.
529 ProcessChunks(CCS, Item);
530
531 // Fill in the kind field of the CompletionItem.
532 Item.kind = getKind(Candidate.Result->Kind, Candidate.Result->CursorKind);
533
534 return Item;
535 }
536
537 virtual void ProcessChunks(const CodeCompletionString &CCS,
538 CompletionItem &Item) const = 0;
539
540 clangd::CodeCompleteOptions ClangdOpts;
541 CompletionList &Items;
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000542 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
543 CodeCompletionTUInfo CCTUInfo;
544
545}; // CompletionItemsCollector
546
Ilya Biryukov686ff9a2017-09-28 18:39:59 +0000547bool isInformativeQualifierChunk(CodeCompletionString::Chunk const &Chunk) {
548 return Chunk.Kind == CodeCompletionString::CK_Informative &&
549 StringRef(Chunk.Text).endswith("::");
550}
551
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000552class PlainTextCompletionItemsCollector final
553 : public CompletionItemsCollector {
554
555public:
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000556 PlainTextCompletionItemsCollector(
Sam McCalla40371b2017-11-15 09:16:29 +0000557 const clangd::CodeCompleteOptions &CodeCompleteOpts,
558 CompletionList &Items)
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000559 : CompletionItemsCollector(CodeCompleteOpts, Items) {}
560
561private:
562 void ProcessChunks(const CodeCompletionString &CCS,
563 CompletionItem &Item) const override {
564 for (const auto &Chunk : CCS) {
Ilya Biryukov686ff9a2017-09-28 18:39:59 +0000565 // Informative qualifier chunks only clutter completion results, skip
566 // them.
567 if (isInformativeQualifierChunk(Chunk))
568 continue;
569
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000570 switch (Chunk.Kind) {
571 case CodeCompletionString::CK_TypedText:
572 // There's always exactly one CK_TypedText chunk.
573 Item.insertText = Item.filterText = Chunk.Text;
574 Item.label += Chunk.Text;
575 break;
576 case CodeCompletionString::CK_ResultType:
577 assert(Item.detail.empty() && "Unexpected extraneous CK_ResultType");
578 Item.detail = Chunk.Text;
579 break;
580 case CodeCompletionString::CK_Optional:
581 break;
582 default:
583 Item.label += Chunk.Text;
584 break;
585 }
586 }
587 }
588}; // PlainTextCompletionItemsCollector
589
590class SnippetCompletionItemsCollector final : public CompletionItemsCollector {
591
592public:
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000593 SnippetCompletionItemsCollector(
Sam McCalla40371b2017-11-15 09:16:29 +0000594 const clangd::CodeCompleteOptions &CodeCompleteOpts,
595 CompletionList &Items)
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000596 : CompletionItemsCollector(CodeCompleteOpts, Items) {}
597
598private:
599 void ProcessChunks(const CodeCompletionString &CCS,
600 CompletionItem &Item) const override {
601 unsigned ArgCount = 0;
602 for (const auto &Chunk : CCS) {
Ilya Biryukov686ff9a2017-09-28 18:39:59 +0000603 // Informative qualifier chunks only clutter completion results, skip
604 // them.
605 if (isInformativeQualifierChunk(Chunk))
606 continue;
607
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000608 switch (Chunk.Kind) {
609 case CodeCompletionString::CK_TypedText:
610 // The piece of text that the user is expected to type to match
611 // the code-completion string, typically a keyword or the name of
612 // a declarator or macro.
613 Item.filterText = Chunk.Text;
Simon Pilgrim948c0bc2017-11-01 09:22:03 +0000614 LLVM_FALLTHROUGH;
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000615 case CodeCompletionString::CK_Text:
616 // A piece of text that should be placed in the buffer,
617 // e.g., parentheses or a comma in a function call.
618 Item.label += Chunk.Text;
619 Item.insertText += Chunk.Text;
620 break;
621 case CodeCompletionString::CK_Optional:
622 // A code completion string that is entirely optional.
623 // For example, an optional code completion string that
624 // describes the default arguments in a function call.
625
626 // FIXME: Maybe add an option to allow presenting the optional chunks?
627 break;
628 case CodeCompletionString::CK_Placeholder:
629 // A string that acts as a placeholder for, e.g., a function call
630 // argument.
631 ++ArgCount;
632 Item.insertText += "${" + std::to_string(ArgCount) + ':' +
633 escapeSnippet(Chunk.Text) + '}';
634 Item.label += Chunk.Text;
635 Item.insertTextFormat = InsertTextFormat::Snippet;
636 break;
637 case CodeCompletionString::CK_Informative:
638 // A piece of text that describes something about the result
639 // but should not be inserted into the buffer.
640 // For example, the word "const" for a const method, or the name of
641 // the base class for methods that are part of the base class.
642 Item.label += Chunk.Text;
643 // Don't put the informative chunks in the insertText.
644 break;
645 case CodeCompletionString::CK_ResultType:
646 // A piece of text that describes the type of an entity or,
647 // for functions and methods, the return type.
648 assert(Item.detail.empty() && "Unexpected extraneous CK_ResultType");
649 Item.detail = Chunk.Text;
650 break;
651 case CodeCompletionString::CK_CurrentParameter:
652 // A piece of text that describes the parameter that corresponds to
653 // the code-completion location within a function call, message send,
654 // macro invocation, etc.
655 //
656 // This should never be present while collecting completion items,
657 // only while collecting overload candidates.
658 llvm_unreachable("Unexpected CK_CurrentParameter while collecting "
659 "CompletionItems");
660 break;
661 case CodeCompletionString::CK_LeftParen:
662 // A left parenthesis ('(').
663 case CodeCompletionString::CK_RightParen:
664 // A right parenthesis (')').
665 case CodeCompletionString::CK_LeftBracket:
666 // A left bracket ('[').
667 case CodeCompletionString::CK_RightBracket:
668 // A right bracket (']').
669 case CodeCompletionString::CK_LeftBrace:
670 // A left brace ('{').
671 case CodeCompletionString::CK_RightBrace:
672 // A right brace ('}').
673 case CodeCompletionString::CK_LeftAngle:
674 // A left angle bracket ('<').
675 case CodeCompletionString::CK_RightAngle:
676 // A right angle bracket ('>').
677 case CodeCompletionString::CK_Comma:
678 // A comma separator (',').
679 case CodeCompletionString::CK_Colon:
680 // A colon (':').
681 case CodeCompletionString::CK_SemiColon:
682 // A semicolon (';').
683 case CodeCompletionString::CK_Equal:
684 // An '=' sign.
685 case CodeCompletionString::CK_HorizontalSpace:
686 // Horizontal whitespace (' ').
687 Item.insertText += Chunk.Text;
688 Item.label += Chunk.Text;
689 break;
690 case CodeCompletionString::CK_VerticalSpace:
691 // Vertical whitespace ('\n' or '\r\n', depending on the
692 // platform).
693 Item.insertText += Chunk.Text;
694 // Don't even add a space to the label.
695 break;
696 }
697 }
698 }
699}; // SnippetCompletionItemsCollector
Ilya Biryukov38d79772017-05-16 09:38:59 +0000700
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000701class SignatureHelpCollector final : public CodeCompleteConsumer {
702
703public:
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000704 SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000705 SignatureHelp &SigHelp)
706 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
707 SigHelp(SigHelp),
708 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
709 CCTUInfo(Allocator) {}
710
711 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
712 OverloadCandidate *Candidates,
713 unsigned NumCandidates) override {
714 SigHelp.signatures.reserve(NumCandidates);
715 // FIXME(rwols): How can we determine the "active overload candidate"?
716 // Right now the overloaded candidates seem to be provided in a "best fit"
717 // order, so I'm not too worried about this.
718 SigHelp.activeSignature = 0;
Simon Pilgrima3aa7242017-10-07 12:24:10 +0000719 assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000720 "too many arguments");
721 SigHelp.activeParameter = static_cast<int>(CurrentArg);
722 for (unsigned I = 0; I < NumCandidates; ++I) {
723 const auto &Candidate = Candidates[I];
724 const auto *CCS = Candidate.CreateSignatureString(
725 CurrentArg, S, *Allocator, CCTUInfo, true);
726 assert(CCS && "Expected the CodeCompletionString to be non-null");
727 SigHelp.signatures.push_back(ProcessOverloadCandidate(Candidate, *CCS));
728 }
729 }
730
731 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
732
733 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
734
735private:
736 SignatureInformation
737 ProcessOverloadCandidate(const OverloadCandidate &Candidate,
738 const CodeCompletionString &CCS) const {
739 SignatureInformation Result;
740 const char *ReturnType = nullptr;
741
742 Result.documentation = getDocumentation(CCS);
743
744 for (const auto &Chunk : CCS) {
745 switch (Chunk.Kind) {
746 case CodeCompletionString::CK_ResultType:
747 // A piece of text that describes the type of an entity or,
748 // for functions and methods, the return type.
749 assert(!ReturnType && "Unexpected CK_ResultType");
750 ReturnType = Chunk.Text;
751 break;
752 case CodeCompletionString::CK_Placeholder:
753 // A string that acts as a placeholder for, e.g., a function call
754 // argument.
755 // Intentional fallthrough here.
756 case CodeCompletionString::CK_CurrentParameter: {
757 // A piece of text that describes the parameter that corresponds to
758 // the code-completion location within a function call, message send,
759 // macro invocation, etc.
760 Result.label += Chunk.Text;
761 ParameterInformation Info;
762 Info.label = Chunk.Text;
763 Result.parameters.push_back(std::move(Info));
764 break;
765 }
766 case CodeCompletionString::CK_Optional: {
767 // The rest of the parameters are defaulted/optional.
768 assert(Chunk.Optional &&
769 "Expected the optional code completion string to be non-null.");
770 Result.label +=
771 getOptionalParameters(*Chunk.Optional, Result.parameters);
772 break;
773 }
774 case CodeCompletionString::CK_VerticalSpace:
775 break;
776 default:
777 Result.label += Chunk.Text;
778 break;
779 }
780 }
781 if (ReturnType) {
782 Result.label += " -> ";
783 Result.label += ReturnType;
784 }
785 return Result;
786 }
787
788 SignatureHelp &SigHelp;
789 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
790 CodeCompletionTUInfo CCTUInfo;
791
792}; // SignatureHelpCollector
793
794bool invokeCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000795 const clang::CodeCompleteOptions &Options,
796 PathRef FileName,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000797 const tooling::CompileCommand &Command,
798 PrecompiledPreamble const *Preamble, StringRef Contents,
799 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
800 std::shared_ptr<PCHContainerOperations> PCHs,
801 clangd::Logger &Logger) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000802 std::vector<const char *> ArgStrs;
803 for (const auto &S : Command.CommandLine)
804 ArgStrs.push_back(S.c_str());
805
Krasimir Georgieve4130d52017-07-25 11:37:43 +0000806 VFS->setCurrentWorkingDirectory(Command.Directory);
807
Ilya Biryukov04db3682017-07-21 13:29:29 +0000808 std::unique_ptr<CompilerInvocation> CI;
809 EmptyDiagsConsumer DummyDiagsConsumer;
810 {
811 IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
812 CompilerInstance::createDiagnostics(new DiagnosticOptions,
813 &DummyDiagsConsumer, false);
814 CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
815 }
816 assert(CI && "Couldn't create CompilerInvocation");
817
818 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
819 llvm::MemoryBuffer::getMemBufferCopy(Contents, FileName);
820
821 // Attempt to reuse the PCH from precompiled preamble, if it was built.
Ilya Biryukov04db3682017-07-21 13:29:29 +0000822 if (Preamble) {
823 auto Bounds =
824 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
Ilya Biryukov02d58702017-08-01 15:51:38 +0000825 if (!Preamble->CanReuse(*CI, ContentsBuffer.get(), Bounds, VFS.get()))
826 Preamble = nullptr;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000827 }
828
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000829 auto Clang = prepareCompilerInstance(
830 std::move(CI), Preamble, std::move(ContentsBuffer), std::move(PCHs),
831 std::move(VFS), DummyDiagsConsumer);
Ilya Biryukov04db3682017-07-21 13:29:29 +0000832 auto &DiagOpts = Clang->getDiagnosticOpts();
833 DiagOpts.IgnoreWarnings = true;
834
835 auto &FrontendOpts = Clang->getFrontendOpts();
836 FrontendOpts.SkipFunctionBodies = true;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000837 FrontendOpts.CodeCompleteOpts = Options;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000838 FrontendOpts.CodeCompletionAt.FileName = FileName;
839 FrontendOpts.CodeCompletionAt.Line = Pos.line + 1;
840 FrontendOpts.CodeCompletionAt.Column = Pos.character + 1;
841
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000842 Clang->setCodeCompletionConsumer(Consumer.release());
Ilya Biryukov38d79772017-05-16 09:38:59 +0000843
Ilya Biryukov04db3682017-07-21 13:29:29 +0000844 SyntaxOnlyAction Action;
845 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
Ilya Biryukove5128f72017-09-20 07:24:15 +0000846 Logger.log("BeginSourceFile() failed when running codeComplete for " +
847 FileName);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000848 return false;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000849 }
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000850 if (!Action.Execute()) {
Ilya Biryukove5128f72017-09-20 07:24:15 +0000851 Logger.log("Execute() failed when running codeComplete for " + FileName);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000852 return false;
853 }
Ilya Biryukove5128f72017-09-20 07:24:15 +0000854
Ilya Biryukov04db3682017-07-21 13:29:29 +0000855 Action.EndSourceFile();
Ilya Biryukov38d79772017-05-16 09:38:59 +0000856
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000857 return true;
858}
859
860} // namespace
861
Sam McCalla40371b2017-11-15 09:16:29 +0000862clang::CodeCompleteOptions
863clangd::CodeCompleteOptions::getClangCompleteOpts() const {
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000864 clang::CodeCompleteOptions Result;
865 Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns;
866 Result.IncludeMacros = IncludeMacros;
867 Result.IncludeGlobals = IncludeGlobals;
868 Result.IncludeBriefComments = IncludeBriefComments;
869
870 return Result;
871}
872
Sam McCalla40371b2017-11-15 09:16:29 +0000873CompletionList
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000874clangd::codeComplete(PathRef FileName, const tooling::CompileCommand &Command,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000875 PrecompiledPreamble const *Preamble, StringRef Contents,
876 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
877 std::shared_ptr<PCHContainerOperations> PCHs,
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000878 clangd::CodeCompleteOptions Opts, clangd::Logger &Logger) {
Sam McCalla40371b2017-11-15 09:16:29 +0000879 CompletionList Results;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000880 std::unique_ptr<CodeCompleteConsumer> Consumer;
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000881 if (Opts.EnableSnippets) {
Sam McCalla40371b2017-11-15 09:16:29 +0000882 Consumer =
883 llvm::make_unique<SnippetCompletionItemsCollector>(Opts, Results);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000884 } else {
Sam McCalla40371b2017-11-15 09:16:29 +0000885 Consumer =
886 llvm::make_unique<PlainTextCompletionItemsCollector>(Opts, Results);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000887 }
Sam McCalla40371b2017-11-15 09:16:29 +0000888 invokeCodeComplete(std::move(Consumer), Opts.getClangCompleteOpts(), FileName,
889 Command, Preamble, Contents, Pos, std::move(VFS),
890 std::move(PCHs), Logger);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000891 return Results;
892}
893
894SignatureHelp
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000895clangd::signatureHelp(PathRef FileName, const tooling::CompileCommand &Command,
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000896 PrecompiledPreamble const *Preamble, StringRef Contents,
897 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
898 std::shared_ptr<PCHContainerOperations> PCHs,
899 clangd::Logger &Logger) {
900 SignatureHelp Result;
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000901 clang::CodeCompleteOptions Options;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000902 Options.IncludeGlobals = false;
903 Options.IncludeMacros = false;
904 Options.IncludeCodePatterns = false;
905 Options.IncludeBriefComments = true;
906 invokeCodeComplete(llvm::make_unique<SignatureHelpCollector>(Options, Result),
907 Options, FileName, Command, Preamble, Contents, Pos,
908 std::move(VFS), std::move(PCHs), Logger);
909 return Result;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000910}
911
Ilya Biryukov02d58702017-08-01 15:51:38 +0000912void clangd::dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) {
913 AST.getASTContext().getTranslationUnitDecl()->dump(OS, true);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000914}
Ilya Biryukovf01af682017-05-23 13:42:59 +0000915
Ilya Biryukov02d58702017-08-01 15:51:38 +0000916llvm::Optional<ParsedAST>
917ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI,
Ilya Biryukov2660cc92017-11-24 13:04:21 +0000918 std::shared_ptr<const PreambleData> Preamble,
Ilya Biryukov02d58702017-08-01 15:51:38 +0000919 std::unique_ptr<llvm::MemoryBuffer> Buffer,
920 std::shared_ptr<PCHContainerOperations> PCHs,
Ilya Biryukove5128f72017-09-20 07:24:15 +0000921 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
922 clangd::Logger &Logger) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000923
924 std::vector<DiagWithFixIts> ASTDiags;
925 StoreDiagsConsumer UnitDiagsConsumer(/*ref*/ ASTDiags);
926
Ilya Biryukov2660cc92017-11-24 13:04:21 +0000927 const PrecompiledPreamble *PreamblePCH =
928 Preamble ? &Preamble->Preamble : nullptr;
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000929 auto Clang = prepareCompilerInstance(
Ilya Biryukov2660cc92017-11-24 13:04:21 +0000930 std::move(CI), PreamblePCH, std::move(Buffer), std::move(PCHs),
Benjamin Kramer5349eed2017-10-28 17:32:56 +0000931 std::move(VFS), /*ref*/ UnitDiagsConsumer);
Ilya Biryukov04db3682017-07-21 13:29:29 +0000932
933 // Recover resources if we crash before exiting this method.
934 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
935 Clang.get());
936
937 auto Action = llvm::make_unique<ClangdFrontendAction>();
Ilya Biryukove5128f72017-09-20 07:24:15 +0000938 const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
939 if (!Action->BeginSourceFile(*Clang, MainInput)) {
940 Logger.log("BeginSourceFile() failed when building AST for " +
941 MainInput.getFile());
Ilya Biryukov04db3682017-07-21 13:29:29 +0000942 return llvm::None;
943 }
Ilya Biryukove5128f72017-09-20 07:24:15 +0000944 if (!Action->Execute())
945 Logger.log("Execute() failed when building AST for " + MainInput.getFile());
Ilya Biryukov04db3682017-07-21 13:29:29 +0000946
947 // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
948 // has a longer lifetime.
949 Clang->getDiagnostics().setClient(new EmptyDiagsConsumer);
950
951 std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls();
Ilya Biryukov2660cc92017-11-24 13:04:21 +0000952 return ParsedAST(std::move(Preamble), std::move(Clang), std::move(Action),
953 std::move(ParsedDecls), std::move(ASTDiags));
Ilya Biryukov04db3682017-07-21 13:29:29 +0000954}
955
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000956namespace {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000957
958SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
959 const FileEntry *FE,
960 unsigned Offset) {
961 SourceLocation FileLoc = Mgr.translateFileLineCol(FE, 1, 1);
962 return Mgr.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
963}
964
965SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
966 const FileEntry *FE, Position Pos) {
967 SourceLocation InputLoc =
968 Mgr.translateFileLineCol(FE, Pos.line + 1, Pos.character + 1);
969 return Mgr.getMacroArgExpandedLocation(InputLoc);
970}
971
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000972/// Finds declarations locations that a given source location refers to.
973class DeclarationLocationsFinder : public index::IndexDataConsumer {
974 std::vector<Location> DeclarationLocations;
975 const SourceLocation &SearchedLocation;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000976 const ASTContext &AST;
977 Preprocessor &PP;
978
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000979public:
980 DeclarationLocationsFinder(raw_ostream &OS,
Ilya Biryukov04db3682017-07-21 13:29:29 +0000981 const SourceLocation &SearchedLocation,
982 ASTContext &AST, Preprocessor &PP)
983 : SearchedLocation(SearchedLocation), AST(AST), PP(PP) {}
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000984
985 std::vector<Location> takeLocations() {
986 // Don't keep the same location multiple times.
987 // This can happen when nodes in the AST are visited twice.
988 std::sort(DeclarationLocations.begin(), DeclarationLocations.end());
Kirill Bobyrev46213872017-06-28 20:57:28 +0000989 auto last =
990 std::unique(DeclarationLocations.begin(), DeclarationLocations.end());
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000991 DeclarationLocations.erase(last, DeclarationLocations.end());
992 return std::move(DeclarationLocations);
993 }
994
Ilya Biryukov02d58702017-08-01 15:51:38 +0000995 bool
996 handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
997 ArrayRef<index::SymbolRelation> Relations, FileID FID,
998 unsigned Offset,
999 index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001000 if (isSearchedLocation(FID, Offset)) {
1001 addDeclarationLocation(D->getSourceRange());
1002 }
1003 return true;
1004 }
1005
1006private:
1007 bool isSearchedLocation(FileID FID, unsigned Offset) const {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001008 const SourceManager &SourceMgr = AST.getSourceManager();
1009 return SourceMgr.getFileOffset(SearchedLocation) == Offset &&
1010 SourceMgr.getFileID(SearchedLocation) == FID;
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001011 }
1012
Ilya Biryukov04db3682017-07-21 13:29:29 +00001013 void addDeclarationLocation(const SourceRange &ValSourceRange) {
1014 const SourceManager &SourceMgr = AST.getSourceManager();
1015 const LangOptions &LangOpts = AST.getLangOpts();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001016 SourceLocation LocStart = ValSourceRange.getBegin();
1017 SourceLocation LocEnd = Lexer::getLocForEndOfToken(ValSourceRange.getEnd(),
Ilya Biryukov04db3682017-07-21 13:29:29 +00001018 0, SourceMgr, LangOpts);
Kirill Bobyrev46213872017-06-28 20:57:28 +00001019 Position Begin;
1020 Begin.line = SourceMgr.getSpellingLineNumber(LocStart) - 1;
1021 Begin.character = SourceMgr.getSpellingColumnNumber(LocStart) - 1;
1022 Position End;
1023 End.line = SourceMgr.getSpellingLineNumber(LocEnd) - 1;
1024 End.character = SourceMgr.getSpellingColumnNumber(LocEnd) - 1;
1025 Range R = {Begin, End};
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001026 Location L;
Marc-Andre Laperleba070102017-11-07 16:16:45 +00001027 if (const FileEntry *F =
1028 SourceMgr.getFileEntryForID(SourceMgr.getFileID(LocStart))) {
1029 StringRef FilePath = F->tryGetRealPathName();
1030 if (FilePath.empty())
1031 FilePath = F->getName();
1032 L.uri = URI::fromFile(FilePath);
1033 L.range = R;
1034 DeclarationLocations.push_back(L);
1035 }
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001036 }
1037
Kirill Bobyrev46213872017-06-28 20:57:28 +00001038 void finish() override {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001039 // Also handle possible macro at the searched location.
1040 Token Result;
Ilya Biryukov04db3682017-07-21 13:29:29 +00001041 if (!Lexer::getRawToken(SearchedLocation, Result, AST.getSourceManager(),
1042 AST.getLangOpts(), false)) {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001043 if (Result.is(tok::raw_identifier)) {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001044 PP.LookUpIdentifierInfo(Result);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001045 }
Ilya Biryukov04db3682017-07-21 13:29:29 +00001046 IdentifierInfo *IdentifierInfo = Result.getIdentifierInfo();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001047 if (IdentifierInfo && IdentifierInfo->hadMacroDefinition()) {
1048 std::pair<FileID, unsigned int> DecLoc =
Ilya Biryukov04db3682017-07-21 13:29:29 +00001049 AST.getSourceManager().getDecomposedExpansionLoc(SearchedLocation);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001050 // Get the definition just before the searched location so that a macro
1051 // referenced in a '#undef MACRO' can still be found.
Ilya Biryukov04db3682017-07-21 13:29:29 +00001052 SourceLocation BeforeSearchedLocation = getMacroArgExpandedLocation(
1053 AST.getSourceManager(),
1054 AST.getSourceManager().getFileEntryForID(DecLoc.first),
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001055 DecLoc.second - 1);
1056 MacroDefinition MacroDef =
Ilya Biryukov04db3682017-07-21 13:29:29 +00001057 PP.getMacroDefinitionAtLoc(IdentifierInfo, BeforeSearchedLocation);
1058 MacroInfo *MacroInf = MacroDef.getMacroInfo();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001059 if (MacroInf) {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001060 addDeclarationLocation(SourceRange(MacroInf->getDefinitionLoc(),
1061 MacroInf->getDefinitionEndLoc()));
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001062 }
1063 }
1064 }
1065 }
1066};
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +00001067
Ilya Biryukov02d58702017-08-01 15:51:38 +00001068} // namespace
Ilya Biryukov04db3682017-07-21 13:29:29 +00001069
Ilya Biryukove5128f72017-09-20 07:24:15 +00001070std::vector<Location> clangd::findDefinitions(ParsedAST &AST, Position Pos,
1071 clangd::Logger &Logger) {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001072 const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
1073 const FileEntry *FE = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
1074 if (!FE)
1075 return {};
1076
1077 SourceLocation SourceLocationBeg = getBeginningOfIdentifier(AST, Pos, FE);
1078
1079 auto DeclLocationsFinder = std::make_shared<DeclarationLocationsFinder>(
1080 llvm::errs(), SourceLocationBeg, AST.getASTContext(),
1081 AST.getPreprocessor());
1082 index::IndexingOptions IndexOpts;
1083 IndexOpts.SystemSymbolFilter =
1084 index::IndexingOptions::SystemSymbolFilterKind::All;
1085 IndexOpts.IndexFunctionLocals = true;
1086
1087 indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(),
1088 DeclLocationsFinder, IndexOpts);
1089
1090 return DeclLocationsFinder->takeLocations();
1091}
1092
1093void ParsedAST::ensurePreambleDeclsDeserialized() {
Ilya Biryukov2660cc92017-11-24 13:04:21 +00001094 if (PreambleDeclsDeserialized || !Preamble)
Ilya Biryukov04db3682017-07-21 13:29:29 +00001095 return;
1096
1097 std::vector<const Decl *> Resolved;
Ilya Biryukov2660cc92017-11-24 13:04:21 +00001098 Resolved.reserve(Preamble->TopLevelDeclIDs.size());
Ilya Biryukov04db3682017-07-21 13:29:29 +00001099
1100 ExternalASTSource &Source = *getASTContext().getExternalSource();
Ilya Biryukov2660cc92017-11-24 13:04:21 +00001101 for (serialization::DeclID TopLevelDecl : Preamble->TopLevelDeclIDs) {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001102 // Resolve the declaration ID to an actual declaration, possibly
1103 // deserializing the declaration in the process.
1104 if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
1105 Resolved.push_back(D);
1106 }
1107
Ilya Biryukov2660cc92017-11-24 13:04:21 +00001108 TopLevelDecls.reserve(TopLevelDecls.size() +
1109 Preamble->TopLevelDeclIDs.size());
Ilya Biryukov04db3682017-07-21 13:29:29 +00001110 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1111
Ilya Biryukov2660cc92017-11-24 13:04:21 +00001112 PreambleDeclsDeserialized = true;
Ilya Biryukov04db3682017-07-21 13:29:29 +00001113}
1114
Ilya Biryukov02d58702017-08-01 15:51:38 +00001115ParsedAST::ParsedAST(ParsedAST &&Other) = default;
Ilya Biryukov04db3682017-07-21 13:29:29 +00001116
Ilya Biryukov02d58702017-08-01 15:51:38 +00001117ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
Ilya Biryukov04db3682017-07-21 13:29:29 +00001118
Ilya Biryukov02d58702017-08-01 15:51:38 +00001119ParsedAST::~ParsedAST() {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001120 if (Action) {
1121 Action->EndSourceFile();
1122 }
1123}
1124
Ilya Biryukov02d58702017-08-01 15:51:38 +00001125ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
1126
1127const ASTContext &ParsedAST::getASTContext() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001128 return Clang->getASTContext();
1129}
1130
Ilya Biryukov02d58702017-08-01 15:51:38 +00001131Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
Ilya Biryukov04db3682017-07-21 13:29:29 +00001132
Ilya Biryukov02d58702017-08-01 15:51:38 +00001133const Preprocessor &ParsedAST::getPreprocessor() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001134 return Clang->getPreprocessor();
1135}
1136
Ilya Biryukov02d58702017-08-01 15:51:38 +00001137ArrayRef<const Decl *> ParsedAST::getTopLevelDecls() {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001138 ensurePreambleDeclsDeserialized();
1139 return TopLevelDecls;
1140}
1141
Ilya Biryukov02d58702017-08-01 15:51:38 +00001142const std::vector<DiagWithFixIts> &ParsedAST::getDiagnostics() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001143 return Diags;
1144}
1145
Ilya Biryukov2660cc92017-11-24 13:04:21 +00001146PreambleData::PreambleData(PrecompiledPreamble Preamble,
1147 std::vector<serialization::DeclID> TopLevelDeclIDs,
1148 std::vector<DiagWithFixIts> Diags)
1149 : Preamble(std::move(Preamble)),
1150 TopLevelDeclIDs(std::move(TopLevelDeclIDs)), Diags(std::move(Diags)) {}
1151
1152ParsedAST::ParsedAST(std::shared_ptr<const PreambleData> Preamble,
1153 std::unique_ptr<CompilerInstance> Clang,
Ilya Biryukov02d58702017-08-01 15:51:38 +00001154 std::unique_ptr<FrontendAction> Action,
1155 std::vector<const Decl *> TopLevelDecls,
Ilya Biryukov02d58702017-08-01 15:51:38 +00001156 std::vector<DiagWithFixIts> Diags)
Ilya Biryukov2660cc92017-11-24 13:04:21 +00001157 : Preamble(std::move(Preamble)), Clang(std::move(Clang)),
1158 Action(std::move(Action)), Diags(std::move(Diags)),
1159 TopLevelDecls(std::move(TopLevelDecls)),
1160 PreambleDeclsDeserialized(false) {
Ilya Biryukov04db3682017-07-21 13:29:29 +00001161 assert(this->Clang);
1162 assert(this->Action);
1163}
1164
Ilya Biryukov02d58702017-08-01 15:51:38 +00001165ParsedASTWrapper::ParsedASTWrapper(ParsedASTWrapper &&Wrapper)
1166 : AST(std::move(Wrapper.AST)) {}
1167
1168ParsedASTWrapper::ParsedASTWrapper(llvm::Optional<ParsedAST> AST)
1169 : AST(std::move(AST)) {}
1170
Ilya Biryukov02d58702017-08-01 15:51:38 +00001171std::shared_ptr<CppFile>
1172CppFile::Create(PathRef FileName, tooling::CompileCommand Command,
Ilya Biryukove9eb7f02017-11-16 16:25:18 +00001173 bool StorePreamblesInMemory,
Ilya Biryukov83ca8a22017-09-20 10:46:58 +00001174 std::shared_ptr<PCHContainerOperations> PCHs,
1175 clangd::Logger &Logger) {
Ilya Biryukove9eb7f02017-11-16 16:25:18 +00001176 return std::shared_ptr<CppFile>(new CppFile(FileName, std::move(Command),
1177 StorePreamblesInMemory,
1178 std::move(PCHs), Logger));
Ilya Biryukov02d58702017-08-01 15:51:38 +00001179}
1180
1181CppFile::CppFile(PathRef FileName, tooling::CompileCommand Command,
Ilya Biryukove9eb7f02017-11-16 16:25:18 +00001182 bool StorePreamblesInMemory,
Ilya Biryukove5128f72017-09-20 07:24:15 +00001183 std::shared_ptr<PCHContainerOperations> PCHs,
1184 clangd::Logger &Logger)
Ilya Biryukove9eb7f02017-11-16 16:25:18 +00001185 : FileName(FileName), Command(std::move(Command)),
1186 StorePreamblesInMemory(StorePreamblesInMemory), RebuildCounter(0),
Ilya Biryukove5128f72017-09-20 07:24:15 +00001187 RebuildInProgress(false), PCHs(std::move(PCHs)), Logger(Logger) {
Sam McCallfae3b022017-11-30 23:16:23 +00001188 Logger.log("Opened file " + FileName + " with command [" +
1189 this->Command.Directory + "] " +
Sam McCall318fbeb2017-11-30 23:21:34 +00001190 llvm::join(this->Command.CommandLine, " "));
Ilya Biryukov02d58702017-08-01 15:51:38 +00001191
1192 std::lock_guard<std::mutex> Lock(Mutex);
1193 LatestAvailablePreamble = nullptr;
1194 PreamblePromise.set_value(nullptr);
1195 PreambleFuture = PreamblePromise.get_future();
1196
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001197 ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
Ilya Biryukov02d58702017-08-01 15:51:38 +00001198 ASTFuture = ASTPromise.get_future();
1199}
1200
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001201void CppFile::cancelRebuild() { deferCancelRebuild()(); }
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001202
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001203UniqueFunction<void()> CppFile::deferCancelRebuild() {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001204 std::unique_lock<std::mutex> Lock(Mutex);
1205 // Cancel an ongoing rebuild, if any, and wait for it to finish.
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001206 unsigned RequestRebuildCounter = ++this->RebuildCounter;
Ilya Biryukov02d58702017-08-01 15:51:38 +00001207 // Rebuild asserts that futures aren't ready if rebuild is cancelled.
1208 // We want to keep this invariant.
1209 if (futureIsReady(PreambleFuture)) {
1210 PreamblePromise = std::promise<std::shared_ptr<const PreambleData>>();
1211 PreambleFuture = PreamblePromise.get_future();
1212 }
1213 if (futureIsReady(ASTFuture)) {
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001214 ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001215 ASTFuture = ASTPromise.get_future();
1216 }
Ilya Biryukov02d58702017-08-01 15:51:38 +00001217
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001218 Lock.unlock();
1219 // Notify about changes to RebuildCounter.
1220 RebuildCond.notify_all();
1221
1222 std::shared_ptr<CppFile> That = shared_from_this();
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001223 return [That, RequestRebuildCounter]() {
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001224 std::unique_lock<std::mutex> Lock(That->Mutex);
1225 CppFile *This = &*That;
1226 This->RebuildCond.wait(Lock, [This, RequestRebuildCounter]() {
1227 return !This->RebuildInProgress ||
1228 This->RebuildCounter != RequestRebuildCounter;
1229 });
1230
1231 // This computation got cancelled itself, do nothing.
1232 if (This->RebuildCounter != RequestRebuildCounter)
1233 return;
1234
1235 // Set empty results for Promises.
1236 That->PreamblePromise.set_value(nullptr);
1237 That->ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001238 };
Ilya Biryukov02d58702017-08-01 15:51:38 +00001239}
1240
1241llvm::Optional<std::vector<DiagWithFixIts>>
1242CppFile::rebuild(StringRef NewContents,
1243 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001244 return deferRebuild(NewContents, std::move(VFS))();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001245}
1246
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001247UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>()>
Ilya Biryukov02d58702017-08-01 15:51:38 +00001248CppFile::deferRebuild(StringRef NewContents,
1249 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
1250 std::shared_ptr<const PreambleData> OldPreamble;
1251 std::shared_ptr<PCHContainerOperations> PCHs;
1252 unsigned RequestRebuildCounter;
1253 {
1254 std::unique_lock<std::mutex> Lock(Mutex);
1255 // Increase RebuildCounter to cancel all ongoing FinishRebuild operations.
1256 // They will try to exit as early as possible and won't call set_value on
1257 // our promises.
1258 RequestRebuildCounter = ++this->RebuildCounter;
1259 PCHs = this->PCHs;
1260
1261 // Remember the preamble to be used during rebuild.
1262 OldPreamble = this->LatestAvailablePreamble;
1263 // Setup std::promises and std::futures for Preamble and AST. Corresponding
1264 // futures will wait until the rebuild process is finished.
1265 if (futureIsReady(this->PreambleFuture)) {
1266 this->PreamblePromise =
1267 std::promise<std::shared_ptr<const PreambleData>>();
1268 this->PreambleFuture = this->PreamblePromise.get_future();
1269 }
1270 if (futureIsReady(this->ASTFuture)) {
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001271 this->ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001272 this->ASTFuture = this->ASTPromise.get_future();
1273 }
1274 } // unlock Mutex.
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001275 // Notify about changes to RebuildCounter.
1276 RebuildCond.notify_all();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001277
1278 // A helper to function to finish the rebuild. May be run on a different
1279 // thread.
1280
1281 // Don't let this CppFile die before rebuild is finished.
1282 std::shared_ptr<CppFile> That = shared_from_this();
1283 auto FinishRebuild = [OldPreamble, VFS, RequestRebuildCounter, PCHs,
Ilya Biryukov11a02522017-11-17 19:05:56 +00001284 That](std::string NewContents) mutable // 'mutable' to
1285 // allow changing
1286 // OldPreamble.
Ilya Biryukov02d58702017-08-01 15:51:38 +00001287 -> llvm::Optional<std::vector<DiagWithFixIts>> {
1288 // Only one execution of this method is possible at a time.
1289 // RebuildGuard will wait for any ongoing rebuilds to finish and will put us
1290 // into a state for doing a rebuild.
1291 RebuildGuard Rebuild(*That, RequestRebuildCounter);
1292 if (Rebuild.wasCancelledBeforeConstruction())
1293 return llvm::None;
1294
1295 std::vector<const char *> ArgStrs;
1296 for (const auto &S : That->Command.CommandLine)
1297 ArgStrs.push_back(S.c_str());
1298
1299 VFS->setCurrentWorkingDirectory(That->Command.Directory);
1300
1301 std::unique_ptr<CompilerInvocation> CI;
1302 {
1303 // FIXME(ibiryukov): store diagnostics from CommandLine when we start
1304 // reporting them.
1305 EmptyDiagsConsumer CommandLineDiagsConsumer;
1306 IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
1307 CompilerInstance::createDiagnostics(new DiagnosticOptions,
1308 &CommandLineDiagsConsumer, false);
1309 CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
1310 }
1311 assert(CI && "Couldn't create CompilerInvocation");
1312
1313 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1314 llvm::MemoryBuffer::getMemBufferCopy(NewContents, That->FileName);
1315
1316 // A helper function to rebuild the preamble or reuse the existing one. Does
Ilya Biryukov11a02522017-11-17 19:05:56 +00001317 // not mutate any fields of CppFile, only does the actual computation.
1318 // Lamdba is marked mutable to call reset() on OldPreamble.
1319 auto DoRebuildPreamble =
1320 [&]() mutable -> std::shared_ptr<const PreambleData> {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001321 auto Bounds =
1322 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
1323 if (OldPreamble && OldPreamble->Preamble.CanReuse(
1324 *CI, ContentsBuffer.get(), Bounds, VFS.get())) {
1325 return OldPreamble;
1326 }
Ilya Biryukov11a02522017-11-17 19:05:56 +00001327 // We won't need the OldPreamble anymore, release it so it can be deleted
1328 // (if there are no other references to it).
1329 OldPreamble.reset();
Ilya Biryukov02d58702017-08-01 15:51:38 +00001330
Sam McCall9cfd9c92017-11-23 17:12:04 +00001331 trace::Span Tracer("Preamble");
1332 SPAN_ATTACH(Tracer, "File", That->FileName);
Ilya Biryukov02d58702017-08-01 15:51:38 +00001333 std::vector<DiagWithFixIts> PreambleDiags;
1334 StoreDiagsConsumer PreambleDiagnosticsConsumer(/*ref*/ PreambleDiags);
1335 IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
1336 CompilerInstance::createDiagnostics(
1337 &CI->getDiagnosticOpts(), &PreambleDiagnosticsConsumer, false);
1338 CppFilePreambleCallbacks SerializedDeclsCollector;
1339 auto BuiltPreamble = PrecompiledPreamble::Build(
1340 *CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine, VFS, PCHs,
Ilya Biryukove9eb7f02017-11-16 16:25:18 +00001341 /*StoreInMemory=*/That->StorePreamblesInMemory,
Ilya Biryukov02d58702017-08-01 15:51:38 +00001342 SerializedDeclsCollector);
1343
1344 if (BuiltPreamble) {
1345 return std::make_shared<PreambleData>(
1346 std::move(*BuiltPreamble),
1347 SerializedDeclsCollector.takeTopLevelDeclIDs(),
1348 std::move(PreambleDiags));
1349 } else {
1350 return nullptr;
1351 }
1352 };
1353
1354 // Compute updated Preamble.
1355 std::shared_ptr<const PreambleData> NewPreamble = DoRebuildPreamble();
1356 // Publish the new Preamble.
1357 {
1358 std::lock_guard<std::mutex> Lock(That->Mutex);
1359 // We always set LatestAvailablePreamble to the new value, hoping that it
1360 // will still be usable in the further requests.
1361 That->LatestAvailablePreamble = NewPreamble;
1362 if (RequestRebuildCounter != That->RebuildCounter)
1363 return llvm::None; // Our rebuild request was cancelled, do nothing.
1364 That->PreamblePromise.set_value(NewPreamble);
1365 } // unlock Mutex
1366
1367 // Prepare the Preamble and supplementary data for rebuilding AST.
Ilya Biryukov02d58702017-08-01 15:51:38 +00001368 std::vector<DiagWithFixIts> Diagnostics;
1369 if (NewPreamble) {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001370 Diagnostics.insert(Diagnostics.begin(), NewPreamble->Diags.begin(),
1371 NewPreamble->Diags.end());
1372 }
1373
1374 // Compute updated AST.
Sam McCall8567cb32017-11-02 09:21:51 +00001375 llvm::Optional<ParsedAST> NewAST;
1376 {
Sam McCall9cfd9c92017-11-23 17:12:04 +00001377 trace::Span Tracer("Build");
1378 SPAN_ATTACH(Tracer, "File", That->FileName);
Ilya Biryukov2660cc92017-11-24 13:04:21 +00001379 NewAST =
1380 ParsedAST::Build(std::move(CI), std::move(NewPreamble),
1381 std::move(ContentsBuffer), PCHs, VFS, That->Logger);
Sam McCall8567cb32017-11-02 09:21:51 +00001382 }
Ilya Biryukov02d58702017-08-01 15:51:38 +00001383
1384 if (NewAST) {
1385 Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(),
1386 NewAST->getDiagnostics().end());
1387 } else {
1388 // Don't report even Preamble diagnostics if we coulnd't build AST.
1389 Diagnostics.clear();
1390 }
1391
1392 // Publish the new AST.
1393 {
1394 std::lock_guard<std::mutex> Lock(That->Mutex);
1395 if (RequestRebuildCounter != That->RebuildCounter)
1396 return Diagnostics; // Our rebuild request was cancelled, don't set
1397 // ASTPromise.
1398
Ilya Biryukov574b7532017-08-02 09:08:39 +00001399 That->ASTPromise.set_value(
1400 std::make_shared<ParsedASTWrapper>(std::move(NewAST)));
Ilya Biryukov02d58702017-08-01 15:51:38 +00001401 } // unlock Mutex
1402
1403 return Diagnostics;
1404 };
1405
Ilya Biryukov98a1fd72017-10-10 16:12:54 +00001406 return BindWithForward(FinishRebuild, NewContents.str());
Ilya Biryukov02d58702017-08-01 15:51:38 +00001407}
1408
1409std::shared_future<std::shared_ptr<const PreambleData>>
1410CppFile::getPreamble() const {
1411 std::lock_guard<std::mutex> Lock(Mutex);
1412 return PreambleFuture;
1413}
1414
1415std::shared_ptr<const PreambleData> CppFile::getPossiblyStalePreamble() const {
1416 std::lock_guard<std::mutex> Lock(Mutex);
1417 return LatestAvailablePreamble;
1418}
1419
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +00001420std::shared_future<std::shared_ptr<ParsedASTWrapper>> CppFile::getAST() const {
Ilya Biryukov02d58702017-08-01 15:51:38 +00001421 std::lock_guard<std::mutex> Lock(Mutex);
1422 return ASTFuture;
1423}
1424
1425tooling::CompileCommand const &CppFile::getCompileCommand() const {
1426 return Command;
1427}
1428
1429CppFile::RebuildGuard::RebuildGuard(CppFile &File,
1430 unsigned RequestRebuildCounter)
1431 : File(File), RequestRebuildCounter(RequestRebuildCounter) {
1432 std::unique_lock<std::mutex> Lock(File.Mutex);
1433 WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
1434 if (WasCancelledBeforeConstruction)
1435 return;
1436
Ilya Biryukovc5ad35f2017-08-14 08:17:24 +00001437 File.RebuildCond.wait(Lock, [&File, RequestRebuildCounter]() {
1438 return !File.RebuildInProgress ||
1439 File.RebuildCounter != RequestRebuildCounter;
1440 });
Ilya Biryukov02d58702017-08-01 15:51:38 +00001441
1442 WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
1443 if (WasCancelledBeforeConstruction)
1444 return;
1445
1446 File.RebuildInProgress = true;
1447}
1448
1449bool CppFile::RebuildGuard::wasCancelledBeforeConstruction() const {
1450 return WasCancelledBeforeConstruction;
1451}
1452
1453CppFile::RebuildGuard::~RebuildGuard() {
1454 if (WasCancelledBeforeConstruction)
1455 return;
1456
1457 std::unique_lock<std::mutex> Lock(File.Mutex);
1458 assert(File.RebuildInProgress);
1459 File.RebuildInProgress = false;
1460
1461 if (File.RebuildCounter == RequestRebuildCounter) {
1462 // Our rebuild request was successful.
1463 assert(futureIsReady(File.ASTFuture));
1464 assert(futureIsReady(File.PreambleFuture));
1465 } else {
1466 // Our rebuild request was cancelled, because further reparse was requested.
1467 assert(!futureIsReady(File.ASTFuture));
1468 assert(!futureIsReady(File.PreambleFuture));
1469 }
1470
1471 Lock.unlock();
1472 File.RebuildCond.notify_all();
1473}
Haojian Wu345099c2017-11-09 11:30:04 +00001474
1475SourceLocation clangd::getBeginningOfIdentifier(ParsedAST &Unit,
1476 const Position &Pos,
1477 const FileEntry *FE) {
1478 // The language server protocol uses zero-based line and column numbers.
1479 // Clang uses one-based numbers.
1480
1481 const ASTContext &AST = Unit.getASTContext();
1482 const SourceManager &SourceMgr = AST.getSourceManager();
1483
1484 SourceLocation InputLocation =
1485 getMacroArgExpandedLocation(SourceMgr, FE, Pos);
1486 if (Pos.character == 0) {
1487 return InputLocation;
1488 }
1489
1490 // This handle cases where the position is in the middle of a token or right
1491 // after the end of a token. In theory we could just use GetBeginningOfToken
1492 // to find the start of the token at the input position, but this doesn't
1493 // work when right after the end, i.e. foo|.
1494 // So try to go back by one and see if we're still inside the an identifier
1495 // token. If so, Take the beginning of this token.
1496 // (It should be the same identifier because you can't have two adjacent
1497 // identifiers without another token in between.)
1498 SourceLocation PeekBeforeLocation = getMacroArgExpandedLocation(
1499 SourceMgr, FE, Position{Pos.line, Pos.character - 1});
1500 Token Result;
1501 if (Lexer::getRawToken(PeekBeforeLocation, Result, SourceMgr,
1502 AST.getLangOpts(), false)) {
1503 // getRawToken failed, just use InputLocation.
1504 return InputLocation;
1505 }
1506
1507 if (Result.is(tok::raw_identifier)) {
1508 return Lexer::GetBeginningOfToken(PeekBeforeLocation, SourceMgr,
1509 AST.getLangOpts());
1510 }
1511
1512 return InputLocation;
1513}