Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 1 | //===--- FileManager.cpp - File System Probing and Caching ----------------===// |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +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 | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileSystemStatCache.h" |
Adrian Prantl | 075bf56 | 2015-07-09 02:53:05 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/PCHContainerOperations.h" |
Chris Lattner | 2f4a89a | 2006-10-30 03:55:17 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "llvm/Config/llvm-config.h" |
Michael J. Spencer | 740857f | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 25 | #include "llvm/Support/FileSystem.h" |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Path.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 26db648 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 29 | #include <map> |
| 30 | #include <set> |
| 31 | #include <string> |
Rafael Espindola | 8a8e554 | 2014-06-12 17:19:42 +0000 | [diff] [blame] | 32 | #include <system_error> |
Chris Lattner | 278038b | 2010-11-23 21:53:15 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
Ted Kremenek | 8d71e25 | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 36 | /// NON_EXISTENT_DIR - A special value distinct from null that is used to |
Chris Lattner | af65375 | 2006-10-30 03:06:54 +0000 | [diff] [blame] | 37 | /// represent a dir name that doesn't exist on the disk. |
Ted Kremenek | 8d71e25 | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 38 | #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1) |
Chris Lattner | af65375 | 2006-10-30 03:06:54 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 9624b69 | 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 | |
Ted Kremenek | 5c04bd8 | 2009-01-28 00:27:31 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
| 45 | // Common logic. |
| 46 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d87eef8 | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 47 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 48 | FileManager::FileManager(const FileSystemOptions &FSO, |
| 49 | IntrusiveRefCntPtr<vfs::FileSystem> FS) |
| 50 | : FS(FS), FileSystemOpts(FSO), |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 51 | SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) { |
Ted Kremenek | d87eef8 | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 52 | NumDirLookups = NumFileLookups = 0; |
| 53 | NumDirCacheMisses = NumFileCacheMisses = 0; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 54 | |
| 55 | // If the caller doesn't provide a virtual file system, just grab the real |
| 56 | // file system. |
| 57 | if (!FS) |
| 58 | this->FS = vfs::getRealFileSystem(); |
Ted Kremenek | d87eef8 | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | FileManager::~FileManager() { |
Chris Lattner | 966b25b | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 62 | for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i) |
| 63 | delete VirtualFileEntries[i]; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 64 | for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i) |
| 65 | delete VirtualDirectoryEntries[i]; |
Ted Kremenek | d87eef8 | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 66 | } |
| 67 | |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 68 | void FileManager::addStatCache(std::unique_ptr<FileSystemStatCache> statCache, |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 69 | bool AtBeginning) { |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 70 | assert(statCache && "No stat cache provided?"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 71 | if (AtBeginning || !StatCache.get()) { |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 72 | statCache->setNextStatCache(std::move(StatCache)); |
| 73 | StatCache = std::move(statCache); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 77 | FileSystemStatCache *LastCache = StatCache.get(); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 78 | while (LastCache->getNextStatCache()) |
| 79 | LastCache = LastCache->getNextStatCache(); |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 80 | |
| 81 | LastCache->setNextStatCache(std::move(statCache)); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 84 | void FileManager::removeStatCache(FileSystemStatCache *statCache) { |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 85 | if (!statCache) |
| 86 | return; |
| 87 | |
| 88 | if (StatCache.get() == statCache) { |
| 89 | // This is the first stat cache. |
David Blaikie | dd0e1e8 | 2014-08-10 16:57:11 +0000 | [diff] [blame] | 90 | StatCache = StatCache->takeNextStatCache(); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
| 94 | // Find the stat cache in the list. |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 95 | FileSystemStatCache *PrevCache = StatCache.get(); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 96 | while (PrevCache && PrevCache->getNextStatCache() != statCache) |
| 97 | PrevCache = PrevCache->getNextStatCache(); |
Chris Lattner | 9624b69 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 98 | |
| 99 | assert(PrevCache && "Stat cache not found for removal"); |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 100 | PrevCache->setNextStatCache(statCache->takeNextStatCache()); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Manuel Klimek | 3aad855 | 2012-07-31 13:56:54 +0000 | [diff] [blame] | 103 | void FileManager::clearStatCaches() { |
David Blaikie | 3875a82 | 2014-07-19 01:06:45 +0000 | [diff] [blame] | 104 | StatCache.reset(); |
Manuel Klimek | 3aad855 | 2012-07-31 13:56:54 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 107 | /// \brief Retrieve the directory that the given file name resides in. |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 108 | /// Filename can point to either a real file or a virtual file. |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 109 | static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr, |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 110 | StringRef Filename, |
| 111 | bool CacheFailure) { |
Zhanyong Wan | f3c0ff7 | 2011-02-11 21:25:35 +0000 | [diff] [blame] | 112 | if (Filename.empty()) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 113 | return nullptr; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 114 | |
Zhanyong Wan | f3c0ff7 | 2011-02-11 21:25:35 +0000 | [diff] [blame] | 115 | if (llvm::sys::path::is_separator(Filename[Filename.size() - 1])) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 116 | return nullptr; // If Filename is a directory. |
Benjamin Kramer | 3cf715d | 2010-11-21 11:32:22 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 118 | StringRef DirName = llvm::sys::path::parent_path(Filename); |
Chris Lattner | 0c0e804 | 2010-11-21 09:50:16 +0000 | [diff] [blame] | 119 | // Use the current directory if file has no path component. |
Zhanyong Wan | f3c0ff7 | 2011-02-11 21:25:35 +0000 | [diff] [blame] | 120 | if (DirName.empty()) |
| 121 | DirName = "."; |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 122 | |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 123 | return FileMgr.getDirectory(DirName, CacheFailure); |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 126 | /// Add all ancestors of the given path (pointing to either a file or |
| 127 | /// a directory) as virtual directories. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 128 | void FileManager::addAncestorsAsVirtualDirs(StringRef Path) { |
| 129 | StringRef DirName = llvm::sys::path::parent_path(Path); |
Zhanyong Wan | f3c0ff7 | 2011-02-11 21:25:35 +0000 | [diff] [blame] | 130 | if (DirName.empty()) |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 131 | return; |
| 132 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 133 | auto &NamedDirEnt = |
| 134 | *SeenDirEntries.insert(std::make_pair(DirName, nullptr)).first; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 135 | |
| 136 | // When caching a virtual directory, we always cache its ancestors |
| 137 | // at the same time. Therefore, if DirName is already in the cache, |
| 138 | // we don't need to recurse as its ancestors must also already be in |
| 139 | // the cache. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 140 | if (NamedDirEnt.second) |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 141 | return; |
| 142 | |
| 143 | // Add the virtual directory to the cache. |
| 144 | DirectoryEntry *UDE = new DirectoryEntry; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 145 | UDE->Name = NamedDirEnt.first().data(); |
| 146 | NamedDirEnt.second = UDE; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 147 | VirtualDirectoryEntries.push_back(UDE); |
| 148 | |
| 149 | // Recursively add the other ancestors. |
| 150 | addAncestorsAsVirtualDirs(DirName); |
| 151 | } |
| 152 | |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 153 | const DirectoryEntry *FileManager::getDirectory(StringRef DirName, |
| 154 | bool CacheFailure) { |
NAKAMURA Takumi | 8bd8ee7 | 2012-06-16 06:04:10 +0000 | [diff] [blame] | 155 | // stat doesn't like trailing separators except for root directory. |
NAKAMURA Takumi | 32f1acf | 2011-11-17 06:16:05 +0000 | [diff] [blame] | 156 | // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'. |
| 157 | // (though it can strip '\\') |
NAKAMURA Takumi | 8bd8ee7 | 2012-06-16 06:04:10 +0000 | [diff] [blame] | 158 | if (DirName.size() > 1 && |
| 159 | DirName != llvm::sys::path::root_path(DirName) && |
| 160 | llvm::sys::path::is_separator(DirName.back())) |
NAKAMURA Takumi | 32f1acf | 2011-11-17 06:16:05 +0000 | [diff] [blame] | 161 | DirName = DirName.substr(0, DirName.size()-1); |
Rafael Espindola | ee30546 | 2013-07-29 15:47:24 +0000 | [diff] [blame] | 162 | #ifdef LLVM_ON_WIN32 |
| 163 | // Fixing a problem with "clang C:test.c" on Windows. |
| 164 | // Stat("C:") does not recognize "C:" as a valid directory |
| 165 | std::string DirNameStr; |
| 166 | if (DirName.size() > 1 && DirName.back() == ':' && |
| 167 | DirName.equals_lower(llvm::sys::path::root_name(DirName))) { |
| 168 | DirNameStr = DirName.str() + '.'; |
| 169 | DirName = DirNameStr; |
| 170 | } |
| 171 | #endif |
NAKAMURA Takumi | 32f1acf | 2011-11-17 06:16:05 +0000 | [diff] [blame] | 172 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 173 | ++NumDirLookups; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 174 | auto &NamedDirEnt = |
| 175 | *SeenDirEntries.insert(std::make_pair(DirName, nullptr)).first; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 177 | // See if there was already an entry in the map. Note that the map |
| 178 | // contains both virtual and real directories. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 179 | if (NamedDirEnt.second) |
| 180 | return NamedDirEnt.second == NON_EXISTENT_DIR ? nullptr |
| 181 | : NamedDirEnt.second; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 183 | ++NumDirCacheMisses; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Chris Lattner | af65375 | 2006-10-30 03:06:54 +0000 | [diff] [blame] | 185 | // By default, initialize it to invalid. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 186 | NamedDirEnt.second = NON_EXISTENT_DIR; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 43fd42e | 2006-10-30 03:40:58 +0000 | [diff] [blame] | 188 | // Get the null-terminated directory name as stored as the key of the |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 189 | // SeenDirEntries map. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 190 | const char *InterndDirName = NamedDirEnt.first().data(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Chris Lattner | af65375 | 2006-10-30 03:06:54 +0000 | [diff] [blame] | 192 | // Check to see if the directory exists. |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 193 | FileData Data; |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 194 | if (getStatValue(InterndDirName, Data, false, nullptr /*directory lookup*/)) { |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 195 | // There's no real directory at the given path. |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 196 | if (!CacheFailure) |
| 197 | SeenDirEntries.erase(DirName); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 198 | return nullptr; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 199 | } |
Ted Kremenek | d87eef8 | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 200 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 201 | // It exists. See if we have already opened a directory with the |
| 202 | // same inode (this occurs on Unix-like systems when one dir is |
| 203 | // symlinked to another, for example) or the same path (on |
| 204 | // Windows). |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 205 | DirectoryEntry &UDE = UniqueRealDirs[Data.UniqueID]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 207 | NamedDirEnt.second = &UDE; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 208 | if (!UDE.getName()) { |
| 209 | // We don't have this directory yet, add it. We use the string |
| 210 | // key from the SeenDirEntries map as the string. |
| 211 | UDE.Name = InterndDirName; |
| 212 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 34d1f5a | 2007-02-08 19:08:49 +0000 | [diff] [blame] | 214 | return &UDE; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 217 | const FileEntry *FileManager::getFile(StringRef Filename, bool openFile, |
| 218 | bool CacheFailure) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 219 | ++NumFileLookups; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 221 | // See if there is already an entry in the map. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 222 | auto &NamedFileEnt = |
| 223 | *SeenFileEntries.insert(std::make_pair(Filename, nullptr)).first; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 2f4a89a | 2006-10-30 03:55:17 +0000 | [diff] [blame] | 225 | // See if there is already an entry in the map. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 226 | if (NamedFileEnt.second) |
| 227 | return NamedFileEnt.second == NON_EXISTENT_FILE ? nullptr |
| 228 | : NamedFileEnt.second; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 230 | ++NumFileCacheMisses; |
| 231 | |
Chris Lattner | 2f4a89a | 2006-10-30 03:55:17 +0000 | [diff] [blame] | 232 | // By default, initialize it to invalid. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 233 | NamedFileEnt.second = NON_EXISTENT_FILE; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 2f4a89a | 2006-10-30 03:55:17 +0000 | [diff] [blame] | 235 | // Get the null-terminated file name as stored as the key of the |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 236 | // SeenFileEntries map. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 237 | const char *InterndFileName = NamedFileEnt.first().data(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 966b25b | 2010-11-23 20:30:42 +0000 | [diff] [blame] | 239 | // Look up the directory for the file. When looking up something like |
| 240 | // sys/foo.h we'll discover all of the search directories that have a 'sys' |
| 241 | // subdirectory. This will let us avoid having to waste time on known-to-fail |
| 242 | // searches when we go to find sys/bar.h, because all the search directories |
| 243 | // without a 'sys' subdir will get a cached failure result. |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 244 | const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename, |
| 245 | CacheFailure); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 246 | if (DirInfo == nullptr) { // Directory doesn't exist, file can't exist. |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 247 | if (!CacheFailure) |
| 248 | SeenFileEntries.erase(Filename); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 249 | |
| 250 | return nullptr; |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 253 | // FIXME: Use the directory info to prune this, before doing the stat syscall. |
| 254 | // FIXME: This will reduce the # syscalls. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 256 | // Nope, there isn't. Check to see if the file exists. |
David Blaikie | 326ffb3 | 2014-07-08 15:46:02 +0000 | [diff] [blame] | 257 | std::unique_ptr<vfs::File> F; |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 258 | FileData Data; |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 259 | if (getStatValue(InterndFileName, Data, true, openFile ? &F : nullptr)) { |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 260 | // There's no real file at the given path. |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 261 | if (!CacheFailure) |
| 262 | SeenFileEntries.erase(Filename); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 263 | |
| 264 | return nullptr; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 265 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | |
Patrik Hagglund | ab01d4b | 2014-02-21 07:23:53 +0000 | [diff] [blame] | 267 | assert((openFile || !F) && "undesired open file"); |
Argyrios Kyrtzidis | d6278e3 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 268 | |
Ted Kremenek | f4c38c9 | 2007-12-18 22:29:39 +0000 | [diff] [blame] | 269 | // It exists. See if we have already opened a file with the same inode. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 270 | // This occurs when one dir is symlinked to another, for example. |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 271 | FileEntry &UFE = UniqueRealFiles[Data.UniqueID]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 273 | NamedFileEnt.second = &UFE; |
Ben Langmuir | ab86fbe | 2014-09-08 16:15:54 +0000 | [diff] [blame] | 274 | |
| 275 | // If the name returned by getStatValue is different than Filename, re-intern |
| 276 | // the name. |
| 277 | if (Data.Name != Filename) { |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 278 | auto &NamedFileEnt = |
| 279 | *SeenFileEntries.insert(std::make_pair(Data.Name, nullptr)).first; |
| 280 | if (!NamedFileEnt.second) |
| 281 | NamedFileEnt.second = &UFE; |
Ben Langmuir | ab86fbe | 2014-09-08 16:15:54 +0000 | [diff] [blame] | 282 | else |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 283 | assert(NamedFileEnt.second == &UFE && |
Ben Langmuir | ab86fbe | 2014-09-08 16:15:54 +0000 | [diff] [blame] | 284 | "filename from getStatValue() refers to wrong file"); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 285 | InterndFileName = NamedFileEnt.first().data(); |
Ben Langmuir | ab86fbe | 2014-09-08 16:15:54 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Ben Langmuir | c8a7146 | 2014-02-27 17:23:33 +0000 | [diff] [blame] | 288 | if (UFE.isValid()) { // Already have an entry with this inode, return it. |
Ben Langmuir | 5de00f3 | 2014-05-23 18:15:47 +0000 | [diff] [blame] | 289 | |
| 290 | // FIXME: this hack ensures that if we look up a file by a virtual path in |
| 291 | // the VFS that the getDir() will have the virtual path, even if we found |
| 292 | // the file by a 'real' path first. This is required in order to find a |
| 293 | // module's structure when its headers/module map are mapped in the VFS. |
| 294 | // We should remove this as soon as we can properly support a file having |
| 295 | // multiple names. |
| 296 | if (DirInfo != UFE.Dir && Data.IsVFSMapped) |
| 297 | UFE.Dir = DirInfo; |
| 298 | |
Manuel Klimek | c0ff990 | 2014-08-13 12:34:41 +0000 | [diff] [blame] | 299 | // Always update the name to use the last name by which a file was accessed. |
| 300 | // FIXME: Neither this nor always using the first name is correct; we want |
| 301 | // to switch towards a design where we return a FileName object that |
| 302 | // encapsulates both the name by which the file was accessed and the |
| 303 | // corresponding FileEntry. |
Ben Langmuir | ab86fbe | 2014-09-08 16:15:54 +0000 | [diff] [blame] | 304 | UFE.Name = InterndFileName; |
Manuel Klimek | c0ff990 | 2014-08-13 12:34:41 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 34d1f5a | 2007-02-08 19:08:49 +0000 | [diff] [blame] | 306 | return &UFE; |
Chris Lattner | dd27843 | 2010-11-23 21:17:56 +0000 | [diff] [blame] | 307 | } |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 308 | |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 309 | // Otherwise, we don't have this file yet, add it. |
Ben Langmuir | ab86fbe | 2014-09-08 16:15:54 +0000 | [diff] [blame] | 310 | UFE.Name = InterndFileName; |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 311 | UFE.Size = Data.Size; |
| 312 | UFE.ModTime = Data.ModTime; |
Chris Lattner | 2f4a89a | 2006-10-30 03:55:17 +0000 | [diff] [blame] | 313 | UFE.Dir = DirInfo; |
| 314 | UFE.UID = NextFileUID++; |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 315 | UFE.UniqueID = Data.UniqueID; |
| 316 | UFE.IsNamedPipe = Data.IsNamedPipe; |
| 317 | UFE.InPCH = Data.InPCH; |
David Blaikie | 326ffb3 | 2014-07-08 15:46:02 +0000 | [diff] [blame] | 318 | UFE.File = std::move(F); |
Ben Langmuir | c8a7146 | 2014-02-27 17:23:33 +0000 | [diff] [blame] | 319 | UFE.IsValid = true; |
Chris Lattner | 34d1f5a | 2007-02-08 19:08:49 +0000 | [diff] [blame] | 320 | return &UFE; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 323 | const FileEntry * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 324 | FileManager::getVirtualFile(StringRef Filename, off_t Size, |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 325 | time_t ModificationTime) { |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 326 | ++NumFileLookups; |
| 327 | |
| 328 | // See if there is already an entry in the map. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 329 | auto &NamedFileEnt = |
| 330 | *SeenFileEntries.insert(std::make_pair(Filename, nullptr)).first; |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 331 | |
| 332 | // See if there is already an entry in the map. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 333 | if (NamedFileEnt.second && NamedFileEnt.second != NON_EXISTENT_FILE) |
| 334 | return NamedFileEnt.second; |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 335 | |
| 336 | ++NumFileCacheMisses; |
| 337 | |
| 338 | // By default, initialize it to invalid. |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 339 | NamedFileEnt.second = NON_EXISTENT_FILE; |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 340 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 341 | addAncestorsAsVirtualDirs(Filename); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 342 | FileEntry *UFE = nullptr; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 343 | |
| 344 | // Now that all ancestors of Filename are in the cache, the |
| 345 | // following call is guaranteed to find the DirectoryEntry from the |
| 346 | // cache. |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 347 | const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename, |
| 348 | /*CacheFailure=*/true); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 349 | assert(DirInfo && |
| 350 | "The directory of a virtual file should already be in the cache."); |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 351 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 352 | // Check to see if the file exists. If so, drop the virtual file |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 353 | FileData Data; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 354 | const char *InterndFileName = NamedFileEnt.first().data(); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 355 | if (getStatValue(InterndFileName, Data, true, nullptr) == 0) { |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 356 | Data.Size = Size; |
| 357 | Data.ModTime = ModificationTime; |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 358 | UFE = &UniqueRealFiles[Data.UniqueID]; |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 359 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 360 | NamedFileEnt.second = UFE; |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 361 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 362 | // If we had already opened this file, close it now so we don't |
| 363 | // leak the descriptor. We're not going to use the file |
| 364 | // descriptor anyway, since this is a virtual file. |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 365 | if (UFE->File) |
| 366 | UFE->closeFile(); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 367 | |
| 368 | // If we already have an entry with this inode, return it. |
Ben Langmuir | c8a7146 | 2014-02-27 17:23:33 +0000 | [diff] [blame] | 369 | if (UFE->isValid()) |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 370 | return UFE; |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 371 | |
| 372 | UFE->UniqueID = Data.UniqueID; |
| 373 | UFE->IsNamedPipe = Data.IsNamedPipe; |
| 374 | UFE->InPCH = Data.InPCH; |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | if (!UFE) { |
| 378 | UFE = new FileEntry(); |
| 379 | VirtualFileEntries.push_back(UFE); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 380 | NamedFileEnt.second = UFE; |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 381 | } |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 9624b69 | 2010-11-23 20:50:22 +0000 | [diff] [blame] | 383 | UFE->Name = InterndFileName; |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 384 | UFE->Size = Size; |
| 385 | UFE->ModTime = ModificationTime; |
| 386 | UFE->Dir = DirInfo; |
| 387 | UFE->UID = NextFileUID++; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 388 | UFE->File.reset(); |
Douglas Gregor | 407e212 | 2009-12-02 18:12:28 +0000 | [diff] [blame] | 389 | return UFE; |
| 390 | } |
| 391 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 392 | void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const { |
| 393 | StringRef pathRef(path.data(), path.size()); |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 394 | |
Anders Carlsson | 9ba8fb1 | 2011-03-14 01:13:54 +0000 | [diff] [blame] | 395 | if (FileSystemOpts.WorkingDir.empty() |
| 396 | || llvm::sys::path::is_absolute(pathRef)) |
Michael J. Spencer | f28df4c | 2010-12-17 21:22:22 +0000 | [diff] [blame] | 397 | return; |
| 398 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 399 | SmallString<128> NewPath(FileSystemOpts.WorkingDir); |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 400 | llvm::sys::path::append(NewPath, pathRef); |
Chris Lattner | 6e64099 | 2010-11-23 04:45:28 +0000 | [diff] [blame] | 401 | path = NewPath; |
| 402 | } |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 403 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 404 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
| 405 | FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile, |
| 406 | bool ShouldCloseOpenFile) { |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 407 | uint64_t FileSize = Entry->getSize(); |
| 408 | // If there's a high enough chance that the file have changed since we |
| 409 | // got its size, force a stat before opening it. |
| 410 | if (isVolatile) |
| 411 | FileSize = -1; |
| 412 | |
Argyrios Kyrtzidis | 669b0b1 | 2011-03-15 00:47:44 +0000 | [diff] [blame] | 413 | const char *Filename = Entry->getName(); |
| 414 | // If the file is already open, use the open file descriptor. |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 415 | if (Entry->File) { |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 416 | auto Result = |
| 417 | Entry->File->getBuffer(Filename, FileSize, |
| 418 | /*RequiresNullTerminator=*/true, isVolatile); |
Ben Langmuir | 9801b25 | 2014-06-20 00:24:56 +0000 | [diff] [blame] | 419 | // FIXME: we need a set of APIs that can make guarantees about whether a |
| 420 | // FileEntry is open or not. |
| 421 | if (ShouldCloseOpenFile) |
| 422 | Entry->closeFile(); |
Rafael Espindola | 6406f7b | 2014-08-26 19:54:40 +0000 | [diff] [blame] | 423 | return Result; |
Argyrios Kyrtzidis | 669b0b1 | 2011-03-15 00:47:44 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // Otherwise, open the file. |
| 427 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 428 | if (FileSystemOpts.WorkingDir.empty()) |
| 429 | return FS->getBufferForFile(Filename, FileSize, |
| 430 | /*RequiresNullTerminator=*/true, isVolatile); |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 431 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 432 | SmallString<128> FilePath(Entry->getName()); |
Anders Carlsson | 878b3e2 | 2011-03-07 01:28:33 +0000 | [diff] [blame] | 433 | FixupRelativePath(FilePath); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 434 | return FS->getBufferForFile(FilePath, FileSize, |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 435 | /*RequiresNullTerminator=*/true, isVolatile); |
Chris Lattner | 26b5c19 | 2010-11-23 09:19:42 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 438 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
| 439 | FileManager::getBufferForFile(StringRef Filename) { |
| 440 | if (FileSystemOpts.WorkingDir.empty()) |
| 441 | return FS->getBufferForFile(Filename); |
Michael J. Spencer | f25faaa | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 442 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 443 | SmallString<128> FilePath(Filename); |
Anders Carlsson | 878b3e2 | 2011-03-07 01:28:33 +0000 | [diff] [blame] | 444 | FixupRelativePath(FilePath); |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 445 | return FS->getBufferForFile(FilePath.c_str()); |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 448 | /// getStatValue - Get the 'stat' information for the specified path, |
| 449 | /// using the cache to accelerate it if possible. This returns true |
| 450 | /// if the path points to a virtual file or does not exist, or returns |
| 451 | /// false if it's an existent real file. If FileDescriptor is NULL, |
| 452 | /// do directory look-up instead of file look-up. |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 453 | bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile, |
David Blaikie | 326ffb3 | 2014-07-08 15:46:02 +0000 | [diff] [blame] | 454 | std::unique_ptr<vfs::File> *F) { |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 455 | // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be |
| 456 | // absolute! |
Chris Lattner | 5769c3d | 2010-11-23 19:56:39 +0000 | [diff] [blame] | 457 | if (FileSystemOpts.WorkingDir.empty()) |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 458 | return FileSystemStatCache::get(Path, Data, isFile, F,StatCache.get(), *FS); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 459 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 460 | SmallString<128> FilePath(Path); |
Anders Carlsson | 878b3e2 | 2011-03-07 01:28:33 +0000 | [diff] [blame] | 461 | FixupRelativePath(FilePath); |
Chris Lattner | 5769c3d | 2010-11-23 19:56:39 +0000 | [diff] [blame] | 462 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 463 | return FileSystemStatCache::get(FilePath.c_str(), Data, isFile, F, |
| 464 | StatCache.get(), *FS); |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Rafael Espindola | e4777f4 | 2013-07-29 18:22:23 +0000 | [diff] [blame] | 467 | bool FileManager::getNoncachedStatValue(StringRef Path, |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 468 | vfs::Status &Result) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 469 | SmallString<128> FilePath(Path); |
Anders Carlsson | 5e36840 | 2011-03-18 19:23:19 +0000 | [diff] [blame] | 470 | FixupRelativePath(FilePath); |
| 471 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 472 | llvm::ErrorOr<vfs::Status> S = FS->status(FilePath.c_str()); |
| 473 | if (!S) |
| 474 | return true; |
| 475 | Result = *S; |
| 476 | return false; |
Anders Carlsson | 5e36840 | 2011-03-18 19:23:19 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Axel Naumann | b307400 | 2012-07-10 16:50:27 +0000 | [diff] [blame] | 479 | void FileManager::invalidateCache(const FileEntry *Entry) { |
| 480 | assert(Entry && "Cannot invalidate a NULL FileEntry"); |
Axel Naumann | 38179d9 | 2012-06-27 09:17:42 +0000 | [diff] [blame] | 481 | |
| 482 | SeenFileEntries.erase(Entry->getName()); |
Axel Naumann | b307400 | 2012-07-10 16:50:27 +0000 | [diff] [blame] | 483 | |
| 484 | // FileEntry invalidation should not block future optimizations in the file |
| 485 | // caches. Possible alternatives are cache truncation (invalidate last N) or |
| 486 | // invalidation of the whole cache. |
Ben Langmuir | c9b7234 | 2014-02-27 22:21:32 +0000 | [diff] [blame] | 487 | UniqueRealFiles.erase(Entry->getUniqueID()); |
Axel Naumann | 38179d9 | 2012-06-27 09:17:42 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 491 | void FileManager::GetUniqueIDMapping( |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 492 | SmallVectorImpl<const FileEntry *> &UIDToFiles) const { |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 493 | UIDToFiles.clear(); |
| 494 | UIDToFiles.resize(NextFileUID); |
| 495 | |
| 496 | // Map file entries |
| 497 | for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 498 | FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end(); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 499 | FE != FEEnd; ++FE) |
| 500 | if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE) |
| 501 | UIDToFiles[FE->getValue()->getUID()] = FE->getValue(); |
| 502 | |
| 503 | // Map virtual file entries |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 504 | for (SmallVectorImpl<FileEntry *>::const_iterator |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 505 | VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end(); |
| 506 | VFE != VFEEnd; ++VFE) |
| 507 | if (*VFE && *VFE != NON_EXISTENT_FILE) |
| 508 | UIDToFiles[(*VFE)->getUID()] = *VFE; |
| 509 | } |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 510 | |
Argyrios Kyrtzidis | 6eec06d | 2012-05-03 21:50:39 +0000 | [diff] [blame] | 511 | void FileManager::modifyFileEntry(FileEntry *File, |
| 512 | off_t Size, time_t ModificationTime) { |
| 513 | File->Size = Size; |
| 514 | File->ModTime = ModificationTime; |
| 515 | } |
| 516 | |
Sean Silva | b963ded | 2015-07-30 00:26:34 +0000 | [diff] [blame] | 517 | /// Remove '.' and '..' path components from the given absolute path. |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 518 | /// \return \c true if any changes were made. |
| 519 | // FIXME: Move this to llvm::sys::path. |
Sean Silva | 3e36e50 | 2015-07-30 00:52:32 +0000 | [diff] [blame] | 520 | bool FileManager::removeDotPaths(SmallVectorImpl<char> &Path, bool RemoveDotDot) { |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 521 | using namespace llvm::sys; |
| 522 | |
| 523 | SmallVector<StringRef, 16> ComponentStack; |
| 524 | StringRef P(Path.data(), Path.size()); |
| 525 | |
| 526 | // Skip the root path, then look for traversal in the components. |
| 527 | StringRef Rel = path::relative_path(P); |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 528 | for (StringRef C : llvm::make_range(path::begin(Rel), path::end(Rel))) { |
Sean Silva | b963ded | 2015-07-30 00:26:34 +0000 | [diff] [blame] | 529 | if (C == ".") |
| 530 | continue; |
Sean Silva | 3e36e50 | 2015-07-30 00:52:32 +0000 | [diff] [blame] | 531 | if (RemoveDotDot) { |
| 532 | if (C == "..") { |
| 533 | if (!ComponentStack.empty()) |
| 534 | ComponentStack.pop_back(); |
| 535 | continue; |
| 536 | } |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 537 | } |
| 538 | ComponentStack.push_back(C); |
| 539 | } |
| 540 | |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 541 | SmallString<256> Buffer = path::root_path(P); |
| 542 | for (StringRef C : ComponentStack) |
| 543 | path::append(Buffer, C); |
| 544 | |
Sean Silva | b963ded | 2015-07-30 00:26:34 +0000 | [diff] [blame] | 545 | bool Changed = (Path != Buffer); |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 546 | Path.swap(Buffer); |
Sean Silva | b963ded | 2015-07-30 00:26:34 +0000 | [diff] [blame] | 547 | return Changed; |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Douglas Gregor | e00c8b2 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 550 | StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) { |
| 551 | // FIXME: use llvm::sys::fs::canonical() when it gets implemented |
Douglas Gregor | e00c8b2 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 552 | llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known |
| 553 | = CanonicalDirNames.find(Dir); |
| 554 | if (Known != CanonicalDirNames.end()) |
| 555 | return Known->second; |
| 556 | |
| 557 | StringRef CanonicalName(Dir->getName()); |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 558 | |
| 559 | #ifdef LLVM_ON_UNIX |
Douglas Gregor | e00c8b2 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 560 | char CanonicalNameBuf[PATH_MAX]; |
| 561 | if (realpath(Dir->getName(), CanonicalNameBuf)) { |
| 562 | unsigned Len = strlen(CanonicalNameBuf); |
| 563 | char *Mem = static_cast<char *>(CanonicalNameStorage.Allocate(Len, 1)); |
| 564 | memcpy(Mem, CanonicalNameBuf, Len); |
| 565 | CanonicalName = StringRef(Mem, Len); |
| 566 | } |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 567 | #else |
| 568 | SmallString<256> CanonicalNameBuf(CanonicalName); |
| 569 | llvm::sys::fs::make_absolute(CanonicalNameBuf); |
| 570 | llvm::sys::path::native(CanonicalNameBuf); |
Sean Silva | de38166 | 2015-07-30 07:30:24 +0000 | [diff] [blame^] | 571 | // We've run into needing to remove '..' here in the wild though, so |
| 572 | // remove it. |
| 573 | // On Windows, symlinks are significantly less prevalent, so removing |
| 574 | // '..' is pretty safe. |
| 575 | // Ideally we'd have an equivalent of `realpath` and could implement |
| 576 | // sys::fs::canonical across all the platforms. |
| 577 | removeDotPaths(CanonicalNameBuf, /*RemoveDotDot*/true); |
Sean Silva | b963ded | 2015-07-30 00:26:34 +0000 | [diff] [blame] | 578 | char *Mem = CanonicalNameStorage.Allocate<char>(CanonicalNameBuf.size()); |
| 579 | memcpy(Mem, CanonicalNameBuf.data(), CanonicalNameBuf.size()); |
| 580 | CanonicalName = StringRef(Mem, CanonicalNameBuf.size()); |
Richard Smith | 54cc3c2 | 2014-12-11 20:50:24 +0000 | [diff] [blame] | 581 | #endif |
Douglas Gregor | e00c8b2 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 582 | |
| 583 | CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName)); |
| 584 | return CanonicalName; |
Douglas Gregor | e00c8b2 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 585 | } |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 587 | void FileManager::PrintStats() const { |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 588 | llvm::errs() << "\n*** File Manager Stats:\n"; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 589 | llvm::errs() << UniqueRealFiles.size() << " real files found, " |
| 590 | << UniqueRealDirs.size() << " real dirs found.\n"; |
| 591 | llvm::errs() << VirtualFileEntries.size() << " virtual files found, " |
| 592 | << VirtualDirectoryEntries.size() << " virtual dirs found.\n"; |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 593 | llvm::errs() << NumDirLookups << " dir lookups, " |
| 594 | << NumDirCacheMisses << " dir cache misses.\n"; |
| 595 | llvm::errs() << NumFileLookups << " file lookups, " |
| 596 | << NumFileCacheMisses << " file cache misses.\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 598 | //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 599 | } |
Adrian Prantl | 075bf56 | 2015-07-09 02:53:05 +0000 | [diff] [blame] | 600 | |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 601 | // Virtual destructors for abstract base classes that need live in Basic. |
| 602 | PCHContainerWriter::~PCHContainerWriter() {} |
| 603 | PCHContainerReader::~PCHContainerReader() {} |