[llvm] [WinEH] Emit state stores before SEH scopes (PR #116546)

Maurice Heumann via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 17 07:31:19 PST 2024


momo5502 wrote:

```C++
#include <cstdio>

struct Destructor {
  Destructor(int* destructorCalled) : mDestructorCalled(destructorCalled) {}
  ~Destructor() { ++*mDestructorCalled; }
  int* mDestructorCalled;
};

void HandleDestructorCallWithException(int* destructorCalled, bool _throw_1)
{
  {
  Destructor x(destructorCalled);

  if(_throw_1)
    *reinterpret_cast<int*>(1) = 1;

  }
  
  *reinterpret_cast<int*>(2) = 2;
}

void CatchNativeExceptions(int* destructorCalled, bool* exceptionThrown, bool _throw_1)
{
  try {
    HandleDestructorCallWithException(destructorCalled, _throw_1);
  }
  catch(...) {
    *exceptionThrown = true;
  }
}


int main()
{
  int destructorCalled = 0;
  bool exceptionThrown = false;

  CatchNativeExceptions(&destructorCalled, &exceptionThrown, true);

  printf("Destructor called %i times\n",destructorCalled );
  puts(exceptionThrown ? "Exception thrown" : "Exception NOT thrown");

  
  destructorCalled = 0;
  exceptionThrown = false;

  CatchNativeExceptions(&destructorCalled, &exceptionThrown, false);

  printf("Destructor called %i times\n",destructorCalled );
  puts(exceptionThrown ? "Exception thrown" : "Exception NOT thrown");

  return 0;
}
```

Output using cl: 

```
Destructor called 1 times
Exception thrown
Destructor called 1 times
Exception thrown
```

Output using clang-cl with the fix here:

```
Destructor called 1 times
Exception thrown
Destructor called 2 times
Exception thrown
```

--> state restore is still needed

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


More information about the llvm-commits mailing list