clang 18.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 *getBaseMapEntry().second->V.get<FileEntry *>();
72 }
73 DirectoryEntryRef getDir() const { return ME->second->Dir; }
74
75 inline off_t getSize() const;
76 inline unsigned getUID() const;
77 inline const llvm::sys::fs::UniqueID &getUniqueID() const;
78 inline time_t getModificationTime() const;
79 inline bool isNamedPipe() const;
80 inline void closeFile() const;
81
82 /// Check if the underlying FileEntry is the same, intentially ignoring
83 /// whether the file was referenced with the same spelling of the filename.
84 friend bool operator==(const FileEntryRef &LHS, const FileEntryRef &RHS) {
85 return &LHS.getFileEntry() == &RHS.getFileEntry();
86 }
87 friend bool operator==(const FileEntry *LHS, const FileEntryRef &RHS) {
88 return LHS == &RHS.getFileEntry();
89 }
90 friend bool operator==(const FileEntryRef &LHS, const FileEntry *RHS) {
91 return &LHS.getFileEntry() == RHS;
92 }
93 friend bool operator!=(const FileEntryRef &LHS, const FileEntryRef &RHS) {
94 return !(LHS == RHS);
95 }
96 friend bool operator!=(const FileEntry *LHS, const FileEntryRef &RHS) {
97 return !(LHS == RHS);
98 }
99 friend bool operator!=(const FileEntryRef &LHS, const FileEntry *RHS) {
100 return !(LHS == RHS);
101 }
102
103 /// Hash code is based on the FileEntry, not the specific named reference,
104 /// just like operator==.
105 friend llvm::hash_code hash_value(FileEntryRef Ref) {
106 return llvm::hash_value(&Ref.getFileEntry());
107 }
108
109 struct MapValue;
110
111 /// Type used in the StringMap.
112 using MapEntry = llvm::StringMapEntry<llvm::ErrorOr<MapValue>>;
113
114 /// Type stored in the StringMap.
115 struct MapValue {
116 /// The pointer at another MapEntry is used when the FileManager should
117 /// silently forward from one name to another, which occurs in Redirecting
118 /// VFSs that use external names. In that case, the \c FileEntryRef
119 /// returned by the \c FileManager will have the external name, and not the
120 /// name that was used to lookup the file.
121 llvm::PointerUnion<FileEntry *, const MapEntry *> V;
122
123 /// Directory the file was found in.
125
126 MapValue() = delete;
127 MapValue(FileEntry &FE, DirectoryEntryRef Dir) : V(&FE), Dir(Dir) {}
128 MapValue(MapEntry &ME, DirectoryEntryRef Dir) : V(&ME), Dir(Dir) {}
129 };
130
131 /// Check if RHS referenced the file in exactly the same way.
132 bool isSameRef(const FileEntryRef &RHS) const { return ME == RHS.ME; }
133
134 /// Allow FileEntryRef to degrade into 'const FileEntry*' to facilitate
135 /// incremental adoption.
136 ///
137 /// The goal is to avoid code churn due to dances like the following:
138 /// \code
139 /// // Old code.
140 /// lvalue = rvalue;
141 ///
142 /// // Temporary code from an incremental patch.
143 /// lvalue = &rvalue.getFileEntry();
144 ///
145 /// // Final code.
146 /// lvalue = rvalue;
147 /// \endcode
148 ///
149 /// FIXME: Once FileEntryRef is "everywhere" and FileEntry::LastRef and
150 /// FileEntry::getName have been deleted, delete this implicit conversion.
151 operator const FileEntry *() const { return &getFileEntry(); }
152
153 FileEntryRef() = delete;
154 explicit FileEntryRef(const MapEntry &ME) : ME(&ME) {
155 assert(ME.second && "Expected payload");
156 assert(ME.second->V && "Expected non-null");
157 }
158
159 /// Expose the underlying MapEntry to simplify packing in a PointerIntPair or
160 /// PointerUnion and allow construction in Optional.
161 const clang::FileEntryRef::MapEntry &getMapEntry() const { return *ME; }
162
163 /// Retrieve the base MapEntry after redirects.
164 const MapEntry &getBaseMapEntry() const {
165 const MapEntry *Base = ME;
166 while (const auto *Next = Base->second->V.dyn_cast<const MapEntry *>())
167 Base = Next;
168 return *Base;
169 }
170
171private:
173 struct optional_none_tag {};
174
175 // Private constructor for use by OptionalStorage.
176 FileEntryRef(optional_none_tag) : ME(nullptr) {}
177 bool hasOptionalValue() const { return ME; }
178
179 friend struct llvm::DenseMapInfo<FileEntryRef>;
180 struct dense_map_empty_tag {};
181 struct dense_map_tombstone_tag {};
182
183 // Private constructors for use by DenseMapInfo.
184 FileEntryRef(dense_map_empty_tag)
185 : ME(llvm::DenseMapInfo<const MapEntry *>::getEmptyKey()) {}
186 FileEntryRef(dense_map_tombstone_tag)
187 : ME(llvm::DenseMapInfo<const MapEntry *>::getTombstoneKey()) {}
188 bool isSpecialDenseMapKey() const {
189 return isSameRef(FileEntryRef(dense_map_empty_tag())) ||
190 isSameRef(FileEntryRef(dense_map_tombstone_tag()));
191 }
192
193 const MapEntry *ME;
194};
195
196static_assert(sizeof(FileEntryRef) == sizeof(const FileEntry *),
197 "FileEntryRef must avoid size overhead");
198
199static_assert(std::is_trivially_copyable<FileEntryRef>::value,
200 "FileEntryRef must be trivially copyable");
201
203
204namespace optional_detail {
205
206/// Customize OptionalStorage<FileEntryRef> to use FileEntryRef and its
207/// optional_none_tag to keep it the size of a single pointer.
208template <>
210 : public clang::FileMgr::MapEntryOptionalStorage<clang::FileEntryRef> {
211 using StorageImpl =
213
214public:
215 OptionalStorage() = default;
216
217 template <class... ArgTypes>
218 explicit OptionalStorage(std::in_place_t, ArgTypes &&...Args)
219 : StorageImpl(std::in_place_t{}, std::forward<ArgTypes>(Args)...) {}
220
222 StorageImpl::operator=(Ref);
223 return *this;
224 }
225};
226
227static_assert(sizeof(OptionalFileEntryRef) == sizeof(FileEntryRef),
228 "OptionalFileEntryRef must avoid size overhead");
229
230static_assert(std::is_trivially_copyable<OptionalFileEntryRef>::value,
231 "OptionalFileEntryRef should be trivially copyable");
232
233} // end namespace optional_detail
234} // namespace clang
235
236namespace llvm {
237
238template <> struct PointerLikeTypeTraits<clang::FileEntryRef> {
239 static inline void *getAsVoidPointer(clang::FileEntryRef File) {
240 return const_cast<clang::FileEntryRef::MapEntry *>(&File.getMapEntry());
241 }
242
243 static inline clang::FileEntryRef getFromVoidPointer(void *Ptr) {
244 return clang::FileEntryRef(
245 *reinterpret_cast<const clang::FileEntryRef::MapEntry *>(Ptr));
246 }
247
248 static constexpr int NumLowBitsAvailable = PointerLikeTypeTraits<
249 const clang::FileEntryRef::MapEntry *>::NumLowBitsAvailable;
250};
251
252template <> struct PointerLikeTypeTraits<clang::OptionalFileEntryRef> {
254 if (!File)
255 return nullptr;
257 }
258
260 if (!Ptr)
261 return std::nullopt;
263 }
264
265 static constexpr int NumLowBitsAvailable =
267};
268
269/// Specialisation of DenseMapInfo for FileEntryRef.
270template <> struct DenseMapInfo<clang::FileEntryRef> {
272 return clang::FileEntryRef(clang::FileEntryRef::dense_map_empty_tag());
273 }
274
276 return clang::FileEntryRef(clang::FileEntryRef::dense_map_tombstone_tag());
277 }
278
279 static unsigned getHashValue(clang::FileEntryRef Val) {
280 return hash_value(Val);
281 }
282
284 // Catch the easy cases: both empty, both tombstone, or the same ref.
285 if (LHS.isSameRef(RHS))
286 return true;
287
288 // Confirm LHS and RHS are valid.
289 if (LHS.isSpecialDenseMapKey() || RHS.isSpecialDenseMapKey())
290 return false;
291
292 // It's safe to use operator==.
293 return LHS == RHS;
294 }
295
296 /// Support for finding `const FileEntry *` in a `DenseMap<FileEntryRef, T>`.
297 /// @{
298 static unsigned getHashValue(const clang::FileEntry *Val) {
299 return llvm::hash_value(Val);
300 }
301 static bool isEqual(const clang::FileEntry *LHS, clang::FileEntryRef RHS) {
302 if (RHS.isSpecialDenseMapKey())
303 return false;
304 return LHS == RHS;
305 }
306 /// @}
307};
308
309} // end namespace llvm
310
311namespace clang {
312
313/// Wrapper around OptionalFileEntryRef that degrades to 'const FileEntry*',
314/// facilitating incremental patches to propagate FileEntryRef.
315///
316/// This class can be used as return value or field where it's convenient for
317/// an OptionalFileEntryRef to degrade to a 'const FileEntry*'. The purpose
318/// is to avoid code churn due to dances like the following:
319/// \code
320/// // Old code.
321/// lvalue = rvalue;
322///
323/// // Temporary code from an incremental patch.
324/// OptionalFileEntryRef MaybeF = rvalue;
325/// lvalue = MaybeF ? &MaybeF.getFileEntry() : nullptr;
326///
327/// // Final code.
328/// lvalue = rvalue;
329/// \endcode
330///
331/// FIXME: Once FileEntryRef is "everywhere" and FileEntry::LastRef and
332/// FileEntry::getName have been deleted, delete this class and replace
333/// instances with OptionalFileEntryRef.
335public:
345
348 : OptionalFileEntryRef(Ref) {}
350 : OptionalFileEntryRef(MaybeRef) {}
351
354 return *this;
355 }
358 return *this;
359 }
363 return *this;
364 }
365
366 /// Degrade to 'const FileEntry *' to allow FileEntry::LastRef and
367 /// FileEntry::getName have been deleted, delete this class and replace
368 /// instances with OptionalFileEntryRef
369 operator const FileEntry *() const {
370 return has_value() ? &(*this)->getFileEntry() : nullptr;
371 }
372};
373
374static_assert(
375 std::is_trivially_copyable<
376 OptionalFileEntryRefDegradesToFileEntryPtr>::value,
377 "OptionalFileEntryRefDegradesToFileEntryPtr should be trivially copyable");
378
379inline bool operator==(const FileEntry *LHS, const OptionalFileEntryRef &RHS) {
380 return LHS == (RHS ? &RHS->getFileEntry() : nullptr);
381}
382inline bool operator==(const OptionalFileEntryRef &LHS, const FileEntry *RHS) {
383 return (LHS ? &LHS->getFileEntry() : nullptr) == RHS;
384}
385inline bool operator!=(const FileEntry *LHS, const OptionalFileEntryRef &RHS) {
386 return !(LHS == RHS);
387}
388inline bool operator!=(const OptionalFileEntryRef &LHS, const FileEntry *RHS) {
389 return !(LHS == RHS);
390}
391
392/// Cached information about one file (either on disk
393/// or in the virtual file system).
394///
395/// If the 'File' member is valid, then this FileEntry has an open file
396/// descriptor for the file.
398 friend class FileManager;
400 FileEntry();
401 FileEntry(const FileEntry &) = delete;
402 FileEntry &operator=(const FileEntry &) = delete;
403
404 std::string RealPathName; // Real path to the file; could be empty.
405 off_t Size = 0; // File size in bytes.
406 time_t ModTime = 0; // Modification time of file.
407 const DirectoryEntry *Dir = nullptr; // Directory file lives in.
408 llvm::sys::fs::UniqueID UniqueID;
409 unsigned UID = 0; // A unique (small) ID for the file.
410 bool IsNamedPipe = false;
411
412 /// The open file, if it is owned by the \p FileEntry.
413 mutable std::unique_ptr<llvm::vfs::File> File;
414
415 /// The file content, if it is owned by the \p FileEntry.
416 std::unique_ptr<llvm::MemoryBuffer> Content;
417
418 // First access name for this FileEntry.
419 //
420 // This is Optional only to allow delayed construction (FileEntryRef has no
421 // default constructor). It should always have a value in practice.
422 //
423 // TODO: remove this once everyone that needs a name uses FileEntryRef.
424 OptionalFileEntryRef LastRef;
425
426public:
428 StringRef getName() const { return LastRef->getName(); }
429 FileEntryRef getLastRef() const { return *LastRef; }
430
431 StringRef tryGetRealPathName() const { return RealPathName; }
432 off_t getSize() const { return Size; }
433 unsigned getUID() const { return UID; }
434 const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
435 time_t getModificationTime() const { return ModTime; }
436
437 /// Return the directory the file lives in.
438 const DirectoryEntry *getDir() const { return Dir; }
439
440 /// Check whether the file is a named pipe (and thus can't be opened by
441 /// the native FileManager methods).
442 bool isNamedPipe() const { return IsNamedPipe; }
443
444 void closeFile() const;
445};
446
447off_t FileEntryRef::getSize() const { return getFileEntry().getSize(); }
448
449unsigned FileEntryRef::getUID() const { return getFileEntry().getUID(); }
450
451const llvm::sys::fs::UniqueID &FileEntryRef::getUniqueID() const {
452 return getFileEntry().getUniqueID();
453}
454
457}
458
460
462
463} // end namespace clang
464
465#endif // LLVM_CLANG_BASIC_FILEENTRY_H
#define V(N, I)
Definition: ASTContext.h:3233
Defines interfaces for clang::DirectoryEntry and clang::DirectoryEntryRef.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
CustomizableOptional & operator=(FileEntryRef &&y)
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:87
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:105
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:132
const clang::FileEntryRef::MapEntry & getMapEntry() const
Expose the underlying MapEntry to simplify packing in a PointerIntPair or PointerUnion and allow cons...
Definition: FileEntry.h:161
void closeFile() const
Definition: FileEntry.h:461
time_t getModificationTime() const
Definition: FileEntry.h:455
friend bool operator!=(const FileEntryRef &LHS, const FileEntryRef &RHS)
Definition: FileEntry.h:93
bool isNamedPipe() const
Definition: FileEntry.h:459
FileEntryRef(const MapEntry &ME)
Definition: FileEntry.h:154
friend bool operator!=(const FileEntry *LHS, const FileEntryRef &RHS)
Definition: FileEntry.h:96
off_t getSize() const
Definition: FileEntry.h:447
llvm::StringMapEntry< llvm::ErrorOr< MapValue > > MapEntry
Type used in the StringMap.
Definition: FileEntry.h:112
StringRef getName() const
The name of this FileEntry.
Definition: FileEntry.h:61
friend bool operator!=(const FileEntryRef &LHS, const FileEntry *RHS)
Definition: FileEntry.h:99
const llvm::sys::fs::UniqueID & getUniqueID() const
Definition: FileEntry.h:451
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:73
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:84
unsigned getUID() const
Definition: FileEntry.h:449
friend bool operator==(const FileEntryRef &LHS, const FileEntry *RHS)
Definition: FileEntry.h:90
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:397
FileEntryRef getLastRef() const
Definition: FileEntry.h:429
const DirectoryEntry * getDir() const
Return the directory the file lives in.
Definition: FileEntry.h:438
StringRef tryGetRealPathName() const
Definition: FileEntry.h:431
StringRef getName() const
Definition: FileEntry.h:428
friend class FileEntryTestHelper
Definition: FileEntry.h:399
time_t getModificationTime() const
Definition: FileEntry.h:435
unsigned getUID() const
Definition: FileEntry.h:433
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:442
void closeFile() const
Definition: FileEntry.cpp:24
off_t getSize() const
Definition: FileEntry.h:432
const llvm::sys::fs::UniqueID & getUniqueID() const
Definition: FileEntry.h:434
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
Customized storage for refs derived from map entires in FileManager, using the private optional_none_...
Wrapper around OptionalFileEntryRef that degrades to 'const FileEntry*', facilitating incremental pat...
Definition: FileEntry.h:334
OptionalFileEntryRefDegradesToFileEntryPtr & operator=(OptionalFileEntryRef MaybeRef)
Definition: FileEntry.h:361
OptionalFileEntryRefDegradesToFileEntryPtr & operator=(std::nullopt_t)
Definition: FileEntry.h:352
OptionalFileEntryRefDegradesToFileEntryPtr & operator=(OptionalFileEntryRefDegradesToFileEntryPtr &&)=default
OptionalFileEntryRefDegradesToFileEntryPtr(const OptionalFileEntryRefDegradesToFileEntryPtr &)=default
OptionalFileEntryRefDegradesToFileEntryPtr(FileEntryRef Ref)
Definition: FileEntry.h:347
OptionalFileEntryRefDegradesToFileEntryPtr & operator=(const OptionalFileEntryRefDegradesToFileEntryPtr &)=default
OptionalFileEntryRefDegradesToFileEntryPtr(OptionalFileEntryRef MaybeRef)
Definition: FileEntry.h:349
OptionalFileEntryRefDegradesToFileEntryPtr(OptionalFileEntryRefDegradesToFileEntryPtr &&)=default
OptionalFileEntryRefDegradesToFileEntryPtr & operator=(FileEntryRef Ref)
Definition: FileEntry.h:356
OptionalStorage(std::in_place_t, ArgTypes &&...Args)
Definition: FileEntry.h:218
OptionalStorage & operator=(clang::FileEntryRef Ref)
Definition: FileEntry.h:221
Code completion in a.
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition: CallGraph.h:207
bool operator!=(CanQual< T > x, CanQual< U > y)
YAML serialization mapping.
Definition: Dominators.h:30
hash_code hash_value(const clang::tooling::dependencies::ModuleID &ID)
Definition: Format.h:5078
Type stored in the StringMap.
Definition: FileEntry.h:115
DirectoryEntryRef Dir
Directory the file was found in.
Definition: FileEntry.h:124
MapValue(FileEntry &FE, DirectoryEntryRef Dir)
Definition: FileEntry.h:127
MapValue(MapEntry &ME, DirectoryEntryRef Dir)
Definition: FileEntry.h:128
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:121
static unsigned getHashValue(const clang::FileEntry *Val)
Support for finding const FileEntry * in a DenseMap<FileEntryRef, T>.
Definition: FileEntry.h:298
static unsigned getHashValue(clang::FileEntryRef Val)
Definition: FileEntry.h:279
static bool isEqual(const clang::FileEntry *LHS, clang::FileEntryRef RHS)
Definition: FileEntry.h:301
static clang::FileEntryRef getTombstoneKey()
Definition: FileEntry.h:275
static clang::FileEntryRef getEmptyKey()
Definition: FileEntry.h:271
static bool isEqual(clang::FileEntryRef LHS, clang::FileEntryRef RHS)
Definition: FileEntry.h:283
static clang::FileEntryRef getFromVoidPointer(void *Ptr)
Definition: FileEntry.h:243
static void * getAsVoidPointer(clang::FileEntryRef File)
Definition: FileEntry.h:239
static clang::OptionalFileEntryRef getFromVoidPointer(void *Ptr)
Definition: FileEntry.h:259
static void * getAsVoidPointer(clang::OptionalFileEntryRef File)
Definition: FileEntry.h:253