Skip to content

Commit 6136019

Browse files
authored
[alpha.webkit.ForwardDeclChecker] Recognize a forward declared template specialization (#134545)
This PR fixes a bug that when a template specialization is declared with a forward declaration of a template, the checker fails to find its definition in the same translation unit and erroneously emit an unsafe forward declaration warning.
1 parent 6c51603 commit 6136019

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,18 @@ class ForwardDeclChecker : public Checker<check::ASTDecl<TranslationUnitDecl>> {
126126
if (!R) // Forward declaration of a Objective-C interface is safe.
127127
return false;
128128
auto Name = R->getName();
129-
return !R->hasDefinition() && !RTC.isUnretained(QT) &&
130-
!SystemTypes.contains(CanonicalType) &&
129+
if (R->hasDefinition())
130+
return false;
131+
// Find a definition amongst template declarations.
132+
if (auto *Specialization = dyn_cast<ClassTemplateSpecializationDecl>(R)) {
133+
if (auto *S = Specialization->getSpecializedTemplate()) {
134+
for (S = S->getMostRecentDecl(); S; S = S->getPreviousDecl()) {
135+
if (S->isThisDeclarationADefinition())
136+
return false;
137+
}
138+
}
139+
}
140+
return !RTC.isUnretained(QT) && !SystemTypes.contains(CanonicalType) &&
131141
!SystemTypes.contains(PointeeType) && !Name.starts_with("Opaque") &&
132142
Name != "_NSZone";
133143
}

clang/test/Analysis/Checkers/WebKit/forward-decl-checker.mm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,20 @@ - (void)doMoreWork:(ObjCObj *)obj {
138138
}
139139

140140
@end
141+
142+
namespace template_forward_declare {
143+
144+
template<typename> class HashSet;
145+
146+
template<typename T>
147+
using SingleThreadHashSet = HashSet<T>;
148+
149+
template<typename> class HashSet { };
150+
151+
struct Font { };
152+
153+
struct ComplexTextController {
154+
SingleThreadHashSet<const Font>* fallbackFonts { nullptr };
155+
};
156+
157+
}

0 commit comments

Comments
 (0)