[cfe-commits] r67250 - in /cfe/trunk: include/clang/Basic/DiagnosticDriverKinds.def include/clang/Basic/DiagnosticDriverKinds.td include/clang/Driver/Compilation.h lib/Driver/Compilation.cpp

Daniel Dunbar daniel at zuster.org
Wed Mar 18 15:44:25 PDT 2009


Author: ddunbar
Date: Wed Mar 18 17:44:24 2009
New Revision: 67250

URL: http://llvm.org/viewvc/llvm-project?rev=67250&view=rev
Log:
Driver: Execute jobs; no pipe support yet.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.def
    cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td
    cfe/trunk/include/clang/Driver/Compilation.h
    cfe/trunk/lib/Driver/Compilation.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.def?rev=67250&r1=67249&r2=67250&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.def Wed Mar 18 17:44:24 2009
@@ -34,6 +34,8 @@
      "unable to make temporary file: %0")
 DIAG(err_drv_unable_to_remove_file, ERROR,
      "unable to remove file: %0")
+DIAG(err_drv_command_failure, ERROR,
+     "unable to execute command: %0")
 
 DIAG(warn_drv_input_file_unused, WARNING,
      "%0: '%1' input file unused when '%2' is present")

Modified: cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td?rev=67250&r1=67249&r2=67250&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td Wed Mar 18 17:44:24 2009
@@ -27,6 +27,8 @@
   "unable to make temporary file: %0">;
 def err_drv_unable_to_remove_file : Error<
   "unable to remove file: %0">;
+def err_drv_command_failure : Error<
+  "unable to execute command: %0">;
 
 def warn_drv_input_file_unused : Warning<
   "%0: '%1' input file unused when '%2' is present">;

Modified: cfe/trunk/include/clang/Driver/Compilation.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Compilation.h?rev=67250&r1=67249&r2=67250&view=diff

==============================================================================
--- cfe/trunk/include/clang/Driver/Compilation.h (original)
+++ cfe/trunk/include/clang/Driver/Compilation.h Wed Mar 18 17:44:24 2009
@@ -105,8 +105,14 @@
   /// OS - The stream to print on.
   /// J - The job to print.
   /// Terminator - A string to print at the end of the line.
-  void PrintJob(llvm::raw_ostream &OS, const Job *J, 
-                const char *Terminator) const;
+  /// Quote - Should separate arguments be quoted.
+  void PrintJob(llvm::raw_ostream &OS, const Job &J, 
+                const char *Terminator, bool Quote) const;
+
+  /// ExecuteJob - Execute a single job.
+  ///
+  /// \return The accumulated result code of the job.
+  int ExecuteJob(const Job &J) const;
 };
 
 } // end namespace driver

Modified: cfe/trunk/lib/Driver/Compilation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Compilation.cpp?rev=67250&r1=67249&r2=67250&view=diff

==============================================================================
--- cfe/trunk/lib/Driver/Compilation.cpp (original)
+++ cfe/trunk/lib/Driver/Compilation.cpp Wed Mar 18 17:44:24 2009
@@ -16,6 +16,7 @@
 #include "clang/Driver/ToolChain.h"
 
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/System/Program.h"
 #include <sys/stat.h>
 #include <errno.h>
 using namespace clang::driver;
@@ -54,23 +55,27 @@
   return *Entry;
 }
 
-void Compilation::PrintJob(llvm::raw_ostream &OS, const Job *J, 
-                           const char *Terminator) const {
-  if (const Command *C = dyn_cast<Command>(J)) {
+void Compilation::PrintJob(llvm::raw_ostream &OS, const Job &J, 
+                           const char *Terminator, bool Quote) const {
+  if (const Command *C = dyn_cast<Command>(&J)) {
     OS << " \"" << C->getExecutable() << '"';
     for (ArgStringList::const_iterator it = C->getArguments().begin(),
-           ie = C->getArguments().end(); it != ie; ++it)
-      OS << " \"" << *it << '"';
+           ie = C->getArguments().end(); it != ie; ++it) {
+      if (Quote)
+        OS << " \"" << *it << '"';
+      else
+        OS << ' ' << *it;
+    }
     OS << Terminator;
-  } else if (const PipedJob *PJ = dyn_cast<PipedJob>(J)) {
+  } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
     for (PipedJob::const_iterator 
            it = PJ->begin(), ie = PJ->end(); it != ie; ++it)
-      PrintJob(OS, *it, (it + 1 != PJ->end()) ? " |\n" : "\n");
+      PrintJob(OS, **it, (it + 1 != PJ->end()) ? " |\n" : "\n", Quote);
   } else {
-    const JobList *Jobs = cast<JobList>(J);
+    const JobList *Jobs = cast<JobList>(&J);
     for (JobList::const_iterator 
            it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
-      PrintJob(OS, *it, Terminator);
+      PrintJob(OS, **it, Terminator, Quote);
   }
 }
 
@@ -90,7 +95,8 @@
 
       // FIXME: Grumble, P.exists() is broken. PR3837.
       struct stat buf;
-      if (::stat(P.c_str(), &buf) || errno != ENOENT) {
+      if (::stat(P.c_str(), &buf) == 0 
+          || errno != ENOENT) {
         if (IssueErrors)
           getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
             << Error;
@@ -102,16 +108,52 @@
   return Success;
 }
 
+int Compilation::ExecuteJob(const Job &J) const {
+  if (const Command *C = dyn_cast<Command>(&J)) {
+    llvm::sys::Path Prog(C->getExecutable());
+    const char **Argv = new const char*[C->getArguments().size() + 2];
+    Argv[0] = C->getExecutable();
+    std::copy(C->getArguments().begin(), C->getArguments().end(), Argv+1);
+    Argv[C->getArguments().size() + 1] = 0;
+
+    if (getDriver().CCCEcho || getArgs().hasArg(options::OPT_v))
+      PrintJob(llvm::errs(), J, "\n", false);
+    
+    std::string Error;
+    int Res = 
+      llvm::sys::Program::ExecuteAndWait(Prog, Argv,
+                                         /*env*/0, /*redirects*/0,
+                                         /*secondsToWait*/0, /*memoryLimit*/0,
+                                         &Error);
+    if (!Error.empty()) {
+      assert(Res && "Error string set with 0 result code!");
+      getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
+    }
+    
+    delete[] Argv;
+    return Res;
+  } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
+    (void) PJ;
+    getDriver().Diag(clang::diag::err_drv_unsupported_opt) << "-pipe";
+    return 1;
+  } else {
+    const JobList *Jobs = cast<JobList>(&J);
+    for (JobList::const_iterator 
+           it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
+      if (int Res = ExecuteJob(**it))
+        return Res;
+    return 0;
+  }
+}
+
 int Compilation::Execute() const {
   // Just print if -### was present.
   if (getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
-    PrintJob(llvm::errs(), &Jobs, "\n");
+    PrintJob(llvm::errs(), Jobs, "\n", true);
     return 0;
   }
 
-  // FIXME: Execute.
-
-  int Res = 0;
+  int Res = ExecuteJob(Jobs);
   
   // Remove temp files.
   CleanupFileList(TempFiles);





More information about the cfe-commits mailing list