https://github.com/hokein created https://github.com/llvm/llvm-project/pull/129031
See discussion in https://github.com/llvm/llvm-project/issues/125071. >From ccd5da1bdc4c80bd3461458fc1f4360d766fb6e2 Mon Sep 17 00:00:00 2001 From: Haojian Wu <hokein...@gmail.com> Date: Thu, 27 Feb 2025 11:15:09 +0100 Subject: [PATCH] Improve the -Wundefined-func-template diagnostic note for invisible function templates --- .../clang/Basic/DiagnosticSemaKinds.td | 2 ++ clang/include/clang/Sema/Sema.h | 3 +- clang/lib/Sema/SemaTemplate.cpp | 5 +++- .../lib/Sema/SemaTemplateInstantiateDecl.cpp | 28 +++++++++++++------ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index f10af8f5bd6b2..78ac2de5f1490 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -5654,6 +5654,8 @@ def warn_func_template_missing : Warning<"instantiation of function %q0 " InGroup<UndefinedFuncTemplate>, DefaultIgnore; def note_forward_template_decl : Note< "forward declaration of template entity is here">; +def note_unreachable_template_decl : Note< + "declaration of template entity is unreachable here">; def note_inst_declaration_hint : Note<"add an explicit instantiation " "declaration to suppress this warning if %q0 is explicitly instantiated in " "another translation unit">; diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index a501b901862b6..8916124ab73f9 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -11189,7 +11189,8 @@ class Sema final : public SemaBase { const NamedDecl *Pattern, const NamedDecl *PatternDef, TemplateSpecializationKind TSK, - bool Complain = true); + bool Complain = true, + bool* Unreachable = nullptr); /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining /// that the template parameter 'PrevDecl' is being shadowed by a new diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 38fa3ff3ab5b4..c9cfba3131419 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -765,7 +765,8 @@ bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, const NamedDecl *Pattern, const NamedDecl *PatternDef, TemplateSpecializationKind TSK, - bool Complain /*= true*/) { + bool Complain /*= true*/, + bool* Unreachable) { assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || isa<VarDecl>(Instantiation)); @@ -778,6 +779,8 @@ bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, if (!hasReachableDefinition(const_cast<NamedDecl *>(PatternDef), &SuggestedDef, /*OnlyNeedComplete*/ false)) { + if (Unreachable) + *Unreachable = true; // If we're allowed to diagnose this and recover, do so. bool Recover = Complain && !isSFINAEContext(); if (Complain) diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 1f42f9500959e..399abd43e69ee 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -20,6 +20,7 @@ #include "clang/AST/ExprCXX.h" #include "clang/AST/PrettyDeclStackTrace.h" #include "clang/AST/TypeLoc.h" +#include "clang/Basic/DiagnosticParse.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/TargetInfo.h" #include "clang/Sema/EnterExpressionEvaluationContext.h" @@ -34,6 +35,7 @@ #include "clang/Sema/SemaSwift.h" #include "clang/Sema/Template.h" #include "clang/Sema/TemplateInstCallback.h" +#include "llvm/Support/Signals.h" #include "llvm/Support/TimeProfiler.h" #include <optional> @@ -5052,12 +5054,15 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, PatternDef = nullptr; } + // True is the template definition is unreachable, otherwise false. + bool Unreachable = false; // FIXME: We need to track the instantiation stack in order to know which // definitions should be visible within this instantiation. - if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Function, - Function->getInstantiatedFromMemberFunction(), - PatternDecl, PatternDef, TSK, - /*Complain*/DefinitionRequired)) { + if (DiagnoseUninstantiableTemplate( + PointOfInstantiation, Function, + Function->getInstantiatedFromMemberFunction(), PatternDecl, + PatternDef, TSK, + /*Complain*/ DefinitionRequired, &Unreachable)) { if (DefinitionRequired) Function->setInvalidDecl(); else if (TSK == TSK_ExplicitInstantiationDefinition || @@ -5082,11 +5087,16 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { Diag(PointOfInstantiation, diag::warn_func_template_missing) - << Function; - Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); - if (getLangOpts().CPlusPlus11) - Diag(PointOfInstantiation, diag::note_inst_declaration_hint) - << Function; + << Function; + if (Unreachable) { + Diag(PatternDecl->getLocation(), + diag::note_unreachable_template_decl); + } else { + Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); + if (getLangOpts().CPlusPlus11) + Diag(PointOfInstantiation, diag::note_inst_declaration_hint) + << Function; + } } } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits