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 // This file defines the FileSystemStatCache interface. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_FILESYSTEMSTATCACHE_H 15 #define LLVM_CLANG_FILESYSTEMSTATCACHE_H 16 17 #include "llvm/ADT/OwningPtr.h" 18 #include "llvm/ADT/StringMap.h" 19 #include <sys/types.h> 20 #include <sys/stat.h> 21 22 namespace clang { 23 24 /// \brief Abstract interface for introducing a FileManager cache for 'stat' 25 /// system calls, which is used by precompiled and pretokenized headers to 26 /// improve performance. 27 class FileSystemStatCache { 28 protected: 29 llvm::OwningPtr<FileSystemStatCache> NextStatCache; 30 31 public: 32 virtual ~FileSystemStatCache() {} 33 34 enum LookupResult { 35 CacheExists, //< We know the file exists and its cached stat data. 36 CacheMissing //< We know that the file doesn't exist. 37 }; 38 39 /// FileSystemStatCache::get - Get the 'stat' information for the specified 40 /// path, using the cache to accellerate it if possible. This returns true if 41 /// the path does not exist or false if it exists. 42 /// 43 /// If FileDescriptor is non-null, then this lookup should only return success 44 /// for files (not directories). If it is null this lookup should only return 45 /// success for directories (not files). On a successful file lookup, the 46 /// implementation can optionally fill in FileDescriptor with a valid 47 /// descriptor and the client guarantees that it will close it. 48 static bool get(const char *Path, struct stat &StatBuf, int *FileDescriptor, 49 FileSystemStatCache *Cache); 50 51 52 /// \brief Sets the next stat call cache in the chain of stat caches. 53 /// Takes ownership of the given stat cache. 54 void setNextStatCache(FileSystemStatCache *Cache) { 55 NextStatCache.reset(Cache); 56 } 57 58 /// \brief Retrieve the next stat call cache in the chain. 59 FileSystemStatCache *getNextStatCache() { return NextStatCache.get(); } 60 61 /// \brief Retrieve the next stat call cache in the chain, transferring 62 /// ownership of this cache (and, transitively, all of the remaining caches) 63 /// to the caller. 64 FileSystemStatCache *takeNextStatCache() { return NextStatCache.take(); } 65 66 protected: 67 virtual LookupResult getStat(const char *Path, struct stat &StatBuf, 68 int *FileDescriptor) = 0; 69 70 LookupResult statChained(const char *Path, struct stat &StatBuf, 71 int *FileDescriptor) { 72 if (FileSystemStatCache *Next = getNextStatCache()) 73 return Next->getStat(Path, StatBuf, FileDescriptor); 74 75 // If we hit the end of the list of stat caches to try, just compute and 76 // return it without a cache. 77 return get(Path, StatBuf, FileDescriptor, 0) ? CacheMissing : CacheExists; 78 } 79 }; 80 81 /// \brief A stat "cache" that can be used by FileManager to keep 82 /// track of the results of stat() calls that occur throughout the 83 /// execution of the front end. 84 class MemorizeStatCalls : public FileSystemStatCache { 85 public: 86 /// \brief The set of stat() calls that have been seen. 87 llvm::StringMap<struct stat, llvm::BumpPtrAllocator> StatCalls; 88 89 typedef llvm::StringMap<struct stat, llvm::BumpPtrAllocator>::const_iterator 90 iterator; 91 92 iterator begin() const { return StatCalls.begin(); } 93 iterator end() const { return StatCalls.end(); } 94 95 virtual LookupResult getStat(const char *Path, struct stat &StatBuf, 96 int *FileDescriptor); 97 }; 98 99 } // end namespace clang 100 101 #endif 102