1 //===- VirtualFileSystem.cpp - Virtual File System Layer ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the VirtualFileSystem interface.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Support/VirtualFileSystem.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/IntrusiveRefCntPtr.h"
17 #include "llvm/ADT/None.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/ADT/Twine.h"
25 #include "llvm/ADT/iterator_range.h"
26 #include "llvm/Config/llvm-config.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/Chrono.h"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/Errc.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/ErrorOr.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/FileSystem/UniqueID.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Path.h"
38 #include "llvm/Support/Process.h"
39 #include "llvm/Support/SMLoc.h"
40 #include "llvm/Support/SourceMgr.h"
41 #include "llvm/Support/YAMLParser.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include <algorithm>
44 #include <atomic>
45 #include <cassert>
46 #include <cstdint>
47 #include <iterator>
48 #include <limits>
49 #include <map>
50 #include <memory>
51 #include <mutex>
52 #include <string>
53 #include <system_error>
54 #include <utility>
55 #include <vector>
56 
57 using namespace llvm;
58 using namespace llvm::vfs;
59 
60 using llvm::sys::fs::file_t;
61 using llvm::sys::fs::file_status;
62 using llvm::sys::fs::file_type;
63 using llvm::sys::fs::kInvalidFile;
64 using llvm::sys::fs::perms;
65 using llvm::sys::fs::UniqueID;
66 
67 Status::Status(const file_status &Status)
68     : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
69       User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
70       Type(Status.type()), Perms(Status.permissions()) {}
71 
72 Status::Status(const Twine &Name, UniqueID UID, sys::TimePoint<> MTime,
73                uint32_t User, uint32_t Group, uint64_t Size, file_type Type,
74                perms Perms)
75     : Name(Name.str()), UID(UID), MTime(MTime), User(User), Group(Group),
76       Size(Size), Type(Type), Perms(Perms) {}
77 
78 Status Status::copyWithNewName(const Status &In, const Twine &NewName) {
79   return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
80                 In.getUser(), In.getGroup(), In.getSize(), In.getType(),
81                 In.getPermissions());
82 }
83 
84 Status Status::copyWithNewName(const file_status &In, const Twine &NewName) {
85   return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
86                 In.getUser(), In.getGroup(), In.getSize(), In.type(),
87                 In.permissions());
88 }
89 
90 bool Status::equivalent(const Status &Other) const {
91   assert(isStatusKnown() && Other.isStatusKnown());
92   return getUniqueID() == Other.getUniqueID();
93 }
94 
95 bool Status::isDirectory() const { return Type == file_type::directory_file; }
96 
97 bool Status::isRegularFile() const { return Type == file_type::regular_file; }
98 
99 bool Status::isOther() const {
100   return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
101 }
102 
103 bool Status::isSymlink() const { return Type == file_type::symlink_file; }
104 
105 bool Status::isStatusKnown() const { return Type != file_type::status_error; }
106 
107 bool Status::exists() const {
108   return isStatusKnown() && Type != file_type::file_not_found;
109 }
110 
111 File::~File() = default;
112 
113 FileSystem::~FileSystem() = default;
114 
115 ErrorOr<std::unique_ptr<MemoryBuffer>>
116 FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize,
117                              bool RequiresNullTerminator, bool IsVolatile) {
118   auto F = openFileForRead(Name);
119   if (!F)
120     return F.getError();
121 
122   return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile);
123 }
124 
125 std::error_code FileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
126   if (llvm::sys::path::is_absolute(Path))
127     return {};
128 
129   auto WorkingDir = getCurrentWorkingDirectory();
130   if (!WorkingDir)
131     return WorkingDir.getError();
132 
133   llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
134   return {};
135 }
136 
137 std::error_code FileSystem::getRealPath(const Twine &Path,
138                                         SmallVectorImpl<char> &Output) const {
139   return errc::operation_not_permitted;
140 }
141 
142 std::error_code FileSystem::isLocal(const Twine &Path, bool &Result) {
143   return errc::operation_not_permitted;
144 }
145 
146 bool FileSystem::exists(const Twine &Path) {
147   auto Status = status(Path);
148   return Status && Status->exists();
149 }
150 
151 #ifndef NDEBUG
152 static bool isTraversalComponent(StringRef Component) {
153   return Component.equals("..") || Component.equals(".");
154 }
155 
156 static bool pathHasTraversal(StringRef Path) {
157   using namespace llvm::sys;
158 
159   for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path)))
160     if (isTraversalComponent(Comp))
161       return true;
162   return false;
163 }
164 #endif
165 
166 //===-----------------------------------------------------------------------===/
167 // RealFileSystem implementation
168 //===-----------------------------------------------------------------------===/
169 
170 namespace {
171 
172 /// Wrapper around a raw file descriptor.
173 class RealFile : public File {
174   friend class RealFileSystem;
175 
176   file_t FD;
177   Status S;
178   std::string RealName;
179 
180   RealFile(file_t RawFD, StringRef NewName, StringRef NewRealPathName)
181       : FD(RawFD), S(NewName, {}, {}, {}, {}, {},
182                      llvm::sys::fs::file_type::status_error, {}),
183         RealName(NewRealPathName.str()) {
184     assert(FD != kInvalidFile && "Invalid or inactive file descriptor");
185   }
186 
187 public:
188   ~RealFile() override;
189 
190   ErrorOr<Status> status() override;
191   ErrorOr<std::string> getName() override;
192   ErrorOr<std::unique_ptr<MemoryBuffer>> getBuffer(const Twine &Name,
193                                                    int64_t FileSize,
194                                                    bool RequiresNullTerminator,
195                                                    bool IsVolatile) override;
196   std::error_code close() override;
197 };
198 
199 } // namespace
200 
201 RealFile::~RealFile() { close(); }
202 
203 ErrorOr<Status> RealFile::status() {
204   assert(FD != kInvalidFile && "cannot stat closed file");
205   if (!S.isStatusKnown()) {
206     file_status RealStatus;
207     if (std::error_code EC = sys::fs::status(FD, RealStatus))
208       return EC;
209     S = Status::copyWithNewName(RealStatus, S.getName());
210   }
211   return S;
212 }
213 
214 ErrorOr<std::string> RealFile::getName() {
215   return RealName.empty() ? S.getName().str() : RealName;
216 }
217 
218 ErrorOr<std::unique_ptr<MemoryBuffer>>
219 RealFile::getBuffer(const Twine &Name, int64_t FileSize,
220                     bool RequiresNullTerminator, bool IsVolatile) {
221   assert(FD != kInvalidFile && "cannot get buffer for closed file");
222   return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator,
223                                    IsVolatile);
224 }
225 
226 std::error_code RealFile::close() {
227   std::error_code EC = sys::fs::closeFile(FD);
228   FD = kInvalidFile;
229   return EC;
230 }
231 
232 namespace {
233 
234 /// A file system according to your operating system.
235 /// This may be linked to the process's working directory, or maintain its own.
236 ///
237 /// Currently, its own working directory is emulated by storing the path and
238 /// sending absolute paths to llvm::sys::fs:: functions.
239 /// A more principled approach would be to push this down a level, modelling
240 /// the working dir as an llvm::sys::fs::WorkingDir or similar.
241 /// This would enable the use of openat()-style functions on some platforms.
242 class RealFileSystem : public FileSystem {
243 public:
244   explicit RealFileSystem(bool LinkCWDToProcess) {
245     if (!LinkCWDToProcess) {
246       SmallString<128> PWD, RealPWD;
247       if (llvm::sys::fs::current_path(PWD))
248         return; // Awful, but nothing to do here.
249       if (llvm::sys::fs::real_path(PWD, RealPWD))
250         WD = {PWD, PWD};
251       else
252         WD = {PWD, RealPWD};
253     }
254   }
255 
256   ErrorOr<Status> status(const Twine &Path) override;
257   ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
258   directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
259 
260   llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
261   std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
262   std::error_code isLocal(const Twine &Path, bool &Result) override;
263   std::error_code getRealPath(const Twine &Path,
264                               SmallVectorImpl<char> &Output) const override;
265 
266 private:
267   // If this FS has its own working dir, use it to make Path absolute.
268   // The returned twine is safe to use as long as both Storage and Path live.
269   Twine adjustPath(const Twine &Path, SmallVectorImpl<char> &Storage) const {
270     if (!WD)
271       return Path;
272     Path.toVector(Storage);
273     sys::fs::make_absolute(WD->Resolved, Storage);
274     return Storage;
275   }
276 
277   struct WorkingDirectory {
278     // The current working directory, without symlinks resolved. (echo $PWD).
279     SmallString<128> Specified;
280     // The current working directory, with links resolved. (readlink .).
281     SmallString<128> Resolved;
282   };
283   Optional<WorkingDirectory> WD;
284 };
285 
286 } // namespace
287 
288 ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
289   SmallString<256> Storage;
290   sys::fs::file_status RealStatus;
291   if (std::error_code EC =
292           sys::fs::status(adjustPath(Path, Storage), RealStatus))
293     return EC;
294   return Status::copyWithNewName(RealStatus, Path);
295 }
296 
297 ErrorOr<std::unique_ptr<File>>
298 RealFileSystem::openFileForRead(const Twine &Name) {
299   SmallString<256> RealName, Storage;
300   Expected<file_t> FDOrErr = sys::fs::openNativeFileForRead(
301       adjustPath(Name, Storage), sys::fs::OF_None, &RealName);
302   if (!FDOrErr)
303     return errorToErrorCode(FDOrErr.takeError());
304   return std::unique_ptr<File>(
305       new RealFile(*FDOrErr, Name.str(), RealName.str()));
306 }
307 
308 llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const {
309   if (WD)
310     return std::string(WD->Specified.str());
311 
312   SmallString<128> Dir;
313   if (std::error_code EC = llvm::sys::fs::current_path(Dir))
314     return EC;
315   return std::string(Dir.str());
316 }
317 
318 std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
319   if (!WD)
320     return llvm::sys::fs::set_current_path(Path);
321 
322   SmallString<128> Absolute, Resolved, Storage;
323   adjustPath(Path, Storage).toVector(Absolute);
324   bool IsDir;
325   if (auto Err = llvm::sys::fs::is_directory(Absolute, IsDir))
326     return Err;
327   if (!IsDir)
328     return std::make_error_code(std::errc::not_a_directory);
329   if (auto Err = llvm::sys::fs::real_path(Absolute, Resolved))
330     return Err;
331   WD = {Absolute, Resolved};
332   return std::error_code();
333 }
334 
335 std::error_code RealFileSystem::isLocal(const Twine &Path, bool &Result) {
336   SmallString<256> Storage;
337   return llvm::sys::fs::is_local(adjustPath(Path, Storage), Result);
338 }
339 
340 std::error_code
341 RealFileSystem::getRealPath(const Twine &Path,
342                             SmallVectorImpl<char> &Output) const {
343   SmallString<256> Storage;
344   return llvm::sys::fs::real_path(adjustPath(Path, Storage), Output);
345 }
346 
347 IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
348   static IntrusiveRefCntPtr<FileSystem> FS(new RealFileSystem(true));
349   return FS;
350 }
351 
352 std::unique_ptr<FileSystem> vfs::createPhysicalFileSystem() {
353   return std::make_unique<RealFileSystem>(false);
354 }
355 
356 namespace {
357 
358 class RealFSDirIter : public llvm::vfs::detail::DirIterImpl {
359   llvm::sys::fs::directory_iterator Iter;
360 
361 public:
362   RealFSDirIter(const Twine &Path, std::error_code &EC) : Iter(Path, EC) {
363     if (Iter != llvm::sys::fs::directory_iterator())
364       CurrentEntry = directory_entry(Iter->path(), Iter->type());
365   }
366 
367   std::error_code increment() override {
368     std::error_code EC;
369     Iter.increment(EC);
370     CurrentEntry = (Iter == llvm::sys::fs::directory_iterator())
371                        ? directory_entry()
372                        : directory_entry(Iter->path(), Iter->type());
373     return EC;
374   }
375 };
376 
377 } // namespace
378 
379 directory_iterator RealFileSystem::dir_begin(const Twine &Dir,
380                                              std::error_code &EC) {
381   SmallString<128> Storage;
382   return directory_iterator(
383       std::make_shared<RealFSDirIter>(adjustPath(Dir, Storage), EC));
384 }
385 
386 //===-----------------------------------------------------------------------===/
387 // OverlayFileSystem implementation
388 //===-----------------------------------------------------------------------===/
389 
390 OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
391   FSList.push_back(std::move(BaseFS));
392 }
393 
394 void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
395   FSList.push_back(FS);
396   // Synchronize added file systems by duplicating the working directory from
397   // the first one in the list.
398   FS->setCurrentWorkingDirectory(getCurrentWorkingDirectory().get());
399 }
400 
401 ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
402   // FIXME: handle symlinks that cross file systems
403   for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
404     ErrorOr<Status> Status = (*I)->status(Path);
405     if (Status || Status.getError() != llvm::errc::no_such_file_or_directory)
406       return Status;
407   }
408   return make_error_code(llvm::errc::no_such_file_or_directory);
409 }
410 
411 ErrorOr<std::unique_ptr<File>>
412 OverlayFileSystem::openFileForRead(const llvm::Twine &Path) {
413   // FIXME: handle symlinks that cross file systems
414   for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
415     auto Result = (*I)->openFileForRead(Path);
416     if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
417       return Result;
418   }
419   return make_error_code(llvm::errc::no_such_file_or_directory);
420 }
421 
422 llvm::ErrorOr<std::string>
423 OverlayFileSystem::getCurrentWorkingDirectory() const {
424   // All file systems are synchronized, just take the first working directory.
425   return FSList.front()->getCurrentWorkingDirectory();
426 }
427 
428 std::error_code
429 OverlayFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
430   for (auto &FS : FSList)
431     if (std::error_code EC = FS->setCurrentWorkingDirectory(Path))
432       return EC;
433   return {};
434 }
435 
436 std::error_code OverlayFileSystem::isLocal(const Twine &Path, bool &Result) {
437   for (auto &FS : FSList)
438     if (FS->exists(Path))
439       return FS->isLocal(Path, Result);
440   return errc::no_such_file_or_directory;
441 }
442 
443 std::error_code
444 OverlayFileSystem::getRealPath(const Twine &Path,
445                                SmallVectorImpl<char> &Output) const {
446   for (const auto &FS : FSList)
447     if (FS->exists(Path))
448       return FS->getRealPath(Path, Output);
449   return errc::no_such_file_or_directory;
450 }
451 
452 llvm::vfs::detail::DirIterImpl::~DirIterImpl() = default;
453 
454 namespace {
455 
456 /// Combines and deduplicates directory entries across multiple file systems.
457 class CombiningDirIterImpl : public llvm::vfs::detail::DirIterImpl {
458   using FileSystemPtr = llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>;
459 
460   /// File systems to check for entries in. Processed in reverse order.
461   SmallVector<FileSystemPtr, 8> FSList;
462   /// The directory iterator for the current filesystem.
463   directory_iterator CurrentDirIter;
464   /// The path of the directory to iterate the entries of.
465   std::string DirPath;
466   /// The set of names already returned as entries.
467   llvm::StringSet<> SeenNames;
468 
469   /// Sets \c CurrentDirIter to an iterator of \c DirPath in the next file
470   /// system in the list, or leaves it as is (at its end position) if we've
471   /// already gone through them all.
472   std::error_code incrementFS() {
473     while (!FSList.empty()) {
474       std::error_code EC;
475       CurrentDirIter = FSList.back()->dir_begin(DirPath, EC);
476       FSList.pop_back();
477       if (EC && EC != errc::no_such_file_or_directory)
478         return EC;
479       if (CurrentDirIter != directory_iterator())
480         break; // found
481     }
482     return {};
483   }
484 
485   std::error_code incrementDirIter(bool IsFirstTime) {
486     assert((IsFirstTime || CurrentDirIter != directory_iterator()) &&
487            "incrementing past end");
488     std::error_code EC;
489     if (!IsFirstTime)
490       CurrentDirIter.increment(EC);
491     if (!EC && CurrentDirIter == directory_iterator())
492       EC = incrementFS();
493     return EC;
494   }
495 
496   std::error_code incrementImpl(bool IsFirstTime) {
497     while (true) {
498       std::error_code EC = incrementDirIter(IsFirstTime);
499       if (EC || CurrentDirIter == directory_iterator()) {
500         CurrentEntry = directory_entry();
501         return EC;
502       }
503       CurrentEntry = *CurrentDirIter;
504       StringRef Name = llvm::sys::path::filename(CurrentEntry.path());
505       if (SeenNames.insert(Name).second)
506         return EC; // name not seen before
507     }
508     llvm_unreachable("returned above");
509   }
510 
511 public:
512   CombiningDirIterImpl(ArrayRef<FileSystemPtr> FileSystems, std::string Dir,
513                        std::error_code &EC)
514       : FSList(FileSystems.begin(), FileSystems.end()),
515         DirPath(std::move(Dir)) {
516     if (!FSList.empty()) {
517       CurrentDirIter = FSList.back()->dir_begin(DirPath, EC);
518       FSList.pop_back();
519       if (!EC || EC == errc::no_such_file_or_directory)
520         EC = incrementImpl(true);
521     }
522   }
523 
524   CombiningDirIterImpl(directory_iterator FirstIter, FileSystemPtr Fallback,
525                        std::string FallbackDir, std::error_code &EC)
526       : FSList({Fallback}), CurrentDirIter(FirstIter),
527         DirPath(std::move(FallbackDir)) {
528     if (!EC || EC == errc::no_such_file_or_directory)
529       EC = incrementImpl(true);
530   }
531 
532   std::error_code increment() override { return incrementImpl(false); }
533 };
534 
535 } // namespace
536 
537 directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir,
538                                                 std::error_code &EC) {
539   return directory_iterator(
540       std::make_shared<CombiningDirIterImpl>(FSList, Dir.str(), EC));
541 }
542 
543 void ProxyFileSystem::anchor() {}
544 
545 namespace llvm {
546 namespace vfs {
547 
548 namespace detail {
549 
550 enum InMemoryNodeKind { IME_File, IME_Directory, IME_HardLink };
551 
552 /// The in memory file system is a tree of Nodes. Every node can either be a
553 /// file , hardlink or a directory.
554 class InMemoryNode {
555   InMemoryNodeKind Kind;
556   std::string FileName;
557 
558 public:
559   InMemoryNode(llvm::StringRef FileName, InMemoryNodeKind Kind)
560       : Kind(Kind), FileName(std::string(llvm::sys::path::filename(FileName))) {
561   }
562   virtual ~InMemoryNode() = default;
563 
564   /// Get the filename of this node (the name without the directory part).
565   StringRef getFileName() const { return FileName; }
566   InMemoryNodeKind getKind() const { return Kind; }
567   virtual std::string toString(unsigned Indent) const = 0;
568 };
569 
570 class InMemoryFile : public InMemoryNode {
571   Status Stat;
572   std::unique_ptr<llvm::MemoryBuffer> Buffer;
573 
574 public:
575   InMemoryFile(Status Stat, std::unique_ptr<llvm::MemoryBuffer> Buffer)
576       : InMemoryNode(Stat.getName(), IME_File), Stat(std::move(Stat)),
577         Buffer(std::move(Buffer)) {}
578 
579   /// Return the \p Status for this node. \p RequestedName should be the name
580   /// through which the caller referred to this node. It will override
581   /// \p Status::Name in the return value, to mimic the behavior of \p RealFile.
582   Status getStatus(const Twine &RequestedName) const {
583     return Status::copyWithNewName(Stat, RequestedName);
584   }
585   llvm::MemoryBuffer *getBuffer() const { return Buffer.get(); }
586 
587   std::string toString(unsigned Indent) const override {
588     return (std::string(Indent, ' ') + Stat.getName() + "\n").str();
589   }
590 
591   static bool classof(const InMemoryNode *N) {
592     return N->getKind() == IME_File;
593   }
594 };
595 
596 namespace {
597 
598 class InMemoryHardLink : public InMemoryNode {
599   const InMemoryFile &ResolvedFile;
600 
601 public:
602   InMemoryHardLink(StringRef Path, const InMemoryFile &ResolvedFile)
603       : InMemoryNode(Path, IME_HardLink), ResolvedFile(ResolvedFile) {}
604   const InMemoryFile &getResolvedFile() const { return ResolvedFile; }
605 
606   std::string toString(unsigned Indent) const override {
607     return std::string(Indent, ' ') + "HardLink to -> " +
608            ResolvedFile.toString(0);
609   }
610 
611   static bool classof(const InMemoryNode *N) {
612     return N->getKind() == IME_HardLink;
613   }
614 };
615 
616 /// Adapt a InMemoryFile for VFS' File interface.  The goal is to make
617 /// \p InMemoryFileAdaptor mimic as much as possible the behavior of
618 /// \p RealFile.
619 class InMemoryFileAdaptor : public File {
620   const InMemoryFile &Node;
621   /// The name to use when returning a Status for this file.
622   std::string RequestedName;
623 
624 public:
625   explicit InMemoryFileAdaptor(const InMemoryFile &Node,
626                                std::string RequestedName)
627       : Node(Node), RequestedName(std::move(RequestedName)) {}
628 
629   llvm::ErrorOr<Status> status() override {
630     return Node.getStatus(RequestedName);
631   }
632 
633   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
634   getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
635             bool IsVolatile) override {
636     llvm::MemoryBuffer *Buf = Node.getBuffer();
637     return llvm::MemoryBuffer::getMemBuffer(
638         Buf->getBuffer(), Buf->getBufferIdentifier(), RequiresNullTerminator);
639   }
640 
641   std::error_code close() override { return {}; }
642 };
643 } // namespace
644 
645 class InMemoryDirectory : public InMemoryNode {
646   Status Stat;
647   llvm::StringMap<std::unique_ptr<InMemoryNode>> Entries;
648 
649 public:
650   InMemoryDirectory(Status Stat)
651       : InMemoryNode(Stat.getName(), IME_Directory), Stat(std::move(Stat)) {}
652 
653   /// Return the \p Status for this node. \p RequestedName should be the name
654   /// through which the caller referred to this node. It will override
655   /// \p Status::Name in the return value, to mimic the behavior of \p RealFile.
656   Status getStatus(const Twine &RequestedName) const {
657     return Status::copyWithNewName(Stat, RequestedName);
658   }
659 
660   UniqueID getUniqueID() const { return Stat.getUniqueID(); }
661 
662   InMemoryNode *getChild(StringRef Name) {
663     auto I = Entries.find(Name);
664     if (I != Entries.end())
665       return I->second.get();
666     return nullptr;
667   }
668 
669   InMemoryNode *addChild(StringRef Name, std::unique_ptr<InMemoryNode> Child) {
670     return Entries.insert(make_pair(Name, std::move(Child)))
671         .first->second.get();
672   }
673 
674   using const_iterator = decltype(Entries)::const_iterator;
675 
676   const_iterator begin() const { return Entries.begin(); }
677   const_iterator end() const { return Entries.end(); }
678 
679   std::string toString(unsigned Indent) const override {
680     std::string Result =
681         (std::string(Indent, ' ') + Stat.getName() + "\n").str();
682     for (const auto &Entry : Entries)
683       Result += Entry.second->toString(Indent + 2);
684     return Result;
685   }
686 
687   static bool classof(const InMemoryNode *N) {
688     return N->getKind() == IME_Directory;
689   }
690 };
691 
692 namespace {
693 Status getNodeStatus(const InMemoryNode *Node, const Twine &RequestedName) {
694   if (auto Dir = dyn_cast<detail::InMemoryDirectory>(Node))
695     return Dir->getStatus(RequestedName);
696   if (auto File = dyn_cast<detail::InMemoryFile>(Node))
697     return File->getStatus(RequestedName);
698   if (auto Link = dyn_cast<detail::InMemoryHardLink>(Node))
699     return Link->getResolvedFile().getStatus(RequestedName);
700   llvm_unreachable("Unknown node type");
701 }
702 } // namespace
703 } // namespace detail
704 
705 // The UniqueID of in-memory files is derived from path and content.
706 // This avoids difficulties in creating exactly equivalent in-memory FSes,
707 // as often needed in multithreaded programs.
708 static sys::fs::UniqueID getUniqueID(hash_code Hash) {
709   return sys::fs::UniqueID(std::numeric_limits<uint64_t>::max(),
710                            uint64_t(size_t(Hash)));
711 }
712 static sys::fs::UniqueID getFileID(sys::fs::UniqueID Parent,
713                                    llvm::StringRef Name,
714                                    llvm::StringRef Contents) {
715   return getUniqueID(llvm::hash_combine(Parent.getFile(), Name, Contents));
716 }
717 static sys::fs::UniqueID getDirectoryID(sys::fs::UniqueID Parent,
718                                         llvm::StringRef Name) {
719   return getUniqueID(llvm::hash_combine(Parent.getFile(), Name));
720 }
721 
722 InMemoryFileSystem::InMemoryFileSystem(bool UseNormalizedPaths)
723     : Root(new detail::InMemoryDirectory(
724           Status("", getDirectoryID(llvm::sys::fs::UniqueID(), ""),
725                  llvm::sys::TimePoint<>(), 0, 0, 0,
726                  llvm::sys::fs::file_type::directory_file,
727                  llvm::sys::fs::perms::all_all))),
728       UseNormalizedPaths(UseNormalizedPaths) {}
729 
730 InMemoryFileSystem::~InMemoryFileSystem() = default;
731 
732 std::string InMemoryFileSystem::toString() const {
733   return Root->toString(/*Indent=*/0);
734 }
735 
736 bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
737                                  std::unique_ptr<llvm::MemoryBuffer> Buffer,
738                                  Optional<uint32_t> User,
739                                  Optional<uint32_t> Group,
740                                  Optional<llvm::sys::fs::file_type> Type,
741                                  Optional<llvm::sys::fs::perms> Perms,
742                                  const detail::InMemoryFile *HardLinkTarget) {
743   SmallString<128> Path;
744   P.toVector(Path);
745 
746   // Fix up relative paths. This just prepends the current working directory.
747   std::error_code EC = makeAbsolute(Path);
748   assert(!EC);
749   (void)EC;
750 
751   if (useNormalizedPaths())
752     llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
753 
754   if (Path.empty())
755     return false;
756 
757   detail::InMemoryDirectory *Dir = Root.get();
758   auto I = llvm::sys::path::begin(Path), E = sys::path::end(Path);
759   const auto ResolvedUser = User.getValueOr(0);
760   const auto ResolvedGroup = Group.getValueOr(0);
761   const auto ResolvedType = Type.getValueOr(sys::fs::file_type::regular_file);
762   const auto ResolvedPerms = Perms.getValueOr(sys::fs::all_all);
763   assert(!(HardLinkTarget && Buffer) && "HardLink cannot have a buffer");
764   // Any intermediate directories we create should be accessible by
765   // the owner, even if Perms says otherwise for the final path.
766   const auto NewDirectoryPerms = ResolvedPerms | sys::fs::owner_all;
767   while (true) {
768     StringRef Name = *I;
769     detail::InMemoryNode *Node = Dir->getChild(Name);
770     ++I;
771     if (!Node) {
772       if (I == E) {
773         // End of the path.
774         std::unique_ptr<detail::InMemoryNode> Child;
775         if (HardLinkTarget)
776           Child.reset(new detail::InMemoryHardLink(P.str(), *HardLinkTarget));
777         else {
778           // Create a new file or directory.
779           Status Stat(
780               P.str(),
781               (ResolvedType == sys::fs::file_type::directory_file)
782                   ? getDirectoryID(Dir->getUniqueID(), Name)
783                   : getFileID(Dir->getUniqueID(), Name, Buffer->getBuffer()),
784               llvm::sys::toTimePoint(ModificationTime), ResolvedUser,
785               ResolvedGroup, Buffer->getBufferSize(), ResolvedType,
786               ResolvedPerms);
787           if (ResolvedType == sys::fs::file_type::directory_file) {
788             Child.reset(new detail::InMemoryDirectory(std::move(Stat)));
789           } else {
790             Child.reset(
791                 new detail::InMemoryFile(std::move(Stat), std::move(Buffer)));
792           }
793         }
794         Dir->addChild(Name, std::move(Child));
795         return true;
796       }
797 
798       // Create a new directory. Use the path up to here.
799       Status Stat(
800           StringRef(Path.str().begin(), Name.end() - Path.str().begin()),
801           getDirectoryID(Dir->getUniqueID(), Name),
802           llvm::sys::toTimePoint(ModificationTime), ResolvedUser, ResolvedGroup,
803           0, sys::fs::file_type::directory_file, NewDirectoryPerms);
804       Dir = cast<detail::InMemoryDirectory>(Dir->addChild(
805           Name, std::make_unique<detail::InMemoryDirectory>(std::move(Stat))));
806       continue;
807     }
808 
809     if (auto *NewDir = dyn_cast<detail::InMemoryDirectory>(Node)) {
810       Dir = NewDir;
811     } else {
812       assert((isa<detail::InMemoryFile>(Node) ||
813               isa<detail::InMemoryHardLink>(Node)) &&
814              "Must be either file, hardlink or directory!");
815 
816       // Trying to insert a directory in place of a file.
817       if (I != E)
818         return false;
819 
820       // Return false only if the new file is different from the existing one.
821       if (auto Link = dyn_cast<detail::InMemoryHardLink>(Node)) {
822         return Link->getResolvedFile().getBuffer()->getBuffer() ==
823                Buffer->getBuffer();
824       }
825       return cast<detail::InMemoryFile>(Node)->getBuffer()->getBuffer() ==
826              Buffer->getBuffer();
827     }
828   }
829 }
830 
831 bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
832                                  std::unique_ptr<llvm::MemoryBuffer> Buffer,
833                                  Optional<uint32_t> User,
834                                  Optional<uint32_t> Group,
835                                  Optional<llvm::sys::fs::file_type> Type,
836                                  Optional<llvm::sys::fs::perms> Perms) {
837   return addFile(P, ModificationTime, std::move(Buffer), User, Group, Type,
838                  Perms, /*HardLinkTarget=*/nullptr);
839 }
840 
841 bool InMemoryFileSystem::addFileNoOwn(const Twine &P, time_t ModificationTime,
842                                       const llvm::MemoryBufferRef &Buffer,
843                                       Optional<uint32_t> User,
844                                       Optional<uint32_t> Group,
845                                       Optional<llvm::sys::fs::file_type> Type,
846                                       Optional<llvm::sys::fs::perms> Perms) {
847   return addFile(P, ModificationTime, llvm::MemoryBuffer::getMemBuffer(Buffer),
848                  std::move(User), std::move(Group), std::move(Type),
849                  std::move(Perms));
850 }
851 
852 static ErrorOr<const detail::InMemoryNode *>
853 lookupInMemoryNode(const InMemoryFileSystem &FS, detail::InMemoryDirectory *Dir,
854                    const Twine &P) {
855   SmallString<128> Path;
856   P.toVector(Path);
857 
858   // Fix up relative paths. This just prepends the current working directory.
859   std::error_code EC = FS.makeAbsolute(Path);
860   assert(!EC);
861   (void)EC;
862 
863   if (FS.useNormalizedPaths())
864     llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
865 
866   if (Path.empty())
867     return Dir;
868 
869   auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path);
870   while (true) {
871     detail::InMemoryNode *Node = Dir->getChild(*I);
872     ++I;
873     if (!Node)
874       return errc::no_such_file_or_directory;
875 
876     // Return the file if it's at the end of the path.
877     if (auto File = dyn_cast<detail::InMemoryFile>(Node)) {
878       if (I == E)
879         return File;
880       return errc::no_such_file_or_directory;
881     }
882 
883     // If Node is HardLink then return the resolved file.
884     if (auto File = dyn_cast<detail::InMemoryHardLink>(Node)) {
885       if (I == E)
886         return &File->getResolvedFile();
887       return errc::no_such_file_or_directory;
888     }
889     // Traverse directories.
890     Dir = cast<detail::InMemoryDirectory>(Node);
891     if (I == E)
892       return Dir;
893   }
894 }
895 
896 bool InMemoryFileSystem::addHardLink(const Twine &FromPath,
897                                      const Twine &ToPath) {
898   auto FromNode = lookupInMemoryNode(*this, Root.get(), FromPath);
899   auto ToNode = lookupInMemoryNode(*this, Root.get(), ToPath);
900   // FromPath must not have been added before. ToPath must have been added
901   // before. Resolved ToPath must be a File.
902   if (!ToNode || FromNode || !isa<detail::InMemoryFile>(*ToNode))
903     return false;
904   return this->addFile(FromPath, 0, nullptr, None, None, None, None,
905                        cast<detail::InMemoryFile>(*ToNode));
906 }
907 
908 llvm::ErrorOr<Status> InMemoryFileSystem::status(const Twine &Path) {
909   auto Node = lookupInMemoryNode(*this, Root.get(), Path);
910   if (Node)
911     return detail::getNodeStatus(*Node, Path);
912   return Node.getError();
913 }
914 
915 llvm::ErrorOr<std::unique_ptr<File>>
916 InMemoryFileSystem::openFileForRead(const Twine &Path) {
917   auto Node = lookupInMemoryNode(*this, Root.get(), Path);
918   if (!Node)
919     return Node.getError();
920 
921   // When we have a file provide a heap-allocated wrapper for the memory buffer
922   // to match the ownership semantics for File.
923   if (auto *F = dyn_cast<detail::InMemoryFile>(*Node))
924     return std::unique_ptr<File>(
925         new detail::InMemoryFileAdaptor(*F, Path.str()));
926 
927   // FIXME: errc::not_a_file?
928   return make_error_code(llvm::errc::invalid_argument);
929 }
930 
931 namespace {
932 
933 /// Adaptor from InMemoryDir::iterator to directory_iterator.
934 class InMemoryDirIterator : public llvm::vfs::detail::DirIterImpl {
935   detail::InMemoryDirectory::const_iterator I;
936   detail::InMemoryDirectory::const_iterator E;
937   std::string RequestedDirName;
938 
939   void setCurrentEntry() {
940     if (I != E) {
941       SmallString<256> Path(RequestedDirName);
942       llvm::sys::path::append(Path, I->second->getFileName());
943       sys::fs::file_type Type = sys::fs::file_type::type_unknown;
944       switch (I->second->getKind()) {
945       case detail::IME_File:
946       case detail::IME_HardLink:
947         Type = sys::fs::file_type::regular_file;
948         break;
949       case detail::IME_Directory:
950         Type = sys::fs::file_type::directory_file;
951         break;
952       }
953       CurrentEntry = directory_entry(std::string(Path.str()), Type);
954     } else {
955       // When we're at the end, make CurrentEntry invalid and DirIterImpl will
956       // do the rest.
957       CurrentEntry = directory_entry();
958     }
959   }
960 
961 public:
962   InMemoryDirIterator() = default;
963 
964   explicit InMemoryDirIterator(const detail::InMemoryDirectory &Dir,
965                                std::string RequestedDirName)
966       : I(Dir.begin()), E(Dir.end()),
967         RequestedDirName(std::move(RequestedDirName)) {
968     setCurrentEntry();
969   }
970 
971   std::error_code increment() override {
972     ++I;
973     setCurrentEntry();
974     return {};
975   }
976 };
977 
978 } // namespace
979 
980 directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir,
981                                                  std::error_code &EC) {
982   auto Node = lookupInMemoryNode(*this, Root.get(), Dir);
983   if (!Node) {
984     EC = Node.getError();
985     return directory_iterator(std::make_shared<InMemoryDirIterator>());
986   }
987 
988   if (auto *DirNode = dyn_cast<detail::InMemoryDirectory>(*Node))
989     return directory_iterator(
990         std::make_shared<InMemoryDirIterator>(*DirNode, Dir.str()));
991 
992   EC = make_error_code(llvm::errc::not_a_directory);
993   return directory_iterator(std::make_shared<InMemoryDirIterator>());
994 }
995 
996 std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) {
997   SmallString<128> Path;
998   P.toVector(Path);
999 
1000   // Fix up relative paths. This just prepends the current working directory.
1001   std::error_code EC = makeAbsolute(Path);
1002   assert(!EC);
1003   (void)EC;
1004 
1005   if (useNormalizedPaths())
1006     llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
1007 
1008   if (!Path.empty())
1009     WorkingDirectory = std::string(Path.str());
1010   return {};
1011 }
1012 
1013 std::error_code
1014 InMemoryFileSystem::getRealPath(const Twine &Path,
1015                                 SmallVectorImpl<char> &Output) const {
1016   auto CWD = getCurrentWorkingDirectory();
1017   if (!CWD || CWD->empty())
1018     return errc::operation_not_permitted;
1019   Path.toVector(Output);
1020   if (auto EC = makeAbsolute(Output))
1021     return EC;
1022   llvm::sys::path::remove_dots(Output, /*remove_dot_dot=*/true);
1023   return {};
1024 }
1025 
1026 std::error_code InMemoryFileSystem::isLocal(const Twine &Path, bool &Result) {
1027   Result = false;
1028   return {};
1029 }
1030 
1031 } // namespace vfs
1032 } // namespace llvm
1033 
1034 //===-----------------------------------------------------------------------===/
1035 // RedirectingFileSystem implementation
1036 //===-----------------------------------------------------------------------===/
1037 
1038 namespace {
1039 
1040 static llvm::sys::path::Style getExistingStyle(llvm::StringRef Path) {
1041   // Detect the path style in use by checking the first separator.
1042   llvm::sys::path::Style style = llvm::sys::path::Style::native;
1043   const size_t n = Path.find_first_of("/\\");
1044   if (n != static_cast<size_t>(-1))
1045     style = (Path[n] == '/') ? llvm::sys::path::Style::posix
1046                              : llvm::sys::path::Style::windows;
1047   return style;
1048 }
1049 
1050 /// Removes leading "./" as well as path components like ".." and ".".
1051 static llvm::SmallString<256> canonicalize(llvm::StringRef Path) {
1052   // First detect the path style in use by checking the first separator.
1053   llvm::sys::path::Style style = getExistingStyle(Path);
1054 
1055   // Now remove the dots.  Explicitly specifying the path style prevents the
1056   // direction of the slashes from changing.
1057   llvm::SmallString<256> result =
1058       llvm::sys::path::remove_leading_dotslash(Path, style);
1059   llvm::sys::path::remove_dots(result, /*remove_dot_dot=*/true, style);
1060   return result;
1061 }
1062 
1063 } // anonymous namespace
1064 
1065 
1066 RedirectingFileSystem::RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> FS)
1067     : ExternalFS(std::move(FS)) {
1068   if (ExternalFS)
1069     if (auto ExternalWorkingDirectory =
1070             ExternalFS->getCurrentWorkingDirectory()) {
1071       WorkingDirectory = *ExternalWorkingDirectory;
1072     }
1073 }
1074 
1075 /// Directory iterator implementation for \c RedirectingFileSystem's
1076 /// directory entries.
1077 class llvm::vfs::RedirectingFSDirIterImpl
1078     : public llvm::vfs::detail::DirIterImpl {
1079   std::string Dir;
1080   RedirectingFileSystem::DirectoryEntry::iterator Current, End;
1081 
1082   std::error_code incrementImpl(bool IsFirstTime) {
1083     assert((IsFirstTime || Current != End) && "cannot iterate past end");
1084     if (!IsFirstTime)
1085       ++Current;
1086     if (Current != End) {
1087       SmallString<128> PathStr(Dir);
1088       llvm::sys::path::append(PathStr, (*Current)->getName());
1089       sys::fs::file_type Type = sys::fs::file_type::type_unknown;
1090       switch ((*Current)->getKind()) {
1091       case RedirectingFileSystem::EK_Directory:
1092         LLVM_FALLTHROUGH;
1093       case RedirectingFileSystem::EK_DirectoryRemap:
1094         Type = sys::fs::file_type::directory_file;
1095         break;
1096       case RedirectingFileSystem::EK_File:
1097         Type = sys::fs::file_type::regular_file;
1098         break;
1099       }
1100       CurrentEntry = directory_entry(std::string(PathStr.str()), Type);
1101     } else {
1102       CurrentEntry = directory_entry();
1103     }
1104     return {};
1105   };
1106 
1107 public:
1108   RedirectingFSDirIterImpl(
1109       const Twine &Path, RedirectingFileSystem::DirectoryEntry::iterator Begin,
1110       RedirectingFileSystem::DirectoryEntry::iterator End, std::error_code &EC)
1111       : Dir(Path.str()), Current(Begin), End(End) {
1112     EC = incrementImpl(/*IsFirstTime=*/true);
1113   }
1114 
1115   std::error_code increment() override {
1116     return incrementImpl(/*IsFirstTime=*/false);
1117   }
1118 };
1119 
1120 namespace {
1121 /// Directory iterator implementation for \c RedirectingFileSystem's
1122 /// directory remap entries that maps the paths reported by the external
1123 /// file system's directory iterator back to the virtual directory's path.
1124 class RedirectingFSDirRemapIterImpl : public llvm::vfs::detail::DirIterImpl {
1125   std::string Dir;
1126   llvm::sys::path::Style DirStyle;
1127   llvm::vfs::directory_iterator ExternalIter;
1128 
1129 public:
1130   RedirectingFSDirRemapIterImpl(std::string DirPath,
1131                                 llvm::vfs::directory_iterator ExtIter)
1132       : Dir(std::move(DirPath)), DirStyle(getExistingStyle(Dir)),
1133         ExternalIter(ExtIter) {
1134     if (ExternalIter != llvm::vfs::directory_iterator())
1135       setCurrentEntry();
1136   }
1137 
1138   void setCurrentEntry() {
1139     StringRef ExternalPath = ExternalIter->path();
1140     llvm::sys::path::Style ExternalStyle = getExistingStyle(ExternalPath);
1141     StringRef File = llvm::sys::path::filename(ExternalPath, ExternalStyle);
1142 
1143     SmallString<128> NewPath(Dir);
1144     llvm::sys::path::append(NewPath, DirStyle, File);
1145 
1146     CurrentEntry = directory_entry(std::string(NewPath), ExternalIter->type());
1147   }
1148 
1149   std::error_code increment() override {
1150     std::error_code EC;
1151     ExternalIter.increment(EC);
1152     if (!EC && ExternalIter != llvm::vfs::directory_iterator())
1153       setCurrentEntry();
1154     else
1155       CurrentEntry = directory_entry();
1156     return EC;
1157   }
1158 };
1159 } // namespace
1160 
1161 llvm::ErrorOr<std::string>
1162 RedirectingFileSystem::getCurrentWorkingDirectory() const {
1163   return WorkingDirectory;
1164 }
1165 
1166 std::error_code
1167 RedirectingFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
1168   // Don't change the working directory if the path doesn't exist.
1169   if (!exists(Path))
1170     return errc::no_such_file_or_directory;
1171 
1172   SmallString<128> AbsolutePath;
1173   Path.toVector(AbsolutePath);
1174   if (std::error_code EC = makeAbsolute(AbsolutePath))
1175     return EC;
1176   WorkingDirectory = std::string(AbsolutePath.str());
1177   return {};
1178 }
1179 
1180 std::error_code RedirectingFileSystem::isLocal(const Twine &Path_,
1181                                                bool &Result) {
1182   SmallString<256> Path;
1183   Path_.toVector(Path);
1184 
1185   if (std::error_code EC = makeCanonical(Path))
1186     return {};
1187 
1188   return ExternalFS->isLocal(Path, Result);
1189 }
1190 
1191 std::error_code RedirectingFileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
1192   if (llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::posix) ||
1193       llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::windows))
1194     return {};
1195 
1196   auto WorkingDir = getCurrentWorkingDirectory();
1197   if (!WorkingDir)
1198     return WorkingDir.getError();
1199 
1200   // We can't use sys::fs::make_absolute because that assumes the path style
1201   // is native and there is no way to override that.  Since we know WorkingDir
1202   // is absolute, we can use it to determine which style we actually have and
1203   // append Path ourselves.
1204   sys::path::Style style = sys::path::Style::windows;
1205   if (sys::path::is_absolute(WorkingDir.get(), sys::path::Style::posix)) {
1206     style = sys::path::Style::posix;
1207   }
1208 
1209   std::string Result = WorkingDir.get();
1210   StringRef Dir(Result);
1211   if (!Dir.endswith(sys::path::get_separator(style))) {
1212     Result += sys::path::get_separator(style);
1213   }
1214   Result.append(Path.data(), Path.size());
1215   Path.assign(Result.begin(), Result.end());
1216 
1217   return {};
1218 }
1219 
1220 directory_iterator RedirectingFileSystem::dir_begin(const Twine &Dir,
1221                                                     std::error_code &EC) {
1222   SmallString<256> Path;
1223   Dir.toVector(Path);
1224 
1225   EC = makeCanonical(Path);
1226   if (EC)
1227     return {};
1228 
1229   ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
1230   if (!Result) {
1231     EC = Result.getError();
1232     if (shouldFallBackToExternalFS(EC))
1233       return ExternalFS->dir_begin(Path, EC);
1234     return {};
1235   }
1236 
1237   // Use status to make sure the path exists and refers to a directory.
1238   ErrorOr<Status> S = status(Path, *Result);
1239   if (!S) {
1240     if (shouldFallBackToExternalFS(S.getError(), Result->E))
1241       return ExternalFS->dir_begin(Dir, EC);
1242     EC = S.getError();
1243     return {};
1244   }
1245   if (!S->isDirectory()) {
1246     EC = std::error_code(static_cast<int>(errc::not_a_directory),
1247                          std::system_category());
1248     return {};
1249   }
1250 
1251   // Create the appropriate directory iterator based on whether we found a
1252   // DirectoryRemapEntry or DirectoryEntry.
1253   directory_iterator DirIter;
1254   if (auto ExtRedirect = Result->getExternalRedirect()) {
1255     auto RE = cast<RedirectingFileSystem::RemapEntry>(Result->E);
1256     DirIter = ExternalFS->dir_begin(*ExtRedirect, EC);
1257 
1258     if (!RE->useExternalName(UseExternalNames)) {
1259       // Update the paths in the results to use the virtual directory's path.
1260       DirIter =
1261           directory_iterator(std::make_shared<RedirectingFSDirRemapIterImpl>(
1262               std::string(Path), DirIter));
1263     }
1264   } else {
1265     auto DE = cast<DirectoryEntry>(Result->E);
1266     DirIter = directory_iterator(std::make_shared<RedirectingFSDirIterImpl>(
1267         Path, DE->contents_begin(), DE->contents_end(), EC));
1268   }
1269 
1270   if (!shouldUseExternalFS())
1271     return DirIter;
1272   return directory_iterator(std::make_shared<CombiningDirIterImpl>(
1273       DirIter, ExternalFS, std::string(Path), EC));
1274 }
1275 
1276 void RedirectingFileSystem::setExternalContentsPrefixDir(StringRef PrefixDir) {
1277   ExternalContentsPrefixDir = PrefixDir.str();
1278 }
1279 
1280 StringRef RedirectingFileSystem::getExternalContentsPrefixDir() const {
1281   return ExternalContentsPrefixDir;
1282 }
1283 
1284 void RedirectingFileSystem::setFallthrough(bool Fallthrough) {
1285   IsFallthrough = Fallthrough;
1286 }
1287 
1288 std::vector<StringRef> RedirectingFileSystem::getRoots() const {
1289   std::vector<StringRef> R;
1290   for (const auto &Root : Roots)
1291     R.push_back(Root->getName());
1292   return R;
1293 }
1294 
1295 void RedirectingFileSystem::dump(raw_ostream &OS) const {
1296   for (const auto &Root : Roots)
1297     dumpEntry(OS, Root.get());
1298 }
1299 
1300 void RedirectingFileSystem::dumpEntry(raw_ostream &OS,
1301                                       RedirectingFileSystem::Entry *E,
1302                                       int NumSpaces) const {
1303   StringRef Name = E->getName();
1304   for (int i = 0, e = NumSpaces; i < e; ++i)
1305     OS << " ";
1306   OS << "'" << Name.str().c_str() << "'"
1307      << "\n";
1308 
1309   if (E->getKind() == RedirectingFileSystem::EK_Directory) {
1310     auto *DE = dyn_cast<RedirectingFileSystem::DirectoryEntry>(E);
1311     assert(DE && "Should be a directory");
1312 
1313     for (std::unique_ptr<Entry> &SubEntry :
1314          llvm::make_range(DE->contents_begin(), DE->contents_end()))
1315       dumpEntry(OS, SubEntry.get(), NumSpaces + 2);
1316   }
1317 }
1318 
1319 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1320 LLVM_DUMP_METHOD void RedirectingFileSystem::dump() const { dump(dbgs()); }
1321 #endif
1322 
1323 /// A helper class to hold the common YAML parsing state.
1324 class llvm::vfs::RedirectingFileSystemParser {
1325   yaml::Stream &Stream;
1326 
1327   void error(yaml::Node *N, const Twine &Msg) { Stream.printError(N, Msg); }
1328 
1329   // false on error
1330   bool parseScalarString(yaml::Node *N, StringRef &Result,
1331                          SmallVectorImpl<char> &Storage) {
1332     const auto *S = dyn_cast<yaml::ScalarNode>(N);
1333 
1334     if (!S) {
1335       error(N, "expected string");
1336       return false;
1337     }
1338     Result = S->getValue(Storage);
1339     return true;
1340   }
1341 
1342   // false on error
1343   bool parseScalarBool(yaml::Node *N, bool &Result) {
1344     SmallString<5> Storage;
1345     StringRef Value;
1346     if (!parseScalarString(N, Value, Storage))
1347       return false;
1348 
1349     if (Value.equals_insensitive("true") || Value.equals_insensitive("on") ||
1350         Value.equals_insensitive("yes") || Value == "1") {
1351       Result = true;
1352       return true;
1353     } else if (Value.equals_insensitive("false") ||
1354                Value.equals_insensitive("off") ||
1355                Value.equals_insensitive("no") || Value == "0") {
1356       Result = false;
1357       return true;
1358     }
1359 
1360     error(N, "expected boolean value");
1361     return false;
1362   }
1363 
1364   struct KeyStatus {
1365     bool Required;
1366     bool Seen = false;
1367 
1368     KeyStatus(bool Required = false) : Required(Required) {}
1369   };
1370 
1371   using KeyStatusPair = std::pair<StringRef, KeyStatus>;
1372 
1373   // false on error
1374   bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
1375                                   DenseMap<StringRef, KeyStatus> &Keys) {
1376     if (!Keys.count(Key)) {
1377       error(KeyNode, "unknown key");
1378       return false;
1379     }
1380     KeyStatus &S = Keys[Key];
1381     if (S.Seen) {
1382       error(KeyNode, Twine("duplicate key '") + Key + "'");
1383       return false;
1384     }
1385     S.Seen = true;
1386     return true;
1387   }
1388 
1389   // false on error
1390   bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
1391     for (const auto &I : Keys) {
1392       if (I.second.Required && !I.second.Seen) {
1393         error(Obj, Twine("missing key '") + I.first + "'");
1394         return false;
1395       }
1396     }
1397     return true;
1398   }
1399 
1400 public:
1401   static RedirectingFileSystem::Entry *
1402   lookupOrCreateEntry(RedirectingFileSystem *FS, StringRef Name,
1403                       RedirectingFileSystem::Entry *ParentEntry = nullptr) {
1404     if (!ParentEntry) { // Look for a existent root
1405       for (const auto &Root : FS->Roots) {
1406         if (Name.equals(Root->getName())) {
1407           ParentEntry = Root.get();
1408           return ParentEntry;
1409         }
1410       }
1411     } else { // Advance to the next component
1412       auto *DE = dyn_cast<RedirectingFileSystem::DirectoryEntry>(ParentEntry);
1413       for (std::unique_ptr<RedirectingFileSystem::Entry> &Content :
1414            llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1415         auto *DirContent =
1416             dyn_cast<RedirectingFileSystem::DirectoryEntry>(Content.get());
1417         if (DirContent && Name.equals(Content->getName()))
1418           return DirContent;
1419       }
1420     }
1421 
1422     // ... or create a new one
1423     std::unique_ptr<RedirectingFileSystem::Entry> E =
1424         std::make_unique<RedirectingFileSystem::DirectoryEntry>(
1425             Name, Status("", getNextVirtualUniqueID(),
1426                          std::chrono::system_clock::now(), 0, 0, 0,
1427                          file_type::directory_file, sys::fs::all_all));
1428 
1429     if (!ParentEntry) { // Add a new root to the overlay
1430       FS->Roots.push_back(std::move(E));
1431       ParentEntry = FS->Roots.back().get();
1432       return ParentEntry;
1433     }
1434 
1435     auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(ParentEntry);
1436     DE->addContent(std::move(E));
1437     return DE->getLastContent();
1438   }
1439 
1440 private:
1441   void uniqueOverlayTree(RedirectingFileSystem *FS,
1442                          RedirectingFileSystem::Entry *SrcE,
1443                          RedirectingFileSystem::Entry *NewParentE = nullptr) {
1444     StringRef Name = SrcE->getName();
1445     switch (SrcE->getKind()) {
1446     case RedirectingFileSystem::EK_Directory: {
1447       auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(SrcE);
1448       // Empty directories could be present in the YAML as a way to
1449       // describe a file for a current directory after some of its subdir
1450       // is parsed. This only leads to redundant walks, ignore it.
1451       if (!Name.empty())
1452         NewParentE = lookupOrCreateEntry(FS, Name, NewParentE);
1453       for (std::unique_ptr<RedirectingFileSystem::Entry> &SubEntry :
1454            llvm::make_range(DE->contents_begin(), DE->contents_end()))
1455         uniqueOverlayTree(FS, SubEntry.get(), NewParentE);
1456       break;
1457     }
1458     case RedirectingFileSystem::EK_DirectoryRemap: {
1459       assert(NewParentE && "Parent entry must exist");
1460       auto *DR = cast<RedirectingFileSystem::DirectoryRemapEntry>(SrcE);
1461       auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(NewParentE);
1462       DE->addContent(
1463           std::make_unique<RedirectingFileSystem::DirectoryRemapEntry>(
1464               Name, DR->getExternalContentsPath(), DR->getUseName()));
1465       break;
1466     }
1467     case RedirectingFileSystem::EK_File: {
1468       assert(NewParentE && "Parent entry must exist");
1469       auto *FE = cast<RedirectingFileSystem::FileEntry>(SrcE);
1470       auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(NewParentE);
1471       DE->addContent(std::make_unique<RedirectingFileSystem::FileEntry>(
1472           Name, FE->getExternalContentsPath(), FE->getUseName()));
1473       break;
1474     }
1475     }
1476   }
1477 
1478   std::unique_ptr<RedirectingFileSystem::Entry>
1479   parseEntry(yaml::Node *N, RedirectingFileSystem *FS, bool IsRootEntry) {
1480     auto *M = dyn_cast<yaml::MappingNode>(N);
1481     if (!M) {
1482       error(N, "expected mapping node for file or directory entry");
1483       return nullptr;
1484     }
1485 
1486     KeyStatusPair Fields[] = {
1487         KeyStatusPair("name", true),
1488         KeyStatusPair("type", true),
1489         KeyStatusPair("contents", false),
1490         KeyStatusPair("external-contents", false),
1491         KeyStatusPair("use-external-name", false),
1492     };
1493 
1494     DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
1495 
1496     enum { CF_NotSet, CF_List, CF_External } ContentsField = CF_NotSet;
1497     std::vector<std::unique_ptr<RedirectingFileSystem::Entry>>
1498         EntryArrayContents;
1499     SmallString<256> ExternalContentsPath;
1500     SmallString<256> Name;
1501     yaml::Node *NameValueNode = nullptr;
1502     auto UseExternalName = RedirectingFileSystem::NK_NotSet;
1503     RedirectingFileSystem::EntryKind Kind;
1504 
1505     for (auto &I : *M) {
1506       StringRef Key;
1507       // Reuse the buffer for key and value, since we don't look at key after
1508       // parsing value.
1509       SmallString<256> Buffer;
1510       if (!parseScalarString(I.getKey(), Key, Buffer))
1511         return nullptr;
1512 
1513       if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
1514         return nullptr;
1515 
1516       StringRef Value;
1517       if (Key == "name") {
1518         if (!parseScalarString(I.getValue(), Value, Buffer))
1519           return nullptr;
1520 
1521         NameValueNode = I.getValue();
1522         // Guarantee that old YAML files containing paths with ".." and "."
1523         // are properly canonicalized before read into the VFS.
1524         Name = canonicalize(Value).str();
1525       } else if (Key == "type") {
1526         if (!parseScalarString(I.getValue(), Value, Buffer))
1527           return nullptr;
1528         if (Value == "file")
1529           Kind = RedirectingFileSystem::EK_File;
1530         else if (Value == "directory")
1531           Kind = RedirectingFileSystem::EK_Directory;
1532         else if (Value == "directory-remap")
1533           Kind = RedirectingFileSystem::EK_DirectoryRemap;
1534         else {
1535           error(I.getValue(), "unknown value for 'type'");
1536           return nullptr;
1537         }
1538       } else if (Key == "contents") {
1539         if (ContentsField != CF_NotSet) {
1540           error(I.getKey(),
1541                 "entry already has 'contents' or 'external-contents'");
1542           return nullptr;
1543         }
1544         ContentsField = CF_List;
1545         auto *Contents = dyn_cast<yaml::SequenceNode>(I.getValue());
1546         if (!Contents) {
1547           // FIXME: this is only for directories, what about files?
1548           error(I.getValue(), "expected array");
1549           return nullptr;
1550         }
1551 
1552         for (auto &I : *Contents) {
1553           if (std::unique_ptr<RedirectingFileSystem::Entry> E =
1554                   parseEntry(&I, FS, /*IsRootEntry*/ false))
1555             EntryArrayContents.push_back(std::move(E));
1556           else
1557             return nullptr;
1558         }
1559       } else if (Key == "external-contents") {
1560         if (ContentsField != CF_NotSet) {
1561           error(I.getKey(),
1562                 "entry already has 'contents' or 'external-contents'");
1563           return nullptr;
1564         }
1565         ContentsField = CF_External;
1566         if (!parseScalarString(I.getValue(), Value, Buffer))
1567           return nullptr;
1568 
1569         SmallString<256> FullPath;
1570         if (FS->IsRelativeOverlay) {
1571           FullPath = FS->getExternalContentsPrefixDir();
1572           assert(!FullPath.empty() &&
1573                  "External contents prefix directory must exist");
1574           llvm::sys::path::append(FullPath, Value);
1575         } else {
1576           FullPath = Value;
1577         }
1578 
1579         // Guarantee that old YAML files containing paths with ".." and "."
1580         // are properly canonicalized before read into the VFS.
1581         FullPath = canonicalize(FullPath);
1582         ExternalContentsPath = FullPath.str();
1583       } else if (Key == "use-external-name") {
1584         bool Val;
1585         if (!parseScalarBool(I.getValue(), Val))
1586           return nullptr;
1587         UseExternalName = Val ? RedirectingFileSystem::NK_External
1588                               : RedirectingFileSystem::NK_Virtual;
1589       } else {
1590         llvm_unreachable("key missing from Keys");
1591       }
1592     }
1593 
1594     if (Stream.failed())
1595       return nullptr;
1596 
1597     // check for missing keys
1598     if (ContentsField == CF_NotSet) {
1599       error(N, "missing key 'contents' or 'external-contents'");
1600       return nullptr;
1601     }
1602     if (!checkMissingKeys(N, Keys))
1603       return nullptr;
1604 
1605     // check invalid configuration
1606     if (Kind == RedirectingFileSystem::EK_Directory &&
1607         UseExternalName != RedirectingFileSystem::NK_NotSet) {
1608       error(N, "'use-external-name' is not supported for 'directory' entries");
1609       return nullptr;
1610     }
1611 
1612     if (Kind == RedirectingFileSystem::EK_DirectoryRemap &&
1613         ContentsField == CF_List) {
1614       error(N, "'contents' is not supported for 'directory-remap' entries");
1615       return nullptr;
1616     }
1617 
1618     sys::path::Style path_style = sys::path::Style::native;
1619     if (IsRootEntry) {
1620       // VFS root entries may be in either Posix or Windows style.  Figure out
1621       // which style we have, and use it consistently.
1622       if (sys::path::is_absolute(Name, sys::path::Style::posix)) {
1623         path_style = sys::path::Style::posix;
1624       } else if (sys::path::is_absolute(Name, sys::path::Style::windows)) {
1625         path_style = sys::path::Style::windows;
1626       } else {
1627         assert(NameValueNode && "Name presence should be checked earlier");
1628         error(NameValueNode,
1629               "entry with relative path at the root level is not discoverable");
1630         return nullptr;
1631       }
1632     }
1633 
1634     // Remove trailing slash(es), being careful not to remove the root path
1635     StringRef Trimmed = Name;
1636     size_t RootPathLen = sys::path::root_path(Trimmed, path_style).size();
1637     while (Trimmed.size() > RootPathLen &&
1638            sys::path::is_separator(Trimmed.back(), path_style))
1639       Trimmed = Trimmed.slice(0, Trimmed.size() - 1);
1640 
1641     // Get the last component
1642     StringRef LastComponent = sys::path::filename(Trimmed, path_style);
1643 
1644     std::unique_ptr<RedirectingFileSystem::Entry> Result;
1645     switch (Kind) {
1646     case RedirectingFileSystem::EK_File:
1647       Result = std::make_unique<RedirectingFileSystem::FileEntry>(
1648           LastComponent, std::move(ExternalContentsPath), UseExternalName);
1649       break;
1650     case RedirectingFileSystem::EK_DirectoryRemap:
1651       Result = std::make_unique<RedirectingFileSystem::DirectoryRemapEntry>(
1652           LastComponent, std::move(ExternalContentsPath), UseExternalName);
1653       break;
1654     case RedirectingFileSystem::EK_Directory:
1655       Result = std::make_unique<RedirectingFileSystem::DirectoryEntry>(
1656           LastComponent, std::move(EntryArrayContents),
1657           Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
1658                  0, 0, 0, file_type::directory_file, sys::fs::all_all));
1659       break;
1660     }
1661 
1662     StringRef Parent = sys::path::parent_path(Trimmed, path_style);
1663     if (Parent.empty())
1664       return Result;
1665 
1666     // if 'name' contains multiple components, create implicit directory entries
1667     for (sys::path::reverse_iterator I = sys::path::rbegin(Parent, path_style),
1668                                      E = sys::path::rend(Parent);
1669          I != E; ++I) {
1670       std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> Entries;
1671       Entries.push_back(std::move(Result));
1672       Result = std::make_unique<RedirectingFileSystem::DirectoryEntry>(
1673           *I, std::move(Entries),
1674           Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
1675                  0, 0, 0, file_type::directory_file, sys::fs::all_all));
1676     }
1677     return Result;
1678   }
1679 
1680 public:
1681   RedirectingFileSystemParser(yaml::Stream &S) : Stream(S) {}
1682 
1683   // false on error
1684   bool parse(yaml::Node *Root, RedirectingFileSystem *FS) {
1685     auto *Top = dyn_cast<yaml::MappingNode>(Root);
1686     if (!Top) {
1687       error(Root, "expected mapping node");
1688       return false;
1689     }
1690 
1691     KeyStatusPair Fields[] = {
1692         KeyStatusPair("version", true),
1693         KeyStatusPair("case-sensitive", false),
1694         KeyStatusPair("use-external-names", false),
1695         KeyStatusPair("overlay-relative", false),
1696         KeyStatusPair("fallthrough", false),
1697         KeyStatusPair("roots", true),
1698     };
1699 
1700     DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
1701     std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> RootEntries;
1702 
1703     // Parse configuration and 'roots'
1704     for (auto &I : *Top) {
1705       SmallString<10> KeyBuffer;
1706       StringRef Key;
1707       if (!parseScalarString(I.getKey(), Key, KeyBuffer))
1708         return false;
1709 
1710       if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
1711         return false;
1712 
1713       if (Key == "roots") {
1714         auto *Roots = dyn_cast<yaml::SequenceNode>(I.getValue());
1715         if (!Roots) {
1716           error(I.getValue(), "expected array");
1717           return false;
1718         }
1719 
1720         for (auto &I : *Roots) {
1721           if (std::unique_ptr<RedirectingFileSystem::Entry> E =
1722                   parseEntry(&I, FS, /*IsRootEntry*/ true))
1723             RootEntries.push_back(std::move(E));
1724           else
1725             return false;
1726         }
1727       } else if (Key == "version") {
1728         StringRef VersionString;
1729         SmallString<4> Storage;
1730         if (!parseScalarString(I.getValue(), VersionString, Storage))
1731           return false;
1732         int Version;
1733         if (VersionString.getAsInteger<int>(10, Version)) {
1734           error(I.getValue(), "expected integer");
1735           return false;
1736         }
1737         if (Version < 0) {
1738           error(I.getValue(), "invalid version number");
1739           return false;
1740         }
1741         if (Version != 0) {
1742           error(I.getValue(), "version mismatch, expected 0");
1743           return false;
1744         }
1745       } else if (Key == "case-sensitive") {
1746         if (!parseScalarBool(I.getValue(), FS->CaseSensitive))
1747           return false;
1748       } else if (Key == "overlay-relative") {
1749         if (!parseScalarBool(I.getValue(), FS->IsRelativeOverlay))
1750           return false;
1751       } else if (Key == "use-external-names") {
1752         if (!parseScalarBool(I.getValue(), FS->UseExternalNames))
1753           return false;
1754       } else if (Key == "fallthrough") {
1755         if (!parseScalarBool(I.getValue(), FS->IsFallthrough))
1756           return false;
1757       } else {
1758         llvm_unreachable("key missing from Keys");
1759       }
1760     }
1761 
1762     if (Stream.failed())
1763       return false;
1764 
1765     if (!checkMissingKeys(Top, Keys))
1766       return false;
1767 
1768     // Now that we sucessefully parsed the YAML file, canonicalize the internal
1769     // representation to a proper directory tree so that we can search faster
1770     // inside the VFS.
1771     for (auto &E : RootEntries)
1772       uniqueOverlayTree(FS, E.get());
1773 
1774     return true;
1775   }
1776 };
1777 
1778 std::unique_ptr<RedirectingFileSystem>
1779 RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer,
1780                               SourceMgr::DiagHandlerTy DiagHandler,
1781                               StringRef YAMLFilePath, void *DiagContext,
1782                               IntrusiveRefCntPtr<FileSystem> ExternalFS) {
1783   SourceMgr SM;
1784   yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
1785 
1786   SM.setDiagHandler(DiagHandler, DiagContext);
1787   yaml::document_iterator DI = Stream.begin();
1788   yaml::Node *Root = DI->getRoot();
1789   if (DI == Stream.end() || !Root) {
1790     SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
1791     return nullptr;
1792   }
1793 
1794   RedirectingFileSystemParser P(Stream);
1795 
1796   std::unique_ptr<RedirectingFileSystem> FS(
1797       new RedirectingFileSystem(ExternalFS));
1798 
1799   if (!YAMLFilePath.empty()) {
1800     // Use the YAML path from -ivfsoverlay to compute the dir to be prefixed
1801     // to each 'external-contents' path.
1802     //
1803     // Example:
1804     //    -ivfsoverlay dummy.cache/vfs/vfs.yaml
1805     // yields:
1806     //  FS->ExternalContentsPrefixDir => /<absolute_path_to>/dummy.cache/vfs
1807     //
1808     SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath);
1809     std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir);
1810     assert(!EC && "Overlay dir final path must be absolute");
1811     (void)EC;
1812     FS->setExternalContentsPrefixDir(OverlayAbsDir);
1813   }
1814 
1815   if (!P.parse(Root, FS.get()))
1816     return nullptr;
1817 
1818   return FS;
1819 }
1820 
1821 std::unique_ptr<RedirectingFileSystem> RedirectingFileSystem::create(
1822     ArrayRef<std::pair<std::string, std::string>> RemappedFiles,
1823     bool UseExternalNames, FileSystem &ExternalFS) {
1824   std::unique_ptr<RedirectingFileSystem> FS(
1825       new RedirectingFileSystem(&ExternalFS));
1826   FS->UseExternalNames = UseExternalNames;
1827 
1828   StringMap<RedirectingFileSystem::Entry *> Entries;
1829 
1830   for (auto &Mapping : llvm::reverse(RemappedFiles)) {
1831     SmallString<128> From = StringRef(Mapping.first);
1832     SmallString<128> To = StringRef(Mapping.second);
1833     {
1834       auto EC = ExternalFS.makeAbsolute(From);
1835       (void)EC;
1836       assert(!EC && "Could not make absolute path");
1837     }
1838 
1839     // Check if we've already mapped this file. The first one we see (in the
1840     // reverse iteration) wins.
1841     RedirectingFileSystem::Entry *&ToEntry = Entries[From];
1842     if (ToEntry)
1843       continue;
1844 
1845     // Add parent directories.
1846     RedirectingFileSystem::Entry *Parent = nullptr;
1847     StringRef FromDirectory = llvm::sys::path::parent_path(From);
1848     for (auto I = llvm::sys::path::begin(FromDirectory),
1849               E = llvm::sys::path::end(FromDirectory);
1850          I != E; ++I) {
1851       Parent = RedirectingFileSystemParser::lookupOrCreateEntry(FS.get(), *I,
1852                                                                 Parent);
1853     }
1854     assert(Parent && "File without a directory?");
1855     {
1856       auto EC = ExternalFS.makeAbsolute(To);
1857       (void)EC;
1858       assert(!EC && "Could not make absolute path");
1859     }
1860 
1861     // Add the file.
1862     auto NewFile = std::make_unique<RedirectingFileSystem::FileEntry>(
1863         llvm::sys::path::filename(From), To,
1864         UseExternalNames ? RedirectingFileSystem::NK_External
1865                          : RedirectingFileSystem::NK_Virtual);
1866     ToEntry = NewFile.get();
1867     cast<RedirectingFileSystem::DirectoryEntry>(Parent)->addContent(
1868         std::move(NewFile));
1869   }
1870 
1871   return FS;
1872 }
1873 
1874 RedirectingFileSystem::LookupResult::LookupResult(
1875     Entry *E, sys::path::const_iterator Start, sys::path::const_iterator End)
1876     : E(E) {
1877   assert(E != nullptr);
1878   // If the matched entry is a DirectoryRemapEntry, set ExternalRedirect to the
1879   // path of the directory it maps to in the external file system plus any
1880   // remaining path components in the provided iterator.
1881   if (auto *DRE = dyn_cast<RedirectingFileSystem::DirectoryRemapEntry>(E)) {
1882     SmallString<256> Redirect(DRE->getExternalContentsPath());
1883     sys::path::append(Redirect, Start, End,
1884                       getExistingStyle(DRE->getExternalContentsPath()));
1885     ExternalRedirect = std::string(Redirect);
1886   }
1887 }
1888 
1889 bool RedirectingFileSystem::shouldFallBackToExternalFS(
1890     std::error_code EC, RedirectingFileSystem::Entry *E) const {
1891   if (E && !isa<RedirectingFileSystem::DirectoryRemapEntry>(E))
1892     return false;
1893   return shouldUseExternalFS() && EC == llvm::errc::no_such_file_or_directory;
1894 }
1895 
1896 std::error_code
1897 RedirectingFileSystem::makeCanonical(SmallVectorImpl<char> &Path) const {
1898   if (std::error_code EC = makeAbsolute(Path))
1899     return EC;
1900 
1901   llvm::SmallString<256> CanonicalPath =
1902       canonicalize(StringRef(Path.data(), Path.size()));
1903   if (CanonicalPath.empty())
1904     return make_error_code(llvm::errc::invalid_argument);
1905 
1906   Path.assign(CanonicalPath.begin(), CanonicalPath.end());
1907   return {};
1908 }
1909 
1910 ErrorOr<RedirectingFileSystem::LookupResult>
1911 RedirectingFileSystem::lookupPath(StringRef Path) const {
1912   sys::path::const_iterator Start = sys::path::begin(Path);
1913   sys::path::const_iterator End = sys::path::end(Path);
1914   for (const auto &Root : Roots) {
1915     ErrorOr<RedirectingFileSystem::LookupResult> Result =
1916         lookupPathImpl(Start, End, Root.get());
1917     if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
1918       return Result;
1919   }
1920   return make_error_code(llvm::errc::no_such_file_or_directory);
1921 }
1922 
1923 ErrorOr<RedirectingFileSystem::LookupResult>
1924 RedirectingFileSystem::lookupPathImpl(
1925     sys::path::const_iterator Start, sys::path::const_iterator End,
1926     RedirectingFileSystem::Entry *From) const {
1927   assert(!isTraversalComponent(*Start) &&
1928          !isTraversalComponent(From->getName()) &&
1929          "Paths should not contain traversal components");
1930 
1931   StringRef FromName = From->getName();
1932 
1933   // Forward the search to the next component in case this is an empty one.
1934   if (!FromName.empty()) {
1935     if (!pathComponentMatches(*Start, FromName))
1936       return make_error_code(llvm::errc::no_such_file_or_directory);
1937 
1938     ++Start;
1939 
1940     if (Start == End) {
1941       // Match!
1942       return LookupResult(From, Start, End);
1943     }
1944   }
1945 
1946   if (isa<RedirectingFileSystem::FileEntry>(From))
1947     return make_error_code(llvm::errc::not_a_directory);
1948 
1949   if (isa<RedirectingFileSystem::DirectoryRemapEntry>(From))
1950     return LookupResult(From, Start, End);
1951 
1952   auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(From);
1953   for (const std::unique_ptr<RedirectingFileSystem::Entry> &DirEntry :
1954        llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1955     ErrorOr<RedirectingFileSystem::LookupResult> Result =
1956         lookupPathImpl(Start, End, DirEntry.get());
1957     if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
1958       return Result;
1959   }
1960 
1961   return make_error_code(llvm::errc::no_such_file_or_directory);
1962 }
1963 
1964 static Status getRedirectedFileStatus(const Twine &Path, bool UseExternalNames,
1965                                       Status ExternalStatus) {
1966   Status S = ExternalStatus;
1967   if (!UseExternalNames)
1968     S = Status::copyWithNewName(S, Path);
1969   S.IsVFSMapped = true;
1970   return S;
1971 }
1972 
1973 ErrorOr<Status> RedirectingFileSystem::status(
1974     const Twine &Path, const RedirectingFileSystem::LookupResult &Result) {
1975   if (Optional<StringRef> ExtRedirect = Result.getExternalRedirect()) {
1976     ErrorOr<Status> S = ExternalFS->status(*ExtRedirect);
1977     if (!S)
1978       return S;
1979     auto *RE = cast<RedirectingFileSystem::RemapEntry>(Result.E);
1980     return getRedirectedFileStatus(Path, RE->useExternalName(UseExternalNames),
1981                                    *S);
1982   }
1983 
1984   auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(Result.E);
1985   return Status::copyWithNewName(DE->getStatus(), Path);
1986 }
1987 
1988 ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path_) {
1989   SmallString<256> Path;
1990   Path_.toVector(Path);
1991 
1992   if (std::error_code EC = makeCanonical(Path))
1993     return EC;
1994 
1995   ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
1996   if (!Result) {
1997     if (shouldFallBackToExternalFS(Result.getError()))
1998       return ExternalFS->status(Path);
1999     return Result.getError();
2000   }
2001 
2002   ErrorOr<Status> S = status(Path, *Result);
2003   if (!S && shouldFallBackToExternalFS(S.getError(), Result->E))
2004     S = ExternalFS->status(Path);
2005   return S;
2006 }
2007 
2008 namespace {
2009 
2010 /// Provide a file wrapper with an overriden status.
2011 class FileWithFixedStatus : public File {
2012   std::unique_ptr<File> InnerFile;
2013   Status S;
2014 
2015 public:
2016   FileWithFixedStatus(std::unique_ptr<File> InnerFile, Status S)
2017       : InnerFile(std::move(InnerFile)), S(std::move(S)) {}
2018 
2019   ErrorOr<Status> status() override { return S; }
2020   ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
2021 
2022   getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
2023             bool IsVolatile) override {
2024     return InnerFile->getBuffer(Name, FileSize, RequiresNullTerminator,
2025                                 IsVolatile);
2026   }
2027 
2028   std::error_code close() override { return InnerFile->close(); }
2029 };
2030 
2031 } // namespace
2032 
2033 ErrorOr<std::unique_ptr<File>>
2034 RedirectingFileSystem::openFileForRead(const Twine &Path_) {
2035   SmallString<256> Path;
2036   Path_.toVector(Path);
2037 
2038   if (std::error_code EC = makeCanonical(Path))
2039     return EC;
2040 
2041   ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
2042   if (!Result) {
2043     if (shouldFallBackToExternalFS(Result.getError()))
2044       return ExternalFS->openFileForRead(Path);
2045     return Result.getError();
2046   }
2047 
2048   if (!Result->getExternalRedirect()) // FIXME: errc::not_a_file?
2049     return make_error_code(llvm::errc::invalid_argument);
2050 
2051   StringRef ExtRedirect = *Result->getExternalRedirect();
2052   auto *RE = cast<RedirectingFileSystem::RemapEntry>(Result->E);
2053 
2054   auto ExternalFile = ExternalFS->openFileForRead(ExtRedirect);
2055   if (!ExternalFile) {
2056     if (shouldFallBackToExternalFS(ExternalFile.getError(), Result->E))
2057       return ExternalFS->openFileForRead(Path);
2058     return ExternalFile;
2059   }
2060 
2061   auto ExternalStatus = (*ExternalFile)->status();
2062   if (!ExternalStatus)
2063     return ExternalStatus.getError();
2064 
2065   // FIXME: Update the status with the name and VFSMapped.
2066   Status S = getRedirectedFileStatus(
2067       Path, RE->useExternalName(UseExternalNames), *ExternalStatus);
2068   return std::unique_ptr<File>(
2069       std::make_unique<FileWithFixedStatus>(std::move(*ExternalFile), S));
2070 }
2071 
2072 std::error_code
2073 RedirectingFileSystem::getRealPath(const Twine &Path_,
2074                                    SmallVectorImpl<char> &Output) const {
2075   SmallString<256> Path;
2076   Path_.toVector(Path);
2077 
2078   if (std::error_code EC = makeCanonical(Path))
2079     return EC;
2080 
2081   ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
2082   if (!Result) {
2083     if (shouldFallBackToExternalFS(Result.getError()))
2084       return ExternalFS->getRealPath(Path, Output);
2085     return Result.getError();
2086   }
2087 
2088   // If we found FileEntry or DirectoryRemapEntry, look up the mapped
2089   // path in the external file system.
2090   if (auto ExtRedirect = Result->getExternalRedirect()) {
2091     auto P = ExternalFS->getRealPath(*ExtRedirect, Output);
2092     if (!P && shouldFallBackToExternalFS(P, Result->E)) {
2093       return ExternalFS->getRealPath(Path, Output);
2094     }
2095     return P;
2096   }
2097 
2098   // If we found a DirectoryEntry, still fall back to ExternalFS if allowed,
2099   // because directories don't have a single external contents path.
2100   return shouldUseExternalFS() ? ExternalFS->getRealPath(Path, Output)
2101                                : llvm::errc::invalid_argument;
2102 }
2103 
2104 std::unique_ptr<FileSystem>
2105 vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
2106                     SourceMgr::DiagHandlerTy DiagHandler,
2107                     StringRef YAMLFilePath, void *DiagContext,
2108                     IntrusiveRefCntPtr<FileSystem> ExternalFS) {
2109   return RedirectingFileSystem::create(std::move(Buffer), DiagHandler,
2110                                        YAMLFilePath, DiagContext,
2111                                        std::move(ExternalFS));
2112 }
2113 
2114 static void getVFSEntries(RedirectingFileSystem::Entry *SrcE,
2115                           SmallVectorImpl<StringRef> &Path,
2116                           SmallVectorImpl<YAMLVFSEntry> &Entries) {
2117   auto Kind = SrcE->getKind();
2118   if (Kind == RedirectingFileSystem::EK_Directory) {
2119     auto *DE = dyn_cast<RedirectingFileSystem::DirectoryEntry>(SrcE);
2120     assert(DE && "Must be a directory");
2121     for (std::unique_ptr<RedirectingFileSystem::Entry> &SubEntry :
2122          llvm::make_range(DE->contents_begin(), DE->contents_end())) {
2123       Path.push_back(SubEntry->getName());
2124       getVFSEntries(SubEntry.get(), Path, Entries);
2125       Path.pop_back();
2126     }
2127     return;
2128   }
2129 
2130   if (Kind == RedirectingFileSystem::EK_DirectoryRemap) {
2131     auto *DR = dyn_cast<RedirectingFileSystem::DirectoryRemapEntry>(SrcE);
2132     assert(DR && "Must be a directory remap");
2133     SmallString<128> VPath;
2134     for (auto &Comp : Path)
2135       llvm::sys::path::append(VPath, Comp);
2136     Entries.push_back(
2137         YAMLVFSEntry(VPath.c_str(), DR->getExternalContentsPath()));
2138     return;
2139   }
2140 
2141   assert(Kind == RedirectingFileSystem::EK_File && "Must be a EK_File");
2142   auto *FE = dyn_cast<RedirectingFileSystem::FileEntry>(SrcE);
2143   assert(FE && "Must be a file");
2144   SmallString<128> VPath;
2145   for (auto &Comp : Path)
2146     llvm::sys::path::append(VPath, Comp);
2147   Entries.push_back(YAMLVFSEntry(VPath.c_str(), FE->getExternalContentsPath()));
2148 }
2149 
2150 void vfs::collectVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
2151                              SourceMgr::DiagHandlerTy DiagHandler,
2152                              StringRef YAMLFilePath,
2153                              SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
2154                              void *DiagContext,
2155                              IntrusiveRefCntPtr<FileSystem> ExternalFS) {
2156   std::unique_ptr<RedirectingFileSystem> VFS = RedirectingFileSystem::create(
2157       std::move(Buffer), DiagHandler, YAMLFilePath, DiagContext,
2158       std::move(ExternalFS));
2159   if (!VFS)
2160     return;
2161   ErrorOr<RedirectingFileSystem::LookupResult> RootResult =
2162       VFS->lookupPath("/");
2163   if (!RootResult)
2164     return;
2165   SmallVector<StringRef, 8> Components;
2166   Components.push_back("/");
2167   getVFSEntries(RootResult->E, Components, CollectedEntries);
2168 }
2169 
2170 UniqueID vfs::getNextVirtualUniqueID() {
2171   static std::atomic<unsigned> UID;
2172   unsigned ID = ++UID;
2173   // The following assumes that uint64_t max will never collide with a real
2174   // dev_t value from the OS.
2175   return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
2176 }
2177 
2178 void YAMLVFSWriter::addEntry(StringRef VirtualPath, StringRef RealPath,
2179                              bool IsDirectory) {
2180   assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute");
2181   assert(sys::path::is_absolute(RealPath) && "real path not absolute");
2182   assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported");
2183   Mappings.emplace_back(VirtualPath, RealPath, IsDirectory);
2184 }
2185 
2186 void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) {
2187   addEntry(VirtualPath, RealPath, /*IsDirectory=*/false);
2188 }
2189 
2190 void YAMLVFSWriter::addDirectoryMapping(StringRef VirtualPath,
2191                                         StringRef RealPath) {
2192   addEntry(VirtualPath, RealPath, /*IsDirectory=*/true);
2193 }
2194 
2195 namespace {
2196 
2197 class JSONWriter {
2198   llvm::raw_ostream &OS;
2199   SmallVector<StringRef, 16> DirStack;
2200 
2201   unsigned getDirIndent() { return 4 * DirStack.size(); }
2202   unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
2203   bool containedIn(StringRef Parent, StringRef Path);
2204   StringRef containedPart(StringRef Parent, StringRef Path);
2205   void startDirectory(StringRef Path);
2206   void endDirectory();
2207   void writeEntry(StringRef VPath, StringRef RPath);
2208 
2209 public:
2210   JSONWriter(llvm::raw_ostream &OS) : OS(OS) {}
2211 
2212   void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> UseExternalNames,
2213              Optional<bool> IsCaseSensitive, Optional<bool> IsOverlayRelative,
2214              StringRef OverlayDir);
2215 };
2216 
2217 } // namespace
2218 
2219 bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
2220   using namespace llvm::sys;
2221 
2222   // Compare each path component.
2223   auto IParent = path::begin(Parent), EParent = path::end(Parent);
2224   for (auto IChild = path::begin(Path), EChild = path::end(Path);
2225        IParent != EParent && IChild != EChild; ++IParent, ++IChild) {
2226     if (*IParent != *IChild)
2227       return false;
2228   }
2229   // Have we exhausted the parent path?
2230   return IParent == EParent;
2231 }
2232 
2233 StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
2234   assert(!Parent.empty());
2235   assert(containedIn(Parent, Path));
2236   return Path.slice(Parent.size() + 1, StringRef::npos);
2237 }
2238 
2239 void JSONWriter::startDirectory(StringRef Path) {
2240   StringRef Name =
2241       DirStack.empty() ? Path : containedPart(DirStack.back(), Path);
2242   DirStack.push_back(Path);
2243   unsigned Indent = getDirIndent();
2244   OS.indent(Indent) << "{\n";
2245   OS.indent(Indent + 2) << "'type': 'directory',\n";
2246   OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n";
2247   OS.indent(Indent + 2) << "'contents': [\n";
2248 }
2249 
2250 void JSONWriter::endDirectory() {
2251   unsigned Indent = getDirIndent();
2252   OS.indent(Indent + 2) << "]\n";
2253   OS.indent(Indent) << "}";
2254 
2255   DirStack.pop_back();
2256 }
2257 
2258 void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) {
2259   unsigned Indent = getFileIndent();
2260   OS.indent(Indent) << "{\n";
2261   OS.indent(Indent + 2) << "'type': 'file',\n";
2262   OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n";
2263   OS.indent(Indent + 2) << "'external-contents': \""
2264                         << llvm::yaml::escape(RPath) << "\"\n";
2265   OS.indent(Indent) << "}";
2266 }
2267 
2268 void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
2269                        Optional<bool> UseExternalNames,
2270                        Optional<bool> IsCaseSensitive,
2271                        Optional<bool> IsOverlayRelative,
2272                        StringRef OverlayDir) {
2273   using namespace llvm::sys;
2274 
2275   OS << "{\n"
2276         "  'version': 0,\n";
2277   if (IsCaseSensitive.hasValue())
2278     OS << "  'case-sensitive': '"
2279        << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n";
2280   if (UseExternalNames.hasValue())
2281     OS << "  'use-external-names': '"
2282        << (UseExternalNames.getValue() ? "true" : "false") << "',\n";
2283   bool UseOverlayRelative = false;
2284   if (IsOverlayRelative.hasValue()) {
2285     UseOverlayRelative = IsOverlayRelative.getValue();
2286     OS << "  'overlay-relative': '" << (UseOverlayRelative ? "true" : "false")
2287        << "',\n";
2288   }
2289   OS << "  'roots': [\n";
2290 
2291   if (!Entries.empty()) {
2292     const YAMLVFSEntry &Entry = Entries.front();
2293 
2294     startDirectory(
2295       Entry.IsDirectory ? Entry.VPath : path::parent_path(Entry.VPath)
2296     );
2297 
2298     StringRef RPath = Entry.RPath;
2299     if (UseOverlayRelative) {
2300       unsigned OverlayDirLen = OverlayDir.size();
2301       assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
2302              "Overlay dir must be contained in RPath");
2303       RPath = RPath.slice(OverlayDirLen, RPath.size());
2304     }
2305 
2306     bool IsCurrentDirEmpty = true;
2307     if (!Entry.IsDirectory) {
2308       writeEntry(path::filename(Entry.VPath), RPath);
2309       IsCurrentDirEmpty = false;
2310     }
2311 
2312     for (const auto &Entry : Entries.slice(1)) {
2313       StringRef Dir =
2314           Entry.IsDirectory ? Entry.VPath : path::parent_path(Entry.VPath);
2315       if (Dir == DirStack.back()) {
2316         if (!IsCurrentDirEmpty) {
2317           OS << ",\n";
2318         }
2319       } else {
2320         bool IsDirPoppedFromStack = false;
2321         while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) {
2322           OS << "\n";
2323           endDirectory();
2324           IsDirPoppedFromStack = true;
2325         }
2326         if (IsDirPoppedFromStack || !IsCurrentDirEmpty) {
2327           OS << ",\n";
2328         }
2329         startDirectory(Dir);
2330         IsCurrentDirEmpty = true;
2331       }
2332       StringRef RPath = Entry.RPath;
2333       if (UseOverlayRelative) {
2334         unsigned OverlayDirLen = OverlayDir.size();
2335         assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
2336                "Overlay dir must be contained in RPath");
2337         RPath = RPath.slice(OverlayDirLen, RPath.size());
2338       }
2339       if (!Entry.IsDirectory) {
2340         writeEntry(path::filename(Entry.VPath), RPath);
2341         IsCurrentDirEmpty = false;
2342       }
2343     }
2344 
2345     while (!DirStack.empty()) {
2346       OS << "\n";
2347       endDirectory();
2348     }
2349     OS << "\n";
2350   }
2351 
2352   OS << "  ]\n"
2353      << "}\n";
2354 }
2355 
2356 void YAMLVFSWriter::write(llvm::raw_ostream &OS) {
2357   llvm::sort(Mappings, [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
2358     return LHS.VPath < RHS.VPath;
2359   });
2360 
2361   JSONWriter(OS).write(Mappings, UseExternalNames, IsCaseSensitive,
2362                        IsOverlayRelative, OverlayDir);
2363 }
2364 
2365 vfs::recursive_directory_iterator::recursive_directory_iterator(
2366     FileSystem &FS_, const Twine &Path, std::error_code &EC)
2367     : FS(&FS_) {
2368   directory_iterator I = FS->dir_begin(Path, EC);
2369   if (I != directory_iterator()) {
2370     State = std::make_shared<detail::RecDirIterState>();
2371     State->Stack.push(I);
2372   }
2373 }
2374 
2375 vfs::recursive_directory_iterator &
2376 recursive_directory_iterator::increment(std::error_code &EC) {
2377   assert(FS && State && !State->Stack.empty() && "incrementing past end");
2378   assert(!State->Stack.top()->path().empty() && "non-canonical end iterator");
2379   vfs::directory_iterator End;
2380 
2381   if (State->HasNoPushRequest)
2382     State->HasNoPushRequest = false;
2383   else {
2384     if (State->Stack.top()->type() == sys::fs::file_type::directory_file) {
2385       vfs::directory_iterator I = FS->dir_begin(State->Stack.top()->path(), EC);
2386       if (I != End) {
2387         State->Stack.push(I);
2388         return *this;
2389       }
2390     }
2391   }
2392 
2393   while (!State->Stack.empty() && State->Stack.top().increment(EC) == End)
2394     State->Stack.pop();
2395 
2396   if (State->Stack.empty())
2397     State.reset(); // end iterator
2398 
2399   return *this;
2400 }
2401