Ted Kremenek | 8fbc88e | 2007-12-04 22:42:20 +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" |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileSystemOptions.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)); |
| 72 | return UniqueDirs.GetOrCreateValue( |
| 73 | FullPath.c_str(), |
| 74 | FullPath.c_str() + FullPath.size() |
| 75 | ).getValue(); |
| 76 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 78 | size_t size() { return UniqueDirs.size(); } |
| 79 | }; |
| 80 | |
| 81 | class FileManager::UniqueFileContainer { |
| 82 | /// UniqueFiles - Cache from full path to existing directories/files. |
| 83 | /// |
Ted Kremenek | 7536889 | 2009-01-28 01:01:07 +0000 | [diff] [blame] | 84 | llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 85 | |
| 86 | public: |
| 87 | FileEntry &getFile(const char *Name, struct stat &StatBuf) { |
| 88 | std::string FullPath(GetFullPath(Name)); |
Chris Lattner | c070da4 | 2010-08-23 23:50:42 +0000 | [diff] [blame] | 89 | |
| 90 | // LowercaseString because Windows filesystem is case insensitive. |
| 91 | FullPath = llvm::LowercaseString(FullPath); |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 92 | return UniqueFiles.GetOrCreateValue( |
| 93 | FullPath.c_str(), |
| 94 | FullPath.c_str() + FullPath.size() |
| 95 | ).getValue(); |
| 96 | } |
| 97 | |
| 98 | size_t size() { return UniqueFiles.size(); } |
| 99 | }; |
| 100 | |
Ted Kremenek | cb8d58b | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 101 | //===----------------------------------------------------------------------===// |
| 102 | // Unix-like Systems. |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 105 | #else |
| 106 | |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 107 | #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/') |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 108 | |
| 109 | class FileManager::UniqueDirContainer { |
| 110 | /// UniqueDirs - Cache from ID's to existing directories/files. |
| 111 | /// |
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 | |
| 119 | size_t size() { return UniqueDirs.size(); } |
| 120 | }; |
| 121 | |
| 122 | class FileManager::UniqueFileContainer { |
| 123 | /// UniqueFiles - Cache from ID's to existing directories/files. |
| 124 | /// |
| 125 | std::set<FileEntry> UniqueFiles; |
| 126 | |
| 127 | public: |
| 128 | FileEntry &getFile(const char *Name, struct stat &StatBuf) { |
| 129 | return |
| 130 | const_cast<FileEntry&>( |
| 131 | *UniqueFiles.insert(FileEntry(StatBuf.st_dev, |
Ted Kremenek | 96438f3 | 2009-02-12 03:17:57 +0000 | [diff] [blame] | 132 | StatBuf.st_ino, |
| 133 | StatBuf.st_mode)).first); |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | size_t size() { return UniqueFiles.size(); } |
| 137 | }; |
| 138 | |
| 139 | #endif |
| 140 | |
Ted Kremenek | cb8d58b | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 141 | //===----------------------------------------------------------------------===// |
| 142 | // Common logic. |
| 143 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 7ad97ff | 2010-11-23 07:51:02 +0000 | [diff] [blame] | 145 | FileManager::FileManager(const FileSystemOptions &FSO) |
| 146 | : FileSystemOpts(FSO), |
| 147 | UniqueDirs(*new UniqueDirContainer), |
Ted Kremenek | fc7052d | 2009-02-12 00:39:05 +0000 | [diff] [blame] | 148 | UniqueFiles(*new UniqueFileContainer), |
Ted Kremenek | 96438f3 | 2009-02-12 03:17:57 +0000 | [diff] [blame] | 149 | DirEntries(64), FileEntries(64), NextFileUID(0) { |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 150 | NumDirLookups = NumFileLookups = 0; |
| 151 | NumDirCacheMisses = NumFileCacheMisses = 0; |
| 152 | } |
| 153 | |
| 154 | FileManager::~FileManager() { |
| 155 | delete &UniqueDirs; |
| 156 | delete &UniqueFiles; |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 157 | for (llvm::SmallVectorImpl<FileEntry *>::iterator |
| 158 | V = VirtualFileEntries.begin(), |
| 159 | VEnd = VirtualFileEntries.end(); |
| 160 | V != VEnd; |
| 161 | ++V) |
| 162 | delete *V; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 165 | void FileManager::addStatCache(StatSysCallCache *statCache, bool AtBeginning) { |
| 166 | assert(statCache && "No stat cache provided?"); |
| 167 | if (AtBeginning || StatCache.get() == 0) { |
| 168 | statCache->setNextStatCache(StatCache.take()); |
| 169 | StatCache.reset(statCache); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | StatSysCallCache *LastCache = StatCache.get(); |
| 174 | while (LastCache->getNextStatCache()) |
| 175 | LastCache = LastCache->getNextStatCache(); |
| 176 | |
| 177 | LastCache->setNextStatCache(statCache); |
| 178 | } |
| 179 | |
| 180 | void FileManager::removeStatCache(StatSysCallCache *statCache) { |
| 181 | if (!statCache) |
| 182 | return; |
| 183 | |
| 184 | if (StatCache.get() == statCache) { |
| 185 | // This is the first stat cache. |
| 186 | StatCache.reset(StatCache->takeNextStatCache()); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | // Find the stat cache in the list. |
| 191 | StatSysCallCache *PrevCache = StatCache.get(); |
| 192 | while (PrevCache && PrevCache->getNextStatCache() != statCache) |
| 193 | PrevCache = PrevCache->getNextStatCache(); |
| 194 | if (PrevCache) |
| 195 | PrevCache->setNextStatCache(statCache->getNextStatCache()); |
| 196 | else |
| 197 | assert(false && "Stat cache not found for removal"); |
| 198 | } |
| 199 | |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 200 | /// \brief Retrieve the directory that the given file name resides in. |
| 201 | static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr, |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 202 | llvm::StringRef Filename, |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 203 | const FileSystemOptions &FileSystemOpts) { |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 204 | // Figure out what directory it is in. If the string contains a / in it, |
| 205 | // strip off everything after it. |
| 206 | // FIXME: this logic should be in sys::Path. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 207 | size_t SlashPos = Filename.size(); |
| 208 | while (SlashPos != 0 && !IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1])) |
| 209 | --SlashPos; |
| 210 | |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 211 | // Use the current directory if file has no path component. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 212 | if (SlashPos == 0) |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 213 | return FileMgr.getDirectory(".", FileSystemOpts); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 214 | |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 215 | if (SlashPos == Filename.size()-1) |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 216 | return 0; // If filename ends with a /, it's a directory. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 217 | |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 218 | // Ignore repeated //'s. |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 219 | while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1])) |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 220 | --SlashPos; |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 221 | |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 222 | return FileMgr.getDirectory(Filename.substr(0, SlashPos), FileSystemOpts); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 225 | /// getDirectory - Lookup, cache, and verify the specified directory. This |
| 226 | /// returns null if the directory doesn't exist. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | /// |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 228 | const DirectoryEntry *FileManager::getDirectory(llvm::StringRef Filename, |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 229 | const FileSystemOptions &FileSystemOpts) { |
John Thompson | 9a6ac54 | 2009-12-18 14:18:21 +0000 | [diff] [blame] | 230 | // stat doesn't like trailing separators (at least on Windows). |
Benjamin Kramer | aa8b2d9 | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 231 | if (Filename.size() > 1 && IS_DIR_SEPARATOR_CHAR(Filename.back())) |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 232 | Filename = Filename.substr(0, Filename.size()-1); |
John Thompson | 9a6ac54 | 2009-12-18 14:18:21 +0000 | [diff] [blame] | 233 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | ++NumDirLookups; |
| 235 | llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt = |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 236 | DirEntries.GetOrCreateValue(Filename); |
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 | // See if there is already an entry in the map. |
| 239 | if (NamedDirEnt.getValue()) |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 240 | return NamedDirEnt.getValue() == NON_EXISTENT_DIR |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 241 | ? 0 : NamedDirEnt.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 243 | ++NumDirCacheMisses; |
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 | // By default, initialize it to invalid. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 246 | NamedDirEnt.setValue(NON_EXISTENT_DIR); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | // Get the null-terminated directory name as stored as the key of the |
| 249 | // DirEntries map. |
| 250 | const char *InterndDirName = NamedDirEnt.getKeyData(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 252 | // Check to see if the directory exists. |
| 253 | struct stat StatBuf; |
Chris Lattner | 7ad97ff | 2010-11-23 07:51:02 +0000 | [diff] [blame] | 254 | if (stat_cached(InterndDirName, &StatBuf) || // Error stat'ing. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 255 | !S_ISDIR(StatBuf.st_mode)) // Not a directory? |
| 256 | return 0; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 257 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 258 | // 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] | 259 | // This occurs when one dir is symlinked to another, for example. |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 260 | DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 262 | NamedDirEnt.setValue(&UDE); |
| 263 | if (UDE.getName()) // Already have an entry with this inode, return it. |
| 264 | return &UDE; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | // Otherwise, we don't have this directory yet, add it. We use the string |
| 267 | // key from the DirEntries map as the string. |
| 268 | UDE.Name = InterndDirName; |
| 269 | return &UDE; |
| 270 | } |
| 271 | |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 272 | /// 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] | 273 | /// represent a filename that doesn't exist on the disk. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 274 | #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | |
| 276 | /// getFile - Lookup, cache, and verify the specified file. This returns null |
| 277 | /// if the file doesn't exist. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | /// |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 279 | const FileEntry *FileManager::getFile(llvm::StringRef Filename, |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 280 | const FileSystemOptions &FileSystemOpts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | ++NumFileLookups; |
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 | // See if there is already an entry in the map. |
| 284 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 285 | FileEntries.GetOrCreateValue(Filename); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | |
| 287 | // See if there is already an entry in the map. |
| 288 | if (NamedFileEnt.getValue()) |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 289 | return NamedFileEnt.getValue() == NON_EXISTENT_FILE |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 290 | ? 0 : NamedFileEnt.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 292 | ++NumFileCacheMisses; |
| 293 | |
| 294 | // By default, initialize it to invalid. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 295 | NamedFileEnt.setValue(NON_EXISTENT_FILE); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 296 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | // Get the null-terminated file name as stored as the key of the |
| 299 | // FileEntries map. |
| 300 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 302 | const DirectoryEntry *DirInfo |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 303 | = getDirectoryFromFile(*this, Filename, FileSystemOpts); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 304 | if (DirInfo == 0) // Directory doesn't exist, file can't exist. |
| 305 | return 0; |
| 306 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | // FIXME: Use the directory info to prune this, before doing the stat syscall. |
| 308 | // FIXME: This will reduce the # syscalls. |
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 | // Nope, there isn't. Check to see if the file exists. |
| 311 | struct stat StatBuf; |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 312 | //llvm::errs() << "STATING: " << Filename; |
Chris Lattner | 7ad97ff | 2010-11-23 07:51:02 +0000 | [diff] [blame] | 313 | if (stat_cached(InterndFileName, &StatBuf) || // Error stat'ing. |
Ted Kremenek | fc7052d | 2009-02-12 00:39:05 +0000 | [diff] [blame] | 314 | S_ISDIR(StatBuf.st_mode)) { // A directory? |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | // If this file doesn't exist, we leave a null in FileEntries for this path. |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 316 | //llvm::errs() << ": Not existing\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 317 | return 0; |
| 318 | } |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 319 | //llvm::errs() << ": exists\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | |
Ted Kremenek | bca6d12 | 2007-12-18 22:29:39 +0000 | [diff] [blame] | 321 | // 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] | 322 | // This occurs when one dir is symlinked to another, for example. |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 323 | FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 325 | NamedFileEnt.setValue(&UFE); |
| 326 | if (UFE.getName()) // Already have an entry with this inode, return it. |
| 327 | return &UFE; |
| 328 | |
| 329 | // Otherwise, we don't have this directory yet, add it. |
| 330 | // FIXME: Change the name to be a char* that points back to the 'FileEntries' |
| 331 | // key. |
| 332 | UFE.Name = InterndFileName; |
| 333 | UFE.Size = StatBuf.st_size; |
| 334 | UFE.ModTime = StatBuf.st_mtime; |
| 335 | UFE.Dir = DirInfo; |
| 336 | UFE.UID = NextFileUID++; |
| 337 | return &UFE; |
| 338 | } |
| 339 | |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 340 | const FileEntry * |
Benjamin Kramer | ec1b1cc | 2010-07-14 23:19:41 +0000 | [diff] [blame] | 341 | FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size, |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 342 | time_t ModificationTime, |
| 343 | const FileSystemOptions &FileSystemOpts) { |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 344 | ++NumFileLookups; |
| 345 | |
| 346 | // See if there is already an entry in the map. |
| 347 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 348 | FileEntries.GetOrCreateValue(Filename); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 349 | |
| 350 | // See if there is already an entry in the map. |
| 351 | if (NamedFileEnt.getValue()) |
| 352 | return NamedFileEnt.getValue() == NON_EXISTENT_FILE |
| 353 | ? 0 : NamedFileEnt.getValue(); |
| 354 | |
| 355 | ++NumFileCacheMisses; |
| 356 | |
| 357 | // By default, initialize it to invalid. |
| 358 | NamedFileEnt.setValue(NON_EXISTENT_FILE); |
| 359 | |
| 360 | const DirectoryEntry *DirInfo |
Chris Lattner | f69a1f3 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 361 | = getDirectoryFromFile(*this, Filename, FileSystemOpts); |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 362 | if (DirInfo == 0) // Directory doesn't exist, file can't exist. |
| 363 | return 0; |
| 364 | |
| 365 | FileEntry *UFE = new FileEntry(); |
| 366 | VirtualFileEntries.push_back(UFE); |
| 367 | NamedFileEnt.setValue(UFE); |
| 368 | |
| 369 | UFE->Name = NamedFileEnt.getKeyData(); |
| 370 | UFE->Size = Size; |
| 371 | UFE->ModTime = ModificationTime; |
| 372 | UFE->Dir = DirInfo; |
| 373 | UFE->UID = NextFileUID++; |
Douglas Gregor | 3e15e0a | 2010-07-26 23:54:23 +0000 | [diff] [blame] | 374 | |
| 375 | // If this virtual file resolves to a file, also map that file to the |
| 376 | // newly-created file entry. |
| 377 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
| 378 | struct stat StatBuf; |
Chris Lattner | 7ad97ff | 2010-11-23 07:51:02 +0000 | [diff] [blame] | 379 | if (!stat_cached(InterndFileName, &StatBuf) && |
Douglas Gregor | 3e15e0a | 2010-07-26 23:54:23 +0000 | [diff] [blame] | 380 | !S_ISDIR(StatBuf.st_mode)) { |
| 381 | llvm::sys::Path FilePath(InterndFileName); |
| 382 | FilePath.makeAbsolute(); |
| 383 | FileEntries[FilePath.str()] = UFE; |
| 384 | } |
| 385 | |
Douglas Gregor | 057e567 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 386 | return UFE; |
| 387 | } |
| 388 | |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 389 | void FileManager::FixupRelativePath(llvm::sys::Path &path, |
| 390 | const FileSystemOptions &FSOpts) { |
| 391 | if (FSOpts.WorkingDir.empty() || path.isAbsolute()) return; |
| 392 | |
| 393 | llvm::sys::Path NewPath(FSOpts.WorkingDir); |
| 394 | NewPath.appendComponent(path.str()); |
| 395 | path = NewPath; |
| 396 | } |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 398 | |
| 399 | |
| 400 | llvm::MemoryBuffer *FileManager:: |
| 401 | getBufferForFile(llvm::StringRef Filename, |
| 402 | const FileSystemOptions &FileSystemOpts, |
Chris Lattner | 151466a | 2010-11-23 06:09:11 +0000 | [diff] [blame] | 403 | std::string *ErrorStr, int64_t FileSize) { |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 404 | if (FileSystemOpts.WorkingDir.empty()) |
Chris Lattner | 151466a | 2010-11-23 06:09:11 +0000 | [diff] [blame] | 405 | return llvm::MemoryBuffer::getFile(Filename, ErrorStr, FileSize); |
Chris Lattner | 67452f5 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 406 | |
| 407 | llvm::sys::Path FilePath(Filename); |
| 408 | FixupRelativePath(FilePath, FileSystemOpts); |
Chris Lattner | 151466a | 2010-11-23 06:09:11 +0000 | [diff] [blame] | 409 | return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr, FileSize); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Chris Lattner | 7ad97ff | 2010-11-23 07:51:02 +0000 | [diff] [blame] | 412 | int FileManager::stat_cached(const char *path, struct stat *buf) { |
Chris Lattner | efc4be2 | 2010-11-23 04:33:43 +0000 | [diff] [blame] | 413 | if (FileSystemOpts.WorkingDir.empty()) |
| 414 | return StatCache.get() ? StatCache->stat(path, buf) : stat(path, buf); |
| 415 | |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 416 | llvm::sys::Path FilePath(path); |
| 417 | FixupRelativePath(FilePath, FileSystemOpts); |
| 418 | |
| 419 | return StatCache.get() ? StatCache->stat(FilePath.c_str(), buf) |
| 420 | : stat(FilePath.c_str(), buf); |
| 421 | } |
| 422 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 423 | void FileManager::PrintStats() const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 424 | llvm::errs() << "\n*** File Manager Stats:\n"; |
| 425 | llvm::errs() << UniqueFiles.size() << " files found, " |
| 426 | << UniqueDirs.size() << " dirs found.\n"; |
| 427 | llvm::errs() << NumDirLookups << " dir lookups, " |
| 428 | << NumDirCacheMisses << " dir cache misses.\n"; |
| 429 | llvm::errs() << NumFileLookups << " file lookups, " |
| 430 | << NumFileCacheMisses << " file cache misses.\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 432 | //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 433 | } |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 434 | |
| 435 | int MemorizeStatCalls::stat(const char *path, struct stat *buf) { |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 436 | int result = StatSysCallCache::stat(path, buf); |
| 437 | |
Daniel Dunbar | 475ddb4 | 2009-12-11 00:27:20 +0000 | [diff] [blame] | 438 | // Do not cache failed stats, it is easy to construct common inconsistent |
| 439 | // situations if we do, and they are not important for PCH performance (which |
| 440 | // currently only needs the stats to construct the initial FileManager |
| 441 | // entries). |
| 442 | if (result != 0) |
| 443 | return result; |
| 444 | |
| 445 | // Cache file 'stat' results and directories with absolutely paths. |
| 446 | if (!S_ISDIR(buf->st_mode) || llvm::sys::Path(path).isAbsolute()) |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 447 | StatCalls[path] = StatResult(result, *buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | |
| 449 | return result; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 450 | } |