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