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