Index: clang/lib/Frontend/CompilerInstance.cpp =================================================================== --- clang/lib/Frontend/CompilerInstance.cpp +++ clang/lib/Frontend/CompilerInstance.cpp @@ -1235,8 +1235,7 @@ // Execute the action to actually build the module in-place. Use a separate // thread so that we get a stack large enough. - llvm::CrashRecoveryContext CRC; - CRC.RunSafelyOnThread( + bool Crashed = !llvm::CrashRecoveryContext().RunSafelyOnThread( [&]() { GenerateModuleFromModuleMapAction Action; Instance.ExecuteAction(Action); @@ -1249,6 +1248,13 @@ diag::remark_module_build_done) << ModuleName; + // Clear the ASTConsumer if it hasn't been already, in case it owns streams + // that must be closed before clearing output files. + if (Crashed) { + Instance.setSema(nullptr); + Instance.setASTConsumer(nullptr); + } + // Delete any remaining temporary files related to Instance, in case the // module generation thread crashed. Instance.clearOutputFiles(/*EraseFiles=*/true);