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