Simplify some getNode calls.
[oota-llvm.git] / tools / llvmc2 / llvmc.cpp
index feed61f89f1156f5d37fc2b3958e2bf50d813621..e073845d09e07978666672e3880cca0241012946 100644 (file)
@@ -1,4 +1,4 @@
-//===--- llvmcc.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
+//===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -15,6 +15,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "CompilationGraph.h"
+#include "Error.h"
 #include "Tool.h"
 
 #include "llvm/System/Path.h"
 
 namespace cl = llvm::cl;
 namespace sys = llvm::sys;
-using namespace llvmcc;
+using namespace llvmc;
 
+// Built-in command-line options.
 // External linkage here is intentional.
-cl::list<std::string> InputFilenames(cl::Positional,
-                                     cl::desc("<input file>"), cl::OneOrMore);
+
+cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input file>"),
+                                     cl::ZeroOrMore);
 cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"),
                                     cl::value_desc("file"));
-cl::opt<bool> VerboseMode("v", cl::desc("Enable verbose mode"));
-
+cl::list<std::string> Languages("x",
+          cl::desc("Specify the language of the following input files"),
+          cl::ZeroOrMore);
+cl::opt<bool> DryRun("dry-run",
+                     cl::desc("Only pretend to run commands"));
+cl::opt<bool> VerboseMode("v",
+                          cl::desc("Enable verbose mode"));
+cl::opt<bool> WriteGraph("write-graph",
+                         cl::desc("Write compilation-graph.dot file"),
+                         cl::Hidden);
+cl::opt<bool> ViewGraph("view-graph",
+                         cl::desc("Show compilation graph in GhostView"),
+                         cl::Hidden);
+cl::opt<bool> SaveTemps("save-temps",
+                         cl::desc("Keep temporary files"),
+                         cl::Hidden);
 
 namespace {
-  int BuildTargets(const CompilationGraph& graph) {
+  /// BuildTargets - A small wrapper for CompilationGraph::Build.
+  int BuildTargets(CompilationGraph& graph) {
     int ret;
-    sys::Path tempDir(sys::Path::GetTemporaryDirectory());
+    const sys::Path& tempDir = SaveTemps
+      ? sys::Path("")
+      : sys::Path(sys::Path::GetTemporaryDirectory());
 
     try {
       ret = graph.Build(tempDir);
@@ -49,7 +69,8 @@ namespace {
       throw;
     }
 
-    tempDir.eraseFromDisk(true);
+    if (!SaveTemps)
+      tempDir.eraseFromDisk(true);
     return ret;
   }
 }
@@ -58,15 +79,35 @@ int main(int argc, char** argv) {
   try {
     CompilationGraph graph;
 
-    cl::ParseCommandLineOptions(argc, argv,
-                                "LLVM Compiler Driver(Work In Progress)");
+    cl::ParseCommandLineOptions
+      (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
     PopulateCompilationGraph(graph);
+
+    if (WriteGraph) {
+      graph.writeGraph();
+      if (!ViewGraph)
+        return 0;
+    }
+
+    if (ViewGraph) {
+      graph.viewGraph();
+      return 0;
+    }
+
+    if (InputFilenames.empty()) {
+      throw std::runtime_error("no input files");
+    }
+
     return BuildTargets(graph);
   }
+  catch(llvmc::error_code& ec) {
+    return ec.code();
+  }
   catch(const std::exception& ex) {
-    std::cerr << ex.what() << '\n';
+    std::cerr << argv[0] << ": " << ex.what() << '\n';
   }
   catch(...) {
-    std::cerr << "Unknown error!\n";
+    std::cerr << argv[0] << ": unknown error!\n";
   }
+  return 1;
 }