From 129311ac0bbbea78d980cb44565c0daeab924511 Mon Sep 17 00:00:00 2001 From: Tal Kedar Date: Sat, 19 Mar 2022 19:08:02 +0000 Subject: [PATCH] [libSupport] make CallBacksToRun static local In order to allow compiling with -Werror=global-constructors with c++20 and above. Discussion: https://discourse.llvm.org/t/llvm-lib-support-signals-cpp-fails-to-compile-due-to-werror-global-constructors/61070 Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D122067 --- llvm/lib/Support/Signals.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Support/Signals.cpp b/llvm/lib/Support/Signals.cpp index f10551d..a6fd845 100644 --- a/llvm/lib/Support/Signals.cpp +++ b/llvm/lib/Support/Signals.cpp @@ -29,6 +29,7 @@ #include "llvm/Support/Program.h" #include "llvm/Support/StringSaver.h" #include "llvm/Support/raw_ostream.h" +#include #include //===----------------------------------------------------------------------===// @@ -80,12 +81,20 @@ struct CallbackAndCookie { enum class Status { Empty, Initializing, Initialized, Executing }; std::atomic Flag; }; + static constexpr size_t MaxSignalHandlerCallbacks = 8; -static CallbackAndCookie CallBacksToRun[MaxSignalHandlerCallbacks]; + +// A global array of CallbackAndCookie may not compile with +// -Werror=global-constructors in c++20 and above +static std::array & +CallBacksToRun() { + static std::array callbacks; + return callbacks; +} // Signal-safe. void sys::RunSignalHandlers() { - for (CallbackAndCookie &RunMe : CallBacksToRun) { + for (CallbackAndCookie &RunMe : CallBacksToRun()) { auto Expected = CallbackAndCookie::Status::Initialized; auto Desired = CallbackAndCookie::Status::Executing; if (!RunMe.Flag.compare_exchange_strong(Expected, Desired)) @@ -100,7 +109,7 @@ void sys::RunSignalHandlers() { // Signal-safe. static void insertSignalHandler(sys::SignalHandlerCallback FnPtr, void *Cookie) { - for (CallbackAndCookie &SetMe : CallBacksToRun) { + for (CallbackAndCookie &SetMe : CallBacksToRun()) { auto Expected = CallbackAndCookie::Status::Empty; auto Desired = CallbackAndCookie::Status::Initializing; if (!SetMe.Flag.compare_exchange_strong(Expected, Desired)) -- 2.7.4