-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[alpha.webkit.ForwardDeclChecker] Recognize a forward declared template specialization #134545
[alpha.webkit.ForwardDeclChecker] Recognize a forward declared template specialization #134545
Conversation
…te 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.
@llvm/pr-subscribers-clang-static-analyzer-1 Author: Ryosuke Niwa (rniwa) ChangesThis 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:
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 };
+};
+
+}
|
@llvm/pr-subscribers-clang Author: Ryosuke Niwa (rniwa) ChangesThis 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:
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 };
+};
+
+}
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Thanks for the review! |
…te specialization (llvm#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.
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.