Ted Kremenek | 8fbc88e | 2007-12-04 22:42:20 +0000 | [diff] [blame] | 1 | ///===--- FileManager.cpp - File System Probing and Caching ----------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the FileManager interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | // TODO: This should index all interesting directories with dirent calls. |
| 15 | // getdirentries ? |
| 16 | // opendir/readdir_r/closedir ? |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #include "clang/Basic/FileManager.h" |
| 21 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 9551a2c | 2007-12-04 18:21:35 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/Serialize.h" |
| 23 | #include "llvm/Bitcode/Deserialize.h" |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Streams.h" |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 25 | #include "llvm/Config/config.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
| 28 | // FIXME: Enhance libsystem to support inode and other fields. |
| 29 | #include <sys/stat.h> |
| 30 | |
Chris Lattner | a8c11c6 | 2007-09-03 18:37:14 +0000 | [diff] [blame] | 31 | #if defined(_MSC_VER) |
| 32 | #define S_ISDIR(s) (_S_IFDIR & s) |
| 33 | #endif |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 35 | /// NON_EXISTENT_DIR - A special value distinct from null that is used to |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | /// represent a dir name that doesn't exist on the disk. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 37 | #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 39 | #ifdef LLVM_ON_WIN32 |
| 40 | |
| 41 | #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\') |
| 42 | |
| 43 | namespace { |
| 44 | static std::string GetFullPath(const char *relPath) |
| 45 | { |
| 46 | char *absPathStrPtr = _fullpath(NULL, relPath, 0); |
| 47 | assert(absPathStrPtr && "_fullpath() returned NULL!"); |
| 48 | |
| 49 | std::string absPath(absPathStrPtr); |
| 50 | |
| 51 | free(absPathStrPtr); |
| 52 | return absPath; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | class FileManager::UniqueDirContainer { |
| 57 | /// UniqueDirs - Cache from full path to existing directories/files. |
| 58 | /// |
| 59 | llvm::StringMap<DirectoryEntry> UniqueDirs; |
| 60 | |
| 61 | public: |
| 62 | DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) { |
| 63 | std::string FullPath(GetFullPath(Name)); |
| 64 | return UniqueDirs.GetOrCreateValue( |
| 65 | FullPath.c_str(), |
| 66 | FullPath.c_str() + FullPath.size() |
| 67 | ).getValue(); |
| 68 | } |
| 69 | |
| 70 | size_t size() { return UniqueDirs.size(); } |
| 71 | }; |
| 72 | |
| 73 | class FileManager::UniqueFileContainer { |
| 74 | /// UniqueFiles - Cache from full path to existing directories/files. |
| 75 | /// |
| 76 | llvm::StringMap<FileEntry> UniqueFiles; |
| 77 | |
| 78 | public: |
| 79 | FileEntry &getFile(const char *Name, struct stat &StatBuf) { |
| 80 | std::string FullPath(GetFullPath(Name)); |
| 81 | return UniqueFiles.GetOrCreateValue( |
| 82 | FullPath.c_str(), |
| 83 | FullPath.c_str() + FullPath.size() |
| 84 | ).getValue(); |
| 85 | } |
| 86 | |
| 87 | size_t size() { return UniqueFiles.size(); } |
| 88 | }; |
| 89 | |
| 90 | #else |
| 91 | |
| 92 | #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/') |
| 93 | |
| 94 | class FileManager::UniqueDirContainer { |
| 95 | /// UniqueDirs - Cache from ID's to existing directories/files. |
| 96 | /// |
| 97 | std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs; |
| 98 | |
| 99 | public: |
| 100 | DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) { |
| 101 | return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)]; |
| 102 | } |
| 103 | |
| 104 | size_t size() { return UniqueDirs.size(); } |
| 105 | }; |
| 106 | |
| 107 | class FileManager::UniqueFileContainer { |
| 108 | /// UniqueFiles - Cache from ID's to existing directories/files. |
| 109 | /// |
| 110 | std::set<FileEntry> UniqueFiles; |
| 111 | |
| 112 | public: |
| 113 | FileEntry &getFile(const char *Name, struct stat &StatBuf) { |
| 114 | return |
| 115 | const_cast<FileEntry&>( |
| 116 | *UniqueFiles.insert(FileEntry(StatBuf.st_dev, |
| 117 | StatBuf.st_ino)).first); |
| 118 | } |
| 119 | |
| 120 | size_t size() { return UniqueFiles.size(); } |
| 121 | }; |
| 122 | |
| 123 | #endif |
| 124 | |
| 125 | |
| 126 | FileManager::FileManager() : UniqueDirs(*new UniqueDirContainer), |
| 127 | UniqueFiles(*new UniqueFileContainer), |
| 128 | DirEntries(64), FileEntries(64), NextFileUID(0) |
| 129 | { |
| 130 | NumDirLookups = NumFileLookups = 0; |
| 131 | NumDirCacheMisses = NumFileCacheMisses = 0; |
| 132 | } |
| 133 | |
| 134 | FileManager::~FileManager() { |
| 135 | delete &UniqueDirs; |
| 136 | delete &UniqueFiles; |
| 137 | } |
| 138 | |
| 139 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | /// getDirectory - Lookup, cache, and verify the specified directory. This |
| 141 | /// returns null if the directory doesn't exist. |
| 142 | /// |
| 143 | const DirectoryEntry *FileManager::getDirectory(const char *NameStart, |
| 144 | const char *NameEnd) { |
| 145 | ++NumDirLookups; |
| 146 | llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt = |
| 147 | DirEntries.GetOrCreateValue(NameStart, NameEnd); |
| 148 | |
| 149 | // See if there is already an entry in the map. |
| 150 | if (NamedDirEnt.getValue()) |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 151 | return NamedDirEnt.getValue() == NON_EXISTENT_DIR |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 152 | ? 0 : NamedDirEnt.getValue(); |
| 153 | |
| 154 | ++NumDirCacheMisses; |
| 155 | |
| 156 | // By default, initialize it to invalid. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 157 | NamedDirEnt.setValue(NON_EXISTENT_DIR); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 158 | |
| 159 | // Get the null-terminated directory name as stored as the key of the |
| 160 | // DirEntries map. |
| 161 | const char *InterndDirName = NamedDirEnt.getKeyData(); |
| 162 | |
| 163 | // Check to see if the directory exists. |
| 164 | struct stat StatBuf; |
| 165 | if (stat(InterndDirName, &StatBuf) || // Error stat'ing. |
| 166 | !S_ISDIR(StatBuf.st_mode)) // Not a directory? |
| 167 | return 0; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 168 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | // It exists. See if we have already opened a directory with the same inode. |
Ted Kremenek | da99544 | 2007-12-18 20:45:25 +0000 | [diff] [blame] | 170 | // This occurs when one dir is symlinked to another, for example. |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 171 | DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | |
| 173 | NamedDirEnt.setValue(&UDE); |
| 174 | if (UDE.getName()) // Already have an entry with this inode, return it. |
| 175 | return &UDE; |
| 176 | |
| 177 | // Otherwise, we don't have this directory yet, add it. We use the string |
| 178 | // key from the DirEntries map as the string. |
| 179 | UDE.Name = InterndDirName; |
| 180 | return &UDE; |
| 181 | } |
| 182 | |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 183 | /// NON_EXISTENT_FILE - A special value distinct from null that is used to |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | /// represent a filename that doesn't exist on the disk. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 185 | #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | |
| 187 | /// getFile - Lookup, cache, and verify the specified file. This returns null |
| 188 | /// if the file doesn't exist. |
| 189 | /// |
| 190 | const FileEntry *FileManager::getFile(const char *NameStart, |
| 191 | const char *NameEnd) { |
| 192 | ++NumFileLookups; |
| 193 | |
| 194 | // See if there is already an entry in the map. |
| 195 | llvm::StringMapEntry<FileEntry *> &NamedFileEnt = |
| 196 | FileEntries.GetOrCreateValue(NameStart, NameEnd); |
| 197 | |
| 198 | // See if there is already an entry in the map. |
| 199 | if (NamedFileEnt.getValue()) |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 200 | return NamedFileEnt.getValue() == NON_EXISTENT_FILE |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | ? 0 : NamedFileEnt.getValue(); |
| 202 | |
| 203 | ++NumFileCacheMisses; |
| 204 | |
| 205 | // By default, initialize it to invalid. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 206 | NamedFileEnt.setValue(NON_EXISTENT_FILE); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 207 | |
| 208 | // Figure out what directory it is in. If the string contains a / in it, |
| 209 | // strip off everything after it. |
| 210 | // FIXME: this logic should be in sys::Path. |
| 211 | const char *SlashPos = NameEnd-1; |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 212 | while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0])) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | --SlashPos; |
| 214 | |
| 215 | const DirectoryEntry *DirInfo; |
| 216 | if (SlashPos < NameStart) { |
| 217 | // Use the current directory if file has no path component. |
| 218 | const char *Name = "."; |
| 219 | DirInfo = getDirectory(Name, Name+1); |
| 220 | } else if (SlashPos == NameEnd-1) |
| 221 | return 0; // If filename ends with a /, it's a directory. |
| 222 | else |
| 223 | DirInfo = getDirectory(NameStart, SlashPos); |
| 224 | |
| 225 | if (DirInfo == 0) // Directory doesn't exist, file can't exist. |
| 226 | return 0; |
| 227 | |
| 228 | // Get the null-terminated file name as stored as the key of the |
| 229 | // FileEntries map. |
| 230 | const char *InterndFileName = NamedFileEnt.getKeyData(); |
| 231 | |
| 232 | // FIXME: Use the directory info to prune this, before doing the stat syscall. |
| 233 | // FIXME: This will reduce the # syscalls. |
| 234 | |
| 235 | // Nope, there isn't. Check to see if the file exists. |
| 236 | struct stat StatBuf; |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 237 | //llvm::cerr << "STATING: " << Filename; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 238 | if (stat(InterndFileName, &StatBuf) || // Error stat'ing. |
| 239 | S_ISDIR(StatBuf.st_mode)) { // A directory? |
| 240 | // If this file doesn't exist, we leave a null in FileEntries for this path. |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 241 | //llvm::cerr << ": Not existing\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 244 | //llvm::cerr << ": exists\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | |
Ted Kremenek | bca6d12 | 2007-12-18 22:29:39 +0000 | [diff] [blame] | 246 | // It exists. See if we have already opened a file with the same inode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | // This occurs when one dir is symlinked to another, for example. |
Ted Kremenek | 6bb816a | 2008-02-24 03:15:25 +0000 | [diff] [blame] | 248 | FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | |
| 250 | NamedFileEnt.setValue(&UFE); |
| 251 | if (UFE.getName()) // Already have an entry with this inode, return it. |
| 252 | return &UFE; |
| 253 | |
| 254 | // Otherwise, we don't have this directory yet, add it. |
| 255 | // FIXME: Change the name to be a char* that points back to the 'FileEntries' |
| 256 | // key. |
| 257 | UFE.Name = InterndFileName; |
| 258 | UFE.Size = StatBuf.st_size; |
| 259 | UFE.ModTime = StatBuf.st_mtime; |
| 260 | UFE.Dir = DirInfo; |
| 261 | UFE.UID = NextFileUID++; |
| 262 | return &UFE; |
| 263 | } |
| 264 | |
| 265 | void FileManager::PrintStats() const { |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 266 | llvm::cerr << "\n*** File Manager Stats:\n"; |
| 267 | llvm::cerr << UniqueFiles.size() << " files found, " |
| 268 | << UniqueDirs.size() << " dirs found.\n"; |
| 269 | llvm::cerr << NumDirLookups << " dir lookups, " |
| 270 | << NumDirCacheMisses << " dir cache misses.\n"; |
| 271 | llvm::cerr << NumFileLookups << " file lookups, " |
| 272 | << NumFileCacheMisses << " file cache misses.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | 3d2da3d | 2008-01-11 20:42:05 +0000 | [diff] [blame] | 274 | //llvm::cerr << PagesMapped << BytesOfPagesMapped << FSLookups; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | } |