maybourne
maybourne

Reputation: 43

Does Clang support SEH?

Edit: I don't mean SEH I mean asynchronous exception handling. As commented SEH is supported by __try, __except. The following example requires asynchronous exception handling (-EHa) which appears to be not available.

I tried the structured exception handling (SEH) example given by Microsoft here:

https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/set-se-translator?view=msvc-170

reproduced below with #pragmas removed (my version is native only).

It works with the Microsoft compiler (cl) and the Intel classic compiler (icl) and has done so for a decade or two.

It fails with clang-cl (version 14.0.5) and the Intel LLVM compiler (icx). The function my_trans_func does not get called.

I used the -EHa switch as instructed and none of the compilers complained about it.

This leads to two questions:

  1. Is there some command line switch needed to enable it (other than -EHa)?
  2. If not - anyone know if there are plans to support it? (An article here: https://reviews.llvm.org/D102817?id=347591 suggests it has been developed)
// crt_set_se_translator_clr.cpp
// compile with: cl /W4 /clr crt_set_se_translator_clr.cpp
#include <windows.h>
#include <eh.h>
#include <stdio.h>
#include <exception>

int thrower_func( int i ) {
   int y = 0;
   int *p = &y;
   *p = i / *p;
   return 0;
}

class SE_Exception : public std::exception
{
private:
    const unsigned int nSE;
public:
    SE_Exception() noexcept : SE_Exception{ 0 } {}
    SE_Exception( unsigned int n ) noexcept : nSE{ n } {}
    unsigned int getSeNumber() const noexcept { return nSE; }
};

class Scoped_SE_Translator
{
private:
    const _se_translator_function old_SE_translator;
public:
    Scoped_SE_Translator( _se_translator_function new_SE_translator ) noexcept
        : old_SE_translator{ _set_se_translator( new_SE_translator ) } {}
    ~Scoped_SE_Translator() noexcept { _set_se_translator( old_SE_translator ); }
};

void my_trans_func( unsigned int u, PEXCEPTION_POINTERS )
{
    throw SE_Exception( u );
}

void DoTest()
{
    try
    {
        thrower_func( 10 );
    }
    catch( const SE_Exception& e )
    {
        printf( "Caught SE_Exception, error %8.8x\n", e.getSeNumber() );
    }
    catch(...)
    {
        printf( "Caught unexpected SEH exception.\n" );
    }
}

int main() {
    Scoped_SE_Translator scoped_se_translator{ my_trans_func };

    DoTest();
}

Upvotes: 1

Views: 1059

Answers (1)

SeshaP-Intel
SeshaP-Intel

Reputation: 137

This issue has been targeted to be fixed in oneAPI 2024.0 version which will released in the coming months.
You can use /EHa flag for the Intel oneAPI C++ compiler (icx) with /Od optimization. The compiler will be able to handle this exception.
If the issue still persists with new release, then you can start a new discussion for the community to investigate.

Upvotes: 0

Related Questions