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" |
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" |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 26 | #include "llvm/System/Path.h" |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 27 | #include "llvm/Config/config.h" |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 28 | #include <map> |
| 29 | #include <set> |
| 30 | #include <string> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | |
| 33 | // FIXME: Enhance libsystem to support inode and other fields. |
| 34 | #include <sys/stat.h> |
| 35 | |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 36 | /// 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] | 37 | /// represent a dir name that doesn't exist on the disk. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 38 | #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 40 | /// NON_EXISTENT_FILE - A special value distinct from null that is used to |
| 41 | /// represent a filename that doesn't exist on the disk. |
| 42 | #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1) |
| 43 | |
| 44 | |
| 45 | FileEntry::~FileEntry() { |
| 46 | // If this FileEntry owns an open file descriptor that never got used, close |
| 47 | // it. |
| 48 | if (FD != -1) ::close(FD); |
| 49 | } |
| 50 | |
Ted Kremenek | cb8d58b | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // Windows. |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 55 | #ifdef LLVM_ON_WIN32 |
| 56 | |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 57 | #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\') |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 58 | |
| 59 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | static std::string GetFullPath(const char *relPath) { |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 61 | char *absPathStrPtr = _fullpath(NULL, relPath, 0); |
| 62 | assert(absPathStrPtr && "_fullpath() returned NULL!"); |
| 63 | |
| 64 | std::string absPath(absPathStrPtr); |
| 65 | |
| 66 | free(absPathStrPtr); |
| 67 | return absPath; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | class FileManager::UniqueDirContainer { |
| 72 | /// UniqueDirs - Cache from full path to existing directories/files. |
| 73 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | llvm::StringMap<DirectoryEntry> UniqueDirs; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 75 | |
| 76 | public: |
| 77 | DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) { |
| 78 | std::string FullPath(GetFullPath(Name)); |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 79 | return UniqueDirs.GetOrCreateValue(FullPath).getValue(); |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 80 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 82 | size_t size() const { return UniqueDirs.size(); } |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | class FileManager::UniqueFileContainer { |
| 86 | /// UniqueFiles - Cache from full path to existing directories/files. |
| 87 | /// |
Ted Kremenek | 7536889 | 2009-01-28 01:01:07 +0000 | [diff] [blame] | 88 | llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 89 | |
| 90 | public: |
| 91 | FileEntry &getFile(const char *Name, struct stat &StatBuf) { |
| 92 | std::string FullPath(GetFullPath(Name)); |
Chris Lattner | c070da4 | 2010-08-23 23:50:42 +0000 | [diff] [blame] | 93 | |
| 94 | // LowercaseString because Windows filesystem is case insensitive. |
| 95 | FullPath = llvm::LowercaseString(FullPath); |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 96 | return UniqueFiles.GetOrCreateValue(FullPath).getValue(); |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 99 | size_t size() const { return UniqueFiles.size(); } |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
Ted Kremenek | cb8d58b | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 102 | //===----------------------------------------------------------------------===// |
| 103 | // Unix-like Systems. |
| 104 | //===----------------------------------------------------------------------===// |
| 105 | |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 106 | #else |
| 107 | |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 108 | #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/') |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 109 | |
| 110 | class FileManager::UniqueDirContainer { |
| 111 | /// UniqueDirs - Cache from ID's to existing directories/files. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 113 | |
| 114 | public: |
| 115 | DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) { |
| 116 | return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)]; |
| 117 | } |
| 118 | |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 119 | size_t size() const { return UniqueDirs.size(); } |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | class FileManager::UniqueFileContainer { |
| 123 | /// UniqueFiles - Cache from ID's to existing directories/files. |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 124 | std::set<FileEntry> UniqueFiles; |
| 125 | |
| 126 | public: |
| 127 | FileEntry &getFile(const char *Name, struct stat &StatBuf) { |
| 128 | return |
| 129 | const_cast<FileEntry&>( |
| 130 | *UniqueFiles.insert(FileEntry(StatBuf.st_dev, |
Ted Kremenek | 96438f3 | 2009-02-12 03:17:57 +0000 | [diff] [blame] | 131 | StatBuf.st_ino, |
| 132 | StatBuf.st_mode)).first); |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 135 | size_t size() const { return UniqueFiles.size(); } |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | #endif |
| 139 | |
Ted Kremenek | cb8d58b | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 140 | //===----------------------------------------------------------------------===// |
| 141 | // Common logic. |
| 142 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 7ad97ff | 2010-11-23 07:51:02 +0000 | [diff] [blame] | 144 | FileManager::FileManager(const FileSystemOptions &FSO) |
| 145 | : FileSystemOpts(FSO), |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 146 | UniqueDirs(*new UniqueDirContainer()), |
| 147 | UniqueFiles(*new UniqueFileContainer()), |
Ted Kremenek | 96438f3 | 2009-02-12 03:17:57 +0000 | [diff] [blame] | 148 | DirEntries(64), FileEntries(64), NextFileUID(0) { |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 149 | NumDirLookups = NumFileLookups = 0; |
| 150 | NumDirCacheMisses = NumFileCacheMisses = 0; |
| 151 | } |
| 152 | |
| 153 | FileManager::~FileManager() { |
| 154 | delete &UniqueDirs; |
| 155 | delete &UniqueFiles; |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 156 | for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i) |
| 157 | delete VirtualFileEntries[i]; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 160 | void FileManager::addStatCache(FileSystemStatCache *statCache, |
| 161 | bool AtBeginning) { |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 162 | assert(statCache && "No stat cache provided?"); |
| 163 | if (AtBeginning || StatCache.get() == 0) { |
| 164 | statCache->setNextStatCache(StatCache.take()); |
| 165 | StatCache.reset(statCache); |
| 166 | return; |
| 167 | } |
| 168 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 169 | FileSystemStatCache *LastCache = StatCache.get(); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 170 | while (LastCache->getNextStatCache()) |
| 171 | LastCache = LastCache->getNextStatCache(); |
| 172 | |
| 173 | LastCache->setNextStatCache(statCache); |
| 174 | } |
| 175 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 176 | void FileManager::removeStatCache(FileSystemStatCache *statCache) { |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 177 | if (!statCache) |
| 178 | return; |
| 179 | |
| 180 | if (StatCache.get() == statCache) { |
| 181 | // This is the first stat cache. |
| 182 | StatCache.reset(StatCache->takeNextStatCache()); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | // Find the stat cache in the list. |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 187 | FileSystemStatCache *PrevCache = StatCache.get(); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 188 | while (PrevCache && PrevCache->getNextStatCache() != statCache) |
| 189 | PrevCache = PrevCache->getNextStatCache(); |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 190 | |
| 191 | assert(PrevCache && "Stat cache not found for removal"); |
| 192 | PrevCache->setNextStatCache(statCache->getNextStatCache()); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 195 | /// \brief Retrieve the directory that the given file name resides in. |
| 196 | static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr, |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 197 | llvm::StringRef Filename) { |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 198 | // Figure out what directory it is in. If the string contains a / in it, |
| 199 | // strip off everything after it. |
| 200 | // FIXME: this logic should be in sys::Path. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 201 | size_t SlashPos = Filename.size(); |
| 202 | while (SlashPos != 0 && !IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1])) |
| 203 | --SlashPos; |
| 204 | |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 205 | // Use the current directory if file has no path component. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 206 | if (SlashPos == 0) |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 207 | return FileMgr.getDirectory("."); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 208 | |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 209 | if (SlashPos == Filename.size()-1) |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 210 | return 0; // If filename ends with a /, it's a directory. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 211 | |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 212 | // Ignore repeated //'s. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 213 | while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1])) |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 214 | --SlashPos; |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 216 | return FileMgr.getDirectory(Filename.substr(0, SlashPos)); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | /// getDirectory - Lookup, cache, and verify the specified directory. This |
| 220 | /// returns null if the directory doesn't exist. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | /// |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 222 | const DirectoryEntry *FileManager::getDirectory(llvm::StringRef Filename) { |
John Thompson | 9a6ac54 | 2009-12-18 14:18:21 +0000 | [diff] [blame] | 223 | // stat doesn't like trailing separators (at least on Windows). |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 224 | if (Filename.size() > 1 && IS_DIR_SEPARATOR_CHAR(Filename.back())) |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 225 | Filename = Filename.substr(0, Filename.size()-1); |
John Thompson | 9a6ac54 | 2009-12-18 14:18:21 +0000 | [diff] [blame] | 226 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 227 | ++NumDirLookups; |
| 228 | llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt = |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 229 | DirEntries.GetOrCreateValue(Filename); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 231 | // See if there is already an entry in the map. |
| 232 | if (NamedDirEnt.getValue()) |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 233 | return NamedDirEnt.getValue() == NON_EXISTENT_DIR |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | ? 0 : NamedDirEnt.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | ++NumDirCacheMisses; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 238 | // By default, initialize it to invalid. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 239 | NamedDirEnt.setValue(NON_EXISTENT_DIR); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 241 | // Get the null-terminated directory name as stored as the key of the |
| 242 | // DirEntries map. |
| 243 | const char *InterndDirName = NamedDirEnt.getKeyData(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | // Check to see if the directory exists. |
| 246 | struct stat StatBuf; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame^] | 247 | if (getStatValue(InterndDirName, StatBuf, 0/*directory lookup*/)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | return 0; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 249 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | // It exists. See if we have already opened a directory with the same inode. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | // This occurs when one dir is symlinked to another, for example. |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 252 | DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 254 | NamedDirEnt.setValue(&UDE); |
| 255 | if (UDE.getName()) // Already have an entry with this inode, return it. |
| 256 | return &UDE; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 258 | // Otherwise, we don't have this directory yet, add it. We use the string |
| 259 | // key from the DirEntries map as the string. |
| 260 | UDE.Name = InterndDirName; |
| 261 | return &UDE; |
| 262 | } |
| 263 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 264 | /// getFile - Lookup, cache, and verify the specified file. This returns null |
| 265 | /// if the file doesn't exist. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | /// |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 267 | const FileEntry *FileManager::getFile(llvm::StringRef Filename) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | ++NumFileLookups; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | // See if there is already an entry in the map. |
| 271 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 272 | FileEntries.GetOrCreateValue(Filename); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 273 | |
| 274 | // See if there is already an entry in the map. |
| 275 | if (NamedFileEnt.getValue()) |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 276 | return NamedFileEnt.getValue() == NON_EXISTENT_FILE |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | ? 0 : NamedFileEnt.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | ++NumFileCacheMisses; |
| 280 | |
| 281 | // By default, initialize it to invalid. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 282 | NamedFileEnt.setValue(NON_EXISTENT_FILE); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 283 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 285 | // Get the null-terminated file name as stored as the key of the |
| 286 | // FileEntries map. |
| 287 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 288 | |
Chris Lattner | f3e8a99 | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 289 | |
| 290 | // Look up the directory for the file. When looking up something like |
| 291 | // sys/foo.h we'll discover all of the search directories that have a 'sys' |
| 292 | // subdirectory. This will let us avoid having to waste time on known-to-fail |
| 293 | // searches when we go to find sys/bar.h, because all the search directories |
| 294 | // without a 'sys' subdir will get a cached failure result. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 295 | const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 296 | if (DirInfo == 0) // Directory doesn't exist, file can't exist. |
| 297 | return 0; |
| 298 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 299 | // FIXME: Use the directory info to prune this, before doing the stat syscall. |
| 300 | // FIXME: This will reduce the # syscalls. |
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 | // Nope, there isn't. Check to see if the file exists. |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame^] | 303 | int FileDescriptor = -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 304 | struct stat StatBuf; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame^] | 305 | if (getStatValue(InterndFileName, StatBuf, &FileDescriptor)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 306 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Ted Kremenek | bca6d12 | 2007-12-18 22:29:39 +0000 | [diff] [blame] | 308 | // 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] | 309 | // This occurs when one dir is symlinked to another, for example. |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 310 | FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf); |
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 | NamedFileEnt.setValue(&UFE); |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame^] | 313 | if (UFE.getName()) { // Already have an entry with this inode, return it. |
| 314 | // If the stat process opened the file, close it to avoid a FD leak. |
| 315 | if (FileDescriptor != -1) |
| 316 | close(FileDescriptor); |
| 317 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 318 | return &UFE; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame^] | 319 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 320 | |
| 321 | // Otherwise, we don't have this directory yet, add it. |
| 322 | // FIXME: Change the name to be a char* that points back to the 'FileEntries' |
| 323 | // key. |
| 324 | UFE.Name = InterndFileName; |
| 325 | UFE.Size = StatBuf.st_size; |
| 326 | UFE.ModTime = StatBuf.st_mtime; |
| 327 | UFE.Dir = DirInfo; |
| 328 | UFE.UID = NextFileUID++; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame^] | 329 | UFE.FD = FileDescriptor; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 330 | return &UFE; |
| 331 | } |
| 332 | |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 333 | const FileEntry * |
Benjamin Kramer | ec1b1cc | 2010-07-14 23:19:41 +0000 | [diff] [blame] | 334 | FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size, |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 335 | time_t ModificationTime) { |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 336 | ++NumFileLookups; |
| 337 | |
| 338 | // See if there is already an entry in the map. |
| 339 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 340 | FileEntries.GetOrCreateValue(Filename); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 341 | |
| 342 | // See if there is already an entry in the map. |
| 343 | if (NamedFileEnt.getValue()) |
| 344 | return NamedFileEnt.getValue() == NON_EXISTENT_FILE |
| 345 | ? 0 : NamedFileEnt.getValue(); |
| 346 | |
| 347 | ++NumFileCacheMisses; |
| 348 | |
| 349 | // By default, initialize it to invalid. |
| 350 | NamedFileEnt.setValue(NON_EXISTENT_FILE); |
| 351 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 352 | const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 353 | if (DirInfo == 0) // Directory doesn't exist, file can't exist. |
| 354 | return 0; |
| 355 | |
| 356 | FileEntry *UFE = new FileEntry(); |
| 357 | VirtualFileEntries.push_back(UFE); |
| 358 | NamedFileEnt.setValue(UFE); |
| 359 | |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 360 | // Get the null-terminated file name as stored as the key of the |
| 361 | // FileEntries map. |
| 362 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
| 363 | |
| 364 | UFE->Name = InterndFileName; |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 365 | UFE->Size = Size; |
| 366 | UFE->ModTime = ModificationTime; |
| 367 | UFE->Dir = DirInfo; |
| 368 | UFE->UID = NextFileUID++; |
Douglas Gregor | 3e15e0a | 2010-07-26 23:54:23 +0000 | [diff] [blame] | 369 | |
| 370 | // If this virtual file resolves to a file, also map that file to the |
| 371 | // newly-created file entry. |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame^] | 372 | int FileDescriptor = -1; |
Douglas Gregor | 3e15e0a | 2010-07-26 23:54:23 +0000 | [diff] [blame] | 373 | struct stat StatBuf; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame^] | 374 | if (getStatValue(InterndFileName, StatBuf, &FileDescriptor)) |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 375 | return UFE; |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame^] | 376 | |
| 377 | UFE->FD = FileDescriptor; |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 378 | llvm::sys::Path FilePath(UFE->Name); |
| 379 | FilePath.makeAbsolute(); |
| 380 | FileEntries[FilePath.str()] = UFE; |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 381 | return UFE; |
| 382 | } |
| 383 | |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 384 | void FileManager::FixupRelativePath(llvm::sys::Path &path, |
| 385 | const FileSystemOptions &FSOpts) { |
| 386 | if (FSOpts.WorkingDir.empty() || path.isAbsolute()) return; |
| 387 | |
| 388 | llvm::sys::Path NewPath(FSOpts.WorkingDir); |
| 389 | NewPath.appendComponent(path.str()); |
| 390 | path = NewPath; |
| 391 | } |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 393 | llvm::MemoryBuffer *FileManager:: |
Chris Lattner | 75dfb65 | 2010-11-23 09:19:42 +0000 | [diff] [blame] | 394 | getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) { |
| 395 | llvm::StringRef Filename = Entry->getName(); |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 396 | if (FileSystemOpts.WorkingDir.empty()) |
Chris Lattner | f8f6129 | 2010-11-23 19:38:22 +0000 | [diff] [blame] | 397 | return llvm::MemoryBuffer::getFile(Filename, ErrorStr, Entry->getSize()); |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 398 | |
| 399 | llvm::sys::Path FilePath(Filename); |
| 400 | FixupRelativePath(FilePath, FileSystemOpts); |
Chris Lattner | f8f6129 | 2010-11-23 19:38:22 +0000 | [diff] [blame] | 401 | return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr, |
| 402 | Entry->getSize()); |
Chris Lattner | 75dfb65 | 2010-11-23 09:19:42 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | llvm::MemoryBuffer *FileManager:: |
| 406 | getBufferForFile(llvm::StringRef Filename, std::string *ErrorStr) { |
| 407 | if (FileSystemOpts.WorkingDir.empty()) |
| 408 | return llvm::MemoryBuffer::getFile(Filename, ErrorStr); |
| 409 | |
| 410 | llvm::sys::Path FilePath(Filename); |
| 411 | FixupRelativePath(FilePath, FileSystemOpts); |
| 412 | return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 415 | /// getStatValue - Get the 'stat' information for the specified path, using the |
| 416 | /// cache to accellerate it if possible. This returns true if the path does not |
| 417 | /// exist or false if it exists. |
Chris Lattner | f9f7766 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 418 | /// |
| 419 | /// The isForDir member indicates whether this is a directory lookup or not. |
| 420 | /// This will return failure if the lookup isn't the expected kind. |
| 421 | bool FileManager::getStatValue(const char *Path, struct stat &StatBuf, |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame^] | 422 | int *FileDescriptor) { |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 423 | // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be |
| 424 | // absolute! |
Chris Lattner | 11aa4b0 | 2010-11-23 19:56:39 +0000 | [diff] [blame] | 425 | if (FileSystemOpts.WorkingDir.empty()) |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame^] | 426 | return FileSystemStatCache::get(Path, StatBuf, FileDescriptor, |
| 427 | StatCache.get()); |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 428 | |
| 429 | llvm::sys::Path FilePath(Path); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 430 | FixupRelativePath(FilePath, FileSystemOpts); |
Chris Lattner | 11aa4b0 | 2010-11-23 19:56:39 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 898a061 | 2010-11-23 21:17:56 +0000 | [diff] [blame^] | 432 | return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor, |
| 433 | StatCache.get()); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Chris Lattner | 10e286a | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 436 | |
| 437 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 438 | void FileManager::PrintStats() const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 439 | llvm::errs() << "\n*** File Manager Stats:\n"; |
| 440 | llvm::errs() << UniqueFiles.size() << " files found, " |
| 441 | << UniqueDirs.size() << " dirs found.\n"; |
| 442 | llvm::errs() << NumDirLookups << " dir lookups, " |
| 443 | << NumDirCacheMisses << " dir cache misses.\n"; |
| 444 | llvm::errs() << NumFileLookups << " file lookups, " |
| 445 | << NumFileCacheMisses << " file cache misses.\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 447 | //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 448 | } |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 449 | |