[clang] [LifetimeSafety] Model scope-exit destructor and cleanup callback as a use (PR #204650)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 22 08:33:22 PDT 2026


================
@@ -807,6 +786,19 @@ void FactsGenerator::handleLifetimeEnds(const CFGLifetimeEnds &LifetimeEnds) {
       ExpiredOID));
 }
 
+void FactsGenerator::handleCleanupFunction(
+    const CFGCleanupFunction &CleanupFunction) {
+  // A variable with __attribute__((cleanup(fn))) has fn(&var) called at scope
+  // exit; like a non-trivial destructor, that callback may read a borrow the
+  // variable holds, so model it as a use of the variable.
+  const VarDecl *VD = CleanupFunction.getVarDecl();
+  if (!VD || !hasOrigins(VD->getType()))
----------------
Xazax-hun wrote:

This is tricky, a cleanup function is taking the object by address, so it can do anything with it, e.g., reading any fields. So it should be considered a use even if it is an owner. 

https://github.com/llvm/llvm-project/pull/204650


More information about the cfe-commits mailing list