clang  8.0.0svn
FileSystemStatCache.h
Go to the documentation of this file.
1 //===- FileSystemStatCache.h - Caching for 'stat' calls ---------*- C++ -*-===//
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 /// \file
11 /// Defines the FileSystemStatCache interface.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H
16 #define LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H
17 
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/Allocator.h"
22 #include "llvm/Support/FileSystem.h"
23 #include <cstdint>
24 #include <ctime>
25 #include <memory>
26 #include <string>
27 #include <utility>
28 
29 namespace llvm {
30 
31 namespace vfs {
32 
33 class File;
34 class FileSystem;
35 
36 } // namespace vfs
37 } // namespace llvm
38 
39 namespace clang {
40 
41 // FIXME: should probably replace this with vfs::Status
42 struct FileData {
43  std::string Name;
44  uint64_t Size = 0;
45  time_t ModTime = 0;
46  llvm::sys::fs::UniqueID UniqueID;
47  bool IsDirectory = false;
48  bool IsNamedPipe = false;
49  bool InPCH = false;
50 
51  // FIXME: remove this when files support multiple names
52  bool IsVFSMapped = false;
53 
54  FileData() = default;
55 };
56 
57 /// Abstract interface for introducing a FileManager cache for 'stat'
58 /// system calls, which is used by precompiled and pretokenized headers to
59 /// improve performance.
61  virtual void anchor();
62 
63 protected:
64  std::unique_ptr<FileSystemStatCache> NextStatCache;
65 
66 public:
67  virtual ~FileSystemStatCache() = default;
68 
69  enum LookupResult {
70  /// We know the file exists and its cached stat data.
72 
73  /// We know that the file doesn't exist.
74  CacheMissing
75  };
76 
77  /// Get the 'stat' information for the specified path, using the cache
78  /// to accelerate it if possible.
79  ///
80  /// \returns \c true if the path does not exist or \c false if it exists.
81  ///
82  /// If isFile is true, then this lookup should only return success for files
83  /// (not directories). If it is false this lookup should only return
84  /// success for directories (not files). On a successful file lookup, the
85  /// implementation can optionally fill in \p F with a valid \p File object and
86  /// the client guarantees that it will close it.
87  static bool get(StringRef Path, FileData &Data, bool isFile,
88  std::unique_ptr<llvm::vfs::File> *F,
89  FileSystemStatCache *Cache, llvm::vfs::FileSystem &FS);
90 
91  /// Sets the next stat call cache in the chain of stat caches.
92  /// Takes ownership of the given stat cache.
93  void setNextStatCache(std::unique_ptr<FileSystemStatCache> Cache) {
94  NextStatCache = std::move(Cache);
95  }
96 
97  /// Retrieve the next stat call cache in the chain.
98  FileSystemStatCache *getNextStatCache() { return NextStatCache.get(); }
99 
100  /// Retrieve the next stat call cache in the chain, transferring
101  /// ownership of this cache (and, transitively, all of the remaining caches)
102  /// to the caller.
103  std::unique_ptr<FileSystemStatCache> takeNextStatCache() {
104  return std::move(NextStatCache);
105  }
106 
107 protected:
108  // FIXME: The pointer here is a non-owning/optional reference to the
109  // unique_ptr. Optional<unique_ptr<vfs::File>&> might be nicer, but
110  // Optional needs some work to support references so this isn't possible yet.
111  virtual LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
112  std::unique_ptr<llvm::vfs::File> *F,
113  llvm::vfs::FileSystem &FS) = 0;
114 
115  LookupResult statChained(StringRef Path, FileData &Data, bool isFile,
116  std::unique_ptr<llvm::vfs::File> *F,
117  llvm::vfs::FileSystem &FS) {
118  if (FileSystemStatCache *Next = getNextStatCache())
119  return Next->getStat(Path, Data, isFile, F, FS);
120 
121  // If we hit the end of the list of stat caches to try, just compute and
122  // return it without a cache.
123  return get(Path, Data, isFile, F, nullptr, FS) ? CacheMissing : CacheExists;
124  }
125 };
126 
127 /// A stat "cache" that can be used by FileManager to keep
128 /// track of the results of stat() calls that occur throughout the
129 /// execution of the front end.
131 public:
132  /// The set of stat() calls that have been seen.
133  llvm::StringMap<FileData, llvm::BumpPtrAllocator> StatCalls;
134 
135  using iterator =
136  llvm::StringMap<FileData, llvm::BumpPtrAllocator>::const_iterator;
137 
138  iterator begin() const { return StatCalls.begin(); }
139  iterator end() const { return StatCalls.end(); }
140 
141  LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
142  std::unique_ptr<llvm::vfs::File> *F,
143  llvm::vfs::FileSystem &FS) override;
144 };
145 
146 } // namespace clang
147 
148 #endif // LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H
LookupResult statChained(StringRef Path, FileData &Data, bool isFile, std::unique_ptr< llvm::vfs::File > *F, llvm::vfs::FileSystem &FS)
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
TypePropertyCache< Private > Cache
Definition: Type.cpp:3479
We know the file exists and its cached stat data.
llvm::StringMap< FileData, llvm::BumpPtrAllocator >::const_iterator iterator
Abstract interface for introducing a FileManager cache for &#39;stat&#39; system calls, which is used by prec...
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
std::unique_ptr< FileSystemStatCache > takeNextStatCache()
Retrieve the next stat call cache in the chain, transferring ownership of this cache (and...
llvm::StringMap< FileData, llvm::BumpPtrAllocator > StatCalls
The set of stat() calls that have been seen.
void setNextStatCache(std::unique_ptr< FileSystemStatCache > Cache)
Sets the next stat call cache in the chain of stat caches.
static bool get(StringRef Path, FileData &Data, bool isFile, std::unique_ptr< llvm::vfs::File > *F, FileSystemStatCache *Cache, llvm::vfs::FileSystem &FS)
Get the &#39;stat&#39; information for the specified path, using the cache to accelerate it if possible...
llvm::sys::fs::UniqueID UniqueID
Dataflow Directional Tag Classes.
std::unique_ptr< FileSystemStatCache > NextStatCache
A stat "cache" that can be used by FileManager to keep track of the results of stat() calls that occu...
FileSystemStatCache * getNextStatCache()
Retrieve the next stat call cache in the chain.