[clang] 2c5c11b - [LifetimeSafety] Model an explicit `a.operator=(b)` call as an assignment (#244607)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 10:49:46 PST 2026


Author: Gábor Horváth
Date: 2026-11-23T18:49:40Z
New Revision: 2c5c11b6bf899f28fa828ad5c8373e596fc7acdd

URL: https://github.com/llvm/llvm-project/commit/2c5c11b6bf899f28fa828ad5c8373e596fc7acdd
DIFF: https://github.com/llvm/llvm-project/commit/2c5c11b6bf899f28fa828ad5c8373e596fc7acdd.diff

LOG: [LifetimeSafety] Model an explicit `a.operator=(b)` call as an assignment (#244607)

`a.operator=(b)` is a member call rather than an operator call, so the
analysis treated it as an ordinary call and lost the stored borrow.
Share the operator= handling between the two spellings.

Assisted-by: Claude Opus 5.5

---------

Co-authored-by: Gabor Horvath <gaborh at apple.com>

Added: 
    

Modified: 
    clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
    clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
    clang/test/Sema/LifetimeSafety/safety.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
index 4262a3e0412113..5734f394548f65 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
@@ -85,6 +85,12 @@ class FactsGenerator : public ConstStmtVisitor<FactsGenerator> {
   void handleAssignment(const Expr *TargetExpr, const Expr *LHSExpr,
                         const Expr *RHSExpr);
 
+  /// Models a call to an overloaded `operator=`, spelled `a = b` or
+  /// `a.operator=(b)`, as an assignment. Returns true if the call was modeled
+  /// as an assignment.
+  bool handleAssignmentOperatorCall(const CallExpr *CE, const Expr *LHSExpr,
+                                    const Expr *RHSExpr);
+
   void handlePointerArithmetic(const BinaryOperator *BO);
 
   bool handlePlacementNew(const CXXNewExpr *NE, OriginList *NewList);

diff  --git a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
index 99a32161b40924..a770373709a98d 100644
--- a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
@@ -290,6 +290,13 @@ void FactsGenerator::VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE) {
     handleFunctionCall(MCE, /*IsGslConstruction=*/true);
     return;
   }
+  // `a.operator=(b)` is `a = b`. For `p->operator=(b)` the object is `*p`.
+  if (const auto *ME = dyn_cast<MemberExpr>(MCE->getCallee());
+      ME && !ME->isArrow() &&
+      MCE->getMethodDecl()->getOverloadedOperator() == OO_Equal &&
+      handleAssignmentOperatorCall(MCE, MCE->getImplicitObjectArgument(),
+                                   MCE->getArg(0)))
+    return;
   handleFunctionCall(MCE, /*IsGslConstruction=*/false);
 }
 
@@ -605,34 +612,41 @@ void FactsGenerator::VisitAbstractConditionalOperator(
          FBPred);
 }
 
+bool FactsGenerator::handleAssignmentOperatorCall(const CallExpr *CE,
+                                                  const Expr *LHSExpr,
+                                                  const Expr *RHSExpr) {
+  QualType LHSTy = LHSExpr->getType();
+  if (!hasOrigins(LHSTy))
+    return false;
+  // Pointer-like types: assignment inherently propagates origins.
+  if (LHSTy->isPointerOrReferenceType() || isGslPointerType(LHSTy) ||
+      isGslOwnerType(LHSTy)) {
+    handleAssignment(CE, LHSExpr, RHSExpr);
+    return true;
+  }
+  // Standard library callable wrappers (e.g., std::function) can propagate
+  // the stored lambda's origins.
+  if (const auto *RD = LHSTy->getAsCXXRecordDecl();
+      RD && isStdCallableWrapperType(RD)) {
+    handleAssignment(CE, LHSExpr, RHSExpr);
+    return true;
+  }
+  // Other tracked types: only defaulted operator= propagates origins.
+  // User-defined operator= has opaque semantics, so don't handle them now.
+  if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(CE->getDirectCallee());
+      MD && MD->isDefaulted()) {
+    handleAssignment(CE, LHSExpr, RHSExpr);
+    return true;
+  }
+  return false;
+}
+
 void FactsGenerator::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *OCE) {
   // Assignment operators have special "kill-then-propagate" semantics
   // and are handled separately.
   if (OCE->getOperator() == OO_Equal && OCE->getNumArgs() == 2 &&
-      hasOrigins(OCE->getArg(0)->getType())) {
-    // Pointer-like types: assignment inherently propagates origins.
-    QualType LHSTy = OCE->getArg(0)->getType();
-    if (LHSTy->isPointerOrReferenceType() || isGslPointerType(LHSTy) ||
-        isGslOwnerType(LHSTy)) {
-      handleAssignment(OCE, OCE->getArg(0), OCE->getArg(1));
-      return;
-    }
-    // Standard library callable wrappers (e.g., std::function) can propagate
-    // the stored lambda's origins.
-    if (const auto *RD = LHSTy->getAsCXXRecordDecl();
-        RD && isStdCallableWrapperType(RD)) {
-      handleAssignment(OCE, OCE->getArg(0), OCE->getArg(1));
-      return;
-    }
-    // Other tracked types: only defaulted operator= propagates origins.
-    // User-defined operator= has opaque semantics, so don't handle them now.
-    if (const auto *MD =
-            dyn_cast_or_null<CXXMethodDecl>(OCE->getDirectCallee());
-        MD && MD->isDefaulted()) {
-      handleAssignment(OCE, OCE->getArg(0), OCE->getArg(1));
-      return;
-    }
-  }
+      handleAssignmentOperatorCall(OCE, OCE->getArg(0), OCE->getArg(1)))
+    return;
 
   handleFunctionCall(OCE);
 }

diff  --git a/clang/test/Sema/LifetimeSafety/safety.cpp b/clang/test/Sema/LifetimeSafety/safety.cpp
index faedd948af4437..077c5de9648cbf 100644
--- a/clang/test/Sema/LifetimeSafety/safety.cpp
+++ b/clang/test/Sema/LifetimeSafety/safety.cpp
@@ -67,6 +67,54 @@ void simple_case_gsl() {
   v.use();      // expected-note {{later used here}}
 }
 
+void explicit_operator_assign_call() {
+  View v;
+  {
+    MyObj s;
+    v.operator=(s); // expected-warning {{local variable 's' does not live long enough}}
+  }                 // expected-note {{local variable 's' is destroyed here}}
+  v.use();          // expected-note {{later used here}}
+}
+
+void explicit_operator_assign_call_string_view() {
+  std::string_view v;
+  {
+    std::string s;
+    v.operator=(s); // expected-warning {{local variable 's' does not live long enough}}
+  }                 // expected-note {{local variable 's' is destroyed here}}
+  (void)v.size();   // expected-note {{later used here}}
+}
+
+void explicit_operator_assign_call_kills_old_loan() {
+  MyObj outer;
+  View v;
+  {
+    MyObj s;
+    v = s;
+  }
+  v.operator=(outer);
+  v.use(); // no-warning
+}
+
+View explicit_operator_assign_call_result_returned() {
+  MyObj s;
+  View v;
+  return v.operator=(s); // expected-warning {{stack memory associated with local variable 's' is returned}} \
+                         // expected-note {{result of call to 'operator=' aliases the storage of local variable 's'}} \
+                         // expected-note {{returned here}}
+}
+
+void explicit_operator_assign_call_through_arrow() {
+  View v;
+  View *pv = &v;
+  {
+    MyObj s;
+    // FIXME: Model the store through the pointer.
+    pv->operator=(s);
+  }
+  pv->use();
+}
+
 void no_use_no_error() {
   MyObj* p;
   {
@@ -3255,6 +3303,16 @@ void user_defined_assignment_should_not_assume_origin_propagation() {
   use(dst);
 }
 
+void user_defined_explicit_assignment_should_not_assume_origin_propagation() {
+  SWithUserDefinedCopyLikeOps dst;
+  {
+    std::string str{"abc"};
+    SWithUserDefinedCopyLikeOps src = getSWithUserDefinedCopyLikeOps(str);
+    dst.operator=(src);
+  }
+  use(dst);
+}
+
 const S &getRef(const std::string &s [[clang::lifetimebound]]);
 
 S from_ref() {


        


More information about the cfe-commits mailing list