[clang] [alpha.webkit.ForwardDeclChecker] Recognize a forward declared template specialization (PR #134545)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 6 09:30:17 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Ryosuke Niwa (rniwa)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/134545.diff
2 Files Affected:
- (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp (+12-2)
- (modified) clang/test/Analysis/Checkers/WebKit/forward-decl-checker.mm (+17)
``````````diff
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 };
+};
+
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/134545
More information about the cfe-commits
mailing list