Skip to content

Commit e331072

Browse files
abhina-sreetmsri
authored andcommitted
[SystemZ][z/OS] Propagate IsText parameter to open text files as text (llvm#107906)
This patch adds an IsText parameter to the following functions openFileForRead, getBufferForFile, getBufferForFileImpl and determines whether a file is text by querying the file tag on z/OS. 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.
1 parent 2b4dcef commit e331072

File tree

22 files changed

+105
-55
lines changed

22 files changed

+105
-55
lines changed

clang-tools-extra/clangd/FS.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ PreambleFileStatusCache::getProducingFS(
6464
: ProxyFileSystem(std::move(FS)), StatCache(StatCache) {}
6565

6666
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
67-
openFileForRead(const llvm::Twine &Path) override {
68-
auto File = getUnderlyingFS().openFileForRead(Path);
67+
openFileForRead(const llvm::Twine &Path, bool IsText = true) override {
68+
auto File = getUnderlyingFS().openFileForRead(Path, IsText);
6969
if (!File || !*File)
7070
return File;
7171
// Eagerly stat opened file, as the followup `status` call on the file

clang-tools-extra/clangd/Preamble.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,9 @@ class TimerFS : public llvm::vfs::ProxyFileSystem {
479479
: ProxyFileSystem(std::move(FS)) {}
480480

481481
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
482-
openFileForRead(const llvm::Twine &Path) override {
482+
openFileForRead(const llvm::Twine &Path, bool IsText = true) override {
483483
WallTimerRegion T(Timer);
484-
auto FileOr = getUnderlyingFS().openFileForRead(Path);
484+
auto FileOr = getUnderlyingFS().openFileForRead(Path, IsText);
485485
if (!FileOr)
486486
return FileOr;
487487
return std::make_unique<TimerFile>(Timer, std::move(FileOr.get()));

clang-tools-extra/clangd/support/ThreadsafeFS.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class VolatileFileSystem : public llvm::vfs::ProxyFileSystem {
2929
: ProxyFileSystem(std::move(FS)) {}
3030

3131
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
32-
openFileForRead(const llvm::Twine &InPath) override {
32+
openFileForRead(const llvm::Twine &InPath, bool IsText = true) override {
3333
llvm::SmallString<128> Path;
3434
InPath.toVector(Path);
3535

clang-tools-extra/clangd/unittests/ClangdTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ TEST(ClangdTests, PreambleVFSStatCache) {
10101010
: ProxyFileSystem(std::move(FS)), CountStats(CountStats) {}
10111011

10121012
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
1013-
openFileForRead(const Twine &Path) override {
1013+
openFileForRead(const Twine &Path, bool IsText = true) override {
10141014
++CountStats[llvm::sys::path::filename(Path.str())];
10151015
return ProxyFileSystem::openFileForRead(Path);
10161016
}

clang/include/clang/Basic/FileManager.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,21 +286,21 @@ class FileManager : public RefCountedBase<FileManager> {
286286
/// MemoryBuffer if successful, otherwise returning null.
287287
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
288288
getBufferForFile(FileEntryRef Entry, bool isVolatile = false,
289-
bool RequiresNullTerminator = true,
289+
bool RequiresNullTerminator = true, bool IsText = true,
290290
std::optional<int64_t> MaybeLimit = std::nullopt);
291291
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
292292
getBufferForFile(StringRef Filename, bool isVolatile = false,
293-
bool RequiresNullTerminator = true,
293+
bool RequiresNullTerminator = true, bool IsText = true,
294294
std::optional<int64_t> MaybeLimit = std::nullopt) const {
295295
return getBufferForFileImpl(Filename,
296296
/*FileSize=*/(MaybeLimit ? *MaybeLimit : -1),
297-
isVolatile, RequiresNullTerminator);
297+
isVolatile, RequiresNullTerminator, IsText);
298298
}
299299

300300
private:
301301
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
302302
getBufferForFileImpl(StringRef Filename, int64_t FileSize, bool isVolatile,
303-
bool RequiresNullTerminator) const;
303+
bool RequiresNullTerminator, bool IsText) const;
304304

305305
DirectoryEntry *&getRealDirEntry(const llvm::vfs::Status &Status);
306306

clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ class DependencyScanningWorkerFilesystem
346346

347347
llvm::ErrorOr<llvm::vfs::Status> status(const Twine &Path) override;
348348
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
349-
openFileForRead(const Twine &Path) override;
349+
openFileForRead(const Twine &Path, bool IsText = true) override;
350350

351351
std::error_code getRealPath(const Twine &Path,
352352
SmallVectorImpl<char> &Output) override;

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/Basic/SourceManager.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,18 @@ ContentCache::getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM,
121121
// Start with the assumption that the buffer is invalid to simplify early
122122
// return paths.
123123
IsBufferInvalid = true;
124-
125-
auto BufferOrError = FM.getBufferForFile(*ContentsEntry, IsFileVolatile);
124+
bool IsText = false;
125+
126+
#ifdef __MVS__
127+
// If the file is tagged with a text ccsid, it may require autoconversion.
128+
llvm::ErrorOr<bool> IsFileText =
129+
llvm::iszOSTextFile(ContentsEntry->getName().data());
130+
if (IsFileText)
131+
IsText = *IsFileText;
132+
#endif
133+
134+
auto BufferOrError = FM.getBufferForFile(
135+
*ContentsEntry, IsFileVolatile, /*RequiresNullTerminator=*/true, IsText);
126136

127137
// If we were unable to open the file, then we are in an inconsistent
128138
// situation where the content cache referenced a file which no longer

clang/lib/Serialization/ASTReader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5317,7 +5317,8 @@ std::string ASTReader::getOriginalSourceFile(
53175317
const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
53185318
// Open the AST file.
53195319
auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false,
5320-
/*RequiresNullTerminator=*/false);
5320+
/*RequiresNullTerminator=*/false,
5321+
/*IsText=*/true);
53215322
if (!Buffer) {
53225323
Diags.Report(diag::err_fe_unable_to_read_pch_file)
53235324
<< ASTFileName << Buffer.getError().message();

clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,13 @@ DepScanFile::create(EntryRef Entry) {
353353
}
354354

355355
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
356-
DependencyScanningWorkerFilesystem::openFileForRead(const Twine &Path) {
356+
DependencyScanningWorkerFilesystem::openFileForRead(const Twine &Path,
357+
bool IsText) {
357358
SmallString<256> OwnedFilename;
358359
StringRef Filename = Path.toStringRef(OwnedFilename);
359360

360361
if (shouldBypass(Filename))
361-
return getUnderlyingFS().openFileForRead(Path);
362+
return getUnderlyingFS().openFileForRead(Path, IsText);
362363

363364
llvm::ErrorOr<EntryRef> Result = getOrCreateFileSystemEntry(Filename);
364365
if (!Result)

0 commit comments

Comments
 (0)