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