From b4d1ddb99e1305f6ea6217e7570dda2a94b16e51 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Sat, 1 Aug 2026 13:01:07 +0800 Subject: [CMake] Match LLVM EH and RTTI defaults Disable C++ exceptions and RTTI by default while retaining explicit, validated opt-in switches. Replace the arena's exception syntax with an RAII construction guard so both configurations share one implementation. --- backend/CMakeLists.txt | 6 ++++++ backend/cmake/modules/RCCBackendBuild.cmake | 12 ++++++++++++ backend/lib/CodeGen/MachineFunction.cpp | 17 +++++++---------- 3 files changed, 25 insertions(+), 10 deletions(-) (limited to 'backend') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 2d5cc82..0f983d0 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -44,6 +44,12 @@ include(RCCBackendDeveloperTools) option(RCC_BACKEND_BUILD_TESTS "Build RCC Backend tests" "${BUILD_TESTING}") option(RCC_BACKEND_WARNINGS_AS_ERRORS "Treat compiler warnings as errors" OFF) option(RCC_BACKEND_ENABLE_CLANG_TIDY "Run clang-tidy during each compilation" OFF) +option(RCC_BACKEND_ENABLE_EH "Enable C++ exception handling" OFF) +option(RCC_BACKEND_ENABLE_RTTI "Enable C++ run-time type information" OFF) + +if(RCC_BACKEND_ENABLE_EH AND NOT RCC_BACKEND_ENABLE_RTTI) + message(FATAL_ERROR "RCC Backend exception handling requires RTTI") +endif() rcc_backend_initialize_toolchain() diff --git a/backend/cmake/modules/RCCBackendBuild.cmake b/backend/cmake/modules/RCCBackendBuild.cmake index 79e7bb1..52e3a71 100644 --- a/backend/cmake/modules/RCCBackendBuild.cmake +++ b/backend/cmake/modules/RCCBackendBuild.cmake @@ -70,6 +70,18 @@ function(rcc_backend_configure_target Target) -Wformat=2 ) + if(RCC_BACKEND_ENABLE_EH) + target_compile_options("${Target}" PRIVATE -fexceptions) + else() + target_compile_options("${Target}" PRIVATE -fno-exceptions) + endif() + + if(RCC_BACKEND_ENABLE_RTTI) + target_compile_options("${Target}" PRIVATE -frtti) + else() + target_compile_options("${Target}" PRIVATE -fno-rtti) + endif() + if(RCC_BACKEND_WARNINGS_AS_ERRORS) target_compile_options("${Target}" PRIVATE -Werror) endif() diff --git a/backend/lib/CodeGen/MachineFunction.cpp b/backend/lib/CodeGen/MachineFunction.cpp index e19c115..fc1508c 100644 --- a/backend/lib/CodeGen/MachineFunction.cpp +++ b/backend/lib/CodeGen/MachineFunction.cpp @@ -35,16 +35,13 @@ public: [[nodiscard]] ObjectT *create(ArgumentTs &&...Arguments) { void *Storage = Resource.allocate(sizeof(ObjectT), alignof(ObjectT)); ObjectT *Object = ::new (Storage) ObjectT(std::forward(Arguments)...); - try { - Destructors.push_back(DestructorRecord{ - Object, - [](void *RawObject) noexcept { std::destroy_at(static_cast(RawObject)); }, - }); - } catch (...) { - std::destroy_at(Object); - throw; - } - return Object; + auto DestroyObject = [](ObjectT *TypedObject) noexcept { std::destroy_at(TypedObject); }; + std::unique_ptr ConstructionGuard(Object, DestroyObject); + Destructors.push_back(DestructorRecord{ + Object, + [](void *RawObject) noexcept { std::destroy_at(static_cast(RawObject)); }, + }); + return ConstructionGuard.release(); } private: -- cgit v1.2.3