FileSystemStatCache.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //===--- FileSystemStatCache.cpp - Caching for 'stat' calls ---------------===//
  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 defines the FileSystemStatCache interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Basic/FileSystemStatCache.h"
  14. #include "llvm/System/Path.h"
  15. using namespace clang;
  16. #if defined(_MSC_VER)
  17. #define S_ISDIR(s) (_S_IFDIR & s)
  18. #endif
  19. /// FileSystemStatCache::get - Get the 'stat' information for the specified
  20. /// path, using the cache to accellerate it if possible. This returns true if
  21. /// the path does not exist or false if it exists.
  22. ///
  23. /// If FileDescriptor is non-null, then this lookup should only return success
  24. /// for files (not directories). If it is null this lookup should only return
  25. /// success for directories (not files). On a successful file lookup, the
  26. /// implementation can optionally fill in FileDescriptor with a valid
  27. /// descriptor and the client guarantees that it will close it.
  28. bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf,
  29. int *FileDescriptor, FileSystemStatCache *Cache) {
  30. LookupResult R;
  31. if (Cache)
  32. R = Cache->getStat(Path, StatBuf, FileDescriptor);
  33. else
  34. R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists;
  35. if (R == CacheMissing) return true;
  36. bool isForDir = FileDescriptor == 0;
  37. return S_ISDIR(StatBuf.st_mode) != isForDir;
  38. }
  39. MemorizeStatCalls::LookupResult
  40. MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf,
  41. int *FileDescriptor) {
  42. LookupResult Result = statChained(Path, StatBuf, FileDescriptor);
  43. // Do not cache failed stats, it is easy to construct common inconsistent
  44. // situations if we do, and they are not important for PCH performance (which
  45. // currently only needs the stats to construct the initial FileManager
  46. // entries).
  47. if (Result == CacheMissing)
  48. return Result;
  49. // Cache file 'stat' results and directories with absolutely paths.
  50. if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::Path(Path).isAbsolute())
  51. StatCalls[Path] = StatBuf;
  52. return Result;
  53. }