clang  10.0.0svn
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]
Collaboration diagram for clang::FileManager:
Collaboration graph
[legend]

Public Member Functions

 FileManager (const FileSystemOptions &FileSystemOpts, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS=nullptr)
 Construct a file manager, optionally with a custom VFS. More...
 
 ~FileManager ()
 
void setStatCache (std::unique_ptr< FileSystemStatCache > statCache)
 Installs the provided FileSystemStatCache object within the FileManager. More...
 
void clearStatCache ()
 Removes the FileSystemStatCache object from the manager. More...
 
size_t getNumUniqueRealFiles () const
 Returns the number of unique real file entries cached by the file manager. More...
 
llvm::Expected< DirectoryEntryRefgetDirectoryRef (StringRef DirName, bool CacheFailure=true)
 Lookup, cache, and verify the specified directory (real or virtual). More...
 
llvm::Optional< DirectoryEntryRefgetOptionalDirectoryRef (StringRef DirName, bool CacheFailure=true)
 Get a DirectoryEntryRef if it exists, without doing anything on error. More...
 
llvm::ErrorOr< const DirectoryEntry * > getDirectory (StringRef DirName, bool CacheFailure=true)
 Lookup, cache, and verify the specified directory (real or virtual). More...
 
llvm::ErrorOr< const FileEntry * > getFile (StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
 Lookup, cache, and verify the specified file (real or virtual). More...
 
llvm::Expected< FileEntryRefgetFileRef (StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
 Lookup, cache, and verify the specified file (real or virtual). More...
 
llvm::Optional< FileEntryRefgetOptionalFileRef (StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
 Get a FileEntryRef if it exists, without doing anything on error. More...
 
FileSystemOptionsgetFileSystemOpts ()
 Returns the current file system options. More...
 
const FileSystemOptionsgetFileSystemOpts () const
 
llvm::vfs::FileSystem & getVirtualFileSystem () const
 
void setVirtualFileSystem (IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS)
 
const FileEntrygetVirtualFile (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. More...
 
llvm::Optional< FileEntryRefgetBypassFile (FileEntryRef VFE)
 Retrieve a FileEntry that bypasses VFE, which is expected to be a virtual file entry, to access the real file. More...
 
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile (const FileEntry *Entry, bool isVolatile=false)
 Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null. More...
 
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile (StringRef Filename, bool isVolatile=false)
 
std::error_code getNoncachedStatValue (StringRef Path, llvm::vfs::Status &Result)
 Get the 'stat' information for the given Path. More...
 
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. More...
 
bool makeAbsolutePath (SmallVectorImpl< char > &Path) const
 Makes Path absolute taking into account FileSystemOptions and the working directory option. More...
 
void GetUniqueIDMapping (SmallVectorImpl< const FileEntry *> &UIDToFiles) const
 Produce an array mapping from the unique IDs assigned to each file to the corresponding FileEntry pointer. More...
 
StringRef getCanonicalName (const DirectoryEntry *Dir)
 Retrieve the canonical name for a given directory. More...
 
void PrintStats () const
 

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 171 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 51 of file FileManager.cpp.

References ~FileManager().

◆ ~FileManager()

FileManager::~FileManager ( )
default

Referenced by FileManager().

Member Function Documentation

◆ clearStatCache()

void FileManager::clearStatCache ( )

Removes the FileSystemStatCache object from the manager.

Definition at line 68 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 421 of file FileManager.cpp.

References clang::FileSystemOptions::WorkingDir.

Referenced by getBufferForFile(), hasSuperInitCall(), and makeAbsolutePath().

◆ getBufferForFile() [1/2]

llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > FileManager::getBufferForFile ( const FileEntry Entry,
bool  isVolatile = false 
)

◆ getBufferForFile() [2/2]

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer> > clang::FileManager::getBufferForFile ( StringRef  Filename,
bool  isVolatile = false 
)
inline

Definition at line 383 of file FileManager.h.

◆ getBypassFile()

llvm::Optional< FileEntryRef > 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 402 of file FileManager.cpp.

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

◆ getCanonicalName()

StringRef FileManager::getCanonicalName ( const DirectoryEntry 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 546 of file FileManager.cpp.

References clang::DirectoryEntry::getName().

Referenced by clang::HeaderSearch::getCachedModuleFileName(), getTopFrameworkDir(), clang::ModuleMap::inferFrameworkModule(), clang::ModuleMap::isBuiltinHeader(), and clang::TextDiagnostic::printDiagnosticMessage().

◆ 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 187 of file FileManager.cpp.

References getDirectoryRef(), and clang::Result.

Referenced by clang::ApplyHeaderSearchOptions(), clang::FrontendAction::BeginSourceFile(), collectIncludePCH(), computeRelativePath(), clang::HeaderSearch::getCachedModuleFileName(), getDirectoryFromFile(), getTopFrameworkDir(), clang::ModuleMap::inferFrameworkModule(), clang::ModuleMap::isBuiltinHeader(), clang::Preprocessor::LookupFile(), clang::HeaderSearch::lookupModule(), needModuleLookup(), and clang::TextDiagnostic::printDiagnosticMessage().

◆ 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 117 of file FileManager.cpp.

Referenced by getDirectory().

◆ 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 195 of file FileManager.cpp.

References getFileRef(), and clang::Result.

Referenced by clang::tooling::Replacement::apply(), clang::tooling::applyAllReplacements(), clang::FrontendAction::BeginSourceFile(), clang::PrecompiledPreamble::Build(), collectModuleHeaderIncludes(), EnableCodeCompletion(), clang::serialization::reader::HeaderFileInfoTrait::EqualKey(), clang::tooling::formatAndApplyAllReplacements(), getPrivateModuleMap(), getPublicModuleMap(), clang::Module::getTopHeaders(), clang::tooling::groupReplacementsByFile(), clang::ASTImporter::Import(), inferFrameworkLink(), InitializeFileRemapping(), llvm::DenseMapInfo< EditEntry >::isEqual(), clang::serialization::ModuleManager::lookupModuleFile(), prepareToBuildModule(), clang::serialization::reader::HeaderFileInfoTrait::ReadData(), clang::SourceManagerForFile::SourceManagerForFile(), 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 203 of file FileManager.cpp.

References Filename, and getDirectoryFromFile().

Referenced by getFile(), clang::DirectoryLookup::getName(), and clang::CompilerInstance::InitializeSourceManager().

◆ getFileSystemOpts() [1/2]

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

Returns the current file system options.

Definition at line 352 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 353 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 513 of file FileManager.cpp.

◆ getNumUniqueRealFiles()

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

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

Definition at line 271 of file FileManager.h.

◆ getOptionalDirectoryRef()

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

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

Definition at line 288 of file FileManager.h.

References Filename.

◆ getOptionalFileRef()

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

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

Definition at line 344 of file FileManager.h.

Referenced by clang::HeaderMap::LookupFile(), clang::DirectoryLookup::LookupFile(), and needModuleLookup().

◆ GetUniqueIDMapping()

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

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

Definition at line 525 of file FileManager.cpp.

Referenced by CreateSLocExpansionAbbrev().

◆ getVirtualFile()

const FileEntry * FileManager::getVirtualFile ( 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 332 of file FileManager.cpp.

References Filename, and getDirectoryFromFile().

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

◆ getVirtualFileSystem()

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

◆ 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 434 of file FileManager.cpp.

References FixupRelativePath().

Referenced by cleanPathForOutput().

◆ PrintStats()

void FileManager::PrintStats ( ) const

Definition at line 563 of file FileManager.cpp.

◆ 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 63 of file FileManager.cpp.

◆ setVirtualFileSystem()

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

Definition at line 357 of file FileManager.h.


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