OSDN Git Service

[NFC] pull a function into its own lambda
authorJF Bastien <jfb@google.com>
Tue, 15 May 2018 04:23:48 +0000 (04:23 +0000)
committerJF Bastien <jfb@google.com>
Tue, 15 May 2018 04:23:48 +0000 (04:23 +0000)
As requested in D46858, pulling this function into its own lambda makes it
easier to read that part of the code and reason as to what's going on because
the scope it can be called from is extremely limited. We want to keep it as a
function because it's called from the two subsequent lines.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332325 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Unix/Signals.inc

index 33edc2a..d828674 100644 (file)
@@ -101,23 +101,6 @@ static struct {
 } RegisteredSignalInfo[array_lengthof(IntSigs) + array_lengthof(KillSigs)];
 
 
-static void RegisterHandler(int Signal) {
-  assert(NumRegisteredSignals < array_lengthof(RegisteredSignalInfo) &&
-         "Out of space for signal handlers!");
-
-  struct sigaction NewHandler;
-
-  NewHandler.sa_handler = SignalHandler;
-  NewHandler.sa_flags = SA_NODEFER | SA_RESETHAND | SA_ONSTACK;
-  sigemptyset(&NewHandler.sa_mask);
-
-  // Install the new handler, save the old one in RegisteredSignalInfo.
-  sigaction(Signal, &NewHandler,
-            &RegisteredSignalInfo[NumRegisteredSignals].SA);
-  RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
-  ++NumRegisteredSignals;
-}
-
 #if defined(HAVE_SIGALTSTACK)
 // Hold onto both the old and new alternate signal stack so that it's not
 // reported as a leak. We don't make any attempt to remove our alt signal
@@ -159,8 +142,27 @@ static void RegisterHandlers() {
   // be able to reliably handle signals due to stack overflow.
   CreateSigAltStack();
 
-  for (auto S : IntSigs) RegisterHandler(S);
-  for (auto S : KillSigs) RegisterHandler(S);
+  auto registerHandler = [&](int Signal) {
+    assert(NumRegisteredSignals < array_lengthof(RegisteredSignalInfo) &&
+           "Out of space for signal handlers!");
+
+    struct sigaction NewHandler;
+
+    NewHandler.sa_handler = SignalHandler;
+    NewHandler.sa_flags = SA_NODEFER | SA_RESETHAND | SA_ONSTACK;
+    sigemptyset(&NewHandler.sa_mask);
+
+    // Install the new handler, save the old one in RegisteredSignalInfo.
+    sigaction(Signal, &NewHandler,
+              &RegisteredSignalInfo[NumRegisteredSignals].SA);
+    RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
+    ++NumRegisteredSignals;
+  };
+
+  for (auto S : IntSigs)
+    registerHandler(S);
+  for (auto S : KillSigs)
+    registerHandler(S);
 }
 
 static void UnregisterHandlers() {