FileManager.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. //===--- FileManager.cpp - File System Probing and Caching ----------------===//
  2. //
  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
  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. #include "clang/Basic/FileManager.h"
  19. #include "clang/Basic/FileSystemStatCache.h"
  20. #include "llvm/ADT/STLExtras.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "llvm/ADT/Statistic.h"
  23. #include "llvm/Config/llvm-config.h"
  24. #include "llvm/Support/FileSystem.h"
  25. #include "llvm/Support/MemoryBuffer.h"
  26. #include "llvm/Support/Path.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include <algorithm>
  29. #include <cassert>
  30. #include <climits>
  31. #include <cstdint>
  32. #include <cstdlib>
  33. #include <string>
  34. #include <utility>
  35. using namespace clang;
  36. #define DEBUG_TYPE "file-search"
  37. ALWAYS_ENABLED_STATISTIC(NumDirLookups, "Number of directory lookups.");
  38. ALWAYS_ENABLED_STATISTIC(NumFileLookups, "Number of file lookups.");
  39. ALWAYS_ENABLED_STATISTIC(NumDirCacheMisses,
  40. "Number of directory cache misses.");
  41. ALWAYS_ENABLED_STATISTIC(NumFileCacheMisses, "Number of file cache misses.");
  42. //===----------------------------------------------------------------------===//
  43. // Common logic.
  44. //===----------------------------------------------------------------------===//
  45. FileManager::FileManager(const FileSystemOptions &FSO,
  46. IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
  47. : FS(std::move(FS)), FileSystemOpts(FSO), SeenDirEntries(64),
  48. SeenFileEntries(64), NextFileUID(0) {
  49. // If the caller doesn't provide a virtual file system, just grab the real
  50. // file system.
  51. if (!this->FS)
  52. this->FS = llvm::vfs::getRealFileSystem();
  53. }
  54. FileManager::~FileManager() = default;
  55. void FileManager::setStatCache(std::unique_ptr<FileSystemStatCache> statCache) {
  56. assert(statCache && "No stat cache provided?");
  57. StatCache = std::move(statCache);
  58. }
  59. void FileManager::clearStatCache() { StatCache.reset(); }
  60. /// Retrieve the directory that the given file name resides in.
  61. /// Filename can point to either a real file or a virtual file.
  62. static llvm::ErrorOr<const DirectoryEntry *>
  63. getDirectoryFromFile(FileManager &FileMgr, StringRef Filename,
  64. bool CacheFailure) {
  65. if (Filename.empty())
  66. return std::errc::no_such_file_or_directory;
  67. if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
  68. return std::errc::is_a_directory;
  69. StringRef DirName = llvm::sys::path::parent_path(Filename);
  70. // Use the current directory if file has no path component.
  71. if (DirName.empty())
  72. DirName = ".";
  73. return FileMgr.getDirectory(DirName, CacheFailure);
  74. }
  75. /// Add all ancestors of the given path (pointing to either a file or
  76. /// a directory) as virtual directories.
  77. void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
  78. StringRef DirName = llvm::sys::path::parent_path(Path);
  79. if (DirName.empty())
  80. DirName = ".";
  81. auto &NamedDirEnt = *SeenDirEntries.insert(
  82. {DirName, std::errc::no_such_file_or_directory}).first;
  83. // When caching a virtual directory, we always cache its ancestors
  84. // at the same time. Therefore, if DirName is already in the cache,
  85. // we don't need to recurse as its ancestors must also already be in
  86. // the cache (or it's a known non-virtual directory).
  87. if (NamedDirEnt.second)
  88. return;
  89. // Add the virtual directory to the cache.
  90. auto UDE = std::make_unique<DirectoryEntry>();
  91. UDE->Name = NamedDirEnt.first();
  92. NamedDirEnt.second = *UDE.get();
  93. VirtualDirectoryEntries.push_back(std::move(UDE));
  94. // Recursively add the other ancestors.
  95. addAncestorsAsVirtualDirs(DirName);
  96. }
  97. llvm::Expected<DirectoryEntryRef>
  98. FileManager::getDirectoryRef(StringRef DirName, bool CacheFailure) {
  99. // stat doesn't like trailing separators except for root directory.
  100. // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
  101. // (though it can strip '\\')
  102. if (DirName.size() > 1 &&
  103. DirName != llvm::sys::path::root_path(DirName) &&
  104. llvm::sys::path::is_separator(DirName.back()))
  105. DirName = DirName.substr(0, DirName.size()-1);
  106. #ifdef _WIN32
  107. // Fixing a problem with "clang C:test.c" on Windows.
  108. // Stat("C:") does not recognize "C:" as a valid directory
  109. std::string DirNameStr;
  110. if (DirName.size() > 1 && DirName.back() == ':' &&
  111. DirName.equals_lower(llvm::sys::path::root_name(DirName))) {
  112. DirNameStr = DirName.str() + '.';
  113. DirName = DirNameStr;
  114. }
  115. #endif
  116. ++NumDirLookups;
  117. // See if there was already an entry in the map. Note that the map
  118. // contains both virtual and real directories.
  119. auto SeenDirInsertResult =
  120. SeenDirEntries.insert({DirName, std::errc::no_such_file_or_directory});
  121. if (!SeenDirInsertResult.second) {
  122. if (SeenDirInsertResult.first->second)
  123. return DirectoryEntryRef(&*SeenDirInsertResult.first);
  124. return llvm::errorCodeToError(SeenDirInsertResult.first->second.getError());
  125. }
  126. // We've not seen this before. Fill it in.
  127. ++NumDirCacheMisses;
  128. auto &NamedDirEnt = *SeenDirInsertResult.first;
  129. assert(!NamedDirEnt.second && "should be newly-created");
  130. // Get the null-terminated directory name as stored as the key of the
  131. // SeenDirEntries map.
  132. StringRef InterndDirName = NamedDirEnt.first();
  133. // Check to see if the directory exists.
  134. llvm::vfs::Status Status;
  135. auto statError = getStatValue(InterndDirName, Status, false,
  136. nullptr /*directory lookup*/);
  137. if (statError) {
  138. // There's no real directory at the given path.
  139. if (CacheFailure)
  140. NamedDirEnt.second = statError;
  141. else
  142. SeenDirEntries.erase(DirName);
  143. return llvm::errorCodeToError(statError);
  144. }
  145. // It exists. See if we have already opened a directory with the
  146. // same inode (this occurs on Unix-like systems when one dir is
  147. // symlinked to another, for example) or the same path (on
  148. // Windows).
  149. DirectoryEntry &UDE = UniqueRealDirs[Status.getUniqueID()];
  150. NamedDirEnt.second = UDE;
  151. if (UDE.getName().empty()) {
  152. // We don't have this directory yet, add it. We use the string
  153. // key from the SeenDirEntries map as the string.
  154. UDE.Name = InterndDirName;
  155. }
  156. return DirectoryEntryRef(&NamedDirEnt);
  157. }
  158. llvm::ErrorOr<const DirectoryEntry *>
  159. FileManager::getDirectory(StringRef DirName, bool CacheFailure) {
  160. auto Result = getDirectoryRef(DirName, CacheFailure);
  161. if (Result)
  162. return &Result->getDirEntry();
  163. return llvm::errorToErrorCode(Result.takeError());
  164. }
  165. llvm::ErrorOr<const FileEntry *>
  166. FileManager::getFile(StringRef Filename, bool openFile, bool CacheFailure) {
  167. auto Result = getFileRef(Filename, openFile, CacheFailure);
  168. if (Result)
  169. return &Result->getFileEntry();
  170. return llvm::errorToErrorCode(Result.takeError());
  171. }
  172. llvm::Expected<FileEntryRef>
  173. FileManager::getFileRef(StringRef Filename, bool openFile, bool CacheFailure) {
  174. ++NumFileLookups;
  175. // See if there is already an entry in the map.
  176. auto SeenFileInsertResult =
  177. SeenFileEntries.insert({Filename, std::errc::no_such_file_or_directory});
  178. if (!SeenFileInsertResult.second) {
  179. if (!SeenFileInsertResult.first->second)
  180. return llvm::errorCodeToError(
  181. SeenFileInsertResult.first->second.getError());
  182. // Construct and return and FileEntryRef, unless it's a redirect to another
  183. // filename.
  184. SeenFileEntryOrRedirect Value = *SeenFileInsertResult.first->second;
  185. FileEntry *FE;
  186. if (LLVM_LIKELY(FE = Value.dyn_cast<FileEntry *>()))
  187. return FileEntryRef(SeenFileInsertResult.first->first(), *FE);
  188. return getFileRef(*Value.get<const StringRef *>(), openFile, CacheFailure);
  189. }
  190. // We've not seen this before. Fill it in.
  191. ++NumFileCacheMisses;
  192. auto &NamedFileEnt = *SeenFileInsertResult.first;
  193. assert(!NamedFileEnt.second && "should be newly-created");
  194. // Get the null-terminated file name as stored as the key of the
  195. // SeenFileEntries map.
  196. StringRef InterndFileName = NamedFileEnt.first();
  197. // Look up the directory for the file. When looking up something like
  198. // sys/foo.h we'll discover all of the search directories that have a 'sys'
  199. // subdirectory. This will let us avoid having to waste time on known-to-fail
  200. // searches when we go to find sys/bar.h, because all the search directories
  201. // without a 'sys' subdir will get a cached failure result.
  202. auto DirInfoOrErr = getDirectoryFromFile(*this, Filename, CacheFailure);
  203. if (!DirInfoOrErr) { // Directory doesn't exist, file can't exist.
  204. if (CacheFailure)
  205. NamedFileEnt.second = DirInfoOrErr.getError();
  206. else
  207. SeenFileEntries.erase(Filename);
  208. return llvm::errorCodeToError(DirInfoOrErr.getError());
  209. }
  210. const DirectoryEntry *DirInfo = *DirInfoOrErr;
  211. // FIXME: Use the directory info to prune this, before doing the stat syscall.
  212. // FIXME: This will reduce the # syscalls.
  213. // Check to see if the file exists.
  214. std::unique_ptr<llvm::vfs::File> F;
  215. llvm::vfs::Status Status;
  216. auto statError = getStatValue(InterndFileName, Status, true,
  217. openFile ? &F : nullptr);
  218. if (statError) {
  219. // There's no real file at the given path.
  220. if (CacheFailure)
  221. NamedFileEnt.second = statError;
  222. else
  223. SeenFileEntries.erase(Filename);
  224. return llvm::errorCodeToError(statError);
  225. }
  226. assert((openFile || !F) && "undesired open file");
  227. // It exists. See if we have already opened a file with the same inode.
  228. // This occurs when one dir is symlinked to another, for example.
  229. FileEntry &UFE = UniqueRealFiles[Status.getUniqueID()];
  230. NamedFileEnt.second = &UFE;
  231. // If the name returned by getStatValue is different than Filename, re-intern
  232. // the name.
  233. if (Status.getName() != Filename) {
  234. auto &NewNamedFileEnt =
  235. *SeenFileEntries.insert({Status.getName(), &UFE}).first;
  236. assert((*NewNamedFileEnt.second).get<FileEntry *>() == &UFE &&
  237. "filename from getStatValue() refers to wrong file");
  238. InterndFileName = NewNamedFileEnt.first().data();
  239. // In addition to re-interning the name, construct a redirecting seen file
  240. // entry, that will point to the name the filesystem actually wants to use.
  241. StringRef *Redirect = new (CanonicalNameStorage) StringRef(InterndFileName);
  242. NamedFileEnt.second = Redirect;
  243. }
  244. if (UFE.isValid()) { // Already have an entry with this inode, return it.
  245. // FIXME: this hack ensures that if we look up a file by a virtual path in
  246. // the VFS that the getDir() will have the virtual path, even if we found
  247. // the file by a 'real' path first. This is required in order to find a
  248. // module's structure when its headers/module map are mapped in the VFS.
  249. // We should remove this as soon as we can properly support a file having
  250. // multiple names.
  251. if (DirInfo != UFE.Dir && Status.IsVFSMapped)
  252. UFE.Dir = DirInfo;
  253. // Always update the name to use the last name by which a file was accessed.
  254. // FIXME: Neither this nor always using the first name is correct; we want
  255. // to switch towards a design where we return a FileName object that
  256. // encapsulates both the name by which the file was accessed and the
  257. // corresponding FileEntry.
  258. // FIXME: The Name should be removed from FileEntry once all clients
  259. // adopt FileEntryRef.
  260. UFE.Name = InterndFileName;
  261. return FileEntryRef(InterndFileName, UFE);
  262. }
  263. // Otherwise, we don't have this file yet, add it.
  264. UFE.Name = InterndFileName;
  265. UFE.Size = Status.getSize();
  266. UFE.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
  267. UFE.Dir = DirInfo;
  268. UFE.UID = NextFileUID++;
  269. UFE.UniqueID = Status.getUniqueID();
  270. UFE.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
  271. UFE.File = std::move(F);
  272. UFE.IsValid = true;
  273. if (UFE.File) {
  274. if (auto PathName = UFE.File->getName())
  275. fillRealPathName(&UFE, *PathName);
  276. } else if (!openFile) {
  277. // We should still fill the path even if we aren't opening the file.
  278. fillRealPathName(&UFE, InterndFileName);
  279. }
  280. return FileEntryRef(InterndFileName, UFE);
  281. }
  282. const FileEntry *
  283. FileManager::getVirtualFile(StringRef Filename, off_t Size,
  284. time_t ModificationTime) {
  285. ++NumFileLookups;
  286. // See if there is already an entry in the map for an existing file.
  287. auto &NamedFileEnt = *SeenFileEntries.insert(
  288. {Filename, std::errc::no_such_file_or_directory}).first;
  289. if (NamedFileEnt.second) {
  290. SeenFileEntryOrRedirect Value = *NamedFileEnt.second;
  291. FileEntry *FE;
  292. if (LLVM_LIKELY(FE = Value.dyn_cast<FileEntry *>()))
  293. return FE;
  294. return getVirtualFile(*Value.get<const StringRef *>(), Size,
  295. ModificationTime);
  296. }
  297. // We've not seen this before, or the file is cached as non-existent.
  298. ++NumFileCacheMisses;
  299. addAncestorsAsVirtualDirs(Filename);
  300. FileEntry *UFE = nullptr;
  301. // Now that all ancestors of Filename are in the cache, the
  302. // following call is guaranteed to find the DirectoryEntry from the
  303. // cache.
  304. auto DirInfo = getDirectoryFromFile(*this, Filename, /*CacheFailure=*/true);
  305. assert(DirInfo &&
  306. "The directory of a virtual file should already be in the cache.");
  307. // Check to see if the file exists. If so, drop the virtual file
  308. llvm::vfs::Status Status;
  309. const char *InterndFileName = NamedFileEnt.first().data();
  310. if (!getStatValue(InterndFileName, Status, true, nullptr)) {
  311. UFE = &UniqueRealFiles[Status.getUniqueID()];
  312. Status = llvm::vfs::Status(
  313. Status.getName(), Status.getUniqueID(),
  314. llvm::sys::toTimePoint(ModificationTime),
  315. Status.getUser(), Status.getGroup(), Size,
  316. Status.getType(), Status.getPermissions());
  317. NamedFileEnt.second = UFE;
  318. // If we had already opened this file, close it now so we don't
  319. // leak the descriptor. We're not going to use the file
  320. // descriptor anyway, since this is a virtual file.
  321. if (UFE->File)
  322. UFE->closeFile();
  323. // If we already have an entry with this inode, return it.
  324. if (UFE->isValid())
  325. return UFE;
  326. UFE->UniqueID = Status.getUniqueID();
  327. UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
  328. fillRealPathName(UFE, Status.getName());
  329. } else {
  330. VirtualFileEntries.push_back(std::make_unique<FileEntry>());
  331. UFE = VirtualFileEntries.back().get();
  332. NamedFileEnt.second = UFE;
  333. }
  334. UFE->Name = InterndFileName;
  335. UFE->Size = Size;
  336. UFE->ModTime = ModificationTime;
  337. UFE->Dir = *DirInfo;
  338. UFE->UID = NextFileUID++;
  339. UFE->IsValid = true;
  340. UFE->File.reset();
  341. return UFE;
  342. }
  343. llvm::Optional<FileEntryRef> FileManager::getBypassFile(FileEntryRef VF) {
  344. // Stat of the file and return nullptr if it doesn't exist.
  345. llvm::vfs::Status Status;
  346. if (getStatValue(VF.getName(), Status, /*isFile=*/true, /*F=*/nullptr))
  347. return None;
  348. // Fill it in from the stat.
  349. BypassFileEntries.push_back(std::make_unique<FileEntry>());
  350. const FileEntry &VFE = VF.getFileEntry();
  351. FileEntry &BFE = *BypassFileEntries.back();
  352. BFE.Name = VFE.getName();
  353. BFE.Size = Status.getSize();
  354. BFE.Dir = VFE.Dir;
  355. BFE.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
  356. BFE.UID = NextFileUID++;
  357. BFE.IsValid = true;
  358. return FileEntryRef(VF.getName(), BFE);
  359. }
  360. bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
  361. StringRef pathRef(path.data(), path.size());
  362. if (FileSystemOpts.WorkingDir.empty()
  363. || llvm::sys::path::is_absolute(pathRef))
  364. return false;
  365. SmallString<128> NewPath(FileSystemOpts.WorkingDir);
  366. llvm::sys::path::append(NewPath, pathRef);
  367. path = NewPath;
  368. return true;
  369. }
  370. bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path) const {
  371. bool Changed = FixupRelativePath(Path);
  372. if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
  373. FS->makeAbsolute(Path);
  374. Changed = true;
  375. }
  376. return Changed;
  377. }
  378. void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) {
  379. llvm::SmallString<128> AbsPath(FileName);
  380. // This is not the same as `VFS::getRealPath()`, which resolves symlinks
  381. // but can be very expensive on real file systems.
  382. // FIXME: the semantic of RealPathName is unclear, and the name might be
  383. // misleading. We need to clean up the interface here.
  384. makeAbsolutePath(AbsPath);
  385. llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true);
  386. UFE->RealPathName = AbsPath.str();
  387. }
  388. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  389. FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile) {
  390. uint64_t FileSize = Entry->getSize();
  391. // If there's a high enough chance that the file have changed since we
  392. // got its size, force a stat before opening it.
  393. if (isVolatile)
  394. FileSize = -1;
  395. StringRef Filename = Entry->getName();
  396. // If the file is already open, use the open file descriptor.
  397. if (Entry->File) {
  398. auto Result =
  399. Entry->File->getBuffer(Filename, FileSize,
  400. /*RequiresNullTerminator=*/true, isVolatile);
  401. Entry->closeFile();
  402. return Result;
  403. }
  404. // Otherwise, open the file.
  405. return getBufferForFileImpl(Filename, FileSize, isVolatile);
  406. }
  407. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  408. FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
  409. bool isVolatile) {
  410. if (FileSystemOpts.WorkingDir.empty())
  411. return FS->getBufferForFile(Filename, FileSize,
  412. /*RequiresNullTerminator=*/true, isVolatile);
  413. SmallString<128> FilePath(Filename);
  414. FixupRelativePath(FilePath);
  415. return FS->getBufferForFile(FilePath, FileSize,
  416. /*RequiresNullTerminator=*/true, isVolatile);
  417. }
  418. /// getStatValue - Get the 'stat' information for the specified path,
  419. /// using the cache to accelerate it if possible. This returns true
  420. /// if the path points to a virtual file or does not exist, or returns
  421. /// false if it's an existent real file. If FileDescriptor is NULL,
  422. /// do directory look-up instead of file look-up.
  423. std::error_code
  424. FileManager::getStatValue(StringRef Path, llvm::vfs::Status &Status,
  425. bool isFile, std::unique_ptr<llvm::vfs::File> *F) {
  426. // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
  427. // absolute!
  428. if (FileSystemOpts.WorkingDir.empty())
  429. return FileSystemStatCache::get(Path, Status, isFile, F,
  430. StatCache.get(), *FS);
  431. SmallString<128> FilePath(Path);
  432. FixupRelativePath(FilePath);
  433. return FileSystemStatCache::get(FilePath.c_str(), Status, isFile, F,
  434. StatCache.get(), *FS);
  435. }
  436. std::error_code
  437. FileManager::getNoncachedStatValue(StringRef Path,
  438. llvm::vfs::Status &Result) {
  439. SmallString<128> FilePath(Path);
  440. FixupRelativePath(FilePath);
  441. llvm::ErrorOr<llvm::vfs::Status> S = FS->status(FilePath.c_str());
  442. if (!S)
  443. return S.getError();
  444. Result = *S;
  445. return std::error_code();
  446. }
  447. void FileManager::GetUniqueIDMapping(
  448. SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
  449. UIDToFiles.clear();
  450. UIDToFiles.resize(NextFileUID);
  451. // Map file entries
  452. for (llvm::StringMap<llvm::ErrorOr<SeenFileEntryOrRedirect>,
  453. llvm::BumpPtrAllocator>::const_iterator
  454. FE = SeenFileEntries.begin(),
  455. FEEnd = SeenFileEntries.end();
  456. FE != FEEnd; ++FE)
  457. if (llvm::ErrorOr<SeenFileEntryOrRedirect> Entry = FE->getValue()) {
  458. if (const auto *FE = (*Entry).dyn_cast<FileEntry *>())
  459. UIDToFiles[FE->getUID()] = FE;
  460. }
  461. // Map virtual file entries
  462. for (const auto &VFE : VirtualFileEntries)
  463. UIDToFiles[VFE->getUID()] = VFE.get();
  464. }
  465. StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
  466. // FIXME: use llvm::sys::fs::canonical() when it gets implemented
  467. llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known
  468. = CanonicalDirNames.find(Dir);
  469. if (Known != CanonicalDirNames.end())
  470. return Known->second;
  471. StringRef CanonicalName(Dir->getName());
  472. SmallString<4096> CanonicalNameBuf;
  473. if (!FS->getRealPath(Dir->getName(), CanonicalNameBuf))
  474. CanonicalName = StringRef(CanonicalNameBuf).copy(CanonicalNameStorage);
  475. CanonicalDirNames.insert({Dir, CanonicalName});
  476. return CanonicalName;
  477. }
  478. void FileManager::PrintStats() const {
  479. llvm::errs() << "\n*** File Manager Stats:\n";
  480. llvm::errs() << UniqueRealFiles.size() << " real files found, "
  481. << UniqueRealDirs.size() << " real dirs found.\n";
  482. llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
  483. << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
  484. llvm::errs() << NumDirLookups << " dir lookups, "
  485. << NumDirCacheMisses << " dir cache misses.\n";
  486. llvm::errs() << NumFileLookups << " file lookups, "
  487. << NumFileCacheMisses << " file cache misses.\n";
  488. //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
  489. }