[clang] [alpha.webkit.ForwardDeclChecker] Recognize a forward declared template specialization (PR #134545)
Ryosuke Niwa via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 6 09:29:44 PDT 2025
https://github.com/rniwa created https://github.com/llvm/llvm-project/pull/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.
>From a6c4a36a0c7a9b24e0f44748f3d92eee013ab645 Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Sun, 6 Apr 2025 09:26:33 -0700
Subject: [PATCH] [alpha.webkit.ForwardDeclChecker] Recognize a forward
declared template specialization
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.
---
.../Checkers/WebKit/ForwardDeclChecker.cpp | 14 ++++++++++++--
.../Checkers/WebKit/forward-decl-checker.mm | 17 +++++++++++++++++
2 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
index 2c63224df129a..4f63e5ed866ed 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<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