r368528 - Properly handle reference initialization when detecting gsl::Pointer initialization chains

Gabor Horvath via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 11 01:05:29 PDT 2019


Author: xazax
Date: Sun Aug 11 01:05:28 2019
New Revision: 368528

URL: http://llvm.org/viewvc/llvm-project?rev=368528&view=rev
Log:
Properly handle reference initialization when detecting gsl::Pointer initialization chains

Modified:
    cfe/trunk/lib/Sema/SemaInit.cpp
    cfe/trunk/test/Sema/warn-lifetime-analysis-nocfg.cpp

Modified: cfe/trunk/lib/Sema/SemaInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=368528&r1=368527&r2=368528&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Sun Aug 11 01:05:28 2019
@@ -7079,8 +7079,12 @@ static SourceRange nextPathEntryRange(co
 }
 
 static bool pathOnlyInitializesGslPointer(IndirectLocalPath &Path) {
-  return !Path.empty() &&
-         Path.back().Kind == IndirectLocalPathEntry::GslPointerInit;
+  for (auto It = Path.rbegin(), End = Path.rend(); It != End; ++It) {
+    if (It->Kind == IndirectLocalPathEntry::VarInit)
+      continue;
+    return It->Kind == IndirectLocalPathEntry::GslPointerInit;
+  }
+  return false;
 }
 
 void Sema::checkInitializerLifetime(const InitializedEntity &Entity,
@@ -7109,8 +7113,8 @@ void Sema::checkInitializerLifetime(cons
     // a local or temporary owner or the address of a local variable/param. We
     // do not want to follow the references when returning a pointer originating
     // from a local owner to avoid the following false positive:
-    //   int &p = *localOwner;
-    //   someContainer.add(std::move(localOwner));
+    //   int &p = *localUniquePtr;
+    //   someContainer.add(std::move(localUniquePtr));
     //   return p;
     if (!IsTempGslOwner && pathOnlyInitializesGslPointer(Path) &&
         !(IsLocalGslOwner && !pathContainsInit(Path)))
@@ -7217,7 +7221,7 @@ void Sema::checkInitializerLifetime(cons
         if (pathContainsInit(Path))
           return false;
 
-        // Suppress false positives for code like the below:
+        // Suppress false positives for code like the one below:
         //   Ctor(unique_ptr<T> up) : member(*up), member2(move(up)) {}
         if (IsLocalGslOwner && pathOnlyInitializesGslPointer(Path))
           return false;

Modified: cfe/trunk/test/Sema/warn-lifetime-analysis-nocfg.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-lifetime-analysis-nocfg.cpp?rev=368528&r1=368527&r2=368528&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-lifetime-analysis-nocfg.cpp (original)
+++ cfe/trunk/test/Sema/warn-lifetime-analysis-nocfg.cpp Sun Aug 11 01:05:28 2019
@@ -130,7 +130,7 @@ typename remove_reference<T>::type &&mov
 template <typename T>
 struct basic_iterator {
   basic_iterator operator++();
-  T& operator*();
+  T& operator*() const;
 };
 
 template<typename T>
@@ -227,8 +227,16 @@ const char *trackThroughMultiplePointer(
 }
 
 struct X {
-  X(std::unique_ptr<int> up) : pointee(*up), pointer(std::move(up)) {}
-
+  X(std::unique_ptr<int> up) :
+    pointee(*up), pointee2(up.get()), pointer(std::move(up)) {}
   int &pointee;
+  int *pointee2;
   std::unique_ptr<int> pointer;
 };
+
+std::vector<int>::iterator getIt();
+
+const int &handleGslPtrInitsThroughReference(const std::vector<int> &v) {
+  const auto &it = getIt(); // Ok, it is lifetime extended.
+  return *it;
+}




More information about the cfe-commits mailing list