Clang-cl与ICX是否支持异步异常处理(-EHa)相关功能?
关于clang-cl/Intel LLVM编译器(icx)中
_set_se_translator配合/EHa失效的问题解答 问题背景
你提到的代码通过_set_se_translator将Windows结构化异常(SEH)转换为C++异常,在MSVC(cl)和Intel经典编译器(icl)中配合/EHa编译可正常运行,但在clang-cl 14.0.5和Intel LLVM编译器(icx)中my_trans_func未被调用,功能完全失效。
问题1:是否需要额外命令行选项?
在clang-cl 14.0.5及旧版icx中,仅添加/EHa编译选项无法启用_set_se_translator的功能——LLVM系编译器对SEH到C++异常的翻译支持在该版本中尚未完全实现,目前没有额外的命令行选项可以强制激活该特性。
问题2:是否有计划支持该功能?
LLVM社区确实在推进_set_se_translator的支持工作,相关开发任务已经在进行中。后续版本的clang-cl和icx已逐步完善该功能:
- 较新的clang版本(如16及以上)已部分实现该特性,可尝试升级编译器版本测试;
- Intel LLVM编译器(icx)会跟随LLVM的开发进度逐步支持该功能,建议关注编译器的官方更新日志。
测试代码
// 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(); }
内容的提问来源于stack exchange,提问作者maybourne
相关产品推荐
相关产品推荐

