2009-06-02 13:58:47 -04:00
|
|
|
//===--- FileManager.h - File System Probing and Caching --------*- C++ -*-===//
|
|
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-08-15 16:02:54 -04:00
|
|
|
///
|
|
|
|
|
/// \file
|
|
|
|
|
/// \brief Defines the clang::FileManager interface and associated types.
|
|
|
|
|
///
|
2009-06-02 13:58:47 -04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_FILEMANAGER_H
|
|
|
|
|
#define LLVM_CLANG_FILEMANAGER_H
|
|
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
#include "clang/Basic/FileSystemOptions.h"
|
2011-10-20 17:14:49 -04:00
|
|
|
#include "clang/Basic/LLVM.h"
|
2011-05-02 15:39:53 -04:00
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
2009-12-15 13:49:47 -05:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2009-06-02 13:58:47 -04:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2009-10-14 14:03:49 -04:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2009-06-02 13:58:47 -04:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
|
// FIXME: Enhance libsystem to support inode and other fields in stat.
|
|
|
|
|
#include <sys/types.h>
|
2011-02-20 08:06:31 -05:00
|
|
|
|
2011-07-17 11:40:56 -04:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
typedef unsigned short mode_t;
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
struct stat;
|
|
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
class MemoryBuffer;
|
|
|
|
|
namespace sys { class Path; }
|
|
|
|
|
}
|
2009-06-02 13:58:47 -04:00
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
class FileManager;
|
2011-02-20 08:06:31 -05:00
|
|
|
class FileSystemStatCache;
|
2012-04-14 10:01:31 -04:00
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief Cached information about one directory (either on disk or in
|
|
|
|
|
/// the virtual file system).
|
2009-06-02 13:58:47 -04:00
|
|
|
class DirectoryEntry {
|
|
|
|
|
const char *Name; // Name of the directory.
|
|
|
|
|
friend class FileManager;
|
|
|
|
|
public:
|
|
|
|
|
DirectoryEntry() : Name(0) {}
|
2009-10-14 14:03:49 -04:00
|
|
|
const char *getName() const { return Name; }
|
2009-06-02 13:58:47 -04:00
|
|
|
};
|
|
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief Cached information about one file (either on disk
|
|
|
|
|
/// or in the virtual file system).
|
2009-06-02 13:58:47 -04:00
|
|
|
///
|
2012-08-15 16:02:54 -04:00
|
|
|
/// If the 'FD' member is valid, then this FileEntry has an open file
|
|
|
|
|
/// descriptor for the file.
|
2009-06-02 13:58:47 -04:00
|
|
|
class FileEntry {
|
|
|
|
|
const char *Name; // Name of the file.
|
|
|
|
|
off_t Size; // File size in bytes.
|
|
|
|
|
time_t ModTime; // Modification time of file.
|
|
|
|
|
const DirectoryEntry *Dir; // Directory file lives in.
|
|
|
|
|
unsigned UID; // A unique (small) ID for the file.
|
|
|
|
|
dev_t Device; // ID for the device containing the file.
|
|
|
|
|
ino_t Inode; // Inode number for the file.
|
|
|
|
|
mode_t FileMode; // The file mode as returned by 'stat'.
|
2012-04-14 10:01:31 -04:00
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
/// FD - The file descriptor for the file entry if it is opened and owned
|
|
|
|
|
/// by the FileEntry. If not, this is set to -1.
|
|
|
|
|
mutable int FD;
|
2009-06-02 13:58:47 -04:00
|
|
|
friend class FileManager;
|
2012-04-14 10:01:31 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
public:
|
|
|
|
|
FileEntry(dev_t device, ino_t inode, mode_t m)
|
2011-02-20 08:06:31 -05:00
|
|
|
: Name(0), Device(device), Inode(inode), FileMode(m), FD(-1) {}
|
2009-06-02 13:58:47 -04:00
|
|
|
// Add a default constructor for use with llvm::StringMap
|
2011-02-20 08:06:31 -05:00
|
|
|
FileEntry() : Name(0), Device(0), Inode(0), FileMode(0), FD(-1) {}
|
|
|
|
|
|
|
|
|
|
FileEntry(const FileEntry &FE) {
|
|
|
|
|
memcpy(this, &FE, sizeof(FE));
|
|
|
|
|
assert(FD == -1 && "Cannot copy a file-owning FileEntry");
|
|
|
|
|
}
|
2012-04-14 10:01:31 -04:00
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
void operator=(const FileEntry &FE) {
|
|
|
|
|
memcpy(this, &FE, sizeof(FE));
|
|
|
|
|
assert(FD == -1 && "Cannot assign a file-owning FileEntry");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~FileEntry();
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
const char *getName() const { return Name; }
|
|
|
|
|
off_t getSize() const { return Size; }
|
|
|
|
|
unsigned getUID() const { return UID; }
|
|
|
|
|
ino_t getInode() const { return Inode; }
|
|
|
|
|
dev_t getDevice() const { return Device; }
|
|
|
|
|
time_t getModificationTime() const { return ModTime; }
|
|
|
|
|
mode_t getFileMode() const { return FileMode; }
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief Return the directory the file lives in.
|
2009-06-02 13:58:47 -04:00
|
|
|
const DirectoryEntry *getDir() const { return Dir; }
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
bool operator<(const FileEntry &RHS) const {
|
2009-06-02 13:58:47 -04:00
|
|
|
return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
|
|
|
|
|
}
|
2012-12-02 08:20:44 -05:00
|
|
|
|
|
|
|
|
/// \brief Check whether the file is a named pipe (and thus can't be opened by
|
|
|
|
|
/// the native FileManager methods).
|
|
|
|
|
bool isNamedPipe() const;
|
2009-06-02 13:58:47 -04:00
|
|
|
};
|
|
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief 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.
|
2009-06-02 13:58:47 -04:00
|
|
|
///
|
2012-04-14 10:01:31 -04:00
|
|
|
class FileManager : public RefCountedBase<FileManager> {
|
2011-02-20 08:06:31 -05:00
|
|
|
FileSystemOptions FileSystemOpts;
|
2009-06-02 13:58:47 -04:00
|
|
|
|
|
|
|
|
class UniqueDirContainer;
|
|
|
|
|
class UniqueFileContainer;
|
|
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief Cache for existing real directories.
|
2011-02-20 08:06:31 -05:00
|
|
|
UniqueDirContainer &UniqueRealDirs;
|
2012-08-15 16:02:54 -04:00
|
|
|
|
|
|
|
|
/// \brief Cache for existing real files.
|
2011-02-20 08:06:31 -05:00
|
|
|
UniqueFileContainer &UniqueRealFiles;
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief The virtual directories that we have allocated.
|
|
|
|
|
///
|
|
|
|
|
/// For each virtual file (e.g. foo/bar/baz.cpp), we add all of its parent
|
2011-02-20 08:06:31 -05:00
|
|
|
/// directories (foo/ and foo/bar/) here.
|
2011-10-20 17:14:49 -04:00
|
|
|
SmallVector<DirectoryEntry*, 4> VirtualDirectoryEntries;
|
2011-02-20 08:06:31 -05:00
|
|
|
/// \brief The virtual files that we have allocated.
|
2011-10-20 17:14:49 -04:00
|
|
|
SmallVector<FileEntry*, 4> VirtualFileEntries;
|
2011-02-20 08:06:31 -05:00
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief A cache that maps paths to directory entries (either real or
|
|
|
|
|
/// virtual) we have looked up
|
|
|
|
|
///
|
|
|
|
|
/// The actual Entries for real directories/files are
|
2011-02-20 08:06:31 -05:00
|
|
|
/// owned by UniqueRealDirs/UniqueRealFiles above, while the Entries
|
|
|
|
|
/// for virtual directories/files are owned by
|
|
|
|
|
/// VirtualDirectoryEntries/VirtualFileEntries above.
|
2009-06-02 13:58:47 -04:00
|
|
|
///
|
2011-02-20 08:06:31 -05:00
|
|
|
llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries;
|
2012-08-15 16:02:54 -04:00
|
|
|
|
|
|
|
|
/// \brief A cache that maps paths to file entries (either real or
|
|
|
|
|
/// virtual) we have looked up.
|
|
|
|
|
///
|
|
|
|
|
/// \see SeenDirEntries
|
2011-02-20 08:06:31 -05:00
|
|
|
llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief Each FileEntry we create is assigned a unique ID #.
|
2009-06-02 13:58:47 -04:00
|
|
|
///
|
|
|
|
|
unsigned NextFileUID;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
// Statistics.
|
|
|
|
|
unsigned NumDirLookups, NumFileLookups;
|
|
|
|
|
unsigned NumDirCacheMisses, NumFileCacheMisses;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
// Caching.
|
2012-04-14 10:01:31 -04:00
|
|
|
OwningPtr<FileSystemStatCache> StatCache;
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
bool getStatValue(const char *Path, struct stat &StatBuf,
|
|
|
|
|
int *FileDescriptor);
|
|
|
|
|
|
|
|
|
|
/// Add all ancestors of the given path (pointing to either a file
|
|
|
|
|
/// or a directory) as virtual directories.
|
2011-10-20 17:14:49 -04:00
|
|
|
void addAncestorsAsVirtualDirs(StringRef Path);
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
public:
|
2011-02-20 08:06:31 -05:00
|
|
|
FileManager(const FileSystemOptions &FileSystemOpts);
|
2009-06-02 13:58:47 -04:00
|
|
|
~FileManager();
|
|
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
/// \brief Installs the provided FileSystemStatCache object within
|
|
|
|
|
/// the FileManager.
|
2009-10-23 10:22:18 -04:00
|
|
|
///
|
|
|
|
|
/// Ownership of this object is transferred to the FileManager.
|
|
|
|
|
///
|
|
|
|
|
/// \param statCache the new stat cache to install. Ownership of this
|
|
|
|
|
/// object is transferred to the FileManager.
|
|
|
|
|
///
|
|
|
|
|
/// \param AtBeginning whether this new stat cache must be installed at the
|
|
|
|
|
/// beginning of the chain of stat caches. Otherwise, it will be added to
|
|
|
|
|
/// the end of the chain.
|
2011-02-20 08:06:31 -05:00
|
|
|
void addStatCache(FileSystemStatCache *statCache, bool AtBeginning = false);
|
2009-10-23 10:22:18 -04:00
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
/// \brief Removes the specified FileSystemStatCache object from the manager.
|
|
|
|
|
void removeStatCache(FileSystemStatCache *statCache);
|
|
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief Removes all FileSystemStatCache objects from the manager.
|
|
|
|
|
void clearStatCaches();
|
|
|
|
|
|
|
|
|
|
/// \brief Lookup, cache, and verify the specified directory (real or
|
|
|
|
|
/// virtual).
|
|
|
|
|
///
|
|
|
|
|
/// This returns NULL if the directory doesn't exist.
|
2009-10-14 14:03:49 -04:00
|
|
|
///
|
2011-10-20 17:14:49 -04:00
|
|
|
/// \param CacheFailure If true and the file does not exist, we'll cache
|
|
|
|
|
/// the failure to find this file.
|
|
|
|
|
const DirectoryEntry *getDirectory(StringRef DirName,
|
|
|
|
|
bool CacheFailure = true);
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-05-02 15:39:53 -04:00
|
|
|
/// \brief Lookup, cache, and verify the specified file (real or
|
2012-08-15 16:02:54 -04:00
|
|
|
/// virtual).
|
|
|
|
|
///
|
|
|
|
|
/// This returns NULL if the file doesn't exist.
|
2009-10-14 14:03:49 -04:00
|
|
|
///
|
2011-10-20 17:14:49 -04:00
|
|
|
/// \param OpenFile if true and the file exists, it will be opened.
|
|
|
|
|
///
|
|
|
|
|
/// \param CacheFailure If true and the file does not exist, we'll cache
|
|
|
|
|
/// the failure to find this file.
|
|
|
|
|
const FileEntry *getFile(StringRef Filename, bool OpenFile = false,
|
|
|
|
|
bool CacheFailure = true);
|
|
|
|
|
|
|
|
|
|
/// \brief Returns the current file system options
|
|
|
|
|
const FileSystemOptions &getFileSystemOptions() { return FileSystemOpts; }
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-12-15 13:49:47 -05:00
|
|
|
/// \brief Retrieve a file entry for a "virtual" file that acts as
|
2012-08-15 16:02:54 -04:00
|
|
|
/// if there were a file with the given name on disk.
|
|
|
|
|
///
|
|
|
|
|
/// The file itself is not accessed.
|
2011-10-20 17:14:49 -04:00
|
|
|
const FileEntry *getVirtualFile(StringRef Filename, off_t Size,
|
2010-07-15 13:07:12 -04:00
|
|
|
time_t ModificationTime);
|
2011-02-20 08:06:31 -05:00
|
|
|
|
|
|
|
|
/// \brief Open the specified file as a MemoryBuffer, returning a new
|
|
|
|
|
/// MemoryBuffer if successful, otherwise returning null.
|
|
|
|
|
llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry,
|
2012-08-15 16:02:54 -04:00
|
|
|
std::string *ErrorStr = 0,
|
|
|
|
|
bool isVolatile = false);
|
2011-10-20 17:14:49 -04:00
|
|
|
llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
|
2011-02-20 08:06:31 -05:00
|
|
|
std::string *ErrorStr = 0);
|
|
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief 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.
|
2011-10-20 17:14:49 -04:00
|
|
|
bool getNoncachedStatValue(StringRef Path, struct stat &StatBuf);
|
2011-05-02 15:39:53 -04:00
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief Remove the real file \p Entry from the cache.
|
|
|
|
|
void invalidateCache(const FileEntry *Entry);
|
|
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
/// \brief If path is not absolute and FileSystemOptions set the working
|
|
|
|
|
/// directory, the path is modified to be relative to the given
|
|
|
|
|
/// working directory.
|
2011-10-20 17:14:49 -04:00
|
|
|
void FixupRelativePath(SmallVectorImpl<char> &path) const;
|
2011-02-20 08:06:31 -05:00
|
|
|
|
|
|
|
|
/// \brief Produce an array mapping from the unique IDs assigned to each
|
|
|
|
|
/// file to the corresponding FileEntry pointer.
|
|
|
|
|
void GetUniqueIDMapping(
|
2011-10-20 17:14:49 -04:00
|
|
|
SmallVectorImpl<const FileEntry *> &UIDToFiles) const;
|
2012-04-14 10:01:31 -04:00
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief Modifies the size and modification time of a previously created
|
|
|
|
|
/// FileEntry. Use with caution.
|
|
|
|
|
static void modifyFileEntry(FileEntry *File, off_t Size,
|
|
|
|
|
time_t ModificationTime);
|
|
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
void PrintStats() const;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
|
|
#endif
|