From 46dc91e7d9a1b6dd0144e628519d06954b7b4e53 Mon Sep 17 00:00:00 2001 From: Abhina Sree Date: Mon, 21 Oct 2024 08:20:22 -0400 Subject: [SystemZ][z/OS] Add new openFileForReadBinary function, and pass IsText parameter to getBufferForFile (#111723) This patch adds an IsText parameter to the following getBufferForFile, getBufferForFileImpl. We introduce a new virtual function openFileForReadBinary which defaults to openFileForRead except in RealFileSystem which uses the OF_None flag instead of OF_Text. The default is set to OF_Text instead of OF_None, this change in value does not affect any other platforms other than z/OS. Setting this parameter correctly is required to open files on z/OS in the correct encoding. The IsText parameter is based on the context of where we open files, for example, in the ASTReader, HeaderMap requires that files always be opened in binary even though they might be tagged as text. --- llvm/lib/Support/VirtualFileSystem.cpp | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'llvm/lib/Support/VirtualFileSystem.cpp') diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp index 3e79ecf2fc7e..b3cdaa3eefc9 100644 --- a/llvm/lib/Support/VirtualFileSystem.cpp +++ b/llvm/lib/Support/VirtualFileSystem.cpp @@ -117,8 +117,9 @@ FileSystem::~FileSystem() = default; ErrorOr> FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize, - bool RequiresNullTerminator, bool IsVolatile) { - auto F = openFileForRead(Name); + bool RequiresNullTerminator, bool IsVolatile, + bool IsText) { + auto F = IsText ? openFileForRead(Name) : openFileForReadBinary(Name); if (!F) return F.getError(); @@ -279,6 +280,8 @@ public: ErrorOr status(const Twine &Path) override; ErrorOr> openFileForRead(const Twine &Path) override; + ErrorOr> + openFileForReadBinary(const Twine &Path) override; directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; llvm::ErrorOr getCurrentWorkingDirectory() const override; @@ -302,6 +305,17 @@ private: return Storage; } + ErrorOr> + openFileForReadWithFlags(const Twine &Name, sys::fs::OpenFlags Flags) { + SmallString<256> RealName, Storage; + Expected FDOrErr = sys::fs::openNativeFileForRead( + adjustPath(Name, Storage), Flags, &RealName); + if (!FDOrErr) + return errorToErrorCode(FDOrErr.takeError()); + return std::unique_ptr( + new RealFile(*FDOrErr, Name.str(), RealName.str())); + } + struct WorkingDirectory { // The current working directory, without symlinks resolved. (echo $PWD). SmallString<128> Specified; @@ -324,13 +338,12 @@ ErrorOr RealFileSystem::status(const Twine &Path) { ErrorOr> RealFileSystem::openFileForRead(const Twine &Name) { - SmallString<256> RealName, Storage; - Expected FDOrErr = sys::fs::openNativeFileForRead( - adjustPath(Name, Storage), sys::fs::OF_None, &RealName); - if (!FDOrErr) - return errorToErrorCode(FDOrErr.takeError()); - return std::unique_ptr( - new RealFile(*FDOrErr, Name.str(), RealName.str())); + return openFileForReadWithFlags(Name, sys::fs::OF_Text); +} + +ErrorOr> +RealFileSystem::openFileForReadBinary(const Twine &Name) { + return openFileForReadWithFlags(Name, sys::fs::OF_None); } llvm::ErrorOr RealFileSystem::getCurrentWorkingDirectory() const { -- cgit v1.2.3