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