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