]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Basic/FileSystemStatCache.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Basic / FileSystemStatCache.h
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 clang {
30
31 namespace vfs {
32
33 class File;
34 class FileSystem;
35
36 } // namespace vfs
37
38 // FIXME: should probably replace this with vfs::Status
39 struct FileData {
40   std::string Name;
41   uint64_t Size = 0;
42   time_t ModTime = 0;
43   llvm::sys::fs::UniqueID UniqueID;
44   bool IsDirectory = false;
45   bool IsNamedPipe = false;
46   bool InPCH = false;
47
48   // FIXME: remove this when files support multiple names
49   bool IsVFSMapped = false;
50
51   FileData() = default;
52 };
53
54 /// Abstract interface for introducing a FileManager cache for 'stat'
55 /// system calls, which is used by precompiled and pretokenized headers to
56 /// improve performance.
57 class FileSystemStatCache {
58   virtual void anchor();
59
60 protected:
61   std::unique_ptr<FileSystemStatCache> NextStatCache;
62
63 public:
64   virtual ~FileSystemStatCache() = default;
65
66   enum LookupResult {
67     /// We know the file exists and its cached stat data.
68     CacheExists,
69
70     /// We know that the file doesn't exist.
71     CacheMissing
72   };
73
74   /// Get the 'stat' information for the specified path, using the cache
75   /// to accelerate it if possible.
76   ///
77   /// \returns \c true if the path does not exist or \c false if it exists.
78   ///
79   /// If isFile is true, then this lookup should only return success for files
80   /// (not directories).  If it is false this lookup should only return
81   /// success for directories (not files).  On a successful file lookup, the
82   /// implementation can optionally fill in \p F with a valid \p File object and
83   /// the client guarantees that it will close it.
84   static bool get(StringRef Path, FileData &Data, bool isFile,
85                   std::unique_ptr<vfs::File> *F, FileSystemStatCache *Cache,
86                   vfs::FileSystem &FS);
87
88   /// Sets the next stat call cache in the chain of stat caches.
89   /// Takes ownership of the given stat cache.
90   void setNextStatCache(std::unique_ptr<FileSystemStatCache> Cache) {
91     NextStatCache = std::move(Cache);
92   }
93
94   /// Retrieve the next stat call cache in the chain.
95   FileSystemStatCache *getNextStatCache() { return NextStatCache.get(); }
96
97   /// Retrieve the next stat call cache in the chain, transferring
98   /// ownership of this cache (and, transitively, all of the remaining caches)
99   /// to the caller.
100   std::unique_ptr<FileSystemStatCache> takeNextStatCache() {
101     return std::move(NextStatCache);
102   }
103
104 protected:
105   // FIXME: The pointer here is a non-owning/optional reference to the
106   // unique_ptr. Optional<unique_ptr<vfs::File>&> might be nicer, but
107   // Optional needs some work to support references so this isn't possible yet.
108   virtual LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
109                                std::unique_ptr<vfs::File> *F,
110                                vfs::FileSystem &FS) = 0;
111
112   LookupResult statChained(StringRef Path, FileData &Data, bool isFile,
113                            std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
114     if (FileSystemStatCache *Next = getNextStatCache())
115       return Next->getStat(Path, Data, isFile, F, FS);
116
117     // If we hit the end of the list of stat caches to try, just compute and
118     // return it without a cache.
119     return get(Path, Data, isFile, F, nullptr, FS) ? CacheMissing : CacheExists;
120   }
121 };
122
123 /// A stat "cache" that can be used by FileManager to keep
124 /// track of the results of stat() calls that occur throughout the
125 /// execution of the front end.
126 class MemorizeStatCalls : public FileSystemStatCache {
127 public:
128   /// The set of stat() calls that have been seen.
129   llvm::StringMap<FileData, llvm::BumpPtrAllocator> StatCalls;
130
131   using iterator =
132       llvm::StringMap<FileData, llvm::BumpPtrAllocator>::const_iterator;
133
134   iterator begin() const { return StatCalls.begin(); }
135   iterator end() const { return StatCalls.end(); }
136
137   LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
138                        std::unique_ptr<vfs::File> *F,
139                        vfs::FileSystem &FS) override;
140 };
141
142 } // namespace clang
143
144 #endif // LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H