Debian Codegraph @

llvm-project/clang-tools-extra/clangd/CodeComplete.cpp

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