1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-18 10:32:48 +02:00

[SystemZ][z/OS][Windows] Add new OF_TextWithCRLF flag and use this flag instead of OF_Text

Problem:
On SystemZ we need to open text files in text mode. On Windows, files opened in text mode adds a CRLF '\r\n' which may not be desirable.

Solution:
This patch adds two new flags

  - OF_CRLF which indicates that CRLF translation is used.
  - OF_TextWithCRLF = OF_Text | OF_CRLF indicates that the file is text and uses CRLF translation.

Developers should now use either the OF_Text or OF_TextWithCRLF for text files and OF_None for binary files. If the developer doesn't want carriage returns on Windows, they should use OF_Text, if they do want carriage returns on Windows, they should use OF_TextWithCRLF.

So this is the behaviour per platform with my patch:

z/OS:
OF_None: open in binary mode
OF_Text : open in text mode
OF_TextWithCRLF: open in text mode

Windows:
OF_None: open file with no carriage return
OF_Text: open file with no carriage return
OF_TextWithCRLF: open file with carriage return

The Major change is in llvm/lib/Support/Windows/Path.inc to only set text mode if the OF_CRLF is set.
```
  if (Flags & OF_CRLF)
    CrtOpenFlags |= _O_TEXT;
```

These following files are the ones that still use OF_Text which I left unchanged. I modified all these except raw_ostream.cpp in recent patches so I know these were previously in Binary mode on Windows.
./llvm/lib/Support/raw_ostream.cpp
./llvm/lib/TableGen/Main.cpp
./llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
./llvm/unittests/Support/Path.cpp
./clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp
./clang/lib/Frontend/CompilerInstance.cpp
./clang/lib/Driver/Driver.cpp
./clang/lib/Driver/ToolChains/Clang.cpp

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D99426
This commit is contained in:
Abhina Sreeskantharajan 2021-04-06 07:22:41 -04:00
parent 92b30fa263
commit 3f0b170fdd
40 changed files with 75 additions and 57 deletions

View File

@ -97,7 +97,7 @@ public:
errs() << "Writing '" << Filename << "'...";
raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
std::string Title = GraphName + " for '" + F.getName().str() + "' function";
@ -160,7 +160,7 @@ public:
errs() << "Writing '" << Filename << "'...";
raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
if (!EC)

View File

@ -742,24 +742,34 @@ enum OpenFlags : unsigned {
OF_None = 0,
F_None = 0, // For compatibility
/// The file should be opened in text mode on platforms that make this
/// distinction.
/// The file should be opened in text mode on platforms like z/OS that make
/// this distinction.
OF_Text = 1,
F_Text = 1, // For compatibility
/// The file should use a carriage linefeed '\r\n'. This flag should only be
/// used with OF_Text. Only makes a difference on Windows.
OF_CRLF = 2,
/// The file should be opened in text mode and use a carriage linefeed '\r\n'.
/// This flag has the same functionality as OF_Text on z/OS but adds a
/// carriage linefeed on Windows.
OF_TextWithCRLF = OF_Text | OF_CRLF,
/// The file should be opened in append mode.
OF_Append = 2,
F_Append = 2, // For compatibility
OF_Append = 4,
F_Append = 4, // For compatibility
/// Delete the file on close. Only makes a difference on windows.
OF_Delete = 4,
OF_Delete = 8,
/// When a child process is launched, this file should remain open in the
/// child process.
OF_ChildInherit = 8,
OF_ChildInherit = 16,
/// Force files Atime to be updated on access. Only makes a difference on windows.
OF_UpdateAtime = 16,
/// Force files Atime to be updated on access. Only makes a difference on
/// Windows.
OF_UpdateAtime = 32,
};
/// Create a potentially unique file name but does not create it.

View File

@ -859,7 +859,7 @@ bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
std::string GraphFileName = FullyQualifiedName + "." + RS.str() +
".pbqpgraph";
std::error_code EC;
raw_fd_ostream OS(GraphFileName, EC, sys::fs::OF_Text);
raw_fd_ostream OS(GraphFileName, EC, sys::fs::OF_TextWithCRLF);
LLVM_DEBUG(dbgs() << "Dumping graph for round " << Round << " to \""
<< GraphFileName << "\"\n");
G.dump(OS);

View File

@ -412,7 +412,7 @@ void LLVMDumpModule(LLVMModuleRef M) {
LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
char **ErrorMessage) {
std::error_code EC;
raw_fd_ostream dest(Filename, EC, sys::fs::OF_Text);
raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
if (EC) {
*ErrorMessage = strdup(EC.message().c_str());
return true;

View File

@ -106,7 +106,7 @@ Expected<std::unique_ptr<ToolOutputFile>> llvm::setupLLVMOptimizationRemarks(
return make_error<LLVMRemarkSetupFormatError>(std::move(E));
std::error_code EC;
auto Flags = *Format == remarks::Format::YAML ? sys::fs::OF_Text
auto Flags = *Format == remarks::Format::YAML ? sys::fs::OF_TextWithCRLF
: sys::fs::OF_None;
auto RemarksFile =
std::make_unique<ToolOutputFile>(RemarksFilename, EC, Flags);

View File

@ -85,8 +85,9 @@ Error Config::addSaveTemps(std::string OutputFileName,
ShouldDiscardValueNames = false;
std::error_code EC;
ResolutionFile = std::make_unique<raw_fd_ostream>(
OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::OF_Text);
ResolutionFile =
std::make_unique<raw_fd_ostream>(OutputFileName + "resolution.txt", EC,
sys::fs::OpenFlags::OF_TextWithCRLF);
if (EC) {
ResolutionFile.reset();
return errorCodeToError(EC);

View File

@ -776,8 +776,9 @@ bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
raw_fd_ostream *OS = getContext().getSecureLog();
if (!OS) {
std::error_code EC;
auto NewOS = std::make_unique<raw_fd_ostream>(
StringRef(SecureLogFile), EC, sys::fs::OF_Append | sys::fs::OF_Text);
auto NewOS = std::make_unique<raw_fd_ostream>(StringRef(SecureLogFile), EC,
sys::fs::OF_Append |
sys::fs::OF_TextWithCRLF);
if (EC)
return Error(IDLoc, Twine("can't open secure log file: ") +
SecureLogFile + " (" + EC.message() + ")");

View File

@ -866,7 +866,7 @@ void Context::print(StringRef filename, StringRef gcno, StringRef gcda,
Optional<raw_fd_ostream> os;
if (!options.UseStdout) {
std::error_code ec;
os.emplace(gcovName, ec, sys::fs::OF_Text);
os.emplace(gcovName, ec, sys::fs::OF_TextWithCRLF);
if (ec) {
errs() << ec.message() << '\n';
continue;
@ -881,7 +881,7 @@ void Context::print(StringRef filename, StringRef gcno, StringRef gcda,
// (PR GCC/82702). We create just one file.
std::string outputPath(sys::path::filename(filename));
std::error_code ec;
raw_fd_ostream os(outputPath + ".gcov", ec, sys::fs::OF_Text);
raw_fd_ostream os(outputPath + ".gcov", ec, sys::fs::OF_TextWithCRLF);
if (ec) {
errs() << ec.message() << '\n';
return;

View File

@ -728,7 +728,7 @@ SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
Format == SPF_Compact_Binary)
OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_None));
else
OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_Text));
OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_TextWithCRLF));
if (EC)
return EC;

View File

@ -241,7 +241,7 @@ std::error_code FileCollector::writeMapping(StringRef MappingFile) {
VFSWriter.setUseExternalNames(false);
std::error_code EC;
raw_fd_ostream os(MappingFile, EC, sys::fs::OF_Text);
raw_fd_ostream os(MappingFile, EC, sys::fs::OF_TextWithCRLF);
if (EC)
return EC;

View File

@ -259,7 +259,7 @@ static ErrorOr<std::unique_ptr<MB>>
getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
bool IsText, bool RequiresNullTerminator, bool IsVolatile) {
Expected<sys::fs::file_t> FDOrErr = sys::fs::openNativeFileForRead(
Filename, IsText ? sys::fs::OF_Text : sys::fs::OF_None);
Filename, IsText ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None);
if (!FDOrErr)
return errorToErrorCode(FDOrErr.takeError());
sys::fs::file_t FD = *FDOrErr;

View File

@ -304,7 +304,7 @@ Error llvm::timeTraceProfilerWrite(StringRef PreferredFileName,
}
std::error_code EC;
raw_fd_ostream OS(Path, EC, sys::fs::OF_Text);
raw_fd_ostream OS(Path, EC, sys::fs::OF_TextWithCRLF);
if (EC)
return createStringError(EC, "Could not open " + Path);

View File

@ -82,7 +82,7 @@ std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {
// info output file before running commands which write to it.
std::error_code EC;
auto Result = std::make_unique<raw_fd_ostream>(
OutputFilename, EC, sys::fs::OF_Append | sys::fs::OF_Text);
OutputFilename, EC, sys::fs::OF_Append | sys::fs::OF_TextWithCRLF);
if (!EC)
return Result;

View File

@ -507,7 +507,7 @@ std::error_code
llvm::sys::writeFileWithEncoding(StringRef FileName, StringRef Contents,
WindowsEncodingMethod Encoding /*unused*/) {
std::error_code EC;
llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OpenFlags::OF_Text);
llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OpenFlags::OF_TextWithCRLF);
if (EC)
return EC;

View File

@ -1083,8 +1083,10 @@ static std::error_code nativeFileToFd(Expected<HANDLE> H, int &ResultFD,
if (Flags & OF_Append)
CrtOpenFlags |= _O_APPEND;
if (Flags & OF_Text)
if (Flags & OF_CRLF) {
assert(Flags & OF_Text && "Flags set OF_CRLF without OF_Text");
CrtOpenFlags |= _O_TEXT;
}
ResultFD = -1;
if (!H)

View File

@ -506,7 +506,7 @@ std::error_code
llvm::sys::writeFileWithEncoding(StringRef FileName, StringRef Contents,
WindowsEncodingMethod Encoding) {
std::error_code EC;
llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OF_Text);
llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OF_TextWithCRLF);
if (EC)
return EC;

View File

@ -2413,7 +2413,7 @@ void AADepGraph::dumpGraph() {
std::error_code EC;
raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
if (!EC)
llvm::WriteGraph(File, this);

View File

@ -1722,7 +1722,7 @@ bool LowerTypeTestsModule::runForTesting(Module &M) {
ExitOnError ExitOnErr("-lowertypetests-write-summary: " + ClWriteSummary +
": ");
std::error_code EC;
raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::OF_Text);
raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::OF_TextWithCRLF);
ExitOnErr(errorCodeToError(EC));
yaml::Output Out(OS);

View File

@ -918,7 +918,7 @@ bool DevirtModule::runForTesting(
ExitOnErr(errorCodeToError(EC));
WriteIndexToFile(*Summary, OS);
} else {
raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::OF_Text);
raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::OF_TextWithCRLF);
ExitOnErr(errorCodeToError(EC));
yaml::Output Out(OS);
Out << *Summary;

View File

@ -440,7 +440,7 @@ static void writeJSON(StringRef OrigDIVerifyBugsReportFilePath,
llvm::json::Array &Bugs) {
std::error_code EC;
raw_fd_ostream OS_FILE{OrigDIVerifyBugsReportFilePath, EC,
sys::fs::OF_Append | sys::fs::OF_Text};
sys::fs::OF_Append | sys::fs::OF_TextWithCRLF};
if (EC) {
errs() << "Could not open file: " << EC.message() << ", "
<< OrigDIVerifyBugsReportFilePath << '\n';

View File

@ -312,7 +312,7 @@ static Error createPlistFile(StringRef Bin, StringRef BundleRoot,
SmallString<128> InfoPlist(BundleRoot);
sys::path::append(InfoPlist, "Contents/Info.plist");
std::error_code EC;
raw_fd_ostream PL(InfoPlist, EC, sys::fs::OF_Text);
raw_fd_ostream PL(InfoPlist, EC, sys::fs::OF_TextWithCRLF);
if (EC)
return make_error<StringError>(
"cannot create Plist: " + toString(errorCodeToError(EC)), EC);

View File

@ -276,7 +276,7 @@ static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
std::error_code EC;
sys::fs::OpenFlags OpenFlags = sys::fs::OF_None;
if (!Binary)
OpenFlags |= sys::fs::OF_Text;
OpenFlags |= sys::fs::OF_TextWithCRLF;
auto FDOut = std::make_unique<ToolOutputFile>(OutputFilename, EC, OpenFlags);
if (EC) {
reportError(EC.message());

View File

@ -791,7 +791,8 @@ static std::function<void(Module &)> createDebugDumper() {
case DumpKind::DumpModsToDisk:
return [](Module &M) {
std::error_code EC;
raw_fd_ostream Out(M.getModuleIdentifier() + ".ll", EC, sys::fs::OF_Text);
raw_fd_ostream Out(M.getModuleIdentifier() + ".ll", EC,
sys::fs::OF_TextWithCRLF);
if (EC) {
errs() << "Couldn't open " << M.getModuleIdentifier()
<< " for dumping.\nError:" << EC.message() << "\n";

View File

@ -145,7 +145,7 @@ int main(int argc, const char *argv[]) {
exitWithErrorCode(RemappingBufOrError.getError(), RemappingFile);
std::error_code EC;
raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_Text);
raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_TextWithCRLF);
if (EC)
exitWithErrorCode(EC, OutputFilename);

View File

@ -203,7 +203,7 @@ int main(int argc, char **argv) {
std::error_code EC;
std::unique_ptr<ToolOutputFile> Out(
new ToolOutputFile(FinalFilename, EC, sys::fs::OF_Text));
new ToolOutputFile(FinalFilename, EC, sys::fs::OF_TextWithCRLF));
if (EC) {
errs() << EC.message() << '\n';
return 1;

View File

@ -628,7 +628,7 @@ int main(int argc, char **argv) {
}
std::error_code EC;
ToolOutputFile OutputFile(OutputFilename, EC, sys::fs::OF_Text);
ToolOutputFile OutputFile(OutputFilename, EC, sys::fs::OF_TextWithCRLF);
error("Unable to open output file" + OutputFilename, EC);
// Don't remove output file if we exit with an error.
OutputFile.keep();

View File

@ -401,8 +401,9 @@ Error InstructionBenchmark::writeYaml(const LLVMState &State,
return Err;
} else {
int ResultFD = 0;
if (auto E = errorCodeToError(openFileForWrite(
Filename, ResultFD, sys::fs::CD_CreateAlways, sys::fs::OF_Text))) {
if (auto E = errorCodeToError(openFileForWrite(Filename, ResultFD,
sys::fs::CD_CreateAlways,
sys::fs::OF_TextWithCRLF))) {
return E;
}
raw_fd_ostream Ostr(ResultFD, true /*shouldClose*/);

View File

@ -468,7 +468,8 @@ int main(int argc, char **argv) {
std::error_code EC;
ToolOutputFile Out(OutputFilename, EC,
OutputAssembly ? sys::fs::OF_Text : sys::fs::OF_None);
OutputAssembly ? sys::fs::OF_TextWithCRLF
: sys::fs::OF_None);
if (EC) {
WithColor::error() << EC.message() << '\n';
return 1;

View File

@ -452,7 +452,8 @@ int main(int argc, char **argv) {
FeaturesStr = Features.getString();
}
sys::fs::OpenFlags Flags = (FileType == OFT_AssemblyFile) ? sys::fs::OF_Text
sys::fs::OpenFlags Flags = (FileType == OFT_AssemblyFile)
? sys::fs::OF_TextWithCRLF
: sys::fs::OF_None;
std::unique_ptr<ToolOutputFile> Out = GetOutputStream(OutputFilename, Flags);
if (!Out)

View File

@ -244,8 +244,8 @@ ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() {
if (OutputFilename == "")
OutputFilename = "-";
std::error_code EC;
auto Out =
std::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::OF_Text);
auto Out = std::make_unique<ToolOutputFile>(OutputFilename, EC,
sys::fs::OF_TextWithCRLF);
if (!EC)
return std::move(Out);
return EC;

View File

@ -247,7 +247,7 @@ static bool readLocationInfo(LocationInfoTy &LocationInfo) {
static bool writeReport(LocationInfoTy &LocationInfo) {
std::error_code EC;
llvm::raw_fd_ostream OS(OutputFileName, EC, llvm::sys::fs::OF_Text);
llvm::raw_fd_ostream OS(OutputFileName, EC, llvm::sys::fs::OF_TextWithCRLF);
if (EC) {
WithColor::error() << "Can't open file " << OutputFileName << ": "
<< EC.message() << "\n";

View File

@ -306,7 +306,7 @@ static void writeInstrProfile(StringRef OutputFilename,
InstrProfWriter &Writer) {
std::error_code EC;
raw_fd_ostream Output(OutputFilename.data(), EC,
OutputFormat == PF_Text ? sys::fs::OF_Text
OutputFormat == PF_Text ? sys::fs::OF_TextWithCRLF
: sys::fs::OF_None);
if (EC)
exitWithErrorCode(EC, OutputFilename);
@ -1931,7 +1931,7 @@ static int overlap_main(int argc, const char *argv[]) {
cl::ParseCommandLineOptions(argc, argv, "LLVM profile data overlap tool\n");
std::error_code EC;
raw_fd_ostream OS(Output.data(), EC, sys::fs::OF_Text);
raw_fd_ostream OS(Output.data(), EC, sys::fs::OF_TextWithCRLF);
if (EC)
exitWithErrorCode(EC, Output);
@ -2457,7 +2457,7 @@ static int show_main(int argc, const char *argv[]) {
}
std::error_code EC;
raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_Text);
raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_TextWithCRLF);
if (EC)
exitWithErrorCode(EC, OutputFilename);

View File

@ -459,7 +459,7 @@ static CommandRegistration Unused(&Account, []() -> Error {
}
std::error_code EC;
raw_fd_ostream OS(AccountOutput, EC, sys::fs::OpenFlags::OF_Text);
raw_fd_ostream OS(AccountOutput, EC, sys::fs::OpenFlags::OF_TextWithCRLF);
if (EC)
return make_error<StringError>(
Twine("Cannot open file '") + AccountOutput + "' for writing.", EC);

View File

@ -381,7 +381,7 @@ static CommandRegistration Unused(&Convert, []() -> Error {
raw_fd_ostream OS(ConvertOutput, EC,
ConvertOutputFormat == ConvertFormats::BINARY
? sys::fs::OpenFlags::OF_None
: sys::fs::OpenFlags::OF_Text);
: sys::fs::OpenFlags::OF_TextWithCRLF);
if (EC)
return make_error<StringError>(
Twine("Cannot open file '") + ConvertOutput + "' for writing.", EC);

View File

@ -83,7 +83,7 @@ static CommandRegistration Unused(&Extract, []() -> Error {
InstrumentationMapOrError.takeError());
std::error_code EC;
raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::OF_Text);
raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::OF_TextWithCRLF);
if (EC)
return make_error<StringError>(
Twine("Cannot open file '") + ExtractOutput + "' for writing.", EC);

View File

@ -456,7 +456,7 @@ static CommandRegistration Unused(&GraphDiff, []() -> Error {
auto &GDR = *GDROrErr;
std::error_code EC;
raw_fd_ostream OS(GraphDiffOutput, EC, sys::fs::OpenFlags::OF_Text);
raw_fd_ostream OS(GraphDiffOutput, EC, sys::fs::OpenFlags::OF_TextWithCRLF);
if (EC)
return make_error<StringError>(
Twine("Cannot open file '") + GraphDiffOutput + "' for writing.", EC);

View File

@ -523,7 +523,7 @@ static CommandRegistration Unused(&GraphC, []() -> Error {
auto &GR = *GROrError;
std::error_code EC;
raw_fd_ostream OS(GraphOutput, EC, sys::fs::OpenFlags::OF_Text);
raw_fd_ostream OS(GraphOutput, EC, sys::fs::OpenFlags::OF_TextWithCRLF);
if (EC)
return make_error<StringError>(
Twine("Cannot open file '") + GraphOutput + "' for writing.", EC);

View File

@ -700,8 +700,8 @@ int main(int argc, char **argv) {
OutputFilename = "-";
std::error_code EC;
sys::fs::OpenFlags Flags = OutputAssembly ? sys::fs::OF_Text
: sys::fs::OF_None;
sys::fs::OpenFlags Flags =
OutputAssembly ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None;
Out.reset(new ToolOutputFile(OutputFilename, EC, Flags));
if (EC) {
errs() << EC.message() << '\n';

View File

@ -136,7 +136,7 @@ bool TempFile::writeBitcode(const Module &M) const {
bool TempFile::writeAssembly(const Module &M) const {
LLVM_DEBUG(dbgs() << " - write assembly\n");
std::error_code EC;
raw_fd_ostream OS(Filename, EC, sys::fs::OF_Text);
raw_fd_ostream OS(Filename, EC, sys::fs::OF_TextWithCRLF);
if (EC) {
errs() << "verify-uselistorder: error: " << EC.message() << "\n";
return true;

View File

@ -1253,7 +1253,7 @@ TEST_F(FileSystemTest, CarriageReturn) {
path::append(FilePathname, "test");
{
raw_fd_ostream File(FilePathname, EC, sys::fs::OF_Text);
raw_fd_ostream File(FilePathname, EC, sys::fs::OF_TextWithCRLF);
ASSERT_NO_ERROR(EC);
File << '\n';
}