Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 1 | //===--- FileManager.cpp - File System Probing and Caching ----------------===// |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +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 | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileSystemStatCache.h" |
Chris Lattner | 2f4a89a | 2006-10-30 03:55:17 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "llvm/Config/llvm-config.h" |
Michael J. Spencer | 740857f | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 26db648 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 28 | #include <map> |
| 29 | #include <set> |
| 30 | #include <string> |
Rafael Espindola | 8a8e554 | 2014-06-12 17:19:42 +0000 | [diff] [blame] | 31 | #include <system_error> |
Chris Lattner | 278038b | 2010-11-23 21:53:15 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | |
Ted Kremenek | 8d71e25 | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 35 | /// NON_EXISTENT_DIR - A special value distinct from null that is used to |
Chris Lattner | af65375 | 2006-10-30 03:06:54 +0000 | [diff] [blame] | 36 | /// represent a dir name that doesn't exist on the disk. |
Ted Kremenek | 8d71e25 | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 37 | #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1) |
Chris Lattner | af65375 | 2006-10-30 03:06:54 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 9624b69 | 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 | 5c04bd8 | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 43 | //===----------------------------------------------------------------------===// |
| 44 | // Common logic. |
| 45 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d87eef8 | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 46 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 47 | FileManager::FileManager(const FileSystemOptions &FSO, |
| 48 | IntrusiveRefCntPtr<vfs::FileSystem> FS) |
| 49 | : FS(FS), FileSystemOpts(FSO), |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 50 | SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) { |
Ted Kremenek | d87eef8 | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 51 | NumDirLookups = NumFileLookups = 0; |
| 52 | NumDirCacheMisses = NumFileCacheMisses = 0; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [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 | d87eef8 | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | FileManager::~FileManager() { |
Chris Lattner | 966b25b | 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 | e1dd3e2 | 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 | d87eef8 | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 65 | } |
| 66 | |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 67 | void FileManager::addStatCache(std::unique_ptr<FileSystemStatCache> statCache, |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 68 | bool AtBeginning) { |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 69 | assert(statCache && "No stat cache provided?"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 70 | if (AtBeginning || !StatCache.get()) { |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 71 | statCache->setNextStatCache(std::move(StatCache)); |
| 72 | StatCache = std::move(statCache); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 76 | FileSystemStatCache *LastCache = StatCache.get(); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 77 | while (LastCache->getNextStatCache()) |
| 78 | LastCache = LastCache->getNextStatCache(); |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 79 | |
| 80 | LastCache->setNextStatCache(std::move(statCache)); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 83 | void FileManager::removeStatCache(FileSystemStatCache *statCache) { |
Douglas Gregor | d2eb58a | 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. |
David Blaikie | dd0e1e8 | 2014-08-10 16:57:11 +0000 | [diff] [blame] | 89 | StatCache = StatCache->takeNextStatCache(); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
| 93 | // Find the stat cache in the list. |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 94 | FileSystemStatCache *PrevCache = StatCache.get(); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 95 | while (PrevCache && PrevCache->getNextStatCache() != statCache) |
| 96 | PrevCache = PrevCache->getNextStatCache(); |
Chris Lattner | 9624b69 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 97 | |
| 98 | assert(PrevCache && "Stat cache not found for removal"); |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 99 | PrevCache->setNextStatCache(statCache->takeNextStatCache()); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Manuel Klimek | 3aad855 | 2012-07-31 13:56:54 +0000 | [diff] [blame] | 102 | void FileManager::clearStatCaches() { |
David Blaikie | 3875a82 | 2014-07-19 01:06:45 +0000 | [diff] [blame] | 103 | StatCache.reset(); |
Manuel Klimek | 3aad855 | 2012-07-31 13:56:54 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 106 | /// \brief Retrieve the directory that the given file name resides in. |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 107 | /// Filename can point to either a real file or a virtual file. |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 108 | static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr, |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 109 | StringRef Filename, |
| 110 | bool CacheFailure) { |
Zhanyong Wan | f3c0ff7 | 2011-02-11 21:25:35 +0000 | [diff] [blame] | 111 | if (Filename.empty()) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 112 | return nullptr; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 113 | |
Zhanyong Wan | f3c0ff7 | 2011-02-11 21:25:35 +0000 | [diff] [blame] | 114 | if (llvm::sys::path::is_separator(Filename[Filename.size() - 1])) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 115 | return nullptr; // If Filename is a directory. |
Benjamin Kramer | 3cf715d | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 117 | StringRef DirName = llvm::sys::path::parent_path(Filename); |
Chris Lattner | 0c0e804 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 118 | // Use the current directory if file has no path component. |
Zhanyong Wan | f3c0ff7 | 2011-02-11 21:25:35 +0000 | [diff] [blame] | 119 | if (DirName.empty()) |
| 120 | DirName = "."; |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 121 | |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 122 | return FileMgr.getDirectory(DirName, CacheFailure); |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Zhanyong Wan | e1dd3e2 | 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 | 0e62c1c | 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 | f3c0ff7 | 2011-02-11 21:25:35 +0000 | [diff] [blame] | 129 | if (DirName.empty()) |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 130 | return; |
| 131 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 132 | llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt = |
| 133 | SeenDirEntries.GetOrCreateValue(DirName); |
| 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. |
| 139 | if (NamedDirEnt.getValue()) |
| 140 | return; |
| 141 | |
| 142 | // Add the virtual directory to the cache. |
| 143 | DirectoryEntry *UDE = new DirectoryEntry; |
| 144 | UDE->Name = NamedDirEnt.getKeyData(); |
| 145 | NamedDirEnt.setValue(UDE); |
| 146 | VirtualDirectoryEntries.push_back(UDE); |
| 147 | |
| 148 | // Recursively add the other ancestors. |
| 149 | addAncestorsAsVirtualDirs(DirName); |
| 150 | } |
| 151 | |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 152 | const DirectoryEntry *FileManager::getDirectory(StringRef DirName, |
| 153 | bool CacheFailure) { |
NAKAMURA Takumi | 8bd8ee7 | 2012-06-16 06:04:10 +0000 | [diff] [blame] | 154 | // stat doesn't like trailing separators except for root directory. |
NAKAMURA Takumi | 32f1acf | 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 | 8bd8ee7 | 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 | 32f1acf | 2011-11-17 06:16:05 +0000 | [diff] [blame] | 160 | DirName = DirName.substr(0, DirName.size()-1); |
Rafael Espindola | ee30546 | 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 | 32f1acf | 2011-11-17 06:16:05 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 172 | ++NumDirLookups; |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 173 | llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt = |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 174 | SeenDirEntries.GetOrCreateValue(DirName); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Zhanyong Wan | e1dd3e2 | 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. |
Chris Lattner | 34d1f5a | 2007-02-08 19:08:49 +0000 | [diff] [blame] | 178 | if (NamedDirEnt.getValue()) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 179 | return NamedDirEnt.getValue() == NON_EXISTENT_DIR ? nullptr |
| 180 | : NamedDirEnt.getValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 182 | ++NumDirCacheMisses; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Chris Lattner | af65375 | 2006-10-30 03:06:54 +0000 | [diff] [blame] | 184 | // By default, initialize it to invalid. |
Ted Kremenek | 8d71e25 | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 185 | NamedDirEnt.setValue(NON_EXISTENT_DIR); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 43fd42e | 2006-10-30 03:40:58 +0000 | [diff] [blame] | 187 | // Get the null-terminated directory name as stored as the key of the |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 188 | // SeenDirEntries map. |
Chris Lattner | 34d1f5a | 2007-02-08 19:08:49 +0000 | [diff] [blame] | 189 | const char *InterndDirName = NamedDirEnt.getKeyData(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Chris Lattner | af65375 | 2006-10-30 03:06:54 +0000 | [diff] [blame] | 191 | // Check to see if the directory exists. |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 192 | FileData Data; |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 193 | if (getStatValue(InterndDirName, Data, false, nullptr /*directory lookup*/)) { |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 194 | // There's no real directory at the given path. |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 195 | if (!CacheFailure) |
| 196 | SeenDirEntries.erase(DirName); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 197 | return nullptr; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 198 | } |
Ted Kremenek | d87eef8 | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 199 | |
Zhanyong Wan | e1dd3e2 | 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). |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 204 | DirectoryEntry &UDE = UniqueRealDirs[Data.UniqueID]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 34d1f5a | 2007-02-08 19:08:49 +0000 | [diff] [blame] | 206 | NamedDirEnt.setValue(&UDE); |
Zhanyong Wan | e1dd3e2 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 34d1f5a | 2007-02-08 19:08:49 +0000 | [diff] [blame] | 213 | return &UDE; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 216 | const FileEntry *FileManager::getFile(StringRef Filename, bool openFile, |
| 217 | bool CacheFailure) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 218 | ++NumFileLookups; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 220 | // See if there is already an entry in the map. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 221 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 222 | SeenFileEntries.GetOrCreateValue(Filename); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 223 | |
Chris Lattner | 2f4a89a | 2006-10-30 03:55:17 +0000 | [diff] [blame] | 224 | // See if there is already an entry in the map. |
Chris Lattner | 34d1f5a | 2007-02-08 19:08:49 +0000 | [diff] [blame] | 225 | if (NamedFileEnt.getValue()) |
Ted Kremenek | 8d71e25 | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 226 | return NamedFileEnt.getValue() == NON_EXISTENT_FILE |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 227 | ? nullptr : NamedFileEnt.getValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 229 | ++NumFileCacheMisses; |
| 230 | |
Chris Lattner | 2f4a89a | 2006-10-30 03:55:17 +0000 | [diff] [blame] | 231 | // By default, initialize it to invalid. |
Ted Kremenek | 8d71e25 | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 232 | NamedFileEnt.setValue(NON_EXISTENT_FILE); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 2f4a89a | 2006-10-30 03:55:17 +0000 | [diff] [blame] | 234 | // Get the null-terminated file name as stored as the key of the |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 235 | // SeenFileEntries map. |
Chris Lattner | 34d1f5a | 2007-02-08 19:08:49 +0000 | [diff] [blame] | 236 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 966b25b | 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 | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 243 | const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename, |
| 244 | CacheFailure); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 245 | if (DirInfo == nullptr) { // Directory doesn't exist, file can't exist. |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 246 | if (!CacheFailure) |
| 247 | SeenFileEntries.erase(Filename); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 248 | |
| 249 | return nullptr; |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 255 | // Nope, there isn't. Check to see if the file exists. |
David Blaikie | 326ffb3 | 2014-07-08 15:46:02 +0000 | [diff] [blame] | 256 | std::unique_ptr<vfs::File> F; |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 257 | FileData Data; |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 258 | if (getStatValue(InterndFileName, Data, true, openFile ? &F : nullptr)) { |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 259 | // There's no real file at the given path. |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 260 | if (!CacheFailure) |
| 261 | SeenFileEntries.erase(Filename); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 262 | |
| 263 | return nullptr; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 264 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
Patrik Hagglund | ab01d4b | 2014-02-21 07:23:53 +0000 | [diff] [blame] | 266 | assert((openFile || !F) && "undesired open file"); |
Argyrios Kyrtzidis | d6278e3 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 267 | |
Ted Kremenek | f4c38c9 | 2007-12-18 22:29:39 +0000 | [diff] [blame] | 268 | // It exists. See if we have already opened a file with the same inode. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 269 | // This occurs when one dir is symlinked to another, for example. |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 270 | FileEntry &UFE = UniqueRealFiles[Data.UniqueID]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 34d1f5a | 2007-02-08 19:08:49 +0000 | [diff] [blame] | 272 | NamedFileEnt.setValue(&UFE); |
Ben Langmuir | ab86fbe | 2014-09-08 16:15:54 +0000 | [diff] [blame] | 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 = SeenFileEntries.GetOrCreateValue(Data.Name); |
| 278 | if (!NamedFileEnt.getValue()) |
| 279 | NamedFileEnt.setValue(&UFE); |
| 280 | else |
| 281 | assert(NamedFileEnt.getValue() == &UFE && |
| 282 | "filename from getStatValue() refers to wrong file"); |
| 283 | InterndFileName = NamedFileEnt.getKeyData(); |
| 284 | } |
| 285 | |
Ben Langmuir | c8a7146 | 2014-02-27 17:23:33 +0000 | [diff] [blame] | 286 | if (UFE.isValid()) { // Already have an entry with this inode, return it. |
Ben Langmuir | 5de00f3 | 2014-05-23 18:15:47 +0000 | [diff] [blame] | 287 | |
| 288 | // FIXME: this hack ensures that if we look up a file by a virtual path in |
| 289 | // the VFS that the getDir() will have the virtual path, even if we found |
| 290 | // the file by a 'real' path first. This is required in order to find a |
| 291 | // module's structure when its headers/module map are mapped in the VFS. |
| 292 | // We should remove this as soon as we can properly support a file having |
| 293 | // multiple names. |
| 294 | if (DirInfo != UFE.Dir && Data.IsVFSMapped) |
| 295 | UFE.Dir = DirInfo; |
| 296 | |
Manuel Klimek | c0ff990 | 2014-08-13 12:34:41 +0000 | [diff] [blame] | 297 | // Always update the name to use the last name by which a file was accessed. |
| 298 | // FIXME: Neither this nor always using the first name is correct; we want |
| 299 | // to switch towards a design where we return a FileName object that |
| 300 | // encapsulates both the name by which the file was accessed and the |
| 301 | // corresponding FileEntry. |
Ben Langmuir | ab86fbe | 2014-09-08 16:15:54 +0000 | [diff] [blame] | 302 | UFE.Name = InterndFileName; |
Manuel Klimek | c0ff990 | 2014-08-13 12:34:41 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 34d1f5a | 2007-02-08 19:08:49 +0000 | [diff] [blame] | 304 | return &UFE; |
Chris Lattner | dd27843 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 305 | } |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 306 | |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 307 | // Otherwise, we don't have this file yet, add it. |
Ben Langmuir | ab86fbe | 2014-09-08 16:15:54 +0000 | [diff] [blame] | 308 | UFE.Name = InterndFileName; |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 309 | UFE.Size = Data.Size; |
| 310 | UFE.ModTime = Data.ModTime; |
Chris Lattner | 2f4a89a | 2006-10-30 03:55:17 +0000 | [diff] [blame] | 311 | UFE.Dir = DirInfo; |
| 312 | UFE.UID = NextFileUID++; |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 313 | UFE.UniqueID = Data.UniqueID; |
| 314 | UFE.IsNamedPipe = Data.IsNamedPipe; |
| 315 | UFE.InPCH = Data.InPCH; |
David Blaikie | 326ffb3 | 2014-07-08 15:46:02 +0000 | [diff] [blame] | 316 | UFE.File = std::move(F); |
Ben Langmuir | c8a7146 | 2014-02-27 17:23:33 +0000 | [diff] [blame] | 317 | UFE.IsValid = true; |
Chris Lattner | 34d1f5a | 2007-02-08 19:08:49 +0000 | [diff] [blame] | 318 | return &UFE; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 321 | const FileEntry * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 322 | FileManager::getVirtualFile(StringRef Filename, off_t Size, |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 323 | time_t ModificationTime) { |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 324 | ++NumFileLookups; |
| 325 | |
| 326 | // See if there is already an entry in the map. |
| 327 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 328 | SeenFileEntries.GetOrCreateValue(Filename); |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 329 | |
| 330 | // See if there is already an entry in the map. |
Axel Naumann | 63fbaed | 2011-01-27 10:55:51 +0000 | [diff] [blame] | 331 | if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE) |
| 332 | return NamedFileEnt.getValue(); |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 333 | |
| 334 | ++NumFileCacheMisses; |
| 335 | |
| 336 | // By default, initialize it to invalid. |
| 337 | NamedFileEnt.setValue(NON_EXISTENT_FILE); |
| 338 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 339 | addAncestorsAsVirtualDirs(Filename); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 340 | FileEntry *UFE = nullptr; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 341 | |
| 342 | // Now that all ancestors of Filename are in the cache, the |
| 343 | // following call is guaranteed to find the DirectoryEntry from the |
| 344 | // cache. |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 345 | const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename, |
| 346 | /*CacheFailure=*/true); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 347 | assert(DirInfo && |
| 348 | "The directory of a virtual file should already be in the cache."); |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 349 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 350 | // Check to see if the file exists. If so, drop the virtual file |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 351 | FileData Data; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 352 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 353 | if (getStatValue(InterndFileName, Data, true, nullptr) == 0) { |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 354 | Data.Size = Size; |
| 355 | Data.ModTime = ModificationTime; |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 356 | UFE = &UniqueRealFiles[Data.UniqueID]; |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 357 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 358 | NamedFileEnt.setValue(UFE); |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 359 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 360 | // If we had already opened this file, close it now so we don't |
| 361 | // leak the descriptor. We're not going to use the file |
| 362 | // descriptor anyway, since this is a virtual file. |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 363 | if (UFE->File) |
| 364 | UFE->closeFile(); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 365 | |
| 366 | // If we already have an entry with this inode, return it. |
Ben Langmuir | c8a7146 | 2014-02-27 17:23:33 +0000 | [diff] [blame] | 367 | if (UFE->isValid()) |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 368 | return UFE; |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 369 | |
| 370 | UFE->UniqueID = Data.UniqueID; |
| 371 | UFE->IsNamedPipe = Data.IsNamedPipe; |
| 372 | UFE->InPCH = Data.InPCH; |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | if (!UFE) { |
| 376 | UFE = new FileEntry(); |
| 377 | VirtualFileEntries.push_back(UFE); |
| 378 | NamedFileEnt.setValue(UFE); |
| 379 | } |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 9624b69 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 381 | UFE->Name = InterndFileName; |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 382 | UFE->Size = Size; |
| 383 | UFE->ModTime = ModificationTime; |
| 384 | UFE->Dir = DirInfo; |
| 385 | UFE->UID = NextFileUID++; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 386 | UFE->File.reset(); |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 387 | return UFE; |
| 388 | } |
| 389 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 390 | void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const { |
| 391 | StringRef pathRef(path.data(), path.size()); |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 392 | |
Anders Carlsson | 9ba8fb1 | 2011-03-14 01:13:54 +0000 | [diff] [blame] | 393 | if (FileSystemOpts.WorkingDir.empty() |
| 394 | || llvm::sys::path::is_absolute(pathRef)) |
Michael J. Spencer | f28df4c | 2010-12-17 21:22:22 +0000 | [diff] [blame] | 395 | return; |
| 396 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 397 | SmallString<128> NewPath(FileSystemOpts.WorkingDir); |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 398 | llvm::sys::path::append(NewPath, pathRef); |
Chris Lattner | 6e64099 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 399 | path = NewPath; |
| 400 | } |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 401 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame^] | 402 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
| 403 | FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile, |
| 404 | bool ShouldCloseOpenFile) { |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 405 | uint64_t FileSize = Entry->getSize(); |
| 406 | // If there's a high enough chance that the file have changed since we |
| 407 | // got its size, force a stat before opening it. |
| 408 | if (isVolatile) |
| 409 | FileSize = -1; |
| 410 | |
Argyrios Kyrtzidis | 669b0b1 | 2011-03-15 00:47:44 +0000 | [diff] [blame] | 411 | const char *Filename = Entry->getName(); |
| 412 | // If the file is already open, use the open file descriptor. |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 413 | if (Entry->File) { |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame^] | 414 | auto Result = |
| 415 | Entry->File->getBuffer(Filename, FileSize, |
| 416 | /*RequiresNullTerminator=*/true, isVolatile); |
Ben Langmuir | 9801b25 | 2014-06-20 00:24:56 +0000 | [diff] [blame] | 417 | // FIXME: we need a set of APIs that can make guarantees about whether a |
| 418 | // FileEntry is open or not. |
| 419 | if (ShouldCloseOpenFile) |
| 420 | Entry->closeFile(); |
Rafael Espindola | 6406f7b | 2014-08-26 19:54:40 +0000 | [diff] [blame] | 421 | return Result; |
Argyrios Kyrtzidis | 669b0b1 | 2011-03-15 00:47:44 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // Otherwise, open the file. |
| 425 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame^] | 426 | if (FileSystemOpts.WorkingDir.empty()) |
| 427 | return FS->getBufferForFile(Filename, FileSize, |
| 428 | /*RequiresNullTerminator=*/true, isVolatile); |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 429 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 430 | SmallString<128> FilePath(Entry->getName()); |
Anders Carlsson | 878b3e2 | 2011-03-07 01:28:33 +0000 | [diff] [blame] | 431 | FixupRelativePath(FilePath); |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame^] | 432 | return FS->getBufferForFile(FilePath.str(), FileSize, |
| 433 | /*RequiresNullTerminator=*/true, isVolatile); |
Chris Lattner | 26b5c19 | 2010-11-23 09:19:42 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame^] | 436 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
| 437 | FileManager::getBufferForFile(StringRef Filename) { |
| 438 | if (FileSystemOpts.WorkingDir.empty()) |
| 439 | return FS->getBufferForFile(Filename); |
Michael J. Spencer | f25faaa | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 440 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 441 | SmallString<128> FilePath(Filename); |
Anders Carlsson | 878b3e2 | 2011-03-07 01:28:33 +0000 | [diff] [blame] | 442 | FixupRelativePath(FilePath); |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame^] | 443 | return FS->getBufferForFile(FilePath.c_str()); |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 446 | /// getStatValue - Get the 'stat' information for the specified path, |
| 447 | /// using the cache to accelerate it if possible. This returns true |
| 448 | /// if the path points to a virtual file or does not exist, or returns |
| 449 | /// false if it's an existent real file. If FileDescriptor is NULL, |
| 450 | /// do directory look-up instead of file look-up. |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 451 | bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile, |
David Blaikie | 326ffb3 | 2014-07-08 15:46:02 +0000 | [diff] [blame] | 452 | std::unique_ptr<vfs::File> *F) { |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 453 | // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be |
| 454 | // absolute! |
Chris Lattner | 5769c3d | 2010-11-23 19:56:39 +0000 | [diff] [blame] | 455 | if (FileSystemOpts.WorkingDir.empty()) |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 456 | return FileSystemStatCache::get(Path, Data, isFile, F,StatCache.get(), *FS); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 457 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 458 | SmallString<128> FilePath(Path); |
Anders Carlsson | 878b3e2 | 2011-03-07 01:28:33 +0000 | [diff] [blame] | 459 | FixupRelativePath(FilePath); |
Chris Lattner | 5769c3d | 2010-11-23 19:56:39 +0000 | [diff] [blame] | 460 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 461 | return FileSystemStatCache::get(FilePath.c_str(), Data, isFile, F, |
| 462 | StatCache.get(), *FS); |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Rafael Espindola | e4777f4 | 2013-07-29 18:22:23 +0000 | [diff] [blame] | 465 | bool FileManager::getNoncachedStatValue(StringRef Path, |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 466 | vfs::Status &Result) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 467 | SmallString<128> FilePath(Path); |
Anders Carlsson | 5e36840 | 2011-03-18 19:23:19 +0000 | [diff] [blame] | 468 | FixupRelativePath(FilePath); |
| 469 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 470 | llvm::ErrorOr<vfs::Status> S = FS->status(FilePath.c_str()); |
| 471 | if (!S) |
| 472 | return true; |
| 473 | Result = *S; |
| 474 | return false; |
Anders Carlsson | 5e36840 | 2011-03-18 19:23:19 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Axel Naumann | b307400 | 2012-07-10 16:50:27 +0000 | [diff] [blame] | 477 | void FileManager::invalidateCache(const FileEntry *Entry) { |
| 478 | assert(Entry && "Cannot invalidate a NULL FileEntry"); |
Axel Naumann | 38179d9 | 2012-06-27 09:17:42 +0000 | [diff] [blame] | 479 | |
| 480 | SeenFileEntries.erase(Entry->getName()); |
Axel Naumann | b307400 | 2012-07-10 16:50:27 +0000 | [diff] [blame] | 481 | |
| 482 | // FileEntry invalidation should not block future optimizations in the file |
| 483 | // caches. Possible alternatives are cache truncation (invalidate last N) or |
| 484 | // invalidation of the whole cache. |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 485 | UniqueRealFiles.erase(Entry->getUniqueID()); |
Axel Naumann | 38179d9 | 2012-06-27 09:17:42 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 489 | void FileManager::GetUniqueIDMapping( |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 490 | SmallVectorImpl<const FileEntry *> &UIDToFiles) const { |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 491 | UIDToFiles.clear(); |
| 492 | UIDToFiles.resize(NextFileUID); |
| 493 | |
| 494 | // Map file entries |
| 495 | for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 496 | FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end(); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 497 | FE != FEEnd; ++FE) |
| 498 | if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE) |
| 499 | UIDToFiles[FE->getValue()->getUID()] = FE->getValue(); |
| 500 | |
| 501 | // Map virtual file entries |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 502 | for (SmallVectorImpl<FileEntry *>::const_iterator |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 503 | VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end(); |
| 504 | VFE != VFEEnd; ++VFE) |
| 505 | if (*VFE && *VFE != NON_EXISTENT_FILE) |
| 506 | UIDToFiles[(*VFE)->getUID()] = *VFE; |
| 507 | } |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 508 | |
Argyrios Kyrtzidis | 6eec06d | 2012-05-03 21:50:39 +0000 | [diff] [blame] | 509 | void FileManager::modifyFileEntry(FileEntry *File, |
| 510 | off_t Size, time_t ModificationTime) { |
| 511 | File->Size = Size; |
| 512 | File->ModTime = ModificationTime; |
| 513 | } |
| 514 | |
Douglas Gregor | e00c8b2 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 515 | StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) { |
| 516 | // FIXME: use llvm::sys::fs::canonical() when it gets implemented |
| 517 | #ifdef LLVM_ON_UNIX |
| 518 | llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known |
| 519 | = CanonicalDirNames.find(Dir); |
| 520 | if (Known != CanonicalDirNames.end()) |
| 521 | return Known->second; |
| 522 | |
| 523 | StringRef CanonicalName(Dir->getName()); |
| 524 | char CanonicalNameBuf[PATH_MAX]; |
| 525 | if (realpath(Dir->getName(), CanonicalNameBuf)) { |
| 526 | unsigned Len = strlen(CanonicalNameBuf); |
| 527 | char *Mem = static_cast<char *>(CanonicalNameStorage.Allocate(Len, 1)); |
| 528 | memcpy(Mem, CanonicalNameBuf, Len); |
| 529 | CanonicalName = StringRef(Mem, Len); |
| 530 | } |
| 531 | |
| 532 | CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName)); |
| 533 | return CanonicalName; |
| 534 | #else |
| 535 | return StringRef(Dir->getName()); |
| 536 | #endif |
| 537 | } |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 539 | void FileManager::PrintStats() const { |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 540 | llvm::errs() << "\n*** File Manager Stats:\n"; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 541 | llvm::errs() << UniqueRealFiles.size() << " real files found, " |
| 542 | << UniqueRealDirs.size() << " real dirs found.\n"; |
| 543 | llvm::errs() << VirtualFileEntries.size() << " virtual files found, " |
| 544 | << VirtualDirectoryEntries.size() << " virtual dirs found.\n"; |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 545 | llvm::errs() << NumDirLookups << " dir lookups, " |
| 546 | << NumDirCacheMisses << " dir cache misses.\n"; |
| 547 | llvm::errs() << NumFileLookups << " file lookups, " |
| 548 | << NumFileCacheMisses << " file cache misses.\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 550 | //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 551 | } |