class FileManager

Declaration

class FileManager : public RefCountedBase { /* full declaration omitted */ };

Description

Implements support for file system lookup, file system caching, and directory search management. This also handles more advanced properties, such as uniquing files based on "inode", so that a file with two names (e.g. symlinked) will be treated as a single file.

Declared at: clang/include/clang/Basic/FileManager.h:53

Inherits from: RefCountedBase

Member Variables

private IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS
private clang::FileSystemOptions FileSystemOpts
private llvm::SpecificBumpPtrAllocator<FileEntry> FilesAlloc
private llvm::SpecificBumpPtrAllocator<DirectoryEntry> DirsAlloc
private llvm::DenseMap<llvm::sys::fs::UniqueID, DirectoryEntry*> UniqueRealDirs
Cache for existing real directories.
private llvm::DenseMap<llvm::sys::fs::UniqueID, FileEntry*> UniqueRealFiles
Cache for existing real files.
private SmallVector<clang::DirectoryEntry*, 4> VirtualDirectoryEntries
For each virtual file (e.g. foo/bar/baz.cpp), we add all of its parent directories (foo/ and foo/bar/) here.
private SmallVector<clang::FileEntry*, 4> VirtualFileEntries
The virtual files that we have allocated.
private SmallVector<clang::FileEntry*, 0> BypassFileEntries
A set of files that bypass the maps and uniquing. They can have conflicting filenames.
private llvm::StringMap<llvm::ErrorOr<DirectoryEntry&>, llvm::BumpPtrAllocator> SeenDirEntries
The actual Entries for real directories/files are owned by UniqueRealDirs/UniqueRealFiles above, while the Entries for virtual directories/files are owned by VirtualDirectoryEntries/VirtualFileEntries above.
private llvm::StringMap< llvm::ErrorOr<FileEntryRef::MapValue>, llvm::BumpPtrAllocator> SeenFileEntries
private std::unique_ptr<llvm::StringMap< llvm::ErrorOr<FileEntryRef::MapValue>>> SeenBypassFileEntries
Don't bother hooking up a BumpPtrAllocator. This should be rarely used, and only on error paths.
private Optional<clang::FileEntryRef> STDIN
The file entry for stdin, if it has been accessed through the FileManager.
private llvm::DenseMap<const void*, llvm::StringRef> CanonicalNames
The canonical names of files and directories .
private llvm::BumpPtrAllocator CanonicalNameStorage
Storage for canonical names that we have computed.
private unsigned int NextFileUID
Each FileEntry we create is assigned a unique ID #.
private std::unique_ptr<FileSystemStatCache> StatCache

Method Overview

  • public FileManager(const clang::FileSystemOptions & FileSystemOpts, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr)
  • public bool FixupRelativePath(SmallVectorImpl<char> & path) const
  • public void GetUniqueIDMapping(SmallVectorImpl<const clang::FileEntry *> & UIDToFiles) const
  • public void PrintStats() const
  • private void addAncestorsAsVirtualDirs(llvm::StringRef Path)
  • public void clearStatCache()
  • private void fillRealPathName(clang::FileEntry * UFE, llvm::StringRef FileName)
  • public llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> getBufferForFile(llvm::StringRef Filename, bool isVolatile = false, bool RequiresNullTerminator = true)
  • public llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> getBufferForFile(const clang::FileEntry * Entry, bool isVolatile = false, bool RequiresNullTerminator = true)
  • private llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> getBufferForFileImpl(llvm::StringRef Filename, int64_t FileSize, bool isVolatile, bool RequiresNullTerminator)
  • public llvm::Optional<FileEntryRef> getBypassFile(clang::FileEntryRef VFE)
  • public llvm::StringRef getCanonicalName(const clang::FileEntry * File)
  • public llvm::StringRef getCanonicalName(const clang::DirectoryEntry * Dir)
  • public llvm::ErrorOr<const DirectoryEntry *> getDirectory(llvm::StringRef DirName, bool CacheFailure = true)
  • public llvm::Expected<DirectoryEntryRef> getDirectoryRef(llvm::StringRef DirName, bool CacheFailure = true)
  • public llvm::ErrorOr<const FileEntry *> getFile(llvm::StringRef Filename, bool OpenFile = false, bool CacheFailure = true)
  • public llvm::Expected<FileEntryRef> getFileRef(llvm::StringRef Filename, bool OpenFile = false, bool CacheFailure = true)
  • public clang::FileSystemOptions & getFileSystemOpts()
  • public const clang::FileSystemOptions & getFileSystemOpts() const
  • public std::error_code getNoncachedStatValue(llvm::StringRef Path, llvm::vfs::Status & Result)
  • public size_t getNumUniqueRealFiles() const
  • public llvm::Optional<DirectoryEntryRef> getOptionalDirectoryRef(llvm::StringRef DirName, bool CacheFailure = true)
  • public llvm::Optional<FileEntryRef> getOptionalFileRef(llvm::StringRef Filename, bool OpenFile = false, bool CacheFailure = true)
  • public llvm::Expected<FileEntryRef> getSTDIN()
  • private std::error_code getStatValue(llvm::StringRef Path, llvm::vfs::Status & Status, bool isFile, std::unique_ptr<llvm::vfs::File> * F)
  • public const clang::FileEntry * getVirtualFile(llvm::StringRef Filename, off_t Size, time_t ModificationTime)
  • public clang::FileEntryRef getVirtualFileRef(llvm::StringRef Filename, off_t Size, time_t ModificationTime)
  • public llvm::vfs::FileSystem & getVirtualFileSystem() const
  • public bool makeAbsolutePath(SmallVectorImpl<char> & Path) const
  • public void setStatCache(std::unique_ptr<FileSystemStatCache> statCache)
  • public void setVirtualFileSystem(IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
  • public ~FileManager()

Methods

FileManager(
    const clang::FileSystemOptions&
        FileSystemOpts,
    IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
        nullptr)

Description

Construct a file manager, optionally with a custom VFS.

Declared at: clang/include/clang/Basic/FileManager.h:136

Parameters

const clang::FileSystemOptions& FileSystemOpts
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr
if non-null, the VFS to use. Otherwise uses llvm::vfs::getRealFileSystem().

bool FixupRelativePath(
    SmallVectorImpl<char>& path) const

Description

If path is not absolute and FileSystemOptions set the working directory, the path is modified to be relative to the given working directory.

Declared at: clang/include/clang/Basic/FileManager.h:302

Parameters

SmallVectorImpl<char>& path

Returns

true if \c path changed.

void GetUniqueIDMapping(
    SmallVectorImpl<const clang::FileEntry*>&
        UIDToFiles) const

Description

Produce an array mapping from the unique IDs assigned to each file to the corresponding FileEntry pointer.

Declared at: clang/include/clang/Basic/FileManager.h:311

Parameters

SmallVectorImpl<const clang::FileEntry*>& UIDToFiles

void PrintStats() const

Declared at: clang/include/clang/Basic/FileManager.h:328

void addAncestorsAsVirtualDirs(
    llvm::StringRef Path)

Description

Add all ancestors of the given path (pointing to either a file or a directory) as virtual directories.

Declared at: clang/include/clang/Basic/FileManager.h:126

Parameters

llvm::StringRef Path

void clearStatCache()

Description

Removes the FileSystemStatCache object from the manager.

Declared at: clang/include/clang/Basic/FileManager.h:150

void fillRealPathName(clang::FileEntry* UFE,
                      llvm::StringRef FileName)

Description

Fills the RealPathName in file entry.

Declared at: clang/include/clang/Basic/FileManager.h:129

Parameters

clang::FileEntry* UFE
llvm::StringRef FileName

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getBufferForFile(
    llvm::StringRef Filename,
    bool isVolatile = false,
    bool RequiresNullTerminator = true)

Declared at: clang/include/clang/Basic/FileManager.h:277

Parameters

llvm::StringRef Filename
bool isVolatile = false
bool RequiresNullTerminator = true

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getBufferForFile(
    const clang::FileEntry* Entry,
    bool isVolatile = false,
    bool RequiresNullTerminator = true)

Description

Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.

Declared at: clang/include/clang/Basic/FileManager.h:274

Parameters

const clang::FileEntry* Entry
bool isVolatile = false
bool RequiresNullTerminator = true

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getBufferForFileImpl(llvm::StringRef Filename,
                     int64_t FileSize,
                     bool isVolatile,
                     bool RequiresNullTerminator)

Declared at: clang/include/clang/Basic/FileManager.h:285

Parameters

llvm::StringRef Filename
int64_t FileSize
bool isVolatile
bool RequiresNullTerminator

llvm::Optional<FileEntryRef> getBypassFile(
    clang::FileEntryRef VFE)

Description

Retrieve a FileEntry that bypasses VFE, which is expected to be a virtual file entry, to access the real file. The returned FileEntry will have the same filename as FE but a different identity and its own stat. This should be used only for rare error recovery paths because it bypasses all mapping and uniquing, blindly creating a new FileEntry. There is no attempt to deduplicate these; if you bypass the same file twice, you get two new file entries.

Declared at: clang/include/clang/Basic/FileManager.h:269

Parameters

clang::FileEntryRef VFE

llvm::StringRef getCanonicalName(
    const clang::FileEntry* File)

Description

Retrieve the canonical name for a given file. This is a very expensive operation, despite its results being cached, and should only be used when the physical layout of the file system is required, which is (almost) never.

Declared at: clang/include/clang/Basic/FileManager.h:326

Parameters

const clang::FileEntry* File

llvm::StringRef getCanonicalName(
    const clang::DirectoryEntry* Dir)

Description

Retrieve the canonical name for a given directory. This is a very expensive operation, despite its results being cached, and should only be used when the physical layout of the file system is required, which is (almost) never.

Declared at: clang/include/clang/Basic/FileManager.h:319

Parameters

const clang::DirectoryEntry* Dir

llvm::ErrorOr<const DirectoryEntry*> getDirectory(
    llvm::StringRef DirName,
    bool CacheFailure = true)

Description

Lookup, cache, and verify the specified directory (real or virtual). This function is deprecated and will be removed at some point in the future, new clients should use\c getDirectoryRef. This returns a \c std::error_code if there was an error reading the directory. If there is no error, the DirectoryEntry is guaranteed to be non-NULL.

Declared at: clang/include/clang/Basic/FileManager.h:188

Parameters

llvm::StringRef DirName
bool CacheFailure = true
If true and the file does not exist, we'll cache the failure to find this file.

llvm::Expected<DirectoryEntryRef> getDirectoryRef(
    llvm::StringRef DirName,
    bool CacheFailure = true)

Description

Lookup, cache, and verify the specified directory (real or virtual). This returns a \c std::error_code if there was an error reading the directory. On success, returns the reference to the directory entry together with the exact path that was used to access a file by a particular call to getDirectoryRef.

Declared at: clang/include/clang/Basic/FileManager.h:165

Parameters

llvm::StringRef DirName
bool CacheFailure = true
If true and the file does not exist, we'll cache the failure to find this file.

llvm::ErrorOr<const FileEntry*> getFile(
    llvm::StringRef Filename,
    bool OpenFile = false,
    bool CacheFailure = true)

Description

Lookup, cache, and verify the specified file (real or virtual). This function is deprecated and will be removed at some point in the future, new clients should use\c getFileRef. This returns a \c std::error_code if there was an error loading the file. If there is no error, the FileEntry is guaranteed to be non-NULL.

Declared at: clang/include/clang/Basic/FileManager.h:205

Parameters

llvm::StringRef Filename
bool OpenFile = false
if true and the file exists, it will be opened.
bool CacheFailure = true
If true and the file does not exist, we'll cache the failure to find this file.

llvm::Expected<FileEntryRef> getFileRef(
    llvm::StringRef Filename,
    bool OpenFile = false,
    bool CacheFailure = true)

Description

Lookup, cache, and verify the specified file (real or virtual). Return the reference to the file entry together with the exact path that was used to access a file by a particular call to getFileRef. If the underlying VFS is a redirecting VFS that uses external file names, the returned FileEntryRef will use the external name instead of the filename that was passed to this method. This returns a \c std::error_code if there was an error loading the file, or a \c FileEntryRef otherwise.

Declared at: clang/include/clang/Basic/FileManager.h:221

Parameters

llvm::StringRef Filename
bool OpenFile = false
if true and the file exists, it will be opened.
bool CacheFailure = true
If true and the file does not exist, we'll cache the failure to find this file.

clang::FileSystemOptions& getFileSystemOpts()

Description

Returns the current file system options

Declared at: clang/include/clang/Basic/FileManager.h:242

const clang::FileSystemOptions&
getFileSystemOpts() const

Declared at: clang/include/clang/Basic/FileManager.h:243

std::error_code getNoncachedStatValue(
    llvm::StringRef Path,
    llvm::vfs::Status& Result)

Description

Get the 'stat' information for the given \p Path. If the path is relative, it will be resolved against the WorkingDir of the FileManager's FileSystemOptions.

Declared at: clang/include/clang/Basic/FileManager.h:295

Parameters

llvm::StringRef Path
llvm::vfs::Status& Result

Returns

a \c std::error_code describing an error, if there was one

size_t getNumUniqueRealFiles() const

Description

Returns the number of unique real file entries cached by the file manager.

Declared at: clang/include/clang/Basic/FileManager.h:153

llvm::Optional<DirectoryEntryRef>
getOptionalDirectoryRef(llvm::StringRef DirName,
                        bool CacheFailure = true)

Description

Get a \c DirectoryEntryRef if it exists, without doing anything on error.

Declared at: clang/include/clang/Basic/FileManager.h:170

Parameters

llvm::StringRef DirName
bool CacheFailure = true

llvm::Optional<FileEntryRef> getOptionalFileRef(
    llvm::StringRef Filename,
    bool OpenFile = false,
    bool CacheFailure = true)

Description

Get a FileEntryRef if it exists, without doing anything on error.

Declared at: clang/include/clang/Basic/FileManager.h:234

Parameters

llvm::StringRef Filename
bool OpenFile = false
bool CacheFailure = true

llvm::Expected<FileEntryRef> getSTDIN()

Description

Get the FileEntryRef for stdin, returning an error if stdin cannot be read. This reads and caches stdin before returning. Subsequent calls return the same file entry, and a reference to the cached input is returned by calls to getBufferForFile.

Declared at: clang/include/clang/Basic/FileManager.h:231

std::error_code getStatValue(
    llvm::StringRef Path,
    llvm::vfs::Status& Status,
    bool isFile,
    std::unique_ptr<llvm::vfs::File>* F)

Declared at: clang/include/clang/Basic/FileManager.h:120

Parameters

llvm::StringRef Path
llvm::vfs::Status& Status
bool isFile
std::unique_ptr<llvm::vfs::File>* F

const clang::FileEntry* getVirtualFile(
    llvm::StringRef Filename,
    off_t Size,
    time_t ModificationTime)

Declared at: clang/include/clang/Basic/FileManager.h:258

Parameters

llvm::StringRef Filename
off_t Size
time_t ModificationTime

clang::FileEntryRef getVirtualFileRef(
    llvm::StringRef Filename,
    off_t Size,
    time_t ModificationTime)

Description

Retrieve a file entry for a "virtual" file that acts as if there were a file with the given name on disk. The file itself is not accessed.

Declared at: clang/include/clang/Basic/FileManager.h:255

Parameters

llvm::StringRef Filename
off_t Size
time_t ModificationTime

llvm::vfs::FileSystem& getVirtualFileSystem()
    const

Declared at: clang/include/clang/Basic/FileManager.h:245

bool makeAbsolutePath(
    SmallVectorImpl<char>& Path) const

Description

Makes \c Path absolute taking into account FileSystemOptions and the working directory option.

Declared at: clang/include/clang/Basic/FileManager.h:307

Parameters

SmallVectorImpl<char>& Path

Returns

true if \c Path changed to absolute.

void setStatCache(
    std::unique_ptr<FileSystemStatCache>
        statCache)

Description

Installs the provided FileSystemStatCache object within the FileManager. Ownership of this object is transferred to the FileManager.

Declared at: clang/include/clang/Basic/FileManager.h:147

Parameters

std::unique_ptr<FileSystemStatCache> statCache
the new stat cache to install. Ownership of this object is transferred to the FileManager.

void setVirtualFileSystem(
    IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)

Declared at: clang/include/clang/Basic/FileManager.h:247

Parameters

IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS

~FileManager()

Declared at: clang/include/clang/Basic/FileManager.h:138