[clang] [LifetimeSafety] Fix use-after-scope from #198784 (PR #199455)

Thurston Dang via cfe-commits cfe-commits at lists.llvm.org
Sun May 24 15:28:53 PDT 2026


https://github.com/thurstond created https://github.com/llvm/llvm-project/pull/199455

This fixes a use-after-scope introduced by #198784 (reported in https://github.com/llvm/llvm-project/pull/198784#issuecomment-4530043621), by manually extending the lifetime.

Note that clang is built using C++17, hence C++23 P2718R0's lifetime extension in range-based for loops does not apply.

>From bd66b08db54ee368ba62cb9ecccebea7a900345d Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Sun, 24 May 2026 22:23:52 +0000
Subject: [PATCH] [LifetimeSafety] Fix use-after-scope from #198784

This fixes a use-after-scope introduced by #198784 (reported in
https://github.com/llvm/llvm-project/pull/198784#issuecomment-4530043621), by manually
extending the lifetime.

Note that clang is built using C++17, hence C++23 P2718R0's lifetime extension in
range-based for loops does not apply.
---
 clang/lib/Analysis/LifetimeSafety/Checker.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Analysis/LifetimeSafety/Checker.cpp b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
index 53899251b9643..7eb948373152b 100644
--- a/clang/lib/Analysis/LifetimeSafety/Checker.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
@@ -356,8 +356,11 @@ class LifetimeChecker {
 
     // We iterate in reverse order (from most recent to oldest) to find
     // the first declaration in each file.
-    for (const FunctionDecl *Redecl :
-         llvm::reverse(llvm::to_vector(FDef->redecls())))
+
+    // Store in temporary variable to manually extend lifetime
+    auto redecls = llvm::to_vector(FDef->redecls());
+
+    for (const FunctionDecl *Redecl : llvm::reverse(redecls))
       AddCrossTUDecl(Redecl);
 
     return Targets;



More information about the cfe-commits mailing list