1 //===--- CodeComplete.cpp ---------------------------------------*- C++-*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===---------------------------------------------------------------------===// 9 // 10 // Code completion has several moving parts: 11 // - AST-based completions are provided using the completion hooks in Sema. 12 // - external completions are retrieved from the index (using hints from Sema) 13 // - the two sources overlap, and must be merged and overloads bundled 14 // - results must be scored and ranked (see Quality.h) before rendering 15 // 16 // Signature help works in a similar way as code completion, but it is simpler: 17 // it's purely AST-based, and there are few candidates. 18 // 19 //===---------------------------------------------------------------------===// 20 21 #include "CodeComplete.h" 22 #include "AST.h" 23 #include "CodeCompletionStrings.h" 24 #include "Compiler.h" 25 #include "FuzzyMatch.h" 26 #include "Headers.h" 27 #include "Logger.h" 28 #include "Quality.h" 29 #include "SourceCode.h" 30 #include "Trace.h" 31 #include "URI.h" 32 #include "index/Index.h" 33 #include "clang/ASTMatchers/ASTMatchFinder.h" 34 #include "clang/Basic/LangOptions.h" 35 #include "clang/Format/Format.h" 36 #include "clang/Frontend/CompilerInstance.h" 37 #include "clang/Frontend/FrontendActions.h" 38 #include "clang/Index/USRGeneration.h" 39 #include "clang/Sema/CodeCompleteConsumer.h" 40 #include "clang/Sema/Sema.h" 41 #include "clang/Tooling/Core/Replacement.h" 42 #include "llvm/Support/Format.h" 43 #include <queue> 44 45 // We log detailed candidate here if you run with -debug-only=codecomplete. 46 #define DEBUG_TYPE "codecomplete" 47 48 namespace clang { 49 namespace clangd { 50 namespace { 51 52 CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) { 53 using SK = index::SymbolKind; 54 switch (Kind) { 55 case SK::Unknown: 56 return CompletionItemKind::Missing; 57 case SK::Module: 58 case SK::Namespace: 59 case SK::NamespaceAlias: 60 return CompletionItemKind::Module; 61 case SK::Macro: 62 return CompletionItemKind::Text; 63 case SK::Enum: 64 return CompletionItemKind::Enum; 65 // FIXME(ioeric): use LSP struct instead of class when it is suppoted in the 66 // protocol. 67 case SK::Struct: 68 case SK::Class: 69 case SK::Protocol: 70 case SK::Extension: 71 case SK::Union: 72 return CompletionItemKind::Class; 73 // FIXME(ioeric): figure out whether reference is the right type for aliases. 74 case SK::TypeAlias: 75 case SK::Using: 76 return CompletionItemKind::Reference; 77 case SK::Function: 78 // FIXME(ioeric): this should probably be an operator. This should be fixed 79 // when `Operator` is support type in the protocol. 80 case SK::ConversionFunction: 81 return CompletionItemKind::Function; 82 case SK::Variable: 83 case SK::Parameter: 84 return CompletionItemKind::Variable; 85 case SK::Field: 86 return CompletionItemKind::Field; 87 // FIXME(ioeric): use LSP enum constant when it is supported in the protocol. 88 case SK::EnumConstant: 89 return CompletionItemKind::Value; 90 case SK::InstanceMethod: 91 case SK::ClassMethod: 92 case SK::StaticMethod: 93 case SK::Destructor: 94 return CompletionItemKind::Method; 95 case SK::InstanceProperty: 96 case SK::ClassProperty: 97 case SK::StaticProperty: 98 return CompletionItemKind::Property; 99 case SK::Constructor: 100 return CompletionItemKind::Constructor; 101 } 102 llvm_unreachable("Unhandled clang::index::SymbolKind."); 103 } 104 105 CompletionItemKind 106 toCompletionItemKind(CodeCompletionResult::ResultKind ResKind, 107 const NamedDecl *Decl) { 108 if (Decl) 109 return toCompletionItemKind(index::getSymbolInfo(Decl).Kind); 110 switch (ResKind) { 111 case CodeCompletionResult::RK_Declaration: 112 llvm_unreachable("RK_Declaration without Decl"); 113 case CodeCompletionResult::RK_Keyword: 114 return CompletionItemKind::Keyword; 115 case CodeCompletionResult::RK_Macro: 116 return CompletionItemKind::Text; // unfortunately, there's no 'Macro' 117 // completion items in LSP. 118 case CodeCompletionResult::RK_Pattern: 119 return CompletionItemKind::Snippet; 120 } 121 llvm_unreachable("Unhandled CodeCompletionResult::ResultKind."); 122 } 123 124 /// Get the optional chunk as a string. This function is possibly recursive. 125 /// 126 /// The parameter info for each parameter is appended to the Parameters. 127 std::string 128 getOptionalParameters(const CodeCompletionString &CCS, 129 std::vector<ParameterInformation> &Parameters) { 130 std::string Result; 131 for (const auto &Chunk : CCS) { 132 switch (Chunk.Kind) { 133 case CodeCompletionString::CK_Optional: 134 assert(Chunk.Optional && 135 "Expected the optional code completion string to be non-null."); 136 Result += getOptionalParameters(*Chunk.Optional, Parameters); 137 break; 138 case CodeCompletionString::CK_VerticalSpace: 139 break; 140 case CodeCompletionString::CK_Placeholder: 141 // A string that acts as a placeholder for, e.g., a function call 142 // argument. 143 // Intentional fallthrough here. 144 case CodeCompletionString::CK_CurrentParameter: { 145 // A piece of text that describes the parameter that corresponds to 146 // the code-completion location within a function call, message send, 147 // macro invocation, etc. 148 Result += Chunk.Text; 149 ParameterInformation Info; 150 Info.label = Chunk.Text; 151 Parameters.push_back(std::move(Info)); 152 break; 153 } 154 default: 155 Result += Chunk.Text; 156 break; 157 } 158 } 159 return Result; 160 } 161 162 /// Creates a `HeaderFile` from \p Header which can be either a URI or a literal 163 /// include. 164 static llvm::Expected<HeaderFile> toHeaderFile(StringRef Header, 165 llvm::StringRef HintPath) { 166 if (isLiteralInclude(Header)) 167 return HeaderFile{Header.str(), /*Verbatim=*/true}; 168 auto U = URI::parse(Header); 169 if (!U) 170 return U.takeError(); 171 172 auto IncludePath = URI::includeSpelling(*U); 173 if (!IncludePath) 174 return IncludePath.takeError(); 175 if (!IncludePath->empty()) 176 return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true}; 177 178 auto Resolved = URI::resolve(*U, HintPath); 179 if (!Resolved) 180 return Resolved.takeError(); 181 return HeaderFile{std::move(*Resolved), /*Verbatim=*/false}; 182 } 183 184 /// A code completion result, in clang-native form. 185 /// It may be promoted to a CompletionItem if it's among the top-ranked results. 186 struct CompletionCandidate { 187 llvm::StringRef Name; // Used for filtering and sorting. 188 // We may have a result from Sema, from the index, or both. 189 const CodeCompletionResult *SemaResult = nullptr; 190 const Symbol *IndexResult = nullptr; 191 192 // Returns a token identifying the overload set this is part of. 193 // 0 indicates it's not part of any overload set. 194 size_t overloadSet() const { 195 SmallString<256> Scratch; 196 if (IndexResult) { 197 switch (IndexResult->SymInfo.Kind) { 198 case index::SymbolKind::ClassMethod: 199 case index::SymbolKind::InstanceMethod: 200 case index::SymbolKind::StaticMethod: 201 assert(false && "Don't expect members from index in code completion"); 202 // fall through 203 case index::SymbolKind::Function: 204 // We can't group overloads together that need different #includes. 205 // This could break #include insertion. 206 return hash_combine( 207 (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch), 208 headerToInsertIfNotPresent().getValueOr("")); 209 default: 210 return 0; 211 } 212 } 213 assert(SemaResult); 214 // We need to make sure we're consistent with the IndexResult case! 215 const NamedDecl *D = SemaResult->Declaration; 216 if (!D || !D->isFunctionOrFunctionTemplate()) 217 return 0; 218 { 219 llvm::raw_svector_ostream OS(Scratch); 220 D->printQualifiedName(OS); 221 } 222 return hash_combine(Scratch, headerToInsertIfNotPresent().getValueOr("")); 223 } 224 225 llvm::Optional<llvm::StringRef> headerToInsertIfNotPresent() const { 226 if (!IndexResult || !IndexResult->Detail || 227 IndexResult->Detail->IncludeHeader.empty()) 228 return llvm::None; 229 if (SemaResult && SemaResult->Declaration) { 230 // Avoid inserting new #include if the declaration is found in the current 231 // file e.g. the symbol is forward declared. 232 auto &SM = SemaResult->Declaration->getASTContext().getSourceManager(); 233 for (const Decl *RD : SemaResult->Declaration->redecls()) 234 if (SM.isInMainFile(SM.getExpansionLoc(RD->getLocStart()))) 235 return llvm::None; 236 } 237 return IndexResult->Detail->IncludeHeader; 238 } 239 240 // Builds an LSP completion item. 241 CompletionItem build(StringRef FileName, const CompletionItemScores &Scores, 242 const CodeCompleteOptions &Opts, 243 CodeCompletionString *SemaCCS, 244 const IncludeInserter &Includes, 245 llvm::StringRef SemaDocComment) const { 246 assert(bool(SemaResult) == bool(SemaCCS)); 247 assert(SemaResult || IndexResult); 248 249 CompletionItem I; 250 bool InsertingInclude = false; // Whether a new #include will be added. 251 if (SemaResult) { 252 llvm::StringRef Name(SemaCCS->getTypedText()); 253 std::string Signature, SnippetSuffix, Qualifiers; 254 getSignature(*SemaCCS, &Signature, &SnippetSuffix, &Qualifiers); 255 I.label = (Qualifiers + Name + Signature).str(); 256 I.filterText = Name; 257 I.insertText = (Qualifiers + Name).str(); 258 if (Opts.EnableSnippets) 259 I.insertText += SnippetSuffix; 260 I.documentation = formatDocumentation(*SemaCCS, SemaDocComment); 261 I.detail = getReturnType(*SemaCCS); 262 if (SemaResult->Kind == CodeCompletionResult::RK_Declaration) 263 if (const auto *D = SemaResult->getDeclaration()) 264 if (const auto *ND = llvm::dyn_cast<NamedDecl>(D)) 265 I.SymbolScope = splitQualifiedName(printQualifiedName(*ND)).first; 266 I.kind = toCompletionItemKind(SemaResult->Kind, SemaResult->Declaration); 267 } 268 if (IndexResult) { 269 if (I.SymbolScope.empty()) 270 I.SymbolScope = IndexResult->Scope; 271 if (I.kind == CompletionItemKind::Missing) 272 I.kind = toCompletionItemKind(IndexResult->SymInfo.Kind); 273 // FIXME: reintroduce a way to show the index source for debugging. 274 if (I.label.empty()) 275 I.label = (IndexResult->Name + IndexResult->Signature).str(); 276 if (I.filterText.empty()) 277 I.filterText = IndexResult->Name; 278 279 // FIXME(ioeric): support inserting/replacing scope qualifiers. 280 if (I.insertText.empty()) { 281 I.insertText = IndexResult->Name; 282 if (Opts.EnableSnippets) 283 I.insertText += IndexResult->CompletionSnippetSuffix; 284 } 285 286 if (auto *D = IndexResult->Detail) { 287 if (I.documentation.empty()) 288 I.documentation = D->Documentation; 289 if (I.detail.empty()) 290 I.detail = D->ReturnType; 291 if (auto Inserted = headerToInsertIfNotPresent()) { 292 auto IncludePath = [&]() -> Expected<std::string> { 293 auto ResolvedDeclaring = toHeaderFile( 294 IndexResult->CanonicalDeclaration.FileURI, FileName); 295 if (!ResolvedDeclaring) 296 return ResolvedDeclaring.takeError(); 297 auto ResolvedInserted = toHeaderFile(*Inserted, FileName); 298 if (!ResolvedInserted) 299 return ResolvedInserted.takeError(); 300 if (!Includes.shouldInsertInclude(*ResolvedDeclaring, 301 *ResolvedInserted)) 302 return ""; 303 return Includes.calculateIncludePath(*ResolvedDeclaring, 304 *ResolvedInserted); 305 }(); 306 if (!IncludePath) { 307 std::string ErrMsg = 308 ("Failed to generate include insertion edits for adding header " 309 "(FileURI=\"" + 310 IndexResult->CanonicalDeclaration.FileURI + 311 "\", IncludeHeader=\"" + D->IncludeHeader + "\") into " + 312 FileName) 313 .str(); 314 log(ErrMsg + ":" + llvm::toString(IncludePath.takeError())); 315 } else if (!IncludePath->empty()) { 316 // FIXME: consider what to show when there is no #include insertion, 317 // and for sema results, for consistency. 318 if (auto Edit = Includes.insert(*IncludePath)) { 319 I.detail += ("\n" + StringRef(*IncludePath).trim('"')).str(); 320 I.additionalTextEdits = {std::move(*Edit)}; 321 InsertingInclude = true; 322 } 323 } 324 } 325 } 326 } 327 I.label = (InsertingInclude ? Opts.IncludeIndicator.Insert 328 : Opts.IncludeIndicator.NoInsert) + 329 I.label; 330 I.scoreInfo = Scores; 331 I.sortText = sortText(Scores.finalScore, Name); 332 I.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet 333 : InsertTextFormat::PlainText; 334 return I; 335 } 336 337 using Bundle = llvm::SmallVector<CompletionCandidate, 4>; 338 339 static CompletionItem build(const Bundle &Bundle, CompletionItem First, 340 const clangd::CodeCompleteOptions &Opts) { 341 if (Bundle.size() == 1) 342 return First; 343 // Patch up the completion item to make it look like a bundle. 344 // This is a bit of a hack but most things are the same. 345 346 // Need to erase the signature. All bundles are function calls. 347 llvm::StringRef Name = Bundle.front().Name; 348 First.insertText = 349 Opts.EnableSnippets ? (Name + "(${0})").str() : Name.str(); 350 // Keep the visual indicator of the original label. 351 bool InsertingInclude = 352 StringRef(First.label).startswith(Opts.IncludeIndicator.Insert); 353 First.label = (Twine(InsertingInclude ? Opts.IncludeIndicator.Insert 354 : Opts.IncludeIndicator.NoInsert) + 355 Name + "(…)") 356 .str(); 357 First.detail = llvm::formatv("[{0} overloads]", Bundle.size()); 358 return First; 359 } 360 }; 361 using ScoredBundle = 362 std::pair<CompletionCandidate::Bundle, CompletionItemScores>; 363 struct ScoredBundleGreater { 364 bool operator()(const ScoredBundle &L, const ScoredBundle &R) { 365 if (L.second.finalScore != R.second.finalScore) 366 return L.second.finalScore > R.second.finalScore; 367 return L.first.front().Name < 368 R.first.front().Name; // Earlier name is better. 369 } 370 }; 371 372 // Determine the symbol ID for a Sema code completion result, if possible. 373 llvm::Optional<SymbolID> getSymbolID(const CodeCompletionResult &R) { 374 switch (R.Kind) { 375 case CodeCompletionResult::RK_Declaration: 376 case CodeCompletionResult::RK_Pattern: { 377 llvm::SmallString<128> USR; 378 if (/*Ignore=*/clang::index::generateUSRForDecl(R.Declaration, USR)) 379 return None; 380 return SymbolID(USR); 381 } 382 case CodeCompletionResult::RK_Macro: 383 // FIXME: Macros do have USRs, but the CCR doesn't contain enough info. 384 case CodeCompletionResult::RK_Keyword: 385 return None; 386 } 387 llvm_unreachable("unknown CodeCompletionResult kind"); 388 } 389 390 // Scopes of the paritial identifier we're trying to complete. 391 // It is used when we query the index for more completion results. 392 struct SpecifiedScope { 393 // The scopes we should look in, determined by Sema. 394 // 395 // If the qualifier was fully resolved, we look for completions in these 396 // scopes; if there is an unresolved part of the qualifier, it should be 397 // resolved within these scopes. 398 // 399 // Examples of qualified completion: 400 // 401 // "::vec" => {""} 402 // "using namespace std; ::vec^" => {"", "std::"} 403 // "namespace ns {using namespace std;} ns::^" => {"ns::", "std::"} 404 // "std::vec^" => {""} // "std" unresolved 405 // 406 // Examples of unqualified completion: 407 // 408 // "vec^" => {""} 409 // "using namespace std; vec^" => {"", "std::"} 410 // "using namespace std; namespace ns { vec^ }" => {"ns::", "std::", ""} 411 // 412 // "" for global namespace, "ns::" for normal namespace. 413 std::vector<std::string> AccessibleScopes; 414 // The full scope qualifier as typed by the user (without the leading "::"). 415 // Set if the qualifier is not fully resolved by Sema. 416 llvm::Optional<std::string> UnresolvedQualifier; 417 418 // Construct scopes being queried in indexes. 419 // This method format the scopes to match the index request representation. 420 std::vector<std::string> scopesForIndexQuery() { 421 std::vector<std::string> Results; 422 for (llvm::StringRef AS : AccessibleScopes) { 423 Results.push_back(AS); 424 if (UnresolvedQualifier) 425 Results.back() += *UnresolvedQualifier; 426 } 427 return Results; 428 } 429 }; 430 431 // Get all scopes that will be queried in indexes. 432 std::vector<std::string> getQueryScopes(CodeCompletionContext &CCContext, 433 const SourceManager &SM) { 434 auto GetAllAccessibleScopes = [](CodeCompletionContext &CCContext) { 435 SpecifiedScope Info; 436 for (auto *Context : CCContext.getVisitedContexts()) { 437 if (isa<TranslationUnitDecl>(Context)) 438 Info.AccessibleScopes.push_back(""); // global namespace 439 else if (const auto *NS = dyn_cast<NamespaceDecl>(Context)) 440 Info.AccessibleScopes.push_back(NS->getQualifiedNameAsString() + "::"); 441 } 442 return Info; 443 }; 444 445 auto SS = CCContext.getCXXScopeSpecifier(); 446 447 // Unqualified completion (e.g. "vec^"). 448 if (!SS) { 449 // FIXME: Once we can insert namespace qualifiers and use the in-scope 450 // namespaces for scoring, search in all namespaces. 451 // FIXME: Capture scopes and use for scoring, for example, 452 // "using namespace std; namespace foo {v^}" => 453 // foo::value > std::vector > boost::variant 454 return GetAllAccessibleScopes(CCContext).scopesForIndexQuery(); 455 } 456 457 // Qualified completion ("std::vec^"), we have two cases depending on whether 458 // the qualifier can be resolved by Sema. 459 if ((*SS)->isValid()) { // Resolved qualifier. 460 return GetAllAccessibleScopes(CCContext).scopesForIndexQuery(); 461 } 462 463 // Unresolved qualifier. 464 // FIXME: When Sema can resolve part of a scope chain (e.g. 465 // "known::unknown::id"), we should expand the known part ("known::") rather 466 // than treating the whole thing as unknown. 467 SpecifiedScope Info; 468 Info.AccessibleScopes.push_back(""); // global namespace 469 470 Info.UnresolvedQualifier = 471 Lexer::getSourceText(CharSourceRange::getCharRange((*SS)->getRange()), SM, 472 clang::LangOptions()) 473 .ltrim("::"); 474 // Sema excludes the trailing "::". 475 if (!Info.UnresolvedQualifier->empty()) 476 *Info.UnresolvedQualifier += "::"; 477 478 return Info.scopesForIndexQuery(); 479 } 480 481 // Should we perform index-based completion in a context of the specified kind? 482 // FIXME: consider allowing completion, but restricting the result types. 483 bool contextAllowsIndex(enum CodeCompletionContext::Kind K) { 484 switch (K) { 485 case CodeCompletionContext::CCC_TopLevel: 486 case CodeCompletionContext::CCC_ObjCInterface: 487 case CodeCompletionContext::CCC_ObjCImplementation: 488 case CodeCompletionContext::CCC_ObjCIvarList: 489 case CodeCompletionContext::CCC_ClassStructUnion: 490 case CodeCompletionContext::CCC_Statement: 491 case CodeCompletionContext::CCC_Expression: 492 case CodeCompletionContext::CCC_ObjCMessageReceiver: 493 case CodeCompletionContext::CCC_EnumTag: 494 case CodeCompletionContext::CCC_UnionTag: 495 case CodeCompletionContext::CCC_ClassOrStructTag: 496 case CodeCompletionContext::CCC_ObjCProtocolName: 497 case CodeCompletionContext::CCC_Namespace: 498 case CodeCompletionContext::CCC_Type: 499 case CodeCompletionContext::CCC_Name: // FIXME: why does ns::^ give this? 500 case CodeCompletionContext::CCC_PotentiallyQualifiedName: 501 case CodeCompletionContext::CCC_ParenthesizedExpression: 502 case CodeCompletionContext::CCC_ObjCInterfaceName: 503 case CodeCompletionContext::CCC_ObjCCategoryName: 504 return true; 505 case CodeCompletionContext::CCC_Other: // Be conservative. 506 case CodeCompletionContext::CCC_OtherWithMacros: 507 case CodeCompletionContext::CCC_DotMemberAccess: 508 case CodeCompletionContext::CCC_ArrowMemberAccess: 509 case CodeCompletionContext::CCC_ObjCPropertyAccess: 510 case CodeCompletionContext::CCC_MacroName: 511 case CodeCompletionContext::CCC_MacroNameUse: 512 case CodeCompletionContext::CCC_PreprocessorExpression: 513 case CodeCompletionContext::CCC_PreprocessorDirective: 514 case CodeCompletionContext::CCC_NaturalLanguage: 515 case CodeCompletionContext::CCC_SelectorName: 516 case CodeCompletionContext::CCC_TypeQualifiers: 517 case CodeCompletionContext::CCC_ObjCInstanceMessage: 518 case CodeCompletionContext::CCC_ObjCClassMessage: 519 case CodeCompletionContext::CCC_Recovery: 520 return false; 521 } 522 llvm_unreachable("unknown code completion context"); 523 } 524 525 // Some member calls are blacklisted because they're so rarely useful. 526 static bool isBlacklistedMember(const NamedDecl &D) { 527 // Destructor completion is rarely useful, and works inconsistently. 528 // (s.^ completes ~string, but s.~st^ is an error). 529 if (D.getKind() == Decl::CXXDestructor) 530 return true; 531 // Injected name may be useful for A::foo(), but who writes A::A::foo()? 532 if (auto *R = dyn_cast_or_null<RecordDecl>(&D)) 533 if (R->isInjectedClassName()) 534 return true; 535 // Explicit calls to operators are also rare. 536 auto NameKind = D.getDeclName().getNameKind(); 537 if (NameKind == DeclarationName::CXXOperatorName || 538 NameKind == DeclarationName::CXXLiteralOperatorName || 539 NameKind == DeclarationName::CXXConversionFunctionName) 540 return true; 541 return false; 542 } 543 544 // The CompletionRecorder captures Sema code-complete output, including context. 545 // It filters out ignored results (but doesn't apply fuzzy-filtering yet). 546 // It doesn't do scoring or conversion to CompletionItem yet, as we want to 547 // merge with index results first. 548 // Generally the fields and methods of this object should only be used from 549 // within the callback. 550 struct CompletionRecorder : public CodeCompleteConsumer { 551 CompletionRecorder(const CodeCompleteOptions &Opts, 552 UniqueFunction<void()> ResultsCallback) 553 : CodeCompleteConsumer(Opts.getClangCompleteOpts(), 554 /*OutputIsBinary=*/false), 555 CCContext(CodeCompletionContext::CCC_Other), Opts(Opts), 556 CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()), 557 CCTUInfo(CCAllocator), ResultsCallback(std::move(ResultsCallback)) { 558 assert(this->ResultsCallback); 559 } 560 561 std::vector<CodeCompletionResult> Results; 562 CodeCompletionContext CCContext; 563 Sema *CCSema = nullptr; // Sema that created the results. 564 // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead? 565 566 void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, 567 CodeCompletionResult *InResults, 568 unsigned NumResults) override final { 569 // If a callback is called without any sema result and the context does not 570 // support index-based completion, we simply skip it to give way to 571 // potential future callbacks with results. 572 if (NumResults == 0 && !contextAllowsIndex(Context.getKind())) 573 return; 574 if (CCSema) { 575 log(llvm::formatv( 576 "Multiple code complete callbacks (parser backtracked?). " 577 "Dropping results from context {0}, keeping results from {1}.", 578 getCompletionKindString(Context.getKind()), 579 getCompletionKindString(this->CCContext.getKind()))); 580 return; 581 } 582 // Record the completion context. 583 CCSema = &S; 584 CCContext = Context; 585 586 // Retain the results we might want. 587 for (unsigned I = 0; I < NumResults; ++I) { 588 auto &Result = InResults[I]; 589 // Drop hidden items which cannot be found by lookup after completion. 590 // Exception: some items can be named by using a qualifier. 591 if (Result.Hidden && (!Result.Qualifier || Result.QualifierIsInformative)) 592 continue; 593 if (!Opts.IncludeIneligibleResults && 594 (Result.Availability == CXAvailability_NotAvailable || 595 Result.Availability == CXAvailability_NotAccessible)) 596 continue; 597 if (Result.Declaration && 598 !Context.getBaseType().isNull() // is this a member-access context? 599 && isBlacklistedMember(*Result.Declaration)) 600 continue; 601 // We choose to never append '::' to completion results in clangd. 602 Result.StartsNestedNameSpecifier = false; 603 Results.push_back(Result); 604 } 605 ResultsCallback(); 606 } 607 608 CodeCompletionAllocator &getAllocator() override { return *CCAllocator; } 609 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; } 610 611 // Returns the filtering/sorting name for Result, which must be from Results. 612 // Returned string is owned by this recorder (or the AST). 613 llvm::StringRef getName(const CodeCompletionResult &Result) { 614 switch (Result.Kind) { 615 case CodeCompletionResult::RK_Declaration: 616 if (auto *ID = Result.Declaration->getIdentifier()) 617 return ID->getName(); 618 break; 619 case CodeCompletionResult::RK_Keyword: 620 return Result.Keyword; 621 case CodeCompletionResult::RK_Macro: 622 return Result.Macro->getName(); 623 case CodeCompletionResult::RK_Pattern: 624 return Result.Pattern->getTypedText(); 625 } 626 auto *CCS = codeCompletionString(Result); 627 return CCS->getTypedText(); 628 } 629 630 // Build a CodeCompletion string for R, which must be from Results. 631 // The CCS will be owned by this recorder. 632 CodeCompletionString *codeCompletionString(const CodeCompletionResult &R) { 633 // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway. 634 return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString( 635 *CCSema, CCContext, *CCAllocator, CCTUInfo, 636 /*IncludeBriefComments=*/false); 637 } 638 639 private: 640 CodeCompleteOptions Opts; 641 std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator; 642 CodeCompletionTUInfo CCTUInfo; 643 UniqueFunction<void()> ResultsCallback; 644 }; 645 646 class SignatureHelpCollector final : public CodeCompleteConsumer { 647 648 public: 649 SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts, 650 SignatureHelp &SigHelp) 651 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false), 652 SigHelp(SigHelp), 653 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()), 654 CCTUInfo(Allocator) {} 655 656 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, 657 OverloadCandidate *Candidates, 658 unsigned NumCandidates) override { 659 SigHelp.signatures.reserve(NumCandidates); 660 // FIXME(rwols): How can we determine the "active overload candidate"? 661 // Right now the overloaded candidates seem to be provided in a "best fit" 662 // order, so I'm not too worried about this. 663 SigHelp.activeSignature = 0; 664 assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() && 665 "too many arguments"); 666 SigHelp.activeParameter = static_cast<int>(CurrentArg); 667 for (unsigned I = 0; I < NumCandidates; ++I) { 668 const auto &Candidate = Candidates[I]; 669 const auto *CCS = Candidate.CreateSignatureString( 670 CurrentArg, S, *Allocator, CCTUInfo, true); 671 assert(CCS && "Expected the CodeCompletionString to be non-null"); 672 // FIXME: for headers, we need to get a comment from the index. 673 SigHelp.signatures.push_back(ProcessOverloadCandidate( 674 Candidate, *CCS, 675 getParameterDocComment(S.getASTContext(), Candidate, CurrentArg, 676 /*CommentsFromHeaders=*/false))); 677 } 678 } 679 680 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; } 681 682 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; } 683 684 private: 685 // FIXME(ioeric): consider moving CodeCompletionString logic here to 686 // CompletionString.h. 687 SignatureInformation 688 ProcessOverloadCandidate(const OverloadCandidate &Candidate, 689 const CodeCompletionString &CCS, 690 llvm::StringRef DocComment) const { 691 SignatureInformation Result; 692 const char *ReturnType = nullptr; 693 694 Result.documentation = formatDocumentation(CCS, DocComment); 695 696 for (const auto &Chunk : CCS) { 697 switch (Chunk.Kind) { 698 case CodeCompletionString::CK_ResultType: 699 // A piece of text that describes the type of an entity or, 700 // for functions and methods, the return type. 701 assert(!ReturnType && "Unexpected CK_ResultType"); 702 ReturnType = Chunk.Text; 703 break; 704 case CodeCompletionString::CK_Placeholder: 705 // A string that acts as a placeholder for, e.g., a function call 706 // argument. 707 // Intentional fallthrough here. 708 case CodeCompletionString::CK_CurrentParameter: { 709 // A piece of text that describes the parameter that corresponds to 710 // the code-completion location within a function call, message send, 711 // macro invocation, etc. 712 Result.label += Chunk.Text; 713 ParameterInformation Info; 714 Info.label = Chunk.Text; 715 Result.parameters.push_back(std::move(Info)); 716 break; 717 } 718 case CodeCompletionString::CK_Optional: { 719 // The rest of the parameters are defaulted/optional. 720 assert(Chunk.Optional && 721 "Expected the optional code completion string to be non-null."); 722 Result.label += 723 getOptionalParameters(*Chunk.Optional, Result.parameters); 724 break; 725 } 726 case CodeCompletionString::CK_VerticalSpace: 727 break; 728 default: 729 Result.label += Chunk.Text; 730 break; 731 } 732 } 733 if (ReturnType) { 734 Result.label += " -> "; 735 Result.label += ReturnType; 736 } 737 return Result; 738 } 739 740 SignatureHelp &SigHelp; 741 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator; 742 CodeCompletionTUInfo CCTUInfo; 743 744 }; // SignatureHelpCollector 745 746 struct SemaCompleteInput { 747 PathRef FileName; 748 const tooling::CompileCommand &Command; 749 PrecompiledPreamble const *Preamble; 750 const std::vector<Inclusion> &PreambleInclusions; 751 StringRef Contents; 752 Position Pos; 753 IntrusiveRefCntPtr<vfs::FileSystem> VFS; 754 std::shared_ptr<PCHContainerOperations> PCHs; 755 }; 756 757 // Invokes Sema code completion on a file. 758 // If \p Includes is set, it will be initialized after a compiler instance has 759 // been set up. 760 bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer, 761 const clang::CodeCompleteOptions &Options, 762 const SemaCompleteInput &Input, 763 std::unique_ptr<IncludeInserter> *Includes = nullptr) { 764 trace::Span Tracer("Sema completion"); 765 std::vector<const char *> ArgStrs; 766 for (const auto &S : Input.Command.CommandLine) 767 ArgStrs.push_back(S.c_str()); 768 769 if (Input.VFS->setCurrentWorkingDirectory(Input.Command.Directory)) { 770 log("Couldn't set working directory"); 771 // We run parsing anyway, our lit-tests rely on results for non-existing 772 // working dirs. 773 } 774 775 IgnoreDiagnostics DummyDiagsConsumer; 776 auto CI = createInvocationFromCommandLine( 777 ArgStrs, 778 CompilerInstance::createDiagnostics(new DiagnosticOptions, 779 &DummyDiagsConsumer, false), 780 Input.VFS); 781 if (!CI) { 782 log("Couldn't create CompilerInvocation"); 783 return false; 784 } 785 auto &FrontendOpts = CI->getFrontendOpts(); 786 FrontendOpts.DisableFree = false; 787 FrontendOpts.SkipFunctionBodies = true; 788 CI->getLangOpts()->CommentOpts.ParseAllComments = true; 789 // Disable typo correction in Sema. 790 CI->getLangOpts()->SpellChecking = false; 791 // Setup code completion. 792 FrontendOpts.CodeCompleteOpts = Options; 793 FrontendOpts.CodeCompletionAt.FileName = Input.FileName; 794 auto Offset = positionToOffset(Input.Contents, Input.Pos); 795 if (!Offset) { 796 log("Code completion position was invalid " + 797 llvm::toString(Offset.takeError())); 798 return false; 799 } 800 std::tie(FrontendOpts.CodeCompletionAt.Line, 801 FrontendOpts.CodeCompletionAt.Column) = 802 offsetToClangLineColumn(Input.Contents, *Offset); 803 804 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer = 805 llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName); 806 // The diagnostic options must be set before creating a CompilerInstance. 807 CI->getDiagnosticOpts().IgnoreWarnings = true; 808 // We reuse the preamble whether it's valid or not. This is a 809 // correctness/performance tradeoff: building without a preamble is slow, and 810 // completion is latency-sensitive. 811 // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise 812 // the remapped buffers do not get freed. 813 auto Clang = prepareCompilerInstance( 814 std::move(CI), Input.Preamble, std::move(ContentsBuffer), 815 std::move(Input.PCHs), std::move(Input.VFS), DummyDiagsConsumer); 816 Clang->setCodeCompletionConsumer(Consumer.release()); 817 818 SyntaxOnlyAction Action; 819 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) { 820 log("BeginSourceFile() failed when running codeComplete for " + 821 Input.FileName); 822 return false; 823 } 824 if (Includes) { 825 // Initialize Includes if provided. 826 827 // FIXME(ioeric): needs more consistent style support in clangd server. 828 auto Style = format::getStyle(format::DefaultFormatStyle, Input.FileName, 829 format::DefaultFallbackStyle, Input.Contents, 830 Input.VFS.get()); 831 if (!Style) { 832 log("ERROR: failed to get FormatStyle for file " + Input.FileName + 833 ". Fall back to use LLVM style. Error: " + 834 llvm::toString(Style.takeError())); 835 Style = format::getLLVMStyle(); 836 } 837 *Includes = llvm::make_unique<IncludeInserter>( 838 Input.FileName, Input.Contents, *Style, Input.Command.Directory, 839 Clang->getPreprocessor().getHeaderSearchInfo()); 840 for (const auto &Inc : Input.PreambleInclusions) 841 Includes->get()->addExisting(Inc); 842 Clang->getPreprocessor().addPPCallbacks(collectInclusionsInMainFileCallback( 843 Clang->getSourceManager(), [Includes](Inclusion Inc) { 844 Includes->get()->addExisting(std::move(Inc)); 845 })); 846 } 847 if (!Action.Execute()) { 848 log("Execute() failed when running codeComplete for " + Input.FileName); 849 return false; 850 } 851 Action.EndSourceFile(); 852 853 return true; 854 } 855 856 // Should we allow index completions in the specified context? 857 bool allowIndex(CodeCompletionContext &CC) { 858 if (!contextAllowsIndex(CC.getKind())) 859 return false; 860 // We also avoid ClassName::bar (but allow namespace::bar). 861 auto Scope = CC.getCXXScopeSpecifier(); 862 if (!Scope) 863 return true; 864 NestedNameSpecifier *NameSpec = (*Scope)->getScopeRep(); 865 if (!NameSpec) 866 return true; 867 // We only query the index when qualifier is a namespace. 868 // If it's a class, we rely solely on sema completions. 869 switch (NameSpec->getKind()) { 870 case NestedNameSpecifier::Global: 871 case NestedNameSpecifier::Namespace: 872 case NestedNameSpecifier::NamespaceAlias: 873 return true; 874 case NestedNameSpecifier::Super: 875 case NestedNameSpecifier::TypeSpec: 876 case NestedNameSpecifier::TypeSpecWithTemplate: 877 // Unresolved inside a template. 878 case NestedNameSpecifier::Identifier: 879 return false; 880 } 881 llvm_unreachable("invalid NestedNameSpecifier kind"); 882 } 883 884 } // namespace 885 886 clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const { 887 clang::CodeCompleteOptions Result; 888 Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns; 889 Result.IncludeMacros = IncludeMacros; 890 Result.IncludeGlobals = true; 891 // We choose to include full comments and not do doxygen parsing in 892 // completion. 893 // FIXME: ideally, we should support doxygen in some form, e.g. do markdown 894 // formatting of the comments. 895 Result.IncludeBriefComments = false; 896 897 // When an is used, Sema is responsible for completing the main file, 898 // the index can provide results from the preamble. 899 // Tell Sema not to deserialize the preamble to look for results. 900 Result.LoadExternal = !Index; 901 902 return Result; 903 } 904 905 // Runs Sema-based (AST) and Index-based completion, returns merged results. 906 // 907 // There are a few tricky considerations: 908 // - the AST provides information needed for the index query (e.g. which 909 // namespaces to search in). So Sema must start first. 910 // - we only want to return the top results (Opts.Limit). 911 // Building CompletionItems for everything else is wasteful, so we want to 912 // preserve the "native" format until we're done with scoring. 913 // - the data underlying Sema completion items is owned by the AST and various 914 // other arenas, which must stay alive for us to build CompletionItems. 915 // - we may get duplicate results from Sema and the Index, we need to merge. 916 // 917 // So we start Sema completion first, and do all our work in its callback. 918 // We use the Sema context information to query the index. 919 // Then we merge the two result sets, producing items that are Sema/Index/Both. 920 // These items are scored, and the top N are synthesized into the LSP response. 921 // Finally, we can clean up the data structures created by Sema completion. 922 // 923 // Main collaborators are: 924 // - semaCodeComplete sets up the compiler machinery to run code completion. 925 // - CompletionRecorder captures Sema completion results, including context. 926 // - SymbolIndex (Opts.Index) provides index completion results as Symbols 927 // - CompletionCandidates are the result of merging Sema and Index results. 928 // Each candidate points to an underlying CodeCompletionResult (Sema), a 929 // Symbol (Index), or both. It computes the result quality score. 930 // CompletionCandidate also does conversion to CompletionItem (at the end). 931 // - FuzzyMatcher scores how the candidate matches the partial identifier. 932 // This score is combined with the result quality score for the final score. 933 // - TopN determines the results with the best score. 934 class CodeCompleteFlow { 935 PathRef FileName; 936 const CodeCompleteOptions &Opts; 937 // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup. 938 CompletionRecorder *Recorder = nullptr; 939 int NSema = 0, NIndex = 0, NBoth = 0; // Counters for logging. 940 bool Incomplete = false; // Would more be available with a higher limit? 941 llvm::Optional<FuzzyMatcher> Filter; // Initialized once Sema runs. 942 std::unique_ptr<IncludeInserter> Includes; // Initialized once compiler runs. 943 FileProximityMatcher FileProximityMatch; 944 945 public: 946 // A CodeCompleteFlow object is only useful for calling run() exactly once. 947 CodeCompleteFlow(PathRef FileName, const CodeCompleteOptions &Opts) 948 : FileName(FileName), Opts(Opts), 949 // FIXME: also use path of the main header corresponding to FileName to 950 // calculate the file proximity, which would capture include/ and src/ 951 // project setup where headers and implementations are not in the same 952 // directory. 953 FileProximityMatch(ArrayRef<StringRef>({FileName})) {} 954 955 CompletionList run(const SemaCompleteInput &SemaCCInput) && { 956 trace::Span Tracer("CodeCompleteFlow"); 957 958 // We run Sema code completion first. It builds an AST and calculates: 959 // - completion results based on the AST. 960 // - partial identifier and context. We need these for the index query. 961 CompletionList Output; 962 auto RecorderOwner = llvm::make_unique<CompletionRecorder>(Opts, [&]() { 963 assert(Recorder && "Recorder is not set"); 964 assert(Includes && "Includes is not set"); 965 // If preprocessor was run, inclusions from preprocessor callback should 966 // already be added to Inclusions. 967 Output = runWithSema(); 968 Includes.reset(); // Make sure this doesn't out-live Clang. 969 SPAN_ATTACH(Tracer, "sema_completion_kind", 970 getCompletionKindString(Recorder->CCContext.getKind())); 971 }); 972 973 Recorder = RecorderOwner.get(); 974 semaCodeComplete(std::move(RecorderOwner), Opts.getClangCompleteOpts(), 975 SemaCCInput, &Includes); 976 977 SPAN_ATTACH(Tracer, "sema_results", NSema); 978 SPAN_ATTACH(Tracer, "index_results", NIndex); 979 SPAN_ATTACH(Tracer, "merged_results", NBoth); 980 SPAN_ATTACH(Tracer, "returned_results", Output.items.size()); 981 SPAN_ATTACH(Tracer, "incomplete", Output.isIncomplete); 982 log(llvm::formatv("Code complete: {0} results from Sema, {1} from Index, " 983 "{2} matched, {3} returned{4}.", 984 NSema, NIndex, NBoth, Output.items.size(), 985 Output.isIncomplete ? " (incomplete)" : "")); 986 assert(!Opts.Limit || Output.items.size() <= Opts.Limit); 987 // We don't assert that isIncomplete means we hit a limit. 988 // Indexes may choose to impose their own limits even if we don't have one. 989 return Output; 990 } 991 992 private: 993 // This is called by run() once Sema code completion is done, but before the 994 // Sema data structures are torn down. It does all the real work. 995 CompletionList runWithSema() { 996 Filter = FuzzyMatcher( 997 Recorder->CCSema->getPreprocessor().getCodeCompletionFilter()); 998 // Sema provides the needed context to query the index. 999 // FIXME: in addition to querying for extra/overlapping symbols, we should 1000 // explicitly request symbols corresponding to Sema results. 1001 // We can use their signals even if the index can't suggest them. 1002 // We must copy index results to preserve them, but there are at most Limit. 1003 auto IndexResults = (Opts.Index && allowIndex(Recorder->CCContext)) 1004 ? queryIndex() 1005 : SymbolSlab(); 1006 // Merge Sema and Index results, score them, and pick the winners. 1007 auto Top = mergeResults(Recorder->Results, IndexResults); 1008 // Convert the results to the desired LSP structs. 1009 CompletionList Output; 1010 for (auto &C : Top) 1011 Output.items.push_back(toCompletionItem(C.first, C.second)); 1012 Output.isIncomplete = Incomplete; 1013 return Output; 1014 } 1015 1016 SymbolSlab queryIndex() { 1017 trace::Span Tracer("Query index"); 1018 SPAN_ATTACH(Tracer, "limit", Opts.Limit); 1019 1020 SymbolSlab::Builder ResultsBuilder; 1021 // Build the query. 1022 FuzzyFindRequest Req; 1023 if (Opts.Limit) 1024 Req.MaxCandidateCount = Opts.Limit; 1025 Req.Query = Filter->pattern(); 1026 Req.RestrictForCodeCompletion = true; 1027 Req.Scopes = getQueryScopes(Recorder->CCContext, 1028 Recorder->CCSema->getSourceManager()); 1029 Req.ProximityPaths.push_back(FileName); 1030 log(llvm::formatv("Code complete: fuzzyFind(\"{0}\", scopes=[{1}])", 1031 Req.Query, 1032 llvm::join(Req.Scopes.begin(), Req.Scopes.end(), ","))); 1033 // Run the query against the index. 1034 if (Opts.Index->fuzzyFind( 1035 Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); })) 1036 Incomplete = true; 1037 return std::move(ResultsBuilder).build(); 1038 } 1039 1040 // Merges Sema and Index results where possible, to form CompletionCandidates. 1041 // Groups overloads if desired, to form CompletionCandidate::Bundles. 1042 // The bundles are scored and top results are returned, best to worst. 1043 std::vector<ScoredBundle> 1044 mergeResults(const std::vector<CodeCompletionResult> &SemaResults, 1045 const SymbolSlab &IndexResults) { 1046 trace::Span Tracer("Merge and score results"); 1047 std::vector<CompletionCandidate::Bundle> Bundles; 1048 llvm::DenseMap<size_t, size_t> BundleLookup; 1049 auto AddToBundles = [&](const CodeCompletionResult *SemaResult, 1050 const Symbol *IndexResult) { 1051 CompletionCandidate C; 1052 C.SemaResult = SemaResult; 1053 C.IndexResult = IndexResult; 1054 C.Name = IndexResult ? IndexResult->Name : Recorder->getName(*SemaResult); 1055 if (auto OverloadSet = Opts.BundleOverloads ? C.overloadSet() : 0) { 1056 auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size()); 1057 if (Ret.second) 1058 Bundles.emplace_back(); 1059 Bundles[Ret.first->second].push_back(std::move(C)); 1060 } else { 1061 Bundles.emplace_back(); 1062 Bundles.back().push_back(std::move(C)); 1063 } 1064 }; 1065 llvm::DenseSet<const Symbol *> UsedIndexResults; 1066 auto CorrespondingIndexResult = 1067 [&](const CodeCompletionResult &SemaResult) -> const Symbol * { 1068 if (auto SymID = getSymbolID(SemaResult)) { 1069 auto I = IndexResults.find(*SymID); 1070 if (I != IndexResults.end()) { 1071 UsedIndexResults.insert(&*I); 1072 return &*I; 1073 } 1074 } 1075 return nullptr; 1076 }; 1077 // Emit all Sema results, merging them with Index results if possible. 1078 for (auto &SemaResult : Recorder->Results) 1079 AddToBundles(&SemaResult, CorrespondingIndexResult(SemaResult)); 1080 // Now emit any Index-only results. 1081 for (const auto &IndexResult : IndexResults) { 1082 if (UsedIndexResults.count(&IndexResult)) 1083 continue; 1084 AddToBundles(/*SemaResult=*/nullptr, &IndexResult); 1085 } 1086 // We only keep the best N results at any time, in "native" format. 1087 TopN<ScoredBundle, ScoredBundleGreater> Top( 1088 Opts.Limit == 0 ? std::numeric_limits<size_t>::max() : Opts.Limit); 1089 for (auto &Bundle : Bundles) 1090 addCandidate(Top, std::move(Bundle)); 1091 return std::move(Top).items(); 1092 } 1093 1094 Optional<float> fuzzyScore(const CompletionCandidate &C) { 1095 // Macros can be very spammy, so we only support prefix completion. 1096 // We won't end up with underfull index results, as macros are sema-only. 1097 if (C.SemaResult && C.SemaResult->Kind == CodeCompletionResult::RK_Macro && 1098 !C.Name.startswith_lower(Filter->pattern())) 1099 return None; 1100 return Filter->match(C.Name); 1101 } 1102 1103 // Scores a candidate and adds it to the TopN structure. 1104 void addCandidate(TopN<ScoredBundle, ScoredBundleGreater> &Candidates, 1105 CompletionCandidate::Bundle Bundle) { 1106 SymbolQualitySignals Quality; 1107 SymbolRelevanceSignals Relevance; 1108 Relevance.Query = SymbolRelevanceSignals::CodeComplete; 1109 Relevance.FileProximityMatch = &FileProximityMatch; 1110 auto &First = Bundle.front(); 1111 if (auto FuzzyScore = fuzzyScore(First)) 1112 Relevance.NameMatch = *FuzzyScore; 1113 else 1114 return; 1115 unsigned SemaResult = 0, IndexResult = 0; 1116 for (const auto &Candidate : Bundle) { 1117 if (Candidate.IndexResult) { 1118 Quality.merge(*Candidate.IndexResult); 1119 Relevance.merge(*Candidate.IndexResult); 1120 ++IndexResult; 1121 } 1122 if (Candidate.SemaResult) { 1123 Quality.merge(*Candidate.SemaResult); 1124 Relevance.merge(*Candidate.SemaResult); 1125 ++SemaResult; 1126 } 1127 } 1128 1129 float QualScore = Quality.evaluate(), RelScore = Relevance.evaluate(); 1130 CompletionItemScores Scores; 1131 Scores.finalScore = evaluateSymbolAndRelevance(QualScore, RelScore); 1132 // The purpose of exporting component scores is to allow NameMatch to be 1133 // replaced on the client-side. So we export (NameMatch, final/NameMatch) 1134 // rather than (RelScore, QualScore). 1135 Scores.filterScore = Relevance.NameMatch; 1136 Scores.symbolScore = 1137 Scores.filterScore ? Scores.finalScore / Scores.filterScore : QualScore; 1138 1139 LLVM_DEBUG(llvm::dbgs() << "CodeComplete: " << First.Name << "(" 1140 << IndexResult << " index) " 1141 << "(" << SemaResult << " sema)" 1142 << " = " << Scores.finalScore << "\n" 1143 << Quality << Relevance << "\n"); 1144 1145 NSema += bool(SemaResult); 1146 NIndex += bool(IndexResult); 1147 NBoth += SemaResult && IndexResult; 1148 if (Candidates.push({std::move(Bundle), Scores})) 1149 Incomplete = true; 1150 } 1151 1152 CompletionItem toCompletionItem(const CompletionCandidate::Bundle &Bundle, 1153 const CompletionItemScores &Scores) { 1154 CodeCompletionString *SemaCCS = nullptr; 1155 std::string FrontDocComment; 1156 if (auto *SR = Bundle.front().SemaResult) { 1157 SemaCCS = Recorder->codeCompletionString(*SR); 1158 if (Opts.IncludeComments) { 1159 assert(Recorder->CCSema); 1160 FrontDocComment = getDocComment(Recorder->CCSema->getASTContext(), *SR, 1161 /*CommentsFromHeader=*/false); 1162 } 1163 } 1164 return CompletionCandidate::build( 1165 Bundle, 1166 Bundle.front().build(FileName, Scores, Opts, SemaCCS, *Includes, 1167 FrontDocComment), 1168 Opts); 1169 } 1170 }; 1171 1172 CompletionList codeComplete(PathRef FileName, 1173 const tooling::CompileCommand &Command, 1174 PrecompiledPreamble const *Preamble, 1175 const std::vector<Inclusion> &PreambleInclusions, 1176 StringRef Contents, Position Pos, 1177 IntrusiveRefCntPtr<vfs::FileSystem> VFS, 1178 std::shared_ptr<PCHContainerOperations> PCHs, 1179 CodeCompleteOptions Opts) { 1180 return CodeCompleteFlow(FileName, Opts) 1181 .run({FileName, Command, Preamble, PreambleInclusions, Contents, Pos, VFS, 1182 PCHs}); 1183 } 1184 1185 SignatureHelp signatureHelp(PathRef FileName, 1186 const tooling::CompileCommand &Command, 1187 PrecompiledPreamble const *Preamble, 1188 StringRef Contents, Position Pos, 1189 IntrusiveRefCntPtr<vfs::FileSystem> VFS, 1190 std::shared_ptr<PCHContainerOperations> PCHs) { 1191 SignatureHelp Result; 1192 clang::CodeCompleteOptions Options; 1193 Options.IncludeGlobals = false; 1194 Options.IncludeMacros = false; 1195 Options.IncludeCodePatterns = false; 1196 Options.IncludeBriefComments = false; 1197 std::vector<Inclusion> PreambleInclusions = {}; // Unused for signatureHelp 1198 semaCodeComplete(llvm::make_unique<SignatureHelpCollector>(Options, Result), 1199 Options, 1200 {FileName, Command, Preamble, PreambleInclusions, Contents, 1201 Pos, std::move(VFS), std::move(PCHs)}); 1202 return Result; 1203 } 1204 1205 bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) { 1206 using namespace clang::ast_matchers; 1207 auto InTopLevelScope = hasDeclContext( 1208 anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl())); 1209 return !match(decl(anyOf(InTopLevelScope, 1210 hasDeclContext( 1211 enumDecl(InTopLevelScope, unless(isScoped()))))), 1212 ND, ASTCtx) 1213 .empty(); 1214 } 1215 1216 } // namespace clangd 1217 } // namespace clang 1218