clang 22.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 struct dense_map_empty_tag {};
187 struct dense_map_tombstone_tag {};
188
189 // Private constructors for use by DenseMapInfo.
190 FileEntryRef(dense_map_empty_tag)
191 : ME(llvm::DenseMapInfo<const MapEntry *>::getEmptyKey()) {}
192 FileEntryRef(dense_map_tombstone_tag)
193 : ME(llvm::DenseMapInfo<const MapEntry *>::getTombstoneKey()) {}
194 bool isSpecialDenseMapKey() const {
195 return isSameRef(FileEntryRef(dense_map_empty_tag())) ||
196 isSameRef(FileEntryRef(dense_map_tombstone_tag()));
197 }
198
199 const MapEntry *ME;
200};
201
202static_assert(sizeof(FileEntryRef) == sizeof(const FileEntry *),
203 "FileEntryRef must avoid size overhead");
204
205static_assert(std::is_trivially_copyable<FileEntryRef>::value,
206 "FileEntryRef must be trivially copyable");
207
209
210namespace optional_detail {
211
212/// Customize OptionalStorage<FileEntryRef> to use FileEntryRef and its
213/// optional_none_tag to keep it the size of a single pointer.
214template <>
216 : public clang::FileMgr::MapEntryOptionalStorage<clang::FileEntryRef> {
217 using StorageImpl =
219
220public:
221 using StorageImpl::StorageImpl;
222
225 return *this;
226 }
227};
228
229static_assert(sizeof(OptionalFileEntryRef) == sizeof(FileEntryRef),
230 "OptionalFileEntryRef must avoid size overhead");
231
232static_assert(std::is_trivially_copyable<OptionalFileEntryRef>::value,
233 "OptionalFileEntryRef should be trivially copyable");
234
235} // end namespace optional_detail
236} // namespace clang
237
238namespace llvm {
239
240/// Specialisation of DenseMapInfo for FileEntryRef.
241template <> struct DenseMapInfo<clang::FileEntryRef> {
243 return clang::FileEntryRef(clang::FileEntryRef::dense_map_empty_tag());
244 }
245
247 return clang::FileEntryRef(clang::FileEntryRef::dense_map_tombstone_tag());
248 }
249
250 static unsigned getHashValue(clang::FileEntryRef Val) {
251 return hash_value(Val);
252 }
253
255 // Catch the easy cases: both empty, both tombstone, or the same ref.
256 if (LHS.isSameRef(RHS))
257 return true;
258
259 // Confirm LHS and RHS are valid.
260 if (LHS.isSpecialDenseMapKey() || RHS.isSpecialDenseMapKey())
261 return false;
262
263 // It's safe to use operator==.
264 return LHS == RHS;
265 }
266
267 /// Support for finding `const FileEntry *` in a `DenseMap<FileEntryRef, T>`.
268 /// @{
269 static unsigned getHashValue(const clang::FileEntry *Val) {
270 return llvm::hash_value(Val);
271 }
272 static bool isEqual(const clang::FileEntry *LHS, clang::FileEntryRef RHS) {
273 if (RHS.isSpecialDenseMapKey())
274 return false;
275 return LHS == RHS;
276 }
277 /// @}
278};
279
280} // end namespace llvm
281
282namespace clang {
283
284inline bool operator==(const FileEntry *LHS, const OptionalFileEntryRef &RHS) {
285 return LHS == (RHS ? &RHS->getFileEntry() : nullptr);
286}
287inline bool operator==(const OptionalFileEntryRef &LHS, const FileEntry *RHS) {
288 return (LHS ? &LHS->getFileEntry() : nullptr) == RHS;
289}
290inline bool operator!=(const FileEntry *LHS, const OptionalFileEntryRef &RHS) {
291 return !(LHS == RHS);
292}
293inline bool operator!=(const OptionalFileEntryRef &LHS, const FileEntry *RHS) {
294 return !(LHS == RHS);
295}
296
297/// Cached information about one file (either on disk
298/// or in the virtual file system).
299///
300/// If the 'File' member is valid, then this FileEntry has an open file
301/// descriptor for the file.
302class FileEntry {
303 friend class FileManager;
305 FileEntry();
306 FileEntry(const FileEntry &) = delete;
307 FileEntry &operator=(const FileEntry &) = delete;
308
309 std::string RealPathName; // Real path to the file; could be empty.
310 off_t Size = 0; // File size in bytes.
311 time_t ModTime = 0; // Modification time of file.
312 const DirectoryEntry *Dir = nullptr; // Directory file lives in.
313 llvm::sys::fs::UniqueID UniqueID;
314 unsigned UID = 0; // A unique (small) ID for the file.
315 bool IsNamedPipe = false;
316 bool IsDeviceFile = false;
317
318 /// The open file, if it is owned by the \p FileEntry.
319 mutable std::unique_ptr<llvm::vfs::File> File;
320
321 /// The file content, if it is owned by the \p FileEntry.
322 std::unique_ptr<llvm::MemoryBuffer> Content;
323
324public:
326
327 StringRef tryGetRealPathName() const { return RealPathName; }
328 off_t getSize() const { return Size; }
329 // Size may increase due to potential z/OS EBCDIC -> UTF-8 conversion.
330 void setSize(off_t NewSize) { Size = NewSize; }
331 unsigned getUID() const { return UID; }
332 const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
333 time_t getModificationTime() const { return ModTime; }
334
335 /// Return the directory the file lives in.
336 const DirectoryEntry *getDir() const { return Dir; }
337
338 /// Check whether the file is a named pipe (and thus can't be opened by
339 /// the native FileManager methods).
340 bool isNamedPipe() const { return IsNamedPipe; }
341 bool isDeviceFile() const { return IsDeviceFile; }
342
343 void closeFile() const;
344};
345
346off_t FileEntryRef::getSize() const { return getFileEntry().getSize(); }
347
348unsigned FileEntryRef::getUID() const { return getFileEntry().getUID(); }
349
350const llvm::sys::fs::UniqueID &FileEntryRef::getUniqueID() const {
351 return getFileEntry().getUniqueID();
352}
353
357
360 return getFileEntry().isDeviceFile();
361}
362
364
366 cast<FileEntry *>(getBaseMapEntry().second->V)->setSize(BufferSize);
367}
368
369} // end namespace clang
370
371#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:359
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:363
time_t getModificationTime() const
Definition FileEntry.h:354
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:358
void updateFileEntryBufferSize(unsigned BufferSize)
Definition FileEntry.h:365
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:346
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:350
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:348
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:302
bool isDeviceFile() const
Definition FileEntry.h:341
const DirectoryEntry * getDir() const
Return the directory the file lives in.
Definition FileEntry.h:336
StringRef tryGetRealPathName() const
Definition FileEntry.h:327
friend class FileEntryTestHelper
Definition FileEntry.h:304
void setSize(off_t NewSize)
Definition FileEntry.h:330
time_t getModificationTime() const
Definition FileEntry.h:333
unsigned getUID() const
Definition FileEntry.h:331
friend class FileManager
Definition FileEntry.h:303
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:340
void closeFile() const
Definition FileEntry.cpp:23
off_t getSize() const
Definition FileEntry.h:328
const llvm::sys::fs::UniqueID & getUniqueID() const
Definition FileEntry.h:332
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:223
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:208
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:204
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::tooling::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:269
static unsigned getHashValue(clang::FileEntryRef Val)
Definition FileEntry.h:250
static bool isEqual(const clang::FileEntry *LHS, clang::FileEntryRef RHS)
Definition FileEntry.h:272
static clang::FileEntryRef getTombstoneKey()
Definition FileEntry.h:246
static clang::FileEntryRef getEmptyKey()
Definition FileEntry.h:242
static bool isEqual(clang::FileEntryRef LHS, clang::FileEntryRef RHS)
Definition FileEntry.h:254