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