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