14#ifndef LLVM_CLANG_BASIC_FILEMANAGER_H
15#define LLVM_CLANG_BASIC_FILEMANAGER_H
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/IntrusiveRefCntPtr.h"
23#include "llvm/ADT/PointerUnion.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringMap.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/Support/Allocator.h"
28#include "llvm/Support/Error.h"
29#include "llvm/Support/ErrorOr.h"
30#include "llvm/Support/FileSystem/UniqueID.h"
31#include "llvm/Support/MemoryBuffer.h"
60 llvm::SpecificBumpPtrAllocator<FileEntry> FilesAlloc;
61 llvm::SpecificBumpPtrAllocator<DirectoryEntry> DirsAlloc;
64 llvm::DenseMap<llvm::sys::fs::UniqueID, DirectoryEntry *> UniqueRealDirs;
67 llvm::DenseMap<llvm::sys::fs::UniqueID, FileEntry *> UniqueRealFiles;
90 llvm::StringMap<llvm::ErrorOr<DirectoryEntry &>, llvm::BumpPtrAllocator>
98 llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>, llvm::BumpPtrAllocator>
105 std::unique_ptr<llvm::StringMap<llvm::ErrorOr<FileEntryRef::MapValue>>>
106 SeenBypassFileEntries;
112 llvm::DenseMap<const void *, llvm::StringRef> CanonicalNames;
115 llvm::BumpPtrAllocator CanonicalNameStorage;
119 unsigned NextFileUID;
122 unsigned NumDirLookups = 0;
123 unsigned NumFileLookups = 0;
124 unsigned NumDirCacheMisses = 0;
125 unsigned NumFileCacheMisses = 0;
127 std::error_code getStatValue(StringRef Path, llvm::vfs::Status &Status,
128 bool isFile, std::unique_ptr<llvm::vfs::File> *F,
133 void addAncestorsAsVirtualDirs(StringRef Path);
140 llvm::ErrorOr<FileEntryRef> getFileRefImpl(StringRef Filename,
bool OpenFile,
141 bool CacheFailure,
bool IsText);
145 llvm::ErrorOr<DirectoryEntryRef> getDirectoryRefImpl(StringRef DirName,
150 llvm::ErrorOr<DirectoryEntryRef> getDirectoryFromFile(StringRef Filename,
179 bool CacheFailure =
true) {
180 auto Ref = getDirectoryRefImpl(DirName, CacheFailure);
183 return llvm::createFileError(DirName, Ref.getError());
188 bool CacheFailure =
true) {
189 if (
auto Ref = getDirectoryRefImpl(DirName, CacheFailure))
209 bool OpenFile =
false,
210 bool CacheFailure =
true,
211 bool IsText =
true) {
212 auto Ref = getFileRefImpl(Filename, OpenFile, CacheFailure, IsText);
215 return llvm::createFileError(Filename, Ref.getError());
228 bool OpenFile =
false,
229 bool CacheFailure =
true,
230 bool IsText =
true) {
231 if (
auto Ref = getFileRefImpl(Filename, OpenFile, CacheFailure, IsText))
255 time_t ModificationTime);
272 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
274 bool RequiresNullTerminator =
true,
275 std::optional<int64_t> MaybeLimit = std::nullopt,
277 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
279 bool RequiresNullTerminator =
true,
280 std::optional<int64_t> MaybeLimit = std::nullopt,
281 bool IsText =
true)
const {
282 return getBufferForFileImpl(Filename,
283 MaybeLimit.value_or(-1),
284 isVolatile, RequiresNullTerminator, IsText);
288 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
289 getBufferForFileImpl(StringRef Filename, int64_t FileSize,
bool isVolatile,
290 bool RequiresNullTerminator,
bool IsText)
const;
292 DirectoryEntry *&getRealDirEntry(
const llvm::vfs::Status &Status);
311 bool Canonicalize =
false)
const;
Defines interfaces for clang::DirectoryEntry and clang::DirectoryEntryRef.
Defines interfaces for clang::FileEntry and clang::FileEntryRef.
Defines the clang::FileSystemOptions interface.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
A reference to a DirectoryEntry that includes the name of the directory as it was accessed by the Fil...
Cached information about one directory (either on disk or in the virtual file system).
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Cached information about one file (either on disk or in the virtual file system).
Implements support for file system lookup, file system caching, and directory search management.
void AddStats(const FileManager &Other)
Import statistics from a child FileManager and add them to this current FileManager.
void trackVFSUsage(bool Active)
Enable or disable tracking of VFS usage.
llvm::vfs::FileSystem & getVirtualFileSystem() const
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(FileEntryRef Entry, bool isVolatile=false, bool RequiresNullTerminator=true, std::optional< int64_t > MaybeLimit=std::nullopt, bool IsText=true)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
llvm::Expected< DirectoryEntryRef > getDirectoryRef(StringRef DirName, bool CacheFailure=true)
Lookup, cache, and verify the specified directory (real or virtual).
llvm::Expected< FileEntryRef > getSTDIN()
Get the FileEntryRef for stdin, returning an error if stdin cannot be read.
llvm::Expected< FileEntryRef > getFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true, bool IsText=true)
Lookup, cache, and verify the specified file (real or virtual).
StringRef getCanonicalName(DirectoryEntryRef Dir)
Retrieve the canonical name for a given directory.
FileManager(const FileSystemOptions &FileSystemOpts, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS)
Construct a file manager, optionally with a custom VFS.
void setVirtualFileSystem(IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS)
FileSystemOptions & getFileSystemOpts()
Returns the current file system options.
const FileSystemOptions & getFileSystemOpts() const
FileEntryRef getVirtualFileRef(StringRef Filename, off_t Size, time_t ModificationTime)
Retrieve a file entry for a "virtual" file that acts as if there were a file with the given name on d...
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVirtualFileSystemPtr() const
bool FixupRelativePath(SmallVectorImpl< char > &Path) const
If path is not absolute and FileSystemOptions set the working directory, the path is modified to be r...
bool makeAbsolutePath(SmallVectorImpl< char > &Path, bool Canonicalize=false) const
Makes Path absolute taking into account FileSystemOptions and the working directory option,...
size_t getNumUniqueRealFiles() const
Returns the number of unique real file entries cached by the file manager.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(StringRef Filename, bool isVolatile=false, bool RequiresNullTerminator=true, std::optional< int64_t > MaybeLimit=std::nullopt, bool IsText=true) const
OptionalFileEntryRef getBypassFile(FileEntryRef VFE)
Retrieve a FileEntry that bypasses VFE, which is expected to be a virtual file entry,...
static bool fixupRelativePath(const FileSystemOptions &FileSystemOpts, SmallVectorImpl< char > &Path)
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true, bool IsText=true)
Get a FileEntryRef if it exists, without doing anything on error.
OptionalDirectoryEntryRef getOptionalDirectoryRef(StringRef DirName, bool CacheFailure=true)
Get a DirectoryEntryRef if it exists, without doing anything on error.
Keeps track of options that affect how file operations are performed.
Top level wrappers for InstallAPI frontend operations.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
CustomizableOptional< DirectoryEntryRef > OptionalDirectoryEntryRef
@ Other
Other implicit parameter.
Diagnostic wrappers for TextAPI types for error reporting.