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