1 //===- FileSystemStatCache.h - Caching for 'stat' calls ---------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 /// \file 10 /// Defines the FileSystemStatCache interface. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H 15 #define LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H 16 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" 23 #include <cstdint> 24 #include <ctime> 25 #include <memory> 26 #include <optional> 27 #include <string> 28 #include <utility> 29 30 namespace clang { 31 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 38 public: 39 virtual ~FileSystemStatCache() = default; 40 41 /// Get the 'stat' information for the specified path, using the cache 42 /// to accelerate it if possible. 43 /// 44 /// \returns \c true if the path does not exist or \c false if it exists. 45 /// 46 /// If isFile is true, then this lookup should only return success for files 47 /// (not directories). If it is false this lookup should only return 48 /// success for directories (not files). On a successful file lookup, the 49 /// implementation can optionally fill in \p F with a valid \p File object and 50 /// the client guarantees that it will close it. 51 static std::error_code 52 get(StringRef Path, llvm::vfs::Status &Status, bool isFile, 53 std::unique_ptr<llvm::vfs::File> *F, 54 FileSystemStatCache *Cache, llvm::vfs::FileSystem &FS); 55 56 protected: 57 // FIXME: The pointer here is a non-owning/optional reference to the 58 // unique_ptr. std::optional<unique_ptr<vfs::File>&> might be nicer, but 59 // Optional needs some work to support references so this isn't possible yet. 60 virtual std::error_code getStat(StringRef Path, llvm::vfs::Status &Status, 61 bool isFile, 62 std::unique_ptr<llvm::vfs::File> *F, 63 llvm::vfs::FileSystem &FS) = 0; 64 }; 65 66 /// A stat "cache" that can be used by FileManager to keep 67 /// track of the results of stat() calls that occur throughout the 68 /// execution of the front end. 69 class MemorizeStatCalls : public FileSystemStatCache { 70 public: 71 /// The set of stat() calls that have been seen. 72 llvm::StringMap<llvm::vfs::Status, llvm::BumpPtrAllocator> StatCalls; 73 74 using iterator = 75 llvm::StringMap<llvm::vfs::Status, 76 llvm::BumpPtrAllocator>::const_iterator; 77 begin()78 iterator begin() const { return StatCalls.begin(); } end()79 iterator end() const { return StatCalls.end(); } 80 81 std::error_code getStat(StringRef Path, llvm::vfs::Status &Status, 82 bool isFile, 83 std::unique_ptr<llvm::vfs::File> *F, 84 llvm::vfs::FileSystem &FS) override; 85 }; 86 87 } // namespace clang 88 89 #endif // LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H 90