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