clang API Documentation

VirtualFileSystem.h
Go to the documentation of this file.
00001 //===- VirtualFileSystem.h - Virtual File System Layer ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 /// \file
00010 /// \brief Defines the virtual file system interface vfs::FileSystem.
00011 //===----------------------------------------------------------------------===//
00012 
00013 #ifndef LLVM_CLANG_BASIC_VIRTUALFILESYSTEM_H
00014 #define LLVM_CLANG_BASIC_VIRTUALFILESYSTEM_H
00015 
00016 #include "clang/Basic/LLVM.h"
00017 #include "llvm/ADT/IntrusiveRefCntPtr.h"
00018 #include "llvm/ADT/Optional.h"
00019 #include "llvm/Support/ErrorOr.h"
00020 #include "llvm/Support/FileSystem.h"
00021 #include "llvm/Support/raw_ostream.h"
00022 #include "llvm/Support/SourceMgr.h"
00023 
00024 namespace llvm {
00025 class MemoryBuffer;
00026 }
00027 
00028 namespace clang {
00029 namespace vfs {
00030 
00031 /// \brief The result of a \p status operation.
00032 class Status {
00033   std::string Name;
00034   llvm::sys::fs::UniqueID UID;
00035   llvm::sys::TimeValue MTime;
00036   uint32_t User;
00037   uint32_t Group;
00038   uint64_t Size;
00039   llvm::sys::fs::file_type Type;
00040   llvm::sys::fs::perms Perms;
00041 
00042 public:
00043   bool IsVFSMapped; // FIXME: remove when files support multiple names
00044 
00045 public:
00046   Status() : Type(llvm::sys::fs::file_type::status_error) {}
00047   Status(const llvm::sys::fs::file_status &Status);
00048   Status(StringRef Name, StringRef RealName, llvm::sys::fs::UniqueID UID,
00049          llvm::sys::TimeValue MTime, uint32_t User, uint32_t Group,
00050          uint64_t Size, llvm::sys::fs::file_type Type,
00051          llvm::sys::fs::perms Perms);
00052 
00053   /// \brief Returns the name that should be used for this file or directory.
00054   StringRef getName() const { return Name; }
00055   void setName(StringRef N) { Name = N; }
00056 
00057   /// @name Status interface from llvm::sys::fs
00058   /// @{
00059   llvm::sys::fs::file_type getType() const { return Type; }
00060   llvm::sys::fs::perms getPermissions() const { return Perms; }
00061   llvm::sys::TimeValue getLastModificationTime() const { return MTime; }
00062   llvm::sys::fs::UniqueID getUniqueID() const { return UID; }
00063   uint32_t getUser() const { return User; }
00064   uint32_t getGroup() const { return Group; }
00065   uint64_t getSize() const { return Size; }
00066   void setType(llvm::sys::fs::file_type v) { Type = v; }
00067   void setPermissions(llvm::sys::fs::perms p) { Perms = p; }
00068   /// @}
00069   /// @name Status queries
00070   /// These are static queries in llvm::sys::fs.
00071   /// @{
00072   bool equivalent(const Status &Other) const;
00073   bool isDirectory() const;
00074   bool isRegularFile() const;
00075   bool isOther() const;
00076   bool isSymlink() const;
00077   bool isStatusKnown() const;
00078   bool exists() const;
00079   /// @}
00080 };
00081 
00082 /// \brief Represents an open file.
00083 class File {
00084 public:
00085   /// \brief Destroy the file after closing it (if open).
00086   /// Sub-classes should generally call close() inside their destructors.  We
00087   /// cannot do that from the base class, since close is virtual.
00088   virtual ~File();
00089   /// \brief Get the status of the file.
00090   virtual llvm::ErrorOr<Status> status() = 0;
00091   /// \brief Get the contents of the file as a \p MemoryBuffer.
00092   virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
00093   getBuffer(const Twine &Name, int64_t FileSize = -1,
00094             bool RequiresNullTerminator = true, bool IsVolatile = false) = 0;
00095   /// \brief Closes the file.
00096   virtual std::error_code close() = 0;
00097   /// \brief Sets the name to use for this file.
00098   virtual void setName(StringRef Name) = 0;
00099 };
00100 
00101 namespace detail {
00102 /// \brief An interface for virtual file systems to provide an iterator over the
00103 /// (non-recursive) contents of a directory.
00104 struct DirIterImpl {
00105   virtual ~DirIterImpl();
00106   /// \brief Sets \c CurrentEntry to the next entry in the directory on success,
00107   /// or returns a system-defined \c error_code.
00108   virtual std::error_code increment() = 0;
00109   Status CurrentEntry;
00110 };
00111 } // end namespace detail
00112 
00113 /// \brief An input iterator over the entries in a virtual path, similar to
00114 /// llvm::sys::fs::directory_iterator.
00115 class directory_iterator {
00116   std::shared_ptr<detail::DirIterImpl> Impl; // Input iterator semantics on copy
00117 
00118 public:
00119   directory_iterator(std::shared_ptr<detail::DirIterImpl> I) : Impl(I) {
00120     assert(Impl.get() != nullptr && "requires non-null implementation");
00121     if (!Impl->CurrentEntry.isStatusKnown())
00122       Impl.reset(); // Normalize the end iterator to Impl == nullptr.
00123   }
00124 
00125   /// \brief Construct an 'end' iterator.
00126   directory_iterator() { }
00127 
00128   /// \brief Equivalent to operator++, with an error code.
00129   directory_iterator &increment(std::error_code &EC) {
00130     assert(Impl && "attempting to increment past end");
00131     EC = Impl->increment();
00132     if (EC || !Impl->CurrentEntry.isStatusKnown())
00133       Impl.reset(); // Normalize the end iterator to Impl == nullptr.
00134     return *this;
00135   }
00136 
00137   const Status &operator*() const { return Impl->CurrentEntry; }
00138   const Status *operator->() const { return &Impl->CurrentEntry; }
00139 
00140   bool operator==(const directory_iterator &RHS) const {
00141     if (Impl && RHS.Impl)
00142       return Impl->CurrentEntry.equivalent(RHS.Impl->CurrentEntry);
00143     return !Impl && !RHS.Impl;
00144   }
00145   bool operator!=(const directory_iterator &RHS) const {
00146     return !(*this == RHS);
00147   }
00148 };
00149 
00150 class FileSystem;
00151 
00152 /// \brief An input iterator over the recursive contents of a virtual path,
00153 /// similar to llvm::sys::fs::recursive_directory_iterator.
00154 class recursive_directory_iterator {
00155   typedef std::stack<directory_iterator, std::vector<directory_iterator>>
00156       IterState;
00157 
00158   FileSystem *FS;
00159   std::shared_ptr<IterState> State; // Input iterator semantics on copy.
00160 
00161 public:
00162   recursive_directory_iterator(FileSystem &FS, const Twine &Path,
00163                                std::error_code &EC);
00164   /// \brief Construct an 'end' iterator.
00165   recursive_directory_iterator() { }
00166 
00167   /// \brief Equivalent to operator++, with an error code.
00168   recursive_directory_iterator &increment(std::error_code &EC);
00169 
00170   const Status &operator*() const { return *State->top(); }
00171   const Status *operator->() const { return &*State->top(); }
00172 
00173   bool operator==(const recursive_directory_iterator &Other) const {
00174     return State == Other.State; // identity
00175   }
00176   bool operator!=(const recursive_directory_iterator &RHS) const {
00177     return !(*this == RHS);
00178   }
00179 };
00180 
00181 /// \brief The virtual file system interface.
00182 class FileSystem : public llvm::ThreadSafeRefCountedBase<FileSystem> {
00183 public:
00184   virtual ~FileSystem();
00185 
00186   /// \brief Get the status of the entry at \p Path, if one exists.
00187   virtual llvm::ErrorOr<Status> status(const Twine &Path) = 0;
00188   /// \brief Get a \p File object for the file at \p Path, if one exists.
00189   virtual llvm::ErrorOr<std::unique_ptr<File>>
00190   openFileForRead(const Twine &Path) = 0;
00191 
00192   /// This is a convenience method that opens a file, gets its content and then
00193   /// closes the file.
00194   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
00195   getBufferForFile(const Twine &Name, int64_t FileSize = -1,
00196                    bool RequiresNullTerminator = true, bool IsVolatile = false);
00197 
00198   /// \brief Get a directory_iterator for \p Dir.
00199   /// \note The 'end' iterator is directory_iterator().
00200   virtual directory_iterator dir_begin(const Twine &Dir,
00201                                        std::error_code &EC) = 0;
00202 };
00203 
00204 /// \brief Gets an \p vfs::FileSystem for the 'real' file system, as seen by
00205 /// the operating system.
00206 IntrusiveRefCntPtr<FileSystem> getRealFileSystem();
00207 
00208 /// \brief A file system that allows overlaying one \p AbstractFileSystem on top
00209 /// of another.
00210 ///
00211 /// Consists of a stack of >=1 \p FileSystem objects, which are treated as being
00212 /// one merged file system. When there is a directory that exists in more than
00213 /// one file system, the \p OverlayFileSystem contains a directory containing
00214 /// the union of their contents.  The attributes (permissions, etc.) of the
00215 /// top-most (most recently added) directory are used.  When there is a file
00216 /// that exists in more than one file system, the file in the top-most file
00217 /// system overrides the other(s).
00218 class OverlayFileSystem : public FileSystem {
00219   typedef SmallVector<IntrusiveRefCntPtr<FileSystem>, 1> FileSystemList;
00220   /// \brief The stack of file systems, implemented as a list in order of
00221   /// their addition.
00222   FileSystemList FSList;
00223 
00224 public:
00225   OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> Base);
00226   /// \brief Pushes a file system on top of the stack.
00227   void pushOverlay(IntrusiveRefCntPtr<FileSystem> FS);
00228 
00229   llvm::ErrorOr<Status> status(const Twine &Path) override;
00230   llvm::ErrorOr<std::unique_ptr<File>>
00231   openFileForRead(const Twine &Path) override;
00232   directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
00233 
00234   typedef FileSystemList::reverse_iterator iterator;
00235   
00236   /// \brief Get an iterator pointing to the most recently added file system.
00237   iterator overlays_begin() { return FSList.rbegin(); }
00238 
00239   /// \brief Get an iterator pointing one-past the least recently added file
00240   /// system.
00241   iterator overlays_end() { return FSList.rend(); }
00242 };
00243 
00244 /// \brief Get a globally unique ID for a virtual file or directory.
00245 llvm::sys::fs::UniqueID getNextVirtualUniqueID();
00246 
00247 /// \brief Gets a \p FileSystem for a virtual file system described in YAML
00248 /// format.
00249 IntrusiveRefCntPtr<FileSystem>
00250 getVFSFromYAML(std::unique_ptr<llvm::MemoryBuffer> Buffer,
00251                llvm::SourceMgr::DiagHandlerTy DiagHandler,
00252                void *DiagContext = nullptr,
00253                IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
00254 
00255 struct YAMLVFSEntry {
00256   template <typename T1, typename T2> YAMLVFSEntry(T1 &&VPath, T2 &&RPath)
00257       : VPath(std::forward<T1>(VPath)), RPath(std::forward<T2>(RPath)) {}
00258   std::string VPath;
00259   std::string RPath;
00260 };
00261 
00262 class YAMLVFSWriter {
00263   std::vector<YAMLVFSEntry> Mappings;
00264   Optional<bool> IsCaseSensitive;
00265 
00266 public:
00267   YAMLVFSWriter() {}
00268   void addFileMapping(StringRef VirtualPath, StringRef RealPath);
00269   void setCaseSensitivity(bool CaseSensitive) {
00270     IsCaseSensitive = CaseSensitive;
00271   }
00272   void write(llvm::raw_ostream &OS);
00273 };
00274 
00275 } // end namespace vfs
00276 } // end namespace clang
00277 #endif