aboutsummaryrefslogtreecommitdiff
path: root/include/clang/Basic/FileManager.h
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2011-02-20 13:06:31 +0000
committerDimitry Andric <dim@FreeBSD.org>2011-02-20 13:06:31 +0000
commitbca07a4524feb4edec581062d631a13116320a24 (patch)
treea9243275843fbeaa590afc07ee888e006b8d54ea /include/clang/Basic/FileManager.h
parent998bc5802ecdd65ce3b270f6c69a8ae8557f0a10 (diff)
downloadsrc-bca07a4524feb4edec581062d631a13116320a24.tar.gz
src-bca07a4524feb4edec581062d631a13116320a24.zip
Vendor import of clang trunk r126079:vendor/clang/clang-r126079
Notes
Notes: svn path=/vendor/clang/dist/; revision=218887 svn path=/vendor/clang/clang-r126079/; revision=218888; tag=vendor/clang/clang-r126079
Diffstat (limited to 'include/clang/Basic/FileManager.h')
-rw-r--r--include/clang/Basic/FileManager.h190
1 files changed, 94 insertions, 96 deletions
diff --git a/include/clang/Basic/FileManager.h b/include/clang/Basic/FileManager.h
index e71f51a0e700..563157fa8fbe 100644
--- a/include/clang/Basic/FileManager.h
+++ b/include/clang/Basic/FileManager.h
@@ -14,6 +14,7 @@
#ifndef LLVM_CLANG_FILEMANAGER_H
#define LLVM_CLANG_FILEMANAGER_H
+#include "clang/Basic/FileSystemOptions.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
@@ -22,12 +23,20 @@
#include "llvm/Config/config.h" // for mode_t
// FIXME: Enhance libsystem to support inode and other fields in stat.
#include <sys/types.h>
-#include <sys/stat.h>
+
+struct stat;
+
+namespace llvm {
+class MemoryBuffer;
+namespace sys { class Path; }
+}
namespace clang {
class FileManager;
-
-/// DirectoryEntry - Cached information about one directory on the disk.
+class FileSystemStatCache;
+
+/// DirectoryEntry - Cached information about one directory (either on
+/// the disk or in the virtual file system).
///
class DirectoryEntry {
const char *Name; // Name of the directory.
@@ -37,7 +46,9 @@ public:
const char *getName() const { return Name; }
};
-/// FileEntry - Cached information about one file on the disk.
+/// FileEntry - Cached information about one file (either on the disk
+/// or in the virtual file system). If the 'FD' member is valid, then
+/// this FileEntry has an open file descriptor for the file.
///
class FileEntry {
const char *Name; // Name of the file.
@@ -48,12 +59,29 @@ class FileEntry {
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'.
+
+ /// 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;
friend class FileManager;
+
public:
FileEntry(dev_t device, ino_t inode, mode_t m)
- : Name(0), Device(device), Inode(inode), FileMode(m) {}
+ : Name(0), Device(device), Inode(inode), FileMode(m), FD(-1) {}
// Add a default constructor for use with llvm::StringMap
- FileEntry() : Name(0), Device(0), Inode(0), FileMode(0) {}
+ 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");
+ }
+
+ void operator=(const FileEntry &FE) {
+ memcpy(this, &FE, sizeof(FE));
+ assert(FD == -1 && "Cannot assign a file-owning FileEntry");
+ }
+
+ ~FileEntry();
const char *getName() const { return Name; }
off_t getSize() const { return Size; }
@@ -67,111 +95,68 @@ public:
///
const DirectoryEntry *getDir() const { return Dir; }
- bool operator<(const FileEntry& RHS) const {
+ bool operator<(const FileEntry &RHS) const {
return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
}
};
-/// \brief Abstract interface for introducing a FileManager cache for 'stat'
-/// system calls, which is used by precompiled and pretokenized headers to
-/// improve performance.
-class StatSysCallCache {
-protected:
- llvm::OwningPtr<StatSysCallCache> NextStatCache;
-
-public:
- virtual ~StatSysCallCache() {}
- virtual int stat(const char *path, struct stat *buf) {
- if (getNextStatCache())
- return getNextStatCache()->stat(path, buf);
-
- return ::stat(path, buf);
- }
-
- /// \brief Sets the next stat call cache in the chain of stat caches.
- /// Takes ownership of the given stat cache.
- void setNextStatCache(StatSysCallCache *Cache) {
- NextStatCache.reset(Cache);
- }
-
- /// \brief Retrieve the next stat call cache in the chain.
- StatSysCallCache *getNextStatCache() { return NextStatCache.get(); }
-
- /// \brief Retrieve the next stat call cache in the chain, transferring
- /// ownership of this cache (and, transitively, all of the remaining caches)
- /// to the caller.
- StatSysCallCache *takeNextStatCache() { return NextStatCache.take(); }
-};
-
-/// \brief A stat "cache" that can be used by FileManager to keep
-/// track of the results of stat() calls that occur throughout the
-/// execution of the front end.
-class MemorizeStatCalls : public StatSysCallCache {
-public:
- /// \brief The result of a stat() call.
- ///
- /// The first member is the result of calling stat(). If stat()
- /// found something, the second member is a copy of the stat
- /// structure.
- typedef std::pair<int, struct stat> StatResult;
-
- /// \brief The set of stat() calls that have been
- llvm::StringMap<StatResult, llvm::BumpPtrAllocator> StatCalls;
-
- typedef llvm::StringMap<StatResult, llvm::BumpPtrAllocator>::const_iterator
- iterator;
-
- iterator begin() const { return StatCalls.begin(); }
- iterator end() const { return StatCalls.end(); }
-
- virtual int stat(const char *path, struct stat *buf);
-};
-
/// FileManager - 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.
///
class FileManager {
+ FileSystemOptions FileSystemOpts;
class UniqueDirContainer;
class UniqueFileContainer;
- /// UniqueDirs/UniqueFiles - Cache for existing directories/files.
+ /// UniqueRealDirs/UniqueRealFiles - Cache for existing real directories/files.
///
- UniqueDirContainer &UniqueDirs;
- UniqueFileContainer &UniqueFiles;
+ UniqueDirContainer &UniqueRealDirs;
+ UniqueFileContainer &UniqueRealFiles;
- /// DirEntries/FileEntries - This is a cache of directory/file entries we have
- /// looked up. The actual Entry is owned by UniqueFiles/UniqueDirs above.
+ /// \brief The virtual directories that we have allocated. For each
+ /// virtual file (e.g. foo/bar/baz.cpp), we add all of its parent
+ /// directories (foo/ and foo/bar/) here.
+ llvm::SmallVector<DirectoryEntry*, 4> VirtualDirectoryEntries;
+ /// \brief The virtual files that we have allocated.
+ llvm::SmallVector<FileEntry*, 4> VirtualFileEntries;
+
+ /// SeenDirEntries/SeenFileEntries - This is a cache that maps paths
+ /// to directory/file entries (either real or virtual) we have
+ /// looked up. 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.
///
- llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> DirEntries;
- llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> FileEntries;
+ llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> SeenDirEntries;
+ llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> SeenFileEntries;
/// NextFileUID - Each FileEntry we create is assigned a unique ID #.
///
unsigned NextFileUID;
- /// \brief The virtual files that we have allocated.
- llvm::SmallVector<FileEntry *, 4> VirtualFileEntries;
-
// Statistics.
unsigned NumDirLookups, NumFileLookups;
unsigned NumDirCacheMisses, NumFileCacheMisses;
// Caching.
- llvm::OwningPtr<StatSysCallCache> StatCache;
+ llvm::OwningPtr<FileSystemStatCache> StatCache;
- int stat_cached(const char* path, struct stat* buf) {
- return StatCache.get() ? StatCache->stat(path, buf) : stat(path, buf);
- }
+ 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.
+ void addAncestorsAsVirtualDirs(llvm::StringRef Path);
public:
- FileManager();
+ FileManager(const FileSystemOptions &FileSystemOpts);
~FileManager();
- /// \brief Installs the provided StatSysCallCache object within
- /// the FileManager.
+ /// \brief Installs the provided FileSystemStatCache object within
+ /// the FileManager.
///
/// Ownership of this object is transferred to the FileManager.
///
@@ -181,33 +166,46 @@ public:
/// \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.
- void addStatCache(StatSysCallCache *statCache, bool AtBeginning = false);
+ void addStatCache(FileSystemStatCache *statCache, bool AtBeginning = false);
- /// \brief Removes the provided StatSysCallCache object from the file manager.
- void removeStatCache(StatSysCallCache *statCache);
-
- /// getDirectory - Lookup, cache, and verify the specified directory. This
- /// returns null if the directory doesn't exist.
+ /// \brief Removes the specified FileSystemStatCache object from the manager.
+ void removeStatCache(FileSystemStatCache *statCache);
+
+ /// getDirectory - Lookup, cache, and verify the specified directory
+ /// (real or virtual). This returns NULL if the directory doesn't exist.
///
- const DirectoryEntry *getDirectory(llvm::StringRef Filename) {
- return getDirectory(Filename.begin(), Filename.end());
- }
- const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd);
+ const DirectoryEntry *getDirectory(llvm::StringRef DirName);
- /// getFile - Lookup, cache, and verify the specified file. This returns null
- /// if the file doesn't exist.
+ /// getFile - Lookup, cache, and verify the specified file (real or
+ /// virtual). This returns NULL if the file doesn't exist.
///
- const FileEntry *getFile(llvm::StringRef Filename) {
- return getFile(Filename.begin(), Filename.end());
- }
- const FileEntry *getFile(const char *FilenameStart,
- const char *FilenameEnd);
+ const FileEntry *getFile(llvm::StringRef Filename);
/// \brief 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.
const FileEntry *getVirtualFile(llvm::StringRef Filename, off_t Size,
time_t ModificationTime);
+
+ /// \brief Open the specified file as a MemoryBuffer, returning a new
+ /// MemoryBuffer if successful, otherwise returning null.
+ llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry,
+ std::string *ErrorStr = 0);
+ llvm::MemoryBuffer *getBufferForFile(llvm::StringRef Filename,
+ std::string *ErrorStr = 0);
+
+ /// \brief If path is not absolute and FileSystemOptions set the working
+ /// directory, the path is modified to be relative to the given
+ /// working directory.
+ static void FixupRelativePath(llvm::sys::Path &path,
+ const FileSystemOptions &FSOpts);
+
+
+ /// \brief Produce an array mapping from the unique IDs assigned to each
+ /// file to the corresponding FileEntry pointer.
+ void GetUniqueIDMapping(
+ llvm::SmallVectorImpl<const FileEntry *> &UIDToFiles) const;
+
void PrintStats() const;
};