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" 51 class VisitedContextCollector :
public VisibleDeclConsumer {
53 void EnteredContext(DeclContext *
Ctx)
override { Visited.push_back(Ctx); }
55 void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
56 bool InBaseClass)
override {}
58 std::vector<DeclContext *> takeVisitedContexts() {
59 return std::move(Visited);
63 std::vector<DeclContext *> Visited;
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:
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);
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:
96 case diag::err_no_member_suggest:
97 if (LastUnresolvedName) {
110 LastUnresolvedName->Loc == Info.getLocation())
111 return fixUnresolvedName();
117 std::vector<Fix> IncludeFixer::fixIncompleteType(
const Type &T)
const {
119 const TagDecl *TD = T.getAsTagDecl();
123 trace::Span Tracer(
"Fix include for incomplete type");
125 vlog(
"Trying to fix include for incomplete type {0}", TypeName);
130 llvm::Optional<const SymbolSlab *>
Symbols = lookupCached(*ID);
134 std::vector<Fix> Fixes;
136 auto &Matched = *Syms.
begin();
137 if (!Matched.IncludeHeaders.empty() && Matched.Definition &&
138 Matched.CanonicalDeclaration.FileURI == Matched.Definition.FileURI)
139 Fixes = fixesForSymbols(Syms);
144 std::vector<Fix> IncludeFixer::fixesForSymbols(
const SymbolSlab &Syms)
const {
145 auto Inserted = [&](
const Symbol &Sym, llvm::StringRef Header)
149 return DeclaringURI.takeError();
151 if (!ResolvedDeclaring)
152 return ResolvedDeclaring.takeError();
154 if (!ResolvedInserted)
155 return ResolvedInserted.takeError();
156 auto Spelled = Inserter->calculateIncludePath(*ResolvedInserted,
File);
158 return llvm::createStringError(llvm::inconvertibleErrorCode(),
159 "Header not on include path");
160 return std::make_pair(
162 Inserter->shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
165 std::vector<Fix> Fixes;
169 llvm::StringSet<> InsertedHeaders;
170 for (
const auto &Sym : Syms) {
172 if (
auto ToInclude = Inserted(Sym, Inc)) {
173 if (ToInclude->second) {
174 auto I = InsertedHeaders.try_emplace(ToInclude->first);
177 if (
auto Edit = Inserter->insert(ToInclude->first))
179 Fix{llvm::formatv(
"Add include {0} for symbol {1}{2}",
180 ToInclude->first, Sym.Scope, Sym.Name),
181 {std::move(*Edit)}});
184 vlog(
"Failed to calculate include insertion for {0} into {1}: {2}", Inc,
185 File, ToInclude.takeError());
199 const LangOptions &LangOpts) {
202 SourceLocation NextLoc =
Loc;
203 while (
auto CCTok = Lexer::findNextToken(NextLoc, SM, LangOpts)) {
204 if (!CCTok->is(tok::coloncolon))
206 auto IDTok = Lexer::findNextToken(CCTok->getLocation(), SM, LangOpts);
207 if (!IDTok || !IDTok->is(tok::raw_identifier))
209 Result.append((
"::" + IDTok->getRawIdentifier()).str());
210 NextLoc = IDTok->getLocation();
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);
242 Result.
Name = Unresolved.getAsString();
243 if (SS && SS->isNotEmpty()) {
244 if (
auto *Nested = SS->getScopeRep()) {
245 if (Nested->getKind() == NestedNameSpecifier::Global)
247 else if (
const auto *NS = Nested->getAsNamespace()) {
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))
265 }
else if (
const auto *ANS = Nested->getAsNamespaceAlias()) {
275 if (UnresolvedIsSpecifier) {
286 if (
auto QualifiedByUnresolved =
300 Result.
Name = Split.second;
307 std::vector<std::string>
309 Sema::LookupNameKind LookupKind) {
310 std::vector<std::string> Scopes;
312 Sem.LookupVisibleDecls(S, LookupKind, Collector,
316 Scopes.push_back(
"");
317 for (
const auto *
Ctx : Collector.takeVisitedContexts()) {
318 if (isa<NamespaceDecl>(
Ctx))
327 : LastUnresolvedName(LastUnresolvedName) {}
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();
342 return clang::TypoCorrection();
345 SemaPtr->SourceMgr, Typo, SS, SemaPtr->LangOpts,
346 static_cast<Sema::LookupNameKind>(LookupKind) ==
347 Sema::LookupNameKind::LookupNestedNameSpecifierName);
349 return TypoCorrection();
351 UnresolvedName Unresolved;
352 Unresolved.Name = Extracted->Name;
353 Unresolved.Loc = Typo.getBeginLoc();
354 if (!Extracted->ResolvedScope && !S)
355 return TypoCorrection();
357 if (Extracted->ResolvedScope)
358 Unresolved.Scopes.push_back(*Extracted->ResolvedScope);
361 *SemaPtr, Typo, S, static_cast<Sema::LookupNameKind>(LookupKind));
363 if (Extracted->UnresolvedScope) {
364 for (std::string &Scope : Unresolved.Scopes)
365 Scope += *Extracted->UnresolvedScope;
368 LastUnresolvedName = std::move(Unresolved);
372 return TypoCorrection();
376 Sema *SemaPtr =
nullptr;
378 llvm::Optional<UnresolvedName> &LastUnresolvedName;
381 llvm::IntrusiveRefCntPtr<ExternalSemaSource>
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,
", "));
394 Req.
Query = Unresolved.Name;
395 Req.
Scopes = Unresolved.Scopes;
399 if (llvm::Optional<const SymbolSlab *> Syms = fuzzyFindCached(Req))
400 return fixesForSymbols(**Syms);
405 llvm::Optional<const SymbolSlab *>
407 auto ReqStr = llvm::formatv(
"{0}",
toJSON(Req)).str();
408 auto I = FuzzyFindCache.find(ReqStr);
409 if (I != FuzzyFindCache.end())
412 if (IndexRequestCount >= IndexRequestLimit)
423 auto Syms = std::move(Matches).build();
424 auto E = FuzzyFindCache.try_emplace(ReqStr, std::move(Syms));
425 return &E.first->second;
428 llvm::Optional<const SymbolSlab *>
429 IncludeFixer::lookupCached(
const SymbolID &ID)
const {
433 auto I = LookupCache.find(ID);
434 if (I != LookupCache.end())
437 if (IndexRequestCount >= IndexRequestLimit)
444 auto Syms = std::move(Matches).build();
446 std::vector<Fix> Fixes;
448 auto &Matched = *Syms.begin();
449 if (!Matched.IncludeHeaders.empty() && Matched.Definition &&
450 Matched.CanonicalDeclaration.FileURI == Matched.Definition.FileURI)
451 Fixes = fixesForSymbols(Syms);
453 auto E = LookupCache.try_emplace(ID, std::move(Syms));
454 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)
bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM)
Returns true iff Loc is inside the main file.
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)
std::vector< std::string > collectAccessibleScopes(Sema &Sem, const DeclarationNameInfo &Typo, Scope *S, Sema::LookupNameKind LookupKind)
Returns all namespace scopes that the unqualified lookup would visit.
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