For PR789:
[oota-llvm.git] / lib / System / Win32 / Signals.inc
index 07d399a50c886f28cc901e6864370ad8dfb506e0..8adf7674faf387077162845395d567d9c7add8e2 100644 (file)
 #include <vector>
 
 #ifdef __MINGW32__
-#include <imagehlp.h>
+ #include <imagehlp.h>
 #else
-#include <dbghelp.h>
+ #include <dbghelp.h>
 #endif
 #include <psapi.h>
 
-#pragma comment(lib, "psapi.lib")
-#pragma comment(lib, "dbghelp.lib")
+#ifdef __MINGW32__
+ #if ((HAVE_LIBIMAGEHLP != 1) || (HAVE_LIBPSAPI != 1))
+  #error "libimagehlp.a & libpsapi.a should be present"
+ #endif
+#else
+ #pragma comment(lib, "psapi.lib")
+ #pragma comment(lib, "dbghelp.lib")
+#endif
 
 // Forward declare.
 static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
 
+// InterruptFunction - The function to call if ctrl-c is pressed.
+static void (*InterruptFunction)() = 0;
+
 static std::vector<llvm::sys::Path> *FilesToRemove = NULL;
 static std::vector<llvm::sys::Path> *DirectoriesToRemove = NULL;
 static bool RegisteredUnhandledExceptionFilter = false;
@@ -71,11 +80,14 @@ static void RegisterHandler() {
 }
 
 // RemoveFileOnSignal - The public API
-void sys::RemoveFileOnSignal(const sys::Path &Filename) {
+bool sys::RemoveFileOnSignal(const sys::Path &Filename, std::string* ErrMsg) {
   RegisterHandler();
 
-  if (CleanupExecuted)
-    throw std::string("Process terminating -- cannot register for removal");
+  if (CleanupExecuted) {
+    if (ErrMsg)
+      *ErrMsg = "Process terminating -- cannot register for removal";
+    return true;
+  }
 
   if (FilesToRemove == NULL)
     FilesToRemove = new std::vector<sys::Path>;
@@ -83,23 +95,35 @@ void sys::RemoveFileOnSignal(const sys::Path &Filename) {
   FilesToRemove->push_back(Filename);
 
   LeaveCriticalSection(&CriticalSection);
+  return false;
 }
 
 // RemoveDirectoryOnSignal - The public API
-void sys::RemoveDirectoryOnSignal(const sys::Path& path) {
-  RegisterHandler();
-
-  if (CleanupExecuted)
-    throw std::string("Process terminating -- cannot register for removal");
+bool sys::RemoveDirectoryOnSignal(const sys::Path& path, std::string* ErrMsg) {
+  // Not a directory?
+  const sys::FileStatus *Status =  path.getFileStatus(false, ErrMsg);
+  if (!Status)
+    return true;
+  if (!Status->isDir) {
+    if (ErrMsg)
+      *ErrMsg = path.toString() + " is not a directory";
+    return true;
+  }
 
-  if (path.isDirectory()) {
-    if (DirectoriesToRemove == NULL)
-      DirectoriesToRemove = new std::vector<sys::Path>;
+  RegisterHandler();
 
-    DirectoriesToRemove->push_back(path);
+  if (CleanupExecuted) {
+    if (ErrMsg)
+      *ErrMsg = "Process terminating -- cannot register for removal";
+    return true;
   }
 
+  if (DirectoriesToRemove == NULL)
+    DirectoriesToRemove = new std::vector<sys::Path>;
+  DirectoriesToRemove->push_back(path);
+
   LeaveCriticalSection(&CriticalSection);
+  return false;
 }
 
 /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
@@ -109,6 +133,12 @@ void sys::PrintStackTraceOnErrorSignal() {
   LeaveCriticalSection(&CriticalSection);
 }
 
+
+void sys::SetInterruptFunction(void (*IF)()) {
+  RegisterHandler();
+  InterruptFunction = IF;
+  LeaveCriticalSection(&CriticalSection);
+}
 }
 
 static void Cleanup() {
@@ -230,9 +260,25 @@ static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
 }
 
 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
+  // We are running in our very own thread, courtesy of Windows.
+  EnterCriticalSection(&CriticalSection);
   Cleanup();
 
+  // If an interrupt function has been set, go and run one it; otherwise,
+  // the process dies.
+  void (*IF)() = InterruptFunction;
+  InterruptFunction = 0;      // Don't run it on another CTRL-C.
+
+  if (IF) {
+    // Note: if the interrupt function throws an exception, there is nothing
+    // to catch it in this thread so it will kill the process.
+    IF();                     // Run it now.
+    LeaveCriticalSection(&CriticalSection);
+    return TRUE;              // Don't kill the process.
+  }
+
   // Allow normal processing to take place; i.e., the process dies.
+  LeaveCriticalSection(&CriticalSection);
   return FALSE;
 }