[llvm] [clang-tools-extra] [clang] [codegen] Emit cleanups for lifetime-extended temporaries when an expr contains control-flow (PR #80698)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 5 13:58:29 PST 2024


efriedma-quic wrote:

If I understand correctly, a "lifetime-extended" cleanup deals with the case of a temporary whose lifetime continues beyond the expression.  In other words, it has different lifetimes depending on how you exit the expression: if the variable's lifetime begins, it lasts until the end of that variable's lifetime.  Otherwise, it only lasts until the end of the full-expression.  Due to the way it's structured, the current mechanism only works correctly with thrown exceptions; it doesn't work with other ways of exiting an expression.  This patch attempts to fix that.

Passing "0" to AddLifetimeExtendedCleanups() seems wrong.  For example, suppose you have the following:

```
int foo();
struct Printy {
  ~Printy() { }
};
struct Printies {
  const Printy &a;
  const Printy &b;
  ~Printies() {}
};
void f() {
  Printies p2{
    Printy(), 
    ({
      for (int i = 0; i < 10; ++i) {
        Printies p3{
          Printy(),
          ({
          if(foo()) {
            break;
          }
          Printy();
        })};
      }
      Printy();
    })};
}
```

The "break" needs to run some of the cleanups associated with lifetime-extended temporaries... but not all of them.

We might want to consider making the whole process of lifetime extensions a bit more explicit.  Currently, we lazily construct the set of temporaries as we evaluate the initializer, then reconstruct the cleanup stack at the end of the full-expression to reflect the lifetime extension.  Instead, we could have some separate code to compute the list of lifetime-extended temporaries associated with a given variable, and create cleanups for them before we start emitting code for the initializer.  That way, we don't need to mess with the cleanup stack later; we just need to make sure we correctly skip executing the cleanups which aren't supposed to run.

See also #12658.

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


More information about the cfe-commits mailing list