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)
llvm-svn: 368588

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

index 2be4e89..17cfd12 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 6503f41..696c04b 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;