blob: f2f8ffafc506479f8cccea04c03ed0be3deb2c07 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- VirtualFileSystem.h - Virtual File System Layer ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
Andrew Scullcdfcccc2018-10-05 20:58:37 +010011/// Defines the virtual file system interface vfs::FileSystem.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010012//
13//===----------------------------------------------------------------------===//
14
Andrew Scull0372a572018-11-16 15:47:06 +000015#ifndef LLVM_SUPPORT_VIRTUALFILESYSTEM_H
16#define LLVM_SUPPORT_VIRTUALFILESYSTEM_H
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010017
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010018#include "llvm/ADT/IntrusiveRefCntPtr.h"
19#include "llvm/ADT/None.h"
20#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/Twine.h"
24#include "llvm/Support/Chrono.h"
25#include "llvm/Support/ErrorOr.h"
26#include "llvm/Support/FileSystem.h"
27#include "llvm/Support/SourceMgr.h"
28#include <cassert>
29#include <cstdint>
30#include <ctime>
31#include <memory>
32#include <stack>
33#include <string>
34#include <system_error>
35#include <utility>
36#include <vector>
37
38namespace llvm {
39
40class MemoryBuffer;
41
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042namespace vfs {
43
Andrew Scullcdfcccc2018-10-05 20:58:37 +010044/// The result of a \p status operation.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010045class Status {
46 std::string Name;
47 llvm::sys::fs::UniqueID UID;
48 llvm::sys::TimePoint<> MTime;
49 uint32_t User;
50 uint32_t Group;
51 uint64_t Size;
52 llvm::sys::fs::file_type Type = llvm::sys::fs::file_type::status_error;
53 llvm::sys::fs::perms Perms;
54
55public:
Andrew Scull0372a572018-11-16 15:47:06 +000056 // FIXME: remove when files support multiple names
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010057 bool IsVFSMapped = false;
58
59 Status() = default;
60 Status(const llvm::sys::fs::file_status &Status);
61 Status(StringRef Name, llvm::sys::fs::UniqueID UID,
62 llvm::sys::TimePoint<> MTime, uint32_t User, uint32_t Group,
63 uint64_t Size, llvm::sys::fs::file_type Type,
64 llvm::sys::fs::perms Perms);
65
66 /// Get a copy of a Status with a different name.
67 static Status copyWithNewName(const Status &In, StringRef NewName);
68 static Status copyWithNewName(const llvm::sys::fs::file_status &In,
69 StringRef NewName);
70
Andrew Scullcdfcccc2018-10-05 20:58:37 +010071 /// Returns the name that should be used for this file or directory.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010072 StringRef getName() const { return Name; }
73
74 /// @name Status interface from llvm::sys::fs
75 /// @{
76 llvm::sys::fs::file_type getType() const { return Type; }
77 llvm::sys::fs::perms getPermissions() const { return Perms; }
78 llvm::sys::TimePoint<> getLastModificationTime() const { return MTime; }
79 llvm::sys::fs::UniqueID getUniqueID() const { return UID; }
80 uint32_t getUser() const { return User; }
81 uint32_t getGroup() const { return Group; }
82 uint64_t getSize() const { return Size; }
83 /// @}
84 /// @name Status queries
85 /// These are static queries in llvm::sys::fs.
86 /// @{
87 bool equivalent(const Status &Other) const;
88 bool isDirectory() const;
89 bool isRegularFile() const;
90 bool isOther() const;
91 bool isSymlink() const;
92 bool isStatusKnown() const;
93 bool exists() const;
94 /// @}
95};
96
Andrew Scullcdfcccc2018-10-05 20:58:37 +010097/// Represents an open file.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010098class File {
99public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100100 /// Destroy the file after closing it (if open).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100101 /// Sub-classes should generally call close() inside their destructors. We
102 /// cannot do that from the base class, since close is virtual.
103 virtual ~File();
104
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100105 /// Get the status of the file.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100106 virtual llvm::ErrorOr<Status> status() = 0;
107
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100108 /// Get the name of the file
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100109 virtual llvm::ErrorOr<std::string> getName() {
110 if (auto Status = status())
111 return Status->getName().str();
112 else
113 return Status.getError();
114 }
115
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100116 /// Get the contents of the file as a \p MemoryBuffer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100117 virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
118 getBuffer(const Twine &Name, int64_t FileSize = -1,
119 bool RequiresNullTerminator = true, bool IsVolatile = false) = 0;
120
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100121 /// Closes the file.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100122 virtual std::error_code close() = 0;
123};
124
Andrew Scull0372a572018-11-16 15:47:06 +0000125/// A member of a directory, yielded by a directory_iterator.
126/// Only information available on most platforms is included.
127class directory_entry {
128 std::string Path;
129 llvm::sys::fs::file_type Type;
130
131public:
132 directory_entry() = default;
133 directory_entry(std::string Path, llvm::sys::fs::file_type Type)
134 : Path(std::move(Path)), Type(Type) {}
135
136 llvm::StringRef path() const { return Path; }
137 llvm::sys::fs::file_type type() const { return Type; }
138};
139
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100140namespace detail {
141
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100142/// An interface for virtual file systems to provide an iterator over the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100143/// (non-recursive) contents of a directory.
144struct DirIterImpl {
145 virtual ~DirIterImpl();
146
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100147 /// Sets \c CurrentEntry to the next entry in the directory on success,
Andrew Scull0372a572018-11-16 15:47:06 +0000148 /// to directory_entry() at end, or returns a system-defined \c error_code.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100149 virtual std::error_code increment() = 0;
150
Andrew Scull0372a572018-11-16 15:47:06 +0000151 directory_entry CurrentEntry;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100152};
153
154} // namespace detail
155
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100156/// An input iterator over the entries in a virtual path, similar to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100157/// llvm::sys::fs::directory_iterator.
158class directory_iterator {
159 std::shared_ptr<detail::DirIterImpl> Impl; // Input iterator semantics on copy
160
161public:
162 directory_iterator(std::shared_ptr<detail::DirIterImpl> I)
163 : Impl(std::move(I)) {
164 assert(Impl.get() != nullptr && "requires non-null implementation");
Andrew Scull0372a572018-11-16 15:47:06 +0000165 if (Impl->CurrentEntry.path().empty())
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100166 Impl.reset(); // Normalize the end iterator to Impl == nullptr.
167 }
168
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100169 /// Construct an 'end' iterator.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100170 directory_iterator() = default;
171
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100172 /// Equivalent to operator++, with an error code.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100173 directory_iterator &increment(std::error_code &EC) {
174 assert(Impl && "attempting to increment past end");
175 EC = Impl->increment();
Andrew Scull0372a572018-11-16 15:47:06 +0000176 if (Impl->CurrentEntry.path().empty())
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100177 Impl.reset(); // Normalize the end iterator to Impl == nullptr.
178 return *this;
179 }
180
Andrew Scull0372a572018-11-16 15:47:06 +0000181 const directory_entry &operator*() const { return Impl->CurrentEntry; }
182 const directory_entry *operator->() const { return &Impl->CurrentEntry; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100183
184 bool operator==(const directory_iterator &RHS) const {
185 if (Impl && RHS.Impl)
Andrew Scull0372a572018-11-16 15:47:06 +0000186 return Impl->CurrentEntry.path() == RHS.Impl->CurrentEntry.path();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100187 return !Impl && !RHS.Impl;
188 }
189 bool operator!=(const directory_iterator &RHS) const {
190 return !(*this == RHS);
191 }
192};
193
194class FileSystem;
195
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100196/// An input iterator over the recursive contents of a virtual path,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100197/// similar to llvm::sys::fs::recursive_directory_iterator.
198class recursive_directory_iterator {
199 using IterState =
200 std::stack<directory_iterator, std::vector<directory_iterator>>;
201
202 FileSystem *FS;
203 std::shared_ptr<IterState> State; // Input iterator semantics on copy.
204
205public:
206 recursive_directory_iterator(FileSystem &FS, const Twine &Path,
207 std::error_code &EC);
208
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100209 /// Construct an 'end' iterator.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100210 recursive_directory_iterator() = default;
211
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100212 /// Equivalent to operator++, with an error code.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100213 recursive_directory_iterator &increment(std::error_code &EC);
214
Andrew Scull0372a572018-11-16 15:47:06 +0000215 const directory_entry &operator*() const { return *State->top(); }
216 const directory_entry *operator->() const { return &*State->top(); }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100217
218 bool operator==(const recursive_directory_iterator &Other) const {
219 return State == Other.State; // identity
220 }
221 bool operator!=(const recursive_directory_iterator &RHS) const {
222 return !(*this == RHS);
223 }
224
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100225 /// Gets the current level. Starting path is at level 0.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100226 int level() const {
227 assert(!State->empty() && "Cannot get level without any iteration state");
Andrew Scull0372a572018-11-16 15:47:06 +0000228 return State->size() - 1;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100229 }
230};
231
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100232/// The virtual file system interface.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100233class FileSystem : public llvm::ThreadSafeRefCountedBase<FileSystem> {
234public:
235 virtual ~FileSystem();
236
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100237 /// Get the status of the entry at \p Path, if one exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100238 virtual llvm::ErrorOr<Status> status(const Twine &Path) = 0;
239
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100240 /// Get a \p File object for the file at \p Path, if one exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100241 virtual llvm::ErrorOr<std::unique_ptr<File>>
242 openFileForRead(const Twine &Path) = 0;
243
244 /// This is a convenience method that opens a file, gets its content and then
245 /// closes the file.
246 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
247 getBufferForFile(const Twine &Name, int64_t FileSize = -1,
248 bool RequiresNullTerminator = true, bool IsVolatile = false);
249
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100250 /// Get a directory_iterator for \p Dir.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100251 /// \note The 'end' iterator is directory_iterator().
252 virtual directory_iterator dir_begin(const Twine &Dir,
253 std::error_code &EC) = 0;
254
255 /// Set the working directory. This will affect all following operations on
256 /// this file system and may propagate down for nested file systems.
257 virtual std::error_code setCurrentWorkingDirectory(const Twine &Path) = 0;
258
259 /// Get the working directory of this file system.
260 virtual llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const = 0;
261
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100262 /// Gets real path of \p Path e.g. collapse all . and .. patterns, resolve
263 /// symlinks. For real file system, this uses `llvm::sys::fs::real_path`.
264 /// This returns errc::operation_not_permitted if not implemented by subclass.
265 virtual std::error_code getRealPath(const Twine &Path,
266 SmallVectorImpl<char> &Output) const;
267
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100268 /// Check whether a file exists. Provided for convenience.
269 bool exists(const Twine &Path);
270
271 /// Make \a Path an absolute path.
272 ///
273 /// Makes \a Path absolute using the current directory if it is not already.
274 /// An empty \a Path will result in the current directory.
275 ///
276 /// /absolute/path => /absolute/path
277 /// relative/../path => <current-directory>/relative/../path
278 ///
279 /// \param Path A path that is modified to be an absolute path.
280 /// \returns success if \a path has been made absolute, otherwise a
281 /// platform-specific error_code.
282 std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const;
283};
284
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100285/// Gets an \p vfs::FileSystem for the 'real' file system, as seen by
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100286/// the operating system.
287IntrusiveRefCntPtr<FileSystem> getRealFileSystem();
288
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100289/// A file system that allows overlaying one \p AbstractFileSystem on top
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100290/// of another.
291///
292/// Consists of a stack of >=1 \p FileSystem objects, which are treated as being
293/// one merged file system. When there is a directory that exists in more than
294/// one file system, the \p OverlayFileSystem contains a directory containing
295/// the union of their contents. The attributes (permissions, etc.) of the
296/// top-most (most recently added) directory are used. When there is a file
297/// that exists in more than one file system, the file in the top-most file
298/// system overrides the other(s).
299class OverlayFileSystem : public FileSystem {
300 using FileSystemList = SmallVector<IntrusiveRefCntPtr<FileSystem>, 1>;
301
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100302 /// The stack of file systems, implemented as a list in order of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100303 /// their addition.
304 FileSystemList FSList;
305
306public:
307 OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> Base);
308
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100309 /// Pushes a file system on top of the stack.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100310 void pushOverlay(IntrusiveRefCntPtr<FileSystem> FS);
311
312 llvm::ErrorOr<Status> status(const Twine &Path) override;
313 llvm::ErrorOr<std::unique_ptr<File>>
314 openFileForRead(const Twine &Path) override;
315 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
316 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
317 std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100318 std::error_code getRealPath(const Twine &Path,
319 SmallVectorImpl<char> &Output) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100320
321 using iterator = FileSystemList::reverse_iterator;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100322 using const_iterator = FileSystemList::const_reverse_iterator;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100323
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100324 /// Get an iterator pointing to the most recently added file system.
325 iterator overlays_begin() { return FSList.rbegin(); }
326 const_iterator overlays_begin() const { return FSList.rbegin(); }
327
328 /// Get an iterator pointing one-past the least recently added file
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100329 /// system.
330 iterator overlays_end() { return FSList.rend(); }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100331 const_iterator overlays_end() const { return FSList.rend(); }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100332};
333
Andrew Scull0372a572018-11-16 15:47:06 +0000334/// By default, this delegates all calls to the underlying file system. This
335/// is useful when derived file systems want to override some calls and still
336/// proxy other calls.
337class ProxyFileSystem : public FileSystem {
338public:
339 explicit ProxyFileSystem(IntrusiveRefCntPtr<FileSystem> FS)
340 : FS(std::move(FS)) {}
341
342 llvm::ErrorOr<Status> status(const Twine &Path) override {
343 return FS->status(Path);
344 }
345 llvm::ErrorOr<std::unique_ptr<File>>
346 openFileForRead(const Twine &Path) override {
347 return FS->openFileForRead(Path);
348 }
349 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override {
350 return FS->dir_begin(Dir, EC);
351 }
352 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
353 return FS->getCurrentWorkingDirectory();
354 }
355 std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
356 return FS->setCurrentWorkingDirectory(Path);
357 }
358 std::error_code getRealPath(const Twine &Path,
359 SmallVectorImpl<char> &Output) const override {
360 return FS->getRealPath(Path, Output);
361 }
362
363protected:
364 FileSystem &getUnderlyingFS() { return *FS; }
365
366private:
367 IntrusiveRefCntPtr<FileSystem> FS;
368};
369
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100370namespace detail {
371
372class InMemoryDirectory;
Andrew Scull0372a572018-11-16 15:47:06 +0000373class InMemoryFile;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100374
375} // namespace detail
376
377/// An in-memory file system.
378class InMemoryFileSystem : public FileSystem {
379 std::unique_ptr<detail::InMemoryDirectory> Root;
380 std::string WorkingDirectory;
381 bool UseNormalizedPaths = true;
382
Andrew Scull0372a572018-11-16 15:47:06 +0000383 /// If HardLinkTarget is non-null, a hardlink is created to the To path which
384 /// must be a file. If it is null then it adds the file as the public addFile.
385 bool addFile(const Twine &Path, time_t ModificationTime,
386 std::unique_ptr<llvm::MemoryBuffer> Buffer,
387 Optional<uint32_t> User, Optional<uint32_t> Group,
388 Optional<llvm::sys::fs::file_type> Type,
389 Optional<llvm::sys::fs::perms> Perms,
390 const detail::InMemoryFile *HardLinkTarget);
391
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100392public:
393 explicit InMemoryFileSystem(bool UseNormalizedPaths = true);
394 ~InMemoryFileSystem() override;
395
396 /// Add a file containing a buffer or a directory to the VFS with a
397 /// path. The VFS owns the buffer. If present, User, Group, Type
398 /// and Perms apply to the newly-created file or directory.
399 /// \return true if the file or directory was successfully added,
400 /// false if the file or directory already exists in the file system with
401 /// different contents.
402 bool addFile(const Twine &Path, time_t ModificationTime,
403 std::unique_ptr<llvm::MemoryBuffer> Buffer,
404 Optional<uint32_t> User = None, Optional<uint32_t> Group = None,
405 Optional<llvm::sys::fs::file_type> Type = None,
406 Optional<llvm::sys::fs::perms> Perms = None);
407
Andrew Scull0372a572018-11-16 15:47:06 +0000408 /// Add a hard link to a file.
409 /// Here hard links are not intended to be fully equivalent to the classical
410 /// filesystem. Both the hard link and the file share the same buffer and
411 /// status (and thus have the same UniqueID). Because of this there is no way
412 /// to distinguish between the link and the file after the link has been
413 /// added.
414 ///
415 /// The To path must be an existing file or a hardlink. The From file must not
416 /// have been added before. The To Path must not be a directory. The From Node
417 /// is added as a hard link which points to the resolved file of To Node.
418 /// \return true if the above condition is satisfied and hardlink was
419 /// successfully created, false otherwise.
420 bool addHardLink(const Twine &From, const Twine &To);
421
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100422 /// Add a buffer to the VFS with a path. The VFS does not own the buffer.
423 /// If present, User, Group, Type and Perms apply to the newly-created file
424 /// or directory.
425 /// \return true if the file or directory was successfully added,
426 /// false if the file or directory already exists in the file system with
427 /// different contents.
428 bool addFileNoOwn(const Twine &Path, time_t ModificationTime,
Andrew Scull0372a572018-11-16 15:47:06 +0000429 llvm::MemoryBuffer *Buffer, Optional<uint32_t> User = None,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100430 Optional<uint32_t> Group = None,
431 Optional<llvm::sys::fs::file_type> Type = None,
432 Optional<llvm::sys::fs::perms> Perms = None);
433
434 std::string toString() const;
435
436 /// Return true if this file system normalizes . and .. in paths.
437 bool useNormalizedPaths() const { return UseNormalizedPaths; }
438
439 llvm::ErrorOr<Status> status(const Twine &Path) override;
440 llvm::ErrorOr<std::unique_ptr<File>>
441 openFileForRead(const Twine &Path) override;
442 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
443
444 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
445 return WorkingDirectory;
446 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100447 /// Canonicalizes \p Path by combining with the current working
448 /// directory and normalizing the path (e.g. remove dots). If the current
449 /// working directory is not set, this returns errc::operation_not_permitted.
450 ///
451 /// This doesn't resolve symlinks as they are not supported in in-memory file
452 /// system.
453 std::error_code getRealPath(const Twine &Path,
454 SmallVectorImpl<char> &Output) const override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100455
456 std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
457};
458
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100459/// Get a globally unique ID for a virtual file or directory.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100460llvm::sys::fs::UniqueID getNextVirtualUniqueID();
461
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100462/// Gets a \p FileSystem for a virtual file system described in YAML
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100463/// format.
464IntrusiveRefCntPtr<FileSystem>
465getVFSFromYAML(std::unique_ptr<llvm::MemoryBuffer> Buffer,
466 llvm::SourceMgr::DiagHandlerTy DiagHandler,
Andrew Scull0372a572018-11-16 15:47:06 +0000467 StringRef YAMLFilePath, void *DiagContext = nullptr,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100468 IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
469
470struct YAMLVFSEntry {
Andrew Scull0372a572018-11-16 15:47:06 +0000471 template <typename T1, typename T2>
472 YAMLVFSEntry(T1 &&VPath, T2 &&RPath)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100473 : VPath(std::forward<T1>(VPath)), RPath(std::forward<T2>(RPath)) {}
474 std::string VPath;
475 std::string RPath;
476};
477
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100478/// Collect all pairs of <virtual path, real path> entries from the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100479/// \p YAMLFilePath. This is used by the module dependency collector to forward
480/// the entries into the reproducer output VFS YAML file.
481void collectVFSFromYAML(
482 std::unique_ptr<llvm::MemoryBuffer> Buffer,
483 llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
484 SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
485 void *DiagContext = nullptr,
486 IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
487
488class YAMLVFSWriter {
489 std::vector<YAMLVFSEntry> Mappings;
490 Optional<bool> IsCaseSensitive;
491 Optional<bool> IsOverlayRelative;
492 Optional<bool> UseExternalNames;
493 Optional<bool> IgnoreNonExistentContents;
494 std::string OverlayDir;
495
496public:
497 YAMLVFSWriter() = default;
498
499 void addFileMapping(StringRef VirtualPath, StringRef RealPath);
500
501 void setCaseSensitivity(bool CaseSensitive) {
502 IsCaseSensitive = CaseSensitive;
503 }
504
Andrew Scull0372a572018-11-16 15:47:06 +0000505 void setUseExternalNames(bool UseExtNames) { UseExternalNames = UseExtNames; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100506
507 void setIgnoreNonExistentContents(bool IgnoreContents) {
508 IgnoreNonExistentContents = IgnoreContents;
509 }
510
511 void setOverlayDir(StringRef OverlayDirectory) {
512 IsOverlayRelative = true;
513 OverlayDir.assign(OverlayDirectory.str());
514 }
515
516 void write(llvm::raw_ostream &OS);
517};
518
519} // namespace vfs
Andrew Scull0372a572018-11-16 15:47:06 +0000520} // namespace llvm
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100521
Andrew Scull0372a572018-11-16 15:47:06 +0000522#endif // LLVM_SUPPORT_VIRTUALFILESYSTEM_H