clang 20.0.0git
Public Member Functions | List of all members
clang::FileManager Class Reference

Implements support for file system lookup, file system caching, and directory search management. More...

#include "clang/Basic/FileManager.h"

Inheritance diagram for clang::FileManager:
Inheritance graph
[legend]

Public Member Functions

 FileManager (const FileSystemOptions &FileSystemOpts, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS=nullptr)
 Construct a file manager, optionally with a custom VFS.
 
 ~FileManager ()
 
void setStatCache (std::unique_ptr< FileSystemStatCache > statCache)
 Installs the provided FileSystemStatCache object within the FileManager.
 
void clearStatCache ()
 Removes the FileSystemStatCache object from the manager.
 
size_t getNumUniqueRealFiles () const
 Returns the number of unique real file entries cached by the file manager.
 
llvm::Expected< DirectoryEntryRefgetDirectoryRef (StringRef DirName, bool CacheFailure=true)
 Lookup, cache, and verify the specified directory (real or virtual).
 
OptionalDirectoryEntryRef getOptionalDirectoryRef (StringRef DirName, bool CacheFailure=true)
 Get a DirectoryEntryRef if it exists, without doing anything on error.
 
llvm::ErrorOr< const DirectoryEntry * > getDirectory (StringRef DirName, bool CacheFailure=true)
 Lookup, cache, and verify the specified directory (real or virtual).
 
llvm::ErrorOr< const FileEntry * > getFile (StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
 Lookup, cache, and verify the specified file (real or virtual).
 
llvm::Expected< FileEntryRefgetFileRef (StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
 Lookup, cache, and verify the specified file (real or virtual).
 
llvm::Expected< FileEntryRefgetSTDIN ()
 Get the FileEntryRef for stdin, returning an error if stdin cannot be read.
 
OptionalFileEntryRef getOptionalFileRef (StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
 Get a FileEntryRef if it exists, without doing anything on error.
 
FileSystemOptionsgetFileSystemOpts ()
 Returns the current file system options.
 
const FileSystemOptionsgetFileSystemOpts () const
 
llvm::vfs::FileSystem & getVirtualFileSystem () const
 
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVirtualFileSystemPtr () const
 
void trackVFSUsage (bool Active)
 Enable or disable tracking of VFS usage.
 
void setVirtualFileSystem (IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS)
 
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 disk.
 
const FileEntrygetVirtualFile (StringRef Filename, off_t Size, time_t ModificationTime)
 
OptionalFileEntryRef getBypassFile (FileEntryRef VFE)
 Retrieve a FileEntry that bypasses VFE, which is expected to be a virtual file entry, to access the real file.
 
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile (FileEntryRef Entry, bool isVolatile=false, bool RequiresNullTerminator=true, std::optional< int64_t > MaybeLimit=std::nullopt)
 Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
 
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile (StringRef Filename, bool isVolatile=false, bool RequiresNullTerminator=true, std::optional< int64_t > MaybeLimit=std::nullopt) const
 
std::error_code getNoncachedStatValue (StringRef Path, llvm::vfs::Status &Result)
 Get the 'stat' information for the given Path.
 
bool FixupRelativePath (SmallVectorImpl< char > &path) const
 If path is not absolute and FileSystemOptions set the working directory, the path is modified to be relative to the given working directory.
 
bool makeAbsolutePath (SmallVectorImpl< char > &Path) const
 Makes Path absolute taking into account FileSystemOptions and the working directory option.
 
void GetUniqueIDMapping (SmallVectorImpl< OptionalFileEntryRef > &UIDToFiles) const
 Produce an array mapping from the unique IDs assigned to each file to the corresponding FileEntryRef.
 
StringRef getCanonicalName (DirectoryEntryRef Dir)
 Retrieve the canonical name for a given directory.
 
StringRef getCanonicalName (FileEntryRef File)
 Retrieve the canonical name for a given file.
 
void PrintStats () const
 
void AddStats (const FileManager &Other)
 Import statistics from a child FileManager and add them to this current FileManager.
 

Detailed 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.

Definition at line 53 of file FileManager.h.

Constructor & Destructor Documentation

◆ FileManager()

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

Construct a file manager, optionally with a custom VFS.

Parameters
FSif non-null, the VFS to use. Otherwise uses llvm::vfs::getRealFileSystem().

Definition at line 46 of file FileManager.cpp.

◆ ~FileManager()

FileManager::~FileManager ( )
default

Member Function Documentation

◆ AddStats()

void FileManager::AddStats ( const FileManager Other)

Import statistics from a child FileManager and add them to this current FileManager.

Definition at line 676 of file FileManager.cpp.

References clang::Other.

◆ clearStatCache()

void FileManager::clearStatCache ( )

Removes the FileSystemStatCache object from the manager.

Definition at line 63 of file FileManager.cpp.

Referenced by clang::tooling::FrontendActionFactory::runInvocation().

◆ FixupRelativePath()

bool FileManager::FixupRelativePath ( SmallVectorImpl< char > &  path) const

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

Returns
true if path changed.

Definition at line 496 of file FileManager.cpp.

References clang::FileSystemOptions::WorkingDir.

Referenced by getNoncachedStatValue(), and makeAbsolutePath().

◆ getBufferForFile() [1/2]

llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > FileManager::getBufferForFile ( FileEntryRef  Entry,
bool  isVolatile = false,
bool  RequiresNullTerminator = true,
std::optional< int64_t >  MaybeLimit = std::nullopt 
)

◆ getBufferForFile() [2/2]

llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > clang::FileManager::getBufferForFile ( StringRef  Filename,
bool  isVolatile = false,
bool  RequiresNullTerminator = true,
std::optional< int64_t >  MaybeLimit = std::nullopt 
) const
inline

Definition at line 292 of file FileManager.h.

References Filename.

◆ getBypassFile()

OptionalFileEntryRef FileManager::getBypassFile ( FileEntryRef  VFE)

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.

Definition at line 467 of file FileManager.cpp.

References clang::FileEntryRef::getDir(), clang::FileEntryRef::getFileEntry(), and clang::FileEntryRef::getName().

Referenced by clang::SourceManager::bypassFileContentsOverride().

◆ getCanonicalName() [1/2]

StringRef FileManager::getCanonicalName ( DirectoryEntryRef  Dir)

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.

Definition at line 630 of file FileManager.cpp.

References getCanonicalName(), and clang::DirectoryEntryRef::getName().

Referenced by clang::ModuleMap::canonicalizeModuleMapPath(), getCanonicalName(), and getTopFrameworkDir().

◆ getCanonicalName() [2/2]

StringRef FileManager::getCanonicalName ( FileEntryRef  File)

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.

Definition at line 634 of file FileManager.cpp.

References clang::File, and getCanonicalName().

◆ getDirectory()

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

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 getDirectoryRef.

This returns a std::error_code if there was an error reading the directory. If there is no error, the DirectoryEntry is guaranteed to be non-NULL.

Parameters
CacheFailureIf true and the file does not exist, we'll cache the failure to find this file.

Definition at line 200 of file FileManager.cpp.

References getDirectoryRef(), and clang::Result.

Referenced by computeRelativePath().

◆ getDirectoryRef()

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

Lookup, cache, and verify the specified directory (real or virtual).

This returns a 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.

Parameters
CacheFailureIf true and the file does not exist, we'll cache the failure to find this file.

Definition at line 139 of file FileManager.cpp.

Referenced by clang::ModuleMap::canonicalizeModuleMapPath(), getDirectory(), getDirectoryFromFile(), getOptionalDirectoryRef(), and clang::Preprocessor::LookupEmbedFile().

◆ getFile()

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

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 getFileRef.

This returns a std::error_code if there was an error loading the file. If there is no error, the FileEntry is guaranteed to be non-NULL.

Parameters
OpenFileif true and the file exists, it will be opened.
CacheFailureIf true and the file does not exist, we'll cache the failure to find this file.

Definition at line 208 of file FileManager.cpp.

References Filename, getFileRef(), and clang::Result.

Referenced by clang::installapi::InstallAPIContext::addKnownHeader(), clang::BackendConsumer::getBestLocationFromDebugLoc(), clang::tooling::groupReplacementsByFile(), clang::serialization::ModuleManager::lookupBuffer(), clang::serialization::ModuleManager::lookupByFileName(), and clang::RewriteIncludesAction::RewriteImportsListener::visitModuleFile().

◆ getFileRef()

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

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 std::error_code if there was an error loading the file, or a FileEntryRef otherwise.

Parameters
OpenFileif true and the file exists, it will be opened.
CacheFailureIf true and the file does not exist, we'll cache the failure to find this file.

Definition at line 216 of file FileManager.cpp.

References Filename, getDirectoryFromFile(), and clang::DirectoryEntryRef::getDirEntry().

Referenced by clang::tooling::formatAndApplyAllReplacements(), getFile(), getOptionalFileRef(), clang::CompilerInstance::InitializeSourceManager(), and clang::Preprocessor::LookupEmbedFile().

◆ getFileSystemOpts() [1/2]

FileSystemOptions & clang::FileManager::getFileSystemOpts ( )
inline

Returns the current file system options.

Definition at line 248 of file FileManager.h.

Referenced by clang::ASTUnit::LoadFromCompilerInvocation(), and clang::tooling::ToolInvocation::run().

◆ getFileSystemOpts() [2/2]

const FileSystemOptions & clang::FileManager::getFileSystemOpts ( ) const
inline

Definition at line 249 of file FileManager.h.

◆ getNoncachedStatValue()

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

Get the 'stat' information for the given Path.

If the path is relative, it will be resolved against the WorkingDir of the FileManager's FileSystemOptions.

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

Definition at line 600 of file FileManager.cpp.

References FixupRelativePath(), Path, and clang::Result.

Referenced by clang::serialization::ModuleManager::addModule().

◆ getNumUniqueRealFiles()

size_t clang::FileManager::getNumUniqueRealFiles ( ) const
inline

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

Definition at line 159 of file FileManager.h.

◆ getOptionalDirectoryRef()

OptionalDirectoryEntryRef clang::FileManager::getOptionalDirectoryRef ( StringRef  DirName,
bool  CacheFailure = true 
)
inline

◆ getOptionalFileRef()

OptionalFileEntryRef clang::FileManager::getOptionalFileRef ( StringRef  Filename,
bool  OpenFile = false,
bool  CacheFailure = true 
)
inline

◆ getSTDIN()

llvm::Expected< FileEntryRef > FileManager::getSTDIN ( )

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.

Definition at line 359 of file FileManager.cpp.

References clang::FileEntryRef::getFileEntry(), and getVirtualFileRef().

Referenced by clang::CompilerInstance::InitializeSourceManager(), and clang::serialization::ModuleManager::lookupModuleFile().

◆ GetUniqueIDMapping()

void FileManager::GetUniqueIDMapping ( SmallVectorImpl< OptionalFileEntryRef > &  UIDToFiles) const

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

Definition at line 612 of file FileManager.cpp.

References clang::FileEntryRef::getName(), and clang::FileEntryRef::getUID().

◆ getVirtualFile()

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

◆ getVirtualFileRef()

FileEntryRef FileManager::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 disk.

The file itself is not accessed.

Definition at line 390 of file FileManager.cpp.

References Filename, and getDirectoryFromFile().

Referenced by getSTDIN(), getVirtualFile(), InitializeFileRemapping(), and clang::HeaderSearch::loadModuleMapFile().

◆ getVirtualFileSystem()

llvm::vfs::FileSystem & clang::FileManager::getVirtualFileSystem ( ) const
inline

◆ getVirtualFileSystemPtr()

llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > clang::FileManager::getVirtualFileSystemPtr ( ) const
inline

Definition at line 253 of file FileManager.h.

Referenced by clang::CodeGenAction::ExecuteAction().

◆ makeAbsolutePath()

bool FileManager::makeAbsolutePath ( SmallVectorImpl< char > &  Path) const

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

Returns
true if Path changed to absolute.

Definition at line 509 of file FileManager.cpp.

References FixupRelativePath(), and Path.

Referenced by cleanPathForOutput(), makeAbsoluteAndPreferred(), and clang::Rewriter::overwriteChangedFiles().

◆ PrintStats()

void FileManager::PrintStats ( ) const

Definition at line 684 of file FileManager.cpp.

Referenced by clang::CompilerInstance::ExecuteAction().

◆ setStatCache()

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

Installs the provided FileSystemStatCache object within the FileManager.

Ownership of this object is transferred to the FileManager.

Parameters
statCachethe new stat cache to install. Ownership of this object is transferred to the FileManager.

Definition at line 58 of file FileManager.cpp.

◆ setVirtualFileSystem()

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

Definition at line 261 of file FileManager.h.

◆ trackVFSUsage()

void FileManager::trackVFSUsage ( bool  Active)

Enable or disable tracking of VFS usage.

Used to not track full header search and implicit modulemap lookup.

Definition at line 378 of file FileManager.cpp.


The documentation for this class was generated from the following files: