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