]> granicus.if.org Git - clang/blob - include/clang/Basic/FileManager.h
pull "is directory" handling into FileManager::getStatValue
[clang] / include / clang / Basic / FileManager.h
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 "clang/Basic/FileSystemOptions.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Config/config.h" // for mode_t
24 // FIXME: Enhance libsystem to support inode and other fields in stat.
25 #include <sys/types.h>
26
27 struct stat;
28
29 namespace llvm {
30 class MemoryBuffer;
31 namespace sys { class Path; }
32 }
33
34 namespace clang {
35 class FileManager;
36 class FileSystemStatCache;
37   
38 /// DirectoryEntry - Cached information about one directory on the disk.
39 ///
40 class DirectoryEntry {
41   const char *Name;   // Name of the directory.
42   friend class FileManager;
43 public:
44   DirectoryEntry() : Name(0) {}
45   const char *getName() const { return Name; }
46 };
47
48 /// FileEntry - Cached information about one file on the disk.  If the 'FD'
49 /// member is valid, then this FileEntry has an open file descriptor for the
50 /// file.
51 ///
52 class FileEntry {
53   const char *Name;           // Name of the file.
54   off_t Size;                 // File size in bytes.
55   time_t ModTime;             // Modification time of file.
56   const DirectoryEntry *Dir;  // Directory file lives in.
57   unsigned UID;               // A unique (small) ID for the file.
58   dev_t Device;               // ID for the device containing the file.
59   ino_t Inode;                // Inode number for the file.
60   mode_t FileMode;            // The file mode as returned by 'stat'.
61   
62   /// FD - The file descriptor for the file entry if it is opened and owned
63   /// by the FileEntry.  If not, this is set to -1.
64   int FD;
65   friend class FileManager;
66   
67   void operator=(const FileEntry&); // DO NOT IMPLEMENT.
68 public:
69   FileEntry(dev_t device, ino_t inode, mode_t m)
70     : Name(0), Device(device), Inode(inode), FileMode(m), FD(-1) {}
71   // Add a default constructor for use with llvm::StringMap
72   FileEntry() : Name(0), Device(0), Inode(0), FileMode(0), FD(-1) {}
73
74   FileEntry(const FileEntry &FE) {
75     memcpy(this, &FE, sizeof(FE));
76     assert(FD == -1 && "Cannot copy an file-owning FileEntry");
77   }
78
79   ~FileEntry();
80
81   const char *getName() const { return Name; }
82   off_t getSize() const { return Size; }
83   unsigned getUID() const { return UID; }
84   ino_t getInode() const { return Inode; }
85   dev_t getDevice() const { return Device; }
86   time_t getModificationTime() const { return ModTime; }
87   mode_t getFileMode() const { return FileMode; }
88
89   /// getDir - Return the directory the file lives in.
90   ///
91   const DirectoryEntry *getDir() const { return Dir; }
92
93   bool operator<(const FileEntry &RHS) const {
94     return Device < RHS.Device || (Device == RHS.Device && Inode < RHS.Inode);
95   }
96 };
97
98 /// FileManager - Implements support for file system lookup, file system
99 /// caching, and directory search management.  This also handles more advanced
100 /// properties, such as uniquing files based on "inode", so that a file with two
101 /// names (e.g. symlinked) will be treated as a single file.
102 ///
103 class FileManager {
104   FileSystemOptions FileSystemOpts;
105   
106   class UniqueDirContainer;
107   class UniqueFileContainer;
108
109   /// UniqueDirs/UniqueFiles - Cache for existing directories/files.
110   ///
111   UniqueDirContainer &UniqueDirs;
112   UniqueFileContainer &UniqueFiles;
113
114   /// DirEntries/FileEntries - This is a cache of directory/file entries we have
115   /// looked up.  The actual Entry is owned by UniqueFiles/UniqueDirs above.
116   ///
117   llvm::StringMap<DirectoryEntry*, llvm::BumpPtrAllocator> DirEntries;
118   llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator> FileEntries;
119
120   /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
121   ///
122   unsigned NextFileUID;
123
124   /// \brief The virtual files that we have allocated.
125   llvm::SmallVector<FileEntry*, 4> VirtualFileEntries;
126
127   // Statistics.
128   unsigned NumDirLookups, NumFileLookups;
129   unsigned NumDirCacheMisses, NumFileCacheMisses;
130
131   // Caching.
132   llvm::OwningPtr<FileSystemStatCache> StatCache;
133
134   bool getStatValue(const char *Path, struct stat &StatBuf, bool isForDir);
135 public:
136   FileManager(const FileSystemOptions &FileSystemOpts);
137   ~FileManager();
138
139   /// \brief Installs the provided FileSystemStatCache object within
140   /// the FileManager. 
141   ///
142   /// Ownership of this object is transferred to the FileManager.
143   ///
144   /// \param statCache the new stat cache to install. Ownership of this
145   /// object is transferred to the FileManager.
146   ///
147   /// \param AtBeginning whether this new stat cache must be installed at the
148   /// beginning of the chain of stat caches. Otherwise, it will be added to
149   /// the end of the chain.
150   void addStatCache(FileSystemStatCache *statCache, bool AtBeginning = false);
151
152   /// \brief Removes the specified FileSystemStatCache object from the manager.
153   void removeStatCache(FileSystemStatCache *statCache);
154   
155   /// getDirectory - Lookup, cache, and verify the specified directory.  This
156   /// returns null if the directory doesn't exist.
157   ///
158   const DirectoryEntry *getDirectory(llvm::StringRef Filename);
159
160   /// getFile - Lookup, cache, and verify the specified file.  This returns null
161   /// if the file doesn't exist.
162   ///
163   const FileEntry *getFile(llvm::StringRef Filename);
164
165   /// \brief Retrieve a file entry for a "virtual" file that acts as
166   /// if there were a file with the given name on disk. The file
167   /// itself is not accessed.
168   const FileEntry *getVirtualFile(llvm::StringRef Filename, off_t Size,
169                                   time_t ModificationTime);
170
171   /// \brief Open the specified file as a MemoryBuffer, returning a new
172   /// MemoryBuffer if successful, otherwise returning null.
173   llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry,
174                                        std::string *ErrorStr = 0);
175   llvm::MemoryBuffer *getBufferForFile(llvm::StringRef Filename,
176                                        std::string *ErrorStr = 0);
177
178   /// \brief If path is not absolute and FileSystemOptions set the working
179   /// directory, the path is modified to be relative to the given
180   /// working directory.
181   static void FixupRelativePath(llvm::sys::Path &path,
182                                 const FileSystemOptions &FSOpts);
183   
184   void PrintStats() const;
185 };
186
187 }  // end namespace clang
188
189 #endif