]> granicus.if.org Git - clang/commitdiff
Fix multiple lifetime warning messages for range based for loop
authorGabor Horvath <xazax.hun@gmail.com>
Mon, 12 Aug 2019 16:19:39 +0000 (16:19 +0000)
committerGabor Horvath <xazax.hun@gmail.com>
Mon, 12 Aug 2019 16:19:39 +0000 (16:19 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@368588 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 2be4e897c8ee67e277bc4f74333e089cf8914f5c..17cfd129324f2863bf06214ca1b28fad37ec0965 100644 (file)
@@ -7070,8 +7070,11 @@ static SourceRange nextPathEntryRange(const IndirectLocalPath &Path, unsigned I,
       // supporting lifetime extension.
       break;
 
-    case IndirectLocalPathEntry::DefaultInit:
     case IndirectLocalPathEntry::VarInit:
+      if (cast<VarDecl>(Path[I].D)->isImplicit())
+        return SourceRange();
+      LLVM_FALLTHROUGH;
+    case IndirectLocalPathEntry::DefaultInit:
       return Path[I].E->getSourceRange();
     }
   }
@@ -7138,7 +7141,7 @@ void Sema::checkInitializerLifetime(const InitializedEntity &Entity,
         return false;
       }
 
-      if (IsGslPtrInitWithGslTempOwner) {
+      if (IsGslPtrInitWithGslTempOwner && DiagLoc.isValid()) {
         Diag(DiagLoc, diag::warn_dangling_lifetime_pointer) << DiagRange;
         return false;
       }
index 6503f4125998898df2585d3e7974b3e5be765a16..696c04b237d82d9e43f50fa6dd5ecf90fd0f114c 100644 (file)
@@ -209,6 +209,13 @@ void danglingReferenceFromTempOwner() {
 std::vector<int> getTempVec();
 std::optional<std::vector<int>> getTempOptVec();
 
+void testLoops() {
+  for (auto i : getTempVec()) // ok
+    ;
+  for (auto i : *getTempOptVec()) // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
+    ;
+}
+
 int &usedToBeFalsePositive(std::vector<int> &v) {
   std::vector<int>::iterator it = v.begin();
   int& value = *it;