[CIR] Fix bugs with array new in a ternary expression (#198869)
While attempting to compile Clang with CIR enabled, we ran into a
problem with the cleanup handling for array new inside of a ternary
operation. The reduced test case ended up showing different failure
modes with and without exceptions enabled, but it failed in both cases.
With exceptions enabled, we were failing to spill the value to be
returned and reload it outside of the cleanup scope. With exceptions
disabled, we were pushing an effectively empty entry on the EH stack and
later asserting when trying to pop it.
This change fixes the spill and reload problem by inserting a
RunCleanupsScope RAII object around the ternary branch generation code
with a call to forceCleanups to generate the spill and reload.
The assertion failure is fixed by replacing the assert call with code to
safely handle the condition of having an empty cleanup on the EH stack.
We should probably modify the code that pushes EH-only cleanups to avoid
doing so when exceptions are disabled, but that's a more extensive
change and having the new handler as a safe backstop seems like a good
idea.
Manual inspection revealed a second location where we're generating a
cir.ternary operation that had the same spill and reload problem
described above, so that is fixed here too.
Assisted-by: Cursor / claude-opus-4.7-thinking-xhigh