FileManager.cpp 21 KB

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