blob: ee4309de937b9f4bce48ca9a3ce9a28a59866242 [file] [log] [blame]
Ted Kremenek6ca076c2007-12-04 22:42:20 +00001///===--- FileManager.cpp - File System Probing and Caching ----------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
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"
Chris Lattner2f4a89a2006-10-30 03:55:17 +000021#include "llvm/ADT/SmallString.h"
Chris Lattner3441b4f2009-08-23 22:45:33 +000022#include "llvm/Support/raw_ostream.h"
Douglas Gregorc5046832009-04-27 18:38:38 +000023#include "llvm/System/Path.h"
Ted Kremenekd87eef82008-02-24 03:15:25 +000024#include "llvm/Config/config.h"
Benjamin Kramer26db6482009-09-05 09:49:39 +000025#include <map>
26#include <set>
27#include <string>
Chris Lattner22eb9722006-06-18 05:43:12 +000028using namespace clang;
29
30// FIXME: Enhance libsystem to support inode and other fields.
31#include <sys/stat.h>
32
Chris Lattnerff3fa8b2007-09-03 18:37:14 +000033#if defined(_MSC_VER)
Chris Lattner882018b2009-02-12 01:37:35 +000034#define S_ISDIR(s) (_S_IFDIR & s)
Chris Lattnerff3fa8b2007-09-03 18:37:14 +000035#endif
Chris Lattneraf653752006-10-30 03:06:54 +000036
Ted Kremenek8d71e252008-01-11 20:42:05 +000037/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Chris Lattneraf653752006-10-30 03:06:54 +000038/// represent a dir name that doesn't exist on the disk.
Ted Kremenek8d71e252008-01-11 20:42:05 +000039#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Chris Lattneraf653752006-10-30 03:06:54 +000040
Ted Kremenek5c04bd82009-01-28 00:27:31 +000041//===----------------------------------------------------------------------===//
42// Windows.
43//===----------------------------------------------------------------------===//
44
Ted Kremenekd87eef82008-02-24 03:15:25 +000045#ifdef LLVM_ON_WIN32
46
47#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
48
49namespace {
Mike Stump11289f42009-09-09 15:08:12 +000050 static std::string GetFullPath(const char *relPath) {
Ted Kremenekd87eef82008-02-24 03:15:25 +000051 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
52 assert(absPathStrPtr && "_fullpath() returned NULL!");
53
54 std::string absPath(absPathStrPtr);
55
56 free(absPathStrPtr);
57 return absPath;
58 }
59}
60
61class FileManager::UniqueDirContainer {
62 /// UniqueDirs - Cache from full path to existing directories/files.
63 ///
Mike Stump11289f42009-09-09 15:08:12 +000064 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenekd87eef82008-02-24 03:15:25 +000065
66public:
67 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
68 std::string FullPath(GetFullPath(Name));
69 return UniqueDirs.GetOrCreateValue(
70 FullPath.c_str(),
71 FullPath.c_str() + FullPath.size()
72 ).getValue();
73 }
Mike Stump11289f42009-09-09 15:08:12 +000074
Ted Kremenekd87eef82008-02-24 03:15:25 +000075 size_t size() { return UniqueDirs.size(); }
76};
77
78class FileManager::UniqueFileContainer {
79 /// UniqueFiles - Cache from full path to existing directories/files.
80 ///
Ted Kremenek1502b7e2009-01-28 01:01:07 +000081 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenekd87eef82008-02-24 03:15:25 +000082
83public:
84 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
85 std::string FullPath(GetFullPath(Name));
86 return UniqueFiles.GetOrCreateValue(
87 FullPath.c_str(),
88 FullPath.c_str() + FullPath.size()
89 ).getValue();
90 }
91
92 size_t size() { return UniqueFiles.size(); }
93};
94
Ted Kremenek5c04bd82009-01-28 00:27:31 +000095//===----------------------------------------------------------------------===//
96// Unix-like Systems.
97//===----------------------------------------------------------------------===//
98
Ted Kremenekd87eef82008-02-24 03:15:25 +000099#else
100
101#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
102
103class FileManager::UniqueDirContainer {
104 /// UniqueDirs - Cache from ID's to existing directories/files.
105 ///
Mike Stump11289f42009-09-09 15:08:12 +0000106 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenekd87eef82008-02-24 03:15:25 +0000107
108public:
109 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
110 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
111 }
112
113 size_t size() { return UniqueDirs.size(); }
114};
115
116class FileManager::UniqueFileContainer {
117 /// UniqueFiles - Cache from ID's to existing directories/files.
118 ///
119 std::set<FileEntry> UniqueFiles;
120
121public:
122 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
123 return
124 const_cast<FileEntry&>(
125 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek5d7e2e12009-02-12 03:17:57 +0000126 StatBuf.st_ino,
127 StatBuf.st_mode)).first);
Ted Kremenekd87eef82008-02-24 03:15:25 +0000128 }
129
130 size_t size() { return UniqueFiles.size(); }
131};
132
133#endif
134
Ted Kremenek5c04bd82009-01-28 00:27:31 +0000135//===----------------------------------------------------------------------===//
136// Common logic.
137//===----------------------------------------------------------------------===//
Ted Kremenekd87eef82008-02-24 03:15:25 +0000138
Ted Kremenek5d7e2e12009-02-12 03:17:57 +0000139FileManager::FileManager()
Ted Kremenekc8b740e2009-02-12 00:39:05 +0000140 : UniqueDirs(*new UniqueDirContainer),
141 UniqueFiles(*new UniqueFileContainer),
Ted Kremenek5d7e2e12009-02-12 03:17:57 +0000142 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenekd87eef82008-02-24 03:15:25 +0000143 NumDirLookups = NumFileLookups = 0;
144 NumDirCacheMisses = NumFileCacheMisses = 0;
145}
146
147FileManager::~FileManager() {
148 delete &UniqueDirs;
149 delete &UniqueFiles;
150}
151
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000152void FileManager::addStatCache(StatSysCallCache *statCache, bool AtBeginning) {
153 assert(statCache && "No stat cache provided?");
154 if (AtBeginning || StatCache.get() == 0) {
155 statCache->setNextStatCache(StatCache.take());
156 StatCache.reset(statCache);
157 return;
158 }
159
160 StatSysCallCache *LastCache = StatCache.get();
161 while (LastCache->getNextStatCache())
162 LastCache = LastCache->getNextStatCache();
163
164 LastCache->setNextStatCache(statCache);
165}
166
167void FileManager::removeStatCache(StatSysCallCache *statCache) {
168 if (!statCache)
169 return;
170
171 if (StatCache.get() == statCache) {
172 // This is the first stat cache.
173 StatCache.reset(StatCache->takeNextStatCache());
174 return;
175 }
176
177 // Find the stat cache in the list.
178 StatSysCallCache *PrevCache = StatCache.get();
179 while (PrevCache && PrevCache->getNextStatCache() != statCache)
180 PrevCache = PrevCache->getNextStatCache();
181 if (PrevCache)
182 PrevCache->setNextStatCache(statCache->getNextStatCache());
183 else
184 assert(false && "Stat cache not found for removal");
185}
186
Chris Lattner22eb9722006-06-18 05:43:12 +0000187/// getDirectory - Lookup, cache, and verify the specified directory. This
188/// returns null if the directory doesn't exist.
Mike Stump11289f42009-09-09 15:08:12 +0000189///
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000190const DirectoryEntry *FileManager::getDirectory(const char *NameStart,
191 const char *NameEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000192 ++NumDirLookups;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000193 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000194 DirEntries.GetOrCreateValue(NameStart, NameEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000195
Chris Lattner22eb9722006-06-18 05:43:12 +0000196 // See if there is already an entry in the map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000197 if (NamedDirEnt.getValue())
Ted Kremenek8d71e252008-01-11 20:42:05 +0000198 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000199 ? 0 : NamedDirEnt.getValue();
Mike Stump11289f42009-09-09 15:08:12 +0000200
Chris Lattner22eb9722006-06-18 05:43:12 +0000201 ++NumDirCacheMisses;
Mike Stump11289f42009-09-09 15:08:12 +0000202
Chris Lattneraf653752006-10-30 03:06:54 +0000203 // By default, initialize it to invalid.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000204 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump11289f42009-09-09 15:08:12 +0000205
Chris Lattner43fd42e2006-10-30 03:40:58 +0000206 // Get the null-terminated directory name as stored as the key of the
207 // DirEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000208 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump11289f42009-09-09 15:08:12 +0000209
Chris Lattneraf653752006-10-30 03:06:54 +0000210 // Check to see if the directory exists.
Chris Lattner22eb9722006-06-18 05:43:12 +0000211 struct stat StatBuf;
Ted Kremenekc8b740e2009-02-12 00:39:05 +0000212 if (stat_cached(InterndDirName, &StatBuf) || // Error stat'ing.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000213 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
Chris Lattner22eb9722006-06-18 05:43:12 +0000214 return 0;
Ted Kremenekd87eef82008-02-24 03:15:25 +0000215
Chris Lattner22eb9722006-06-18 05:43:12 +0000216 // It exists. See if we have already opened a directory with the same inode.
Mike Stump11289f42009-09-09 15:08:12 +0000217 // This occurs when one dir is symlinked to another, for example.
Ted Kremenekd87eef82008-02-24 03:15:25 +0000218 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump11289f42009-09-09 15:08:12 +0000219
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000220 NamedDirEnt.setValue(&UDE);
221 if (UDE.getName()) // Already have an entry with this inode, return it.
222 return &UDE;
Mike Stump11289f42009-09-09 15:08:12 +0000223
Chris Lattnera85cbe22006-10-30 03:11:40 +0000224 // Otherwise, we don't have this directory yet, add it. We use the string
225 // key from the DirEntries map as the string.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000226 UDE.Name = InterndDirName;
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000227 return &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000228}
229
Ted Kremenek8d71e252008-01-11 20:42:05 +0000230/// NON_EXISTENT_FILE - A special value distinct from null that is used to
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000231/// represent a filename that doesn't exist on the disk.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000232#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000233
Chris Lattner22eb9722006-06-18 05:43:12 +0000234/// getFile - Lookup, cache, and verify the specified file. This returns null
235/// if the file doesn't exist.
Mike Stump11289f42009-09-09 15:08:12 +0000236///
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000237const FileEntry *FileManager::getFile(const char *NameStart,
238 const char *NameEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000239 ++NumFileLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000240
Chris Lattner22eb9722006-06-18 05:43:12 +0000241 // See if there is already an entry in the map.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000242 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000243 FileEntries.GetOrCreateValue(NameStart, NameEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000244
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000245 // See if there is already an entry in the map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000246 if (NamedFileEnt.getValue())
Ted Kremenek8d71e252008-01-11 20:42:05 +0000247 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000248 ? 0 : NamedFileEnt.getValue();
Mike Stump11289f42009-09-09 15:08:12 +0000249
Chris Lattner22eb9722006-06-18 05:43:12 +0000250 ++NumFileCacheMisses;
251
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000252 // By default, initialize it to invalid.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000253 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Chris Lattner22eb9722006-06-18 05:43:12 +0000254
Chris Lattner9c59bda2006-10-30 04:34:28 +0000255 // Figure out what directory it is in. If the string contains a / in it,
256 // strip off everything after it.
Chris Lattner22eb9722006-06-18 05:43:12 +0000257 // FIXME: this logic should be in sys::Path.
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000258 const char *SlashPos = NameEnd-1;
Ted Kremenekd87eef82008-02-24 03:15:25 +0000259 while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0]))
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000260 --SlashPos;
Chris Lattner91f5ff62009-08-12 17:50:39 +0000261 // Ignore duplicate //'s.
262 while (SlashPos > NameStart && IS_DIR_SEPARATOR_CHAR(SlashPos[-1]))
263 --SlashPos;
Mike Stump11289f42009-09-09 15:08:12 +0000264
Chris Lattner9c59bda2006-10-30 04:34:28 +0000265 const DirectoryEntry *DirInfo;
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000266 if (SlashPos < NameStart) {
267 // Use the current directory if file has no path component.
Chris Lattner9c59bda2006-10-30 04:34:28 +0000268 const char *Name = ".";
269 DirInfo = getDirectory(Name, Name+1);
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000270 } else if (SlashPos == NameEnd-1)
Chris Lattner22eb9722006-06-18 05:43:12 +0000271 return 0; // If filename ends with a /, it's a directory.
272 else
Chris Lattner9c59bda2006-10-30 04:34:28 +0000273 DirInfo = getDirectory(NameStart, SlashPos);
Mike Stump11289f42009-09-09 15:08:12 +0000274
Chris Lattner22eb9722006-06-18 05:43:12 +0000275 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
276 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000277
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000278 // Get the null-terminated file name as stored as the key of the
279 // FileEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000280 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump11289f42009-09-09 15:08:12 +0000281
Chris Lattner22eb9722006-06-18 05:43:12 +0000282 // FIXME: Use the directory info to prune this, before doing the stat syscall.
283 // FIXME: This will reduce the # syscalls.
Mike Stump11289f42009-09-09 15:08:12 +0000284
Chris Lattner22eb9722006-06-18 05:43:12 +0000285 // Nope, there isn't. Check to see if the file exists.
286 struct stat StatBuf;
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000287 //llvm::errs() << "STATING: " << Filename;
Ted Kremenekc8b740e2009-02-12 00:39:05 +0000288 if (stat_cached(InterndFileName, &StatBuf) || // Error stat'ing.
289 S_ISDIR(StatBuf.st_mode)) { // A directory?
Chris Lattner81500bc2006-07-19 03:40:07 +0000290 // If this file doesn't exist, we leave a null in FileEntries for this path.
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000291 //llvm::errs() << ": Not existing\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000292 return 0;
Chris Lattner81500bc2006-07-19 03:40:07 +0000293 }
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000294 //llvm::errs() << ": exists\n";
Mike Stump11289f42009-09-09 15:08:12 +0000295
Ted Kremenekf4c38c92007-12-18 22:29:39 +0000296 // It exists. See if we have already opened a file with the same inode.
Chris Lattner22eb9722006-06-18 05:43:12 +0000297 // This occurs when one dir is symlinked to another, for example.
Ted Kremenekd87eef82008-02-24 03:15:25 +0000298 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Mike Stump11289f42009-09-09 15:08:12 +0000299
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000300 NamedFileEnt.setValue(&UFE);
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000301 if (UFE.getName()) // Already have an entry with this inode, return it.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000302 return &UFE;
Chris Lattner269c2322006-06-25 06:23:00 +0000303
Chris Lattner22eb9722006-06-18 05:43:12 +0000304 // Otherwise, we don't have this directory yet, add it.
Chris Lattner8b1e8482006-10-30 02:45:16 +0000305 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
306 // key.
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000307 UFE.Name = InterndFileName;
308 UFE.Size = StatBuf.st_size;
309 UFE.ModTime = StatBuf.st_mtime;
310 UFE.Dir = DirInfo;
311 UFE.UID = NextFileUID++;
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000312 return &UFE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000313}
314
315void FileManager::PrintStats() const {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000316 llvm::errs() << "\n*** File Manager Stats:\n";
317 llvm::errs() << UniqueFiles.size() << " files found, "
318 << UniqueDirs.size() << " dirs found.\n";
319 llvm::errs() << NumDirLookups << " dir lookups, "
320 << NumDirCacheMisses << " dir cache misses.\n";
321 llvm::errs() << NumFileLookups << " file lookups, "
322 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump11289f42009-09-09 15:08:12 +0000323
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000324 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Chris Lattner22eb9722006-06-18 05:43:12 +0000325}
Douglas Gregorc5046832009-04-27 18:38:38 +0000326
327int MemorizeStatCalls::stat(const char *path, struct stat *buf) {
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000328 int result = StatSysCallCache::stat(path, buf);
329
Mike Stump11289f42009-09-09 15:08:12 +0000330 if (result != 0) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000331 // Cache failed 'stat' results.
332 struct stat empty;
Chris Lattner01ce06f2009-09-18 04:51:01 +0000333 memset(&empty, 0, sizeof(empty));
Douglas Gregorc5046832009-04-27 18:38:38 +0000334 StatCalls[path] = StatResult(result, empty);
335 }
336 else if (!S_ISDIR(buf->st_mode) || llvm::sys::Path(path).isAbsolute()) {
337 // Cache file 'stat' results and directories with absolutely
338 // paths.
339 StatCalls[path] = StatResult(result, *buf);
340 }
Mike Stump11289f42009-09-09 15:08:12 +0000341
342 return result;
Douglas Gregorc5046832009-04-27 18:38:38 +0000343}