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