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)
147 auto ResolvedDeclaring =
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;
305 std::vector<std::string>
307 Sema::LookupNameKind LookupKind) {
308 std::vector<std::string> Scopes;
310 Sem.LookupVisibleDecls(S, LookupKind, Collector,
314 Scopes.push_back(
"");
315 for (
const auto *
Ctx : Collector.takeVisitedContexts()) {
316 if (isa<NamespaceDecl>(
Ctx))
325 : LastUnresolvedName(LastUnresolvedName) {}
331 TypoCorrection
CorrectTypo(
const DeclarationNameInfo &Typo,
int LookupKind,
332 Scope *S, CXXScopeSpec *SS,
333 CorrectionCandidateCallback &CCC,
334 DeclContext *MemberContext,
bool EnteringContext,
335 const ObjCObjectPointerType *OPT)
override {
336 assert(SemaPtr &&
"Sema must have been set.");
337 if (SemaPtr->isSFINAEContext())
338 return TypoCorrection();
340 return clang::TypoCorrection();
343 SemaPtr->SourceMgr, Typo, SS, SemaPtr->LangOpts,
344 static_cast<Sema::LookupNameKind>(LookupKind) ==
345 Sema::LookupNameKind::LookupNestedNameSpecifierName);
347 return TypoCorrection();
349 UnresolvedName Unresolved;
350 Unresolved.Name = Extracted->Name;
351 Unresolved.Loc = Typo.getBeginLoc();
352 if (!Extracted->ResolvedScope && !S)
353 return TypoCorrection();
355 if (Extracted->ResolvedScope)
356 Unresolved.Scopes.push_back(*Extracted->ResolvedScope);
359 *SemaPtr, Typo, S, static_cast<Sema::LookupNameKind>(LookupKind));
361 if (Extracted->UnresolvedScope) {
362 for (std::string &Scope : Unresolved.Scopes)
363 Scope += *Extracted->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 vlog(
"Trying to fix unresolved name \"{0}\" in scopes: [{1}]",
388 Unresolved.Name,
llvm::join(Unresolved.Scopes,
", "));
392 Req.
Query = Unresolved.Name;
393 Req.
Scopes = Unresolved.Scopes;
397 if (llvm::Optional<const SymbolSlab *> Syms = fuzzyFindCached(Req))
398 return fixesForSymbols(**Syms);
403 llvm::Optional<const SymbolSlab *>
405 auto ReqStr = llvm::formatv(
"{0}",
toJSON(Req)).str();
406 auto I = FuzzyFindCache.find(ReqStr);
407 if (I != FuzzyFindCache.end())
410 if (IndexRequestCount >= IndexRequestLimit)
421 auto Syms = std::move(Matches).build();
422 auto E = FuzzyFindCache.try_emplace(ReqStr, std::move(Syms));
423 return &
E.first->second;
426 llvm::Optional<const SymbolSlab *>
427 IncludeFixer::lookupCached(
const SymbolID &ID)
const {
431 auto I = LookupCache.find(ID);
432 if (I != LookupCache.end())
435 if (IndexRequestCount >= IndexRequestLimit)
442 auto Syms = std::move(Matches).build();
444 std::vector<Fix> Fixes;
446 auto &Matched = *Syms.begin();
447 if (!Matched.IncludeHeaders.empty() && Matched.Definition &&
448 Matched.CanonicalDeclaration.FileURI == Matched.Definition.FileURI)
449 Fixes = fixesForSymbols(Syms);
451 auto E = LookupCache.try_emplace(ID, std::move(Syms));
452 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 included 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.
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 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.
A set of edits generated for a single file.
UnresolvedNameRecorder(llvm::Optional< UnresolvedName > &LastUnresolvedName)
const SymbolIndex * Index