[clang] [clang] Fix -Wdangling false negative regressions caused by 117315 (PR #118088)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 29 04:41:24 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Haojian Wu (hokein)
<details>
<summary>Changes</summary>
A specialization declaration can have an attribute even if the primary template does not, particularly when the specialization is instantiated from an annotated using-alias declaration.
Fix #<!-- -->118064
---
Full diff: https://github.com/llvm/llvm-project/pull/118088.diff
2 Files Affected:
- (modified) clang/lib/Sema/CheckExprLifetime.cpp (+16-2)
- (modified) clang/test/Sema/warn-lifetime-analysis-nocfg.cpp (+23)
``````````diff
diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index 15ceff873693a5..86a5fa662aed0e 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -258,9 +258,23 @@ template <typename T> static bool isRecordWithAttr(QualType Type) {
auto *RD = Type->getAsCXXRecordDecl();
if (!RD)
return false;
+ // Generally, if a primary template class declaration is annotated with an
+ // attribute, all its specializations generated from template instantiations
+ // should inherit the attribute.
+ //
+ // However, since lifetime analysis occurs during parsing, we may encounter
+ // cases where a full definition of the specialization is not required. In
+ // such cases, the specialization declaration remains incomplete and lacks the
+ // attribute. Therefore, we fall back to checking the primary template class.
+ //
+ // Note: it is possible for a specialization declaration to have an attribute
+ // even if the primary template does not.
+ bool Result = RD->hasAttr<T>();
+
if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
- RD = CTSD->getSpecializedTemplate()->getTemplatedDecl();
- return RD->hasAttr<T>();
+ Result |= (bool)CTSD->getSpecializedTemplate()->getTemplatedDecl();
+
+ return Result;
}
bool isPointerLikeType(QualType QT) {
diff --git a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
index c18ecd86ad06f0..fc876926ba2e63 100644
--- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
@@ -670,3 +670,26 @@ void test13() {
}
} // namespace GH100526
+
+namespace std {
+template <typename T>
+class __set_iterator {};
+
+template<typename T>
+struct BB {
+ typedef __set_iterator<T> iterator;
+};
+
+template <typename T>
+class set {
+public:
+ typedef typename BB<T>::iterator iterator;
+ iterator begin() const;
+};
+} // namespace std
+namespace GH118064{
+
+void test() {
+ auto y = std::set<int>{}.begin(); // expected-warning {{object backing the pointer}}
+}
+} // namespace GH118064
``````````
</details>
https://github.com/llvm/llvm-project/pull/118088
More information about the cfe-commits
mailing list