[clang] 6136019 - [alpha.webkit.ForwardDeclChecker] Recognize a forward declared template specialization (#134545)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 10 15:28:39 PDT 2025
Author: Ryosuke Niwa
Date: 2025-04-10T15:28:36-07:00
New Revision: 613601978073058f4a4620e93bb84ac8f387934a
URL: https://github.com/llvm/llvm-project/commit/613601978073058f4a4620e93bb84ac8f387934a
DIFF: https://github.com/llvm/llvm-project/commit/613601978073058f4a4620e93bb84ac8f387934a.diff
LOG: [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.
Added:
Modified:
clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
clang/test/Analysis/Checkers/WebKit/forward-decl-checker.mm
Removed:
################################################################################
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
index 0d2c3bc53b085..a216d9abc4f76 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
@@ -126,8 +126,18 @@ class ForwardDeclChecker : public Checker<check::ASTDecl<TranslationUnitDecl>> {
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<ClassTemplateSpecializationDecl>(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<typename> class HashSet;
+
+template<typename T>
+using SingleThreadHashSet = HashSet<T>;
+
+template<typename> class HashSet { };
+
+struct Font { };
+
+struct ComplexTextController {
+ SingleThreadHashSet<const Font>* fallbackFonts { nullptr };
+};
+
+}
More information about the cfe-commits
mailing list