[clang] [clang] Fix -Wdangling false negative regressions caused by 117315 (PR #118088)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 29 04:40:46 PST 2024


https://github.com/hokein created https://github.com/llvm/llvm-project/pull/118088

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

>From b165ad7accfc746210c5ca894b488553124253ed Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Fri, 29 Nov 2024 13:35:56 +0100
Subject: [PATCH] [clang] Fix a regression.

---
 clang/lib/Sema/CheckExprLifetime.cpp          | 18 +++++++++++++--
 .../Sema/warn-lifetime-analysis-nocfg.cpp     | 23 +++++++++++++++++++
 2 files changed, 39 insertions(+), 2 deletions(-)

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



More information about the cfe-commits mailing list