13 #ifndef LLVM_CLANG_BASIC_VIRTUALFILESYSTEM_H 14 #define LLVM_CLANG_BASIC_VIRTUALFILESYSTEM_H 17 #include "llvm/ADT/IntrusiveRefCntPtr.h" 18 #include "llvm/ADT/Optional.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/ADT/Twine.h" 22 #include "llvm/Support/Chrono.h" 23 #include "llvm/Support/ErrorOr.h" 24 #include "llvm/Support/FileSystem.h" 25 #include "llvm/Support/SourceMgr.h" 26 #include "llvm/Support/raw_ostream.h" 34 #include <system_error> 50 llvm::sys::fs::UniqueID UID;
51 llvm::sys::TimePoint<> MTime;
55 llvm::sys::fs::file_type
Type;
56 llvm::sys::fs::perms Perms;
62 Status() : Type(
llvm::sys::fs::file_type::status_error) {}
64 Status(StringRef Name, llvm::sys::fs::UniqueID UID,
65 llvm::sys::TimePoint<> MTime, uint32_t User, uint32_t Group,
66 uint64_t Size, llvm::sys::fs::file_type Type,
67 llvm::sys::fs::perms Perms);
70 static Status copyWithNewName(
const Status &In, StringRef NewName);
71 static Status copyWithNewName(
const llvm::sys::fs::file_status &In,
75 StringRef
getName()
const {
return Name; }
79 llvm::sys::fs::file_type
getType()
const {
return Type; }
90 bool equivalent(
const Status &Other)
const;
91 bool isDirectory()
const;
92 bool isRegularFile()
const;
94 bool isSymlink()
const;
95 bool isStatusKnown()
const;
109 virtual llvm::ErrorOr<Status> status() = 0;
112 virtual llvm::ErrorOr<std::string>
getName() {
113 if (
auto Status = status())
120 virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
121 getBuffer(
const Twine &Name, int64_t FileSize = -1,
122 bool RequiresNullTerminator =
true,
bool IsVolatile =
false) = 0;
125 virtual std::error_code close() = 0;
137 virtual std::error_code increment() = 0;
147 std::shared_ptr<detail::DirIterImpl> Impl;
151 : Impl(
std::move(I)) {
152 assert(Impl.get() !=
nullptr &&
"requires non-null implementation");
153 if (!Impl->CurrentEntry.isStatusKnown())
162 assert(Impl &&
"attempting to increment past end");
163 EC = Impl->increment();
164 if (!Impl->CurrentEntry.isStatusKnown())
173 if (Impl && RHS.Impl)
174 return Impl->CurrentEntry.equivalent(RHS.Impl->CurrentEntry);
175 return !Impl && !RHS.Impl;
178 return !(*
this == RHS);
187 typedef std::stack<directory_iterator, std::vector<directory_iterator>>
191 std::shared_ptr<IterState>
State;
195 std::error_code &EC);
206 return State == Other.State;
209 return !(*
this == RHS);
214 assert(State->size() &&
"Cannot get level without any iteration state");
215 return State->size()-1;
220 class FileSystem :
public llvm::ThreadSafeRefCountedBase<FileSystem> {
225 virtual llvm::ErrorOr<Status> status(
const Twine &Path) = 0;
227 virtual llvm::ErrorOr<std::unique_ptr<File>>
228 openFileForRead(
const Twine &Path) = 0;
232 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
233 getBufferForFile(
const Twine &Name, int64_t FileSize = -1,
234 bool RequiresNullTerminator =
true,
bool IsVolatile =
false);
239 std::error_code &EC) = 0;
243 virtual std::error_code setCurrentWorkingDirectory(
const Twine &Path) = 0;
245 virtual llvm::ErrorOr<std::string> getCurrentWorkingDirectory()
const = 0;
248 bool exists(
const Twine &Path);
282 FileSystemList FSList;
289 llvm::ErrorOr<Status> status(
const Twine &Path)
override;
290 llvm::ErrorOr<std::unique_ptr<File>>
291 openFileForRead(
const Twine &Path)
override;
293 llvm::ErrorOr<std::string> getCurrentWorkingDirectory()
const override;
294 std::error_code setCurrentWorkingDirectory(
const Twine &Path)
override;
308 class InMemoryDirectory;
314 std::unique_ptr<detail::InMemoryDirectory> Root;
315 std::string WorkingDirectory;
316 bool UseNormalizedPaths =
true;
328 bool addFile(
const Twine &Path, time_t ModificationTime,
329 std::unique_ptr<llvm::MemoryBuffer> Buffer,
339 bool addFileNoOwn(
const Twine &Path, time_t ModificationTime,
340 llvm::MemoryBuffer *Buffer,
350 llvm::ErrorOr<Status> status(
const Twine &Path)
override;
351 llvm::ErrorOr<std::unique_ptr<File>>
352 openFileForRead(
const Twine &Path)
override;
355 return WorkingDirectory;
357 std::error_code setCurrentWorkingDirectory(
const Twine &Path)
override;
367 llvm::SourceMgr::DiagHandlerTy DiagHandler,
368 StringRef YAMLFilePath,
369 void *DiagContext =
nullptr,
373 template <
typename T1,
typename T2>
YAMLVFSEntry(T1 &&VPath, T2 &&RPath)
374 : VPath(
std::forward<T1>(VPath)), RPath(
std::forward<T2>(RPath)) {}
383 std::unique_ptr<llvm::MemoryBuffer> Buffer,
384 llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
386 void *DiagContext =
nullptr,
390 std::vector<YAMLVFSEntry> Mappings;
395 std::string OverlayDir;
400 void addFileMapping(StringRef VirtualPath, StringRef RealPath);
403 IsCaseSensitive = CaseSensitive;
407 UseExternalNames = UseExtNames;
411 IgnoreNonExistentContents = IgnoreContents;
415 IsOverlayRelative =
true;
416 OverlayDir.assign(OverlayDirectory.str());
419 void write(llvm::raw_ostream &OS);
425 #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.
FileSystemList::reverse_iterator iterator
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.
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
std::string toString(const til::SExpr *E)
void setUseExternalNames(bool UseExtNames)
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 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