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