diff --git a/clang-tools-extra/clangd/FS.cpp b/clang-tools-extra/clangd/FS.cpp index 5729b9341d9d4..bd3c6440c24b0 100644 --- a/clang-tools-extra/clangd/FS.cpp +++ b/clang-tools-extra/clangd/FS.cpp @@ -64,8 +64,8 @@ PreambleFileStatusCache::getProducingFS( : ProxyFileSystem(std::move(FS)), StatCache(StatCache) {} llvm::ErrorOr> - openFileForRead(const llvm::Twine &Path) override { - auto File = getUnderlyingFS().openFileForRead(Path); + openFileForRead(const llvm::Twine &Path, bool IsText = true) override { + auto File = getUnderlyingFS().openFileForRead(Path, IsText); if (!File || !*File) return File; // Eagerly stat opened file, as the followup `status` call on the file diff --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp index c14c4d1ba103f..55cfe1c97fc6f 100644 --- a/clang-tools-extra/clangd/Preamble.cpp +++ b/clang-tools-extra/clangd/Preamble.cpp @@ -479,9 +479,9 @@ class TimerFS : public llvm::vfs::ProxyFileSystem { : ProxyFileSystem(std::move(FS)) {} llvm::ErrorOr> - openFileForRead(const llvm::Twine &Path) override { + openFileForRead(const llvm::Twine &Path, bool IsText = true) override { WallTimerRegion T(Timer); - auto FileOr = getUnderlyingFS().openFileForRead(Path); + auto FileOr = getUnderlyingFS().openFileForRead(Path, IsText); if (!FileOr) return FileOr; return std::make_unique(Timer, std::move(FileOr.get())); diff --git a/clang-tools-extra/clangd/support/ThreadsafeFS.cpp b/clang-tools-extra/clangd/support/ThreadsafeFS.cpp index 7398e4258527b..bc0b984e577cb 100644 --- a/clang-tools-extra/clangd/support/ThreadsafeFS.cpp +++ b/clang-tools-extra/clangd/support/ThreadsafeFS.cpp @@ -29,7 +29,7 @@ class VolatileFileSystem : public llvm::vfs::ProxyFileSystem { : ProxyFileSystem(std::move(FS)) {} llvm::ErrorOr> - openFileForRead(const llvm::Twine &InPath) override { + openFileForRead(const llvm::Twine &InPath, bool IsText = true) override { llvm::SmallString<128> Path; InPath.toVector(Path); diff --git a/clang-tools-extra/clangd/unittests/ClangdTests.cpp b/clang-tools-extra/clangd/unittests/ClangdTests.cpp index 643b8e9f12d75..e86385c2072b3 100644 --- a/clang-tools-extra/clangd/unittests/ClangdTests.cpp +++ b/clang-tools-extra/clangd/unittests/ClangdTests.cpp @@ -1010,7 +1010,7 @@ TEST(ClangdTests, PreambleVFSStatCache) { : ProxyFileSystem(std::move(FS)), CountStats(CountStats) {} llvm::ErrorOr> - openFileForRead(const Twine &Path) override { + openFileForRead(const Twine &Path, bool IsText = true) override { ++CountStats[llvm::sys::path::filename(Path.str())]; return ProxyFileSystem::openFileForRead(Path); } diff --git a/clang/include/clang/Basic/FileManager.h b/clang/include/clang/Basic/FileManager.h index ce4e8c1fbe16e..d987fb05a94a3 100644 --- a/clang/include/clang/Basic/FileManager.h +++ b/clang/include/clang/Basic/FileManager.h @@ -292,21 +292,21 @@ class FileManager : public RefCountedBase { /// MemoryBuffer if successful, otherwise returning null. llvm::ErrorOr> getBufferForFile(FileEntryRef Entry, bool isVolatile = false, - bool RequiresNullTerminator = true, + bool RequiresNullTerminator = true, bool IsText = true, std::optional MaybeLimit = std::nullopt); llvm::ErrorOr> getBufferForFile(StringRef Filename, bool isVolatile = false, - bool RequiresNullTerminator = true, + bool RequiresNullTerminator = true, bool IsText = true, std::optional MaybeLimit = std::nullopt) const { return getBufferForFileImpl(Filename, /*FileSize=*/MaybeLimit.value_or(-1), - isVolatile, RequiresNullTerminator); + isVolatile, RequiresNullTerminator, IsText); } private: llvm::ErrorOr> getBufferForFileImpl(StringRef Filename, int64_t FileSize, bool isVolatile, - bool RequiresNullTerminator) const; + bool RequiresNullTerminator, bool IsText) const; DirectoryEntry *&getRealDirEntry(const llvm::vfs::Status &Status); diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h index d12814e7c9253..635fdd0e00c43 100644 --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h @@ -346,7 +346,7 @@ class DependencyScanningWorkerFilesystem llvm::ErrorOr status(const Twine &Path) override; llvm::ErrorOr> - openFileForRead(const Twine &Path) override; + openFileForRead(const Twine &Path, bool IsText = true) override; std::error_code getRealPath(const Twine &Path, SmallVectorImpl &Output) override; diff --git a/clang/lib/Basic/FileManager.cpp b/clang/lib/Basic/FileManager.cpp index 6097b85a03064..27075cefafdc2 100644 --- a/clang/lib/Basic/FileManager.cpp +++ b/clang/lib/Basic/FileManager.cpp @@ -530,7 +530,7 @@ void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) { llvm::ErrorOr> FileManager::getBufferForFile(FileEntryRef FE, bool isVolatile, - bool RequiresNullTerminator, + bool RequiresNullTerminator, bool IsText, std::optional MaybeLimit) { const FileEntry *Entry = &FE.getFileEntry(); // If the content is living on the file entry, return a reference to it. @@ -558,21 +558,21 @@ FileManager::getBufferForFile(FileEntryRef FE, bool isVolatile, // Otherwise, open the file. return getBufferForFileImpl(Filename, FileSize, isVolatile, - RequiresNullTerminator); + RequiresNullTerminator, IsText); } llvm::ErrorOr> FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize, - bool isVolatile, - bool RequiresNullTerminator) const { + bool isVolatile, bool RequiresNullTerminator, + bool IsText) const { if (FileSystemOpts.WorkingDir.empty()) return FS->getBufferForFile(Filename, FileSize, RequiresNullTerminator, - isVolatile); + isVolatile, IsText); SmallString<128> FilePath(Filename); FixupRelativePath(FilePath); return FS->getBufferForFile(FilePath, FileSize, RequiresNullTerminator, - isVolatile); + isVolatile, IsText); } /// getStatValue - Get the 'stat' information for the specified path, diff --git a/clang/lib/Lex/HeaderMap.cpp b/clang/lib/Lex/HeaderMap.cpp index 00bf880726ee3..35c68b304a452 100644 --- a/clang/lib/Lex/HeaderMap.cpp +++ b/clang/lib/Lex/HeaderMap.cpp @@ -54,7 +54,9 @@ std::unique_ptr HeaderMap::Create(FileEntryRef FE, FileManager &FM) { unsigned FileSize = FE.getSize(); if (FileSize <= sizeof(HMapHeader)) return nullptr; - auto FileBuffer = FM.getBufferForFile(FE); + auto FileBuffer = + FM.getBufferForFile(FE, /*IsVolatile=*/false, + /*RequiresNullTerminator=*/true, /*IsText=*/false); if (!FileBuffer || !*FileBuffer) return nullptr; bool NeedsByteSwap; diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 8623c030b6d59..c575869a9061d 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -5328,7 +5328,8 @@ std::string ASTReader::getOriginalSourceFile( const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { // Open the AST file. auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false, - /*RequiresNullTerminator=*/false); + /*RequiresNullTerminator=*/false, + /*IsText=*/false); if (!Buffer) { Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << Buffer.getError().message(); diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp index 4d738e4bea41a..7d6239a0732fe 100644 --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp @@ -353,12 +353,13 @@ DepScanFile::create(EntryRef Entry) { } llvm::ErrorOr> -DependencyScanningWorkerFilesystem::openFileForRead(const Twine &Path) { +DependencyScanningWorkerFilesystem::openFileForRead(const Twine &Path, + bool IsText) { SmallString<256> OwnedFilename; StringRef Filename = Path.toStringRef(OwnedFilename); if (shouldBypass(Filename)) - return getUnderlyingFS().openFileForRead(Path); + return getUnderlyingFS().openFileForRead(Path, IsText); llvm::ErrorOr Result = getOrCreateFileSystemEntry(Filename); if (!Result) diff --git a/clang/unittests/Driver/DistroTest.cpp b/clang/unittests/Driver/DistroTest.cpp index 43efc0dd8f51e..4c13b8bbb9408 100644 --- a/clang/unittests/Driver/DistroTest.cpp +++ b/clang/unittests/Driver/DistroTest.cpp @@ -352,9 +352,9 @@ TEST(DistroTest, DetectWindowsAndCrossCompile) { } llvm::ErrorOr> - openFileForRead(const llvm::Twine &Path) override { + openFileForRead(const llvm::Twine &Path, bool IsText = true) override { ++Count; - return llvm::vfs::ProxyFileSystem::openFileForRead(Path); + return llvm::vfs::ProxyFileSystem::openFileForRead(Path, IsText); } unsigned Count{}; diff --git a/clang/unittests/Driver/ToolChainTest.cpp b/clang/unittests/Driver/ToolChainTest.cpp index 8542a168f93c2..672742cea5d6b 100644 --- a/clang/unittests/Driver/ToolChainTest.cpp +++ b/clang/unittests/Driver/ToolChainTest.cpp @@ -683,7 +683,7 @@ struct FileSystemWithError : public llvm::vfs::FileSystem { return std::make_error_code(std::errc::no_such_file_or_directory); } llvm::ErrorOr> - openFileForRead(const Twine &Path) override { + openFileForRead(const Twine &Path, bool IsText = true) override { return std::make_error_code(std::errc::permission_denied); } llvm::vfs::directory_iterator dir_begin(const Twine &Dir, diff --git a/clang/unittests/Frontend/PCHPreambleTest.cpp b/clang/unittests/Frontend/PCHPreambleTest.cpp index 2ce24c91ac0f1..137dcf2377be4 100644 --- a/clang/unittests/Frontend/PCHPreambleTest.cpp +++ b/clang/unittests/Frontend/PCHPreambleTest.cpp @@ -36,10 +36,10 @@ class ReadCountingInMemoryFileSystem : public vfs::InMemoryFileSystem std::map ReadCounts; public: - ErrorOr> openFileForRead(const Twine &Path) override - { + ErrorOr> + openFileForRead(const Twine &Path, bool IsText = true) override { ++ReadCounts[Canonicalize(Path)]; - return InMemoryFileSystem::openFileForRead(Path); + return InMemoryFileSystem::openFileForRead(Path, IsText); } unsigned GetReadCount(const Twine &Path) const diff --git a/clang/unittests/Tooling/DependencyScanning/DependencyScannerTest.cpp b/clang/unittests/Tooling/DependencyScanning/DependencyScannerTest.cpp index ec0e143be4a20..1012016ed03c7 100644 --- a/clang/unittests/Tooling/DependencyScanning/DependencyScannerTest.cpp +++ b/clang/unittests/Tooling/DependencyScanning/DependencyScannerTest.cpp @@ -277,9 +277,9 @@ TEST(DependencyScanner, ScanDepsWithModuleLookup) { } llvm::ErrorOr> - openFileForRead(const Twine &Path) override { + openFileForRead(const Twine &Path, bool IsText = true) override { ReadFiles.push_back(Path.str()); - return ProxyFileSystem::openFileForRead(Path); + return ProxyFileSystem::openFileForRead(Path, IsText); } }; diff --git a/lldb/unittests/Host/FileSystemTest.cpp b/lldb/unittests/Host/FileSystemTest.cpp index 58887f6b2467e..3c76f1d1bed21 100644 --- a/lldb/unittests/Host/FileSystemTest.cpp +++ b/lldb/unittests/Host/FileSystemTest.cpp @@ -59,7 +59,7 @@ class DummyFileSystem : public vfs::FileSystem { return I->second; } ErrorOr> - openFileForRead(const Twine &Path) override { + openFileForRead(const Twine &Path, bool Text = true) override { auto S = status(Path); if (S) return std::unique_ptr(new DummyFile{*S}); diff --git a/lldb/unittests/Utility/MockSymlinkFileSystem.h b/lldb/unittests/Utility/MockSymlinkFileSystem.h index 7fa1f93bfa38a..32a86cd669fb1 100644 --- a/lldb/unittests/Utility/MockSymlinkFileSystem.h +++ b/lldb/unittests/Utility/MockSymlinkFileSystem.h @@ -43,7 +43,7 @@ class MockSymlinkFileSystem : public llvm::vfs::FileSystem { return llvm::errc::operation_not_permitted; } llvm::ErrorOr> - openFileForRead(const llvm::Twine &Path) override { + openFileForRead(const llvm::Twine &Path, bool IsText = true) override { return llvm::errc::operation_not_permitted; } llvm::vfs::directory_iterator dir_begin(const llvm::Twine &Dir, diff --git a/llvm/include/llvm/Support/VirtualFileSystem.h b/llvm/include/llvm/Support/VirtualFileSystem.h index 2531c075f262d..2256fde2faa66 100644 --- a/llvm/include/llvm/Support/VirtualFileSystem.h +++ b/llvm/include/llvm/Support/VirtualFileSystem.h @@ -273,13 +273,14 @@ class FileSystem : public llvm::ThreadSafeRefCountedBase, /// Get a \p File object for the file at \p Path, if one exists. virtual llvm::ErrorOr> - openFileForRead(const Twine &Path) = 0; + openFileForRead(const Twine &Path, bool IsText = true) = 0; /// This is a convenience method that opens a file, gets its content and then /// closes the file. llvm::ErrorOr> getBufferForFile(const Twine &Name, int64_t FileSize = -1, - bool RequiresNullTerminator = true, bool IsVolatile = false); + bool RequiresNullTerminator = true, bool IsVolatile = false, + bool IsText = true); /// Get a directory_iterator for \p Dir. /// \note The 'end' iterator is directory_iterator(). @@ -392,7 +393,7 @@ class OverlayFileSystem : public RTTIExtends { llvm::ErrorOr status(const Twine &Path) override; bool exists(const Twine &Path) override; llvm::ErrorOr> - openFileForRead(const Twine &Path) override; + openFileForRead(const Twine &Path, bool IsText = true) override; directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; llvm::ErrorOr getCurrentWorkingDirectory() const override; std::error_code setCurrentWorkingDirectory(const Twine &Path) override; @@ -446,8 +447,8 @@ class ProxyFileSystem : public RTTIExtends { } bool exists(const Twine &Path) override { return FS->exists(Path); } llvm::ErrorOr> - openFileForRead(const Twine &Path) override { - return FS->openFileForRead(Path); + openFileForRead(const Twine &Path, bool IsText = true) override { + return FS->openFileForRead(Path, IsText); } directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override { return FS->dir_begin(Dir, EC); @@ -615,7 +616,7 @@ class InMemoryFileSystem : public RTTIExtends { llvm::ErrorOr status(const Twine &Path) override; llvm::ErrorOr> - openFileForRead(const Twine &Path) override; + openFileForRead(const Twine &Path, bool IsText = true) override; directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; llvm::ErrorOr getCurrentWorkingDirectory() const override { @@ -1051,7 +1052,8 @@ class RedirectingFileSystem ErrorOr status(const Twine &Path) override; bool exists(const Twine &Path) override; - ErrorOr> openFileForRead(const Twine &Path) override; + ErrorOr> openFileForRead(const Twine &Path, + bool IsText = true) override; std::error_code getRealPath(const Twine &Path, SmallVectorImpl &Output) override; @@ -1155,9 +1157,10 @@ class TracingFileSystem return ProxyFileSystem::status(Path); } - ErrorOr> openFileForRead(const Twine &Path) override { + ErrorOr> openFileForRead(const Twine &Path, + bool IsText = true) override { ++NumOpenFileForReadCalls; - return ProxyFileSystem::openFileForRead(Path); + return ProxyFileSystem::openFileForRead(Path, IsText); } directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override { diff --git a/llvm/lib/Support/FileCollector.cpp b/llvm/lib/Support/FileCollector.cpp index 29436f85c2f23..966c5ac90e941 100644 --- a/llvm/lib/Support/FileCollector.cpp +++ b/llvm/lib/Support/FileCollector.cpp @@ -268,8 +268,8 @@ class FileCollectorFileSystem : public vfs::FileSystem { } llvm::ErrorOr> - openFileForRead(const Twine &Path) override { - auto Result = FS->openFileForRead(Path); + openFileForRead(const Twine &Path, bool IsText = true) override { + auto Result = FS->openFileForRead(Path, IsText); if (Result && *Result) Collector->addFile(Path); return Result; diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp index 928c0b5a24ed6..4c671acd28521 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 = openFileForRead(Name, IsText); if (!F) return F.getError(); @@ -278,7 +279,8 @@ class RealFileSystem : public FileSystem { } ErrorOr status(const Twine &Path) override; - ErrorOr> openFileForRead(const Twine &Path) override; + ErrorOr> openFileForRead(const Twine &Path, + bool IsText = true) override; directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; llvm::ErrorOr getCurrentWorkingDirectory() const override; @@ -323,10 +325,11 @@ ErrorOr RealFileSystem::status(const Twine &Path) { } ErrorOr> -RealFileSystem::openFileForRead(const Twine &Name) { +RealFileSystem::openFileForRead(const Twine &Name, bool IsText) { SmallString<256> RealName, Storage; Expected FDOrErr = sys::fs::openNativeFileForRead( - adjustPath(Name, Storage), sys::fs::OF_None, &RealName); + adjustPath(Name, Storage), IsText ? sys::fs::OF_Text : sys::fs::OF_None, + &RealName); if (!FDOrErr) return errorToErrorCode(FDOrErr.takeError()); return std::unique_ptr( @@ -458,10 +461,10 @@ bool OverlayFileSystem::exists(const Twine &Path) { } ErrorOr> -OverlayFileSystem::openFileForRead(const llvm::Twine &Path) { +OverlayFileSystem::openFileForRead(const llvm::Twine &Path, bool IsText) { // FIXME: handle symlinks that cross file systems for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) { - auto Result = (*I)->openFileForRead(Path); + auto Result = (*I)->openFileForRead(Path, IsText); if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) return Result; } @@ -1073,7 +1076,7 @@ llvm::ErrorOr InMemoryFileSystem::status(const Twine &Path) { } llvm::ErrorOr> -InMemoryFileSystem::openFileForRead(const Twine &Path) { +InMemoryFileSystem::openFileForRead(const Twine &Path, bool IsText) { auto Node = lookupNode(Path,/*FollowFinalSymlink=*/true); if (!Node) return Node.getError(); @@ -2537,7 +2540,7 @@ File::getWithPath(ErrorOr> Result, const Twine &P) { } ErrorOr> -RedirectingFileSystem::openFileForRead(const Twine &OriginalPath) { +RedirectingFileSystem::openFileForRead(const Twine &OriginalPath, bool IsText) { SmallString<256> Path; OriginalPath.toVector(Path); @@ -2547,7 +2550,8 @@ RedirectingFileSystem::openFileForRead(const Twine &OriginalPath) { if (Redirection == RedirectKind::Fallback) { // Attempt to find the original file first, only falling back to the // mapped file if that fails. - auto F = File::getWithPath(ExternalFS->openFileForRead(Path), OriginalPath); + auto F = File::getWithPath(ExternalFS->openFileForRead(Path, IsText), + OriginalPath); if (F) return F; } @@ -2558,7 +2562,8 @@ RedirectingFileSystem::openFileForRead(const Twine &OriginalPath) { // that was the specified redirection type. if (Redirection == RedirectKind::Fallthrough && isFileNotFound(Result.getError())) - return File::getWithPath(ExternalFS->openFileForRead(Path), OriginalPath); + return File::getWithPath(ExternalFS->openFileForRead(Path, IsText), + OriginalPath); return Result.getError(); } @@ -2572,15 +2577,16 @@ RedirectingFileSystem::openFileForRead(const Twine &OriginalPath) { auto *RE = cast(Result->E); - auto ExternalFile = - File::getWithPath(ExternalFS->openFileForRead(RemappedPath), ExtRedirect); + auto ExternalFile = File::getWithPath( + ExternalFS->openFileForRead(RemappedPath, IsText), ExtRedirect); if (!ExternalFile) { if (Redirection == RedirectKind::Fallthrough && isFileNotFound(ExternalFile.getError(), Result->E)) { // Mapped the file but it wasn't found in the underlying filesystem, // fallthrough to using the original path if that was the specified // redirection type. - return File::getWithPath(ExternalFS->openFileForRead(Path), OriginalPath); + return File::getWithPath(ExternalFS->openFileForRead(Path, IsText), + OriginalPath); } return ExternalFile; } diff --git a/llvm/unittests/Support/VirtualFileSystemTest.cpp b/llvm/unittests/Support/VirtualFileSystemTest.cpp index eb590e474c2ec..219aaefd43f65 100644 --- a/llvm/unittests/Support/VirtualFileSystemTest.cpp +++ b/llvm/unittests/Support/VirtualFileSystemTest.cpp @@ -67,7 +67,7 @@ class DummyFileSystem : public vfs::FileSystem { return I->second; } ErrorOr> - openFileForRead(const Twine &Path) override { + openFileForRead(const Twine &Path, bool IsText = true) override { auto S = status(Path); if (S) return std::unique_ptr(new DummyFile{*S}); @@ -3389,9 +3389,9 @@ TEST(RedirectingFileSystemTest, ExternalPaths) { } llvm::ErrorOr> - openFileForRead(const Twine &Path) override { + openFileForRead(const Twine &Path, bool IsText = true) override { SeenPaths.push_back(Path.str()); - return ProxyFileSystem::openFileForRead(Path); + return ProxyFileSystem::openFileForRead(Path, IsText); } std::error_code isLocal(const Twine &Path, bool &Result) override {