Index: clang/include/clang/Driver/Driver.h =================================================================== --- clang/include/clang/Driver/Driver.h +++ clang/include/clang/Driver/Driver.h @@ -466,6 +466,8 @@ StringRef AdditionalInformation = "", CompilationDiagnosticReport *GeneratedReport = nullptr); + bool shouldEmitDiagnosticsOnError(Compilation &C) const; + /// @} /// @name Helper Methods /// @{ Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -1377,6 +1377,9 @@ def fcrash_diagnostics_dir : Joined<["-"], "fcrash-diagnostics-dir=">, Group, Flags<[NoArgumentUnused, CoreOption]>, HelpText<"Put crash-report files in ">, MetaVarName<"">; +def fcrash_diagnostics_on_err : Flag<["-"], "fcrash-diagnostics-on-error">, + Group, Flags<[NoArgumentUnused, CoreOption]>, + HelpText<"Emit preprocessed source files and a script for reproduction on any tool failure">; def fcreate_profile : Flag<["-"], "fcreate-profile">, Group; defm cxx_exceptions: BoolFOption<"cxx-exceptions", LangOpts<"CXXExceptions">, DefaultFalse, Index: clang/lib/Driver/Driver.cpp =================================================================== --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -1390,6 +1390,10 @@ return false; } +bool Driver::shouldEmitDiagnosticsOnError(Compilation &C) const { + return C.getArgs().hasArg(options::OPT_fcrash_diagnostics_on_err); +} + // When clang crashes, produce diagnostic information including the fully // preprocessed source file(s). Request that the developer attach the // diagnostic information to a bug report. Index: clang/test/Driver/crash-report.cpp =================================================================== --- clang/test/Driver/crash-report.cpp +++ clang/test/Driver/crash-report.cpp @@ -27,6 +27,13 @@ // RUN: cat %t/crash-report-*.cpp | FileCheck --check-prefix=CHECKSRC %s // RUN: cat %t/crash-report-*.sh | FileCheck --check-prefix=CHECKSH %s +// RUN: env TMPDIR=%t TEMP=%t TMP=%t RC_DEBUG_OPTIONS=1 \ +// RUN: CC_PRINT_HEADERS=1 CC_LOG_DIAGNOSTICS=1 \ +// RUN: not %clang %s @%t.rsp -fcrash-diagnostics-on-error 2>&1 | \ +// RUN: FileCheck %s +// RUN: cat %t/crash-report-*.cpp | FileCheck --check-prefix=CHECKSRC %s +// RUN: cat %t/crash-report-*.sh | FileCheck --check-prefix=CHECKSH %s + // REQUIRES: crash-recovery #ifdef PARSER Index: clang/tools/driver/driver.cpp =================================================================== --- clang/tools/driver/driver.cpp +++ clang/tools/driver/driver.cpp @@ -529,6 +529,10 @@ if (IsCrash) { TheDriver.generateCompilationDiagnostics(*C, *FailingCommand); break; + } else if (TheDriver.shouldEmitDiagnosticsOnError(*C)) { + // Hack to ensure that diagnostic notes get emitted. + Diags.setLastDiagnosticIgnored(false); + TheDriver.generateCompilationDiagnostics(*C, *FailingCommand); } } }