blob: 36b53850bccf9620a5f00969f9514f86d6009283 [file] [log] [blame]
Ted Kremenek8fbc88e2007-12-04 22:42:20 +00001///===--- FileManager.cpp - File System Probing and Caching ----------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +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"
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +000021#include "clang/Basic/FileSystemOptions.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/ADT/SmallString.h"
Chris Lattnerc070da42010-08-23 23:50:42 +000023#include "llvm/ADT/StringExtras.h"
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000025#include "llvm/Support/raw_ostream.h"
Douglas Gregor4fed3f42009-04-27 18:38:38 +000026#include "llvm/System/Path.h"
Ted Kremenek6bb816a2008-02-24 03:15:25 +000027#include "llvm/Config/config.h"
Benjamin Kramer458fb102009-09-05 09:49:39 +000028#include <map>
29#include <set>
30#include <string>
Reid Spencer5f016e22007-07-11 17:01:13 +000031using namespace clang;
32
33// FIXME: Enhance libsystem to support inode and other fields.
34#include <sys/stat.h>
35
Chris Lattnera8c11c62007-09-03 18:37:14 +000036#if defined(_MSC_VER)
Chris Lattner3102c832009-02-12 01:37:35 +000037#define S_ISDIR(s) (_S_IFDIR & s)
Chris Lattnera8c11c62007-09-03 18:37:14 +000038#endif
Reid Spencer5f016e22007-07-11 17:01:13 +000039
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000040/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +000041/// represent a dir name that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000042#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +000043
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000044//===----------------------------------------------------------------------===//
45// Windows.
46//===----------------------------------------------------------------------===//
47
Ted Kremenek6bb816a2008-02-24 03:15:25 +000048#ifdef LLVM_ON_WIN32
49
Benjamin Krameraa8b2d92010-11-21 11:32:22 +000050#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
Ted Kremenek6bb816a2008-02-24 03:15:25 +000051
52namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000053 static std::string GetFullPath(const char *relPath) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +000054 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
55 assert(absPathStrPtr && "_fullpath() returned NULL!");
56
57 std::string absPath(absPathStrPtr);
58
59 free(absPathStrPtr);
60 return absPath;
61 }
62}
63
64class FileManager::UniqueDirContainer {
65 /// UniqueDirs - Cache from full path to existing directories/files.
66 ///
Mike Stump1eb44332009-09-09 15:08:12 +000067 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000068
69public:
70 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
71 std::string FullPath(GetFullPath(Name));
72 return UniqueDirs.GetOrCreateValue(
73 FullPath.c_str(),
74 FullPath.c_str() + FullPath.size()
75 ).getValue();
76 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Ted Kremenek6bb816a2008-02-24 03:15:25 +000078 size_t size() { return UniqueDirs.size(); }
79};
80
81class FileManager::UniqueFileContainer {
82 /// UniqueFiles - Cache from full path to existing directories/files.
83 ///
Ted Kremenek75368892009-01-28 01:01:07 +000084 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000085
86public:
87 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
88 std::string FullPath(GetFullPath(Name));
Chris Lattnerc070da42010-08-23 23:50:42 +000089
90 // LowercaseString because Windows filesystem is case insensitive.
91 FullPath = llvm::LowercaseString(FullPath);
Ted Kremenek6bb816a2008-02-24 03:15:25 +000092 return UniqueFiles.GetOrCreateValue(
93 FullPath.c_str(),
94 FullPath.c_str() + FullPath.size()
95 ).getValue();
96 }
97
98 size_t size() { return UniqueFiles.size(); }
99};
100
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000101//===----------------------------------------------------------------------===//
102// Unix-like Systems.
103//===----------------------------------------------------------------------===//
104
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000105#else
106
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000107#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000108
109class FileManager::UniqueDirContainer {
110 /// UniqueDirs - Cache from ID's to existing directories/files.
111 ///
Mike Stump1eb44332009-09-09 15:08:12 +0000112 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000113
114public:
115 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
116 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
117 }
118
119 size_t size() { return UniqueDirs.size(); }
120};
121
122class FileManager::UniqueFileContainer {
123 /// UniqueFiles - Cache from ID's to existing directories/files.
124 ///
125 std::set<FileEntry> UniqueFiles;
126
127public:
128 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
129 return
130 const_cast<FileEntry&>(
131 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek96438f32009-02-12 03:17:57 +0000132 StatBuf.st_ino,
133 StatBuf.st_mode)).first);
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000134 }
135
136 size_t size() { return UniqueFiles.size(); }
137};
138
139#endif
140
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000141//===----------------------------------------------------------------------===//
142// Common logic.
143//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000144
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000145FileManager::FileManager(const FileSystemOptions &FSO)
146 : FileSystemOpts(FSO),
147 UniqueDirs(*new UniqueDirContainer),
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000148 UniqueFiles(*new UniqueFileContainer),
Ted Kremenek96438f32009-02-12 03:17:57 +0000149 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000150 NumDirLookups = NumFileLookups = 0;
151 NumDirCacheMisses = NumFileCacheMisses = 0;
152}
153
154FileManager::~FileManager() {
155 delete &UniqueDirs;
156 delete &UniqueFiles;
Douglas Gregor057e5672009-12-02 18:12:28 +0000157 for (llvm::SmallVectorImpl<FileEntry *>::iterator
158 V = VirtualFileEntries.begin(),
159 VEnd = VirtualFileEntries.end();
160 V != VEnd;
161 ++V)
162 delete *V;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000163}
164
Douglas Gregor52e71082009-10-16 18:18:30 +0000165void FileManager::addStatCache(StatSysCallCache *statCache, bool AtBeginning) {
166 assert(statCache && "No stat cache provided?");
167 if (AtBeginning || StatCache.get() == 0) {
168 statCache->setNextStatCache(StatCache.take());
169 StatCache.reset(statCache);
170 return;
171 }
172
173 StatSysCallCache *LastCache = StatCache.get();
174 while (LastCache->getNextStatCache())
175 LastCache = LastCache->getNextStatCache();
176
177 LastCache->setNextStatCache(statCache);
178}
179
180void FileManager::removeStatCache(StatSysCallCache *statCache) {
181 if (!statCache)
182 return;
183
184 if (StatCache.get() == statCache) {
185 // This is the first stat cache.
186 StatCache.reset(StatCache->takeNextStatCache());
187 return;
188 }
189
190 // Find the stat cache in the list.
191 StatSysCallCache *PrevCache = StatCache.get();
192 while (PrevCache && PrevCache->getNextStatCache() != statCache)
193 PrevCache = PrevCache->getNextStatCache();
194 if (PrevCache)
195 PrevCache->setNextStatCache(statCache->getNextStatCache());
196 else
197 assert(false && "Stat cache not found for removal");
198}
199
Douglas Gregor057e5672009-12-02 18:12:28 +0000200/// \brief Retrieve the directory that the given file name resides in.
201static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000202 llvm::StringRef Filename) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000203 // Figure out what directory it is in. If the string contains a / in it,
204 // strip off everything after it.
205 // FIXME: this logic should be in sys::Path.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000206 size_t SlashPos = Filename.size();
207 while (SlashPos != 0 && !IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
208 --SlashPos;
209
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000210 // Use the current directory if file has no path component.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000211 if (SlashPos == 0)
Chris Lattner39b49bc2010-11-23 08:35:12 +0000212 return FileMgr.getDirectory(".");
Douglas Gregor057e5672009-12-02 18:12:28 +0000213
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000214 if (SlashPos == Filename.size()-1)
Douglas Gregor057e5672009-12-02 18:12:28 +0000215 return 0; // If filename ends with a /, it's a directory.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000216
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000217 // Ignore repeated //'s.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000218 while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000219 --SlashPos;
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000220
Chris Lattner39b49bc2010-11-23 08:35:12 +0000221 return FileMgr.getDirectory(Filename.substr(0, SlashPos));
Douglas Gregor057e5672009-12-02 18:12:28 +0000222}
223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224/// getDirectory - Lookup, cache, and verify the specified directory. This
225/// returns null if the directory doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000226///
Chris Lattner39b49bc2010-11-23 08:35:12 +0000227const DirectoryEntry *FileManager::getDirectory(llvm::StringRef Filename) {
John Thompson9a6ac542009-12-18 14:18:21 +0000228 // stat doesn't like trailing separators (at least on Windows).
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000229 if (Filename.size() > 1 && IS_DIR_SEPARATOR_CHAR(Filename.back()))
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000230 Filename = Filename.substr(0, Filename.size()-1);
John Thompson9a6ac542009-12-18 14:18:21 +0000231
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 ++NumDirLookups;
233 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000234 DirEntries.GetOrCreateValue(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 // See if there is already an entry in the map.
237 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000238 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 ? 0 : NamedDirEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 ++NumDirCacheMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000244 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 // Get the null-terminated directory name as stored as the key of the
247 // DirEntries map.
248 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 // Check to see if the directory exists.
251 struct stat StatBuf;
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000252 if (stat_cached(InterndDirName, &StatBuf) || // Error stat'ing.
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
254 return 0;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000255
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 // It exists. See if we have already opened a directory with the same inode.
Mike Stump1eb44332009-09-09 15:08:12 +0000257 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000258 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 NamedDirEnt.setValue(&UDE);
261 if (UDE.getName()) // Already have an entry with this inode, return it.
262 return &UDE;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 // Otherwise, we don't have this directory yet, add it. We use the string
265 // key from the DirEntries map as the string.
266 UDE.Name = InterndDirName;
267 return &UDE;
268}
269
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000270/// NON_EXISTENT_FILE - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +0000271/// represent a filename that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000272#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +0000273
274/// getFile - Lookup, cache, and verify the specified file. This returns null
275/// if the file doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000276///
Chris Lattner39b49bc2010-11-23 08:35:12 +0000277const FileEntry *FileManager::getFile(llvm::StringRef Filename) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 ++NumFileLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 // See if there is already an entry in the map.
281 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000282 FileEntries.GetOrCreateValue(Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000283
284 // See if there is already an entry in the map.
285 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000286 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 ? 0 : NamedFileEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 ++NumFileCacheMisses;
290
291 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000292 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000293
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // Get the null-terminated file name as stored as the key of the
296 // FileEntries map.
297 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Chris Lattner39b49bc2010-11-23 08:35:12 +0000299 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000300 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
301 return 0;
302
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 // FIXME: Use the directory info to prune this, before doing the stat syscall.
304 // FIXME: This will reduce the # syscalls.
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 // Nope, there isn't. Check to see if the file exists.
307 struct stat StatBuf;
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000308 //llvm::errs() << "STATING: " << Filename;
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000309 if (stat_cached(InterndFileName, &StatBuf) || // Error stat'ing.
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000310 S_ISDIR(StatBuf.st_mode)) { // A directory?
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 // If this file doesn't exist, we leave a null in FileEntries for this path.
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000312 //llvm::errs() << ": Not existing\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 return 0;
314 }
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000315 //llvm::errs() << ": exists\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenekbca6d122007-12-18 22:29:39 +0000317 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000319 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 NamedFileEnt.setValue(&UFE);
322 if (UFE.getName()) // Already have an entry with this inode, return it.
323 return &UFE;
324
325 // Otherwise, we don't have this directory yet, add it.
326 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
327 // key.
328 UFE.Name = InterndFileName;
329 UFE.Size = StatBuf.st_size;
330 UFE.ModTime = StatBuf.st_mtime;
331 UFE.Dir = DirInfo;
332 UFE.UID = NextFileUID++;
333 return &UFE;
334}
335
Douglas Gregor057e5672009-12-02 18:12:28 +0000336const FileEntry *
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000337FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000338 time_t ModificationTime) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000339 ++NumFileLookups;
340
341 // See if there is already an entry in the map.
342 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000343 FileEntries.GetOrCreateValue(Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000344
345 // See if there is already an entry in the map.
346 if (NamedFileEnt.getValue())
347 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
348 ? 0 : NamedFileEnt.getValue();
349
350 ++NumFileCacheMisses;
351
352 // By default, initialize it to invalid.
353 NamedFileEnt.setValue(NON_EXISTENT_FILE);
354
Chris Lattner39b49bc2010-11-23 08:35:12 +0000355 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000356 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
357 return 0;
358
359 FileEntry *UFE = new FileEntry();
360 VirtualFileEntries.push_back(UFE);
361 NamedFileEnt.setValue(UFE);
362
363 UFE->Name = NamedFileEnt.getKeyData();
364 UFE->Size = Size;
365 UFE->ModTime = ModificationTime;
366 UFE->Dir = DirInfo;
367 UFE->UID = NextFileUID++;
Douglas Gregor3e15e0a2010-07-26 23:54:23 +0000368
369 // If this virtual file resolves to a file, also map that file to the
370 // newly-created file entry.
371 const char *InterndFileName = NamedFileEnt.getKeyData();
372 struct stat StatBuf;
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000373 if (!stat_cached(InterndFileName, &StatBuf) &&
Douglas Gregor3e15e0a2010-07-26 23:54:23 +0000374 !S_ISDIR(StatBuf.st_mode)) {
375 llvm::sys::Path FilePath(InterndFileName);
376 FilePath.makeAbsolute();
377 FileEntries[FilePath.str()] = UFE;
378 }
379
Douglas Gregor057e5672009-12-02 18:12:28 +0000380 return UFE;
381}
382
Chris Lattner67452f52010-11-23 04:45:28 +0000383void FileManager::FixupRelativePath(llvm::sys::Path &path,
384 const FileSystemOptions &FSOpts) {
385 if (FSOpts.WorkingDir.empty() || path.isAbsolute()) return;
386
387 llvm::sys::Path NewPath(FSOpts.WorkingDir);
388 NewPath.appendComponent(path.str());
389 path = NewPath;
390}
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000391
Chris Lattner67452f52010-11-23 04:45:28 +0000392
393
394llvm::MemoryBuffer *FileManager::
395getBufferForFile(llvm::StringRef Filename,
Chris Lattner151466a2010-11-23 06:09:11 +0000396 std::string *ErrorStr, int64_t FileSize) {
Chris Lattner67452f52010-11-23 04:45:28 +0000397 if (FileSystemOpts.WorkingDir.empty())
Chris Lattner151466a2010-11-23 06:09:11 +0000398 return llvm::MemoryBuffer::getFile(Filename, ErrorStr, FileSize);
Chris Lattner67452f52010-11-23 04:45:28 +0000399
400 llvm::sys::Path FilePath(Filename);
401 FixupRelativePath(FilePath, FileSystemOpts);
Chris Lattner151466a2010-11-23 06:09:11 +0000402 return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr, FileSize);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000403}
404
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000405int FileManager::stat_cached(const char *path, struct stat *buf) {
Chris Lattnerefc4be22010-11-23 04:33:43 +0000406 if (FileSystemOpts.WorkingDir.empty())
407 return StatCache.get() ? StatCache->stat(path, buf) : stat(path, buf);
408
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000409 llvm::sys::Path FilePath(path);
410 FixupRelativePath(FilePath, FileSystemOpts);
411
412 return StatCache.get() ? StatCache->stat(FilePath.c_str(), buf)
413 : stat(FilePath.c_str(), buf);
414}
415
Reid Spencer5f016e22007-07-11 17:01:13 +0000416void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000417 llvm::errs() << "\n*** File Manager Stats:\n";
418 llvm::errs() << UniqueFiles.size() << " files found, "
419 << UniqueDirs.size() << " dirs found.\n";
420 llvm::errs() << NumDirLookups << " dir lookups, "
421 << NumDirCacheMisses << " dir cache misses.\n";
422 llvm::errs() << NumFileLookups << " file lookups, "
423 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000425 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000426}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000427
428int MemorizeStatCalls::stat(const char *path, struct stat *buf) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000429 int result = StatSysCallCache::stat(path, buf);
430
Daniel Dunbar475ddb42009-12-11 00:27:20 +0000431 // Do not cache failed stats, it is easy to construct common inconsistent
432 // situations if we do, and they are not important for PCH performance (which
433 // currently only needs the stats to construct the initial FileManager
434 // entries).
435 if (result != 0)
436 return result;
437
438 // Cache file 'stat' results and directories with absolutely paths.
439 if (!S_ISDIR(buf->st_mode) || llvm::sys::Path(path).isAbsolute())
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000440 StatCalls[path] = StatResult(result, *buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000441
442 return result;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000443}