Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 1 | //===--- FileManager.cpp - File System Probing and Caching ----------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the FileManager interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | // TODO: This should index all interesting directories with dirent calls. |
| 15 | // getdirentries ? |
| 16 | // opendir/readdir_r/closedir ? |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileSystemStatCache.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "llvm/Config/llvm-config.h" |
Michael J. Spencer | fbfd180 | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 28 | #include "llvm/Support/system_error.h" |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 29 | #include <map> |
| 30 | #include <set> |
| 31 | #include <string> |
Chris Lattner | 291fcf0 | 2010-11-23 21:53:15 +0000 | [diff] [blame] | 32 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | |
| 35 | // FIXME: Enhance libsystem to support inode and other fields. |
| 36 | #include <sys/stat.h> |
| 37 | |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 38 | /// NON_EXISTENT_DIR - A special value distinct from null that is used to |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | /// represent a dir name that doesn't exist on the disk. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 40 | #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 42 | /// NON_EXISTENT_FILE - A special value distinct from null that is used to |
| 43 | /// represent a filename that doesn't exist on the disk. |
| 44 | #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1) |
| 45 | |
Ted Kremenek | cb8d58b | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | // Common logic. |
| 48 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 49 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 50 | FileManager::FileManager(const FileSystemOptions &FSO, |
| 51 | IntrusiveRefCntPtr<vfs::FileSystem> FS) |
| 52 | : FS(FS), FileSystemOpts(FSO), |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 53 | SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) { |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 54 | NumDirLookups = NumFileLookups = 0; |
| 55 | NumDirCacheMisses = NumFileCacheMisses = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 56 | |
| 57 | // If the caller doesn't provide a virtual file system, just grab the real |
| 58 | // file system. |
| 59 | if (!FS) |
| 60 | this->FS = vfs::getRealFileSystem(); |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | FileManager::~FileManager() { |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 64 | for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i) |
| 65 | delete VirtualFileEntries[i]; |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 66 | for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i) |
| 67 | delete VirtualDirectoryEntries[i]; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 70 | void FileManager::addStatCache(FileSystemStatCache *statCache, |
| 71 | bool AtBeginning) { |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 72 | assert(statCache && "No stat cache provided?"); |
| 73 | if (AtBeginning || StatCache.get() == 0) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 74 | statCache->setNextStatCache(StatCache.release()); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 75 | StatCache.reset(statCache); |
| 76 | return; |
| 77 | } |
| 78 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 79 | FileSystemStatCache *LastCache = StatCache.get(); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 80 | while (LastCache->getNextStatCache()) |
| 81 | LastCache = LastCache->getNextStatCache(); |
| 82 | |
| 83 | LastCache->setNextStatCache(statCache); |
| 84 | } |
| 85 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 86 | void FileManager::removeStatCache(FileSystemStatCache *statCache) { |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 87 | if (!statCache) |
| 88 | return; |
| 89 | |
| 90 | if (StatCache.get() == statCache) { |
| 91 | // This is the first stat cache. |
| 92 | StatCache.reset(StatCache->takeNextStatCache()); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | // Find the stat cache in the list. |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 97 | FileSystemStatCache *PrevCache = StatCache.get(); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 98 | while (PrevCache && PrevCache->getNextStatCache() != statCache) |
| 99 | PrevCache = PrevCache->getNextStatCache(); |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 100 | |
| 101 | assert(PrevCache && "Stat cache not found for removal"); |
| 102 | PrevCache->setNextStatCache(statCache->getNextStatCache()); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Manuel Klimek | 98be860 | 2012-07-31 13:56:54 +0000 | [diff] [blame] | 105 | void FileManager::clearStatCaches() { |
| 106 | StatCache.reset(0); |
| 107 | } |
| 108 | |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 109 | /// \brief Retrieve the directory that the given file name resides in. |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 110 | /// Filename can point to either a real file or a virtual file. |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 111 | static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr, |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 112 | StringRef Filename, |
| 113 | bool CacheFailure) { |
Zhanyong Wan | 21af887 | 2011-02-11 21:25:35 +0000 | [diff] [blame] | 114 | if (Filename.empty()) |
| 115 | return NULL; |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 116 | |
Zhanyong Wan | 21af887 | 2011-02-11 21:25:35 +0000 | [diff] [blame] | 117 | if (llvm::sys::path::is_separator(Filename[Filename.size() - 1])) |
| 118 | return NULL; // If Filename is a directory. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 120 | StringRef DirName = llvm::sys::path::parent_path(Filename); |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 121 | // Use the current directory if file has no path component. |
Zhanyong Wan | 21af887 | 2011-02-11 21:25:35 +0000 | [diff] [blame] | 122 | if (DirName.empty()) |
| 123 | DirName = "."; |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 124 | |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 125 | return FileMgr.getDirectory(DirName, CacheFailure); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 128 | /// Add all ancestors of the given path (pointing to either a file or |
| 129 | /// a directory) as virtual directories. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 130 | void FileManager::addAncestorsAsVirtualDirs(StringRef Path) { |
| 131 | StringRef DirName = llvm::sys::path::parent_path(Path); |
Zhanyong Wan | 21af887 | 2011-02-11 21:25:35 +0000 | [diff] [blame] | 132 | if (DirName.empty()) |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 133 | return; |
| 134 | |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 135 | llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt = |
| 136 | SeenDirEntries.GetOrCreateValue(DirName); |
| 137 | |
| 138 | // When caching a virtual directory, we always cache its ancestors |
| 139 | // at the same time. Therefore, if DirName is already in the cache, |
| 140 | // we don't need to recurse as its ancestors must also already be in |
| 141 | // the cache. |
| 142 | if (NamedDirEnt.getValue()) |
| 143 | return; |
| 144 | |
| 145 | // Add the virtual directory to the cache. |
| 146 | DirectoryEntry *UDE = new DirectoryEntry; |
| 147 | UDE->Name = NamedDirEnt.getKeyData(); |
| 148 | NamedDirEnt.setValue(UDE); |
| 149 | VirtualDirectoryEntries.push_back(UDE); |
| 150 | |
| 151 | // Recursively add the other ancestors. |
| 152 | addAncestorsAsVirtualDirs(DirName); |
| 153 | } |
| 154 | |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 155 | const DirectoryEntry *FileManager::getDirectory(StringRef DirName, |
| 156 | bool CacheFailure) { |
NAKAMURA Takumi | 759a4b4 | 2012-06-16 06:04:10 +0000 | [diff] [blame] | 157 | // stat doesn't like trailing separators except for root directory. |
NAKAMURA Takumi | 678a3ea | 2011-11-17 06:16:05 +0000 | [diff] [blame] | 158 | // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'. |
| 159 | // (though it can strip '\\') |
NAKAMURA Takumi | 759a4b4 | 2012-06-16 06:04:10 +0000 | [diff] [blame] | 160 | if (DirName.size() > 1 && |
| 161 | DirName != llvm::sys::path::root_path(DirName) && |
| 162 | llvm::sys::path::is_separator(DirName.back())) |
NAKAMURA Takumi | 678a3ea | 2011-11-17 06:16:05 +0000 | [diff] [blame] | 163 | DirName = DirName.substr(0, DirName.size()-1); |
Rafael Espindola | 146d57f | 2013-07-29 15:47:24 +0000 | [diff] [blame] | 164 | #ifdef LLVM_ON_WIN32 |
| 165 | // Fixing a problem with "clang C:test.c" on Windows. |
| 166 | // Stat("C:") does not recognize "C:" as a valid directory |
| 167 | std::string DirNameStr; |
| 168 | if (DirName.size() > 1 && DirName.back() == ':' && |
| 169 | DirName.equals_lower(llvm::sys::path::root_name(DirName))) { |
| 170 | DirNameStr = DirName.str() + '.'; |
| 171 | DirName = DirNameStr; |
| 172 | } |
| 173 | #endif |
NAKAMURA Takumi | 678a3ea | 2011-11-17 06:16:05 +0000 | [diff] [blame] | 174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | ++NumDirLookups; |
| 176 | llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt = |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 177 | SeenDirEntries.GetOrCreateValue(DirName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 179 | // See if there was already an entry in the map. Note that the map |
| 180 | // contains both virtual and real directories. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | if (NamedDirEnt.getValue()) |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 182 | return NamedDirEnt.getValue() == NON_EXISTENT_DIR |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 183 | ? 0 : NamedDirEnt.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 185 | ++NumDirCacheMisses; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 187 | // By default, initialize it to invalid. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 188 | NamedDirEnt.setValue(NON_EXISTENT_DIR); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | // Get the null-terminated directory name as stored as the key of the |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 191 | // SeenDirEntries map. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | const char *InterndDirName = NamedDirEnt.getKeyData(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | // Check to see if the directory exists. |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 195 | FileData Data; |
| 196 | if (getStatValue(InterndDirName, Data, false, 0 /*directory lookup*/)) { |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 197 | // There's no real directory at the given path. |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 198 | if (!CacheFailure) |
| 199 | SeenDirEntries.erase(DirName); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | return 0; |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 201 | } |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 202 | |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 203 | // It exists. See if we have already opened a directory with the |
| 204 | // same inode (this occurs on Unix-like systems when one dir is |
| 205 | // symlinked to another, for example) or the same path (on |
| 206 | // Windows). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 207 | DirectoryEntry &UDE = UniqueRealDirs[Data.UniqueID]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | NamedDirEnt.setValue(&UDE); |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 210 | if (!UDE.getName()) { |
| 211 | // We don't have this directory yet, add it. We use the string |
| 212 | // key from the SeenDirEntries map as the string. |
| 213 | UDE.Name = InterndDirName; |
| 214 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | return &UDE; |
| 217 | } |
| 218 | |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 219 | const FileEntry *FileManager::getFile(StringRef Filename, bool openFile, |
| 220 | bool CacheFailure) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 221 | ++NumFileLookups; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 223 | // See if there is already an entry in the map. |
| 224 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 225 | SeenFileEntries.GetOrCreateValue(Filename); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | |
| 227 | // See if there is already an entry in the map. |
| 228 | if (NamedFileEnt.getValue()) |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 229 | return NamedFileEnt.getValue() == NON_EXISTENT_FILE |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 230 | ? 0 : NamedFileEnt.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 232 | ++NumFileCacheMisses; |
| 233 | |
| 234 | // By default, initialize it to invalid. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 235 | NamedFileEnt.setValue(NON_EXISTENT_FILE); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 237 | // Get the null-terminated file name as stored as the key of the |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 238 | // SeenFileEntries map. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 239 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 241 | // Look up the directory for the file. When looking up something like |
| 242 | // sys/foo.h we'll discover all of the search directories that have a 'sys' |
| 243 | // subdirectory. This will let us avoid having to waste time on known-to-fail |
| 244 | // searches when we go to find sys/bar.h, because all the search directories |
| 245 | // without a 'sys' subdir will get a cached failure result. |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 246 | const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename, |
| 247 | CacheFailure); |
| 248 | if (DirInfo == 0) { // Directory doesn't exist, file can't exist. |
| 249 | if (!CacheFailure) |
| 250 | SeenFileEntries.erase(Filename); |
| 251 | |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 252 | return 0; |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 255 | // FIXME: Use the directory info to prune this, before doing the stat syscall. |
| 256 | // FIXME: This will reduce the # syscalls. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 258 | // Nope, there isn't. Check to see if the file exists. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 259 | vfs::File *F = 0; |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 260 | FileData Data; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 261 | if (getStatValue(InterndFileName, Data, true, openFile ? &F : 0)) { |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 262 | // There's no real file at the given path. |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 263 | if (!CacheFailure) |
| 264 | SeenFileEntries.erase(Filename); |
| 265 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | return 0; |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 267 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 269 | assert((openFile || !F) && "undesired open file"); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | bca6d12 | 2007-12-18 22:29:39 +0000 | [diff] [blame] | 271 | // It exists. See if we have already opened a file with the same inode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 272 | // This occurs when one dir is symlinked to another, for example. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 273 | FileEntry &UFE = UniqueRealFiles[Data.UniqueID]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | NamedFileEnt.setValue(&UFE); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 276 | if (UFE.isValid()) { // Already have an entry with this inode, return it. |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 277 | // If the stat process opened the file, close it to avoid a FD leak. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 278 | if (F) |
| 279 | delete F; |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 280 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | return &UFE; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 282 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 283 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 284 | // Otherwise, we don't have this file yet, add it. |
| 285 | UFE.Name = Data.Name; |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 286 | UFE.Size = Data.Size; |
| 287 | UFE.ModTime = Data.ModTime; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | UFE.Dir = DirInfo; |
| 289 | UFE.UID = NextFileUID++; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 290 | UFE.UniqueID = Data.UniqueID; |
| 291 | UFE.IsNamedPipe = Data.IsNamedPipe; |
| 292 | UFE.InPCH = Data.InPCH; |
| 293 | UFE.File.reset(F); |
| 294 | UFE.IsValid = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | return &UFE; |
| 296 | } |
| 297 | |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 298 | const FileEntry * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 299 | FileManager::getVirtualFile(StringRef Filename, off_t Size, |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 300 | time_t ModificationTime) { |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 301 | ++NumFileLookups; |
| 302 | |
| 303 | // See if there is already an entry in the map. |
| 304 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 305 | SeenFileEntries.GetOrCreateValue(Filename); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 306 | |
| 307 | // See if there is already an entry in the map. |
Axel Naumann | 0433116 | 2011-01-27 10:55:51 +0000 | [diff] [blame] | 308 | if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE) |
| 309 | return NamedFileEnt.getValue(); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 310 | |
| 311 | ++NumFileCacheMisses; |
| 312 | |
| 313 | // By default, initialize it to invalid. |
| 314 | NamedFileEnt.setValue(NON_EXISTENT_FILE); |
| 315 | |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 316 | addAncestorsAsVirtualDirs(Filename); |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 317 | FileEntry *UFE = 0; |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 318 | |
| 319 | // Now that all ancestors of Filename are in the cache, the |
| 320 | // following call is guaranteed to find the DirectoryEntry from the |
| 321 | // cache. |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 322 | const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename, |
| 323 | /*CacheFailure=*/true); |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 324 | assert(DirInfo && |
| 325 | "The directory of a virtual file should already be in the cache."); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 326 | |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 327 | // Check to see if the file exists. If so, drop the virtual file |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 328 | FileData Data; |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 329 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 330 | if (getStatValue(InterndFileName, Data, true, 0) == 0) { |
| 331 | Data.Size = Size; |
| 332 | Data.ModTime = ModificationTime; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 333 | UFE = &UniqueRealFiles[Data.UniqueID]; |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 334 | |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 335 | NamedFileEnt.setValue(UFE); |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 336 | |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 337 | // If we had already opened this file, close it now so we don't |
| 338 | // leak the descriptor. We're not going to use the file |
| 339 | // descriptor anyway, since this is a virtual file. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 340 | if (UFE->File) |
| 341 | UFE->closeFile(); |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 342 | |
| 343 | // If we already have an entry with this inode, return it. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 344 | if (UFE->isValid()) |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 345 | return UFE; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 346 | |
| 347 | UFE->UniqueID = Data.UniqueID; |
| 348 | UFE->IsNamedPipe = Data.IsNamedPipe; |
| 349 | UFE->InPCH = Data.InPCH; |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | if (!UFE) { |
| 353 | UFE = new FileEntry(); |
| 354 | VirtualFileEntries.push_back(UFE); |
| 355 | NamedFileEnt.setValue(UFE); |
| 356 | } |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 357 | |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 358 | UFE->Name = InterndFileName; |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 359 | UFE->Size = Size; |
| 360 | UFE->ModTime = ModificationTime; |
| 361 | UFE->Dir = DirInfo; |
| 362 | UFE->UID = NextFileUID++; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 363 | UFE->File.reset(); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 364 | return UFE; |
| 365 | } |
| 366 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 367 | void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const { |
| 368 | StringRef pathRef(path.data(), path.size()); |
Anders Carlsson | af036a6 | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 369 | |
Anders Carlsson | 2e2468e | 2011-03-14 01:13:54 +0000 | [diff] [blame] | 370 | if (FileSystemOpts.WorkingDir.empty() |
| 371 | || llvm::sys::path::is_absolute(pathRef)) |
Michael J. Spencer | 256053b | 2010-12-17 21:22:22 +0000 | [diff] [blame] | 372 | return; |
| 373 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 374 | SmallString<128> NewPath(FileSystemOpts.WorkingDir); |
Anders Carlsson | af036a6 | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 375 | llvm::sys::path::append(NewPath, pathRef); |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 376 | path = NewPath; |
| 377 | } |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 379 | llvm::MemoryBuffer *FileManager:: |
Argyrios Kyrtzidis | ff39896 | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 380 | getBufferForFile(const FileEntry *Entry, std::string *ErrorStr, |
| 381 | bool isVolatile) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 382 | std::unique_ptr<llvm::MemoryBuffer> Result; |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 383 | llvm::error_code ec; |
Argyrios Kyrtzidis | a8d530e | 2011-03-15 00:47:44 +0000 | [diff] [blame] | 384 | |
Argyrios Kyrtzidis | ff39896 | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 385 | uint64_t FileSize = Entry->getSize(); |
| 386 | // If there's a high enough chance that the file have changed since we |
| 387 | // got its size, force a stat before opening it. |
| 388 | if (isVolatile) |
| 389 | FileSize = -1; |
| 390 | |
Argyrios Kyrtzidis | a8d530e | 2011-03-15 00:47:44 +0000 | [diff] [blame] | 391 | const char *Filename = Entry->getName(); |
| 392 | // If the file is already open, use the open file descriptor. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 393 | if (Entry->File) { |
| 394 | ec = Entry->File->getBuffer(Filename, Result, FileSize); |
Argyrios Kyrtzidis | a8d530e | 2011-03-15 00:47:44 +0000 | [diff] [blame] | 395 | if (ErrorStr) |
| 396 | *ErrorStr = ec.message(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 397 | Entry->closeFile(); |
| 398 | return Result.release(); |
Argyrios Kyrtzidis | a8d530e | 2011-03-15 00:47:44 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // Otherwise, open the file. |
| 402 | |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 403 | if (FileSystemOpts.WorkingDir.empty()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 404 | ec = FS->getBufferForFile(Filename, Result, FileSize); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 405 | if (ec && ErrorStr) |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 406 | *ErrorStr = ec.message(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 407 | return Result.release(); |
Chris Lattner | 5cc1c73 | 2010-11-23 22:32:37 +0000 | [diff] [blame] | 408 | } |
Anders Carlsson | af036a6 | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 409 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 410 | SmallString<128> FilePath(Entry->getName()); |
Anders Carlsson | 03fd362 | 2011-03-07 01:28:33 +0000 | [diff] [blame] | 411 | FixupRelativePath(FilePath); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 412 | ec = FS->getBufferForFile(FilePath.str(), Result, FileSize); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 413 | if (ec && ErrorStr) |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 414 | *ErrorStr = ec.message(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 415 | return Result.release(); |
Chris Lattner | 75dfb65 | 2010-11-23 09:19:42 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | llvm::MemoryBuffer *FileManager:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 419 | getBufferForFile(StringRef Filename, std::string *ErrorStr) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 420 | std::unique_ptr<llvm::MemoryBuffer> Result; |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 421 | llvm::error_code ec; |
| 422 | if (FileSystemOpts.WorkingDir.empty()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 423 | ec = FS->getBufferForFile(Filename, Result); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 424 | if (ec && ErrorStr) |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 425 | *ErrorStr = ec.message(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 426 | return Result.release(); |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 429 | SmallString<128> FilePath(Filename); |
Anders Carlsson | 03fd362 | 2011-03-07 01:28:33 +0000 | [diff] [blame] | 430 | FixupRelativePath(FilePath); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 431 | ec = FS->getBufferForFile(FilePath.c_str(), Result); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 432 | if (ec && ErrorStr) |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 433 | *ErrorStr = ec.message(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 434 | return Result.release(); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 437 | /// getStatValue - Get the 'stat' information for the specified path, |
| 438 | /// using the cache to accelerate it if possible. This returns true |
| 439 | /// if the path points to a virtual file or does not exist, or returns |
| 440 | /// false if it's an existent real file. If FileDescriptor is NULL, |
| 441 | /// do directory look-up instead of file look-up. |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 442 | bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 443 | vfs::File **F) { |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 444 | // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be |
| 445 | // absolute! |
Chris Lattner | 11aa4b0 | 2010-11-23 19:56:39 +0000 | [diff] [blame] | 446 | if (FileSystemOpts.WorkingDir.empty()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 447 | return FileSystemStatCache::get(Path, Data, isFile, F,StatCache.get(), *FS); |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 448 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 449 | SmallString<128> FilePath(Path); |
Anders Carlsson | 03fd362 | 2011-03-07 01:28:33 +0000 | [diff] [blame] | 450 | FixupRelativePath(FilePath); |
Chris Lattner | 11aa4b0 | 2010-11-23 19:56:39 +0000 | [diff] [blame] | 451 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 452 | return FileSystemStatCache::get(FilePath.c_str(), Data, isFile, F, |
| 453 | StatCache.get(), *FS); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Rafael Espindola | aefb1d3 | 2013-07-29 18:22:23 +0000 | [diff] [blame] | 456 | bool FileManager::getNoncachedStatValue(StringRef Path, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 457 | vfs::Status &Result) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 458 | SmallString<128> FilePath(Path); |
Anders Carlsson | 7dbafb3 | 2011-03-18 19:23:19 +0000 | [diff] [blame] | 459 | FixupRelativePath(FilePath); |
| 460 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 461 | llvm::ErrorOr<vfs::Status> S = FS->status(FilePath.c_str()); |
| 462 | if (!S) |
| 463 | return true; |
| 464 | Result = *S; |
| 465 | return false; |
Anders Carlsson | 7dbafb3 | 2011-03-18 19:23:19 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Axel Naumann | 5ba0559 | 2012-07-10 16:50:27 +0000 | [diff] [blame] | 468 | void FileManager::invalidateCache(const FileEntry *Entry) { |
| 469 | assert(Entry && "Cannot invalidate a NULL FileEntry"); |
Axel Naumann | 3ce42c3 | 2012-06-27 09:17:42 +0000 | [diff] [blame] | 470 | |
| 471 | SeenFileEntries.erase(Entry->getName()); |
Axel Naumann | 5ba0559 | 2012-07-10 16:50:27 +0000 | [diff] [blame] | 472 | |
| 473 | // FileEntry invalidation should not block future optimizations in the file |
| 474 | // caches. Possible alternatives are cache truncation (invalidate last N) or |
| 475 | // invalidation of the whole cache. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 476 | UniqueRealFiles.erase(Entry->getUniqueID()); |
Axel Naumann | 3ce42c3 | 2012-06-27 09:17:42 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 480 | void FileManager::GetUniqueIDMapping( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 481 | SmallVectorImpl<const FileEntry *> &UIDToFiles) const { |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 482 | UIDToFiles.clear(); |
| 483 | UIDToFiles.resize(NextFileUID); |
| 484 | |
| 485 | // Map file entries |
| 486 | for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 487 | FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end(); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 488 | FE != FEEnd; ++FE) |
| 489 | if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE) |
| 490 | UIDToFiles[FE->getValue()->getUID()] = FE->getValue(); |
| 491 | |
| 492 | // Map virtual file entries |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 493 | for (SmallVectorImpl<FileEntry *>::const_iterator |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 494 | VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end(); |
| 495 | VFE != VFEEnd; ++VFE) |
| 496 | if (*VFE && *VFE != NON_EXISTENT_FILE) |
| 497 | UIDToFiles[(*VFE)->getUID()] = *VFE; |
| 498 | } |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 499 | |
Argyrios Kyrtzidis | d54dff0 | 2012-05-03 21:50:39 +0000 | [diff] [blame] | 500 | void FileManager::modifyFileEntry(FileEntry *File, |
| 501 | off_t Size, time_t ModificationTime) { |
| 502 | File->Size = Size; |
| 503 | File->ModTime = ModificationTime; |
| 504 | } |
| 505 | |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 506 | StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) { |
| 507 | // FIXME: use llvm::sys::fs::canonical() when it gets implemented |
| 508 | #ifdef LLVM_ON_UNIX |
| 509 | llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known |
| 510 | = CanonicalDirNames.find(Dir); |
| 511 | if (Known != CanonicalDirNames.end()) |
| 512 | return Known->second; |
| 513 | |
| 514 | StringRef CanonicalName(Dir->getName()); |
| 515 | char CanonicalNameBuf[PATH_MAX]; |
| 516 | if (realpath(Dir->getName(), CanonicalNameBuf)) { |
| 517 | unsigned Len = strlen(CanonicalNameBuf); |
| 518 | char *Mem = static_cast<char *>(CanonicalNameStorage.Allocate(Len, 1)); |
| 519 | memcpy(Mem, CanonicalNameBuf, Len); |
| 520 | CanonicalName = StringRef(Mem, Len); |
| 521 | } |
| 522 | |
| 523 | CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName)); |
| 524 | return CanonicalName; |
| 525 | #else |
| 526 | return StringRef(Dir->getName()); |
| 527 | #endif |
| 528 | } |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 529 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 530 | void FileManager::PrintStats() const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 531 | llvm::errs() << "\n*** File Manager Stats:\n"; |
Zhanyong Wan | 9b555ea | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 532 | llvm::errs() << UniqueRealFiles.size() << " real files found, " |
| 533 | << UniqueRealDirs.size() << " real dirs found.\n"; |
| 534 | llvm::errs() << VirtualFileEntries.size() << " virtual files found, " |
| 535 | << VirtualDirectoryEntries.size() << " virtual dirs found.\n"; |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 536 | llvm::errs() << NumDirLookups << " dir lookups, " |
| 537 | << NumDirCacheMisses << " dir cache misses.\n"; |
| 538 | llvm::errs() << NumFileLookups << " file lookups, " |
| 539 | << NumFileCacheMisses << " file cache misses.\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 541 | //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 542 | } |