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" 49 class VisitedContextCollector :
public VisibleDeclConsumer {
51 void EnteredContext(DeclContext *
Ctx)
override { Visited.push_back(Ctx); }
53 void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
54 bool InBaseClass)
override {}
56 std::vector<DeclContext *> takeVisitedContexts() {
57 return std::move(Visited);
61 std::vector<DeclContext *> Visited;
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:
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);
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:
94 case diag::err_no_member_suggest:
95 if (LastUnresolvedName) {
108 LastUnresolvedName->Loc == Info.getLocation())
109 return fixUnresolvedName();
115 std::vector<Fix> IncludeFixer::fixIncompleteType(
const Type &T)
const {
117 const TagDecl *TD = T.getAsTagDecl();
121 trace::Span Tracer(
"Fix include for incomplete type");
123 vlog(
"Trying to fix include for incomplete type {0}", TypeName);
128 llvm::Optional<const SymbolSlab *>
Symbols = lookupCached(*ID);
132 std::vector<Fix> Fixes;
134 auto &Matched = *Syms.
begin();
135 if (!Matched.IncludeHeaders.empty() && Matched.Definition &&
136 Matched.CanonicalDeclaration.FileURI == Matched.Definition.FileURI)
137 Fixes = fixesForSymbols(Syms);
142 std::vector<Fix> IncludeFixer::fixesForSymbols(
const SymbolSlab &Syms)
const {
143 auto Inserted = [&](
const Symbol &Sym, llvm::StringRef Header)
147 return DeclaringURI.takeError();
149 if (!ResolvedDeclaring)
150 return ResolvedDeclaring.takeError();
152 if (!ResolvedInserted)
153 return ResolvedInserted.takeError();
154 auto Spelled = Inserter->calculateIncludePath(*ResolvedInserted,
File);
156 return llvm::createStringError(llvm::inconvertibleErrorCode(),
157 "Header not on include path");
158 return std::make_pair(
160 Inserter->shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
163 std::vector<Fix> Fixes;
167 llvm::StringSet<> InsertedHeaders;
168 for (
const auto &Sym : Syms) {
170 if (
auto ToInclude = Inserted(Sym, Inc)) {
171 if (ToInclude->second) {
172 auto I = InsertedHeaders.try_emplace(ToInclude->first);
175 if (
auto Edit = Inserter->insert(ToInclude->first))
177 Fix{llvm::formatv(
"Add include {0} for symbol {1}{2}",
178 ToInclude->first, Sym.Scope, Sym.Name),
179 {std::move(*Edit)}});
182 vlog(
"Failed to calculate include insertion for {0} into {1}: {2}", Inc,
183 File, ToInclude.takeError());
197 const LangOptions &LangOpts) {
200 SourceLocation NextLoc =
Loc;
201 while (
auto CCTok = Lexer::findNextToken(NextLoc, SM, LangOpts)) {
202 if (!CCTok->is(tok::coloncolon))
204 auto IDTok = Lexer::findNextToken(CCTok->getLocation(), SM, LangOpts);
205 if (!IDTok || !IDTok->is(tok::raw_identifier))
207 Result.append((
"::" + IDTok->getRawIdentifier()).str());
208 NextLoc = IDTok->getLocation();
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);
240 Result.
Name = Unresolved.getAsString();
241 if (SS && SS->isNotEmpty()) {
242 if (
auto *Nested = SS->getScopeRep()) {
243 if (Nested->getKind() == NestedNameSpecifier::Global)
245 else if (
const auto *NS = Nested->getAsNamespace()) {
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))
263 }
else if (
const auto *ANS = Nested->getAsNamespaceAlias()) {
273 if (UnresolvedIsSpecifier) {
284 if (
auto QualifiedByUnresolved =
298 Result.
Name = Split.second;
307 : LastUnresolvedName(LastUnresolvedName) {}
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();
327 SemaPtr->SourceMgr, Typo, SS, SemaPtr->LangOpts,
328 static_cast<Sema::LookupNameKind>(LookupKind) ==
329 Sema::LookupNameKind::LookupNestedNameSpecifierName);
331 return TypoCorrection();
332 auto CheapUnresolved = std::move(*Extracted);
333 UnresolvedName Unresolved;
334 Unresolved.Name = CheapUnresolved.Name;
335 Unresolved.Loc = Typo.getBeginLoc();
337 if (!CheapUnresolved.ResolvedScope && !S)
338 return TypoCorrection();
341 Unresolved.GetScopes = [Sem, CheapUnresolved, S, LookupKind]() {
342 std::vector<std::string> Scopes;
343 if (CheapUnresolved.ResolvedScope) {
344 Scopes.push_back(*CheapUnresolved.ResolvedScope);
350 Sem->LookupVisibleDecls(
351 S, static_cast<Sema::LookupNameKind>(LookupKind), Collector,
355 Scopes.push_back(
"");
356 for (
const auto *
Ctx : Collector.takeVisitedContexts())
357 if (isa<NamespaceDecl>(
Ctx))
361 if (CheapUnresolved.UnresolvedScope)
362 for (
auto &Scope : Scopes)
363 Scope.append(*CheapUnresolved.UnresolvedScope);
366 LastUnresolvedName = std::move(Unresolved);
370 return TypoCorrection();
374 Sema *SemaPtr =
nullptr;
376 llvm::Optional<UnresolvedName> &LastUnresolvedName;
379 llvm::IntrusiveRefCntPtr<ExternalSemaSource>
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(),
", "));
393 Req.
Query = Unresolved.Name;
398 if (llvm::Optional<const SymbolSlab *> Syms = fuzzyFindCached(Req))
399 return fixesForSymbols(**Syms);
404 llvm::Optional<const SymbolSlab *>
406 auto ReqStr = llvm::formatv(
"{0}",
toJSON(Req)).str();
407 auto I = FuzzyFindCache.find(ReqStr);
408 if (I != FuzzyFindCache.end())
411 if (IndexRequestCount >= IndexRequestLimit)
422 auto Syms = std::move(Matches).build();
423 auto E = FuzzyFindCache.try_emplace(ReqStr, std::move(Syms));
424 return &E.first->second;
427 llvm::Optional<const SymbolSlab *>
428 IncludeFixer::lookupCached(
const SymbolID &ID)
const {
432 auto I = LookupCache.find(ID);
433 if (I != LookupCache.end())
436 if (IndexRequestCount >= IndexRequestLimit)
443 auto Syms = std::move(Matches).build();
445 std::vector<Fix> Fixes;
447 auto &Matched = *Syms.begin();
448 if (!Matched.IncludeHeaders.empty() && Matched.Definition &&
449 Matched.CanonicalDeclaration.FileURI == Matched.Definition.FileURI)
450 Fixes = fixesForSymbols(Syms);
452 auto E = LookupCache.try_emplace(ID, std::move(Syms));
453 return &E.first->second;
SourceLocation Loc
'#' 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.
llvm::json::Value toJSON(const FuzzyFindRequest &Request)
bool AnyScope
If set to true, allow symbols from any scope.
std::string printQualifiedName(const NamedDecl &ND)
Returns the qualified name of ND.
llvm::Optional< SymbolID > getSymbolID(const Decl *D)
Gets the symbol ID for a declaration, if possible.
bool RestrictForCodeCompletion
If set to true, only symbols for completion support will be considered.
std::string printNamespaceScope(const DeclContext &DC)
Returns the first enclosing namespace scope starting from DC.
llvm::DenseSet< SymbolID > IDs
std::pair< StringRef, StringRef > splitQualifiedName(StringRef QName)
void insert(const Symbol &S)
Adds a symbol, overwriting any existing one with the same ID.
void InitializeSema(Sema &S) override
Documents should not be synced at all.
llvm::Optional< std::string > UnresolvedScope
void vlog(const char *Fmt, Ts &&... Vals)
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 'freeze' to SymbolSlab.
std::vector< std::string > Scopes
If this is non-empty, symbols must be in at least one of the scopes (e.g.
llvm::Optional< std::string > ResolvedScope
llvm::SmallVector< IncludeHeaderWithReferences, 1 > IncludeHeaders
One Symbol can potentially be incuded via different headers.
std::string Query
A query string for the fuzzy find.
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.
SymbolLocation CanonicalDeclaration
The location of the preferred declaration of the symbol.
std::shared_ptr< SymbolCollector > Collector
Represents a single fix-it that editor can apply to fix the error.
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.
===– 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).
const_iterator begin() const
llvm::SmallVector< llvm::StringRef, 1 > getRankedIncludes(const Symbol &Sym)
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
std::vector< const char * > Expected
llvm::Optional< uint32_t > Limit
The number of top candidates to return.
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
static llvm::Expected< URI > parse(llvm::StringRef Uri)
Parse a URI string "<scheme>:[//<authority>/]<path>".
static std::string join(ArrayRef< SpecialMemberFunctionsCheck::SpecialMemberFunctionKind > SMFS, llvm::StringRef AndOr)
Records an event whose duration is the lifetime of the Span object.
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
UnresolvedNameRecorder(llvm::Optional< UnresolvedName > &LastUnresolvedName)
const SymbolIndex * Index