File: | llvm/lib/Support/VirtualFileSystem.cpp |
Warning: | line 1578, column 14 The left operand of '==' is a garbage value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- VirtualFileSystem.cpp - Virtual File System Layer ------------------===// | ||||||||
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 | // This file implements the VirtualFileSystem interface. | ||||||||
10 | // | ||||||||
11 | //===----------------------------------------------------------------------===// | ||||||||
12 | |||||||||
13 | #include "llvm/Support/VirtualFileSystem.h" | ||||||||
14 | #include "llvm/ADT/ArrayRef.h" | ||||||||
15 | #include "llvm/ADT/DenseMap.h" | ||||||||
16 | #include "llvm/ADT/IntrusiveRefCntPtr.h" | ||||||||
17 | #include "llvm/ADT/None.h" | ||||||||
18 | #include "llvm/ADT/Optional.h" | ||||||||
19 | #include "llvm/ADT/STLExtras.h" | ||||||||
20 | #include "llvm/ADT/SmallString.h" | ||||||||
21 | #include "llvm/ADT/SmallVector.h" | ||||||||
22 | #include "llvm/ADT/StringRef.h" | ||||||||
23 | #include "llvm/ADT/StringSet.h" | ||||||||
24 | #include "llvm/ADT/Twine.h" | ||||||||
25 | #include "llvm/ADT/iterator_range.h" | ||||||||
26 | #include "llvm/Config/llvm-config.h" | ||||||||
27 | #include "llvm/Support/Casting.h" | ||||||||
28 | #include "llvm/Support/Chrono.h" | ||||||||
29 | #include "llvm/Support/Compiler.h" | ||||||||
30 | #include "llvm/Support/Debug.h" | ||||||||
31 | #include "llvm/Support/Errc.h" | ||||||||
32 | #include "llvm/Support/ErrorHandling.h" | ||||||||
33 | #include "llvm/Support/ErrorOr.h" | ||||||||
34 | #include "llvm/Support/FileSystem.h" | ||||||||
35 | #include "llvm/Support/MemoryBuffer.h" | ||||||||
36 | #include "llvm/Support/Path.h" | ||||||||
37 | #include "llvm/Support/Process.h" | ||||||||
38 | #include "llvm/Support/SMLoc.h" | ||||||||
39 | #include "llvm/Support/SourceMgr.h" | ||||||||
40 | #include "llvm/Support/YAMLParser.h" | ||||||||
41 | #include "llvm/Support/raw_ostream.h" | ||||||||
42 | #include <algorithm> | ||||||||
43 | #include <atomic> | ||||||||
44 | #include <cassert> | ||||||||
45 | #include <cstdint> | ||||||||
46 | #include <iterator> | ||||||||
47 | #include <limits> | ||||||||
48 | #include <map> | ||||||||
49 | #include <memory> | ||||||||
50 | #include <mutex> | ||||||||
51 | #include <string> | ||||||||
52 | #include <system_error> | ||||||||
53 | #include <utility> | ||||||||
54 | #include <vector> | ||||||||
55 | |||||||||
56 | using namespace llvm; | ||||||||
57 | using namespace llvm::vfs; | ||||||||
58 | |||||||||
59 | using llvm::sys::fs::file_t; | ||||||||
60 | using llvm::sys::fs::file_status; | ||||||||
61 | using llvm::sys::fs::file_type; | ||||||||
62 | using llvm::sys::fs::kInvalidFile; | ||||||||
63 | using llvm::sys::fs::perms; | ||||||||
64 | using llvm::sys::fs::UniqueID; | ||||||||
65 | |||||||||
66 | Status::Status(const file_status &Status) | ||||||||
67 | : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()), | ||||||||
68 | User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()), | ||||||||
69 | Type(Status.type()), Perms(Status.permissions()) {} | ||||||||
70 | |||||||||
71 | Status::Status(const Twine &Name, UniqueID UID, sys::TimePoint<> MTime, | ||||||||
72 | uint32_t User, uint32_t Group, uint64_t Size, file_type Type, | ||||||||
73 | perms Perms) | ||||||||
74 | : Name(Name.str()), UID(UID), MTime(MTime), User(User), Group(Group), | ||||||||
75 | Size(Size), Type(Type), Perms(Perms) {} | ||||||||
76 | |||||||||
77 | Status Status::copyWithNewName(const Status &In, const Twine &NewName) { | ||||||||
78 | return Status(NewName, In.getUniqueID(), In.getLastModificationTime(), | ||||||||
79 | In.getUser(), In.getGroup(), In.getSize(), In.getType(), | ||||||||
80 | In.getPermissions()); | ||||||||
81 | } | ||||||||
82 | |||||||||
83 | Status Status::copyWithNewName(const file_status &In, const Twine &NewName) { | ||||||||
84 | return Status(NewName, In.getUniqueID(), In.getLastModificationTime(), | ||||||||
85 | In.getUser(), In.getGroup(), In.getSize(), In.type(), | ||||||||
86 | In.permissions()); | ||||||||
87 | } | ||||||||
88 | |||||||||
89 | bool Status::equivalent(const Status &Other) const { | ||||||||
90 | assert(isStatusKnown() && Other.isStatusKnown())(static_cast <bool> (isStatusKnown() && Other.isStatusKnown ()) ? void (0) : __assert_fail ("isStatusKnown() && Other.isStatusKnown()" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 90, __extension__ __PRETTY_FUNCTION__)); | ||||||||
91 | return getUniqueID() == Other.getUniqueID(); | ||||||||
92 | } | ||||||||
93 | |||||||||
94 | bool Status::isDirectory() const { return Type == file_type::directory_file; } | ||||||||
95 | |||||||||
96 | bool Status::isRegularFile() const { return Type == file_type::regular_file; } | ||||||||
97 | |||||||||
98 | bool Status::isOther() const { | ||||||||
99 | return exists() && !isRegularFile() && !isDirectory() && !isSymlink(); | ||||||||
100 | } | ||||||||
101 | |||||||||
102 | bool Status::isSymlink() const { return Type == file_type::symlink_file; } | ||||||||
103 | |||||||||
104 | bool Status::isStatusKnown() const { return Type != file_type::status_error; } | ||||||||
105 | |||||||||
106 | bool Status::exists() const { | ||||||||
107 | return isStatusKnown() && Type != file_type::file_not_found; | ||||||||
108 | } | ||||||||
109 | |||||||||
110 | File::~File() = default; | ||||||||
111 | |||||||||
112 | FileSystem::~FileSystem() = default; | ||||||||
113 | |||||||||
114 | ErrorOr<std::unique_ptr<MemoryBuffer>> | ||||||||
115 | FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize, | ||||||||
116 | bool RequiresNullTerminator, bool IsVolatile) { | ||||||||
117 | auto F = openFileForRead(Name); | ||||||||
118 | if (!F) | ||||||||
119 | return F.getError(); | ||||||||
120 | |||||||||
121 | return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile); | ||||||||
122 | } | ||||||||
123 | |||||||||
124 | std::error_code FileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const { | ||||||||
125 | if (llvm::sys::path::is_absolute(Path)) | ||||||||
126 | return {}; | ||||||||
127 | |||||||||
128 | auto WorkingDir = getCurrentWorkingDirectory(); | ||||||||
129 | if (!WorkingDir) | ||||||||
130 | return WorkingDir.getError(); | ||||||||
131 | |||||||||
132 | llvm::sys::fs::make_absolute(WorkingDir.get(), Path); | ||||||||
133 | return {}; | ||||||||
134 | } | ||||||||
135 | |||||||||
136 | std::error_code FileSystem::getRealPath(const Twine &Path, | ||||||||
137 | SmallVectorImpl<char> &Output) const { | ||||||||
138 | return errc::operation_not_permitted; | ||||||||
139 | } | ||||||||
140 | |||||||||
141 | std::error_code FileSystem::isLocal(const Twine &Path, bool &Result) { | ||||||||
142 | return errc::operation_not_permitted; | ||||||||
143 | } | ||||||||
144 | |||||||||
145 | bool FileSystem::exists(const Twine &Path) { | ||||||||
146 | auto Status = status(Path); | ||||||||
147 | return Status && Status->exists(); | ||||||||
148 | } | ||||||||
149 | |||||||||
150 | #ifndef NDEBUG | ||||||||
151 | static bool isTraversalComponent(StringRef Component) { | ||||||||
152 | return Component.equals("..") || Component.equals("."); | ||||||||
153 | } | ||||||||
154 | |||||||||
155 | static bool pathHasTraversal(StringRef Path) { | ||||||||
156 | using namespace llvm::sys; | ||||||||
157 | |||||||||
158 | for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path))) | ||||||||
159 | if (isTraversalComponent(Comp)) | ||||||||
160 | return true; | ||||||||
161 | return false; | ||||||||
162 | } | ||||||||
163 | #endif | ||||||||
164 | |||||||||
165 | //===-----------------------------------------------------------------------===/ | ||||||||
166 | // RealFileSystem implementation | ||||||||
167 | //===-----------------------------------------------------------------------===/ | ||||||||
168 | |||||||||
169 | namespace { | ||||||||
170 | |||||||||
171 | /// Wrapper around a raw file descriptor. | ||||||||
172 | class RealFile : public File { | ||||||||
173 | friend class RealFileSystem; | ||||||||
174 | |||||||||
175 | file_t FD; | ||||||||
176 | Status S; | ||||||||
177 | std::string RealName; | ||||||||
178 | |||||||||
179 | RealFile(file_t RawFD, StringRef NewName, StringRef NewRealPathName) | ||||||||
180 | : FD(RawFD), S(NewName, {}, {}, {}, {}, {}, | ||||||||
181 | llvm::sys::fs::file_type::status_error, {}), | ||||||||
182 | RealName(NewRealPathName.str()) { | ||||||||
183 | assert(FD != kInvalidFile && "Invalid or inactive file descriptor")(static_cast <bool> (FD != kInvalidFile && "Invalid or inactive file descriptor" ) ? void (0) : __assert_fail ("FD != kInvalidFile && \"Invalid or inactive file descriptor\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 183, __extension__ __PRETTY_FUNCTION__)); | ||||||||
184 | } | ||||||||
185 | |||||||||
186 | public: | ||||||||
187 | ~RealFile() override; | ||||||||
188 | |||||||||
189 | ErrorOr<Status> status() override; | ||||||||
190 | ErrorOr<std::string> getName() override; | ||||||||
191 | ErrorOr<std::unique_ptr<MemoryBuffer>> getBuffer(const Twine &Name, | ||||||||
192 | int64_t FileSize, | ||||||||
193 | bool RequiresNullTerminator, | ||||||||
194 | bool IsVolatile) override; | ||||||||
195 | std::error_code close() override; | ||||||||
196 | }; | ||||||||
197 | |||||||||
198 | } // namespace | ||||||||
199 | |||||||||
200 | RealFile::~RealFile() { close(); } | ||||||||
201 | |||||||||
202 | ErrorOr<Status> RealFile::status() { | ||||||||
203 | assert(FD != kInvalidFile && "cannot stat closed file")(static_cast <bool> (FD != kInvalidFile && "cannot stat closed file" ) ? void (0) : __assert_fail ("FD != kInvalidFile && \"cannot stat closed file\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 203, __extension__ __PRETTY_FUNCTION__)); | ||||||||
204 | if (!S.isStatusKnown()) { | ||||||||
205 | file_status RealStatus; | ||||||||
206 | if (std::error_code EC = sys::fs::status(FD, RealStatus)) | ||||||||
207 | return EC; | ||||||||
208 | S = Status::copyWithNewName(RealStatus, S.getName()); | ||||||||
209 | } | ||||||||
210 | return S; | ||||||||
211 | } | ||||||||
212 | |||||||||
213 | ErrorOr<std::string> RealFile::getName() { | ||||||||
214 | return RealName.empty() ? S.getName().str() : RealName; | ||||||||
215 | } | ||||||||
216 | |||||||||
217 | ErrorOr<std::unique_ptr<MemoryBuffer>> | ||||||||
218 | RealFile::getBuffer(const Twine &Name, int64_t FileSize, | ||||||||
219 | bool RequiresNullTerminator, bool IsVolatile) { | ||||||||
220 | assert(FD != kInvalidFile && "cannot get buffer for closed file")(static_cast <bool> (FD != kInvalidFile && "cannot get buffer for closed file" ) ? void (0) : __assert_fail ("FD != kInvalidFile && \"cannot get buffer for closed file\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 220, __extension__ __PRETTY_FUNCTION__)); | ||||||||
221 | return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator, | ||||||||
222 | IsVolatile); | ||||||||
223 | } | ||||||||
224 | |||||||||
225 | std::error_code RealFile::close() { | ||||||||
226 | std::error_code EC = sys::fs::closeFile(FD); | ||||||||
227 | FD = kInvalidFile; | ||||||||
228 | return EC; | ||||||||
229 | } | ||||||||
230 | |||||||||
231 | namespace { | ||||||||
232 | |||||||||
233 | /// A file system according to your operating system. | ||||||||
234 | /// This may be linked to the process's working directory, or maintain its own. | ||||||||
235 | /// | ||||||||
236 | /// Currently, its own working directory is emulated by storing the path and | ||||||||
237 | /// sending absolute paths to llvm::sys::fs:: functions. | ||||||||
238 | /// A more principled approach would be to push this down a level, modelling | ||||||||
239 | /// the working dir as an llvm::sys::fs::WorkingDir or similar. | ||||||||
240 | /// This would enable the use of openat()-style functions on some platforms. | ||||||||
241 | class RealFileSystem : public FileSystem { | ||||||||
242 | public: | ||||||||
243 | explicit RealFileSystem(bool LinkCWDToProcess) { | ||||||||
244 | if (!LinkCWDToProcess) { | ||||||||
245 | SmallString<128> PWD, RealPWD; | ||||||||
246 | if (llvm::sys::fs::current_path(PWD)) | ||||||||
247 | return; // Awful, but nothing to do here. | ||||||||
248 | if (llvm::sys::fs::real_path(PWD, RealPWD)) | ||||||||
249 | WD = {PWD, PWD}; | ||||||||
250 | else | ||||||||
251 | WD = {PWD, RealPWD}; | ||||||||
252 | } | ||||||||
253 | } | ||||||||
254 | |||||||||
255 | ErrorOr<Status> status(const Twine &Path) override; | ||||||||
256 | ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override; | ||||||||
257 | directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; | ||||||||
258 | |||||||||
259 | llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override; | ||||||||
260 | std::error_code setCurrentWorkingDirectory(const Twine &Path) override; | ||||||||
261 | std::error_code isLocal(const Twine &Path, bool &Result) override; | ||||||||
262 | std::error_code getRealPath(const Twine &Path, | ||||||||
263 | SmallVectorImpl<char> &Output) const override; | ||||||||
264 | |||||||||
265 | private: | ||||||||
266 | // If this FS has its own working dir, use it to make Path absolute. | ||||||||
267 | // The returned twine is safe to use as long as both Storage and Path live. | ||||||||
268 | Twine adjustPath(const Twine &Path, SmallVectorImpl<char> &Storage) const { | ||||||||
269 | if (!WD) | ||||||||
270 | return Path; | ||||||||
271 | Path.toVector(Storage); | ||||||||
272 | sys::fs::make_absolute(WD->Resolved, Storage); | ||||||||
273 | return Storage; | ||||||||
274 | } | ||||||||
275 | |||||||||
276 | struct WorkingDirectory { | ||||||||
277 | // The current working directory, without symlinks resolved. (echo $PWD). | ||||||||
278 | SmallString<128> Specified; | ||||||||
279 | // The current working directory, with links resolved. (readlink .). | ||||||||
280 | SmallString<128> Resolved; | ||||||||
281 | }; | ||||||||
282 | Optional<WorkingDirectory> WD; | ||||||||
283 | }; | ||||||||
284 | |||||||||
285 | } // namespace | ||||||||
286 | |||||||||
287 | ErrorOr<Status> RealFileSystem::status(const Twine &Path) { | ||||||||
288 | SmallString<256> Storage; | ||||||||
289 | sys::fs::file_status RealStatus; | ||||||||
290 | if (std::error_code EC = | ||||||||
291 | sys::fs::status(adjustPath(Path, Storage), RealStatus)) | ||||||||
292 | return EC; | ||||||||
293 | return Status::copyWithNewName(RealStatus, Path); | ||||||||
294 | } | ||||||||
295 | |||||||||
296 | ErrorOr<std::unique_ptr<File>> | ||||||||
297 | RealFileSystem::openFileForRead(const Twine &Name) { | ||||||||
298 | SmallString<256> RealName, Storage; | ||||||||
299 | Expected<file_t> FDOrErr = sys::fs::openNativeFileForRead( | ||||||||
300 | adjustPath(Name, Storage), sys::fs::OF_None, &RealName); | ||||||||
301 | if (!FDOrErr) | ||||||||
302 | return errorToErrorCode(FDOrErr.takeError()); | ||||||||
303 | return std::unique_ptr<File>( | ||||||||
304 | new RealFile(*FDOrErr, Name.str(), RealName.str())); | ||||||||
305 | } | ||||||||
306 | |||||||||
307 | llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const { | ||||||||
308 | if (WD) | ||||||||
309 | return std::string(WD->Specified.str()); | ||||||||
310 | |||||||||
311 | SmallString<128> Dir; | ||||||||
312 | if (std::error_code EC = llvm::sys::fs::current_path(Dir)) | ||||||||
313 | return EC; | ||||||||
314 | return std::string(Dir.str()); | ||||||||
315 | } | ||||||||
316 | |||||||||
317 | std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) { | ||||||||
318 | if (!WD) | ||||||||
319 | return llvm::sys::fs::set_current_path(Path); | ||||||||
320 | |||||||||
321 | SmallString<128> Absolute, Resolved, Storage; | ||||||||
322 | adjustPath(Path, Storage).toVector(Absolute); | ||||||||
323 | bool IsDir; | ||||||||
324 | if (auto Err = llvm::sys::fs::is_directory(Absolute, IsDir)) | ||||||||
325 | return Err; | ||||||||
326 | if (!IsDir) | ||||||||
327 | return std::make_error_code(std::errc::not_a_directory); | ||||||||
328 | if (auto Err = llvm::sys::fs::real_path(Absolute, Resolved)) | ||||||||
329 | return Err; | ||||||||
330 | WD = {Absolute, Resolved}; | ||||||||
331 | return std::error_code(); | ||||||||
332 | } | ||||||||
333 | |||||||||
334 | std::error_code RealFileSystem::isLocal(const Twine &Path, bool &Result) { | ||||||||
335 | SmallString<256> Storage; | ||||||||
336 | return llvm::sys::fs::is_local(adjustPath(Path, Storage), Result); | ||||||||
337 | } | ||||||||
338 | |||||||||
339 | std::error_code | ||||||||
340 | RealFileSystem::getRealPath(const Twine &Path, | ||||||||
341 | SmallVectorImpl<char> &Output) const { | ||||||||
342 | SmallString<256> Storage; | ||||||||
343 | return llvm::sys::fs::real_path(adjustPath(Path, Storage), Output); | ||||||||
344 | } | ||||||||
345 | |||||||||
346 | IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() { | ||||||||
347 | static IntrusiveRefCntPtr<FileSystem> FS(new RealFileSystem(true)); | ||||||||
348 | return FS; | ||||||||
349 | } | ||||||||
350 | |||||||||
351 | std::unique_ptr<FileSystem> vfs::createPhysicalFileSystem() { | ||||||||
352 | return std::make_unique<RealFileSystem>(false); | ||||||||
353 | } | ||||||||
354 | |||||||||
355 | namespace { | ||||||||
356 | |||||||||
357 | class RealFSDirIter : public llvm::vfs::detail::DirIterImpl { | ||||||||
358 | llvm::sys::fs::directory_iterator Iter; | ||||||||
359 | |||||||||
360 | public: | ||||||||
361 | RealFSDirIter(const Twine &Path, std::error_code &EC) : Iter(Path, EC) { | ||||||||
362 | if (Iter != llvm::sys::fs::directory_iterator()) | ||||||||
363 | CurrentEntry = directory_entry(Iter->path(), Iter->type()); | ||||||||
364 | } | ||||||||
365 | |||||||||
366 | std::error_code increment() override { | ||||||||
367 | std::error_code EC; | ||||||||
368 | Iter.increment(EC); | ||||||||
369 | CurrentEntry = (Iter == llvm::sys::fs::directory_iterator()) | ||||||||
370 | ? directory_entry() | ||||||||
371 | : directory_entry(Iter->path(), Iter->type()); | ||||||||
372 | return EC; | ||||||||
373 | } | ||||||||
374 | }; | ||||||||
375 | |||||||||
376 | } // namespace | ||||||||
377 | |||||||||
378 | directory_iterator RealFileSystem::dir_begin(const Twine &Dir, | ||||||||
379 | std::error_code &EC) { | ||||||||
380 | SmallString<128> Storage; | ||||||||
381 | return directory_iterator( | ||||||||
382 | std::make_shared<RealFSDirIter>(adjustPath(Dir, Storage), EC)); | ||||||||
383 | } | ||||||||
384 | |||||||||
385 | //===-----------------------------------------------------------------------===/ | ||||||||
386 | // OverlayFileSystem implementation | ||||||||
387 | //===-----------------------------------------------------------------------===/ | ||||||||
388 | |||||||||
389 | OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) { | ||||||||
390 | FSList.push_back(std::move(BaseFS)); | ||||||||
391 | } | ||||||||
392 | |||||||||
393 | void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) { | ||||||||
394 | FSList.push_back(FS); | ||||||||
395 | // Synchronize added file systems by duplicating the working directory from | ||||||||
396 | // the first one in the list. | ||||||||
397 | FS->setCurrentWorkingDirectory(getCurrentWorkingDirectory().get()); | ||||||||
398 | } | ||||||||
399 | |||||||||
400 | ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) { | ||||||||
401 | // FIXME: handle symlinks that cross file systems | ||||||||
402 | for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) { | ||||||||
403 | ErrorOr<Status> Status = (*I)->status(Path); | ||||||||
404 | if (Status || Status.getError() != llvm::errc::no_such_file_or_directory) | ||||||||
405 | return Status; | ||||||||
406 | } | ||||||||
407 | return make_error_code(llvm::errc::no_such_file_or_directory); | ||||||||
408 | } | ||||||||
409 | |||||||||
410 | ErrorOr<std::unique_ptr<File>> | ||||||||
411 | OverlayFileSystem::openFileForRead(const llvm::Twine &Path) { | ||||||||
412 | // FIXME: handle symlinks that cross file systems | ||||||||
413 | for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) { | ||||||||
414 | auto Result = (*I)->openFileForRead(Path); | ||||||||
415 | if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) | ||||||||
416 | return Result; | ||||||||
417 | } | ||||||||
418 | return make_error_code(llvm::errc::no_such_file_or_directory); | ||||||||
419 | } | ||||||||
420 | |||||||||
421 | llvm::ErrorOr<std::string> | ||||||||
422 | OverlayFileSystem::getCurrentWorkingDirectory() const { | ||||||||
423 | // All file systems are synchronized, just take the first working directory. | ||||||||
424 | return FSList.front()->getCurrentWorkingDirectory(); | ||||||||
425 | } | ||||||||
426 | |||||||||
427 | std::error_code | ||||||||
428 | OverlayFileSystem::setCurrentWorkingDirectory(const Twine &Path) { | ||||||||
429 | for (auto &FS : FSList) | ||||||||
430 | if (std::error_code EC = FS->setCurrentWorkingDirectory(Path)) | ||||||||
431 | return EC; | ||||||||
432 | return {}; | ||||||||
433 | } | ||||||||
434 | |||||||||
435 | std::error_code OverlayFileSystem::isLocal(const Twine &Path, bool &Result) { | ||||||||
436 | for (auto &FS : FSList) | ||||||||
437 | if (FS->exists(Path)) | ||||||||
438 | return FS->isLocal(Path, Result); | ||||||||
439 | return errc::no_such_file_or_directory; | ||||||||
440 | } | ||||||||
441 | |||||||||
442 | std::error_code | ||||||||
443 | OverlayFileSystem::getRealPath(const Twine &Path, | ||||||||
444 | SmallVectorImpl<char> &Output) const { | ||||||||
445 | for (auto &FS : FSList) | ||||||||
446 | if (FS->exists(Path)) | ||||||||
447 | return FS->getRealPath(Path, Output); | ||||||||
448 | return errc::no_such_file_or_directory; | ||||||||
449 | } | ||||||||
450 | |||||||||
451 | llvm::vfs::detail::DirIterImpl::~DirIterImpl() = default; | ||||||||
452 | |||||||||
453 | namespace { | ||||||||
454 | |||||||||
455 | /// Combines and deduplicates directory entries across multiple file systems. | ||||||||
456 | class CombiningDirIterImpl : public llvm::vfs::detail::DirIterImpl { | ||||||||
457 | using FileSystemPtr = llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>; | ||||||||
458 | |||||||||
459 | /// File systems to check for entries in. Processed in reverse order. | ||||||||
460 | SmallVector<FileSystemPtr, 8> FSList; | ||||||||
461 | /// The directory iterator for the current filesystem. | ||||||||
462 | directory_iterator CurrentDirIter; | ||||||||
463 | /// The path of the directory to iterate the entries of. | ||||||||
464 | std::string DirPath; | ||||||||
465 | /// The set of names already returned as entries. | ||||||||
466 | llvm::StringSet<> SeenNames; | ||||||||
467 | |||||||||
468 | /// Sets \c CurrentDirIter to an iterator of \c DirPath in the next file | ||||||||
469 | /// system in the list, or leaves it as is (at its end position) if we've | ||||||||
470 | /// already gone through them all. | ||||||||
471 | std::error_code incrementFS() { | ||||||||
472 | while (!FSList.empty()) { | ||||||||
473 | std::error_code EC; | ||||||||
474 | CurrentDirIter = FSList.back()->dir_begin(DirPath, EC); | ||||||||
475 | FSList.pop_back(); | ||||||||
476 | if (EC && EC != errc::no_such_file_or_directory) | ||||||||
477 | return EC; | ||||||||
478 | if (CurrentDirIter != directory_iterator()) | ||||||||
479 | break; // found | ||||||||
480 | } | ||||||||
481 | return {}; | ||||||||
482 | } | ||||||||
483 | |||||||||
484 | std::error_code incrementDirIter(bool IsFirstTime) { | ||||||||
485 | assert((IsFirstTime || CurrentDirIter != directory_iterator()) &&(static_cast <bool> ((IsFirstTime || CurrentDirIter != directory_iterator ()) && "incrementing past end") ? void (0) : __assert_fail ("(IsFirstTime || CurrentDirIter != directory_iterator()) && \"incrementing past end\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 486, __extension__ __PRETTY_FUNCTION__)) | ||||||||
486 | "incrementing past end")(static_cast <bool> ((IsFirstTime || CurrentDirIter != directory_iterator ()) && "incrementing past end") ? void (0) : __assert_fail ("(IsFirstTime || CurrentDirIter != directory_iterator()) && \"incrementing past end\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 486, __extension__ __PRETTY_FUNCTION__)); | ||||||||
487 | std::error_code EC; | ||||||||
488 | if (!IsFirstTime) | ||||||||
489 | CurrentDirIter.increment(EC); | ||||||||
490 | if (!EC && CurrentDirIter == directory_iterator()) | ||||||||
491 | EC = incrementFS(); | ||||||||
492 | return EC; | ||||||||
493 | } | ||||||||
494 | |||||||||
495 | std::error_code incrementImpl(bool IsFirstTime) { | ||||||||
496 | while (true) { | ||||||||
497 | std::error_code EC = incrementDirIter(IsFirstTime); | ||||||||
498 | if (EC || CurrentDirIter == directory_iterator()) { | ||||||||
499 | CurrentEntry = directory_entry(); | ||||||||
500 | return EC; | ||||||||
501 | } | ||||||||
502 | CurrentEntry = *CurrentDirIter; | ||||||||
503 | StringRef Name = llvm::sys::path::filename(CurrentEntry.path()); | ||||||||
504 | if (SeenNames.insert(Name).second) | ||||||||
505 | return EC; // name not seen before | ||||||||
506 | } | ||||||||
507 | llvm_unreachable("returned above")::llvm::llvm_unreachable_internal("returned above", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 507); | ||||||||
508 | } | ||||||||
509 | |||||||||
510 | public: | ||||||||
511 | CombiningDirIterImpl(ArrayRef<FileSystemPtr> FileSystems, std::string Dir, | ||||||||
512 | std::error_code &EC) | ||||||||
513 | : FSList(FileSystems.begin(), FileSystems.end()), | ||||||||
514 | DirPath(std::move(Dir)) { | ||||||||
515 | if (!FSList.empty()) { | ||||||||
516 | CurrentDirIter = FSList.back()->dir_begin(DirPath, EC); | ||||||||
517 | FSList.pop_back(); | ||||||||
518 | if (!EC || EC == errc::no_such_file_or_directory) | ||||||||
519 | EC = incrementImpl(true); | ||||||||
520 | } | ||||||||
521 | } | ||||||||
522 | |||||||||
523 | CombiningDirIterImpl(directory_iterator FirstIter, FileSystemPtr Fallback, | ||||||||
524 | std::string FallbackDir, std::error_code &EC) | ||||||||
525 | : FSList({Fallback}), CurrentDirIter(FirstIter), | ||||||||
526 | DirPath(std::move(FallbackDir)) { | ||||||||
527 | if (!EC || EC == errc::no_such_file_or_directory) | ||||||||
528 | EC = incrementImpl(true); | ||||||||
529 | } | ||||||||
530 | |||||||||
531 | std::error_code increment() override { return incrementImpl(false); } | ||||||||
532 | }; | ||||||||
533 | |||||||||
534 | } // namespace | ||||||||
535 | |||||||||
536 | directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir, | ||||||||
537 | std::error_code &EC) { | ||||||||
538 | return directory_iterator( | ||||||||
539 | std::make_shared<CombiningDirIterImpl>(FSList, Dir.str(), EC)); | ||||||||
540 | } | ||||||||
541 | |||||||||
542 | void ProxyFileSystem::anchor() {} | ||||||||
543 | |||||||||
544 | namespace llvm { | ||||||||
545 | namespace vfs { | ||||||||
546 | |||||||||
547 | namespace detail { | ||||||||
548 | |||||||||
549 | enum InMemoryNodeKind { IME_File, IME_Directory, IME_HardLink }; | ||||||||
550 | |||||||||
551 | /// The in memory file system is a tree of Nodes. Every node can either be a | ||||||||
552 | /// file , hardlink or a directory. | ||||||||
553 | class InMemoryNode { | ||||||||
554 | InMemoryNodeKind Kind; | ||||||||
555 | std::string FileName; | ||||||||
556 | |||||||||
557 | public: | ||||||||
558 | InMemoryNode(llvm::StringRef FileName, InMemoryNodeKind Kind) | ||||||||
559 | : Kind(Kind), FileName(std::string(llvm::sys::path::filename(FileName))) { | ||||||||
560 | } | ||||||||
561 | virtual ~InMemoryNode() = default; | ||||||||
562 | |||||||||
563 | /// Get the filename of this node (the name without the directory part). | ||||||||
564 | StringRef getFileName() const { return FileName; } | ||||||||
565 | InMemoryNodeKind getKind() const { return Kind; } | ||||||||
566 | virtual std::string toString(unsigned Indent) const = 0; | ||||||||
567 | }; | ||||||||
568 | |||||||||
569 | class InMemoryFile : public InMemoryNode { | ||||||||
570 | Status Stat; | ||||||||
571 | std::unique_ptr<llvm::MemoryBuffer> Buffer; | ||||||||
572 | |||||||||
573 | public: | ||||||||
574 | InMemoryFile(Status Stat, std::unique_ptr<llvm::MemoryBuffer> Buffer) | ||||||||
575 | : InMemoryNode(Stat.getName(), IME_File), Stat(std::move(Stat)), | ||||||||
576 | Buffer(std::move(Buffer)) {} | ||||||||
577 | |||||||||
578 | /// Return the \p Status for this node. \p RequestedName should be the name | ||||||||
579 | /// through which the caller referred to this node. It will override | ||||||||
580 | /// \p Status::Name in the return value, to mimic the behavior of \p RealFile. | ||||||||
581 | Status getStatus(const Twine &RequestedName) const { | ||||||||
582 | return Status::copyWithNewName(Stat, RequestedName); | ||||||||
583 | } | ||||||||
584 | llvm::MemoryBuffer *getBuffer() const { return Buffer.get(); } | ||||||||
585 | |||||||||
586 | std::string toString(unsigned Indent) const override { | ||||||||
587 | return (std::string(Indent, ' ') + Stat.getName() + "\n").str(); | ||||||||
588 | } | ||||||||
589 | |||||||||
590 | static bool classof(const InMemoryNode *N) { | ||||||||
591 | return N->getKind() == IME_File; | ||||||||
592 | } | ||||||||
593 | }; | ||||||||
594 | |||||||||
595 | namespace { | ||||||||
596 | |||||||||
597 | class InMemoryHardLink : public InMemoryNode { | ||||||||
598 | const InMemoryFile &ResolvedFile; | ||||||||
599 | |||||||||
600 | public: | ||||||||
601 | InMemoryHardLink(StringRef Path, const InMemoryFile &ResolvedFile) | ||||||||
602 | : InMemoryNode(Path, IME_HardLink), ResolvedFile(ResolvedFile) {} | ||||||||
603 | const InMemoryFile &getResolvedFile() const { return ResolvedFile; } | ||||||||
604 | |||||||||
605 | std::string toString(unsigned Indent) const override { | ||||||||
606 | return std::string(Indent, ' ') + "HardLink to -> " + | ||||||||
607 | ResolvedFile.toString(0); | ||||||||
608 | } | ||||||||
609 | |||||||||
610 | static bool classof(const InMemoryNode *N) { | ||||||||
611 | return N->getKind() == IME_HardLink; | ||||||||
612 | } | ||||||||
613 | }; | ||||||||
614 | |||||||||
615 | /// Adapt a InMemoryFile for VFS' File interface. The goal is to make | ||||||||
616 | /// \p InMemoryFileAdaptor mimic as much as possible the behavior of | ||||||||
617 | /// \p RealFile. | ||||||||
618 | class InMemoryFileAdaptor : public File { | ||||||||
619 | const InMemoryFile &Node; | ||||||||
620 | /// The name to use when returning a Status for this file. | ||||||||
621 | std::string RequestedName; | ||||||||
622 | |||||||||
623 | public: | ||||||||
624 | explicit InMemoryFileAdaptor(const InMemoryFile &Node, | ||||||||
625 | std::string RequestedName) | ||||||||
626 | : Node(Node), RequestedName(std::move(RequestedName)) {} | ||||||||
627 | |||||||||
628 | llvm::ErrorOr<Status> status() override { | ||||||||
629 | return Node.getStatus(RequestedName); | ||||||||
630 | } | ||||||||
631 | |||||||||
632 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> | ||||||||
633 | getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator, | ||||||||
634 | bool IsVolatile) override { | ||||||||
635 | llvm::MemoryBuffer *Buf = Node.getBuffer(); | ||||||||
636 | return llvm::MemoryBuffer::getMemBuffer( | ||||||||
637 | Buf->getBuffer(), Buf->getBufferIdentifier(), RequiresNullTerminator); | ||||||||
638 | } | ||||||||
639 | |||||||||
640 | std::error_code close() override { return {}; } | ||||||||
641 | }; | ||||||||
642 | } // namespace | ||||||||
643 | |||||||||
644 | class InMemoryDirectory : public InMemoryNode { | ||||||||
645 | Status Stat; | ||||||||
646 | llvm::StringMap<std::unique_ptr<InMemoryNode>> Entries; | ||||||||
647 | |||||||||
648 | public: | ||||||||
649 | InMemoryDirectory(Status Stat) | ||||||||
650 | : InMemoryNode(Stat.getName(), IME_Directory), Stat(std::move(Stat)) {} | ||||||||
651 | |||||||||
652 | /// Return the \p Status for this node. \p RequestedName should be the name | ||||||||
653 | /// through which the caller referred to this node. It will override | ||||||||
654 | /// \p Status::Name in the return value, to mimic the behavior of \p RealFile. | ||||||||
655 | Status getStatus(const Twine &RequestedName) const { | ||||||||
656 | return Status::copyWithNewName(Stat, RequestedName); | ||||||||
657 | } | ||||||||
658 | InMemoryNode *getChild(StringRef Name) { | ||||||||
659 | auto I = Entries.find(Name); | ||||||||
660 | if (I != Entries.end()) | ||||||||
661 | return I->second.get(); | ||||||||
662 | return nullptr; | ||||||||
663 | } | ||||||||
664 | |||||||||
665 | InMemoryNode *addChild(StringRef Name, std::unique_ptr<InMemoryNode> Child) { | ||||||||
666 | return Entries.insert(make_pair(Name, std::move(Child))) | ||||||||
667 | .first->second.get(); | ||||||||
668 | } | ||||||||
669 | |||||||||
670 | using const_iterator = decltype(Entries)::const_iterator; | ||||||||
671 | |||||||||
672 | const_iterator begin() const { return Entries.begin(); } | ||||||||
673 | const_iterator end() const { return Entries.end(); } | ||||||||
674 | |||||||||
675 | std::string toString(unsigned Indent) const override { | ||||||||
676 | std::string Result = | ||||||||
677 | (std::string(Indent, ' ') + Stat.getName() + "\n").str(); | ||||||||
678 | for (const auto &Entry : Entries) | ||||||||
679 | Result += Entry.second->toString(Indent + 2); | ||||||||
680 | return Result; | ||||||||
681 | } | ||||||||
682 | |||||||||
683 | static bool classof(const InMemoryNode *N) { | ||||||||
684 | return N->getKind() == IME_Directory; | ||||||||
685 | } | ||||||||
686 | }; | ||||||||
687 | |||||||||
688 | namespace { | ||||||||
689 | Status getNodeStatus(const InMemoryNode *Node, const Twine &RequestedName) { | ||||||||
690 | if (auto Dir = dyn_cast<detail::InMemoryDirectory>(Node)) | ||||||||
691 | return Dir->getStatus(RequestedName); | ||||||||
692 | if (auto File = dyn_cast<detail::InMemoryFile>(Node)) | ||||||||
693 | return File->getStatus(RequestedName); | ||||||||
694 | if (auto Link = dyn_cast<detail::InMemoryHardLink>(Node)) | ||||||||
695 | return Link->getResolvedFile().getStatus(RequestedName); | ||||||||
696 | llvm_unreachable("Unknown node type")::llvm::llvm_unreachable_internal("Unknown node type", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 696); | ||||||||
697 | } | ||||||||
698 | } // namespace | ||||||||
699 | } // namespace detail | ||||||||
700 | |||||||||
701 | InMemoryFileSystem::InMemoryFileSystem(bool UseNormalizedPaths) | ||||||||
702 | : Root(new detail::InMemoryDirectory( | ||||||||
703 | Status("", getNextVirtualUniqueID(), llvm::sys::TimePoint<>(), 0, 0, | ||||||||
704 | 0, llvm::sys::fs::file_type::directory_file, | ||||||||
705 | llvm::sys::fs::perms::all_all))), | ||||||||
706 | UseNormalizedPaths(UseNormalizedPaths) {} | ||||||||
707 | |||||||||
708 | InMemoryFileSystem::~InMemoryFileSystem() = default; | ||||||||
709 | |||||||||
710 | std::string InMemoryFileSystem::toString() const { | ||||||||
711 | return Root->toString(/*Indent=*/0); | ||||||||
712 | } | ||||||||
713 | |||||||||
714 | bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime, | ||||||||
715 | std::unique_ptr<llvm::MemoryBuffer> Buffer, | ||||||||
716 | Optional<uint32_t> User, | ||||||||
717 | Optional<uint32_t> Group, | ||||||||
718 | Optional<llvm::sys::fs::file_type> Type, | ||||||||
719 | Optional<llvm::sys::fs::perms> Perms, | ||||||||
720 | const detail::InMemoryFile *HardLinkTarget) { | ||||||||
721 | SmallString<128> Path; | ||||||||
722 | P.toVector(Path); | ||||||||
723 | |||||||||
724 | // Fix up relative paths. This just prepends the current working directory. | ||||||||
725 | std::error_code EC = makeAbsolute(Path); | ||||||||
726 | assert(!EC)(static_cast <bool> (!EC) ? void (0) : __assert_fail ("!EC" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 726, __extension__ __PRETTY_FUNCTION__)); | ||||||||
727 | (void)EC; | ||||||||
728 | |||||||||
729 | if (useNormalizedPaths()) | ||||||||
730 | llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true); | ||||||||
731 | |||||||||
732 | if (Path.empty()) | ||||||||
733 | return false; | ||||||||
734 | |||||||||
735 | detail::InMemoryDirectory *Dir = Root.get(); | ||||||||
736 | auto I = llvm::sys::path::begin(Path), E = sys::path::end(Path); | ||||||||
737 | const auto ResolvedUser = User.getValueOr(0); | ||||||||
738 | const auto ResolvedGroup = Group.getValueOr(0); | ||||||||
739 | const auto ResolvedType = Type.getValueOr(sys::fs::file_type::regular_file); | ||||||||
740 | const auto ResolvedPerms = Perms.getValueOr(sys::fs::all_all); | ||||||||
741 | assert(!(HardLinkTarget && Buffer) && "HardLink cannot have a buffer")(static_cast <bool> (!(HardLinkTarget && Buffer ) && "HardLink cannot have a buffer") ? void (0) : __assert_fail ("!(HardLinkTarget && Buffer) && \"HardLink cannot have a buffer\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 741, __extension__ __PRETTY_FUNCTION__)); | ||||||||
742 | // Any intermediate directories we create should be accessible by | ||||||||
743 | // the owner, even if Perms says otherwise for the final path. | ||||||||
744 | const auto NewDirectoryPerms = ResolvedPerms | sys::fs::owner_all; | ||||||||
745 | while (true) { | ||||||||
746 | StringRef Name = *I; | ||||||||
747 | detail::InMemoryNode *Node = Dir->getChild(Name); | ||||||||
748 | ++I; | ||||||||
749 | if (!Node) { | ||||||||
750 | if (I == E) { | ||||||||
751 | // End of the path. | ||||||||
752 | std::unique_ptr<detail::InMemoryNode> Child; | ||||||||
753 | if (HardLinkTarget) | ||||||||
754 | Child.reset(new detail::InMemoryHardLink(P.str(), *HardLinkTarget)); | ||||||||
755 | else { | ||||||||
756 | // Create a new file or directory. | ||||||||
757 | Status Stat(P.str(), getNextVirtualUniqueID(), | ||||||||
758 | llvm::sys::toTimePoint(ModificationTime), ResolvedUser, | ||||||||
759 | ResolvedGroup, Buffer->getBufferSize(), ResolvedType, | ||||||||
760 | ResolvedPerms); | ||||||||
761 | if (ResolvedType == sys::fs::file_type::directory_file) { | ||||||||
762 | Child.reset(new detail::InMemoryDirectory(std::move(Stat))); | ||||||||
763 | } else { | ||||||||
764 | Child.reset( | ||||||||
765 | new detail::InMemoryFile(std::move(Stat), std::move(Buffer))); | ||||||||
766 | } | ||||||||
767 | } | ||||||||
768 | Dir->addChild(Name, std::move(Child)); | ||||||||
769 | return true; | ||||||||
770 | } | ||||||||
771 | |||||||||
772 | // Create a new directory. Use the path up to here. | ||||||||
773 | Status Stat( | ||||||||
774 | StringRef(Path.str().begin(), Name.end() - Path.str().begin()), | ||||||||
775 | getNextVirtualUniqueID(), llvm::sys::toTimePoint(ModificationTime), | ||||||||
776 | ResolvedUser, ResolvedGroup, 0, sys::fs::file_type::directory_file, | ||||||||
777 | NewDirectoryPerms); | ||||||||
778 | Dir = cast<detail::InMemoryDirectory>(Dir->addChild( | ||||||||
779 | Name, std::make_unique<detail::InMemoryDirectory>(std::move(Stat)))); | ||||||||
780 | continue; | ||||||||
781 | } | ||||||||
782 | |||||||||
783 | if (auto *NewDir = dyn_cast<detail::InMemoryDirectory>(Node)) { | ||||||||
784 | Dir = NewDir; | ||||||||
785 | } else { | ||||||||
786 | assert((isa<detail::InMemoryFile>(Node) ||(static_cast <bool> ((isa<detail::InMemoryFile>(Node ) || isa<detail::InMemoryHardLink>(Node)) && "Must be either file, hardlink or directory!" ) ? void (0) : __assert_fail ("(isa<detail::InMemoryFile>(Node) || isa<detail::InMemoryHardLink>(Node)) && \"Must be either file, hardlink or directory!\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 788, __extension__ __PRETTY_FUNCTION__)) | ||||||||
787 | isa<detail::InMemoryHardLink>(Node)) &&(static_cast <bool> ((isa<detail::InMemoryFile>(Node ) || isa<detail::InMemoryHardLink>(Node)) && "Must be either file, hardlink or directory!" ) ? void (0) : __assert_fail ("(isa<detail::InMemoryFile>(Node) || isa<detail::InMemoryHardLink>(Node)) && \"Must be either file, hardlink or directory!\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 788, __extension__ __PRETTY_FUNCTION__)) | ||||||||
788 | "Must be either file, hardlink or directory!")(static_cast <bool> ((isa<detail::InMemoryFile>(Node ) || isa<detail::InMemoryHardLink>(Node)) && "Must be either file, hardlink or directory!" ) ? void (0) : __assert_fail ("(isa<detail::InMemoryFile>(Node) || isa<detail::InMemoryHardLink>(Node)) && \"Must be either file, hardlink or directory!\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 788, __extension__ __PRETTY_FUNCTION__)); | ||||||||
789 | |||||||||
790 | // Trying to insert a directory in place of a file. | ||||||||
791 | if (I != E) | ||||||||
792 | return false; | ||||||||
793 | |||||||||
794 | // Return false only if the new file is different from the existing one. | ||||||||
795 | if (auto Link = dyn_cast<detail::InMemoryHardLink>(Node)) { | ||||||||
796 | return Link->getResolvedFile().getBuffer()->getBuffer() == | ||||||||
797 | Buffer->getBuffer(); | ||||||||
798 | } | ||||||||
799 | return cast<detail::InMemoryFile>(Node)->getBuffer()->getBuffer() == | ||||||||
800 | Buffer->getBuffer(); | ||||||||
801 | } | ||||||||
802 | } | ||||||||
803 | } | ||||||||
804 | |||||||||
805 | bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime, | ||||||||
806 | std::unique_ptr<llvm::MemoryBuffer> Buffer, | ||||||||
807 | Optional<uint32_t> User, | ||||||||
808 | Optional<uint32_t> Group, | ||||||||
809 | Optional<llvm::sys::fs::file_type> Type, | ||||||||
810 | Optional<llvm::sys::fs::perms> Perms) { | ||||||||
811 | return addFile(P, ModificationTime, std::move(Buffer), User, Group, Type, | ||||||||
812 | Perms, /*HardLinkTarget=*/nullptr); | ||||||||
813 | } | ||||||||
814 | |||||||||
815 | bool InMemoryFileSystem::addFileNoOwn(const Twine &P, time_t ModificationTime, | ||||||||
816 | const llvm::MemoryBufferRef &Buffer, | ||||||||
817 | Optional<uint32_t> User, | ||||||||
818 | Optional<uint32_t> Group, | ||||||||
819 | Optional<llvm::sys::fs::file_type> Type, | ||||||||
820 | Optional<llvm::sys::fs::perms> Perms) { | ||||||||
821 | return addFile(P, ModificationTime, llvm::MemoryBuffer::getMemBuffer(Buffer), | ||||||||
822 | std::move(User), std::move(Group), std::move(Type), | ||||||||
823 | std::move(Perms)); | ||||||||
824 | } | ||||||||
825 | |||||||||
826 | static ErrorOr<const detail::InMemoryNode *> | ||||||||
827 | lookupInMemoryNode(const InMemoryFileSystem &FS, detail::InMemoryDirectory *Dir, | ||||||||
828 | const Twine &P) { | ||||||||
829 | SmallString<128> Path; | ||||||||
830 | P.toVector(Path); | ||||||||
831 | |||||||||
832 | // Fix up relative paths. This just prepends the current working directory. | ||||||||
833 | std::error_code EC = FS.makeAbsolute(Path); | ||||||||
834 | assert(!EC)(static_cast <bool> (!EC) ? void (0) : __assert_fail ("!EC" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 834, __extension__ __PRETTY_FUNCTION__)); | ||||||||
835 | (void)EC; | ||||||||
836 | |||||||||
837 | if (FS.useNormalizedPaths()) | ||||||||
838 | llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true); | ||||||||
839 | |||||||||
840 | if (Path.empty()) | ||||||||
841 | return Dir; | ||||||||
842 | |||||||||
843 | auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path); | ||||||||
844 | while (true) { | ||||||||
845 | detail::InMemoryNode *Node = Dir->getChild(*I); | ||||||||
846 | ++I; | ||||||||
847 | if (!Node) | ||||||||
848 | return errc::no_such_file_or_directory; | ||||||||
849 | |||||||||
850 | // Return the file if it's at the end of the path. | ||||||||
851 | if (auto File = dyn_cast<detail::InMemoryFile>(Node)) { | ||||||||
852 | if (I == E) | ||||||||
853 | return File; | ||||||||
854 | return errc::no_such_file_or_directory; | ||||||||
855 | } | ||||||||
856 | |||||||||
857 | // If Node is HardLink then return the resolved file. | ||||||||
858 | if (auto File = dyn_cast<detail::InMemoryHardLink>(Node)) { | ||||||||
859 | if (I == E) | ||||||||
860 | return &File->getResolvedFile(); | ||||||||
861 | return errc::no_such_file_or_directory; | ||||||||
862 | } | ||||||||
863 | // Traverse directories. | ||||||||
864 | Dir = cast<detail::InMemoryDirectory>(Node); | ||||||||
865 | if (I == E) | ||||||||
866 | return Dir; | ||||||||
867 | } | ||||||||
868 | } | ||||||||
869 | |||||||||
870 | bool InMemoryFileSystem::addHardLink(const Twine &FromPath, | ||||||||
871 | const Twine &ToPath) { | ||||||||
872 | auto FromNode = lookupInMemoryNode(*this, Root.get(), FromPath); | ||||||||
873 | auto ToNode = lookupInMemoryNode(*this, Root.get(), ToPath); | ||||||||
874 | // FromPath must not have been added before. ToPath must have been added | ||||||||
875 | // before. Resolved ToPath must be a File. | ||||||||
876 | if (!ToNode || FromNode || !isa<detail::InMemoryFile>(*ToNode)) | ||||||||
877 | return false; | ||||||||
878 | return this->addFile(FromPath, 0, nullptr, None, None, None, None, | ||||||||
879 | cast<detail::InMemoryFile>(*ToNode)); | ||||||||
880 | } | ||||||||
881 | |||||||||
882 | llvm::ErrorOr<Status> InMemoryFileSystem::status(const Twine &Path) { | ||||||||
883 | auto Node = lookupInMemoryNode(*this, Root.get(), Path); | ||||||||
884 | if (Node) | ||||||||
885 | return detail::getNodeStatus(*Node, Path); | ||||||||
886 | return Node.getError(); | ||||||||
887 | } | ||||||||
888 | |||||||||
889 | llvm::ErrorOr<std::unique_ptr<File>> | ||||||||
890 | InMemoryFileSystem::openFileForRead(const Twine &Path) { | ||||||||
891 | auto Node = lookupInMemoryNode(*this, Root.get(), Path); | ||||||||
892 | if (!Node) | ||||||||
893 | return Node.getError(); | ||||||||
894 | |||||||||
895 | // When we have a file provide a heap-allocated wrapper for the memory buffer | ||||||||
896 | // to match the ownership semantics for File. | ||||||||
897 | if (auto *F = dyn_cast<detail::InMemoryFile>(*Node)) | ||||||||
898 | return std::unique_ptr<File>( | ||||||||
899 | new detail::InMemoryFileAdaptor(*F, Path.str())); | ||||||||
900 | |||||||||
901 | // FIXME: errc::not_a_file? | ||||||||
902 | return make_error_code(llvm::errc::invalid_argument); | ||||||||
903 | } | ||||||||
904 | |||||||||
905 | namespace { | ||||||||
906 | |||||||||
907 | /// Adaptor from InMemoryDir::iterator to directory_iterator. | ||||||||
908 | class InMemoryDirIterator : public llvm::vfs::detail::DirIterImpl { | ||||||||
909 | detail::InMemoryDirectory::const_iterator I; | ||||||||
910 | detail::InMemoryDirectory::const_iterator E; | ||||||||
911 | std::string RequestedDirName; | ||||||||
912 | |||||||||
913 | void setCurrentEntry() { | ||||||||
914 | if (I != E) { | ||||||||
915 | SmallString<256> Path(RequestedDirName); | ||||||||
916 | llvm::sys::path::append(Path, I->second->getFileName()); | ||||||||
917 | sys::fs::file_type Type = sys::fs::file_type::type_unknown; | ||||||||
918 | switch (I->second->getKind()) { | ||||||||
919 | case detail::IME_File: | ||||||||
920 | case detail::IME_HardLink: | ||||||||
921 | Type = sys::fs::file_type::regular_file; | ||||||||
922 | break; | ||||||||
923 | case detail::IME_Directory: | ||||||||
924 | Type = sys::fs::file_type::directory_file; | ||||||||
925 | break; | ||||||||
926 | } | ||||||||
927 | CurrentEntry = directory_entry(std::string(Path.str()), Type); | ||||||||
928 | } else { | ||||||||
929 | // When we're at the end, make CurrentEntry invalid and DirIterImpl will | ||||||||
930 | // do the rest. | ||||||||
931 | CurrentEntry = directory_entry(); | ||||||||
932 | } | ||||||||
933 | } | ||||||||
934 | |||||||||
935 | public: | ||||||||
936 | InMemoryDirIterator() = default; | ||||||||
937 | |||||||||
938 | explicit InMemoryDirIterator(const detail::InMemoryDirectory &Dir, | ||||||||
939 | std::string RequestedDirName) | ||||||||
940 | : I(Dir.begin()), E(Dir.end()), | ||||||||
941 | RequestedDirName(std::move(RequestedDirName)) { | ||||||||
942 | setCurrentEntry(); | ||||||||
943 | } | ||||||||
944 | |||||||||
945 | std::error_code increment() override { | ||||||||
946 | ++I; | ||||||||
947 | setCurrentEntry(); | ||||||||
948 | return {}; | ||||||||
949 | } | ||||||||
950 | }; | ||||||||
951 | |||||||||
952 | } // namespace | ||||||||
953 | |||||||||
954 | directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir, | ||||||||
955 | std::error_code &EC) { | ||||||||
956 | auto Node = lookupInMemoryNode(*this, Root.get(), Dir); | ||||||||
957 | if (!Node) { | ||||||||
958 | EC = Node.getError(); | ||||||||
959 | return directory_iterator(std::make_shared<InMemoryDirIterator>()); | ||||||||
960 | } | ||||||||
961 | |||||||||
962 | if (auto *DirNode = dyn_cast<detail::InMemoryDirectory>(*Node)) | ||||||||
963 | return directory_iterator( | ||||||||
964 | std::make_shared<InMemoryDirIterator>(*DirNode, Dir.str())); | ||||||||
965 | |||||||||
966 | EC = make_error_code(llvm::errc::not_a_directory); | ||||||||
967 | return directory_iterator(std::make_shared<InMemoryDirIterator>()); | ||||||||
968 | } | ||||||||
969 | |||||||||
970 | std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) { | ||||||||
971 | SmallString<128> Path; | ||||||||
972 | P.toVector(Path); | ||||||||
973 | |||||||||
974 | // Fix up relative paths. This just prepends the current working directory. | ||||||||
975 | std::error_code EC = makeAbsolute(Path); | ||||||||
976 | assert(!EC)(static_cast <bool> (!EC) ? void (0) : __assert_fail ("!EC" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 976, __extension__ __PRETTY_FUNCTION__)); | ||||||||
977 | (void)EC; | ||||||||
978 | |||||||||
979 | if (useNormalizedPaths()) | ||||||||
980 | llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true); | ||||||||
981 | |||||||||
982 | if (!Path.empty()) | ||||||||
983 | WorkingDirectory = std::string(Path.str()); | ||||||||
984 | return {}; | ||||||||
985 | } | ||||||||
986 | |||||||||
987 | std::error_code | ||||||||
988 | InMemoryFileSystem::getRealPath(const Twine &Path, | ||||||||
989 | SmallVectorImpl<char> &Output) const { | ||||||||
990 | auto CWD = getCurrentWorkingDirectory(); | ||||||||
991 | if (!CWD || CWD->empty()) | ||||||||
992 | return errc::operation_not_permitted; | ||||||||
993 | Path.toVector(Output); | ||||||||
994 | if (auto EC = makeAbsolute(Output)) | ||||||||
995 | return EC; | ||||||||
996 | llvm::sys::path::remove_dots(Output, /*remove_dot_dot=*/true); | ||||||||
997 | return {}; | ||||||||
998 | } | ||||||||
999 | |||||||||
1000 | std::error_code InMemoryFileSystem::isLocal(const Twine &Path, bool &Result) { | ||||||||
1001 | Result = false; | ||||||||
1002 | return {}; | ||||||||
1003 | } | ||||||||
1004 | |||||||||
1005 | } // namespace vfs | ||||||||
1006 | } // namespace llvm | ||||||||
1007 | |||||||||
1008 | //===-----------------------------------------------------------------------===/ | ||||||||
1009 | // RedirectingFileSystem implementation | ||||||||
1010 | //===-----------------------------------------------------------------------===/ | ||||||||
1011 | |||||||||
1012 | namespace { | ||||||||
1013 | |||||||||
1014 | static llvm::sys::path::Style getExistingStyle(llvm::StringRef Path) { | ||||||||
1015 | // Detect the path style in use by checking the first separator. | ||||||||
1016 | llvm::sys::path::Style style = llvm::sys::path::Style::native; | ||||||||
1017 | const size_t n = Path.find_first_of("/\\"); | ||||||||
1018 | if (n != static_cast<size_t>(-1)) | ||||||||
1019 | style = (Path[n] == '/') ? llvm::sys::path::Style::posix | ||||||||
1020 | : llvm::sys::path::Style::windows; | ||||||||
1021 | return style; | ||||||||
1022 | } | ||||||||
1023 | |||||||||
1024 | /// Removes leading "./" as well as path components like ".." and ".". | ||||||||
1025 | static llvm::SmallString<256> canonicalize(llvm::StringRef Path) { | ||||||||
1026 | // First detect the path style in use by checking the first separator. | ||||||||
1027 | llvm::sys::path::Style style = getExistingStyle(Path); | ||||||||
1028 | |||||||||
1029 | // Now remove the dots. Explicitly specifying the path style prevents the | ||||||||
1030 | // direction of the slashes from changing. | ||||||||
1031 | llvm::SmallString<256> result = | ||||||||
1032 | llvm::sys::path::remove_leading_dotslash(Path, style); | ||||||||
1033 | llvm::sys::path::remove_dots(result, /*remove_dot_dot=*/true, style); | ||||||||
1034 | return result; | ||||||||
1035 | } | ||||||||
1036 | |||||||||
1037 | } // anonymous namespace | ||||||||
1038 | |||||||||
1039 | |||||||||
1040 | RedirectingFileSystem::RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> FS) | ||||||||
1041 | : ExternalFS(std::move(FS)) { | ||||||||
1042 | if (ExternalFS) | ||||||||
1043 | if (auto ExternalWorkingDirectory = | ||||||||
1044 | ExternalFS->getCurrentWorkingDirectory()) { | ||||||||
1045 | WorkingDirectory = *ExternalWorkingDirectory; | ||||||||
1046 | } | ||||||||
1047 | } | ||||||||
1048 | |||||||||
1049 | /// Directory iterator implementation for \c RedirectingFileSystem's | ||||||||
1050 | /// directory entries. | ||||||||
1051 | class llvm::vfs::RedirectingFSDirIterImpl | ||||||||
1052 | : public llvm::vfs::detail::DirIterImpl { | ||||||||
1053 | std::string Dir; | ||||||||
1054 | RedirectingFileSystem::DirectoryEntry::iterator Current, End; | ||||||||
1055 | |||||||||
1056 | std::error_code incrementImpl(bool IsFirstTime) { | ||||||||
1057 | assert((IsFirstTime || Current != End) && "cannot iterate past end")(static_cast <bool> ((IsFirstTime || Current != End) && "cannot iterate past end") ? void (0) : __assert_fail ("(IsFirstTime || Current != End) && \"cannot iterate past end\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1057, __extension__ __PRETTY_FUNCTION__)); | ||||||||
1058 | if (!IsFirstTime) | ||||||||
1059 | ++Current; | ||||||||
1060 | if (Current != End) { | ||||||||
1061 | SmallString<128> PathStr(Dir); | ||||||||
1062 | llvm::sys::path::append(PathStr, (*Current)->getName()); | ||||||||
1063 | sys::fs::file_type Type = sys::fs::file_type::type_unknown; | ||||||||
1064 | switch ((*Current)->getKind()) { | ||||||||
1065 | case RedirectingFileSystem::EK_Directory: | ||||||||
1066 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||
1067 | case RedirectingFileSystem::EK_DirectoryRemap: | ||||||||
1068 | Type = sys::fs::file_type::directory_file; | ||||||||
1069 | break; | ||||||||
1070 | case RedirectingFileSystem::EK_File: | ||||||||
1071 | Type = sys::fs::file_type::regular_file; | ||||||||
1072 | break; | ||||||||
1073 | } | ||||||||
1074 | CurrentEntry = directory_entry(std::string(PathStr.str()), Type); | ||||||||
1075 | } else { | ||||||||
1076 | CurrentEntry = directory_entry(); | ||||||||
1077 | } | ||||||||
1078 | return {}; | ||||||||
1079 | }; | ||||||||
1080 | |||||||||
1081 | public: | ||||||||
1082 | RedirectingFSDirIterImpl( | ||||||||
1083 | const Twine &Path, RedirectingFileSystem::DirectoryEntry::iterator Begin, | ||||||||
1084 | RedirectingFileSystem::DirectoryEntry::iterator End, std::error_code &EC) | ||||||||
1085 | : Dir(Path.str()), Current(Begin), End(End) { | ||||||||
1086 | EC = incrementImpl(/*IsFirstTime=*/true); | ||||||||
1087 | } | ||||||||
1088 | |||||||||
1089 | std::error_code increment() override { | ||||||||
1090 | return incrementImpl(/*IsFirstTime=*/false); | ||||||||
1091 | } | ||||||||
1092 | }; | ||||||||
1093 | |||||||||
1094 | /// Directory iterator implementation for \c RedirectingFileSystem's | ||||||||
1095 | /// directory remap entries that maps the paths reported by the external | ||||||||
1096 | /// file system's directory iterator back to the virtual directory's path. | ||||||||
1097 | class RedirectingFSDirRemapIterImpl : public llvm::vfs::detail::DirIterImpl { | ||||||||
1098 | std::string Dir; | ||||||||
1099 | llvm::sys::path::Style DirStyle; | ||||||||
1100 | llvm::vfs::directory_iterator ExternalIter; | ||||||||
1101 | |||||||||
1102 | public: | ||||||||
1103 | RedirectingFSDirRemapIterImpl(std::string DirPath, | ||||||||
1104 | llvm::vfs::directory_iterator ExtIter) | ||||||||
1105 | : Dir(std::move(DirPath)), DirStyle(getExistingStyle(Dir)), | ||||||||
1106 | ExternalIter(ExtIter) { | ||||||||
1107 | if (ExternalIter != llvm::vfs::directory_iterator()) | ||||||||
1108 | setCurrentEntry(); | ||||||||
1109 | } | ||||||||
1110 | |||||||||
1111 | void setCurrentEntry() { | ||||||||
1112 | StringRef ExternalPath = ExternalIter->path(); | ||||||||
1113 | llvm::sys::path::Style ExternalStyle = getExistingStyle(ExternalPath); | ||||||||
1114 | StringRef File = llvm::sys::path::filename(ExternalPath, ExternalStyle); | ||||||||
1115 | |||||||||
1116 | SmallString<128> NewPath(Dir); | ||||||||
1117 | llvm::sys::path::append(NewPath, DirStyle, File); | ||||||||
1118 | |||||||||
1119 | CurrentEntry = directory_entry(std::string(NewPath), ExternalIter->type()); | ||||||||
1120 | } | ||||||||
1121 | |||||||||
1122 | std::error_code increment() override { | ||||||||
1123 | std::error_code EC; | ||||||||
1124 | ExternalIter.increment(EC); | ||||||||
1125 | if (!EC && ExternalIter != llvm::vfs::directory_iterator()) | ||||||||
1126 | setCurrentEntry(); | ||||||||
1127 | else | ||||||||
1128 | CurrentEntry = directory_entry(); | ||||||||
1129 | return EC; | ||||||||
1130 | } | ||||||||
1131 | }; | ||||||||
1132 | |||||||||
1133 | llvm::ErrorOr<std::string> | ||||||||
1134 | RedirectingFileSystem::getCurrentWorkingDirectory() const { | ||||||||
1135 | return WorkingDirectory; | ||||||||
1136 | } | ||||||||
1137 | |||||||||
1138 | std::error_code | ||||||||
1139 | RedirectingFileSystem::setCurrentWorkingDirectory(const Twine &Path) { | ||||||||
1140 | // Don't change the working directory if the path doesn't exist. | ||||||||
1141 | if (!exists(Path)) | ||||||||
1142 | return errc::no_such_file_or_directory; | ||||||||
1143 | |||||||||
1144 | SmallString<128> AbsolutePath; | ||||||||
1145 | Path.toVector(AbsolutePath); | ||||||||
1146 | if (std::error_code EC = makeAbsolute(AbsolutePath)) | ||||||||
1147 | return EC; | ||||||||
1148 | WorkingDirectory = std::string(AbsolutePath.str()); | ||||||||
1149 | return {}; | ||||||||
1150 | } | ||||||||
1151 | |||||||||
1152 | std::error_code RedirectingFileSystem::isLocal(const Twine &Path_, | ||||||||
1153 | bool &Result) { | ||||||||
1154 | SmallString<256> Path; | ||||||||
1155 | Path_.toVector(Path); | ||||||||
1156 | |||||||||
1157 | if (std::error_code EC = makeCanonical(Path)) | ||||||||
1158 | return {}; | ||||||||
1159 | |||||||||
1160 | return ExternalFS->isLocal(Path, Result); | ||||||||
1161 | } | ||||||||
1162 | |||||||||
1163 | std::error_code RedirectingFileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const { | ||||||||
1164 | if (llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::posix) || | ||||||||
1165 | llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::windows)) | ||||||||
1166 | return {}; | ||||||||
1167 | |||||||||
1168 | auto WorkingDir = getCurrentWorkingDirectory(); | ||||||||
1169 | if (!WorkingDir) | ||||||||
1170 | return WorkingDir.getError(); | ||||||||
1171 | |||||||||
1172 | // We can't use sys::fs::make_absolute because that assumes the path style | ||||||||
1173 | // is native and there is no way to override that. Since we know WorkingDir | ||||||||
1174 | // is absolute, we can use it to determine which style we actually have and | ||||||||
1175 | // append Path ourselves. | ||||||||
1176 | sys::path::Style style = sys::path::Style::windows; | ||||||||
1177 | if (sys::path::is_absolute(WorkingDir.get(), sys::path::Style::posix)) { | ||||||||
1178 | style = sys::path::Style::posix; | ||||||||
1179 | } | ||||||||
1180 | |||||||||
1181 | std::string Result = WorkingDir.get(); | ||||||||
1182 | StringRef Dir(Result); | ||||||||
1183 | if (!Dir.endswith(sys::path::get_separator(style))) { | ||||||||
1184 | Result += sys::path::get_separator(style); | ||||||||
1185 | } | ||||||||
1186 | Result.append(Path.data(), Path.size()); | ||||||||
1187 | Path.assign(Result.begin(), Result.end()); | ||||||||
1188 | |||||||||
1189 | return {}; | ||||||||
1190 | } | ||||||||
1191 | |||||||||
1192 | directory_iterator RedirectingFileSystem::dir_begin(const Twine &Dir, | ||||||||
1193 | std::error_code &EC) { | ||||||||
1194 | SmallString<256> Path; | ||||||||
1195 | Dir.toVector(Path); | ||||||||
1196 | |||||||||
1197 | EC = makeCanonical(Path); | ||||||||
1198 | if (EC) | ||||||||
1199 | return {}; | ||||||||
1200 | |||||||||
1201 | ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path); | ||||||||
1202 | if (!Result) { | ||||||||
1203 | EC = Result.getError(); | ||||||||
1204 | if (shouldFallBackToExternalFS(EC)) | ||||||||
1205 | return ExternalFS->dir_begin(Path, EC); | ||||||||
1206 | return {}; | ||||||||
1207 | } | ||||||||
1208 | |||||||||
1209 | // Use status to make sure the path exists and refers to a directory. | ||||||||
1210 | ErrorOr<Status> S = status(Path, *Result); | ||||||||
1211 | if (!S) { | ||||||||
1212 | if (shouldFallBackToExternalFS(S.getError(), Result->E)) | ||||||||
1213 | return ExternalFS->dir_begin(Dir, EC); | ||||||||
1214 | EC = S.getError(); | ||||||||
1215 | return {}; | ||||||||
1216 | } | ||||||||
1217 | if (!S->isDirectory()) { | ||||||||
1218 | EC = std::error_code(static_cast<int>(errc::not_a_directory), | ||||||||
1219 | std::system_category()); | ||||||||
1220 | return {}; | ||||||||
1221 | } | ||||||||
1222 | |||||||||
1223 | // Create the appropriate directory iterator based on whether we found a | ||||||||
1224 | // DirectoryRemapEntry or DirectoryEntry. | ||||||||
1225 | directory_iterator DirIter; | ||||||||
1226 | if (auto ExtRedirect = Result->getExternalRedirect()) { | ||||||||
1227 | auto RE = cast<RedirectingFileSystem::RemapEntry>(Result->E); | ||||||||
1228 | DirIter = ExternalFS->dir_begin(*ExtRedirect, EC); | ||||||||
1229 | |||||||||
1230 | if (!RE->useExternalName(UseExternalNames)) { | ||||||||
1231 | // Update the paths in the results to use the virtual directory's path. | ||||||||
1232 | DirIter = | ||||||||
1233 | directory_iterator(std::make_shared<RedirectingFSDirRemapIterImpl>( | ||||||||
1234 | std::string(Path), DirIter)); | ||||||||
1235 | } | ||||||||
1236 | } else { | ||||||||
1237 | auto DE = cast<DirectoryEntry>(Result->E); | ||||||||
1238 | DirIter = directory_iterator(std::make_shared<RedirectingFSDirIterImpl>( | ||||||||
1239 | Path, DE->contents_begin(), DE->contents_end(), EC)); | ||||||||
1240 | } | ||||||||
1241 | |||||||||
1242 | if (!shouldUseExternalFS()) | ||||||||
1243 | return DirIter; | ||||||||
1244 | return directory_iterator(std::make_shared<CombiningDirIterImpl>( | ||||||||
1245 | DirIter, ExternalFS, std::string(Path), EC)); | ||||||||
1246 | } | ||||||||
1247 | |||||||||
1248 | void RedirectingFileSystem::setExternalContentsPrefixDir(StringRef PrefixDir) { | ||||||||
1249 | ExternalContentsPrefixDir = PrefixDir.str(); | ||||||||
1250 | } | ||||||||
1251 | |||||||||
1252 | StringRef RedirectingFileSystem::getExternalContentsPrefixDir() const { | ||||||||
1253 | return ExternalContentsPrefixDir; | ||||||||
1254 | } | ||||||||
1255 | |||||||||
1256 | void RedirectingFileSystem::setFallthrough(bool Fallthrough) { | ||||||||
1257 | IsFallthrough = Fallthrough; | ||||||||
1258 | } | ||||||||
1259 | |||||||||
1260 | std::vector<StringRef> RedirectingFileSystem::getRoots() const { | ||||||||
1261 | std::vector<StringRef> R; | ||||||||
1262 | for (const auto &Root : Roots) | ||||||||
1263 | R.push_back(Root->getName()); | ||||||||
1264 | return R; | ||||||||
1265 | } | ||||||||
1266 | |||||||||
1267 | void RedirectingFileSystem::dump(raw_ostream &OS) const { | ||||||||
1268 | for (const auto &Root : Roots) | ||||||||
1269 | dumpEntry(OS, Root.get()); | ||||||||
1270 | } | ||||||||
1271 | |||||||||
1272 | void RedirectingFileSystem::dumpEntry(raw_ostream &OS, | ||||||||
1273 | RedirectingFileSystem::Entry *E, | ||||||||
1274 | int NumSpaces) const { | ||||||||
1275 | StringRef Name = E->getName(); | ||||||||
1276 | for (int i = 0, e = NumSpaces; i < e; ++i) | ||||||||
1277 | OS << " "; | ||||||||
1278 | OS << "'" << Name.str().c_str() << "'" | ||||||||
1279 | << "\n"; | ||||||||
1280 | |||||||||
1281 | if (E->getKind() == RedirectingFileSystem::EK_Directory) { | ||||||||
1282 | auto *DE = dyn_cast<RedirectingFileSystem::DirectoryEntry>(E); | ||||||||
1283 | assert(DE && "Should be a directory")(static_cast <bool> (DE && "Should be a directory" ) ? void (0) : __assert_fail ("DE && \"Should be a directory\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1283, __extension__ __PRETTY_FUNCTION__)); | ||||||||
1284 | |||||||||
1285 | for (std::unique_ptr<Entry> &SubEntry : | ||||||||
1286 | llvm::make_range(DE->contents_begin(), DE->contents_end())) | ||||||||
1287 | dumpEntry(OS, SubEntry.get(), NumSpaces + 2); | ||||||||
1288 | } | ||||||||
1289 | } | ||||||||
1290 | |||||||||
1291 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) | ||||||||
1292 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RedirectingFileSystem::dump() const { dump(dbgs()); } | ||||||||
1293 | #endif | ||||||||
1294 | |||||||||
1295 | /// A helper class to hold the common YAML parsing state. | ||||||||
1296 | class llvm::vfs::RedirectingFileSystemParser { | ||||||||
1297 | yaml::Stream &Stream; | ||||||||
1298 | |||||||||
1299 | void error(yaml::Node *N, const Twine &Msg) { Stream.printError(N, Msg); } | ||||||||
1300 | |||||||||
1301 | // false on error | ||||||||
1302 | bool parseScalarString(yaml::Node *N, StringRef &Result, | ||||||||
1303 | SmallVectorImpl<char> &Storage) { | ||||||||
1304 | const auto *S = dyn_cast<yaml::ScalarNode>(N); | ||||||||
1305 | |||||||||
1306 | if (!S
| ||||||||
1307 | error(N, "expected string"); | ||||||||
1308 | return false; | ||||||||
1309 | } | ||||||||
1310 | Result = S->getValue(Storage); | ||||||||
1311 | return true; | ||||||||
1312 | } | ||||||||
1313 | |||||||||
1314 | // false on error | ||||||||
1315 | bool parseScalarBool(yaml::Node *N, bool &Result) { | ||||||||
1316 | SmallString<5> Storage; | ||||||||
1317 | StringRef Value; | ||||||||
1318 | if (!parseScalarString(N, Value, Storage)) | ||||||||
1319 | return false; | ||||||||
1320 | |||||||||
1321 | if (Value.equals_insensitive("true") || Value.equals_insensitive("on") || | ||||||||
1322 | Value.equals_insensitive("yes") || Value == "1") { | ||||||||
1323 | Result = true; | ||||||||
1324 | return true; | ||||||||
1325 | } else if (Value.equals_insensitive("false") || | ||||||||
1326 | Value.equals_insensitive("off") || | ||||||||
1327 | Value.equals_insensitive("no") || Value == "0") { | ||||||||
1328 | Result = false; | ||||||||
1329 | return true; | ||||||||
1330 | } | ||||||||
1331 | |||||||||
1332 | error(N, "expected boolean value"); | ||||||||
1333 | return false; | ||||||||
1334 | } | ||||||||
1335 | |||||||||
1336 | struct KeyStatus { | ||||||||
1337 | bool Required; | ||||||||
1338 | bool Seen = false; | ||||||||
1339 | |||||||||
1340 | KeyStatus(bool Required = false) : Required(Required) {} | ||||||||
1341 | }; | ||||||||
1342 | |||||||||
1343 | using KeyStatusPair = std::pair<StringRef, KeyStatus>; | ||||||||
1344 | |||||||||
1345 | // false on error | ||||||||
1346 | bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key, | ||||||||
1347 | DenseMap<StringRef, KeyStatus> &Keys) { | ||||||||
1348 | if (!Keys.count(Key)) { | ||||||||
1349 | error(KeyNode, "unknown key"); | ||||||||
1350 | return false; | ||||||||
1351 | } | ||||||||
1352 | KeyStatus &S = Keys[Key]; | ||||||||
1353 | if (S.Seen) { | ||||||||
1354 | error(KeyNode, Twine("duplicate key '") + Key + "'"); | ||||||||
1355 | return false; | ||||||||
1356 | } | ||||||||
1357 | S.Seen = true; | ||||||||
1358 | return true; | ||||||||
1359 | } | ||||||||
1360 | |||||||||
1361 | // false on error | ||||||||
1362 | bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) { | ||||||||
1363 | for (const auto &I : Keys) { | ||||||||
1364 | if (I.second.Required && !I.second.Seen) { | ||||||||
1365 | error(Obj, Twine("missing key '") + I.first + "'"); | ||||||||
1366 | return false; | ||||||||
1367 | } | ||||||||
1368 | } | ||||||||
1369 | return true; | ||||||||
1370 | } | ||||||||
1371 | |||||||||
1372 | public: | ||||||||
1373 | static RedirectingFileSystem::Entry * | ||||||||
1374 | lookupOrCreateEntry(RedirectingFileSystem *FS, StringRef Name, | ||||||||
1375 | RedirectingFileSystem::Entry *ParentEntry = nullptr) { | ||||||||
1376 | if (!ParentEntry) { // Look for a existent root | ||||||||
1377 | for (const auto &Root : FS->Roots) { | ||||||||
1378 | if (Name.equals(Root->getName())) { | ||||||||
1379 | ParentEntry = Root.get(); | ||||||||
1380 | return ParentEntry; | ||||||||
1381 | } | ||||||||
1382 | } | ||||||||
1383 | } else { // Advance to the next component | ||||||||
1384 | auto *DE = dyn_cast<RedirectingFileSystem::DirectoryEntry>(ParentEntry); | ||||||||
1385 | for (std::unique_ptr<RedirectingFileSystem::Entry> &Content : | ||||||||
1386 | llvm::make_range(DE->contents_begin(), DE->contents_end())) { | ||||||||
1387 | auto *DirContent = | ||||||||
1388 | dyn_cast<RedirectingFileSystem::DirectoryEntry>(Content.get()); | ||||||||
1389 | if (DirContent && Name.equals(Content->getName())) | ||||||||
1390 | return DirContent; | ||||||||
1391 | } | ||||||||
1392 | } | ||||||||
1393 | |||||||||
1394 | // ... or create a new one | ||||||||
1395 | std::unique_ptr<RedirectingFileSystem::Entry> E = | ||||||||
1396 | std::make_unique<RedirectingFileSystem::DirectoryEntry>( | ||||||||
1397 | Name, Status("", getNextVirtualUniqueID(), | ||||||||
1398 | std::chrono::system_clock::now(), 0, 0, 0, | ||||||||
1399 | file_type::directory_file, sys::fs::all_all)); | ||||||||
1400 | |||||||||
1401 | if (!ParentEntry) { // Add a new root to the overlay | ||||||||
1402 | FS->Roots.push_back(std::move(E)); | ||||||||
1403 | ParentEntry = FS->Roots.back().get(); | ||||||||
1404 | return ParentEntry; | ||||||||
1405 | } | ||||||||
1406 | |||||||||
1407 | auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(ParentEntry); | ||||||||
1408 | DE->addContent(std::move(E)); | ||||||||
1409 | return DE->getLastContent(); | ||||||||
1410 | } | ||||||||
1411 | |||||||||
1412 | private: | ||||||||
1413 | void uniqueOverlayTree(RedirectingFileSystem *FS, | ||||||||
1414 | RedirectingFileSystem::Entry *SrcE, | ||||||||
1415 | RedirectingFileSystem::Entry *NewParentE = nullptr) { | ||||||||
1416 | StringRef Name = SrcE->getName(); | ||||||||
1417 | switch (SrcE->getKind()) { | ||||||||
1418 | case RedirectingFileSystem::EK_Directory: { | ||||||||
1419 | auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(SrcE); | ||||||||
1420 | // Empty directories could be present in the YAML as a way to | ||||||||
1421 | // describe a file for a current directory after some of its subdir | ||||||||
1422 | // is parsed. This only leads to redundant walks, ignore it. | ||||||||
1423 | if (!Name.empty()) | ||||||||
1424 | NewParentE = lookupOrCreateEntry(FS, Name, NewParentE); | ||||||||
1425 | for (std::unique_ptr<RedirectingFileSystem::Entry> &SubEntry : | ||||||||
1426 | llvm::make_range(DE->contents_begin(), DE->contents_end())) | ||||||||
1427 | uniqueOverlayTree(FS, SubEntry.get(), NewParentE); | ||||||||
1428 | break; | ||||||||
1429 | } | ||||||||
1430 | case RedirectingFileSystem::EK_DirectoryRemap: { | ||||||||
1431 | assert(NewParentE && "Parent entry must exist")(static_cast <bool> (NewParentE && "Parent entry must exist" ) ? void (0) : __assert_fail ("NewParentE && \"Parent entry must exist\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1431, __extension__ __PRETTY_FUNCTION__)); | ||||||||
1432 | auto *DR = cast<RedirectingFileSystem::DirectoryRemapEntry>(SrcE); | ||||||||
1433 | auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(NewParentE); | ||||||||
1434 | DE->addContent( | ||||||||
1435 | std::make_unique<RedirectingFileSystem::DirectoryRemapEntry>( | ||||||||
1436 | Name, DR->getExternalContentsPath(), DR->getUseName())); | ||||||||
1437 | break; | ||||||||
1438 | } | ||||||||
1439 | case RedirectingFileSystem::EK_File: { | ||||||||
1440 | assert(NewParentE && "Parent entry must exist")(static_cast <bool> (NewParentE && "Parent entry must exist" ) ? void (0) : __assert_fail ("NewParentE && \"Parent entry must exist\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1440, __extension__ __PRETTY_FUNCTION__)); | ||||||||
1441 | auto *FE = cast<RedirectingFileSystem::FileEntry>(SrcE); | ||||||||
1442 | auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(NewParentE); | ||||||||
1443 | DE->addContent(std::make_unique<RedirectingFileSystem::FileEntry>( | ||||||||
1444 | Name, FE->getExternalContentsPath(), FE->getUseName())); | ||||||||
1445 | break; | ||||||||
1446 | } | ||||||||
1447 | } | ||||||||
1448 | } | ||||||||
1449 | |||||||||
1450 | std::unique_ptr<RedirectingFileSystem::Entry> | ||||||||
1451 | parseEntry(yaml::Node *N, RedirectingFileSystem *FS, bool IsRootEntry) { | ||||||||
1452 | auto *M = dyn_cast<yaml::MappingNode>(N); | ||||||||
1453 | if (!M
| ||||||||
1454 | error(N, "expected mapping node for file or directory entry"); | ||||||||
1455 | return nullptr; | ||||||||
1456 | } | ||||||||
1457 | |||||||||
1458 | KeyStatusPair Fields[] = { | ||||||||
1459 | KeyStatusPair("name", true), | ||||||||
1460 | KeyStatusPair("type", true), | ||||||||
1461 | KeyStatusPair("contents", false), | ||||||||
1462 | KeyStatusPair("external-contents", false), | ||||||||
1463 | KeyStatusPair("use-external-name", false), | ||||||||
1464 | }; | ||||||||
1465 | |||||||||
1466 | DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields)); | ||||||||
1467 | |||||||||
1468 | enum { CF_NotSet, CF_List, CF_External } ContentsField = CF_NotSet; | ||||||||
1469 | std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> | ||||||||
1470 | EntryArrayContents; | ||||||||
1471 | SmallString<256> ExternalContentsPath; | ||||||||
1472 | SmallString<256> Name; | ||||||||
1473 | yaml::Node *NameValueNode = nullptr; | ||||||||
1474 | auto UseExternalName = RedirectingFileSystem::NK_NotSet; | ||||||||
1475 | RedirectingFileSystem::EntryKind Kind; | ||||||||
1476 | |||||||||
1477 | for (auto &I : *M) { | ||||||||
1478 | StringRef Key; | ||||||||
1479 | // Reuse the buffer for key and value, since we don't look at key after | ||||||||
1480 | // parsing value. | ||||||||
1481 | SmallString<256> Buffer; | ||||||||
1482 | if (!parseScalarString(I.getKey(), Key, Buffer)) | ||||||||
1483 | return nullptr; | ||||||||
1484 | |||||||||
1485 | if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys)) | ||||||||
1486 | return nullptr; | ||||||||
1487 | |||||||||
1488 | StringRef Value; | ||||||||
1489 | if (Key == "name") { | ||||||||
1490 | if (!parseScalarString(I.getValue(), Value, Buffer)) | ||||||||
1491 | return nullptr; | ||||||||
1492 | |||||||||
1493 | NameValueNode = I.getValue(); | ||||||||
1494 | // Guarantee that old YAML files containing paths with ".." and "." | ||||||||
1495 | // are properly canonicalized before read into the VFS. | ||||||||
1496 | Name = canonicalize(Value).str(); | ||||||||
1497 | } else if (Key == "type") { | ||||||||
1498 | if (!parseScalarString(I.getValue(), Value, Buffer)) | ||||||||
1499 | return nullptr; | ||||||||
1500 | if (Value == "file") | ||||||||
1501 | Kind = RedirectingFileSystem::EK_File; | ||||||||
1502 | else if (Value == "directory") | ||||||||
1503 | Kind = RedirectingFileSystem::EK_Directory; | ||||||||
1504 | else if (Value == "directory-remap") | ||||||||
1505 | Kind = RedirectingFileSystem::EK_DirectoryRemap; | ||||||||
1506 | else { | ||||||||
1507 | error(I.getValue(), "unknown value for 'type'"); | ||||||||
1508 | return nullptr; | ||||||||
1509 | } | ||||||||
1510 | } else if (Key == "contents") { | ||||||||
1511 | if (ContentsField != CF_NotSet) { | ||||||||
1512 | error(I.getKey(), | ||||||||
1513 | "entry already has 'contents' or 'external-contents'"); | ||||||||
1514 | return nullptr; | ||||||||
1515 | } | ||||||||
1516 | ContentsField = CF_List; | ||||||||
1517 | auto *Contents = dyn_cast<yaml::SequenceNode>(I.getValue()); | ||||||||
1518 | if (!Contents) { | ||||||||
1519 | // FIXME: this is only for directories, what about files? | ||||||||
1520 | error(I.getValue(), "expected array"); | ||||||||
1521 | return nullptr; | ||||||||
1522 | } | ||||||||
1523 | |||||||||
1524 | for (auto &I : *Contents) { | ||||||||
1525 | if (std::unique_ptr<RedirectingFileSystem::Entry> E = | ||||||||
1526 | parseEntry(&I, FS, /*IsRootEntry*/ false)) | ||||||||
1527 | EntryArrayContents.push_back(std::move(E)); | ||||||||
1528 | else | ||||||||
1529 | return nullptr; | ||||||||
1530 | } | ||||||||
1531 | } else if (Key == "external-contents") { | ||||||||
1532 | if (ContentsField
| ||||||||
1533 | error(I.getKey(), | ||||||||
1534 | "entry already has 'contents' or 'external-contents'"); | ||||||||
1535 | return nullptr; | ||||||||
1536 | } | ||||||||
1537 | ContentsField = CF_External; | ||||||||
1538 | if (!parseScalarString(I.getValue(), Value, Buffer)) | ||||||||
1539 | return nullptr; | ||||||||
1540 | |||||||||
1541 | SmallString<256> FullPath; | ||||||||
1542 | if (FS->IsRelativeOverlay
| ||||||||
1543 | FullPath = FS->getExternalContentsPrefixDir(); | ||||||||
1544 | assert(!FullPath.empty() &&(static_cast <bool> (!FullPath.empty() && "External contents prefix directory must exist" ) ? void (0) : __assert_fail ("!FullPath.empty() && \"External contents prefix directory must exist\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1545, __extension__ __PRETTY_FUNCTION__)) | ||||||||
1545 | "External contents prefix directory must exist")(static_cast <bool> (!FullPath.empty() && "External contents prefix directory must exist" ) ? void (0) : __assert_fail ("!FullPath.empty() && \"External contents prefix directory must exist\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1545, __extension__ __PRETTY_FUNCTION__)); | ||||||||
1546 | llvm::sys::path::append(FullPath, Value); | ||||||||
1547 | } else { | ||||||||
1548 | FullPath = Value; | ||||||||
1549 | } | ||||||||
1550 | |||||||||
1551 | // Guarantee that old YAML files containing paths with ".." and "." | ||||||||
1552 | // are properly canonicalized before read into the VFS. | ||||||||
1553 | FullPath = canonicalize(FullPath); | ||||||||
1554 | ExternalContentsPath = FullPath.str(); | ||||||||
1555 | } else if (Key == "use-external-name") { | ||||||||
1556 | bool Val; | ||||||||
1557 | if (!parseScalarBool(I.getValue(), Val)) | ||||||||
1558 | return nullptr; | ||||||||
1559 | UseExternalName = Val ? RedirectingFileSystem::NK_External | ||||||||
1560 | : RedirectingFileSystem::NK_Virtual; | ||||||||
1561 | } else { | ||||||||
1562 | llvm_unreachable("key missing from Keys")::llvm::llvm_unreachable_internal("key missing from Keys", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1562); | ||||||||
1563 | } | ||||||||
1564 | } | ||||||||
1565 | |||||||||
1566 | if (Stream.failed()) | ||||||||
1567 | return nullptr; | ||||||||
1568 | |||||||||
1569 | // check for missing keys | ||||||||
1570 | if (ContentsField
| ||||||||
1571 | error(N, "missing key 'contents' or 'external-contents'"); | ||||||||
1572 | return nullptr; | ||||||||
1573 | } | ||||||||
1574 | if (!checkMissingKeys(N, Keys)) | ||||||||
1575 | return nullptr; | ||||||||
1576 | |||||||||
1577 | // check invalid configuration | ||||||||
1578 | if (Kind == RedirectingFileSystem::EK_Directory && | ||||||||
| |||||||||
1579 | UseExternalName != RedirectingFileSystem::NK_NotSet) { | ||||||||
1580 | error(N, "'use-external-name' is not supported for 'directory' entries"); | ||||||||
1581 | return nullptr; | ||||||||
1582 | } | ||||||||
1583 | |||||||||
1584 | if (Kind == RedirectingFileSystem::EK_DirectoryRemap && | ||||||||
1585 | ContentsField == CF_List) { | ||||||||
1586 | error(N, "'contents' is not supported for 'directory-remap' entries"); | ||||||||
1587 | return nullptr; | ||||||||
1588 | } | ||||||||
1589 | |||||||||
1590 | sys::path::Style path_style = sys::path::Style::native; | ||||||||
1591 | if (IsRootEntry) { | ||||||||
1592 | // VFS root entries may be in either Posix or Windows style. Figure out | ||||||||
1593 | // which style we have, and use it consistently. | ||||||||
1594 | if (sys::path::is_absolute(Name, sys::path::Style::posix)) { | ||||||||
1595 | path_style = sys::path::Style::posix; | ||||||||
1596 | } else if (sys::path::is_absolute(Name, sys::path::Style::windows)) { | ||||||||
1597 | path_style = sys::path::Style::windows; | ||||||||
1598 | } else { | ||||||||
1599 | assert(NameValueNode && "Name presence should be checked earlier")(static_cast <bool> (NameValueNode && "Name presence should be checked earlier" ) ? void (0) : __assert_fail ("NameValueNode && \"Name presence should be checked earlier\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1599, __extension__ __PRETTY_FUNCTION__)); | ||||||||
1600 | error(NameValueNode, | ||||||||
1601 | "entry with relative path at the root level is not discoverable"); | ||||||||
1602 | return nullptr; | ||||||||
1603 | } | ||||||||
1604 | } | ||||||||
1605 | |||||||||
1606 | // Remove trailing slash(es), being careful not to remove the root path | ||||||||
1607 | StringRef Trimmed = Name; | ||||||||
1608 | size_t RootPathLen = sys::path::root_path(Trimmed, path_style).size(); | ||||||||
1609 | while (Trimmed.size() > RootPathLen && | ||||||||
1610 | sys::path::is_separator(Trimmed.back(), path_style)) | ||||||||
1611 | Trimmed = Trimmed.slice(0, Trimmed.size() - 1); | ||||||||
1612 | |||||||||
1613 | // Get the last component | ||||||||
1614 | StringRef LastComponent = sys::path::filename(Trimmed, path_style); | ||||||||
1615 | |||||||||
1616 | std::unique_ptr<RedirectingFileSystem::Entry> Result; | ||||||||
1617 | switch (Kind) { | ||||||||
1618 | case RedirectingFileSystem::EK_File: | ||||||||
1619 | Result = std::make_unique<RedirectingFileSystem::FileEntry>( | ||||||||
1620 | LastComponent, std::move(ExternalContentsPath), UseExternalName); | ||||||||
1621 | break; | ||||||||
1622 | case RedirectingFileSystem::EK_DirectoryRemap: | ||||||||
1623 | Result = std::make_unique<RedirectingFileSystem::DirectoryRemapEntry>( | ||||||||
1624 | LastComponent, std::move(ExternalContentsPath), UseExternalName); | ||||||||
1625 | break; | ||||||||
1626 | case RedirectingFileSystem::EK_Directory: | ||||||||
1627 | Result = std::make_unique<RedirectingFileSystem::DirectoryEntry>( | ||||||||
1628 | LastComponent, std::move(EntryArrayContents), | ||||||||
1629 | Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(), | ||||||||
1630 | 0, 0, 0, file_type::directory_file, sys::fs::all_all)); | ||||||||
1631 | break; | ||||||||
1632 | } | ||||||||
1633 | |||||||||
1634 | StringRef Parent = sys::path::parent_path(Trimmed, path_style); | ||||||||
1635 | if (Parent.empty()) | ||||||||
1636 | return Result; | ||||||||
1637 | |||||||||
1638 | // if 'name' contains multiple components, create implicit directory entries | ||||||||
1639 | for (sys::path::reverse_iterator I = sys::path::rbegin(Parent, path_style), | ||||||||
1640 | E = sys::path::rend(Parent); | ||||||||
1641 | I != E; ++I) { | ||||||||
1642 | std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> Entries; | ||||||||
1643 | Entries.push_back(std::move(Result)); | ||||||||
1644 | Result = std::make_unique<RedirectingFileSystem::DirectoryEntry>( | ||||||||
1645 | *I, std::move(Entries), | ||||||||
1646 | Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(), | ||||||||
1647 | 0, 0, 0, file_type::directory_file, sys::fs::all_all)); | ||||||||
1648 | } | ||||||||
1649 | return Result; | ||||||||
1650 | } | ||||||||
1651 | |||||||||
1652 | public: | ||||||||
1653 | RedirectingFileSystemParser(yaml::Stream &S) : Stream(S) {} | ||||||||
1654 | |||||||||
1655 | // false on error | ||||||||
1656 | bool parse(yaml::Node *Root, RedirectingFileSystem *FS) { | ||||||||
1657 | auto *Top = dyn_cast<yaml::MappingNode>(Root); | ||||||||
1658 | if (!Top
| ||||||||
1659 | error(Root, "expected mapping node"); | ||||||||
1660 | return false; | ||||||||
1661 | } | ||||||||
1662 | |||||||||
1663 | KeyStatusPair Fields[] = { | ||||||||
1664 | KeyStatusPair("version", true), | ||||||||
1665 | KeyStatusPair("case-sensitive", false), | ||||||||
1666 | KeyStatusPair("use-external-names", false), | ||||||||
1667 | KeyStatusPair("overlay-relative", false), | ||||||||
1668 | KeyStatusPair("fallthrough", false), | ||||||||
1669 | KeyStatusPair("roots", true), | ||||||||
1670 | }; | ||||||||
1671 | |||||||||
1672 | DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields)); | ||||||||
1673 | std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> RootEntries; | ||||||||
1674 | |||||||||
1675 | // Parse configuration and 'roots' | ||||||||
1676 | for (auto &I : *Top) { | ||||||||
1677 | SmallString<10> KeyBuffer; | ||||||||
1678 | StringRef Key; | ||||||||
1679 | if (!parseScalarString(I.getKey(), Key, KeyBuffer)) | ||||||||
1680 | return false; | ||||||||
1681 | |||||||||
1682 | if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys)) | ||||||||
1683 | return false; | ||||||||
1684 | |||||||||
1685 | if (Key == "roots") { | ||||||||
1686 | auto *Roots = dyn_cast<yaml::SequenceNode>(I.getValue()); | ||||||||
1687 | if (!Roots
| ||||||||
1688 | error(I.getValue(), "expected array"); | ||||||||
1689 | return false; | ||||||||
1690 | } | ||||||||
1691 | |||||||||
1692 | for (auto &I : *Roots) { | ||||||||
1693 | if (std::unique_ptr<RedirectingFileSystem::Entry> E = | ||||||||
1694 | parseEntry(&I, FS, /*IsRootEntry*/ true)) | ||||||||
1695 | RootEntries.push_back(std::move(E)); | ||||||||
1696 | else | ||||||||
1697 | return false; | ||||||||
1698 | } | ||||||||
1699 | } else if (Key == "version") { | ||||||||
1700 | StringRef VersionString; | ||||||||
1701 | SmallString<4> Storage; | ||||||||
1702 | if (!parseScalarString(I.getValue(), VersionString, Storage)) | ||||||||
1703 | return false; | ||||||||
1704 | int Version; | ||||||||
1705 | if (VersionString.getAsInteger<int>(10, Version)) { | ||||||||
1706 | error(I.getValue(), "expected integer"); | ||||||||
1707 | return false; | ||||||||
1708 | } | ||||||||
1709 | if (Version < 0) { | ||||||||
1710 | error(I.getValue(), "invalid version number"); | ||||||||
1711 | return false; | ||||||||
1712 | } | ||||||||
1713 | if (Version != 0) { | ||||||||
1714 | error(I.getValue(), "version mismatch, expected 0"); | ||||||||
1715 | return false; | ||||||||
1716 | } | ||||||||
1717 | } else if (Key == "case-sensitive") { | ||||||||
1718 | if (!parseScalarBool(I.getValue(), FS->CaseSensitive)) | ||||||||
1719 | return false; | ||||||||
1720 | } else if (Key == "overlay-relative") { | ||||||||
1721 | if (!parseScalarBool(I.getValue(), FS->IsRelativeOverlay)) | ||||||||
1722 | return false; | ||||||||
1723 | } else if (Key == "use-external-names") { | ||||||||
1724 | if (!parseScalarBool(I.getValue(), FS->UseExternalNames)) | ||||||||
1725 | return false; | ||||||||
1726 | } else if (Key == "fallthrough") { | ||||||||
1727 | if (!parseScalarBool(I.getValue(), FS->IsFallthrough)) | ||||||||
1728 | return false; | ||||||||
1729 | } else { | ||||||||
1730 | llvm_unreachable("key missing from Keys")::llvm::llvm_unreachable_internal("key missing from Keys", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1730); | ||||||||
1731 | } | ||||||||
1732 | } | ||||||||
1733 | |||||||||
1734 | if (Stream.failed()) | ||||||||
1735 | return false; | ||||||||
1736 | |||||||||
1737 | if (!checkMissingKeys(Top, Keys)) | ||||||||
1738 | return false; | ||||||||
1739 | |||||||||
1740 | // Now that we sucessefully parsed the YAML file, canonicalize the internal | ||||||||
1741 | // representation to a proper directory tree so that we can search faster | ||||||||
1742 | // inside the VFS. | ||||||||
1743 | for (auto &E : RootEntries) | ||||||||
1744 | uniqueOverlayTree(FS, E.get()); | ||||||||
1745 | |||||||||
1746 | return true; | ||||||||
1747 | } | ||||||||
1748 | }; | ||||||||
1749 | |||||||||
1750 | std::unique_ptr<RedirectingFileSystem> | ||||||||
1751 | RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer, | ||||||||
1752 | SourceMgr::DiagHandlerTy DiagHandler, | ||||||||
1753 | StringRef YAMLFilePath, void *DiagContext, | ||||||||
1754 | IntrusiveRefCntPtr<FileSystem> ExternalFS) { | ||||||||
1755 | SourceMgr SM; | ||||||||
1756 | yaml::Stream Stream(Buffer->getMemBufferRef(), SM); | ||||||||
1757 | |||||||||
1758 | SM.setDiagHandler(DiagHandler, DiagContext); | ||||||||
1759 | yaml::document_iterator DI = Stream.begin(); | ||||||||
1760 | yaml::Node *Root = DI->getRoot(); | ||||||||
1761 | if (DI == Stream.end() || !Root) { | ||||||||
1762 | SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node"); | ||||||||
1763 | return nullptr; | ||||||||
1764 | } | ||||||||
1765 | |||||||||
1766 | RedirectingFileSystemParser P(Stream); | ||||||||
1767 | |||||||||
1768 | std::unique_ptr<RedirectingFileSystem> FS( | ||||||||
1769 | new RedirectingFileSystem(ExternalFS)); | ||||||||
1770 | |||||||||
1771 | if (!YAMLFilePath.empty()) { | ||||||||
1772 | // Use the YAML path from -ivfsoverlay to compute the dir to be prefixed | ||||||||
1773 | // to each 'external-contents' path. | ||||||||
1774 | // | ||||||||
1775 | // Example: | ||||||||
1776 | // -ivfsoverlay dummy.cache/vfs/vfs.yaml | ||||||||
1777 | // yields: | ||||||||
1778 | // FS->ExternalContentsPrefixDir => /<absolute_path_to>/dummy.cache/vfs | ||||||||
1779 | // | ||||||||
1780 | SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath); | ||||||||
1781 | std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir); | ||||||||
1782 | assert(!EC && "Overlay dir final path must be absolute")(static_cast <bool> (!EC && "Overlay dir final path must be absolute" ) ? void (0) : __assert_fail ("!EC && \"Overlay dir final path must be absolute\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1782, __extension__ __PRETTY_FUNCTION__)); | ||||||||
1783 | (void)EC; | ||||||||
1784 | FS->setExternalContentsPrefixDir(OverlayAbsDir); | ||||||||
1785 | } | ||||||||
1786 | |||||||||
1787 | if (!P.parse(Root, FS.get())) | ||||||||
1788 | return nullptr; | ||||||||
1789 | |||||||||
1790 | return FS; | ||||||||
1791 | } | ||||||||
1792 | |||||||||
1793 | std::unique_ptr<RedirectingFileSystem> RedirectingFileSystem::create( | ||||||||
1794 | ArrayRef<std::pair<std::string, std::string>> RemappedFiles, | ||||||||
1795 | bool UseExternalNames, FileSystem &ExternalFS) { | ||||||||
1796 | std::unique_ptr<RedirectingFileSystem> FS( | ||||||||
1797 | new RedirectingFileSystem(&ExternalFS)); | ||||||||
1798 | FS->UseExternalNames = UseExternalNames; | ||||||||
1799 | |||||||||
1800 | StringMap<RedirectingFileSystem::Entry *> Entries; | ||||||||
1801 | |||||||||
1802 | for (auto &Mapping : llvm::reverse(RemappedFiles)) { | ||||||||
1803 | SmallString<128> From = StringRef(Mapping.first); | ||||||||
1804 | SmallString<128> To = StringRef(Mapping.second); | ||||||||
1805 | { | ||||||||
1806 | auto EC = ExternalFS.makeAbsolute(From); | ||||||||
1807 | (void)EC; | ||||||||
1808 | assert(!EC && "Could not make absolute path")(static_cast <bool> (!EC && "Could not make absolute path" ) ? void (0) : __assert_fail ("!EC && \"Could not make absolute path\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1808, __extension__ __PRETTY_FUNCTION__)); | ||||||||
1809 | } | ||||||||
1810 | |||||||||
1811 | // Check if we've already mapped this file. The first one we see (in the | ||||||||
1812 | // reverse iteration) wins. | ||||||||
1813 | RedirectingFileSystem::Entry *&ToEntry = Entries[From]; | ||||||||
1814 | if (ToEntry) | ||||||||
1815 | continue; | ||||||||
1816 | |||||||||
1817 | // Add parent directories. | ||||||||
1818 | RedirectingFileSystem::Entry *Parent = nullptr; | ||||||||
1819 | StringRef FromDirectory = llvm::sys::path::parent_path(From); | ||||||||
1820 | for (auto I = llvm::sys::path::begin(FromDirectory), | ||||||||
1821 | E = llvm::sys::path::end(FromDirectory); | ||||||||
1822 | I != E; ++I) { | ||||||||
1823 | Parent = RedirectingFileSystemParser::lookupOrCreateEntry(FS.get(), *I, | ||||||||
1824 | Parent); | ||||||||
1825 | } | ||||||||
1826 | assert(Parent && "File without a directory?")(static_cast <bool> (Parent && "File without a directory?" ) ? void (0) : __assert_fail ("Parent && \"File without a directory?\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1826, __extension__ __PRETTY_FUNCTION__)); | ||||||||
1827 | { | ||||||||
1828 | auto EC = ExternalFS.makeAbsolute(To); | ||||||||
1829 | (void)EC; | ||||||||
1830 | assert(!EC && "Could not make absolute path")(static_cast <bool> (!EC && "Could not make absolute path" ) ? void (0) : __assert_fail ("!EC && \"Could not make absolute path\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1830, __extension__ __PRETTY_FUNCTION__)); | ||||||||
1831 | } | ||||||||
1832 | |||||||||
1833 | // Add the file. | ||||||||
1834 | auto NewFile = std::make_unique<RedirectingFileSystem::FileEntry>( | ||||||||
1835 | llvm::sys::path::filename(From), To, | ||||||||
1836 | UseExternalNames ? RedirectingFileSystem::NK_External | ||||||||
1837 | : RedirectingFileSystem::NK_Virtual); | ||||||||
1838 | ToEntry = NewFile.get(); | ||||||||
1839 | cast<RedirectingFileSystem::DirectoryEntry>(Parent)->addContent( | ||||||||
1840 | std::move(NewFile)); | ||||||||
1841 | } | ||||||||
1842 | |||||||||
1843 | return FS; | ||||||||
1844 | } | ||||||||
1845 | |||||||||
1846 | RedirectingFileSystem::LookupResult::LookupResult( | ||||||||
1847 | Entry *E, sys::path::const_iterator Start, sys::path::const_iterator End) | ||||||||
1848 | : E(E) { | ||||||||
1849 | assert(E != nullptr)(static_cast <bool> (E != nullptr) ? void (0) : __assert_fail ("E != nullptr", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1849, __extension__ __PRETTY_FUNCTION__)); | ||||||||
1850 | // If the matched entry is a DirectoryRemapEntry, set ExternalRedirect to the | ||||||||
1851 | // path of the directory it maps to in the external file system plus any | ||||||||
1852 | // remaining path components in the provided iterator. | ||||||||
1853 | if (auto *DRE = dyn_cast<RedirectingFileSystem::DirectoryRemapEntry>(E)) { | ||||||||
1854 | SmallString<256> Redirect(DRE->getExternalContentsPath()); | ||||||||
1855 | sys::path::append(Redirect, Start, End, | ||||||||
1856 | getExistingStyle(DRE->getExternalContentsPath())); | ||||||||
1857 | ExternalRedirect = std::string(Redirect); | ||||||||
1858 | } | ||||||||
1859 | } | ||||||||
1860 | |||||||||
1861 | bool RedirectingFileSystem::shouldFallBackToExternalFS( | ||||||||
1862 | std::error_code EC, RedirectingFileSystem::Entry *E) const { | ||||||||
1863 | if (E && !isa<RedirectingFileSystem::DirectoryRemapEntry>(E)) | ||||||||
1864 | return false; | ||||||||
1865 | return shouldUseExternalFS() && EC == llvm::errc::no_such_file_or_directory; | ||||||||
1866 | } | ||||||||
1867 | |||||||||
1868 | std::error_code | ||||||||
1869 | RedirectingFileSystem::makeCanonical(SmallVectorImpl<char> &Path) const { | ||||||||
1870 | if (std::error_code EC = makeAbsolute(Path)) | ||||||||
1871 | return EC; | ||||||||
1872 | |||||||||
1873 | llvm::SmallString<256> CanonicalPath = | ||||||||
1874 | canonicalize(StringRef(Path.data(), Path.size())); | ||||||||
1875 | if (CanonicalPath.empty()) | ||||||||
1876 | return make_error_code(llvm::errc::invalid_argument); | ||||||||
1877 | |||||||||
1878 | Path.assign(CanonicalPath.begin(), CanonicalPath.end()); | ||||||||
1879 | return {}; | ||||||||
1880 | } | ||||||||
1881 | |||||||||
1882 | ErrorOr<RedirectingFileSystem::LookupResult> | ||||||||
1883 | RedirectingFileSystem::lookupPath(StringRef Path) const { | ||||||||
1884 | sys::path::const_iterator Start = sys::path::begin(Path); | ||||||||
1885 | sys::path::const_iterator End = sys::path::end(Path); | ||||||||
1886 | for (const auto &Root : Roots) { | ||||||||
1887 | ErrorOr<RedirectingFileSystem::LookupResult> Result = | ||||||||
1888 | lookupPathImpl(Start, End, Root.get()); | ||||||||
1889 | if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) | ||||||||
1890 | return Result; | ||||||||
1891 | } | ||||||||
1892 | return make_error_code(llvm::errc::no_such_file_or_directory); | ||||||||
1893 | } | ||||||||
1894 | |||||||||
1895 | ErrorOr<RedirectingFileSystem::LookupResult> | ||||||||
1896 | RedirectingFileSystem::lookupPathImpl( | ||||||||
1897 | sys::path::const_iterator Start, sys::path::const_iterator End, | ||||||||
1898 | RedirectingFileSystem::Entry *From) const { | ||||||||
1899 | assert(!isTraversalComponent(*Start) &&(static_cast <bool> (!isTraversalComponent(*Start) && !isTraversalComponent(From->getName()) && "Paths should not contain traversal components" ) ? void (0) : __assert_fail ("!isTraversalComponent(*Start) && !isTraversalComponent(From->getName()) && \"Paths should not contain traversal components\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1901, __extension__ __PRETTY_FUNCTION__)) | ||||||||
1900 | !isTraversalComponent(From->getName()) &&(static_cast <bool> (!isTraversalComponent(*Start) && !isTraversalComponent(From->getName()) && "Paths should not contain traversal components" ) ? void (0) : __assert_fail ("!isTraversalComponent(*Start) && !isTraversalComponent(From->getName()) && \"Paths should not contain traversal components\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1901, __extension__ __PRETTY_FUNCTION__)) | ||||||||
1901 | "Paths should not contain traversal components")(static_cast <bool> (!isTraversalComponent(*Start) && !isTraversalComponent(From->getName()) && "Paths should not contain traversal components" ) ? void (0) : __assert_fail ("!isTraversalComponent(*Start) && !isTraversalComponent(From->getName()) && \"Paths should not contain traversal components\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 1901, __extension__ __PRETTY_FUNCTION__)); | ||||||||
1902 | |||||||||
1903 | StringRef FromName = From->getName(); | ||||||||
1904 | |||||||||
1905 | // Forward the search to the next component in case this is an empty one. | ||||||||
1906 | if (!FromName.empty()) { | ||||||||
1907 | if (!pathComponentMatches(*Start, FromName)) | ||||||||
1908 | return make_error_code(llvm::errc::no_such_file_or_directory); | ||||||||
1909 | |||||||||
1910 | ++Start; | ||||||||
1911 | |||||||||
1912 | if (Start == End) { | ||||||||
1913 | // Match! | ||||||||
1914 | return LookupResult(From, Start, End); | ||||||||
1915 | } | ||||||||
1916 | } | ||||||||
1917 | |||||||||
1918 | if (isa<RedirectingFileSystem::FileEntry>(From)) | ||||||||
1919 | return make_error_code(llvm::errc::not_a_directory); | ||||||||
1920 | |||||||||
1921 | if (isa<RedirectingFileSystem::DirectoryRemapEntry>(From)) | ||||||||
1922 | return LookupResult(From, Start, End); | ||||||||
1923 | |||||||||
1924 | auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(From); | ||||||||
1925 | for (const std::unique_ptr<RedirectingFileSystem::Entry> &DirEntry : | ||||||||
1926 | llvm::make_range(DE->contents_begin(), DE->contents_end())) { | ||||||||
1927 | ErrorOr<RedirectingFileSystem::LookupResult> Result = | ||||||||
1928 | lookupPathImpl(Start, End, DirEntry.get()); | ||||||||
1929 | if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) | ||||||||
1930 | return Result; | ||||||||
1931 | } | ||||||||
1932 | |||||||||
1933 | return make_error_code(llvm::errc::no_such_file_or_directory); | ||||||||
1934 | } | ||||||||
1935 | |||||||||
1936 | static Status getRedirectedFileStatus(const Twine &Path, bool UseExternalNames, | ||||||||
1937 | Status ExternalStatus) { | ||||||||
1938 | Status S = ExternalStatus; | ||||||||
1939 | if (!UseExternalNames) | ||||||||
1940 | S = Status::copyWithNewName(S, Path); | ||||||||
1941 | S.IsVFSMapped = true; | ||||||||
1942 | return S; | ||||||||
1943 | } | ||||||||
1944 | |||||||||
1945 | ErrorOr<Status> RedirectingFileSystem::status( | ||||||||
1946 | const Twine &Path, const RedirectingFileSystem::LookupResult &Result) { | ||||||||
1947 | if (Optional<StringRef> ExtRedirect = Result.getExternalRedirect()) { | ||||||||
1948 | ErrorOr<Status> S = ExternalFS->status(*ExtRedirect); | ||||||||
1949 | if (!S) | ||||||||
1950 | return S; | ||||||||
1951 | auto *RE = cast<RedirectingFileSystem::RemapEntry>(Result.E); | ||||||||
1952 | return getRedirectedFileStatus(Path, RE->useExternalName(UseExternalNames), | ||||||||
1953 | *S); | ||||||||
1954 | } | ||||||||
1955 | |||||||||
1956 | auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(Result.E); | ||||||||
1957 | return Status::copyWithNewName(DE->getStatus(), Path); | ||||||||
1958 | } | ||||||||
1959 | |||||||||
1960 | ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path_) { | ||||||||
1961 | SmallString<256> Path; | ||||||||
1962 | Path_.toVector(Path); | ||||||||
1963 | |||||||||
1964 | if (std::error_code EC = makeCanonical(Path)) | ||||||||
1965 | return EC; | ||||||||
1966 | |||||||||
1967 | ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path); | ||||||||
1968 | if (!Result) { | ||||||||
1969 | if (shouldFallBackToExternalFS(Result.getError())) | ||||||||
1970 | return ExternalFS->status(Path); | ||||||||
1971 | return Result.getError(); | ||||||||
1972 | } | ||||||||
1973 | |||||||||
1974 | ErrorOr<Status> S = status(Path, *Result); | ||||||||
1975 | if (!S && shouldFallBackToExternalFS(S.getError(), Result->E)) | ||||||||
1976 | S = ExternalFS->status(Path); | ||||||||
1977 | return S; | ||||||||
1978 | } | ||||||||
1979 | |||||||||
1980 | namespace { | ||||||||
1981 | |||||||||
1982 | /// Provide a file wrapper with an overriden status. | ||||||||
1983 | class FileWithFixedStatus : public File { | ||||||||
1984 | std::unique_ptr<File> InnerFile; | ||||||||
1985 | Status S; | ||||||||
1986 | |||||||||
1987 | public: | ||||||||
1988 | FileWithFixedStatus(std::unique_ptr<File> InnerFile, Status S) | ||||||||
1989 | : InnerFile(std::move(InnerFile)), S(std::move(S)) {} | ||||||||
1990 | |||||||||
1991 | ErrorOr<Status> status() override { return S; } | ||||||||
1992 | ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> | ||||||||
1993 | |||||||||
1994 | getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator, | ||||||||
1995 | bool IsVolatile) override { | ||||||||
1996 | return InnerFile->getBuffer(Name, FileSize, RequiresNullTerminator, | ||||||||
1997 | IsVolatile); | ||||||||
1998 | } | ||||||||
1999 | |||||||||
2000 | std::error_code close() override { return InnerFile->close(); } | ||||||||
2001 | }; | ||||||||
2002 | |||||||||
2003 | } // namespace | ||||||||
2004 | |||||||||
2005 | ErrorOr<std::unique_ptr<File>> | ||||||||
2006 | RedirectingFileSystem::openFileForRead(const Twine &Path_) { | ||||||||
2007 | SmallString<256> Path; | ||||||||
2008 | Path_.toVector(Path); | ||||||||
2009 | |||||||||
2010 | if (std::error_code EC = makeCanonical(Path)) | ||||||||
2011 | return EC; | ||||||||
2012 | |||||||||
2013 | ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path); | ||||||||
2014 | if (!Result) { | ||||||||
2015 | if (shouldFallBackToExternalFS(Result.getError())) | ||||||||
2016 | return ExternalFS->openFileForRead(Path); | ||||||||
2017 | return Result.getError(); | ||||||||
2018 | } | ||||||||
2019 | |||||||||
2020 | if (!Result->getExternalRedirect()) // FIXME: errc::not_a_file? | ||||||||
2021 | return make_error_code(llvm::errc::invalid_argument); | ||||||||
2022 | |||||||||
2023 | StringRef ExtRedirect = *Result->getExternalRedirect(); | ||||||||
2024 | auto *RE = cast<RedirectingFileSystem::RemapEntry>(Result->E); | ||||||||
2025 | |||||||||
2026 | auto ExternalFile = ExternalFS->openFileForRead(ExtRedirect); | ||||||||
2027 | if (!ExternalFile) { | ||||||||
2028 | if (shouldFallBackToExternalFS(ExternalFile.getError(), Result->E)) | ||||||||
2029 | return ExternalFS->openFileForRead(Path); | ||||||||
2030 | return ExternalFile; | ||||||||
2031 | } | ||||||||
2032 | |||||||||
2033 | auto ExternalStatus = (*ExternalFile)->status(); | ||||||||
2034 | if (!ExternalStatus) | ||||||||
2035 | return ExternalStatus.getError(); | ||||||||
2036 | |||||||||
2037 | // FIXME: Update the status with the name and VFSMapped. | ||||||||
2038 | Status S = getRedirectedFileStatus( | ||||||||
2039 | Path, RE->useExternalName(UseExternalNames), *ExternalStatus); | ||||||||
2040 | return std::unique_ptr<File>( | ||||||||
2041 | std::make_unique<FileWithFixedStatus>(std::move(*ExternalFile), S)); | ||||||||
2042 | } | ||||||||
2043 | |||||||||
2044 | std::error_code | ||||||||
2045 | RedirectingFileSystem::getRealPath(const Twine &Path_, | ||||||||
2046 | SmallVectorImpl<char> &Output) const { | ||||||||
2047 | SmallString<256> Path; | ||||||||
2048 | Path_.toVector(Path); | ||||||||
2049 | |||||||||
2050 | if (std::error_code EC = makeCanonical(Path)) | ||||||||
2051 | return EC; | ||||||||
2052 | |||||||||
2053 | ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path); | ||||||||
2054 | if (!Result) { | ||||||||
2055 | if (shouldFallBackToExternalFS(Result.getError())) | ||||||||
2056 | return ExternalFS->getRealPath(Path, Output); | ||||||||
2057 | return Result.getError(); | ||||||||
2058 | } | ||||||||
2059 | |||||||||
2060 | // If we found FileEntry or DirectoryRemapEntry, look up the mapped | ||||||||
2061 | // path in the external file system. | ||||||||
2062 | if (auto ExtRedirect = Result->getExternalRedirect()) { | ||||||||
2063 | auto P = ExternalFS->getRealPath(*ExtRedirect, Output); | ||||||||
2064 | if (!P && shouldFallBackToExternalFS(P, Result->E)) { | ||||||||
2065 | return ExternalFS->getRealPath(Path, Output); | ||||||||
2066 | } | ||||||||
2067 | return P; | ||||||||
2068 | } | ||||||||
2069 | |||||||||
2070 | // If we found a DirectoryEntry, still fall back to ExternalFS if allowed, | ||||||||
2071 | // because directories don't have a single external contents path. | ||||||||
2072 | return shouldUseExternalFS() ? ExternalFS->getRealPath(Path, Output) | ||||||||
2073 | : llvm::errc::invalid_argument; | ||||||||
2074 | } | ||||||||
2075 | |||||||||
2076 | std::unique_ptr<FileSystem> | ||||||||
2077 | vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer, | ||||||||
2078 | SourceMgr::DiagHandlerTy DiagHandler, | ||||||||
2079 | StringRef YAMLFilePath, void *DiagContext, | ||||||||
2080 | IntrusiveRefCntPtr<FileSystem> ExternalFS) { | ||||||||
2081 | return RedirectingFileSystem::create(std::move(Buffer), DiagHandler, | ||||||||
2082 | YAMLFilePath, DiagContext, | ||||||||
2083 | std::move(ExternalFS)); | ||||||||
2084 | } | ||||||||
2085 | |||||||||
2086 | static void getVFSEntries(RedirectingFileSystem::Entry *SrcE, | ||||||||
2087 | SmallVectorImpl<StringRef> &Path, | ||||||||
2088 | SmallVectorImpl<YAMLVFSEntry> &Entries) { | ||||||||
2089 | auto Kind = SrcE->getKind(); | ||||||||
2090 | if (Kind == RedirectingFileSystem::EK_Directory) { | ||||||||
2091 | auto *DE = dyn_cast<RedirectingFileSystem::DirectoryEntry>(SrcE); | ||||||||
2092 | assert(DE && "Must be a directory")(static_cast <bool> (DE && "Must be a directory" ) ? void (0) : __assert_fail ("DE && \"Must be a directory\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2092, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2093 | for (std::unique_ptr<RedirectingFileSystem::Entry> &SubEntry : | ||||||||
2094 | llvm::make_range(DE->contents_begin(), DE->contents_end())) { | ||||||||
2095 | Path.push_back(SubEntry->getName()); | ||||||||
2096 | getVFSEntries(SubEntry.get(), Path, Entries); | ||||||||
2097 | Path.pop_back(); | ||||||||
2098 | } | ||||||||
2099 | return; | ||||||||
2100 | } | ||||||||
2101 | |||||||||
2102 | if (Kind == RedirectingFileSystem::EK_DirectoryRemap) { | ||||||||
2103 | auto *DR = dyn_cast<RedirectingFileSystem::DirectoryRemapEntry>(SrcE); | ||||||||
2104 | assert(DR && "Must be a directory remap")(static_cast <bool> (DR && "Must be a directory remap" ) ? void (0) : __assert_fail ("DR && \"Must be a directory remap\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2104, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2105 | SmallString<128> VPath; | ||||||||
2106 | for (auto &Comp : Path) | ||||||||
2107 | llvm::sys::path::append(VPath, Comp); | ||||||||
2108 | Entries.push_back( | ||||||||
2109 | YAMLVFSEntry(VPath.c_str(), DR->getExternalContentsPath())); | ||||||||
2110 | return; | ||||||||
2111 | } | ||||||||
2112 | |||||||||
2113 | assert(Kind == RedirectingFileSystem::EK_File && "Must be a EK_File")(static_cast <bool> (Kind == RedirectingFileSystem::EK_File && "Must be a EK_File") ? void (0) : __assert_fail ( "Kind == RedirectingFileSystem::EK_File && \"Must be a EK_File\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2113, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2114 | auto *FE = dyn_cast<RedirectingFileSystem::FileEntry>(SrcE); | ||||||||
2115 | assert(FE && "Must be a file")(static_cast <bool> (FE && "Must be a file") ? void (0) : __assert_fail ("FE && \"Must be a file\"", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2115, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2116 | SmallString<128> VPath; | ||||||||
2117 | for (auto &Comp : Path) | ||||||||
2118 | llvm::sys::path::append(VPath, Comp); | ||||||||
2119 | Entries.push_back(YAMLVFSEntry(VPath.c_str(), FE->getExternalContentsPath())); | ||||||||
2120 | } | ||||||||
2121 | |||||||||
2122 | void vfs::collectVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer, | ||||||||
2123 | SourceMgr::DiagHandlerTy DiagHandler, | ||||||||
2124 | StringRef YAMLFilePath, | ||||||||
2125 | SmallVectorImpl<YAMLVFSEntry> &CollectedEntries, | ||||||||
2126 | void *DiagContext, | ||||||||
2127 | IntrusiveRefCntPtr<FileSystem> ExternalFS) { | ||||||||
2128 | std::unique_ptr<RedirectingFileSystem> VFS = RedirectingFileSystem::create( | ||||||||
| |||||||||
2129 | std::move(Buffer), DiagHandler, YAMLFilePath, DiagContext, | ||||||||
2130 | std::move(ExternalFS)); | ||||||||
2131 | if (!VFS) | ||||||||
2132 | return; | ||||||||
2133 | ErrorOr<RedirectingFileSystem::LookupResult> RootResult = | ||||||||
2134 | VFS->lookupPath("/"); | ||||||||
2135 | if (!RootResult) | ||||||||
2136 | return; | ||||||||
2137 | SmallVector<StringRef, 8> Components; | ||||||||
2138 | Components.push_back("/"); | ||||||||
2139 | getVFSEntries(RootResult->E, Components, CollectedEntries); | ||||||||
2140 | } | ||||||||
2141 | |||||||||
2142 | UniqueID vfs::getNextVirtualUniqueID() { | ||||||||
2143 | static std::atomic<unsigned> UID; | ||||||||
2144 | unsigned ID = ++UID; | ||||||||
2145 | // The following assumes that uint64_t max will never collide with a real | ||||||||
2146 | // dev_t value from the OS. | ||||||||
2147 | return UniqueID(std::numeric_limits<uint64_t>::max(), ID); | ||||||||
2148 | } | ||||||||
2149 | |||||||||
2150 | void YAMLVFSWriter::addEntry(StringRef VirtualPath, StringRef RealPath, | ||||||||
2151 | bool IsDirectory) { | ||||||||
2152 | assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute")(static_cast <bool> (sys::path::is_absolute(VirtualPath ) && "virtual path not absolute") ? void (0) : __assert_fail ("sys::path::is_absolute(VirtualPath) && \"virtual path not absolute\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2152, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2153 | assert(sys::path::is_absolute(RealPath) && "real path not absolute")(static_cast <bool> (sys::path::is_absolute(RealPath) && "real path not absolute") ? void (0) : __assert_fail ("sys::path::is_absolute(RealPath) && \"real path not absolute\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2153, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2154 | assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported")(static_cast <bool> (!pathHasTraversal(VirtualPath) && "path traversal is not supported") ? void (0) : __assert_fail ("!pathHasTraversal(VirtualPath) && \"path traversal is not supported\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2154, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2155 | Mappings.emplace_back(VirtualPath, RealPath, IsDirectory); | ||||||||
2156 | } | ||||||||
2157 | |||||||||
2158 | void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) { | ||||||||
2159 | addEntry(VirtualPath, RealPath, /*IsDirectory=*/false); | ||||||||
2160 | } | ||||||||
2161 | |||||||||
2162 | void YAMLVFSWriter::addDirectoryMapping(StringRef VirtualPath, | ||||||||
2163 | StringRef RealPath) { | ||||||||
2164 | addEntry(VirtualPath, RealPath, /*IsDirectory=*/true); | ||||||||
2165 | } | ||||||||
2166 | |||||||||
2167 | namespace { | ||||||||
2168 | |||||||||
2169 | class JSONWriter { | ||||||||
2170 | llvm::raw_ostream &OS; | ||||||||
2171 | SmallVector<StringRef, 16> DirStack; | ||||||||
2172 | |||||||||
2173 | unsigned getDirIndent() { return 4 * DirStack.size(); } | ||||||||
2174 | unsigned getFileIndent() { return 4 * (DirStack.size() + 1); } | ||||||||
2175 | bool containedIn(StringRef Parent, StringRef Path); | ||||||||
2176 | StringRef containedPart(StringRef Parent, StringRef Path); | ||||||||
2177 | void startDirectory(StringRef Path); | ||||||||
2178 | void endDirectory(); | ||||||||
2179 | void writeEntry(StringRef VPath, StringRef RPath); | ||||||||
2180 | |||||||||
2181 | public: | ||||||||
2182 | JSONWriter(llvm::raw_ostream &OS) : OS(OS) {} | ||||||||
2183 | |||||||||
2184 | void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> UseExternalNames, | ||||||||
2185 | Optional<bool> IsCaseSensitive, Optional<bool> IsOverlayRelative, | ||||||||
2186 | StringRef OverlayDir); | ||||||||
2187 | }; | ||||||||
2188 | |||||||||
2189 | } // namespace | ||||||||
2190 | |||||||||
2191 | bool JSONWriter::containedIn(StringRef Parent, StringRef Path) { | ||||||||
2192 | using namespace llvm::sys; | ||||||||
2193 | |||||||||
2194 | // Compare each path component. | ||||||||
2195 | auto IParent = path::begin(Parent), EParent = path::end(Parent); | ||||||||
2196 | for (auto IChild = path::begin(Path), EChild = path::end(Path); | ||||||||
2197 | IParent != EParent && IChild != EChild; ++IParent, ++IChild) { | ||||||||
2198 | if (*IParent != *IChild) | ||||||||
2199 | return false; | ||||||||
2200 | } | ||||||||
2201 | // Have we exhausted the parent path? | ||||||||
2202 | return IParent == EParent; | ||||||||
2203 | } | ||||||||
2204 | |||||||||
2205 | StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) { | ||||||||
2206 | assert(!Parent.empty())(static_cast <bool> (!Parent.empty()) ? void (0) : __assert_fail ("!Parent.empty()", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2206, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2207 | assert(containedIn(Parent, Path))(static_cast <bool> (containedIn(Parent, Path)) ? void ( 0) : __assert_fail ("containedIn(Parent, Path)", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2207, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2208 | return Path.slice(Parent.size() + 1, StringRef::npos); | ||||||||
2209 | } | ||||||||
2210 | |||||||||
2211 | void JSONWriter::startDirectory(StringRef Path) { | ||||||||
2212 | StringRef Name = | ||||||||
2213 | DirStack.empty() ? Path : containedPart(DirStack.back(), Path); | ||||||||
2214 | DirStack.push_back(Path); | ||||||||
2215 | unsigned Indent = getDirIndent(); | ||||||||
2216 | OS.indent(Indent) << "{\n"; | ||||||||
2217 | OS.indent(Indent + 2) << "'type': 'directory',\n"; | ||||||||
2218 | OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n"; | ||||||||
2219 | OS.indent(Indent + 2) << "'contents': [\n"; | ||||||||
2220 | } | ||||||||
2221 | |||||||||
2222 | void JSONWriter::endDirectory() { | ||||||||
2223 | unsigned Indent = getDirIndent(); | ||||||||
2224 | OS.indent(Indent + 2) << "]\n"; | ||||||||
2225 | OS.indent(Indent) << "}"; | ||||||||
2226 | |||||||||
2227 | DirStack.pop_back(); | ||||||||
2228 | } | ||||||||
2229 | |||||||||
2230 | void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) { | ||||||||
2231 | unsigned Indent = getFileIndent(); | ||||||||
2232 | OS.indent(Indent) << "{\n"; | ||||||||
2233 | OS.indent(Indent + 2) << "'type': 'file',\n"; | ||||||||
2234 | OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n"; | ||||||||
2235 | OS.indent(Indent + 2) << "'external-contents': \"" | ||||||||
2236 | << llvm::yaml::escape(RPath) << "\"\n"; | ||||||||
2237 | OS.indent(Indent) << "}"; | ||||||||
2238 | } | ||||||||
2239 | |||||||||
2240 | void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries, | ||||||||
2241 | Optional<bool> UseExternalNames, | ||||||||
2242 | Optional<bool> IsCaseSensitive, | ||||||||
2243 | Optional<bool> IsOverlayRelative, | ||||||||
2244 | StringRef OverlayDir) { | ||||||||
2245 | using namespace llvm::sys; | ||||||||
2246 | |||||||||
2247 | OS << "{\n" | ||||||||
2248 | " 'version': 0,\n"; | ||||||||
2249 | if (IsCaseSensitive.hasValue()) | ||||||||
2250 | OS << " 'case-sensitive': '" | ||||||||
2251 | << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n"; | ||||||||
2252 | if (UseExternalNames.hasValue()) | ||||||||
2253 | OS << " 'use-external-names': '" | ||||||||
2254 | << (UseExternalNames.getValue() ? "true" : "false") << "',\n"; | ||||||||
2255 | bool UseOverlayRelative = false; | ||||||||
2256 | if (IsOverlayRelative.hasValue()) { | ||||||||
2257 | UseOverlayRelative = IsOverlayRelative.getValue(); | ||||||||
2258 | OS << " 'overlay-relative': '" << (UseOverlayRelative ? "true" : "false") | ||||||||
2259 | << "',\n"; | ||||||||
2260 | } | ||||||||
2261 | OS << " 'roots': [\n"; | ||||||||
2262 | |||||||||
2263 | if (!Entries.empty()) { | ||||||||
2264 | const YAMLVFSEntry &Entry = Entries.front(); | ||||||||
2265 | |||||||||
2266 | startDirectory( | ||||||||
2267 | Entry.IsDirectory ? Entry.VPath : path::parent_path(Entry.VPath) | ||||||||
2268 | ); | ||||||||
2269 | |||||||||
2270 | StringRef RPath = Entry.RPath; | ||||||||
2271 | if (UseOverlayRelative) { | ||||||||
2272 | unsigned OverlayDirLen = OverlayDir.size(); | ||||||||
2273 | assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&(static_cast <bool> (RPath.substr(0, OverlayDirLen) == OverlayDir && "Overlay dir must be contained in RPath") ? void ( 0) : __assert_fail ("RPath.substr(0, OverlayDirLen) == OverlayDir && \"Overlay dir must be contained in RPath\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2274, __extension__ __PRETTY_FUNCTION__)) | ||||||||
2274 | "Overlay dir must be contained in RPath")(static_cast <bool> (RPath.substr(0, OverlayDirLen) == OverlayDir && "Overlay dir must be contained in RPath") ? void ( 0) : __assert_fail ("RPath.substr(0, OverlayDirLen) == OverlayDir && \"Overlay dir must be contained in RPath\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2274, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2275 | RPath = RPath.slice(OverlayDirLen, RPath.size()); | ||||||||
2276 | } | ||||||||
2277 | |||||||||
2278 | bool IsCurrentDirEmpty = true; | ||||||||
2279 | if (!Entry.IsDirectory) { | ||||||||
2280 | writeEntry(path::filename(Entry.VPath), RPath); | ||||||||
2281 | IsCurrentDirEmpty = false; | ||||||||
2282 | } | ||||||||
2283 | |||||||||
2284 | for (const auto &Entry : Entries.slice(1)) { | ||||||||
2285 | StringRef Dir = | ||||||||
2286 | Entry.IsDirectory ? Entry.VPath : path::parent_path(Entry.VPath); | ||||||||
2287 | if (Dir == DirStack.back()) { | ||||||||
2288 | if (!IsCurrentDirEmpty) { | ||||||||
2289 | OS << ",\n"; | ||||||||
2290 | } | ||||||||
2291 | } else { | ||||||||
2292 | bool IsDirPoppedFromStack = false; | ||||||||
2293 | while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) { | ||||||||
2294 | OS << "\n"; | ||||||||
2295 | endDirectory(); | ||||||||
2296 | IsDirPoppedFromStack = true; | ||||||||
2297 | } | ||||||||
2298 | if (IsDirPoppedFromStack || !IsCurrentDirEmpty) { | ||||||||
2299 | OS << ",\n"; | ||||||||
2300 | } | ||||||||
2301 | startDirectory(Dir); | ||||||||
2302 | IsCurrentDirEmpty = true; | ||||||||
2303 | } | ||||||||
2304 | StringRef RPath = Entry.RPath; | ||||||||
2305 | if (UseOverlayRelative) { | ||||||||
2306 | unsigned OverlayDirLen = OverlayDir.size(); | ||||||||
2307 | assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&(static_cast <bool> (RPath.substr(0, OverlayDirLen) == OverlayDir && "Overlay dir must be contained in RPath") ? void ( 0) : __assert_fail ("RPath.substr(0, OverlayDirLen) == OverlayDir && \"Overlay dir must be contained in RPath\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2308, __extension__ __PRETTY_FUNCTION__)) | ||||||||
2308 | "Overlay dir must be contained in RPath")(static_cast <bool> (RPath.substr(0, OverlayDirLen) == OverlayDir && "Overlay dir must be contained in RPath") ? void ( 0) : __assert_fail ("RPath.substr(0, OverlayDirLen) == OverlayDir && \"Overlay dir must be contained in RPath\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2308, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2309 | RPath = RPath.slice(OverlayDirLen, RPath.size()); | ||||||||
2310 | } | ||||||||
2311 | if (!Entry.IsDirectory) { | ||||||||
2312 | writeEntry(path::filename(Entry.VPath), RPath); | ||||||||
2313 | IsCurrentDirEmpty = false; | ||||||||
2314 | } | ||||||||
2315 | } | ||||||||
2316 | |||||||||
2317 | while (!DirStack.empty()) { | ||||||||
2318 | OS << "\n"; | ||||||||
2319 | endDirectory(); | ||||||||
2320 | } | ||||||||
2321 | OS << "\n"; | ||||||||
2322 | } | ||||||||
2323 | |||||||||
2324 | OS << " ]\n" | ||||||||
2325 | << "}\n"; | ||||||||
2326 | } | ||||||||
2327 | |||||||||
2328 | void YAMLVFSWriter::write(llvm::raw_ostream &OS) { | ||||||||
2329 | llvm::sort(Mappings, [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) { | ||||||||
2330 | return LHS.VPath < RHS.VPath; | ||||||||
2331 | }); | ||||||||
2332 | |||||||||
2333 | JSONWriter(OS).write(Mappings, UseExternalNames, IsCaseSensitive, | ||||||||
2334 | IsOverlayRelative, OverlayDir); | ||||||||
2335 | } | ||||||||
2336 | |||||||||
2337 | vfs::recursive_directory_iterator::recursive_directory_iterator( | ||||||||
2338 | FileSystem &FS_, const Twine &Path, std::error_code &EC) | ||||||||
2339 | : FS(&FS_) { | ||||||||
2340 | directory_iterator I = FS->dir_begin(Path, EC); | ||||||||
2341 | if (I != directory_iterator()) { | ||||||||
2342 | State = std::make_shared<detail::RecDirIterState>(); | ||||||||
2343 | State->Stack.push(I); | ||||||||
2344 | } | ||||||||
2345 | } | ||||||||
2346 | |||||||||
2347 | vfs::recursive_directory_iterator & | ||||||||
2348 | recursive_directory_iterator::increment(std::error_code &EC) { | ||||||||
2349 | assert(FS && State && !State->Stack.empty() && "incrementing past end")(static_cast <bool> (FS && State && !State ->Stack.empty() && "incrementing past end") ? void (0) : __assert_fail ("FS && State && !State->Stack.empty() && \"incrementing past end\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2349, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2350 | assert(!State->Stack.top()->path().empty() && "non-canonical end iterator")(static_cast <bool> (!State->Stack.top()->path(). empty() && "non-canonical end iterator") ? void (0) : __assert_fail ("!State->Stack.top()->path().empty() && \"non-canonical end iterator\"" , "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/Support/VirtualFileSystem.cpp" , 2350, __extension__ __PRETTY_FUNCTION__)); | ||||||||
2351 | vfs::directory_iterator End; | ||||||||
2352 | |||||||||
2353 | if (State->HasNoPushRequest) | ||||||||
2354 | State->HasNoPushRequest = false; | ||||||||
2355 | else { | ||||||||
2356 | if (State->Stack.top()->type() == sys::fs::file_type::directory_file) { | ||||||||
2357 | vfs::directory_iterator I = FS->dir_begin(State->Stack.top()->path(), EC); | ||||||||
2358 | if (I != End) { | ||||||||
2359 | State->Stack.push(I); | ||||||||
2360 | return *this; | ||||||||
2361 | } | ||||||||
2362 | } | ||||||||
2363 | } | ||||||||
2364 | |||||||||
2365 | while (!State->Stack.empty() && State->Stack.top().increment(EC) == End) | ||||||||
2366 | State->Stack.pop(); | ||||||||
2367 | |||||||||
2368 | if (State->Stack.empty()) | ||||||||
2369 | State.reset(); // end iterator | ||||||||
2370 | |||||||||
2371 | return *this; | ||||||||
2372 | } |
1 | // unique_ptr implementation -*- C++ -*- |
2 | |
3 | // Copyright (C) 2008-2020 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for more details. |
15 | |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version |
18 | // 3.1, as published by the Free Software Foundation. |
19 | |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
23 | // <http://www.gnu.org/licenses/>. |
24 | |
25 | /** @file bits/unique_ptr.h |
26 | * This is an internal header file, included by other library headers. |
27 | * Do not attempt to use it directly. @headername{memory} |
28 | */ |
29 | |
30 | #ifndef _UNIQUE_PTR_H1 |
31 | #define _UNIQUE_PTR_H1 1 |
32 | |
33 | #include <bits/c++config.h> |
34 | #include <debug/assertions.h> |
35 | #include <type_traits> |
36 | #include <utility> |
37 | #include <tuple> |
38 | #include <bits/stl_function.h> |
39 | #include <bits/functional_hash.h> |
40 | #if __cplusplus201402L > 201703L |
41 | # include <compare> |
42 | # include <ostream> |
43 | #endif |
44 | |
45 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
46 | { |
47 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
48 | |
49 | /** |
50 | * @addtogroup pointer_abstractions |
51 | * @{ |
52 | */ |
53 | |
54 | #if _GLIBCXX_USE_DEPRECATED1 |
55 | #pragma GCC diagnostic push |
56 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
57 | template<typename> class auto_ptr; |
58 | #pragma GCC diagnostic pop |
59 | #endif |
60 | |
61 | /// Primary template of default_delete, used by unique_ptr for single objects |
62 | template<typename _Tp> |
63 | struct default_delete |
64 | { |
65 | /// Default constructor |
66 | constexpr default_delete() noexcept = default; |
67 | |
68 | /** @brief Converting constructor. |
69 | * |
70 | * Allows conversion from a deleter for objects of another type, `_Up`, |
71 | * only if `_Up*` is convertible to `_Tp*`. |
72 | */ |
73 | template<typename _Up, |
74 | typename = _Require<is_convertible<_Up*, _Tp*>>> |
75 | default_delete(const default_delete<_Up>&) noexcept { } |
76 | |
77 | /// Calls `delete __ptr` |
78 | void |
79 | operator()(_Tp* __ptr) const |
80 | { |
81 | static_assert(!is_void<_Tp>::value, |
82 | "can't delete pointer to incomplete type"); |
83 | static_assert(sizeof(_Tp)>0, |
84 | "can't delete pointer to incomplete type"); |
85 | delete __ptr; |
86 | } |
87 | }; |
88 | |
89 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
90 | // DR 740 - omit specialization for array objects with a compile time length |
91 | |
92 | /// Specialization of default_delete for arrays, used by `unique_ptr<T[]>` |
93 | template<typename _Tp> |
94 | struct default_delete<_Tp[]> |
95 | { |
96 | public: |
97 | /// Default constructor |
98 | constexpr default_delete() noexcept = default; |
99 | |
100 | /** @brief Converting constructor. |
101 | * |
102 | * Allows conversion from a deleter for arrays of another type, such as |
103 | * a const-qualified version of `_Tp`. |
104 | * |
105 | * Conversions from types derived from `_Tp` are not allowed because |
106 | * it is undefined to `delete[]` an array of derived types through a |
107 | * pointer to the base type. |
108 | */ |
109 | template<typename _Up, |
110 | typename = _Require<is_convertible<_Up(*)[], _Tp(*)[]>>> |
111 | default_delete(const default_delete<_Up[]>&) noexcept { } |
112 | |
113 | /// Calls `delete[] __ptr` |
114 | template<typename _Up> |
115 | typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type |
116 | operator()(_Up* __ptr) const |
117 | { |
118 | static_assert(sizeof(_Tp)>0, |
119 | "can't delete pointer to incomplete type"); |
120 | delete [] __ptr; |
121 | } |
122 | }; |
123 | |
124 | /// @cond undocumented |
125 | |
126 | // Manages the pointer and deleter of a unique_ptr |
127 | template <typename _Tp, typename _Dp> |
128 | class __uniq_ptr_impl |
129 | { |
130 | template <typename _Up, typename _Ep, typename = void> |
131 | struct _Ptr |
132 | { |
133 | using type = _Up*; |
134 | }; |
135 | |
136 | template <typename _Up, typename _Ep> |
137 | struct |
138 | _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>> |
139 | { |
140 | using type = typename remove_reference<_Ep>::type::pointer; |
141 | }; |
142 | |
143 | public: |
144 | using _DeleterConstraint = enable_if< |
145 | __and_<__not_<is_pointer<_Dp>>, |
146 | is_default_constructible<_Dp>>::value>; |
147 | |
148 | using pointer = typename _Ptr<_Tp, _Dp>::type; |
149 | |
150 | static_assert( !is_rvalue_reference<_Dp>::value, |
151 | "unique_ptr's deleter type must be a function object type" |
152 | " or an lvalue reference type" ); |
153 | |
154 | __uniq_ptr_impl() = default; |
155 | __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; } |
156 | |
157 | template<typename _Del> |
158 | __uniq_ptr_impl(pointer __p, _Del&& __d) |
159 | : _M_t(__p, std::forward<_Del>(__d)) { } |
160 | |
161 | __uniq_ptr_impl(__uniq_ptr_impl&& __u) noexcept |
162 | : _M_t(std::move(__u._M_t)) |
163 | { __u._M_ptr() = nullptr; } |
164 | |
165 | __uniq_ptr_impl& operator=(__uniq_ptr_impl&& __u) noexcept |
166 | { |
167 | reset(__u.release()); |
168 | _M_deleter() = std::forward<_Dp>(__u._M_deleter()); |
169 | return *this; |
170 | } |
171 | |
172 | pointer& _M_ptr() { return std::get<0>(_M_t); } |
173 | pointer _M_ptr() const { return std::get<0>(_M_t); } |
174 | _Dp& _M_deleter() { return std::get<1>(_M_t); } |
175 | const _Dp& _M_deleter() const { return std::get<1>(_M_t); } |
176 | |
177 | void reset(pointer __p) noexcept |
178 | { |
179 | const pointer __old_p = _M_ptr(); |
180 | _M_ptr() = __p; |
181 | if (__old_p) |
182 | _M_deleter()(__old_p); |
183 | } |
184 | |
185 | pointer release() noexcept |
186 | { |
187 | pointer __p = _M_ptr(); |
188 | _M_ptr() = nullptr; |
189 | return __p; |
190 | } |
191 | |
192 | void |
193 | swap(__uniq_ptr_impl& __rhs) noexcept |
194 | { |
195 | using std::swap; |
196 | swap(this->_M_ptr(), __rhs._M_ptr()); |
197 | swap(this->_M_deleter(), __rhs._M_deleter()); |
198 | } |
199 | |
200 | private: |
201 | tuple<pointer, _Dp> _M_t; |
202 | }; |
203 | |
204 | // Defines move construction + assignment as either defaulted or deleted. |
205 | template <typename _Tp, typename _Dp, |
206 | bool = is_move_constructible<_Dp>::value, |
207 | bool = is_move_assignable<_Dp>::value> |
208 | struct __uniq_ptr_data : __uniq_ptr_impl<_Tp, _Dp> |
209 | { |
210 | using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl; |
211 | __uniq_ptr_data(__uniq_ptr_data&&) = default; |
212 | __uniq_ptr_data& operator=(__uniq_ptr_data&&) = default; |
213 | }; |
214 | |
215 | template <typename _Tp, typename _Dp> |
216 | struct __uniq_ptr_data<_Tp, _Dp, true, false> : __uniq_ptr_impl<_Tp, _Dp> |
217 | { |
218 | using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl; |
219 | __uniq_ptr_data(__uniq_ptr_data&&) = default; |
220 | __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete; |
221 | }; |
222 | |
223 | template <typename _Tp, typename _Dp> |
224 | struct __uniq_ptr_data<_Tp, _Dp, false, true> : __uniq_ptr_impl<_Tp, _Dp> |
225 | { |
226 | using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl; |
227 | __uniq_ptr_data(__uniq_ptr_data&&) = delete; |
228 | __uniq_ptr_data& operator=(__uniq_ptr_data&&) = default; |
229 | }; |
230 | |
231 | template <typename _Tp, typename _Dp> |
232 | struct __uniq_ptr_data<_Tp, _Dp, false, false> : __uniq_ptr_impl<_Tp, _Dp> |
233 | { |
234 | using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl; |
235 | __uniq_ptr_data(__uniq_ptr_data&&) = delete; |
236 | __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete; |
237 | }; |
238 | /// @endcond |
239 | |
240 | /// 20.7.1.2 unique_ptr for single objects. |
241 | template <typename _Tp, typename _Dp = default_delete<_Tp>> |
242 | class unique_ptr |
243 | { |
244 | template <typename _Up> |
245 | using _DeleterConstraint = |
246 | typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type; |
247 | |
248 | __uniq_ptr_data<_Tp, _Dp> _M_t; |
249 | |
250 | public: |
251 | using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer; |
252 | using element_type = _Tp; |
253 | using deleter_type = _Dp; |
254 | |
255 | private: |
256 | // helper template for detecting a safe conversion from another |
257 | // unique_ptr |
258 | template<typename _Up, typename _Ep> |
259 | using __safe_conversion_up = __and_< |
260 | is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>, |
261 | __not_<is_array<_Up>> |
262 | >; |
263 | |
264 | public: |
265 | // Constructors. |
266 | |
267 | /// Default constructor, creates a unique_ptr that owns nothing. |
268 | template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>> |
269 | constexpr unique_ptr() noexcept |
270 | : _M_t() |
271 | { } |
272 | |
273 | /** Takes ownership of a pointer. |
274 | * |
275 | * @param __p A pointer to an object of @c element_type |
276 | * |
277 | * The deleter will be value-initialized. |
278 | */ |
279 | template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>> |
280 | explicit |
281 | unique_ptr(pointer __p) noexcept |
282 | : _M_t(__p) |
283 | { } |
284 | |
285 | /** Takes ownership of a pointer. |
286 | * |
287 | * @param __p A pointer to an object of @c element_type |
288 | * @param __d A reference to a deleter. |
289 | * |
290 | * The deleter will be initialized with @p __d |
291 | */ |
292 | template<typename _Del = deleter_type, |
293 | typename = _Require<is_copy_constructible<_Del>>> |
294 | unique_ptr(pointer __p, const deleter_type& __d) noexcept |
295 | : _M_t(__p, __d) { } |
296 | |
297 | /** Takes ownership of a pointer. |
298 | * |
299 | * @param __p A pointer to an object of @c element_type |
300 | * @param __d An rvalue reference to a (non-reference) deleter. |
301 | * |
302 | * The deleter will be initialized with @p std::move(__d) |
303 | */ |
304 | template<typename _Del = deleter_type, |
305 | typename = _Require<is_move_constructible<_Del>>> |
306 | unique_ptr(pointer __p, |
307 | __enable_if_t<!is_lvalue_reference<_Del>::value, |
308 | _Del&&> __d) noexcept |
309 | : _M_t(__p, std::move(__d)) |
310 | { } |
311 | |
312 | template<typename _Del = deleter_type, |
313 | typename _DelUnref = typename remove_reference<_Del>::type> |
314 | unique_ptr(pointer, |
315 | __enable_if_t<is_lvalue_reference<_Del>::value, |
316 | _DelUnref&&>) = delete; |
317 | |
318 | /// Creates a unique_ptr that owns nothing. |
319 | template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>> |
320 | constexpr unique_ptr(nullptr_t) noexcept |
321 | : _M_t() |
322 | { } |
323 | |
324 | // Move constructors. |
325 | |
326 | /// Move constructor. |
327 | unique_ptr(unique_ptr&&) = default; |
328 | |
329 | /** @brief Converting constructor from another type |
330 | * |
331 | * Requires that the pointer owned by @p __u is convertible to the |
332 | * type of pointer owned by this object, @p __u does not own an array, |
333 | * and @p __u has a compatible deleter type. |
334 | */ |
335 | template<typename _Up, typename _Ep, typename = _Require< |
336 | __safe_conversion_up<_Up, _Ep>, |
337 | typename conditional<is_reference<_Dp>::value, |
338 | is_same<_Ep, _Dp>, |
339 | is_convertible<_Ep, _Dp>>::type>> |
340 | unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept |
341 | : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter())) |
342 | { } |
343 | |
344 | #if _GLIBCXX_USE_DEPRECATED1 |
345 | #pragma GCC diagnostic push |
346 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
347 | /// Converting constructor from @c auto_ptr |
348 | template<typename _Up, typename = _Require< |
349 | is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>> |
350 | unique_ptr(auto_ptr<_Up>&& __u) noexcept; |
351 | #pragma GCC diagnostic pop |
352 | #endif |
353 | |
354 | /// Destructor, invokes the deleter if the stored pointer is not null. |
355 | ~unique_ptr() noexcept |
356 | { |
357 | static_assert(__is_invocable<deleter_type&, pointer>::value, |
358 | "unique_ptr's deleter must be invocable with a pointer"); |
359 | auto& __ptr = _M_t._M_ptr(); |
360 | if (__ptr != nullptr) |
361 | get_deleter()(std::move(__ptr)); |
362 | __ptr = pointer(); |
363 | } |
364 | |
365 | // Assignment. |
366 | |
367 | /** @brief Move assignment operator. |
368 | * |
369 | * Invokes the deleter if this object owns a pointer. |
370 | */ |
371 | unique_ptr& operator=(unique_ptr&&) = default; |
372 | |
373 | /** @brief Assignment from another type. |
374 | * |
375 | * @param __u The object to transfer ownership from, which owns a |
376 | * convertible pointer to a non-array object. |
377 | * |
378 | * Invokes the deleter if this object owns a pointer. |
379 | */ |
380 | template<typename _Up, typename _Ep> |
381 | typename enable_if< __and_< |
382 | __safe_conversion_up<_Up, _Ep>, |
383 | is_assignable<deleter_type&, _Ep&&> |
384 | >::value, |
385 | unique_ptr&>::type |
386 | operator=(unique_ptr<_Up, _Ep>&& __u) noexcept |
387 | { |
388 | reset(__u.release()); |
389 | get_deleter() = std::forward<_Ep>(__u.get_deleter()); |
390 | return *this; |
391 | } |
392 | |
393 | /// Reset the %unique_ptr to empty, invoking the deleter if necessary. |
394 | unique_ptr& |
395 | operator=(nullptr_t) noexcept |
396 | { |
397 | reset(); |
398 | return *this; |
399 | } |
400 | |
401 | // Observers. |
402 | |
403 | /// Dereference the stored pointer. |
404 | typename add_lvalue_reference<element_type>::type |
405 | operator*() const |
406 | { |
407 | __glibcxx_assert(get() != pointer()); |
408 | return *get(); |
409 | } |
410 | |
411 | /// Return the stored pointer. |
412 | pointer |
413 | operator->() const noexcept |
414 | { |
415 | _GLIBCXX_DEBUG_PEDASSERT(get() != pointer()); |
416 | return get(); |
417 | } |
418 | |
419 | /// Return the stored pointer. |
420 | pointer |
421 | get() const noexcept |
422 | { return _M_t._M_ptr(); } |
423 | |
424 | /// Return a reference to the stored deleter. |
425 | deleter_type& |
426 | get_deleter() noexcept |
427 | { return _M_t._M_deleter(); } |
428 | |
429 | /// Return a reference to the stored deleter. |
430 | const deleter_type& |
431 | get_deleter() const noexcept |
432 | { return _M_t._M_deleter(); } |
433 | |
434 | /// Return @c true if the stored pointer is not null. |
435 | explicit operator bool() const noexcept |
436 | { return get() == pointer() ? false : true; } |
437 | |
438 | // Modifiers. |
439 | |
440 | /// Release ownership of any stored pointer. |
441 | pointer |
442 | release() noexcept |
443 | { return _M_t.release(); } |
444 | |
445 | /** @brief Replace the stored pointer. |
446 | * |
447 | * @param __p The new pointer to store. |
448 | * |
449 | * The deleter will be invoked if a pointer is already owned. |
450 | */ |
451 | void |
452 | reset(pointer __p = pointer()) noexcept |
453 | { |
454 | static_assert(__is_invocable<deleter_type&, pointer>::value, |
455 | "unique_ptr's deleter must be invocable with a pointer"); |
456 | _M_t.reset(std::move(__p)); |
457 | } |
458 | |
459 | /// Exchange the pointer and deleter with another object. |
460 | void |
461 | swap(unique_ptr& __u) noexcept |
462 | { |
463 | static_assert(__is_swappable<_Dp>::value, "deleter must be swappable"); |
464 | _M_t.swap(__u._M_t); |
465 | } |
466 | |
467 | // Disable copy from lvalue. |
468 | unique_ptr(const unique_ptr&) = delete; |
469 | unique_ptr& operator=(const unique_ptr&) = delete; |
470 | }; |
471 | |
472 | /// 20.7.1.3 unique_ptr for array objects with a runtime length |
473 | // [unique.ptr.runtime] |
474 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
475 | // DR 740 - omit specialization for array objects with a compile time length |
476 | template<typename _Tp, typename _Dp> |
477 | class unique_ptr<_Tp[], _Dp> |
478 | { |
479 | template <typename _Up> |
480 | using _DeleterConstraint = |
481 | typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type; |
482 | |
483 | __uniq_ptr_data<_Tp, _Dp> _M_t; |
484 | |
485 | template<typename _Up> |
486 | using __remove_cv = typename remove_cv<_Up>::type; |
487 | |
488 | // like is_base_of<_Tp, _Up> but false if unqualified types are the same |
489 | template<typename _Up> |
490 | using __is_derived_Tp |
491 | = __and_< is_base_of<_Tp, _Up>, |
492 | __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >; |
493 | |
494 | public: |
495 | using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer; |
496 | using element_type = _Tp; |
497 | using deleter_type = _Dp; |
498 | |
499 | // helper template for detecting a safe conversion from another |
500 | // unique_ptr |
501 | template<typename _Up, typename _Ep, |
502 | typename _UPtr = unique_ptr<_Up, _Ep>, |
503 | typename _UP_pointer = typename _UPtr::pointer, |
504 | typename _UP_element_type = typename _UPtr::element_type> |
505 | using __safe_conversion_up = __and_< |
506 | is_array<_Up>, |
507 | is_same<pointer, element_type*>, |
508 | is_same<_UP_pointer, _UP_element_type*>, |
509 | is_convertible<_UP_element_type(*)[], element_type(*)[]> |
510 | >; |
511 | |
512 | // helper template for detecting a safe conversion from a raw pointer |
513 | template<typename _Up> |
514 | using __safe_conversion_raw = __and_< |
515 | __or_<__or_<is_same<_Up, pointer>, |
516 | is_same<_Up, nullptr_t>>, |
517 | __and_<is_pointer<_Up>, |
518 | is_same<pointer, element_type*>, |
519 | is_convertible< |
520 | typename remove_pointer<_Up>::type(*)[], |
521 | element_type(*)[]> |
522 | > |
523 | > |
524 | >; |
525 | |
526 | // Constructors. |
527 | |
528 | /// Default constructor, creates a unique_ptr that owns nothing. |
529 | template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>> |
530 | constexpr unique_ptr() noexcept |
531 | : _M_t() |
532 | { } |
533 | |
534 | /** Takes ownership of a pointer. |
535 | * |
536 | * @param __p A pointer to an array of a type safely convertible |
537 | * to an array of @c element_type |
538 | * |
539 | * The deleter will be value-initialized. |
540 | */ |
541 | template<typename _Up, |
542 | typename _Vp = _Dp, |
543 | typename = _DeleterConstraint<_Vp>, |
544 | typename = typename enable_if< |
545 | __safe_conversion_raw<_Up>::value, bool>::type> |
546 | explicit |
547 | unique_ptr(_Up __p) noexcept |
548 | : _M_t(__p) |
549 | { } |
550 | |
551 | /** Takes ownership of a pointer. |
552 | * |
553 | * @param __p A pointer to an array of a type safely convertible |
554 | * to an array of @c element_type |
555 | * @param __d A reference to a deleter. |
556 | * |
557 | * The deleter will be initialized with @p __d |
558 | */ |
559 | template<typename _Up, typename _Del = deleter_type, |
560 | typename = _Require<__safe_conversion_raw<_Up>, |
561 | is_copy_constructible<_Del>>> |
562 | unique_ptr(_Up __p, const deleter_type& __d) noexcept |
563 | : _M_t(__p, __d) { } |
564 | |
565 | /** Takes ownership of a pointer. |
566 | * |
567 | * @param __p A pointer to an array of a type safely convertible |
568 | * to an array of @c element_type |
569 | * @param __d A reference to a deleter. |
570 | * |
571 | * The deleter will be initialized with @p std::move(__d) |
572 | */ |
573 | template<typename _Up, typename _Del = deleter_type, |
574 | typename = _Require<__safe_conversion_raw<_Up>, |
575 | is_move_constructible<_Del>>> |
576 | unique_ptr(_Up __p, |
577 | __enable_if_t<!is_lvalue_reference<_Del>::value, |
578 | _Del&&> __d) noexcept |
579 | : _M_t(std::move(__p), std::move(__d)) |
580 | { } |
581 | |
582 | template<typename _Up, typename _Del = deleter_type, |
583 | typename _DelUnref = typename remove_reference<_Del>::type, |
584 | typename = _Require<__safe_conversion_raw<_Up>>> |
585 | unique_ptr(_Up, |
586 | __enable_if_t<is_lvalue_reference<_Del>::value, |
587 | _DelUnref&&>) = delete; |
588 | |
589 | /// Move constructor. |
590 | unique_ptr(unique_ptr&&) = default; |
591 | |
592 | /// Creates a unique_ptr that owns nothing. |
593 | template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>> |
594 | constexpr unique_ptr(nullptr_t) noexcept |
595 | : _M_t() |
596 | { } |
597 | |
598 | template<typename _Up, typename _Ep, typename = _Require< |
599 | __safe_conversion_up<_Up, _Ep>, |
600 | typename conditional<is_reference<_Dp>::value, |
601 | is_same<_Ep, _Dp>, |
602 | is_convertible<_Ep, _Dp>>::type>> |
603 | unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept |
604 | : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter())) |
605 | { } |
606 | |
607 | /// Destructor, invokes the deleter if the stored pointer is not null. |
608 | ~unique_ptr() |
609 | { |
610 | auto& __ptr = _M_t._M_ptr(); |
611 | if (__ptr != nullptr) |
612 | get_deleter()(__ptr); |
613 | __ptr = pointer(); |
614 | } |
615 | |
616 | // Assignment. |
617 | |
618 | /** @brief Move assignment operator. |
619 | * |
620 | * Invokes the deleter if this object owns a pointer. |
621 | */ |
622 | unique_ptr& |
623 | operator=(unique_ptr&&) = default; |
624 | |
625 | /** @brief Assignment from another type. |
626 | * |
627 | * @param __u The object to transfer ownership from, which owns a |
628 | * convertible pointer to an array object. |
629 | * |
630 | * Invokes the deleter if this object owns a pointer. |
631 | */ |
632 | template<typename _Up, typename _Ep> |
633 | typename |
634 | enable_if<__and_<__safe_conversion_up<_Up, _Ep>, |
635 | is_assignable<deleter_type&, _Ep&&> |
636 | >::value, |
637 | unique_ptr&>::type |
638 | operator=(unique_ptr<_Up, _Ep>&& __u) noexcept |
639 | { |
640 | reset(__u.release()); |
641 | get_deleter() = std::forward<_Ep>(__u.get_deleter()); |
642 | return *this; |
643 | } |
644 | |
645 | /// Reset the %unique_ptr to empty, invoking the deleter if necessary. |
646 | unique_ptr& |
647 | operator=(nullptr_t) noexcept |
648 | { |
649 | reset(); |
650 | return *this; |
651 | } |
652 | |
653 | // Observers. |
654 | |
655 | /// Access an element of owned array. |
656 | typename std::add_lvalue_reference<element_type>::type |
657 | operator[](size_t __i) const |
658 | { |
659 | __glibcxx_assert(get() != pointer()); |
660 | return get()[__i]; |
661 | } |
662 | |
663 | /// Return the stored pointer. |
664 | pointer |
665 | get() const noexcept |
666 | { return _M_t._M_ptr(); } |
667 | |
668 | /// Return a reference to the stored deleter. |
669 | deleter_type& |
670 | get_deleter() noexcept |
671 | { return _M_t._M_deleter(); } |
672 | |
673 | /// Return a reference to the stored deleter. |
674 | const deleter_type& |
675 | get_deleter() const noexcept |
676 | { return _M_t._M_deleter(); } |
677 | |
678 | /// Return @c true if the stored pointer is not null. |
679 | explicit operator bool() const noexcept |
680 | { return get() == pointer() ? false : true; } |
681 | |
682 | // Modifiers. |
683 | |
684 | /// Release ownership of any stored pointer. |
685 | pointer |
686 | release() noexcept |
687 | { return _M_t.release(); } |
688 | |
689 | /** @brief Replace the stored pointer. |
690 | * |
691 | * @param __p The new pointer to store. |
692 | * |
693 | * The deleter will be invoked if a pointer is already owned. |
694 | */ |
695 | template <typename _Up, |
696 | typename = _Require< |
697 | __or_<is_same<_Up, pointer>, |
698 | __and_<is_same<pointer, element_type*>, |
699 | is_pointer<_Up>, |
700 | is_convertible< |
701 | typename remove_pointer<_Up>::type(*)[], |
702 | element_type(*)[] |
703 | > |
704 | > |
705 | > |
706 | >> |
707 | void |
708 | reset(_Up __p) noexcept |
709 | { _M_t.reset(std::move(__p)); } |
710 | |
711 | void reset(nullptr_t = nullptr) noexcept |
712 | { reset(pointer()); } |
713 | |
714 | /// Exchange the pointer and deleter with another object. |
715 | void |
716 | swap(unique_ptr& __u) noexcept |
717 | { |
718 | static_assert(__is_swappable<_Dp>::value, "deleter must be swappable"); |
719 | _M_t.swap(__u._M_t); |
720 | } |
721 | |
722 | // Disable copy from lvalue. |
723 | unique_ptr(const unique_ptr&) = delete; |
724 | unique_ptr& operator=(const unique_ptr&) = delete; |
725 | }; |
726 | |
727 | /// @relates unique_ptr @{ |
728 | |
729 | /// Swap overload for unique_ptr |
730 | template<typename _Tp, typename _Dp> |
731 | inline |
732 | #if __cplusplus201402L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11 |
733 | // Constrained free swap overload, see p0185r1 |
734 | typename enable_if<__is_swappable<_Dp>::value>::type |
735 | #else |
736 | void |
737 | #endif |
738 | swap(unique_ptr<_Tp, _Dp>& __x, |
739 | unique_ptr<_Tp, _Dp>& __y) noexcept |
740 | { __x.swap(__y); } |
741 | |
742 | #if __cplusplus201402L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11 |
743 | template<typename _Tp, typename _Dp> |
744 | typename enable_if<!__is_swappable<_Dp>::value>::type |
745 | swap(unique_ptr<_Tp, _Dp>&, |
746 | unique_ptr<_Tp, _Dp>&) = delete; |
747 | #endif |
748 | |
749 | /// Equality operator for unique_ptr objects, compares the owned pointers |
750 | template<typename _Tp, typename _Dp, |
751 | typename _Up, typename _Ep> |
752 | _GLIBCXX_NODISCARD inline bool |
753 | operator==(const unique_ptr<_Tp, _Dp>& __x, |
754 | const unique_ptr<_Up, _Ep>& __y) |
755 | { return __x.get() == __y.get(); } |
756 | |
757 | /// unique_ptr comparison with nullptr |
758 | template<typename _Tp, typename _Dp> |
759 | _GLIBCXX_NODISCARD inline bool |
760 | operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept |
761 | { return !__x; } |
762 | |
763 | #ifndef __cpp_lib_three_way_comparison |
764 | /// unique_ptr comparison with nullptr |
765 | template<typename _Tp, typename _Dp> |
766 | _GLIBCXX_NODISCARD inline bool |
767 | operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept |
768 | { return !__x; } |
769 | |
770 | /// Inequality operator for unique_ptr objects, compares the owned pointers |
771 | template<typename _Tp, typename _Dp, |
772 | typename _Up, typename _Ep> |
773 | _GLIBCXX_NODISCARD inline bool |
774 | operator!=(const unique_ptr<_Tp, _Dp>& __x, |
775 | const unique_ptr<_Up, _Ep>& __y) |
776 | { return __x.get() != __y.get(); } |
777 | |
778 | /// unique_ptr comparison with nullptr |
779 | template<typename _Tp, typename _Dp> |
780 | _GLIBCXX_NODISCARD inline bool |
781 | operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept |
782 | { return (bool)__x; } |
783 | |
784 | /// unique_ptr comparison with nullptr |
785 | template<typename _Tp, typename _Dp> |
786 | _GLIBCXX_NODISCARD inline bool |
787 | operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept |
788 | { return (bool)__x; } |
789 | #endif // three way comparison |
790 | |
791 | /// Relational operator for unique_ptr objects, compares the owned pointers |
792 | template<typename _Tp, typename _Dp, |
793 | typename _Up, typename _Ep> |
794 | _GLIBCXX_NODISCARD inline bool |
795 | operator<(const unique_ptr<_Tp, _Dp>& __x, |
796 | const unique_ptr<_Up, _Ep>& __y) |
797 | { |
798 | typedef typename |
799 | std::common_type<typename unique_ptr<_Tp, _Dp>::pointer, |
800 | typename unique_ptr<_Up, _Ep>::pointer>::type _CT; |
801 | return std::less<_CT>()(__x.get(), __y.get()); |
802 | } |
803 | |
804 | /// unique_ptr comparison with nullptr |
805 | template<typename _Tp, typename _Dp> |
806 | _GLIBCXX_NODISCARD inline bool |
807 | operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
808 | { |
809 | return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(), |
810 | nullptr); |
811 | } |
812 | |
813 | /// unique_ptr comparison with nullptr |
814 | template<typename _Tp, typename _Dp> |
815 | _GLIBCXX_NODISCARD inline bool |
816 | operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) |
817 | { |
818 | return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr, |
819 | __x.get()); |
820 | } |
821 | |
822 | /// Relational operator for unique_ptr objects, compares the owned pointers |
823 | template<typename _Tp, typename _Dp, |
824 | typename _Up, typename _Ep> |
825 | _GLIBCXX_NODISCARD inline bool |
826 | operator<=(const unique_ptr<_Tp, _Dp>& __x, |
827 | const unique_ptr<_Up, _Ep>& __y) |
828 | { return !(__y < __x); } |
829 | |
830 | /// unique_ptr comparison with nullptr |
831 | template<typename _Tp, typename _Dp> |
832 | _GLIBCXX_NODISCARD inline bool |
833 | operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
834 | { return !(nullptr < __x); } |
835 | |
836 | /// unique_ptr comparison with nullptr |
837 | template<typename _Tp, typename _Dp> |
838 | _GLIBCXX_NODISCARD inline bool |
839 | operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) |
840 | { return !(__x < nullptr); } |
841 | |
842 | /// Relational operator for unique_ptr objects, compares the owned pointers |
843 | template<typename _Tp, typename _Dp, |
844 | typename _Up, typename _Ep> |
845 | _GLIBCXX_NODISCARD inline bool |
846 | operator>(const unique_ptr<_Tp, _Dp>& __x, |
847 | const unique_ptr<_Up, _Ep>& __y) |
848 | { return (__y < __x); } |
849 | |
850 | /// unique_ptr comparison with nullptr |
851 | template<typename _Tp, typename _Dp> |
852 | _GLIBCXX_NODISCARD inline bool |
853 | operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
854 | { |
855 | return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr, |
856 | __x.get()); |
857 | } |
858 | |
859 | /// unique_ptr comparison with nullptr |
860 | template<typename _Tp, typename _Dp> |
861 | _GLIBCXX_NODISCARD inline bool |
862 | operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) |
863 | { |
864 | return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(), |
865 | nullptr); |
866 | } |
867 | |
868 | /// Relational operator for unique_ptr objects, compares the owned pointers |
869 | template<typename _Tp, typename _Dp, |
870 | typename _Up, typename _Ep> |
871 | _GLIBCXX_NODISCARD inline bool |
872 | operator>=(const unique_ptr<_Tp, _Dp>& __x, |
873 | const unique_ptr<_Up, _Ep>& __y) |
874 | { return !(__x < __y); } |
875 | |
876 | /// unique_ptr comparison with nullptr |
877 | template<typename _Tp, typename _Dp> |
878 | _GLIBCXX_NODISCARD inline bool |
879 | operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
880 | { return !(__x < nullptr); } |
881 | |
882 | /// unique_ptr comparison with nullptr |
883 | template<typename _Tp, typename _Dp> |
884 | _GLIBCXX_NODISCARD inline bool |
885 | operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) |
886 | { return !(nullptr < __x); } |
887 | |
888 | #ifdef __cpp_lib_three_way_comparison |
889 | template<typename _Tp, typename _Dp, typename _Up, typename _Ep> |
890 | requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer, |
891 | typename unique_ptr<_Up, _Ep>::pointer> |
892 | inline |
893 | compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer, |
894 | typename unique_ptr<_Up, _Ep>::pointer> |
895 | operator<=>(const unique_ptr<_Tp, _Dp>& __x, |
896 | const unique_ptr<_Up, _Ep>& __y) |
897 | { return compare_three_way()(__x.get(), __y.get()); } |
898 | |
899 | template<typename _Tp, typename _Dp> |
900 | requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer> |
901 | inline |
902 | compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer> |
903 | operator<=>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
904 | { |
905 | using pointer = typename unique_ptr<_Tp, _Dp>::pointer; |
906 | return compare_three_way()(__x.get(), static_cast<pointer>(nullptr)); |
907 | } |
908 | #endif |
909 | // @} relates unique_ptr |
910 | |
911 | /// @cond undocumented |
912 | template<typename _Up, typename _Ptr = typename _Up::pointer, |
913 | bool = __poison_hash<_Ptr>::__enable_hash_call> |
914 | struct __uniq_ptr_hash |
915 | #if ! _GLIBCXX_INLINE_VERSION0 |
916 | : private __poison_hash<_Ptr> |
917 | #endif |
918 | { |
919 | size_t |
920 | operator()(const _Up& __u) const |
921 | noexcept(noexcept(std::declval<hash<_Ptr>>()(std::declval<_Ptr>()))) |
922 | { return hash<_Ptr>()(__u.get()); } |
923 | }; |
924 | |
925 | template<typename _Up, typename _Ptr> |
926 | struct __uniq_ptr_hash<_Up, _Ptr, false> |
927 | : private __poison_hash<_Ptr> |
928 | { }; |
929 | /// @endcond |
930 | |
931 | /// std::hash specialization for unique_ptr. |
932 | template<typename _Tp, typename _Dp> |
933 | struct hash<unique_ptr<_Tp, _Dp>> |
934 | : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>, |
935 | public __uniq_ptr_hash<unique_ptr<_Tp, _Dp>> |
936 | { }; |
937 | |
938 | #if __cplusplus201402L >= 201402L |
939 | /// @relates unique_ptr @{ |
940 | #define __cpp_lib_make_unique201304 201304 |
941 | |
942 | /// @cond undocumented |
943 | |
944 | template<typename _Tp> |
945 | struct _MakeUniq |
946 | { typedef unique_ptr<_Tp> __single_object; }; |
947 | |
948 | template<typename _Tp> |
949 | struct _MakeUniq<_Tp[]> |
950 | { typedef unique_ptr<_Tp[]> __array; }; |
951 | |
952 | template<typename _Tp, size_t _Bound> |
953 | struct _MakeUniq<_Tp[_Bound]> |
954 | { struct __invalid_type { }; }; |
955 | |
956 | /// @endcond |
957 | |
958 | /// std::make_unique for single objects |
959 | template<typename _Tp, typename... _Args> |
960 | inline typename _MakeUniq<_Tp>::__single_object |
961 | make_unique(_Args&&... __args) |
962 | { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); } |
963 | |
964 | /// std::make_unique for arrays of unknown bound |
965 | template<typename _Tp> |
966 | inline typename _MakeUniq<_Tp>::__array |
967 | make_unique(size_t __num) |
968 | { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); } |
969 | |
970 | /// Disable std::make_unique for arrays of known bound |
971 | template<typename _Tp, typename... _Args> |
972 | inline typename _MakeUniq<_Tp>::__invalid_type |
973 | make_unique(_Args&&...) = delete; |
974 | // @} relates unique_ptr |
975 | #endif // C++14 |
976 | |
977 | #if __cplusplus201402L > 201703L && __cpp_concepts |
978 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
979 | // 2948. unique_ptr does not define operator<< for stream output |
980 | /// Stream output operator for unique_ptr |
981 | template<typename _CharT, typename _Traits, typename _Tp, typename _Dp> |
982 | inline basic_ostream<_CharT, _Traits>& |
983 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
984 | const unique_ptr<_Tp, _Dp>& __p) |
985 | requires requires { __os << __p.get(); } |
986 | { |
987 | __os << __p.get(); |
988 | return __os; |
989 | } |
990 | #endif // C++20 |
991 | |
992 | // @} group pointer_abstractions |
993 | |
994 | #if __cplusplus201402L >= 201703L |
995 | namespace __detail::__variant |
996 | { |
997 | template<typename> struct _Never_valueless_alt; // see <variant> |
998 | |
999 | // Provide the strong exception-safety guarantee when emplacing a |
1000 | // unique_ptr into a variant. |
1001 | template<typename _Tp, typename _Del> |
1002 | struct _Never_valueless_alt<std::unique_ptr<_Tp, _Del>> |
1003 | : std::true_type |
1004 | { }; |
1005 | } // namespace __detail::__variant |
1006 | #endif // C++17 |
1007 | |
1008 | _GLIBCXX_END_NAMESPACE_VERSION |
1009 | } // namespace |
1010 | |
1011 | #endif /* _UNIQUE_PTR_H */ |