Home | History | Annotate | Download | only in Basic
      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 "clang/Basic/LLVM.h"
     18 #include "llvm/ADT/OwningPtr.h"
     19 #include "llvm/ADT/StringMap.h"
     20 #include <sys/types.h>
     21 #include <sys/stat.h>
     22 
     23 namespace clang {
     24 
     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();
     30 protected:
     31   OwningPtr<FileSystemStatCache> NextStatCache;
     32 
     33 public:
     34   virtual ~FileSystemStatCache() {}
     35 
     36   enum LookupResult {
     37     CacheExists,   //< We know the file exists and its cached stat data.
     38     CacheMissing   //< We know that the file doesn't exist.
     39   };
     40 
     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.
     44   ///
     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);
     52 
     53 
     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);
     58   }
     59 
     60   /// \brief Retrieve the next stat call cache in the chain.
     61   FileSystemStatCache *getNextStatCache() { return NextStatCache.get(); }
     62 
     63   /// \brief Retrieve the next stat call cache in the chain, transferring
     64   /// ownership of this cache (and, transitively, all of the remaining caches)
     65   /// to the caller.
     66   FileSystemStatCache *takeNextStatCache() { return NextStatCache.take(); }
     67 
     68 protected:
     69   virtual LookupResult getStat(const char *Path, struct stat &StatBuf,
     70                                int *FileDescriptor) = 0;
     71 
     72   LookupResult statChained(const char *Path, struct stat &StatBuf,
     73                            int *FileDescriptor) {
     74     if (FileSystemStatCache *Next = getNextStatCache())
     75       return Next->getStat(Path, StatBuf, FileDescriptor);
     76 
     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;
     80   }
     81 };
     82 
     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 {
     87 public:
     88   /// \brief The set of stat() calls that have been seen.
     89   llvm::StringMap<struct stat, llvm::BumpPtrAllocator> StatCalls;
     90 
     91   typedef llvm::StringMap<struct stat, llvm::BumpPtrAllocator>::const_iterator
     92   iterator;
     93 
     94   iterator begin() const { return StatCalls.begin(); }
     95   iterator end() const { return StatCalls.end(); }
     96 
     97   virtual LookupResult getStat(const char *Path, struct stat &StatBuf,
     98                                int *FileDescriptor);
     99 };
    100 
    101 } // end namespace clang
    102 
    103 #endif
    104