1 //===--- FileSystemStatCache.h - Caching for 'stat' calls -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the FileSystemStatCache interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_FILESYSTEMSTATCACHE_H
15 #define LLVM_CLANG_FILESYSTEMSTATCACHE_H
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/ADT/StringMap.h"
19 #include <sys/types.h>
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 {
29 llvm::OwningPtr<FileSystemStatCache> NextStatCache;
32 virtual ~FileSystemStatCache() {}
35 CacheExists, //< We know the file exists and its cached stat data.
36 CacheMissing //< We know that the file doesn't exist.
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.
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);
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);
58 /// \brief Retrieve the next stat call cache in the chain.
59 FileSystemStatCache *getNextStatCache() { return NextStatCache.get(); }
61 /// \brief Retrieve the next stat call cache in the chain, transferring
62 /// ownership of this cache (and, transitively, all of the remaining caches)
64 FileSystemStatCache *takeNextStatCache() { return NextStatCache.take(); }
67 virtual LookupResult getStat(const char *Path, struct stat &StatBuf,
68 int *FileDescriptor) = 0;
70 LookupResult statChained(const char *Path, struct stat &StatBuf,
71 int *FileDescriptor) {
72 if (FileSystemStatCache *Next = getNextStatCache())
73 return Next->getStat(Path, StatBuf, FileDescriptor);
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;
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 {
86 /// \brief The set of stat() calls that have been seen.
87 llvm::StringMap<struct stat, llvm::BumpPtrAllocator> StatCalls;
89 typedef llvm::StringMap<struct stat, llvm::BumpPtrAllocator>::const_iterator
92 iterator begin() const { return StatCalls.begin(); }
93 iterator end() const { return StatCalls.end(); }
95 virtual LookupResult getStat(const char *Path, struct stat &StatBuf,
99 } // end namespace clang