From: Gabor Horvath Date: Sun, 11 Aug 2019 14:39:42 +0000 (+0000) Subject: Properly detect temporary gsl::Owners through reference initialization chains. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c9a615ed810c08ac0190a589f2c658433d726728;p=clang Properly detect temporary gsl::Owners through reference initialization chains. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@368534 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index 90b8d4a30d..980696f421 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -7104,7 +7104,8 @@ void Sema::checkInitializerLifetime(const InitializedEntity &Entity, SourceLocation DiagLoc = DiagRange.getBegin(); auto *MTE = dyn_cast(L); - bool IsTempGslOwner = MTE && isRecordWithAttr(MTE->getType()); + bool IsTempGslOwner = MTE && !MTE->getExtendingDecl() && + isRecordWithAttr(MTE->getType()); bool IsLocalGslOwner = isa(L) && isRecordWithAttr(L->getType()); diff --git a/test/Sema/warn-lifetime-analysis-nocfg.cpp b/test/Sema/warn-lifetime-analysis-nocfg.cpp index 2e85a3f6f7..6503f41259 100644 --- a/test/Sema/warn-lifetime-analysis-nocfg.cpp +++ b/test/Sema/warn-lifetime-analysis-nocfg.cpp @@ -141,7 +141,7 @@ struct vector { typedef basic_iterator iterator; iterator begin(); iterator end(); - T *data(); + const T *data() const; T &at(int n); }; @@ -235,8 +235,14 @@ struct X { }; std::vector::iterator getIt(); +std::vector getVec(); -const int &handleGslPtrInitsThroughReference(const std::vector &v) { +const int &handleGslPtrInitsThroughReference() { const auto &it = getIt(); // Ok, it is lifetime extended. return *it; } + +void handleGslPtrInitsThroughReference2() { + const std::vector &v = getVec(); + const int *val = v.data(); // Ok, it is lifetime extended. +}