[clang] [LifetimeSafety] Track a temporary materialized by a default argument (PR #239586)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 04:48:36 PST 2026
https://github.com/Xazax-hun created https://github.com/llvm/llvm-project/pull/239586
The CFG does not visit a default argument's expression (PR13385), but it does expire the temporaries it creates. Such a temporary therefore had no loan, and a view of it could dangle unreported. Issue the loan at the CXXDefaultArgExpr, so it is reported at the call.
Assisted-by: Claude Opus 5.5
>From 34d19a77073c7282ce76cd68fa218449dc04aa68 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Fri, 6 Nov 2026 12:47:05 +0000
Subject: [PATCH] [LifetimeSafety] Track a temporary materialized by a default
argument
The CFG does not visit a default argument's expression (PR13385), but it
does expire the temporaries it creates. Such a temporary therefore had no
loan, and a view of it could dangle unreported. Issue the loan at the
CXXDefaultArgExpr, so it is reported at the call.
Assisted-by: Claude Opus 5.5
---
.../Analyses/LifetimeSafety/FactsGenerator.h | 1 +
.../LifetimeSafety/FactsGenerator.cpp | 26 ++++-
clang/lib/Sema/SemaLifetimeSafety.h | 2 +
clang/test/Sema/LifetimeSafety/safety.cpp | 107 ++++++++++++++++++
4 files changed, 132 insertions(+), 4 deletions(-)
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
index 9e6f70b9e6ffe..6e92e5a8dbbe5 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
@@ -41,6 +41,7 @@ class FactsGenerator : public ConstStmtVisitor<FactsGenerator> {
void VisitDeclStmt(const DeclStmt *DS);
void VisitDeclRefExpr(const DeclRefExpr *DRE);
void VisitCXXConstructExpr(const CXXConstructExpr *CCE);
+ void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *DAE);
void VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *DIE);
void VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE);
void VisitMemberExpr(const MemberExpr *ME);
diff --git a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
index 4fe5c5a18ae41..b70c02f94d711 100644
--- a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
@@ -101,11 +101,14 @@ static const Loan *createLoan(FactManager &FactMgr, const ValueDecl &VD,
/// Creates a loan for the storage location of a temporary object.
/// \param MTE The MaterializeTemporaryExpr that represents the temporary
-/// binding. \return The new Loan.
+/// binding.
+/// \param IssueExpr The expression the loan is reported at, if not \p MTE.
+/// \return The new Loan.
static const Loan *createLoan(FactManager &FactMgr,
- const MaterializeTemporaryExpr *MTE) {
- AccessPath Path(MTE);
- return FactMgr.getLoanMgr().createLoan(Path, MTE);
+ const MaterializeTemporaryExpr *MTE,
+ const Expr *IssueExpr = nullptr) {
+ return FactMgr.getLoanMgr().createLoan(AccessPath(MTE),
+ IssueExpr ? IssueExpr : MTE);
}
/// Creates a loan for an allocation through 'new'
@@ -233,6 +236,21 @@ void FactsGenerator::VisitCXXConstructExpr(const CXXConstructExpr *CCE) {
handleFunctionCall(CCE, /*IsGslConstruction=*/false);
}
+void FactsGenerator::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *DAE) {
+ // The CFG does not visit a default argument's expression (PR13385), but it
+ // does expire the temporaries it materializes. Issue the loan here, reported
+ // at the call.
+ // FIXME: Model temporaries nested deeper in the default argument.
+ const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(
+ DAE->getExpr()->IgnoreParenBaseCasts());
+ OriginList *List = getOriginsList(*DAE);
+ if (!MTE || !List)
+ return;
+ const Loan *L = createLoan(FactMgr, MTE, /*IssueExpr=*/DAE);
+ CurrentBlockFacts.push_back(
+ FactMgr.createFact<IssueFact>(L->getID(), List->getOuterOriginID()));
+}
+
void FactsGenerator::VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *DIE) {
if (const Expr *Init = DIE->getExpr())
killAndFlowOrigin(*DIE, *Init);
diff --git a/clang/lib/Sema/SemaLifetimeSafety.h b/clang/lib/Sema/SemaLifetimeSafety.h
index 37533f6ce9fb0..8a3055920c309 100644
--- a/clang/lib/Sema/SemaLifetimeSafety.h
+++ b/clang/lib/Sema/SemaLifetimeSafety.h
@@ -753,6 +753,8 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
std::string getDiagSubjectDescription(const Expr *E) {
E = E->IgnoreImpCasts();
+ if (const auto *DAE = dyn_cast<CXXDefaultArgExpr>(E))
+ return getDiagSubjectDescription(DAE->getExpr()->IgnoreParenBaseCasts());
if (isa<MaterializeTemporaryExpr>(E))
return "temporary object";
if (isa<CXXNewExpr>(E))
diff --git a/clang/test/Sema/LifetimeSafety/safety.cpp b/clang/test/Sema/LifetimeSafety/safety.cpp
index 1dcce2398d172..e6922c58d8ded 100644
--- a/clang/test/Sema/LifetimeSafety/safety.cpp
+++ b/clang/test/Sema/LifetimeSafety/safety.cpp
@@ -2200,6 +2200,113 @@ thread_local const std::string &tl = make();
std::string_view thread_local_extension() { return tl; }
} // namespace lifetime_extended_temporary
+namespace default_argument_temporary {
+struct DerivedObj : MyObj {};
+View view(const MyObj &obj [[clang::lifetimebound]] = MyObj());
+View view_converted(const MyObj &obj [[clang::lifetimebound]] = 1);
+View view_of_base(const MyObj &obj [[clang::lifetimebound]] = DerivedObj());
+View view_by_value(View v [[clang::lifetimebound]] = MyObj());
+View view_nested(View v [[clang::lifetimebound]] = view());
+template <typename T> View view_t(const T &obj [[clang::lifetimebound]] = T());
+struct S {
+ View get(const MyObj &obj [[clang::lifetimebound]] = MyObj());
+};
+struct [[gsl::Pointer()]] DefaultedView {
+ DefaultedView(const MyObj &obj [[clang::lifetimebound]] = MyObj());
+};
+
+void use_after_call() {
+ View v;
+ v = view(); // expected-warning {{temporary object does not live long enough}} \
+ // expected-note {{temporary object is destroyed here}} \
+ // expected-note {{result of call to 'view' aliases the storage of temporary object because parameter 'obj' is marked as lifetimebound}}
+ use(v); // expected-note {{later used here}}
+}
+
+void implicit_conversion() {
+ View v;
+ v = view_converted(); // expected-warning {{temporary object does not live long enough}} \
+ // expected-note {{temporary object is destroyed here}} \
+ // expected-note {{result of call to 'view_converted' aliases the storage of temporary object because parameter 'obj' is marked as lifetimebound}}
+ use(v); // expected-note {{later used here}}
+}
+
+void derived_to_base() {
+ View v;
+ v = view_of_base(); // expected-warning {{temporary object does not live long enough}} \
+ // expected-note {{temporary object is destroyed here}} \
+ // expected-note {{result of call to 'view_of_base' aliases the storage of temporary object because parameter 'obj' is marked as lifetimebound}}
+ use(v); // expected-note {{later used here}}
+}
+
+void each_call_reported() {
+ View a, b;
+ a = view(); // expected-warning {{temporary object does not live long enough}} \
+ // expected-note {{temporary object is destroyed here}} \
+ // expected-note {{result of call to 'view' aliases the storage of temporary object because parameter 'obj' is marked as lifetimebound}}
+ b = view(); // expected-warning {{temporary object does not live long enough}} \
+ // expected-note {{temporary object is destroyed here}} \
+ // expected-note {{result of call to 'view' aliases the storage of temporary object because parameter 'obj' is marked as lifetimebound}}
+ use(a, b); // expected-note 2 {{later used here}}
+}
+
+void in_loop() {
+ View v;
+ for (int i = 0; i < 2; ++i) {
+ v = view(); // expected-warning {{temporary object does not live long enough}} \
+ // expected-note {{temporary object is destroyed here}} \
+ // expected-note {{result of call to 'view' aliases the storage of temporary object because parameter 'obj' is marked as lifetimebound}}
+ use(v); // expected-note {{later used here}}
+ }
+}
+
+void member_call(S &s) {
+ View v;
+ v = s.get(); // expected-warning {{temporary object does not live long enough}} \
+ // expected-note {{temporary object is destroyed here}} \
+ // expected-note {{result of call to 'get' aliases the storage of temporary object because parameter 'obj' is marked as lifetimebound}}
+ use(v); // expected-note {{later used here}}
+}
+
+void constructor() {
+ DefaultedView v; // expected-warning {{temporary object does not live long enough}} \
+ // expected-note {{temporary object is destroyed here}}
+ use(v); // expected-note {{later used here}}
+}
+
+void template_default() {
+ View v;
+ v = view_t<MyObj>(); // expected-warning {{temporary object does not live long enough}} \
+ // expected-note {{temporary object is destroyed here}} \
+ // expected-note {{result of call to 'view_t<MyObj>' aliases the storage of temporary object because parameter 'obj' is marked as lifetimebound}}
+ use(v); // expected-note {{later used here}}
+}
+
+View returned() {
+ return view(); // expected-warning {{stack memory associated with temporary object is returned}} \
+ // expected-note {{returned here}}
+}
+
+void used_within_full_expression() {
+ use(view());
+ for (int i = 0; i < 2; ++i)
+ use(view());
+ MyObj obj;
+ View v = view(obj);
+ use(v);
+}
+
+void not_yet_modeled() {
+ View v;
+ // FIXME: These dangle, but the temporary is nested inside the default
+ // argument.
+ v = view_by_value();
+ use(v);
+ v = view_nested();
+ use(v);
+}
+} // namespace default_argument_temporary
+
namespace field_access {
struct S {
More information about the cfe-commits
mailing list