|
| 1 | +//===- clang/Basic/FileEntry.h - File references ----------------*- C++ -*-===// |
| 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 | +/// \file |
| 10 | +/// Defines interfaces for clang::FileEntry and clang::FileEntryRef. |
| 11 | +/// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#ifndef LLVM_CLANG_BASIC_FILEENTRY_H |
| 15 | +#define LLVM_CLANG_BASIC_FILEENTRY_H |
| 16 | + |
| 17 | +#include "clang/Basic/LLVM.h" |
| 18 | +#include "llvm/ADT/PointerUnion.h" |
| 19 | +#include "llvm/ADT/StringMap.h" |
| 20 | +#include "llvm/ADT/StringRef.h" |
| 21 | +#include "llvm/Support/ErrorOr.h" |
| 22 | +#include "llvm/Support/FileSystem/UniqueID.h" |
| 23 | + |
| 24 | +namespace llvm { |
| 25 | +namespace vfs { |
| 26 | + |
| 27 | +class File; |
| 28 | + |
| 29 | +} // namespace vfs |
| 30 | +} // namespace llvm |
| 31 | + |
| 32 | +namespace clang { |
| 33 | + |
| 34 | +class DirectoryEntry; |
| 35 | +class FileEntry; |
| 36 | + |
| 37 | +/// A reference to a \c FileEntry that includes the name of the file as it was |
| 38 | +/// accessed by the FileManager's client. |
| 39 | +class FileEntryRef { |
| 40 | +public: |
| 41 | + const StringRef getName() const { return Entry->first(); } |
| 42 | + const FileEntry &getFileEntry() const { |
| 43 | + return *Entry->second->V.get<FileEntry *>(); |
| 44 | + } |
| 45 | + |
| 46 | + inline bool isValid() const; |
| 47 | + inline off_t getSize() const; |
| 48 | + inline unsigned getUID() const; |
| 49 | + inline const llvm::sys::fs::UniqueID &getUniqueID() const; |
| 50 | + inline time_t getModificationTime() const; |
| 51 | + |
| 52 | + friend bool operator==(const FileEntryRef &LHS, const FileEntryRef &RHS) { |
| 53 | + return LHS.Entry == RHS.Entry; |
| 54 | + } |
| 55 | + friend bool operator!=(const FileEntryRef &LHS, const FileEntryRef &RHS) { |
| 56 | + return !(LHS == RHS); |
| 57 | + } |
| 58 | + |
| 59 | + struct MapValue; |
| 60 | + |
| 61 | + /// Type used in the StringMap. |
| 62 | + using MapEntry = llvm::StringMapEntry<llvm::ErrorOr<MapValue>>; |
| 63 | + |
| 64 | + /// Type stored in the StringMap. |
| 65 | + struct MapValue { |
| 66 | + /// The pointer at another MapEntry is used when the FileManager should |
| 67 | + /// silently forward from one name to another, which occurs in Redirecting |
| 68 | + /// VFSs that use external names. In that case, the \c FileEntryRef |
| 69 | + /// returned by the \c FileManager will have the external name, and not the |
| 70 | + /// name that was used to lookup the file. |
| 71 | + /// |
| 72 | + /// The second type is really a `const MapEntry *`, but that confuses |
| 73 | + /// gcc5.3. Once that's no longer supported, change this back. |
| 74 | + llvm::PointerUnion<FileEntry *, const void *> V; |
| 75 | + |
| 76 | + MapValue() = delete; |
| 77 | + MapValue(FileEntry &FE) : V(&FE) {} |
| 78 | + MapValue(MapEntry &ME) : V(&ME) {} |
| 79 | + }; |
| 80 | + |
| 81 | +private: |
| 82 | + friend class FileManager; |
| 83 | + |
| 84 | + FileEntryRef() = delete; |
| 85 | + explicit FileEntryRef(const MapEntry &Entry) |
| 86 | + : Entry(&Entry) { |
| 87 | + assert(Entry.second && "Expected payload"); |
| 88 | + assert(Entry.second->V && "Expected non-null"); |
| 89 | + assert(Entry.second->V.is<FileEntry *>() && "Expected FileEntry"); |
| 90 | + } |
| 91 | + |
| 92 | + const MapEntry *Entry; |
| 93 | +}; |
| 94 | + |
| 95 | +/// Cached information about one file (either on disk |
| 96 | +/// or in the virtual file system). |
| 97 | +/// |
| 98 | +/// If the 'File' member is valid, then this FileEntry has an open file |
| 99 | +/// descriptor for the file. |
| 100 | +class FileEntry { |
| 101 | + friend class FileManager; |
| 102 | + |
| 103 | + std::string RealPathName; // Real path to the file; could be empty. |
| 104 | + off_t Size; // File size in bytes. |
| 105 | + time_t ModTime; // Modification time of file. |
| 106 | + const DirectoryEntry *Dir; // Directory file lives in. |
| 107 | + llvm::sys::fs::UniqueID UniqueID; |
| 108 | + unsigned UID; // A unique (small) ID for the file. |
| 109 | + bool IsNamedPipe; |
| 110 | + bool IsValid; // Is this \c FileEntry initialized and valid? |
| 111 | + |
| 112 | + /// The open file, if it is owned by the \p FileEntry. |
| 113 | + mutable std::unique_ptr<llvm::vfs::File> File; |
| 114 | + |
| 115 | + // First access name for this FileEntry. |
| 116 | + // |
| 117 | + // This is Optional only to allow delayed construction (FileEntryRef has no |
| 118 | + // default constructor). It should always have a value in practice. |
| 119 | + // |
| 120 | + // TODO: remove this once everyone that needs a name uses FileEntryRef. |
| 121 | + Optional<FileEntryRef> LastRef; |
| 122 | + |
| 123 | +public: |
| 124 | + FileEntry(); |
| 125 | + ~FileEntry(); |
| 126 | + |
| 127 | + FileEntry(const FileEntry &) = delete; |
| 128 | + FileEntry &operator=(const FileEntry &) = delete; |
| 129 | + |
| 130 | + StringRef getName() const { return LastRef->getName(); } |
| 131 | + FileEntryRef getLastRef() const { return *LastRef; } |
| 132 | + |
| 133 | + StringRef tryGetRealPathName() const { return RealPathName; } |
| 134 | + bool isValid() const { return IsValid; } |
| 135 | + off_t getSize() const { return Size; } |
| 136 | + unsigned getUID() const { return UID; } |
| 137 | + const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; } |
| 138 | + time_t getModificationTime() const { return ModTime; } |
| 139 | + |
| 140 | + /// Return the directory the file lives in. |
| 141 | + const DirectoryEntry *getDir() const { return Dir; } |
| 142 | + |
| 143 | + bool operator<(const FileEntry &RHS) const { return UniqueID < RHS.UniqueID; } |
| 144 | + |
| 145 | + /// Check whether the file is a named pipe (and thus can't be opened by |
| 146 | + /// the native FileManager methods). |
| 147 | + bool isNamedPipe() const { return IsNamedPipe; } |
| 148 | + |
| 149 | + void closeFile() const; |
| 150 | + |
| 151 | + // Only for use in tests to see if deferred opens are happening, rather than |
| 152 | + // relying on RealPathName being empty. |
| 153 | + bool isOpenForTests() const { return File != nullptr; } |
| 154 | +}; |
| 155 | + |
| 156 | +bool FileEntryRef::isValid() const { return getFileEntry().isValid(); } |
| 157 | + |
| 158 | +off_t FileEntryRef::getSize() const { return getFileEntry().getSize(); } |
| 159 | + |
| 160 | +unsigned FileEntryRef::getUID() const { return getFileEntry().getUID(); } |
| 161 | + |
| 162 | +const llvm::sys::fs::UniqueID &FileEntryRef::getUniqueID() const { |
| 163 | + return getFileEntry().getUniqueID(); |
| 164 | +} |
| 165 | + |
| 166 | +time_t FileEntryRef::getModificationTime() const { |
| 167 | + return getFileEntry().getModificationTime(); |
| 168 | +} |
| 169 | + |
| 170 | +} // end namespace clang |
| 171 | + |
| 172 | +#endif // LLVM_CLANG_BASIC_FILEENTRY_H |
0 commit comments