From: Gabor Horvath Date: Mon, 12 Aug 2019 16:19:39 +0000 (+0000) Subject: Fix multiple lifetime warning messages for range based for loop X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=16b3be6701750d9b084bb9bbb6bfb0e2c0f43f16;p=clang Fix multiple lifetime warning messages for range based for loop git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@368588 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index 2be4e897c8..17cfd12932 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -7070,8 +7070,11 @@ static SourceRange nextPathEntryRange(const IndirectLocalPath &Path, unsigned I, // supporting lifetime extension. break; - case IndirectLocalPathEntry::DefaultInit: case IndirectLocalPathEntry::VarInit: + if (cast(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; } diff --git a/test/Sema/warn-lifetime-analysis-nocfg.cpp b/test/Sema/warn-lifetime-analysis-nocfg.cpp index 6503f41259..696c04b237 100644 --- a/test/Sema/warn-lifetime-analysis-nocfg.cpp +++ b/test/Sema/warn-lifetime-analysis-nocfg.cpp @@ -209,6 +209,13 @@ void danglingReferenceFromTempOwner() { std::vector getTempVec(); std::optional> 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 &v) { std::vector::iterator it = v.begin(); int& value = *it;