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