xazax.hun created this revision.
xazax.hun added reviewers: rsmith, mgehre.
xazax.hun added a project: clang.
Herald added subscribers: Charusso, gamesh411, Szelethus, dkrupp, rnkovacs.
xazax.hun requested review of this revision.
Herald added a subscriber: cfe-commits.

After some interaction between the statement local lifetime analysis and some 
newly introduced warnings 
(https://github.com/llvm/llvm-project/commit/2177e4555ab84771c611a3295ab1149af7f79c29),
 a new false positive pattern appeared.  This patch updates the logic that is 
responsible to make sure we only warn for a returned `GslPointer` when it was 
initialized from a value with a local owner.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97605

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/warn-lifetime-analysis-nocfg.cpp


Index: clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
===================================================================
--- clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
+++ clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
@@ -171,12 +171,22 @@
   const T *begin() const;
 };
 
+template<class _Mystr> struct iter {
+    iter& operator-=(int);
+
+    iter operator-(int _Off) const {
+        iter _Tmp = *this;
+        return _Tmp -= _Off;
+    }
+};
+
 template<typename T>
 struct basic_string {
   basic_string();
   basic_string(const T *);
   const T *c_str() const;
   operator basic_string_view<T> () const;
+  using const_iterator = iter<T>;
 };
 
 
@@ -455,3 +465,8 @@
   std::vector<std::vector<int>::iterator> iters;
   return iters.at(0);
 }
+
+void testForBug49342()
+{
+  auto it = std::iter<char>{} - 2; // Used to be false positive.
+}
Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -7521,6 +7521,8 @@
       continue;
     if (It->Kind == IndirectLocalPathEntry::AddressOf)
       continue;
+    if (It->Kind == IndirectLocalPathEntry::LifetimeBoundCall)
+      continue;
     return It->Kind == IndirectLocalPathEntry::GslPointerInit ||
            It->Kind == IndirectLocalPathEntry::GslReferenceInit;
   }


Index: clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
===================================================================
--- clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
+++ clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
@@ -171,12 +171,22 @@
   const T *begin() const;
 };
 
+template<class _Mystr> struct iter {
+    iter& operator-=(int);
+
+    iter operator-(int _Off) const {
+        iter _Tmp = *this;
+        return _Tmp -= _Off;
+    }
+};
+
 template<typename T>
 struct basic_string {
   basic_string();
   basic_string(const T *);
   const T *c_str() const;
   operator basic_string_view<T> () const;
+  using const_iterator = iter<T>;
 };
 
 
@@ -455,3 +465,8 @@
   std::vector<std::vector<int>::iterator> iters;
   return iters.at(0);
 }
+
+void testForBug49342()
+{
+  auto it = std::iter<char>{} - 2; // Used to be false positive.
+}
Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -7521,6 +7521,8 @@
       continue;
     if (It->Kind == IndirectLocalPathEntry::AddressOf)
       continue;
+    if (It->Kind == IndirectLocalPathEntry::LifetimeBoundCall)
+      continue;
     return It->Kind == IndirectLocalPathEntry::GslPointerInit ||
            It->Kind == IndirectLocalPathEntry::GslReferenceInit;
   }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D97605: [Lifetimes... Gábor Horváth via Phabricator via cfe-commits

Reply via email to