https://github.com/usx95 updated https://github.com/llvm/llvm-project/pull/182300
>From 057b865093cc2165046404e9d89701c42554a722 Mon Sep 17 00:00:00 2001 From: Utkarsh Saxena <[email protected]> Date: Thu, 19 Feb 2026 14:50:25 +0000 Subject: [PATCH] lambda and captured this --- .../lib/Analysis/LifetimeSafety/FactsGenerator.cpp | 5 +++-- clang/lib/Analysis/LifetimeSafety/Origins.cpp | 13 ++++++++----- .../test/Sema/warn-lifetime-safety-suggestions.cpp | 10 ++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp index 0b25a6a401e08..6983c5a8c6015 100644 --- a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp +++ b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp @@ -711,8 +711,9 @@ llvm::SmallVector<Fact *> FactsGenerator::issuePlaceholderLoans() { return {}; llvm::SmallVector<Fact *> PlaceholderLoanFacts; - if (const auto *MD = dyn_cast<CXXMethodDecl>(FD); MD && MD->isInstance()) { - OriginList *List = *FactMgr.getOriginMgr().getThisOrigins(); + if (auto ThisOrigins = FactMgr.getOriginMgr().getThisOrigins()) { + OriginList *List = *ThisOrigins; + const auto *MD = dyn_cast<CXXMethodDecl>(FD); const PlaceholderLoan *L = FactMgr.getLoanMgr().createLoan<PlaceholderLoan>(MD); PlaceholderLoanFacts.push_back( diff --git a/clang/lib/Analysis/LifetimeSafety/Origins.cpp b/clang/lib/Analysis/LifetimeSafety/Origins.cpp index 9141859a81345..f1cb9dac05bd2 100644 --- a/clang/lib/Analysis/LifetimeSafety/Origins.cpp +++ b/clang/lib/Analysis/LifetimeSafety/Origins.cpp @@ -88,9 +88,13 @@ bool doesDeclHaveStorage(const ValueDecl *D) { } OriginManager::OriginManager(ASTContext &AST, const Decl *D) : AST(AST) { - if (const auto *MD = llvm::dyn_cast_or_null<CXXMethodDecl>(D); - MD && MD->isInstance()) - ThisOrigins = buildListForType(MD->getThisType(), MD); + // Create OriginList for 'this' expr. + const auto *MD = llvm::dyn_cast_or_null<CXXMethodDecl>(D); + if (!MD || !MD->isInstance()) + return; + if (const CXXRecordDecl *P = MD->getParent(); P && P->isLambda()) + return; + ThisOrigins = buildListForType(MD->getThisType(), MD); } OriginList *OriginManager::createNode(const ValueDecl *D, QualType QT) { @@ -146,8 +150,7 @@ OriginList *OriginManager::getOrCreateList(const Expr *E) { QualType Type = E->getType(); // Special handling for 'this' expressions to share origins with the method's // implicit object parameter. - if (llvm::isa<CXXThisExpr>(E)) { - assert(ThisOrigins && "origins for 'this' should be set for a method decl"); + if (isa<CXXThisExpr>(E) && ThisOrigins) { return *ThisOrigins; } diff --git a/clang/test/Sema/warn-lifetime-safety-suggestions.cpp b/clang/test/Sema/warn-lifetime-safety-suggestions.cpp index 1887b2f257226..4bd8a717d9d30 100644 --- a/clang/test/Sema/warn-lifetime-safety-suggestions.cpp +++ b/clang/test/Sema/warn-lifetime-safety-suggestions.cpp @@ -393,3 +393,13 @@ View Reassigned(View a) { a = Global; return a; } + +struct NoSuggestionForThisCapturedByLambda { + MyObj s; + bool cond; + void foo() { + auto x = [&]() { + return cond > 0 ? &this->s : &s; + }; + } +}; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
