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