diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp index 2c63224df129a..73a0e9eda5b20 100644 --- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp @@ -125,8 +125,18 @@ class ForwardDeclChecker : public Checker> { if (!R) // Forward declaration of a Objective-C interface is safe. return false; auto Name = R->getName(); - return !R->hasDefinition() && !RTC.isUnretained(QT) && - !SystemTypes.contains(CanonicalType) && + if (R->hasDefinition()) + return false; + // Find a definition amongst template declarations. + if (auto *Specialization = dyn_cast(R)) { + if (auto *S = Specialization->getSpecializedTemplate()) { + for (S = S->getMostRecentDecl(); S; S = S->getPreviousDecl()) { + if (S->isThisDeclarationADefinition()) + return false; + } + } + } + return !RTC.isUnretained(QT) && !SystemTypes.contains(CanonicalType) && !SystemTypes.contains(PointeeType) && !Name.starts_with("Opaque") && Name != "_NSZone"; } diff --git a/clang/test/Analysis/Checkers/WebKit/forward-decl-checker.mm b/clang/test/Analysis/Checkers/WebKit/forward-decl-checker.mm index 64100d60c4867..084b47322d7f9 100644 --- a/clang/test/Analysis/Checkers/WebKit/forward-decl-checker.mm +++ b/clang/test/Analysis/Checkers/WebKit/forward-decl-checker.mm @@ -138,3 +138,20 @@ - (void)doMoreWork:(ObjCObj *)obj { } @end + +namespace template_forward_declare { + +template class HashSet; + +template +using SingleThreadHashSet = HashSet; + +template class HashSet { }; + +struct Font { }; + +struct ComplexTextController { + SingleThreadHashSet* fallbackFonts { nullptr }; +}; + +}