[clang] [Sema] Fix lifetime extension for temporaries in range-based for loops in C++23 (PR #145164)
Marco Vitale via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 24 08:22:14 PDT 2025
mrcvtl wrote:
I’ve taken some time to better understand the code and think through the solution.
I tried using `isInLifetimeExtendingContext()`, but it still returns false, and I believe I now understand why. In your PR, the flag is set here:
https://github.com/llvm/llvm-project/blob/b581f9d056babadf55098b9d5d100271621b90db/clang/lib/Parse/ParseDecl.cpp#L2314-L2318
However, the warning is actually triggered starting from this location:
https://github.com/llvm/llvm-project/blob/b581f9d056babadf55098b9d5d100271621b90db/clang/lib/Parse/ParseDecl.cpp#L2266
An alternative approach to apply your logic and set again the flag in here:
https://github.com/llvm/llvm-project/blob/43d042b350af8ee8c7401d6b102df68d6c176b5a/clang/lib/Parse/ParseStmt.cpp#L2174-L2179
Maybe we could act on `ForRangeInfo.LifetimeExtendTemps.back()`, or even directly on `Actions.currentEvaluationContext();`. If that works, we might be able to rely solely on `isInLifetimeExtendingContext()` and remove the need for `isRangeBasedForLoopVariable` altogether.
What do you think? I'm absolutely open to every approach (the cleaner, the better!).
https://github.com/llvm/llvm-project/pull/145164
More information about the cfe-commits
mailing list