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