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 // AST-based completions are provided using the completion hooks in Sema. 11 // 12 // Signature help works in a similar way as code completion, but it is simpler 13 // as there are typically fewer candidates. 14 // 15 //===---------------------------------------------------------------------===// 16 17 #include "CodeComplete.h" 18 #include "CodeCompletionStrings.h" 19 #include "Compiler.h" 20 #include "FuzzyMatch.h" 21 #include "Logger.h" 22 #include "index/Index.h" 23 #include "clang/Frontend/CompilerInstance.h" 24 #include "clang/Frontend/FrontendActions.h" 25 #include "clang/Index/USRGeneration.h" 26 #include "clang/Sema/CodeCompleteConsumer.h" 27 #include "clang/Sema/Sema.h" 28 #include "llvm/Support/Format.h" 29 #include <queue> 30 31 namespace clang { 32 namespace clangd { 33 namespace { 34 35 CompletionItemKind toCompletionItemKind(CXCursorKind CursorKind) { 36 switch (CursorKind) { 37 case CXCursor_MacroInstantiation: 38 case CXCursor_MacroDefinition: 39 return CompletionItemKind::Text; 40 case CXCursor_CXXMethod: 41 case CXCursor_Destructor: 42 return CompletionItemKind::Method; 43 case CXCursor_FunctionDecl: 44 case CXCursor_FunctionTemplate: 45 return CompletionItemKind::Function; 46 case CXCursor_Constructor: 47 return CompletionItemKind::Constructor; 48 case CXCursor_FieldDecl: 49 return CompletionItemKind::Field; 50 case CXCursor_VarDecl: 51 case CXCursor_ParmDecl: 52 return CompletionItemKind::Variable; 53 // FIXME(ioeric): use LSP struct instead of class when it is suppoted in the 54 // protocol. 55 case CXCursor_StructDecl: 56 case CXCursor_ClassDecl: 57 case CXCursor_UnionDecl: 58 case CXCursor_ClassTemplate: 59 case CXCursor_ClassTemplatePartialSpecialization: 60 return CompletionItemKind::Class; 61 case CXCursor_Namespace: 62 case CXCursor_NamespaceAlias: 63 case CXCursor_NamespaceRef: 64 return CompletionItemKind::Module; 65 case CXCursor_EnumConstantDecl: 66 return CompletionItemKind::Value; 67 case CXCursor_EnumDecl: 68 return CompletionItemKind::Enum; 69 // FIXME(ioeric): figure out whether reference is the right type for aliases. 70 case CXCursor_TypeAliasDecl: 71 case CXCursor_TypeAliasTemplateDecl: 72 case CXCursor_TypedefDecl: 73 case CXCursor_MemberRef: 74 case CXCursor_TypeRef: 75 return CompletionItemKind::Reference; 76 default: 77 return CompletionItemKind::Missing; 78 } 79 } 80 81 CompletionItemKind 82 toCompletionItemKind(CodeCompletionResult::ResultKind ResKind, 83 CXCursorKind CursorKind) { 84 switch (ResKind) { 85 case CodeCompletionResult::RK_Declaration: 86 return toCompletionItemKind(CursorKind); 87 case CodeCompletionResult::RK_Keyword: 88 return CompletionItemKind::Keyword; 89 case CodeCompletionResult::RK_Macro: 90 return CompletionItemKind::Text; // unfortunately, there's no 'Macro' 91 // completion items in LSP. 92 case CodeCompletionResult::RK_Pattern: 93 return CompletionItemKind::Snippet; 94 } 95 llvm_unreachable("Unhandled CodeCompletionResult::ResultKind."); 96 } 97 98 CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) { 99 using SK = index::SymbolKind; 100 switch (Kind) { 101 case SK::Unknown: 102 return CompletionItemKind::Missing; 103 case SK::Module: 104 case SK::Namespace: 105 case SK::NamespaceAlias: 106 return CompletionItemKind::Module; 107 case SK::Macro: 108 return CompletionItemKind::Text; 109 case SK::Enum: 110 return CompletionItemKind::Enum; 111 // FIXME(ioeric): use LSP struct instead of class when it is suppoted in the 112 // protocol. 113 case SK::Struct: 114 case SK::Class: 115 case SK::Protocol: 116 case SK::Extension: 117 case SK::Union: 118 return CompletionItemKind::Class; 119 // FIXME(ioeric): figure out whether reference is the right type for aliases. 120 case SK::TypeAlias: 121 case SK::Using: 122 return CompletionItemKind::Reference; 123 case SK::Function: 124 // FIXME(ioeric): this should probably be an operator. This should be fixed 125 // when `Operator` is support type in the protocol. 126 case SK::ConversionFunction: 127 return CompletionItemKind::Function; 128 case SK::Variable: 129 case SK::Parameter: 130 return CompletionItemKind::Variable; 131 case SK::Field: 132 return CompletionItemKind::Field; 133 // FIXME(ioeric): use LSP enum constant when it is supported in the protocol. 134 case SK::EnumConstant: 135 return CompletionItemKind::Value; 136 case SK::InstanceMethod: 137 case SK::ClassMethod: 138 case SK::StaticMethod: 139 case SK::Destructor: 140 return CompletionItemKind::Method; 141 case SK::InstanceProperty: 142 case SK::ClassProperty: 143 case SK::StaticProperty: 144 return CompletionItemKind::Property; 145 case SK::Constructor: 146 return CompletionItemKind::Constructor; 147 } 148 llvm_unreachable("Unhandled clang::index::SymbolKind."); 149 } 150 151 /// Get the optional chunk as a string. This function is possibly recursive. 152 /// 153 /// The parameter info for each parameter is appended to the Parameters. 154 std::string 155 getOptionalParameters(const CodeCompletionString &CCS, 156 std::vector<ParameterInformation> &Parameters) { 157 std::string Result; 158 for (const auto &Chunk : CCS) { 159 switch (Chunk.Kind) { 160 case CodeCompletionString::CK_Optional: 161 assert(Chunk.Optional && 162 "Expected the optional code completion string to be non-null."); 163 Result += getOptionalParameters(*Chunk.Optional, Parameters); 164 break; 165 case CodeCompletionString::CK_VerticalSpace: 166 break; 167 case CodeCompletionString::CK_Placeholder: 168 // A string that acts as a placeholder for, e.g., a function call 169 // argument. 170 // Intentional fallthrough here. 171 case CodeCompletionString::CK_CurrentParameter: { 172 // A piece of text that describes the parameter that corresponds to 173 // the code-completion location within a function call, message send, 174 // macro invocation, etc. 175 Result += Chunk.Text; 176 ParameterInformation Info; 177 Info.label = Chunk.Text; 178 Parameters.push_back(std::move(Info)); 179 break; 180 } 181 default: 182 Result += Chunk.Text; 183 break; 184 } 185 } 186 return Result; 187 } 188 189 // Produces an integer that sorts in the same order as F. 190 // That is: a < b <==> encodeFloat(a) < encodeFloat(b). 191 uint32_t encodeFloat(float F) { 192 static_assert(std::numeric_limits<float>::is_iec559, ""); 193 static_assert(sizeof(float) == sizeof(uint32_t), ""); 194 constexpr uint32_t TopBit = ~(~uint32_t{0} >> 1); 195 196 // Get the bits of the float. Endianness is the same as for integers. 197 uint32_t U; 198 memcpy(&U, &F, sizeof(float)); 199 // IEEE 754 floats compare like sign-magnitude integers. 200 if (U & TopBit) // Negative float. 201 return 0 - U; // Map onto the low half of integers, order reversed. 202 return U + TopBit; // Positive floats map onto the high half of integers. 203 } 204 205 // Returns a string that sorts in the same order as (-Score, Name), for LSP. 206 std::string sortText(float Score, llvm::StringRef Name) { 207 // We convert -Score to an integer, and hex-encode for readability. 208 // Example: [0.5, "foo"] -> "41000000foo" 209 std::string S; 210 llvm::raw_string_ostream OS(S); 211 write_hex(OS, encodeFloat(-Score), llvm::HexPrintStyle::Lower, 212 /*Width=*/2 * sizeof(Score)); 213 OS << Name; 214 OS.flush(); 215 return S; 216 } 217 218 /// A code completion result, in clang-native form. 219 /// It may be promoted to a CompletionItem if it's among the top-ranked results. 220 struct CompletionCandidate { 221 llvm::StringRef Name; // Used for filtering and sorting. 222 // We may have a result from Sema, from the index, or both. 223 const CodeCompletionResult *SemaResult = nullptr; 224 const Symbol *IndexResult = nullptr; 225 226 // Computes the "symbol quality" score for this completion. Higher is better. 227 float score() const { 228 // For now we just use the Sema priority, mapping it onto a 0-1 interval. 229 if (!SemaResult) // FIXME(sammccall): better scoring for index results. 230 return 0.3; // fixed mediocre score for index-only results. 231 232 // Priority 80 is a really bad score. 233 float Score = 1 - std::min<float>(80, SemaResult->Priority) / 80; 234 235 switch (static_cast<CXAvailabilityKind>(SemaResult->Availability)) { 236 case CXAvailability_Available: 237 // No penalty. 238 break; 239 case CXAvailability_Deprecated: 240 Score *= 0.1f; 241 break; 242 case CXAvailability_NotAccessible: 243 case CXAvailability_NotAvailable: 244 Score = 0; 245 break; 246 } 247 return Score; 248 } 249 250 // Builds an LSP completion item. 251 CompletionItem build(const CompletionItemScores &Scores, 252 const CodeCompleteOptions &Opts, 253 CodeCompletionString *SemaCCS) const { 254 assert(bool(SemaResult) == bool(SemaCCS)); 255 CompletionItem I; 256 if (SemaResult) { 257 I.kind = toCompletionItemKind(SemaResult->Kind, SemaResult->CursorKind); 258 getLabelAndInsertText(*SemaCCS, &I.label, &I.insertText, 259 Opts.EnableSnippets); 260 I.filterText = getFilterText(*SemaCCS); 261 I.documentation = getDocumentation(*SemaCCS); 262 I.detail = getDetail(*SemaCCS); 263 } 264 if (IndexResult) { 265 if (I.kind == CompletionItemKind::Missing) 266 I.kind = toCompletionItemKind(IndexResult->SymInfo.Kind); 267 // FIXME: reintroduce a way to show the index source for debugging. 268 if (I.label.empty()) 269 I.label = IndexResult->CompletionLabel; 270 if (I.filterText.empty()) 271 I.filterText = IndexResult->Name; 272 273 // FIXME(ioeric): support inserting/replacing scope qualifiers. 274 if (I.insertText.empty()) 275 I.insertText = Opts.EnableSnippets 276 ? IndexResult->CompletionSnippetInsertText 277 : IndexResult->CompletionPlainInsertText; 278 279 if (auto *D = IndexResult->Detail) { 280 if (I.documentation.empty()) 281 I.documentation = D->Documentation; 282 if (I.detail.empty()) 283 I.detail = D->CompletionDetail; 284 } 285 } 286 I.scoreInfo = Scores; 287 I.sortText = sortText(Scores.finalScore, Name); 288 I.insertTextFormat = Opts.EnableSnippets ? InsertTextFormat::Snippet 289 : InsertTextFormat::PlainText; 290 return I; 291 } 292 }; 293 294 // Determine the symbol ID for a Sema code completion result, if possible. 295 llvm::Optional<SymbolID> getSymbolID(const CodeCompletionResult &R) { 296 switch (R.Kind) { 297 case CodeCompletionResult::RK_Declaration: 298 case CodeCompletionResult::RK_Pattern: { 299 llvm::SmallString<128> USR; 300 if (/*Ignore=*/clang::index::generateUSRForDecl(R.Declaration, USR)) 301 return None; 302 return SymbolID(USR); 303 } 304 case CodeCompletionResult::RK_Macro: 305 // FIXME: Macros do have USRs, but the CCR doesn't contain enough info. 306 case CodeCompletionResult::RK_Keyword: 307 return None; 308 } 309 llvm_unreachable("unknown CodeCompletionResult kind"); 310 } 311 312 /// \brief Information about the scope specifier in the qualified-id code 313 /// completion (e.g. "ns::ab?"). 314 struct SpecifiedScope { 315 /// The scope specifier as written. For example, for completion "ns::ab?", the 316 /// written scope specifier is "ns::". 317 std::string Written; 318 // If this scope specifier is recognized in Sema (e.g. as a namespace 319 // context), this will be set to the fully qualfied name of the corresponding 320 // context. 321 std::string Resolved; 322 323 llvm::StringRef forIndex() { 324 return Resolved.empty() ? StringRef(Written).ltrim("::") 325 : StringRef(Resolved); 326 } 327 }; 328 329 // The CompletionRecorder captures Sema code-complete output, including context. 330 // It filters out ignored results (but doesn't apply fuzzy-filtering yet). 331 // It doesn't do scoring or conversion to CompletionItem yet, as we want to 332 // merge with index results first. 333 struct CompletionRecorder : public CodeCompleteConsumer { 334 CompletionRecorder(const CodeCompleteOptions &Opts) 335 : CodeCompleteConsumer(Opts.getClangCompleteOpts(), 336 /*OutputIsBinary=*/false), 337 CCContext(CodeCompletionContext::CCC_Other), Opts(Opts), 338 CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()), 339 CCTUInfo(CCAllocator) {} 340 std::vector<CodeCompletionResult> Results; 341 CodeCompletionContext CCContext; 342 Sema *CCSema = nullptr; // Sema that created the results. 343 // FIXME: Sema is scary. Can we store ASTContext and Preprocessor, instead? 344 345 void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, 346 CodeCompletionResult *InResults, 347 unsigned NumResults) override final { 348 // Record the completion context. 349 assert(!CCSema && "ProcessCodeCompleteResults called multiple times!"); 350 CCSema = &S; 351 CCContext = Context; 352 353 // Retain the results we might want. 354 for (unsigned I = 0; I < NumResults; ++I) { 355 auto &Result = InResults[I]; 356 // Drop hidden items which cannot be found by lookup after completion. 357 // Exception: some items can be named by using a qualifier. 358 if (Result.Hidden && (!Result.Qualifier || Result.QualifierIsInformative)) 359 continue; 360 if (!Opts.IncludeIneligibleResults && 361 (Result.Availability == CXAvailability_NotAvailable || 362 Result.Availability == CXAvailability_NotAccessible)) 363 continue; 364 Results.push_back(Result); 365 } 366 } 367 368 CodeCompletionAllocator &getAllocator() override { return *CCAllocator; } 369 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; } 370 371 // Returns the filtering/sorting name for Result, which must be from Results. 372 // Returned string is owned by this recorder (or the AST). 373 llvm::StringRef getName(const CodeCompletionResult &Result) { 374 switch (Result.Kind) { 375 case CodeCompletionResult::RK_Declaration: 376 if (auto *ID = Result.Declaration->getIdentifier()) 377 return ID->getName(); 378 break; 379 case CodeCompletionResult::RK_Keyword: 380 return Result.Keyword; 381 case CodeCompletionResult::RK_Macro: 382 return Result.Macro->getName(); 383 case CodeCompletionResult::RK_Pattern: 384 return Result.Pattern->getTypedText(); 385 } 386 auto *CCS = codeCompletionString(Result, /*IncludeBriefComments=*/false); 387 return CCS->getTypedText(); 388 } 389 390 // Build a CodeCompletion string for R, which must be from Results. 391 // The CCS will be owned by this recorder. 392 CodeCompletionString *codeCompletionString(const CodeCompletionResult &R, 393 bool IncludeBriefComments) { 394 // CodeCompletionResult doesn't seem to be const-correct. We own it, anyway. 395 return const_cast<CodeCompletionResult &>(R).CreateCodeCompletionString( 396 *CCSema, CCContext, *CCAllocator, CCTUInfo, IncludeBriefComments); 397 } 398 399 private: 400 CodeCompleteOptions Opts; 401 std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator; 402 CodeCompletionTUInfo CCTUInfo; 403 }; 404 405 // Tracks a bounded number of candidates with the best scores. 406 class TopN { 407 public: 408 using value_type = std::pair<CompletionCandidate, CompletionItemScores>; 409 static constexpr size_t Unbounded = std::numeric_limits<size_t>::max(); 410 411 TopN(size_t N) : N(N) {} 412 413 // Adds a candidate to the set. 414 // Returns true if a candidate was dropped to get back under N. 415 bool push(value_type &&V) { 416 bool Dropped = false; 417 if (Heap.size() >= N) { 418 Dropped = true; 419 if (N > 0 && greater(V, Heap.front())) { 420 std::pop_heap(Heap.begin(), Heap.end(), greater); 421 Heap.back() = std::move(V); 422 std::push_heap(Heap.begin(), Heap.end(), greater); 423 } 424 } else { 425 Heap.push_back(std::move(V)); 426 std::push_heap(Heap.begin(), Heap.end(), greater); 427 } 428 assert(Heap.size() <= N); 429 assert(std::is_heap(Heap.begin(), Heap.end(), greater)); 430 return Dropped; 431 } 432 433 // Returns candidates from best to worst. 434 std::vector<value_type> items() && { 435 std::sort_heap(Heap.begin(), Heap.end(), greater); 436 assert(Heap.size() <= N); 437 return std::move(Heap); 438 } 439 440 private: 441 static bool greater(const value_type &L, const value_type &R) { 442 if (L.second.finalScore != R.second.finalScore) 443 return L.second.finalScore > R.second.finalScore; 444 return L.first.Name < R.first.Name; // Earlier name is better. 445 } 446 447 const size_t N; 448 std::vector<value_type> Heap; // Min-heap, comparator is greater(). 449 }; 450 451 class SignatureHelpCollector final : public CodeCompleteConsumer { 452 453 public: 454 SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts, 455 SignatureHelp &SigHelp) 456 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false), 457 SigHelp(SigHelp), 458 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()), 459 CCTUInfo(Allocator) {} 460 461 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, 462 OverloadCandidate *Candidates, 463 unsigned NumCandidates) override { 464 SigHelp.signatures.reserve(NumCandidates); 465 // FIXME(rwols): How can we determine the "active overload candidate"? 466 // Right now the overloaded candidates seem to be provided in a "best fit" 467 // order, so I'm not too worried about this. 468 SigHelp.activeSignature = 0; 469 assert(CurrentArg <= (unsigned)std::numeric_limits<int>::max() && 470 "too many arguments"); 471 SigHelp.activeParameter = static_cast<int>(CurrentArg); 472 for (unsigned I = 0; I < NumCandidates; ++I) { 473 const auto &Candidate = Candidates[I]; 474 const auto *CCS = Candidate.CreateSignatureString( 475 CurrentArg, S, *Allocator, CCTUInfo, true); 476 assert(CCS && "Expected the CodeCompletionString to be non-null"); 477 SigHelp.signatures.push_back(ProcessOverloadCandidate(Candidate, *CCS)); 478 } 479 } 480 481 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; } 482 483 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; } 484 485 private: 486 // FIXME(ioeric): consider moving CodeCompletionString logic here to 487 // CompletionString.h. 488 SignatureInformation 489 ProcessOverloadCandidate(const OverloadCandidate &Candidate, 490 const CodeCompletionString &CCS) const { 491 SignatureInformation Result; 492 const char *ReturnType = nullptr; 493 494 Result.documentation = getDocumentation(CCS); 495 496 for (const auto &Chunk : CCS) { 497 switch (Chunk.Kind) { 498 case CodeCompletionString::CK_ResultType: 499 // A piece of text that describes the type of an entity or, 500 // for functions and methods, the return type. 501 assert(!ReturnType && "Unexpected CK_ResultType"); 502 ReturnType = Chunk.Text; 503 break; 504 case CodeCompletionString::CK_Placeholder: 505 // A string that acts as a placeholder for, e.g., a function call 506 // argument. 507 // Intentional fallthrough here. 508 case CodeCompletionString::CK_CurrentParameter: { 509 // A piece of text that describes the parameter that corresponds to 510 // the code-completion location within a function call, message send, 511 // macro invocation, etc. 512 Result.label += Chunk.Text; 513 ParameterInformation Info; 514 Info.label = Chunk.Text; 515 Result.parameters.push_back(std::move(Info)); 516 break; 517 } 518 case CodeCompletionString::CK_Optional: { 519 // The rest of the parameters are defaulted/optional. 520 assert(Chunk.Optional && 521 "Expected the optional code completion string to be non-null."); 522 Result.label += 523 getOptionalParameters(*Chunk.Optional, Result.parameters); 524 break; 525 } 526 case CodeCompletionString::CK_VerticalSpace: 527 break; 528 default: 529 Result.label += Chunk.Text; 530 break; 531 } 532 } 533 if (ReturnType) { 534 Result.label += " -> "; 535 Result.label += ReturnType; 536 } 537 return Result; 538 } 539 540 SignatureHelp &SigHelp; 541 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator; 542 CodeCompletionTUInfo CCTUInfo; 543 544 }; // SignatureHelpCollector 545 546 struct SemaCompleteInput { 547 PathRef FileName; 548 const tooling::CompileCommand &Command; 549 PrecompiledPreamble const *Preamble; 550 StringRef Contents; 551 Position Pos; 552 IntrusiveRefCntPtr<vfs::FileSystem> VFS; 553 std::shared_ptr<PCHContainerOperations> PCHs; 554 }; 555 556 // Invokes Sema code completion on a file. 557 // Callback will be invoked once completion is done, but before cleaning up. 558 bool semaCodeComplete(const Context &Ctx, 559 std::unique_ptr<CodeCompleteConsumer> Consumer, 560 const clang::CodeCompleteOptions &Options, 561 const SemaCompleteInput &Input, 562 llvm::function_ref<void()> Callback = nullptr) { 563 std::vector<const char *> ArgStrs; 564 for (const auto &S : Input.Command.CommandLine) 565 ArgStrs.push_back(S.c_str()); 566 567 Input.VFS->setCurrentWorkingDirectory(Input.Command.Directory); 568 569 IgnoreDiagnostics DummyDiagsConsumer; 570 auto CI = createInvocationFromCommandLine( 571 ArgStrs, 572 CompilerInstance::createDiagnostics(new DiagnosticOptions, 573 &DummyDiagsConsumer, false), 574 Input.VFS); 575 assert(CI && "Couldn't create CompilerInvocation"); 576 CI->getFrontendOpts().DisableFree = false; 577 578 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer = 579 llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName); 580 581 // We reuse the preamble whether it's valid or not. This is a 582 // correctness/performance tradeoff: building without a preamble is slow, and 583 // completion is latency-sensitive. 584 if (Input.Preamble) { 585 auto Bounds = 586 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0); 587 // FIXME(ibiryukov): Remove this call to CanReuse() after we'll fix 588 // clients relying on getting stats for preamble files during code 589 // completion. 590 // Note that results of CanReuse() are ignored, see the comment above. 591 Input.Preamble->CanReuse(*CI, ContentsBuffer.get(), Bounds, 592 Input.VFS.get()); 593 } 594 auto Clang = prepareCompilerInstance( 595 std::move(CI), Input.Preamble, std::move(ContentsBuffer), 596 std::move(Input.PCHs), std::move(Input.VFS), DummyDiagsConsumer); 597 auto &DiagOpts = Clang->getDiagnosticOpts(); 598 DiagOpts.IgnoreWarnings = true; 599 600 auto &FrontendOpts = Clang->getFrontendOpts(); 601 FrontendOpts.SkipFunctionBodies = true; 602 FrontendOpts.CodeCompleteOpts = Options; 603 FrontendOpts.CodeCompletionAt.FileName = Input.FileName; 604 FrontendOpts.CodeCompletionAt.Line = Input.Pos.line + 1; 605 FrontendOpts.CodeCompletionAt.Column = Input.Pos.character + 1; 606 607 Clang->setCodeCompletionConsumer(Consumer.release()); 608 609 SyntaxOnlyAction Action; 610 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) { 611 log(Ctx, "BeginSourceFile() failed when running codeComplete for " + 612 Input.FileName); 613 return false; 614 } 615 if (!Action.Execute()) { 616 log(Ctx, 617 "Execute() failed when running codeComplete for " + Input.FileName); 618 return false; 619 } 620 621 if (Callback) 622 Callback(); 623 Action.EndSourceFile(); 624 625 return true; 626 } 627 628 SpecifiedScope getSpecifiedScope(Sema &S, const CXXScopeSpec &SS) { 629 SpecifiedScope Info; 630 auto &SM = S.getSourceManager(); 631 auto SpecifierRange = SS.getRange(); 632 Info.Written = Lexer::getSourceText( 633 CharSourceRange::getCharRange(SpecifierRange), SM, clang::LangOptions()); 634 if (!Info.Written.empty()) 635 Info.Written += "::"; // Sema excludes the trailing ::. 636 if (SS.isValid()) { 637 DeclContext *DC = S.computeDeclContext(SS); 638 if (auto *NS = llvm::dyn_cast<NamespaceDecl>(DC)) { 639 Info.Resolved = NS->getQualifiedNameAsString() + "::"; 640 } else if (llvm::dyn_cast<TranslationUnitDecl>(DC) != nullptr) { 641 Info.Resolved = ""; 642 } 643 } 644 return Info; 645 } 646 647 // Should we perform index-based completion in this context? 648 // FIXME: consider allowing completion, but restricting the result types. 649 bool allowIndex(enum CodeCompletionContext::Kind K) { 650 switch (K) { 651 case CodeCompletionContext::CCC_TopLevel: 652 case CodeCompletionContext::CCC_ObjCInterface: 653 case CodeCompletionContext::CCC_ObjCImplementation: 654 case CodeCompletionContext::CCC_ObjCIvarList: 655 case CodeCompletionContext::CCC_ClassStructUnion: 656 case CodeCompletionContext::CCC_Statement: 657 case CodeCompletionContext::CCC_Expression: 658 case CodeCompletionContext::CCC_ObjCMessageReceiver: 659 case CodeCompletionContext::CCC_EnumTag: 660 case CodeCompletionContext::CCC_UnionTag: 661 case CodeCompletionContext::CCC_ClassOrStructTag: 662 case CodeCompletionContext::CCC_ObjCProtocolName: 663 case CodeCompletionContext::CCC_Namespace: 664 case CodeCompletionContext::CCC_Type: 665 case CodeCompletionContext::CCC_Name: // FIXME: why does ns::^ give this? 666 case CodeCompletionContext::CCC_PotentiallyQualifiedName: 667 case CodeCompletionContext::CCC_ParenthesizedExpression: 668 case CodeCompletionContext::CCC_ObjCInterfaceName: 669 case CodeCompletionContext::CCC_ObjCCategoryName: 670 return true; 671 case CodeCompletionContext::CCC_Other: // Be conservative. 672 case CodeCompletionContext::CCC_OtherWithMacros: 673 case CodeCompletionContext::CCC_DotMemberAccess: 674 case CodeCompletionContext::CCC_ArrowMemberAccess: 675 case CodeCompletionContext::CCC_ObjCPropertyAccess: 676 case CodeCompletionContext::CCC_MacroName: 677 case CodeCompletionContext::CCC_MacroNameUse: 678 case CodeCompletionContext::CCC_PreprocessorExpression: 679 case CodeCompletionContext::CCC_PreprocessorDirective: 680 case CodeCompletionContext::CCC_NaturalLanguage: 681 case CodeCompletionContext::CCC_SelectorName: 682 case CodeCompletionContext::CCC_TypeQualifiers: 683 case CodeCompletionContext::CCC_ObjCInstanceMessage: 684 case CodeCompletionContext::CCC_ObjCClassMessage: 685 case CodeCompletionContext::CCC_Recovery: 686 return false; 687 } 688 llvm_unreachable("unknown code completion context"); 689 } 690 691 } // namespace 692 693 clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const { 694 clang::CodeCompleteOptions Result; 695 Result.IncludeCodePatterns = EnableSnippets && IncludeCodePatterns; 696 Result.IncludeMacros = IncludeMacros; 697 Result.IncludeGlobals = true; 698 Result.IncludeBriefComments = IncludeBriefComments; 699 700 // When an is used, Sema is responsible for completing the main file, 701 // the index can provide results from the preamble. 702 // Tell Sema not to deserialize the preamble to look for results. 703 Result.LoadExternal = !Index; 704 705 return Result; 706 } 707 708 // Runs Sema-based (AST) and Index-based completion, returns merged results. 709 // 710 // There are a few tricky considerations: 711 // - the AST provides information needed for the index query (e.g. which 712 // namespaces to search in). So Sema must start first. 713 // - we only want to return the top results (Opts.Limit). 714 // Building CompletionItems for everything else is wasteful, so we want to 715 // preserve the "native" format until we're done with scoring. 716 // - the data underlying Sema completion items is owned by the AST and various 717 // other arenas, which must stay alive for us to build CompletionItems. 718 // - we may get duplicate results from Sema and the Index, we need to merge. 719 // 720 // So we start Sema completion first, but defer its cleanup until we're done. 721 // We use the Sema context information to query the index. 722 // Then we merge the two result sets, producing items that are Sema/Index/Both. 723 // These items are scored, and the top N are synthesized into the LSP response. 724 // Finally, we can clean up the data structures created by Sema completion. 725 // 726 // Main collaborators are: 727 // - semaCodeComplete sets up the compiler machinery to run code completion. 728 // - CompletionRecorder captures Sema completion results, including context. 729 // - SymbolIndex (Opts.Index) provides index completion results as Symbols 730 // - CompletionCandidates are the result of merging Sema and Index results. 731 // Each candidate points to an underlying CodeCompletionResult (Sema), a 732 // Symbol (Index), or both. It computes the result quality score. 733 // CompletionCandidate also does conversion to CompletionItem (at the end). 734 // - FuzzyMatcher scores how the candidate matches the partial identifier. 735 // This score is combined with the result quality score for the final score. 736 // - TopN determines the results with the best score. 737 class CodeCompleteFlow { 738 const Context &Ctx; 739 const CodeCompleteOptions &Opts; 740 // Sema takes ownership of Recorder. Recorder is valid until Sema cleanup. 741 std::unique_ptr<CompletionRecorder> RecorderOwner; 742 CompletionRecorder &Recorder; 743 int NSema = 0, NIndex = 0, NBoth = 0; // Counters for logging. 744 bool Incomplete = false; // Would more be available with a higher limit? 745 llvm::Optional<FuzzyMatcher> Filter; // Initialized once Sema runs. 746 747 public: 748 // A CodeCompleteFlow object is only useful for calling run() exactly once. 749 CodeCompleteFlow(const Context &Ctx, const CodeCompleteOptions &Opts) 750 : Ctx(Ctx), Opts(Opts), RecorderOwner(new CompletionRecorder(Opts)), 751 Recorder(*RecorderOwner) {} 752 753 CompletionList run(const SemaCompleteInput &SemaCCInput) && { 754 // We run Sema code completion first. It builds an AST and calculates: 755 // - completion results based on the AST. These are saved for merging. 756 // - partial identifier and context. We need these for the index query. 757 CompletionList Output; 758 semaCodeComplete(Ctx, std::move(RecorderOwner), Opts.getClangCompleteOpts(), 759 SemaCCInput, [&] { 760 if (Recorder.CCSema) 761 Output = runWithSema(); 762 else 763 log(Ctx, "Code complete: no Sema callback, 0 results"); 764 }); 765 766 log(Ctx, 767 llvm::formatv("Code complete: {0} results from Sema, {1} from Index, " 768 "{2} matched, {3} returned{4}.", 769 NSema, NIndex, NBoth, Output.items.size(), 770 Output.isIncomplete ? " (incomplete)" : "")); 771 assert(!Opts.Limit || Output.items.size() <= Opts.Limit); 772 // We don't assert that isIncomplete means we hit a limit. 773 // Indexes may choose to impose their own limits even if we don't have one. 774 return Output; 775 } 776 777 private: 778 // This is called by run() once Sema code completion is done, but before the 779 // Sema data structures are torn down. It does all the real work. 780 CompletionList runWithSema() { 781 Filter = FuzzyMatcher( 782 Recorder.CCSema->getPreprocessor().getCodeCompletionFilter()); 783 // Sema provides the needed context to query the index. 784 // FIXME: in addition to querying for extra/overlapping symbols, we should 785 // explicitly request symbols corresponding to Sema results. 786 // We can use their signals even if the index can't suggest them. 787 // We must copy index results to preserve them, but there are at most Limit. 788 auto IndexResults = queryIndex(); 789 // Merge Sema and Index results, score them, and pick the winners. 790 auto Top = mergeResults(Recorder.Results, IndexResults); 791 // Convert the results to the desired LSP structs. 792 CompletionList Output; 793 for (auto &C : Top) 794 Output.items.push_back(toCompletionItem(C.first, C.second)); 795 Output.isIncomplete = Incomplete; 796 return Output; 797 } 798 799 SymbolSlab queryIndex() { 800 if (!Opts.Index || !allowIndex(Recorder.CCContext.getKind())) 801 return SymbolSlab(); 802 SymbolSlab::Builder ResultsBuilder; 803 // Build the query. 804 FuzzyFindRequest Req; 805 Req.Query = Filter->pattern(); 806 // If the user typed a scope, e.g. a::b::xxx(), restrict to that scope. 807 // FIXME(ioeric): add scopes based on using directives and enclosing ns. 808 if (auto SS = Recorder.CCContext.getCXXScopeSpecifier()) 809 Req.Scopes = {getSpecifiedScope(*Recorder.CCSema, **SS).forIndex()}; 810 else 811 // Unless the user typed a ns qualifier, complete in global scope only. 812 // FIXME: once we know what namespaces are in scope (D42073), use those. 813 // FIXME: once we can insert namespace qualifiers and use the in-scope 814 // namespaces for scoring, search in all namespaces. 815 Req.Scopes = {""}; 816 // Run the query against the index. 817 Incomplete |= !Opts.Index->fuzzyFind( 818 Ctx, Req, [&](const Symbol &Sym) { ResultsBuilder.insert(Sym); }); 819 return std::move(ResultsBuilder).build(); 820 } 821 822 // Merges the Sema and Index results where possible, scores them, and 823 // returns the top results from best to worst. 824 std::vector<std::pair<CompletionCandidate, CompletionItemScores>> 825 mergeResults(const std::vector<CodeCompletionResult> &SemaResults, 826 const SymbolSlab &IndexResults) { 827 // We only keep the best N results at any time, in "native" format. 828 TopN Top(Opts.Limit == 0 ? TopN::Unbounded : Opts.Limit); 829 llvm::DenseSet<const Symbol *> UsedIndexResults; 830 auto CorrespondingIndexResult = 831 [&](const CodeCompletionResult &SemaResult) -> const Symbol * { 832 if (auto SymID = getSymbolID(SemaResult)) { 833 auto I = IndexResults.find(*SymID); 834 if (I != IndexResults.end()) { 835 UsedIndexResults.insert(&*I); 836 return &*I; 837 } 838 } 839 return nullptr; 840 }; 841 // Emit all Sema results, merging them with Index results if possible. 842 for (auto &SemaResult : Recorder.Results) 843 addCandidate(Top, &SemaResult, CorrespondingIndexResult(SemaResult)); 844 // Now emit any Index-only results. 845 for (const auto &IndexResult : IndexResults) { 846 if (UsedIndexResults.count(&IndexResult)) 847 continue; 848 addCandidate(Top, /*SemaResult=*/nullptr, &IndexResult); 849 } 850 return std::move(Top).items(); 851 } 852 853 // Scores a candidate and adds it to the TopN structure. 854 void addCandidate(TopN &Candidates, const CodeCompletionResult *SemaResult, 855 const Symbol *IndexResult) { 856 CompletionCandidate C; 857 C.SemaResult = SemaResult; 858 C.IndexResult = IndexResult; 859 C.Name = IndexResult ? IndexResult->Name : Recorder.getName(*SemaResult); 860 861 CompletionItemScores Scores; 862 if (auto FuzzyScore = Filter->match(C.Name)) 863 Scores.filterScore = *FuzzyScore; 864 else 865 return; 866 Scores.symbolScore = C.score(); 867 // We score candidates by multiplying symbolScore ("quality" of the result) 868 // with filterScore (how well it matched the query). 869 // This is sensitive to the distribution of both component scores! 870 Scores.finalScore = Scores.filterScore * Scores.symbolScore; 871 872 NSema += bool(SemaResult); 873 NIndex += bool(IndexResult); 874 NBoth += SemaResult && IndexResult; 875 Incomplete |= Candidates.push({C, Scores}); 876 } 877 878 CompletionItem toCompletionItem(const CompletionCandidate &Candidate, 879 const CompletionItemScores &Scores) { 880 CodeCompletionString *SemaCCS = nullptr; 881 if (auto *SR = Candidate.SemaResult) 882 SemaCCS = Recorder.codeCompletionString(*SR, Opts.IncludeBriefComments); 883 return Candidate.build(Scores, Opts, SemaCCS); 884 } 885 }; 886 887 CompletionList codeComplete(const Context &Ctx, PathRef FileName, 888 const tooling::CompileCommand &Command, 889 PrecompiledPreamble const *Preamble, 890 StringRef Contents, Position Pos, 891 IntrusiveRefCntPtr<vfs::FileSystem> VFS, 892 std::shared_ptr<PCHContainerOperations> PCHs, 893 CodeCompleteOptions Opts) { 894 return CodeCompleteFlow(Ctx, Opts).run( 895 {FileName, Command, Preamble, Contents, Pos, VFS, PCHs}); 896 } 897 898 SignatureHelp signatureHelp(const Context &Ctx, PathRef FileName, 899 const tooling::CompileCommand &Command, 900 PrecompiledPreamble const *Preamble, 901 StringRef Contents, Position Pos, 902 IntrusiveRefCntPtr<vfs::FileSystem> VFS, 903 std::shared_ptr<PCHContainerOperations> PCHs) { 904 SignatureHelp Result; 905 clang::CodeCompleteOptions Options; 906 Options.IncludeGlobals = false; 907 Options.IncludeMacros = false; 908 Options.IncludeCodePatterns = false; 909 Options.IncludeBriefComments = true; 910 semaCodeComplete( 911 Ctx, llvm::make_unique<SignatureHelpCollector>(Options, Result), Options, 912 {FileName, Command, Preamble, Contents, Pos, std::move(VFS), 913 std::move(PCHs)}); 914 return Result; 915 } 916 917 } // namespace clangd 918 } // namespace clang 919