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