15 #ifndef LLVM_CLANG_BASIC_VIRTUALFILESYSTEM_H 16 #define LLVM_CLANG_BASIC_VIRTUALFILESYSTEM_H 19 #include "llvm/ADT/IntrusiveRefCntPtr.h" 20 #include "llvm/ADT/None.h" 21 #include "llvm/ADT/Optional.h" 22 #include "llvm/ADT/SmallVector.h" 23 #include "llvm/ADT/StringRef.h" 24 #include "llvm/ADT/Twine.h" 25 #include "llvm/Support/Chrono.h" 26 #include "llvm/Support/ErrorOr.h" 27 #include "llvm/Support/FileSystem.h" 28 #include "llvm/Support/SourceMgr.h" 35 #include <system_error> 51 llvm::sys::fs::UniqueID UID;
52 llvm::sys::TimePoint<> MTime;
56 llvm::sys::fs::file_type
Type = llvm::sys::fs::file_type::status_error;
57 llvm::sys::fs::perms Perms;
61 bool IsVFSMapped =
false;
65 Status(StringRef Name, llvm::sys::fs::UniqueID UID,
66 llvm::sys::TimePoint<> MTime, uint32_t User, uint32_t Group,
67 uint64_t Size, llvm::sys::fs::file_type Type,
68 llvm::sys::fs::perms Perms);
71 static Status copyWithNewName(
const Status &In, StringRef NewName);
72 static Status copyWithNewName(
const llvm::sys::fs::file_status &In,
76 StringRef
getName()
const {
return Name; }
80 llvm::sys::fs::file_type
getType()
const {
return Type; }
91 bool equivalent(
const Status &Other)
const;
92 bool isDirectory()
const;
93 bool isRegularFile()
const;
95 bool isSymlink()
const;
96 bool isStatusKnown()
const;
110 virtual llvm::ErrorOr<Status> status() = 0;
113 virtual llvm::ErrorOr<std::string>
getName() {
114 if (
auto Status = status())
121 virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
122 getBuffer(
const Twine &Name, int64_t FileSize = -1,
123 bool RequiresNullTerminator =
true,
bool IsVolatile =
false) = 0;
126 virtual std::error_code close() = 0;
138 virtual std::error_code increment() = 0;
148 std::shared_ptr<detail::DirIterImpl> Impl;
152 : Impl(
std::move(I)) {
153 assert(Impl.get() !=
nullptr &&
"requires non-null implementation");
154 if (!Impl->CurrentEntry.isStatusKnown())
163 assert(Impl &&
"attempting to increment past end");
164 EC = Impl->increment();
165 if (!Impl->CurrentEntry.isStatusKnown())
174 if (Impl && RHS.Impl)
175 return Impl->CurrentEntry.equivalent(RHS.Impl->CurrentEntry);
176 return !Impl && !RHS.Impl;
179 return !(*
this == RHS);
189 std::stack<directory_iterator, std::vector<directory_iterator>>;
192 std::shared_ptr<IterState>
State;
196 std::error_code &EC);
208 return State == Other.State;
211 return !(*
this == RHS);
216 assert(!State->empty() &&
"Cannot get level without any iteration state");
217 return State->size()-1;
222 class FileSystem :
public llvm::ThreadSafeRefCountedBase<FileSystem> {
227 virtual llvm::ErrorOr<Status> status(
const Twine &Path) = 0;
230 virtual llvm::ErrorOr<std::unique_ptr<File>>
231 openFileForRead(
const Twine &Path) = 0;
235 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
236 getBufferForFile(
const Twine &Name, int64_t FileSize = -1,
237 bool RequiresNullTerminator =
true,
bool IsVolatile =
false);
242 std::error_code &EC) = 0;
246 virtual std::error_code setCurrentWorkingDirectory(
const Twine &Path) = 0;
249 virtual llvm::ErrorOr<std::string> getCurrentWorkingDirectory()
const = 0;
254 virtual std::error_code getRealPath(
const Twine &Path,
258 bool exists(
const Twine &Path);
301 llvm::ErrorOr<Status> status(
const Twine &Path)
override;
302 llvm::ErrorOr<std::unique_ptr<File>>
303 openFileForRead(
const Twine &Path)
override;
305 llvm::ErrorOr<std::string> getCurrentWorkingDirectory()
const override;
306 std::error_code setCurrentWorkingDirectory(
const Twine &Path)
override;
307 std::error_code getRealPath(
const Twine &Path,
325 class InMemoryDirectory;
331 std::unique_ptr<detail::InMemoryDirectory> Root;
332 std::string WorkingDirectory;
333 bool UseNormalizedPaths =
true;
345 bool addFile(
const Twine &Path, time_t ModificationTime,
346 std::unique_ptr<llvm::MemoryBuffer> Buffer,
357 bool addFileNoOwn(
const Twine &Path, time_t ModificationTime,
358 llvm::MemoryBuffer *Buffer,
369 llvm::ErrorOr<Status> status(
const Twine &Path)
override;
370 llvm::ErrorOr<std::unique_ptr<File>>
371 openFileForRead(
const Twine &Path)
override;
375 return WorkingDirectory;
383 std::error_code getRealPath(
const Twine &Path,
386 std::error_code setCurrentWorkingDirectory(
const Twine &Path)
override;
396 llvm::SourceMgr::DiagHandlerTy DiagHandler,
397 StringRef YAMLFilePath,
398 void *DiagContext =
nullptr,
402 template <
typename T1,
typename T2>
YAMLVFSEntry(T1 &&VPath, T2 &&RPath)
403 : VPath(
std::forward<T1>(VPath)), RPath(
std::forward<T2>(RPath)) {}
412 std::unique_ptr<llvm::MemoryBuffer> Buffer,
413 llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
415 void *DiagContext =
nullptr,
419 std::vector<YAMLVFSEntry> Mappings;
424 std::string OverlayDir;
429 void addFileMapping(StringRef VirtualPath, StringRef RealPath);
432 IsCaseSensitive = CaseSensitive;
436 UseExternalNames = UseExtNames;
440 IgnoreNonExistentContents = IgnoreContents;
444 IsOverlayRelative =
true;
445 OverlayDir.assign(OverlayDirectory.str());
448 void write(llvm::raw_ostream &OS);
454 #endif // LLVM_CLANG_BASIC_VIRTUALFILESYSTEM_H DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
IntrusiveRefCntPtr< FileSystem > getRealFileSystem()
Gets an vfs::FileSystem for the 'real' file system, as seen by the operating system.
void setOverlayDir(StringRef OverlayDirectory)
llvm::sys::fs::perms getPermissions() const
The base class of the type hierarchy.
StringRef getName() const
Returns the name that should be used for this file or directory.
void setIgnoreNonExistentContents(bool IgnoreContents)
bool operator==(const recursive_directory_iterator &Other) const
The virtual file system interface.
llvm::ErrorOr< std::string > getCurrentWorkingDirectory() const override
Get the working directory of this file system.
IntrusiveRefCntPtr< FileSystem > getVFSFromYAML(std::unique_ptr< llvm::MemoryBuffer > Buffer, llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath, void *DiagContext=nullptr, IntrusiveRefCntPtr< FileSystem > ExternalFS=getRealFileSystem())
Gets a FileSystem for a virtual file system described in YAML format.
An input iterator over the recursive contents of a virtual path, similar to llvm::sys::fs::recursive_...
directory_iterator(std::shared_ptr< detail::DirIterImpl > I)
An in-memory file system.
bool useNormalizedPaths() const
Return true if this file system normalizes . and .. in paths.
A file system that allows overlaying one AbstractFileSystem on top of another.
directory_iterator & increment(std::error_code &EC)
Equivalent to operator++, with an error code.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
int level() const
Gets the current level. Starting path is at level 0.
const Status & operator*() const
The result of a status operation.
const Status * operator->() const
iterator overlays_end()
Get an iterator pointing one-past the least recently added file system.
void setCaseSensitivity(bool CaseSensitive)
virtual llvm::ErrorOr< std::string > getName()
Get the name of the file.
FileSystemList::reverse_iterator iterator
llvm::sys::fs::file_type getType() const
void collectVFSFromYAML(std::unique_ptr< llvm::MemoryBuffer > Buffer, llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath, SmallVectorImpl< YAMLVFSEntry > &CollectedEntries, void *DiagContext=nullptr, IntrusiveRefCntPtr< FileSystem > ExternalFS=getRealFileSystem())
Collect all pairs of <virtual path, real path> entries from the YAMLFilePath.
iterator overlays_begin()
Get an iterator pointing to the most recently added file system.
llvm::sys::TimePoint getLastModificationTime() const
YAMLVFSEntry(T1 &&VPath, T2 &&RPath)
Dataflow Directional Tag Classes.
llvm::sys::fs::UniqueID getNextVirtualUniqueID()
Get a globally unique ID for a virtual file or directory.
bool operator==(const directory_iterator &RHS) const
FileSystemList::const_reverse_iterator const_iterator
std::string toString(const til::SExpr *E)
void setUseExternalNames(bool UseExtNames)
const_iterator overlays_end() const
llvm::sys::fs::UniqueID getUniqueID() const
const Status & operator*() const
An input iterator over the entries in a virtual path, similar to llvm::sys::fs::directory_iterator.
const_iterator overlays_begin() const
const Status * operator->() const
An interface for virtual file systems to provide an iterator over the (non-recursive) contents of a d...
bool operator!=(const directory_iterator &RHS) const
uint32_t getGroup() const
bool operator!=(const recursive_directory_iterator &RHS) const