2010-11-24 03:19:34 +08:00
|
|
|
//===--- FileManager.cpp - File System Probing and Caching ----------------===//
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the FileManager interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// TODO: This should index all interesting directories with dirent calls.
|
|
|
|
// getdirentries ?
|
|
|
|
// opendir/readdir_r/closedir ?
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Basic/FileManager.h"
|
2010-11-24 03:19:34 +08:00
|
|
|
#include "clang/Basic/FileSystemStatCache.h"
|
2006-10-30 11:55:17 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2010-12-22 00:45:57 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2010-11-04 06:45:23 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-09-05 17:49:39 +08:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2014-06-13 01:19:42 +08:00
|
|
|
#include <system_error>
|
2010-11-24 05:53:15 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-01-12 04:42:05 +08:00
|
|
|
/// NON_EXISTENT_DIR - A special value distinct from null that is used to
|
2006-10-30 11:06:54 +08:00
|
|
|
/// represent a dir name that doesn't exist on the disk.
|
2008-01-12 04:42:05 +08:00
|
|
|
#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
|
2006-10-30 11:06:54 +08:00
|
|
|
|
2010-11-24 04:50:22 +08:00
|
|
|
/// NON_EXISTENT_FILE - A special value distinct from null that is used to
|
|
|
|
/// represent a filename that doesn't exist on the disk.
|
|
|
|
#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
|
|
|
|
|
2009-01-28 08:27:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Common logic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-02-24 11:15:25 +08:00
|
|
|
|
2014-02-21 05:59:23 +08:00
|
|
|
FileManager::FileManager(const FileSystemOptions &FSO,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> FS)
|
|
|
|
: FS(FS), FileSystemOpts(FSO),
|
2011-02-12 02:44:49 +08:00
|
|
|
SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
|
2008-02-24 11:15:25 +08:00
|
|
|
NumDirLookups = NumFileLookups = 0;
|
|
|
|
NumDirCacheMisses = NumFileCacheMisses = 0;
|
2014-02-21 05:59:23 +08:00
|
|
|
|
|
|
|
// If the caller doesn't provide a virtual file system, just grab the real
|
|
|
|
// file system.
|
|
|
|
if (!FS)
|
|
|
|
this->FS = vfs::getRealFileSystem();
|
2008-02-24 11:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FileManager::~FileManager() {
|
2010-11-24 04:30:42 +08:00
|
|
|
for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
|
|
|
|
delete VirtualFileEntries[i];
|
2011-02-12 02:44:49 +08:00
|
|
|
for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i)
|
|
|
|
delete VirtualDirectoryEntries[i];
|
2008-02-24 11:15:25 +08:00
|
|
|
}
|
|
|
|
|
2014-08-12 05:29:24 +08:00
|
|
|
void FileManager::addStatCache(std::unique_ptr<FileSystemStatCache> statCache,
|
2010-11-24 03:19:34 +08:00
|
|
|
bool AtBeginning) {
|
2009-10-17 02:18:30 +08:00
|
|
|
assert(statCache && "No stat cache provided?");
|
2014-05-08 14:41:40 +08:00
|
|
|
if (AtBeginning || !StatCache.get()) {
|
2014-08-12 05:29:24 +08:00
|
|
|
statCache->setNextStatCache(std::move(StatCache));
|
|
|
|
StatCache = std::move(statCache);
|
2009-10-17 02:18:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-24 03:19:34 +08:00
|
|
|
FileSystemStatCache *LastCache = StatCache.get();
|
2009-10-17 02:18:30 +08:00
|
|
|
while (LastCache->getNextStatCache())
|
|
|
|
LastCache = LastCache->getNextStatCache();
|
2014-08-12 05:29:24 +08:00
|
|
|
|
|
|
|
LastCache->setNextStatCache(std::move(statCache));
|
2009-10-17 02:18:30 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 03:19:34 +08:00
|
|
|
void FileManager::removeStatCache(FileSystemStatCache *statCache) {
|
2009-10-17 02:18:30 +08:00
|
|
|
if (!statCache)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (StatCache.get() == statCache) {
|
|
|
|
// This is the first stat cache.
|
2014-08-11 00:57:11 +08:00
|
|
|
StatCache = StatCache->takeNextStatCache();
|
2009-10-17 02:18:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the stat cache in the list.
|
2010-11-24 03:19:34 +08:00
|
|
|
FileSystemStatCache *PrevCache = StatCache.get();
|
2009-10-17 02:18:30 +08:00
|
|
|
while (PrevCache && PrevCache->getNextStatCache() != statCache)
|
|
|
|
PrevCache = PrevCache->getNextStatCache();
|
2010-11-24 04:50:22 +08:00
|
|
|
|
|
|
|
assert(PrevCache && "Stat cache not found for removal");
|
2014-08-12 05:29:24 +08:00
|
|
|
PrevCache->setNextStatCache(statCache->takeNextStatCache());
|
2009-10-17 02:18:30 +08:00
|
|
|
}
|
|
|
|
|
2012-07-31 21:56:54 +08:00
|
|
|
void FileManager::clearStatCaches() {
|
2014-07-19 09:06:45 +08:00
|
|
|
StatCache.reset();
|
2012-07-31 21:56:54 +08:00
|
|
|
}
|
|
|
|
|
2009-12-03 02:12:28 +08:00
|
|
|
/// \brief Retrieve the directory that the given file name resides in.
|
2011-02-12 02:44:49 +08:00
|
|
|
/// Filename can point to either a real file or a virtual file.
|
2009-12-03 02:12:28 +08:00
|
|
|
static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
|
2011-09-14 07:15:45 +08:00
|
|
|
StringRef Filename,
|
|
|
|
bool CacheFailure) {
|
2011-02-12 05:25:35 +08:00
|
|
|
if (Filename.empty())
|
2014-05-08 14:41:40 +08:00
|
|
|
return nullptr;
|
2011-02-12 02:44:49 +08:00
|
|
|
|
2011-02-12 05:25:35 +08:00
|
|
|
if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
|
2014-05-08 14:41:40 +08:00
|
|
|
return nullptr; // If Filename is a directory.
|
2010-11-21 19:32:22 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef DirName = llvm::sys::path::parent_path(Filename);
|
2010-11-21 17:50:16 +08:00
|
|
|
// Use the current directory if file has no path component.
|
2011-02-12 05:25:35 +08:00
|
|
|
if (DirName.empty())
|
|
|
|
DirName = ".";
|
2010-11-21 19:32:22 +08:00
|
|
|
|
2011-09-14 07:15:45 +08:00
|
|
|
return FileMgr.getDirectory(DirName, CacheFailure);
|
2009-12-03 02:12:28 +08:00
|
|
|
}
|
|
|
|
|
2011-02-12 02:44:49 +08:00
|
|
|
/// Add all ancestors of the given path (pointing to either a file or
|
|
|
|
/// a directory) as virtual directories.
|
2011-07-23 18:55:15 +08:00
|
|
|
void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
|
|
|
|
StringRef DirName = llvm::sys::path::parent_path(Path);
|
2011-02-12 05:25:35 +08:00
|
|
|
if (DirName.empty())
|
2011-02-12 02:44:49 +08:00
|
|
|
return;
|
|
|
|
|
2014-11-19 11:06:06 +08:00
|
|
|
auto &NamedDirEnt =
|
|
|
|
*SeenDirEntries.insert(std::make_pair(DirName, nullptr)).first;
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
// When caching a virtual directory, we always cache its ancestors
|
|
|
|
// at the same time. Therefore, if DirName is already in the cache,
|
|
|
|
// we don't need to recurse as its ancestors must also already be in
|
|
|
|
// the cache.
|
2014-11-19 11:06:06 +08:00
|
|
|
if (NamedDirEnt.second)
|
2011-02-12 02:44:49 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Add the virtual directory to the cache.
|
|
|
|
DirectoryEntry *UDE = new DirectoryEntry;
|
2014-11-19 11:06:06 +08:00
|
|
|
UDE->Name = NamedDirEnt.first().data();
|
|
|
|
NamedDirEnt.second = UDE;
|
2011-02-12 02:44:49 +08:00
|
|
|
VirtualDirectoryEntries.push_back(UDE);
|
|
|
|
|
|
|
|
// Recursively add the other ancestors.
|
|
|
|
addAncestorsAsVirtualDirs(DirName);
|
|
|
|
}
|
|
|
|
|
2011-09-14 07:15:45 +08:00
|
|
|
const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
|
|
|
|
bool CacheFailure) {
|
2012-06-16 14:04:10 +08:00
|
|
|
// stat doesn't like trailing separators except for root directory.
|
2011-11-17 14:16:05 +08:00
|
|
|
// At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
|
|
|
|
// (though it can strip '\\')
|
2012-06-16 14:04:10 +08:00
|
|
|
if (DirName.size() > 1 &&
|
|
|
|
DirName != llvm::sys::path::root_path(DirName) &&
|
|
|
|
llvm::sys::path::is_separator(DirName.back()))
|
2011-11-17 14:16:05 +08:00
|
|
|
DirName = DirName.substr(0, DirName.size()-1);
|
2013-07-29 23:47:24 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
// Fixing a problem with "clang C:test.c" on Windows.
|
|
|
|
// Stat("C:") does not recognize "C:" as a valid directory
|
|
|
|
std::string DirNameStr;
|
|
|
|
if (DirName.size() > 1 && DirName.back() == ':' &&
|
|
|
|
DirName.equals_lower(llvm::sys::path::root_name(DirName))) {
|
|
|
|
DirNameStr = DirName.str() + '.';
|
|
|
|
DirName = DirNameStr;
|
|
|
|
}
|
|
|
|
#endif
|
2011-11-17 14:16:05 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumDirLookups;
|
2014-11-19 11:06:06 +08:00
|
|
|
auto &NamedDirEnt =
|
|
|
|
*SeenDirEntries.insert(std::make_pair(DirName, nullptr)).first;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-12 02:44:49 +08:00
|
|
|
// See if there was already an entry in the map. Note that the map
|
|
|
|
// contains both virtual and real directories.
|
2014-11-19 11:06:06 +08:00
|
|
|
if (NamedDirEnt.second)
|
|
|
|
return NamedDirEnt.second == NON_EXISTENT_DIR ? nullptr
|
|
|
|
: NamedDirEnt.second;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumDirCacheMisses;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-30 11:06:54 +08:00
|
|
|
// By default, initialize it to invalid.
|
2014-11-19 11:06:06 +08:00
|
|
|
NamedDirEnt.second = NON_EXISTENT_DIR;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-30 11:40:58 +08:00
|
|
|
// Get the null-terminated directory name as stored as the key of the
|
2011-02-12 02:44:49 +08:00
|
|
|
// SeenDirEntries map.
|
2014-11-19 11:06:06 +08:00
|
|
|
const char *InterndDirName = NamedDirEnt.first().data();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-30 11:06:54 +08:00
|
|
|
// Check to see if the directory exists.
|
2013-08-02 05:42:11 +08:00
|
|
|
FileData Data;
|
2014-05-08 14:41:40 +08:00
|
|
|
if (getStatValue(InterndDirName, Data, false, nullptr /*directory lookup*/)) {
|
2011-02-12 02:44:49 +08:00
|
|
|
// There's no real directory at the given path.
|
2011-09-14 07:15:45 +08:00
|
|
|
if (!CacheFailure)
|
|
|
|
SeenDirEntries.erase(DirName);
|
2014-05-08 14:41:40 +08:00
|
|
|
return nullptr;
|
2011-02-12 02:44:49 +08:00
|
|
|
}
|
2008-02-24 11:15:25 +08:00
|
|
|
|
2011-02-12 02:44:49 +08:00
|
|
|
// It exists. See if we have already opened a directory with the
|
|
|
|
// same inode (this occurs on Unix-like systems when one dir is
|
|
|
|
// symlinked to another, for example) or the same path (on
|
|
|
|
// Windows).
|
2014-02-28 06:21:32 +08:00
|
|
|
DirectoryEntry &UDE = UniqueRealDirs[Data.UniqueID];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-11-19 11:06:06 +08:00
|
|
|
NamedDirEnt.second = &UDE;
|
2011-02-12 02:44:49 +08:00
|
|
|
if (!UDE.getName()) {
|
|
|
|
// We don't have this directory yet, add it. We use the string
|
|
|
|
// key from the SeenDirEntries map as the string.
|
|
|
|
UDE.Name = InterndDirName;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-02-09 03:08:49 +08:00
|
|
|
return &UDE;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2011-09-14 07:15:45 +08:00
|
|
|
const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
|
|
|
|
bool CacheFailure) {
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumFileLookups;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// See if there is already an entry in the map.
|
2014-11-19 11:06:06 +08:00
|
|
|
auto &NamedFileEnt =
|
|
|
|
*SeenFileEntries.insert(std::make_pair(Filename, nullptr)).first;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-10-30 11:55:17 +08:00
|
|
|
// See if there is already an entry in the map.
|
2014-11-19 11:06:06 +08:00
|
|
|
if (NamedFileEnt.second)
|
|
|
|
return NamedFileEnt.second == NON_EXISTENT_FILE ? nullptr
|
|
|
|
: NamedFileEnt.second;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
++NumFileCacheMisses;
|
|
|
|
|
2006-10-30 11:55:17 +08:00
|
|
|
// By default, initialize it to invalid.
|
2014-11-19 11:06:06 +08:00
|
|
|
NamedFileEnt.second = NON_EXISTENT_FILE;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-10-30 11:55:17 +08:00
|
|
|
// Get the null-terminated file name as stored as the key of the
|
2011-02-12 02:44:49 +08:00
|
|
|
// SeenFileEntries map.
|
2014-11-19 11:06:06 +08:00
|
|
|
const char *InterndFileName = NamedFileEnt.first().data();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-11-24 04:30:42 +08:00
|
|
|
// Look up the directory for the file. When looking up something like
|
|
|
|
// sys/foo.h we'll discover all of the search directories that have a 'sys'
|
|
|
|
// subdirectory. This will let us avoid having to waste time on known-to-fail
|
|
|
|
// searches when we go to find sys/bar.h, because all the search directories
|
|
|
|
// without a 'sys' subdir will get a cached failure result.
|
2011-09-14 07:15:45 +08:00
|
|
|
const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
|
|
|
|
CacheFailure);
|
2014-05-08 14:41:40 +08:00
|
|
|
if (DirInfo == nullptr) { // Directory doesn't exist, file can't exist.
|
2011-09-14 07:15:45 +08:00
|
|
|
if (!CacheFailure)
|
|
|
|
SeenFileEntries.erase(Filename);
|
2014-05-08 14:41:40 +08:00
|
|
|
|
|
|
|
return nullptr;
|
2011-09-14 07:15:45 +08:00
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// FIXME: Use the directory info to prune this, before doing the stat syscall.
|
|
|
|
// FIXME: This will reduce the # syscalls.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Nope, there isn't. Check to see if the file exists.
|
2014-07-08 23:46:02 +08:00
|
|
|
std::unique_ptr<vfs::File> F;
|
2013-08-02 05:42:11 +08:00
|
|
|
FileData Data;
|
2014-05-08 14:41:40 +08:00
|
|
|
if (getStatValue(InterndFileName, Data, true, openFile ? &F : nullptr)) {
|
2011-02-12 02:44:49 +08:00
|
|
|
// There's no real file at the given path.
|
2011-09-14 07:15:45 +08:00
|
|
|
if (!CacheFailure)
|
|
|
|
SeenFileEntries.erase(Filename);
|
2014-05-08 14:41:40 +08:00
|
|
|
|
|
|
|
return nullptr;
|
2011-02-12 02:44:49 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-02-21 15:23:53 +08:00
|
|
|
assert((openFile || !F) && "undesired open file");
|
2011-03-17 03:17:25 +08:00
|
|
|
|
2007-12-19 06:29:39 +08:00
|
|
|
// It exists. See if we have already opened a file with the same inode.
|
2006-06-18 13:43:12 +08:00
|
|
|
// This occurs when one dir is symlinked to another, for example.
|
2014-02-28 06:21:32 +08:00
|
|
|
FileEntry &UFE = UniqueRealFiles[Data.UniqueID];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-11-19 11:06:06 +08:00
|
|
|
NamedFileEnt.second = &UFE;
|
2014-09-09 00:15:54 +08:00
|
|
|
|
|
|
|
// If the name returned by getStatValue is different than Filename, re-intern
|
|
|
|
// the name.
|
|
|
|
if (Data.Name != Filename) {
|
2014-11-19 11:06:06 +08:00
|
|
|
auto &NamedFileEnt =
|
|
|
|
*SeenFileEntries.insert(std::make_pair(Data.Name, nullptr)).first;
|
|
|
|
if (!NamedFileEnt.second)
|
|
|
|
NamedFileEnt.second = &UFE;
|
2014-09-09 00:15:54 +08:00
|
|
|
else
|
2014-11-19 11:06:06 +08:00
|
|
|
assert(NamedFileEnt.second == &UFE &&
|
2014-09-09 00:15:54 +08:00
|
|
|
"filename from getStatValue() refers to wrong file");
|
2014-11-19 11:06:06 +08:00
|
|
|
InterndFileName = NamedFileEnt.first().data();
|
2014-09-09 00:15:54 +08:00
|
|
|
}
|
|
|
|
|
2014-02-28 01:23:33 +08:00
|
|
|
if (UFE.isValid()) { // Already have an entry with this inode, return it.
|
2014-05-24 02:15:47 +08:00
|
|
|
|
|
|
|
// FIXME: this hack ensures that if we look up a file by a virtual path in
|
|
|
|
// the VFS that the getDir() will have the virtual path, even if we found
|
|
|
|
// the file by a 'real' path first. This is required in order to find a
|
|
|
|
// module's structure when its headers/module map are mapped in the VFS.
|
|
|
|
// We should remove this as soon as we can properly support a file having
|
|
|
|
// multiple names.
|
|
|
|
if (DirInfo != UFE.Dir && Data.IsVFSMapped)
|
|
|
|
UFE.Dir = DirInfo;
|
|
|
|
|
2014-08-13 20:34:41 +08:00
|
|
|
// Always update the name to use the last name by which a file was accessed.
|
|
|
|
// FIXME: Neither this nor always using the first name is correct; we want
|
|
|
|
// to switch towards a design where we return a FileName object that
|
|
|
|
// encapsulates both the name by which the file was accessed and the
|
|
|
|
// corresponding FileEntry.
|
2014-09-09 00:15:54 +08:00
|
|
|
UFE.Name = InterndFileName;
|
2014-08-13 20:34:41 +08:00
|
|
|
|
2007-02-09 03:08:49 +08:00
|
|
|
return &UFE;
|
2010-11-24 05:17:56 +08:00
|
|
|
}
|
2006-06-25 14:23:00 +08:00
|
|
|
|
2014-02-28 06:21:32 +08:00
|
|
|
// Otherwise, we don't have this file yet, add it.
|
2014-09-09 00:15:54 +08:00
|
|
|
UFE.Name = InterndFileName;
|
2013-08-02 05:42:11 +08:00
|
|
|
UFE.Size = Data.Size;
|
|
|
|
UFE.ModTime = Data.ModTime;
|
2006-10-30 11:55:17 +08:00
|
|
|
UFE.Dir = DirInfo;
|
|
|
|
UFE.UID = NextFileUID++;
|
2014-02-28 06:21:32 +08:00
|
|
|
UFE.UniqueID = Data.UniqueID;
|
|
|
|
UFE.IsNamedPipe = Data.IsNamedPipe;
|
|
|
|
UFE.InPCH = Data.InPCH;
|
2014-07-08 23:46:02 +08:00
|
|
|
UFE.File = std::move(F);
|
2014-02-28 01:23:33 +08:00
|
|
|
UFE.IsValid = true;
|
2007-02-09 03:08:49 +08:00
|
|
|
return &UFE;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2009-12-03 02:12:28 +08:00
|
|
|
const FileEntry *
|
2011-07-23 18:55:15 +08:00
|
|
|
FileManager::getVirtualFile(StringRef Filename, off_t Size,
|
2010-11-23 16:35:12 +08:00
|
|
|
time_t ModificationTime) {
|
2009-12-03 02:12:28 +08:00
|
|
|
++NumFileLookups;
|
|
|
|
|
|
|
|
// See if there is already an entry in the map.
|
2014-11-19 11:06:06 +08:00
|
|
|
auto &NamedFileEnt =
|
|
|
|
*SeenFileEntries.insert(std::make_pair(Filename, nullptr)).first;
|
2009-12-03 02:12:28 +08:00
|
|
|
|
|
|
|
// See if there is already an entry in the map.
|
2014-11-19 11:06:06 +08:00
|
|
|
if (NamedFileEnt.second && NamedFileEnt.second != NON_EXISTENT_FILE)
|
|
|
|
return NamedFileEnt.second;
|
2009-12-03 02:12:28 +08:00
|
|
|
|
|
|
|
++NumFileCacheMisses;
|
|
|
|
|
|
|
|
// By default, initialize it to invalid.
|
2014-11-19 11:06:06 +08:00
|
|
|
NamedFileEnt.second = NON_EXISTENT_FILE;
|
2009-12-03 02:12:28 +08:00
|
|
|
|
2011-02-12 02:44:49 +08:00
|
|
|
addAncestorsAsVirtualDirs(Filename);
|
2014-05-08 14:41:40 +08:00
|
|
|
FileEntry *UFE = nullptr;
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
// Now that all ancestors of Filename are in the cache, the
|
|
|
|
// following call is guaranteed to find the DirectoryEntry from the
|
|
|
|
// cache.
|
2011-09-14 07:15:45 +08:00
|
|
|
const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
|
|
|
|
/*CacheFailure=*/true);
|
2011-02-12 02:44:49 +08:00
|
|
|
assert(DirInfo &&
|
|
|
|
"The directory of a virtual file should already be in the cache.");
|
|
|
|
|
|
|
|
// Check to see if the file exists. If so, drop the virtual file
|
2013-08-02 05:42:11 +08:00
|
|
|
FileData Data;
|
2014-11-19 11:06:06 +08:00
|
|
|
const char *InterndFileName = NamedFileEnt.first().data();
|
2014-05-08 14:41:40 +08:00
|
|
|
if (getStatValue(InterndFileName, Data, true, nullptr) == 0) {
|
2013-08-02 05:42:11 +08:00
|
|
|
Data.Size = Size;
|
|
|
|
Data.ModTime = ModificationTime;
|
2014-02-28 06:21:32 +08:00
|
|
|
UFE = &UniqueRealFiles[Data.UniqueID];
|
2011-02-12 02:44:49 +08:00
|
|
|
|
2014-11-19 11:06:06 +08:00
|
|
|
NamedFileEnt.second = UFE;
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
// If we had already opened this file, close it now so we don't
|
|
|
|
// leak the descriptor. We're not going to use the file
|
|
|
|
// descriptor anyway, since this is a virtual file.
|
2014-02-21 05:59:23 +08:00
|
|
|
if (UFE->File)
|
|
|
|
UFE->closeFile();
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
// If we already have an entry with this inode, return it.
|
2014-02-28 01:23:33 +08:00
|
|
|
if (UFE->isValid())
|
2011-02-12 02:44:49 +08:00
|
|
|
return UFE;
|
2014-02-28 06:21:32 +08:00
|
|
|
|
|
|
|
UFE->UniqueID = Data.UniqueID;
|
|
|
|
UFE->IsNamedPipe = Data.IsNamedPipe;
|
|
|
|
UFE->InPCH = Data.InPCH;
|
2011-02-06 03:42:43 +08:00
|
|
|
}
|
2009-12-03 02:12:28 +08:00
|
|
|
|
2011-02-06 03:42:43 +08:00
|
|
|
if (!UFE) {
|
|
|
|
UFE = new FileEntry();
|
|
|
|
VirtualFileEntries.push_back(UFE);
|
2014-11-19 11:06:06 +08:00
|
|
|
NamedFileEnt.second = UFE;
|
2011-02-06 03:42:43 +08:00
|
|
|
}
|
2009-12-03 02:12:28 +08:00
|
|
|
|
2010-11-24 04:50:22 +08:00
|
|
|
UFE->Name = InterndFileName;
|
2009-12-03 02:12:28 +08:00
|
|
|
UFE->Size = Size;
|
|
|
|
UFE->ModTime = ModificationTime;
|
|
|
|
UFE->Dir = DirInfo;
|
|
|
|
UFE->UID = NextFileUID++;
|
2014-02-21 05:59:23 +08:00
|
|
|
UFE->File.reset();
|
2009-12-03 02:12:28 +08:00
|
|
|
return UFE;
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
|
|
|
|
StringRef pathRef(path.data(), path.size());
|
2011-03-07 06:25:35 +08:00
|
|
|
|
2011-03-14 09:13:54 +08:00
|
|
|
if (FileSystemOpts.WorkingDir.empty()
|
|
|
|
|| llvm::sys::path::is_absolute(pathRef))
|
2010-12-18 05:22:22 +08:00
|
|
|
return;
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> NewPath(FileSystemOpts.WorkingDir);
|
2011-03-07 06:25:35 +08:00
|
|
|
llvm::sys::path::append(NewPath, pathRef);
|
2010-11-23 12:45:28 +08:00
|
|
|
path = NewPath;
|
|
|
|
}
|
|
|
|
|
2014-10-27 06:44:13 +08:00
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
|
|
|
|
FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile,
|
|
|
|
bool ShouldCloseOpenFile) {
|
2012-07-12 04:59:04 +08:00
|
|
|
uint64_t FileSize = Entry->getSize();
|
|
|
|
// If there's a high enough chance that the file have changed since we
|
|
|
|
// got its size, force a stat before opening it.
|
|
|
|
if (isVolatile)
|
|
|
|
FileSize = -1;
|
|
|
|
|
2011-03-15 08:47:44 +08:00
|
|
|
const char *Filename = Entry->getName();
|
|
|
|
// If the file is already open, use the open file descriptor.
|
2014-02-21 05:59:23 +08:00
|
|
|
if (Entry->File) {
|
2014-10-27 06:44:13 +08:00
|
|
|
auto Result =
|
|
|
|
Entry->File->getBuffer(Filename, FileSize,
|
|
|
|
/*RequiresNullTerminator=*/true, isVolatile);
|
2014-06-20 08:24:56 +08:00
|
|
|
// FIXME: we need a set of APIs that can make guarantees about whether a
|
|
|
|
// FileEntry is open or not.
|
|
|
|
if (ShouldCloseOpenFile)
|
|
|
|
Entry->closeFile();
|
2014-08-27 03:54:40 +08:00
|
|
|
return Result;
|
2011-03-15 08:47:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, open the file.
|
|
|
|
|
2014-10-27 06:44:13 +08:00
|
|
|
if (FileSystemOpts.WorkingDir.empty())
|
|
|
|
return FS->getBufferForFile(Filename, FileSize,
|
|
|
|
/*RequiresNullTerminator=*/true, isVolatile);
|
2011-03-07 06:25:35 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> FilePath(Entry->getName());
|
2011-03-07 09:28:33 +08:00
|
|
|
FixupRelativePath(FilePath);
|
2015-03-18 18:17:07 +08:00
|
|
|
return FS->getBufferForFile(FilePath, FileSize,
|
2014-10-27 06:44:13 +08:00
|
|
|
/*RequiresNullTerminator=*/true, isVolatile);
|
2010-11-23 17:19:42 +08:00
|
|
|
}
|
2010-11-23 12:45:28 +08:00
|
|
|
|
2014-10-27 06:44:13 +08:00
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
|
|
|
|
FileManager::getBufferForFile(StringRef Filename) {
|
|
|
|
if (FileSystemOpts.WorkingDir.empty())
|
|
|
|
return FS->getBufferForFile(Filename);
|
2010-12-10 01:36:38 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> FilePath(Filename);
|
2011-03-07 09:28:33 +08:00
|
|
|
FixupRelativePath(FilePath);
|
2014-10-27 06:44:13 +08:00
|
|
|
return FS->getBufferForFile(FilePath.c_str());
|
2010-11-04 06:45:23 +08:00
|
|
|
}
|
|
|
|
|
2011-02-12 02:44:49 +08:00
|
|
|
/// getStatValue - Get the 'stat' information for the specified path,
|
|
|
|
/// using the cache to accelerate it if possible. This returns true
|
|
|
|
/// if the path points to a virtual file or does not exist, or returns
|
|
|
|
/// false if it's an existent real file. If FileDescriptor is NULL,
|
|
|
|
/// do directory look-up instead of file look-up.
|
2013-08-02 05:42:11 +08:00
|
|
|
bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile,
|
2014-07-08 23:46:02 +08:00
|
|
|
std::unique_ptr<vfs::File> *F) {
|
2010-11-24 03:19:34 +08:00
|
|
|
// FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
|
|
|
|
// absolute!
|
2010-11-24 03:56:39 +08:00
|
|
|
if (FileSystemOpts.WorkingDir.empty())
|
2014-02-21 05:59:23 +08:00
|
|
|
return FileSystemStatCache::get(Path, Data, isFile, F,StatCache.get(), *FS);
|
2011-02-12 02:44:49 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> FilePath(Path);
|
2011-03-07 09:28:33 +08:00
|
|
|
FixupRelativePath(FilePath);
|
2010-11-24 03:56:39 +08:00
|
|
|
|
2014-02-21 05:59:23 +08:00
|
|
|
return FileSystemStatCache::get(FilePath.c_str(), Data, isFile, F,
|
|
|
|
StatCache.get(), *FS);
|
2010-11-04 06:45:23 +08:00
|
|
|
}
|
|
|
|
|
2013-07-30 02:22:23 +08:00
|
|
|
bool FileManager::getNoncachedStatValue(StringRef Path,
|
2014-02-21 05:59:23 +08:00
|
|
|
vfs::Status &Result) {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> FilePath(Path);
|
2011-03-19 03:23:19 +08:00
|
|
|
FixupRelativePath(FilePath);
|
|
|
|
|
2014-02-21 05:59:23 +08:00
|
|
|
llvm::ErrorOr<vfs::Status> S = FS->status(FilePath.c_str());
|
|
|
|
if (!S)
|
|
|
|
return true;
|
|
|
|
Result = *S;
|
|
|
|
return false;
|
2011-03-19 03:23:19 +08:00
|
|
|
}
|
|
|
|
|
2012-07-11 00:50:27 +08:00
|
|
|
void FileManager::invalidateCache(const FileEntry *Entry) {
|
|
|
|
assert(Entry && "Cannot invalidate a NULL FileEntry");
|
2012-06-27 17:17:42 +08:00
|
|
|
|
|
|
|
SeenFileEntries.erase(Entry->getName());
|
2012-07-11 00:50:27 +08:00
|
|
|
|
|
|
|
// FileEntry invalidation should not block future optimizations in the file
|
|
|
|
// caches. Possible alternatives are cache truncation (invalidate last N) or
|
|
|
|
// invalidation of the whole cache.
|
2014-02-28 06:21:32 +08:00
|
|
|
UniqueRealFiles.erase(Entry->getUniqueID());
|
2012-06-27 17:17:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
Implement two related optimizations that make de-serialization of
AST/PCH files more lazy:
- Don't preload all of the file source-location entries when reading
the AST file. Instead, load them lazily, when needed.
- Only look up header-search information (whether a header was already
#import'd, how many times it's been included, etc.) when it's needed
by the preprocessor, rather than pre-populating it.
Previously, we would pre-load all of the file source-location entries,
which also populated the header-search information structure. This was
a relatively minor performance issue, since we would end up stat()'ing
all of the headers stored within a AST/PCH file when the AST/PCH file
was loaded. In the normal PCH use case, the stat()s were cached, so
the cost--of preloading ~860 source-location entries in the Cocoa.h
case---was relatively low.
However, the recent optimization that replaced stat+open with
open+fstat turned this into a major problem, since the preloading of
source-location entries would now end up opening those files. Worse,
those files wouldn't be closed until the file manager was destroyed,
so just opening a Cocoa.h PCH file would hold on to ~860 file
descriptors, and it was easy to blow through the process's limit on
the number of open file descriptors.
By eliminating the preloading of these files, we neither open nor stat
the headers stored in the PCH/AST file until they're actually needed
for something. Concretely, we went from
*** HeaderSearch Stats:
835 files tracked.
364 #import/#pragma once files.
823 included exactly once.
6 max times a file is included.
3 #include/#include_next/#import.
0 #includes skipped due to the multi-include optimization.
1 framework lookups.
0 subframework lookups.
*** Source Manager Stats:
835 files mapped, 3 mem buffers mapped.
37460 SLocEntry's allocated, 11215575B of Sloc address space used.
62 bytes of files mapped, 0 files with line #'s computed.
with a trivial program that uses a chained PCH including a Cocoa PCH
to
*** HeaderSearch Stats:
4 files tracked.
1 #import/#pragma once files.
3 included exactly once.
2 max times a file is included.
3 #include/#include_next/#import.
0 #includes skipped due to the multi-include optimization.
1 framework lookups.
0 subframework lookups.
*** Source Manager Stats:
3 files mapped, 3 mem buffers mapped.
37460 SLocEntry's allocated, 11215575B of Sloc address space used.
62 bytes of files mapped, 0 files with line #'s computed.
for the same program.
llvm-svn: 125286
2011-02-11 01:09:37 +08:00
|
|
|
void FileManager::GetUniqueIDMapping(
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
|
Implement two related optimizations that make de-serialization of
AST/PCH files more lazy:
- Don't preload all of the file source-location entries when reading
the AST file. Instead, load them lazily, when needed.
- Only look up header-search information (whether a header was already
#import'd, how many times it's been included, etc.) when it's needed
by the preprocessor, rather than pre-populating it.
Previously, we would pre-load all of the file source-location entries,
which also populated the header-search information structure. This was
a relatively minor performance issue, since we would end up stat()'ing
all of the headers stored within a AST/PCH file when the AST/PCH file
was loaded. In the normal PCH use case, the stat()s were cached, so
the cost--of preloading ~860 source-location entries in the Cocoa.h
case---was relatively low.
However, the recent optimization that replaced stat+open with
open+fstat turned this into a major problem, since the preloading of
source-location entries would now end up opening those files. Worse,
those files wouldn't be closed until the file manager was destroyed,
so just opening a Cocoa.h PCH file would hold on to ~860 file
descriptors, and it was easy to blow through the process's limit on
the number of open file descriptors.
By eliminating the preloading of these files, we neither open nor stat
the headers stored in the PCH/AST file until they're actually needed
for something. Concretely, we went from
*** HeaderSearch Stats:
835 files tracked.
364 #import/#pragma once files.
823 included exactly once.
6 max times a file is included.
3 #include/#include_next/#import.
0 #includes skipped due to the multi-include optimization.
1 framework lookups.
0 subframework lookups.
*** Source Manager Stats:
835 files mapped, 3 mem buffers mapped.
37460 SLocEntry's allocated, 11215575B of Sloc address space used.
62 bytes of files mapped, 0 files with line #'s computed.
with a trivial program that uses a chained PCH including a Cocoa PCH
to
*** HeaderSearch Stats:
4 files tracked.
1 #import/#pragma once files.
3 included exactly once.
2 max times a file is included.
3 #include/#include_next/#import.
0 #includes skipped due to the multi-include optimization.
1 framework lookups.
0 subframework lookups.
*** Source Manager Stats:
3 files mapped, 3 mem buffers mapped.
37460 SLocEntry's allocated, 11215575B of Sloc address space used.
62 bytes of files mapped, 0 files with line #'s computed.
for the same program.
llvm-svn: 125286
2011-02-11 01:09:37 +08:00
|
|
|
UIDToFiles.clear();
|
|
|
|
UIDToFiles.resize(NextFileUID);
|
|
|
|
|
|
|
|
// Map file entries
|
|
|
|
for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
|
2011-02-12 02:44:49 +08:00
|
|
|
FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
|
Implement two related optimizations that make de-serialization of
AST/PCH files more lazy:
- Don't preload all of the file source-location entries when reading
the AST file. Instead, load them lazily, when needed.
- Only look up header-search information (whether a header was already
#import'd, how many times it's been included, etc.) when it's needed
by the preprocessor, rather than pre-populating it.
Previously, we would pre-load all of the file source-location entries,
which also populated the header-search information structure. This was
a relatively minor performance issue, since we would end up stat()'ing
all of the headers stored within a AST/PCH file when the AST/PCH file
was loaded. In the normal PCH use case, the stat()s were cached, so
the cost--of preloading ~860 source-location entries in the Cocoa.h
case---was relatively low.
However, the recent optimization that replaced stat+open with
open+fstat turned this into a major problem, since the preloading of
source-location entries would now end up opening those files. Worse,
those files wouldn't be closed until the file manager was destroyed,
so just opening a Cocoa.h PCH file would hold on to ~860 file
descriptors, and it was easy to blow through the process's limit on
the number of open file descriptors.
By eliminating the preloading of these files, we neither open nor stat
the headers stored in the PCH/AST file until they're actually needed
for something. Concretely, we went from
*** HeaderSearch Stats:
835 files tracked.
364 #import/#pragma once files.
823 included exactly once.
6 max times a file is included.
3 #include/#include_next/#import.
0 #includes skipped due to the multi-include optimization.
1 framework lookups.
0 subframework lookups.
*** Source Manager Stats:
835 files mapped, 3 mem buffers mapped.
37460 SLocEntry's allocated, 11215575B of Sloc address space used.
62 bytes of files mapped, 0 files with line #'s computed.
with a trivial program that uses a chained PCH including a Cocoa PCH
to
*** HeaderSearch Stats:
4 files tracked.
1 #import/#pragma once files.
3 included exactly once.
2 max times a file is included.
3 #include/#include_next/#import.
0 #includes skipped due to the multi-include optimization.
1 framework lookups.
0 subframework lookups.
*** Source Manager Stats:
3 files mapped, 3 mem buffers mapped.
37460 SLocEntry's allocated, 11215575B of Sloc address space used.
62 bytes of files mapped, 0 files with line #'s computed.
for the same program.
llvm-svn: 125286
2011-02-11 01:09:37 +08:00
|
|
|
FE != FEEnd; ++FE)
|
|
|
|
if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
|
|
|
|
UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
|
|
|
|
|
|
|
|
// Map virtual file entries
|
2013-07-04 11:08:24 +08:00
|
|
|
for (SmallVectorImpl<FileEntry *>::const_iterator
|
Implement two related optimizations that make de-serialization of
AST/PCH files more lazy:
- Don't preload all of the file source-location entries when reading
the AST file. Instead, load them lazily, when needed.
- Only look up header-search information (whether a header was already
#import'd, how many times it's been included, etc.) when it's needed
by the preprocessor, rather than pre-populating it.
Previously, we would pre-load all of the file source-location entries,
which also populated the header-search information structure. This was
a relatively minor performance issue, since we would end up stat()'ing
all of the headers stored within a AST/PCH file when the AST/PCH file
was loaded. In the normal PCH use case, the stat()s were cached, so
the cost--of preloading ~860 source-location entries in the Cocoa.h
case---was relatively low.
However, the recent optimization that replaced stat+open with
open+fstat turned this into a major problem, since the preloading of
source-location entries would now end up opening those files. Worse,
those files wouldn't be closed until the file manager was destroyed,
so just opening a Cocoa.h PCH file would hold on to ~860 file
descriptors, and it was easy to blow through the process's limit on
the number of open file descriptors.
By eliminating the preloading of these files, we neither open nor stat
the headers stored in the PCH/AST file until they're actually needed
for something. Concretely, we went from
*** HeaderSearch Stats:
835 files tracked.
364 #import/#pragma once files.
823 included exactly once.
6 max times a file is included.
3 #include/#include_next/#import.
0 #includes skipped due to the multi-include optimization.
1 framework lookups.
0 subframework lookups.
*** Source Manager Stats:
835 files mapped, 3 mem buffers mapped.
37460 SLocEntry's allocated, 11215575B of Sloc address space used.
62 bytes of files mapped, 0 files with line #'s computed.
with a trivial program that uses a chained PCH including a Cocoa PCH
to
*** HeaderSearch Stats:
4 files tracked.
1 #import/#pragma once files.
3 included exactly once.
2 max times a file is included.
3 #include/#include_next/#import.
0 #includes skipped due to the multi-include optimization.
1 framework lookups.
0 subframework lookups.
*** Source Manager Stats:
3 files mapped, 3 mem buffers mapped.
37460 SLocEntry's allocated, 11215575B of Sloc address space used.
62 bytes of files mapped, 0 files with line #'s computed.
for the same program.
llvm-svn: 125286
2011-02-11 01:09:37 +08:00
|
|
|
VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end();
|
|
|
|
VFE != VFEEnd; ++VFE)
|
|
|
|
if (*VFE && *VFE != NON_EXISTENT_FILE)
|
|
|
|
UIDToFiles[(*VFE)->getUID()] = *VFE;
|
|
|
|
}
|
2010-11-24 03:19:34 +08:00
|
|
|
|
2012-05-04 05:50:39 +08:00
|
|
|
void FileManager::modifyFileEntry(FileEntry *File,
|
|
|
|
off_t Size, time_t ModificationTime) {
|
|
|
|
File->Size = Size;
|
|
|
|
File->ModTime = ModificationTime;
|
|
|
|
}
|
|
|
|
|
2014-12-12 04:50:24 +08:00
|
|
|
/// Remove '.' path components from the given absolute path.
|
|
|
|
/// \return \c true if any changes were made.
|
|
|
|
// FIXME: Move this to llvm::sys::path.
|
|
|
|
bool FileManager::removeDotPaths(SmallVectorImpl<char> &Path) {
|
|
|
|
using namespace llvm::sys;
|
|
|
|
|
|
|
|
SmallVector<StringRef, 16> ComponentStack;
|
|
|
|
StringRef P(Path.data(), Path.size());
|
|
|
|
|
|
|
|
// Skip the root path, then look for traversal in the components.
|
|
|
|
StringRef Rel = path::relative_path(P);
|
|
|
|
bool AnyDots = false;
|
|
|
|
for (StringRef C : llvm::make_range(path::begin(Rel), path::end(Rel))) {
|
|
|
|
if (C == ".") {
|
|
|
|
AnyDots = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ComponentStack.push_back(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AnyDots)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallString<256> Buffer = path::root_path(P);
|
|
|
|
for (StringRef C : ComponentStack)
|
|
|
|
path::append(Buffer, C);
|
|
|
|
|
|
|
|
Path.swap(Buffer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-26 08:55:12 +08:00
|
|
|
StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
|
|
|
|
// FIXME: use llvm::sys::fs::canonical() when it gets implemented
|
|
|
|
llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known
|
|
|
|
= CanonicalDirNames.find(Dir);
|
|
|
|
if (Known != CanonicalDirNames.end())
|
|
|
|
return Known->second;
|
|
|
|
|
|
|
|
StringRef CanonicalName(Dir->getName());
|
2014-12-12 04:50:24 +08:00
|
|
|
|
|
|
|
#ifdef LLVM_ON_UNIX
|
2013-01-26 08:55:12 +08:00
|
|
|
char CanonicalNameBuf[PATH_MAX];
|
|
|
|
if (realpath(Dir->getName(), CanonicalNameBuf)) {
|
|
|
|
unsigned Len = strlen(CanonicalNameBuf);
|
|
|
|
char *Mem = static_cast<char *>(CanonicalNameStorage.Allocate(Len, 1));
|
|
|
|
memcpy(Mem, CanonicalNameBuf, Len);
|
|
|
|
CanonicalName = StringRef(Mem, Len);
|
|
|
|
}
|
2014-12-12 04:50:24 +08:00
|
|
|
#else
|
|
|
|
SmallString<256> CanonicalNameBuf(CanonicalName);
|
|
|
|
llvm::sys::fs::make_absolute(CanonicalNameBuf);
|
|
|
|
llvm::sys::path::native(CanonicalNameBuf);
|
|
|
|
removeDotPaths(CanonicalNameBuf);
|
|
|
|
#endif
|
2013-01-26 08:55:12 +08:00
|
|
|
|
|
|
|
CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName));
|
|
|
|
return CanonicalName;
|
|
|
|
}
|
2010-11-24 03:19:34 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
void FileManager::PrintStats() const {
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << "\n*** File Manager Stats:\n";
|
2011-02-12 02:44:49 +08:00
|
|
|
llvm::errs() << UniqueRealFiles.size() << " real files found, "
|
|
|
|
<< UniqueRealDirs.size() << " real dirs found.\n";
|
|
|
|
llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
|
|
|
|
<< VirtualDirectoryEntries.size() << " virtual dirs found.\n";
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << NumDirLookups << " dir lookups, "
|
|
|
|
<< NumDirCacheMisses << " dir cache misses.\n";
|
|
|
|
llvm::errs() << NumFileLookups << " file lookups, "
|
|
|
|
<< NumFileCacheMisses << " file cache misses.\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-23 20:08:50 +08:00
|
|
|
//llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|