FileManager.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. //===--- FileManager.cpp - File System Probing and Caching ----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the FileManager interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // TODO: This should index all interesting directories with dirent calls.
  15. // getdirentries ?
  16. // opendir/readdir_r/closedir ?
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include "clang/Basic/FileManager.h"
  20. #include "clang/Basic/FileSystemStatCache.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "llvm/Config/llvm-config.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 "llvm/Support/system_error.h"
  28. #include <map>
  29. #include <set>
  30. #include <string>
  31. // FIXME: This is terrible, we need this for ::close.
  32. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  33. #include <unistd.h>
  34. #include <sys/uio.h>
  35. #else
  36. #include <io.h>
  37. #ifndef S_ISFIFO
  38. #define S_ISFIFO(x) (0)
  39. #endif
  40. #endif
  41. #if defined(LLVM_ON_UNIX)
  42. #include <limits.h>
  43. #endif
  44. using namespace clang;
  45. // FIXME: Enhance libsystem to support inode and other fields.
  46. #include <sys/stat.h>
  47. /// NON_EXISTENT_DIR - A special value distinct from null that is used to
  48. /// represent a dir name that doesn't exist on the disk.
  49. #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
  50. /// NON_EXISTENT_FILE - A special value distinct from null that is used to
  51. /// represent a filename that doesn't exist on the disk.
  52. #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
  53. FileEntry::~FileEntry() {
  54. // If this FileEntry owns an open file descriptor that never got used, close
  55. // it.
  56. if (FD != -1) ::close(FD);
  57. }
  58. bool FileEntry::isNamedPipe() const {
  59. return S_ISFIFO(FileMode);
  60. }
  61. //===----------------------------------------------------------------------===//
  62. // Windows.
  63. //===----------------------------------------------------------------------===//
  64. #ifdef LLVM_ON_WIN32
  65. namespace {
  66. static std::string GetFullPath(const char *relPath) {
  67. char *absPathStrPtr = _fullpath(NULL, relPath, 0);
  68. assert(absPathStrPtr && "_fullpath() returned NULL!");
  69. std::string absPath(absPathStrPtr);
  70. free(absPathStrPtr);
  71. return absPath;
  72. }
  73. }
  74. class FileManager::UniqueDirContainer {
  75. /// UniqueDirs - Cache from full path to existing directories/files.
  76. ///
  77. llvm::StringMap<DirectoryEntry> UniqueDirs;
  78. public:
  79. /// getDirectory - Return an existing DirectoryEntry with the given
  80. /// name if there is already one; otherwise create and return a
  81. /// default-constructed DirectoryEntry.
  82. DirectoryEntry &getDirectory(const char *Name,
  83. const struct stat & /*StatBuf*/) {
  84. std::string FullPath(GetFullPath(Name));
  85. return UniqueDirs.GetOrCreateValue(FullPath).getValue();
  86. }
  87. size_t size() const { return UniqueDirs.size(); }
  88. };
  89. class FileManager::UniqueFileContainer {
  90. /// UniqueFiles - Cache from full path to existing directories/files.
  91. ///
  92. llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
  93. public:
  94. /// getFile - Return an existing FileEntry with the given name if
  95. /// there is already one; otherwise create and return a
  96. /// default-constructed FileEntry.
  97. FileEntry &getFile(const char *Name, const struct stat & /*StatBuf*/) {
  98. std::string FullPath(GetFullPath(Name));
  99. // Lowercase string because Windows filesystem is case insensitive.
  100. FullPath = StringRef(FullPath).lower();
  101. return UniqueFiles.GetOrCreateValue(FullPath).getValue();
  102. }
  103. size_t size() const { return UniqueFiles.size(); }
  104. void erase(const FileEntry *Entry) {
  105. std::string FullPath(GetFullPath(Entry->getName()));
  106. // Lowercase string because Windows filesystem is case insensitive.
  107. FullPath = StringRef(FullPath).lower();
  108. UniqueFiles.erase(FullPath);
  109. }
  110. };
  111. //===----------------------------------------------------------------------===//
  112. // Unix-like Systems.
  113. //===----------------------------------------------------------------------===//
  114. #else
  115. class FileManager::UniqueDirContainer {
  116. /// UniqueDirs - Cache from ID's to existing directories/files.
  117. std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
  118. public:
  119. /// getDirectory - Return an existing DirectoryEntry with the given
  120. /// ID's if there is already one; otherwise create and return a
  121. /// default-constructed DirectoryEntry.
  122. DirectoryEntry &getDirectory(const char * /*Name*/,
  123. const struct stat &StatBuf) {
  124. return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
  125. }
  126. size_t size() const { return UniqueDirs.size(); }
  127. };
  128. class FileManager::UniqueFileContainer {
  129. /// UniqueFiles - Cache from ID's to existing directories/files.
  130. std::set<FileEntry> UniqueFiles;
  131. public:
  132. /// getFile - Return an existing FileEntry with the given ID's if
  133. /// there is already one; otherwise create and return a
  134. /// default-constructed FileEntry.
  135. FileEntry &getFile(const char * /*Name*/, const struct stat &StatBuf) {
  136. return
  137. const_cast<FileEntry&>(
  138. *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
  139. StatBuf.st_ino,
  140. StatBuf.st_mode)).first);
  141. }
  142. size_t size() const { return UniqueFiles.size(); }
  143. void erase(const FileEntry *Entry) { UniqueFiles.erase(*Entry); }
  144. };
  145. #endif
  146. //===----------------------------------------------------------------------===//
  147. // Common logic.
  148. //===----------------------------------------------------------------------===//
  149. FileManager::FileManager(const FileSystemOptions &FSO)
  150. : FileSystemOpts(FSO),
  151. UniqueRealDirs(*new UniqueDirContainer()),
  152. UniqueRealFiles(*new UniqueFileContainer()),
  153. SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
  154. NumDirLookups = NumFileLookups = 0;
  155. NumDirCacheMisses = NumFileCacheMisses = 0;
  156. }
  157. FileManager::~FileManager() {
  158. delete &UniqueRealDirs;
  159. delete &UniqueRealFiles;
  160. for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
  161. delete VirtualFileEntries[i];
  162. for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i)
  163. delete VirtualDirectoryEntries[i];
  164. }
  165. void FileManager::addStatCache(FileSystemStatCache *statCache,
  166. bool AtBeginning) {
  167. assert(statCache && "No stat cache provided?");
  168. if (AtBeginning || StatCache.get() == 0) {
  169. statCache->setNextStatCache(StatCache.take());
  170. StatCache.reset(statCache);
  171. return;
  172. }
  173. FileSystemStatCache *LastCache = StatCache.get();
  174. while (LastCache->getNextStatCache())
  175. LastCache = LastCache->getNextStatCache();
  176. LastCache->setNextStatCache(statCache);
  177. }
  178. void FileManager::removeStatCache(FileSystemStatCache *statCache) {
  179. if (!statCache)
  180. return;
  181. if (StatCache.get() == statCache) {
  182. // This is the first stat cache.
  183. StatCache.reset(StatCache->takeNextStatCache());
  184. return;
  185. }
  186. // Find the stat cache in the list.
  187. FileSystemStatCache *PrevCache = StatCache.get();
  188. while (PrevCache && PrevCache->getNextStatCache() != statCache)
  189. PrevCache = PrevCache->getNextStatCache();
  190. assert(PrevCache && "Stat cache not found for removal");
  191. PrevCache->setNextStatCache(statCache->getNextStatCache());
  192. }
  193. void FileManager::clearStatCaches() {
  194. StatCache.reset(0);
  195. }
  196. /// \brief Retrieve the directory that the given file name resides in.
  197. /// Filename can point to either a real file or a virtual file.
  198. static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
  199. StringRef Filename,
  200. bool CacheFailure) {
  201. if (Filename.empty())
  202. return NULL;
  203. if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
  204. return NULL; // If Filename is a directory.
  205. StringRef DirName = llvm::sys::path::parent_path(Filename);
  206. // Use the current directory if file has no path component.
  207. if (DirName.empty())
  208. DirName = ".";
  209. return FileMgr.getDirectory(DirName, CacheFailure);
  210. }
  211. /// Add all ancestors of the given path (pointing to either a file or
  212. /// a directory) as virtual directories.
  213. void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
  214. StringRef DirName = llvm::sys::path::parent_path(Path);
  215. if (DirName.empty())
  216. return;
  217. llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
  218. SeenDirEntries.GetOrCreateValue(DirName);
  219. // When caching a virtual directory, we always cache its ancestors
  220. // at the same time. Therefore, if DirName is already in the cache,
  221. // we don't need to recurse as its ancestors must also already be in
  222. // the cache.
  223. if (NamedDirEnt.getValue())
  224. return;
  225. // Add the virtual directory to the cache.
  226. DirectoryEntry *UDE = new DirectoryEntry;
  227. UDE->Name = NamedDirEnt.getKeyData();
  228. NamedDirEnt.setValue(UDE);
  229. VirtualDirectoryEntries.push_back(UDE);
  230. // Recursively add the other ancestors.
  231. addAncestorsAsVirtualDirs(DirName);
  232. }
  233. const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
  234. bool CacheFailure) {
  235. // stat doesn't like trailing separators except for root directory.
  236. // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
  237. // (though it can strip '\\')
  238. if (DirName.size() > 1 &&
  239. DirName != llvm::sys::path::root_path(DirName) &&
  240. llvm::sys::path::is_separator(DirName.back()))
  241. DirName = DirName.substr(0, DirName.size()-1);
  242. ++NumDirLookups;
  243. llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
  244. SeenDirEntries.GetOrCreateValue(DirName);
  245. // See if there was already an entry in the map. Note that the map
  246. // contains both virtual and real directories.
  247. if (NamedDirEnt.getValue())
  248. return NamedDirEnt.getValue() == NON_EXISTENT_DIR
  249. ? 0 : NamedDirEnt.getValue();
  250. ++NumDirCacheMisses;
  251. // By default, initialize it to invalid.
  252. NamedDirEnt.setValue(NON_EXISTENT_DIR);
  253. // Get the null-terminated directory name as stored as the key of the
  254. // SeenDirEntries map.
  255. const char *InterndDirName = NamedDirEnt.getKeyData();
  256. // Check to see if the directory exists.
  257. struct stat StatBuf;
  258. if (getStatValue(InterndDirName, StatBuf, false, 0/*directory lookup*/)) {
  259. // There's no real directory at the given path.
  260. if (!CacheFailure)
  261. SeenDirEntries.erase(DirName);
  262. return 0;
  263. }
  264. // It exists. See if we have already opened a directory with the
  265. // same inode (this occurs on Unix-like systems when one dir is
  266. // symlinked to another, for example) or the same path (on
  267. // Windows).
  268. DirectoryEntry &UDE = UniqueRealDirs.getDirectory(InterndDirName, StatBuf);
  269. NamedDirEnt.setValue(&UDE);
  270. if (!UDE.getName()) {
  271. // We don't have this directory yet, add it. We use the string
  272. // key from the SeenDirEntries map as the string.
  273. UDE.Name = InterndDirName;
  274. }
  275. return &UDE;
  276. }
  277. const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
  278. bool CacheFailure) {
  279. ++NumFileLookups;
  280. // See if there is already an entry in the map.
  281. llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
  282. SeenFileEntries.GetOrCreateValue(Filename);
  283. // See if there is already an entry in the map.
  284. if (NamedFileEnt.getValue())
  285. return NamedFileEnt.getValue() == NON_EXISTENT_FILE
  286. ? 0 : NamedFileEnt.getValue();
  287. ++NumFileCacheMisses;
  288. // By default, initialize it to invalid.
  289. NamedFileEnt.setValue(NON_EXISTENT_FILE);
  290. // Get the null-terminated file name as stored as the key of the
  291. // SeenFileEntries map.
  292. const char *InterndFileName = NamedFileEnt.getKeyData();
  293. // Look up the directory for the file. When looking up something like
  294. // sys/foo.h we'll discover all of the search directories that have a 'sys'
  295. // subdirectory. This will let us avoid having to waste time on known-to-fail
  296. // searches when we go to find sys/bar.h, because all the search directories
  297. // without a 'sys' subdir will get a cached failure result.
  298. const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
  299. CacheFailure);
  300. if (DirInfo == 0) { // Directory doesn't exist, file can't exist.
  301. if (!CacheFailure)
  302. SeenFileEntries.erase(Filename);
  303. return 0;
  304. }
  305. // FIXME: Use the directory info to prune this, before doing the stat syscall.
  306. // FIXME: This will reduce the # syscalls.
  307. // Nope, there isn't. Check to see if the file exists.
  308. int FileDescriptor = -1;
  309. struct stat StatBuf;
  310. if (getStatValue(InterndFileName, StatBuf, true,
  311. openFile ? &FileDescriptor : 0)) {
  312. // There's no real file at the given path.
  313. if (!CacheFailure)
  314. SeenFileEntries.erase(Filename);
  315. return 0;
  316. }
  317. if (FileDescriptor != -1 && !openFile) {
  318. close(FileDescriptor);
  319. FileDescriptor = -1;
  320. }
  321. // It exists. See if we have already opened a file with the same inode.
  322. // This occurs when one dir is symlinked to another, for example.
  323. FileEntry &UFE = UniqueRealFiles.getFile(InterndFileName, StatBuf);
  324. NamedFileEnt.setValue(&UFE);
  325. if (UFE.getName()) { // Already have an entry with this inode, return it.
  326. // If the stat process opened the file, close it to avoid a FD leak.
  327. if (FileDescriptor != -1)
  328. close(FileDescriptor);
  329. return &UFE;
  330. }
  331. // Otherwise, we don't have this directory yet, add it.
  332. // FIXME: Change the name to be a char* that points back to the
  333. // 'SeenFileEntries' key.
  334. UFE.Name = InterndFileName;
  335. UFE.Size = StatBuf.st_size;
  336. UFE.ModTime = StatBuf.st_mtime;
  337. UFE.Dir = DirInfo;
  338. UFE.UID = NextFileUID++;
  339. UFE.FD = FileDescriptor;
  340. return &UFE;
  341. }
  342. const FileEntry *
  343. FileManager::getVirtualFile(StringRef Filename, off_t Size,
  344. time_t ModificationTime) {
  345. ++NumFileLookups;
  346. // See if there is already an entry in the map.
  347. llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
  348. SeenFileEntries.GetOrCreateValue(Filename);
  349. // See if there is already an entry in the map.
  350. if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE)
  351. return NamedFileEnt.getValue();
  352. ++NumFileCacheMisses;
  353. // By default, initialize it to invalid.
  354. NamedFileEnt.setValue(NON_EXISTENT_FILE);
  355. addAncestorsAsVirtualDirs(Filename);
  356. FileEntry *UFE = 0;
  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.
  360. const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename,
  361. /*CacheFailure=*/true);
  362. assert(DirInfo &&
  363. "The directory of a virtual file should already be in the cache.");
  364. // Check to see if the file exists. If so, drop the virtual file
  365. struct stat StatBuf;
  366. const char *InterndFileName = NamedFileEnt.getKeyData();
  367. if (getStatValue(InterndFileName, StatBuf, true, 0) == 0) {
  368. StatBuf.st_size = Size;
  369. StatBuf.st_mtime = ModificationTime;
  370. UFE = &UniqueRealFiles.getFile(InterndFileName, StatBuf);
  371. NamedFileEnt.setValue(UFE);
  372. // If we had already opened this file, close it now so we don't
  373. // leak the descriptor. We're not going to use the file
  374. // descriptor anyway, since this is a virtual file.
  375. if (UFE->FD != -1) {
  376. close(UFE->FD);
  377. UFE->FD = -1;
  378. }
  379. // If we already have an entry with this inode, return it.
  380. if (UFE->getName())
  381. return UFE;
  382. }
  383. if (!UFE) {
  384. UFE = new FileEntry();
  385. VirtualFileEntries.push_back(UFE);
  386. NamedFileEnt.setValue(UFE);
  387. }
  388. UFE->Name = InterndFileName;
  389. UFE->Size = Size;
  390. UFE->ModTime = ModificationTime;
  391. UFE->Dir = DirInfo;
  392. UFE->UID = NextFileUID++;
  393. UFE->FD = -1;
  394. return UFE;
  395. }
  396. void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
  397. StringRef pathRef(path.data(), path.size());
  398. if (FileSystemOpts.WorkingDir.empty()
  399. || llvm::sys::path::is_absolute(pathRef))
  400. return;
  401. SmallString<128> NewPath(FileSystemOpts.WorkingDir);
  402. llvm::sys::path::append(NewPath, pathRef);
  403. path = NewPath;
  404. }
  405. llvm::MemoryBuffer *FileManager::
  406. getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
  407. bool isVolatile) {
  408. OwningPtr<llvm::MemoryBuffer> Result;
  409. llvm::error_code ec;
  410. uint64_t FileSize = Entry->getSize();
  411. // If there's a high enough chance that the file have changed since we
  412. // got its size, force a stat before opening it.
  413. if (isVolatile)
  414. FileSize = -1;
  415. const char *Filename = Entry->getName();
  416. // If the file is already open, use the open file descriptor.
  417. if (Entry->FD != -1) {
  418. ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result, FileSize);
  419. if (ErrorStr)
  420. *ErrorStr = ec.message();
  421. close(Entry->FD);
  422. Entry->FD = -1;
  423. return Result.take();
  424. }
  425. // Otherwise, open the file.
  426. if (FileSystemOpts.WorkingDir.empty()) {
  427. ec = llvm::MemoryBuffer::getFile(Filename, Result, FileSize);
  428. if (ec && ErrorStr)
  429. *ErrorStr = ec.message();
  430. return Result.take();
  431. }
  432. SmallString<128> FilePath(Entry->getName());
  433. FixupRelativePath(FilePath);
  434. ec = llvm::MemoryBuffer::getFile(FilePath.str(), Result, FileSize);
  435. if (ec && ErrorStr)
  436. *ErrorStr = ec.message();
  437. return Result.take();
  438. }
  439. llvm::MemoryBuffer *FileManager::
  440. getBufferForFile(StringRef Filename, std::string *ErrorStr) {
  441. OwningPtr<llvm::MemoryBuffer> Result;
  442. llvm::error_code ec;
  443. if (FileSystemOpts.WorkingDir.empty()) {
  444. ec = llvm::MemoryBuffer::getFile(Filename, Result);
  445. if (ec && ErrorStr)
  446. *ErrorStr = ec.message();
  447. return Result.take();
  448. }
  449. SmallString<128> FilePath(Filename);
  450. FixupRelativePath(FilePath);
  451. ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result);
  452. if (ec && ErrorStr)
  453. *ErrorStr = ec.message();
  454. return Result.take();
  455. }
  456. /// getStatValue - Get the 'stat' information for the specified path,
  457. /// using the cache to accelerate it if possible. This returns true
  458. /// if the path points to a virtual file or does not exist, or returns
  459. /// false if it's an existent real file. If FileDescriptor is NULL,
  460. /// do directory look-up instead of file look-up.
  461. bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
  462. bool isFile, int *FileDescriptor) {
  463. // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
  464. // absolute!
  465. if (FileSystemOpts.WorkingDir.empty())
  466. return FileSystemStatCache::get(Path, StatBuf, isFile, FileDescriptor,
  467. StatCache.get());
  468. SmallString<128> FilePath(Path);
  469. FixupRelativePath(FilePath);
  470. return FileSystemStatCache::get(FilePath.c_str(), StatBuf,
  471. isFile, FileDescriptor, StatCache.get());
  472. }
  473. bool FileManager::getNoncachedStatValue(StringRef Path,
  474. struct stat &StatBuf) {
  475. SmallString<128> FilePath(Path);
  476. FixupRelativePath(FilePath);
  477. return ::stat(FilePath.c_str(), &StatBuf) != 0;
  478. }
  479. void FileManager::invalidateCache(const FileEntry *Entry) {
  480. assert(Entry && "Cannot invalidate a NULL FileEntry");
  481. SeenFileEntries.erase(Entry->getName());
  482. // FileEntry invalidation should not block future optimizations in the file
  483. // caches. Possible alternatives are cache truncation (invalidate last N) or
  484. // invalidation of the whole cache.
  485. UniqueRealFiles.erase(Entry);
  486. }
  487. void FileManager::GetUniqueIDMapping(
  488. SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
  489. UIDToFiles.clear();
  490. UIDToFiles.resize(NextFileUID);
  491. // Map file entries
  492. for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
  493. FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
  494. FE != FEEnd; ++FE)
  495. if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
  496. UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
  497. // Map virtual file entries
  498. for (SmallVectorImpl<FileEntry *>::const_iterator
  499. VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end();
  500. VFE != VFEEnd; ++VFE)
  501. if (*VFE && *VFE != NON_EXISTENT_FILE)
  502. UIDToFiles[(*VFE)->getUID()] = *VFE;
  503. }
  504. void FileManager::modifyFileEntry(FileEntry *File,
  505. off_t Size, time_t ModificationTime) {
  506. File->Size = Size;
  507. File->ModTime = ModificationTime;
  508. }
  509. StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
  510. // FIXME: use llvm::sys::fs::canonical() when it gets implemented
  511. #ifdef LLVM_ON_UNIX
  512. llvm::DenseMap<const DirectoryEntry *, llvm::StringRef>::iterator Known
  513. = CanonicalDirNames.find(Dir);
  514. if (Known != CanonicalDirNames.end())
  515. return Known->second;
  516. StringRef CanonicalName(Dir->getName());
  517. char CanonicalNameBuf[PATH_MAX];
  518. if (realpath(Dir->getName(), CanonicalNameBuf)) {
  519. unsigned Len = strlen(CanonicalNameBuf);
  520. char *Mem = static_cast<char *>(CanonicalNameStorage.Allocate(Len, 1));
  521. memcpy(Mem, CanonicalNameBuf, Len);
  522. CanonicalName = StringRef(Mem, Len);
  523. }
  524. CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName));
  525. return CanonicalName;
  526. #else
  527. return StringRef(Dir->getName());
  528. #endif
  529. }
  530. void FileManager::PrintStats() const {
  531. llvm::errs() << "\n*** File Manager Stats:\n";
  532. llvm::errs() << UniqueRealFiles.size() << " real files found, "
  533. << UniqueRealDirs.size() << " real dirs found.\n";
  534. llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
  535. << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
  536. llvm::errs() << NumDirLookups << " dir lookups, "
  537. << NumDirCacheMisses << " dir cache misses.\n";
  538. llvm::errs() << NumFileLookups << " file lookups, "
  539. << NumFileCacheMisses << " file cache misses.\n";
  540. //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
  541. }