Skip to content

Commit c1676e4

Browse files
committed
[SystemZ][z/OS] Add new openFileForReadBinary function, and pass IsText parameter to getBufferForFile
1 parent d905a3c commit c1676e4

File tree

6 files changed

+48
-23
lines changed

6 files changed

+48
-23
lines changed

clang/include/clang/Basic/FileManager.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,21 +292,21 @@ class FileManager : public RefCountedBase<FileManager> {
292292
/// MemoryBuffer if successful, otherwise returning null.
293293
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
294294
getBufferForFile(FileEntryRef Entry, bool isVolatile = false,
295-
bool RequiresNullTerminator = true,
295+
bool RequiresNullTerminator = true, bool IsText = true,
296296
std::optional<int64_t> MaybeLimit = std::nullopt);
297297
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
298298
getBufferForFile(StringRef Filename, bool isVolatile = false,
299-
bool RequiresNullTerminator = true,
299+
bool RequiresNullTerminator = true, bool IsText = true,
300300
std::optional<int64_t> MaybeLimit = std::nullopt) const {
301301
return getBufferForFileImpl(Filename,
302302
/*FileSize=*/MaybeLimit.value_or(-1),
303-
isVolatile, RequiresNullTerminator);
303+
isVolatile, RequiresNullTerminator, IsText);
304304
}
305305

306306
private:
307307
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
308308
getBufferForFileImpl(StringRef Filename, int64_t FileSize, bool isVolatile,
309-
bool RequiresNullTerminator) const;
309+
bool RequiresNullTerminator, bool IsText) const;
310310

311311
DirectoryEntry *&getRealDirEntry(const llvm::vfs::Status &Status);
312312

clang/lib/Basic/FileManager.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) {
530530

531531
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
532532
FileManager::getBufferForFile(FileEntryRef FE, bool isVolatile,
533-
bool RequiresNullTerminator,
533+
bool RequiresNullTerminator, bool IsText,
534534
std::optional<int64_t> MaybeLimit) {
535535
const FileEntry *Entry = &FE.getFileEntry();
536536
// If the content is living on the file entry, return a reference to it.
@@ -558,21 +558,21 @@ FileManager::getBufferForFile(FileEntryRef FE, bool isVolatile,
558558

559559
// Otherwise, open the file.
560560
return getBufferForFileImpl(Filename, FileSize, isVolatile,
561-
RequiresNullTerminator);
561+
RequiresNullTerminator, IsText);
562562
}
563563

564564
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
565565
FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
566-
bool isVolatile,
567-
bool RequiresNullTerminator) const {
566+
bool isVolatile, bool RequiresNullTerminator,
567+
bool IsText) const {
568568
if (FileSystemOpts.WorkingDir.empty())
569569
return FS->getBufferForFile(Filename, FileSize, RequiresNullTerminator,
570-
isVolatile);
570+
isVolatile, IsText);
571571

572572
SmallString<128> FilePath(Filename);
573573
FixupRelativePath(FilePath);
574574
return FS->getBufferForFile(FilePath, FileSize, RequiresNullTerminator,
575-
isVolatile);
575+
isVolatile, IsText);
576576
}
577577

578578
/// getStatValue - Get the 'stat' information for the specified path,

clang/lib/Lex/HeaderMap.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ std::unique_ptr<HeaderMap> HeaderMap::Create(FileEntryRef FE, FileManager &FM) {
5454
unsigned FileSize = FE.getSize();
5555
if (FileSize <= sizeof(HMapHeader)) return nullptr;
5656

57-
auto FileBuffer = FM.getBufferForFile(FE);
57+
auto FileBuffer =
58+
FM.getBufferForFile(FE, /*IsVolatile=*/false,
59+
/*RequiresNullTerminator=*/true, /*IsText=*/false);
5860
if (!FileBuffer || !*FileBuffer)
5961
return nullptr;
6062
bool NeedsByteSwap;

clang/lib/Serialization/ASTReader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5333,7 +5333,8 @@ std::string ASTReader::getOriginalSourceFile(
53335333
const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
53345334
// Open the AST file.
53355335
auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false,
5336-
/*RequiresNullTerminator=*/false);
5336+
/*RequiresNullTerminator=*/false,
5337+
/*IsText=*/false);
53375338
if (!Buffer) {
53385339
Diags.Report(diag::err_fe_unable_to_read_pch_file)
53395340
<< ASTFileName << Buffer.getError().message();

llvm/include/llvm/Support/VirtualFileSystem.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,24 @@ class FileSystem : public llvm::ThreadSafeRefCountedBase<FileSystem>,
271271
/// Get the status of the entry at \p Path, if one exists.
272272
virtual llvm::ErrorOr<Status> status(const Twine &Path) = 0;
273273

274-
/// Get a \p File object for the file at \p Path, if one exists.
274+
/// Get a \p File object for the text file at \p Path, if one exists.
275275
virtual llvm::ErrorOr<std::unique_ptr<File>>
276276
openFileForRead(const Twine &Path) = 0;
277277

278+
/// Get a \p File objct for the binary file at \p Path, if one exists.
279+
/// This function should be called instead of openFileForRead if the file
280+
/// should be opened as a binary file.
281+
virtual llvm::ErrorOr<std::unique_ptr<File>>
282+
openFileForReadBinary(const Twine &Path) {
283+
return openFileForRead(Path);
284+
}
285+
278286
/// This is a convenience method that opens a file, gets its content and then
279287
/// closes the file.
280288
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
281289
getBufferForFile(const Twine &Name, int64_t FileSize = -1,
282-
bool RequiresNullTerminator = true, bool IsVolatile = false);
290+
bool RequiresNullTerminator = true, bool IsVolatile = false,
291+
bool IsText = true);
283292

284293
/// Get a directory_iterator for \p Dir.
285294
/// \note The 'end' iterator is directory_iterator().

llvm/lib/Support/VirtualFileSystem.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ FileSystem::~FileSystem() = default;
117117

118118
ErrorOr<std::unique_ptr<MemoryBuffer>>
119119
FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize,
120-
bool RequiresNullTerminator, bool IsVolatile) {
121-
auto F = openFileForRead(Name);
120+
bool RequiresNullTerminator, bool IsVolatile,
121+
bool IsText) {
122+
auto F = IsText ? openFileForRead(Name) : openFileForReadBinary(Name);
122123
if (!F)
123124
return F.getError();
124125

@@ -279,6 +280,8 @@ class RealFileSystem : public FileSystem {
279280

280281
ErrorOr<Status> status(const Twine &Path) override;
281282
ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
283+
ErrorOr<std::unique_ptr<File>>
284+
openFileForReadBinary(const Twine &Path) override;
282285
directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
283286

284287
llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
@@ -302,6 +305,17 @@ class RealFileSystem : public FileSystem {
302305
return Storage;
303306
}
304307

308+
ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Name,
309+
sys::fs::OpenFlags Flags) {
310+
SmallString<256> RealName, Storage;
311+
Expected<file_t> FDOrErr = sys::fs::openNativeFileForRead(
312+
adjustPath(Name, Storage), Flags, &RealName);
313+
if (!FDOrErr)
314+
return errorToErrorCode(FDOrErr.takeError());
315+
return std::unique_ptr<File>(
316+
new RealFile(*FDOrErr, Name.str(), RealName.str()));
317+
}
318+
305319
struct WorkingDirectory {
306320
// The current working directory, without symlinks resolved. (echo $PWD).
307321
SmallString<128> Specified;
@@ -324,13 +338,12 @@ ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
324338

325339
ErrorOr<std::unique_ptr<File>>
326340
RealFileSystem::openFileForRead(const Twine &Name) {
327-
SmallString<256> RealName, Storage;
328-
Expected<file_t> FDOrErr = sys::fs::openNativeFileForRead(
329-
adjustPath(Name, Storage), sys::fs::OF_None, &RealName);
330-
if (!FDOrErr)
331-
return errorToErrorCode(FDOrErr.takeError());
332-
return std::unique_ptr<File>(
333-
new RealFile(*FDOrErr, Name.str(), RealName.str()));
341+
return openFileForRead(Name, sys::fs::OF_Text);
342+
}
343+
344+
ErrorOr<std::unique_ptr<File>>
345+
RealFileSystem::openFileForReadBinary(const Twine &Name) {
346+
return openFileForRead(Name, sys::fs::OF_None);
334347
}
335348

336349
llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const {

0 commit comments

Comments
 (0)