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-Tag: llvmorg-11-init~12175 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e812bf5530872e4b33c8f205da81acccc2a41413;p=platform%2Fupstream%2Fllvm.git Properly detect temporary gsl::Owners through reference initialization chains. llvm-svn: 368534 --- diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index 90b8d4a..980696f 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/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/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp index 2e85a3f..6503f41 100644 --- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp +++ b/clang/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. +}