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