FileSystemStatCache.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- FileSystemStatCache.h - Caching for 'stat' calls ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// \file
  15. /// Defines the FileSystemStatCache interface.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H
  19. #define LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H
  20. #include "clang/Basic/LLVM.h"
  21. #include "llvm/ADT/StringMap.h"
  22. #include "llvm/ADT/StringRef.h"
  23. #include "llvm/Support/Allocator.h"
  24. #include "llvm/Support/FileSystem.h"
  25. #include "llvm/Support/VirtualFileSystem.h"
  26. #include <cstdint>
  27. #include <ctime>
  28. #include <memory>
  29. #include <string>
  30. #include <utility>
  31. namespace clang {
  32. /// Abstract interface for introducing a FileManager cache for 'stat'
  33. /// system calls, which is used by precompiled and pretokenized headers to
  34. /// improve performance.
  35. class FileSystemStatCache {
  36. virtual void anchor();
  37. public:
  38. virtual ~FileSystemStatCache() = default;
  39. /// Get the 'stat' information for the specified path, using the cache
  40. /// to accelerate it if possible.
  41. ///
  42. /// \returns \c true if the path does not exist or \c false if it exists.
  43. ///
  44. /// If isFile is true, then this lookup should only return success for files
  45. /// (not directories). If it is false this lookup should only return
  46. /// success for directories (not files). On a successful file lookup, the
  47. /// implementation can optionally fill in \p F with a valid \p File object and
  48. /// the client guarantees that it will close it.
  49. static std::error_code
  50. get(StringRef Path, llvm::vfs::Status &Status, bool isFile,
  51. std::unique_ptr<llvm::vfs::File> *F,
  52. FileSystemStatCache *Cache, llvm::vfs::FileSystem &FS);
  53. protected:
  54. // FIXME: The pointer here is a non-owning/optional reference to the
  55. // unique_ptr. Optional<unique_ptr<vfs::File>&> might be nicer, but
  56. // Optional needs some work to support references so this isn't possible yet.
  57. virtual std::error_code getStat(StringRef Path, llvm::vfs::Status &Status,
  58. bool isFile,
  59. std::unique_ptr<llvm::vfs::File> *F,
  60. llvm::vfs::FileSystem &FS) = 0;
  61. };
  62. /// A stat "cache" that can be used by FileManager to keep
  63. /// track of the results of stat() calls that occur throughout the
  64. /// execution of the front end.
  65. class MemorizeStatCalls : public FileSystemStatCache {
  66. public:
  67. /// The set of stat() calls that have been seen.
  68. llvm::StringMap<llvm::vfs::Status, llvm::BumpPtrAllocator> StatCalls;
  69. using iterator =
  70. llvm::StringMap<llvm::vfs::Status,
  71. llvm::BumpPtrAllocator>::const_iterator;
  72. iterator begin() const { return StatCalls.begin(); }
  73. iterator end() const { return StatCalls.end(); }
  74. std::error_code getStat(StringRef Path, llvm::vfs::Status &Status,
  75. bool isFile,
  76. std::unique_ptr<llvm::vfs::File> *F,
  77. llvm::vfs::FileSystem &FS) override;
  78. };
  79. } // namespace clang
  80. #endif // LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H
  81. #ifdef __GNUC__
  82. #pragma GCC diagnostic pop
  83. #endif