fix warning when assertions disabled.
[oota-llvm.git] / tools / llvmc2 / Action.cpp
index 8d34910286a23c25e4ac1b3c2f692fdfa714d423..08416263a1bdc97c1cfd9dfcea67277aaa161cbf 100644 (file)
@@ -1,4 +1,4 @@
-//===--- Tools.h - The LLVM Compiler Driver ---------------------*- C++ -*-===//
+//===--- Action.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
 //
 #include <stdexcept>
 
 using namespace llvm;
+using namespace llvmc;
 
+extern cl::opt<bool> DryRun;
 extern cl::opt<bool> VerboseMode;
 
 namespace {
   int ExecuteProgram(const std::string& name,
-                     const std::vector<std::string>& args) {
+                     const StrVector& args) {
     sys::Path prog = sys::Program::FindProgramByName(name);
 
     if (prog.isEmpty())
@@ -33,14 +35,29 @@ namespace {
     if (!prog.canExecute())
       throw std::runtime_error("Program '" + name + "' is not executable.");
 
-    // Invoke the program
-    std::vector<const char*> argv((args.size()+2));
-    argv[0] = name.c_str();
-    for (unsigned i = 1; i <= args.size(); ++i)
-      argv[i] = args[i-1].c_str();
-    argv[args.size()+1] = 0;  // null terminate list.
+    // Build the command line vector and the redirects array.
+    const sys::Path* redirects[3] = {0,0,0};
+    sys::Path stdout_redirect;
 
-    return sys::Program::ExecuteAndWait(prog, &argv[0]);
+    std::vector<const char*> argv;
+    argv.reserve((args.size()+2));
+    argv.push_back(name.c_str());
+
+    for (StrVector::const_iterator B = args.begin(), E = args.end();
+         B!=E; ++B) {
+      if (*B == ">") {
+        ++B;
+        stdout_redirect.set(*B);
+        redirects[1] = &stdout_redirect;
+      }
+      else {
+        argv.push_back((*B).c_str());
+      }
+    }
+    argv.push_back(0);  // null terminate list.
+
+    // Invoke the program.
+    return sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
   }
 
   void print_string (const std::string& str) {
@@ -48,11 +65,14 @@ namespace {
   }
 }
 
-int llvmcc::Action::Execute() const {
-  if (VerboseMode) {
+int llvmc::Action::Execute() const {
+  if (DryRun || VerboseMode) {
     std::cerr << Command_ << " ";
     std::for_each(Args_.begin(), Args_.end(), print_string);
     std::cerr << '\n';
   }
-  return ExecuteProgram(Command_, Args_);
+  if (DryRun)
+    return 0;
+  else
+    return ExecuteProgram(Command_, Args_);
 }