clang 23.0.0git
FileEntry.h
Go to the documentation of this file.
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
19#include "clang/Basic/LLVM.h"
20#include "llvm/ADT/DenseMapInfo.h"
21#include "llvm/ADT/Hashing.h"
22#include "llvm/ADT/PointerUnion.h"
23#include "llvm/ADT/StringMap.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/Support/ErrorOr.h"
26#include "llvm/Support/FileSystem/UniqueID.h"
27
28#include <optional>
29#include <utility>
30
31namespace llvm {
32
33class MemoryBuffer;
34
35namespace vfs {
36
37class File;
38
39} // namespace vfs
40} // namespace llvm
41
42namespace clang {
43
44class FileEntryRef;
45
46namespace optional_detail {
47
48/// Forward declare a template specialization for OptionalStorage.
49template <> class OptionalStorage<clang::FileEntryRef>;
50
51} // namespace optional_detail
52
53class FileEntry;
54
55/// A reference to a \c FileEntry that includes the name of the file as it was
56/// accessed by the FileManager's client.
58public:
59 /// The name of this FileEntry. If a VFS uses 'use-external-name', this is
60 /// the redirected name. See getRequestedName().
61 StringRef getName() const { return getBaseMapEntry().first(); }
62
63 /// The name of this FileEntry, as originally requested without applying any
64 /// remappings for VFS 'use-external-name'.
65 ///
66 /// FIXME: this should be the semantics of getName(). See comment in
67 /// FileManager::getFileRef().
68 StringRef getNameAsRequested() const { return ME->first(); }
69
70 const FileEntry &getFileEntry() const {
71 return *cast<FileEntry *>(getBaseMapEntry().second->V);
72 }
73
74 // This function is used if the buffer size needs to be increased
75 // due to potential z/OS EBCDIC -> UTF-8 conversion
76 inline void updateFileEntryBufferSize(unsigned BufferSize);
77
78 DirectoryEntryRef getDir() const { return ME->second->Dir; }
79
80 inline off_t getSize() const;
81 inline unsigned getUID() const;
82 inline const llvm::sys::fs::UniqueID &getUniqueID() const;
83 inline time_t getModificationTime() const;
84 inline bool isNamedPipe() const;
85 inline bool isDeviceFile() const;
86 inline void closeFile() const;
87
88 /// Check if the underlying FileEntry is the same, intentially ignoring
89 /// whether the file was referenced with the same spelling of the filename.
90 friend bool operator==(const FileEntryRef &LHS, const FileEntryRef &RHS) {
91 return &LHS.getFileEntry() == &RHS.getFileEntry();
92 }
93 friend bool operator==(const FileEntry *LHS, const FileEntryRef &RHS) {
94 return LHS == &RHS.getFileEntry();
95 }
96 friend bool operator==(const FileEntryRef &LHS, const FileEntry *RHS) {
97 return &LHS.getFileEntry() == RHS;
98 }
99 friend bool operator!=(const FileEntryRef &LHS, const FileEntryRef &RHS) {
100 return !(LHS == RHS);
101 }
102 friend bool operator!=(const FileEntry *LHS, const FileEntryRef &RHS) {
103 return !(LHS == RHS);
104 }
105 friend bool operator!=(const FileEntryRef &LHS, const FileEntry *RHS) {
106 return !(LHS == RHS);
107 }
108
109 /// Hash code is based on the FileEntry, not the specific named reference,
110 /// just like operator==.
111 friend llvm::hash_code hash_value(FileEntryRef Ref) {
112 return llvm::hash_value(&Ref.getFileEntry());
113 }
114
115 struct MapValue;
116
117 /// Type used in the StringMap.
118 using MapEntry = llvm::StringMapEntry<llvm::ErrorOr<MapValue>>;
119
120 /// Type stored in the StringMap.
121 struct MapValue {
122 /// The pointer at another MapEntry is used when the FileManager should
123 /// silently forward from one name to another, which occurs in Redirecting
124 /// VFSs that use external names. In that case, the \c FileEntryRef
125 /// returned by the \c FileManager will have the external name, and not the
126 /// name that was used to lookup the file.
127 llvm::PointerUnion<FileEntry *, const MapEntry *> V;
128
129 /// Directory the file was found in.
131
132 MapValue() = delete;
135 };
136
137 /// Check if RHS referenced the file in exactly the same way.
138 bool isSameRef(const FileEntryRef &RHS) const { return ME == RHS.ME; }
139
140 /// Allow FileEntryRef to degrade into 'const FileEntry*' to facilitate
141 /// incremental adoption.
142 ///
143 /// The goal is to avoid code churn due to dances like the following:
144 /// \code
145 /// // Old code.
146 /// lvalue = rvalue;
147 ///
148 /// // Temporary code from an incremental patch.
149 /// lvalue = &rvalue.getFileEntry();
150 ///
151 /// // Final code.
152 /// lvalue = rvalue;
153 /// \endcode
154 ///
155 /// FIXME: Once FileEntryRef is "everywhere" and FileEntry::LastRef and
156 /// FileEntry::getName have been deleted, delete this implicit conversion.
157 operator const FileEntry *() const { return &getFileEntry(); }
158
159 FileEntryRef() = delete;
160 explicit FileEntryRef(const MapEntry &ME) : ME(&ME) {
161 assert(ME.second && "Expected payload");
162 assert(ME.second->V && "Expected non-null");
163 }
164
165 /// Expose the underlying MapEntry to simplify packing in a PointerIntPair or
166 /// PointerUnion and allow construction in Optional.
167 const clang::FileEntryRef::MapEntry &getMapEntry() const { return *ME; }
168
169 /// Retrieve the base MapEntry after redirects.
170 const MapEntry &getBaseMapEntry() const {
171 const MapEntry *Base = ME;
172 while (const auto *Next = Base->second->V.dyn_cast<const MapEntry *>())
173 Base = Next;
174 return *Base;
175 }
176
177private:
179 struct optional_none_tag {};
180
181 // Private constructor for use by OptionalStorage.
182 FileEntryRef(optional_none_tag) : ME(nullptr) {}
183 bool hasOptionalValue() const { return ME; }
184
185 friend struct llvm::DenseMapInfo<FileEntryRef>;
186
187 const MapEntry *ME;
188};
189
190static_assert(sizeof(FileEntryRef) == sizeof(const FileEntry *),
191 "FileEntryRef must avoid size overhead");
192
193static_assert(std::is_trivially_copyable<FileEntryRef>::value,
194 "FileEntryRef must be trivially copyable");
195
197
198namespace optional_detail {
199
200/// Customize OptionalStorage<FileEntryRef> to use FileEntryRef and its
201/// optional_none_tag to keep it the size of a single pointer.
202template <>
204 : public clang::FileMgr::MapEntryOptionalStorage<clang::FileEntryRef> {
205 using StorageImpl =
207
208public:
209 using StorageImpl::StorageImpl;
210
213 return *this;
214 }
215};
216
217static_assert(sizeof(OptionalFileEntryRef) == sizeof(FileEntryRef),
218 "OptionalFileEntryRef must avoid size overhead");
219
220static_assert(std::is_trivially_copyable<OptionalFileEntryRef>::value,
221 "OptionalFileEntryRef should be trivially copyable");
222
223} // end namespace optional_detail
224} // namespace clang
225
226namespace llvm {
227
228/// Specialisation of DenseMapInfo for FileEntryRef.
229template <> struct DenseMapInfo<clang::FileEntryRef> {
230 static unsigned getHashValue(clang::FileEntryRef Val) {
231 return hash_value(Val);
232 }
233
235 if (LHS.isSameRef(RHS))
236 return true;
237 return LHS == RHS;
238 }
239
240 /// Support for finding `const FileEntry *` in a `DenseMap<FileEntryRef, T>`.
241 /// @{
242 static unsigned getHashValue(const clang::FileEntry *Val) {
243 return llvm::hash_value(Val);
244 }
245 static bool isEqual(const clang::FileEntry *LHS, clang::FileEntryRef RHS) {
246 return LHS == RHS;
247 }
248 /// @}
249};
250
251} // end namespace llvm
252
253namespace clang {
254
255inline bool operator==(const FileEntry *LHS, const OptionalFileEntryRef &RHS) {
256 return LHS == (RHS ? &RHS->getFileEntry() : nullptr);
257}
258inline bool operator==(const OptionalFileEntryRef &LHS, const FileEntry *RHS) {
259 return (LHS ? &LHS->getFileEntry() : nullptr) == RHS;
260}
261inline bool operator!=(const FileEntry *LHS, const OptionalFileEntryRef &RHS) {
262 return !(LHS == RHS);
263}
264inline bool operator!=(const OptionalFileEntryRef &LHS, const FileEntry *RHS) {
265 return !(LHS == RHS);
266}
267
268/// Cached information about one file (either on disk
269/// or in the virtual file system).
270///
271/// If the 'File' member is valid, then this FileEntry has an open file
272/// descriptor for the file.
273class FileEntry {
274 friend class FileManager;
276 FileEntry();
277 FileEntry(const FileEntry &) = delete;
278 FileEntry &operator=(const FileEntry &) = delete;
279
280 std::string RealPathName; // Real path to the file; could be empty.
281 off_t Size = 0; // File size in bytes.
282 time_t ModTime = 0; // Modification time of file.
283 const DirectoryEntry *Dir = nullptr; // Directory file lives in.
284 llvm::sys::fs::UniqueID UniqueID;
285 unsigned UID = 0; // A unique (small) ID for the file.
286 bool IsNamedPipe = false;
287 bool IsDeviceFile = false;
288
289 /// The open file, if it is owned by the \p FileEntry.
290 mutable std::unique_ptr<llvm::vfs::File> File;
291
292 /// The file content, if it is owned by the \p FileEntry.
293 std::unique_ptr<llvm::MemoryBuffer> Content;
294
295public:
297
298 StringRef tryGetRealPathName() const { return RealPathName; }
299 off_t getSize() const { return Size; }
300 // Size may increase due to potential z/OS EBCDIC -> UTF-8 conversion.
301 void setSize(off_t NewSize) { Size = NewSize; }
302 unsigned getUID() const { return UID; }
303 const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
304 time_t getModificationTime() const { return ModTime; }
305
306 /// Return the directory the file lives in.
307 const DirectoryEntry *getDir() const { return Dir; }
308
309 /// Check whether the file is a named pipe (and thus can't be opened by
310 /// the native FileManager methods).
311 bool isNamedPipe() const { return IsNamedPipe; }
312 bool isDeviceFile() const { return IsDeviceFile; }
313
314 void closeFile() const;
315};
316
317off_t FileEntryRef::getSize() const { return getFileEntry().getSize(); }
318
319unsigned FileEntryRef::getUID() const { return getFileEntry().getUID(); }
320
321const llvm::sys::fs::UniqueID &FileEntryRef::getUniqueID() const {
322 return getFileEntry().getUniqueID();
323}
324
328
331 return getFileEntry().isDeviceFile();
332}
333
335
337 cast<FileEntry *>(getBaseMapEntry().second->V)->setSize(BufferSize);
338}
339
340} // end namespace clang
341
342#endif // LLVM_CLANG_BASIC_FILEENTRY_H
Defines interfaces for clang::DirectoryEntry and clang::DirectoryEntryRef.
FormatToken * Next
The next token in the unwrapped line.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
A reference to a DirectoryEntry that includes the name of the directory as it was accessed by the Fil...
Cached information about one directory (either on disk or in the virtual file system).
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition FileEntry.h:57
friend bool operator==(const FileEntry *LHS, const FileEntryRef &RHS)
Definition FileEntry.h:93
bool isDeviceFile() const
Definition FileEntry.h:330
friend llvm::hash_code hash_value(FileEntryRef Ref)
Hash code is based on the FileEntry, not the specific named reference, just like operator==.
Definition FileEntry.h:111
const MapEntry & getBaseMapEntry() const
Retrieve the base MapEntry after redirects.
Definition FileEntry.h:170
const FileEntry & getFileEntry() const
Definition FileEntry.h:70
bool isSameRef(const FileEntryRef &RHS) const
Check if RHS referenced the file in exactly the same way.
Definition FileEntry.h:138
const clang::FileEntryRef::MapEntry & getMapEntry() const
Expose the underlying MapEntry to simplify packing in a PointerIntPair or PointerUnion and allow cons...
Definition FileEntry.h:167
void closeFile() const
Definition FileEntry.h:334
time_t getModificationTime() const
Definition FileEntry.h:325
friend bool operator!=(const FileEntryRef &LHS, const FileEntryRef &RHS)
Definition FileEntry.h:99
llvm::StringMapEntry< llvm::ErrorOr< MapValue > > MapEntry
Type used in the StringMap.
Definition FileEntry.h:118
bool isNamedPipe() const
Definition FileEntry.h:329
void updateFileEntryBufferSize(unsigned BufferSize)
Definition FileEntry.h:336
FileEntryRef(const MapEntry &ME)
Definition FileEntry.h:160
friend bool operator!=(const FileEntry *LHS, const FileEntryRef &RHS)
Definition FileEntry.h:102
off_t getSize() const
Definition FileEntry.h:317
StringRef getName() const
The name of this FileEntry.
Definition FileEntry.h:61
friend bool operator!=(const FileEntryRef &LHS, const FileEntry *RHS)
Definition FileEntry.h:105
const llvm::sys::fs::UniqueID & getUniqueID() const
Definition FileEntry.h:321
StringRef getNameAsRequested() const
The name of this FileEntry, as originally requested without applying any remappings for VFS 'use-exte...
Definition FileEntry.h:68
DirectoryEntryRef getDir() const
Definition FileEntry.h:78
friend bool operator==(const FileEntryRef &LHS, const FileEntryRef &RHS)
Check if the underlying FileEntry is the same, intentially ignoring whether the file was referenced w...
Definition FileEntry.h:90
unsigned getUID() const
Definition FileEntry.h:319
friend bool operator==(const FileEntryRef &LHS, const FileEntry *RHS)
Definition FileEntry.h:96
Cached information about one file (either on disk or in the virtual file system).
Definition FileEntry.h:273
bool isDeviceFile() const
Definition FileEntry.h:312
const DirectoryEntry * getDir() const
Return the directory the file lives in.
Definition FileEntry.h:307
StringRef tryGetRealPathName() const
Definition FileEntry.h:298
friend class FileEntryTestHelper
Definition FileEntry.h:275
void setSize(off_t NewSize)
Definition FileEntry.h:301
time_t getModificationTime() const
Definition FileEntry.h:304
unsigned getUID() const
Definition FileEntry.h:302
friend class FileManager
Definition FileEntry.h:274
bool isNamedPipe() const
Check whether the file is a named pipe (and thus can't be opened by the native FileManager methods).
Definition FileEntry.h:311
void closeFile() const
Definition FileEntry.cpp:23
off_t getSize() const
Definition FileEntry.h:299
const llvm::sys::fs::UniqueID & getUniqueID() const
Definition FileEntry.h:303
Customized storage for refs derived from map entires in FileManager, using the private optional_none_...
MapEntryOptionalStorage & operator=(clang::FileEntryRef Ref)
OptionalStorage & operator=(clang::FileEntryRef Ref)
Definition FileEntry.h:211
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:196
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:207
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
bool operator!=(CanQual< T > x, CanQual< U > y)
U cast(CodeGen::Address addr)
Definition Address.h:327
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
hash_code hash_value(const clang::dependencies::ModuleID &ID)
DirectoryEntryRef Dir
Directory the file was found in.
Definition FileEntry.h:130
MapValue(FileEntry &FE, DirectoryEntryRef Dir)
Definition FileEntry.h:133
MapValue(MapEntry &ME, DirectoryEntryRef Dir)
Definition FileEntry.h:134
llvm::PointerUnion< FileEntry *, const MapEntry * > V
The pointer at another MapEntry is used when the FileManager should silently forward from one name to...
Definition FileEntry.h:127
static unsigned getHashValue(const clang::FileEntry *Val)
Support for finding const FileEntry * in a DenseMap<FileEntryRef, T>.
Definition FileEntry.h:242
static unsigned getHashValue(clang::FileEntryRef Val)
Definition FileEntry.h:230
static bool isEqual(const clang::FileEntry *LHS, clang::FileEntryRef RHS)
Definition FileEntry.h:245
static bool isEqual(clang::FileEntryRef LHS, clang::FileEntryRef RHS)
Definition FileEntry.h:234