diff --git a/clang/lib/Frontend/Rewrite/FrontendActions.cpp b/clang/lib/Frontend/Rewrite/FrontendActions.cpp --- a/clang/lib/Frontend/Rewrite/FrontendActions.cpp +++ b/clang/lib/Frontend/Rewrite/FrontendActions.cpp @@ -24,6 +24,7 @@ #include "clang/Serialization/ModuleFile.h" #include "clang/Serialization/ModuleManager.h" #include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/Triple.h" #include "llvm/Support/CrashRecoveryContext.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" @@ -184,8 +185,10 @@ void RewriteMacrosAction::ExecuteAction() { CompilerInstance &CI = getCompilerInstance(); - std::unique_ptr OS = - CI.createDefaultOutputFile(/*Binary=*/false, getCurrentFileOrBufferName()); + // On Windows, set binary mode to avoid CRLF translation. + std::unique_ptr OS = CI.createDefaultOutputFile( + /*Binary=*/llvm::Triple(LLVM_HOST_TRIPLE).isOSWindows(), + getCurrentFileOrBufferName()); if (!OS) return; RewriteMacrosInInput(CI.getPreprocessor(), OS.get()); @@ -193,8 +196,10 @@ void RewriteTestAction::ExecuteAction() { CompilerInstance &CI = getCompilerInstance(); - std::unique_ptr OS = - CI.createDefaultOutputFile(false, getCurrentFileOrBufferName()); + // On Windows, set binary mode to avoid CRLF translation. + std::unique_ptr OS = CI.createDefaultOutputFile( + /*Binary=*/llvm::Triple(LLVM_HOST_TRIPLE).isOSWindows(), + getCurrentFileOrBufferName()); if (!OS) return; DoRewriteTest(CI.getPreprocessor(), OS.get()); diff --git a/llvm/lib/TableGen/Main.cpp b/llvm/lib/TableGen/Main.cpp --- a/llvm/lib/TableGen/Main.cpp +++ b/llvm/lib/TableGen/Main.cpp @@ -17,6 +17,7 @@ #include "llvm/TableGen/Main.h" #include "TGParser.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/Triple.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" @@ -90,10 +91,13 @@ Records.startPhaseTiming(); // Parse the input file. + // On Windows, set binary mode to turn off CRLF translation. + bool IsWindows = llvm::Triple(LLVM_HOST_TRIPLE).isOSWindows(); Records.startTimer("Parse, build records"); ErrorOr> FileOrErr = - MemoryBuffer::getFileOrSTDIN(InputFilename, /*IsText=*/true); + MemoryBuffer::getFileOrSTDIN(InputFilename, + IsWindows ? /*IsText=*/false : true); if (std::error_code EC = FileOrErr.getError()) return reportError(argv0, "Could not open input file '" + InputFilename + "': " + EC.message() + "\n"); @@ -137,8 +141,8 @@ // Only updates the real output file if there are any differences. // This prevents recompilation of all the files depending on it if there // aren't any. - if (auto ExistingOrErr = - MemoryBuffer::getFile(OutputFilename, /*IsText=*/true)) + if (auto ExistingOrErr = MemoryBuffer::getFile( + OutputFilename, IsWindows ? /*IsText=*/false : true)) if (std::move(ExistingOrErr.get())->getBuffer() == Out.str()) WriteFile = false; }