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 <string> 27 #include <utility> 28 29 namespace clang { 30 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(); 36 37 public: 38 virtual ~FileSystemStatCache() = default; 39 40 /// Get the 'stat' information for the specified path, using the cache 41 /// to accelerate it if possible. 42 /// 43 /// \returns \c true if the path does not exist or \c false if it exists. 44 /// 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); 54 55 protected: 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, 60 bool isFile, 61 std::unique_ptr<llvm::vfs::File> *F, 62 llvm::vfs::FileSystem &FS) = 0; 63 }; 64 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 { 69 public: 70 /// The set of stat() calls that have been seen. 71 llvm::StringMap<llvm::vfs::Status, llvm::BumpPtrAllocator> StatCalls; 72 73 using iterator = 74 llvm::StringMap<llvm::vfs::Status, 75 llvm::BumpPtrAllocator>::const_iterator; 76 77 iterator begin() const { return StatCalls.begin(); } 78 iterator end() const { return StatCalls.end(); } 79 80 std::error_code getStat(StringRef Path, llvm::vfs::Status &Status, 81 bool isFile, 82 std::unique_ptr<llvm::vfs::File> *F, 83 llvm::vfs::FileSystem &FS) override; 84 }; 85 86 } // namespace clang 87 88 #endif // LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H 89