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