Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:49

0001 //===- FileSystemStatCache.h - Caching for 'stat' calls ---------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file
0010 /// Defines the FileSystemStatCache interface.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H
0015 #define LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H
0016 
0017 #include "clang/Basic/LLVM.h"
0018 #include "llvm/ADT/StringMap.h"
0019 #include "llvm/ADT/StringRef.h"
0020 #include "llvm/Support/Allocator.h"
0021 #include "llvm/Support/FileSystem.h"
0022 #include "llvm/Support/VirtualFileSystem.h"
0023 #include <cstdint>
0024 #include <ctime>
0025 #include <memory>
0026 #include <optional>
0027 #include <string>
0028 #include <utility>
0029 
0030 namespace clang {
0031 
0032 /// Abstract interface for introducing a FileManager cache for 'stat'
0033 /// system calls, which is used by precompiled and pretokenized headers to
0034 /// improve performance.
0035 class FileSystemStatCache {
0036   virtual void anchor();
0037 
0038 public:
0039   virtual ~FileSystemStatCache() = default;
0040 
0041   /// Get the 'stat' information for the specified path, using the cache
0042   /// to accelerate it if possible.
0043   ///
0044   /// \returns \c true if the path does not exist or \c false if it exists.
0045   ///
0046   /// If isFile is true, then this lookup should only return success for files
0047   /// (not directories).  If it is false this lookup should only return
0048   /// success for directories (not files).  On a successful file lookup, the
0049   /// implementation can optionally fill in \p F with a valid \p File object and
0050   /// the client guarantees that it will close it.
0051   static std::error_code get(StringRef Path, llvm::vfs::Status &Status,
0052                              bool isFile, std::unique_ptr<llvm::vfs::File> *F,
0053                              FileSystemStatCache *Cache,
0054                              llvm::vfs::FileSystem &FS, bool IsText = true);
0055 
0056 protected:
0057   // FIXME: The pointer here is a non-owning/optional reference to the
0058   // unique_ptr. std::optional<unique_ptr<vfs::File>&> might be nicer, but
0059   // Optional needs some work to support references so this isn't possible yet.
0060   virtual std::error_code getStat(StringRef Path, llvm::vfs::Status &Status,
0061                                   bool isFile,
0062                                   std::unique_ptr<llvm::vfs::File> *F,
0063                                   llvm::vfs::FileSystem &FS) = 0;
0064 };
0065 
0066 /// A stat "cache" that can be used by FileManager to keep
0067 /// track of the results of stat() calls that occur throughout the
0068 /// execution of the front end.
0069 class MemorizeStatCalls : public FileSystemStatCache {
0070 public:
0071   /// The set of stat() calls that have been seen.
0072   llvm::StringMap<llvm::vfs::Status, llvm::BumpPtrAllocator> StatCalls;
0073 
0074   using iterator =
0075       llvm::StringMap<llvm::vfs::Status,
0076                       llvm::BumpPtrAllocator>::const_iterator;
0077 
0078   iterator begin() const { return StatCalls.begin(); }
0079   iterator end() const { return StatCalls.end(); }
0080 
0081   std::error_code getStat(StringRef Path, llvm::vfs::Status &Status,
0082                           bool isFile,
0083                           std::unique_ptr<llvm::vfs::File> *F,
0084                           llvm::vfs::FileSystem &FS) override;
0085 };
0086 
0087 } // namespace clang
0088 
0089 #endif // LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H