blob: 2465cac56eb2954cf4378aef72971007d21c3944 [file] [log] [blame]
Chris Lattner10e286a2010-11-23 19:19:34 +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"
Chris Lattner10e286a2010-11-23 19:19:34 +000021#include "clang/Basic/FileSystemStatCache.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));
Chris Lattnerf3e8a992010-11-23 20:30:42 +000072 return UniqueDirs.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +000073 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Chris Lattnerf3e8a992010-11-23 20:30:42 +000075 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +000076};
77
78class FileManager::UniqueFileContainer {
79 /// UniqueFiles - Cache from full path to existing directories/files.
80 ///
Ted Kremenek75368892009-01-28 01:01:07 +000081 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000082
83public:
84 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
85 std::string FullPath(GetFullPath(Name));
Chris Lattnerc070da42010-08-23 23:50:42 +000086
87 // LowercaseString because Windows filesystem is case insensitive.
88 FullPath = llvm::LowercaseString(FullPath);
Chris Lattnerf3e8a992010-11-23 20:30:42 +000089 return UniqueFiles.GetOrCreateValue(FullPath).getValue();
Ted Kremenek6bb816a2008-02-24 03:15:25 +000090 }
91
Chris Lattnerf3e8a992010-11-23 20:30:42 +000092 size_t size() const { return UniqueFiles.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +000093};
94
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000095//===----------------------------------------------------------------------===//
96// Unix-like Systems.
97//===----------------------------------------------------------------------===//
98
Ted Kremenek6bb816a2008-02-24 03:15:25 +000099#else
100
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000101#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000102
103class FileManager::UniqueDirContainer {
104 /// UniqueDirs - Cache from ID's to existing directories/files.
105 ///
Mike Stump1eb44332009-09-09 15:08:12 +0000106 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenek6bb816a2008-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
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000113 size_t size() const { return UniqueDirs.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000114};
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 Kremenek96438f32009-02-12 03:17:57 +0000126 StatBuf.st_ino,
127 StatBuf.st_mode)).first);
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000128 }
129
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000130 size_t size() const { return UniqueFiles.size(); }
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000131};
132
133#endif
134
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000135//===----------------------------------------------------------------------===//
136// Common logic.
137//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000138
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000139FileManager::FileManager(const FileSystemOptions &FSO)
140 : FileSystemOpts(FSO),
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000141 UniqueDirs(*new UniqueDirContainer()),
142 UniqueFiles(*new UniqueFileContainer()),
Ted Kremenek96438f32009-02-12 03:17:57 +0000143 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000144 NumDirLookups = NumFileLookups = 0;
145 NumDirCacheMisses = NumFileCacheMisses = 0;
146}
147
148FileManager::~FileManager() {
149 delete &UniqueDirs;
150 delete &UniqueFiles;
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000151 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
152 delete VirtualFileEntries[i];
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000153}
154
Chris Lattner10e286a2010-11-23 19:19:34 +0000155void FileManager::addStatCache(FileSystemStatCache *statCache,
156 bool AtBeginning) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000157 assert(statCache && "No stat cache provided?");
158 if (AtBeginning || StatCache.get() == 0) {
159 statCache->setNextStatCache(StatCache.take());
160 StatCache.reset(statCache);
161 return;
162 }
163
Chris Lattner10e286a2010-11-23 19:19:34 +0000164 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000165 while (LastCache->getNextStatCache())
166 LastCache = LastCache->getNextStatCache();
167
168 LastCache->setNextStatCache(statCache);
169}
170
Chris Lattner10e286a2010-11-23 19:19:34 +0000171void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregor52e71082009-10-16 18:18:30 +0000172 if (!statCache)
173 return;
174
175 if (StatCache.get() == statCache) {
176 // This is the first stat cache.
177 StatCache.reset(StatCache->takeNextStatCache());
178 return;
179 }
180
181 // Find the stat cache in the list.
Chris Lattner10e286a2010-11-23 19:19:34 +0000182 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregor52e71082009-10-16 18:18:30 +0000183 while (PrevCache && PrevCache->getNextStatCache() != statCache)
184 PrevCache = PrevCache->getNextStatCache();
185 if (PrevCache)
186 PrevCache->setNextStatCache(statCache->getNextStatCache());
187 else
188 assert(false && "Stat cache not found for removal");
189}
190
Douglas Gregor057e5672009-12-02 18:12:28 +0000191/// \brief Retrieve the directory that the given file name resides in.
192static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000193 llvm::StringRef Filename) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000194 // Figure out what directory it is in. If the string contains a / in it,
195 // strip off everything after it.
196 // FIXME: this logic should be in sys::Path.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000197 size_t SlashPos = Filename.size();
198 while (SlashPos != 0 && !IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
199 --SlashPos;
200
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000201 // Use the current directory if file has no path component.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000202 if (SlashPos == 0)
Chris Lattner39b49bc2010-11-23 08:35:12 +0000203 return FileMgr.getDirectory(".");
Douglas Gregor057e5672009-12-02 18:12:28 +0000204
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000205 if (SlashPos == Filename.size()-1)
Douglas Gregor057e5672009-12-02 18:12:28 +0000206 return 0; // If filename ends with a /, it's a directory.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000207
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000208 // Ignore repeated //'s.
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000209 while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000210 --SlashPos;
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000211
Chris Lattner39b49bc2010-11-23 08:35:12 +0000212 return FileMgr.getDirectory(Filename.substr(0, SlashPos));
Douglas Gregor057e5672009-12-02 18:12:28 +0000213}
214
Reid Spencer5f016e22007-07-11 17:01:13 +0000215/// getDirectory - Lookup, cache, and verify the specified directory. This
216/// returns null if the directory doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000217///
Chris Lattner39b49bc2010-11-23 08:35:12 +0000218const DirectoryEntry *FileManager::getDirectory(llvm::StringRef Filename) {
John Thompson9a6ac542009-12-18 14:18:21 +0000219 // stat doesn't like trailing separators (at least on Windows).
Benjamin Krameraa8b2d92010-11-21 11:32:22 +0000220 if (Filename.size() > 1 && IS_DIR_SEPARATOR_CHAR(Filename.back()))
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000221 Filename = Filename.substr(0, Filename.size()-1);
John Thompson9a6ac542009-12-18 14:18:21 +0000222
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 ++NumDirLookups;
224 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000225 DirEntries.GetOrCreateValue(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 // See if there is already an entry in the map.
228 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000229 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 ? 0 : NamedDirEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 ++NumDirCacheMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000235 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 // Get the null-terminated directory name as stored as the key of the
238 // DirEntries map.
239 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 // Check to see if the directory exists.
242 struct stat StatBuf;
Chris Lattner10e286a2010-11-23 19:19:34 +0000243 if (getStatValue(InterndDirName, StatBuf) || // Error stat'ing.
244 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 return 0;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 // It exists. See if we have already opened a directory with the same inode.
Mike Stump1eb44332009-09-09 15:08:12 +0000248 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000249 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 NamedDirEnt.setValue(&UDE);
252 if (UDE.getName()) // Already have an entry with this inode, return it.
253 return &UDE;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 // Otherwise, we don't have this directory yet, add it. We use the string
256 // key from the DirEntries map as the string.
257 UDE.Name = InterndDirName;
258 return &UDE;
259}
260
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000261/// NON_EXISTENT_FILE - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +0000262/// represent a filename that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000263#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +0000264
265/// getFile - Lookup, cache, and verify the specified file. This returns null
266/// if the file doesn't exist.
Mike Stump1eb44332009-09-09 15:08:12 +0000267///
Chris Lattner39b49bc2010-11-23 08:35:12 +0000268const FileEntry *FileManager::getFile(llvm::StringRef Filename) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 ++NumFileLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 // See if there is already an entry in the map.
272 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000273 FileEntries.GetOrCreateValue(Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000274
275 // See if there is already an entry in the map.
276 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000277 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 ? 0 : NamedFileEnt.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 ++NumFileCacheMisses;
281
282 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000283 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000284
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 // Get the null-terminated file name as stored as the key of the
287 // FileEntries map.
288 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Chris Lattnerf3e8a992010-11-23 20:30:42 +0000290
291 // Look up the directory for the file. When looking up something like
292 // sys/foo.h we'll discover all of the search directories that have a 'sys'
293 // subdirectory. This will let us avoid having to waste time on known-to-fail
294 // searches when we go to find sys/bar.h, because all the search directories
295 // without a 'sys' subdir will get a cached failure result.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000296 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000297 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
298 return 0;
299
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 // FIXME: Use the directory info to prune this, before doing the stat syscall.
301 // FIXME: This will reduce the # syscalls.
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 // Nope, there isn't. Check to see if the file exists.
304 struct stat StatBuf;
Chris Lattner10e286a2010-11-23 19:19:34 +0000305 if (getStatValue(InterndFileName, StatBuf) || // Error stat'ing.
306 S_ISDIR(StatBuf.st_mode)) { // A directory?
Chris Lattner11aa4b02010-11-23 19:56:39 +0000307 // If this file doesn't exist, we leave NON_EXISTENT_FILE in FileEntries for
308 // this path so subsequent queries get the negative result.
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 return 0;
310 }
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Ted Kremenekbca6d122007-12-18 22:29:39 +0000312 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000314 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 NamedFileEnt.setValue(&UFE);
317 if (UFE.getName()) // Already have an entry with this inode, return it.
318 return &UFE;
319
320 // Otherwise, we don't have this directory yet, add it.
321 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
322 // key.
323 UFE.Name = InterndFileName;
324 UFE.Size = StatBuf.st_size;
325 UFE.ModTime = StatBuf.st_mtime;
326 UFE.Dir = DirInfo;
327 UFE.UID = NextFileUID++;
328 return &UFE;
329}
330
Douglas Gregor057e5672009-12-02 18:12:28 +0000331const FileEntry *
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000332FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000333 time_t ModificationTime) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000334 ++NumFileLookups;
335
336 // See if there is already an entry in the map.
337 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattnerf69a1f32010-11-21 09:50:16 +0000338 FileEntries.GetOrCreateValue(Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000339
340 // See if there is already an entry in the map.
341 if (NamedFileEnt.getValue())
342 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
343 ? 0 : NamedFileEnt.getValue();
344
345 ++NumFileCacheMisses;
346
347 // By default, initialize it to invalid.
348 NamedFileEnt.setValue(NON_EXISTENT_FILE);
349
Chris Lattner39b49bc2010-11-23 08:35:12 +0000350 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor057e5672009-12-02 18:12:28 +0000351 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
352 return 0;
353
354 FileEntry *UFE = new FileEntry();
355 VirtualFileEntries.push_back(UFE);
356 NamedFileEnt.setValue(UFE);
357
358 UFE->Name = NamedFileEnt.getKeyData();
359 UFE->Size = Size;
360 UFE->ModTime = ModificationTime;
361 UFE->Dir = DirInfo;
362 UFE->UID = NextFileUID++;
Douglas Gregor3e15e0a2010-07-26 23:54:23 +0000363
364 // If this virtual file resolves to a file, also map that file to the
365 // newly-created file entry.
366 const char *InterndFileName = NamedFileEnt.getKeyData();
367 struct stat StatBuf;
Chris Lattner10e286a2010-11-23 19:19:34 +0000368 if (!getStatValue(InterndFileName, StatBuf) &&
Douglas Gregor3e15e0a2010-07-26 23:54:23 +0000369 !S_ISDIR(StatBuf.st_mode)) {
370 llvm::sys::Path FilePath(InterndFileName);
371 FilePath.makeAbsolute();
372 FileEntries[FilePath.str()] = UFE;
373 }
374
Douglas Gregor057e5672009-12-02 18:12:28 +0000375 return UFE;
376}
377
Chris Lattner67452f52010-11-23 04:45:28 +0000378void FileManager::FixupRelativePath(llvm::sys::Path &path,
379 const FileSystemOptions &FSOpts) {
380 if (FSOpts.WorkingDir.empty() || path.isAbsolute()) return;
381
382 llvm::sys::Path NewPath(FSOpts.WorkingDir);
383 NewPath.appendComponent(path.str());
384 path = NewPath;
385}
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000386
Chris Lattner67452f52010-11-23 04:45:28 +0000387llvm::MemoryBuffer *FileManager::
Chris Lattner75dfb652010-11-23 09:19:42 +0000388getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) {
389 llvm::StringRef Filename = Entry->getName();
Chris Lattner67452f52010-11-23 04:45:28 +0000390 if (FileSystemOpts.WorkingDir.empty())
Chris Lattnerf8f61292010-11-23 19:38:22 +0000391 return llvm::MemoryBuffer::getFile(Filename, ErrorStr, Entry->getSize());
Chris Lattner67452f52010-11-23 04:45:28 +0000392
393 llvm::sys::Path FilePath(Filename);
394 FixupRelativePath(FilePath, FileSystemOpts);
Chris Lattnerf8f61292010-11-23 19:38:22 +0000395 return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr,
396 Entry->getSize());
Chris Lattner75dfb652010-11-23 09:19:42 +0000397}
398
399llvm::MemoryBuffer *FileManager::
400getBufferForFile(llvm::StringRef Filename, std::string *ErrorStr) {
401 if (FileSystemOpts.WorkingDir.empty())
402 return llvm::MemoryBuffer::getFile(Filename, ErrorStr);
403
404 llvm::sys::Path FilePath(Filename);
405 FixupRelativePath(FilePath, FileSystemOpts);
406 return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000407}
408
Chris Lattner10e286a2010-11-23 19:19:34 +0000409/// getStatValue - Get the 'stat' information for the specified path, using the
410/// cache to accellerate it if possible. This returns true if the path does not
411/// exist or false if it exists.
412bool FileManager::getStatValue(const char *Path, struct stat &StatBuf) {
Chris Lattner10e286a2010-11-23 19:19:34 +0000413 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
414 // absolute!
Chris Lattner11aa4b02010-11-23 19:56:39 +0000415 if (FileSystemOpts.WorkingDir.empty())
416 return FileSystemStatCache::get(Path, StatBuf, StatCache.get());
Chris Lattner10e286a2010-11-23 19:19:34 +0000417
418 llvm::sys::Path FilePath(Path);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000419 FixupRelativePath(FilePath, FileSystemOpts);
Chris Lattner11aa4b02010-11-23 19:56:39 +0000420
421 return FileSystemStatCache::get(FilePath.c_str(), StatBuf, StatCache.get());
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000422}
423
Chris Lattner10e286a2010-11-23 19:19:34 +0000424
425
Reid Spencer5f016e22007-07-11 17:01:13 +0000426void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000427 llvm::errs() << "\n*** File Manager Stats:\n";
428 llvm::errs() << UniqueFiles.size() << " files found, "
429 << UniqueDirs.size() << " dirs found.\n";
430 llvm::errs() << NumDirLookups << " dir lookups, "
431 << NumDirCacheMisses << " dir cache misses.\n";
432 llvm::errs() << NumFileLookups << " file lookups, "
433 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000435 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000436}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000437