[llvm-commits] CVS: llvm/lib/System/Win32/Program.inc Win32.h

Reid Spencer reid at x10sys.com
Sun Aug 20 23:03:00 PDT 2006



Changes in directory llvm/lib/System/Win32:

Program.inc updated: 1.16 -> 1.17
Win32.h updated: 1.6 -> 1.7
---
Log message:

For PR797: http://llvm.org/PR797 :
Remove all exception code from Program.inc and implement its new interface
with an ErrMsg string argument. 


---
Diffs of the changes:  (+59 -31)

 Program.inc |   74 +++++++++++++++++++++++++++++++++++-------------------------
 Win32.h     |   16 ++++++++++++
 2 files changed, 59 insertions(+), 31 deletions(-)


Index: llvm/lib/System/Win32/Program.inc
diff -u llvm/lib/System/Win32/Program.inc:1.16 llvm/lib/System/Win32/Program.inc:1.17
--- llvm/lib/System/Win32/Program.inc:1.16	Wed Jun  7 18:18:34 2006
+++ llvm/lib/System/Win32/Program.inc	Mon Aug 21 01:02:44 2006
@@ -69,7 +69,7 @@
   }
 }
 
-static HANDLE RedirectIO(const Path *path, int fd) {
+static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) {
   HANDLE h;
   if (path == 0) {
     DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
@@ -91,8 +91,9 @@
                  &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
                  FILE_ATTRIBUTE_NORMAL, NULL);
   if (h == INVALID_HANDLE_VALUE) {
-    ThrowError(std::string(fname) + ": Can't open file for " +
+    MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
         (fd ? "input: " : "output: "));
+    return h;
   }
   return h;
 }
@@ -102,9 +103,13 @@
                         const char** args,
                         const char** envp,
                         const Path** redirects,
-                        unsigned secondsToWait) {
-  if (!path.canExecute())
-    throw path.toString() + " is not executable"; 
+                        unsigned secondsToWait,
+                        std::string* ErrMsg) {
+  if (!path.canExecute()) {
+    if (ErrMsg)
+      *ErrMsg = "program not executable";
+    return -1;
+  }
 
   // Windows wants a command line, not an array of args, to pass to the new
   // process.  We have to concatenate them all, while quoting the args that
@@ -148,21 +153,29 @@
   if (redirects) {
     si.dwFlags = STARTF_USESTDHANDLES;
     
-    try {
-      si.hStdInput = RedirectIO(redirects[0], 0);
-      si.hStdOutput = RedirectIO(redirects[1], 1);
-      if (redirects[1] && redirects[2] && *(redirects[1]) != *(redirects[2])) {
-        si.hStdError = RedirectIO(redirects[2], 2);
-      } else {
-        DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
-                        GetCurrentProcess(), &si.hStdError,
-                        0, TRUE, DUPLICATE_SAME_ACCESS);
-      }
-    } catch (...) {
+    si.hStdInput = RedirectIO(redirects[0], 0, ErrMsg);
+    if (si.hStdInput == INVALID_HANDLE_VALUE) {
+      MakeErrMsg(ErrMsg, "can't redirect stdin");
+      return -1;
+    }
+    si.hStdOutput = RedirectIO(redirects[1], 1);
+    if (si.hStdOutput == INVALID_HANDLE_VALUE) {
       CloseHandle(si.hStdInput);
-      CloseHandle(si.hStdOutput);
-      CloseHandle(si.hStdError);
-      throw;
+      MakeErrMsg(ErrMsg, "can't redirect stdout");
+      return -1;
+    }
+    if (redirects[1] && redirects[2] && *(redirects[1]) != *(redirects[2])) {
+      si.hStdError = RedirectIO(redirects[2], 2);
+      if (si.hStdError == INVALID_HANDLE_VALUE) {
+        CloseHandle(si.hStdInput);
+        CloseHandle(si.hStdOutput);
+        MakeErrMsg(ErrMsg, "can't redirect stderr");
+        return -1;
+      }
+    } else {
+      DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
+                      GetCurrentProcess(), &si.hStdError,
+                      0, TRUE, DUPLICATE_SAME_ACCESS);
     }
   }
 
@@ -181,12 +194,13 @@
   CloseHandle(si.hStdOutput);
   CloseHandle(si.hStdError);
 
-  // Now throw an error if the process didn't get created.
+  // Now return an error if the process didn't get created.
   if (!rc)
   {
     SetLastError(err);
-    ThrowError(std::string("Couldn't execute program '") + 
+    MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") + 
                path.toString() + "'");
+    return -1;
   }
 
   // Wait for it to terminate.
@@ -196,8 +210,9 @@
 
   if (WaitForSingleObject(pi.hProcess, millisecondsToWait) == WAIT_TIMEOUT) {
     if (!TerminateProcess(pi.hProcess, 1)) {
-      ThrowError(std::string("Failed to terminate timed-out program '") + 
-                 path.toString() + "'");
+      MakeErrMsg(ErrMsg, std::string("Failed to terminate timed-out program '")
+          + path.toString() + "'");
+      return -1;
     }
     WaitForSingleObject(pi.hProcess, INFINITE);
   }
@@ -213,23 +228,22 @@
 
   if (!rc) {
     SetLastError(err);
-    ThrowError(std::string("Failed getting status for program '") + 
+    MakeErrMsg(ErrMsg, std::string("Failed getting status for program '") + 
                path.toString() + "'");
+    return -1;
   }
 
   return status;
 }
 
-void Program::ChangeStdinToBinary(){
+bool Program::ChangeStdinToBinary(){
   int result = _setmode( _fileno(stdin), _O_BINARY );
-  if( result == -1 )
-    throw std::string("Cannot set input mode on stdin to binary.");
+  return result == -1;
 }
 
-void Program::ChangeStdoutToBinary(){
+bool Program::ChangeStdoutToBinary(){
   int result = _setmode( _fileno(stdout), _O_BINARY );
-  if( result == -1 )
-    throw std::string("Cannot set output mode on stdout to binary.");
+  return result == -1;
 }
 
 }


Index: llvm/lib/System/Win32/Win32.h
diff -u llvm/lib/System/Win32/Win32.h:1.6 llvm/lib/System/Win32/Win32.h:1.7
--- llvm/lib/System/Win32/Win32.h:1.6	Fri Jul  7 12:26:47 2006
+++ llvm/lib/System/Win32/Win32.h	Mon Aug 21 01:02:44 2006
@@ -44,6 +44,20 @@
   throw s;
 }
 
+inline void MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
+  if (!ErrMsg)
+    return;
+  char *buffer = NULL;
+  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
+      NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
+  ErrMsg = prefix + buffer;
+  LocalFree(buffer);
+}
+
 inline void ThrowErrno(const std::string& prefix) {
-    ThrowError(prefix + ": " + strerror(errno));
+  ThrowError(prefix + ": " + strerror(errno));
+}
+
+inline void MakeErrnoMsg(std::string* ErrMsg, const std::string & prefix) {
+  MakeErrorMsg(prefix + ": " + strerror(errno));
 }






More information about the llvm-commits mailing list