[clang] [Clang] Diagnose dangling references in std::vector. (PR #111753)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 9 12:39:49 PDT 2024


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

This is a follow-up to https://github.com/llvm/llvm-project/pull/108344.

The original bailout check was overly strict, causing it to miss cases like the vector(initializer_list, allocator) constructor. This patch relaxes the check to address that issue.

Fix #111680

>From 354b33432d0dafbf54667a9ee973f5b2712c5773 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Wed, 9 Oct 2024 21:34:49 +0200
Subject: [PATCH] [clang] Diagnose the dangling references for vector.

---
 clang/lib/Sema/CheckExprLifetime.cpp             | 2 +-
 clang/test/Sema/warn-lifetime-analysis-nocfg.cpp | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index 009b8d000e6b0e..8a44a131c02cb4 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -404,7 +404,7 @@ shouldTrackFirstArgumentForConstructor(const CXXConstructExpr *Ctor) {
   if (LHSRecordDecl->hasAttr<PointerAttr>())
     return true;
 
-  if (Ctor->getConstructor()->getNumParams() != 1 ||
+  if (Ctor->getConstructor()->getNumParams() < 1 ||
       !isContainerOfPointer(LHSRecordDecl))
     return false;
 
diff --git a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
index 731639ab16a735..688f55edfe84df 100644
--- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
@@ -164,14 +164,16 @@ template<typename T>
 struct initializer_list {
   const T* ptr; size_t sz;
 };
-template <typename T>
+template<typename T> class allocator {};
+template <typename T, typename Alloc = allocator<T>>
 struct vector {
   typedef __gnu_cxx::basic_iterator<T> iterator;
   iterator begin();
   iterator end();
   const T *data() const;
   vector();
-  vector(initializer_list<T> __l);
+  vector(initializer_list<T> __l,
+         const Alloc& alloc = Alloc());
 
   template<typename InputIterator>
 	vector(InputIterator first, InputIterator __last);



More information about the cfe-commits mailing list