Index: include/clang/Frontend/CompilerInstance.h =================================================================== --- include/clang/Frontend/CompilerInstance.h +++ include/clang/Frontend/CompilerInstance.h @@ -153,14 +153,17 @@ std::string Filename; std::string TempFilename; std::unique_ptr OS; + /// \brief true if the file stream should be closed before to be destroyed. + bool ShouldClose; OutputFile(std::string filename, std::string tempFilename, - std::unique_ptr OS) + std::unique_ptr OS, bool ShouldClose = false) : Filename(std::move(filename)), TempFilename(std::move(tempFilename)), - OS(std::move(OS)) {} + OS(std::move(OS)), ShouldClose(ShouldClose) {} OutputFile(OutputFile &&O) : Filename(std::move(O.Filename)), - TempFilename(std::move(O.TempFilename)), OS(std::move(O.OS)) {} + TempFilename(std::move(O.TempFilename)), OS(std::move(O.OS)), + ShouldClose(O.ShouldClose) {} }; /// If the output doesn't support seeking (terminal, pipe). we switch Index: lib/Frontend/CompilerInstance.cpp =================================================================== --- lib/Frontend/CompilerInstance.cpp +++ lib/Frontend/CompilerInstance.cpp @@ -531,6 +531,12 @@ void CompilerInstance::clearOutputFiles(bool EraseFiles) { for (OutputFile &OF : OutputFiles) { // Manually close the stream before we rename it. + if (OF.OS && OF.ShouldClose) { + // Close the stream and clear error state for stdout. + auto *Stream = static_cast(OF.OS.get()); + Stream->close(); + Stream->clear_error(); + } OF.OS.reset(); if (!OF.TempFilename.empty()) { @@ -555,6 +561,11 @@ } OutputFiles.clear(); + if (NonSeekStream) { + // Close the stream and clear error state for stdout. + NonSeekStream->close(); + NonSeekStream->clear_error(); + } NonSeekStream.reset(); } @@ -593,7 +604,8 @@ // Add the output file -- but don't try to remove "-", since this means we are // using stdin. addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "", - TempPathName, std::move(OS))); + TempPathName, std::move(OS), + OutputPathName == "-" && !Binary)); return Ret; } Index: test/Frontend/empty.cpp =================================================================== --- test/Frontend/empty.cpp +++ test/Frontend/empty.cpp @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -verify %s -x c++ -triple x86_64-unknown-linux-gnu -dependency-file - -MT - -emit-llvm -o - | FileCheck %s +// Check that no additional error messages are generated. +int main() { + return; // expected-error {{non-void function 'main' should return a value}} +} +// CHECK: -: