Index: include/clang/Driver/Driver.h
===================================================================
--- include/clang/Driver/Driver.h	(revision 135078)
+++ include/clang/Driver/Driver.h	(working copy)
@@ -31,6 +31,7 @@
 namespace driver {
   class Action;
   class ArgList;
+  class Command;
   class Compilation;
   class DerivedArgList;
   class HostInfo;
@@ -134,6 +135,9 @@
   /// format.
   unsigned CCLogDiagnostics : 1;
 
+  /// Whether we driver is generating diagnostics for debugging purposes.
+  unsigned CCGenDiagnostics : 1;
+
 private:
   /// Name to use when invoking gcc/g++.
   std::string CCCGenericGCCName;
@@ -263,7 +267,14 @@
   /// This routine handles additional processing that must be done in addition
   /// to just running the subprocesses, for example reporting errors, removing
   /// temporary files, etc.
-  int ExecuteCompilation(const Compilation &C) const;
+  int ExecuteCompilation(const Compilation &C,
+                         const Command *&FailingCommand) const;
+  
+  /// generateCompilationDiagnostics - Generate diagnostics information 
+  /// including preprocessed source file and relevant command line arguments.
+  /// 
+  void generateCompilationDiagnostics(Compilation &C,
+                                      const Command *FailingCommand);
 
   /// @}
   /// @name Helper Methods
Index: include/clang/Driver/ArgList.h
===================================================================
--- include/clang/Driver/ArgList.h	(revision 135078)
+++ include/clang/Driver/ArgList.h	(working copy)
@@ -153,6 +153,13 @@
     }
 
     /// @}
+    /// @name Arg Removal
+    /// @{
+
+    /// eraseArg - Remove any option matching \arg Id.
+    void eraseArg(OptSpecifier Id);
+
+    /// @}
     /// @name Arg Access
     /// @{
 
@@ -245,6 +252,10 @@
     /// option id.
     void ClaimAllArgs(OptSpecifier Id0) const;
 
+    /// ClaimAllArgs - Claim all arguments.
+    ///
+    void ClaimAllArgs() const;
+
     /// @}
     /// @name Arg Synthesis
     /// @{
Index: include/clang/Driver/Job.h
===================================================================
--- include/clang/Driver/Job.h	(revision 135078)
+++ include/clang/Driver/Job.h	(working copy)
@@ -103,6 +103,9 @@
   /// Add a job to the list (taking ownership).
   void addJob(Job *J) { Jobs.push_back(J); }
 
+  /// Clear the job list.
+  void clear();
+
   const list_type &getJobs() const { return Jobs; }
 
   size_type size() const { return Jobs.size(); }
Index: include/clang/Driver/Compilation.h
===================================================================
--- include/clang/Driver/Compilation.h	(revision 135078)
+++ include/clang/Driver/Compilation.h	(working copy)
@@ -14,6 +14,7 @@
 #include "clang/Driver/Util.h"
 
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/Support/Path.h"
 
 namespace llvm {
   class raw_ostream;
@@ -60,6 +61,9 @@
   /// Result files which should be removed on failure.
   ArgStringList ResultFiles;
 
+  /// Redirection for stdout, stderr, etc.
+  const llvm::sys::Path **Redirects;
+
 public:
   Compilation(const Driver &D, const ToolChain &DefaultToolChain,
               InputArgList *Args, DerivedArgList *TranslatedArgs);
@@ -84,7 +88,7 @@
   const ArgStringList &getTempFiles() const { return TempFiles; }
 
   const ArgStringList &getResultFiles() const { return ResultFiles; }
-
+  
   /// getArgsForToolChain - Return the derived argument list for the
   /// tool chain \arg TC (or the default tool chain, if TC is not
   /// specified).
@@ -136,6 +140,11 @@
   /// Command which failed.
   /// \return The accumulated result code of the job.
   int ExecuteJob(const Job &J, const Command *&FailingCommand) const;
+
+  /// initCompilationForDiagnostics - Remove stale state and supress output
+  /// so compilation can be reexecuted to generate additional diagnostic
+  /// information (e.g., preprocessed source).
+  void initCompilationForDiagnostics();
 };
 
 } // end namespace driver
Index: tools/driver/driver.cpp
===================================================================
--- tools/driver/driver.cpp	(revision 135078)
+++ tools/driver/driver.cpp	(working copy)
@@ -458,9 +458,13 @@
 
   llvm::OwningPtr<Compilation> C(TheDriver.BuildCompilation(argv));
   int Res = 0;
+  const Command *FailingCommand = 0;
   if (C.get())
-    Res = TheDriver.ExecuteCompilation(*C);
-  
+    Res = TheDriver.ExecuteCompilation(*C, FailingCommand);
+
+  if (Res)
+    TheDriver.generateCompilationDiagnostics(*C, FailingCommand);
+
   // If any timers were active but haven't been destroyed yet, print their
   // results now.  This happens in -disable-free mode.
   llvm::TimerGroup::printAll(llvm::errs());
Index: lib/Driver/Tools.cpp
===================================================================
--- lib/Driver/Tools.cpp	(revision 135078)
+++ lib/Driver/Tools.cpp	(working copy)
@@ -1376,7 +1376,7 @@
 
   Args.AddAllArgs(CmdArgs, options::OPT_v);
   Args.AddLastArg(CmdArgs, options::OPT_H);
-  if (D.CCPrintHeaders) {
+  if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
     CmdArgs.push_back("-header-include-file");
     CmdArgs.push_back(D.CCPrintHeadersFilename ?
                       D.CCPrintHeadersFilename : "-");
@@ -1384,7 +1384,7 @@
   Args.AddLastArg(CmdArgs, options::OPT_P);
   Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
 
-  if (D.CCLogDiagnostics) {
+  if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
     CmdArgs.push_back("-diagnostic-log-file");
     CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
                       D.CCLogDiagnosticsFilename : "-");
Index: lib/Driver/ArgList.cpp
===================================================================
--- lib/Driver/ArgList.cpp	(revision 135078)
+++ lib/Driver/ArgList.cpp	(working copy)
@@ -46,6 +46,16 @@
   Args.push_back(A);
 }
 
+void ArgList::eraseArg(OptSpecifier Id) {
+  for (iterator it = begin(), ie = end(); it != ie; ++it) {
+    if ((*it)->getOption().matches(Id)) {
+      Args.erase(it);
+      it = begin();
+      ie = end();
+    }
+  }
+}
+
 Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const {
   // FIXME: Make search efficient?
   for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
@@ -192,6 +202,12 @@
     (*it)->claim();
 }
 
+void ArgList::ClaimAllArgs() const {
+  for (const_iterator it = begin(), ie = end(); it != ie; ++it)
+    if (!(*it)->isClaimed())
+      (*it)->claim();
+}
+
 const char *ArgList::MakeArgString(const llvm::Twine &T) const {
   llvm::SmallString<256> Str;
   T.toVector(Str);
Index: lib/Driver/Job.cpp
===================================================================
--- lib/Driver/Job.cpp	(revision 135078)
+++ lib/Driver/Job.cpp	(working copy)
@@ -28,6 +28,12 @@
     delete *it;
 }
 
+void JobList::clear() {
+  for (iterator it = begin(), ie = end(); it != ie; ++it)
+    delete *it;
+  Jobs.clear();
+}
+
 void Job::addCommand(Command *C) {
   cast<JobList>(this)->addJob(C);
 }
Index: lib/Driver/Compilation.cpp
===================================================================
--- lib/Driver/Compilation.cpp	(revision 135078)
+++ lib/Driver/Compilation.cpp	(working copy)
@@ -25,7 +25,7 @@
 Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
                          InputArgList *_Args, DerivedArgList *_TranslatedArgs)
   : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
-    TranslatedArgs(_TranslatedArgs) {
+    TranslatedArgs(_TranslatedArgs), Redirects(0) {
 }
 
 Compilation::~Compilation() {
@@ -43,6 +43,13 @@
   for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
        it != ie; ++it)
     delete *it;
+
+  // Free redirections of stdout/stderr
+  if (Redirects) {
+    delete Redirects[1];
+    delete Redirects[2];
+    delete Redirects;
+  }
 }
 
 const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
@@ -135,8 +142,8 @@
   std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
   Argv[C.getArguments().size() + 1] = 0;
 
-  if (getDriver().CCCEcho || getDriver().CCPrintOptions ||
-      getArgs().hasArg(options::OPT_v)) {
+  if ((getDriver().CCCEcho || getDriver().CCPrintOptions ||
+       getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
     llvm::raw_ostream *OS = &llvm::errs();
 
     // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
@@ -167,7 +174,7 @@
   std::string Error;
   int Res =
     llvm::sys::Program::ExecuteAndWait(Prog, Argv,
-                                       /*env*/0, /*redirects*/0,
+                                       /*env*/0, Redirects,
                                        /*secondsToWait*/0, /*memoryLimit*/0,
                                        &Error);
   if (!Error.empty()) {
@@ -195,3 +202,27 @@
     return 0;
   }
 }
+
+void Compilation::initCompilationForDiagnostics(void) {
+  // Free actions and jobs, if built.
+  for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
+       it != ie; ++it)
+    delete *it;
+  Actions.clear();
+  Jobs.clear();
+
+  // Clear temporary and results file lists.
+  TempFiles.clear();
+  ResultFiles.clear();
+
+  // Remove any user specified output.  Claim any unclaimed arguments, so as
+  // to avoid emitting warnings about unused args.
+  if (TranslatedArgs->hasArg(options::OPT_o))
+    TranslatedArgs->eraseArg(options::OPT_o);
+  TranslatedArgs->ClaimAllArgs();
+
+  // Redirect stdout/stderr to /dev/null
+  Redirects = new const llvm::sys::Path*[3]();
+  Redirects[1] = new const llvm::sys::Path();
+  Redirects[2] = new const llvm::sys::Path();
+}
Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp	(revision 135078)
+++ lib/Driver/Driver.cpp	(working copy)
@@ -60,9 +60,9 @@
     CCLogDiagnosticsFilename(0), CCCIsCXX(false),
     CCCIsCPP(false),CCCEcho(false), CCCPrintBindings(false),
     CCPrintOptions(false), CCPrintHeaders(false), CCLogDiagnostics(false),
-    CCCGenericGCCName(""), CheckInputsExist(true), CCCUseClang(true),
-    CCCUseClangCXX(true), CCCUseClangCPP(true), CCCUsePCH(true),
-    SuppressMissingInputWarning(false) {
+    CCGenDiagnostics(false), CCCGenericGCCName(""), CheckInputsExist(true),
+    CCCUseClang(true), CCCUseClangCXX(true), CCCUseClangCPP(true),
+    CCCUsePCH(true), SuppressMissingInputWarning(false) {
   if (IsProduction) {
     // In a "production" build, only use clang on architectures we expect to
     // work, and don't use clang C++.
@@ -313,7 +313,62 @@
   return C;
 }
 
-int Driver::ExecuteCompilation(const Compilation &C) const {
+// When clang crashes, produce diagnostic information including the fully 
+// preprocessed source file and relevant command line arguments.  Request  
+// that the developer attach the diagnostic information to a radar.
+void Driver::generateCompilationDiagnostics(Compilation &C,
+                                            const Command *FailingCommand) {
+  llvm::errs() << "\nCompilation error detected, generating diagnostic "
+               << "information including preprocessed source code and "
+               << "relevant command line arguments.  Please submit a "
+               << "radar/bugzilla and include all diagnostic information.\n";
+  const Tool &FailingTool = FailingCommand->getCreator();
+  llvm::errs() << "Failing Tool: " << FailingTool.getShortName() << "\n";
+  llvm::errs() << "Failing command:\n";
+  C.PrintJob(llvm::errs(), *FailingCommand, "\n", true);
+
+  // Emit preprocessed source to file.
+  CCCIsCPP = true;
+
+  // Suppress output and direct preprocessor output to temp file, not stderr.
+  CCGenDiagnostics = true;
+
+  // Clear stale state and suppress output.
+  C.initCompilationForDiagnostics();
+  
+  // Construct the list of abstract actions to perform for this compilation.
+  if (Host->useDriverDriver())
+    BuildUniversalActions(C.getDefaultToolChain(), C.getArgs(),
+                          C.getActions());
+  else
+    BuildActions(C.getDefaultToolChain(), C.getArgs(), C.getActions());
+
+  BuildJobs(C);
+
+  assert (!getDiags().hasErrorOccurred() && "Error generating diagnostics.");
+
+  // Generate preprocessed output.
+  FailingCommand = 0;
+  int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
+
+  // If the command succeeded, we are done.
+  if (Res == 0) {
+    llvm::errs() << "Emitting preprocessed source(s) to:\n";
+    ArgStringList Files = C.getTempFiles();
+    for (ArgStringList::const_iterator it = Files.begin(), ie = Files.end(); 
+         it != ie; ++it)
+      llvm::errs() << *it << "\n";    
+  } else {
+    // Failure, remove preprocessed files.
+    if (!C.getArgs().hasArg(options::OPT_save_temps))
+      C.CleanupFileList(C.getTempFiles(), true);
+
+    llvm::errs() << "Error generating preprocessed source(s).\n";
+  }
+}
+
+int Driver::ExecuteCompilation(const Compilation &C,
+                               const Command *&FailingCommand) const {
   // Just print if -### was present.
   if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
     C.PrintJob(llvm::errs(), C.getJobs(), "\n", true);
@@ -324,7 +379,6 @@
   if (getDiags().hasErrorOccurred())
     return 1;
 
-  const Command *FailingCommand = 0;
   int Res = C.ExecuteJob(C.getJobs(), FailingCommand);
 
   // Remove temp files.
@@ -1189,7 +1243,7 @@
                        A->getType(), BaseInput);
   }
 
-  if (CCCPrintBindings) {
+  if (CCCPrintBindings && !CCGenDiagnostics) {
     llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
                  << " - \"" << T.getName() << "\", inputs: [";
     for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
@@ -1216,11 +1270,12 @@
   }
 
   // Default to writing to stdout?
-  if (AtTopLevel && isa<PreprocessJobAction>(JA))
+  if (AtTopLevel && isa<PreprocessJobAction>(JA) && !CCGenDiagnostics)
     return "-";
 
   // Output to a temporary file?
-  if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
+  if ((!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) ||
+      CCGenDiagnostics) {
     std::string TmpName =
       GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
     return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
