1 //===--- CodeComplete.cpp ----------------------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Code completion has several moving parts:
10 // - AST-based completions are provided using the completion hooks in Sema.
11 // - external completions are retrieved from the index (using hints from Sema)
12 // - the two sources overlap, and must be merged and overloads bundled
13 // - results must be scored and ranked (see Quality.h) before rendering
14 //
15 // Signature help works in a similar way as code completion, but it is simpler:
16 // it's purely AST-based, and there are few candidates.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #include "CodeComplete.h"
21 #include "AST.h"
22 #include "CodeCompletionStrings.h"
23 #include "Compiler.h"
24 #include "ExpectedTypes.h"
25 #include "FileDistance.h"
26 #include "FuzzyMatch.h"
27 #include "Headers.h"
28 #include "Hover.h"
29 #include "Preamble.h"
30 #include "Protocol.h"
31 #include "Quality.h"
32 #include "SourceCode.h"
33 #include "URI.h"
34 #include "index/Index.h"
35 #include "index/Symbol.h"
36 #include "index/SymbolOrigin.h"
37 #include "support/Logger.h"
38 #include "support/Markup.h"
39 #include "support/Threading.h"
40 #include "support/ThreadsafeFS.h"
41 #include "support/Trace.h"
42 #include "clang/AST/Decl.h"
43 #include "clang/AST/DeclBase.h"
44 #include "clang/Basic/CharInfo.h"
45 #include "clang/Basic/LangOptions.h"
46 #include "clang/Basic/SourceLocation.h"
47 #include "clang/Basic/TokenKinds.h"
48 #include "clang/Format/Format.h"
49 #include "clang/Frontend/CompilerInstance.h"
50 #include "clang/Frontend/FrontendActions.h"
51 #include "clang/Lex/ExternalPreprocessorSource.h"
52 #include "clang/Lex/Lexer.h"
53 #include "clang/Lex/Preprocessor.h"
54 #include "clang/Lex/PreprocessorOptions.h"
55 #include "clang/Sema/CodeCompleteConsumer.h"
56 #include "clang/Sema/DeclSpec.h"
57 #include "clang/Sema/Sema.h"
58 #include "llvm/ADT/ArrayRef.h"
59 #include "llvm/ADT/None.h"
60 #include "llvm/ADT/Optional.h"
61 #include "llvm/ADT/SmallVector.h"
62 #include "llvm/ADT/StringExtras.h"
63 #include "llvm/ADT/StringRef.h"
64 #include "llvm/Support/Casting.h"
65 #include "llvm/Support/Compiler.h"
66 #include "llvm/Support/Debug.h"
67 #include "llvm/Support/Error.h"
68 #include "llvm/Support/FormatVariadic.h"
69 #include "llvm/Support/ScopedPrinter.h"
70 #include <algorithm>
71 #include <iterator>
72 #include <limits>
73
74 // We log detailed candidate here if you run with -debug-only=codecomplete.
75 #define DEBUG_TYPE "CodeComplete"
76
77 namespace clang {
78 namespace clangd {
79 namespace {
80
toCompletionItemKind(index::SymbolKind Kind)81 CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) {
82 using SK = index::SymbolKind;
83 switch (Kind) {
84 case SK::Unknown:
85 return CompletionItemKind::Missing;
86 case SK::Module:
87 case SK::Namespace:
88 case SK::NamespaceAlias:
89 return CompletionItemKind::Module;
90 case SK::Macro:
91 return CompletionItemKind::Text;
92 case SK::Enum:
93 return CompletionItemKind::Enum;
94 case SK::Struct:
95 return CompletionItemKind::Struct;
96 case SK::Class:
97 case SK::Protocol:
98 case SK::Extension:
99 case SK::Union:
100 return CompletionItemKind::Class;
101 case SK::TypeAlias:
102 // We use the same kind as the VSCode C++ extension.
103 // FIXME: pick a better option when we have one.
104 return CompletionItemKind::Interface;
105 case SK::Using:
106 return CompletionItemKind::Reference;
107 case SK::Function:
108 case SK::ConversionFunction:
109 return CompletionItemKind::Function;
110 case SK::Variable:
111 case SK::Parameter:
112 case SK::NonTypeTemplateParm:
113 return CompletionItemKind::Variable;
114 case SK::Field:
115 return CompletionItemKind::Field;
116 case SK::EnumConstant:
117 return CompletionItemKind::EnumMember;
118 case SK::InstanceMethod:
119 case SK::ClassMethod:
120 case SK::StaticMethod:
121 case SK::Destructor:
122 return CompletionItemKind::Method;
123 case SK::InstanceProperty:
124 case SK::ClassProperty:
125 case SK::StaticProperty:
126 return CompletionItemKind::Property;
127 case SK::Constructor:
128 return CompletionItemKind::Constructor;
129 case SK::TemplateTypeParm:
130 case SK::TemplateTemplateParm:
131 return CompletionItemKind::TypeParameter;
132 case SK::Concept:
133 return CompletionItemKind::Interface;
134 }
135 llvm_unreachable("Unhandled clang::index::SymbolKind.");
136 }
137
138 CompletionItemKind
toCompletionItemKind(CodeCompletionResult::ResultKind ResKind,const NamedDecl * Decl,CodeCompletionContext::Kind CtxKind)139 toCompletionItemKind(CodeCompletionResult::ResultKind ResKind,
140 const NamedDecl *Decl,
141 CodeCompletionContext::Kind CtxKind) {
142 if (Decl)
143 return toCompletionItemKind(index::getSymbolInfo(Decl).Kind);
144 if (CtxKind == CodeCompletionContext::CCC_IncludedFile)
145 return CompletionItemKind::File;
146 switch (ResKind) {
147 case CodeCompletionResult::RK_Declaration:
148 llvm_unreachable("RK_Declaration without Decl");
149 case CodeCompletionResult::RK_Keyword:
150 return CompletionItemKind::Keyword;
151 case CodeCompletionResult::RK_Macro:
152 return CompletionItemKind::Text; // unfortunately, there's no 'Macro'
153 // completion items in LSP.
154 case CodeCompletionResult::RK_Pattern:
155 return CompletionItemKind::Snippet;
156 }
157 llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
158 }
159
160 // FIXME: find a home for this (that can depend on both markup and Protocol).
renderDoc(const markup::Document & Doc,MarkupKind Kind)161 MarkupContent renderDoc(const markup::Document &Doc, MarkupKind Kind) {
162 MarkupContent Result;
163 Result.kind = Kind;
164 switch (Kind) {
165 case MarkupKind::PlainText:
166 Result.value.append(Doc.asPlainText());
167 break;
168 case MarkupKind::Markdown:
169 Result.value.append(Doc.asMarkdown());
170 break;
171 }
172 return Result;
173 }
174
175 // Identifier code completion result.
176 struct RawIdentifier {
177 llvm::StringRef Name;
178 unsigned References; // # of usages in file.
179 };
180
181 /// A code completion result, in clang-native form.
182 /// It may be promoted to a CompletionItem if it's among the top-ranked results.
183 struct CompletionCandidate {
184 llvm::StringRef Name; // Used for filtering and sorting.
185 // We may have a result from Sema, from the index, or both.
186 const CodeCompletionResult *SemaResult = nullptr;
187 const Symbol *IndexResult = nullptr;
188 const RawIdentifier *IdentifierResult = nullptr;
189 llvm::SmallVector<llvm::StringRef, 1> RankedIncludeHeaders;
190
191 // Returns a token identifying the overload set this is part of.
192 // 0 indicates it's not part of any overload set.
overloadSetclang::clangd::__anonc7a99c3f0111::CompletionCandidate193 size_t overloadSet(const CodeCompleteOptions &Opts, llvm::StringRef FileName,
194 IncludeInserter *Inserter) const {
195 if (!Opts.BundleOverloads.value_or(false))
196 return 0;
197
198 // Depending on the index implementation, we can see different header
199 // strings (literal or URI) mapping to the same file. We still want to
200 // bundle those, so we must resolve the header to be included here.
201 std::string HeaderForHash;
202 if (Inserter) {
203 if (auto Header = headerToInsertIfAllowed(Opts)) {
204 if (auto HeaderFile = toHeaderFile(*Header, FileName)) {
205 if (auto Spelled =
206 Inserter->calculateIncludePath(*HeaderFile, FileName))
207 HeaderForHash = *Spelled;
208 } else {
209 vlog("Code completion header path manipulation failed {0}",
210 HeaderFile.takeError());
211 }
212 }
213 }
214
215 llvm::SmallString<256> Scratch;
216 if (IndexResult) {
217 switch (IndexResult->SymInfo.Kind) {
218 case index::SymbolKind::ClassMethod:
219 case index::SymbolKind::InstanceMethod:
220 case index::SymbolKind::StaticMethod:
221 #ifndef NDEBUG
222 llvm_unreachable("Don't expect members from index in code completion");
223 #else
224 LLVM_FALLTHROUGH;
225 #endif
226 case index::SymbolKind::Function:
227 // We can't group overloads together that need different #includes.
228 // This could break #include insertion.
229 return llvm::hash_combine(
230 (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch),
231 HeaderForHash);
232 default:
233 return 0;
234 }
235 }
236 if (SemaResult) {
237 // We need to make sure we're consistent with the IndexResult case!
238 const NamedDecl *D = SemaResult->Declaration;
239 if (!D || !D->isFunctionOrFunctionTemplate())
240 return 0;
241 {
242 llvm::raw_svector_ostream OS(Scratch);
243 D->printQualifiedName(OS);
244 }
245 return llvm::hash_combine(Scratch, HeaderForHash);
246 }
247 assert(IdentifierResult);
248 return 0;
249 }
250
251 // The best header to include if include insertion is allowed.
252 llvm::Optional<llvm::StringRef>
headerToInsertIfAllowedclang::clangd::__anonc7a99c3f0111::CompletionCandidate253 headerToInsertIfAllowed(const CodeCompleteOptions &Opts) const {
254 if (Opts.InsertIncludes == CodeCompleteOptions::NeverInsert ||
255 RankedIncludeHeaders.empty())
256 return None;
257 if (SemaResult && SemaResult->Declaration) {
258 // Avoid inserting new #include if the declaration is found in the current
259 // file e.g. the symbol is forward declared.
260 auto &SM = SemaResult->Declaration->getASTContext().getSourceManager();
261 for (const Decl *RD : SemaResult->Declaration->redecls())
262 if (SM.isInMainFile(SM.getExpansionLoc(RD->getBeginLoc())))
263 return None;
264 }
265 return RankedIncludeHeaders[0];
266 }
267
268 using Bundle = llvm::SmallVector<CompletionCandidate, 4>;
269 };
270 using ScoredBundle =
271 std::pair<CompletionCandidate::Bundle, CodeCompletion::Scores>;
272 struct ScoredBundleGreater {
operator ()clang::clangd::__anonc7a99c3f0111::ScoredBundleGreater273 bool operator()(const ScoredBundle &L, const ScoredBundle &R) {
274 if (L.second.Total != R.second.Total)
275 return L.second.Total > R.second.Total;
276 return L.first.front().Name <
277 R.first.front().Name; // Earlier name is better.
278 }
279 };
280
281 // Assembles a code completion out of a bundle of >=1 completion candidates.
282 // Many of the expensive strings are only computed at this point, once we know
283 // the candidate bundle is going to be returned.
284 //
285 // Many fields are the same for all candidates in a bundle (e.g. name), and are
286 // computed from the first candidate, in the constructor.
287 // Others vary per candidate, so add() must be called for remaining candidates.
288 struct CodeCompletionBuilder {
CodeCompletionBuilderclang::clangd::__anonc7a99c3f0111::CodeCompletionBuilder289 CodeCompletionBuilder(ASTContext *ASTCtx, const CompletionCandidate &C,
290 CodeCompletionString *SemaCCS,
291 llvm::ArrayRef<std::string> QueryScopes,
292 const IncludeInserter &Includes,
293 llvm::StringRef FileName,
294 CodeCompletionContext::Kind ContextKind,
295 const CodeCompleteOptions &Opts,
296 bool IsUsingDeclaration, tok::TokenKind NextTokenKind)
297 : ASTCtx(ASTCtx),
298 EnableFunctionArgSnippets(Opts.EnableFunctionArgSnippets),
299 IsUsingDeclaration(IsUsingDeclaration), NextTokenKind(NextTokenKind) {
300 Completion.Deprecated = true; // cleared by any non-deprecated overload.
301 add(C, SemaCCS);
302 if (C.SemaResult) {
303 assert(ASTCtx);
304 Completion.Origin |= SymbolOrigin::AST;
305 Completion.Name = std::string(llvm::StringRef(SemaCCS->getTypedText()));
306 Completion.FilterText = SemaCCS->getAllTypedText();
307 if (Completion.Scope.empty()) {
308 if ((C.SemaResult->Kind == CodeCompletionResult::RK_Declaration) ||
309 (C.SemaResult->Kind == CodeCompletionResult::RK_Pattern))
310 if (const auto *D = C.SemaResult->getDeclaration())
311 if (const auto *ND = dyn_cast<NamedDecl>(D))
312 Completion.Scope = std::string(
313 splitQualifiedName(printQualifiedName(*ND)).first);
314 }
315 Completion.Kind = toCompletionItemKind(
316 C.SemaResult->Kind, C.SemaResult->Declaration, ContextKind);
317 // Sema could provide more info on whether the completion was a file or
318 // folder.
319 if (Completion.Kind == CompletionItemKind::File &&
320 Completion.Name.back() == '/')
321 Completion.Kind = CompletionItemKind::Folder;
322 for (const auto &FixIt : C.SemaResult->FixIts) {
323 Completion.FixIts.push_back(toTextEdit(
324 FixIt, ASTCtx->getSourceManager(), ASTCtx->getLangOpts()));
325 }
326 llvm::sort(Completion.FixIts, [](const TextEdit &X, const TextEdit &Y) {
327 return std::tie(X.range.start.line, X.range.start.character) <
328 std::tie(Y.range.start.line, Y.range.start.character);
329 });
330 }
331 if (C.IndexResult) {
332 Completion.Origin |= C.IndexResult->Origin;
333 if (Completion.Scope.empty())
334 Completion.Scope = std::string(C.IndexResult->Scope);
335 if (Completion.Kind == CompletionItemKind::Missing)
336 Completion.Kind = toCompletionItemKind(C.IndexResult->SymInfo.Kind);
337 if (Completion.Name.empty())
338 Completion.Name = std::string(C.IndexResult->Name);
339 if (Completion.FilterText.empty())
340 Completion.FilterText = Completion.Name;
341 // If the completion was visible to Sema, no qualifier is needed. This
342 // avoids unneeded qualifiers in cases like with `using ns::X`.
343 if (Completion.RequiredQualifier.empty() && !C.SemaResult) {
344 llvm::StringRef ShortestQualifier = C.IndexResult->Scope;
345 for (llvm::StringRef Scope : QueryScopes) {
346 llvm::StringRef Qualifier = C.IndexResult->Scope;
347 if (Qualifier.consume_front(Scope) &&
348 Qualifier.size() < ShortestQualifier.size())
349 ShortestQualifier = Qualifier;
350 }
351 Completion.RequiredQualifier = std::string(ShortestQualifier);
352 }
353 }
354 if (C.IdentifierResult) {
355 Completion.Origin |= SymbolOrigin::Identifier;
356 Completion.Kind = CompletionItemKind::Text;
357 Completion.Name = std::string(C.IdentifierResult->Name);
358 Completion.FilterText = Completion.Name;
359 }
360
361 // Turn absolute path into a literal string that can be #included.
362 auto Inserted = [&](llvm::StringRef Header)
363 -> llvm::Expected<std::pair<std::string, bool>> {
364 auto ResolvedDeclaring =
365 URI::resolve(C.IndexResult->CanonicalDeclaration.FileURI, FileName);
366 if (!ResolvedDeclaring)
367 return ResolvedDeclaring.takeError();
368 auto ResolvedInserted = toHeaderFile(Header, FileName);
369 if (!ResolvedInserted)
370 return ResolvedInserted.takeError();
371 auto Spelled = Includes.calculateIncludePath(*ResolvedInserted, FileName);
372 if (!Spelled)
373 return error("Header not on include path");
374 return std::make_pair(
375 std::move(*Spelled),
376 Includes.shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
377 };
378 bool ShouldInsert = C.headerToInsertIfAllowed(Opts).has_value();
379 // Calculate include paths and edits for all possible headers.
380 for (const auto &Inc : C.RankedIncludeHeaders) {
381 if (auto ToInclude = Inserted(Inc)) {
382 CodeCompletion::IncludeCandidate Include;
383 Include.Header = ToInclude->first;
384 if (ToInclude->second && ShouldInsert)
385 Include.Insertion = Includes.insert(ToInclude->first);
386 Completion.Includes.push_back(std::move(Include));
387 } else
388 log("Failed to generate include insertion edits for adding header "
389 "(FileURI='{0}', IncludeHeader='{1}') into {2}: {3}",
390 C.IndexResult->CanonicalDeclaration.FileURI, Inc, FileName,
391 ToInclude.takeError());
392 }
393 // Prefer includes that do not need edits (i.e. already exist).
394 std::stable_partition(Completion.Includes.begin(),
395 Completion.Includes.end(),
396 [](const CodeCompletion::IncludeCandidate &I) {
397 return !I.Insertion.has_value();
398 });
399 }
400
addclang::clangd::__anonc7a99c3f0111::CodeCompletionBuilder401 void add(const CompletionCandidate &C, CodeCompletionString *SemaCCS) {
402 assert(bool(C.SemaResult) == bool(SemaCCS));
403 Bundled.emplace_back();
404 BundledEntry &S = Bundled.back();
405 if (C.SemaResult) {
406 bool IsPattern = C.SemaResult->Kind == CodeCompletionResult::RK_Pattern;
407 getSignature(*SemaCCS, &S.Signature, &S.SnippetSuffix,
408 &Completion.RequiredQualifier, IsPattern);
409 S.ReturnType = getReturnType(*SemaCCS);
410 } else if (C.IndexResult) {
411 S.Signature = std::string(C.IndexResult->Signature);
412 S.SnippetSuffix = std::string(C.IndexResult->CompletionSnippetSuffix);
413 S.ReturnType = std::string(C.IndexResult->ReturnType);
414 }
415 if (!Completion.Documentation) {
416 auto SetDoc = [&](llvm::StringRef Doc) {
417 if (!Doc.empty()) {
418 Completion.Documentation.emplace();
419 parseDocumentation(Doc, *Completion.Documentation);
420 }
421 };
422 if (C.IndexResult) {
423 SetDoc(C.IndexResult->Documentation);
424 } else if (C.SemaResult) {
425 const auto DocComment = getDocComment(*ASTCtx, *C.SemaResult,
426 /*CommentsFromHeaders=*/false);
427 SetDoc(formatDocumentation(*SemaCCS, DocComment));
428 }
429 }
430 if (Completion.Deprecated) {
431 if (C.SemaResult)
432 Completion.Deprecated &=
433 C.SemaResult->Availability == CXAvailability_Deprecated;
434 if (C.IndexResult)
435 Completion.Deprecated &=
436 bool(C.IndexResult->Flags & Symbol::Deprecated);
437 }
438 }
439
buildclang::clangd::__anonc7a99c3f0111::CodeCompletionBuilder440 CodeCompletion build() {
441 Completion.ReturnType = summarizeReturnType();
442 Completion.Signature = summarizeSignature();
443 Completion.SnippetSuffix = summarizeSnippet();
444 Completion.BundleSize = Bundled.size();
445 return std::move(Completion);
446 }
447
448 private:
449 struct BundledEntry {
450 std::string SnippetSuffix;
451 std::string Signature;
452 std::string ReturnType;
453 };
454
455 // If all BundledEntries have the same value for a property, return it.
456 template <std::string BundledEntry::*Member>
onlyValueclang::clangd::__anonc7a99c3f0111::CodeCompletionBuilder457 const std::string *onlyValue() const {
458 auto B = Bundled.begin(), E = Bundled.end();
459 for (auto *I = B + 1; I != E; ++I)
460 if (I->*Member != B->*Member)
461 return nullptr;
462 return &(B->*Member);
463 }
464
onlyValueclang::clangd::__anonc7a99c3f0111::CodeCompletionBuilder465 template <bool BundledEntry::*Member> const bool *onlyValue() const {
466 auto B = Bundled.begin(), E = Bundled.end();
467 for (auto *I = B + 1; I != E; ++I)
468 if (I->*Member != B->*Member)
469 return nullptr;
470 return &(B->*Member);
471 }
472
summarizeReturnTypeclang::clangd::__anonc7a99c3f0111::CodeCompletionBuilder473 std::string summarizeReturnType() const {
474 if (auto *RT = onlyValue<&BundledEntry::ReturnType>())
475 return *RT;
476 return "";
477 }
478
summarizeSnippetclang::clangd::__anonc7a99c3f0111::CodeCompletionBuilder479 std::string summarizeSnippet() const {
480 if (IsUsingDeclaration)
481 return "";
482 auto *Snippet = onlyValue<&BundledEntry::SnippetSuffix>();
483 if (!Snippet)
484 // All bundles are function calls.
485 // FIXME(ibiryukov): sometimes add template arguments to a snippet, e.g.
486 // we need to complete 'forward<$1>($0)'.
487 return "($0)";
488
489 if (Snippet->empty())
490 return "";
491
492 bool MayHaveArgList = Completion.Kind == CompletionItemKind::Function ||
493 Completion.Kind == CompletionItemKind::Method ||
494 Completion.Kind == CompletionItemKind::Constructor ||
495 Completion.Kind == CompletionItemKind::Text /*Macro*/;
496 // If likely arg list already exists, don't add new parens & placeholders.
497 // Snippet: function(int x, int y)
498 // func^(1,2) -> function(1, 2)
499 // NOT function(int x, int y)(1, 2)
500 if (MayHaveArgList) {
501 // Check for a template argument list in the code.
502 // Snippet: function<class T>(int x)
503 // fu^<int>(1) -> function<int>(1)
504 if (NextTokenKind == tok::less && Snippet->front() == '<')
505 return "";
506 // Potentially followed by regular argument list.
507 if (NextTokenKind == tok::l_paren) {
508 // Snippet: function<class T>(int x)
509 // fu^(1,2) -> function<class T>(1, 2)
510 if (Snippet->front() == '<') {
511 // Find matching '>', handling nested brackets.
512 int Balance = 0;
513 size_t I = 0;
514 do {
515 if (Snippet->at(I) == '>')
516 --Balance;
517 else if (Snippet->at(I) == '<')
518 ++Balance;
519 ++I;
520 } while (Balance > 0);
521 return Snippet->substr(0, I);
522 }
523 return "";
524 }
525 }
526 if (EnableFunctionArgSnippets)
527 return *Snippet;
528
529 // Replace argument snippets with a simplified pattern.
530 if (MayHaveArgList) {
531 // Functions snippets can be of 2 types:
532 // - containing only function arguments, e.g.
533 // foo(${1:int p1}, ${2:int p2});
534 // We transform this pattern to '($0)' or '()'.
535 // - template arguments and function arguments, e.g.
536 // foo<${1:class}>(${2:int p1}).
537 // We transform this pattern to '<$1>()$0' or '<$0>()'.
538
539 bool EmptyArgs = llvm::StringRef(*Snippet).endswith("()");
540 if (Snippet->front() == '<')
541 return EmptyArgs ? "<$1>()$0" : "<$1>($0)";
542 if (Snippet->front() == '(')
543 return EmptyArgs ? "()" : "($0)";
544 return *Snippet; // Not an arg snippet?
545 }
546 // 'CompletionItemKind::Interface' matches template type aliases.
547 if (Completion.Kind == CompletionItemKind::Interface ||
548 Completion.Kind == CompletionItemKind::Class) {
549 if (Snippet->front() != '<')
550 return *Snippet; // Not an arg snippet?
551
552 // Classes and template using aliases can only have template arguments,
553 // e.g. Foo<${1:class}>.
554 if (llvm::StringRef(*Snippet).endswith("<>"))
555 return "<>"; // can happen with defaulted template arguments.
556 return "<$0>";
557 }
558 return *Snippet;
559 }
560
summarizeSignatureclang::clangd::__anonc7a99c3f0111::CodeCompletionBuilder561 std::string summarizeSignature() const {
562 if (auto *Signature = onlyValue<&BundledEntry::Signature>())
563 return *Signature;
564 // All bundles are function calls.
565 return "(…)";
566 }
567
568 // ASTCtx can be nullptr if not run with sema.
569 ASTContext *ASTCtx;
570 CodeCompletion Completion;
571 llvm::SmallVector<BundledEntry, 1> Bundled;
572 bool EnableFunctionArgSnippets;
573 // No snippets will be generated for using declarations and when the function
574 // arguments are already present.
575 bool IsUsingDeclaration;
576 tok::TokenKind NextTokenKind;
577 };
578
579 // Determine the symbol ID for a Sema code completion result, if possible.
getSymbolID(const CodeCompletionResult & R,const SourceManager & SM)580 SymbolID getSymbolID(const CodeCompletionResult &R, const SourceManager &SM) {
581 switch (R.Kind) {
582 case CodeCompletionResult::RK_Declaration:
583 case CodeCompletionResult::RK_Pattern: {
584 // Computing USR caches linkage, which may change after code completion.
585 if (hasUnstableLinkage(R.Declaration))
586 return {};
587 return clang::clangd::getSymbolID(R.Declaration);
588 }
589 case CodeCompletionResult::RK_Macro:
590 return clang::clangd::getSymbolID(R.Macro->getName(), R.MacroDefInfo, SM);
591 case CodeCompletionResult::RK_Keyword:
592 return {};
593 }
594 llvm_unreachable("unknown CodeCompletionResult kind");
595 }
596
597 // Scopes of the partial identifier we're trying to complete.
598 // It is used when we query the index for more completion results.
599 struct SpecifiedScope {
600 // The scopes we should look in, determined by Sema.
601 //
602 // If the qualifier was fully resolved, we look for completions in these
603 // scopes; if there is an unresolved part of the qualifier, it should be
604 // resolved within these scopes.
605 //
606 // Examples of qualified completion:
607 //
608 // "::vec" => {""}
609 // "using namespace std; ::vec^" => {"", "std::"}
610 // "namespace ns {using namespace std;} ns::^" => {"ns::", "std::"}
611 // "std::vec^" => {""} // "std" unresolved
612 //
613 // Examples of unqualified completion:
614 //
615 // "vec^" => {""}
616 // "using namespace std; vec^" => {"", "std::"}
617 // "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""}
618 //
619 // "" for global namespace, "ns::" for normal namespace.
620 std::vector<std::string> AccessibleScopes;
621 // The full scope qualifier as typed by the user (without the leading "::").
622 // Set if the qualifier is not fully resolved by Sema.
623 llvm::Optional<std::string> UnresolvedQualifier;
624
625 // Construct scopes being queried in indexes. The results are deduplicated.
626 // This method format the scopes to match the index request representation.
scopesForIndexQueryclang::clangd::__anonc7a99c3f0111::SpecifiedScope627 std::vector<std::string> scopesForIndexQuery() {
628 std::set<std::string> Results;
629 for (llvm::StringRef AS : AccessibleScopes)
630 Results.insert(
631 (AS + (UnresolvedQualifier ? *UnresolvedQualifier : "")).str());
632 return {Results.begin(), Results.end()};
633 }
634 };
635
636 // Get all scopes that will be queried in indexes and whether symbols from
637 // any scope is allowed. The first scope in the list is the preferred scope
638 // (e.g. enclosing namespace).
639 std::pair<std::vector<std::string>, bool>
getQueryScopes(CodeCompletionContext & CCContext,const Sema & CCSema,const CompletionPrefix & HeuristicPrefix,const CodeCompleteOptions & Opts)640 getQueryScopes(CodeCompletionContext &CCContext, const Sema &CCSema,
641 const CompletionPrefix &HeuristicPrefix,
642 const CodeCompleteOptions &Opts) {
643 SpecifiedScope Scopes;
644 for (auto *Context : CCContext.getVisitedContexts()) {
645 if (isa<TranslationUnitDecl>(Context))
646 Scopes.AccessibleScopes.push_back(""); // global namespace
647 else if (isa<NamespaceDecl>(Context))
648 Scopes.AccessibleScopes.push_back(printNamespaceScope(*Context));
649 }
650
651 const CXXScopeSpec *SemaSpecifier =
652 CCContext.getCXXScopeSpecifier().value_or(nullptr);
653 // Case 1: unqualified completion.
654 if (!SemaSpecifier) {
655 // Case 2 (exception): sema saw no qualifier, but there appears to be one!
656 // This can happen e.g. in incomplete macro expansions. Use heuristics.
657 if (!HeuristicPrefix.Qualifier.empty()) {
658 vlog("Sema said no scope specifier, but we saw {0} in the source code",
659 HeuristicPrefix.Qualifier);
660 StringRef SpelledSpecifier = HeuristicPrefix.Qualifier;
661 if (SpelledSpecifier.consume_front("::"))
662 Scopes.AccessibleScopes = {""};
663 Scopes.UnresolvedQualifier = std::string(SpelledSpecifier);
664 return {Scopes.scopesForIndexQuery(), false};
665 }
666 // The enclosing namespace must be first, it gets a quality boost.
667 std::vector<std::string> EnclosingAtFront;
668 std::string EnclosingScope = printNamespaceScope(*CCSema.CurContext);
669 EnclosingAtFront.push_back(EnclosingScope);
670 for (auto &S : Scopes.scopesForIndexQuery()) {
671 if (EnclosingScope != S)
672 EnclosingAtFront.push_back(std::move(S));
673 }
674 // Allow AllScopes completion as there is no explicit scope qualifier.
675 return {EnclosingAtFront, Opts.AllScopes};
676 }
677 // Case 3: sema saw and resolved a scope qualifier.
678 if (SemaSpecifier && SemaSpecifier->isValid())
679 return {Scopes.scopesForIndexQuery(), false};
680
681 // Case 4: There was a qualifier, and Sema didn't resolve it.
682 Scopes.AccessibleScopes.push_back(""); // Make sure global scope is included.
683 llvm::StringRef SpelledSpecifier = Lexer::getSourceText(
684 CharSourceRange::getCharRange(SemaSpecifier->getRange()),
685 CCSema.SourceMgr, clang::LangOptions());
686 if (SpelledSpecifier.consume_front("::"))
687 Scopes.AccessibleScopes = {""};
688 Scopes.UnresolvedQualifier = std::string(SpelledSpecifier);
689 // Sema excludes the trailing "::".
690 if (!Scopes.UnresolvedQualifier->empty())
691 *Scopes.UnresolvedQualifier += "::";
692
693 return {Scopes.scopesForIndexQuery(), false};
694 }
695
696 // Should we perform index-based completion in a context of the specified kind?
697 // FIXME: consider allowing completion, but restricting the result types.
contextAllowsIndex(enum CodeCompletionContext::Kind K)698 bool contextAllowsIndex(enum CodeCompletionContext::Kind K) {
699 switch (K) {
700 case CodeCompletionContext::CCC_TopLevel:
701 case CodeCompletionContext::CCC_ObjCInterface:
702 case CodeCompletionContext::CCC_ObjCImplementation:
703 case CodeCompletionContext::CCC_ObjCIvarList:
704 case CodeCompletionContext::CCC_ClassStructUnion:
705 case CodeCompletionContext::CCC_Statement:
706 case CodeCompletionContext::CCC_Expression:
707 case CodeCompletionContext::CCC_ObjCMessageReceiver:
708 case CodeCompletionContext::CCC_EnumTag:
709 case CodeCompletionContext::CCC_UnionTag:
710 case CodeCompletionContext::CCC_ClassOrStructTag:
711 case CodeCompletionContext::CCC_ObjCProtocolName:
712 case CodeCompletionContext::CCC_Namespace:
713 case CodeCompletionContext::CCC_Type:
714 case CodeCompletionContext::CCC_ParenthesizedExpression:
715 case CodeCompletionContext::CCC_ObjCInterfaceName:
716 case CodeCompletionContext::CCC_ObjCCategoryName:
717 case CodeCompletionContext::CCC_Symbol:
718 case CodeCompletionContext::CCC_SymbolOrNewName:
719 return true;
720 case CodeCompletionContext::CCC_OtherWithMacros:
721 case CodeCompletionContext::CCC_DotMemberAccess:
722 case CodeCompletionContext::CCC_ArrowMemberAccess:
723 case CodeCompletionContext::CCC_ObjCPropertyAccess:
724 case CodeCompletionContext::CCC_MacroName:
725 case CodeCompletionContext::CCC_MacroNameUse:
726 case CodeCompletionContext::CCC_PreprocessorExpression:
727 case CodeCompletionContext::CCC_PreprocessorDirective:
728 case CodeCompletionContext::CCC_SelectorName:
729 case CodeCompletionContext::CCC_TypeQualifiers:
730 case CodeCompletionContext::CCC_ObjCInstanceMessage:
731 case CodeCompletionContext::CCC_ObjCClassMessage:
732 case CodeCompletionContext::CCC_IncludedFile:
733 case CodeCompletionContext::CCC_Attribute:
734 // FIXME: Provide identifier based completions for the following contexts:
735 case CodeCompletionContext::CCC_Other: // Be conservative.
736 case CodeCompletionContext::CCC_NaturalLanguage:
737 case CodeCompletionContext::CCC_Recovery:
738 case CodeCompletionContext::CCC_NewName:
739 return false;
740 }
741 llvm_unreachable("unknown code completion context");
742 }
743
isInjectedClass(const NamedDecl & D)744 static bool isInjectedClass(const NamedDecl &D) {
745 if (auto *R = dyn_cast_or_null<RecordDecl>(&D))
746 if (R->isInjectedClassName())
747 return true;
748 return false;
749 }
750
751 // Some member calls are excluded because they're so rarely useful.
isExcludedMember(const NamedDecl & D)752 static bool isExcludedMember(const NamedDecl &D) {
753 // Destructor completion is rarely useful, and works inconsistently.
754 // (s.^ completes ~string, but s.~st^ is an error).
755 if (D.getKind() == Decl::CXXDestructor)
756 return true;
757 // Injected name may be useful for A::foo(), but who writes A::A::foo()?
758 if (isInjectedClass(D))
759 return true;
760 // Explicit calls to operators are also rare.
761 auto NameKind = D.getDeclName().getNameKind();
762 if (NameKind == DeclarationName::CXXOperatorName ||
763 NameKind == DeclarationName::CXXLiteralOperatorName ||
764 NameKind == DeclarationName::CXXConversionFunctionName)
765 return true;
766 return false;
767 }
768
769 // The CompletionRecorder captures Sema code-complete output, including context.
770 // It filters out ignored results (but doesn't apply fuzzy-filtering yet).
771 // It doesn't do scoring or conversion to CompletionItem yet, as we want to
772 // merge with index results first.
773 // Generally the fields and methods of this object should only be used from
774 // within the callback.
775 struct CompletionRecorder : public CodeCompleteConsumer {
CompletionRecorderclang::clangd::__anonc7a99c3f0111::CompletionRecorder776 CompletionRecorder(const CodeCompleteOptions &Opts,
777 llvm::unique_function<void()> ResultsCallback)
778 : CodeCompleteConsumer(Opts.getClangCompleteOpts()),
779 CCContext(CodeCompletionContext::CCC_Other), Opts(Opts),
780 CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
781 CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) {
782 assert(this->ResultsCallback);
783 }
784
785 std::vector<CodeCompletionResult> Results;
786 CodeCompletionContext CCContext;
787 Sema *CCSema = nullptr; // Sema that created the results.
788 // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead?
789
ProcessCodeCompleteResultsclang::clangd::__anonc7a99c3f0111::CompletionRecorder790 void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context,
791 CodeCompletionResult *InResults,
792 unsigned NumResults) final {
793 // Results from recovery mode are generally useless, and the callback after
794 // recovery (if any) is usually more interesting. To make sure we handle the
795 // future callback from sema, we just ignore all callbacks in recovery mode,
796 // as taking only results from recovery mode results in poor completion
797 // results.
798 // FIXME: in case there is no future sema completion callback after the
799 // recovery mode, we might still want to provide some results (e.g. trivial
800 // identifier-based completion).
801 if (Context.getKind() == CodeCompletionContext::CCC_Recovery) {
802 log("Code complete: Ignoring sema code complete callback with Recovery "
803 "context.");
804 return;
805 }
806 // If a callback is called without any sema result and the context does not
807 // support index-based completion, we simply skip it to give way to
808 // potential future callbacks with results.
809 if (NumResults == 0 && !contextAllowsIndex(Context.getKind()))
810 return;
811 if (CCSema) {
812 log("Multiple code complete callbacks (parser backtracked?). "
813 "Dropping results from context {0}, keeping results from {1}.",
814 getCompletionKindString(Context.getKind()),
815 getCompletionKindString(this->CCContext.getKind()));
816 return;
817 }
818 // Record the completion context.
819 CCSema = &S;
820 CCContext = Context;
821
822 // Retain the results we might want.
823 for (unsigned I = 0; I < NumResults; ++I) {
824 auto &Result = InResults[I];
825 // Class members that are shadowed by subclasses are usually noise.
826 if (Result.Hidden && Result.Declaration &&
827 Result.Declaration->isCXXClassMember())
828 continue;
829 if (!Opts.IncludeIneligibleResults &&
830 (Result.Availability == CXAvailability_NotAvailable ||
831 Result.Availability == CXAvailability_NotAccessible))
832 continue;
833 if (Result.Declaration &&
834 !Context.getBaseType().isNull() // is this a member-access context?
835 && isExcludedMember(*Result.Declaration))
836 continue;
837 // Skip injected class name when no class scope is not explicitly set.
838 // E.g. show injected A::A in `using A::A^` but not in "A^".
839 if (Result.Declaration && !Context.getCXXScopeSpecifier() &&
840 isInjectedClass(*Result.Declaration))
841 continue;
842 // We choose to never append '::' to completion results in clangd.
843 Result.StartsNestedNameSpecifier = false;
844 Results.push_back(Result);
845 }
846 ResultsCallback();
847 }
848
getAllocatorclang::clangd::__anonc7a99c3f0111::CompletionRecorder849 CodeCompletionAllocator &getAllocator() override { return *CCAllocator; }
getCodeCompletionTUInfoclang::clangd::__anonc7a99c3f0111::CompletionRecorder850 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
851
852 // Returns the filtering/sorting name for Result, which must be from Results.
853 // Returned string is owned by this recorder (or the AST).
getNameclang::clangd::__anonc7a99c3f0111::CompletionRecorder854 llvm::StringRef getName(const CodeCompletionResult &Result) {
855 switch (Result.Kind) {
856 case CodeCompletionResult::RK_Declaration:
857 if (auto *ID = Result.Declaration->getIdentifier())
858 return ID->getName();
859 break;
860 case CodeCompletionResult::RK_Keyword:
861 return Result.Keyword;
862 case CodeCompletionResult::RK_Macro:
863 return Result.Macro->getName();
864 case CodeCompletionResult::RK_Pattern:
865 break;
866 }
867 auto *CCS = codeCompletionString(Result);
868 const CodeCompletionString::Chunk *OnlyText = nullptr;
869 for (auto &C : *CCS) {
870 if (C.Kind != CodeCompletionString::CK_TypedText)
871 continue;
872 if (OnlyText)
873 return CCAllocator->CopyString(CCS->getAllTypedText());
874 OnlyText = &C;
875 }
876 return OnlyText ? OnlyText->Text : llvm::StringRef();
877 }
878
879 // Build a CodeCompletion string for R, which must be from Results.
880 // The CCS will be owned by this recorder.
codeCompletionStringclang::clangd::__anonc7a99c3f0111::CompletionRecorder881 CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) {
882 // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway.
883 return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString(
884 *CCSema, CCContext, *CCAllocator, CCTUInfo,
885 /*IncludeBriefComments=*/false);
886 }
887
888 private:
889 CodeCompleteOptions Opts;
890 std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
891 CodeCompletionTUInfo CCTUInfo;
892 llvm::unique_function<void()> ResultsCallback;
893 };
894
895 struct ScoredSignature {
896 // When not null, requires documentation to be requested from the index with
897 // this ID.
898 SymbolID IDForDoc;
899 SignatureInformation Signature;
900 SignatureQualitySignals Quality;
901 };
902
903 // Returns the index of the parameter matching argument number "Arg.
904 // This is usually just "Arg", except for variadic functions/templates, where
905 // "Arg" might be higher than the number of parameters. When that happens, we
906 // assume the last parameter is variadic and assume all further args are
907 // part of it.
paramIndexForArg(const CodeCompleteConsumer::OverloadCandidate & Candidate,int Arg)908 int paramIndexForArg(const CodeCompleteConsumer::OverloadCandidate &Candidate,
909 int Arg) {
910 int NumParams = Candidate.getNumParams();
911 if (auto *T = Candidate.getFunctionType()) {
912 if (auto *Proto = T->getAs<FunctionProtoType>()) {
913 if (Proto->isVariadic())
914 ++NumParams;
915 }
916 }
917 return std::min(Arg, std::max(NumParams - 1, 0));
918 }
919
920 class SignatureHelpCollector final : public CodeCompleteConsumer {
921 public:
SignatureHelpCollector(const clang::CodeCompleteOptions & CodeCompleteOpts,MarkupKind DocumentationFormat,const SymbolIndex * Index,SignatureHelp & SigHelp)922 SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
923 MarkupKind DocumentationFormat,
924 const SymbolIndex *Index, SignatureHelp &SigHelp)
925 : CodeCompleteConsumer(CodeCompleteOpts), SigHelp(SigHelp),
926 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
927 CCTUInfo(Allocator), Index(Index),
928 DocumentationFormat(DocumentationFormat) {}
929
ProcessOverloadCandidates(Sema & S,unsigned CurrentArg,OverloadCandidate * Candidates,unsigned NumCandidates,SourceLocation OpenParLoc,bool Braced)930 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
931 OverloadCandidate *Candidates,
932 unsigned NumCandidates,
933 SourceLocation OpenParLoc,
934 bool Braced) override {
935 assert(!OpenParLoc.isInvalid());
936 SourceManager &SrcMgr = S.getSourceManager();
937 OpenParLoc = SrcMgr.getFileLoc(OpenParLoc);
938 if (SrcMgr.isInMainFile(OpenParLoc))
939 SigHelp.argListStart = sourceLocToPosition(SrcMgr, OpenParLoc);
940 else
941 elog("Location oustide main file in signature help: {0}",
942 OpenParLoc.printToString(SrcMgr));
943
944 std::vector<ScoredSignature> ScoredSignatures;
945 SigHelp.signatures.reserve(NumCandidates);
946 ScoredSignatures.reserve(NumCandidates);
947 // FIXME(rwols): How can we determine the "active overload candidate"?
948 // Right now the overloaded candidates seem to be provided in a "best fit"
949 // order, so I'm not too worried about this.
950 SigHelp.activeSignature = 0;
951 assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
952 "too many arguments");
953
954 SigHelp.activeParameter = static_cast<int>(CurrentArg);
955
956 for (unsigned I = 0; I < NumCandidates; ++I) {
957 OverloadCandidate Candidate = Candidates[I];
958 // We want to avoid showing instantiated signatures, because they may be
959 // long in some cases (e.g. when 'T' is substituted with 'std::string', we
960 // would get 'std::basic_string<char>').
961 if (auto *Func = Candidate.getFunction()) {
962 if (auto *Pattern = Func->getTemplateInstantiationPattern())
963 Candidate = OverloadCandidate(Pattern);
964 }
965 if (static_cast<int>(I) == SigHelp.activeSignature) {
966 // The activeParameter in LSP relates to the activeSignature. There is
967 // another, per-signature field, but we currently do not use it and not
968 // all clients might support it.
969 // FIXME: Add support for per-signature activeParameter field.
970 SigHelp.activeParameter =
971 paramIndexForArg(Candidate, SigHelp.activeParameter);
972 }
973
974 const auto *CCS = Candidate.CreateSignatureString(
975 CurrentArg, S, *Allocator, CCTUInfo,
976 /*IncludeBriefComments=*/true, Braced);
977 assert(CCS && "Expected the CodeCompletionString to be non-null");
978 ScoredSignatures.push_back(processOverloadCandidate(
979 Candidate, *CCS,
980 Candidate.getFunction()
981 ? getDeclComment(S.getASTContext(), *Candidate.getFunction())
982 : ""));
983 }
984
985 // Sema does not load the docs from the preamble, so we need to fetch extra
986 // docs from the index instead.
987 llvm::DenseMap<SymbolID, std::string> FetchedDocs;
988 if (Index) {
989 LookupRequest IndexRequest;
990 for (const auto &S : ScoredSignatures) {
991 if (!S.IDForDoc)
992 continue;
993 IndexRequest.IDs.insert(S.IDForDoc);
994 }
995 Index->lookup(IndexRequest, [&](const Symbol &S) {
996 if (!S.Documentation.empty())
997 FetchedDocs[S.ID] = std::string(S.Documentation);
998 });
999 vlog("SigHelp: requested docs for {0} symbols from the index, got {1} "
1000 "symbols with non-empty docs in the response",
1001 IndexRequest.IDs.size(), FetchedDocs.size());
1002 }
1003
1004 llvm::sort(ScoredSignatures, [](const ScoredSignature &L,
1005 const ScoredSignature &R) {
1006 // Ordering follows:
1007 // - Less number of parameters is better.
1008 // - Aggregate > Function > FunctionType > FunctionTemplate
1009 // - High score is better.
1010 // - Shorter signature is better.
1011 // - Alphabetically smaller is better.
1012 if (L.Quality.NumberOfParameters != R.Quality.NumberOfParameters)
1013 return L.Quality.NumberOfParameters < R.Quality.NumberOfParameters;
1014 if (L.Quality.NumberOfOptionalParameters !=
1015 R.Quality.NumberOfOptionalParameters)
1016 return L.Quality.NumberOfOptionalParameters <
1017 R.Quality.NumberOfOptionalParameters;
1018 if (L.Quality.Kind != R.Quality.Kind) {
1019 using OC = CodeCompleteConsumer::OverloadCandidate;
1020 auto KindPriority = [&](OC::CandidateKind K) {
1021 switch (K) {
1022 case OC::CK_Aggregate:
1023 return 0;
1024 case OC::CK_Function:
1025 return 1;
1026 case OC::CK_FunctionType:
1027 return 2;
1028 case OC::CK_FunctionProtoTypeLoc:
1029 return 3;
1030 case OC::CK_FunctionTemplate:
1031 return 4;
1032 case OC::CK_Template:
1033 return 5;
1034 }
1035 llvm_unreachable("Unknown overload candidate type.");
1036 };
1037 return KindPriority(L.Quality.Kind) < KindPriority(R.Quality.Kind);
1038 }
1039 if (L.Signature.label.size() != R.Signature.label.size())
1040 return L.Signature.label.size() < R.Signature.label.size();
1041 return L.Signature.label < R.Signature.label;
1042 });
1043
1044 for (auto &SS : ScoredSignatures) {
1045 auto IndexDocIt =
1046 SS.IDForDoc ? FetchedDocs.find(SS.IDForDoc) : FetchedDocs.end();
1047 if (IndexDocIt != FetchedDocs.end()) {
1048 markup::Document SignatureComment;
1049 parseDocumentation(IndexDocIt->second, SignatureComment);
1050 SS.Signature.documentation =
1051 renderDoc(SignatureComment, DocumentationFormat);
1052 }
1053
1054 SigHelp.signatures.push_back(std::move(SS.Signature));
1055 }
1056 }
1057
getAllocator()1058 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
1059
getCodeCompletionTUInfo()1060 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
1061
1062 private:
processParameterChunk(llvm::StringRef ChunkText,SignatureInformation & Signature) const1063 void processParameterChunk(llvm::StringRef ChunkText,
1064 SignatureInformation &Signature) const {
1065 // (!) this is O(n), should still be fast compared to building ASTs.
1066 unsigned ParamStartOffset = lspLength(Signature.label);
1067 unsigned ParamEndOffset = ParamStartOffset + lspLength(ChunkText);
1068 // A piece of text that describes the parameter that corresponds to
1069 // the code-completion location within a function call, message send,
1070 // macro invocation, etc.
1071 Signature.label += ChunkText;
1072 ParameterInformation Info;
1073 Info.labelOffsets.emplace(ParamStartOffset, ParamEndOffset);
1074 // FIXME: only set 'labelOffsets' when all clients migrate out of it.
1075 Info.labelString = std::string(ChunkText);
1076
1077 Signature.parameters.push_back(std::move(Info));
1078 }
1079
processOptionalChunk(const CodeCompletionString & CCS,SignatureInformation & Signature,SignatureQualitySignals & Signal) const1080 void processOptionalChunk(const CodeCompletionString &CCS,
1081 SignatureInformation &Signature,
1082 SignatureQualitySignals &Signal) const {
1083 for (const auto &Chunk : CCS) {
1084 switch (Chunk.Kind) {
1085 case CodeCompletionString::CK_Optional:
1086 assert(Chunk.Optional &&
1087 "Expected the optional code completion string to be non-null.");
1088 processOptionalChunk(*Chunk.Optional, Signature, Signal);
1089 break;
1090 case CodeCompletionString::CK_VerticalSpace:
1091 break;
1092 case CodeCompletionString::CK_CurrentParameter:
1093 case CodeCompletionString::CK_Placeholder:
1094 processParameterChunk(Chunk.Text, Signature);
1095 Signal.NumberOfOptionalParameters++;
1096 break;
1097 default:
1098 Signature.label += Chunk.Text;
1099 break;
1100 }
1101 }
1102 }
1103
1104 // FIXME(ioeric): consider moving CodeCompletionString logic here to
1105 // CompletionString.h.
processOverloadCandidate(const OverloadCandidate & Candidate,const CodeCompletionString & CCS,llvm::StringRef DocComment) const1106 ScoredSignature processOverloadCandidate(const OverloadCandidate &Candidate,
1107 const CodeCompletionString &CCS,
1108 llvm::StringRef DocComment) const {
1109 SignatureInformation Signature;
1110 SignatureQualitySignals Signal;
1111 const char *ReturnType = nullptr;
1112
1113 markup::Document OverloadComment;
1114 parseDocumentation(formatDocumentation(CCS, DocComment), OverloadComment);
1115 Signature.documentation = renderDoc(OverloadComment, DocumentationFormat);
1116 Signal.Kind = Candidate.getKind();
1117
1118 for (const auto &Chunk : CCS) {
1119 switch (Chunk.Kind) {
1120 case CodeCompletionString::CK_ResultType:
1121 // A piece of text that describes the type of an entity or,
1122 // for functions and methods, the return type.
1123 assert(!ReturnType && "Unexpected CK_ResultType");
1124 ReturnType = Chunk.Text;
1125 break;
1126 case CodeCompletionString::CK_CurrentParameter:
1127 case CodeCompletionString::CK_Placeholder:
1128 processParameterChunk(Chunk.Text, Signature);
1129 Signal.NumberOfParameters++;
1130 break;
1131 case CodeCompletionString::CK_Optional: {
1132 // The rest of the parameters are defaulted/optional.
1133 assert(Chunk.Optional &&
1134 "Expected the optional code completion string to be non-null.");
1135 processOptionalChunk(*Chunk.Optional, Signature, Signal);
1136 break;
1137 }
1138 case CodeCompletionString::CK_VerticalSpace:
1139 break;
1140 default:
1141 Signature.label += Chunk.Text;
1142 break;
1143 }
1144 }
1145 if (ReturnType) {
1146 Signature.label += " -> ";
1147 Signature.label += ReturnType;
1148 }
1149 dlog("Signal for {0}: {1}", Signature, Signal);
1150 ScoredSignature Result;
1151 Result.Signature = std::move(Signature);
1152 Result.Quality = Signal;
1153 const FunctionDecl *Func = Candidate.getFunction();
1154 if (Func && Result.Signature.documentation.value.empty()) {
1155 // Computing USR caches linkage, which may change after code completion.
1156 if (!hasUnstableLinkage(Func))
1157 Result.IDForDoc = clangd::getSymbolID(Func);
1158 }
1159 return Result;
1160 }
1161
1162 SignatureHelp &SigHelp;
1163 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
1164 CodeCompletionTUInfo CCTUInfo;
1165 const SymbolIndex *Index;
1166 MarkupKind DocumentationFormat;
1167 }; // SignatureHelpCollector
1168
1169 // Used only for completion of C-style comments in function call (i.e.
1170 // /*foo=*/7). Similar to SignatureHelpCollector, but needs to do less work.
1171 class ParamNameCollector final : public CodeCompleteConsumer {
1172 public:
ParamNameCollector(const clang::CodeCompleteOptions & CodeCompleteOpts,std::set<std::string> & ParamNames)1173 ParamNameCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
1174 std::set<std::string> &ParamNames)
1175 : CodeCompleteConsumer(CodeCompleteOpts),
1176 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
1177 CCTUInfo(Allocator), ParamNames(ParamNames) {}
1178
ProcessOverloadCandidates(Sema & S,unsigned CurrentArg,OverloadCandidate * Candidates,unsigned NumCandidates,SourceLocation OpenParLoc,bool Braced)1179 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1180 OverloadCandidate *Candidates,
1181 unsigned NumCandidates,
1182 SourceLocation OpenParLoc,
1183 bool Braced) override {
1184 assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() &&
1185 "too many arguments");
1186
1187 for (unsigned I = 0; I < NumCandidates; ++I) {
1188 if (const NamedDecl *ND = Candidates[I].getParamDecl(CurrentArg))
1189 if (const auto *II = ND->getIdentifier())
1190 ParamNames.emplace(II->getName());
1191 }
1192 }
1193
1194 private:
getAllocator()1195 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
1196
getCodeCompletionTUInfo()1197 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
1198
1199 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
1200 CodeCompletionTUInfo CCTUInfo;
1201 std::set<std::string> &ParamNames;
1202 };
1203
1204 struct SemaCompleteInput {
1205 PathRef FileName;
1206 size_t Offset;
1207 const PreambleData &Preamble;
1208 const llvm::Optional<PreamblePatch> Patch;
1209 const ParseInputs &ParseInput;
1210 };
1211
loadMainFilePreambleMacros(const Preprocessor & PP,const PreambleData & Preamble)1212 void loadMainFilePreambleMacros(const Preprocessor &PP,
1213 const PreambleData &Preamble) {
1214 // The ExternalPreprocessorSource has our macros, if we know where to look.
1215 // We can read all the macros using PreambleMacros->ReadDefinedMacros(),
1216 // but this includes transitively included files, so may deserialize a lot.
1217 ExternalPreprocessorSource *PreambleMacros = PP.getExternalSource();
1218 // As we have the names of the macros, we can look up their IdentifierInfo
1219 // and then use this to load just the macros we want.
1220 const auto &ITable = PP.getIdentifierTable();
1221 IdentifierInfoLookup *PreambleIdentifiers =
1222 ITable.getExternalIdentifierLookup();
1223
1224 if (!PreambleIdentifiers || !PreambleMacros)
1225 return;
1226 for (const auto &MacroName : Preamble.Macros.Names) {
1227 if (ITable.find(MacroName.getKey()) != ITable.end())
1228 continue;
1229 if (auto *II = PreambleIdentifiers->get(MacroName.getKey()))
1230 if (II->isOutOfDate())
1231 PreambleMacros->updateOutOfDateIdentifier(*II);
1232 }
1233 }
1234
1235 // Invokes Sema code completion on a file.
1236 // If \p Includes is set, it will be updated based on the compiler invocation.
semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,const clang::CodeCompleteOptions & Options,const SemaCompleteInput & Input,IncludeStructure * Includes=nullptr)1237 bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
1238 const clang::CodeCompleteOptions &Options,
1239 const SemaCompleteInput &Input,
1240 IncludeStructure *Includes = nullptr) {
1241 trace::Span Tracer("Sema completion");
1242
1243 IgnoreDiagnostics IgnoreDiags;
1244 auto CI = buildCompilerInvocation(Input.ParseInput, IgnoreDiags);
1245 if (!CI) {
1246 elog("Couldn't create CompilerInvocation");
1247 return false;
1248 }
1249 auto &FrontendOpts = CI->getFrontendOpts();
1250 FrontendOpts.SkipFunctionBodies = true;
1251 // Disable typo correction in Sema.
1252 CI->getLangOpts()->SpellChecking = false;
1253 // Code completion won't trigger in delayed template bodies.
1254 // This is on-by-default in windows to allow parsing SDK headers; we're only
1255 // disabling it for the main-file (not preamble).
1256 CI->getLangOpts()->DelayedTemplateParsing = false;
1257 // Setup code completion.
1258 FrontendOpts.CodeCompleteOpts = Options;
1259 FrontendOpts.CodeCompletionAt.FileName = std::string(Input.FileName);
1260 std::tie(FrontendOpts.CodeCompletionAt.Line,
1261 FrontendOpts.CodeCompletionAt.Column) =
1262 offsetToClangLineColumn(Input.ParseInput.Contents, Input.Offset);
1263
1264 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1265 llvm::MemoryBuffer::getMemBuffer(Input.ParseInput.Contents,
1266 Input.FileName);
1267 // The diagnostic options must be set before creating a CompilerInstance.
1268 CI->getDiagnosticOpts().IgnoreWarnings = true;
1269 // We reuse the preamble whether it's valid or not. This is a
1270 // correctness/performance tradeoff: building without a preamble is slow, and
1271 // completion is latency-sensitive.
1272 // However, if we're completing *inside* the preamble section of the draft,
1273 // overriding the preamble will break sema completion. Fortunately we can just
1274 // skip all includes in this case; these completions are really simple.
1275 PreambleBounds PreambleRegion =
1276 ComputePreambleBounds(*CI->getLangOpts(), *ContentsBuffer, 0);
1277 bool CompletingInPreamble = Input.Offset < PreambleRegion.Size ||
1278 (!PreambleRegion.PreambleEndsAtStartOfLine &&
1279 Input.Offset == PreambleRegion.Size);
1280 if (Input.Patch)
1281 Input.Patch->apply(*CI);
1282 // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
1283 // the remapped buffers do not get freed.
1284 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
1285 Input.ParseInput.TFS->view(Input.ParseInput.CompileCommand.Directory);
1286 if (Input.Preamble.StatCache)
1287 VFS = Input.Preamble.StatCache->getConsumingFS(std::move(VFS));
1288 auto Clang = prepareCompilerInstance(
1289 std::move(CI), !CompletingInPreamble ? &Input.Preamble.Preamble : nullptr,
1290 std::move(ContentsBuffer), std::move(VFS), IgnoreDiags);
1291 Clang->getPreprocessorOpts().SingleFileParseMode = CompletingInPreamble;
1292 Clang->setCodeCompletionConsumer(Consumer.release());
1293
1294 SyntaxOnlyAction Action;
1295 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
1296 log("BeginSourceFile() failed when running codeComplete for {0}",
1297 Input.FileName);
1298 return false;
1299 }
1300 // Macros can be defined within the preamble region of the main file.
1301 // They don't fall nicely into our index/Sema dichotomy:
1302 // - they're not indexed for completion (they're not available across files)
1303 // - but Sema code complete won't see them: as part of the preamble, they're
1304 // deserialized only when mentioned.
1305 // Force them to be deserialized so SemaCodeComplete sees them.
1306 loadMainFilePreambleMacros(Clang->getPreprocessor(), Input.Preamble);
1307 if (Includes)
1308 Includes->collect(*Clang);
1309 if (llvm::Error Err = Action.Execute()) {
1310 log("Execute() failed when running codeComplete for {0}: {1}",
1311 Input.FileName, toString(std::move(Err)));
1312 return false;
1313 }
1314 Action.EndSourceFile();
1315
1316 return true;
1317 }
1318
1319 // Should we allow index completions in the specified context?
allowIndex(CodeCompletionContext & CC)1320 bool allowIndex(CodeCompletionContext &CC) {
1321 if (!contextAllowsIndex(CC.getKind()))
1322 return false;
1323 // We also avoid ClassName::bar (but allow namespace::bar).
1324 auto Scope = CC.getCXXScopeSpecifier();
1325 if (!Scope)
1326 return true;
1327 NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep();
1328 if (!NameSpec)
1329 return true;
1330 // We only query the index when qualifier is a namespace.
1331 // If it's a class, we rely solely on sema completions.
1332 switch (NameSpec->getKind()) {
1333 case NestedNameSpecifier::Global:
1334 case NestedNameSpecifier::Namespace:
1335 case NestedNameSpecifier::NamespaceAlias:
1336 return true;
1337 case NestedNameSpecifier::Super:
1338 case NestedNameSpecifier::TypeSpec:
1339 case NestedNameSpecifier::TypeSpecWithTemplate:
1340 // Unresolved inside a template.
1341 case NestedNameSpecifier::Identifier:
1342 return false;
1343 }
1344 llvm_unreachable("invalid NestedNameSpecifier kind");
1345 }
1346
startAsyncFuzzyFind(const SymbolIndex & Index,const FuzzyFindRequest & Req)1347 std::future<SymbolSlab> startAsyncFuzzyFind(const SymbolIndex &Index,
1348 const FuzzyFindRequest &Req) {
1349 return runAsync<SymbolSlab>([&Index, Req]() {
1350 trace::Span Tracer("Async fuzzyFind");
1351 SymbolSlab::Builder Syms;
1352 Index.fuzzyFind(Req, [&Syms](const Symbol &Sym) { Syms.insert(Sym); });
1353 return std::move(Syms).build();
1354 });
1355 }
1356
1357 // Creates a `FuzzyFindRequest` based on the cached index request from the
1358 // last completion, if any, and the speculated completion filter text in the
1359 // source code.
speculativeFuzzyFindRequestForCompletion(FuzzyFindRequest CachedReq,const CompletionPrefix & HeuristicPrefix)1360 FuzzyFindRequest speculativeFuzzyFindRequestForCompletion(
1361 FuzzyFindRequest CachedReq, const CompletionPrefix &HeuristicPrefix) {
1362 CachedReq.Query = std::string(HeuristicPrefix.Name);
1363 return CachedReq;
1364 }
1365
1366 // Runs Sema-based (AST) and Index-based completion, returns merged results.
1367 //
1368 // There are a few tricky considerations:
1369 // - the AST provides information needed for the index query (e.g. which
1370 // namespaces to search in). So Sema must start first.
1371 // - we only want to return the top results (Opts.Limit).
1372 // Building CompletionItems for everything else is wasteful, so we want to
1373 // preserve the "native" format until we're done with scoring.
1374 // - the data underlying Sema completion items is owned by the AST and various
1375 // other arenas, which must stay alive for us to build CompletionItems.
1376 // - we may get duplicate results from Sema and the Index, we need to merge.
1377 //
1378 // So we start Sema completion first, and do all our work in its callback.
1379 // We use the Sema context information to query the index.
1380 // Then we merge the two result sets, producing items that are Sema/Index/Both.
1381 // These items are scored, and the top N are synthesized into the LSP response.
1382 // Finally, we can clean up the data structures created by Sema completion.
1383 //
1384 // Main collaborators are:
1385 // - semaCodeComplete sets up the compiler machinery to run code completion.
1386 // - CompletionRecorder captures Sema completion results, including context.
1387 // - SymbolIndex (Opts.Index) provides index completion results as Symbols
1388 // - CompletionCandidates are the result of merging Sema and Index results.
1389 // Each candidate points to an underlying CodeCompletionResult (Sema), a
1390 // Symbol (Index), or both. It computes the result quality score.
1391 // CompletionCandidate also does conversion to CompletionItem (at the end).
1392 // - FuzzyMatcher scores how the candidate matches the partial identifier.
1393 // This score is combined with the result quality score for the final score.
1394 // - TopN determines the results with the best score.
1395 class CodeCompleteFlow {
1396 PathRef FileName;
1397 IncludeStructure Includes; // Complete once the compiler runs.
1398 SpeculativeFuzzyFind *SpecFuzzyFind; // Can be nullptr.
1399 const CodeCompleteOptions &Opts;
1400
1401 // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup.
1402 CompletionRecorder *Recorder = nullptr;
1403 CodeCompletionContext::Kind CCContextKind = CodeCompletionContext::CCC_Other;
1404 bool IsUsingDeclaration = false;
1405 // The snippets will not be generated if the token following completion
1406 // location is an opening parenthesis (tok::l_paren) because this would add
1407 // extra parenthesis.
1408 tok::TokenKind NextTokenKind = tok::eof;
1409 // Counters for logging.
1410 int NSema = 0, NIndex = 0, NSemaAndIndex = 0, NIdent = 0;
1411 bool Incomplete = false; // Would more be available with a higher limit?
1412 CompletionPrefix HeuristicPrefix;
1413 llvm::Optional<FuzzyMatcher> Filter; // Initialized once Sema runs.
1414 Range ReplacedRange;
1415 std::vector<std::string> QueryScopes; // Initialized once Sema runs.
1416 // Initialized once QueryScopes is initialized, if there are scopes.
1417 llvm::Optional<ScopeDistance> ScopeProximity;
1418 llvm::Optional<OpaqueType> PreferredType; // Initialized once Sema runs.
1419 // Whether to query symbols from any scope. Initialized once Sema runs.
1420 bool AllScopes = false;
1421 llvm::StringSet<> ContextWords;
1422 // Include-insertion and proximity scoring rely on the include structure.
1423 // This is available after Sema has run.
1424 llvm::Optional<IncludeInserter> Inserter; // Available during runWithSema.
1425 llvm::Optional<URIDistance> FileProximity; // Initialized once Sema runs.
1426 /// Speculative request based on the cached request and the filter text before
1427 /// the cursor.
1428 /// Initialized right before sema run. This is only set if `SpecFuzzyFind` is
1429 /// set and contains a cached request.
1430 llvm::Optional<FuzzyFindRequest> SpecReq;
1431
1432 public:
1433 // A CodeCompleteFlow object is only useful for calling run() exactly once.
CodeCompleteFlow(PathRef FileName,const IncludeStructure & Includes,SpeculativeFuzzyFind * SpecFuzzyFind,const CodeCompleteOptions & Opts)1434 CodeCompleteFlow(PathRef FileName, const IncludeStructure &Includes,
1435 SpeculativeFuzzyFind *SpecFuzzyFind,
1436 const CodeCompleteOptions &Opts)
1437 : FileName(FileName), Includes(Includes), SpecFuzzyFind(SpecFuzzyFind),
1438 Opts(Opts) {}
1439
run(const SemaCompleteInput & SemaCCInput)1440 CodeCompleteResult run(const SemaCompleteInput &SemaCCInput) && {
1441 trace::Span Tracer("CodeCompleteFlow");
1442 HeuristicPrefix = guessCompletionPrefix(SemaCCInput.ParseInput.Contents,
1443 SemaCCInput.Offset);
1444 populateContextWords(SemaCCInput.ParseInput.Contents);
1445 if (Opts.Index && SpecFuzzyFind && SpecFuzzyFind->CachedReq) {
1446 assert(!SpecFuzzyFind->Result.valid());
1447 SpecReq = speculativeFuzzyFindRequestForCompletion(
1448 *SpecFuzzyFind->CachedReq, HeuristicPrefix);
1449 SpecFuzzyFind->Result = startAsyncFuzzyFind(*Opts.Index, *SpecReq);
1450 }
1451
1452 // We run Sema code completion first. It builds an AST and calculates:
1453 // - completion results based on the AST.
1454 // - partial identifier and context. We need these for the index query.
1455 CodeCompleteResult Output;
1456 auto RecorderOwner = std::make_unique<CompletionRecorder>(Opts, [&]() {
1457 assert(Recorder && "Recorder is not set");
1458 CCContextKind = Recorder->CCContext.getKind();
1459 IsUsingDeclaration = Recorder->CCContext.isUsingDeclaration();
1460 auto Style = getFormatStyleForFile(SemaCCInput.FileName,
1461 SemaCCInput.ParseInput.Contents,
1462 *SemaCCInput.ParseInput.TFS);
1463 const auto NextToken = Lexer::findNextToken(
1464 Recorder->CCSema->getPreprocessor().getCodeCompletionLoc(),
1465 Recorder->CCSema->getSourceManager(), Recorder->CCSema->LangOpts);
1466 if (NextToken)
1467 NextTokenKind = NextToken->getKind();
1468 // If preprocessor was run, inclusions from preprocessor callback should
1469 // already be added to Includes.
1470 Inserter.emplace(
1471 SemaCCInput.FileName, SemaCCInput.ParseInput.Contents, Style,
1472 SemaCCInput.ParseInput.CompileCommand.Directory,
1473 &Recorder->CCSema->getPreprocessor().getHeaderSearchInfo());
1474 for (const auto &Inc : Includes.MainFileIncludes)
1475 Inserter->addExisting(Inc);
1476
1477 // Most of the cost of file proximity is in initializing the FileDistance
1478 // structures based on the observed includes, once per query. Conceptually
1479 // that happens here (though the per-URI-scheme initialization is lazy).
1480 // The per-result proximity scoring is (amortized) very cheap.
1481 FileDistanceOptions ProxOpts{}; // Use defaults.
1482 const auto &SM = Recorder->CCSema->getSourceManager();
1483 llvm::StringMap<SourceParams> ProxSources;
1484 auto MainFileID =
1485 Includes.getID(SM.getFileEntryForID(SM.getMainFileID()));
1486 assert(MainFileID);
1487 for (auto &HeaderIDAndDepth : Includes.includeDepth(*MainFileID)) {
1488 auto &Source =
1489 ProxSources[Includes.getRealPath(HeaderIDAndDepth.getFirst())];
1490 Source.Cost = HeaderIDAndDepth.getSecond() * ProxOpts.IncludeCost;
1491 // Symbols near our transitive includes are good, but only consider
1492 // things in the same directory or below it. Otherwise there can be
1493 // many false positives.
1494 if (HeaderIDAndDepth.getSecond() > 0)
1495 Source.MaxUpTraversals = 1;
1496 }
1497 FileProximity.emplace(ProxSources, ProxOpts);
1498
1499 Output = runWithSema();
1500 Inserter.reset(); // Make sure this doesn't out-live Clang.
1501 SPAN_ATTACH(Tracer, "sema_completion_kind",
1502 getCompletionKindString(CCContextKind));
1503 log("Code complete: sema context {0}, query scopes [{1}] (AnyScope={2}), "
1504 "expected type {3}{4}",
1505 getCompletionKindString(CCContextKind),
1506 llvm::join(QueryScopes.begin(), QueryScopes.end(), ","), AllScopes,
1507 PreferredType ? Recorder->CCContext.getPreferredType().getAsString()
1508 : "<none>",
1509 IsUsingDeclaration ? ", inside using declaration" : "");
1510 });
1511
1512 Recorder = RecorderOwner.get();
1513
1514 semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(),
1515 SemaCCInput, &Includes);
1516 logResults(Output, Tracer);
1517 return Output;
1518 }
1519
logResults(const CodeCompleteResult & Output,const trace::Span & Tracer)1520 void logResults(const CodeCompleteResult &Output, const trace::Span &Tracer) {
1521 SPAN_ATTACH(Tracer, "sema_results", NSema);
1522 SPAN_ATTACH(Tracer, "index_results", NIndex);
1523 SPAN_ATTACH(Tracer, "merged_results", NSemaAndIndex);
1524 SPAN_ATTACH(Tracer, "identifier_results", NIdent);
1525 SPAN_ATTACH(Tracer, "returned_results", int64_t(Output.Completions.size()));
1526 SPAN_ATTACH(Tracer, "incomplete", Output.HasMore);
1527 log("Code complete: {0} results from Sema, {1} from Index, "
1528 "{2} matched, {3} from identifiers, {4} returned{5}.",
1529 NSema, NIndex, NSemaAndIndex, NIdent, Output.Completions.size(),
1530 Output.HasMore ? " (incomplete)" : "");
1531 assert(!Opts.Limit || Output.Completions.size() <= Opts.Limit);
1532 // We don't assert that isIncomplete means we hit a limit.
1533 // Indexes may choose to impose their own limits even if we don't have one.
1534 }
1535
runWithoutSema(llvm::StringRef Content,size_t Offset,const ThreadsafeFS & TFS)1536 CodeCompleteResult runWithoutSema(llvm::StringRef Content, size_t Offset,
1537 const ThreadsafeFS &TFS) && {
1538 trace::Span Tracer("CodeCompleteWithoutSema");
1539 // Fill in fields normally set by runWithSema()
1540 HeuristicPrefix = guessCompletionPrefix(Content, Offset);
1541 populateContextWords(Content);
1542 CCContextKind = CodeCompletionContext::CCC_Recovery;
1543 IsUsingDeclaration = false;
1544 Filter = FuzzyMatcher(HeuristicPrefix.Name);
1545 auto Pos = offsetToPosition(Content, Offset);
1546 ReplacedRange.start = ReplacedRange.end = Pos;
1547 ReplacedRange.start.character -= HeuristicPrefix.Name.size();
1548
1549 llvm::StringMap<SourceParams> ProxSources;
1550 ProxSources[FileName].Cost = 0;
1551 FileProximity.emplace(ProxSources);
1552
1553 auto Style = getFormatStyleForFile(FileName, Content, TFS);
1554 // This will only insert verbatim headers.
1555 Inserter.emplace(FileName, Content, Style,
1556 /*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr);
1557
1558 auto Identifiers = collectIdentifiers(Content, Style);
1559 std::vector<RawIdentifier> IdentifierResults;
1560 for (const auto &IDAndCount : Identifiers) {
1561 RawIdentifier ID;
1562 ID.Name = IDAndCount.first();
1563 ID.References = IDAndCount.second;
1564 // Avoid treating typed filter as an identifier.
1565 if (ID.Name == HeuristicPrefix.Name)
1566 --ID.References;
1567 if (ID.References > 0)
1568 IdentifierResults.push_back(std::move(ID));
1569 }
1570
1571 // Simplified version of getQueryScopes():
1572 // - accessible scopes are determined heuristically.
1573 // - all-scopes query if no qualifier was typed (and it's allowed).
1574 SpecifiedScope Scopes;
1575 Scopes.AccessibleScopes = visibleNamespaces(
1576 Content.take_front(Offset), format::getFormattingLangOpts(Style));
1577 for (std::string &S : Scopes.AccessibleScopes)
1578 if (!S.empty())
1579 S.append("::"); // visibleNamespaces doesn't include trailing ::.
1580 if (HeuristicPrefix.Qualifier.empty())
1581 AllScopes = Opts.AllScopes;
1582 else if (HeuristicPrefix.Qualifier.startswith("::")) {
1583 Scopes.AccessibleScopes = {""};
1584 Scopes.UnresolvedQualifier =
1585 std::string(HeuristicPrefix.Qualifier.drop_front(2));
1586 } else
1587 Scopes.UnresolvedQualifier = std::string(HeuristicPrefix.Qualifier);
1588 // First scope is the (modified) enclosing scope.
1589 QueryScopes = Scopes.scopesForIndexQuery();
1590 ScopeProximity.emplace(QueryScopes);
1591
1592 SymbolSlab IndexResults = Opts.Index ? queryIndex() : SymbolSlab();
1593
1594 CodeCompleteResult Output = toCodeCompleteResult(mergeResults(
1595 /*SemaResults=*/{}, IndexResults, IdentifierResults));
1596 Output.RanParser = false;
1597 logResults(Output, Tracer);
1598 return Output;
1599 }
1600
1601 private:
populateContextWords(llvm::StringRef Content)1602 void populateContextWords(llvm::StringRef Content) {
1603 // Take last 3 lines before the completion point.
1604 unsigned RangeEnd = HeuristicPrefix.Qualifier.begin() - Content.data(),
1605 RangeBegin = RangeEnd;
1606 for (size_t I = 0; I < 3 && RangeBegin > 0; ++I) {
1607 auto PrevNL = Content.rfind('\n', RangeBegin);
1608 if (PrevNL == StringRef::npos) {
1609 RangeBegin = 0;
1610 break;
1611 }
1612 RangeBegin = PrevNL;
1613 }
1614
1615 ContextWords = collectWords(Content.slice(RangeBegin, RangeEnd));
1616 dlog("Completion context words: {0}",
1617 llvm::join(ContextWords.keys(), ", "));
1618 }
1619
1620 // This is called by run() once Sema code completion is done, but before the
1621 // Sema data structures are torn down. It does all the real work.
runWithSema()1622 CodeCompleteResult runWithSema() {
1623 const auto &CodeCompletionRange = CharSourceRange::getCharRange(
1624 Recorder->CCSema->getPreprocessor().getCodeCompletionTokenRange());
1625 // When we are getting completions with an empty identifier, for example
1626 // std::vector<int> asdf;
1627 // asdf.^;
1628 // Then the range will be invalid and we will be doing insertion, use
1629 // current cursor position in such cases as range.
1630 if (CodeCompletionRange.isValid()) {
1631 ReplacedRange = halfOpenToRange(Recorder->CCSema->getSourceManager(),
1632 CodeCompletionRange);
1633 } else {
1634 const auto &Pos = sourceLocToPosition(
1635 Recorder->CCSema->getSourceManager(),
1636 Recorder->CCSema->getPreprocessor().getCodeCompletionLoc());
1637 ReplacedRange.start = ReplacedRange.end = Pos;
1638 }
1639 Filter = FuzzyMatcher(
1640 Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
1641 std::tie(QueryScopes, AllScopes) = getQueryScopes(
1642 Recorder->CCContext, *Recorder->CCSema, HeuristicPrefix, Opts);
1643 if (!QueryScopes.empty())
1644 ScopeProximity.emplace(QueryScopes);
1645 PreferredType =
1646 OpaqueType::fromType(Recorder->CCSema->getASTContext(),
1647 Recorder->CCContext.getPreferredType());
1648 // Sema provides the needed context to query the index.
1649 // FIXME: in addition to querying for extra/overlapping symbols, we should
1650 // explicitly request symbols corresponding to Sema results.
1651 // We can use their signals even if the index can't suggest them.
1652 // We must copy index results to preserve them, but there are at most Limit.
1653 auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext))
1654 ? queryIndex()
1655 : SymbolSlab();
1656 trace::Span Tracer("Populate CodeCompleteResult");
1657 // Merge Sema and Index results, score them, and pick the winners.
1658 auto Top =
1659 mergeResults(Recorder->Results, IndexResults, /*Identifiers*/ {});
1660 return toCodeCompleteResult(Top);
1661 }
1662
1663 CodeCompleteResult
toCodeCompleteResult(const std::vector<ScoredBundle> & Scored)1664 toCodeCompleteResult(const std::vector<ScoredBundle> &Scored) {
1665 CodeCompleteResult Output;
1666
1667 // Convert the results to final form, assembling the expensive strings.
1668 for (auto &C : Scored) {
1669 Output.Completions.push_back(toCodeCompletion(C.first));
1670 Output.Completions.back().Score = C.second;
1671 Output.Completions.back().CompletionTokenRange = ReplacedRange;
1672 }
1673 Output.HasMore = Incomplete;
1674 Output.Context = CCContextKind;
1675 Output.CompletionRange = ReplacedRange;
1676 return Output;
1677 }
1678
includeSymbolFromIndex(const Symbol & Sym)1679 bool includeSymbolFromIndex(const Symbol &Sym) {
1680 if (CCContextKind == CodeCompletionContext::CCC_ObjCProtocolName) {
1681 return Sym.SymInfo.Lang == index::SymbolLanguage::ObjC &&
1682 Sym.SymInfo.Kind == index::SymbolKind::Protocol;
1683 }
1684 return true;
1685 }
1686
queryIndex()1687 SymbolSlab queryIndex() {
1688 trace::Span Tracer("Query index");
1689 SPAN_ATTACH(Tracer, "limit", int64_t(Opts.Limit));
1690
1691 // Build the query.
1692 FuzzyFindRequest Req;
1693 if (Opts.Limit)
1694 Req.Limit = Opts.Limit;
1695 Req.Query = std::string(Filter->pattern());
1696 Req.RestrictForCodeCompletion = true;
1697 Req.Scopes = QueryScopes;
1698 Req.AnyScope = AllScopes;
1699 // FIXME: we should send multiple weighted paths here.
1700 Req.ProximityPaths.push_back(std::string(FileName));
1701 if (PreferredType)
1702 Req.PreferredTypes.push_back(std::string(PreferredType->raw()));
1703 vlog("Code complete: fuzzyFind({0:2})", toJSON(Req));
1704
1705 if (SpecFuzzyFind)
1706 SpecFuzzyFind->NewReq = Req;
1707 if (SpecFuzzyFind && SpecFuzzyFind->Result.valid() && (*SpecReq == Req)) {
1708 vlog("Code complete: speculative fuzzy request matches the actual index "
1709 "request. Waiting for the speculative index results.");
1710 SPAN_ATTACH(Tracer, "Speculative results", true);
1711
1712 trace::Span WaitSpec("Wait speculative results");
1713 return SpecFuzzyFind->Result.get();
1714 }
1715
1716 SPAN_ATTACH(Tracer, "Speculative results", false);
1717
1718 // Run the query against the index.
1719 SymbolSlab::Builder ResultsBuilder;
1720 if (Opts.Index->fuzzyFind(Req, [&](const Symbol &Sym) {
1721 if (includeSymbolFromIndex(Sym))
1722 ResultsBuilder.insert(Sym);
1723 }))
1724 Incomplete = true;
1725 return std::move(ResultsBuilder).build();
1726 }
1727
1728 // Merges Sema and Index results where possible, to form CompletionCandidates.
1729 // \p Identifiers is raw identifiers that can also be completion candidates.
1730 // Identifiers are not merged with results from index or sema.
1731 // Groups overloads if desired, to form CompletionCandidate::Bundles. The
1732 // bundles are scored and top results are returned, best to worst.
1733 std::vector<ScoredBundle>
mergeResults(const std::vector<CodeCompletionResult> & SemaResults,const SymbolSlab & IndexResults,const std::vector<RawIdentifier> & IdentifierResults)1734 mergeResults(const std::vector<CodeCompletionResult> &SemaResults,
1735 const SymbolSlab &IndexResults,
1736 const std::vector<RawIdentifier> &IdentifierResults) {
1737 trace::Span Tracer("Merge and score results");
1738 std::vector<CompletionCandidate::Bundle> Bundles;
1739 llvm::DenseMap<size_t, size_t> BundleLookup;
1740 auto AddToBundles = [&](const CodeCompletionResult *SemaResult,
1741 const Symbol *IndexResult,
1742 const RawIdentifier *IdentifierResult) {
1743 CompletionCandidate C;
1744 C.SemaResult = SemaResult;
1745 C.IndexResult = IndexResult;
1746 C.IdentifierResult = IdentifierResult;
1747 if (C.IndexResult) {
1748 C.Name = IndexResult->Name;
1749 C.RankedIncludeHeaders = getRankedIncludes(*C.IndexResult);
1750 } else if (C.SemaResult) {
1751 C.Name = Recorder->getName(*SemaResult);
1752 } else {
1753 assert(IdentifierResult);
1754 C.Name = IdentifierResult->Name;
1755 }
1756 if (auto OverloadSet = C.overloadSet(
1757 Opts, FileName, Inserter ? Inserter.getPointer() : nullptr)) {
1758 auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
1759 if (Ret.second)
1760 Bundles.emplace_back();
1761 Bundles[Ret.first->second].push_back(std::move(C));
1762 } else {
1763 Bundles.emplace_back();
1764 Bundles.back().push_back(std::move(C));
1765 }
1766 };
1767 llvm::DenseSet<const Symbol *> UsedIndexResults;
1768 auto CorrespondingIndexResult =
1769 [&](const CodeCompletionResult &SemaResult) -> const Symbol * {
1770 if (auto SymID =
1771 getSymbolID(SemaResult, Recorder->CCSema->getSourceManager())) {
1772 auto I = IndexResults.find(SymID);
1773 if (I != IndexResults.end()) {
1774 UsedIndexResults.insert(&*I);
1775 return &*I;
1776 }
1777 }
1778 return nullptr;
1779 };
1780 // Emit all Sema results, merging them with Index results if possible.
1781 for (auto &SemaResult : SemaResults)
1782 AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult), nullptr);
1783 // Now emit any Index-only results.
1784 for (const auto &IndexResult : IndexResults) {
1785 if (UsedIndexResults.count(&IndexResult))
1786 continue;
1787 AddToBundles(/*SemaResult=*/nullptr, &IndexResult, nullptr);
1788 }
1789 // Emit identifier results.
1790 for (const auto &Ident : IdentifierResults)
1791 AddToBundles(/*SemaResult=*/nullptr, /*IndexResult=*/nullptr, &Ident);
1792 // We only keep the best N results at any time, in "native" format.
1793 TopN<ScoredBundle, ScoredBundleGreater> Top(
1794 Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit);
1795 for (auto &Bundle : Bundles)
1796 addCandidate(Top, std::move(Bundle));
1797 return std::move(Top).items();
1798 }
1799
fuzzyScore(const CompletionCandidate & C)1800 llvm::Optional<float> fuzzyScore(const CompletionCandidate &C) {
1801 // Macros can be very spammy, so we only support prefix completion.
1802 if (((C.SemaResult &&
1803 C.SemaResult->Kind == CodeCompletionResult::RK_Macro) ||
1804 (C.IndexResult &&
1805 C.IndexResult->SymInfo.Kind == index::SymbolKind::Macro)) &&
1806 !C.Name.startswith_insensitive(Filter->pattern()))
1807 return None;
1808 return Filter->match(C.Name);
1809 }
1810
1811 CodeCompletion::Scores
evaluateCompletion(const SymbolQualitySignals & Quality,const SymbolRelevanceSignals & Relevance)1812 evaluateCompletion(const SymbolQualitySignals &Quality,
1813 const SymbolRelevanceSignals &Relevance) {
1814 using RM = CodeCompleteOptions::CodeCompletionRankingModel;
1815 CodeCompletion::Scores Scores;
1816 switch (Opts.RankingModel) {
1817 case RM::Heuristics:
1818 Scores.Quality = Quality.evaluateHeuristics();
1819 Scores.Relevance = Relevance.evaluateHeuristics();
1820 Scores.Total =
1821 evaluateSymbolAndRelevance(Scores.Quality, Scores.Relevance);
1822 // NameMatch is in fact a multiplier on total score, so rescoring is
1823 // sound.
1824 Scores.ExcludingName =
1825 Relevance.NameMatch > std::numeric_limits<float>::epsilon()
1826 ? Scores.Total / Relevance.NameMatch
1827 : Scores.Quality;
1828 return Scores;
1829
1830 case RM::DecisionForest:
1831 DecisionForestScores DFScores = Opts.DecisionForestScorer(
1832 Quality, Relevance, Opts.DecisionForestBase);
1833 Scores.ExcludingName = DFScores.ExcludingName;
1834 Scores.Total = DFScores.Total;
1835 return Scores;
1836 }
1837 llvm_unreachable("Unhandled CodeCompletion ranking model.");
1838 }
1839
1840 // Scores a candidate and adds it to the TopN structure.
addCandidate(TopN<ScoredBundle,ScoredBundleGreater> & Candidates,CompletionCandidate::Bundle Bundle)1841 void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates,
1842 CompletionCandidate::Bundle Bundle) {
1843 SymbolQualitySignals Quality;
1844 SymbolRelevanceSignals Relevance;
1845 Relevance.Context = CCContextKind;
1846 Relevance.Name = Bundle.front().Name;
1847 Relevance.FilterLength = HeuristicPrefix.Name.size();
1848 Relevance.Query = SymbolRelevanceSignals::CodeComplete;
1849 Relevance.FileProximityMatch = FileProximity.getPointer();
1850 if (ScopeProximity)
1851 Relevance.ScopeProximityMatch = ScopeProximity.getPointer();
1852 if (PreferredType)
1853 Relevance.HadContextType = true;
1854 Relevance.ContextWords = &ContextWords;
1855 Relevance.MainFileSignals = Opts.MainFileSignals;
1856
1857 auto &First = Bundle.front();
1858 if (auto FuzzyScore = fuzzyScore(First))
1859 Relevance.NameMatch = *FuzzyScore;
1860 else
1861 return;
1862 SymbolOrigin Origin = SymbolOrigin::Unknown;
1863 bool FromIndex = false;
1864 for (const auto &Candidate : Bundle) {
1865 if (Candidate.IndexResult) {
1866 Quality.merge(*Candidate.IndexResult);
1867 Relevance.merge(*Candidate.IndexResult);
1868 Origin |= Candidate.IndexResult->Origin;
1869 FromIndex = true;
1870 if (!Candidate.IndexResult->Type.empty())
1871 Relevance.HadSymbolType |= true;
1872 if (PreferredType &&
1873 PreferredType->raw() == Candidate.IndexResult->Type) {
1874 Relevance.TypeMatchesPreferred = true;
1875 }
1876 }
1877 if (Candidate.SemaResult) {
1878 Quality.merge(*Candidate.SemaResult);
1879 Relevance.merge(*Candidate.SemaResult);
1880 if (PreferredType) {
1881 if (auto CompletionType = OpaqueType::fromCompletionResult(
1882 Recorder->CCSema->getASTContext(), *Candidate.SemaResult)) {
1883 Relevance.HadSymbolType |= true;
1884 if (PreferredType == CompletionType)
1885 Relevance.TypeMatchesPreferred = true;
1886 }
1887 }
1888 Origin |= SymbolOrigin::AST;
1889 }
1890 if (Candidate.IdentifierResult) {
1891 Quality.References = Candidate.IdentifierResult->References;
1892 Relevance.Scope = SymbolRelevanceSignals::FileScope;
1893 Origin |= SymbolOrigin::Identifier;
1894 }
1895 }
1896
1897 CodeCompletion::Scores Scores = evaluateCompletion(Quality, Relevance);
1898 if (Opts.RecordCCResult)
1899 Opts.RecordCCResult(toCodeCompletion(Bundle), Quality, Relevance,
1900 Scores.Total);
1901
1902 dlog("CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First.Name,
1903 llvm::to_string(Origin), Scores.Total, llvm::to_string(Quality),
1904 llvm::to_string(Relevance));
1905
1906 NSema += bool(Origin & SymbolOrigin::AST);
1907 NIndex += FromIndex;
1908 NSemaAndIndex += bool(Origin & SymbolOrigin::AST) && FromIndex;
1909 NIdent += bool(Origin & SymbolOrigin::Identifier);
1910 if (Candidates.push({std::move(Bundle), Scores}))
1911 Incomplete = true;
1912 }
1913
toCodeCompletion(const CompletionCandidate::Bundle & Bundle)1914 CodeCompletion toCodeCompletion(const CompletionCandidate::Bundle &Bundle) {
1915 llvm::Optional<CodeCompletionBuilder> Builder;
1916 for (const auto &Item : Bundle) {
1917 CodeCompletionString *SemaCCS =
1918 Item.SemaResult ? Recorder->codeCompletionString(*Item.SemaResult)
1919 : nullptr;
1920 if (!Builder)
1921 Builder.emplace(Recorder ? &Recorder->CCSema->getASTContext() : nullptr,
1922 Item, SemaCCS, QueryScopes, *Inserter, FileName,
1923 CCContextKind, Opts, IsUsingDeclaration, NextTokenKind);
1924 else
1925 Builder->add(Item, SemaCCS);
1926 }
1927 return Builder->build();
1928 }
1929 };
1930
1931 } // namespace
1932
getClangCompleteOpts() const1933 clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const {
1934 clang::CodeCompleteOptions Result;
1935 Result.IncludeCodePatterns = EnableSnippets;
1936 Result.IncludeMacros = true;
1937 Result.IncludeGlobals = true;
1938 // We choose to include full comments and not do doxygen parsing in
1939 // completion.
1940 // FIXME: ideally, we should support doxygen in some form, e.g. do markdown
1941 // formatting of the comments.
1942 Result.IncludeBriefComments = false;
1943
1944 // When an is used, Sema is responsible for completing the main file,
1945 // the index can provide results from the preamble.
1946 // Tell Sema not to deserialize the preamble to look for results.
1947 Result.LoadExternal = !Index;
1948 Result.IncludeFixIts = IncludeFixIts;
1949
1950 return Result;
1951 }
1952
guessCompletionPrefix(llvm::StringRef Content,unsigned Offset)1953 CompletionPrefix guessCompletionPrefix(llvm::StringRef Content,
1954 unsigned Offset) {
1955 assert(Offset <= Content.size());
1956 StringRef Rest = Content.take_front(Offset);
1957 CompletionPrefix Result;
1958
1959 // Consume the unqualified name. We only handle ASCII characters.
1960 // isAsciiIdentifierContinue will let us match "0invalid", but we don't mind.
1961 while (!Rest.empty() && isAsciiIdentifierContinue(Rest.back()))
1962 Rest = Rest.drop_back();
1963 Result.Name = Content.slice(Rest.size(), Offset);
1964
1965 // Consume qualifiers.
1966 while (Rest.consume_back("::") && !Rest.endswith(":")) // reject ::::
1967 while (!Rest.empty() && isAsciiIdentifierContinue(Rest.back()))
1968 Rest = Rest.drop_back();
1969 Result.Qualifier =
1970 Content.slice(Rest.size(), Result.Name.begin() - Content.begin());
1971
1972 return Result;
1973 }
1974
1975 // Code complete the argument name on "/*" inside function call.
1976 // Offset should be pointing to the start of the comment, i.e.:
1977 // foo(^/*, rather than foo(/*^) where the cursor probably is.
codeCompleteComment(PathRef FileName,unsigned Offset,llvm::StringRef Prefix,const PreambleData * Preamble,const ParseInputs & ParseInput)1978 CodeCompleteResult codeCompleteComment(PathRef FileName, unsigned Offset,
1979 llvm::StringRef Prefix,
1980 const PreambleData *Preamble,
1981 const ParseInputs &ParseInput) {
1982 if (Preamble == nullptr) // Can't run without Sema.
1983 return CodeCompleteResult();
1984
1985 clang::CodeCompleteOptions Options;
1986 Options.IncludeGlobals = false;
1987 Options.IncludeMacros = false;
1988 Options.IncludeCodePatterns = false;
1989 Options.IncludeBriefComments = false;
1990 std::set<std::string> ParamNames;
1991 // We want to see signatures coming from newly introduced includes, hence a
1992 // full patch.
1993 semaCodeComplete(
1994 std::make_unique<ParamNameCollector>(Options, ParamNames), Options,
1995 {FileName, Offset, *Preamble,
1996 PreamblePatch::createFullPatch(FileName, ParseInput, *Preamble),
1997 ParseInput});
1998 if (ParamNames.empty())
1999 return CodeCompleteResult();
2000
2001 CodeCompleteResult Result;
2002 Result.Context = CodeCompletionContext::CCC_NaturalLanguage;
2003 for (llvm::StringRef Name : ParamNames) {
2004 if (!Name.startswith(Prefix))
2005 continue;
2006 CodeCompletion Item;
2007 Item.Name = Name.str() + "=";
2008 Item.FilterText = Item.Name;
2009 Item.Kind = CompletionItemKind::Text;
2010 Result.Completions.push_back(Item);
2011 }
2012
2013 return Result;
2014 }
2015
2016 // If Offset is inside what looks like argument comment (e.g.
2017 // "/*^" or "/* foo^"), returns new offset pointing to the start of the /*
2018 // (place where semaCodeComplete should run).
2019 llvm::Optional<unsigned>
maybeFunctionArgumentCommentStart(llvm::StringRef Content)2020 maybeFunctionArgumentCommentStart(llvm::StringRef Content) {
2021 while (!Content.empty() && isAsciiIdentifierContinue(Content.back()))
2022 Content = Content.drop_back();
2023 Content = Content.rtrim();
2024 if (Content.endswith("/*"))
2025 return Content.size() - 2;
2026 return None;
2027 }
2028
codeComplete(PathRef FileName,Position Pos,const PreambleData * Preamble,const ParseInputs & ParseInput,CodeCompleteOptions Opts,SpeculativeFuzzyFind * SpecFuzzyFind)2029 CodeCompleteResult codeComplete(PathRef FileName, Position Pos,
2030 const PreambleData *Preamble,
2031 const ParseInputs &ParseInput,
2032 CodeCompleteOptions Opts,
2033 SpeculativeFuzzyFind *SpecFuzzyFind) {
2034 auto Offset = positionToOffset(ParseInput.Contents, Pos);
2035 if (!Offset) {
2036 elog("Code completion position was invalid {0}", Offset.takeError());
2037 return CodeCompleteResult();
2038 }
2039
2040 auto Content = llvm::StringRef(ParseInput.Contents).take_front(*Offset);
2041 if (auto OffsetBeforeComment = maybeFunctionArgumentCommentStart(Content)) {
2042 // We are doing code completion of a comment, where we currently only
2043 // support completing param names in function calls. To do this, we
2044 // require information from Sema, but Sema's comment completion stops at
2045 // parsing, so we must move back the position before running it, extract
2046 // information we need and construct completion items ourselves.
2047 auto CommentPrefix = Content.substr(*OffsetBeforeComment + 2).trim();
2048 return codeCompleteComment(FileName, *OffsetBeforeComment, CommentPrefix,
2049 Preamble, ParseInput);
2050 }
2051
2052 auto Flow = CodeCompleteFlow(
2053 FileName, Preamble ? Preamble->Includes : IncludeStructure(),
2054 SpecFuzzyFind, Opts);
2055 return (!Preamble || Opts.RunParser == CodeCompleteOptions::NeverParse)
2056 ? std::move(Flow).runWithoutSema(ParseInput.Contents, *Offset,
2057 *ParseInput.TFS)
2058 : std::move(Flow).run({FileName, *Offset, *Preamble,
2059 /*PreamblePatch=*/
2060 PreamblePatch::createMacroPatch(
2061 FileName, ParseInput, *Preamble),
2062 ParseInput});
2063 }
2064
signatureHelp(PathRef FileName,Position Pos,const PreambleData & Preamble,const ParseInputs & ParseInput,MarkupKind DocumentationFormat)2065 SignatureHelp signatureHelp(PathRef FileName, Position Pos,
2066 const PreambleData &Preamble,
2067 const ParseInputs &ParseInput,
2068 MarkupKind DocumentationFormat) {
2069 auto Offset = positionToOffset(ParseInput.Contents, Pos);
2070 if (!Offset) {
2071 elog("Signature help position was invalid {0}", Offset.takeError());
2072 return SignatureHelp();
2073 }
2074 SignatureHelp Result;
2075 clang::CodeCompleteOptions Options;
2076 Options.IncludeGlobals = false;
2077 Options.IncludeMacros = false;
2078 Options.IncludeCodePatterns = false;
2079 Options.IncludeBriefComments = false;
2080 semaCodeComplete(
2081 std::make_unique<SignatureHelpCollector>(Options, DocumentationFormat,
2082 ParseInput.Index, Result),
2083 Options,
2084 {FileName, *Offset, Preamble,
2085 PreamblePatch::createFullPatch(FileName, ParseInput, Preamble),
2086 ParseInput});
2087 return Result;
2088 }
2089
isIndexedForCodeCompletion(const NamedDecl & ND,ASTContext & ASTCtx)2090 bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) {
2091 auto InTopLevelScope = [](const NamedDecl &ND) {
2092 switch (ND.getDeclContext()->getDeclKind()) {
2093 case Decl::TranslationUnit:
2094 case Decl::Namespace:
2095 case Decl::LinkageSpec:
2096 return true;
2097 default:
2098 break;
2099 };
2100 return false;
2101 };
2102 // We only complete symbol's name, which is the same as the name of the
2103 // *primary* template in case of template specializations.
2104 if (isExplicitTemplateSpecialization(&ND))
2105 return false;
2106
2107 // Category decls are not useful on their own outside the interface or
2108 // implementation blocks. Moreover, sema already provides completion for
2109 // these, even if it requires preamble deserialization. So by excluding them
2110 // from the index, we reduce the noise in all the other completion scopes.
2111 if (llvm::isa<ObjCCategoryDecl>(&ND) || llvm::isa<ObjCCategoryImplDecl>(&ND))
2112 return false;
2113
2114 if (InTopLevelScope(ND))
2115 return true;
2116
2117 if (const auto *EnumDecl = dyn_cast<clang::EnumDecl>(ND.getDeclContext()))
2118 return InTopLevelScope(*EnumDecl) && !EnumDecl->isScoped();
2119
2120 return false;
2121 }
2122
render(const CodeCompleteOptions & Opts) const2123 CompletionItem CodeCompletion::render(const CodeCompleteOptions &Opts) const {
2124 CompletionItem LSP;
2125 const auto *InsertInclude = Includes.empty() ? nullptr : &Includes[0];
2126 LSP.label = ((InsertInclude && InsertInclude->Insertion)
2127 ? Opts.IncludeIndicator.Insert
2128 : Opts.IncludeIndicator.NoInsert) +
2129 (Opts.ShowOrigins ? "[" + llvm::to_string(Origin) + "]" : "") +
2130 RequiredQualifier + Name + Signature;
2131
2132 LSP.kind = Kind;
2133 LSP.detail = BundleSize > 1
2134 ? std::string(llvm::formatv("[{0} overloads]", BundleSize))
2135 : ReturnType;
2136 LSP.deprecated = Deprecated;
2137 // Combine header information and documentation in LSP `documentation` field.
2138 // This is not quite right semantically, but tends to display well in editors.
2139 if (InsertInclude || Documentation) {
2140 markup::Document Doc;
2141 if (InsertInclude)
2142 Doc.addParagraph().appendText("From ").appendCode(InsertInclude->Header);
2143 if (Documentation)
2144 Doc.append(*Documentation);
2145 LSP.documentation = renderDoc(Doc, Opts.DocumentationFormat);
2146 }
2147 LSP.sortText = sortText(Score.Total, FilterText);
2148 LSP.filterText = FilterText;
2149 LSP.textEdit = {CompletionTokenRange, RequiredQualifier + Name};
2150 // Merge continuous additionalTextEdits into main edit. The main motivation
2151 // behind this is to help LSP clients, it seems most of them are confused when
2152 // they are provided with additionalTextEdits that are consecutive to main
2153 // edit.
2154 // Note that we store additional text edits from back to front in a line. That
2155 // is mainly to help LSP clients again, so that changes do not effect each
2156 // other.
2157 for (const auto &FixIt : FixIts) {
2158 if (FixIt.range.end == LSP.textEdit->range.start) {
2159 LSP.textEdit->newText = FixIt.newText + LSP.textEdit->newText;
2160 LSP.textEdit->range.start = FixIt.range.start;
2161 } else {
2162 LSP.additionalTextEdits.push_back(FixIt);
2163 }
2164 }
2165 if (Opts.EnableSnippets)
2166 LSP.textEdit->newText += SnippetSuffix;
2167
2168 // FIXME(kadircet): Do not even fill insertText after making sure textEdit is
2169 // compatible with most of the editors.
2170 LSP.insertText = LSP.textEdit->newText;
2171 // Some clients support snippets but work better with plaintext.
2172 // So if the snippet is trivial, let the client know.
2173 // https://github.com/clangd/clangd/issues/922
2174 LSP.insertTextFormat = (Opts.EnableSnippets && !SnippetSuffix.empty())
2175 ? InsertTextFormat::Snippet
2176 : InsertTextFormat::PlainText;
2177 if (InsertInclude && InsertInclude->Insertion)
2178 LSP.additionalTextEdits.push_back(*InsertInclude->Insertion);
2179
2180 LSP.score = Score.ExcludingName;
2181
2182 return LSP;
2183 }
2184
operator <<(llvm::raw_ostream & OS,const CodeCompletion & C)2185 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const CodeCompletion &C) {
2186 // For now just lean on CompletionItem.
2187 return OS << C.render(CodeCompleteOptions());
2188 }
2189
operator <<(llvm::raw_ostream & OS,const CodeCompleteResult & R)2190 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
2191 const CodeCompleteResult &R) {
2192 OS << "CodeCompleteResult: " << R.Completions.size() << (R.HasMore ? "+" : "")
2193 << " (" << getCompletionKindString(R.Context) << ")"
2194 << " items:\n";
2195 for (const auto &C : R.Completions)
2196 OS << C << "\n";
2197 return OS;
2198 }
2199
2200 // Heuristically detect whether the `Line` is an unterminated include filename.
isIncludeFile(llvm::StringRef Line)2201 bool isIncludeFile(llvm::StringRef Line) {
2202 Line = Line.ltrim();
2203 if (!Line.consume_front("#"))
2204 return false;
2205 Line = Line.ltrim();
2206 if (!(Line.consume_front("include_next") || Line.consume_front("include") ||
2207 Line.consume_front("import")))
2208 return false;
2209 Line = Line.ltrim();
2210 if (Line.consume_front("<"))
2211 return Line.count('>') == 0;
2212 if (Line.consume_front("\""))
2213 return Line.count('"') == 0;
2214 return false;
2215 }
2216
allowImplicitCompletion(llvm::StringRef Content,unsigned Offset)2217 bool allowImplicitCompletion(llvm::StringRef Content, unsigned Offset) {
2218 // Look at last line before completion point only.
2219 Content = Content.take_front(Offset);
2220 auto Pos = Content.rfind('\n');
2221 if (Pos != llvm::StringRef::npos)
2222 Content = Content.substr(Pos + 1);
2223
2224 // Complete after scope operators.
2225 if (Content.endswith(".") || Content.endswith("->") ||
2226 Content.endswith("::") || Content.endswith("/*"))
2227 return true;
2228 // Complete after `#include <` and #include `<foo/`.
2229 if ((Content.endswith("<") || Content.endswith("\"") ||
2230 Content.endswith("/")) &&
2231 isIncludeFile(Content))
2232 return true;
2233
2234 // Complete words. Give non-ascii characters the benefit of the doubt.
2235 return !Content.empty() && (isAsciiIdentifierContinue(Content.back()) ||
2236 !llvm::isASCII(Content.back()));
2237 }
2238
2239 } // namespace clangd
2240 } // namespace clang
2241