[clang] lambda and captured this (PR #182300)
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 19 07:13:30 PST 2026
https://github.com/usx95 created https://github.com/llvm/llvm-project/pull/182300
None
>From 44a15dc02102697162e51cec02fc56c508de0ffc Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <usx at google.com>
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 | 11 +++++++++++
3 files changed, 22 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..435d8e31f85f4 100644
--- a/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
+++ b/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
@@ -10,6 +10,8 @@ View definition_before_header(View a);
#ifndef TEST_HEADER_H
#define TEST_HEADER_H
+#include "Inputs/lifetime-analysis.h"
+
struct View;
struct [[gsl::Owner]] MyObj {
@@ -393,3 +395,12 @@ View Reassigned(View a) {
a = Global;
return a;
}
+
+struct NoSuggestionForThisCapturedByLambda {
+ std::string s;
+ void foo() {
+ auto x = [&]() {
+ return s.size() > 0 ? this->s.data() : s.data();
+ };
+ }
+};
More information about the cfe-commits
mailing list