Bug Summary

File:llvm/lib/Support/VirtualFileSystem.cpp
Warning:line 1424, column 14
The left operand of '==' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name VirtualFileSystem.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/lib/Support -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/include -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/lib/Support -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2019-12-09-002921-48462-1 -x c++ /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp
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
56using namespace llvm;
57using namespace llvm::vfs;
58
59using llvm::sys::fs::file_t;
60using llvm::sys::fs::file_status;
61using llvm::sys::fs::file_type;
62using llvm::sys::fs::kInvalidFile;
63using llvm::sys::fs::perms;
64using llvm::sys::fs::UniqueID;
65
66Status::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
71Status::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
77Status 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
83Status 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
89bool Status::equivalent(const Status &Other) const {
90 assert(isStatusKnown() && Other.isStatusKnown())((isStatusKnown() && Other.isStatusKnown()) ? static_cast
<void> (0) : __assert_fail ("isStatusKnown() && Other.isStatusKnown()"
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 90, __PRETTY_FUNCTION__))
;
91 return getUniqueID() == Other.getUniqueID();
92}
93
94bool Status::isDirectory() const { return Type == file_type::directory_file; }
95
96bool Status::isRegularFile() const { return Type == file_type::regular_file; }
97
98bool Status::isOther() const {
99 return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
100}
101
102bool Status::isSymlink() const { return Type == file_type::symlink_file; }
103
104bool Status::isStatusKnown() const { return Type != file_type::status_error; }
105
106bool Status::exists() const {
107 return isStatusKnown() && Type != file_type::file_not_found;
108}
109
110File::~File() = default;
111
112FileSystem::~FileSystem() = default;
113
114ErrorOr<std::unique_ptr<MemoryBuffer>>
115FileSystem::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
124std::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
136std::error_code FileSystem::getRealPath(const Twine &Path,
137 SmallVectorImpl<char> &Output) const {
138 return errc::operation_not_permitted;
139}
140
141std::error_code FileSystem::isLocal(const Twine &Path, bool &Result) {
142 return errc::operation_not_permitted;
143}
144
145bool FileSystem::exists(const Twine &Path) {
146 auto Status = status(Path);
147 return Status && Status->exists();
148}
149
150#ifndef NDEBUG
151static bool isTraversalComponent(StringRef Component) {
152 return Component.equals("..") || Component.equals(".");
153}
154
155static 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
169namespace {
170
171/// Wrapper around a raw file descriptor.
172class 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")((FD != kInvalidFile && "Invalid or inactive file descriptor"
) ? static_cast<void> (0) : __assert_fail ("FD != kInvalidFile && \"Invalid or inactive file descriptor\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 183, __PRETTY_FUNCTION__))
;
184 }
185
186public:
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
200RealFile::~RealFile() { close(); }
201
202ErrorOr<Status> RealFile::status() {
203 assert(FD != kInvalidFile && "cannot stat closed file")((FD != kInvalidFile && "cannot stat closed file") ? static_cast
<void> (0) : __assert_fail ("FD != kInvalidFile && \"cannot stat closed file\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 203, __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
213ErrorOr<std::string> RealFile::getName() {
214 return RealName.empty() ? S.getName().str() : RealName;
215}
216
217ErrorOr<std::unique_ptr<MemoryBuffer>>
218RealFile::getBuffer(const Twine &Name, int64_t FileSize,
219 bool RequiresNullTerminator, bool IsVolatile) {
220 assert(FD != kInvalidFile && "cannot get buffer for closed file")((FD != kInvalidFile && "cannot get buffer for closed file"
) ? static_cast<void> (0) : __assert_fail ("FD != kInvalidFile && \"cannot get buffer for closed file\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 220, __PRETTY_FUNCTION__))
;
221 return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator,
222 IsVolatile);
223}
224
225std::error_code RealFile::close() {
226 std::error_code EC = sys::fs::closeFile(FD);
227 FD = kInvalidFile;
228 return EC;
229}
230
231namespace {
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.
241class RealFileSystem : public FileSystem {
242public:
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
265private:
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
287ErrorOr<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
296ErrorOr<std::unique_ptr<File>>
297RealFileSystem::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
307llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const {
308 if (WD)
309 return 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 Dir.str();
315}
316
317std::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
334std::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
339std::error_code
340RealFileSystem::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
346IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
347 static IntrusiveRefCntPtr<FileSystem> FS(new RealFileSystem(true));
348 return FS;
349}
350
351std::unique_ptr<FileSystem> vfs::createPhysicalFileSystem() {
352 return std::make_unique<RealFileSystem>(false);
353}
354
355namespace {
356
357class RealFSDirIter : public llvm::vfs::detail::DirIterImpl {
358 llvm::sys::fs::directory_iterator Iter;
359
360public:
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
378directory_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
389OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
390 FSList.push_back(std::move(BaseFS));
391}
392
393void 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
400ErrorOr<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
410ErrorOr<std::unique_ptr<File>>
411OverlayFileSystem::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
421llvm::ErrorOr<std::string>
422OverlayFileSystem::getCurrentWorkingDirectory() const {
423 // All file systems are synchronized, just take the first working directory.
424 return FSList.front()->getCurrentWorkingDirectory();
425}
426
427std::error_code
428OverlayFileSystem::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
435std::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
442std::error_code
443OverlayFileSystem::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
451llvm::vfs::detail::DirIterImpl::~DirIterImpl() = default;
452
453namespace {
454
455class OverlayFSDirIterImpl : public llvm::vfs::detail::DirIterImpl {
456 OverlayFileSystem &Overlays;
457 std::string Path;
458 OverlayFileSystem::iterator CurrentFS;
459 directory_iterator CurrentDirIter;
460 llvm::StringSet<> SeenNames;
461
462 std::error_code incrementFS() {
463 assert(CurrentFS != Overlays.overlays_end() && "incrementing past end")((CurrentFS != Overlays.overlays_end() && "incrementing past end"
) ? static_cast<void> (0) : __assert_fail ("CurrentFS != Overlays.overlays_end() && \"incrementing past end\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 463, __PRETTY_FUNCTION__))
;
464 ++CurrentFS;
465 for (auto E = Overlays.overlays_end(); CurrentFS != E; ++CurrentFS) {
466 std::error_code EC;
467 CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
468 if (EC && EC != errc::no_such_file_or_directory)
469 return EC;
470 if (CurrentDirIter != directory_iterator())
471 break; // found
472 }
473 return {};
474 }
475
476 std::error_code incrementDirIter(bool IsFirstTime) {
477 assert((IsFirstTime || CurrentDirIter != directory_iterator()) &&(((IsFirstTime || CurrentDirIter != directory_iterator()) &&
"incrementing past end") ? static_cast<void> (0) : __assert_fail
("(IsFirstTime || CurrentDirIter != directory_iterator()) && \"incrementing past end\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 478, __PRETTY_FUNCTION__))
478 "incrementing past end")(((IsFirstTime || CurrentDirIter != directory_iterator()) &&
"incrementing past end") ? static_cast<void> (0) : __assert_fail
("(IsFirstTime || CurrentDirIter != directory_iterator()) && \"incrementing past end\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 478, __PRETTY_FUNCTION__))
;
479 std::error_code EC;
480 if (!IsFirstTime)
481 CurrentDirIter.increment(EC);
482 if (!EC && CurrentDirIter == directory_iterator())
483 EC = incrementFS();
484 return EC;
485 }
486
487 std::error_code incrementImpl(bool IsFirstTime) {
488 while (true) {
489 std::error_code EC = incrementDirIter(IsFirstTime);
490 if (EC || CurrentDirIter == directory_iterator()) {
491 CurrentEntry = directory_entry();
492 return EC;
493 }
494 CurrentEntry = *CurrentDirIter;
495 StringRef Name = llvm::sys::path::filename(CurrentEntry.path());
496 if (SeenNames.insert(Name).second)
497 return EC; // name not seen before
498 }
499 llvm_unreachable("returned above")::llvm::llvm_unreachable_internal("returned above", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 499)
;
500 }
501
502public:
503 OverlayFSDirIterImpl(const Twine &Path, OverlayFileSystem &FS,
504 std::error_code &EC)
505 : Overlays(FS), Path(Path.str()), CurrentFS(Overlays.overlays_begin()) {
506 CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
507 EC = incrementImpl(true);
508 }
509
510 std::error_code increment() override { return incrementImpl(false); }
511};
512
513} // namespace
514
515directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir,
516 std::error_code &EC) {
517 return directory_iterator(
518 std::make_shared<OverlayFSDirIterImpl>(Dir, *this, EC));
519}
520
521void ProxyFileSystem::anchor() {}
522
523namespace llvm {
524namespace vfs {
525
526namespace detail {
527
528enum InMemoryNodeKind { IME_File, IME_Directory, IME_HardLink };
529
530/// The in memory file system is a tree of Nodes. Every node can either be a
531/// file , hardlink or a directory.
532class InMemoryNode {
533 InMemoryNodeKind Kind;
534 std::string FileName;
535
536public:
537 InMemoryNode(llvm::StringRef FileName, InMemoryNodeKind Kind)
538 : Kind(Kind), FileName(llvm::sys::path::filename(FileName)) {}
539 virtual ~InMemoryNode() = default;
540
541 /// Get the filename of this node (the name without the directory part).
542 StringRef getFileName() const { return FileName; }
543 InMemoryNodeKind getKind() const { return Kind; }
544 virtual std::string toString(unsigned Indent) const = 0;
545};
546
547class InMemoryFile : public InMemoryNode {
548 Status Stat;
549 std::unique_ptr<llvm::MemoryBuffer> Buffer;
550
551public:
552 InMemoryFile(Status Stat, std::unique_ptr<llvm::MemoryBuffer> Buffer)
553 : InMemoryNode(Stat.getName(), IME_File), Stat(std::move(Stat)),
554 Buffer(std::move(Buffer)) {}
555
556 /// Return the \p Status for this node. \p RequestedName should be the name
557 /// through which the caller referred to this node. It will override
558 /// \p Status::Name in the return value, to mimic the behavior of \p RealFile.
559 Status getStatus(const Twine &RequestedName) const {
560 return Status::copyWithNewName(Stat, RequestedName);
561 }
562 llvm::MemoryBuffer *getBuffer() const { return Buffer.get(); }
563
564 std::string toString(unsigned Indent) const override {
565 return (std::string(Indent, ' ') + Stat.getName() + "\n").str();
566 }
567
568 static bool classof(const InMemoryNode *N) {
569 return N->getKind() == IME_File;
570 }
571};
572
573namespace {
574
575class InMemoryHardLink : public InMemoryNode {
576 const InMemoryFile &ResolvedFile;
577
578public:
579 InMemoryHardLink(StringRef Path, const InMemoryFile &ResolvedFile)
580 : InMemoryNode(Path, IME_HardLink), ResolvedFile(ResolvedFile) {}
581 const InMemoryFile &getResolvedFile() const { return ResolvedFile; }
582
583 std::string toString(unsigned Indent) const override {
584 return std::string(Indent, ' ') + "HardLink to -> " +
585 ResolvedFile.toString(0);
586 }
587
588 static bool classof(const InMemoryNode *N) {
589 return N->getKind() == IME_HardLink;
590 }
591};
592
593/// Adapt a InMemoryFile for VFS' File interface. The goal is to make
594/// \p InMemoryFileAdaptor mimic as much as possible the behavior of
595/// \p RealFile.
596class InMemoryFileAdaptor : public File {
597 const InMemoryFile &Node;
598 /// The name to use when returning a Status for this file.
599 std::string RequestedName;
600
601public:
602 explicit InMemoryFileAdaptor(const InMemoryFile &Node,
603 std::string RequestedName)
604 : Node(Node), RequestedName(std::move(RequestedName)) {}
605
606 llvm::ErrorOr<Status> status() override {
607 return Node.getStatus(RequestedName);
608 }
609
610 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
611 getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
612 bool IsVolatile) override {
613 llvm::MemoryBuffer *Buf = Node.getBuffer();
614 return llvm::MemoryBuffer::getMemBuffer(
615 Buf->getBuffer(), Buf->getBufferIdentifier(), RequiresNullTerminator);
616 }
617
618 std::error_code close() override { return {}; }
619};
620} // namespace
621
622class InMemoryDirectory : public InMemoryNode {
623 Status Stat;
624 llvm::StringMap<std::unique_ptr<InMemoryNode>> Entries;
625
626public:
627 InMemoryDirectory(Status Stat)
628 : InMemoryNode(Stat.getName(), IME_Directory), Stat(std::move(Stat)) {}
629
630 /// Return the \p Status for this node. \p RequestedName should be the name
631 /// through which the caller referred to this node. It will override
632 /// \p Status::Name in the return value, to mimic the behavior of \p RealFile.
633 Status getStatus(const Twine &RequestedName) const {
634 return Status::copyWithNewName(Stat, RequestedName);
635 }
636 InMemoryNode *getChild(StringRef Name) {
637 auto I = Entries.find(Name);
638 if (I != Entries.end())
639 return I->second.get();
640 return nullptr;
641 }
642
643 InMemoryNode *addChild(StringRef Name, std::unique_ptr<InMemoryNode> Child) {
644 return Entries.insert(make_pair(Name, std::move(Child)))
645 .first->second.get();
646 }
647
648 using const_iterator = decltype(Entries)::const_iterator;
649
650 const_iterator begin() const { return Entries.begin(); }
651 const_iterator end() const { return Entries.end(); }
652
653 std::string toString(unsigned Indent) const override {
654 std::string Result =
655 (std::string(Indent, ' ') + Stat.getName() + "\n").str();
656 for (const auto &Entry : Entries)
657 Result += Entry.second->toString(Indent + 2);
658 return Result;
659 }
660
661 static bool classof(const InMemoryNode *N) {
662 return N->getKind() == IME_Directory;
663 }
664};
665
666namespace {
667Status getNodeStatus(const InMemoryNode *Node, const Twine &RequestedName) {
668 if (auto Dir = dyn_cast<detail::InMemoryDirectory>(Node))
669 return Dir->getStatus(RequestedName);
670 if (auto File = dyn_cast<detail::InMemoryFile>(Node))
671 return File->getStatus(RequestedName);
672 if (auto Link = dyn_cast<detail::InMemoryHardLink>(Node))
673 return Link->getResolvedFile().getStatus(RequestedName);
674 llvm_unreachable("Unknown node type")::llvm::llvm_unreachable_internal("Unknown node type", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 674)
;
675}
676} // namespace
677} // namespace detail
678
679InMemoryFileSystem::InMemoryFileSystem(bool UseNormalizedPaths)
680 : Root(new detail::InMemoryDirectory(
681 Status("", getNextVirtualUniqueID(), llvm::sys::TimePoint<>(), 0, 0,
682 0, llvm::sys::fs::file_type::directory_file,
683 llvm::sys::fs::perms::all_all))),
684 UseNormalizedPaths(UseNormalizedPaths) {}
685
686InMemoryFileSystem::~InMemoryFileSystem() = default;
687
688std::string InMemoryFileSystem::toString() const {
689 return Root->toString(/*Indent=*/0);
690}
691
692bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
693 std::unique_ptr<llvm::MemoryBuffer> Buffer,
694 Optional<uint32_t> User,
695 Optional<uint32_t> Group,
696 Optional<llvm::sys::fs::file_type> Type,
697 Optional<llvm::sys::fs::perms> Perms,
698 const detail::InMemoryFile *HardLinkTarget) {
699 SmallString<128> Path;
700 P.toVector(Path);
701
702 // Fix up relative paths. This just prepends the current working directory.
703 std::error_code EC = makeAbsolute(Path);
704 assert(!EC)((!EC) ? static_cast<void> (0) : __assert_fail ("!EC", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 704, __PRETTY_FUNCTION__))
;
705 (void)EC;
706
707 if (useNormalizedPaths())
708 llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
709
710 if (Path.empty())
711 return false;
712
713 detail::InMemoryDirectory *Dir = Root.get();
714 auto I = llvm::sys::path::begin(Path), E = sys::path::end(Path);
715 const auto ResolvedUser = User.getValueOr(0);
716 const auto ResolvedGroup = Group.getValueOr(0);
717 const auto ResolvedType = Type.getValueOr(sys::fs::file_type::regular_file);
718 const auto ResolvedPerms = Perms.getValueOr(sys::fs::all_all);
719 assert(!(HardLinkTarget && Buffer) && "HardLink cannot have a buffer")((!(HardLinkTarget && Buffer) && "HardLink cannot have a buffer"
) ? static_cast<void> (0) : __assert_fail ("!(HardLinkTarget && Buffer) && \"HardLink cannot have a buffer\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 719, __PRETTY_FUNCTION__))
;
720 // Any intermediate directories we create should be accessible by
721 // the owner, even if Perms says otherwise for the final path.
722 const auto NewDirectoryPerms = ResolvedPerms | sys::fs::owner_all;
723 while (true) {
724 StringRef Name = *I;
725 detail::InMemoryNode *Node = Dir->getChild(Name);
726 ++I;
727 if (!Node) {
728 if (I == E) {
729 // End of the path.
730 std::unique_ptr<detail::InMemoryNode> Child;
731 if (HardLinkTarget)
732 Child.reset(new detail::InMemoryHardLink(P.str(), *HardLinkTarget));
733 else {
734 // Create a new file or directory.
735 Status Stat(P.str(), getNextVirtualUniqueID(),
736 llvm::sys::toTimePoint(ModificationTime), ResolvedUser,
737 ResolvedGroup, Buffer->getBufferSize(), ResolvedType,
738 ResolvedPerms);
739 if (ResolvedType == sys::fs::file_type::directory_file) {
740 Child.reset(new detail::InMemoryDirectory(std::move(Stat)));
741 } else {
742 Child.reset(
743 new detail::InMemoryFile(std::move(Stat), std::move(Buffer)));
744 }
745 }
746 Dir->addChild(Name, std::move(Child));
747 return true;
748 }
749
750 // Create a new directory. Use the path up to here.
751 Status Stat(
752 StringRef(Path.str().begin(), Name.end() - Path.str().begin()),
753 getNextVirtualUniqueID(), llvm::sys::toTimePoint(ModificationTime),
754 ResolvedUser, ResolvedGroup, 0, sys::fs::file_type::directory_file,
755 NewDirectoryPerms);
756 Dir = cast<detail::InMemoryDirectory>(Dir->addChild(
757 Name, std::make_unique<detail::InMemoryDirectory>(std::move(Stat))));
758 continue;
759 }
760
761 if (auto *NewDir = dyn_cast<detail::InMemoryDirectory>(Node)) {
762 Dir = NewDir;
763 } else {
764 assert((isa<detail::InMemoryFile>(Node) ||(((isa<detail::InMemoryFile>(Node) || isa<detail::InMemoryHardLink
>(Node)) && "Must be either file, hardlink or directory!"
) ? static_cast<void> (0) : __assert_fail ("(isa<detail::InMemoryFile>(Node) || isa<detail::InMemoryHardLink>(Node)) && \"Must be either file, hardlink or directory!\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 766, __PRETTY_FUNCTION__))
765 isa<detail::InMemoryHardLink>(Node)) &&(((isa<detail::InMemoryFile>(Node) || isa<detail::InMemoryHardLink
>(Node)) && "Must be either file, hardlink or directory!"
) ? static_cast<void> (0) : __assert_fail ("(isa<detail::InMemoryFile>(Node) || isa<detail::InMemoryHardLink>(Node)) && \"Must be either file, hardlink or directory!\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 766, __PRETTY_FUNCTION__))
766 "Must be either file, hardlink or directory!")(((isa<detail::InMemoryFile>(Node) || isa<detail::InMemoryHardLink
>(Node)) && "Must be either file, hardlink or directory!"
) ? static_cast<void> (0) : __assert_fail ("(isa<detail::InMemoryFile>(Node) || isa<detail::InMemoryHardLink>(Node)) && \"Must be either file, hardlink or directory!\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 766, __PRETTY_FUNCTION__))
;
767
768 // Trying to insert a directory in place of a file.
769 if (I != E)
770 return false;
771
772 // Return false only if the new file is different from the existing one.
773 if (auto Link = dyn_cast<detail::InMemoryHardLink>(Node)) {
774 return Link->getResolvedFile().getBuffer()->getBuffer() ==
775 Buffer->getBuffer();
776 }
777 return cast<detail::InMemoryFile>(Node)->getBuffer()->getBuffer() ==
778 Buffer->getBuffer();
779 }
780 }
781}
782
783bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
784 std::unique_ptr<llvm::MemoryBuffer> Buffer,
785 Optional<uint32_t> User,
786 Optional<uint32_t> Group,
787 Optional<llvm::sys::fs::file_type> Type,
788 Optional<llvm::sys::fs::perms> Perms) {
789 return addFile(P, ModificationTime, std::move(Buffer), User, Group, Type,
790 Perms, /*HardLinkTarget=*/nullptr);
791}
792
793bool InMemoryFileSystem::addFileNoOwn(const Twine &P, time_t ModificationTime,
794 llvm::MemoryBuffer *Buffer,
795 Optional<uint32_t> User,
796 Optional<uint32_t> Group,
797 Optional<llvm::sys::fs::file_type> Type,
798 Optional<llvm::sys::fs::perms> Perms) {
799 return addFile(P, ModificationTime,
800 llvm::MemoryBuffer::getMemBuffer(
801 Buffer->getBuffer(), Buffer->getBufferIdentifier()),
802 std::move(User), std::move(Group), std::move(Type),
803 std::move(Perms));
804}
805
806static ErrorOr<const detail::InMemoryNode *>
807lookupInMemoryNode(const InMemoryFileSystem &FS, detail::InMemoryDirectory *Dir,
808 const Twine &P) {
809 SmallString<128> Path;
810 P.toVector(Path);
811
812 // Fix up relative paths. This just prepends the current working directory.
813 std::error_code EC = FS.makeAbsolute(Path);
814 assert(!EC)((!EC) ? static_cast<void> (0) : __assert_fail ("!EC", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 814, __PRETTY_FUNCTION__))
;
815 (void)EC;
816
817 if (FS.useNormalizedPaths())
818 llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
819
820 if (Path.empty())
821 return Dir;
822
823 auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path);
824 while (true) {
825 detail::InMemoryNode *Node = Dir->getChild(*I);
826 ++I;
827 if (!Node)
828 return errc::no_such_file_or_directory;
829
830 // Return the file if it's at the end of the path.
831 if (auto File = dyn_cast<detail::InMemoryFile>(Node)) {
832 if (I == E)
833 return File;
834 return errc::no_such_file_or_directory;
835 }
836
837 // If Node is HardLink then return the resolved file.
838 if (auto File = dyn_cast<detail::InMemoryHardLink>(Node)) {
839 if (I == E)
840 return &File->getResolvedFile();
841 return errc::no_such_file_or_directory;
842 }
843 // Traverse directories.
844 Dir = cast<detail::InMemoryDirectory>(Node);
845 if (I == E)
846 return Dir;
847 }
848}
849
850bool InMemoryFileSystem::addHardLink(const Twine &FromPath,
851 const Twine &ToPath) {
852 auto FromNode = lookupInMemoryNode(*this, Root.get(), FromPath);
853 auto ToNode = lookupInMemoryNode(*this, Root.get(), ToPath);
854 // FromPath must not have been added before. ToPath must have been added
855 // before. Resolved ToPath must be a File.
856 if (!ToNode || FromNode || !isa<detail::InMemoryFile>(*ToNode))
857 return false;
858 return this->addFile(FromPath, 0, nullptr, None, None, None, None,
859 cast<detail::InMemoryFile>(*ToNode));
860}
861
862llvm::ErrorOr<Status> InMemoryFileSystem::status(const Twine &Path) {
863 auto Node = lookupInMemoryNode(*this, Root.get(), Path);
864 if (Node)
865 return detail::getNodeStatus(*Node, Path);
866 return Node.getError();
867}
868
869llvm::ErrorOr<std::unique_ptr<File>>
870InMemoryFileSystem::openFileForRead(const Twine &Path) {
871 auto Node = lookupInMemoryNode(*this, Root.get(), Path);
872 if (!Node)
873 return Node.getError();
874
875 // When we have a file provide a heap-allocated wrapper for the memory buffer
876 // to match the ownership semantics for File.
877 if (auto *F = dyn_cast<detail::InMemoryFile>(*Node))
878 return std::unique_ptr<File>(
879 new detail::InMemoryFileAdaptor(*F, Path.str()));
880
881 // FIXME: errc::not_a_file?
882 return make_error_code(llvm::errc::invalid_argument);
883}
884
885namespace {
886
887/// Adaptor from InMemoryDir::iterator to directory_iterator.
888class InMemoryDirIterator : public llvm::vfs::detail::DirIterImpl {
889 detail::InMemoryDirectory::const_iterator I;
890 detail::InMemoryDirectory::const_iterator E;
891 std::string RequestedDirName;
892
893 void setCurrentEntry() {
894 if (I != E) {
895 SmallString<256> Path(RequestedDirName);
896 llvm::sys::path::append(Path, I->second->getFileName());
897 sys::fs::file_type Type = sys::fs::file_type::type_unknown;
898 switch (I->second->getKind()) {
899 case detail::IME_File:
900 case detail::IME_HardLink:
901 Type = sys::fs::file_type::regular_file;
902 break;
903 case detail::IME_Directory:
904 Type = sys::fs::file_type::directory_file;
905 break;
906 }
907 CurrentEntry = directory_entry(Path.str(), Type);
908 } else {
909 // When we're at the end, make CurrentEntry invalid and DirIterImpl will
910 // do the rest.
911 CurrentEntry = directory_entry();
912 }
913 }
914
915public:
916 InMemoryDirIterator() = default;
917
918 explicit InMemoryDirIterator(const detail::InMemoryDirectory &Dir,
919 std::string RequestedDirName)
920 : I(Dir.begin()), E(Dir.end()),
921 RequestedDirName(std::move(RequestedDirName)) {
922 setCurrentEntry();
923 }
924
925 std::error_code increment() override {
926 ++I;
927 setCurrentEntry();
928 return {};
929 }
930};
931
932} // namespace
933
934directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir,
935 std::error_code &EC) {
936 auto Node = lookupInMemoryNode(*this, Root.get(), Dir);
937 if (!Node) {
938 EC = Node.getError();
939 return directory_iterator(std::make_shared<InMemoryDirIterator>());
940 }
941
942 if (auto *DirNode = dyn_cast<detail::InMemoryDirectory>(*Node))
943 return directory_iterator(
944 std::make_shared<InMemoryDirIterator>(*DirNode, Dir.str()));
945
946 EC = make_error_code(llvm::errc::not_a_directory);
947 return directory_iterator(std::make_shared<InMemoryDirIterator>());
948}
949
950std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) {
951 SmallString<128> Path;
952 P.toVector(Path);
953
954 // Fix up relative paths. This just prepends the current working directory.
955 std::error_code EC = makeAbsolute(Path);
956 assert(!EC)((!EC) ? static_cast<void> (0) : __assert_fail ("!EC", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 956, __PRETTY_FUNCTION__))
;
957 (void)EC;
958
959 if (useNormalizedPaths())
960 llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
961
962 if (!Path.empty())
963 WorkingDirectory = Path.str();
964 return {};
965}
966
967std::error_code
968InMemoryFileSystem::getRealPath(const Twine &Path,
969 SmallVectorImpl<char> &Output) const {
970 auto CWD = getCurrentWorkingDirectory();
971 if (!CWD || CWD->empty())
972 return errc::operation_not_permitted;
973 Path.toVector(Output);
974 if (auto EC = makeAbsolute(Output))
975 return EC;
976 llvm::sys::path::remove_dots(Output, /*remove_dot_dot=*/true);
977 return {};
978}
979
980std::error_code InMemoryFileSystem::isLocal(const Twine &Path, bool &Result) {
981 Result = false;
982 return {};
983}
984
985} // namespace vfs
986} // namespace llvm
987
988//===-----------------------------------------------------------------------===/
989// RedirectingFileSystem implementation
990//===-----------------------------------------------------------------------===/
991
992RedirectingFileSystem::RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> FS)
993 : ExternalFS(std::move(FS)) {
994 if (ExternalFS)
995 if (auto ExternalWorkingDirectory =
996 ExternalFS->getCurrentWorkingDirectory()) {
997 WorkingDirectory = *ExternalWorkingDirectory;
998 ExternalFSValidWD = true;
999 }
1000}
1001
1002// FIXME: reuse implementation common with OverlayFSDirIterImpl as these
1003// iterators are conceptually similar.
1004class llvm::vfs::VFSFromYamlDirIterImpl
1005 : public llvm::vfs::detail::DirIterImpl {
1006 std::string Dir;
1007 RedirectingFileSystem::RedirectingDirectoryEntry::iterator Current, End;
1008
1009 // To handle 'fallthrough' mode we need to iterate at first through
1010 // RedirectingDirectoryEntry and then through ExternalFS. These operations are
1011 // done sequentially, we just need to keep a track of what kind of iteration
1012 // we are currently performing.
1013
1014 /// Flag telling if we should iterate through ExternalFS or stop at the last
1015 /// RedirectingDirectoryEntry::iterator.
1016 bool IterateExternalFS;
1017 /// Flag telling if we have switched to iterating through ExternalFS.
1018 bool IsExternalFSCurrent = false;
1019 FileSystem &ExternalFS;
1020 directory_iterator ExternalDirIter;
1021 llvm::StringSet<> SeenNames;
1022
1023 /// To combine multiple iterations, different methods are responsible for
1024 /// different iteration steps.
1025 /// @{
1026
1027 /// Responsible for dispatching between RedirectingDirectoryEntry iteration
1028 /// and ExternalFS iteration.
1029 std::error_code incrementImpl(bool IsFirstTime);
1030 /// Responsible for RedirectingDirectoryEntry iteration.
1031 std::error_code incrementContent(bool IsFirstTime);
1032 /// Responsible for ExternalFS iteration.
1033 std::error_code incrementExternal();
1034 /// @}
1035
1036public:
1037 VFSFromYamlDirIterImpl(
1038 const Twine &Path,
1039 RedirectingFileSystem::RedirectingDirectoryEntry::iterator Begin,
1040 RedirectingFileSystem::RedirectingDirectoryEntry::iterator End,
1041 bool IterateExternalFS, FileSystem &ExternalFS, std::error_code &EC);
1042
1043 std::error_code increment() override;
1044};
1045
1046llvm::ErrorOr<std::string>
1047RedirectingFileSystem::getCurrentWorkingDirectory() const {
1048 return WorkingDirectory;
1049}
1050
1051std::error_code
1052RedirectingFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
1053 // Don't change the working directory if the path doesn't exist.
1054 if (!exists(Path))
1055 return errc::no_such_file_or_directory;
1056
1057 // Always change the external FS but ignore its result.
1058 if (ExternalFS) {
1059 auto EC = ExternalFS->setCurrentWorkingDirectory(Path);
1060 ExternalFSValidWD = !static_cast<bool>(EC);
1061 }
1062
1063 SmallString<128> AbsolutePath;
1064 Path.toVector(AbsolutePath);
1065 if (std::error_code EC = makeAbsolute(AbsolutePath))
1066 return EC;
1067 WorkingDirectory = AbsolutePath.str();
1068 return {};
1069}
1070
1071std::error_code RedirectingFileSystem::isLocal(const Twine &Path,
1072 bool &Result) {
1073 return ExternalFS->isLocal(Path, Result);
1074}
1075
1076directory_iterator RedirectingFileSystem::dir_begin(const Twine &Dir,
1077 std::error_code &EC) {
1078 ErrorOr<RedirectingFileSystem::Entry *> E = lookupPath(Dir);
1079 if (!E) {
1080 EC = E.getError();
1081 if (shouldUseExternalFS() && EC == errc::no_such_file_or_directory)
1082 return ExternalFS->dir_begin(Dir, EC);
1083 return {};
1084 }
1085 ErrorOr<Status> S = status(Dir, *E);
1086 if (!S) {
1087 EC = S.getError();
1088 return {};
1089 }
1090 if (!S->isDirectory()) {
1091 EC = std::error_code(static_cast<int>(errc::not_a_directory),
1092 std::system_category());
1093 return {};
1094 }
1095
1096 auto *D = cast<RedirectingFileSystem::RedirectingDirectoryEntry>(*E);
1097 return directory_iterator(std::make_shared<VFSFromYamlDirIterImpl>(
1098 Dir, D->contents_begin(), D->contents_end(),
1099 /*IterateExternalFS=*/shouldUseExternalFS(), *ExternalFS, EC));
1100}
1101
1102void RedirectingFileSystem::setExternalContentsPrefixDir(StringRef PrefixDir) {
1103 ExternalContentsPrefixDir = PrefixDir.str();
1104}
1105
1106StringRef RedirectingFileSystem::getExternalContentsPrefixDir() const {
1107 return ExternalContentsPrefixDir;
1108}
1109
1110void RedirectingFileSystem::dump(raw_ostream &OS) const {
1111 for (const auto &Root : Roots)
1112 dumpEntry(OS, Root.get());
1113}
1114
1115void RedirectingFileSystem::dumpEntry(raw_ostream &OS,
1116 RedirectingFileSystem::Entry *E,
1117 int NumSpaces) const {
1118 StringRef Name = E->getName();
1119 for (int i = 0, e = NumSpaces; i < e; ++i)
1120 OS << " ";
1121 OS << "'" << Name.str().c_str() << "'"
1122 << "\n";
1123
1124 if (E->getKind() == RedirectingFileSystem::EK_Directory) {
1125 auto *DE = dyn_cast<RedirectingFileSystem::RedirectingDirectoryEntry>(E);
1126 assert(DE && "Should be a directory")((DE && "Should be a directory") ? static_cast<void
> (0) : __assert_fail ("DE && \"Should be a directory\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1126, __PRETTY_FUNCTION__))
;
1127
1128 for (std::unique_ptr<Entry> &SubEntry :
1129 llvm::make_range(DE->contents_begin(), DE->contents_end()))
1130 dumpEntry(OS, SubEntry.get(), NumSpaces + 2);
1131 }
1132}
1133
1134#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1135LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RedirectingFileSystem::dump() const { dump(dbgs()); }
1136#endif
1137
1138/// A helper class to hold the common YAML parsing state.
1139class llvm::vfs::RedirectingFileSystemParser {
1140 yaml::Stream &Stream;
1141
1142 void error(yaml::Node *N, const Twine &Msg) { Stream.printError(N, Msg); }
1143
1144 // false on error
1145 bool parseScalarString(yaml::Node *N, StringRef &Result,
1146 SmallVectorImpl<char> &Storage) {
1147 const auto *S = dyn_cast<yaml::ScalarNode>(N);
20
Assuming 'N' is a 'ScalarNode'
1148
1149 if (!S
20.1
'S' is non-null
) {
21
Taking false branch
1150 error(N, "expected string");
1151 return false;
1152 }
1153 Result = S->getValue(Storage);
1154 return true;
22
Returning the value 1, which participates in a condition later
1155 }
1156
1157 // false on error
1158 bool parseScalarBool(yaml::Node *N, bool &Result) {
1159 SmallString<5> Storage;
1160 StringRef Value;
1161 if (!parseScalarString(N, Value, Storage))
1162 return false;
1163
1164 if (Value.equals_lower("true") || Value.equals_lower("on") ||
1165 Value.equals_lower("yes") || Value == "1") {
1166 Result = true;
1167 return true;
1168 } else if (Value.equals_lower("false") || Value.equals_lower("off") ||
1169 Value.equals_lower("no") || Value == "0") {
1170 Result = false;
1171 return true;
1172 }
1173
1174 error(N, "expected boolean value");
1175 return false;
1176 }
1177
1178 struct KeyStatus {
1179 bool Required;
1180 bool Seen = false;
1181
1182 KeyStatus(bool Required = false) : Required(Required) {}
1183 };
1184
1185 using KeyStatusPair = std::pair<StringRef, KeyStatus>;
1186
1187 // false on error
1188 bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
1189 DenseMap<StringRef, KeyStatus> &Keys) {
1190 if (!Keys.count(Key)) {
26
Assuming the condition is false
27
Taking false branch
1191 error(KeyNode, "unknown key");
1192 return false;
1193 }
1194 KeyStatus &S = Keys[Key];
1195 if (S.Seen) {
28
Assuming field 'Seen' is false
29
Taking false branch
1196 error(KeyNode, Twine("duplicate key '") + Key + "'");
1197 return false;
1198 }
1199 S.Seen = true;
1200 return true;
30
Returning the value 1, which participates in a condition later
1201 }
1202
1203 // false on error
1204 bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
1205 for (const auto &I : Keys) {
1206 if (I.second.Required && !I.second.Seen) {
1207 error(Obj, Twine("missing key '") + I.first + "'");
1208 return false;
1209 }
1210 }
1211 return true;
46
Returning the value 1, which participates in a condition later
1212 }
1213
1214 RedirectingFileSystem::Entry *
1215 lookupOrCreateEntry(RedirectingFileSystem *FS, StringRef Name,
1216 RedirectingFileSystem::Entry *ParentEntry = nullptr) {
1217 if (!ParentEntry) { // Look for a existent root
1218 for (const auto &Root : FS->Roots) {
1219 if (Name.equals(Root->getName())) {
1220 ParentEntry = Root.get();
1221 return ParentEntry;
1222 }
1223 }
1224 } else { // Advance to the next component
1225 auto *DE = dyn_cast<RedirectingFileSystem::RedirectingDirectoryEntry>(
1226 ParentEntry);
1227 for (std::unique_ptr<RedirectingFileSystem::Entry> &Content :
1228 llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1229 auto *DirContent =
1230 dyn_cast<RedirectingFileSystem::RedirectingDirectoryEntry>(
1231 Content.get());
1232 if (DirContent && Name.equals(Content->getName()))
1233 return DirContent;
1234 }
1235 }
1236
1237 // ... or create a new one
1238 std::unique_ptr<RedirectingFileSystem::Entry> E =
1239 std::make_unique<RedirectingFileSystem::RedirectingDirectoryEntry>(
1240 Name, Status("", getNextVirtualUniqueID(),
1241 std::chrono::system_clock::now(), 0, 0, 0,
1242 file_type::directory_file, sys::fs::all_all));
1243
1244 if (!ParentEntry) { // Add a new root to the overlay
1245 FS->Roots.push_back(std::move(E));
1246 ParentEntry = FS->Roots.back().get();
1247 return ParentEntry;
1248 }
1249
1250 auto *DE =
1251 cast<RedirectingFileSystem::RedirectingDirectoryEntry>(ParentEntry);
1252 DE->addContent(std::move(E));
1253 return DE->getLastContent();
1254 }
1255
1256 void uniqueOverlayTree(RedirectingFileSystem *FS,
1257 RedirectingFileSystem::Entry *SrcE,
1258 RedirectingFileSystem::Entry *NewParentE = nullptr) {
1259 StringRef Name = SrcE->getName();
1260 switch (SrcE->getKind()) {
1261 case RedirectingFileSystem::EK_Directory: {
1262 auto *DE = cast<RedirectingFileSystem::RedirectingDirectoryEntry>(SrcE);
1263 // Empty directories could be present in the YAML as a way to
1264 // describe a file for a current directory after some of its subdir
1265 // is parsed. This only leads to redundant walks, ignore it.
1266 if (!Name.empty())
1267 NewParentE = lookupOrCreateEntry(FS, Name, NewParentE);
1268 for (std::unique_ptr<RedirectingFileSystem::Entry> &SubEntry :
1269 llvm::make_range(DE->contents_begin(), DE->contents_end()))
1270 uniqueOverlayTree(FS, SubEntry.get(), NewParentE);
1271 break;
1272 }
1273 case RedirectingFileSystem::EK_File: {
1274 assert(NewParentE && "Parent entry must exist")((NewParentE && "Parent entry must exist") ? static_cast
<void> (0) : __assert_fail ("NewParentE && \"Parent entry must exist\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1274, __PRETTY_FUNCTION__))
;
1275 auto *FE = cast<RedirectingFileSystem::RedirectingFileEntry>(SrcE);
1276 auto *DE =
1277 cast<RedirectingFileSystem::RedirectingDirectoryEntry>(NewParentE);
1278 DE->addContent(
1279 std::make_unique<RedirectingFileSystem::RedirectingFileEntry>(
1280 Name, FE->getExternalContentsPath(), FE->getUseName()));
1281 break;
1282 }
1283 }
1284 }
1285
1286 std::unique_ptr<RedirectingFileSystem::Entry>
1287 parseEntry(yaml::Node *N, RedirectingFileSystem *FS, bool IsRootEntry) {
1288 auto *M = dyn_cast<yaml::MappingNode>(N);
16
Assuming 'N' is a 'MappingNode'
1289 if (!M
16.1
'M' is non-null
) {
17
Taking false branch
1290 error(N, "expected mapping node for file or directory entry");
1291 return nullptr;
1292 }
1293
1294 KeyStatusPair Fields[] = {
1295 KeyStatusPair("name", true),
1296 KeyStatusPair("type", true),
1297 KeyStatusPair("contents", false),
1298 KeyStatusPair("external-contents", false),
1299 KeyStatusPair("use-external-name", false),
1300 };
1301
1302 DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
1303
1304 bool HasContents = false; // external or otherwise
1305 std::vector<std::unique_ptr<RedirectingFileSystem::Entry>>
1306 EntryArrayContents;
1307 std::string ExternalContentsPath;
1308 std::string Name;
1309 yaml::Node *NameValueNode = nullptr;
1310 auto UseExternalName =
1311 RedirectingFileSystem::RedirectingFileEntry::NK_NotSet;
1312 RedirectingFileSystem::EntryKind Kind;
18
'Kind' declared without an initial value
1313
1314 for (auto &I : *M) {
1315 StringRef Key;
1316 // Reuse the buffer for key and value, since we don't look at key after
1317 // parsing value.
1318 SmallString<256> Buffer;
1319 if (!parseScalarString(I.getKey(), Key, Buffer))
19
Calling 'RedirectingFileSystemParser::parseScalarString'
23
Returning from 'RedirectingFileSystemParser::parseScalarString'
24
Taking false branch
1320 return nullptr;
1321
1322 if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
25
Calling 'RedirectingFileSystemParser::checkDuplicateOrUnknownKey'
31
Returning from 'RedirectingFileSystemParser::checkDuplicateOrUnknownKey'
32
Taking false branch
1323 return nullptr;
1324
1325 StringRef Value;
1326 if (Key == "name") {
33
Assuming the condition is false
34
Taking false branch
1327 if (!parseScalarString(I.getValue(), Value, Buffer))
1328 return nullptr;
1329
1330 NameValueNode = I.getValue();
1331 if (FS->UseCanonicalizedPaths) {
1332 SmallString<256> Path(Value);
1333 // Guarantee that old YAML files containing paths with ".." and "."
1334 // are properly canonicalized before read into the VFS.
1335 Path = sys::path::remove_leading_dotslash(Path);
1336 sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
1337 Name = Path.str();
1338 } else {
1339 Name = Value;
1340 }
1341 } else if (Key == "type") {
35
Assuming the condition is false
36
Taking false branch
1342 if (!parseScalarString(I.getValue(), Value, Buffer))
1343 return nullptr;
1344 if (Value == "file")
1345 Kind = RedirectingFileSystem::EK_File;
1346 else if (Value == "directory")
1347 Kind = RedirectingFileSystem::EK_Directory;
1348 else {
1349 error(I.getValue(), "unknown value for 'type'");
1350 return nullptr;
1351 }
1352 } else if (Key == "contents") {
37
Assuming the condition is true
38
Taking true branch
1353 if (HasContents
38.1
'HasContents' is false
) {
39
Taking false branch
1354 error(I.getKey(),
1355 "entry already has 'contents' or 'external-contents'");
1356 return nullptr;
1357 }
1358 HasContents = true;
1359 auto *Contents = dyn_cast<yaml::SequenceNode>(I.getValue());
40
Assuming the object is a 'SequenceNode'
1360 if (!Contents
40.1
'Contents' is non-null
) {
41
Taking false branch
1361 // FIXME: this is only for directories, what about files?
1362 error(I.getValue(), "expected array");
1363 return nullptr;
1364 }
1365
1366 for (auto &I : *Contents) {
1367 if (std::unique_ptr<RedirectingFileSystem::Entry> E =
1368 parseEntry(&I, FS, /*IsRootEntry*/ false))
1369 EntryArrayContents.push_back(std::move(E));
1370 else
1371 return nullptr;
1372 }
1373 } else if (Key == "external-contents") {
1374 if (HasContents) {
1375 error(I.getKey(),
1376 "entry already has 'contents' or 'external-contents'");
1377 return nullptr;
1378 }
1379 HasContents = true;
1380 if (!parseScalarString(I.getValue(), Value, Buffer))
1381 return nullptr;
1382
1383 SmallString<256> FullPath;
1384 if (FS->IsRelativeOverlay) {
1385 FullPath = FS->getExternalContentsPrefixDir();
1386 assert(!FullPath.empty() &&((!FullPath.empty() && "External contents prefix directory must exist"
) ? static_cast<void> (0) : __assert_fail ("!FullPath.empty() && \"External contents prefix directory must exist\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1387, __PRETTY_FUNCTION__))
1387 "External contents prefix directory must exist")((!FullPath.empty() && "External contents prefix directory must exist"
) ? static_cast<void> (0) : __assert_fail ("!FullPath.empty() && \"External contents prefix directory must exist\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1387, __PRETTY_FUNCTION__))
;
1388 llvm::sys::path::append(FullPath, Value);
1389 } else {
1390 FullPath = Value;
1391 }
1392
1393 if (FS->UseCanonicalizedPaths) {
1394 // Guarantee that old YAML files containing paths with ".." and "."
1395 // are properly canonicalized before read into the VFS.
1396 FullPath = sys::path::remove_leading_dotslash(FullPath);
1397 sys::path::remove_dots(FullPath, /*remove_dot_dot=*/true);
1398 }
1399 ExternalContentsPath = FullPath.str();
1400 } else if (Key == "use-external-name") {
1401 bool Val;
1402 if (!parseScalarBool(I.getValue(), Val))
1403 return nullptr;
1404 UseExternalName =
1405 Val ? RedirectingFileSystem::RedirectingFileEntry::NK_External
1406 : RedirectingFileSystem::RedirectingFileEntry::NK_Virtual;
1407 } else {
1408 llvm_unreachable("key missing from Keys")::llvm::llvm_unreachable_internal("key missing from Keys", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1408)
;
1409 }
1410 }
1411
1412 if (Stream.failed())
42
Assuming the condition is false
43
Taking false branch
1413 return nullptr;
1414
1415 // check for missing keys
1416 if (!HasContents
43.1
'HasContents' is true
) {
44
Taking false branch
1417 error(N, "missing key 'contents' or 'external-contents'");
1418 return nullptr;
1419 }
1420 if (!checkMissingKeys(N, Keys))
45
Calling 'RedirectingFileSystemParser::checkMissingKeys'
47
Returning from 'RedirectingFileSystemParser::checkMissingKeys'
48
Taking false branch
1421 return nullptr;
1422
1423 // check invalid configuration
1424 if (Kind == RedirectingFileSystem::EK_Directory &&
49
The left operand of '==' is a garbage value
1425 UseExternalName !=
1426 RedirectingFileSystem::RedirectingFileEntry::NK_NotSet) {
1427 error(N, "'use-external-name' is not supported for directories");
1428 return nullptr;
1429 }
1430
1431 if (IsRootEntry && !sys::path::is_absolute(Name)) {
1432 assert(NameValueNode && "Name presence should be checked earlier")((NameValueNode && "Name presence should be checked earlier"
) ? static_cast<void> (0) : __assert_fail ("NameValueNode && \"Name presence should be checked earlier\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1432, __PRETTY_FUNCTION__))
;
1433 error(NameValueNode,
1434 "entry with relative path at the root level is not discoverable");
1435 return nullptr;
1436 }
1437
1438 // Remove trailing slash(es), being careful not to remove the root path
1439 StringRef Trimmed(Name);
1440 size_t RootPathLen = sys::path::root_path(Trimmed).size();
1441 while (Trimmed.size() > RootPathLen &&
1442 sys::path::is_separator(Trimmed.back()))
1443 Trimmed = Trimmed.slice(0, Trimmed.size() - 1);
1444 // Get the last component
1445 StringRef LastComponent = sys::path::filename(Trimmed);
1446
1447 std::unique_ptr<RedirectingFileSystem::Entry> Result;
1448 switch (Kind) {
1449 case RedirectingFileSystem::EK_File:
1450 Result = std::make_unique<RedirectingFileSystem::RedirectingFileEntry>(
1451 LastComponent, std::move(ExternalContentsPath), UseExternalName);
1452 break;
1453 case RedirectingFileSystem::EK_Directory:
1454 Result =
1455 std::make_unique<RedirectingFileSystem::RedirectingDirectoryEntry>(
1456 LastComponent, std::move(EntryArrayContents),
1457 Status("", getNextVirtualUniqueID(),
1458 std::chrono::system_clock::now(), 0, 0, 0,
1459 file_type::directory_file, sys::fs::all_all));
1460 break;
1461 }
1462
1463 StringRef Parent = sys::path::parent_path(Trimmed);
1464 if (Parent.empty())
1465 return Result;
1466
1467 // if 'name' contains multiple components, create implicit directory entries
1468 for (sys::path::reverse_iterator I = sys::path::rbegin(Parent),
1469 E = sys::path::rend(Parent);
1470 I != E; ++I) {
1471 std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> Entries;
1472 Entries.push_back(std::move(Result));
1473 Result =
1474 std::make_unique<RedirectingFileSystem::RedirectingDirectoryEntry>(
1475 *I, std::move(Entries),
1476 Status("", getNextVirtualUniqueID(),
1477 std::chrono::system_clock::now(), 0, 0, 0,
1478 file_type::directory_file, sys::fs::all_all));
1479 }
1480 return Result;
1481 }
1482
1483public:
1484 RedirectingFileSystemParser(yaml::Stream &S) : Stream(S) {}
1485
1486 // false on error
1487 bool parse(yaml::Node *Root, RedirectingFileSystem *FS) {
1488 auto *Top = dyn_cast<yaml::MappingNode>(Root);
7
Assuming 'Root' is a 'MappingNode'
1489 if (!Top
7.1
'Top' is non-null
) {
8
Taking false branch
1490 error(Root, "expected mapping node");
1491 return false;
1492 }
1493
1494 KeyStatusPair Fields[] = {
1495 KeyStatusPair("version", true),
1496 KeyStatusPair("case-sensitive", false),
1497 KeyStatusPair("use-external-names", false),
1498 KeyStatusPair("overlay-relative", false),
1499 KeyStatusPair("fallthrough", false),
1500 KeyStatusPair("roots", true),
1501 };
1502
1503 DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
1504 std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> RootEntries;
1505
1506 // Parse configuration and 'roots'
1507 for (auto &I : *Top) {
1508 SmallString<10> KeyBuffer;
1509 StringRef Key;
1510 if (!parseScalarString(I.getKey(), Key, KeyBuffer))
9
Taking false branch
1511 return false;
1512
1513 if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
10
Taking false branch
1514 return false;
1515
1516 if (Key == "roots") {
11
Assuming the condition is true
12
Taking true branch
1517 auto *Roots = dyn_cast<yaml::SequenceNode>(I.getValue());
13
Assuming the object is a 'SequenceNode'
1518 if (!Roots
13.1
'Roots' is non-null
) {
14
Taking false branch
1519 error(I.getValue(), "expected array");
1520 return false;
1521 }
1522
1523 for (auto &I : *Roots) {
1524 if (std::unique_ptr<RedirectingFileSystem::Entry> E =
1525 parseEntry(&I, FS, /*IsRootEntry*/ true))
15
Calling 'RedirectingFileSystemParser::parseEntry'
1526 RootEntries.push_back(std::move(E));
1527 else
1528 return false;
1529 }
1530 } else if (Key == "version") {
1531 StringRef VersionString;
1532 SmallString<4> Storage;
1533 if (!parseScalarString(I.getValue(), VersionString, Storage))
1534 return false;
1535 int Version;
1536 if (VersionString.getAsInteger<int>(10, Version)) {
1537 error(I.getValue(), "expected integer");
1538 return false;
1539 }
1540 if (Version < 0) {
1541 error(I.getValue(), "invalid version number");
1542 return false;
1543 }
1544 if (Version != 0) {
1545 error(I.getValue(), "version mismatch, expected 0");
1546 return false;
1547 }
1548 } else if (Key == "case-sensitive") {
1549 if (!parseScalarBool(I.getValue(), FS->CaseSensitive))
1550 return false;
1551 } else if (Key == "overlay-relative") {
1552 if (!parseScalarBool(I.getValue(), FS->IsRelativeOverlay))
1553 return false;
1554 } else if (Key == "use-external-names") {
1555 if (!parseScalarBool(I.getValue(), FS->UseExternalNames))
1556 return false;
1557 } else if (Key == "fallthrough") {
1558 if (!parseScalarBool(I.getValue(), FS->IsFallthrough))
1559 return false;
1560 } else {
1561 llvm_unreachable("key missing from Keys")::llvm::llvm_unreachable_internal("key missing from Keys", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1561)
;
1562 }
1563 }
1564
1565 if (Stream.failed())
1566 return false;
1567
1568 if (!checkMissingKeys(Top, Keys))
1569 return false;
1570
1571 // Now that we sucessefully parsed the YAML file, canonicalize the internal
1572 // representation to a proper directory tree so that we can search faster
1573 // inside the VFS.
1574 for (auto &E : RootEntries)
1575 uniqueOverlayTree(FS, E.get());
1576
1577 return true;
1578 }
1579};
1580
1581RedirectingFileSystem *
1582RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer,
1583 SourceMgr::DiagHandlerTy DiagHandler,
1584 StringRef YAMLFilePath, void *DiagContext,
1585 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
1586 SourceMgr SM;
1587 yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
1588
1589 SM.setDiagHandler(DiagHandler, DiagContext);
1590 yaml::document_iterator DI = Stream.begin();
1591 yaml::Node *Root = DI->getRoot();
1592 if (DI == Stream.end() || !Root) {
2
Assuming 'Root' is non-null
3
Taking false branch
1593 SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
1594 return nullptr;
1595 }
1596
1597 RedirectingFileSystemParser P(Stream);
1598
1599 std::unique_ptr<RedirectingFileSystem> FS(
1600 new RedirectingFileSystem(ExternalFS));
1601
1602 if (!YAMLFilePath.empty()) {
4
Assuming the condition is false
5
Taking false branch
1603 // Use the YAML path from -ivfsoverlay to compute the dir to be prefixed
1604 // to each 'external-contents' path.
1605 //
1606 // Example:
1607 // -ivfsoverlay dummy.cache/vfs/vfs.yaml
1608 // yields:
1609 // FS->ExternalContentsPrefixDir => /<absolute_path_to>/dummy.cache/vfs
1610 //
1611 SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath);
1612 std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir);
1613 assert(!EC && "Overlay dir final path must be absolute")((!EC && "Overlay dir final path must be absolute") ?
static_cast<void> (0) : __assert_fail ("!EC && \"Overlay dir final path must be absolute\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1613, __PRETTY_FUNCTION__))
;
1614 (void)EC;
1615 FS->setExternalContentsPrefixDir(OverlayAbsDir);
1616 }
1617
1618 if (!P.parse(Root, FS.get()))
6
Calling 'RedirectingFileSystemParser::parse'
1619 return nullptr;
1620
1621 return FS.release();
1622}
1623
1624ErrorOr<RedirectingFileSystem::Entry *>
1625RedirectingFileSystem::lookupPath(const Twine &Path_) const {
1626 SmallString<256> Path;
1627 Path_.toVector(Path);
1628
1629 // Handle relative paths
1630 if (std::error_code EC = makeAbsolute(Path))
1631 return EC;
1632
1633 // Canonicalize path by removing ".", "..", "./", etc components. This is
1634 // a VFS request, do bot bother about symlinks in the path components
1635 // but canonicalize in order to perform the correct entry search.
1636 if (UseCanonicalizedPaths) {
1637 Path = sys::path::remove_leading_dotslash(Path);
1638 sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
1639 }
1640
1641 if (Path.empty())
1642 return make_error_code(llvm::errc::invalid_argument);
1643
1644 sys::path::const_iterator Start = sys::path::begin(Path);
1645 sys::path::const_iterator End = sys::path::end(Path);
1646 for (const auto &Root : Roots) {
1647 ErrorOr<RedirectingFileSystem::Entry *> Result =
1648 lookupPath(Start, End, Root.get());
1649 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
1650 return Result;
1651 }
1652 return make_error_code(llvm::errc::no_such_file_or_directory);
1653}
1654
1655ErrorOr<RedirectingFileSystem::Entry *>
1656RedirectingFileSystem::lookupPath(sys::path::const_iterator Start,
1657 sys::path::const_iterator End,
1658 RedirectingFileSystem::Entry *From) const {
1659#ifndef _WIN32
1660 assert(!isTraversalComponent(*Start) &&((!isTraversalComponent(*Start) && !isTraversalComponent
(From->getName()) && "Paths should not contain traversal components"
) ? static_cast<void> (0) : __assert_fail ("!isTraversalComponent(*Start) && !isTraversalComponent(From->getName()) && \"Paths should not contain traversal components\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1662, __PRETTY_FUNCTION__))
1661 !isTraversalComponent(From->getName()) &&((!isTraversalComponent(*Start) && !isTraversalComponent
(From->getName()) && "Paths should not contain traversal components"
) ? static_cast<void> (0) : __assert_fail ("!isTraversalComponent(*Start) && !isTraversalComponent(From->getName()) && \"Paths should not contain traversal components\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1662, __PRETTY_FUNCTION__))
1662 "Paths should not contain traversal components")((!isTraversalComponent(*Start) && !isTraversalComponent
(From->getName()) && "Paths should not contain traversal components"
) ? static_cast<void> (0) : __assert_fail ("!isTraversalComponent(*Start) && !isTraversalComponent(From->getName()) && \"Paths should not contain traversal components\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1662, __PRETTY_FUNCTION__))
;
1663#else
1664 // FIXME: this is here to support windows, remove it once canonicalized
1665 // paths become globally default.
1666 if (Start->equals("."))
1667 ++Start;
1668#endif
1669
1670 StringRef FromName = From->getName();
1671
1672 // Forward the search to the next component in case this is an empty one.
1673 if (!FromName.empty()) {
1674 if (CaseSensitive ? !Start->equals(FromName)
1675 : !Start->equals_lower(FromName))
1676 // failure to match
1677 return make_error_code(llvm::errc::no_such_file_or_directory);
1678
1679 ++Start;
1680
1681 if (Start == End) {
1682 // Match!
1683 return From;
1684 }
1685 }
1686
1687 auto *DE = dyn_cast<RedirectingFileSystem::RedirectingDirectoryEntry>(From);
1688 if (!DE)
1689 return make_error_code(llvm::errc::not_a_directory);
1690
1691 for (const std::unique_ptr<RedirectingFileSystem::Entry> &DirEntry :
1692 llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1693 ErrorOr<RedirectingFileSystem::Entry *> Result =
1694 lookupPath(Start, End, DirEntry.get());
1695 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
1696 return Result;
1697 }
1698 return make_error_code(llvm::errc::no_such_file_or_directory);
1699}
1700
1701static Status getRedirectedFileStatus(const Twine &Path, bool UseExternalNames,
1702 Status ExternalStatus) {
1703 Status S = ExternalStatus;
1704 if (!UseExternalNames)
1705 S = Status::copyWithNewName(S, Path);
1706 S.IsVFSMapped = true;
1707 return S;
1708}
1709
1710ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path,
1711 RedirectingFileSystem::Entry *E) {
1712 assert(E != nullptr)((E != nullptr) ? static_cast<void> (0) : __assert_fail
("E != nullptr", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1712, __PRETTY_FUNCTION__))
;
1713 if (auto *F = dyn_cast<RedirectingFileSystem::RedirectingFileEntry>(E)) {
1714 ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath());
1715 assert(!S || S->getName() == F->getExternalContentsPath())((!S || S->getName() == F->getExternalContentsPath()) ?
static_cast<void> (0) : __assert_fail ("!S || S->getName() == F->getExternalContentsPath()"
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1715, __PRETTY_FUNCTION__))
;
1716 if (S)
1717 return getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames),
1718 *S);
1719 return S;
1720 } else { // directory
1721 auto *DE = cast<RedirectingFileSystem::RedirectingDirectoryEntry>(E);
1722 return Status::copyWithNewName(DE->getStatus(), Path);
1723 }
1724}
1725
1726ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path) {
1727 ErrorOr<RedirectingFileSystem::Entry *> Result = lookupPath(Path);
1728 if (!Result) {
1729 if (shouldUseExternalFS() &&
1730 Result.getError() == llvm::errc::no_such_file_or_directory) {
1731 return ExternalFS->status(Path);
1732 }
1733 return Result.getError();
1734 }
1735 return status(Path, *Result);
1736}
1737
1738namespace {
1739
1740/// Provide a file wrapper with an overriden status.
1741class FileWithFixedStatus : public File {
1742 std::unique_ptr<File> InnerFile;
1743 Status S;
1744
1745public:
1746 FileWithFixedStatus(std::unique_ptr<File> InnerFile, Status S)
1747 : InnerFile(std::move(InnerFile)), S(std::move(S)) {}
1748
1749 ErrorOr<Status> status() override { return S; }
1750 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
1751
1752 getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
1753 bool IsVolatile) override {
1754 return InnerFile->getBuffer(Name, FileSize, RequiresNullTerminator,
1755 IsVolatile);
1756 }
1757
1758 std::error_code close() override { return InnerFile->close(); }
1759};
1760
1761} // namespace
1762
1763ErrorOr<std::unique_ptr<File>>
1764RedirectingFileSystem::openFileForRead(const Twine &Path) {
1765 ErrorOr<RedirectingFileSystem::Entry *> E = lookupPath(Path);
1766 if (!E) {
1767 if (shouldUseExternalFS() &&
1768 E.getError() == llvm::errc::no_such_file_or_directory) {
1769 return ExternalFS->openFileForRead(Path);
1770 }
1771 return E.getError();
1772 }
1773
1774 auto *F = dyn_cast<RedirectingFileSystem::RedirectingFileEntry>(*E);
1775 if (!F) // FIXME: errc::not_a_file?
1776 return make_error_code(llvm::errc::invalid_argument);
1777
1778 auto Result = ExternalFS->openFileForRead(F->getExternalContentsPath());
1779 if (!Result)
1780 return Result;
1781
1782 auto ExternalStatus = (*Result)->status();
1783 if (!ExternalStatus)
1784 return ExternalStatus.getError();
1785
1786 // FIXME: Update the status with the name and VFSMapped.
1787 Status S = getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames),
1788 *ExternalStatus);
1789 return std::unique_ptr<File>(
1790 std::make_unique<FileWithFixedStatus>(std::move(*Result), S));
1791}
1792
1793std::error_code
1794RedirectingFileSystem::getRealPath(const Twine &Path,
1795 SmallVectorImpl<char> &Output) const {
1796 ErrorOr<RedirectingFileSystem::Entry *> Result = lookupPath(Path);
1797 if (!Result) {
1798 if (shouldUseExternalFS() &&
1799 Result.getError() == llvm::errc::no_such_file_or_directory) {
1800 return ExternalFS->getRealPath(Path, Output);
1801 }
1802 return Result.getError();
1803 }
1804
1805 if (auto *F =
1806 dyn_cast<RedirectingFileSystem::RedirectingFileEntry>(*Result)) {
1807 return ExternalFS->getRealPath(F->getExternalContentsPath(), Output);
1808 }
1809 // Even if there is a directory entry, fall back to ExternalFS if allowed,
1810 // because directories don't have a single external contents path.
1811 return shouldUseExternalFS() ? ExternalFS->getRealPath(Path, Output)
1812 : llvm::errc::invalid_argument;
1813}
1814
1815IntrusiveRefCntPtr<FileSystem>
1816vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
1817 SourceMgr::DiagHandlerTy DiagHandler,
1818 StringRef YAMLFilePath, void *DiagContext,
1819 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
1820 return RedirectingFileSystem::create(std::move(Buffer), DiagHandler,
1821 YAMLFilePath, DiagContext,
1822 std::move(ExternalFS));
1823}
1824
1825static void getVFSEntries(RedirectingFileSystem::Entry *SrcE,
1826 SmallVectorImpl<StringRef> &Path,
1827 SmallVectorImpl<YAMLVFSEntry> &Entries) {
1828 auto Kind = SrcE->getKind();
1829 if (Kind == RedirectingFileSystem::EK_Directory) {
1830 auto *DE = dyn_cast<RedirectingFileSystem::RedirectingDirectoryEntry>(SrcE);
1831 assert(DE && "Must be a directory")((DE && "Must be a directory") ? static_cast<void>
(0) : __assert_fail ("DE && \"Must be a directory\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1831, __PRETTY_FUNCTION__))
;
1832 for (std::unique_ptr<RedirectingFileSystem::Entry> &SubEntry :
1833 llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1834 Path.push_back(SubEntry->getName());
1835 getVFSEntries(SubEntry.get(), Path, Entries);
1836 Path.pop_back();
1837 }
1838 return;
1839 }
1840
1841 assert(Kind == RedirectingFileSystem::EK_File && "Must be a EK_File")((Kind == RedirectingFileSystem::EK_File && "Must be a EK_File"
) ? static_cast<void> (0) : __assert_fail ("Kind == RedirectingFileSystem::EK_File && \"Must be a EK_File\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1841, __PRETTY_FUNCTION__))
;
1842 auto *FE = dyn_cast<RedirectingFileSystem::RedirectingFileEntry>(SrcE);
1843 assert(FE && "Must be a file")((FE && "Must be a file") ? static_cast<void> (
0) : __assert_fail ("FE && \"Must be a file\"", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1843, __PRETTY_FUNCTION__))
;
1844 SmallString<128> VPath;
1845 for (auto &Comp : Path)
1846 llvm::sys::path::append(VPath, Comp);
1847 Entries.push_back(YAMLVFSEntry(VPath.c_str(), FE->getExternalContentsPath()));
1848}
1849
1850void vfs::collectVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
1851 SourceMgr::DiagHandlerTy DiagHandler,
1852 StringRef YAMLFilePath,
1853 SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
1854 void *DiagContext,
1855 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
1856 RedirectingFileSystem *VFS = RedirectingFileSystem::create(
1
Calling 'RedirectingFileSystem::create'
1857 std::move(Buffer), DiagHandler, YAMLFilePath, DiagContext,
1858 std::move(ExternalFS));
1859 ErrorOr<RedirectingFileSystem::Entry *> RootE = VFS->lookupPath("/");
1860 if (!RootE)
1861 return;
1862 SmallVector<StringRef, 8> Components;
1863 Components.push_back("/");
1864 getVFSEntries(*RootE, Components, CollectedEntries);
1865}
1866
1867UniqueID vfs::getNextVirtualUniqueID() {
1868 static std::atomic<unsigned> UID;
1869 unsigned ID = ++UID;
1870 // The following assumes that uint64_t max will never collide with a real
1871 // dev_t value from the OS.
1872 return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
1873}
1874
1875void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) {
1876 assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute")((sys::path::is_absolute(VirtualPath) && "virtual path not absolute"
) ? static_cast<void> (0) : __assert_fail ("sys::path::is_absolute(VirtualPath) && \"virtual path not absolute\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1876, __PRETTY_FUNCTION__))
;
1877 assert(sys::path::is_absolute(RealPath) && "real path not absolute")((sys::path::is_absolute(RealPath) && "real path not absolute"
) ? static_cast<void> (0) : __assert_fail ("sys::path::is_absolute(RealPath) && \"real path not absolute\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1877, __PRETTY_FUNCTION__))
;
1878 assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported")((!pathHasTraversal(VirtualPath) && "path traversal is not supported"
) ? static_cast<void> (0) : __assert_fail ("!pathHasTraversal(VirtualPath) && \"path traversal is not supported\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1878, __PRETTY_FUNCTION__))
;
1879 Mappings.emplace_back(VirtualPath, RealPath);
1880}
1881
1882namespace {
1883
1884class JSONWriter {
1885 llvm::raw_ostream &OS;
1886 SmallVector<StringRef, 16> DirStack;
1887
1888 unsigned getDirIndent() { return 4 * DirStack.size(); }
1889 unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
1890 bool containedIn(StringRef Parent, StringRef Path);
1891 StringRef containedPart(StringRef Parent, StringRef Path);
1892 void startDirectory(StringRef Path);
1893 void endDirectory();
1894 void writeEntry(StringRef VPath, StringRef RPath);
1895
1896public:
1897 JSONWriter(llvm::raw_ostream &OS) : OS(OS) {}
1898
1899 void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> UseExternalNames,
1900 Optional<bool> IsCaseSensitive, Optional<bool> IsOverlayRelative,
1901 StringRef OverlayDir);
1902};
1903
1904} // namespace
1905
1906bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
1907 using namespace llvm::sys;
1908
1909 // Compare each path component.
1910 auto IParent = path::begin(Parent), EParent = path::end(Parent);
1911 for (auto IChild = path::begin(Path), EChild = path::end(Path);
1912 IParent != EParent && IChild != EChild; ++IParent, ++IChild) {
1913 if (*IParent != *IChild)
1914 return false;
1915 }
1916 // Have we exhausted the parent path?
1917 return IParent == EParent;
1918}
1919
1920StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
1921 assert(!Parent.empty())((!Parent.empty()) ? static_cast<void> (0) : __assert_fail
("!Parent.empty()", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1921, __PRETTY_FUNCTION__))
;
1922 assert(containedIn(Parent, Path))((containedIn(Parent, Path)) ? static_cast<void> (0) : __assert_fail
("containedIn(Parent, Path)", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1922, __PRETTY_FUNCTION__))
;
1923 return Path.slice(Parent.size() + 1, StringRef::npos);
1924}
1925
1926void JSONWriter::startDirectory(StringRef Path) {
1927 StringRef Name =
1928 DirStack.empty() ? Path : containedPart(DirStack.back(), Path);
1929 DirStack.push_back(Path);
1930 unsigned Indent = getDirIndent();
1931 OS.indent(Indent) << "{\n";
1932 OS.indent(Indent + 2) << "'type': 'directory',\n";
1933 OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n";
1934 OS.indent(Indent + 2) << "'contents': [\n";
1935}
1936
1937void JSONWriter::endDirectory() {
1938 unsigned Indent = getDirIndent();
1939 OS.indent(Indent + 2) << "]\n";
1940 OS.indent(Indent) << "}";
1941
1942 DirStack.pop_back();
1943}
1944
1945void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) {
1946 unsigned Indent = getFileIndent();
1947 OS.indent(Indent) << "{\n";
1948 OS.indent(Indent + 2) << "'type': 'file',\n";
1949 OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n";
1950 OS.indent(Indent + 2) << "'external-contents': \""
1951 << llvm::yaml::escape(RPath) << "\"\n";
1952 OS.indent(Indent) << "}";
1953}
1954
1955void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
1956 Optional<bool> UseExternalNames,
1957 Optional<bool> IsCaseSensitive,
1958 Optional<bool> IsOverlayRelative,
1959 StringRef OverlayDir) {
1960 using namespace llvm::sys;
1961
1962 OS << "{\n"
1963 " 'version': 0,\n";
1964 if (IsCaseSensitive.hasValue())
1965 OS << " 'case-sensitive': '"
1966 << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n";
1967 if (UseExternalNames.hasValue())
1968 OS << " 'use-external-names': '"
1969 << (UseExternalNames.getValue() ? "true" : "false") << "',\n";
1970 bool UseOverlayRelative = false;
1971 if (IsOverlayRelative.hasValue()) {
1972 UseOverlayRelative = IsOverlayRelative.getValue();
1973 OS << " 'overlay-relative': '" << (UseOverlayRelative ? "true" : "false")
1974 << "',\n";
1975 }
1976 OS << " 'roots': [\n";
1977
1978 if (!Entries.empty()) {
1979 const YAMLVFSEntry &Entry = Entries.front();
1980 startDirectory(path::parent_path(Entry.VPath));
1981
1982 StringRef RPath = Entry.RPath;
1983 if (UseOverlayRelative) {
1984 unsigned OverlayDirLen = OverlayDir.size();
1985 assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&((RPath.substr(0, OverlayDirLen) == OverlayDir && "Overlay dir must be contained in RPath"
) ? static_cast<void> (0) : __assert_fail ("RPath.substr(0, OverlayDirLen) == OverlayDir && \"Overlay dir must be contained in RPath\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1986, __PRETTY_FUNCTION__))
1986 "Overlay dir must be contained in RPath")((RPath.substr(0, OverlayDirLen) == OverlayDir && "Overlay dir must be contained in RPath"
) ? static_cast<void> (0) : __assert_fail ("RPath.substr(0, OverlayDirLen) == OverlayDir && \"Overlay dir must be contained in RPath\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 1986, __PRETTY_FUNCTION__))
;
1987 RPath = RPath.slice(OverlayDirLen, RPath.size());
1988 }
1989
1990 writeEntry(path::filename(Entry.VPath), RPath);
1991
1992 for (const auto &Entry : Entries.slice(1)) {
1993 StringRef Dir = path::parent_path(Entry.VPath);
1994 if (Dir == DirStack.back())
1995 OS << ",\n";
1996 else {
1997 while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) {
1998 OS << "\n";
1999 endDirectory();
2000 }
2001 OS << ",\n";
2002 startDirectory(Dir);
2003 }
2004 StringRef RPath = Entry.RPath;
2005 if (UseOverlayRelative) {
2006 unsigned OverlayDirLen = OverlayDir.size();
2007 assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&((RPath.substr(0, OverlayDirLen) == OverlayDir && "Overlay dir must be contained in RPath"
) ? static_cast<void> (0) : __assert_fail ("RPath.substr(0, OverlayDirLen) == OverlayDir && \"Overlay dir must be contained in RPath\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 2008, __PRETTY_FUNCTION__))
2008 "Overlay dir must be contained in RPath")((RPath.substr(0, OverlayDirLen) == OverlayDir && "Overlay dir must be contained in RPath"
) ? static_cast<void> (0) : __assert_fail ("RPath.substr(0, OverlayDirLen) == OverlayDir && \"Overlay dir must be contained in RPath\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 2008, __PRETTY_FUNCTION__))
;
2009 RPath = RPath.slice(OverlayDirLen, RPath.size());
2010 }
2011 writeEntry(path::filename(Entry.VPath), RPath);
2012 }
2013
2014 while (!DirStack.empty()) {
2015 OS << "\n";
2016 endDirectory();
2017 }
2018 OS << "\n";
2019 }
2020
2021 OS << " ]\n"
2022 << "}\n";
2023}
2024
2025void YAMLVFSWriter::write(llvm::raw_ostream &OS) {
2026 llvm::sort(Mappings, [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
2027 return LHS.VPath < RHS.VPath;
2028 });
2029
2030 JSONWriter(OS).write(Mappings, UseExternalNames, IsCaseSensitive,
2031 IsOverlayRelative, OverlayDir);
2032}
2033
2034VFSFromYamlDirIterImpl::VFSFromYamlDirIterImpl(
2035 const Twine &_Path,
2036 RedirectingFileSystem::RedirectingDirectoryEntry::iterator Begin,
2037 RedirectingFileSystem::RedirectingDirectoryEntry::iterator End,
2038 bool IterateExternalFS, FileSystem &ExternalFS, std::error_code &EC)
2039 : Dir(_Path.str()), Current(Begin), End(End),
2040 IterateExternalFS(IterateExternalFS), ExternalFS(ExternalFS) {
2041 EC = incrementImpl(/*IsFirstTime=*/true);
2042}
2043
2044std::error_code VFSFromYamlDirIterImpl::increment() {
2045 return incrementImpl(/*IsFirstTime=*/false);
2046}
2047
2048std::error_code VFSFromYamlDirIterImpl::incrementExternal() {
2049 assert(!(IsExternalFSCurrent && ExternalDirIter == directory_iterator()) &&((!(IsExternalFSCurrent && ExternalDirIter == directory_iterator
()) && "incrementing past end") ? static_cast<void
> (0) : __assert_fail ("!(IsExternalFSCurrent && ExternalDirIter == directory_iterator()) && \"incrementing past end\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 2050, __PRETTY_FUNCTION__))
2050 "incrementing past end")((!(IsExternalFSCurrent && ExternalDirIter == directory_iterator
()) && "incrementing past end") ? static_cast<void
> (0) : __assert_fail ("!(IsExternalFSCurrent && ExternalDirIter == directory_iterator()) && \"incrementing past end\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 2050, __PRETTY_FUNCTION__))
;
2051 std::error_code EC;
2052 if (IsExternalFSCurrent) {
2053 ExternalDirIter.increment(EC);
2054 } else if (IterateExternalFS) {
2055 ExternalDirIter = ExternalFS.dir_begin(Dir, EC);
2056 IsExternalFSCurrent = true;
2057 if (EC && EC != errc::no_such_file_or_directory)
2058 return EC;
2059 EC = {};
2060 }
2061 if (EC || ExternalDirIter == directory_iterator()) {
2062 CurrentEntry = directory_entry();
2063 } else {
2064 CurrentEntry = *ExternalDirIter;
2065 }
2066 return EC;
2067}
2068
2069std::error_code VFSFromYamlDirIterImpl::incrementContent(bool IsFirstTime) {
2070 assert((IsFirstTime || Current != End) && "cannot iterate past end")(((IsFirstTime || Current != End) && "cannot iterate past end"
) ? static_cast<void> (0) : __assert_fail ("(IsFirstTime || Current != End) && \"cannot iterate past end\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 2070, __PRETTY_FUNCTION__))
;
2071 if (!IsFirstTime)
2072 ++Current;
2073 while (Current != End) {
2074 SmallString<128> PathStr(Dir);
2075 llvm::sys::path::append(PathStr, (*Current)->getName());
2076 sys::fs::file_type Type = sys::fs::file_type::type_unknown;
2077 switch ((*Current)->getKind()) {
2078 case RedirectingFileSystem::EK_Directory:
2079 Type = sys::fs::file_type::directory_file;
2080 break;
2081 case RedirectingFileSystem::EK_File:
2082 Type = sys::fs::file_type::regular_file;
2083 break;
2084 }
2085 CurrentEntry = directory_entry(PathStr.str(), Type);
2086 return {};
2087 }
2088 return incrementExternal();
2089}
2090
2091std::error_code VFSFromYamlDirIterImpl::incrementImpl(bool IsFirstTime) {
2092 while (true) {
2093 std::error_code EC = IsExternalFSCurrent ? incrementExternal()
2094 : incrementContent(IsFirstTime);
2095 if (EC || CurrentEntry.path().empty())
2096 return EC;
2097 StringRef Name = llvm::sys::path::filename(CurrentEntry.path());
2098 if (SeenNames.insert(Name).second)
2099 return EC; // name not seen before
2100 }
2101 llvm_unreachable("returned above")::llvm::llvm_unreachable_internal("returned above", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 2101)
;
2102}
2103
2104vfs::recursive_directory_iterator::recursive_directory_iterator(
2105 FileSystem &FS_, const Twine &Path, std::error_code &EC)
2106 : FS(&FS_) {
2107 directory_iterator I = FS->dir_begin(Path, EC);
2108 if (I != directory_iterator()) {
2109 State = std::make_shared<detail::RecDirIterState>();
2110 State->Stack.push(I);
2111 }
2112}
2113
2114vfs::recursive_directory_iterator &
2115recursive_directory_iterator::increment(std::error_code &EC) {
2116 assert(FS && State && !State->Stack.empty() && "incrementing past end")((FS && State && !State->Stack.empty() &&
"incrementing past end") ? static_cast<void> (0) : __assert_fail
("FS && State && !State->Stack.empty() && \"incrementing past end\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 2116, __PRETTY_FUNCTION__))
;
2117 assert(!State->Stack.top()->path().empty() && "non-canonical end iterator")((!State->Stack.top()->path().empty() && "non-canonical end iterator"
) ? static_cast<void> (0) : __assert_fail ("!State->Stack.top()->path().empty() && \"non-canonical end iterator\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Support/VirtualFileSystem.cpp"
, 2117, __PRETTY_FUNCTION__))
;
2118 vfs::directory_iterator End;
2119
2120 if (State->HasNoPushRequest)
2121 State->HasNoPushRequest = false;
2122 else {
2123 if (State->Stack.top()->type() == sys::fs::file_type::directory_file) {
2124 vfs::directory_iterator I = FS->dir_begin(State->Stack.top()->path(), EC);
2125 if (I != End) {
2126 State->Stack.push(I);
2127 return *this;
2128 }
2129 }
2130 }
2131
2132 while (!State->Stack.empty() && State->Stack.top().increment(EC) == End)
2133 State->Stack.pop();
2134
2135 if (State->Stack.empty())
2136 State.reset(); // end iterator
2137
2138 return *this;
2139}