Get rid of exceptions in llvmc.

llvmc can be now compiled with llvm-gcc on Windows.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@109215 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CompilerDriver/Main.cpp b/lib/CompilerDriver/Main.cpp
index b5e507d..28b2737 100644
--- a/lib/CompilerDriver/Main.cpp
+++ b/lib/CompilerDriver/Main.cpp
@@ -20,7 +20,6 @@
 #include "llvm/System/Path.h"
 
 #include <sstream>
-#include <stdexcept>
 #include <string>
 
 namespace cl = llvm::cl;
@@ -31,9 +30,7 @@
 
   std::stringstream* GlobalTimeLog;
 
-  sys::Path getTempDir() {
-    sys::Path tempDir;
-
+  int getTempDir(sys::Path& tempDir) {
     // The --temp-dir option.
     if (!TempDirname.empty()) {
       tempDir = TempDirname;
@@ -41,7 +38,7 @@
     // GCC 4.5-style -save-temps handling.
     else if (SaveTemps == SaveTempsEnum::Unset) {
       tempDir = sys::Path::GetTemporaryDirectory();
-      return tempDir;
+      return 0;
     }
     else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
       tempDir = OutputFilename;
@@ -49,35 +46,34 @@
     }
     else {
       // SaveTemps == Cwd --> use current dir (leave tempDir empty).
-      return tempDir;
+      return 0;
     }
 
     if (!tempDir.exists()) {
       std::string ErrMsg;
-      if (tempDir.createDirectoryOnDisk(true, &ErrMsg))
-        throw std::runtime_error(ErrMsg);
+      if (tempDir.createDirectoryOnDisk(true, &ErrMsg)) {
+        PrintError(ErrMsg);
+        return -1;
+      }
     }
 
-    return tempDir;
+    return 0;
   }
 
   /// BuildTargets - A small wrapper for CompilationGraph::Build.
   int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
     int ret;
-    const sys::Path& tempDir = getTempDir();
+    sys::Path tempDir;
     bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
 
-    try {
-      ret = graph.Build(tempDir, langMap);
-    }
-    catch(...) {
-      if (toDelete)
-        tempDir.eraseFromDisk(true);
-      throw;
-    }
+    if (int ret = getTempDir(tempDir))
+      return ret;
+
+    ret = graph.Build(tempDir, langMap);
 
     if (toDelete)
       tempDir.eraseFromDisk(true);
+
     return ret;
   }
 }
@@ -93,64 +89,54 @@
 const char* ProgramName;
 
 int Main(int argc, char** argv) {
-  try {
-    LanguageMap langMap;
-    CompilationGraph graph;
+  int ret = 0;
+  LanguageMap langMap;
+  CompilationGraph graph;
 
-    ProgramName = argv[0];
+  ProgramName = argv[0];
 
-    cl::ParseCommandLineOptions
-      (argc, argv, "LLVM Compiler Driver (Work In Progress)",
-       /* ReadResponseFiles = */ false);
+  cl::ParseCommandLineOptions
+    (argc, argv, "LLVM Compiler Driver (Work In Progress)",
+     /* ReadResponseFiles = */ false);
 
-    PluginLoader Plugins;
-    Plugins.RunInitialization(langMap, graph);
+  PluginLoader Plugins;
+  if (int ret = Plugins.RunInitialization(langMap, graph))
+    return ret;
 
-    if (CheckGraph) {
-      int ret = graph.Check();
-      if (!ret)
-        llvm::errs() << "check-graph: no errors found.\n";
-
-      return ret;
-    }
-
-    if (ViewGraph) {
-      graph.viewGraph();
-      if (!WriteGraph)
-        return 0;
-    }
-
-    if (WriteGraph) {
-      graph.writeGraph(OutputFilename.empty()
-                       ? std::string("compilation-graph.dot")
-                       : OutputFilename);
-      return 0;
-    }
-
-    if (Time) {
-      GlobalTimeLog = new std::stringstream;
-      GlobalTimeLog->precision(2);
-    }
-
-    int ret = BuildTargets(graph, langMap);
-
-    if (Time) {
-      llvm::errs() << GlobalTimeLog->str();
-      delete GlobalTimeLog;
-    }
+  if (CheckGraph) {
+    ret = graph.Check();
+    if (!ret)
+      llvm::errs() << "check-graph: no errors found.\n";
 
     return ret;
   }
-  catch(llvmc::error_code& ec) {
-    return ec.code();
+
+  if (ViewGraph) {
+    graph.viewGraph();
+    if (!WriteGraph)
+      return 0;
   }
-  catch(const std::exception& ex) {
-    llvm::errs() << argv[0] << ": " << ex.what() << '\n';
+
+  if (WriteGraph) {
+    const std::string& Out = (OutputFilename.empty()
+                              ? std::string("compilation-graph.dot")
+                              : OutputFilename);
+    return graph.writeGraph(Out);
   }
-  catch(...) {
-    llvm::errs() << argv[0] << ": unknown error!\n";
+
+  if (Time) {
+    GlobalTimeLog = new std::stringstream;
+    GlobalTimeLog->precision(2);
   }
-  return 1;
+
+  ret = BuildTargets(graph, langMap);
+
+  if (Time) {
+    llvm::errs() << GlobalTimeLog->str();
+    delete GlobalTimeLog;
+  }
+
+  return ret;
 }
 
 } // end namespace llvmc