[PATCH] D97605: [Lifetimes] Fix false positive warning from BUG 49342

Gábor Horváth via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 26 18:59:16 PST 2021


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;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97605.326869.patch
Type: text/x-patch
Size: 1358 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210227/dce1a648/attachment-0001.bin>


More information about the cfe-commits mailing list