diff options
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Support/VirtualFileSystem.cpp')
| -rw-r--r-- | contrib/llvm-project/llvm/lib/Support/VirtualFileSystem.cpp | 154 |
1 files changed, 54 insertions, 100 deletions
diff --git a/contrib/llvm-project/llvm/lib/Support/VirtualFileSystem.cpp b/contrib/llvm-project/llvm/lib/Support/VirtualFileSystem.cpp index edd4234fe501..5d3480e97148 100644 --- a/contrib/llvm-project/llvm/lib/Support/VirtualFileSystem.cpp +++ b/contrib/llvm-project/llvm/lib/Support/VirtualFileSystem.cpp @@ -176,9 +176,9 @@ class RealFile : public File { Status S; std::string RealName; - RealFile(file_t RawFD, StringRef NewName, StringRef NewRealPathName) - : FD(RawFD), S(NewName, {}, {}, {}, {}, {}, - llvm::sys::fs::file_type::status_error, {}), + RealFile(file_t FD, StringRef NewName, StringRef NewRealPathName) + : FD(FD), S(NewName, {}, {}, {}, {}, {}, + llvm::sys::fs::file_type::status_error, {}), RealName(NewRealPathName.str()) { assert(FD != kInvalidFile && "Invalid or inactive file descriptor"); } @@ -349,7 +349,7 @@ IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() { } std::unique_ptr<FileSystem> vfs::createPhysicalFileSystem() { - return std::make_unique<RealFileSystem>(false); + return llvm::make_unique<RealFileSystem>(false); } namespace { @@ -754,7 +754,7 @@ bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime, ResolvedUser, ResolvedGroup, 0, sys::fs::file_type::directory_file, NewDirectoryPerms); Dir = cast<detail::InMemoryDirectory>(Dir->addChild( - Name, std::make_unique<detail::InMemoryDirectory>(std::move(Stat)))); + Name, llvm::make_unique<detail::InMemoryDirectory>(std::move(Stat)))); continue; } @@ -894,7 +894,7 @@ class InMemoryDirIterator : public llvm::vfs::detail::DirIterImpl { if (I != E) { SmallString<256> Path(RequestedDirName); llvm::sys::path::append(Path, I->second->getFileName()); - sys::fs::file_type Type = sys::fs::file_type::type_unknown; + sys::fs::file_type Type; switch (I->second->getKind()) { case detail::IME_File: case detail::IME_HardLink: @@ -989,16 +989,6 @@ std::error_code InMemoryFileSystem::isLocal(const Twine &Path, bool &Result) { // RedirectingFileSystem implementation //===-----------------------------------------------------------------------===/ -RedirectingFileSystem::RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> FS) - : ExternalFS(std::move(FS)) { - if (ExternalFS) - if (auto ExternalWorkingDirectory = - ExternalFS->getCurrentWorkingDirectory()) { - WorkingDirectory = *ExternalWorkingDirectory; - ExternalFSValidWD = true; - } -} - // FIXME: reuse implementation common with OverlayFSDirIterImpl as these // iterators are conceptually similar. class llvm::vfs::VFSFromYamlDirIterImpl @@ -1045,27 +1035,12 @@ public: llvm::ErrorOr<std::string> RedirectingFileSystem::getCurrentWorkingDirectory() const { - return WorkingDirectory; + return ExternalFS->getCurrentWorkingDirectory(); } std::error_code RedirectingFileSystem::setCurrentWorkingDirectory(const Twine &Path) { - // Don't change the working directory if the path doesn't exist. - if (!exists(Path)) - return errc::no_such_file_or_directory; - - // Always change the external FS but ignore its result. - if (ExternalFS) { - auto EC = ExternalFS->setCurrentWorkingDirectory(Path); - ExternalFSValidWD = !static_cast<bool>(EC); - } - - SmallString<128> AbsolutePath; - Path.toVector(AbsolutePath); - if (std::error_code EC = makeAbsolute(AbsolutePath)) - return EC; - WorkingDirectory = AbsolutePath.str(); - return {}; + return ExternalFS->setCurrentWorkingDirectory(Path); } std::error_code RedirectingFileSystem::isLocal(const Twine &Path, @@ -1073,25 +1048,12 @@ std::error_code RedirectingFileSystem::isLocal(const Twine &Path, return ExternalFS->isLocal(Path, Result); } -std::error_code RedirectingFileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const { - if (llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::posix) || - llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::windows)) - return {}; - - auto WorkingDir = getCurrentWorkingDirectory(); - if (!WorkingDir) - return WorkingDir.getError(); - - llvm::sys::fs::make_absolute(WorkingDir.get(), Path); - return {}; -} - directory_iterator RedirectingFileSystem::dir_begin(const Twine &Dir, std::error_code &EC) { ErrorOr<RedirectingFileSystem::Entry *> E = lookupPath(Dir); if (!E) { EC = E.getError(); - if (shouldUseExternalFS() && EC == errc::no_such_file_or_directory) + if (IsFallthrough && EC == errc::no_such_file_or_directory) return ExternalFS->dir_begin(Dir, EC); return {}; } @@ -1109,7 +1071,7 @@ directory_iterator RedirectingFileSystem::dir_begin(const Twine &Dir, auto *D = cast<RedirectingFileSystem::RedirectingDirectoryEntry>(*E); return directory_iterator(std::make_shared<VFSFromYamlDirIterImpl>( Dir, D->contents_begin(), D->contents_end(), - /*IterateExternalFS=*/shouldUseExternalFS(), *ExternalFS, EC)); + /*IterateExternalFS=*/IsFallthrough, *ExternalFS, EC)); } void RedirectingFileSystem::setExternalContentsPrefixDir(StringRef PrefixDir) { @@ -1120,19 +1082,20 @@ StringRef RedirectingFileSystem::getExternalContentsPrefixDir() const { return ExternalContentsPrefixDir; } -void RedirectingFileSystem::dump(raw_ostream &OS) const { +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) +LLVM_DUMP_METHOD void RedirectingFileSystem::dump() const { for (const auto &Root : Roots) - dumpEntry(OS, Root.get()); + dumpEntry(Root.get()); } -void RedirectingFileSystem::dumpEntry(raw_ostream &OS, - RedirectingFileSystem::Entry *E, - int NumSpaces) const { +LLVM_DUMP_METHOD void +RedirectingFileSystem::dumpEntry(RedirectingFileSystem::Entry *E, + int NumSpaces) const { StringRef Name = E->getName(); for (int i = 0, e = NumSpaces; i < e; ++i) - OS << " "; - OS << "'" << Name.str().c_str() << "'" - << "\n"; + dbgs() << " "; + dbgs() << "'" << Name.str().c_str() << "'" + << "\n"; if (E->getKind() == RedirectingFileSystem::EK_Directory) { auto *DE = dyn_cast<RedirectingFileSystem::RedirectingDirectoryEntry>(E); @@ -1140,12 +1103,9 @@ void RedirectingFileSystem::dumpEntry(raw_ostream &OS, for (std::unique_ptr<Entry> &SubEntry : llvm::make_range(DE->contents_begin(), DE->contents_end())) - dumpEntry(OS, SubEntry.get(), NumSpaces + 2); + dumpEntry(SubEntry.get(), NumSpaces + 2); } } - -#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) -LLVM_DUMP_METHOD void RedirectingFileSystem::dump() const { dump(dbgs()); } #endif /// A helper class to hold the common YAML parsing state. @@ -1249,7 +1209,7 @@ class llvm::vfs::RedirectingFileSystemParser { // ... or create a new one std::unique_ptr<RedirectingFileSystem::Entry> E = - std::make_unique<RedirectingFileSystem::RedirectingDirectoryEntry>( + llvm::make_unique<RedirectingFileSystem::RedirectingDirectoryEntry>( Name, Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(), 0, 0, 0, file_type::directory_file, sys::fs::all_all)); @@ -1261,7 +1221,7 @@ class llvm::vfs::RedirectingFileSystemParser { } auto *DE = - cast<RedirectingFileSystem::RedirectingDirectoryEntry>(ParentEntry); + dyn_cast<RedirectingFileSystem::RedirectingDirectoryEntry>(ParentEntry); DE->addContent(std::move(E)); return DE->getLastContent(); } @@ -1272,7 +1232,9 @@ class llvm::vfs::RedirectingFileSystemParser { StringRef Name = SrcE->getName(); switch (SrcE->getKind()) { case RedirectingFileSystem::EK_Directory: { - auto *DE = cast<RedirectingFileSystem::RedirectingDirectoryEntry>(SrcE); + auto *DE = + dyn_cast<RedirectingFileSystem::RedirectingDirectoryEntry>(SrcE); + assert(DE && "Must be a directory"); // Empty directories could be present in the YAML as a way to // describe a file for a current directory after some of its subdir // is parsed. This only leads to redundant walks, ignore it. @@ -1284,12 +1246,13 @@ class llvm::vfs::RedirectingFileSystemParser { break; } case RedirectingFileSystem::EK_File: { + auto *FE = dyn_cast<RedirectingFileSystem::RedirectingFileEntry>(SrcE); + assert(FE && "Must be a file"); assert(NewParentE && "Parent entry must exist"); - auto *FE = cast<RedirectingFileSystem::RedirectingFileEntry>(SrcE); - auto *DE = - cast<RedirectingFileSystem::RedirectingDirectoryEntry>(NewParentE); + auto *DE = dyn_cast<RedirectingFileSystem::RedirectingDirectoryEntry>( + NewParentE); DE->addContent( - std::make_unique<RedirectingFileSystem::RedirectingFileEntry>( + llvm::make_unique<RedirectingFileSystem::RedirectingFileEntry>( Name, FE->getExternalContentsPath(), FE->getUseName())); break; } @@ -1441,41 +1404,31 @@ class llvm::vfs::RedirectingFileSystemParser { return nullptr; } - sys::path::Style path_style = sys::path::Style::native; - if (IsRootEntry) { - // VFS root entries may be in either Posix or Windows style. Figure out - // which style we have, and use it consistently. - if (sys::path::is_absolute(Name, sys::path::Style::posix)) { - path_style = sys::path::Style::posix; - } else if (sys::path::is_absolute(Name, sys::path::Style::windows)) { - path_style = sys::path::Style::windows; - } else { - assert(NameValueNode && "Name presence should be checked earlier"); - error(NameValueNode, - "entry with relative path at the root level is not discoverable"); - return nullptr; - } + if (IsRootEntry && !sys::path::is_absolute(Name)) { + assert(NameValueNode && "Name presence should be checked earlier"); + error(NameValueNode, + "entry with relative path at the root level is not discoverable"); + return nullptr; } // Remove trailing slash(es), being careful not to remove the root path StringRef Trimmed(Name); - size_t RootPathLen = sys::path::root_path(Trimmed, path_style).size(); + size_t RootPathLen = sys::path::root_path(Trimmed).size(); while (Trimmed.size() > RootPathLen && - sys::path::is_separator(Trimmed.back(), path_style)) + sys::path::is_separator(Trimmed.back())) Trimmed = Trimmed.slice(0, Trimmed.size() - 1); - // Get the last component - StringRef LastComponent = sys::path::filename(Trimmed, path_style); + StringRef LastComponent = sys::path::filename(Trimmed); std::unique_ptr<RedirectingFileSystem::Entry> Result; switch (Kind) { case RedirectingFileSystem::EK_File: - Result = std::make_unique<RedirectingFileSystem::RedirectingFileEntry>( + Result = llvm::make_unique<RedirectingFileSystem::RedirectingFileEntry>( LastComponent, std::move(ExternalContentsPath), UseExternalName); break; case RedirectingFileSystem::EK_Directory: Result = - std::make_unique<RedirectingFileSystem::RedirectingDirectoryEntry>( + llvm::make_unique<RedirectingFileSystem::RedirectingDirectoryEntry>( LastComponent, std::move(EntryArrayContents), Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(), 0, 0, 0, @@ -1483,18 +1436,18 @@ class llvm::vfs::RedirectingFileSystemParser { break; } - StringRef Parent = sys::path::parent_path(Trimmed, path_style); + StringRef Parent = sys::path::parent_path(Trimmed); if (Parent.empty()) return Result; // if 'name' contains multiple components, create implicit directory entries - for (sys::path::reverse_iterator I = sys::path::rbegin(Parent, path_style), + for (sys::path::reverse_iterator I = sys::path::rbegin(Parent), E = sys::path::rend(Parent); I != E; ++I) { std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> Entries; Entries.push_back(std::move(Result)); Result = - std::make_unique<RedirectingFileSystem::RedirectingDirectoryEntry>( + llvm::make_unique<RedirectingFileSystem::RedirectingDirectoryEntry>( *I, std::move(Entries), Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(), 0, 0, 0, @@ -1620,7 +1573,7 @@ RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer, RedirectingFileSystemParser P(Stream); std::unique_ptr<RedirectingFileSystem> FS( - new RedirectingFileSystem(ExternalFS)); + new RedirectingFileSystem(std::move(ExternalFS))); if (!YAMLFilePath.empty()) { // Use the YAML path from -ivfsoverlay to compute the dir to be prefixed @@ -1694,7 +1647,9 @@ RedirectingFileSystem::lookupPath(sys::path::const_iterator Start, // Forward the search to the next component in case this is an empty one. if (!FromName.empty()) { - if (!pathComponentMatches(*Start, FromName)) + if (CaseSensitive ? !Start->equals(FromName) + : !Start->equals_lower(FromName)) + // failure to match return make_error_code(llvm::errc::no_such_file_or_directory); ++Start; @@ -1716,7 +1671,6 @@ RedirectingFileSystem::lookupPath(sys::path::const_iterator Start, if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) return Result; } - return make_error_code(llvm::errc::no_such_file_or_directory); } @@ -1748,7 +1702,7 @@ ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path, ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path) { ErrorOr<RedirectingFileSystem::Entry *> Result = lookupPath(Path); if (!Result) { - if (shouldUseExternalFS() && + if (IsFallthrough && Result.getError() == llvm::errc::no_such_file_or_directory) { return ExternalFS->status(Path); } @@ -1786,7 +1740,7 @@ ErrorOr<std::unique_ptr<File>> RedirectingFileSystem::openFileForRead(const Twine &Path) { ErrorOr<RedirectingFileSystem::Entry *> E = lookupPath(Path); if (!E) { - if (shouldUseExternalFS() && + if (IsFallthrough && E.getError() == llvm::errc::no_such_file_or_directory) { return ExternalFS->openFileForRead(Path); } @@ -1809,7 +1763,7 @@ RedirectingFileSystem::openFileForRead(const Twine &Path) { Status S = getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames), *ExternalStatus); return std::unique_ptr<File>( - std::make_unique<FileWithFixedStatus>(std::move(*Result), S)); + llvm::make_unique<FileWithFixedStatus>(std::move(*Result), S)); } std::error_code @@ -1817,7 +1771,7 @@ RedirectingFileSystem::getRealPath(const Twine &Path, SmallVectorImpl<char> &Output) const { ErrorOr<RedirectingFileSystem::Entry *> Result = lookupPath(Path); if (!Result) { - if (shouldUseExternalFS() && + if (IsFallthrough && Result.getError() == llvm::errc::no_such_file_or_directory) { return ExternalFS->getRealPath(Path, Output); } @@ -1830,8 +1784,8 @@ RedirectingFileSystem::getRealPath(const Twine &Path, } // Even if there is a directory entry, fall back to ExternalFS if allowed, // because directories don't have a single external contents path. - return shouldUseExternalFS() ? ExternalFS->getRealPath(Path, Output) - : llvm::errc::invalid_argument; + return IsFallthrough ? ExternalFS->getRealPath(Path, Output) + : llvm::errc::invalid_argument; } IntrusiveRefCntPtr<FileSystem> @@ -2095,7 +2049,7 @@ std::error_code VFSFromYamlDirIterImpl::incrementContent(bool IsFirstTime) { while (Current != End) { SmallString<128> PathStr(Dir); llvm::sys::path::append(PathStr, (*Current)->getName()); - sys::fs::file_type Type = sys::fs::file_type::type_unknown; + sys::fs::file_type Type; switch ((*Current)->getKind()) { case RedirectingFileSystem::EK_Directory: Type = sys::fs::file_type::directory_file; |
