head	1.1;
branch	1.1.1;
access;
symbols
	netbsd-11-0-RC4:1.1.1.9
	netbsd-11-0-RC3:1.1.1.9
	netbsd-11-0-RC2:1.1.1.9
	netbsd-11-0-RC1:1.1.1.9
	perseant-exfatfs-base-20250801:1.1.1.9
	netbsd-11:1.1.1.9.0.10
	netbsd-11-base:1.1.1.9
	netbsd-10-1-RELEASE:1.1.1.9
	perseant-exfatfs-base-20240630:1.1.1.9
	perseant-exfatfs:1.1.1.9.0.8
	perseant-exfatfs-base:1.1.1.9
	netbsd-8-3-RELEASE:1.1.1.6
	netbsd-9-4-RELEASE:1.1.1.8
	netbsd-10-0-RELEASE:1.1.1.9
	netbsd-10-0-RC6:1.1.1.9
	netbsd-10-0-RC5:1.1.1.9
	netbsd-10-0-RC4:1.1.1.9
	netbsd-10-0-RC3:1.1.1.9
	netbsd-10-0-RC2:1.1.1.9
	netbsd-10-0-RC1:1.1.1.9
	netbsd-10:1.1.1.9.0.6
	netbsd-10-base:1.1.1.9
	netbsd-9-3-RELEASE:1.1.1.8
	cjep_sun2x:1.1.1.9.0.4
	cjep_sun2x-base:1.1.1.9
	cjep_staticlib_x-base1:1.1.1.9
	netbsd-9-2-RELEASE:1.1.1.8
	cjep_staticlib_x:1.1.1.9.0.2
	cjep_staticlib_x-base:1.1.1.9
	netbsd-9-1-RELEASE:1.1.1.8
	phil-wifi-20200421:1.1.1.9
	phil-wifi-20200411:1.1.1.9
	phil-wifi-20200406:1.1.1.9
	netbsd-8-2-RELEASE:1.1.1.6
	netbsd-9-0-RELEASE:1.1.1.8
	netbsd-9-0-RC2:1.1.1.8
	netbsd-9-0-RC1:1.1.1.8
	netbsd-9:1.1.1.8.0.2
	netbsd-9-base:1.1.1.8
	phil-wifi-20190609:1.1.1.8
	netbsd-8-1-RELEASE:1.1.1.6
	netbsd-8-1-RC1:1.1.1.6
	pgoyette-compat-merge-20190127:1.1.1.7.2.1
	pgoyette-compat-20190127:1.1.1.8
	pgoyette-compat-20190118:1.1.1.8
	pgoyette-compat-1226:1.1.1.8
	pgoyette-compat-1126:1.1.1.8
	pgoyette-compat-1020:1.1.1.8
	pgoyette-compat-0930:1.1.1.8
	pgoyette-compat-0906:1.1.1.8
	netbsd-7-2-RELEASE:1.1.1.3.2.1
	pgoyette-compat-0728:1.1.1.8
	clang-337282:1.1.1.8
	netbsd-8-0-RELEASE:1.1.1.6
	phil-wifi:1.1.1.7.0.4
	phil-wifi-base:1.1.1.7
	pgoyette-compat-0625:1.1.1.7
	netbsd-8-0-RC2:1.1.1.6
	pgoyette-compat-0521:1.1.1.7
	pgoyette-compat-0502:1.1.1.7
	pgoyette-compat-0422:1.1.1.7
	netbsd-8-0-RC1:1.1.1.6
	pgoyette-compat-0415:1.1.1.7
	pgoyette-compat-0407:1.1.1.7
	pgoyette-compat-0330:1.1.1.7
	pgoyette-compat-0322:1.1.1.7
	pgoyette-compat-0315:1.1.1.7
	netbsd-7-1-2-RELEASE:1.1.1.3.2.1
	pgoyette-compat:1.1.1.7.0.2
	pgoyette-compat-base:1.1.1.7
	netbsd-7-1-1-RELEASE:1.1.1.3.2.1
	clang-319952:1.1.1.7
	matt-nb8-mediatek:1.1.1.6.0.10
	matt-nb8-mediatek-base:1.1.1.6
	clang-309604:1.1.1.7
	perseant-stdc-iso10646:1.1.1.6.0.8
	perseant-stdc-iso10646-base:1.1.1.6
	netbsd-8:1.1.1.6.0.6
	netbsd-8-base:1.1.1.6
	prg-localcount2-base3:1.1.1.6
	prg-localcount2-base2:1.1.1.6
	prg-localcount2-base1:1.1.1.6
	prg-localcount2:1.1.1.6.0.4
	prg-localcount2-base:1.1.1.6
	pgoyette-localcount-20170426:1.1.1.6
	bouyer-socketcan-base1:1.1.1.6
	pgoyette-localcount-20170320:1.1.1.6
	netbsd-7-1:1.1.1.3.2.1.0.6
	netbsd-7-1-RELEASE:1.1.1.3.2.1
	netbsd-7-1-RC2:1.1.1.3.2.1
	clang-294123:1.1.1.6
	netbsd-7-nhusb-base-20170116:1.1.1.3.2.1
	bouyer-socketcan:1.1.1.6.0.2
	bouyer-socketcan-base:1.1.1.6
	clang-291444:1.1.1.6
	pgoyette-localcount-20170107:1.1.1.5
	netbsd-7-1-RC1:1.1.1.3.2.1
	pgoyette-localcount-20161104:1.1.1.5
	netbsd-7-0-2-RELEASE:1.1.1.3.2.1
	localcount-20160914:1.1.1.5
	netbsd-7-nhusb:1.1.1.3.2.1.0.4
	netbsd-7-nhusb-base:1.1.1.3.2.1
	clang-280599:1.1.1.5
	pgoyette-localcount-20160806:1.1.1.5
	pgoyette-localcount-20160726:1.1.1.5
	pgoyette-localcount:1.1.1.5.0.2
	pgoyette-localcount-base:1.1.1.5
	netbsd-7-0-1-RELEASE:1.1.1.3.2.1
	clang-261930:1.1.1.5
	netbsd-7-0:1.1.1.3.2.1.0.2
	netbsd-7-0-RELEASE:1.1.1.3.2.1
	netbsd-7-0-RC3:1.1.1.3.2.1
	netbsd-7-0-RC2:1.1.1.3.2.1
	netbsd-7-0-RC1:1.1.1.3.2.1
	clang-237755:1.1.1.4
	clang-232565:1.1.1.4
	clang-227398:1.1.1.4
	tls-maxphys-base:1.1.1.3
	tls-maxphys:1.1.1.3.0.4
	netbsd-7:1.1.1.3.0.2
	netbsd-7-base:1.1.1.3
	clang-215315:1.1.1.3
	clang-209886:1.1.1.2
	yamt-pagecache:1.1.1.1.0.4
	yamt-pagecache-base9:1.1.1.1
	tls-earlyentropy:1.1.1.1.0.2
	tls-earlyentropy-base:1.1.1.2
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.1.1.1
	riastradh-drm2-base3:1.1.1.1
	clang-202566:1.1.1.1
	LLVM:1.1.1;
locks; strict;
comment	@// @;


1.1
date	2014.03.04.19.54.03;	author joerg;	state Exp;
branches
	1.1.1.1;
next	;
commitid	29z1hJonZISIXprx;

1.1.1.1
date	2014.03.04.19.54.03;	author joerg;	state Exp;
branches
	1.1.1.1.2.1
	1.1.1.1.4.1;
next	1.1.1.2;
commitid	29z1hJonZISIXprx;

1.1.1.2
date	2014.05.30.18.14.40;	author joerg;	state Exp;
branches;
next	1.1.1.3;
commitid	8q0kdlBlCn09GACx;

1.1.1.3
date	2014.08.10.17.08.27;	author joerg;	state Exp;
branches
	1.1.1.3.2.1
	1.1.1.3.4.1;
next	1.1.1.4;
commitid	N85tXAN6Ex9VZPLx;

1.1.1.4
date	2015.01.29.19.57.33;	author joerg;	state Exp;
branches;
next	1.1.1.5;
commitid	mlISSizlPKvepX7y;

1.1.1.5
date	2016.02.27.22.11.54;	author joerg;	state Exp;
branches
	1.1.1.5.2.1;
next	1.1.1.6;
commitid	tIimz3oDlh1NpBWy;

1.1.1.6
date	2017.01.11.10.34.55;	author joerg;	state Exp;
branches;
next	1.1.1.7;
commitid	CNnUNfII1jgNmxBz;

1.1.1.7
date	2017.08.01.19.34.55;	author joerg;	state Exp;
branches
	1.1.1.7.2.1
	1.1.1.7.4.1;
next	1.1.1.8;
commitid	pMuDy65V0VicSx1A;

1.1.1.8
date	2018.07.17.18.31.24;	author joerg;	state Exp;
branches;
next	1.1.1.9;
commitid	wDzL46ALjrCZgwKA;

1.1.1.9
date	2019.11.13.22.19.20;	author joerg;	state dead;
branches;
next	;
commitid	QD8YATxuNG34YJKB;

1.1.1.1.2.1
date	2014.08.10.07.08.07;	author tls;	state Exp;
branches;
next	;
commitid	t01A1TLTYxkpGMLx;

1.1.1.1.4.1
date	2014.03.04.19.54.03;	author yamt;	state dead;
branches;
next	1.1.1.1.4.2;
commitid	WSrDtL5nYAUyiyBx;

1.1.1.1.4.2
date	2014.05.22.16.18.26;	author yamt;	state Exp;
branches;
next	;
commitid	WSrDtL5nYAUyiyBx;

1.1.1.3.2.1
date	2015.06.04.20.04.27;	author snj;	state Exp;
branches;
next	;
commitid	yRnjq9fueSo6n9oy;

1.1.1.3.4.1
date	2014.08.10.17.08.27;	author tls;	state dead;
branches;
next	1.1.1.3.4.2;
commitid	jTnpym9Qu0o4R1Nx;

1.1.1.3.4.2
date	2014.08.19.23.47.26;	author tls;	state Exp;
branches;
next	;
commitid	jTnpym9Qu0o4R1Nx;

1.1.1.5.2.1
date	2017.03.20.06.52.36;	author pgoyette;	state Exp;
branches;
next	;
commitid	jjw7cAwgyKq7RfKz;

1.1.1.7.2.1
date	2018.07.28.04.33.16;	author pgoyette;	state Exp;
branches;
next	;
commitid	1UP1xAIUxv1ZgRLA;

1.1.1.7.4.1
date	2019.06.10.21.45.20;	author christos;	state Exp;
branches;
next	1.1.1.7.4.2;
commitid	jtc8rnCzWiEEHGqB;

1.1.1.7.4.2
date	2020.04.13.07.46.31;	author martin;	state dead;
branches;
next	;
commitid	X01YhRUPVUDaec4C;


desc
@@


1.1
log
@Initial revision
@
text
@//===- VirtualFileSystem.cpp - Virtual File System Layer --------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// This file implements the VirtualFileSystem interface.
//===----------------------------------------------------------------------===//

#include "clang/Basic/VirtualFileSystem.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Atomic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/YAMLParser.h"

using namespace clang;
using namespace clang::vfs;
using namespace llvm;
using llvm::sys::fs::file_status;
using llvm::sys::fs::file_type;
using llvm::sys::fs::perms;
using llvm::sys::fs::UniqueID;

Status::Status(const file_status &Status)
    : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
      User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
      Type(Status.type()), Perms(Status.permissions()) {}

Status::Status(StringRef Name, StringRef ExternalName, UniqueID UID,
               sys::TimeValue MTime, uint32_t User, uint32_t Group,
               uint64_t Size, file_type Type, perms Perms)
    : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size),
      Type(Type), Perms(Perms) {}

bool Status::equivalent(const Status &Other) const {
  return getUniqueID() == Other.getUniqueID();
}
bool Status::isDirectory() const {
  return Type == file_type::directory_file;
}
bool Status::isRegularFile() const {
  return Type == file_type::regular_file;
}
bool Status::isOther() const {
  return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
}
bool Status::isSymlink() const {
  return Type == file_type::symlink_file;
}
bool Status::isStatusKnown() const {
  return Type != file_type::status_error;
}
bool Status::exists() const {
  return isStatusKnown() && Type != file_type::file_not_found;
}

File::~File() {}

FileSystem::~FileSystem() {}

error_code FileSystem::getBufferForFile(const llvm::Twine &Name,
                                        OwningPtr<MemoryBuffer> &Result,
                                        int64_t FileSize,
                                        bool RequiresNullTerminator) {
  llvm::OwningPtr<File> F;
  if (error_code EC = openFileForRead(Name, F))
    return EC;

  error_code EC = F->getBuffer(Name, Result, FileSize, RequiresNullTerminator);
  return EC;
}

//===-----------------------------------------------------------------------===/
// RealFileSystem implementation
//===-----------------------------------------------------------------------===/

/// \brief Wrapper around a raw file descriptor.
class RealFile : public File {
  int FD;
  Status S;
  friend class RealFileSystem;
  RealFile(int FD) : FD(FD) {
    assert(FD >= 0 && "Invalid or inactive file descriptor");
  }

public:
  ~RealFile();
  ErrorOr<Status> status() LLVM_OVERRIDE;
  error_code getBuffer(const Twine &Name, OwningPtr<MemoryBuffer> &Result,
                       int64_t FileSize = -1,
                       bool RequiresNullTerminator = true) LLVM_OVERRIDE;
  error_code close() LLVM_OVERRIDE;
  void setName(StringRef Name) LLVM_OVERRIDE;
};
RealFile::~RealFile() { close(); }

ErrorOr<Status> RealFile::status() {
  assert(FD != -1 && "cannot stat closed file");
  if (!S.isStatusKnown()) {
    file_status RealStatus;
    if (error_code EC = sys::fs::status(FD, RealStatus))
      return EC;
    Status NewS(RealStatus);
    NewS.setName(S.getName());
    S = llvm_move(NewS);
  }
  return S;
}

error_code RealFile::getBuffer(const Twine &Name,
                               OwningPtr<MemoryBuffer> &Result,
                               int64_t FileSize, bool RequiresNullTerminator) {
  assert(FD != -1 && "cannot get buffer for closed file");
  return MemoryBuffer::getOpenFile(FD, Name.str().c_str(), Result, FileSize,
                                   RequiresNullTerminator);
}

// FIXME: This is terrible, we need this for ::close.
#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <unistd.h>
#include <sys/uio.h>
#else
#include <io.h>
#ifndef S_ISFIFO
#define S_ISFIFO(x) (0)
#endif
#endif
error_code RealFile::close() {
  if (::close(FD))
    return error_code(errno, system_category());
  FD = -1;
  return error_code::success();
}

void RealFile::setName(StringRef Name) {
  S.setName(Name);
}

/// \brief The file system according to your operating system.
class RealFileSystem : public FileSystem {
public:
  ErrorOr<Status> status(const Twine &Path) LLVM_OVERRIDE;
  error_code openFileForRead(const Twine &Path,
                             OwningPtr<File> &Result) LLVM_OVERRIDE;
};

ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
  sys::fs::file_status RealStatus;
  if (error_code EC = sys::fs::status(Path, RealStatus))
    return EC;
  Status Result(RealStatus);
  Result.setName(Path.str());
  return Result;
}

error_code RealFileSystem::openFileForRead(const Twine &Name,
                                           OwningPtr<File> &Result) {
  int FD;
  if (error_code EC = sys::fs::openFileForRead(Name, FD))
    return EC;
  Result.reset(new RealFile(FD));
  Result->setName(Name.str());
  return error_code::success();
}

IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
  static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem();
  return FS;
}

//===-----------------------------------------------------------------------===/
// OverlayFileSystem implementation
//===-----------------------------------------------------------------------===/
OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
  pushOverlay(BaseFS);
}

void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
  FSList.push_back(FS);
}

ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
  // FIXME: handle symlinks that cross file systems
  for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
    ErrorOr<Status> Status = (*I)->status(Path);
    if (Status || Status.getError() != errc::no_such_file_or_directory)
      return Status;
  }
  return error_code(errc::no_such_file_or_directory, system_category());
}

error_code OverlayFileSystem::openFileForRead(const llvm::Twine &Path,
                                              OwningPtr<File> &Result) {
  // FIXME: handle symlinks that cross file systems
  for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
    error_code EC = (*I)->openFileForRead(Path, Result);
    if (!EC || EC != errc::no_such_file_or_directory)
      return EC;
  }
  return error_code(errc::no_such_file_or_directory, system_category());
}

//===-----------------------------------------------------------------------===/
// VFSFromYAML implementation
//===-----------------------------------------------------------------------===/

// Allow DenseMap<StringRef, ...>.  This is useful below because we know all the
// strings are literals and will outlive the map, and there is no reason to
// store them.
namespace llvm {
  template<>
  struct DenseMapInfo<StringRef> {
    // This assumes that "" will never be a valid key.
    static inline StringRef getEmptyKey() { return StringRef(""); }
    static inline StringRef getTombstoneKey() { return StringRef(); }
    static unsigned getHashValue(StringRef Val) { return HashString(Val); }
    static bool isEqual(StringRef LHS, StringRef RHS) { return LHS == RHS; }
  };
}

namespace {

enum EntryKind {
  EK_Directory,
  EK_File
};

/// \brief A single file or directory in the VFS.
class Entry {
  EntryKind Kind;
  std::string Name;

public:
  virtual ~Entry();
  Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
  StringRef getName() const { return Name; }
  EntryKind getKind() const { return Kind; }
};

class DirectoryEntry : public Entry {
  std::vector<Entry *> Contents;
  Status S;

public:
  virtual ~DirectoryEntry();
#if LLVM_HAS_RVALUE_REFERENCES
  DirectoryEntry(StringRef Name, std::vector<Entry *> Contents, Status S)
      : Entry(EK_Directory, Name), Contents(std::move(Contents)),
        S(std::move(S)) {}
#endif
  DirectoryEntry(StringRef Name, ArrayRef<Entry *> Contents, const Status &S)
      : Entry(EK_Directory, Name), Contents(Contents), S(S) {}
  Status getStatus() { return S; }
  typedef std::vector<Entry *>::iterator iterator;
  iterator contents_begin() { return Contents.begin(); }
  iterator contents_end() { return Contents.end(); }
  static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
};

class FileEntry : public Entry {
public:
  enum NameKind {
    NK_NotSet,
    NK_External,
    NK_Virtual
  };
private:
  std::string ExternalContentsPath;
  NameKind UseName;
public:
  FileEntry(StringRef Name, StringRef ExternalContentsPath, NameKind UseName)
      : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath),
        UseName(UseName) {}
  StringRef getExternalContentsPath() const { return ExternalContentsPath; }
  /// \brief whether to use the external path as the name for this file.
  bool useExternalName(bool GlobalUseExternalName) const {
    return UseName == NK_NotSet ? GlobalUseExternalName
                                : (UseName == NK_External);
  }
  static bool classof(const Entry *E) { return E->getKind() == EK_File; }
};

/// \brief A virtual file system parsed from a YAML file.
///
/// Currently, this class allows creating virtual directories and mapping
/// virtual file paths to existing external files, available in \c ExternalFS.
///
/// The basic structure of the parsed file is:
/// \verbatim
/// {
///   'version': <version number>,
///   <optional configuration>
///   'roots': [
///              <directory entries>
///            ]
/// }
/// \endverbatim
///
/// All configuration options are optional.
///   'case-sensitive': <boolean, default=true>
///   'use-external-names': <boolean, default=true>
///
/// Virtual directories are represented as
/// \verbatim
/// {
///   'type': 'directory',
///   'name': <string>,
///   'contents': [ <file or directory entries> ]
/// }
/// \endverbatim
///
/// The default attributes for virtual directories are:
/// \verbatim
/// MTime = now() when created
/// Perms = 0777
/// User = Group = 0
/// Size = 0
/// UniqueID = unspecified unique value
/// \endverbatim
///
/// Re-mapped files are represented as
/// \verbatim
/// {
///   'type': 'file',
///   'name': <string>,
///   'use-external-name': <boolean> # Optional
///   'external-contents': <path to external file>)
/// }
/// \endverbatim
///
/// and inherit their attributes from the external contents.
///
/// In both cases, the 'name' field may contain multiple path components (e.g.
/// /path/to/file). However, any directory that contains more than one child
/// must be uniquely represented by a directory entry.
class VFSFromYAML : public vfs::FileSystem {
  std::vector<Entry *> Roots; ///< The root(s) of the virtual file system.
  /// \brief The file system to use for external references.
  IntrusiveRefCntPtr<FileSystem> ExternalFS;

  /// @@name Configuration
  /// @@{

  /// \brief Whether to perform case-sensitive comparisons.
  ///
  /// Currently, case-insensitive matching only works correctly with ASCII.
  bool CaseSensitive;

  /// \brief Whether to use to use the value of 'external-contents' for the
  /// names of files.  This global value is overridable on a per-file basis.
  bool UseExternalNames;
  /// @@}

  friend class VFSFromYAMLParser;

private:
  VFSFromYAML(IntrusiveRefCntPtr<FileSystem> ExternalFS)
      : ExternalFS(ExternalFS), CaseSensitive(true), UseExternalNames(true) {}

  /// \brief Looks up \p Path in \c Roots.
  ErrorOr<Entry *> lookupPath(const Twine &Path);

  /// \brief Looks up the path <tt>[Start, End)</tt> in \p From, possibly
  /// recursing into the contents of \p From if it is a directory.
  ErrorOr<Entry *> lookupPath(sys::path::const_iterator Start,
                              sys::path::const_iterator End, Entry *From);

public:
  ~VFSFromYAML();

  /// \brief Parses \p Buffer, which is expected to be in YAML format and
  /// returns a virtual file system representing its contents.
  ///
  /// Takes ownership of \p Buffer.
  static VFSFromYAML *create(MemoryBuffer *Buffer,
                             SourceMgr::DiagHandlerTy DiagHandler,
                             void *DiagContext,
                             IntrusiveRefCntPtr<FileSystem> ExternalFS);

  ErrorOr<Status> status(const Twine &Path) LLVM_OVERRIDE;
  error_code openFileForRead(const Twine &Path,
                             OwningPtr<File> &Result) LLVM_OVERRIDE;
};

/// \brief A helper class to hold the common YAML parsing state.
class VFSFromYAMLParser {
  yaml::Stream &Stream;

  void error(yaml::Node *N, const Twine &Msg) {
    Stream.printError(N, Msg);
  }

  // false on error
  bool parseScalarString(yaml::Node *N, StringRef &Result,
                         SmallVectorImpl<char> &Storage) {
    yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N);
    if (!S) {
      error(N, "expected string");
      return false;
    }
    Result = S->getValue(Storage);
    return true;
  }

  // false on error
  bool parseScalarBool(yaml::Node *N, bool &Result) {
    SmallString<5> Storage;
    StringRef Value;
    if (!parseScalarString(N, Value, Storage))
      return false;

    if (Value.equals_lower("true") || Value.equals_lower("on") ||
        Value.equals_lower("yes") || Value == "1") {
      Result = true;
      return true;
    } else if (Value.equals_lower("false") || Value.equals_lower("off") ||
               Value.equals_lower("no") || Value == "0") {
      Result = false;
      return true;
    }

    error(N, "expected boolean value");
    return false;
  }

  struct KeyStatus {
    KeyStatus(bool Required=false) : Required(Required), Seen(false) {}
    bool Required;
    bool Seen;
  };
  typedef std::pair<StringRef, KeyStatus> KeyStatusPair;

  // false on error
  bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
                                  DenseMap<StringRef, KeyStatus> &Keys) {
    if (!Keys.count(Key)) {
      error(KeyNode, "unknown key");
      return false;
    }
    KeyStatus &S = Keys[Key];
    if (S.Seen) {
      error(KeyNode, Twine("duplicate key '") + Key + "'");
      return false;
    }
    S.Seen = true;
    return true;
  }

  // false on error
  bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
    for (DenseMap<StringRef, KeyStatus>::iterator I = Keys.begin(),
         E = Keys.end();
         I != E; ++I) {
      if (I->second.Required && !I->second.Seen) {
        error(Obj, Twine("missing key '") + I->first + "'");
        return false;
      }
    }
    return true;
  }

  Entry *parseEntry(yaml::Node *N) {
    yaml::MappingNode *M = dyn_cast<yaml::MappingNode>(N);
    if (!M) {
      error(N, "expected mapping node for file or directory entry");
      return NULL;
    }

    KeyStatusPair Fields[] = {
      KeyStatusPair("name", true),
      KeyStatusPair("type", true),
      KeyStatusPair("contents", false),
      KeyStatusPair("external-contents", false),
      KeyStatusPair("use-external-name", false),
    };

    DenseMap<StringRef, KeyStatus> Keys(
        &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));

    bool HasContents = false; // external or otherwise
    std::vector<Entry *> EntryArrayContents;
    std::string ExternalContentsPath;
    std::string Name;
    FileEntry::NameKind UseExternalName = FileEntry::NK_NotSet;
    EntryKind Kind;

    for (yaml::MappingNode::iterator I = M->begin(), E = M->end(); I != E;
         ++I) {
      StringRef Key;
      // Reuse the buffer for key and value, since we don't look at key after
      // parsing value.
      SmallString<256> Buffer;
      if (!parseScalarString(I->getKey(), Key, Buffer))
        return NULL;

      if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
        return NULL;

      StringRef Value;
      if (Key == "name") {
        if (!parseScalarString(I->getValue(), Value, Buffer))
          return NULL;
        Name = Value;
      } else if (Key == "type") {
        if (!parseScalarString(I->getValue(), Value, Buffer))
          return NULL;
        if (Value == "file")
          Kind = EK_File;
        else if (Value == "directory")
          Kind = EK_Directory;
        else {
          error(I->getValue(), "unknown value for 'type'");
          return NULL;
        }
      } else if (Key == "contents") {
        if (HasContents) {
          error(I->getKey(),
                "entry already has 'contents' or 'external-contents'");
          return NULL;
        }
        HasContents = true;
        yaml::SequenceNode *Contents =
            dyn_cast<yaml::SequenceNode>(I->getValue());
        if (!Contents) {
          // FIXME: this is only for directories, what about files?
          error(I->getValue(), "expected array");
          return NULL;
        }

        for (yaml::SequenceNode::iterator I = Contents->begin(),
                                          E = Contents->end();
             I != E; ++I) {
          if (Entry *E = parseEntry(&*I))
            EntryArrayContents.push_back(E);
          else
            return NULL;
        }
      } else if (Key == "external-contents") {
        if (HasContents) {
          error(I->getKey(),
                "entry already has 'contents' or 'external-contents'");
          return NULL;
        }
        HasContents = true;
        if (!parseScalarString(I->getValue(), Value, Buffer))
          return NULL;
        ExternalContentsPath = Value;
      } else if (Key == "use-external-name") {
        bool Val;
        if (!parseScalarBool(I->getValue(), Val))
          return NULL;
        UseExternalName = Val ? FileEntry::NK_External : FileEntry::NK_Virtual;
      } else {
        llvm_unreachable("key missing from Keys");
      }
    }

    if (Stream.failed())
      return NULL;

    // check for missing keys
    if (!HasContents) {
      error(N, "missing key 'contents' or 'external-contents'");
      return NULL;
    }
    if (!checkMissingKeys(N, Keys))
      return NULL;

    // check invalid configuration
    if (Kind == EK_Directory && UseExternalName != FileEntry::NK_NotSet) {
      error(N, "'use-external-name' is not supported for directories");
      return NULL;
    }

    // Remove trailing slash(es)
    StringRef Trimmed(Name);
    while (Trimmed.size() > 1 && sys::path::is_separator(Trimmed.back()))
      Trimmed = Trimmed.slice(0, Trimmed.size()-1);
    // Get the last component
    StringRef LastComponent = sys::path::filename(Trimmed);

    Entry *Result = 0;
    switch (Kind) {
    case EK_File:
      Result = new FileEntry(LastComponent, llvm_move(ExternalContentsPath),
                             UseExternalName);
      break;
    case EK_Directory:
      Result = new DirectoryEntry(LastComponent, llvm_move(EntryArrayContents),
          Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
                 0, file_type::directory_file, sys::fs::all_all));
      break;
    }

    StringRef Parent = sys::path::parent_path(Trimmed);
    if (Parent.empty())
      return Result;

    // if 'name' contains multiple components, create implicit directory entries
    for (sys::path::reverse_iterator I = sys::path::rbegin(Parent),
                                     E = sys::path::rend(Parent);
         I != E; ++I) {
      Result = new DirectoryEntry(*I, Result,
          Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
                 0, file_type::directory_file, sys::fs::all_all));
    }
    return Result;
  }

public:
  VFSFromYAMLParser(yaml::Stream &S) : Stream(S) {}

  // false on error
  bool parse(yaml::Node *Root, VFSFromYAML *FS) {
    yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root);
    if (!Top) {
      error(Root, "expected mapping node");
      return false;
    }

    KeyStatusPair Fields[] = {
      KeyStatusPair("version", true),
      KeyStatusPair("case-sensitive", false),
      KeyStatusPair("use-external-names", false),
      KeyStatusPair("roots", true),
    };

    DenseMap<StringRef, KeyStatus> Keys(
        &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));

    // Parse configuration and 'roots'
    for (yaml::MappingNode::iterator I = Top->begin(), E = Top->end(); I != E;
         ++I) {
      SmallString<10> KeyBuffer;
      StringRef Key;
      if (!parseScalarString(I->getKey(), Key, KeyBuffer))
        return false;

      if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
        return false;

      if (Key == "roots") {
        yaml::SequenceNode *Roots = dyn_cast<yaml::SequenceNode>(I->getValue());
        if (!Roots) {
          error(I->getValue(), "expected array");
          return false;
        }

        for (yaml::SequenceNode::iterator I = Roots->begin(), E = Roots->end();
             I != E; ++I) {
          if (Entry *E = parseEntry(&*I))
            FS->Roots.push_back(E);
          else
            return false;
        }
      } else if (Key == "version") {
        StringRef VersionString;
        SmallString<4> Storage;
        if (!parseScalarString(I->getValue(), VersionString, Storage))
          return false;
        int Version;
        if (VersionString.getAsInteger<int>(10, Version)) {
          error(I->getValue(), "expected integer");
          return false;
        }
        if (Version < 0) {
          error(I->getValue(), "invalid version number");
          return false;
        }
        if (Version != 0) {
          error(I->getValue(), "version mismatch, expected 0");
          return false;
        }
      } else if (Key == "case-sensitive") {
        if (!parseScalarBool(I->getValue(), FS->CaseSensitive))
          return false;
      } else if (Key == "use-external-names") {
        if (!parseScalarBool(I->getValue(), FS->UseExternalNames))
          return false;
      } else {
        llvm_unreachable("key missing from Keys");
      }
    }

    if (Stream.failed())
      return false;

    if (!checkMissingKeys(Top, Keys))
      return false;
    return true;
  }
};
} // end of anonymous namespace

Entry::~Entry() {}
DirectoryEntry::~DirectoryEntry() { llvm::DeleteContainerPointers(Contents); }

VFSFromYAML::~VFSFromYAML() { llvm::DeleteContainerPointers(Roots); }

VFSFromYAML *VFSFromYAML::create(MemoryBuffer *Buffer,
                                 SourceMgr::DiagHandlerTy DiagHandler,
                                 void *DiagContext,
                                 IntrusiveRefCntPtr<FileSystem> ExternalFS) {

  SourceMgr SM;
  yaml::Stream Stream(Buffer, SM);

  SM.setDiagHandler(DiagHandler, DiagContext);
  yaml::document_iterator DI = Stream.begin();
  yaml::Node *Root = DI->getRoot();
  if (DI == Stream.end() || !Root) {
    SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
    return NULL;
  }

  VFSFromYAMLParser P(Stream);

  OwningPtr<VFSFromYAML> FS(new VFSFromYAML(ExternalFS));
  if (!P.parse(Root, FS.get()))
    return NULL;

  return FS.take();
}

ErrorOr<Entry *> VFSFromYAML::lookupPath(const Twine &Path_) {
  SmallVector<char, 256> Storage;
  StringRef Path = Path_.toNullTerminatedStringRef(Storage);

  if (Path.empty())
    return error_code(errc::invalid_argument, system_category());

  sys::path::const_iterator Start = sys::path::begin(Path);
  sys::path::const_iterator End = sys::path::end(Path);
  for (std::vector<Entry *>::iterator I = Roots.begin(), E = Roots.end();
       I != E; ++I) {
    ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
    if (Result || Result.getError() != errc::no_such_file_or_directory)
      return Result;
  }
  return error_code(errc::no_such_file_or_directory, system_category());
}

ErrorOr<Entry *> VFSFromYAML::lookupPath(sys::path::const_iterator Start,
                                         sys::path::const_iterator End,
                                         Entry *From) {
  // FIXME: handle . and ..
  if (CaseSensitive ? !Start->equals(From->getName())
                    : !Start->equals_lower(From->getName()))
    // failure to match
    return error_code(errc::no_such_file_or_directory, system_category());

  ++Start;

  if (Start == End) {
    // Match!
    return From;
  }

  DirectoryEntry *DE = dyn_cast<DirectoryEntry>(From);
  if (!DE)
    return error_code(errc::not_a_directory, system_category());

  for (DirectoryEntry::iterator I = DE->contents_begin(),
                                E = DE->contents_end();
       I != E; ++I) {
    ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
    if (Result || Result.getError() != errc::no_such_file_or_directory)
      return Result;
  }
  return error_code(errc::no_such_file_or_directory, system_category());
}

ErrorOr<Status> VFSFromYAML::status(const Twine &Path) {
  ErrorOr<Entry *> Result = lookupPath(Path);
  if (!Result)
    return Result.getError();

  std::string PathStr(Path.str());
  if (FileEntry *F = dyn_cast<FileEntry>(*Result)) {
    ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath());
    assert(!S || S->getName() == F->getExternalContentsPath());
    if (S && !F->useExternalName(UseExternalNames))
      S->setName(PathStr);
    return S;
  } else { // directory
    DirectoryEntry *DE = cast<DirectoryEntry>(*Result);
    Status S = DE->getStatus();
    S.setName(PathStr);
    return S;
  }
}

error_code VFSFromYAML::openFileForRead(const Twine &Path,
                                        OwningPtr<vfs::File> &Result) {
  ErrorOr<Entry *> E = lookupPath(Path);
  if (!E)
    return E.getError();

  FileEntry *F = dyn_cast<FileEntry>(*E);
  if (!F) // FIXME: errc::not_a_file?
    return error_code(errc::invalid_argument, system_category());

  if (error_code EC = ExternalFS->openFileForRead(F->getExternalContentsPath(),
                                                  Result))
    return EC;

  if (!F->useExternalName(UseExternalNames))
    Result->setName(Path.str());

  return error_code::success();
}

IntrusiveRefCntPtr<FileSystem>
vfs::getVFSFromYAML(MemoryBuffer *Buffer, SourceMgr::DiagHandlerTy DiagHandler,
                    void *DiagContext,
                    IntrusiveRefCntPtr<FileSystem> ExternalFS) {
  return VFSFromYAML::create(Buffer, DiagHandler, DiagContext, ExternalFS);
}

UniqueID vfs::getNextVirtualUniqueID() {
  static volatile sys::cas_flag UID = 0;
  sys::cas_flag ID = llvm::sys::AtomicIncrement(&UID);
  // The following assumes that uint64_t max will never collide with a real
  // dev_t value from the OS.
  return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
}
@


1.1.1.1
log
@Import Clang 3.5svn r202566.
@
text
@@


1.1.1.1.2.1
log
@Rebase.
@
text
@d14 1
a14 1
#include "llvm/ADT/iterator_range.h"
d17 1
a20 2
#include <atomic>
#include <memory>
d33 1
a33 1
      Type(Status.type()), Perms(Status.permissions()), IsVFSMapped(false)  {}
d39 1
a39 1
      Type(Type), Perms(Perms), IsVFSMapped(false) {}
d68 1
a68 1
                                        std::unique_ptr<MemoryBuffer> &Result,
d70 2
a71 3
                                        bool RequiresNullTerminator,
                                        bool IsVolatile) {
  std::unique_ptr<File> F;
d75 1
a75 2
  error_code EC = F->getBuffer(Name, Result, FileSize, RequiresNullTerminator,
                               IsVolatile);
a82 1
namespace {
d94 2
a95 2
  ErrorOr<Status> status() override;
  error_code getBuffer(const Twine &Name, std::unique_ptr<MemoryBuffer> &Result,
d97 3
a99 4
                       bool RequiresNullTerminator = true,
                       bool IsVolatile = false) override;
  error_code close() override;
  void setName(StringRef Name) override;
a100 1
} // end anonymous namespace
d111 1
a111 1
    S = std::move(NewS);
d117 2
a118 3
                               std::unique_ptr<MemoryBuffer> &Result,
                               int64_t FileSize, bool RequiresNullTerminator,
                               bool IsVolatile) {
d121 1
a121 1
                                   RequiresNullTerminator, IsVolatile);
a144 1
namespace {
d148 1
a148 1
  ErrorOr<Status> status(const Twine &Path) override;
d150 1
a150 1
                             std::unique_ptr<File> &Result) override;
a151 1
} // end anonymous namespace
d163 1
a163 1
                                           std::unique_ptr<File> &Result) {
d199 1
a199 1
                                              std::unique_ptr<File> &Result) {
d252 1
d256 3
d386 1
a386 1
  ErrorOr<Status> status(const Twine &Path) override;
d388 1
a388 1
                             std::unique_ptr<File> &Result) override;
d472 1
a472 1
      return nullptr;
d500 1
a500 1
        return nullptr;
d503 1
a503 1
        return nullptr;
d508 1
a508 1
          return nullptr;
d512 1
a512 1
          return nullptr;
d519 1
a519 1
          return nullptr;
d525 1
a525 1
          return nullptr;
d533 1
a533 1
          return nullptr;
d542 1
a542 1
            return nullptr;
d548 1
a548 1
          return nullptr;
d552 1
a552 1
          return nullptr;
d557 1
a557 1
          return nullptr;
d565 1
a565 1
      return nullptr;
d570 1
a570 1
      return nullptr;
d573 1
a573 1
      return nullptr;
d578 1
a578 1
      return nullptr;
d581 1
a581 1
    // Remove trailing slash(es), being careful not to remove the root path
d583 1
a583 3
    size_t RootPathLen = sys::path::root_path(Trimmed).size();
    while (Trimmed.size() > RootPathLen &&
           sys::path::is_separator(Trimmed.back()))
d588 1
a588 1
    Entry *Result = nullptr;
d591 1
a591 1
      Result = new FileEntry(LastComponent, std::move(ExternalContentsPath),
d595 1
a595 1
      Result = new DirectoryEntry(LastComponent, std::move(EntryArrayContents),
d609 1
a609 1
      Result = new DirectoryEntry(*I, llvm::makeArrayRef(Result),
d719 1
a719 1
    return nullptr;
d724 1
a724 1
  std::unique_ptr<VFSFromYAML> FS(new VFSFromYAML(ExternalFS));
d726 1
a726 1
    return nullptr;
d728 1
a728 1
  return FS.release();
d732 2
a733 6
  SmallString<256> Path;
  Path_.toVector(Path);

  // Handle relative paths
  if (error_code EC = sys::fs::make_absolute(Path))
    return EC;
d752 1
a752 4
  if (Start->equals("."))
    ++Start;

  // FIXME: handle ..
a789 2
    if (S)
      S->IsVFSMapped = true;
d800 1
a800 1
                                        std::unique_ptr<vfs::File> &Result) {
d827 2
a828 2
  static std::atomic<unsigned> UID;
  unsigned ID = ++UID;
a832 135

#ifndef NDEBUG
static bool pathHasTraversal(StringRef Path) {
  using namespace llvm::sys;
  for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path)))
    if (Comp == "." || Comp == "..")
      return true;
  return false;
}
#endif

void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) {
  assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute");
  assert(sys::path::is_absolute(RealPath) && "real path not absolute");
  assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported");
  Mappings.emplace_back(VirtualPath, RealPath);
}

namespace {
class JSONWriter {
  llvm::raw_ostream &OS;
  SmallVector<StringRef, 16> DirStack;
  inline unsigned getDirIndent() { return 4 * DirStack.size(); }
  inline unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
  bool containedIn(StringRef Parent, StringRef Path);
  StringRef containedPart(StringRef Parent, StringRef Path);
  void startDirectory(StringRef Path);
  void endDirectory();
  void writeEntry(StringRef VPath, StringRef RPath);

public:
  JSONWriter(llvm::raw_ostream &OS) : OS(OS) {}
  void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> IsCaseSensitive);
};
}

bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
  using namespace llvm::sys;
  // Compare each path component.
  auto IParent = path::begin(Parent), EParent = path::end(Parent);
  for (auto IChild = path::begin(Path), EChild = path::end(Path);
       IParent != EParent && IChild != EChild; ++IParent, ++IChild) {
    if (*IParent != *IChild)
      return false;
  }
  // Have we exhausted the parent path?
  return IParent == EParent;
}

StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
  assert(!Parent.empty());
  assert(containedIn(Parent, Path));
  return Path.slice(Parent.size() + 1, StringRef::npos);
}

void JSONWriter::startDirectory(StringRef Path) {
  StringRef Name =
      DirStack.empty() ? Path : containedPart(DirStack.back(), Path);
  DirStack.push_back(Path);
  unsigned Indent = getDirIndent();
  OS.indent(Indent) << "{\n";
  OS.indent(Indent + 2) << "'type': 'directory',\n";
  OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n";
  OS.indent(Indent + 2) << "'contents': [\n";
}

void JSONWriter::endDirectory() {
  unsigned Indent = getDirIndent();
  OS.indent(Indent + 2) << "]\n";
  OS.indent(Indent) << "}";

  DirStack.pop_back();
}

void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) {
  unsigned Indent = getFileIndent();
  OS.indent(Indent) << "{\n";
  OS.indent(Indent + 2) << "'type': 'file',\n";
  OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n";
  OS.indent(Indent + 2) << "'external-contents': \""
                        << llvm::yaml::escape(RPath) << "\"\n";
  OS.indent(Indent) << "}";
}

void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
                       Optional<bool> IsCaseSensitive) {
  using namespace llvm::sys;

  OS << "{\n"
        "  'version': 0,\n";
  if (IsCaseSensitive.hasValue())
    OS << "  'case-sensitive': '"
       << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n";
  OS << "  'roots': [\n";

  if (Entries.empty())
    return;

  const YAMLVFSEntry &Entry = Entries.front();
  startDirectory(path::parent_path(Entry.VPath));
  writeEntry(path::filename(Entry.VPath), Entry.RPath);

  for (const auto &Entry : Entries.slice(1)) {
    StringRef Dir = path::parent_path(Entry.VPath);
    if (Dir == DirStack.back())
      OS << ",\n";
    else {
      while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) {
        OS << "\n";
        endDirectory();
      }
      OS << ",\n";
      startDirectory(Dir);
    }
    writeEntry(path::filename(Entry.VPath), Entry.RPath);
  }

  while (!DirStack.empty()) {
    OS << "\n";
    endDirectory();
  }

  OS << "\n"
     << "  ]\n"
     << "}\n";
}

void YAMLVFSWriter::write(llvm::raw_ostream &OS) {
  std::sort(Mappings.begin(), Mappings.end(),
            [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
    return LHS.VPath < RHS.VPath;
  });

  JSONWriter(OS).write(Mappings, IsCaseSensitive);
}
@


1.1.1.2
log
@Import Clang 3.5svn r209886.
@
text
@d14 1
a14 1
#include "llvm/ADT/iterator_range.h"
d17 1
a20 2
#include <atomic>
#include <memory>
d33 1
a33 1
      Type(Status.type()), Perms(Status.permissions()), IsVFSMapped(false)  {}
d39 1
a39 1
      Type(Type), Perms(Perms), IsVFSMapped(false) {}
d68 1
a68 1
                                        std::unique_ptr<MemoryBuffer> &Result,
d70 2
a71 3
                                        bool RequiresNullTerminator,
                                        bool IsVolatile) {
  std::unique_ptr<File> F;
d75 1
a75 2
  error_code EC = F->getBuffer(Name, Result, FileSize, RequiresNullTerminator,
                               IsVolatile);
a82 1
namespace {
d94 2
a95 2
  ErrorOr<Status> status() override;
  error_code getBuffer(const Twine &Name, std::unique_ptr<MemoryBuffer> &Result,
d97 3
a99 4
                       bool RequiresNullTerminator = true,
                       bool IsVolatile = false) override;
  error_code close() override;
  void setName(StringRef Name) override;
a100 1
} // end anonymous namespace
d111 1
a111 1
    S = std::move(NewS);
d117 2
a118 3
                               std::unique_ptr<MemoryBuffer> &Result,
                               int64_t FileSize, bool RequiresNullTerminator,
                               bool IsVolatile) {
d121 1
a121 1
                                   RequiresNullTerminator, IsVolatile);
a144 1
namespace {
d148 1
a148 1
  ErrorOr<Status> status(const Twine &Path) override;
d150 1
a150 1
                             std::unique_ptr<File> &Result) override;
a151 1
} // end anonymous namespace
d163 1
a163 1
                                           std::unique_ptr<File> &Result) {
d199 1
a199 1
                                              std::unique_ptr<File> &Result) {
d252 1
d256 3
d386 1
a386 1
  ErrorOr<Status> status(const Twine &Path) override;
d388 1
a388 1
                             std::unique_ptr<File> &Result) override;
d472 1
a472 1
      return nullptr;
d500 1
a500 1
        return nullptr;
d503 1
a503 1
        return nullptr;
d508 1
a508 1
          return nullptr;
d512 1
a512 1
          return nullptr;
d519 1
a519 1
          return nullptr;
d525 1
a525 1
          return nullptr;
d533 1
a533 1
          return nullptr;
d542 1
a542 1
            return nullptr;
d548 1
a548 1
          return nullptr;
d552 1
a552 1
          return nullptr;
d557 1
a557 1
          return nullptr;
d565 1
a565 1
      return nullptr;
d570 1
a570 1
      return nullptr;
d573 1
a573 1
      return nullptr;
d578 1
a578 1
      return nullptr;
d581 1
a581 1
    // Remove trailing slash(es), being careful not to remove the root path
d583 1
a583 3
    size_t RootPathLen = sys::path::root_path(Trimmed).size();
    while (Trimmed.size() > RootPathLen &&
           sys::path::is_separator(Trimmed.back()))
d588 1
a588 1
    Entry *Result = nullptr;
d591 1
a591 1
      Result = new FileEntry(LastComponent, std::move(ExternalContentsPath),
d595 1
a595 1
      Result = new DirectoryEntry(LastComponent, std::move(EntryArrayContents),
d609 1
a609 1
      Result = new DirectoryEntry(*I, llvm::makeArrayRef(Result),
d719 1
a719 1
    return nullptr;
d724 1
a724 1
  std::unique_ptr<VFSFromYAML> FS(new VFSFromYAML(ExternalFS));
d726 1
a726 1
    return nullptr;
d728 1
a728 1
  return FS.release();
d732 2
a733 6
  SmallString<256> Path;
  Path_.toVector(Path);

  // Handle relative paths
  if (error_code EC = sys::fs::make_absolute(Path))
    return EC;
d752 1
a752 4
  if (Start->equals("."))
    ++Start;

  // FIXME: handle ..
a789 2
    if (S)
      S->IsVFSMapped = true;
d800 1
a800 1
                                        std::unique_ptr<vfs::File> &Result) {
d827 2
a828 2
  static std::atomic<unsigned> UID;
  unsigned ID = ++UID;
a832 135

#ifndef NDEBUG
static bool pathHasTraversal(StringRef Path) {
  using namespace llvm::sys;
  for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path)))
    if (Comp == "." || Comp == "..")
      return true;
  return false;
}
#endif

void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) {
  assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute");
  assert(sys::path::is_absolute(RealPath) && "real path not absolute");
  assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported");
  Mappings.emplace_back(VirtualPath, RealPath);
}

namespace {
class JSONWriter {
  llvm::raw_ostream &OS;
  SmallVector<StringRef, 16> DirStack;
  inline unsigned getDirIndent() { return 4 * DirStack.size(); }
  inline unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
  bool containedIn(StringRef Parent, StringRef Path);
  StringRef containedPart(StringRef Parent, StringRef Path);
  void startDirectory(StringRef Path);
  void endDirectory();
  void writeEntry(StringRef VPath, StringRef RPath);

public:
  JSONWriter(llvm::raw_ostream &OS) : OS(OS) {}
  void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> IsCaseSensitive);
};
}

bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
  using namespace llvm::sys;
  // Compare each path component.
  auto IParent = path::begin(Parent), EParent = path::end(Parent);
  for (auto IChild = path::begin(Path), EChild = path::end(Path);
       IParent != EParent && IChild != EChild; ++IParent, ++IChild) {
    if (*IParent != *IChild)
      return false;
  }
  // Have we exhausted the parent path?
  return IParent == EParent;
}

StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
  assert(!Parent.empty());
  assert(containedIn(Parent, Path));
  return Path.slice(Parent.size() + 1, StringRef::npos);
}

void JSONWriter::startDirectory(StringRef Path) {
  StringRef Name =
      DirStack.empty() ? Path : containedPart(DirStack.back(), Path);
  DirStack.push_back(Path);
  unsigned Indent = getDirIndent();
  OS.indent(Indent) << "{\n";
  OS.indent(Indent + 2) << "'type': 'directory',\n";
  OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n";
  OS.indent(Indent + 2) << "'contents': [\n";
}

void JSONWriter::endDirectory() {
  unsigned Indent = getDirIndent();
  OS.indent(Indent + 2) << "]\n";
  OS.indent(Indent) << "}";

  DirStack.pop_back();
}

void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) {
  unsigned Indent = getFileIndent();
  OS.indent(Indent) << "{\n";
  OS.indent(Indent + 2) << "'type': 'file',\n";
  OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n";
  OS.indent(Indent + 2) << "'external-contents': \""
                        << llvm::yaml::escape(RPath) << "\"\n";
  OS.indent(Indent) << "}";
}

void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
                       Optional<bool> IsCaseSensitive) {
  using namespace llvm::sys;

  OS << "{\n"
        "  'version': 0,\n";
  if (IsCaseSensitive.hasValue())
    OS << "  'case-sensitive': '"
       << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n";
  OS << "  'roots': [\n";

  if (Entries.empty())
    return;

  const YAMLVFSEntry &Entry = Entries.front();
  startDirectory(path::parent_path(Entry.VPath));
  writeEntry(path::filename(Entry.VPath), Entry.RPath);

  for (const auto &Entry : Entries.slice(1)) {
    StringRef Dir = path::parent_path(Entry.VPath);
    if (Dir == DirStack.back())
      OS << ",\n";
    else {
      while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) {
        OS << "\n";
        endDirectory();
      }
      OS << ",\n";
      startDirectory(Dir);
    }
    writeEntry(path::filename(Entry.VPath), Entry.RPath);
  }

  while (!DirStack.empty()) {
    OS << "\n";
    endDirectory();
  }

  OS << "\n"
     << "  ]\n"
     << "}\n";
}

void YAMLVFSWriter::write(llvm::raw_ostream &OS) {
  std::sort(Mappings.begin(), Mappings.end(),
            [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
    return LHS.VPath < RHS.VPath;
  });

  JSONWriter(OS).write(Mappings, IsCaseSensitive);
}
@


1.1.1.3
log
@Import clang 3.6svn r215315.
@
text
@a16 2
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Errc.h"
d68 5
a72 3
std::error_code FileSystem::getBufferForFile(
    const llvm::Twine &Name, std::unique_ptr<MemoryBuffer> &Result,
    int64_t FileSize, bool RequiresNullTerminator, bool IsVolatile) {
d74 1
a74 1
  if (std::error_code EC = openFileForRead(Name, F))
d77 2
a78 2
  std::error_code EC =
      F->getBuffer(Name, Result, FileSize, RequiresNullTerminator, IsVolatile);
d99 5
a103 6
  std::error_code getBuffer(const Twine &Name,
                            std::unique_ptr<MemoryBuffer> &Result,
                            int64_t FileSize = -1,
                            bool RequiresNullTerminator = true,
                            bool IsVolatile = false) override;
  std::error_code close() override;
d113 1
a113 1
    if (std::error_code EC = sys::fs::status(FD, RealStatus))
d122 4
a125 5
std::error_code RealFile::getBuffer(const Twine &Name,
                                    std::unique_ptr<MemoryBuffer> &Result,
                                    int64_t FileSize,
                                    bool RequiresNullTerminator,
                                    bool IsVolatile) {
d127 2
a128 7
  ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
      MemoryBuffer::getOpenFile(FD, Name.str().c_str(), FileSize,
                                RequiresNullTerminator, IsVolatile);
  if (std::error_code EC = BufferOrErr.getError())
    return EC;
  Result = std::move(BufferOrErr.get());
  return std::error_code();
d141 1
a141 1
std::error_code RealFile::close() {
d143 1
a143 1
    return std::error_code(errno, std::generic_category());
d145 1
a145 1
  return std::error_code();
d157 2
a158 3
  std::error_code openFileForRead(const Twine &Path,
                                  std::unique_ptr<File> &Result) override;
  directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
d164 1
a164 1
  if (std::error_code EC = sys::fs::status(Path, RealStatus))
d171 2
a172 2
std::error_code RealFileSystem::openFileForRead(const Twine &Name,
                                                std::unique_ptr<File> &Result) {
d174 1
a174 1
  if (std::error_code EC = sys::fs::openFileForRead(Name, FD))
d178 1
a178 1
  return std::error_code();
a185 40
namespace {
class RealFSDirIter : public clang::vfs::detail::DirIterImpl {
  std::string Path;
  llvm::sys::fs::directory_iterator Iter;
public:
  RealFSDirIter(const Twine &_Path, std::error_code &EC)
      : Path(_Path.str()), Iter(Path, EC) {
    if (!EC && Iter != llvm::sys::fs::directory_iterator()) {
      llvm::sys::fs::file_status S;
      EC = Iter->status(S);
      if (!EC) {
        CurrentEntry = Status(S);
        CurrentEntry.setName(Iter->path());
      }
    }
  }

  std::error_code increment() override {
    std::error_code EC;
    Iter.increment(EC);
    if (EC) {
      return EC;
    } else if (Iter == llvm::sys::fs::directory_iterator()) {
      CurrentEntry = Status();
    } else {
      llvm::sys::fs::file_status S;
      EC = Iter->status(S);
      CurrentEntry = Status(S);
      CurrentEntry.setName(Iter->path());
    }
    return EC;
  }
};
}

directory_iterator RealFileSystem::dir_begin(const Twine &Dir,
                                             std::error_code &EC) {
  return directory_iterator(std::make_shared<RealFSDirIter>(Dir, EC));
}

d201 1
a201 1
    if (Status || Status.getError() != llvm::errc::no_such_file_or_directory)
d204 1
a204 1
  return make_error_code(llvm::errc::no_such_file_or_directory);
d207 2
a208 3
std::error_code
OverlayFileSystem::openFileForRead(const llvm::Twine &Path,
                                   std::unique_ptr<File> &Result) {
d211 2
a212 2
    std::error_code EC = (*I)->openFileForRead(Path, Result);
    if (!EC || EC != llvm::errc::no_such_file_or_directory)
d215 1
a215 69
  return make_error_code(llvm::errc::no_such_file_or_directory);
}

clang::vfs::detail::DirIterImpl::~DirIterImpl() { }

namespace {
class OverlayFSDirIterImpl : public clang::vfs::detail::DirIterImpl {
  OverlayFileSystem &Overlays;
  std::string Path;
  OverlayFileSystem::iterator CurrentFS;
  directory_iterator CurrentDirIter;
  llvm::StringSet<> SeenNames;

  std::error_code incrementFS() {
    assert(CurrentFS != Overlays.overlays_end() && "incrementing past end");
    ++CurrentFS;
    for (auto E = Overlays.overlays_end(); CurrentFS != E; ++CurrentFS) {
      std::error_code EC;
      CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
      if (EC && EC != errc::no_such_file_or_directory)
        return EC;
      if (CurrentDirIter != directory_iterator())
        break; // found
    }
    return std::error_code();
  }

  std::error_code incrementDirIter(bool IsFirstTime) {
    assert((IsFirstTime || CurrentDirIter != directory_iterator()) &&
           "incrementing past end");
    std::error_code EC;
    if (!IsFirstTime)
      CurrentDirIter.increment(EC);
    if (!EC && CurrentDirIter == directory_iterator())
      EC = incrementFS();
    return EC;
  }

  std::error_code incrementImpl(bool IsFirstTime) {
    while (true) {
      std::error_code EC = incrementDirIter(IsFirstTime);
      if (EC || CurrentDirIter == directory_iterator()) {
        CurrentEntry = Status();
        return EC;
      }
      CurrentEntry = *CurrentDirIter;
      StringRef Name = llvm::sys::path::filename(CurrentEntry.getName());
      if (SeenNames.insert(Name))
        return EC; // name not seen before
    }
    llvm_unreachable("returned above");
  }

public:
  OverlayFSDirIterImpl(const Twine &Path, OverlayFileSystem &FS,
                       std::error_code &EC)
      : Overlays(FS), Path(Path.str()), CurrentFS(Overlays.overlays_begin()) {
    CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
    EC = incrementImpl(true);
  }

  std::error_code increment() override { return incrementImpl(false); }
};
} // end anonymous namespace

directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir,
                                                std::error_code &EC) {
  return directory_iterator(
      std::make_shared<OverlayFSDirIterImpl>(Dir, *this, EC));
a293 13
class VFSFromYAML;

class VFSFromYamlDirIterImpl : public clang::vfs::detail::DirIterImpl {
  std::string Dir;
  VFSFromYAML &FS;
  DirectoryEntry::iterator Current, End;
public:
  VFSFromYamlDirIterImpl(const Twine &Path, VFSFromYAML &FS,
                         DirectoryEntry::iterator Begin,
                         DirectoryEntry::iterator End, std::error_code &EC);
  std::error_code increment() override;
};

a378 3
  /// \brief Get the status of a given an \c Entry.
  ErrorOr<Status> status(const Twine &Path, Entry *E);

d392 2
a393 24
  std::error_code openFileForRead(const Twine &Path,
                                  std::unique_ptr<File> &Result) override;

  directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override{
    ErrorOr<Entry *> E = lookupPath(Dir);
    if (!E) {
      EC = E.getError();
      return directory_iterator();
    }
    ErrorOr<Status> S = status(Dir, *E);
    if (!S) {
      EC = S.getError();
      return directory_iterator();
    }
    if (!S->isDirectory()) {
      EC = std::error_code(static_cast<int>(errc::not_a_directory),
                           std::system_category());
      return directory_iterator();
    }

    DirectoryEntry *D = cast<DirectoryEntry>(*E);
    return directory_iterator(std::make_shared<VFSFromYamlDirIterImpl>(Dir,
        *this, D->contents_begin(), D->contents_end(), EC));
  }
d743 1
a743 1
  if (std::error_code EC = sys::fs::make_absolute(Path))
d747 1
a747 1
    return make_error_code(llvm::errc::invalid_argument);
d754 1
a754 1
    if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
d757 1
a757 1
  return make_error_code(llvm::errc::no_such_file_or_directory);
d770 1
a770 1
    return make_error_code(llvm::errc::no_such_file_or_directory);
d781 1
a781 1
    return make_error_code(llvm::errc::not_a_directory);
d787 1
a787 1
    if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
d790 1
a790 1
  return make_error_code(llvm::errc::no_such_file_or_directory);
d793 5
a797 2
ErrorOr<Status> VFSFromYAML::status(const Twine &Path, Entry *E) {
  assert(E != nullptr);
d799 1
a799 1
  if (FileEntry *F = dyn_cast<FileEntry>(E)) {
d808 1
a808 1
    DirectoryEntry *DE = cast<DirectoryEntry>(E);
d815 2
a816 10
ErrorOr<Status> VFSFromYAML::status(const Twine &Path) {
  ErrorOr<Entry *> Result = lookupPath(Path);
  if (!Result)
    return Result.getError();
  return status(Path, *Result);
}

std::error_code
VFSFromYAML::openFileForRead(const Twine &Path,
                             std::unique_ptr<vfs::File> &Result) {
d823 1
a823 1
    return make_error_code(llvm::errc::invalid_argument);
d825 2
a826 2
  if (std::error_code EC =
          ExternalFS->openFileForRead(F->getExternalContentsPath(), Result))
d832 1
a832 1
  return std::error_code();
d944 2
a945 4
  if (!Entries.empty()) {
    const YAMLVFSEntry &Entry = Entries.front();
    startDirectory(path::parent_path(Entry.VPath));
    writeEntry(path::filename(Entry.VPath), Entry.RPath);
d947 12
a958 11
    for (const auto &Entry : Entries.slice(1)) {
      StringRef Dir = path::parent_path(Entry.VPath);
      if (Dir == DirStack.back())
        OS << ",\n";
      else {
        while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) {
          OS << "\n";
          endDirectory();
        }
        OS << ",\n";
        startDirectory(Dir);
d960 2
a961 1
      writeEntry(path::filename(Entry.VPath), Entry.RPath);
d963 2
d966 1
a966 4
    while (!DirStack.empty()) {
      OS << "\n";
      endDirectory();
    }
d968 1
d971 2
a972 1
  OS << "  ]\n"
a983 67

VFSFromYamlDirIterImpl::VFSFromYamlDirIterImpl(const Twine &_Path,
                                               VFSFromYAML &FS,
                                               DirectoryEntry::iterator Begin,
                                               DirectoryEntry::iterator End,
                                               std::error_code &EC)
    : Dir(_Path.str()), FS(FS), Current(Begin), End(End) {
  if (Current != End) {
    SmallString<128> PathStr(Dir);
    llvm::sys::path::append(PathStr, (*Current)->getName());
    llvm::ErrorOr<vfs::Status> S = FS.status(PathStr.str());
    if (S)
      CurrentEntry = *S;
    else
      EC = S.getError();
  }
}

std::error_code VFSFromYamlDirIterImpl::increment() {
  assert(Current != End && "cannot iterate past end");
  if (++Current != End) {
    SmallString<128> PathStr(Dir);
    llvm::sys::path::append(PathStr, (*Current)->getName());
    llvm::ErrorOr<vfs::Status> S = FS.status(PathStr.str());
    if (!S)
      return S.getError();
    CurrentEntry = *S;
  } else {
    CurrentEntry = Status();
  }
  return std::error_code();
}

vfs::recursive_directory_iterator::recursive_directory_iterator(FileSystem &FS_,
                                                           const Twine &Path,
                                                           std::error_code &EC)
    : FS(&FS_) {
  directory_iterator I = FS->dir_begin(Path, EC);
  if (!EC && I != directory_iterator()) {
    State = std::make_shared<IterState>();
    State->push(I);
  }
}

vfs::recursive_directory_iterator &
recursive_directory_iterator::increment(std::error_code &EC) {
  assert(FS && State && !State->empty() && "incrementing past end");
  assert(State->top()->isStatusKnown() && "non-canonical end iterator");
  vfs::directory_iterator End;
  if (State->top()->isDirectory()) {
    vfs::directory_iterator I = FS->dir_begin(State->top()->getName(), EC);
    if (EC)
      return *this;
    if (I != End) {
      State->push(I);
      return *this;
    }
  }

  while (!State->empty() && State->top().increment(EC) == End)
    State->pop();

  if (State->empty())
    State.reset(); // end iterator

  return *this;
}
@


1.1.1.3.2.1
log
@Update LLVM to 3.6.1, requested by joerg in ticket 824.
@
text
@d14 1
a17 1
#include "llvm/ADT/iterator_range.h"
d70 6
a75 6
ErrorOr<std::unique_ptr<MemoryBuffer>>
FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize,
                             bool RequiresNullTerminator, bool IsVolatile) {
  auto F = openFileForRead(Name);
  if (!F)
    return F.getError();
d77 3
a79 1
  return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile);
d99 5
a103 4
  ErrorOr<std::unique_ptr<MemoryBuffer>>
  getBuffer(const Twine &Name, int64_t FileSize = -1,
            bool RequiresNullTerminator = true,
            bool IsVolatile = false) override;
d123 5
a127 3
ErrorOr<std::unique_ptr<MemoryBuffer>>
RealFile::getBuffer(const Twine &Name, int64_t FileSize,
                    bool RequiresNullTerminator, bool IsVolatile) {
d129 7
a135 2
  return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator,
                                   IsVolatile);
d164 2
a165 1
  ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
d179 2
a180 2
ErrorOr<std::unique_ptr<File>>
RealFileSystem::openFileForRead(const Twine &Name) {
d184 1
a184 1
  std::unique_ptr<File> Result(new RealFile(FD));
d186 1
a186 1
  return std::move(Result);
d255 3
a257 2
ErrorOr<std::unique_ptr<File>>
OverlayFileSystem::openFileForRead(const llvm::Twine &Path) {
d260 3
a262 3
    auto Result = (*I)->openFileForRead(Path);
    if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
      return Result;
d311 1
a311 1
      if (SeenNames.insert(Name).second)
d517 3
a519 1
  static VFSFromYAML *create(std::unique_ptr<MemoryBuffer> Buffer,
d525 2
a526 1
  ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
d868 1
a868 1
VFSFromYAML *VFSFromYAML::create(std::unique_ptr<MemoryBuffer> Buffer,
d874 1
a874 1
  yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
d974 3
a976 1
ErrorOr<std::unique_ptr<File>> VFSFromYAML::openFileForRead(const Twine &Path) {
d985 3
a987 3
  auto Result = ExternalFS->openFileForRead(F->getExternalContentsPath());
  if (!Result)
    return Result;
d990 1
a990 1
    (*Result)->setName(Path.str());
d992 1
a992 1
  return Result;
d996 2
a997 2
vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
                    SourceMgr::DiagHandlerTy DiagHandler, void *DiagContext,
d999 1
a999 2
  return VFSFromYAML::create(std::move(Buffer), DiagHandler, DiagContext,
                             ExternalFS);
@


1.1.1.4
log
@Import Clang 3.6RC1 r227398.
@
text
@d14 1
a17 1
#include "llvm/ADT/iterator_range.h"
d70 6
a75 6
ErrorOr<std::unique_ptr<MemoryBuffer>>
FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize,
                             bool RequiresNullTerminator, bool IsVolatile) {
  auto F = openFileForRead(Name);
  if (!F)
    return F.getError();
d77 3
a79 1
  return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile);
d99 5
a103 4
  ErrorOr<std::unique_ptr<MemoryBuffer>>
  getBuffer(const Twine &Name, int64_t FileSize = -1,
            bool RequiresNullTerminator = true,
            bool IsVolatile = false) override;
d123 5
a127 3
ErrorOr<std::unique_ptr<MemoryBuffer>>
RealFile::getBuffer(const Twine &Name, int64_t FileSize,
                    bool RequiresNullTerminator, bool IsVolatile) {
d129 7
a135 2
  return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator,
                                   IsVolatile);
d164 2
a165 1
  ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
d179 2
a180 2
ErrorOr<std::unique_ptr<File>>
RealFileSystem::openFileForRead(const Twine &Name) {
d184 1
a184 1
  std::unique_ptr<File> Result(new RealFile(FD));
d186 1
a186 1
  return std::move(Result);
d255 3
a257 2
ErrorOr<std::unique_ptr<File>>
OverlayFileSystem::openFileForRead(const llvm::Twine &Path) {
d260 3
a262 3
    auto Result = (*I)->openFileForRead(Path);
    if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
      return Result;
d311 1
a311 1
      if (SeenNames.insert(Name).second)
d517 3
a519 1
  static VFSFromYAML *create(std::unique_ptr<MemoryBuffer> Buffer,
d525 2
a526 1
  ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
d868 1
a868 1
VFSFromYAML *VFSFromYAML::create(std::unique_ptr<MemoryBuffer> Buffer,
d874 1
a874 1
  yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
d974 3
a976 1
ErrorOr<std::unique_ptr<File>> VFSFromYAML::openFileForRead(const Twine &Path) {
d985 3
a987 3
  auto Result = ExternalFS->openFileForRead(F->getExternalContentsPath());
  if (!Result)
    return Result;
d990 1
a990 1
    (*Result)->setName(Path.str());
d992 1
a992 1
  return Result;
d996 2
a997 2
vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
                    SourceMgr::DiagHandlerTy DiagHandler, void *DiagContext,
d999 1
a999 2
  return VFSFromYAML::create(std::move(Buffer), DiagHandler, DiagContext,
                             ExternalFS);
@


1.1.1.5
log
@Import Clang 3.8.0rc3 r261930.
@
text
@a12 1
#include "clang/Basic/FileManager.h"
a21 1
#include "llvm/Config/llvm-config.h"
a24 7
// For chdir.
#ifdef LLVM_ON_WIN32
#  include <direct.h>
#else
#  include <unistd.h>
#endif

d38 3
a40 3
Status::Status(StringRef Name, UniqueID UID, sys::TimeValue MTime,
               uint32_t User, uint32_t Group, uint64_t Size, file_type Type,
               perms Perms)
a43 12
Status Status::copyWithNewName(const Status &In, StringRef NewName) {
  return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
                In.getUser(), In.getGroup(), In.getSize(), In.getType(),
                In.getPermissions());
}

Status Status::copyWithNewName(const file_status &In, StringRef NewName) {
  return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
                In.getUser(), In.getGroup(), In.getSize(), In.type(),
                In.permissions());
}

a79 13
std::error_code FileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
  auto WorkingDir = getCurrentWorkingDirectory();
  if (!WorkingDir)
    return WorkingDir.getError();

  return llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
}

bool FileSystem::exists(const Twine &Path) {
  auto Status = status(Path);
  return Status && Status->exists();
}

d90 1
a90 3
  RealFile(int FD, StringRef NewName)
      : FD(FD), S(NewName, {}, {}, {}, {}, {},
                  llvm::sys::fs::file_type::status_error, {}) {
d95 1
a95 1
  ~RealFile() override;
d97 4
a100 4
  ErrorOr<std::unique_ptr<MemoryBuffer>> getBuffer(const Twine &Name,
                                                   int64_t FileSize,
                                                   bool RequiresNullTerminator,
                                                   bool IsVolatile) override;
d102 1
d113 3
a115 1
    S = Status::copyWithNewName(RealStatus, S.getName());
d145 4
a155 3

  llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
  std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
d163 3
a165 1
  return Status::copyWithNewName(RealStatus, Path.str());
d173 3
a175 23
  return std::unique_ptr<File>(new RealFile(FD, Name.str()));
}

llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const {
  SmallString<256> Dir;
  if (std::error_code EC = llvm::sys::fs::current_path(Dir))
    return EC;
  return Dir.str().str();
}

std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
  // FIXME: chdir is thread hostile; on the other hand, creating the same
  // behavior as chdir is complex: chdir resolves the path once, thus
  // guaranteeing that all subsequent relative path operations work
  // on the same path the original chdir resulted in. This makes a
  // difference for example on network filesystems, where symlinks might be
  // switched during runtime of the tool. Fixing this depends on having a
  // file system abstraction that allows openat() style interactions.
  SmallString<256> Storage;
  StringRef Dir = Path.toNullTerminatedStringRef(Storage);
  if (int Err = ::chdir(Dir.data()))
    return std::error_code(Err, std::generic_category());
  return std::error_code();
d193 4
a196 2
      if (!EC)
        CurrentEntry = Status::copyWithNewName(S, Iter->path());
d210 2
a211 1
      CurrentEntry = Status::copyWithNewName(S, Iter->path());
d227 1
a227 1
  FSList.push_back(BaseFS);
a231 3
  // Synchronize added file systems by duplicating the working directory from
  // the first one in the list.
  FS->setCurrentWorkingDirectory(getCurrentWorkingDirectory().get());
a254 13
llvm::ErrorOr<std::string>
OverlayFileSystem::getCurrentWorkingDirectory() const {
  // All file systems are synchronized, just take the first working directory.
  return FSList.front()->getCurrentWorkingDirectory();
}
std::error_code
OverlayFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
  for (auto &FS : FSList)
    if (std::error_code EC = FS->setCurrentWorkingDirectory(Path))
      return EC;
  return std::error_code();
}

d323 3
a325 11
namespace clang {
namespace vfs {
namespace detail {

enum InMemoryNodeKind { IME_File, IME_Directory };

/// The in memory file system is a tree of Nodes. Every node can either be a
/// file or a directory.
class InMemoryNode {
  Status Stat;
  InMemoryNodeKind Kind;
d327 12
a338 223
public:
  InMemoryNode(Status Stat, InMemoryNodeKind Kind)
      : Stat(std::move(Stat)), Kind(Kind) {}
  virtual ~InMemoryNode() {}
  const Status &getStatus() const { return Stat; }
  InMemoryNodeKind getKind() const { return Kind; }
  virtual std::string toString(unsigned Indent) const = 0;
};

namespace {
class InMemoryFile : public InMemoryNode {
  std::unique_ptr<llvm::MemoryBuffer> Buffer;

public:
  InMemoryFile(Status Stat, std::unique_ptr<llvm::MemoryBuffer> Buffer)
      : InMemoryNode(std::move(Stat), IME_File), Buffer(std::move(Buffer)) {}

  llvm::MemoryBuffer *getBuffer() { return Buffer.get(); }
  std::string toString(unsigned Indent) const override {
    return (std::string(Indent, ' ') + getStatus().getName() + "\n").str();
  }
  static bool classof(const InMemoryNode *N) {
    return N->getKind() == IME_File;
  }
};

/// Adapt a InMemoryFile for VFS' File interface.
class InMemoryFileAdaptor : public File {
  InMemoryFile &Node;

public:
  explicit InMemoryFileAdaptor(InMemoryFile &Node) : Node(Node) {}

  llvm::ErrorOr<Status> status() override { return Node.getStatus(); }
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
            bool IsVolatile) override {
    llvm::MemoryBuffer *Buf = Node.getBuffer();
    return llvm::MemoryBuffer::getMemBuffer(
        Buf->getBuffer(), Buf->getBufferIdentifier(), RequiresNullTerminator);
  }
  std::error_code close() override { return std::error_code(); }
};
} // end anonymous namespace

class InMemoryDirectory : public InMemoryNode {
  std::map<std::string, std::unique_ptr<InMemoryNode>> Entries;

public:
  InMemoryDirectory(Status Stat)
      : InMemoryNode(std::move(Stat), IME_Directory) {}
  InMemoryNode *getChild(StringRef Name) {
    auto I = Entries.find(Name);
    if (I != Entries.end())
      return I->second.get();
    return nullptr;
  }
  InMemoryNode *addChild(StringRef Name, std::unique_ptr<InMemoryNode> Child) {
    return Entries.insert(make_pair(Name, std::move(Child)))
        .first->second.get();
  }

  typedef decltype(Entries)::const_iterator const_iterator;
  const_iterator begin() const { return Entries.begin(); }
  const_iterator end() const { return Entries.end(); }

  std::string toString(unsigned Indent) const override {
    std::string Result =
        (std::string(Indent, ' ') + getStatus().getName() + "\n").str();
    for (const auto &Entry : Entries) {
      Result += Entry.second->toString(Indent + 2);
    }
    return Result;
  }
  static bool classof(const InMemoryNode *N) {
    return N->getKind() == IME_Directory;
  }
};
}

InMemoryFileSystem::InMemoryFileSystem(bool UseNormalizedPaths)
    : Root(new detail::InMemoryDirectory(
          Status("", getNextVirtualUniqueID(), llvm::sys::TimeValue::MinTime(),
                 0, 0, 0, llvm::sys::fs::file_type::directory_file,
                 llvm::sys::fs::perms::all_all))),
      UseNormalizedPaths(UseNormalizedPaths) {}

InMemoryFileSystem::~InMemoryFileSystem() {}

std::string InMemoryFileSystem::toString() const {
  return Root->toString(/*Indent=*/0);
}

bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
                                 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
  SmallString<128> Path;
  P.toVector(Path);

  // Fix up relative paths. This just prepends the current working directory.
  std::error_code EC = makeAbsolute(Path);
  assert(!EC);
  (void)EC;

  if (useNormalizedPaths())
    llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);

  if (Path.empty())
    return false;

  detail::InMemoryDirectory *Dir = Root.get();
  auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path);
  while (true) {
    StringRef Name = *I;
    detail::InMemoryNode *Node = Dir->getChild(Name);
    ++I;
    if (!Node) {
      if (I == E) {
        // End of the path, create a new file.
        // FIXME: expose the status details in the interface.
        Status Stat(P.str(), getNextVirtualUniqueID(),
                    llvm::sys::TimeValue(ModificationTime, 0), 0, 0,
                    Buffer->getBufferSize(),
                    llvm::sys::fs::file_type::regular_file,
                    llvm::sys::fs::all_all);
        Dir->addChild(Name, llvm::make_unique<detail::InMemoryFile>(
                                std::move(Stat), std::move(Buffer)));
        return true;
      }

      // Create a new directory. Use the path up to here.
      // FIXME: expose the status details in the interface.
      Status Stat(
          StringRef(Path.str().begin(), Name.end() - Path.str().begin()),
          getNextVirtualUniqueID(), llvm::sys::TimeValue(ModificationTime, 0),
          0, 0, Buffer->getBufferSize(),
          llvm::sys::fs::file_type::directory_file, llvm::sys::fs::all_all);
      Dir = cast<detail::InMemoryDirectory>(Dir->addChild(
          Name, llvm::make_unique<detail::InMemoryDirectory>(std::move(Stat))));
      continue;
    }

    if (auto *NewDir = dyn_cast<detail::InMemoryDirectory>(Node)) {
      Dir = NewDir;
    } else {
      assert(isa<detail::InMemoryFile>(Node) &&
             "Must be either file or directory!");

      // Trying to insert a directory in place of a file.
      if (I != E)
        return false;

      // Return false only if the new file is different from the existing one.
      return cast<detail::InMemoryFile>(Node)->getBuffer()->getBuffer() ==
             Buffer->getBuffer();
    }
  }
}

bool InMemoryFileSystem::addFileNoOwn(const Twine &P, time_t ModificationTime,
                                      llvm::MemoryBuffer *Buffer) {
  return addFile(P, ModificationTime,
                 llvm::MemoryBuffer::getMemBuffer(
                     Buffer->getBuffer(), Buffer->getBufferIdentifier()));
}

static ErrorOr<detail::InMemoryNode *>
lookupInMemoryNode(const InMemoryFileSystem &FS, detail::InMemoryDirectory *Dir,
                   const Twine &P) {
  SmallString<128> Path;
  P.toVector(Path);

  // Fix up relative paths. This just prepends the current working directory.
  std::error_code EC = FS.makeAbsolute(Path);
  assert(!EC);
  (void)EC;

  if (FS.useNormalizedPaths())
    llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);

  if (Path.empty())
    return Dir;

  auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path);
  while (true) {
    detail::InMemoryNode *Node = Dir->getChild(*I);
    ++I;
    if (!Node)
      return errc::no_such_file_or_directory;

    // Return the file if it's at the end of the path.
    if (auto File = dyn_cast<detail::InMemoryFile>(Node)) {
      if (I == E)
        return File;
      return errc::no_such_file_or_directory;
    }

    // Traverse directories.
    Dir = cast<detail::InMemoryDirectory>(Node);
    if (I == E)
      return Dir;
  }
}

llvm::ErrorOr<Status> InMemoryFileSystem::status(const Twine &Path) {
  auto Node = lookupInMemoryNode(*this, Root.get(), Path);
  if (Node)
    return (*Node)->getStatus();
  return Node.getError();
}

llvm::ErrorOr<std::unique_ptr<File>>
InMemoryFileSystem::openFileForRead(const Twine &Path) {
  auto Node = lookupInMemoryNode(*this, Root.get(), Path);
  if (!Node)
    return Node.getError();

  // When we have a file provide a heap-allocated wrapper for the memory buffer
  // to match the ownership semantics for File.
  if (auto *F = dyn_cast<detail::InMemoryFile>(*Node))
    return std::unique_ptr<File>(new detail::InMemoryFileAdaptor(*F));

  // FIXME: errc::not_a_file?
  return make_error_code(llvm::errc::invalid_argument);
a341 62
/// Adaptor from InMemoryDir::iterator to directory_iterator.
class InMemoryDirIterator : public clang::vfs::detail::DirIterImpl {
  detail::InMemoryDirectory::const_iterator I;
  detail::InMemoryDirectory::const_iterator E;

public:
  InMemoryDirIterator() {}
  explicit InMemoryDirIterator(detail::InMemoryDirectory &Dir)
      : I(Dir.begin()), E(Dir.end()) {
    if (I != E)
      CurrentEntry = I->second->getStatus();
  }

  std::error_code increment() override {
    ++I;
    // When we're at the end, make CurrentEntry invalid and DirIterImpl will do
    // the rest.
    CurrentEntry = I != E ? I->second->getStatus() : Status();
    return std::error_code();
  }
};
} // end anonymous namespace

directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir,
                                                 std::error_code &EC) {
  auto Node = lookupInMemoryNode(*this, Root.get(), Dir);
  if (!Node) {
    EC = Node.getError();
    return directory_iterator(std::make_shared<InMemoryDirIterator>());
  }

  if (auto *DirNode = dyn_cast<detail::InMemoryDirectory>(*Node))
    return directory_iterator(std::make_shared<InMemoryDirIterator>(*DirNode));

  EC = make_error_code(llvm::errc::not_a_directory);
  return directory_iterator(std::make_shared<InMemoryDirIterator>());
}

std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) {
  SmallString<128> Path;
  P.toVector(Path);

  // Fix up relative paths. This just prepends the current working directory.
  std::error_code EC = makeAbsolute(Path);
  assert(!EC);
  (void)EC;

  if (useNormalizedPaths())
    llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);

  if (!Path.empty())
    WorkingDirectory = Path.str();
  return std::error_code();
}
}
}

//===-----------------------------------------------------------------------===/
// RedirectingFileSystem implementation
//===-----------------------------------------------------------------------===/

namespace {
d360 2
a361 2
class RedirectingDirectoryEntry : public Entry {
  std::vector<std::unique_ptr<Entry>> Contents;
d365 2
a366 3
  RedirectingDirectoryEntry(StringRef Name,
                            std::vector<std::unique_ptr<Entry>> Contents,
                            Status S)
d370 1
a370 1
  typedef decltype(Contents)::iterator iterator;
d376 1
a376 1
class RedirectingFileEntry : public Entry {
d387 1
a387 2
  RedirectingFileEntry(StringRef Name, StringRef ExternalContentsPath,
                       NameKind UseName)
d399 1
a399 1
class RedirectingFileSystem;
d403 2
a404 3
  RedirectingFileSystem &FS;
  RedirectingDirectoryEntry::iterator Current, End;

d406 3
a408 4
  VFSFromYamlDirIterImpl(const Twine &Path, RedirectingFileSystem &FS,
                         RedirectingDirectoryEntry::iterator Begin,
                         RedirectingDirectoryEntry::iterator End,
                         std::error_code &EC);
d465 2
a466 3
class RedirectingFileSystem : public vfs::FileSystem {
  /// The root(s) of the virtual file system.
  std::vector<std::unique_ptr<Entry>> Roots;
d483 1
a483 1
  friend class RedirectingFileSystemParser;
d486 1
a486 1
  RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> ExternalFS)
d501 2
d505 4
a508 4
  static RedirectingFileSystem *
  create(std::unique_ptr<MemoryBuffer> Buffer,
         SourceMgr::DiagHandlerTy DiagHandler, void *DiagContext,
         IntrusiveRefCntPtr<FileSystem> ExternalFS);
a512 7
  llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
    return ExternalFS->getCurrentWorkingDirectory();
  }
  std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
    return ExternalFS->setCurrentWorkingDirectory(Path);
  }

d530 1
a530 1
    auto *D = cast<RedirectingDirectoryEntry>(*E);
d537 1
a537 1
class RedirectingFileSystemParser {
d613 1
a613 1
  std::unique_ptr<Entry> parseEntry(yaml::Node *N) {
d628 2
a629 1
    DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
d632 1
a632 1
    std::vector<std::unique_ptr<Entry>> EntryArrayContents;
d635 1
a635 1
    auto UseExternalName = RedirectingFileEntry::NK_NotSet;
d684 2
a685 2
          if (std::unique_ptr<Entry> E = parseEntry(&*I))
            EntryArrayContents.push_back(std::move(E));
d703 1
a703 2
        UseExternalName = Val ? RedirectingFileEntry::NK_External
                              : RedirectingFileEntry::NK_Virtual;
d721 1
a721 2
    if (Kind == EK_Directory &&
        UseExternalName != RedirectingFileEntry::NK_NotSet) {
d735 1
a735 1
    std::unique_ptr<Entry> Result;
d738 2
a739 2
      Result = llvm::make_unique<RedirectingFileEntry>(
          LastComponent, std::move(ExternalContentsPath), UseExternalName);
d742 3
a744 4
      Result = llvm::make_unique<RedirectingDirectoryEntry>(
          LastComponent, std::move(EntryArrayContents),
          Status("", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, 0,
                 file_type::directory_file, sys::fs::all_all));
d756 3
a758 6
      std::vector<std::unique_ptr<Entry>> Entries;
      Entries.push_back(std::move(Result));
      Result = llvm::make_unique<RedirectingDirectoryEntry>(
          *I, std::move(Entries),
          Status("", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, 0,
                 file_type::directory_file, sys::fs::all_all));
d764 1
a764 1
  RedirectingFileSystemParser(yaml::Stream &S) : Stream(S) {}
d767 1
a767 1
  bool parse(yaml::Node *Root, RedirectingFileSystem *FS) {
d781 2
a782 1
    DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
d804 2
a805 2
          if (std::unique_ptr<Entry> E = parseEntry(&*I))
            FS->Roots.push_back(std::move(E));
d848 4
a851 1
Entry::~Entry() = default;
d853 4
a856 3
RedirectingFileSystem *RedirectingFileSystem::create(
    std::unique_ptr<MemoryBuffer> Buffer, SourceMgr::DiagHandlerTy DiagHandler,
    void *DiagContext, IntrusiveRefCntPtr<FileSystem> ExternalFS) {
d869 1
a869 1
  RedirectingFileSystemParser P(Stream);
d871 1
a871 2
  std::unique_ptr<RedirectingFileSystem> FS(
      new RedirectingFileSystem(ExternalFS));
d878 1
a878 1
ErrorOr<Entry *> RedirectingFileSystem::lookupPath(const Twine &Path_) {
d883 1
a883 1
  if (std::error_code EC = makeAbsolute(Path))
d891 3
a893 2
  for (const std::unique_ptr<Entry> &Root : Roots) {
    ErrorOr<Entry *> Result = lookupPath(Start, End, Root.get());
d900 3
a902 3
ErrorOr<Entry *>
RedirectingFileSystem::lookupPath(sys::path::const_iterator Start,
                                  sys::path::const_iterator End, Entry *From) {
d919 1
a919 1
  auto *DE = dyn_cast<RedirectingDirectoryEntry>(From);
d923 4
a926 3
  for (const std::unique_ptr<Entry> &DirEntry :
       llvm::make_range(DE->contents_begin(), DE->contents_end())) {
    ErrorOr<Entry *> Result = lookupPath(Start, End, DirEntry.get());
d933 1
a933 10
static Status getRedirectedFileStatus(const Twine &Path, bool UseExternalNames,
                                      Status ExternalStatus) {
  Status S = ExternalStatus;
  if (!UseExternalNames)
    S = Status::copyWithNewName(S, Path.str());
  S.IsVFSMapped = true;
  return S;
}

ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path, Entry *E) {
d935 2
a936 1
  if (auto *F = dyn_cast<RedirectingFileEntry>(E)) {
d939 2
d942 1
a942 2
      return getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames),
                                     *S);
d945 4
a948 2
    auto *DE = cast<RedirectingDirectoryEntry>(E);
    return Status::copyWithNewName(DE->getStatus(), Path.str());
d952 1
a952 1
ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path) {
d959 1
a959 23
namespace {
/// Provide a file wrapper with an overriden status.
class FileWithFixedStatus : public File {
  std::unique_ptr<File> InnerFile;
  Status S;

public:
  FileWithFixedStatus(std::unique_ptr<File> InnerFile, Status S)
      : InnerFile(std::move(InnerFile)), S(S) {}

  ErrorOr<Status> status() override { return S; }
  ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
            bool IsVolatile) override {
    return InnerFile->getBuffer(Name, FileSize, RequiresNullTerminator,
                                IsVolatile);
  }
  std::error_code close() override { return InnerFile->close(); }
};
} // end anonymous namespace

ErrorOr<std::unique_ptr<File>>
RedirectingFileSystem::openFileForRead(const Twine &Path) {
d964 1
a964 1
  auto *F = dyn_cast<RedirectingFileEntry>(*E);
d972 4
a975 9
  auto ExternalStatus = (*Result)->status();
  if (!ExternalStatus)
    return ExternalStatus.getError();

  // FIXME: Update the status with the name and VFSMapped.
  Status S = getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames),
                                     *ExternalStatus);
  return std::unique_ptr<File>(
      llvm::make_unique<FileWithFixedStatus>(std::move(*Result), S));
d982 2
a983 2
  return RedirectingFileSystem::create(std::move(Buffer), DiagHandler,
                                       DiagContext, ExternalFS);
d1128 5
a1132 4
VFSFromYamlDirIterImpl::VFSFromYamlDirIterImpl(
    const Twine &_Path, RedirectingFileSystem &FS,
    RedirectingDirectoryEntry::iterator Begin,
    RedirectingDirectoryEntry::iterator End, std::error_code &EC)
d1137 1
a1137 1
    llvm::ErrorOr<vfs::Status> S = FS.status(PathStr);
d1150 1
a1150 1
    llvm::ErrorOr<vfs::Status> S = FS.status(PathStr);
@


1.1.1.5.2.1
log
@Sync with HEAD
@
text
@a18 2
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Debug.h"
a21 1
#include "llvm/Support/Process.h"
d23 1
a25 1
#include <utility>
d47 1
a47 1
Status::Status(StringRef Name, UniqueID UID, sys::TimePoint<> MTime,
a101 3
  if (llvm::sys::path::is_absolute(Path))
    return std::error_code();

a113 14
#ifndef NDEBUG
static bool isTraversalComponent(StringRef Component) {
  return Component.equals("..") || Component.equals(".");
}

static bool pathHasTraversal(StringRef Path) {
  using namespace llvm::sys;
  for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path)))
    if (isTraversalComponent(Comp))
      return true;
  return false;
}
#endif

a122 1
  std::string RealName;
d124 1
a124 1
  RealFile(int FD, StringRef NewName, StringRef NewRealPathName)
d126 1
a126 2
                  llvm::sys::fs::file_type::status_error, {}),
        RealName(NewRealPathName.str()) {
a132 1
  ErrorOr<std::string> getName() override;
a152 4
ErrorOr<std::string> RealFile::getName() {
  return RealName.empty() ? S.getName().str() : RealName;
}

d161 10
d172 2
a173 1
  std::error_code EC = sys::Process::SafelyCloseFileDescriptor(FD);
d175 1
a175 1
  return EC;
d201 1
a201 2
  SmallString<256> RealName;
  if (std::error_code EC = sys::fs::openFileForRead(Name, FD, &RealName))
d203 1
a203 1
  return std::unique_ptr<File>(new RealFile(FD, Name.str(), RealName.str()));
d274 1
a274 1
  FSList.push_back(std::move(BaseFS));
d480 2
a481 2
          Status("", getNextVirtualUniqueID(), llvm::sys::TimePoint<>(), 0, 0,
                 0, llvm::sys::fs::file_type::directory_file,
d518 1
a518 1
                    llvm::sys::toTimePoint(ModificationTime), 0, 0,
d531 3
a533 3
          getNextVirtualUniqueID(), llvm::sys::toTimePoint(ModificationTime), 0,
          0, Buffer->getBufferSize(), llvm::sys::fs::file_type::directory_file,
          llvm::sys::fs::all_all);
a713 2
  RedirectingDirectoryEntry(StringRef Name, Status S)
      : Entry(EK_Directory, Name), S(std::move(S)) {}
a714 4
  void addContent(std::unique_ptr<Entry> Content) {
    Contents.push_back(std::move(Content));
  }
  Entry *getLastContent() const { return Contents.back().get(); }
a741 1
  NameKind getUseName() const { return UseName; }
a778 2
///   'overlay-relative': <boolean, default=false>
///   'ignore-non-existent-contents': <boolean, default=true>
a817 4
  /// If IsRelativeOverlay is set, this represents the directory
  /// path that should be prefixed to each 'external-contents' entry
  /// when reading from YAML files.
  std::string ExternalContentsPrefixDir;
d825 1
a825 5
  bool CaseSensitive = true;

  /// IsRelativeOverlay marks whether a IsExternalContentsPrefixDir path must
  /// be prefixed in every 'external-contents' when reading from YAML files.
  bool IsRelativeOverlay = false;
d829 1
a829 9
  bool UseExternalNames = true;

  /// \brief Whether an invalid path obtained via 'external-contents' should
  /// cause iteration on the VFS to stop. If 'true', the VFS should ignore
  /// the entry and continue with the next. Allows YAML files to be shared
  /// across multiple compiler invocations regardless of prior existent
  /// paths in 'external-contents'. This global value is overridable on a
  /// per-file basis.
  bool IgnoreNonExistentContents = true;
a831 10
  /// Virtual file paths and external files could be canonicalized without "..",
  /// "." and "./" in their paths. FIXME: some unittests currently fail on
  /// win32 when using remove_dots and remove_leading_dotslash on paths.
  bool UseCanonicalizedPaths =
#ifdef LLVM_ON_WIN32
      false;
#else
      true;
#endif

d836 4
a839 1
      : ExternalFS(std::move(ExternalFS)) {}
a849 3
  /// \brief Looks up \p Path in \c Roots.
  ErrorOr<Entry *> lookupPath(const Twine &Path);

d854 2
a855 2
         SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
         void *DiagContext, IntrusiveRefCntPtr<FileSystem> ExternalFS);
a887 36

  void setExternalContentsPrefixDir(StringRef PrefixDir) {
    ExternalContentsPrefixDir = PrefixDir.str();
  }

  StringRef getExternalContentsPrefixDir() const {
    return ExternalContentsPrefixDir;
  }

  bool ignoreNonExistentContents() const {
    return IgnoreNonExistentContents;
  }

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void dump() const {
    for (const std::unique_ptr<Entry> &Root : Roots)
      dumpEntry(Root.get());
  }

LLVM_DUMP_METHOD void dumpEntry(Entry *E, int NumSpaces = 0) const {
    StringRef Name = E->getName();
    for (int i = 0, e = NumSpaces; i < e; ++i)
      dbgs() << " ";
    dbgs() << "'" << Name.str().c_str() << "'" << "\n";

    if (E->getKind() == EK_Directory) {
      auto *DE = dyn_cast<RedirectingDirectoryEntry>(E);
      assert(DE && "Should be a directory");

      for (std::unique_ptr<Entry> &SubEntry :
           llvm::make_range(DE->contents_begin(), DE->contents_end()))
        dumpEntry(SubEntry.get(), NumSpaces+2);
    }
  }
#endif

d967 1
a967 66
  Entry *lookupOrCreateEntry(RedirectingFileSystem *FS, StringRef Name,
                             Entry *ParentEntry = nullptr) {
    if (!ParentEntry) { // Look for a existent root
      for (const std::unique_ptr<Entry> &Root : FS->Roots) {
        if (Name.equals(Root->getName())) {
          ParentEntry = Root.get();
          return ParentEntry;
        }
      }
    } else { // Advance to the next component
      auto *DE = dyn_cast<RedirectingDirectoryEntry>(ParentEntry);
      for (std::unique_ptr<Entry> &Content :
           llvm::make_range(DE->contents_begin(), DE->contents_end())) {
        auto *DirContent = dyn_cast<RedirectingDirectoryEntry>(Content.get());
        if (DirContent && Name.equals(Content->getName()))
          return DirContent;
      }
    }

    // ... or create a new one
    std::unique_ptr<Entry> E = llvm::make_unique<RedirectingDirectoryEntry>(
        Name,
        Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
               0, 0, 0, file_type::directory_file, sys::fs::all_all));

    if (!ParentEntry) { // Add a new root to the overlay
      FS->Roots.push_back(std::move(E));
      ParentEntry = FS->Roots.back().get();
      return ParentEntry;
    }

    auto *DE = dyn_cast<RedirectingDirectoryEntry>(ParentEntry);
    DE->addContent(std::move(E));
    return DE->getLastContent();
  }

  void uniqueOverlayTree(RedirectingFileSystem *FS, Entry *SrcE,
                         Entry *NewParentE = nullptr) {
    StringRef Name = SrcE->getName();
    switch (SrcE->getKind()) {
    case EK_Directory: {
      auto *DE = dyn_cast<RedirectingDirectoryEntry>(SrcE);
      assert(DE && "Must be a directory");
      // Empty directories could be present in the YAML as a way to
      // describe a file for a current directory after some of its subdir
      // is parsed. This only leads to redundant walks, ignore it.
      if (!Name.empty())
        NewParentE = lookupOrCreateEntry(FS, Name, NewParentE);
      for (std::unique_ptr<Entry> &SubEntry :
           llvm::make_range(DE->contents_begin(), DE->contents_end()))
        uniqueOverlayTree(FS, SubEntry.get(), NewParentE);
      break;
    }
    case EK_File: {
      auto *FE = dyn_cast<RedirectingFileEntry>(SrcE);
      assert(FE && "Must be a file");
      assert(NewParentE && "Parent entry must exist");
      auto *DE = dyn_cast<RedirectingDirectoryEntry>(NewParentE);
      DE->addContent(llvm::make_unique<RedirectingFileEntry>(
          Name, FE->getExternalContentsPath(), FE->getUseName()));
      break;
    }
    }
  }

  std::unique_ptr<Entry> parseEntry(yaml::Node *N, RedirectingFileSystem *FS) {
d1007 1
a1007 11

        if (FS->UseCanonicalizedPaths) {
          SmallString<256> Path(Value);
          // Guarantee that old YAML files containing paths with ".." and "."
          // are properly canonicalized before read into the VFS.
          Path = sys::path::remove_leading_dotslash(Path);
          sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
          Name = Path.str();
        } else {
          Name = Value;
        }
d1037 1
a1037 1
          if (std::unique_ptr<Entry> E = parseEntry(&*I, FS))
d1051 1
a1051 18

        SmallString<256> FullPath;
        if (FS->IsRelativeOverlay) {
          FullPath = FS->getExternalContentsPrefixDir();
          assert(!FullPath.empty() &&
                 "External contents prefix directory must exist");
          llvm::sys::path::append(FullPath, Value);
        } else {
          FullPath = Value;
        }

        if (FS->UseCanonicalizedPaths) {
          // Guarantee that old YAML files containing paths with ".." and "."
          // are properly canonicalized before read into the VFS.
          FullPath = sys::path::remove_leading_dotslash(FullPath);
          sys::path::remove_dots(FullPath, /*remove_dot_dot=*/true);
        }
        ExternalContentsPath = FullPath.str();
d1099 2
a1100 2
          Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
                 0, 0, 0, file_type::directory_file, sys::fs::all_all));
d1116 2
a1117 2
          Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
                 0, 0, 0, file_type::directory_file, sys::fs::all_all));
a1136 2
      KeyStatusPair("overlay-relative", false),
      KeyStatusPair("ignore-non-existent-contents", false),
a1140 1
    std::vector<std::unique_ptr<Entry>> RootEntries;
d1162 2
a1163 2
          if (std::unique_ptr<Entry> E = parseEntry(&*I, FS))
            RootEntries.push_back(std::move(E));
a1187 3
      } else if (Key == "overlay-relative") {
        if (!parseScalarBool(I->getValue(), FS->IsRelativeOverlay))
          return false;
a1190 3
      } else if (Key == "ignore-non-existent-contents") {
        if (!parseScalarBool(I->getValue(), FS->IgnoreNonExistentContents))
          return false;
a1200 7

    // Now that we sucessefully parsed the YAML file, canonicalize the internal
    // representation to a proper directory tree so that we can search faster
    // inside the VFS.
    for (std::unique_ptr<Entry> &E : RootEntries)
      uniqueOverlayTree(FS, E.get());

d1208 3
a1210 5
RedirectingFileSystem *
RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer,
                              SourceMgr::DiagHandlerTy DiagHandler,
                              StringRef YAMLFilePath, void *DiagContext,
                              IntrusiveRefCntPtr<FileSystem> ExternalFS) {
d1226 1
a1226 18
      new RedirectingFileSystem(std::move(ExternalFS)));

  if (!YAMLFilePath.empty()) {
    // Use the YAML path from -ivfsoverlay to compute the dir to be prefixed
    // to each 'external-contents' path.
    //
    // Example:
    //    -ivfsoverlay dummy.cache/vfs/vfs.yaml
    // yields:
    //  FS->ExternalContentsPrefixDir => /<absolute_path_to>/dummy.cache/vfs
    //
    SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath);
    std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir);
    assert(!EC && "Overlay dir final path must be absolute");
    (void)EC;
    FS->setExternalContentsPrefixDir(OverlayAbsDir);
  }

a1240 8
  // Canonicalize path by removing ".", "..", "./", etc components. This is
  // a VFS request, do bot bother about symlinks in the path components
  // but canonicalize in order to perform the correct entry search.
  if (UseCanonicalizedPaths) {
    Path = sys::path::remove_leading_dotslash(Path);
    sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
  }

a1256 7
#ifndef LLVM_ON_WIN32
  assert(!isTraversalComponent(*Start) &&
         !isTraversalComponent(From->getName()) &&
         "Paths should not contain traversal components");
#else
  // FIXME: this is here to support windows, remove it once canonicalized
  // paths become globally default.
a1258 3
#endif

  StringRef FromName = From->getName();
d1260 11
a1270 13
  // Forward the search to the next component in case this is an empty one.
  if (!FromName.empty()) {
    if (CaseSensitive ? !Start->equals(FromName)
                      : !Start->equals_lower(FromName))
      // failure to match
      return make_error_code(llvm::errc::no_such_file_or_directory);

    ++Start;

    if (Start == End) {
      // Match!
      return From;
    }
d1325 1
a1325 1
      : InnerFile(std::move(InnerFile)), S(std::move(S)) {}
d1365 1
a1365 3
                    SourceMgr::DiagHandlerTy DiagHandler,
                    StringRef YAMLFilePath,
                    void *DiagContext,
d1368 1
a1368 43
                                       YAMLFilePath, DiagContext,
                                       std::move(ExternalFS));
}

static void getVFSEntries(Entry *SrcE, SmallVectorImpl<StringRef> &Path,
                          SmallVectorImpl<YAMLVFSEntry> &Entries) {
  auto Kind = SrcE->getKind();
  if (Kind == EK_Directory) {
    auto *DE = dyn_cast<RedirectingDirectoryEntry>(SrcE);
    assert(DE && "Must be a directory");
    for (std::unique_ptr<Entry> &SubEntry :
         llvm::make_range(DE->contents_begin(), DE->contents_end())) {
      Path.push_back(SubEntry->getName());
      getVFSEntries(SubEntry.get(), Path, Entries);
      Path.pop_back();
    }
    return;
  }

  assert(Kind == EK_File && "Must be a EK_File");
  auto *FE = dyn_cast<RedirectingFileEntry>(SrcE);
  assert(FE && "Must be a file");
  SmallString<128> VPath;
  for (auto &Comp : Path)
    llvm::sys::path::append(VPath, Comp);
  Entries.push_back(YAMLVFSEntry(VPath.c_str(), FE->getExternalContentsPath()));
}

void vfs::collectVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
                             SourceMgr::DiagHandlerTy DiagHandler,
                             StringRef YAMLFilePath,
                             SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
                             void *DiagContext,
                             IntrusiveRefCntPtr<FileSystem> ExternalFS) {
  RedirectingFileSystem *VFS = RedirectingFileSystem::create(
      std::move(Buffer), DiagHandler, YAMLFilePath, DiagContext,
      std::move(ExternalFS));
  ErrorOr<Entry *> RootE = VFS->lookupPath("/");
  if (!RootE)
    return;
  SmallVector<StringRef, 8> Components;
  Components.push_back("/");
  getVFSEntries(*RootE, Components, CollectedEntries);
d1379 10
d1410 1
a1410 3
  void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> UseExternalNames,
             Optional<bool> IsCaseSensitive, Optional<bool> IsOverlayRelative,
             Optional<bool> IgnoreNonExistentContents, StringRef OverlayDir);
d1463 1
a1463 5
                       Optional<bool> UseExternalNames,
                       Optional<bool> IsCaseSensitive,
                       Optional<bool> IsOverlayRelative,
                       Optional<bool> IgnoreNonExistentContents,
                       StringRef OverlayDir) {
a1470 12
  if (UseExternalNames.hasValue())
    OS << "  'use-external-names': '"
       << (UseExternalNames.getValue() ? "true" : "false") << "',\n";
  bool UseOverlayRelative = false;
  if (IsOverlayRelative.hasValue()) {
    UseOverlayRelative = IsOverlayRelative.getValue();
    OS << "  'overlay-relative': '"
       << (UseOverlayRelative ? "true" : "false") << "',\n";
  }
  if (IgnoreNonExistentContents.hasValue())
    OS << "  'ignore-non-existent-contents': '"
       << (IgnoreNonExistentContents.getValue() ? "true" : "false") << "',\n";
d1476 1
a1476 10

    StringRef RPath = Entry.RPath;
    if (UseOverlayRelative) {
      unsigned OverlayDirLen = OverlayDir.size();
      assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
             "Overlay dir must be contained in RPath");
      RPath = RPath.slice(OverlayDirLen, RPath.size());
    }

    writeEntry(path::filename(Entry.VPath), RPath);
d1490 1
a1490 8
      StringRef RPath = Entry.RPath;
      if (UseOverlayRelative) {
        unsigned OverlayDirLen = OverlayDir.size();
        assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
               "Overlay dir must be contained in RPath");
        RPath = RPath.slice(OverlayDirLen, RPath.size());
      }
      writeEntry(path::filename(Entry.VPath), RPath);
d1510 1
a1510 3
  JSONWriter(OS).write(Mappings, UseExternalNames, IsCaseSensitive,
                       IsOverlayRelative, IgnoreNonExistentContents,
                       OverlayDir);
d1518 1
a1518 1
  while (Current != End) {
d1522 1
a1522 1
    if (S) {
d1524 1
a1524 8
      return;
    }
    // Skip entries which do not map to a reliable external content.
    if (FS.ignoreNonExistentContents() &&
        S.getError() == llvm::errc::no_such_file_or_directory) {
      ++Current;
      continue;
    } else {
a1525 2
      break;
    }
d1531 1
a1531 1
  while (++Current != End) {
d1535 2
a1536 9
    if (!S) {
      // Skip entries which do not map to a reliable external content.
      if (FS.ignoreNonExistentContents() &&
          S.getError() == llvm::errc::no_such_file_or_directory) {
        continue;
      } else {
        return S.getError();
      }
    }
d1538 2
a1539 1
    break;
a1540 3

  if (Current == End)
    CurrentEntry = Status();
@


1.1.1.6
log
@Import Clang pre-4.0.0 r291444.
@
text
@a18 2
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Debug.h"
a21 1
#include "llvm/Support/Process.h"
d23 1
a25 1
#include <utility>
d47 1
a47 1
Status::Status(StringRef Name, UniqueID UID, sys::TimePoint<> MTime,
a101 3
  if (llvm::sys::path::is_absolute(Path))
    return std::error_code();

a113 14
#ifndef NDEBUG
static bool isTraversalComponent(StringRef Component) {
  return Component.equals("..") || Component.equals(".");
}

static bool pathHasTraversal(StringRef Path) {
  using namespace llvm::sys;
  for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path)))
    if (isTraversalComponent(Comp))
      return true;
  return false;
}
#endif

a122 1
  std::string RealName;
d124 1
a124 1
  RealFile(int FD, StringRef NewName, StringRef NewRealPathName)
d126 1
a126 2
                  llvm::sys::fs::file_type::status_error, {}),
        RealName(NewRealPathName.str()) {
a132 1
  ErrorOr<std::string> getName() override;
a152 4
ErrorOr<std::string> RealFile::getName() {
  return RealName.empty() ? S.getName().str() : RealName;
}

d161 10
d172 2
a173 1
  std::error_code EC = sys::Process::SafelyCloseFileDescriptor(FD);
d175 1
a175 1
  return EC;
d201 1
a201 2
  SmallString<256> RealName;
  if (std::error_code EC = sys::fs::openFileForRead(Name, FD, &RealName))
d203 1
a203 1
  return std::unique_ptr<File>(new RealFile(FD, Name.str(), RealName.str()));
d274 1
a274 1
  FSList.push_back(std::move(BaseFS));
d480 2
a481 2
          Status("", getNextVirtualUniqueID(), llvm::sys::TimePoint<>(), 0, 0,
                 0, llvm::sys::fs::file_type::directory_file,
d518 1
a518 1
                    llvm::sys::toTimePoint(ModificationTime), 0, 0,
d531 3
a533 3
          getNextVirtualUniqueID(), llvm::sys::toTimePoint(ModificationTime), 0,
          0, Buffer->getBufferSize(), llvm::sys::fs::file_type::directory_file,
          llvm::sys::fs::all_all);
a713 2
  RedirectingDirectoryEntry(StringRef Name, Status S)
      : Entry(EK_Directory, Name), S(std::move(S)) {}
a714 4
  void addContent(std::unique_ptr<Entry> Content) {
    Contents.push_back(std::move(Content));
  }
  Entry *getLastContent() const { return Contents.back().get(); }
a741 1
  NameKind getUseName() const { return UseName; }
a778 2
///   'overlay-relative': <boolean, default=false>
///   'ignore-non-existent-contents': <boolean, default=true>
a817 4
  /// If IsRelativeOverlay is set, this represents the directory
  /// path that should be prefixed to each 'external-contents' entry
  /// when reading from YAML files.
  std::string ExternalContentsPrefixDir;
d825 1
a825 5
  bool CaseSensitive = true;

  /// IsRelativeOverlay marks whether a IsExternalContentsPrefixDir path must
  /// be prefixed in every 'external-contents' when reading from YAML files.
  bool IsRelativeOverlay = false;
d829 1
a829 9
  bool UseExternalNames = true;

  /// \brief Whether an invalid path obtained via 'external-contents' should
  /// cause iteration on the VFS to stop. If 'true', the VFS should ignore
  /// the entry and continue with the next. Allows YAML files to be shared
  /// across multiple compiler invocations regardless of prior existent
  /// paths in 'external-contents'. This global value is overridable on a
  /// per-file basis.
  bool IgnoreNonExistentContents = true;
a831 10
  /// Virtual file paths and external files could be canonicalized without "..",
  /// "." and "./" in their paths. FIXME: some unittests currently fail on
  /// win32 when using remove_dots and remove_leading_dotslash on paths.
  bool UseCanonicalizedPaths =
#ifdef LLVM_ON_WIN32
      false;
#else
      true;
#endif

d836 4
a839 1
      : ExternalFS(std::move(ExternalFS)) {}
a849 3
  /// \brief Looks up \p Path in \c Roots.
  ErrorOr<Entry *> lookupPath(const Twine &Path);

d854 2
a855 2
         SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
         void *DiagContext, IntrusiveRefCntPtr<FileSystem> ExternalFS);
a887 36

  void setExternalContentsPrefixDir(StringRef PrefixDir) {
    ExternalContentsPrefixDir = PrefixDir.str();
  }

  StringRef getExternalContentsPrefixDir() const {
    return ExternalContentsPrefixDir;
  }

  bool ignoreNonExistentContents() const {
    return IgnoreNonExistentContents;
  }

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void dump() const {
    for (const std::unique_ptr<Entry> &Root : Roots)
      dumpEntry(Root.get());
  }

LLVM_DUMP_METHOD void dumpEntry(Entry *E, int NumSpaces = 0) const {
    StringRef Name = E->getName();
    for (int i = 0, e = NumSpaces; i < e; ++i)
      dbgs() << " ";
    dbgs() << "'" << Name.str().c_str() << "'" << "\n";

    if (E->getKind() == EK_Directory) {
      auto *DE = dyn_cast<RedirectingDirectoryEntry>(E);
      assert(DE && "Should be a directory");

      for (std::unique_ptr<Entry> &SubEntry :
           llvm::make_range(DE->contents_begin(), DE->contents_end()))
        dumpEntry(SubEntry.get(), NumSpaces+2);
    }
  }
#endif

d967 1
a967 66
  Entry *lookupOrCreateEntry(RedirectingFileSystem *FS, StringRef Name,
                             Entry *ParentEntry = nullptr) {
    if (!ParentEntry) { // Look for a existent root
      for (const std::unique_ptr<Entry> &Root : FS->Roots) {
        if (Name.equals(Root->getName())) {
          ParentEntry = Root.get();
          return ParentEntry;
        }
      }
    } else { // Advance to the next component
      auto *DE = dyn_cast<RedirectingDirectoryEntry>(ParentEntry);
      for (std::unique_ptr<Entry> &Content :
           llvm::make_range(DE->contents_begin(), DE->contents_end())) {
        auto *DirContent = dyn_cast<RedirectingDirectoryEntry>(Content.get());
        if (DirContent && Name.equals(Content->getName()))
          return DirContent;
      }
    }

    // ... or create a new one
    std::unique_ptr<Entry> E = llvm::make_unique<RedirectingDirectoryEntry>(
        Name,
        Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
               0, 0, 0, file_type::directory_file, sys::fs::all_all));

    if (!ParentEntry) { // Add a new root to the overlay
      FS->Roots.push_back(std::move(E));
      ParentEntry = FS->Roots.back().get();
      return ParentEntry;
    }

    auto *DE = dyn_cast<RedirectingDirectoryEntry>(ParentEntry);
    DE->addContent(std::move(E));
    return DE->getLastContent();
  }

  void uniqueOverlayTree(RedirectingFileSystem *FS, Entry *SrcE,
                         Entry *NewParentE = nullptr) {
    StringRef Name = SrcE->getName();
    switch (SrcE->getKind()) {
    case EK_Directory: {
      auto *DE = dyn_cast<RedirectingDirectoryEntry>(SrcE);
      assert(DE && "Must be a directory");
      // Empty directories could be present in the YAML as a way to
      // describe a file for a current directory after some of its subdir
      // is parsed. This only leads to redundant walks, ignore it.
      if (!Name.empty())
        NewParentE = lookupOrCreateEntry(FS, Name, NewParentE);
      for (std::unique_ptr<Entry> &SubEntry :
           llvm::make_range(DE->contents_begin(), DE->contents_end()))
        uniqueOverlayTree(FS, SubEntry.get(), NewParentE);
      break;
    }
    case EK_File: {
      auto *FE = dyn_cast<RedirectingFileEntry>(SrcE);
      assert(FE && "Must be a file");
      assert(NewParentE && "Parent entry must exist");
      auto *DE = dyn_cast<RedirectingDirectoryEntry>(NewParentE);
      DE->addContent(llvm::make_unique<RedirectingFileEntry>(
          Name, FE->getExternalContentsPath(), FE->getUseName()));
      break;
    }
    }
  }

  std::unique_ptr<Entry> parseEntry(yaml::Node *N, RedirectingFileSystem *FS) {
d1007 1
a1007 11

        if (FS->UseCanonicalizedPaths) {
          SmallString<256> Path(Value);
          // Guarantee that old YAML files containing paths with ".." and "."
          // are properly canonicalized before read into the VFS.
          Path = sys::path::remove_leading_dotslash(Path);
          sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
          Name = Path.str();
        } else {
          Name = Value;
        }
d1037 1
a1037 1
          if (std::unique_ptr<Entry> E = parseEntry(&*I, FS))
d1051 1
a1051 18

        SmallString<256> FullPath;
        if (FS->IsRelativeOverlay) {
          FullPath = FS->getExternalContentsPrefixDir();
          assert(!FullPath.empty() &&
                 "External contents prefix directory must exist");
          llvm::sys::path::append(FullPath, Value);
        } else {
          FullPath = Value;
        }

        if (FS->UseCanonicalizedPaths) {
          // Guarantee that old YAML files containing paths with ".." and "."
          // are properly canonicalized before read into the VFS.
          FullPath = sys::path::remove_leading_dotslash(FullPath);
          sys::path::remove_dots(FullPath, /*remove_dot_dot=*/true);
        }
        ExternalContentsPath = FullPath.str();
d1099 2
a1100 2
          Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
                 0, 0, 0, file_type::directory_file, sys::fs::all_all));
d1116 2
a1117 2
          Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
                 0, 0, 0, file_type::directory_file, sys::fs::all_all));
a1136 2
      KeyStatusPair("overlay-relative", false),
      KeyStatusPair("ignore-non-existent-contents", false),
a1140 1
    std::vector<std::unique_ptr<Entry>> RootEntries;
d1162 2
a1163 2
          if (std::unique_ptr<Entry> E = parseEntry(&*I, FS))
            RootEntries.push_back(std::move(E));
a1187 3
      } else if (Key == "overlay-relative") {
        if (!parseScalarBool(I->getValue(), FS->IsRelativeOverlay))
          return false;
a1190 3
      } else if (Key == "ignore-non-existent-contents") {
        if (!parseScalarBool(I->getValue(), FS->IgnoreNonExistentContents))
          return false;
a1200 7

    // Now that we sucessefully parsed the YAML file, canonicalize the internal
    // representation to a proper directory tree so that we can search faster
    // inside the VFS.
    for (std::unique_ptr<Entry> &E : RootEntries)
      uniqueOverlayTree(FS, E.get());

d1208 3
a1210 5
RedirectingFileSystem *
RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer,
                              SourceMgr::DiagHandlerTy DiagHandler,
                              StringRef YAMLFilePath, void *DiagContext,
                              IntrusiveRefCntPtr<FileSystem> ExternalFS) {
d1226 1
a1226 18
      new RedirectingFileSystem(std::move(ExternalFS)));

  if (!YAMLFilePath.empty()) {
    // Use the YAML path from -ivfsoverlay to compute the dir to be prefixed
    // to each 'external-contents' path.
    //
    // Example:
    //    -ivfsoverlay dummy.cache/vfs/vfs.yaml
    // yields:
    //  FS->ExternalContentsPrefixDir => /<absolute_path_to>/dummy.cache/vfs
    //
    SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath);
    std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir);
    assert(!EC && "Overlay dir final path must be absolute");
    (void)EC;
    FS->setExternalContentsPrefixDir(OverlayAbsDir);
  }

a1240 8
  // Canonicalize path by removing ".", "..", "./", etc components. This is
  // a VFS request, do bot bother about symlinks in the path components
  // but canonicalize in order to perform the correct entry search.
  if (UseCanonicalizedPaths) {
    Path = sys::path::remove_leading_dotslash(Path);
    sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
  }

a1256 7
#ifndef LLVM_ON_WIN32
  assert(!isTraversalComponent(*Start) &&
         !isTraversalComponent(From->getName()) &&
         "Paths should not contain traversal components");
#else
  // FIXME: this is here to support windows, remove it once canonicalized
  // paths become globally default.
a1258 3
#endif

  StringRef FromName = From->getName();
d1260 11
a1270 13
  // Forward the search to the next component in case this is an empty one.
  if (!FromName.empty()) {
    if (CaseSensitive ? !Start->equals(FromName)
                      : !Start->equals_lower(FromName))
      // failure to match
      return make_error_code(llvm::errc::no_such_file_or_directory);

    ++Start;

    if (Start == End) {
      // Match!
      return From;
    }
d1325 1
a1325 1
      : InnerFile(std::move(InnerFile)), S(std::move(S)) {}
d1365 1
a1365 3
                    SourceMgr::DiagHandlerTy DiagHandler,
                    StringRef YAMLFilePath,
                    void *DiagContext,
d1368 1
a1368 43
                                       YAMLFilePath, DiagContext,
                                       std::move(ExternalFS));
}

static void getVFSEntries(Entry *SrcE, SmallVectorImpl<StringRef> &Path,
                          SmallVectorImpl<YAMLVFSEntry> &Entries) {
  auto Kind = SrcE->getKind();
  if (Kind == EK_Directory) {
    auto *DE = dyn_cast<RedirectingDirectoryEntry>(SrcE);
    assert(DE && "Must be a directory");
    for (std::unique_ptr<Entry> &SubEntry :
         llvm::make_range(DE->contents_begin(), DE->contents_end())) {
      Path.push_back(SubEntry->getName());
      getVFSEntries(SubEntry.get(), Path, Entries);
      Path.pop_back();
    }
    return;
  }

  assert(Kind == EK_File && "Must be a EK_File");
  auto *FE = dyn_cast<RedirectingFileEntry>(SrcE);
  assert(FE && "Must be a file");
  SmallString<128> VPath;
  for (auto &Comp : Path)
    llvm::sys::path::append(VPath, Comp);
  Entries.push_back(YAMLVFSEntry(VPath.c_str(), FE->getExternalContentsPath()));
}

void vfs::collectVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
                             SourceMgr::DiagHandlerTy DiagHandler,
                             StringRef YAMLFilePath,
                             SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
                             void *DiagContext,
                             IntrusiveRefCntPtr<FileSystem> ExternalFS) {
  RedirectingFileSystem *VFS = RedirectingFileSystem::create(
      std::move(Buffer), DiagHandler, YAMLFilePath, DiagContext,
      std::move(ExternalFS));
  ErrorOr<Entry *> RootE = VFS->lookupPath("/");
  if (!RootE)
    return;
  SmallVector<StringRef, 8> Components;
  Components.push_back("/");
  getVFSEntries(*RootE, Components, CollectedEntries);
d1379 10
d1410 1
a1410 3
  void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> UseExternalNames,
             Optional<bool> IsCaseSensitive, Optional<bool> IsOverlayRelative,
             Optional<bool> IgnoreNonExistentContents, StringRef OverlayDir);
d1463 1
a1463 5
                       Optional<bool> UseExternalNames,
                       Optional<bool> IsCaseSensitive,
                       Optional<bool> IsOverlayRelative,
                       Optional<bool> IgnoreNonExistentContents,
                       StringRef OverlayDir) {
a1470 12
  if (UseExternalNames.hasValue())
    OS << "  'use-external-names': '"
       << (UseExternalNames.getValue() ? "true" : "false") << "',\n";
  bool UseOverlayRelative = false;
  if (IsOverlayRelative.hasValue()) {
    UseOverlayRelative = IsOverlayRelative.getValue();
    OS << "  'overlay-relative': '"
       << (UseOverlayRelative ? "true" : "false") << "',\n";
  }
  if (IgnoreNonExistentContents.hasValue())
    OS << "  'ignore-non-existent-contents': '"
       << (IgnoreNonExistentContents.getValue() ? "true" : "false") << "',\n";
d1476 1
a1476 10

    StringRef RPath = Entry.RPath;
    if (UseOverlayRelative) {
      unsigned OverlayDirLen = OverlayDir.size();
      assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
             "Overlay dir must be contained in RPath");
      RPath = RPath.slice(OverlayDirLen, RPath.size());
    }

    writeEntry(path::filename(Entry.VPath), RPath);
d1490 1
a1490 8
      StringRef RPath = Entry.RPath;
      if (UseOverlayRelative) {
        unsigned OverlayDirLen = OverlayDir.size();
        assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
               "Overlay dir must be contained in RPath");
        RPath = RPath.slice(OverlayDirLen, RPath.size());
      }
      writeEntry(path::filename(Entry.VPath), RPath);
d1510 1
a1510 3
  JSONWriter(OS).write(Mappings, UseExternalNames, IsCaseSensitive,
                       IsOverlayRelative, IgnoreNonExistentContents,
                       OverlayDir);
d1518 1
a1518 1
  while (Current != End) {
d1522 1
a1522 1
    if (S) {
d1524 1
a1524 8
      return;
    }
    // Skip entries which do not map to a reliable external content.
    if (FS.ignoreNonExistentContents() &&
        S.getError() == llvm::errc::no_such_file_or_directory) {
      ++Current;
      continue;
    } else {
a1525 2
      break;
    }
d1531 1
a1531 1
  while (++Current != End) {
d1535 2
a1536 9
    if (!S) {
      // Skip entries which do not map to a reliable external content.
      if (FS.ignoreNonExistentContents() &&
          S.getError() == llvm::errc::no_such_file_or_directory) {
        continue;
      } else {
        return S.getError();
      }
    }
d1538 2
a1539 1
    break;
a1540 3

  if (Current == End)
    CurrentEntry = Status();
@


1.1.1.7
log
@Import clang r309604 from branches/release_50
@
text
@d30 7
d238 5
a242 1
  return llvm::sys::fs::set_current_path(Path);
d252 1
d255 2
a256 1
  RealFSDirIter(const Twine &Path, std::error_code &EC) : Iter(Path, EC) {
d260 2
a261 1
      CurrentEntry = Status::copyWithNewName(S, Iter->path());
d1872 1
a1872 1
  if (I != directory_iterator()) {
d1885 2
@


1.1.1.7.4.1
log
@Sync with HEAD
@
text
@d1 1
a1 1
//===- VirtualFileSystem.cpp - Virtual File System Layer ------------------===//
a8 1
//
a9 1
//
d13 1
a13 2
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
a14 2
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/Optional.h"
d16 1
a16 3
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
a17 1
#include "llvm/ADT/Twine.h"
a19 3
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Chrono.h"
a21 3
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
a24 2
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/SourceMgr.h"
a25 2
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
a26 5
#include <cassert>
#include <cstdint>
#include <iterator>
#include <limits>
#include <map>
a27 2
#include <string>
#include <system_error>
a28 1
#include <vector>
d31 1
a31 1
using namespace vfs;
a32 1

d41 1
a41 1
      Type(Status.type()), Perms(Status.permissions())  {}
d47 1
a47 1
      Type(Type), Perms(Perms) {}
a61 1
  assert(isStatusKnown() && Other.isStatusKnown());
a63 1

a66 1

a69 1

a72 1

a75 1

a78 1

d83 1
a83 1
File::~File() = default;
d85 1
a85 1
FileSystem::~FileSystem() = default;
d99 1
a99 1
    return {};
a107 5
std::error_code FileSystem::getRealPath(const Twine &Path,
                                        SmallVectorImpl<char> &Output) const {
  return errc::operation_not_permitted;
}

a119 1

d132 1
a132 2

/// Wrapper around a raw file descriptor.
a133 2
  friend class RealFileSystem;

d137 1
a137 1

a146 1

d155 1
a155 3

} // namespace

d188 1
a188 2

/// The file system according to your operating system.
a196 2
  std::error_code getRealPath(const Twine &Path,
                              SmallVectorImpl<char> &Output) const override;
d198 1
a198 2

} // namespace
d211 1
a211 2
  if (std::error_code EC =
          sys::fs::openFileForRead(Name, FD, sys::fs::OF_None, &RealName))
a233 6
std::error_code
RealFileSystem::getRealPath(const Twine &Path,
                            SmallVectorImpl<char> &Output) const {
  return llvm::sys::fs::real_path(Path, Output);
}

a239 1

a241 1

d244 1
a244 1
    if (Iter != llvm::sys::fs::directory_iterator()) {
d246 1
a246 1
      std::error_code ErrorCode = llvm::sys::fs::status(Iter->path(), S, true);
a247 2
      if (!EC)
        EC = ErrorCode;
d254 3
a256 1
    if (Iter == llvm::sys::fs::directory_iterator()) {
d260 1
a260 1
      std::error_code ErrorCode = llvm::sys::fs::status(Iter->path(), S, true);
a261 2
      if (!EC)
        EC = ErrorCode;
d266 1
a266 2

} // namespace
a275 1

a312 1

d318 1
a318 1
  return {};
d321 1
a321 10
std::error_code
OverlayFileSystem::getRealPath(const Twine &Path,
                               SmallVectorImpl<char> &Output) const {
  for (auto &FS : FSList)
    if (FS->exists(Path))
      return FS->getRealPath(Path, Output);
  return errc::no_such_file_or_directory;
}

clang::vfs::detail::DirIterImpl::~DirIterImpl() = default;
a323 1

d342 1
a342 1
    return {};
d381 1
a381 2

} // namespace
a390 1

d404 1
a404 2
  virtual ~InMemoryNode() = default;

a410 1

a418 1

a421 1

a434 1

d442 1
a442 2

  std::error_code close() override { return {}; }
d444 1
a444 2

} // namespace
a451 1

a457 1

d463 1
a463 2
  using const_iterator = decltype(Entries)::const_iterator;

d470 1
a470 1
    for (const auto &Entry : Entries)
d472 1
a474 1

d479 1
a479 2

} // namespace detail
d488 1
a488 1
InMemoryFileSystem::~InMemoryFileSystem() = default;
d495 1
a495 5
                                 std::unique_ptr<llvm::MemoryBuffer> Buffer,
                                 Optional<uint32_t> User,
                                 Optional<uint32_t> Group,
                                 Optional<llvm::sys::fs::file_type> Type,
                                 Optional<llvm::sys::fs::perms> Perms) {
d511 1
a511 8
  auto I = llvm::sys::path::begin(Path), E = sys::path::end(Path);
  const auto ResolvedUser = User.getValueOr(0);
  const auto ResolvedGroup = Group.getValueOr(0);
  const auto ResolvedType = Type.getValueOr(sys::fs::file_type::regular_file);
  const auto ResolvedPerms = Perms.getValueOr(sys::fs::all_all);
  // Any intermediate directories we create should be accessible by
  // the owner, even if Perms says otherwise for the final path.
  const auto NewDirectoryPerms = ResolvedPerms | sys::fs::owner_all;
d518 2
a519 1
        // End of the path, create a new file or directory.
d521 6
a526 11
                    llvm::sys::toTimePoint(ModificationTime), ResolvedUser,
                    ResolvedGroup, Buffer->getBufferSize(), ResolvedType,
                    ResolvedPerms);
        std::unique_ptr<detail::InMemoryNode> Child;
        if (ResolvedType == sys::fs::file_type::directory_file) {
          Child.reset(new detail::InMemoryDirectory(std::move(Stat)));
        } else {
          Child.reset(new detail::InMemoryFile(std::move(Stat),
                                               std::move(Buffer)));
        }
        Dir->addChild(Name, std::move(Child));
d531 1
d534 3
a536 3
          getNextVirtualUniqueID(), llvm::sys::toTimePoint(ModificationTime),
          ResolvedUser, ResolvedGroup, Buffer->getBufferSize(),
          sys::fs::file_type::directory_file, NewDirectoryPerms);
d560 1
a560 5
                                      llvm::MemoryBuffer *Buffer,
                                      Optional<uint32_t> User,
                                      Optional<uint32_t> Group,
                                      Optional<llvm::sys::fs::file_type> Type,
                                      Optional<llvm::sys::fs::perms> Perms) {
d563 1
a563 3
                     Buffer->getBuffer(), Buffer->getBufferIdentifier()),
                 std::move(User), std::move(Group), std::move(Type),
                 std::move(Perms));
a626 1

d633 1
a633 2
  InMemoryDirIterator() = default;

d645 1
a645 1
    return {};
d648 1
a648 2

} // namespace
d679 2
a680 1
  return {};
a681 12

std::error_code
InMemoryFileSystem::getRealPath(const Twine &Path,
                                SmallVectorImpl<char> &Output) const {
  auto CWD = getCurrentWorkingDirectory();
  if (!CWD || CWD->empty())
    return errc::operation_not_permitted;
  Path.toVector(Output);
  if (auto EC = makeAbsolute(Output))
    return EC;
  llvm::sys::path::remove_dots(Output, /*remove_dot_dot=*/true);
  return {};
a683 3
} // namespace vfs
} // namespace clang

d695 1
a695 1
/// A single file or directory in the VFS.
d701 1
a702 2
  virtual ~Entry() = default;

a718 1

a719 1

a722 1

d724 1
a724 3

  using iterator = decltype(Contents)::iterator;

a726 1

a736 1

a739 1

a744 1

d746 1
a746 2

  /// whether to use the external path as the name for this file.
a750 1

a751 1

a766 1

d770 1
a770 1
/// A virtual file system parsed from a YAML file.
a825 2
  friend class RedirectingFileSystemParser;

d828 1
a828 2

  /// The file system to use for external references.
a829 1

d838 1
a838 1
  /// Whether to perform case-sensitive comparisons.
d847 1
a847 1
  /// Whether to use to use the value of 'external-contents' for the
d851 1
a851 1
  /// Whether an invalid path obtained via 'external-contents' should
d864 1
a864 1
#ifdef _WIN32
d870 2
d876 1
a876 1
  /// Looks up the path <tt>[Start, End)</tt> in \p From, possibly
d881 1
a881 1
  /// Get the status of a given an \c Entry.
d885 1
a885 1
  /// Looks up \p Path in \c Roots.
d888 1
a888 1
  /// Parses \p Buffer, which is expected to be in YAML format and
a900 1

d909 1
a909 1
      return {};
d914 1
a914 1
      return {};
d919 1
a919 1
      return {};
d941 1
a941 1
    for (const auto &Root : Roots)
d961 1
d964 1
a964 1
/// A helper class to hold the common YAML parsing state.
d975 1
a975 2
    const auto *S = dyn_cast<yaml::ScalarNode>(N);

d1006 1
d1008 1
a1008 3
    bool Seen = false;

    KeyStatus(bool Required = false) : Required(Required) {}
d1010 1
a1010 2

  using KeyStatusPair = std::pair<StringRef, KeyStatus>;
d1030 5
a1034 3
    for (const auto &I : Keys) {
      if (I.second.Required && !I.second.Seen) {
        error(Obj, Twine("missing key '") + I.first + "'");
d1044 1
a1044 1
      for (const auto &Root : FS->Roots) {
d1107 1
a1107 1
    auto *M = dyn_cast<yaml::MappingNode>(N);
d1130 2
a1131 1
    for (auto &I : *M) {
d1136 1
a1136 1
      if (!parseScalarString(I.getKey(), Key, Buffer))
d1139 1
a1139 1
      if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
d1144 1
a1144 1
        if (!parseScalarString(I.getValue(), Value, Buffer))
d1158 1
a1158 1
        if (!parseScalarString(I.getValue(), Value, Buffer))
d1165 1
a1165 1
          error(I.getValue(), "unknown value for 'type'");
d1170 1
a1170 1
          error(I.getKey(),
d1175 2
a1176 1
        auto *Contents = dyn_cast<yaml::SequenceNode>(I.getValue());
d1179 1
a1179 1
          error(I.getValue(), "expected array");
d1183 4
a1186 2
        for (auto &I : *Contents) {
          if (std::unique_ptr<Entry> E = parseEntry(&I, FS))
d1193 1
a1193 1
          error(I.getKey(),
d1198 1
a1198 1
        if (!parseScalarString(I.getValue(), Value, Buffer))
d1220 1
a1220 1
        if (!parseScalarBool(I.getValue(), Val))
d1293 1
a1293 1
    auto *Top = dyn_cast<yaml::MappingNode>(Root);
d1312 2
a1313 1
    for (auto &I : *Top) {
d1316 1
a1316 1
      if (!parseScalarString(I.getKey(), Key, KeyBuffer))
d1319 1
a1319 1
      if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
d1323 1
a1323 1
        auto *Roots = dyn_cast<yaml::SequenceNode>(I.getValue());
d1325 1
a1325 1
          error(I.getValue(), "expected array");
d1329 3
a1331 2
        for (auto &I : *Roots) {
          if (std::unique_ptr<Entry> E = parseEntry(&I, FS))
d1339 1
a1339 1
        if (!parseScalarString(I.getValue(), VersionString, Storage))
d1343 1
a1343 1
          error(I.getValue(), "expected integer");
d1347 1
a1347 1
          error(I.getValue(), "invalid version number");
d1351 1
a1351 1
          error(I.getValue(), "version mismatch, expected 0");
d1355 1
a1355 1
        if (!parseScalarBool(I.getValue(), FS->CaseSensitive))
d1358 1
a1358 1
        if (!parseScalarBool(I.getValue(), FS->IsRelativeOverlay))
d1361 1
a1361 1
        if (!parseScalarBool(I.getValue(), FS->UseExternalNames))
d1364 1
a1364 1
        if (!parseScalarBool(I.getValue(), FS->IgnoreNonExistentContents))
d1380 1
a1380 1
    for (auto &E : RootEntries)
d1386 1
d1388 1
a1388 1
} // namespace
d1395 1
d1455 1
a1455 1
  for (const auto &Root : Roots) {
d1466 1
a1466 1
#ifndef _WIN32
a1538 1

a1549 1

a1554 1

d1557 1
a1557 2

} // namespace
a1651 1

d1655 2
a1656 3

  unsigned getDirIndent() { return 4 * DirStack.size(); }
  unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
a1664 1

d1669 1
a1669 2

} // namespace
a1672 1

d1794 2
a1795 2
  llvm::sort(Mappings.begin(), Mappings.end(),
             [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
d1850 1
a1850 1
  return {};
@


1.1.1.7.4.2
log
@Mostly merge changes from HEAD upto 20200411
@
text
@@


1.1.1.7.2.1
log
@Sync with HEAD
@
text
@d1 1
a1 1
//===- VirtualFileSystem.cpp - Virtual File System Layer ------------------===//
a8 1
//
a9 1
//
d13 1
a13 2
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
a14 2
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/Optional.h"
d16 1
a16 3
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
a17 1
#include "llvm/ADT/Twine.h"
a19 3
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Chrono.h"
a21 3
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
a24 2
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/SourceMgr.h"
a25 2
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
a26 5
#include <cassert>
#include <cstdint>
#include <iterator>
#include <limits>
#include <map>
a27 2
#include <string>
#include <system_error>
a28 1
#include <vector>
d31 1
a31 1
using namespace vfs;
a32 1

d41 1
a41 1
      Type(Status.type()), Perms(Status.permissions())  {}
d47 1
a47 1
      Type(Type), Perms(Perms) {}
a61 1
  assert(isStatusKnown() && Other.isStatusKnown());
a63 1

a66 1

a69 1

a72 1

a75 1

a78 1

d83 1
a83 1
File::~File() = default;
d85 1
a85 1
FileSystem::~FileSystem() = default;
d99 1
a99 1
    return {};
a107 5
std::error_code FileSystem::getRealPath(const Twine &Path,
                                        SmallVectorImpl<char> &Output) const {
  return errc::operation_not_permitted;
}

a119 1

d132 1
a132 2

/// Wrapper around a raw file descriptor.
a133 2
  friend class RealFileSystem;

d137 1
a137 1

a146 1

d155 1
a155 3

} // namespace

d188 1
a188 2

/// The file system according to your operating system.
a196 2
  std::error_code getRealPath(const Twine &Path,
                              SmallVectorImpl<char> &Output) const override;
d198 1
a198 2

} // namespace
d211 1
a211 2
  if (std::error_code EC =
          sys::fs::openFileForRead(Name, FD, sys::fs::OF_None, &RealName))
a233 6
std::error_code
RealFileSystem::getRealPath(const Twine &Path,
                            SmallVectorImpl<char> &Output) const {
  return llvm::sys::fs::real_path(Path, Output);
}

a239 1

a241 1

d244 1
a244 1
    if (Iter != llvm::sys::fs::directory_iterator()) {
d246 1
a246 1
      std::error_code ErrorCode = llvm::sys::fs::status(Iter->path(), S, true);
a247 2
      if (!EC)
        EC = ErrorCode;
d254 3
a256 1
    if (Iter == llvm::sys::fs::directory_iterator()) {
d260 1
a260 1
      std::error_code ErrorCode = llvm::sys::fs::status(Iter->path(), S, true);
a261 2
      if (!EC)
        EC = ErrorCode;
d266 1
a266 2

} // namespace
a275 1

a312 1

d318 1
a318 1
  return {};
d321 1
a321 10
std::error_code
OverlayFileSystem::getRealPath(const Twine &Path,
                               SmallVectorImpl<char> &Output) const {
  for (auto &FS : FSList)
    if (FS->exists(Path))
      return FS->getRealPath(Path, Output);
  return errc::no_such_file_or_directory;
}

clang::vfs::detail::DirIterImpl::~DirIterImpl() = default;
a323 1

d342 1
a342 1
    return {};
d381 1
a381 2

} // namespace
a390 1

d404 1
a404 2
  virtual ~InMemoryNode() = default;

a410 1

a418 1

a421 1

a434 1

d442 1
a442 2

  std::error_code close() override { return {}; }
d444 1
a444 2

} // namespace
a451 1

a457 1

d463 1
a463 2
  using const_iterator = decltype(Entries)::const_iterator;

d470 1
a470 1
    for (const auto &Entry : Entries)
d472 1
a474 1

d479 1
a479 2

} // namespace detail
d488 1
a488 1
InMemoryFileSystem::~InMemoryFileSystem() = default;
d495 1
a495 5
                                 std::unique_ptr<llvm::MemoryBuffer> Buffer,
                                 Optional<uint32_t> User,
                                 Optional<uint32_t> Group,
                                 Optional<llvm::sys::fs::file_type> Type,
                                 Optional<llvm::sys::fs::perms> Perms) {
d511 1
a511 8
  auto I = llvm::sys::path::begin(Path), E = sys::path::end(Path);
  const auto ResolvedUser = User.getValueOr(0);
  const auto ResolvedGroup = Group.getValueOr(0);
  const auto ResolvedType = Type.getValueOr(sys::fs::file_type::regular_file);
  const auto ResolvedPerms = Perms.getValueOr(sys::fs::all_all);
  // Any intermediate directories we create should be accessible by
  // the owner, even if Perms says otherwise for the final path.
  const auto NewDirectoryPerms = ResolvedPerms | sys::fs::owner_all;
d518 2
a519 1
        // End of the path, create a new file or directory.
d521 6
a526 11
                    llvm::sys::toTimePoint(ModificationTime), ResolvedUser,
                    ResolvedGroup, Buffer->getBufferSize(), ResolvedType,
                    ResolvedPerms);
        std::unique_ptr<detail::InMemoryNode> Child;
        if (ResolvedType == sys::fs::file_type::directory_file) {
          Child.reset(new detail::InMemoryDirectory(std::move(Stat)));
        } else {
          Child.reset(new detail::InMemoryFile(std::move(Stat),
                                               std::move(Buffer)));
        }
        Dir->addChild(Name, std::move(Child));
d531 1
d534 3
a536 3
          getNextVirtualUniqueID(), llvm::sys::toTimePoint(ModificationTime),
          ResolvedUser, ResolvedGroup, Buffer->getBufferSize(),
          sys::fs::file_type::directory_file, NewDirectoryPerms);
d560 1
a560 5
                                      llvm::MemoryBuffer *Buffer,
                                      Optional<uint32_t> User,
                                      Optional<uint32_t> Group,
                                      Optional<llvm::sys::fs::file_type> Type,
                                      Optional<llvm::sys::fs::perms> Perms) {
d563 1
a563 3
                     Buffer->getBuffer(), Buffer->getBufferIdentifier()),
                 std::move(User), std::move(Group), std::move(Type),
                 std::move(Perms));
a626 1

d633 1
a633 2
  InMemoryDirIterator() = default;

d645 1
a645 1
    return {};
d648 1
a648 2

} // namespace
d679 2
a680 1
  return {};
a681 12

std::error_code
InMemoryFileSystem::getRealPath(const Twine &Path,
                                SmallVectorImpl<char> &Output) const {
  auto CWD = getCurrentWorkingDirectory();
  if (!CWD || CWD->empty())
    return errc::operation_not_permitted;
  Path.toVector(Output);
  if (auto EC = makeAbsolute(Output))
    return EC;
  llvm::sys::path::remove_dots(Output, /*remove_dot_dot=*/true);
  return {};
a683 3
} // namespace vfs
} // namespace clang

d695 1
a695 1
/// A single file or directory in the VFS.
d701 1
a702 2
  virtual ~Entry() = default;

a718 1

a719 1

a722 1

d724 1
a724 3

  using iterator = decltype(Contents)::iterator;

a726 1

a736 1

a739 1

a744 1

d746 1
a746 2

  /// whether to use the external path as the name for this file.
a750 1

a751 1

a766 1

d770 1
a770 1
/// A virtual file system parsed from a YAML file.
a825 2
  friend class RedirectingFileSystemParser;

d828 1
a828 2

  /// The file system to use for external references.
a829 1

d838 1
a838 1
  /// Whether to perform case-sensitive comparisons.
d847 1
a847 1
  /// Whether to use to use the value of 'external-contents' for the
d851 1
a851 1
  /// Whether an invalid path obtained via 'external-contents' should
d864 1
a864 1
#ifdef _WIN32
d870 2
d876 1
a876 1
  /// Looks up the path <tt>[Start, End)</tt> in \p From, possibly
d881 1
a881 1
  /// Get the status of a given an \c Entry.
d885 1
a885 1
  /// Looks up \p Path in \c Roots.
d888 1
a888 1
  /// Parses \p Buffer, which is expected to be in YAML format and
a900 1

d909 1
a909 1
      return {};
d914 1
a914 1
      return {};
d919 1
a919 1
      return {};
d941 1
a941 1
    for (const auto &Root : Roots)
d961 1
d964 1
a964 1
/// A helper class to hold the common YAML parsing state.
d975 1
a975 2
    const auto *S = dyn_cast<yaml::ScalarNode>(N);

d1006 1
d1008 1
a1008 3
    bool Seen = false;

    KeyStatus(bool Required = false) : Required(Required) {}
d1010 1
a1010 2

  using KeyStatusPair = std::pair<StringRef, KeyStatus>;
d1030 5
a1034 3
    for (const auto &I : Keys) {
      if (I.second.Required && !I.second.Seen) {
        error(Obj, Twine("missing key '") + I.first + "'");
d1044 1
a1044 1
      for (const auto &Root : FS->Roots) {
d1107 1
a1107 1
    auto *M = dyn_cast<yaml::MappingNode>(N);
d1130 2
a1131 1
    for (auto &I : *M) {
d1136 1
a1136 1
      if (!parseScalarString(I.getKey(), Key, Buffer))
d1139 1
a1139 1
      if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
d1144 1
a1144 1
        if (!parseScalarString(I.getValue(), Value, Buffer))
d1158 1
a1158 1
        if (!parseScalarString(I.getValue(), Value, Buffer))
d1165 1
a1165 1
          error(I.getValue(), "unknown value for 'type'");
d1170 1
a1170 1
          error(I.getKey(),
d1175 2
a1176 1
        auto *Contents = dyn_cast<yaml::SequenceNode>(I.getValue());
d1179 1
a1179 1
          error(I.getValue(), "expected array");
d1183 4
a1186 2
        for (auto &I : *Contents) {
          if (std::unique_ptr<Entry> E = parseEntry(&I, FS))
d1193 1
a1193 1
          error(I.getKey(),
d1198 1
a1198 1
        if (!parseScalarString(I.getValue(), Value, Buffer))
d1220 1
a1220 1
        if (!parseScalarBool(I.getValue(), Val))
d1293 1
a1293 1
    auto *Top = dyn_cast<yaml::MappingNode>(Root);
d1312 2
a1313 1
    for (auto &I : *Top) {
d1316 1
a1316 1
      if (!parseScalarString(I.getKey(), Key, KeyBuffer))
d1319 1
a1319 1
      if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
d1323 1
a1323 1
        auto *Roots = dyn_cast<yaml::SequenceNode>(I.getValue());
d1325 1
a1325 1
          error(I.getValue(), "expected array");
d1329 3
a1331 2
        for (auto &I : *Roots) {
          if (std::unique_ptr<Entry> E = parseEntry(&I, FS))
d1339 1
a1339 1
        if (!parseScalarString(I.getValue(), VersionString, Storage))
d1343 1
a1343 1
          error(I.getValue(), "expected integer");
d1347 1
a1347 1
          error(I.getValue(), "invalid version number");
d1351 1
a1351 1
          error(I.getValue(), "version mismatch, expected 0");
d1355 1
a1355 1
        if (!parseScalarBool(I.getValue(), FS->CaseSensitive))
d1358 1
a1358 1
        if (!parseScalarBool(I.getValue(), FS->IsRelativeOverlay))
d1361 1
a1361 1
        if (!parseScalarBool(I.getValue(), FS->UseExternalNames))
d1364 1
a1364 1
        if (!parseScalarBool(I.getValue(), FS->IgnoreNonExistentContents))
d1380 1
a1380 1
    for (auto &E : RootEntries)
d1386 1
d1388 1
a1388 1
} // namespace
d1395 1
d1455 1
a1455 1
  for (const auto &Root : Roots) {
d1466 1
a1466 1
#ifndef _WIN32
a1538 1

a1549 1

a1554 1

d1557 1
a1557 2

} // namespace
a1651 1

d1655 2
a1656 3

  unsigned getDirIndent() { return 4 * DirStack.size(); }
  unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
a1664 1

d1669 1
a1669 2

} // namespace
a1672 1

d1794 2
a1795 2
  llvm::sort(Mappings.begin(), Mappings.end(),
             [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
d1850 1
a1850 1
  return {};
@


1.1.1.8
log
@Import clang r337282 from trunk
@
text
@d1 1
a1 1
//===- VirtualFileSystem.cpp - Virtual File System Layer ------------------===//
a8 1
//
a9 1
//
d13 1
a13 2
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
a14 2
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/Optional.h"
d16 1
a16 3
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
a17 1
#include "llvm/ADT/Twine.h"
a19 3
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Chrono.h"
a21 3
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
a24 2
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/SourceMgr.h"
a25 2
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
a26 5
#include <cassert>
#include <cstdint>
#include <iterator>
#include <limits>
#include <map>
a27 2
#include <string>
#include <system_error>
a28 1
#include <vector>
d31 1
a31 1
using namespace vfs;
a32 1

d41 1
a41 1
      Type(Status.type()), Perms(Status.permissions())  {}
d47 1
a47 1
      Type(Type), Perms(Perms) {}
a61 1
  assert(isStatusKnown() && Other.isStatusKnown());
a63 1

a66 1

a69 1

a72 1

a75 1

a78 1

d83 1
a83 1
File::~File() = default;
d85 1
a85 1
FileSystem::~FileSystem() = default;
d99 1
a99 1
    return {};
a107 5
std::error_code FileSystem::getRealPath(const Twine &Path,
                                        SmallVectorImpl<char> &Output) const {
  return errc::operation_not_permitted;
}

a119 1

d132 1
a132 2

/// Wrapper around a raw file descriptor.
a133 2
  friend class RealFileSystem;

d137 1
a137 1

a146 1

d155 1
a155 3

} // namespace

d188 1
a188 2

/// The file system according to your operating system.
a196 2
  std::error_code getRealPath(const Twine &Path,
                              SmallVectorImpl<char> &Output) const override;
d198 1
a198 2

} // namespace
d211 1
a211 2
  if (std::error_code EC =
          sys::fs::openFileForRead(Name, FD, sys::fs::OF_None, &RealName))
a233 6
std::error_code
RealFileSystem::getRealPath(const Twine &Path,
                            SmallVectorImpl<char> &Output) const {
  return llvm::sys::fs::real_path(Path, Output);
}

a239 1

a241 1

d244 1
a244 1
    if (Iter != llvm::sys::fs::directory_iterator()) {
d246 1
a246 1
      std::error_code ErrorCode = llvm::sys::fs::status(Iter->path(), S, true);
a247 2
      if (!EC)
        EC = ErrorCode;
d254 3
a256 1
    if (Iter == llvm::sys::fs::directory_iterator()) {
d260 1
a260 1
      std::error_code ErrorCode = llvm::sys::fs::status(Iter->path(), S, true);
a261 2
      if (!EC)
        EC = ErrorCode;
d266 1
a266 2

} // namespace
a275 1

a312 1

d318 1
a318 1
  return {};
d321 1
a321 10
std::error_code
OverlayFileSystem::getRealPath(const Twine &Path,
                               SmallVectorImpl<char> &Output) const {
  for (auto &FS : FSList)
    if (FS->exists(Path))
      return FS->getRealPath(Path, Output);
  return errc::no_such_file_or_directory;
}

clang::vfs::detail::DirIterImpl::~DirIterImpl() = default;
a323 1

d342 1
a342 1
    return {};
d381 1
a381 2

} // namespace
a390 1

d404 1
a404 2
  virtual ~InMemoryNode() = default;

a410 1

a418 1

a421 1

a434 1

d442 1
a442 2

  std::error_code close() override { return {}; }
d444 1
a444 2

} // namespace
a451 1

a457 1

d463 1
a463 2
  using const_iterator = decltype(Entries)::const_iterator;

d470 1
a470 1
    for (const auto &Entry : Entries)
d472 1
a474 1

d479 1
a479 2

} // namespace detail
d488 1
a488 1
InMemoryFileSystem::~InMemoryFileSystem() = default;
d495 1
a495 5
                                 std::unique_ptr<llvm::MemoryBuffer> Buffer,
                                 Optional<uint32_t> User,
                                 Optional<uint32_t> Group,
                                 Optional<llvm::sys::fs::file_type> Type,
                                 Optional<llvm::sys::fs::perms> Perms) {
d511 1
a511 8
  auto I = llvm::sys::path::begin(Path), E = sys::path::end(Path);
  const auto ResolvedUser = User.getValueOr(0);
  const auto ResolvedGroup = Group.getValueOr(0);
  const auto ResolvedType = Type.getValueOr(sys::fs::file_type::regular_file);
  const auto ResolvedPerms = Perms.getValueOr(sys::fs::all_all);
  // Any intermediate directories we create should be accessible by
  // the owner, even if Perms says otherwise for the final path.
  const auto NewDirectoryPerms = ResolvedPerms | sys::fs::owner_all;
d518 2
a519 1
        // End of the path, create a new file or directory.
d521 6
a526 11
                    llvm::sys::toTimePoint(ModificationTime), ResolvedUser,
                    ResolvedGroup, Buffer->getBufferSize(), ResolvedType,
                    ResolvedPerms);
        std::unique_ptr<detail::InMemoryNode> Child;
        if (ResolvedType == sys::fs::file_type::directory_file) {
          Child.reset(new detail::InMemoryDirectory(std::move(Stat)));
        } else {
          Child.reset(new detail::InMemoryFile(std::move(Stat),
                                               std::move(Buffer)));
        }
        Dir->addChild(Name, std::move(Child));
d531 1
d534 3
a536 3
          getNextVirtualUniqueID(), llvm::sys::toTimePoint(ModificationTime),
          ResolvedUser, ResolvedGroup, Buffer->getBufferSize(),
          sys::fs::file_type::directory_file, NewDirectoryPerms);
d560 1
a560 5
                                      llvm::MemoryBuffer *Buffer,
                                      Optional<uint32_t> User,
                                      Optional<uint32_t> Group,
                                      Optional<llvm::sys::fs::file_type> Type,
                                      Optional<llvm::sys::fs::perms> Perms) {
d563 1
a563 3
                     Buffer->getBuffer(), Buffer->getBufferIdentifier()),
                 std::move(User), std::move(Group), std::move(Type),
                 std::move(Perms));
a626 1

d633 1
a633 2
  InMemoryDirIterator() = default;

d645 1
a645 1
    return {};
d648 1
a648 2

} // namespace
d679 2
a680 1
  return {};
a681 12

std::error_code
InMemoryFileSystem::getRealPath(const Twine &Path,
                                SmallVectorImpl<char> &Output) const {
  auto CWD = getCurrentWorkingDirectory();
  if (!CWD || CWD->empty())
    return errc::operation_not_permitted;
  Path.toVector(Output);
  if (auto EC = makeAbsolute(Output))
    return EC;
  llvm::sys::path::remove_dots(Output, /*remove_dot_dot=*/true);
  return {};
a683 3
} // namespace vfs
} // namespace clang

d695 1
a695 1
/// A single file or directory in the VFS.
d701 1
a702 2
  virtual ~Entry() = default;

a718 1

a719 1

a722 1

d724 1
a724 3

  using iterator = decltype(Contents)::iterator;

a726 1

a736 1

a739 1

a744 1

d746 1
a746 2

  /// whether to use the external path as the name for this file.
a750 1

a751 1

a766 1

d770 1
a770 1
/// A virtual file system parsed from a YAML file.
a825 2
  friend class RedirectingFileSystemParser;

d828 1
a828 2

  /// The file system to use for external references.
a829 1

d838 1
a838 1
  /// Whether to perform case-sensitive comparisons.
d847 1
a847 1
  /// Whether to use to use the value of 'external-contents' for the
d851 1
a851 1
  /// Whether an invalid path obtained via 'external-contents' should
d864 1
a864 1
#ifdef _WIN32
d870 2
d876 1
a876 1
  /// Looks up the path <tt>[Start, End)</tt> in \p From, possibly
d881 1
a881 1
  /// Get the status of a given an \c Entry.
d885 1
a885 1
  /// Looks up \p Path in \c Roots.
d888 1
a888 1
  /// Parses \p Buffer, which is expected to be in YAML format and
a900 1

d909 1
a909 1
      return {};
d914 1
a914 1
      return {};
d919 1
a919 1
      return {};
d941 1
a941 1
    for (const auto &Root : Roots)
d961 1
d964 1
a964 1
/// A helper class to hold the common YAML parsing state.
d975 1
a975 2
    const auto *S = dyn_cast<yaml::ScalarNode>(N);

d1006 1
d1008 1
a1008 3
    bool Seen = false;

    KeyStatus(bool Required = false) : Required(Required) {}
d1010 1
a1010 2

  using KeyStatusPair = std::pair<StringRef, KeyStatus>;
d1030 5
a1034 3
    for (const auto &I : Keys) {
      if (I.second.Required && !I.second.Seen) {
        error(Obj, Twine("missing key '") + I.first + "'");
d1044 1
a1044 1
      for (const auto &Root : FS->Roots) {
d1107 1
a1107 1
    auto *M = dyn_cast<yaml::MappingNode>(N);
d1130 2
a1131 1
    for (auto &I : *M) {
d1136 1
a1136 1
      if (!parseScalarString(I.getKey(), Key, Buffer))
d1139 1
a1139 1
      if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
d1144 1
a1144 1
        if (!parseScalarString(I.getValue(), Value, Buffer))
d1158 1
a1158 1
        if (!parseScalarString(I.getValue(), Value, Buffer))
d1165 1
a1165 1
          error(I.getValue(), "unknown value for 'type'");
d1170 1
a1170 1
          error(I.getKey(),
d1175 2
a1176 1
        auto *Contents = dyn_cast<yaml::SequenceNode>(I.getValue());
d1179 1
a1179 1
          error(I.getValue(), "expected array");
d1183 4
a1186 2
        for (auto &I : *Contents) {
          if (std::unique_ptr<Entry> E = parseEntry(&I, FS))
d1193 1
a1193 1
          error(I.getKey(),
d1198 1
a1198 1
        if (!parseScalarString(I.getValue(), Value, Buffer))
d1220 1
a1220 1
        if (!parseScalarBool(I.getValue(), Val))
d1293 1
a1293 1
    auto *Top = dyn_cast<yaml::MappingNode>(Root);
d1312 2
a1313 1
    for (auto &I : *Top) {
d1316 1
a1316 1
      if (!parseScalarString(I.getKey(), Key, KeyBuffer))
d1319 1
a1319 1
      if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
d1323 1
a1323 1
        auto *Roots = dyn_cast<yaml::SequenceNode>(I.getValue());
d1325 1
a1325 1
          error(I.getValue(), "expected array");
d1329 3
a1331 2
        for (auto &I : *Roots) {
          if (std::unique_ptr<Entry> E = parseEntry(&I, FS))
d1339 1
a1339 1
        if (!parseScalarString(I.getValue(), VersionString, Storage))
d1343 1
a1343 1
          error(I.getValue(), "expected integer");
d1347 1
a1347 1
          error(I.getValue(), "invalid version number");
d1351 1
a1351 1
          error(I.getValue(), "version mismatch, expected 0");
d1355 1
a1355 1
        if (!parseScalarBool(I.getValue(), FS->CaseSensitive))
d1358 1
a1358 1
        if (!parseScalarBool(I.getValue(), FS->IsRelativeOverlay))
d1361 1
a1361 1
        if (!parseScalarBool(I.getValue(), FS->UseExternalNames))
d1364 1
a1364 1
        if (!parseScalarBool(I.getValue(), FS->IgnoreNonExistentContents))
d1380 1
a1380 1
    for (auto &E : RootEntries)
d1386 1
d1388 1
a1388 1
} // namespace
d1395 1
d1455 1
a1455 1
  for (const auto &Root : Roots) {
d1466 1
a1466 1
#ifndef _WIN32
a1538 1

a1549 1

a1554 1

d1557 1
a1557 2

} // namespace
a1651 1

d1655 2
a1656 3

  unsigned getDirIndent() { return 4 * DirStack.size(); }
  unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
a1664 1

d1669 1
a1669 2

} // namespace
a1672 1

d1794 2
a1795 2
  llvm::sort(Mappings.begin(), Mappings.end(),
             [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
d1850 1
a1850 1
  return {};
@


1.1.1.9
log
@Mark old LLVM instance as dead.
@
text
@@


1.1.1.3.4.1
log
@file VirtualFileSystem.cpp was added on branch tls-maxphys on 2014-08-19 23:47:26 +0000
@
text
@d1 1209
@


1.1.1.3.4.2
log
@Rebase to HEAD as of a few days ago.
@
text
@a0 1209
//===- VirtualFileSystem.cpp - Virtual File System Layer --------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// This file implements the VirtualFileSystem interface.
//===----------------------------------------------------------------------===//

#include "clang/Basic/VirtualFileSystem.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/YAMLParser.h"
#include <atomic>
#include <memory>

using namespace clang;
using namespace clang::vfs;
using namespace llvm;
using llvm::sys::fs::file_status;
using llvm::sys::fs::file_type;
using llvm::sys::fs::perms;
using llvm::sys::fs::UniqueID;

Status::Status(const file_status &Status)
    : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
      User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
      Type(Status.type()), Perms(Status.permissions()), IsVFSMapped(false)  {}

Status::Status(StringRef Name, StringRef ExternalName, UniqueID UID,
               sys::TimeValue MTime, uint32_t User, uint32_t Group,
               uint64_t Size, file_type Type, perms Perms)
    : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size),
      Type(Type), Perms(Perms), IsVFSMapped(false) {}

bool Status::equivalent(const Status &Other) const {
  return getUniqueID() == Other.getUniqueID();
}
bool Status::isDirectory() const {
  return Type == file_type::directory_file;
}
bool Status::isRegularFile() const {
  return Type == file_type::regular_file;
}
bool Status::isOther() const {
  return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
}
bool Status::isSymlink() const {
  return Type == file_type::symlink_file;
}
bool Status::isStatusKnown() const {
  return Type != file_type::status_error;
}
bool Status::exists() const {
  return isStatusKnown() && Type != file_type::file_not_found;
}

File::~File() {}

FileSystem::~FileSystem() {}

std::error_code FileSystem::getBufferForFile(
    const llvm::Twine &Name, std::unique_ptr<MemoryBuffer> &Result,
    int64_t FileSize, bool RequiresNullTerminator, bool IsVolatile) {
  std::unique_ptr<File> F;
  if (std::error_code EC = openFileForRead(Name, F))
    return EC;

  std::error_code EC =
      F->getBuffer(Name, Result, FileSize, RequiresNullTerminator, IsVolatile);
  return EC;
}

//===-----------------------------------------------------------------------===/
// RealFileSystem implementation
//===-----------------------------------------------------------------------===/

namespace {
/// \brief Wrapper around a raw file descriptor.
class RealFile : public File {
  int FD;
  Status S;
  friend class RealFileSystem;
  RealFile(int FD) : FD(FD) {
    assert(FD >= 0 && "Invalid or inactive file descriptor");
  }

public:
  ~RealFile();
  ErrorOr<Status> status() override;
  std::error_code getBuffer(const Twine &Name,
                            std::unique_ptr<MemoryBuffer> &Result,
                            int64_t FileSize = -1,
                            bool RequiresNullTerminator = true,
                            bool IsVolatile = false) override;
  std::error_code close() override;
  void setName(StringRef Name) override;
};
} // end anonymous namespace
RealFile::~RealFile() { close(); }

ErrorOr<Status> RealFile::status() {
  assert(FD != -1 && "cannot stat closed file");
  if (!S.isStatusKnown()) {
    file_status RealStatus;
    if (std::error_code EC = sys::fs::status(FD, RealStatus))
      return EC;
    Status NewS(RealStatus);
    NewS.setName(S.getName());
    S = std::move(NewS);
  }
  return S;
}

std::error_code RealFile::getBuffer(const Twine &Name,
                                    std::unique_ptr<MemoryBuffer> &Result,
                                    int64_t FileSize,
                                    bool RequiresNullTerminator,
                                    bool IsVolatile) {
  assert(FD != -1 && "cannot get buffer for closed file");
  ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
      MemoryBuffer::getOpenFile(FD, Name.str().c_str(), FileSize,
                                RequiresNullTerminator, IsVolatile);
  if (std::error_code EC = BufferOrErr.getError())
    return EC;
  Result = std::move(BufferOrErr.get());
  return std::error_code();
}

// FIXME: This is terrible, we need this for ::close.
#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <unistd.h>
#include <sys/uio.h>
#else
#include <io.h>
#ifndef S_ISFIFO
#define S_ISFIFO(x) (0)
#endif
#endif
std::error_code RealFile::close() {
  if (::close(FD))
    return std::error_code(errno, std::generic_category());
  FD = -1;
  return std::error_code();
}

void RealFile::setName(StringRef Name) {
  S.setName(Name);
}

namespace {
/// \brief The file system according to your operating system.
class RealFileSystem : public FileSystem {
public:
  ErrorOr<Status> status(const Twine &Path) override;
  std::error_code openFileForRead(const Twine &Path,
                                  std::unique_ptr<File> &Result) override;
  directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
};
} // end anonymous namespace

ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
  sys::fs::file_status RealStatus;
  if (std::error_code EC = sys::fs::status(Path, RealStatus))
    return EC;
  Status Result(RealStatus);
  Result.setName(Path.str());
  return Result;
}

std::error_code RealFileSystem::openFileForRead(const Twine &Name,
                                                std::unique_ptr<File> &Result) {
  int FD;
  if (std::error_code EC = sys::fs::openFileForRead(Name, FD))
    return EC;
  Result.reset(new RealFile(FD));
  Result->setName(Name.str());
  return std::error_code();
}

IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
  static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem();
  return FS;
}

namespace {
class RealFSDirIter : public clang::vfs::detail::DirIterImpl {
  std::string Path;
  llvm::sys::fs::directory_iterator Iter;
public:
  RealFSDirIter(const Twine &_Path, std::error_code &EC)
      : Path(_Path.str()), Iter(Path, EC) {
    if (!EC && Iter != llvm::sys::fs::directory_iterator()) {
      llvm::sys::fs::file_status S;
      EC = Iter->status(S);
      if (!EC) {
        CurrentEntry = Status(S);
        CurrentEntry.setName(Iter->path());
      }
    }
  }

  std::error_code increment() override {
    std::error_code EC;
    Iter.increment(EC);
    if (EC) {
      return EC;
    } else if (Iter == llvm::sys::fs::directory_iterator()) {
      CurrentEntry = Status();
    } else {
      llvm::sys::fs::file_status S;
      EC = Iter->status(S);
      CurrentEntry = Status(S);
      CurrentEntry.setName(Iter->path());
    }
    return EC;
  }
};
}

directory_iterator RealFileSystem::dir_begin(const Twine &Dir,
                                             std::error_code &EC) {
  return directory_iterator(std::make_shared<RealFSDirIter>(Dir, EC));
}

//===-----------------------------------------------------------------------===/
// OverlayFileSystem implementation
//===-----------------------------------------------------------------------===/
OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
  pushOverlay(BaseFS);
}

void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
  FSList.push_back(FS);
}

ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
  // FIXME: handle symlinks that cross file systems
  for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
    ErrorOr<Status> Status = (*I)->status(Path);
    if (Status || Status.getError() != llvm::errc::no_such_file_or_directory)
      return Status;
  }
  return make_error_code(llvm::errc::no_such_file_or_directory);
}

std::error_code
OverlayFileSystem::openFileForRead(const llvm::Twine &Path,
                                   std::unique_ptr<File> &Result) {
  // FIXME: handle symlinks that cross file systems
  for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
    std::error_code EC = (*I)->openFileForRead(Path, Result);
    if (!EC || EC != llvm::errc::no_such_file_or_directory)
      return EC;
  }
  return make_error_code(llvm::errc::no_such_file_or_directory);
}

clang::vfs::detail::DirIterImpl::~DirIterImpl() { }

namespace {
class OverlayFSDirIterImpl : public clang::vfs::detail::DirIterImpl {
  OverlayFileSystem &Overlays;
  std::string Path;
  OverlayFileSystem::iterator CurrentFS;
  directory_iterator CurrentDirIter;
  llvm::StringSet<> SeenNames;

  std::error_code incrementFS() {
    assert(CurrentFS != Overlays.overlays_end() && "incrementing past end");
    ++CurrentFS;
    for (auto E = Overlays.overlays_end(); CurrentFS != E; ++CurrentFS) {
      std::error_code EC;
      CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
      if (EC && EC != errc::no_such_file_or_directory)
        return EC;
      if (CurrentDirIter != directory_iterator())
        break; // found
    }
    return std::error_code();
  }

  std::error_code incrementDirIter(bool IsFirstTime) {
    assert((IsFirstTime || CurrentDirIter != directory_iterator()) &&
           "incrementing past end");
    std::error_code EC;
    if (!IsFirstTime)
      CurrentDirIter.increment(EC);
    if (!EC && CurrentDirIter == directory_iterator())
      EC = incrementFS();
    return EC;
  }

  std::error_code incrementImpl(bool IsFirstTime) {
    while (true) {
      std::error_code EC = incrementDirIter(IsFirstTime);
      if (EC || CurrentDirIter == directory_iterator()) {
        CurrentEntry = Status();
        return EC;
      }
      CurrentEntry = *CurrentDirIter;
      StringRef Name = llvm::sys::path::filename(CurrentEntry.getName());
      if (SeenNames.insert(Name))
        return EC; // name not seen before
    }
    llvm_unreachable("returned above");
  }

public:
  OverlayFSDirIterImpl(const Twine &Path, OverlayFileSystem &FS,
                       std::error_code &EC)
      : Overlays(FS), Path(Path.str()), CurrentFS(Overlays.overlays_begin()) {
    CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
    EC = incrementImpl(true);
  }

  std::error_code increment() override { return incrementImpl(false); }
};
} // end anonymous namespace

directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir,
                                                std::error_code &EC) {
  return directory_iterator(
      std::make_shared<OverlayFSDirIterImpl>(Dir, *this, EC));
}

//===-----------------------------------------------------------------------===/
// VFSFromYAML implementation
//===-----------------------------------------------------------------------===/

// Allow DenseMap<StringRef, ...>.  This is useful below because we know all the
// strings are literals and will outlive the map, and there is no reason to
// store them.
namespace llvm {
  template<>
  struct DenseMapInfo<StringRef> {
    // This assumes that "" will never be a valid key.
    static inline StringRef getEmptyKey() { return StringRef(""); }
    static inline StringRef getTombstoneKey() { return StringRef(); }
    static unsigned getHashValue(StringRef Val) { return HashString(Val); }
    static bool isEqual(StringRef LHS, StringRef RHS) { return LHS == RHS; }
  };
}

namespace {

enum EntryKind {
  EK_Directory,
  EK_File
};

/// \brief A single file or directory in the VFS.
class Entry {
  EntryKind Kind;
  std::string Name;

public:
  virtual ~Entry();
  Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
  StringRef getName() const { return Name; }
  EntryKind getKind() const { return Kind; }
};

class DirectoryEntry : public Entry {
  std::vector<Entry *> Contents;
  Status S;

public:
  virtual ~DirectoryEntry();
  DirectoryEntry(StringRef Name, std::vector<Entry *> Contents, Status S)
      : Entry(EK_Directory, Name), Contents(std::move(Contents)),
        S(std::move(S)) {}
  Status getStatus() { return S; }
  typedef std::vector<Entry *>::iterator iterator;
  iterator contents_begin() { return Contents.begin(); }
  iterator contents_end() { return Contents.end(); }
  static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
};

class FileEntry : public Entry {
public:
  enum NameKind {
    NK_NotSet,
    NK_External,
    NK_Virtual
  };
private:
  std::string ExternalContentsPath;
  NameKind UseName;
public:
  FileEntry(StringRef Name, StringRef ExternalContentsPath, NameKind UseName)
      : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath),
        UseName(UseName) {}
  StringRef getExternalContentsPath() const { return ExternalContentsPath; }
  /// \brief whether to use the external path as the name for this file.
  bool useExternalName(bool GlobalUseExternalName) const {
    return UseName == NK_NotSet ? GlobalUseExternalName
                                : (UseName == NK_External);
  }
  static bool classof(const Entry *E) { return E->getKind() == EK_File; }
};

class VFSFromYAML;

class VFSFromYamlDirIterImpl : public clang::vfs::detail::DirIterImpl {
  std::string Dir;
  VFSFromYAML &FS;
  DirectoryEntry::iterator Current, End;
public:
  VFSFromYamlDirIterImpl(const Twine &Path, VFSFromYAML &FS,
                         DirectoryEntry::iterator Begin,
                         DirectoryEntry::iterator End, std::error_code &EC);
  std::error_code increment() override;
};

/// \brief A virtual file system parsed from a YAML file.
///
/// Currently, this class allows creating virtual directories and mapping
/// virtual file paths to existing external files, available in \c ExternalFS.
///
/// The basic structure of the parsed file is:
/// \verbatim
/// {
///   'version': <version number>,
///   <optional configuration>
///   'roots': [
///              <directory entries>
///            ]
/// }
/// \endverbatim
///
/// All configuration options are optional.
///   'case-sensitive': <boolean, default=true>
///   'use-external-names': <boolean, default=true>
///
/// Virtual directories are represented as
/// \verbatim
/// {
///   'type': 'directory',
///   'name': <string>,
///   'contents': [ <file or directory entries> ]
/// }
/// \endverbatim
///
/// The default attributes for virtual directories are:
/// \verbatim
/// MTime = now() when created
/// Perms = 0777
/// User = Group = 0
/// Size = 0
/// UniqueID = unspecified unique value
/// \endverbatim
///
/// Re-mapped files are represented as
/// \verbatim
/// {
///   'type': 'file',
///   'name': <string>,
///   'use-external-name': <boolean> # Optional
///   'external-contents': <path to external file>)
/// }
/// \endverbatim
///
/// and inherit their attributes from the external contents.
///
/// In both cases, the 'name' field may contain multiple path components (e.g.
/// /path/to/file). However, any directory that contains more than one child
/// must be uniquely represented by a directory entry.
class VFSFromYAML : public vfs::FileSystem {
  std::vector<Entry *> Roots; ///< The root(s) of the virtual file system.
  /// \brief The file system to use for external references.
  IntrusiveRefCntPtr<FileSystem> ExternalFS;

  /// @@name Configuration
  /// @@{

  /// \brief Whether to perform case-sensitive comparisons.
  ///
  /// Currently, case-insensitive matching only works correctly with ASCII.
  bool CaseSensitive;

  /// \brief Whether to use to use the value of 'external-contents' for the
  /// names of files.  This global value is overridable on a per-file basis.
  bool UseExternalNames;
  /// @@}

  friend class VFSFromYAMLParser;

private:
  VFSFromYAML(IntrusiveRefCntPtr<FileSystem> ExternalFS)
      : ExternalFS(ExternalFS), CaseSensitive(true), UseExternalNames(true) {}

  /// \brief Looks up \p Path in \c Roots.
  ErrorOr<Entry *> lookupPath(const Twine &Path);

  /// \brief Looks up the path <tt>[Start, End)</tt> in \p From, possibly
  /// recursing into the contents of \p From if it is a directory.
  ErrorOr<Entry *> lookupPath(sys::path::const_iterator Start,
                              sys::path::const_iterator End, Entry *From);

  /// \brief Get the status of a given an \c Entry.
  ErrorOr<Status> status(const Twine &Path, Entry *E);

public:
  ~VFSFromYAML();

  /// \brief Parses \p Buffer, which is expected to be in YAML format and
  /// returns a virtual file system representing its contents.
  ///
  /// Takes ownership of \p Buffer.
  static VFSFromYAML *create(MemoryBuffer *Buffer,
                             SourceMgr::DiagHandlerTy DiagHandler,
                             void *DiagContext,
                             IntrusiveRefCntPtr<FileSystem> ExternalFS);

  ErrorOr<Status> status(const Twine &Path) override;
  std::error_code openFileForRead(const Twine &Path,
                                  std::unique_ptr<File> &Result) override;

  directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override{
    ErrorOr<Entry *> E = lookupPath(Dir);
    if (!E) {
      EC = E.getError();
      return directory_iterator();
    }
    ErrorOr<Status> S = status(Dir, *E);
    if (!S) {
      EC = S.getError();
      return directory_iterator();
    }
    if (!S->isDirectory()) {
      EC = std::error_code(static_cast<int>(errc::not_a_directory),
                           std::system_category());
      return directory_iterator();
    }

    DirectoryEntry *D = cast<DirectoryEntry>(*E);
    return directory_iterator(std::make_shared<VFSFromYamlDirIterImpl>(Dir,
        *this, D->contents_begin(), D->contents_end(), EC));
  }
};

/// \brief A helper class to hold the common YAML parsing state.
class VFSFromYAMLParser {
  yaml::Stream &Stream;

  void error(yaml::Node *N, const Twine &Msg) {
    Stream.printError(N, Msg);
  }

  // false on error
  bool parseScalarString(yaml::Node *N, StringRef &Result,
                         SmallVectorImpl<char> &Storage) {
    yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N);
    if (!S) {
      error(N, "expected string");
      return false;
    }
    Result = S->getValue(Storage);
    return true;
  }

  // false on error
  bool parseScalarBool(yaml::Node *N, bool &Result) {
    SmallString<5> Storage;
    StringRef Value;
    if (!parseScalarString(N, Value, Storage))
      return false;

    if (Value.equals_lower("true") || Value.equals_lower("on") ||
        Value.equals_lower("yes") || Value == "1") {
      Result = true;
      return true;
    } else if (Value.equals_lower("false") || Value.equals_lower("off") ||
               Value.equals_lower("no") || Value == "0") {
      Result = false;
      return true;
    }

    error(N, "expected boolean value");
    return false;
  }

  struct KeyStatus {
    KeyStatus(bool Required=false) : Required(Required), Seen(false) {}
    bool Required;
    bool Seen;
  };
  typedef std::pair<StringRef, KeyStatus> KeyStatusPair;

  // false on error
  bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
                                  DenseMap<StringRef, KeyStatus> &Keys) {
    if (!Keys.count(Key)) {
      error(KeyNode, "unknown key");
      return false;
    }
    KeyStatus &S = Keys[Key];
    if (S.Seen) {
      error(KeyNode, Twine("duplicate key '") + Key + "'");
      return false;
    }
    S.Seen = true;
    return true;
  }

  // false on error
  bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
    for (DenseMap<StringRef, KeyStatus>::iterator I = Keys.begin(),
         E = Keys.end();
         I != E; ++I) {
      if (I->second.Required && !I->second.Seen) {
        error(Obj, Twine("missing key '") + I->first + "'");
        return false;
      }
    }
    return true;
  }

  Entry *parseEntry(yaml::Node *N) {
    yaml::MappingNode *M = dyn_cast<yaml::MappingNode>(N);
    if (!M) {
      error(N, "expected mapping node for file or directory entry");
      return nullptr;
    }

    KeyStatusPair Fields[] = {
      KeyStatusPair("name", true),
      KeyStatusPair("type", true),
      KeyStatusPair("contents", false),
      KeyStatusPair("external-contents", false),
      KeyStatusPair("use-external-name", false),
    };

    DenseMap<StringRef, KeyStatus> Keys(
        &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));

    bool HasContents = false; // external or otherwise
    std::vector<Entry *> EntryArrayContents;
    std::string ExternalContentsPath;
    std::string Name;
    FileEntry::NameKind UseExternalName = FileEntry::NK_NotSet;
    EntryKind Kind;

    for (yaml::MappingNode::iterator I = M->begin(), E = M->end(); I != E;
         ++I) {
      StringRef Key;
      // Reuse the buffer for key and value, since we don't look at key after
      // parsing value.
      SmallString<256> Buffer;
      if (!parseScalarString(I->getKey(), Key, Buffer))
        return nullptr;

      if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
        return nullptr;

      StringRef Value;
      if (Key == "name") {
        if (!parseScalarString(I->getValue(), Value, Buffer))
          return nullptr;
        Name = Value;
      } else if (Key == "type") {
        if (!parseScalarString(I->getValue(), Value, Buffer))
          return nullptr;
        if (Value == "file")
          Kind = EK_File;
        else if (Value == "directory")
          Kind = EK_Directory;
        else {
          error(I->getValue(), "unknown value for 'type'");
          return nullptr;
        }
      } else if (Key == "contents") {
        if (HasContents) {
          error(I->getKey(),
                "entry already has 'contents' or 'external-contents'");
          return nullptr;
        }
        HasContents = true;
        yaml::SequenceNode *Contents =
            dyn_cast<yaml::SequenceNode>(I->getValue());
        if (!Contents) {
          // FIXME: this is only for directories, what about files?
          error(I->getValue(), "expected array");
          return nullptr;
        }

        for (yaml::SequenceNode::iterator I = Contents->begin(),
                                          E = Contents->end();
             I != E; ++I) {
          if (Entry *E = parseEntry(&*I))
            EntryArrayContents.push_back(E);
          else
            return nullptr;
        }
      } else if (Key == "external-contents") {
        if (HasContents) {
          error(I->getKey(),
                "entry already has 'contents' or 'external-contents'");
          return nullptr;
        }
        HasContents = true;
        if (!parseScalarString(I->getValue(), Value, Buffer))
          return nullptr;
        ExternalContentsPath = Value;
      } else if (Key == "use-external-name") {
        bool Val;
        if (!parseScalarBool(I->getValue(), Val))
          return nullptr;
        UseExternalName = Val ? FileEntry::NK_External : FileEntry::NK_Virtual;
      } else {
        llvm_unreachable("key missing from Keys");
      }
    }

    if (Stream.failed())
      return nullptr;

    // check for missing keys
    if (!HasContents) {
      error(N, "missing key 'contents' or 'external-contents'");
      return nullptr;
    }
    if (!checkMissingKeys(N, Keys))
      return nullptr;

    // check invalid configuration
    if (Kind == EK_Directory && UseExternalName != FileEntry::NK_NotSet) {
      error(N, "'use-external-name' is not supported for directories");
      return nullptr;
    }

    // Remove trailing slash(es), being careful not to remove the root path
    StringRef Trimmed(Name);
    size_t RootPathLen = sys::path::root_path(Trimmed).size();
    while (Trimmed.size() > RootPathLen &&
           sys::path::is_separator(Trimmed.back()))
      Trimmed = Trimmed.slice(0, Trimmed.size()-1);
    // Get the last component
    StringRef LastComponent = sys::path::filename(Trimmed);

    Entry *Result = nullptr;
    switch (Kind) {
    case EK_File:
      Result = new FileEntry(LastComponent, std::move(ExternalContentsPath),
                             UseExternalName);
      break;
    case EK_Directory:
      Result = new DirectoryEntry(LastComponent, std::move(EntryArrayContents),
          Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
                 0, file_type::directory_file, sys::fs::all_all));
      break;
    }

    StringRef Parent = sys::path::parent_path(Trimmed);
    if (Parent.empty())
      return Result;

    // if 'name' contains multiple components, create implicit directory entries
    for (sys::path::reverse_iterator I = sys::path::rbegin(Parent),
                                     E = sys::path::rend(Parent);
         I != E; ++I) {
      Result = new DirectoryEntry(*I, llvm::makeArrayRef(Result),
          Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
                 0, file_type::directory_file, sys::fs::all_all));
    }
    return Result;
  }

public:
  VFSFromYAMLParser(yaml::Stream &S) : Stream(S) {}

  // false on error
  bool parse(yaml::Node *Root, VFSFromYAML *FS) {
    yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root);
    if (!Top) {
      error(Root, "expected mapping node");
      return false;
    }

    KeyStatusPair Fields[] = {
      KeyStatusPair("version", true),
      KeyStatusPair("case-sensitive", false),
      KeyStatusPair("use-external-names", false),
      KeyStatusPair("roots", true),
    };

    DenseMap<StringRef, KeyStatus> Keys(
        &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));

    // Parse configuration and 'roots'
    for (yaml::MappingNode::iterator I = Top->begin(), E = Top->end(); I != E;
         ++I) {
      SmallString<10> KeyBuffer;
      StringRef Key;
      if (!parseScalarString(I->getKey(), Key, KeyBuffer))
        return false;

      if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
        return false;

      if (Key == "roots") {
        yaml::SequenceNode *Roots = dyn_cast<yaml::SequenceNode>(I->getValue());
        if (!Roots) {
          error(I->getValue(), "expected array");
          return false;
        }

        for (yaml::SequenceNode::iterator I = Roots->begin(), E = Roots->end();
             I != E; ++I) {
          if (Entry *E = parseEntry(&*I))
            FS->Roots.push_back(E);
          else
            return false;
        }
      } else if (Key == "version") {
        StringRef VersionString;
        SmallString<4> Storage;
        if (!parseScalarString(I->getValue(), VersionString, Storage))
          return false;
        int Version;
        if (VersionString.getAsInteger<int>(10, Version)) {
          error(I->getValue(), "expected integer");
          return false;
        }
        if (Version < 0) {
          error(I->getValue(), "invalid version number");
          return false;
        }
        if (Version != 0) {
          error(I->getValue(), "version mismatch, expected 0");
          return false;
        }
      } else if (Key == "case-sensitive") {
        if (!parseScalarBool(I->getValue(), FS->CaseSensitive))
          return false;
      } else if (Key == "use-external-names") {
        if (!parseScalarBool(I->getValue(), FS->UseExternalNames))
          return false;
      } else {
        llvm_unreachable("key missing from Keys");
      }
    }

    if (Stream.failed())
      return false;

    if (!checkMissingKeys(Top, Keys))
      return false;
    return true;
  }
};
} // end of anonymous namespace

Entry::~Entry() {}
DirectoryEntry::~DirectoryEntry() { llvm::DeleteContainerPointers(Contents); }

VFSFromYAML::~VFSFromYAML() { llvm::DeleteContainerPointers(Roots); }

VFSFromYAML *VFSFromYAML::create(MemoryBuffer *Buffer,
                                 SourceMgr::DiagHandlerTy DiagHandler,
                                 void *DiagContext,
                                 IntrusiveRefCntPtr<FileSystem> ExternalFS) {

  SourceMgr SM;
  yaml::Stream Stream(Buffer, SM);

  SM.setDiagHandler(DiagHandler, DiagContext);
  yaml::document_iterator DI = Stream.begin();
  yaml::Node *Root = DI->getRoot();
  if (DI == Stream.end() || !Root) {
    SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
    return nullptr;
  }

  VFSFromYAMLParser P(Stream);

  std::unique_ptr<VFSFromYAML> FS(new VFSFromYAML(ExternalFS));
  if (!P.parse(Root, FS.get()))
    return nullptr;

  return FS.release();
}

ErrorOr<Entry *> VFSFromYAML::lookupPath(const Twine &Path_) {
  SmallString<256> Path;
  Path_.toVector(Path);

  // Handle relative paths
  if (std::error_code EC = sys::fs::make_absolute(Path))
    return EC;

  if (Path.empty())
    return make_error_code(llvm::errc::invalid_argument);

  sys::path::const_iterator Start = sys::path::begin(Path);
  sys::path::const_iterator End = sys::path::end(Path);
  for (std::vector<Entry *>::iterator I = Roots.begin(), E = Roots.end();
       I != E; ++I) {
    ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
    if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
      return Result;
  }
  return make_error_code(llvm::errc::no_such_file_or_directory);
}

ErrorOr<Entry *> VFSFromYAML::lookupPath(sys::path::const_iterator Start,
                                         sys::path::const_iterator End,
                                         Entry *From) {
  if (Start->equals("."))
    ++Start;

  // FIXME: handle ..
  if (CaseSensitive ? !Start->equals(From->getName())
                    : !Start->equals_lower(From->getName()))
    // failure to match
    return make_error_code(llvm::errc::no_such_file_or_directory);

  ++Start;

  if (Start == End) {
    // Match!
    return From;
  }

  DirectoryEntry *DE = dyn_cast<DirectoryEntry>(From);
  if (!DE)
    return make_error_code(llvm::errc::not_a_directory);

  for (DirectoryEntry::iterator I = DE->contents_begin(),
                                E = DE->contents_end();
       I != E; ++I) {
    ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
    if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
      return Result;
  }
  return make_error_code(llvm::errc::no_such_file_or_directory);
}

ErrorOr<Status> VFSFromYAML::status(const Twine &Path, Entry *E) {
  assert(E != nullptr);
  std::string PathStr(Path.str());
  if (FileEntry *F = dyn_cast<FileEntry>(E)) {
    ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath());
    assert(!S || S->getName() == F->getExternalContentsPath());
    if (S && !F->useExternalName(UseExternalNames))
      S->setName(PathStr);
    if (S)
      S->IsVFSMapped = true;
    return S;
  } else { // directory
    DirectoryEntry *DE = cast<DirectoryEntry>(E);
    Status S = DE->getStatus();
    S.setName(PathStr);
    return S;
  }
}

ErrorOr<Status> VFSFromYAML::status(const Twine &Path) {
  ErrorOr<Entry *> Result = lookupPath(Path);
  if (!Result)
    return Result.getError();
  return status(Path, *Result);
}

std::error_code
VFSFromYAML::openFileForRead(const Twine &Path,
                             std::unique_ptr<vfs::File> &Result) {
  ErrorOr<Entry *> E = lookupPath(Path);
  if (!E)
    return E.getError();

  FileEntry *F = dyn_cast<FileEntry>(*E);
  if (!F) // FIXME: errc::not_a_file?
    return make_error_code(llvm::errc::invalid_argument);

  if (std::error_code EC =
          ExternalFS->openFileForRead(F->getExternalContentsPath(), Result))
    return EC;

  if (!F->useExternalName(UseExternalNames))
    Result->setName(Path.str());

  return std::error_code();
}

IntrusiveRefCntPtr<FileSystem>
vfs::getVFSFromYAML(MemoryBuffer *Buffer, SourceMgr::DiagHandlerTy DiagHandler,
                    void *DiagContext,
                    IntrusiveRefCntPtr<FileSystem> ExternalFS) {
  return VFSFromYAML::create(Buffer, DiagHandler, DiagContext, ExternalFS);
}

UniqueID vfs::getNextVirtualUniqueID() {
  static std::atomic<unsigned> UID;
  unsigned ID = ++UID;
  // The following assumes that uint64_t max will never collide with a real
  // dev_t value from the OS.
  return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
}

#ifndef NDEBUG
static bool pathHasTraversal(StringRef Path) {
  using namespace llvm::sys;
  for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path)))
    if (Comp == "." || Comp == "..")
      return true;
  return false;
}
#endif

void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) {
  assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute");
  assert(sys::path::is_absolute(RealPath) && "real path not absolute");
  assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported");
  Mappings.emplace_back(VirtualPath, RealPath);
}

namespace {
class JSONWriter {
  llvm::raw_ostream &OS;
  SmallVector<StringRef, 16> DirStack;
  inline unsigned getDirIndent() { return 4 * DirStack.size(); }
  inline unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
  bool containedIn(StringRef Parent, StringRef Path);
  StringRef containedPart(StringRef Parent, StringRef Path);
  void startDirectory(StringRef Path);
  void endDirectory();
  void writeEntry(StringRef VPath, StringRef RPath);

public:
  JSONWriter(llvm::raw_ostream &OS) : OS(OS) {}
  void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> IsCaseSensitive);
};
}

bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
  using namespace llvm::sys;
  // Compare each path component.
  auto IParent = path::begin(Parent), EParent = path::end(Parent);
  for (auto IChild = path::begin(Path), EChild = path::end(Path);
       IParent != EParent && IChild != EChild; ++IParent, ++IChild) {
    if (*IParent != *IChild)
      return false;
  }
  // Have we exhausted the parent path?
  return IParent == EParent;
}

StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
  assert(!Parent.empty());
  assert(containedIn(Parent, Path));
  return Path.slice(Parent.size() + 1, StringRef::npos);
}

void JSONWriter::startDirectory(StringRef Path) {
  StringRef Name =
      DirStack.empty() ? Path : containedPart(DirStack.back(), Path);
  DirStack.push_back(Path);
  unsigned Indent = getDirIndent();
  OS.indent(Indent) << "{\n";
  OS.indent(Indent + 2) << "'type': 'directory',\n";
  OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n";
  OS.indent(Indent + 2) << "'contents': [\n";
}

void JSONWriter::endDirectory() {
  unsigned Indent = getDirIndent();
  OS.indent(Indent + 2) << "]\n";
  OS.indent(Indent) << "}";

  DirStack.pop_back();
}

void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) {
  unsigned Indent = getFileIndent();
  OS.indent(Indent) << "{\n";
  OS.indent(Indent + 2) << "'type': 'file',\n";
  OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n";
  OS.indent(Indent + 2) << "'external-contents': \""
                        << llvm::yaml::escape(RPath) << "\"\n";
  OS.indent(Indent) << "}";
}

void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
                       Optional<bool> IsCaseSensitive) {
  using namespace llvm::sys;

  OS << "{\n"
        "  'version': 0,\n";
  if (IsCaseSensitive.hasValue())
    OS << "  'case-sensitive': '"
       << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n";
  OS << "  'roots': [\n";

  if (!Entries.empty()) {
    const YAMLVFSEntry &Entry = Entries.front();
    startDirectory(path::parent_path(Entry.VPath));
    writeEntry(path::filename(Entry.VPath), Entry.RPath);

    for (const auto &Entry : Entries.slice(1)) {
      StringRef Dir = path::parent_path(Entry.VPath);
      if (Dir == DirStack.back())
        OS << ",\n";
      else {
        while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) {
          OS << "\n";
          endDirectory();
        }
        OS << ",\n";
        startDirectory(Dir);
      }
      writeEntry(path::filename(Entry.VPath), Entry.RPath);
    }

    while (!DirStack.empty()) {
      OS << "\n";
      endDirectory();
    }
    OS << "\n";
  }

  OS << "  ]\n"
     << "}\n";
}

void YAMLVFSWriter::write(llvm::raw_ostream &OS) {
  std::sort(Mappings.begin(), Mappings.end(),
            [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
    return LHS.VPath < RHS.VPath;
  });

  JSONWriter(OS).write(Mappings, IsCaseSensitive);
}

VFSFromYamlDirIterImpl::VFSFromYamlDirIterImpl(const Twine &_Path,
                                               VFSFromYAML &FS,
                                               DirectoryEntry::iterator Begin,
                                               DirectoryEntry::iterator End,
                                               std::error_code &EC)
    : Dir(_Path.str()), FS(FS), Current(Begin), End(End) {
  if (Current != End) {
    SmallString<128> PathStr(Dir);
    llvm::sys::path::append(PathStr, (*Current)->getName());
    llvm::ErrorOr<vfs::Status> S = FS.status(PathStr.str());
    if (S)
      CurrentEntry = *S;
    else
      EC = S.getError();
  }
}

std::error_code VFSFromYamlDirIterImpl::increment() {
  assert(Current != End && "cannot iterate past end");
  if (++Current != End) {
    SmallString<128> PathStr(Dir);
    llvm::sys::path::append(PathStr, (*Current)->getName());
    llvm::ErrorOr<vfs::Status> S = FS.status(PathStr.str());
    if (!S)
      return S.getError();
    CurrentEntry = *S;
  } else {
    CurrentEntry = Status();
  }
  return std::error_code();
}

vfs::recursive_directory_iterator::recursive_directory_iterator(FileSystem &FS_,
                                                           const Twine &Path,
                                                           std::error_code &EC)
    : FS(&FS_) {
  directory_iterator I = FS->dir_begin(Path, EC);
  if (!EC && I != directory_iterator()) {
    State = std::make_shared<IterState>();
    State->push(I);
  }
}

vfs::recursive_directory_iterator &
recursive_directory_iterator::increment(std::error_code &EC) {
  assert(FS && State && !State->empty() && "incrementing past end");
  assert(State->top()->isStatusKnown() && "non-canonical end iterator");
  vfs::directory_iterator End;
  if (State->top()->isDirectory()) {
    vfs::directory_iterator I = FS->dir_begin(State->top()->getName(), EC);
    if (EC)
      return *this;
    if (I != End) {
      State->push(I);
      return *this;
    }
  }

  while (!State->empty() && State->top().increment(EC) == End)
    State->pop();

  if (State->empty())
    State.reset(); // end iterator

  return *this;
}
@


1.1.1.1.4.1
log
@file VirtualFileSystem.cpp was added on branch yamt-pagecache on 2014-05-22 16:18:26 +0000
@
text
@d1 832
@


1.1.1.1.4.2
log
@sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs.  ("Protocol error: too many arguments")
@
text
@a0 832
//===- VirtualFileSystem.cpp - Virtual File System Layer --------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// This file implements the VirtualFileSystem interface.
//===----------------------------------------------------------------------===//

#include "clang/Basic/VirtualFileSystem.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Atomic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/YAMLParser.h"

using namespace clang;
using namespace clang::vfs;
using namespace llvm;
using llvm::sys::fs::file_status;
using llvm::sys::fs::file_type;
using llvm::sys::fs::perms;
using llvm::sys::fs::UniqueID;

Status::Status(const file_status &Status)
    : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
      User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
      Type(Status.type()), Perms(Status.permissions()) {}

Status::Status(StringRef Name, StringRef ExternalName, UniqueID UID,
               sys::TimeValue MTime, uint32_t User, uint32_t Group,
               uint64_t Size, file_type Type, perms Perms)
    : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size),
      Type(Type), Perms(Perms) {}

bool Status::equivalent(const Status &Other) const {
  return getUniqueID() == Other.getUniqueID();
}
bool Status::isDirectory() const {
  return Type == file_type::directory_file;
}
bool Status::isRegularFile() const {
  return Type == file_type::regular_file;
}
bool Status::isOther() const {
  return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
}
bool Status::isSymlink() const {
  return Type == file_type::symlink_file;
}
bool Status::isStatusKnown() const {
  return Type != file_type::status_error;
}
bool Status::exists() const {
  return isStatusKnown() && Type != file_type::file_not_found;
}

File::~File() {}

FileSystem::~FileSystem() {}

error_code FileSystem::getBufferForFile(const llvm::Twine &Name,
                                        OwningPtr<MemoryBuffer> &Result,
                                        int64_t FileSize,
                                        bool RequiresNullTerminator) {
  llvm::OwningPtr<File> F;
  if (error_code EC = openFileForRead(Name, F))
    return EC;

  error_code EC = F->getBuffer(Name, Result, FileSize, RequiresNullTerminator);
  return EC;
}

//===-----------------------------------------------------------------------===/
// RealFileSystem implementation
//===-----------------------------------------------------------------------===/

/// \brief Wrapper around a raw file descriptor.
class RealFile : public File {
  int FD;
  Status S;
  friend class RealFileSystem;
  RealFile(int FD) : FD(FD) {
    assert(FD >= 0 && "Invalid or inactive file descriptor");
  }

public:
  ~RealFile();
  ErrorOr<Status> status() LLVM_OVERRIDE;
  error_code getBuffer(const Twine &Name, OwningPtr<MemoryBuffer> &Result,
                       int64_t FileSize = -1,
                       bool RequiresNullTerminator = true) LLVM_OVERRIDE;
  error_code close() LLVM_OVERRIDE;
  void setName(StringRef Name) LLVM_OVERRIDE;
};
RealFile::~RealFile() { close(); }

ErrorOr<Status> RealFile::status() {
  assert(FD != -1 && "cannot stat closed file");
  if (!S.isStatusKnown()) {
    file_status RealStatus;
    if (error_code EC = sys::fs::status(FD, RealStatus))
      return EC;
    Status NewS(RealStatus);
    NewS.setName(S.getName());
    S = llvm_move(NewS);
  }
  return S;
}

error_code RealFile::getBuffer(const Twine &Name,
                               OwningPtr<MemoryBuffer> &Result,
                               int64_t FileSize, bool RequiresNullTerminator) {
  assert(FD != -1 && "cannot get buffer for closed file");
  return MemoryBuffer::getOpenFile(FD, Name.str().c_str(), Result, FileSize,
                                   RequiresNullTerminator);
}

// FIXME: This is terrible, we need this for ::close.
#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <unistd.h>
#include <sys/uio.h>
#else
#include <io.h>
#ifndef S_ISFIFO
#define S_ISFIFO(x) (0)
#endif
#endif
error_code RealFile::close() {
  if (::close(FD))
    return error_code(errno, system_category());
  FD = -1;
  return error_code::success();
}

void RealFile::setName(StringRef Name) {
  S.setName(Name);
}

/// \brief The file system according to your operating system.
class RealFileSystem : public FileSystem {
public:
  ErrorOr<Status> status(const Twine &Path) LLVM_OVERRIDE;
  error_code openFileForRead(const Twine &Path,
                             OwningPtr<File> &Result) LLVM_OVERRIDE;
};

ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
  sys::fs::file_status RealStatus;
  if (error_code EC = sys::fs::status(Path, RealStatus))
    return EC;
  Status Result(RealStatus);
  Result.setName(Path.str());
  return Result;
}

error_code RealFileSystem::openFileForRead(const Twine &Name,
                                           OwningPtr<File> &Result) {
  int FD;
  if (error_code EC = sys::fs::openFileForRead(Name, FD))
    return EC;
  Result.reset(new RealFile(FD));
  Result->setName(Name.str());
  return error_code::success();
}

IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
  static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem();
  return FS;
}

//===-----------------------------------------------------------------------===/
// OverlayFileSystem implementation
//===-----------------------------------------------------------------------===/
OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
  pushOverlay(BaseFS);
}

void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
  FSList.push_back(FS);
}

ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
  // FIXME: handle symlinks that cross file systems
  for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
    ErrorOr<Status> Status = (*I)->status(Path);
    if (Status || Status.getError() != errc::no_such_file_or_directory)
      return Status;
  }
  return error_code(errc::no_such_file_or_directory, system_category());
}

error_code OverlayFileSystem::openFileForRead(const llvm::Twine &Path,
                                              OwningPtr<File> &Result) {
  // FIXME: handle symlinks that cross file systems
  for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
    error_code EC = (*I)->openFileForRead(Path, Result);
    if (!EC || EC != errc::no_such_file_or_directory)
      return EC;
  }
  return error_code(errc::no_such_file_or_directory, system_category());
}

//===-----------------------------------------------------------------------===/
// VFSFromYAML implementation
//===-----------------------------------------------------------------------===/

// Allow DenseMap<StringRef, ...>.  This is useful below because we know all the
// strings are literals and will outlive the map, and there is no reason to
// store them.
namespace llvm {
  template<>
  struct DenseMapInfo<StringRef> {
    // This assumes that "" will never be a valid key.
    static inline StringRef getEmptyKey() { return StringRef(""); }
    static inline StringRef getTombstoneKey() { return StringRef(); }
    static unsigned getHashValue(StringRef Val) { return HashString(Val); }
    static bool isEqual(StringRef LHS, StringRef RHS) { return LHS == RHS; }
  };
}

namespace {

enum EntryKind {
  EK_Directory,
  EK_File
};

/// \brief A single file or directory in the VFS.
class Entry {
  EntryKind Kind;
  std::string Name;

public:
  virtual ~Entry();
  Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
  StringRef getName() const { return Name; }
  EntryKind getKind() const { return Kind; }
};

class DirectoryEntry : public Entry {
  std::vector<Entry *> Contents;
  Status S;

public:
  virtual ~DirectoryEntry();
#if LLVM_HAS_RVALUE_REFERENCES
  DirectoryEntry(StringRef Name, std::vector<Entry *> Contents, Status S)
      : Entry(EK_Directory, Name), Contents(std::move(Contents)),
        S(std::move(S)) {}
#endif
  DirectoryEntry(StringRef Name, ArrayRef<Entry *> Contents, const Status &S)
      : Entry(EK_Directory, Name), Contents(Contents), S(S) {}
  Status getStatus() { return S; }
  typedef std::vector<Entry *>::iterator iterator;
  iterator contents_begin() { return Contents.begin(); }
  iterator contents_end() { return Contents.end(); }
  static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
};

class FileEntry : public Entry {
public:
  enum NameKind {
    NK_NotSet,
    NK_External,
    NK_Virtual
  };
private:
  std::string ExternalContentsPath;
  NameKind UseName;
public:
  FileEntry(StringRef Name, StringRef ExternalContentsPath, NameKind UseName)
      : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath),
        UseName(UseName) {}
  StringRef getExternalContentsPath() const { return ExternalContentsPath; }
  /// \brief whether to use the external path as the name for this file.
  bool useExternalName(bool GlobalUseExternalName) const {
    return UseName == NK_NotSet ? GlobalUseExternalName
                                : (UseName == NK_External);
  }
  static bool classof(const Entry *E) { return E->getKind() == EK_File; }
};

/// \brief A virtual file system parsed from a YAML file.
///
/// Currently, this class allows creating virtual directories and mapping
/// virtual file paths to existing external files, available in \c ExternalFS.
///
/// The basic structure of the parsed file is:
/// \verbatim
/// {
///   'version': <version number>,
///   <optional configuration>
///   'roots': [
///              <directory entries>
///            ]
/// }
/// \endverbatim
///
/// All configuration options are optional.
///   'case-sensitive': <boolean, default=true>
///   'use-external-names': <boolean, default=true>
///
/// Virtual directories are represented as
/// \verbatim
/// {
///   'type': 'directory',
///   'name': <string>,
///   'contents': [ <file or directory entries> ]
/// }
/// \endverbatim
///
/// The default attributes for virtual directories are:
/// \verbatim
/// MTime = now() when created
/// Perms = 0777
/// User = Group = 0
/// Size = 0
/// UniqueID = unspecified unique value
/// \endverbatim
///
/// Re-mapped files are represented as
/// \verbatim
/// {
///   'type': 'file',
///   'name': <string>,
///   'use-external-name': <boolean> # Optional
///   'external-contents': <path to external file>)
/// }
/// \endverbatim
///
/// and inherit their attributes from the external contents.
///
/// In both cases, the 'name' field may contain multiple path components (e.g.
/// /path/to/file). However, any directory that contains more than one child
/// must be uniquely represented by a directory entry.
class VFSFromYAML : public vfs::FileSystem {
  std::vector<Entry *> Roots; ///< The root(s) of the virtual file system.
  /// \brief The file system to use for external references.
  IntrusiveRefCntPtr<FileSystem> ExternalFS;

  /// @@name Configuration
  /// @@{

  /// \brief Whether to perform case-sensitive comparisons.
  ///
  /// Currently, case-insensitive matching only works correctly with ASCII.
  bool CaseSensitive;

  /// \brief Whether to use to use the value of 'external-contents' for the
  /// names of files.  This global value is overridable on a per-file basis.
  bool UseExternalNames;
  /// @@}

  friend class VFSFromYAMLParser;

private:
  VFSFromYAML(IntrusiveRefCntPtr<FileSystem> ExternalFS)
      : ExternalFS(ExternalFS), CaseSensitive(true), UseExternalNames(true) {}

  /// \brief Looks up \p Path in \c Roots.
  ErrorOr<Entry *> lookupPath(const Twine &Path);

  /// \brief Looks up the path <tt>[Start, End)</tt> in \p From, possibly
  /// recursing into the contents of \p From if it is a directory.
  ErrorOr<Entry *> lookupPath(sys::path::const_iterator Start,
                              sys::path::const_iterator End, Entry *From);

public:
  ~VFSFromYAML();

  /// \brief Parses \p Buffer, which is expected to be in YAML format and
  /// returns a virtual file system representing its contents.
  ///
  /// Takes ownership of \p Buffer.
  static VFSFromYAML *create(MemoryBuffer *Buffer,
                             SourceMgr::DiagHandlerTy DiagHandler,
                             void *DiagContext,
                             IntrusiveRefCntPtr<FileSystem> ExternalFS);

  ErrorOr<Status> status(const Twine &Path) LLVM_OVERRIDE;
  error_code openFileForRead(const Twine &Path,
                             OwningPtr<File> &Result) LLVM_OVERRIDE;
};

/// \brief A helper class to hold the common YAML parsing state.
class VFSFromYAMLParser {
  yaml::Stream &Stream;

  void error(yaml::Node *N, const Twine &Msg) {
    Stream.printError(N, Msg);
  }

  // false on error
  bool parseScalarString(yaml::Node *N, StringRef &Result,
                         SmallVectorImpl<char> &Storage) {
    yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N);
    if (!S) {
      error(N, "expected string");
      return false;
    }
    Result = S->getValue(Storage);
    return true;
  }

  // false on error
  bool parseScalarBool(yaml::Node *N, bool &Result) {
    SmallString<5> Storage;
    StringRef Value;
    if (!parseScalarString(N, Value, Storage))
      return false;

    if (Value.equals_lower("true") || Value.equals_lower("on") ||
        Value.equals_lower("yes") || Value == "1") {
      Result = true;
      return true;
    } else if (Value.equals_lower("false") || Value.equals_lower("off") ||
               Value.equals_lower("no") || Value == "0") {
      Result = false;
      return true;
    }

    error(N, "expected boolean value");
    return false;
  }

  struct KeyStatus {
    KeyStatus(bool Required=false) : Required(Required), Seen(false) {}
    bool Required;
    bool Seen;
  };
  typedef std::pair<StringRef, KeyStatus> KeyStatusPair;

  // false on error
  bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
                                  DenseMap<StringRef, KeyStatus> &Keys) {
    if (!Keys.count(Key)) {
      error(KeyNode, "unknown key");
      return false;
    }
    KeyStatus &S = Keys[Key];
    if (S.Seen) {
      error(KeyNode, Twine("duplicate key '") + Key + "'");
      return false;
    }
    S.Seen = true;
    return true;
  }

  // false on error
  bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
    for (DenseMap<StringRef, KeyStatus>::iterator I = Keys.begin(),
         E = Keys.end();
         I != E; ++I) {
      if (I->second.Required && !I->second.Seen) {
        error(Obj, Twine("missing key '") + I->first + "'");
        return false;
      }
    }
    return true;
  }

  Entry *parseEntry(yaml::Node *N) {
    yaml::MappingNode *M = dyn_cast<yaml::MappingNode>(N);
    if (!M) {
      error(N, "expected mapping node for file or directory entry");
      return NULL;
    }

    KeyStatusPair Fields[] = {
      KeyStatusPair("name", true),
      KeyStatusPair("type", true),
      KeyStatusPair("contents", false),
      KeyStatusPair("external-contents", false),
      KeyStatusPair("use-external-name", false),
    };

    DenseMap<StringRef, KeyStatus> Keys(
        &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));

    bool HasContents = false; // external or otherwise
    std::vector<Entry *> EntryArrayContents;
    std::string ExternalContentsPath;
    std::string Name;
    FileEntry::NameKind UseExternalName = FileEntry::NK_NotSet;
    EntryKind Kind;

    for (yaml::MappingNode::iterator I = M->begin(), E = M->end(); I != E;
         ++I) {
      StringRef Key;
      // Reuse the buffer for key and value, since we don't look at key after
      // parsing value.
      SmallString<256> Buffer;
      if (!parseScalarString(I->getKey(), Key, Buffer))
        return NULL;

      if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
        return NULL;

      StringRef Value;
      if (Key == "name") {
        if (!parseScalarString(I->getValue(), Value, Buffer))
          return NULL;
        Name = Value;
      } else if (Key == "type") {
        if (!parseScalarString(I->getValue(), Value, Buffer))
          return NULL;
        if (Value == "file")
          Kind = EK_File;
        else if (Value == "directory")
          Kind = EK_Directory;
        else {
          error(I->getValue(), "unknown value for 'type'");
          return NULL;
        }
      } else if (Key == "contents") {
        if (HasContents) {
          error(I->getKey(),
                "entry already has 'contents' or 'external-contents'");
          return NULL;
        }
        HasContents = true;
        yaml::SequenceNode *Contents =
            dyn_cast<yaml::SequenceNode>(I->getValue());
        if (!Contents) {
          // FIXME: this is only for directories, what about files?
          error(I->getValue(), "expected array");
          return NULL;
        }

        for (yaml::SequenceNode::iterator I = Contents->begin(),
                                          E = Contents->end();
             I != E; ++I) {
          if (Entry *E = parseEntry(&*I))
            EntryArrayContents.push_back(E);
          else
            return NULL;
        }
      } else if (Key == "external-contents") {
        if (HasContents) {
          error(I->getKey(),
                "entry already has 'contents' or 'external-contents'");
          return NULL;
        }
        HasContents = true;
        if (!parseScalarString(I->getValue(), Value, Buffer))
          return NULL;
        ExternalContentsPath = Value;
      } else if (Key == "use-external-name") {
        bool Val;
        if (!parseScalarBool(I->getValue(), Val))
          return NULL;
        UseExternalName = Val ? FileEntry::NK_External : FileEntry::NK_Virtual;
      } else {
        llvm_unreachable("key missing from Keys");
      }
    }

    if (Stream.failed())
      return NULL;

    // check for missing keys
    if (!HasContents) {
      error(N, "missing key 'contents' or 'external-contents'");
      return NULL;
    }
    if (!checkMissingKeys(N, Keys))
      return NULL;

    // check invalid configuration
    if (Kind == EK_Directory && UseExternalName != FileEntry::NK_NotSet) {
      error(N, "'use-external-name' is not supported for directories");
      return NULL;
    }

    // Remove trailing slash(es)
    StringRef Trimmed(Name);
    while (Trimmed.size() > 1 && sys::path::is_separator(Trimmed.back()))
      Trimmed = Trimmed.slice(0, Trimmed.size()-1);
    // Get the last component
    StringRef LastComponent = sys::path::filename(Trimmed);

    Entry *Result = 0;
    switch (Kind) {
    case EK_File:
      Result = new FileEntry(LastComponent, llvm_move(ExternalContentsPath),
                             UseExternalName);
      break;
    case EK_Directory:
      Result = new DirectoryEntry(LastComponent, llvm_move(EntryArrayContents),
          Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
                 0, file_type::directory_file, sys::fs::all_all));
      break;
    }

    StringRef Parent = sys::path::parent_path(Trimmed);
    if (Parent.empty())
      return Result;

    // if 'name' contains multiple components, create implicit directory entries
    for (sys::path::reverse_iterator I = sys::path::rbegin(Parent),
                                     E = sys::path::rend(Parent);
         I != E; ++I) {
      Result = new DirectoryEntry(*I, Result,
          Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
                 0, file_type::directory_file, sys::fs::all_all));
    }
    return Result;
  }

public:
  VFSFromYAMLParser(yaml::Stream &S) : Stream(S) {}

  // false on error
  bool parse(yaml::Node *Root, VFSFromYAML *FS) {
    yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root);
    if (!Top) {
      error(Root, "expected mapping node");
      return false;
    }

    KeyStatusPair Fields[] = {
      KeyStatusPair("version", true),
      KeyStatusPair("case-sensitive", false),
      KeyStatusPair("use-external-names", false),
      KeyStatusPair("roots", true),
    };

    DenseMap<StringRef, KeyStatus> Keys(
        &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));

    // Parse configuration and 'roots'
    for (yaml::MappingNode::iterator I = Top->begin(), E = Top->end(); I != E;
         ++I) {
      SmallString<10> KeyBuffer;
      StringRef Key;
      if (!parseScalarString(I->getKey(), Key, KeyBuffer))
        return false;

      if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
        return false;

      if (Key == "roots") {
        yaml::SequenceNode *Roots = dyn_cast<yaml::SequenceNode>(I->getValue());
        if (!Roots) {
          error(I->getValue(), "expected array");
          return false;
        }

        for (yaml::SequenceNode::iterator I = Roots->begin(), E = Roots->end();
             I != E; ++I) {
          if (Entry *E = parseEntry(&*I))
            FS->Roots.push_back(E);
          else
            return false;
        }
      } else if (Key == "version") {
        StringRef VersionString;
        SmallString<4> Storage;
        if (!parseScalarString(I->getValue(), VersionString, Storage))
          return false;
        int Version;
        if (VersionString.getAsInteger<int>(10, Version)) {
          error(I->getValue(), "expected integer");
          return false;
        }
        if (Version < 0) {
          error(I->getValue(), "invalid version number");
          return false;
        }
        if (Version != 0) {
          error(I->getValue(), "version mismatch, expected 0");
          return false;
        }
      } else if (Key == "case-sensitive") {
        if (!parseScalarBool(I->getValue(), FS->CaseSensitive))
          return false;
      } else if (Key == "use-external-names") {
        if (!parseScalarBool(I->getValue(), FS->UseExternalNames))
          return false;
      } else {
        llvm_unreachable("key missing from Keys");
      }
    }

    if (Stream.failed())
      return false;

    if (!checkMissingKeys(Top, Keys))
      return false;
    return true;
  }
};
} // end of anonymous namespace

Entry::~Entry() {}
DirectoryEntry::~DirectoryEntry() { llvm::DeleteContainerPointers(Contents); }

VFSFromYAML::~VFSFromYAML() { llvm::DeleteContainerPointers(Roots); }

VFSFromYAML *VFSFromYAML::create(MemoryBuffer *Buffer,
                                 SourceMgr::DiagHandlerTy DiagHandler,
                                 void *DiagContext,
                                 IntrusiveRefCntPtr<FileSystem> ExternalFS) {

  SourceMgr SM;
  yaml::Stream Stream(Buffer, SM);

  SM.setDiagHandler(DiagHandler, DiagContext);
  yaml::document_iterator DI = Stream.begin();
  yaml::Node *Root = DI->getRoot();
  if (DI == Stream.end() || !Root) {
    SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
    return NULL;
  }

  VFSFromYAMLParser P(Stream);

  OwningPtr<VFSFromYAML> FS(new VFSFromYAML(ExternalFS));
  if (!P.parse(Root, FS.get()))
    return NULL;

  return FS.take();
}

ErrorOr<Entry *> VFSFromYAML::lookupPath(const Twine &Path_) {
  SmallVector<char, 256> Storage;
  StringRef Path = Path_.toNullTerminatedStringRef(Storage);

  if (Path.empty())
    return error_code(errc::invalid_argument, system_category());

  sys::path::const_iterator Start = sys::path::begin(Path);
  sys::path::const_iterator End = sys::path::end(Path);
  for (std::vector<Entry *>::iterator I = Roots.begin(), E = Roots.end();
       I != E; ++I) {
    ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
    if (Result || Result.getError() != errc::no_such_file_or_directory)
      return Result;
  }
  return error_code(errc::no_such_file_or_directory, system_category());
}

ErrorOr<Entry *> VFSFromYAML::lookupPath(sys::path::const_iterator Start,
                                         sys::path::const_iterator End,
                                         Entry *From) {
  // FIXME: handle . and ..
  if (CaseSensitive ? !Start->equals(From->getName())
                    : !Start->equals_lower(From->getName()))
    // failure to match
    return error_code(errc::no_such_file_or_directory, system_category());

  ++Start;

  if (Start == End) {
    // Match!
    return From;
  }

  DirectoryEntry *DE = dyn_cast<DirectoryEntry>(From);
  if (!DE)
    return error_code(errc::not_a_directory, system_category());

  for (DirectoryEntry::iterator I = DE->contents_begin(),
                                E = DE->contents_end();
       I != E; ++I) {
    ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
    if (Result || Result.getError() != errc::no_such_file_or_directory)
      return Result;
  }
  return error_code(errc::no_such_file_or_directory, system_category());
}

ErrorOr<Status> VFSFromYAML::status(const Twine &Path) {
  ErrorOr<Entry *> Result = lookupPath(Path);
  if (!Result)
    return Result.getError();

  std::string PathStr(Path.str());
  if (FileEntry *F = dyn_cast<FileEntry>(*Result)) {
    ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath());
    assert(!S || S->getName() == F->getExternalContentsPath());
    if (S && !F->useExternalName(UseExternalNames))
      S->setName(PathStr);
    return S;
  } else { // directory
    DirectoryEntry *DE = cast<DirectoryEntry>(*Result);
    Status S = DE->getStatus();
    S.setName(PathStr);
    return S;
  }
}

error_code VFSFromYAML::openFileForRead(const Twine &Path,
                                        OwningPtr<vfs::File> &Result) {
  ErrorOr<Entry *> E = lookupPath(Path);
  if (!E)
    return E.getError();

  FileEntry *F = dyn_cast<FileEntry>(*E);
  if (!F) // FIXME: errc::not_a_file?
    return error_code(errc::invalid_argument, system_category());

  if (error_code EC = ExternalFS->openFileForRead(F->getExternalContentsPath(),
                                                  Result))
    return EC;

  if (!F->useExternalName(UseExternalNames))
    Result->setName(Path.str());

  return error_code::success();
}

IntrusiveRefCntPtr<FileSystem>
vfs::getVFSFromYAML(MemoryBuffer *Buffer, SourceMgr::DiagHandlerTy DiagHandler,
                    void *DiagContext,
                    IntrusiveRefCntPtr<FileSystem> ExternalFS) {
  return VFSFromYAML::create(Buffer, DiagHandler, DiagContext, ExternalFS);
}

UniqueID vfs::getNextVirtualUniqueID() {
  static volatile sys::cas_flag UID = 0;
  sys::cas_flag ID = llvm::sys::AtomicIncrement(&UID);
  // The following assumes that uint64_t max will never collide with a real
  // dev_t value from the OS.
  return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
}
@


