brintos

brintos / llvm-project-archived public Read only

0
0
Text · 23.9 KiB · 3f3d7fb Raw
629 lines · cpp
1//===--- IncludeFixer.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "IncludeFixer.h"10#include "AST.h"11#include "Diagnostics.h"12#include "SourceCode.h"13#include "index/Index.h"14#include "index/Symbol.h"15#include "support/Logger.h"16#include "support/Trace.h"17#include "clang/AST/Decl.h"18#include "clang/AST/DeclBase.h"19#include "clang/AST/DeclarationName.h"20#include "clang/AST/NestedNameSpecifier.h"21#include "clang/AST/Type.h"22#include "clang/Basic/Diagnostic.h"23#include "clang/Basic/DiagnosticParse.h"24#include "clang/Basic/DiagnosticSema.h"25#include "clang/Basic/LangOptions.h"26#include "clang/Basic/SourceLocation.h"27#include "clang/Basic/SourceManager.h"28#include "clang/Basic/TokenKinds.h"29#include "clang/Lex/Lexer.h"30#include "clang/Sema/DeclSpec.h"31#include "clang/Sema/Lookup.h"32#include "clang/Sema/Scope.h"33#include "clang/Sema/Sema.h"34#include "clang/Sema/TypoCorrection.h"35#include "llvm/ADT/DenseMap.h"36#include "llvm/ADT/STLExtras.h"37#include "llvm/ADT/StringExtras.h"38#include "llvm/ADT/StringRef.h"39#include "llvm/ADT/StringSet.h"40#include "llvm/Support/Error.h"41#include "llvm/Support/FormatVariadic.h"42#include <algorithm>43#include <optional>44#include <set>45#include <string>46#include <vector>47 48namespace clang {49namespace clangd {50namespace {51 52std::optional<llvm::StringRef> getArgStr(const clang::Diagnostic &Info,53                                         unsigned Index) {54  switch (Info.getArgKind(Index)) {55  case DiagnosticsEngine::ak_c_string:56    return llvm::StringRef(Info.getArgCStr(Index));57  case DiagnosticsEngine::ak_std_string:58    return llvm::StringRef(Info.getArgStdStr(Index));59  default:60    return std::nullopt;61  }62}63 64std::vector<Fix> only(std::optional<Fix> F) {65  if (F)66    return {std::move(*F)};67  return {};68}69 70} // namespace71 72std::vector<Fix> IncludeFixer::fix(DiagnosticsEngine::Level DiagLevel,73                                   const clang::Diagnostic &Info) const {74  switch (Info.getID()) {75  /*76   There are many "incomplete type" diagnostics!77   They are almost all Sema diagnostics with "incomplete" in the name.78 79   sed -n '/CLASS_NOTE/! s/DIAG(\\([^,]*\\).*)/  case diag::\\1:/p' \80     tools/clang/include/clang/Basic/DiagnosticSemaKinds.inc | grep incomplete81  */82  // clang-format off83  //case diag::err_alignof_member_of_incomplete_type:84  case diag::err_array_incomplete_or_sizeless_type:85  case diag::err_array_size_incomplete_type:86  case diag::err_asm_incomplete_type:87  case diag::err_bad_cast_incomplete:88  case diag::err_call_function_incomplete_return:89  case diag::err_call_incomplete_argument:90  case diag::err_call_incomplete_return:91  case diag::err_capture_of_incomplete_or_sizeless_type:92  case diag::err_catch_incomplete:93  case diag::err_catch_incomplete_ptr:94  case diag::err_catch_incomplete_ref:95  case diag::err_cconv_incomplete_param_type:96  case diag::err_coroutine_promise_type_incomplete:97  case diag::err_covariant_return_incomplete:98  //case diag::err_deduced_class_template_incomplete:99  case diag::err_delete_incomplete_class_type:100  case diag::err_dereference_incomplete_type:101  case diag::err_exception_spec_incomplete_type:102  case diag::err_field_incomplete_or_sizeless:103  case diag::err_for_range_incomplete_type:104  case diag::err_func_def_incomplete_result:105  case diag::err_ice_incomplete_type:106  case diag::err_illegal_message_expr_incomplete_type:107  case diag::err_incomplete_base_class:108  case diag::err_incomplete_enum:109  case diag::err_incomplete_in_exception_spec:110  case diag::err_incomplete_member_access:111  case diag::err_incomplete_nested_name_spec:112  case diag::err_incomplete_object_call:113  case diag::err_incomplete_receiver_type:114  case diag::err_incomplete_synthesized_property:115  case diag::err_incomplete_type:116  case diag::err_incomplete_type_objc_at_encode:117  case diag::err_incomplete_type_used_in_type_trait_expr:118  case diag::err_incomplete_typeid:119  case diag::err_init_incomplete_type:120  case diag::err_invalid_incomplete_type_use:121  case diag::err_lambda_incomplete_result:122  //case diag::err_matrix_incomplete_index:123  //case diag::err_matrix_separate_incomplete_index:124  case diag::err_memptr_incomplete:125  case diag::err_new_incomplete_or_sizeless_type:126  case diag::err_objc_incomplete_boxed_expression_type:127  case diag::err_objc_index_incomplete_class_type:128  case diag::err_offsetof_incomplete_type:129  case diag::err_omp_firstprivate_incomplete_type:130  case diag::err_omp_incomplete_type:131  case diag::err_omp_lastprivate_incomplete_type:132  case diag::err_omp_linear_incomplete_type:133  case diag::err_omp_private_incomplete_type:134  case diag::err_omp_reduction_incomplete_type:135  case diag::err_omp_section_incomplete_type:136  case diag::err_omp_threadprivate_incomplete_type:137  case diag::err_second_parameter_to_va_arg_incomplete:138  case diag::err_sizeof_alignof_incomplete_or_sizeless_type:139  case diag::err_subscript_incomplete_or_sizeless_type:140  case diag::err_switch_incomplete_class_type:141  case diag::err_temp_copy_incomplete:142  //case diag::err_template_arg_deduced_incomplete_pack:143  case diag::err_template_nontype_parm_incomplete:144  //case diag::err_tentative_def_incomplete_type:145  case diag::err_throw_incomplete:146  case diag::err_throw_incomplete_ptr:147  case diag::err_typecheck_arithmetic_incomplete_or_sizeless_type:148  case diag::err_typecheck_cast_to_incomplete:149  case diag::err_typecheck_decl_incomplete_type:150  //case diag::err_typecheck_incomplete_array_needs_initializer:151  case diag::err_typecheck_incomplete_tag:152  case diag::err_typecheck_incomplete_type_not_modifiable_lvalue:153  case diag::err_typecheck_nonviable_condition_incomplete:154  case diag::err_underlying_type_of_incomplete_enum:155  case diag::ext_incomplete_in_exception_spec:156  //case diag::ext_typecheck_compare_complete_incomplete_pointers:157  case diag::ext_typecheck_decl_incomplete_type:158  case diag::warn_delete_incomplete:159  case diag::warn_incomplete_encoded_type:160  //case diag::warn_printf_incomplete_specifier:161  case diag::warn_return_value_udt_incomplete:162  //case diag::warn_scanf_scanlist_incomplete:163  //case diag::warn_tentative_incomplete_array:164    //  clang-format on165    // Incomplete type diagnostics should have a QualType argument for the166    // incomplete type.167    for (unsigned Idx = 0; Idx < Info.getNumArgs(); ++Idx) {168      if (Info.getArgKind(Idx) == DiagnosticsEngine::ak_qualtype) {169        auto QT = QualType::getFromOpaquePtr((void *)Info.getRawArg(Idx));170        if (const Type *T = QT.getTypePtrOrNull()) {171          if (T->isIncompleteType())172            return fixIncompleteType(*T);173          // `enum x : int;' is not formally an incomplete type.174          // We may need a full definition anyway.175          if (auto * ET = llvm::dyn_cast<EnumType>(T))176            if (!ET->getDecl()->getDefinition())177              return fixIncompleteType(*T);178        }179      }180    }181    break;182 183  case diag::err_unknown_typename:184  case diag::err_unknown_typename_suggest:185  case diag::err_unknown_type_or_class_name_suggest:186  case diag::err_expected_class_name:187  case diag::err_typename_nested_not_found:188  case diag::err_no_template:189  case diag::err_no_template_suggest:190  case diag::err_undeclared_use:191  case diag::err_undeclared_use_suggest:192  case diag::err_undeclared_var_use:193  case diag::err_undeclared_var_use_suggest:194  case diag::err_no_member: // Could be no member in namespace.195  case diag::err_no_member_suggest:196  case diag::err_no_member_template:197  case diag::err_no_member_template_suggest:198  case diag::warn_implicit_function_decl:199  case diag::ext_implicit_function_decl_c99:200    dlog("Unresolved name at {0}, last typo was {1}",201         Info.getLocation().printToString(Info.getSourceManager()),202         LastUnresolvedName203             ? LastUnresolvedName->Loc.printToString(Info.getSourceManager())204             : "none");205    if (LastUnresolvedName) {206      // Try to fix unresolved name caused by missing declaration.207      // E.g.208      //   clang::SourceManager SM;209      //          ~~~~~~~~~~~~~210      //          UnresolvedName211      //   or212      //   namespace clang {  SourceManager SM; }213      //                      ~~~~~~~~~~~~~214      //                      UnresolvedName215      // We only attempt to recover a diagnostic if it has the same location as216      // the last seen unresolved name.217      if (LastUnresolvedName->Loc == Info.getLocation())218        return fixUnresolvedName();219    }220    break;221 222  // Cases where clang explicitly knows which header to include.223  // (There's no fix provided for boring formatting reasons).224  case diag::err_implied_std_initializer_list_not_found:225    return only(insertHeader("<initializer_list>"));226  case diag::err_need_header_before_typeid:227    return only(insertHeader("<typeinfo>"));228  case diag::err_need_header_before_placement_new:229  case diag::err_implicit_coroutine_std_nothrow_type_not_found:230    return only(insertHeader("<new>"));231  case diag::err_omp_implied_type_not_found:232  case diag::err_omp_interop_type_not_found:233    return only(insertHeader("<omp.h>"));234  case diag::err_implied_coroutine_type_not_found:235    return only(insertHeader("<coroutine>"));236  case diag::err_implied_comparison_category_type_not_found:237    return only(insertHeader("<compare>"));238  case diag::note_include_header_or_declare:239    if (Info.getNumArgs() > 0)240      if (auto Header = getArgStr(Info, 0))241        return only(insertHeader(("<" + *Header + ">").str(),242                                 getArgStr(Info, 1).value_or("")));243    break;244  }245 246  return {};247}248 249std::optional<Fix> IncludeFixer::insertHeader(llvm::StringRef Spelled,250                                               llvm::StringRef Symbol,251                                               tooling::IncludeDirective Directive) const {252  Fix F;253 254  if (auto Edit = Inserter->insert(Spelled, Directive))255    F.Edits.push_back(std::move(*Edit));256  else257    return std::nullopt;258 259  llvm::StringRef DirectiveSpelling =260      Directive == tooling::IncludeDirective::Include ? "Include" : "Import";261  if (Symbol.empty())262    F.Message = llvm::formatv("{0} {1}", DirectiveSpelling, Spelled);263  else264    F.Message = llvm::formatv("{0} {1} for symbol {2}",265        DirectiveSpelling, Spelled, Symbol);266 267  return F;268}269 270std::vector<Fix> IncludeFixer::fixIncompleteType(const Type &T) const {271  // Only handle incomplete TagDecl type.272  const TagDecl *TD = T.getAsTagDecl();273  if (!TD)274    return {};275  std::string TypeName = printQualifiedName(*TD);276  trace::Span Tracer("Fix include for incomplete type");277  SPAN_ATTACH(Tracer, "type", TypeName);278  vlog("Trying to fix include for incomplete type {0}", TypeName);279 280  auto ID = getSymbolID(TD);281  if (!ID)282    return {};283  std::optional<const SymbolSlab *> Symbols = lookupCached(ID);284  if (!Symbols)285    return {};286  const SymbolSlab &Syms = **Symbols;287  std::vector<Fix> Fixes;288  if (!Syms.empty()) {289    auto &Matched = *Syms.begin();290    if (!Matched.IncludeHeaders.empty() && Matched.Definition &&291        Matched.CanonicalDeclaration.FileURI == Matched.Definition.FileURI)292      Fixes = fixesForSymbols(Syms);293  }294  return Fixes;295}296 297std::vector<Fix> IncludeFixer::fixesForSymbols(const SymbolSlab &Syms) const {298  auto Inserted = [&](const Symbol &Sym, llvm::StringRef Header)299      -> llvm::Expected<std::pair<std::string, bool>> {300    auto ResolvedDeclaring =301        URI::resolve(Sym.CanonicalDeclaration.FileURI, File);302    if (!ResolvedDeclaring)303      return ResolvedDeclaring.takeError();304    auto ResolvedInserted = toHeaderFile(Header, File);305    if (!ResolvedInserted)306      return ResolvedInserted.takeError();307    auto Spelled = Inserter->calculateIncludePath(*ResolvedInserted, File);308    if (!Spelled)309      return error("Header not on include path");310    return std::make_pair(311        std::move(*Spelled),312        Inserter->shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));313  };314 315  std::vector<Fix> Fixes;316  // Deduplicate fixes by include headers. This doesn't distinguish symbols in317  // different scopes from the same header, but this case should be rare and is318  // thus ignored.319  llvm::StringSet<> InsertedHeaders;320  for (const auto &Sym : Syms) {321    for (const auto &Inc : getRankedIncludes(Sym)) {322      if ((Inc.Directive & Directive) == 0)323        continue;324      if (auto ToInclude = Inserted(Sym, Inc.Header)) {325        if (ToInclude->second) {326          if (!InsertedHeaders.try_emplace(ToInclude->first).second)327            continue;328          if (auto Fix =329                  insertHeader(ToInclude->first, (Sym.Scope + Sym.Name).str(),330                               Directive == Symbol::Import331                                  ? tooling::IncludeDirective::Import332                                  : tooling::IncludeDirective::Include))333            Fixes.push_back(std::move(*Fix));334        }335      } else {336        vlog("Failed to calculate include insertion for {0} into {1}: {2}",337             Inc.Header, File, ToInclude.takeError());338      }339    }340  }341  return Fixes;342}343 344// Returns the identifiers qualified by an unresolved name. \p Loc is the345// start location of the unresolved name. For the example below, this returns346// "::X::Y" that is qualified by unresolved name "clangd":347//     clang::clangd::X::Y348//            ~349std::optional<std::string> qualifiedByUnresolved(const SourceManager &SM,350                                                  SourceLocation Loc,351                                                  const LangOptions &LangOpts) {352  std::string Result;353  // Accept qualifier written within macro arguments, but not macro bodies.354  SourceLocation NextLoc = SM.getTopMacroCallerLoc(Loc);355  while (auto CCTok = Lexer::findNextToken(NextLoc, SM, LangOpts)) {356    if (!CCTok->is(tok::coloncolon))357      break;358    auto IDTok = Lexer::findNextToken(CCTok->getLocation(), SM, LangOpts);359    if (!IDTok || !IDTok->is(tok::raw_identifier))360      break;361    Result.append(("::" + IDTok->getRawIdentifier()).str());362    NextLoc = IDTok->getLocation();363  }364  if (Result.empty())365    return std::nullopt;366  return Result;367}368 369// An unresolved name and its scope information that can be extracted cheaply.370struct CheapUnresolvedName {371  std::string Name;372  // This is the part of what was typed that was resolved, and it's in its373  // resolved form not its typed form (think `namespace clang { clangd::x }` -->374  // `clang::clangd::`).375  std::optional<std::string> ResolvedScope;376 377  // Unresolved part of the scope. When the unresolved name is a specifier, we378  // use the name that comes after it as the alternative name to resolve and use379  // the specifier as the extra scope in the accessible scopes.380  std::optional<std::string> UnresolvedScope;381};382 383std::optional<std::string> getSpelledSpecifier(const CXXScopeSpec &SS,384    const SourceManager &SM) {385  // Support specifiers written within a single macro argument.386  if (!SM.isWrittenInSameFile(SS.getBeginLoc(), SS.getEndLoc()))387    return std::nullopt;388  SourceRange Range(SM.getTopMacroCallerLoc(SS.getBeginLoc()), SM.getTopMacroCallerLoc(SS.getEndLoc()));389  if (Range.getBegin().isMacroID() || Range.getEnd().isMacroID())390    return std::nullopt;391 392  return (toSourceCode(SM, Range) + "::").str();393}394 395// Extracts unresolved name and scope information around \p Unresolved.396// FIXME: try to merge this with the scope-wrangling code in CodeComplete.397std::optional<CheapUnresolvedName> extractUnresolvedNameCheaply(398    const SourceManager &SM, const DeclarationNameInfo &Unresolved,399    CXXScopeSpec *SS, const LangOptions &LangOpts, bool UnresolvedIsSpecifier) {400  CheapUnresolvedName Result;401  Result.Name = Unresolved.getAsString();402  if (SS && SS->isNotEmpty()) { // "::" or "ns::"403    NestedNameSpecifier Nested = SS->getScopeRep();404    if (Nested.getKind() == NestedNameSpecifier::Kind::Global) {405      Result.ResolvedScope = "";406    } else if (Nested.getKind() == NestedNameSpecifier::Kind::Namespace) {407      const NamespaceBaseDecl *NSB = Nested.getAsNamespaceAndPrefix().Namespace;408      if (const auto *NS = dyn_cast<NamespaceDecl>(NSB)) {409        std::string SpecifiedNS = printNamespaceScope(*NS);410        std::optional<std::string> Spelling = getSpelledSpecifier(*SS, SM);411 412        // Check the specifier spelled in the source.413        // If the resolved scope doesn't end with the spelled scope, the414        // resolved scope may come from a sema typo correction. For example,415        // sema assumes that "clangd::" is a typo of "clang::" and uses416        // "clang::" as the specified scope in:417        //     namespace clang { clangd::X; }418        // In this case, we use the "typo" specifier as extra scope instead419        // of using the scope assumed by sema.420        if (!Spelling || llvm::StringRef(SpecifiedNS).ends_with(*Spelling)) {421          Result.ResolvedScope = std::move(SpecifiedNS);422        } else {423          Result.UnresolvedScope = std::move(*Spelling);424        }425      } else {426        Result.ResolvedScope = printNamespaceScope(*cast<NamespaceAliasDecl>(NSB)->getNamespace());427      }428    } else {429      // We don't fix symbols in scopes that are not top-level e.g. class430      // members, as we don't collect includes for them.431      return std::nullopt;432    }433  }434 435  if (UnresolvedIsSpecifier) {436    // If the unresolved name is a specifier e.g.437    //      clang::clangd::X438    //             ~~~~~~439    // We try to resolve clang::clangd::X instead of clang::clangd.440    // FIXME: We won't be able to fix include if the specifier is what we441    // should resolve (e.g. it's a class scope specifier). Collecting include442    // headers for nested types could make this work.443 444    // Not using the end location as it doesn't always point to the end of445    // identifier.446    if (auto QualifiedByUnresolved =447            qualifiedByUnresolved(SM, Unresolved.getBeginLoc(), LangOpts)) {448      auto Split = splitQualifiedName(*QualifiedByUnresolved);449      if (!Result.UnresolvedScope)450        Result.UnresolvedScope.emplace();451      // If UnresolvedSpecifiedScope is already set, we simply append the452      // extra scope. Suppose the unresolved name is "index" in the following453      // example:454      //   namespace clang {  clangd::index::X; }455      //                      ~~~~~~  ~~~~~456      // "clangd::" is assumed to be clang:: by Sema, and we would have used457      // it as extra scope. With "index" being a specifier, we append "index::"458      // to the extra scope.459      Result.UnresolvedScope->append((Result.Name + Split.first).str());460      Result.Name = std::string(Split.second);461    }462  }463  return Result;464}465 466/// Returns all namespace scopes that the unqualified lookup would visit.467std::vector<std::string>468collectAccessibleScopes(Sema &Sem, const DeclarationNameInfo &Typo, Scope *S,469                        Sema::LookupNameKind LookupKind) {470  // Collects contexts visited during a Sema name lookup.471  struct VisitedContextCollector : public VisibleDeclConsumer {472    VisitedContextCollector(std::vector<std::string> &Out) : Out(Out) {}473    void EnteredContext(DeclContext *Ctx) override {474      if (llvm::isa<NamespaceDecl>(Ctx))475        Out.push_back(printNamespaceScope(*Ctx));476    }477    void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,478                   bool InBaseClass) override {}479    std::vector<std::string> &Out;480  };481 482  std::vector<std::string> Scopes;483  Scopes.push_back("");484  VisitedContextCollector Collector(Scopes);485  Sem.LookupVisibleDecls(S, LookupKind, Collector,486                         /*IncludeGlobalScope=*/false,487                         /*LoadExternal=*/false);488  llvm::sort(Scopes);489  Scopes.erase(llvm::unique(Scopes), Scopes.end());490  return Scopes;491}492 493class IncludeFixer::UnresolvedNameRecorder : public ExternalSemaSource {494public:495  UnresolvedNameRecorder(std::optional<UnresolvedName> &LastUnresolvedName)496      : LastUnresolvedName(LastUnresolvedName) {}497 498  void InitializeSema(Sema &S) override { this->SemaPtr = &S; }499 500  // Captures the latest typo and treat it as an unresolved name that can501  // potentially be fixed by adding #includes.502  TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind,503                             Scope *S, CXXScopeSpec *SS,504                             CorrectionCandidateCallback &CCC,505                             DeclContext *MemberContext, bool EnteringContext,506                             const ObjCObjectPointerType *OPT) override {507    dlog("CorrectTypo: {0}", Typo.getAsString());508    assert(SemaPtr && "Sema must have been set.");509    if (SemaPtr->isSFINAEContext())510      return TypoCorrection();511    if (!isInsideMainFile(Typo.getLoc(), SemaPtr->SourceMgr))512      return clang::TypoCorrection();513 514    auto Extracted = extractUnresolvedNameCheaply(515        SemaPtr->SourceMgr, Typo, SS, SemaPtr->LangOpts,516        static_cast<Sema::LookupNameKind>(LookupKind) ==517            Sema::LookupNameKind::LookupNestedNameSpecifierName);518    if (!Extracted)519      return TypoCorrection();520 521    UnresolvedName Unresolved;522    Unresolved.Name = Extracted->Name;523    Unresolved.Loc = Typo.getBeginLoc();524    if (!Extracted->ResolvedScope && !S) // Give up if no scope available.525      return TypoCorrection();526 527    if (Extracted->ResolvedScope)528      Unresolved.Scopes.push_back(*Extracted->ResolvedScope);529    else // no qualifier or qualifier is unresolved.530      Unresolved.Scopes = collectAccessibleScopes(531          *SemaPtr, Typo, S, static_cast<Sema::LookupNameKind>(LookupKind));532 533    if (Extracted->UnresolvedScope) {534      for (std::string &Scope : Unresolved.Scopes)535        Scope += *Extracted->UnresolvedScope;536    }537 538    LastUnresolvedName = std::move(Unresolved);539 540    // Never return a valid correction to try to recover. Our suggested fixes541    // always require a rebuild.542    return TypoCorrection();543  }544 545private:546  Sema *SemaPtr = nullptr;547 548  std::optional<UnresolvedName> &LastUnresolvedName;549};550 551llvm::IntrusiveRefCntPtr<ExternalSemaSource>552IncludeFixer::unresolvedNameRecorder() {553  return new UnresolvedNameRecorder(LastUnresolvedName);554}555 556std::vector<Fix> IncludeFixer::fixUnresolvedName() const {557  assert(LastUnresolvedName);558  auto &Unresolved = *LastUnresolvedName;559  vlog("Trying to fix unresolved name \"{0}\" in scopes: [{1}]",560       Unresolved.Name, llvm::join(Unresolved.Scopes, ", "));561 562  FuzzyFindRequest Req;563  Req.AnyScope = false;564  Req.Query = Unresolved.Name;565  Req.Scopes = Unresolved.Scopes;566  Req.RestrictForCodeCompletion = true;567  Req.Limit = 100;568 569  if (std::optional<const SymbolSlab *> Syms = fuzzyFindCached(Req))570    return fixesForSymbols(**Syms);571 572  return {};573}574 575std::optional<const SymbolSlab *>576IncludeFixer::fuzzyFindCached(const FuzzyFindRequest &Req) const {577  auto ReqStr = llvm::formatv("{0}", toJSON(Req)).str();578  auto I = FuzzyFindCache.find(ReqStr);579  if (I != FuzzyFindCache.end())580    return &I->second;581 582  if (IndexRequestCount >= IndexRequestLimit)583    return std::nullopt;584  IndexRequestCount++;585 586  SymbolSlab::Builder Matches;587  Index.fuzzyFind(Req, [&](const Symbol &Sym) {588    if (Sym.Name != Req.Query)589      return;590    if (!Sym.IncludeHeaders.empty())591      Matches.insert(Sym);592  });593  auto Syms = std::move(Matches).build();594  auto E = FuzzyFindCache.try_emplace(ReqStr, std::move(Syms));595  return &E.first->second;596}597 598std::optional<const SymbolSlab *>599IncludeFixer::lookupCached(const SymbolID &ID) const {600  LookupRequest Req;601  Req.IDs.insert(ID);602 603  auto I = LookupCache.find(ID);604  if (I != LookupCache.end())605    return &I->second;606 607  if (IndexRequestCount >= IndexRequestLimit)608    return std::nullopt;609  IndexRequestCount++;610 611  // FIXME: consider batching the requests for all diagnostics.612  SymbolSlab::Builder Matches;613  Index.lookup(Req, [&](const Symbol &Sym) { Matches.insert(Sym); });614  auto Syms = std::move(Matches).build();615 616  std::vector<Fix> Fixes;617  if (!Syms.empty()) {618    auto &Matched = *Syms.begin();619    if (!Matched.IncludeHeaders.empty() && Matched.Definition &&620        Matched.CanonicalDeclaration.FileURI == Matched.Definition.FileURI)621      Fixes = fixesForSymbols(Syms);622  }623  auto E = LookupCache.try_emplace(ID, std::move(Syms));624  return &E.first->second;625}626 627} // namespace clangd628} // namespace clang629