[LLVMdev] Catching C++ exceptions, cleaning up, rethrowing

Bill Wendling wendling at apple.com
Wed Apr 11 22:50:11 PDT 2012


On Apr 11, 2012, at 8:41 AM, Paul J. Lucas wrote:

> On Apr 8, 2012, at 10:40 PM, Bill Wendling wrote:
> 
>> On Apr 8, 2012, at 8:47 AM, Paul J. Lucas wrote:
>> 
>>> But I assume [the personality function] works only for a *nix/g++ system.  My code also needs to work when compiled on Windows using Visual Studio and I'm guessing that it needs a different personality function.  Where do I get that?
>> 
>> That's not something I'm familiar with. You may be able to use the same personality function, but I know that Windows also uses SEH for its exception handling scheme. This is not supported by LLVM.
>> 
>> My suggestion is to use clang to compile a program with exceptions on your Windows box and then use the personality function that it generates.
> 
> Assuming I do that and it works, wouldn't IR code generated on Windows then be incompatible with IR-code-compiled-to-machine-code on *nix (because it would use the wrong personality function)?
> 
I suggested this only so that you can then copy what personality function declaration it uses. :-)

> Doing all the grunt work of emitting landingpads, resumes, having to get the right personality function, etc., seems a lot more work that my original proposal of catching C++ exceptions in my C thunk functions and calling a linked-list of destructors to run myself.  This also has the benefit of not having to deal with platform-specific personality functions.
> 
> I realize I'm a newb and there may be something subtle I'm not getting, but I have to ask: why is doing the work in IR "better"?
> 
What you seem to be proposing is code that's already handled via the personality function and the unwind library. I'm trying to save you from duplicating that effort. But I also can't stop you from going along with your scheme. 

Keep in mind that catching a C++ exception is a non-trivial matter. You would have to ensure that it has the same semantics as the existing DWARF implementation. E.g., running cleanups on all of the functions up the stack from where it was thrown to where it was caught (a two-phase process). And that's just the tip of the iceberg.

It *sounds* like I'm asking you to do a lot, but that's the nature of IR. You should eventually find that it isn't a lot of work to implement. :-)

-bw




More information about the llvm-dev mailing list