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/DiagnosticParse.h"
24#include "clang/Basic/DiagnosticSema.h"
25#include "clang/Basic/LangOptions.h"
26#include "clang/Basic/SourceLocation.h"
27#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/TokenKinds.h"
29#include "clang/Lex/Lexer.h"
30#include "clang/Sema/DeclSpec.h"
31#include "clang/Sema/Lookup.h"
32#include "clang/Sema/Scope.h"
33#include "clang/Sema/Sema.h"
34#include "clang/Sema/TypoCorrection.h"
35#include "llvm/ADT/DenseMap.h"
36#include "llvm/ADT/STLExtras.h"
37#include "llvm/ADT/StringExtras.h"
38#include "llvm/ADT/StringRef.h"
39#include "llvm/ADT/StringSet.h"
40#include "llvm/Support/Error.h"
41#include "llvm/Support/FormatVariadic.h"
52std::optional<llvm::StringRef> getArgStr(
const clang::Diagnostic &
Info,
54 switch (
Info.getArgKind(Index)) {
55 case DiagnosticsEngine::ak_c_string:
56 return llvm::StringRef(
Info.getArgCStr(Index));
57 case DiagnosticsEngine::ak_std_string:
58 return llvm::StringRef(
Info.getArgStdStr(Index));
64std::vector<Fix> only(std::optional<Fix> F) {
66 return {std::move(*F)};
73 const clang::Diagnostic &
Info)
const {
74 switch (
Info.getID()) {
84 case diag::err_array_incomplete_or_sizeless_type:
85 case diag::err_array_size_incomplete_type:
86 case diag::err_asm_incomplete_type:
87 case diag::err_assoc_type_incomplete:
88 case diag::err_bad_cast_incomplete:
89 case diag::err_call_function_incomplete_return:
90 case diag::err_call_incomplete_argument:
91 case diag::err_call_incomplete_return:
92 case diag::err_capture_of_incomplete_or_sizeless_type:
93 case diag::err_catch_incomplete:
94 case diag::err_catch_incomplete_ptr:
95 case diag::err_catch_incomplete_ref:
96 case diag::err_cconv_incomplete_param_type:
97 case diag::err_coroutine_promise_type_incomplete:
98 case diag::err_covariant_return_incomplete:
100 case diag::err_delete_incomplete_class_type:
101 case diag::err_dereference_incomplete_type:
102 case diag::err_exception_spec_incomplete_type:
103 case diag::err_field_incomplete_or_sizeless:
104 case diag::err_for_range_incomplete_type:
105 case diag::err_func_def_incomplete_result:
106 case diag::err_ice_incomplete_type:
107 case diag::err_illegal_message_expr_incomplete_type:
108 case diag::err_incomplete_base_class:
109 case diag::err_incomplete_enum:
110 case diag::err_incomplete_in_exception_spec:
111 case diag::err_incomplete_member_access:
112 case diag::err_incomplete_nested_name_spec:
113 case diag::err_incomplete_object_call:
114 case diag::err_incomplete_receiver_type:
115 case diag::err_incomplete_synthesized_property:
116 case diag::err_incomplete_type:
117 case diag::err_incomplete_type_objc_at_encode:
118 case diag::err_incomplete_type_used_in_type_trait_expr:
119 case diag::err_incomplete_typeid:
120 case diag::err_init_incomplete_type:
121 case diag::err_invalid_incomplete_type_use:
122 case diag::err_lambda_incomplete_result:
125 case diag::err_memptr_incomplete:
126 case diag::err_new_incomplete_or_sizeless_type:
127 case diag::err_objc_incomplete_boxed_expression_type:
128 case diag::err_objc_index_incomplete_class_type:
129 case diag::err_offsetof_incomplete_type:
130 case diag::err_omp_firstprivate_incomplete_type:
131 case diag::err_omp_incomplete_type:
132 case diag::err_omp_lastprivate_incomplete_type:
133 case diag::err_omp_linear_incomplete_type:
134 case diag::err_omp_private_incomplete_type:
135 case diag::err_omp_reduction_incomplete_type:
136 case diag::err_omp_section_incomplete_type:
137 case diag::err_omp_threadprivate_incomplete_type:
138 case diag::err_second_parameter_to_va_arg_incomplete:
139 case diag::err_sizeof_alignof_incomplete_or_sizeless_type:
140 case diag::err_subscript_incomplete_or_sizeless_type:
141 case diag::err_switch_incomplete_class_type:
142 case diag::err_temp_copy_incomplete:
144 case diag::err_template_nontype_parm_incomplete:
146 case diag::err_throw_incomplete:
147 case diag::err_throw_incomplete_ptr:
148 case diag::err_typecheck_arithmetic_incomplete_or_sizeless_type:
149 case diag::err_typecheck_cast_to_incomplete:
150 case diag::err_typecheck_decl_incomplete_type:
152 case diag::err_typecheck_incomplete_tag:
153 case diag::err_typecheck_incomplete_type_not_modifiable_lvalue:
154 case diag::err_typecheck_nonviable_condition_incomplete:
155 case diag::err_underlying_type_of_incomplete_enum:
156 case diag::ext_incomplete_in_exception_spec:
158 case diag::ext_typecheck_decl_incomplete_type:
159 case diag::warn_delete_incomplete:
160 case diag::warn_incomplete_encoded_type:
162 case diag::warn_return_value_udt_incomplete:
168 for (
unsigned Idx = 0; Idx <
Info.getNumArgs(); ++Idx) {
169 if (
Info.getArgKind(Idx) == DiagnosticsEngine::ak_qualtype) {
170 auto QT = QualType::getFromOpaquePtr((
void *)
Info.getRawArg(Idx));
171 if (
const Type *T = QT.getTypePtrOrNull()) {
172 if (T->isIncompleteType())
173 return fixIncompleteType(*T);
176 if (
auto * ET = llvm::dyn_cast<EnumType>(T))
177 if (!ET->getDecl()->getDefinition())
178 return fixIncompleteType(*T);
184 case diag::err_unknown_typename:
185 case diag::err_unknown_typename_suggest:
186 case diag::err_unknown_type_or_class_name_suggest:
187 case diag::err_expected_class_name:
188 case diag::err_typename_nested_not_found:
189 case diag::err_no_template:
190 case diag::err_no_template_suggest:
191 case diag::err_undeclared_use:
192 case diag::err_undeclared_use_suggest:
193 case diag::err_undeclared_var_use:
194 case diag::err_undeclared_var_use_suggest:
195 case diag::err_no_member:
196 case diag::err_no_member_suggest:
197 case diag::err_no_member_template:
198 case diag::err_no_member_template_suggest:
199 case diag::warn_implicit_function_decl:
200 case diag::ext_implicit_function_decl_c99:
201 dlog(
"Unresolved name at {0}, last typo was {1}",
202 Info.getLocation().printToString(
Info.getSourceManager()),
204 ? LastUnresolvedName->Loc.printToString(
Info.getSourceManager())
206 if (LastUnresolvedName) {
218 if (LastUnresolvedName->Loc ==
Info.getLocation())
219 return fixUnresolvedName();
225 case diag::err_implied_std_initializer_list_not_found:
226 return only(insertHeader(
"<initializer_list>"));
227 case diag::err_need_header_before_typeid:
228 return only(insertHeader(
"<typeinfo>"));
229 case diag::err_need_header_before_placement_new:
230 case diag::err_implicit_coroutine_std_nothrow_type_not_found:
231 return only(insertHeader(
"<new>"));
232 case diag::err_omp_implied_type_not_found:
233 case diag::err_omp_interop_type_not_found:
234 return only(insertHeader(
"<omp.h>"));
235 case diag::err_implied_coroutine_type_not_found:
236 return only(insertHeader(
"<coroutine>"));
237 case diag::err_implied_comparison_category_type_not_found:
238 return only(insertHeader(
"<compare>"));
239 case diag::note_include_header_or_declare:
240 if (
Info.getNumArgs() > 0)
241 if (
auto Header = getArgStr(
Info, 0))
242 return only(insertHeader((
"<" + *Header +
">").str(),
243 getArgStr(
Info, 1).value_or(
"")));
250std::optional<Fix> IncludeFixer::insertHeader(llvm::StringRef
Spelled,
252 tooling::IncludeDirective
Directive)
const {
255 if (
auto Edit = Inserter->insert(
Spelled, Directive))
256 F.Edits.push_back(std::move(*
Edit));
260 llvm::StringRef DirectiveSpelling =
261 Directive == tooling::IncludeDirective::Include ?
"Include" :
"Import";
263 F.Message = llvm::formatv(
"{0} {1}", DirectiveSpelling,
Spelled);
265 F.Message = llvm::formatv(
"{0} {1} for symbol {2}",
271std::vector<Fix> IncludeFixer::fixIncompleteType(
const Type &T)
const {
273 const TagDecl *TD = T.getAsTagDecl();
277 trace::Span Tracer(
"Fix include for incomplete type");
279 vlog(
"Trying to fix include for incomplete type {0}", TypeName);
284 std::optional<const SymbolSlab *>
Symbols = lookupCached(
ID);
287 const SymbolSlab &Syms = **
Symbols;
288 std::vector<Fix> Fixes;
290 auto &Matched = *Syms.begin();
291 if (!Matched.IncludeHeaders.empty() && Matched.Definition &&
292 Matched.CanonicalDeclaration.FileURI == Matched.Definition.FileURI)
293 Fixes = fixesForSymbols(Syms);
298std::vector<Fix> IncludeFixer::fixesForSymbols(
const SymbolSlab &Syms)
const {
299 auto Inserted = [&](
const Symbol &Sym, llvm::StringRef Header)
300 -> llvm::Expected<std::pair<std::string, bool>> {
301 auto ResolvedDeclaring =
303 if (!ResolvedDeclaring)
304 return ResolvedDeclaring.takeError();
306 if (!ResolvedInserted)
307 return ResolvedInserted.takeError();
308 auto Spelled = Inserter->calculateIncludePath(*ResolvedInserted, File);
310 return error(
"Header not on include path");
311 return std::make_pair(
313 Inserter->shouldInsertInclude(*ResolvedDeclaring, *ResolvedInserted));
316 std::vector<Fix> Fixes;
320 llvm::StringSet<> InsertedHeaders;
321 for (
const auto &Sym : Syms) {
323 if ((Inc.Directive & Directive) == 0)
325 if (
auto ToInclude = Inserted(Sym, Inc.Header)) {
326 if (ToInclude->second) {
327 if (!InsertedHeaders.try_emplace(ToInclude->first).second)
330 insertHeader(ToInclude->first, (Sym.Scope + Sym.Name).str(),
332 ? tooling::IncludeDirective::Import
333 : tooling::IncludeDirective::Include))
334 Fixes.push_back(std::move(*Fix));
337 vlog(
"Failed to calculate include insertion for {0} into {1}: {2}",
338 Inc.Header, File, ToInclude.takeError());
352 const LangOptions &LangOpts) {
355 SourceLocation NextLoc = SM.getTopMacroCallerLoc(
Loc);
356 while (
auto CCTok = Lexer::findNextToken(NextLoc, SM, LangOpts)) {
357 if (!CCTok->is(tok::coloncolon))
359 auto IDTok = Lexer::findNextToken(CCTok->getLocation(), SM, LangOpts);
360 if (!IDTok || !IDTok->is(tok::raw_identifier))
362 Result.append((
"::" + IDTok->getRawIdentifier()).str());
363 NextLoc = IDTok->getLocation();
385 const SourceManager &SM) {
387 if (!SM.isWrittenInSameFile(SS.getBeginLoc(), SS.getEndLoc()))
389 SourceRange
Range(SM.getTopMacroCallerLoc(SS.getBeginLoc()), SM.getTopMacroCallerLoc(SS.getEndLoc()));
390 if (
Range.getBegin().isMacroID() ||
Range.getEnd().isMacroID())
399 const SourceManager &SM,
const DeclarationNameInfo &Unresolved,
400 CXXScopeSpec *SS,
const LangOptions &LangOpts,
bool UnresolvedIsSpecifier) {
402 Result.Name = Unresolved.getAsString();
403 if (SS && SS->isNotEmpty()) {
404 if (
auto *Nested = SS->getScopeRep()) {
405 if (Nested->getKind() == NestedNameSpecifier::Global) {
406 Result.ResolvedScope =
"";
407 }
else if (
const auto *NS = Nested->getAsNamespace()) {
419 if (!Spelling || llvm::StringRef(SpecifiedNS).ends_with(*Spelling)) {
420 Result.ResolvedScope = std::move(SpecifiedNS);
422 Result.UnresolvedScope = std::move(*Spelling);
424 }
else if (
const auto *ANS = Nested->getAsNamespaceAlias()) {
434 if (UnresolvedIsSpecifier) {
445 if (
auto QualifiedByUnresolved =
448 if (!Result.UnresolvedScope)
449 Result.UnresolvedScope.emplace();
458 Result.UnresolvedScope->append((Result.Name + Split.first).str());
459 Result.Name = std::string(Split.second);
466std::vector<std::string>
468 Sema::LookupNameKind LookupKind) {
470 struct VisitedContextCollector :
public VisibleDeclConsumer {
471 VisitedContextCollector(std::vector<std::string> &
Out) :
Out(
Out) {}
472 void EnteredContext(DeclContext *Ctx)
override {
473 if (llvm::isa<NamespaceDecl>(Ctx))
476 void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
477 bool InBaseClass)
override {}
478 std::vector<std::string> &
Out;
481 std::vector<std::string> Scopes;
482 Scopes.push_back(
"");
483 VisitedContextCollector Collector(Scopes);
484 Sem.LookupVisibleDecls(S, LookupKind, Collector,
488 Scopes.erase(std::unique(Scopes.begin(), Scopes.end()), Scopes.end());
495 : LastUnresolvedName(LastUnresolvedName) {}
501 TypoCorrection
CorrectTypo(
const DeclarationNameInfo &Typo,
int LookupKind,
502 Scope *S, CXXScopeSpec *SS,
503 CorrectionCandidateCallback &CCC,
504 DeclContext *MemberContext,
bool EnteringContext,
505 const ObjCObjectPointerType *OPT)
override {
506 dlog(
"CorrectTypo: {0}", Typo.getAsString());
507 assert(SemaPtr &&
"Sema must have been set.");
508 if (SemaPtr->isSFINAEContext())
509 return TypoCorrection();
511 return clang::TypoCorrection();
514 SemaPtr->SourceMgr, Typo, SS, SemaPtr->LangOpts,
515 static_cast<Sema::LookupNameKind
>(LookupKind) ==
516 Sema::LookupNameKind::LookupNestedNameSpecifierName);
518 return TypoCorrection();
520 UnresolvedName Unresolved;
521 Unresolved.Name = Extracted->Name;
522 Unresolved.Loc = Typo.getBeginLoc();
523 if (!Extracted->ResolvedScope && !S)
524 return TypoCorrection();
526 if (Extracted->ResolvedScope)
527 Unresolved.Scopes.push_back(*Extracted->ResolvedScope);
530 *SemaPtr, Typo, S,
static_cast<Sema::LookupNameKind
>(LookupKind));
532 if (Extracted->UnresolvedScope) {
533 for (std::string &Scope : Unresolved.Scopes)
534 Scope += *Extracted->UnresolvedScope;
537 LastUnresolvedName = std::move(Unresolved);
541 return TypoCorrection();
545 Sema *SemaPtr =
nullptr;
547 std::optional<UnresolvedName> &LastUnresolvedName;
550llvm::IntrusiveRefCntPtr<ExternalSemaSource>
555std::vector<Fix> IncludeFixer::fixUnresolvedName()
const {
556 assert(LastUnresolvedName);
557 auto &Unresolved = *LastUnresolvedName;
558 vlog(
"Trying to fix unresolved name \"{0}\" in scopes: [{1}]",
559 Unresolved.Name, llvm::join(Unresolved.Scopes,
", "));
563 Req.
Query = Unresolved.Name;
564 Req.
Scopes = Unresolved.Scopes;
568 if (std::optional<const SymbolSlab *> Syms = fuzzyFindCached(Req))
569 return fixesForSymbols(**Syms);
574std::optional<const SymbolSlab *>
575IncludeFixer::fuzzyFindCached(
const FuzzyFindRequest &Req)
const {
576 auto ReqStr = llvm::formatv(
"{0}",
toJSON(Req)).str();
577 auto I = FuzzyFindCache.find(ReqStr);
578 if (I != FuzzyFindCache.end())
581 if (IndexRequestCount >= IndexRequestLimit)
585 SymbolSlab::Builder Matches;
586 Index.fuzzyFind(Req, [&](
const Symbol &Sym) {
587 if (Sym.Name != Req.Query)
589 if (!Sym.IncludeHeaders.empty())
592 auto Syms = std::move(Matches).build();
593 auto E = FuzzyFindCache.try_emplace(ReqStr, std::move(Syms));
594 return &
E.first->second;
597std::optional<const SymbolSlab *>
598IncludeFixer::lookupCached(
const SymbolID &
ID)
const {
602 auto I = LookupCache.find(
ID);
603 if (I != LookupCache.end())
606 if (IndexRequestCount >= IndexRequestLimit)
611 SymbolSlab::Builder Matches;
612 Index.lookup(Req, [&](
const Symbol &Sym) { Matches.insert(Sym); });
613 auto Syms = std::move(Matches).build();
615 std::vector<Fix> Fixes;
617 auto &Matched = *Syms.begin();
618 if (!Matched.IncludeHeaders.empty() && Matched.Definition &&
619 Matched.CanonicalDeclaration.FileURI == Matched.Definition.FileURI)
620 Fixes = fixesForSymbols(Syms);
622 auto E = LookupCache.try_emplace(
ID, std::move(Syms));
623 return &
E.first->second;
CompiledFragmentImpl & Out
const MacroDirective * Directive
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind, Scope *S, CXXScopeSpec *SS, CorrectionCandidateCallback &CCC, DeclContext *MemberContext, bool EnteringContext, const ObjCObjectPointerType *OPT) override
void InitializeSema(Sema &S) override
UnresolvedNameRecorder(std::optional< UnresolvedName > &LastUnresolvedName)
llvm::IntrusiveRefCntPtr< ExternalSemaSource > unresolvedNameRecorder()
Returns an ExternalSemaSource that records failed name lookups in Sema.
std::vector< Fix > fix(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) const
Returns include insertions that can potentially recover the diagnostic.
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
std::pair< StringRef, StringRef > splitQualifiedName(StringRef QName)
@ Info
An information message.
SymbolID getSymbolID(const Decl *D)
Gets the symbol ID for a declaration. Returned SymbolID might be null.
std::optional< CheapUnresolvedName > extractUnresolvedNameCheaply(const SourceManager &SM, const DeclarationNameInfo &Unresolved, CXXScopeSpec *SS, const LangOptions &LangOpts, bool UnresolvedIsSpecifier)
bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM)
Returns true iff Loc is inside the main file.
void vlog(const char *Fmt, Ts &&... Vals)
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
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.
std::optional< std::string > getSpelledSpecifier(const CXXScopeSpec &SS, const SourceManager &SM)
std::optional< std::string > qualifiedByUnresolved(const SourceManager &SM, SourceLocation Loc, const LangOptions &LangOpts)
llvm::json::Value toJSON(const FuzzyFindRequest &Request)
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.
llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R)
Returns the source code covered by the source range.
@ Type
An inlay hint that for a type annotation.
llvm::SmallVector< SymbolInclude, 1 > getRankedIncludes(const Symbol &Sym)
std::string printQualifiedName(const NamedDecl &ND)
Returns the qualified name of ND.
std::string printNamespaceScope(const DeclContext &DC)
Returns the first enclosing namespace scope starting from DC.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::optional< std::string > UnresolvedScope
std::optional< std::string > ResolvedScope
A set of edits generated for a single file.
Represents a single fix-it that editor can apply to fix the error.
std::vector< std::string > Scopes
If this is non-empty, symbols must be in at least one of the scopes (e.g.
bool RestrictForCodeCompletion
If set to true, only symbols for completion support will be considered.
std::string Query
A query string for the fuzzy find.
bool AnyScope
If set to true, allow symbols from any scope.
std::optional< uint32_t > Limit
The number of top candidates to return.
The class presents a C++ symbol, e.g.
@ Import
#import "header.h"