clang-tools  9.0.0
clangd/IncludeFixer.cpp
Go to the documentation of this file.
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "IncludeFixer.h"
10 #include "AST.h"
11 #include "Diagnostics.h"
12 #include "Logger.h"
13 #include "SourceCode.h"
14 #include "Trace.h"
15 #include "index/Index.h"
16 #include "index/Symbol.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclBase.h"
19 #include "clang/AST/NestedNameSpecifier.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/Diagnostic.h"
22 #include "clang/Basic/DiagnosticSema.h"
23 #include "clang/Basic/LangOptions.h"
24 #include "clang/Basic/SourceLocation.h"
25 #include "clang/Basic/SourceManager.h"
26 #include "clang/Basic/TokenKinds.h"
27 #include "clang/Lex/Lexer.h"
28 #include "clang/Sema/DeclSpec.h"
29 #include "clang/Sema/Lookup.h"
30 #include "clang/Sema/Scope.h"
31 #include "clang/Sema/Sema.h"
32 #include "clang/Sema/TypoCorrection.h"
33 #include "llvm/ADT/ArrayRef.h"
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/None.h"
36 #include "llvm/ADT/Optional.h"
37 #include "llvm/ADT/StringRef.h"
38 #include "llvm/ADT/StringSet.h"
39 #include "llvm/Support/Error.h"
40 #include "llvm/Support/FormatVariadic.h"
41 #include <vector>
42 
43 namespace clang {
44 namespace clangd {
45 
46 namespace {
47 
48 // Collects contexts visited during a Sema name lookup.
49 class VisitedContextCollector : public VisibleDeclConsumer {
50 public:
51  void EnteredContext(DeclContext *Ctx) override { Visited.push_back(Ctx); }
52 
53  void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
54  bool InBaseClass) override {}
55 
56  std::vector<DeclContext *> takeVisitedContexts() {
57  return std::move(Visited);
58  }
59 
60 private:
61  std::vector<DeclContext *> Visited;
62 };
63 
64 } // namespace
65 
66 std::vector<Fix> IncludeFixer::fix(DiagnosticsEngine::Level DiagLevel,
67  const clang::Diagnostic &Info) const {
68  switch (Info.getID()) {
69  case diag::err_incomplete_type:
70  case diag::err_incomplete_member_access:
71  case diag::err_incomplete_base_class:
72  case diag::err_incomplete_nested_name_spec:
73  // Incomplete type diagnostics should have a QualType argument for the
74  // incomplete type.
75  for (unsigned Idx = 0; Idx < Info.getNumArgs(); ++Idx) {
76  if (Info.getArgKind(Idx) == DiagnosticsEngine::ak_qualtype) {
77  auto QT = QualType::getFromOpaquePtr((void *)Info.getRawArg(Idx));
78  if (const Type *T = QT.getTypePtrOrNull())
79  if (T->isIncompleteType())
80  return fixIncompleteType(*T);
81  }
82  }
83  break;
84  case diag::err_unknown_typename:
85  case diag::err_unknown_typename_suggest:
86  case diag::err_typename_nested_not_found:
87  case diag::err_no_template:
88  case diag::err_no_template_suggest:
89  case diag::err_undeclared_use:
90  case diag::err_undeclared_use_suggest:
91  case diag::err_undeclared_var_use:
92  case diag::err_undeclared_var_use_suggest:
93  case diag::err_no_member: // Could be no member in namespace.
94  case diag::err_no_member_suggest:
95  if (LastUnresolvedName) {
96  // Try to fix unresolved name caused by missing declaraion.
97  // E.g.
98  // clang::SourceManager SM;
99  // ~~~~~~~~~~~~~
100  // UnresolvedName
101  // or
102  // namespace clang { SourceManager SM; }
103  // ~~~~~~~~~~~~~
104  // UnresolvedName
105  // We only attempt to recover a diagnostic if it has the same location as
106  // the last seen unresolved name.
107  if (DiagLevel >= DiagnosticsEngine::Error &&
108  LastUnresolvedName->Loc == Info.getLocation())
109  return fixUnresolvedName();
110  }
111  }
112  return {};
113 }
114 
115 std::vector<Fix> IncludeFixer::fixIncompleteType(const Type &T) const {
116  // Only handle incomplete TagDecl type.
117  const TagDecl *TD = T.getAsTagDecl();
118  if (!TD)
119  return {};
120  std::string TypeName = printQualifiedName(*TD);
121  trace::Span Tracer("Fix include for incomplete type");
122  SPAN_ATTACH(Tracer, "type", TypeName);
123  vlog("Trying to fix include for incomplete type {0}", TypeName);
124 
125  auto ID = getSymbolID(TD);
126  if (!ID)
127  return {};
128  llvm::Optional<const SymbolSlab *> Symbols = lookupCached(*ID);
129  if (!Symbols)
130  return {};
131  const SymbolSlab &Syms = **Symbols;
132  std::vector<Fix> Fixes;
133  if (!Syms.empty()) {
134  auto &Matched = *Syms.begin();
135  if (!Matched.IncludeHeaders.empty() && Matched.Definition &&
136  Matched.CanonicalDeclaration.FileURI == Matched.Definition.FileURI)
137  Fixes = fixesForSymbols(Syms);
138  }
139  return Fixes;
140 }
141 
142 std::vector<Fix> IncludeFixer::fixesForSymbols(const SymbolSlab &Syms) const {
143  auto Inserted = [&](const Symbol &Sym, llvm::StringRef Header)
144  -> llvm::Expected<std::pair<std::string, bool>> {
145  auto DeclaringURI = URI::parse(Sym.CanonicalDeclaration.FileURI);
146  if (!DeclaringURI)
147  return DeclaringURI.takeError();
148  auto ResolvedDeclaring = URI::resolve(*DeclaringURI, File);
149  if (!ResolvedDeclaring)
150  return ResolvedDeclaring.takeError();
151  auto ResolvedInserted = toHeaderFile(Header, File);
152  if (!ResolvedInserted)
153  return ResolvedInserted.takeError();
154  auto Spelled = Inserter->calculateIncludePath(*ResolvedInserted, File);
155  if (!Spelled)
156  return llvm::createStringError(llvm::inconvertibleErrorCode(),
157  "Header not on include path");
158  return std::make_pair(
159  std::move(*Spelled),
160  Inserter->shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
161  };
162 
163  std::vector<Fix> Fixes;
164  // Deduplicate fixes by include headers. This doesn't distiguish symbols in
165  // different scopes from the same header, but this case should be rare and is
166  // thus ignored.
167  llvm::StringSet<> InsertedHeaders;
168  for (const auto &Sym : Syms) {
169  for (const auto &Inc : getRankedIncludes(Sym)) {
170  if (auto ToInclude = Inserted(Sym, Inc)) {
171  if (ToInclude->second) {
172  auto I = InsertedHeaders.try_emplace(ToInclude->first);
173  if (!I.second)
174  continue;
175  if (auto Edit = Inserter->insert(ToInclude->first))
176  Fixes.push_back(
177  Fix{llvm::formatv("Add include {0} for symbol {1}{2}",
178  ToInclude->first, Sym.Scope, Sym.Name),
179  {std::move(*Edit)}});
180  }
181  } else {
182  vlog("Failed to calculate include insertion for {0} into {1}: {2}", Inc,
183  File, ToInclude.takeError());
184  }
185  }
186  }
187  return Fixes;
188 }
189 
190 // Returns the identifiers qualified by an unresolved name. \p Loc is the
191 // start location of the unresolved name. For the example below, this returns
192 // "::X::Y" that is qualified by unresolved name "clangd":
193 // clang::clangd::X::Y
194 // ~
195 llvm::Optional<std::string> qualifiedByUnresolved(const SourceManager &SM,
196  SourceLocation Loc,
197  const LangOptions &LangOpts) {
198  std::string Result;
199 
200  SourceLocation NextLoc = Loc;
201  while (auto CCTok = Lexer::findNextToken(NextLoc, SM, LangOpts)) {
202  if (!CCTok->is(tok::coloncolon))
203  break;
204  auto IDTok = Lexer::findNextToken(CCTok->getLocation(), SM, LangOpts);
205  if (!IDTok || !IDTok->is(tok::raw_identifier))
206  break;
207  Result.append(("::" + IDTok->getRawIdentifier()).str());
208  NextLoc = IDTok->getLocation();
209  }
210  if (Result.empty())
211  return llvm::None;
212  return Result;
213 }
214 
215 // An unresolved name and its scope information that can be extracted cheaply.
217  std::string Name;
218  // This is the part of what was typed that was resolved, and it's in its
219  // resolved form not its typed form (think `namespace clang { clangd::x }` -->
220  // `clang::clangd::`).
221  llvm::Optional<std::string> ResolvedScope;
222 
223  // Unresolved part of the scope. When the unresolved name is a specifier, we
224  // use the name that comes after it as the alternative name to resolve and use
225  // the specifier as the extra scope in the accessible scopes.
226  llvm::Optional<std::string> UnresolvedScope;
227 };
228 
229 // Extracts unresolved name and scope information around \p Unresolved.
230 // FIXME: try to merge this with the scope-wrangling code in CodeComplete.
231 llvm::Optional<CheapUnresolvedName> extractUnresolvedNameCheaply(
232  const SourceManager &SM, const DeclarationNameInfo &Unresolved,
233  CXXScopeSpec *SS, const LangOptions &LangOpts, bool UnresolvedIsSpecifier) {
234  bool Invalid = false;
235  llvm::StringRef Code = SM.getBufferData(
236  SM.getDecomposedLoc(Unresolved.getBeginLoc()).first, &Invalid);
237  if (Invalid)
238  return llvm::None;
240  Result.Name = Unresolved.getAsString();
241  if (SS && SS->isNotEmpty()) { // "::" or "ns::"
242  if (auto *Nested = SS->getScopeRep()) {
243  if (Nested->getKind() == NestedNameSpecifier::Global)
244  Result.ResolvedScope = "";
245  else if (const auto *NS = Nested->getAsNamespace()) {
246  auto SpecifiedNS = printNamespaceScope(*NS);
247 
248  // Check the specifier spelled in the source.
249  // If the resolved scope doesn't end with the spelled scope. The
250  // resolved scope can come from a sema typo correction. For example,
251  // sema assumes that "clangd::" is a typo of "clang::" and uses
252  // "clang::" as the specified scope in:
253  // namespace clang { clangd::X; }
254  // In this case, we use the "typo" specifier as extra scope instead
255  // of using the scope assumed by sema.
256  auto B = SM.getFileOffset(SS->getBeginLoc());
257  auto E = SM.getFileOffset(SS->getEndLoc());
258  std::string Spelling = (Code.substr(B, E - B) + "::").str();
259  if (llvm::StringRef(SpecifiedNS).endswith(Spelling))
260  Result.ResolvedScope = SpecifiedNS;
261  else
262  Result.UnresolvedScope = Spelling;
263  } else if (const auto *ANS = Nested->getAsNamespaceAlias()) {
264  Result.ResolvedScope = printNamespaceScope(*ANS->getNamespace());
265  } else {
266  // We don't fix symbols in scopes that are not top-level e.g. class
267  // members, as we don't collect includes for them.
268  return llvm::None;
269  }
270  }
271  }
272 
273  if (UnresolvedIsSpecifier) {
274  // If the unresolved name is a specifier e.g.
275  // clang::clangd::X
276  // ~~~~~~
277  // We try to resolve clang::clangd::X instead of clang::clangd.
278  // FIXME: We won't be able to fix include if the specifier is what we
279  // should resolve (e.g. it's a class scope specifier). Collecting include
280  // headers for nested types could make this work.
281 
282  // Not using the end location as it doesn't always point to the end of
283  // identifier.
284  if (auto QualifiedByUnresolved =
285  qualifiedByUnresolved(SM, Unresolved.getBeginLoc(), LangOpts)) {
286  auto Split = splitQualifiedName(*QualifiedByUnresolved);
287  if (!Result.UnresolvedScope)
288  Result.UnresolvedScope.emplace();
289  // If UnresolvedSpecifiedScope is already set, we simply append the
290  // extra scope. Suppose the unresolved name is "index" in the following
291  // example:
292  // namespace clang { clangd::index::X; }
293  // ~~~~~~ ~~~~~
294  // "clangd::" is assumed to be clang:: by Sema, and we would have used
295  // it as extra scope. With "index" being a specifier, we append "index::"
296  // to the extra scope.
297  Result.UnresolvedScope->append((Result.Name + Split.first).str());
298  Result.Name = Split.second;
299  }
300  }
301  return Result;
302 }
303 
305 public:
306  UnresolvedNameRecorder(llvm::Optional<UnresolvedName> &LastUnresolvedName)
307  : LastUnresolvedName(LastUnresolvedName) {}
308 
309  void InitializeSema(Sema &S) override { this->SemaPtr = &S; }
310 
311  // Captures the latest typo and treat it as an unresolved name that can
312  // potentially be fixed by adding #includes.
313  TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind,
314  Scope *S, CXXScopeSpec *SS,
315  CorrectionCandidateCallback &CCC,
316  DeclContext *MemberContext, bool EnteringContext,
317  const ObjCObjectPointerType *OPT) override {
318  assert(SemaPtr && "Sema must have been set.");
319  if (SemaPtr->isSFINAEContext())
320  return TypoCorrection();
321  if (!SemaPtr->SourceMgr.isWrittenInMainFile(Typo.getLoc()))
322  return clang::TypoCorrection();
323 
324  // This is not done lazily because `SS` can get out of scope and it's
325  // relatively cheap.
326  auto Extracted = extractUnresolvedNameCheaply(
327  SemaPtr->SourceMgr, Typo, SS, SemaPtr->LangOpts,
328  static_cast<Sema::LookupNameKind>(LookupKind) ==
329  Sema::LookupNameKind::LookupNestedNameSpecifierName);
330  if (!Extracted)
331  return TypoCorrection();
332  auto CheapUnresolved = std::move(*Extracted);
333  UnresolvedName Unresolved;
334  Unresolved.Name = CheapUnresolved.Name;
335  Unresolved.Loc = Typo.getBeginLoc();
336 
337  if (!CheapUnresolved.ResolvedScope && !S) // Give up if no scope available.
338  return TypoCorrection();
339 
340  auto *Sem = SemaPtr; // Avoid capturing `this`.
341  Unresolved.GetScopes = [Sem, CheapUnresolved, S, LookupKind]() {
342  std::vector<std::string> Scopes;
343  if (CheapUnresolved.ResolvedScope) {
344  Scopes.push_back(*CheapUnresolved.ResolvedScope);
345  } else {
346  assert(S);
347  // No scope specifier is specified. Collect all accessible scopes in the
348  // context.
349  VisitedContextCollector Collector;
350  Sem->LookupVisibleDecls(
351  S, static_cast<Sema::LookupNameKind>(LookupKind), Collector,
352  /*IncludeGlobalScope=*/false,
353  /*LoadExternal=*/false);
354 
355  Scopes.push_back("");
356  for (const auto *Ctx : Collector.takeVisitedContexts())
357  if (isa<NamespaceDecl>(Ctx))
358  Scopes.push_back(printNamespaceScope(*Ctx));
359  }
360 
361  if (CheapUnresolved.UnresolvedScope)
362  for (auto &Scope : Scopes)
363  Scope.append(*CheapUnresolved.UnresolvedScope);
364  return Scopes;
365  };
366  LastUnresolvedName = std::move(Unresolved);
367 
368  // Never return a valid correction to try to recover. Our suggested fixes
369  // always require a rebuild.
370  return TypoCorrection();
371  }
372 
373 private:
374  Sema *SemaPtr = nullptr;
375 
376  llvm::Optional<UnresolvedName> &LastUnresolvedName;
377 };
378 
379 llvm::IntrusiveRefCntPtr<ExternalSemaSource>
381  return new UnresolvedNameRecorder(LastUnresolvedName);
382 }
383 
384 std::vector<Fix> IncludeFixer::fixUnresolvedName() const {
385  assert(LastUnresolvedName.hasValue());
386  auto &Unresolved = *LastUnresolvedName;
387  std::vector<std::string> Scopes = Unresolved.GetScopes();
388  vlog("Trying to fix unresolved name \"{0}\" in scopes: [{1}]",
389  Unresolved.Name, llvm::join(Scopes.begin(), Scopes.end(), ", "));
390 
391  FuzzyFindRequest Req;
392  Req.AnyScope = false;
393  Req.Query = Unresolved.Name;
394  Req.Scopes = Scopes;
395  Req.RestrictForCodeCompletion = true;
396  Req.Limit = 100;
397 
398  if (llvm::Optional<const SymbolSlab *> Syms = fuzzyFindCached(Req))
399  return fixesForSymbols(**Syms);
400 
401  return {};
402 }
403 
404 llvm::Optional<const SymbolSlab *>
405 IncludeFixer::fuzzyFindCached(const FuzzyFindRequest &Req) const {
406  auto ReqStr = llvm::formatv("{0}", toJSON(Req)).str();
407  auto I = FuzzyFindCache.find(ReqStr);
408  if (I != FuzzyFindCache.end())
409  return &I->second;
410 
411  if (IndexRequestCount >= IndexRequestLimit)
412  return llvm::None;
413  IndexRequestCount++;
414 
415  SymbolSlab::Builder Matches;
416  Index.fuzzyFind(Req, [&](const Symbol &Sym) {
417  if (Sym.Name != Req.Query)
418  return;
419  if (!Sym.IncludeHeaders.empty())
420  Matches.insert(Sym);
421  });
422  auto Syms = std::move(Matches).build();
423  auto E = FuzzyFindCache.try_emplace(ReqStr, std::move(Syms));
424  return &E.first->second;
425 }
426 
427 llvm::Optional<const SymbolSlab *>
428 IncludeFixer::lookupCached(const SymbolID &ID) const {
429  LookupRequest Req;
430  Req.IDs.insert(ID);
431 
432  auto I = LookupCache.find(ID);
433  if (I != LookupCache.end())
434  return &I->second;
435 
436  if (IndexRequestCount >= IndexRequestLimit)
437  return llvm::None;
438  IndexRequestCount++;
439 
440  // FIXME: consider batching the requests for all diagnostics.
441  SymbolSlab::Builder Matches;
442  Index.lookup(Req, [&](const Symbol &Sym) { Matches.insert(Sym); });
443  auto Syms = std::move(Matches).build();
444 
445  std::vector<Fix> Fixes;
446  if (!Syms.empty()) {
447  auto &Matched = *Syms.begin();
448  if (!Matched.IncludeHeaders.empty() && Matched.Definition &&
449  Matched.CanonicalDeclaration.FileURI == Matched.Definition.FileURI)
450  Fixes = fixesForSymbols(Syms);
451  }
452  auto E = LookupCache.try_emplace(ID, std::move(Syms));
453  return &E.first->second;
454 }
455 
456 } // namespace clangd
457 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
llvm::Optional< std::string > qualifiedByUnresolved(const SourceManager &SM, SourceLocation Loc, const LangOptions &LangOpts)
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
llvm::json::Value toJSON(const FuzzyFindRequest &Request)
Definition: Index.cpp:48
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:39
std::string printQualifiedName(const NamedDecl &ND)
Returns the qualified name of ND.
Definition: AST.cpp:75
llvm::Optional< SymbolID > getSymbolID(const Decl *D)
Gets the symbol ID for a declaration, if possible.
Definition: AST.cpp:154
bool RestrictForCodeCompletion
If set to true, only symbols for completion support will be considered.
Definition: Index.h:44
std::string printNamespaceScope(const DeclContext &DC)
Returns the first enclosing namespace scope starting from DC.
Definition: AST.cpp:146
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:64
std::pair< StringRef, StringRef > splitQualifiedName(StringRef QName)
Definition: SourceCode.cpp:391
void insert(const Symbol &S)
Adds a symbol, overwriting any existing one with the same ID.
Definition: Symbol.cpp:50
Documents should not be synced at all.
llvm::Optional< std::string > UnresolvedScope
void vlog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:67
llvm::Optional< CheapUnresolvedName > extractUnresolvedNameCheaply(const SourceManager &SM, const DeclarationNameInfo &Unresolved, CXXScopeSpec *SS, const LangOptions &LangOpts, bool UnresolvedIsSpecifier)
SymbolSlab::Builder is a mutable container that can &#39;freeze&#39; to SymbolSlab.
Definition: Symbol.h:199
std::vector< std::string > Scopes
If this is non-empty, symbols must be in at least one of the scopes (e.g.
Definition: Index.h:36
Context Ctx
llvm::Optional< std::string > ResolvedScope
bool empty() const
Definition: Symbol.h:190
llvm::SmallVector< IncludeHeaderWithReferences, 1 > IncludeHeaders
One Symbol can potentially be incuded via different headers.
Definition: Symbol.h:111
std::string Query
A query string for the fuzzy find.
Definition: Index.h:29
llvm::Expected< HeaderFile > toHeaderFile(llvm::StringRef Header, llvm::StringRef HintPath)
Creates a HeaderFile from Header which can be either a URI or a literal include.
Definition: Headers.cpp:77
SymbolLocation CanonicalDeclaration
The location of the preferred declaration of the symbol.
Definition: Symbol.h:56
std::shared_ptr< SymbolCollector > Collector
Represents a single fix-it that editor can apply to fix the error.
Definition: Diagnostics.h:71
SymbolSlab Symbols
An information message.
TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind, Scope *S, CXXScopeSpec *SS, CorrectionCandidateCallback &CCC, DeclContext *MemberContext, bool EnteringContext, const ObjCObjectPointerType *OPT) override
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::IntrusiveRefCntPtr< ExternalSemaSource > unresolvedNameRecorder()
Returns an ExternalSemaSource that records failed name lookups in Sema.
llvm::StringRef Name
The unqualified name of the symbol, e.g. "bar" (for ns::bar).
Definition: Symbol.h:42
const_iterator begin() const
Definition: Symbol.h:185
llvm::SmallVector< llvm::StringRef, 1 > getRankedIncludes(const Symbol &Sym)
Definition: Headers.cpp:97
std::vector< Fix > fix(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) const
Returns include insertions that can potentially recover the diagnostic.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: Rename.cpp:36
std::vector< const char * > Expected
llvm::Optional< uint32_t > Limit
The number of top candidates to return.
Definition: Index.h:42
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
Definition: URI.cpp:222
static llvm::Expected< URI > parse(llvm::StringRef Uri)
Parse a URI string "<scheme>:[//<authority>/]<path>".
Definition: URI.cpp:164
static std::string join(ArrayRef< SpecialMemberFunctionsCheck::SpecialMemberFunctionKind > SMFS, llvm::StringRef AndOr)
Records an event whose duration is the lifetime of the Span object.
Definition: Trace.h:82
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
Definition: Trace.h:98
NodeType Type
UnresolvedNameRecorder(llvm::Optional< UnresolvedName > &LastUnresolvedName)
const SymbolIndex * Index
Definition: Dexp.cpp:84