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