[clang] b598dcb - [LifetimeSafety] Add support for derived-to-base conversions (#175631)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 13 00:12:48 PST 2026
Author: Utkarsh Saxena
Date: 2026-01-13T13:42:42+05:30
New Revision: b598dcbd757e18f257552d3208ab9734ef40d86d
URL: https://github.com/llvm/llvm-project/commit/b598dcbd757e18f257552d3208ab9734ef40d86d
DIFF: https://github.com/llvm/llvm-project/commit/b598dcbd757e18f257552d3208ab9734ef40d86d.diff
LOG: [LifetimeSafety] Add support for derived-to-base conversions (#175631)
Add support for derived-to-base conversions in lifetime analysis.
Added handling for `CK_UncheckedDerivedToBase` and `CK_DerivedToBase` cast kinds in the `FactsGenerator::VisitImplicitCastExpr` method. These cast kinds are now treated similarly to other conversions by flowing origins from source to destination.
Added a unit test `DerivedToBaseThisArg` that verifies lifetime information is correctly propagated through derived-to-base conversions when using member functions inherited from a base class.
Added:
Modified:
clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
clang/unittests/Analysis/LifetimeSafetyTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
index f3993b7e7e261..60b0bf7e1bdac 100644
--- a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
@@ -219,6 +219,8 @@ void FactsGenerator::VisitImplicitCastExpr(const ImplicitCastExpr *ICE) {
case CK_NoOp:
case CK_ConstructorConversion:
case CK_UserDefinedConversion:
+ case CK_UncheckedDerivedToBase:
+ case CK_DerivedToBase:
flow(Dest, SrcList, /*Kill=*/true);
return;
case CK_FunctionToPointerDecay:
diff --git a/clang/unittests/Analysis/LifetimeSafetyTest.cpp b/clang/unittests/Analysis/LifetimeSafetyTest.cpp
index b1f74a7a2c850..5c892b656d5c8 100644
--- a/clang/unittests/Analysis/LifetimeSafetyTest.cpp
+++ b/clang/unittests/Analysis/LifetimeSafetyTest.cpp
@@ -1778,5 +1778,28 @@ TEST_F(LifetimeAnalysisTest, TrackFirstArgument_StdAnyCast) {
EXPECT_THAT(Origin("r"), HasLoansTo({"a"}, "p1"));
}
+TEST_F(LifetimeAnalysisTest, DerivedToBaseThisArg) {
+ SetupTest(R"(
+ template <typename T>
+ struct OperatorBase {
+ const T& value() const& [[clang::lifetimebound]];
+ const T&& value() const&& [[clang::lifetimebound]];
+ };
+
+ template <typename T>
+ class StatusOr : private OperatorBase<T> {
+ public:
+ using StatusOr::OperatorBase::value;
+ };
+
+ void target() {
+ View view;
+ StatusOr<MyObj> my_obj_or;
+ view = my_obj_or.value();
+ POINT(p1);
+ }
+ )");
+ EXPECT_THAT(Origin("view"), HasLoansTo({"my_obj_or"}, "p1"));
+}
} // anonymous namespace
} // namespace clang::lifetimes::internal
More information about the cfe-commits
mailing list