zcov: / include/clang/Basic/FileManager.h


Files: 1 Branches Taken: 64.7% 11 / 17
Generated: 2010-02-10 01:31 Branches Executed: 70.6% 12 / 17
Line Coverage: 100.0% 36 / 36


Programs: 37 Runs 65549


       1                 : //===--- FileManager.h - File System Probing and Caching --------*- 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 FileManager interface.
      11                 : //
      12                 : //===----------------------------------------------------------------------===//
      13                 : 
      14                 : #ifndef LLVM_CLANG_FILEMANAGER_H
      15                 : #define LLVM_CLANG_FILEMANAGER_H
      16                 : 
      17                 : #include "llvm/ADT/SmallVector.h"
      18                 : #include "llvm/ADT/StringMap.h"
      19                 : #include "llvm/ADT/StringRef.h"
      20                 : #include "llvm/ADT/OwningPtr.h"
      21                 : #include "llvm/Support/Allocator.h"
      22                 : #include "llvm/Config/config.h" // for mode_t
      23                 : // FIXME: Enhance libsystem to support inode and other fields in stat.
      24                 : #include <sys/types.h>
      25                 : #include <sys/stat.h>
      26                 : 
      27                 : namespace clang {
      28                 : class FileManager;
      29                 : 
      30                 : /// DirectoryEntry - Cached information about one directory on the disk.
      31                 : ///
      32                 : class DirectoryEntry {
      33                 :   const char *Name;   // Name of the directory.
      34                 :   friend class FileManager;
      35                 : public:
      36            13382:   DirectoryEntry() : Name(0) {}
      37            16531:   const char *getName() const { return Name; }
      38                 : };
      39                 : 
      40                 : /// FileEntry - Cached information about one file on the disk.
      41                 : ///
      42             2994: class FileEntry {
      43                 :   const char *Name;           // Name of the file.
      44                 :   off_t Size;                 // File size in bytes.
      45                 :   time_t ModTime;             // Modification time of file.
      46                 :   const DirectoryEntry *Dir;  // Directory file lives in.
      47                 :   unsigned UID;               // A unique (small) ID for the file.
      48                 :   dev_t Device;               // ID for the device containing the file.
      49                 :   ino_t Inode;                // Inode number for the file.
      50                 :   mode_t FileMode;            // The file mode as returned by 'stat'.
      51                 :   friend class FileManager;
      52                 : public:
      53             2998:   FileEntry(dev_t device, ino_t inode, mode_t m)
      54             2998:     : Name(0), Device(device), Inode(inode), FileMode(m) {}
      55                 :   // Add a default constructor for use with llvm::StringMap
      56               12:   FileEntry() : Name(0), Device(0), Inode(0), FileMode(0) {}
      57                 : 
      58            10895:   const char *getName() const { return Name; }
      59             5877:   off_t getSize() const { return Size; }
      60            12317:   unsigned getUID() const { return UID; }
      61                2:   ino_t getInode() const { return Inode; }
      62                2:   dev_t getDevice() const { return Device; }
      63                2:   time_t getModificationTime() const { return ModTime; }
      64                2:   mode_t getFileMode() const { return FileMode; }
      65                 : 
      66                 :   /// getDir - Return the directory the file lives in.
      67                 :   ///
      68               63:   const DirectoryEntry *getDir() const { return Dir; }
      69                 : 
      70             2338:   bool operator<(const FileEntry& RHS) const {
                     2224: branch 0 taken
                      114: branch 1 taken
                     1944: branch 2 taken
                      280: branch 3 taken
                     1141: branch 4 taken
                      803: branch 5 taken
      71             2338:     return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
      72                 :   }
      73                 : };
      74                 : 
      75                 : /// \brief Abstract interface for introducing a FileManager cache for 'stat'
      76                 : /// system calls, which is used by precompiled and pretokenized headers to
      77                 : /// improve performance.
      78               92: class StatSysCallCache {
      79                 : protected:
      80                 :   llvm::OwningPtr<StatSysCallCache> NextStatCache;
      81                 :   
      82                 : public:
                        0: branch 1 not taken
                       89: branch 2 taken
                        0: branch 5 not taken
                        0: branch 6 not taken
                        0: branch 9 not taken
                        0: branch 10 not taken
      83               89:   virtual ~StatSysCallCache() {}
      84              134:   virtual int stat(const char *path, struct stat *buf) {
                        4: branch 1 taken
                      130: branch 2 taken
      85              134:     if (getNextStatCache())
      86                4:       return getNextStatCache()->stat(path, buf);
      87                 :     
      88              130:     return ::stat(path, buf);
      89                 :   }
      90                 :   
      91                 :   /// \brief Sets the next stat call cache in the chain of stat caches.
      92                 :   /// Takes ownership of the given stat cache.
      93               92:   void setNextStatCache(StatSysCallCache *Cache) {
      94               92:     NextStatCache.reset(Cache);
      95               92:   }
      96                 :   
      97                 :   /// \brief Retrieve the next stat call cache in the chain.
      98              139:   StatSysCallCache *getNextStatCache() { return NextStatCache.get(); }
      99                 : 
     100                 :   /// \brief Retrieve the next stat call cache in the chain, transferring
     101                 :   /// ownership of this cache (and, transitively, all of the remaining caches)
     102                 :   /// to the caller.
     103                1:   StatSysCallCache *takeNextStatCache() { return NextStatCache.take(); }
     104                 : };
     105                 : 
     106                 : /// \brief A stat "cache" that can be used by FileManager to keep
     107                 : /// track of the results of stat() calls that occur throughout the
     108                 : /// execution of the front end.
                        0: branch 8 not taken
     109               88: class MemorizeStatCalls : public StatSysCallCache {
     110                 : public:
     111                 :   /// \brief The result of a stat() call.
     112                 :   ///
     113                 :   /// The first member is the result of calling stat(). If stat()
     114                 :   /// found something, the second member is a copy of the stat
     115                 :   /// structure.
     116                 :   typedef std::pair<int, struct stat> StatResult;
     117                 : 
     118                 :   /// \brief The set of stat() calls that have been
     119                 :   llvm::StringMap<StatResult, llvm::BumpPtrAllocator> StatCalls;
     120                 : 
     121                 :   typedef llvm::StringMap<StatResult, llvm::BumpPtrAllocator>::const_iterator
     122                 :     iterator;
     123                 : 
     124               43:   iterator begin() const { return StatCalls.begin(); }
     125               43:   iterator end() const { return StatCalls.end(); }
     126                 : 
     127                 :   virtual int stat(const char *path, struct stat *buf);
     128                 : };
     129                 : 
     130                 : /// FileManager - Implements support for file system lookup, file system
     131                 : /// caching, and directory search management.  This also handles more advanced
     132                 : /// properties, such as uniquing files based on "inode", so that a file with two
     133                 : /// names (e.g. symlinked) will be treated as a single file.
     134                 : ///
     135                 : class FileManager {
     136                 : 
     137                 :   class UniqueDirContainer;
     138                 :   class UniqueFileContainer;
     139                 : 
     140                 :   /// UniqueDirs/UniqueFiles - Cache for existing directories/files.
     141                 :   ///
     142                 :   UniqueDirContainer &UniqueDirs;
     143                 :   UniqueFileContainer &UniqueFiles;
     144                 : 
     145                 :   /// DirEntries/FileEntries - This is a cache of directory/file entries we have
     146                 :   /// looked up.  The actual Entry is owned by UniqueFiles/UniqueDirs above.
     147                 :   ///
     148                 :   llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> DirEntries;
     149                 :   llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> FileEntries;
     150                 : 
     151                 :   /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
     152                 :   ///
     153                 :   unsigned NextFileUID;
     154                 : 
     155                 :   /// \brief The virtual files that we have allocated.
     156                 :   llvm::SmallVector<FileEntry *, 4> VirtualFileEntries;
     157                 : 
     158                 :   // Statistics.
     159                 :   unsigned NumDirLookups, NumFileLookups;
     160                 :   unsigned NumDirCacheMisses, NumFileCacheMisses;
     161                 : 
     162                 :   // Caching.
     163                 :   llvm::OwningPtr<StatSysCallCache> StatCache;
     164                 : 
     165            73642:   int stat_cached(const char* path, struct stat* buf) {
                      227: branch 1 taken
                    73415: branch 2 taken
     166            73642:     return StatCache.get() ? StatCache->stat(path, buf) : stat(path, buf);
     167                 :   }
     168                 : 
     169                 : public:
     170                 :   FileManager();
     171                 :   ~FileManager();
     172                 : 
     173                 :   /// \brief Installs the provided StatSysCallCache object within
     174                 :   /// the FileManager. 
     175                 :   ///
     176                 :   /// Ownership of this object is transferred to the FileManager.
     177                 :   ///
     178                 :   /// \param statCache the new stat cache to install. Ownership of this
     179                 :   /// object is transferred to the FileManager.
     180                 :   ///
     181                 :   /// \param AtBeginning whether this new stat cache must be installed at the
     182                 :   /// beginning of the chain of stat caches. Otherwise, it will be added to
     183                 :   /// the end of the chain.
     184                 :   void addStatCache(StatSysCallCache *statCache, bool AtBeginning = false);
     185                 : 
     186                 :   /// \brief Removes the provided StatSysCallCache object from the file manager.
     187                 :   void removeStatCache(StatSysCallCache *statCache);
     188                 :   
     189                 :   /// getDirectory - Lookup, cache, and verify the specified directory.  This
     190                 :   /// returns null if the directory doesn't exist.
     191                 :   ///
     192            65603:   const DirectoryEntry *getDirectory(const llvm::StringRef &Filename) {
     193            65603:     return getDirectory(Filename.begin(), Filename.end());
     194                 :   }
     195                 :   const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd);
     196                 : 
     197                 :   /// getFile - Lookup, cache, and verify the specified file.  This returns null
     198                 :   /// if the file doesn't exist.
     199                 :   ///
     200            51497:   const FileEntry *getFile(const llvm::StringRef &Filename) {
     201            51497:     return getFile(Filename.begin(), Filename.end());
     202                 :   }
     203                 :   const FileEntry *getFile(const char *FilenameStart,
     204                 :                            const char *FilenameEnd);
     205                 : 
     206                 :   /// \brief Retrieve a file entry for a "virtual" file that acts as
     207                 :   /// if there were a file with the given name on disk. The file
     208                 :   /// itself is not accessed.
     209                 :   const FileEntry *getVirtualFile(const llvm::StringRef &Filename,
     210                 :                                   off_t Size, time_t ModificationTime);
     211                 :   void PrintStats() const;
     212                 : };
     213                 : 
     214                 : }  // end namespace clang
     215                 : 
     216                 : #endif

Generated: 2010-02-10 01:31 by zcov